fat.c 23 KB

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