fat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /* FAT12/16/32 filesystem, device-parameterized (reads/writes via blk_read/blk_write). */
  2. #include "fat.h"
  3. #include "io.h"
  4. #define SECTOR_SIZE 512
  5. #define ROOT_ENTRY_SIZE 32
  6. static unsigned char sector_buf[SECTOR_SIZE];
  7. static unsigned int rd16(const unsigned char *p)
  8. {
  9. return (unsigned int)p[0] | ((unsigned int)p[1] << 8);
  10. }
  11. static unsigned int rd32(const unsigned char *p)
  12. {
  13. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  14. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  15. }
  16. /* ---- low-level sector access (fs-relative LBA) ---- */
  17. static int fat_read_sector(fat_fs_t *fs, unsigned int lba)
  18. {
  19. if (blk_read(fs->dev, fs->part_lba + lba, sector_buf, 1) != 0)
  20. return -2;
  21. return 0;
  22. }
  23. static int fat_write_sector(fat_fs_t *fs, unsigned int lba)
  24. {
  25. if (blk_write(fs->dev, fs->part_lba + lba, sector_buf, 1) != 0)
  26. return -2;
  27. return 0;
  28. }
  29. static int fat_read_data(fat_fs_t *fs, unsigned int lba, void *buf, unsigned int count)
  30. {
  31. if (blk_read(fs->dev, fs->part_lba + lba, buf, count) != 0)
  32. return -2;
  33. return 0;
  34. }
  35. /* ---- FAT entry access (12/16/32-bit) ---- */
  36. static int fat_is_eoc(fat_fs_t *fs, unsigned int v)
  37. {
  38. if (fs->type == FAT12) return v >= 0xFF8;
  39. if (fs->type == FAT16) return v >= 0xFFF8;
  40. return v >= 0x0FFFFFF8u;
  41. }
  42. static unsigned int fat_eoc(fat_fs_t *fs)
  43. {
  44. if (fs->type == FAT32) return 0x0FFFFFFFu;
  45. if (fs->type == FAT16) return 0xFFFFu;
  46. return 0xFFFu;
  47. }
  48. static unsigned int fat_entry_offs(fat_fs_t *fs, unsigned int cluster,
  49. unsigned int *nbytes)
  50. {
  51. if (fs->type == FAT12) {
  52. *nbytes = 2;
  53. return (cluster * 3) / 2;
  54. }
  55. if (fs->type == FAT16) {
  56. *nbytes = 2;
  57. return cluster * 2;
  58. }
  59. *nbytes = 4;
  60. return cluster * 4;
  61. }
  62. static int fat_entry_get(fat_fs_t *fs, unsigned int cluster, unsigned int *value)
  63. {
  64. unsigned int nbytes, byte_off, sec, within, i;
  65. unsigned char b[4];
  66. byte_off = fat_entry_offs(fs, cluster, &nbytes);
  67. sec = fs->reserved + byte_off / SECTOR_SIZE;
  68. within = byte_off % SECTOR_SIZE;
  69. if (fat_read_sector(fs, sec) != 0)
  70. return -2;
  71. for (i = 0; i < nbytes; i++) {
  72. unsigned int idx = within + i;
  73. if (idx < SECTOR_SIZE) {
  74. b[i] = sector_buf[idx];
  75. } else {
  76. if (idx == SECTOR_SIZE && fat_read_sector(fs, sec + 1) != 0)
  77. return -2;
  78. b[i] = sector_buf[idx - SECTOR_SIZE];
  79. }
  80. }
  81. if (fs->type == FAT12) {
  82. unsigned int w = (unsigned int)b[0] | ((unsigned int)b[1] << 8);
  83. *value = (cluster & 1) ? (w >> 4) : (w & 0xFFF);
  84. } else if (fs->type == FAT16) {
  85. *value = (unsigned int)b[0] | ((unsigned int)b[1] << 8);
  86. } else {
  87. *value = ((unsigned int)b[0] | ((unsigned int)b[1] << 8) |
  88. ((unsigned int)b[2] << 16) | ((unsigned int)b[3] << 24)) & 0x0FFFFFFFu;
  89. }
  90. return 0;
  91. }
  92. static int fat_entry_set(fat_fs_t *fs, unsigned int cluster, unsigned int value)
  93. {
  94. unsigned int nbytes, byte_off, sec, within, i;
  95. unsigned char b[4];
  96. byte_off = fat_entry_offs(fs, cluster, &nbytes);
  97. sec = fs->reserved + byte_off / SECTOR_SIZE;
  98. within = byte_off % SECTOR_SIZE;
  99. /* Read current bytes (may load sec+1 on a straddle). */
  100. if (fat_read_sector(fs, sec) != 0)
  101. return -2;
  102. for (i = 0; i < nbytes; i++) {
  103. unsigned int idx = within + i;
  104. if (idx < SECTOR_SIZE) {
  105. b[i] = sector_buf[idx];
  106. } else {
  107. if (idx == SECTOR_SIZE && fat_read_sector(fs, sec + 1) != 0)
  108. return -2;
  109. b[i] = sector_buf[idx - SECTOR_SIZE];
  110. }
  111. }
  112. if (fs->type == FAT12) {
  113. value &= 0xFFF;
  114. if (cluster & 1) {
  115. b[0] = (unsigned char)((b[0] & 0x0F) | ((value << 4) & 0xF0));
  116. b[1] = (unsigned char)((value >> 4) & 0xFF);
  117. } else {
  118. b[0] = (unsigned char)(value & 0xFF);
  119. b[1] = (unsigned char)((b[1] & 0xF0) | ((value >> 8) & 0x0F));
  120. }
  121. } else if (fs->type == FAT16) {
  122. b[0] = (unsigned char)(value & 0xFF);
  123. b[1] = (unsigned char)((value >> 8) & 0xFF);
  124. } else {
  125. value &= 0x0FFFFFFFu;
  126. b[0] = (unsigned char)(value & 0xFF);
  127. b[1] = (unsigned char)((value >> 8) & 0xFF);
  128. b[2] = (unsigned char)((value >> 16) & 0xFF);
  129. b[3] = (unsigned char)((value >> 24) & 0xFF);
  130. }
  131. /* Write back (may straddle two sectors). */
  132. if (fat_read_sector(fs, sec) != 0)
  133. return -2;
  134. for (i = 0; i < nbytes && within + i < SECTOR_SIZE; i++)
  135. sector_buf[within + i] = b[i];
  136. if (fat_write_sector(fs, sec) != 0)
  137. return -2;
  138. if (within + nbytes > SECTOR_SIZE) {
  139. if (fat_read_sector(fs, sec + 1) != 0)
  140. return -2;
  141. for (; i < nbytes; i++)
  142. sector_buf[within + i - SECTOR_SIZE] = b[i];
  143. if (fat_write_sector(fs, sec + 1) != 0)
  144. return -2;
  145. }
  146. return 0;
  147. }
  148. /* ---- mount ---- */
  149. int fat_mount(blk_dev_t *dev, unsigned int part_lba, fat_fs_t *fs)
  150. {
  151. unsigned char *b = sector_buf;
  152. unsigned int root_entries, total, sec_per_fat16, sec_per_fat32;
  153. unsigned int root_dir_sectors, data_sectors, clusters, spc, media;
  154. unsigned int fsinfo_sec;
  155. if (!dev || !fs)
  156. return -1;
  157. fs->dev = dev;
  158. fs->part_lba = part_lba;
  159. fs->mounted = 0;
  160. if (blk_read(dev, part_lba, sector_buf, 1) != 0)
  161. return -2;
  162. if (sector_buf[510] != 0x55 || sector_buf[511] != 0xAA)
  163. return -1;
  164. fs->bytes_per_sector = rd16(b + 0x0B);
  165. if (fs->bytes_per_sector != 512)
  166. return -1;
  167. spc = b[0x0D];
  168. if (spc == 0 || (spc & (spc - 1)) != 0 || spc > 128)
  169. return -1;
  170. fs->sectors_per_cluster = spc;
  171. fs->reserved = rd16(b + 0x0E);
  172. if (fs->reserved < 1)
  173. return -1;
  174. fs->num_fats = b[0x10];
  175. if (fs->num_fats < 1 || fs->num_fats > 2)
  176. return -1;
  177. root_entries = rd16(b + 0x11);
  178. total = rd16(b + 0x13);
  179. if (total == 0)
  180. total = rd32(b + 0x20);
  181. if (total == 0)
  182. return -1;
  183. sec_per_fat16 = rd16(b + 0x16);
  184. sec_per_fat32 = rd32(b + 0x24);
  185. fs->sec_per_fat = sec_per_fat16 ? sec_per_fat16 : sec_per_fat32;
  186. if (fs->sec_per_fat == 0)
  187. return -1;
  188. root_dir_sectors = (root_entries * 32 + SECTOR_SIZE - 1) / SECTOR_SIZE;
  189. if (total < fs->reserved + fs->num_fats * fs->sec_per_fat + root_dir_sectors)
  190. return -1;
  191. data_sectors = total - fs->reserved - fs->num_fats * fs->sec_per_fat - root_dir_sectors;
  192. if (data_sectors < spc)
  193. return -1;
  194. clusters = data_sectors / spc;
  195. if (clusters < 4085)
  196. fs->type = FAT12;
  197. else if (clusters < 65525)
  198. fs->type = FAT16;
  199. else
  200. fs->type = FAT32;
  201. fs->root_entries = (fs->type == FAT32) ? 0 : root_entries;
  202. fs->root_dir_sectors = (fs->type == FAT32) ? 0 : root_dir_sectors;
  203. fs->root_cluster = (fs->type == FAT32) ? rd32(b + 0x2C) : 0;
  204. if (fs->type == FAT32 && fs->root_cluster < 2)
  205. return -1;
  206. fs->root_start = fs->reserved + fs->num_fats * fs->sec_per_fat;
  207. fs->data_start = fs->root_start + fs->root_dir_sectors;
  208. fs->total_sectors = total;
  209. fs->max_cluster = 2 + (total - fs->data_start) / spc;
  210. /* Volume label (11 bytes): 0x2B for FAT12/16, 0x47 for FAT32. */
  211. {
  212. unsigned int label_off = (fs->type == FAT32) ? 0x47 : 0x2B;
  213. unsigned int k;
  214. for (k = 0; k < 11; k++)
  215. fs->label[k] = (char)b[label_off + k];
  216. fs->label[11] = '\0';
  217. for (k = 11; k > 0 && (fs->label[k - 1] == ' ' || fs->label[k - 1] == '\0'); k--)
  218. ;
  219. fs->label[k] = '\0';
  220. }
  221. /* Capture the FSInfo sector number before the media check clobbers
  222. * sector_buf (see the allocation-hint block below). */
  223. fsinfo_sec = (fs->type == FAT32) ? rd16(b + 0x30) : 0;
  224. /* Validate the FAT media byte: rejects a boot disk whose BPB is fake
  225. * (its FAT area is actually kernel code). */
  226. media = b[0x15];
  227. if (fat_read_sector(fs, fs->reserved) != 0)
  228. return -2;
  229. if (sector_buf[0] != media)
  230. return -1;
  231. /* Allocation hint: for FAT32 start from the FSInfo "next free cluster"
  232. * when it is sane, so repeated writes do not rescan the whole FAT. */
  233. fs->next_free = 2;
  234. if (fs->type == FAT32 && fsinfo_sec != 0 &&
  235. fat_read_sector(fs, fsinfo_sec) == 0 &&
  236. rd32(sector_buf + 0x000) == 0x41615252u &&
  237. rd32(sector_buf + 0x1FC) == 0xAA550000u) {
  238. unsigned int nf = rd32(sector_buf + 0x1EC);
  239. if (nf >= 2 && nf < fs->max_cluster)
  240. fs->next_free = nf;
  241. }
  242. fs->mounted = 1;
  243. return 0;
  244. }
  245. /* ---- name helpers (8.3) ---- */
  246. static void trim_name_8_3(const char *in, char *out)
  247. {
  248. unsigned int i;
  249. for (i = 0; i < 8 && in[i] && in[i] != ' '; i++)
  250. out[i] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  251. for (; i < 8; i++)
  252. out[i] = ' ';
  253. for (i = 0; i < 3 && in[8 + i] && in[8 + i] != ' '; i++)
  254. out[8 + i] = (char)(in[8 + i] >= 'a' && in[8 + i] <= 'z' ? in[8 + i] - 32 : in[8 + i]);
  255. for (; i < 3; i++)
  256. out[8 + i] = ' ';
  257. out[11] = '\0';
  258. }
  259. static void fat_name_to_8_3(const char *in, unsigned int len, unsigned char out[11])
  260. {
  261. unsigned int i, j, dot;
  262. for (i = 0; i < len && in[i] == ' '; i++)
  263. ;
  264. dot = i;
  265. while (dot < len && in[dot] != '.' && in[dot] != ' ')
  266. dot++;
  267. j = 0;
  268. while (i < dot && j < 8) {
  269. out[j++] = (unsigned char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  270. i++;
  271. }
  272. while (j < 8)
  273. out[j++] = ' ';
  274. if (i < len && in[i] == '.')
  275. i++;
  276. while (i < len && in[i] != ' ' && j < 11) {
  277. out[j++] = (unsigned char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  278. i++;
  279. }
  280. while (j < 11)
  281. out[j++] = ' ';
  282. }
  283. static unsigned int dir_entry_cluster(fat_fs_t *fs, const unsigned char *e)
  284. {
  285. unsigned int c = (unsigned int)e[26] | ((unsigned int)e[27] << 8);
  286. if (fs->type == FAT32)
  287. c |= ((unsigned int)e[20] << 16) | ((unsigned int)e[21] << 24);
  288. return c;
  289. }
  290. /* ---- directory iteration ---- */
  291. static int dir_each_entry(fat_fs_t *fs, unsigned int dir_cluster,
  292. int (*cb)(unsigned int lba, unsigned int offset, void *ctx),
  293. void *ctx)
  294. {
  295. if (dir_cluster == 0) {
  296. /* FAT12/16 fixed root directory. */
  297. unsigned int sec;
  298. for (sec = 0; sec < fs->root_dir_sectors; sec++) {
  299. unsigned int off;
  300. if (fat_read_sector(fs, fs->root_start + sec) != 0)
  301. return -2;
  302. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE)
  303. if (cb(fs->root_start + sec, off, ctx))
  304. return 0;
  305. }
  306. } else {
  307. unsigned int cluster = dir_cluster;
  308. while (cluster >= 2 && !fat_is_eoc(fs, cluster)) {
  309. unsigned int base = fs->data_start + (cluster - 2) * fs->sectors_per_cluster;
  310. unsigned int i;
  311. for (i = 0; i < fs->sectors_per_cluster; i++) {
  312. unsigned int off;
  313. if (fat_read_sector(fs, base + i) != 0)
  314. return -2;
  315. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE)
  316. if (cb(base + i, off, ctx))
  317. return 0;
  318. }
  319. if (fat_entry_get(fs, cluster, &cluster) != 0)
  320. return -2;
  321. }
  322. }
  323. return 0;
  324. }
  325. struct lookup_ctx {
  326. fat_fs_t *fs;
  327. const unsigned char *name;
  328. unsigned int cluster, size;
  329. unsigned char attr;
  330. unsigned int lba, offset;
  331. int found;
  332. };
  333. static int lookup_cb(unsigned int lba, unsigned int offset, void *vctx)
  334. {
  335. struct lookup_ctx *c = vctx;
  336. unsigned char *e = sector_buf + offset;
  337. unsigned int i;
  338. if (e[0] == 0x00)
  339. return 1; /* end of directory */
  340. if (e[0] == 0xE5)
  341. return 0; /* deleted */
  342. if ((e[11] & 0x18) == 0x08)
  343. return 0; /* volume label or LFN entry */
  344. for (i = 0; i < 11; i++)
  345. if ((e[i] >= 'a' ? e[i] - 32 : e[i]) != c->name[i])
  346. break;
  347. if (i == 11) {
  348. c->cluster = dir_entry_cluster(c->fs, e);
  349. c->size = (unsigned int)e[28] | ((unsigned int)e[29] << 8)
  350. | ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
  351. c->attr = e[11];
  352. c->lba = lba;
  353. c->offset = offset;
  354. c->found = 1;
  355. return 1;
  356. }
  357. return 0;
  358. }
  359. static int fat_lookup(fat_fs_t *fs, unsigned int dir_cluster, const unsigned char name[11],
  360. unsigned int *entry_cluster, unsigned int *size, unsigned char *attr,
  361. unsigned int *entry_lba, unsigned int *entry_offset)
  362. {
  363. struct lookup_ctx c;
  364. c.fs = fs;
  365. c.name = name;
  366. c.found = 0;
  367. c.cluster = c.size = 0;
  368. c.attr = 0;
  369. if (dir_each_entry(fs, dir_cluster, lookup_cb, &c) != 0)
  370. return -2;
  371. if (!c.found)
  372. return -1;
  373. if (entry_cluster) *entry_cluster = c.cluster;
  374. if (size) *size = c.size;
  375. if (attr) *attr = c.attr;
  376. if (entry_lba) *entry_lba = c.lba;
  377. if (entry_offset) *entry_offset = c.offset;
  378. return 0;
  379. }
  380. /* ---- public directory / read API ---- */
  381. struct list_ctx {
  382. fat_root_callback_t callback;
  383. };
  384. static int list_cb(unsigned int lba, unsigned int offset, void *vctx)
  385. {
  386. struct list_ctx *c = vctx;
  387. unsigned char *e = sector_buf + offset;
  388. char name_buf[12];
  389. unsigned int size;
  390. (void)lba;
  391. if (e[0] == 0x00)
  392. return 1; /* end of directory */
  393. if (e[0] == 0xE5)
  394. return 0;
  395. if ((e[11] & 0x18) == 0x08)
  396. return 0; /* volume label or LFN */
  397. trim_name_8_3((const char *)e, name_buf);
  398. size = (unsigned int)e[28] | ((unsigned int)e[29] << 8)
  399. | ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
  400. c->callback(name_buf, (e[11] & 0x10) ? 1 : 0, size);
  401. return 0;
  402. }
  403. int fat_list_dir(fat_fs_t *fs, unsigned int dir_cluster, fat_root_callback_t callback)
  404. {
  405. struct list_ctx c;
  406. c.callback = callback;
  407. if (!fs || !fs->mounted || !callback)
  408. return -1;
  409. return (dir_each_entry(fs, dir_cluster, list_cb, &c) != 0) ? -2 : 0;
  410. }
  411. int fat_read_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
  412. void *buf, unsigned int max_len)
  413. {
  414. unsigned int cluster, size;
  415. unsigned char attr;
  416. unsigned int copied = 0;
  417. unsigned char *dst = buf;
  418. int r;
  419. if (!fs || !fs->mounted || !buf || !name_8_3)
  420. return -1;
  421. r = fat_lookup(fs, dir_cluster, (const unsigned char *)name_8_3,
  422. &cluster, &size, &attr, 0, 0);
  423. if (r == -2)
  424. return -2;
  425. if (r != 0 || (attr & 0x10))
  426. return -1;
  427. if (size > max_len)
  428. size = max_len;
  429. while (cluster >= 2 && !fat_is_eoc(fs, cluster) && copied < size) {
  430. unsigned int sector = fs->data_start + (cluster - 2) * fs->sectors_per_cluster;
  431. unsigned int chunk = fs->sectors_per_cluster * SECTOR_SIZE;
  432. unsigned int n_sec;
  433. if (chunk > size - copied)
  434. chunk = size - copied;
  435. n_sec = (chunk + SECTOR_SIZE - 1) / SECTOR_SIZE;
  436. if (fat_read_data(fs, sector, dst + copied, n_sec) != 0)
  437. return -2;
  438. copied += chunk;
  439. if (copied >= size)
  440. break;
  441. if (fat_entry_get(fs, cluster, &cluster) != 0)
  442. return -2;
  443. }
  444. return (int)copied;
  445. }
  446. /* ---- write support ---- */
  447. static int fat_find_free_cluster(fat_fs_t *fs)
  448. {
  449. unsigned int c, start;
  450. /* Scan forward from the last allocation (wrapping once). Without the
  451. * hint a FAT32 root scan would re-read the whole FAT every time. */
  452. start = fs->next_free;
  453. if (start < 2 || start >= fs->max_cluster)
  454. start = 2;
  455. for (c = start; c < fs->max_cluster; c++) {
  456. unsigned int val;
  457. if (fat_entry_get(fs, c, &val) != 0)
  458. return -2;
  459. if (val == 0x000) {
  460. fs->next_free = c + 1;
  461. return (int)c;
  462. }
  463. }
  464. for (c = 2; c < start; c++) {
  465. unsigned int val;
  466. if (fat_entry_get(fs, c, &val) != 0)
  467. return -2;
  468. if (val == 0x000) {
  469. fs->next_free = c + 1;
  470. return (int)c;
  471. }
  472. }
  473. return -1;
  474. }
  475. static int fat_free_chain(fat_fs_t *fs, unsigned int cluster)
  476. {
  477. while (cluster >= 2 && cluster < fs->max_cluster) {
  478. unsigned int next;
  479. if (fat_entry_get(fs, cluster, &next) != 0)
  480. return -2;
  481. if (fat_entry_set(fs, cluster, 0x000) != 0)
  482. return -2;
  483. if (cluster < fs->next_free)
  484. fs->next_free = cluster;
  485. if (next < 2 || fat_is_eoc(fs, next))
  486. break;
  487. cluster = next;
  488. }
  489. return 0;
  490. }
  491. struct free_ctx {
  492. unsigned int lba, offset;
  493. int found;
  494. };
  495. static int find_free_cb(unsigned int lba, unsigned int offset, void *vctx)
  496. {
  497. struct free_ctx *c = vctx;
  498. if (sector_buf[offset] == 0x00 || sector_buf[offset] == 0xE5) {
  499. c->lba = lba;
  500. c->offset = offset;
  501. c->found = 1;
  502. return 1;
  503. }
  504. return 0;
  505. }
  506. static int dir_find_free(fat_fs_t *fs, unsigned int dir_cluster,
  507. unsigned int *lba, unsigned int *offset)
  508. {
  509. struct free_ctx c;
  510. c.found = 0;
  511. if (dir_each_entry(fs, dir_cluster, find_free_cb, &c) != 0)
  512. return -2;
  513. if (!c.found)
  514. return -1;
  515. *lba = c.lba;
  516. *offset = c.offset;
  517. return 0;
  518. }
  519. static int fat_dir_entry_write(fat_fs_t *fs, unsigned int lba, unsigned int offset,
  520. const unsigned char name[11], unsigned char attr,
  521. unsigned int cluster, unsigned int size)
  522. {
  523. unsigned int i;
  524. if (fat_read_sector(fs, lba) != 0)
  525. return -2;
  526. for (i = 0; i < 11; i++)
  527. sector_buf[offset + i] = name[i];
  528. sector_buf[offset + 11] = attr;
  529. for (i = 12; i < 26; i++)
  530. sector_buf[offset + i] = 0;
  531. sector_buf[offset + 26] = (unsigned char)(cluster & 0xFF);
  532. sector_buf[offset + 27] = (unsigned char)((cluster >> 8) & 0xFF);
  533. if (fs->type == FAT32) { /* high 16 bits of the first cluster */
  534. sector_buf[offset + 20] = (unsigned char)((cluster >> 16) & 0xFF);
  535. sector_buf[offset + 21] = (unsigned char)((cluster >> 24) & 0xFF);
  536. }
  537. sector_buf[offset + 28] = (unsigned char)(size & 0xFF);
  538. sector_buf[offset + 29] = (unsigned char)((size >> 8) & 0xFF);
  539. sector_buf[offset + 30] = (unsigned char)((size >> 16) & 0xFF);
  540. sector_buf[offset + 31] = (unsigned char)((size >> 24) & 0xFF);
  541. return (fat_write_sector(fs, lba) != 0) ? -2 : 0;
  542. }
  543. static int fat_mark_deleted(fat_fs_t *fs, unsigned int lba, unsigned int offset)
  544. {
  545. if (fat_read_sector(fs, lba) != 0)
  546. return -2;
  547. sector_buf[offset] = 0xE5;
  548. return (fat_write_sector(fs, lba) != 0) ? -2 : 0;
  549. }
  550. int fat_write_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3,
  551. const void *buf, unsigned int size)
  552. {
  553. unsigned char name[11];
  554. unsigned int i;
  555. unsigned int first_cluster = 0, prev = 0, cluster;
  556. unsigned int remaining = size;
  557. const unsigned char *src = buf;
  558. unsigned int lba = 0, offset = 0, old_cluster = 0;
  559. unsigned char old_attr = 0;
  560. int r;
  561. if (!fs || !fs->mounted || !name_8_3)
  562. return -1;
  563. for (i = 0; i < 11; i++)
  564. name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
  565. ? name_8_3[i] - 32 : name_8_3[i]);
  566. r = fat_lookup(fs, dir_cluster, name, &old_cluster, 0, &old_attr, &lba, &offset);
  567. if (r == -2)
  568. return -2;
  569. if (r == 0 && (old_attr & 0x10)) {
  570. io_putstr("write: is a directory\n");
  571. return -1;
  572. }
  573. if (r != 0) {
  574. r = dir_find_free(fs, dir_cluster, &lba, &offset);
  575. if (r == -2)
  576. return -2;
  577. if (r != 0) {
  578. io_putstr("write: directory full\n");
  579. return -1;
  580. }
  581. } else if (old_cluster >= 2) {
  582. fat_free_chain(fs, old_cluster);
  583. }
  584. if (remaining > 0) {
  585. unsigned int cluster_size = fs->sectors_per_cluster * SECTOR_SIZE;
  586. while (remaining > 0) {
  587. int c = fat_find_free_cluster(fs);
  588. if (c == -2)
  589. return -2;
  590. if (c < 0) {
  591. if (first_cluster)
  592. fat_free_chain(fs, first_cluster);
  593. io_putstr("write: disk full\n");
  594. return -1;
  595. }
  596. cluster = (unsigned int)c;
  597. if (first_cluster == 0)
  598. first_cluster = cluster;
  599. else if (fat_entry_set(fs, prev, cluster) != 0)
  600. return -2;
  601. {
  602. unsigned int chunk = (remaining < cluster_size) ? remaining : cluster_size;
  603. unsigned int lba2 = fs->data_start + (cluster - 2) * fs->sectors_per_cluster;
  604. unsigned int full_secs = chunk / SECTOR_SIZE;
  605. unsigned int rem_bytes = chunk % SECTOR_SIZE;
  606. if (full_secs > 0) {
  607. if (blk_write(fs->dev, fs->part_lba + lba2, src, full_secs) != 0)
  608. return -2;
  609. src += full_secs * SECTOR_SIZE;
  610. lba2 += full_secs;
  611. }
  612. if (rem_bytes > 0) {
  613. unsigned int j;
  614. for (j = 0; j < rem_bytes; j++)
  615. sector_buf[j] = src[j];
  616. for (j = rem_bytes; j < SECTOR_SIZE; j++)
  617. sector_buf[j] = 0;
  618. if (blk_write(fs->dev, fs->part_lba + lba2, sector_buf, 1) != 0)
  619. return -2;
  620. src += rem_bytes;
  621. }
  622. remaining -= chunk;
  623. }
  624. prev = cluster;
  625. }
  626. if (fat_entry_set(fs, prev, fat_eoc(fs)) != 0)
  627. return -2;
  628. }
  629. if (fat_dir_entry_write(fs, lba, offset, name, 0x20, first_cluster, size) != 0)
  630. return -2;
  631. return 0;
  632. }
  633. int fat_delete_file_in(fat_fs_t *fs, unsigned int dir_cluster, const char *name_8_3)
  634. {
  635. unsigned char name[11];
  636. unsigned int i, cluster = 0;
  637. unsigned char attr = 0;
  638. unsigned int lba, offset;
  639. int r;
  640. if (!fs || !fs->mounted || !name_8_3)
  641. return -1;
  642. for (i = 0; i < 11; i++)
  643. name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
  644. ? name_8_3[i] - 32 : name_8_3[i]);
  645. r = fat_lookup(fs, dir_cluster, name, &cluster, 0, &attr, &lba, &offset);
  646. if (r == -2)
  647. return -2;
  648. if (r != 0)
  649. return -1;
  650. if (attr & 0x10)
  651. return -1; /* it's a directory; use rmdir */
  652. if (cluster >= 2)
  653. fat_free_chain(fs, cluster);
  654. return fat_mark_deleted(fs, lba, offset);
  655. }
  656. static int dir_empty_cb(unsigned int lba, unsigned int offset, void *vctx)
  657. {
  658. int *nonempty = vctx;
  659. unsigned char *e = sector_buf + offset;
  660. (void)lba;
  661. if (e[0] == 0x00 || e[0] == 0xE5)
  662. return 0;
  663. if (e[0] == '.')
  664. return 0; /* "." or ".." */
  665. *nonempty = 1;
  666. return 1;
  667. }
  668. int fat_mkdir(fat_fs_t *fs, unsigned int parent_cluster, const char *name_8_3)
  669. {
  670. unsigned char name[11];
  671. unsigned int i, cluster;
  672. unsigned int lba, offset;
  673. int r;
  674. if (!fs || !fs->mounted || !name_8_3)
  675. return -1;
  676. for (i = 0; i < 11; i++)
  677. name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
  678. ? name_8_3[i] - 32 : name_8_3[i]);
  679. r = fat_lookup(fs, parent_cluster, name, 0, 0, 0, 0, 0);
  680. if (r == -2)
  681. return -2;
  682. if (r == 0) {
  683. io_putstr("mkdir: already exists\n");
  684. return -1;
  685. }
  686. r = fat_find_free_cluster(fs);
  687. if (r == -2)
  688. return -2;
  689. if (r < 0) {
  690. io_putstr("mkdir: disk full\n");
  691. return -1;
  692. }
  693. cluster = (unsigned int)r;
  694. if (fat_entry_set(fs, cluster, fat_eoc(fs)) != 0)
  695. return -2;
  696. {
  697. unsigned int base = fs->data_start + (cluster - 2) * fs->sectors_per_cluster;
  698. for (i = 0; i < SECTOR_SIZE; i++)
  699. sector_buf[i] = 0;
  700. sector_buf[0] = '.';
  701. for (i = 1; i < 11; i++)
  702. sector_buf[i] = ' ';
  703. sector_buf[11] = 0x10;
  704. sector_buf[26] = (unsigned char)(cluster & 0xFF);
  705. sector_buf[27] = (unsigned char)((cluster >> 8) & 0xFF);
  706. sector_buf[32] = '.';
  707. sector_buf[33] = '.';
  708. for (i = 34; i < 43; i++)
  709. sector_buf[i] = ' ';
  710. sector_buf[43] = 0x10;
  711. sector_buf[58] = (unsigned char)(parent_cluster & 0xFF);
  712. sector_buf[59] = (unsigned char)((parent_cluster >> 8) & 0xFF);
  713. if (fs->type == FAT32) { /* high halves of "." and ".." */
  714. sector_buf[20] = (unsigned char)((cluster >> 16) & 0xFF);
  715. sector_buf[21] = (unsigned char)((cluster >> 24) & 0xFF);
  716. sector_buf[52] = (unsigned char)((parent_cluster >> 16) & 0xFF);
  717. sector_buf[53] = (unsigned char)((parent_cluster >> 24) & 0xFF);
  718. }
  719. if (fat_write_sector(fs, base) != 0)
  720. return -2;
  721. }
  722. r = dir_find_free(fs, parent_cluster, &lba, &offset);
  723. if (r == -2)
  724. return -2;
  725. if (r != 0) {
  726. fat_free_chain(fs, cluster);
  727. io_putstr("mkdir: directory full\n");
  728. return -1;
  729. }
  730. return fat_dir_entry_write(fs, lba, offset, name, 0x10, cluster, 0);
  731. }
  732. int fat_rmdir(fat_fs_t *fs, unsigned int parent_cluster, const char *name_8_3)
  733. {
  734. unsigned char name[11];
  735. unsigned int i, cluster = 0;
  736. unsigned char attr = 0;
  737. unsigned int lba, offset;
  738. int r, nonempty = 0;
  739. if (!fs || !fs->mounted || !name_8_3)
  740. return -1;
  741. for (i = 0; i < 11; i++)
  742. name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
  743. ? name_8_3[i] - 32 : name_8_3[i]);
  744. r = fat_lookup(fs, parent_cluster, name, &cluster, 0, &attr, &lba, &offset);
  745. if (r == -2)
  746. return -2;
  747. if (r != 0)
  748. return -1;
  749. if (!(attr & 0x10)) {
  750. io_putstr("rmdir: not a directory\n");
  751. return -1;
  752. }
  753. if (dir_each_entry(fs, cluster, dir_empty_cb, &nonempty) != 0)
  754. return -2;
  755. if (nonempty) {
  756. io_putstr("rmdir: directory not empty\n");
  757. return -1;
  758. }
  759. if (cluster >= 2)
  760. fat_free_chain(fs, cluster);
  761. return fat_mark_deleted(fs, lba, offset);
  762. }
  763. /* ---- path resolution ---- */
  764. int fat_resolve_path(fat_fs_t *fs, const char *path, unsigned int *dir_cluster,
  765. char name_8_3[12])
  766. {
  767. unsigned int dir = (fs->type == FAT32) ? fs->root_cluster : 0;
  768. const char *p = path;
  769. if (!fs || !fs->mounted || !path || !dir_cluster || !name_8_3)
  770. return -1;
  771. for (;;) {
  772. const char *start;
  773. unsigned int len;
  774. while (*p == '/')
  775. p++;
  776. if (!*p)
  777. return -1; /* empty path */
  778. start = p;
  779. len = 0;
  780. while (p[len] && p[len] != '/')
  781. len++;
  782. if (p[len] == '/') {
  783. unsigned char name[11];
  784. unsigned int cluster, size;
  785. unsigned char attr;
  786. int r;
  787. fat_name_to_8_3(start, len, name);
  788. r = fat_lookup(fs, dir, name, &cluster, &size, &attr, 0, 0);
  789. if (r == -2)
  790. return -2;
  791. if (r != 0 || !(attr & 0x10))
  792. return -1;
  793. dir = cluster;
  794. p += len + 1;
  795. } else {
  796. fat_name_to_8_3(start, len, (unsigned char *)name_8_3);
  797. name_8_3[11] = '\0';
  798. *dir_cluster = dir;
  799. return 0;
  800. }
  801. }
  802. }
  803. int fat_resolve_dir(fat_fs_t *fs, const char *path, unsigned int *dir_cluster)
  804. {
  805. unsigned int dir = (fs->type == FAT32) ? fs->root_cluster : 0;
  806. const char *p = path;
  807. if (!fs || !fs->mounted || !path || !dir_cluster)
  808. return -1;
  809. if (!*p) {
  810. *dir_cluster = dir;
  811. return 0;
  812. }
  813. for (;;) {
  814. const char *start;
  815. unsigned int len;
  816. while (*p == '/')
  817. p++;
  818. if (!*p)
  819. break; /* trailing slash */
  820. start = p;
  821. len = 0;
  822. while (p[len] && p[len] != '/')
  823. len++;
  824. {
  825. unsigned char name[11];
  826. unsigned int cluster, size;
  827. unsigned char attr;
  828. int r;
  829. fat_name_to_8_3(start, len, name);
  830. r = fat_lookup(fs, dir, name, &cluster, &size, &attr, 0, 0);
  831. if (r == -2)
  832. return -2;
  833. if (r != 0 || !(attr & 0x10))
  834. return -1;
  835. dir = cluster;
  836. }
  837. if (!p[len])
  838. break;
  839. p += len + 1;
  840. }
  841. *dir_cluster = dir;
  842. return 0;
  843. }