fat.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /* FAT12: BPB parse, root list, read/write files by following FAT chain. */
  2. #include "disk.h"
  3. #include "fat.h"
  4. #include "io.h"
  5. #define SECTOR_SIZE 512
  6. #define ROOT_ENTRY_SIZE 32
  7. #define FAT12_EOC_MIN 0xFF8
  8. /* BPB offsets (same for FAT12 1.44MB floppy) */
  9. #define BPB_BYTES_PER_SEC 0x0B
  10. #define BPB_SEC_PER_CLUSTER 0x0D
  11. #define BPB_RESERVED 0x0E
  12. #define BPB_NUM_FATS 0x10
  13. #define BPB_ROOT_ENTRIES 0x11
  14. #define BPB_SEC_PER_FAT 0x16
  15. #define BPB_TOTAL_SEC16 0x13 /* total sectors (16-bit) */
  16. #define BPB_TOTAL_SEC32 0x20 /* total sectors (32-bit) */
  17. #define BPB_SIGNATURE 0x1FE /* 0x55 0xAA at end of boot sector */
  18. static unsigned int fat_reserved;
  19. static unsigned int fat_root_start;
  20. static unsigned int fat_root_sectors;
  21. static unsigned int fat_data_start;
  22. static unsigned int fat_sec_per_cluster;
  23. static unsigned int fat_num_fats;
  24. static unsigned int fat_sec_per_fat;
  25. static unsigned int fat_total_sectors;
  26. static unsigned int fat_max_cluster; /* 2 + number of data clusters */
  27. static int fat_ready;
  28. static unsigned char sector_buf[SECTOR_SIZE];
  29. static int fat_read_sector(unsigned int lba)
  30. {
  31. if (disk_read_sectors(lba, sector_buf, 1) != 0)
  32. return -1;
  33. return 0;
  34. }
  35. /* Get 12-bit FAT entry at cluster index. FAT is in sector_buf (one sector). */
  36. static unsigned int fat_get_entry(unsigned int cluster_idx)
  37. {
  38. unsigned int byte_off = (cluster_idx * 3) / 2;
  39. unsigned int word_val;
  40. if (byte_off + 2 > SECTOR_SIZE)
  41. return 0xFFFF;
  42. word_val = (unsigned int)sector_buf[byte_off] | ((unsigned int)sector_buf[byte_off + 1] << 8);
  43. if (cluster_idx & 1)
  44. return word_val >> 4;
  45. return word_val & 0xFFF;
  46. }
  47. int fat_init(void)
  48. {
  49. unsigned int root_entries;
  50. if (fat_read_sector(0) != 0) {
  51. io_putstr("run: disk read error (boot sector LBA=0)\n");
  52. return -2; /* disk I/O error */
  53. }
  54. if (sector_buf[BPB_SIGNATURE] != 0x55 || sector_buf[BPB_SIGNATURE + 1] != 0xAA)
  55. return -1; /* no valid FAT filesystem */
  56. fat_reserved = (unsigned int)sector_buf[BPB_RESERVED] | ((unsigned int)sector_buf[BPB_RESERVED + 1] << 8);
  57. fat_num_fats = sector_buf[BPB_NUM_FATS];
  58. root_entries = (unsigned int)sector_buf[BPB_ROOT_ENTRIES] | ((unsigned int)sector_buf[BPB_ROOT_ENTRIES + 1] << 8);
  59. fat_sec_per_fat = (unsigned int)sector_buf[BPB_SEC_PER_FAT] | ((unsigned int)sector_buf[BPB_SEC_PER_FAT + 1] << 8);
  60. fat_sec_per_cluster = sector_buf[BPB_SEC_PER_CLUSTER];
  61. fat_total_sectors = (unsigned int)sector_buf[BPB_TOTAL_SEC16] | ((unsigned int)sector_buf[BPB_TOTAL_SEC16 + 1] << 8);
  62. if (fat_total_sectors == 0)
  63. fat_total_sectors = (unsigned int)sector_buf[BPB_TOTAL_SEC32]
  64. | ((unsigned int)sector_buf[BPB_TOTAL_SEC32 + 1] << 8)
  65. | ((unsigned int)sector_buf[BPB_TOTAL_SEC32 + 2] << 16)
  66. | ((unsigned int)sector_buf[BPB_TOTAL_SEC32 + 3] << 24);
  67. fat_root_start = fat_reserved + fat_num_fats * fat_sec_per_fat;
  68. fat_root_sectors = (root_entries * ROOT_ENTRY_SIZE + SECTOR_SIZE - 1) / SECTOR_SIZE;
  69. fat_data_start = fat_root_start + fat_root_sectors;
  70. if (fat_total_sectors > fat_data_start && fat_sec_per_cluster > 0)
  71. fat_max_cluster = 2 + (fat_total_sectors - fat_data_start) / fat_sec_per_cluster;
  72. else
  73. fat_max_cluster = 2;
  74. fat_ready = 1;
  75. return 0;
  76. }
  77. static void trim_name_8_3(const char *in, char *out)
  78. {
  79. unsigned int i;
  80. for (i = 0; i < 8 && in[i] && in[i] != ' '; i++)
  81. out[i] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  82. for (; i < 8; i++)
  83. out[i] = ' ';
  84. for (i = 0; i < 3 && in[8 + i] && in[8 + i] != ' '; i++)
  85. out[8 + i] = (char)(in[8 + i] >= 'a' && in[8 + i] <= 'z' ? in[8 + i] - 32 : in[8 + i]);
  86. for (; i < 3; i++)
  87. out[8 + i] = ' ';
  88. out[11] = '\0';
  89. }
  90. int fat_list_root(fat_root_callback_t callback)
  91. {
  92. unsigned int sec, off;
  93. char name_buf[12];
  94. if (!fat_ready || !callback)
  95. return -1;
  96. for (sec = 0; sec < fat_root_sectors; sec++) {
  97. if (fat_read_sector(fat_root_start + sec) != 0) {
  98. io_putstr("run: disk read error (root dir sec=");
  99. io_puthex(sec);
  100. io_putstr(" LBA=");
  101. io_puthex(fat_root_start + sec);
  102. io_putstr(")\n");
  103. return -2;
  104. }
  105. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
  106. unsigned char *e = sector_buf + off;
  107. if (e[0] == 0x00)
  108. return 0;
  109. if (e[0] == 0xE5)
  110. continue;
  111. if ((e[11] & 0x18) == 0x08)
  112. continue;
  113. trim_name_8_3((const char *)e, name_buf);
  114. {
  115. unsigned int size = (unsigned int)e[28] | ((unsigned int)e[29] << 8) |
  116. ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
  117. (*callback)(name_buf, (e[11] & 0x10) ? 1 : 0, size);
  118. }
  119. }
  120. }
  121. return 0;
  122. }
  123. /* Find root entry by 8.3 name; return first cluster and size, or -1. */
  124. static int fat_find_root(const char *name_8_3, unsigned int *out_cluster, unsigned int *out_size)
  125. {
  126. unsigned int sec, off;
  127. char want[12];
  128. unsigned int i;
  129. for (i = 0; i < 11; i++)
  130. want[i] = (char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z' ? name_8_3[i] - 32 : name_8_3[i]);
  131. want[11] = '\0';
  132. for (sec = 0; sec < fat_root_sectors; sec++) {
  133. if (fat_read_sector(fat_root_start + sec) != 0) {
  134. io_putstr("run: disk read error (find root sec=");
  135. io_puthex(sec);
  136. io_putstr(" LBA=");
  137. io_puthex(fat_root_start + sec);
  138. io_putstr(")\n");
  139. return -2;
  140. }
  141. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
  142. unsigned char *e = sector_buf + off;
  143. if (e[0] == 0x00)
  144. return -1;
  145. if (e[0] == 0xE5 || (e[11] & 0x10))
  146. continue;
  147. for (i = 0; i < 11; i++)
  148. if ((e[i] >= 'a' ? e[i] - 32 : e[i]) != (unsigned char)want[i])
  149. break;
  150. if (i < 11)
  151. continue;
  152. *out_cluster = (unsigned int)e[26] | ((unsigned int)e[27] << 8);
  153. *out_size = (unsigned int)e[28] | ((unsigned int)e[29] << 8) |
  154. ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
  155. return 0;
  156. }
  157. }
  158. return -1;
  159. }
  160. int fat_read_file(const char *name_8_3, void *buf, unsigned int max_len)
  161. {
  162. unsigned int cluster, size;
  163. unsigned int fat_start = fat_reserved;
  164. unsigned int copied = 0;
  165. unsigned char *dst = (unsigned char *)buf;
  166. unsigned int loaded_fat_sec;
  167. if (!fat_ready || !buf)
  168. return -1;
  169. {
  170. int fr = fat_find_root(name_8_3, &cluster, &size);
  171. if (fr == -2)
  172. return -2;
  173. if (fr != 0)
  174. return -1;
  175. }
  176. if (size > max_len)
  177. size = max_len;
  178. /* Pre-load the FAT sector covering the first cluster so we can follow
  179. * the chain without re-reading the FAT between every data cluster.
  180. * Data reads use DMA + memcpy to dst and never touch sector_buf,
  181. * so the cached FAT data stays valid throughout. */
  182. loaded_fat_sec = fat_start + (cluster * 3) / (2 * SECTOR_SIZE);
  183. if (fat_read_sector(loaded_fat_sec) != 0) {
  184. io_putstr("run: disk read error (FAT preload LBA=");
  185. io_puthex(loaded_fat_sec);
  186. io_putstr(")\n");
  187. return -2;
  188. }
  189. while (cluster >= 2 && cluster < 0xFF8 && copied < size) {
  190. unsigned int sector = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
  191. unsigned int chunk = fat_sec_per_cluster * SECTOR_SIZE;
  192. unsigned int n_sec;
  193. if (chunk > size - copied)
  194. chunk = size - copied;
  195. n_sec = (chunk + SECTOR_SIZE - 1) / SECTOR_SIZE;
  196. if (disk_read_sectors(sector, dst + copied, n_sec) != 0) {
  197. io_putstr("run: disk read error (file data cluster=");
  198. io_puthex(cluster);
  199. io_putstr(" LBA=");
  200. io_puthex(sector);
  201. io_putstr(" n_sec=");
  202. io_puthex(n_sec);
  203. io_putstr(")\n");
  204. return -2;
  205. }
  206. copied += chunk;
  207. if (copied >= size)
  208. break;
  209. /* Only re-read FAT if the chain crosses into a different FAT sector. */
  210. {
  211. unsigned int need_fat_sec = fat_start + (cluster * 3) / (2 * SECTOR_SIZE);
  212. if (need_fat_sec != loaded_fat_sec) {
  213. if (fat_read_sector(need_fat_sec) != 0) {
  214. io_putstr("run: disk read error (FAT cluster=");
  215. io_puthex(cluster);
  216. io_putstr(" LBA=");
  217. io_puthex(need_fat_sec);
  218. io_putstr(")\n");
  219. return -2;
  220. }
  221. loaded_fat_sec = need_fat_sec;
  222. }
  223. }
  224. cluster = fat_get_entry(cluster);
  225. }
  226. return (int)copied;
  227. }
  228. /* ---- write support ---- */
  229. /* Read a 12-bit FAT entry for `cluster`, handling the case where the two
  230. * bytes straddle a FAT sector boundary. Returns 0, or -2 on disk error. */
  231. static int fat_entry_read(unsigned int cluster, unsigned int *value)
  232. {
  233. unsigned int byte_off = (cluster * 3) / 2;
  234. unsigned int sec = fat_reserved + byte_off / SECTOR_SIZE;
  235. unsigned int within = byte_off % SECTOR_SIZE;
  236. unsigned char b0, b1;
  237. if (within == SECTOR_SIZE - 1) {
  238. if (fat_read_sector(sec) != 0)
  239. return -2;
  240. b0 = sector_buf[within];
  241. if (fat_read_sector(sec + 1) != 0)
  242. return -2;
  243. b1 = sector_buf[0];
  244. } else {
  245. if (fat_read_sector(sec) != 0)
  246. return -2;
  247. b0 = sector_buf[within];
  248. b1 = sector_buf[within + 1];
  249. }
  250. if (cluster & 1)
  251. *value = ((unsigned int)b0 >> 4) | ((unsigned int)b1 << 4);
  252. else
  253. *value = (unsigned int)b0 | (((unsigned int)b1 & 0x0F) << 8);
  254. return 0;
  255. }
  256. /* Write a 12-bit FAT entry for `cluster` (read-modify-write), handling the
  257. * sector-boundary case. Returns 0, or -2 on disk error. */
  258. static int fat_entry_write(unsigned int cluster, unsigned int value)
  259. {
  260. unsigned int byte_off = (cluster * 3) / 2;
  261. unsigned int sec = fat_reserved + byte_off / SECTOR_SIZE;
  262. unsigned int within = byte_off % SECTOR_SIZE;
  263. value &= 0xFFF;
  264. if (within == SECTOR_SIZE - 1) {
  265. /* Entry straddles into the next FAT sector. */
  266. if (cluster & 1) {
  267. if (fat_read_sector(sec) != 0) return -2;
  268. sector_buf[within] = (sector_buf[within] & 0x0F) | (unsigned char)((value << 4) & 0xF0);
  269. if (disk_write_sectors(sec, sector_buf, 1) != 0) return -2;
  270. if (fat_read_sector(sec + 1) != 0) return -2;
  271. sector_buf[0] = (unsigned char)((value >> 4) & 0xFF);
  272. if (disk_write_sectors(sec + 1, sector_buf, 1) != 0) return -2;
  273. } else {
  274. if (fat_read_sector(sec) != 0) return -2;
  275. sector_buf[within] = (unsigned char)(value & 0xFF);
  276. if (disk_write_sectors(sec, sector_buf, 1) != 0) return -2;
  277. if (fat_read_sector(sec + 1) != 0) return -2;
  278. sector_buf[0] = (sector_buf[0] & 0xF0) | (unsigned char)((value >> 8) & 0x0F);
  279. if (disk_write_sectors(sec + 1, sector_buf, 1) != 0) return -2;
  280. }
  281. } else {
  282. if (fat_read_sector(sec) != 0) return -2;
  283. if (cluster & 1) {
  284. sector_buf[within] = (sector_buf[within] & 0x0F) | (unsigned char)((value << 4) & 0xF0);
  285. sector_buf[within + 1] = (unsigned char)((value >> 4) & 0xFF);
  286. } else {
  287. sector_buf[within] = (unsigned char)(value & 0xFF);
  288. sector_buf[within + 1] = (sector_buf[within + 1] & 0xF0) | (unsigned char)((value >> 8) & 0x0F);
  289. }
  290. if (disk_write_sectors(sec, sector_buf, 1) != 0) return -2;
  291. }
  292. return 0;
  293. }
  294. /* Find a free data cluster (FAT entry == 0x000). Returns cluster >= 2, -1 if
  295. * the disk is full, -2 on disk error. */
  296. static int fat_find_free_cluster(void)
  297. {
  298. unsigned int cluster;
  299. for (cluster = 2; cluster < fat_max_cluster; cluster++) {
  300. unsigned int val;
  301. if (fat_entry_read(cluster, &val) != 0)
  302. return -2;
  303. if (val == 0x000)
  304. return (int)cluster;
  305. }
  306. return -1;
  307. }
  308. /* Mark every cluster in a chain as free (0x000). */
  309. static int fat_free_chain(unsigned int cluster)
  310. {
  311. while (cluster >= 2 && cluster < fat_max_cluster) {
  312. unsigned int next;
  313. if (fat_entry_read(cluster, &next) != 0)
  314. return -2;
  315. if (fat_entry_write(cluster, 0x000) != 0)
  316. return -2;
  317. if (next < 2 || next >= FAT12_EOC_MIN)
  318. break;
  319. cluster = next;
  320. }
  321. return 0;
  322. }
  323. /* Convert a name (length `len`) into an 11-byte 8.3 name (uppercase, padded). */
  324. static void fat_name_to_8_3(const char *in, unsigned int len, unsigned char out[11])
  325. {
  326. unsigned int i, j, dot;
  327. for (i = 0; i < len && in[i] == ' '; i++)
  328. ;
  329. dot = i;
  330. while (dot < len && in[dot] != '.' && in[dot] != ' ')
  331. dot++;
  332. j = 0;
  333. while (i < dot && j < 8) {
  334. out[j++] = (unsigned char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  335. i++;
  336. }
  337. while (j < 8)
  338. out[j++] = ' ';
  339. if (i < len && in[i] == '.')
  340. i++;
  341. while (i < len && in[i] != ' ' && j < 11) {
  342. out[j++] = (unsigned char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  343. i++;
  344. }
  345. while (j < 11)
  346. out[j++] = ' ';
  347. }
  348. /* Iterate the entries of a directory (0 = root, else first cluster). For each
  349. * entry load the sector and call cb(lba, offset, ctx); cb returns non-zero to
  350. * stop. Returns 0, or -2 on disk error. */
  351. static int dir_each_entry(unsigned int dir_cluster,
  352. int (*cb)(unsigned int lba, unsigned int offset, void *ctx),
  353. void *ctx)
  354. {
  355. if (dir_cluster == 0) {
  356. unsigned int sec;
  357. for (sec = 0; sec < fat_root_sectors; sec++) {
  358. unsigned int off;
  359. if (fat_read_sector(fat_root_start + sec) != 0)
  360. return -2;
  361. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE)
  362. if (cb(fat_root_start + sec, off, ctx))
  363. return 0;
  364. }
  365. } else {
  366. unsigned int cluster = dir_cluster;
  367. while (cluster >= 2 && cluster < FAT12_EOC_MIN) {
  368. unsigned int base = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
  369. unsigned int i;
  370. for (i = 0; i < fat_sec_per_cluster; i++) {
  371. unsigned int off;
  372. if (fat_read_sector(base + i) != 0)
  373. return -2;
  374. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE)
  375. if (cb(base + i, off, ctx))
  376. return 0;
  377. }
  378. if (fat_entry_read(cluster, &cluster) != 0)
  379. return -2;
  380. }
  381. }
  382. return 0;
  383. }
  384. struct lookup_ctx {
  385. const unsigned char *name;
  386. unsigned int cluster, size;
  387. unsigned char attr;
  388. unsigned int lba, offset;
  389. int found;
  390. };
  391. static int lookup_cb(unsigned int lba, unsigned int offset, void *vctx)
  392. {
  393. struct lookup_ctx *c = vctx;
  394. unsigned char *e = sector_buf + offset;
  395. unsigned int i;
  396. if (e[0] == 0x00)
  397. return 1; /* end of directory */
  398. if (e[0] == 0xE5)
  399. return 0; /* deleted */
  400. for (i = 0; i < 11; i++)
  401. if ((e[i] >= 'a' ? e[i] - 32 : e[i]) != c->name[i])
  402. break;
  403. if (i == 11) {
  404. c->cluster = (unsigned int)e[26] | ((unsigned int)e[27] << 8);
  405. c->size = (unsigned int)e[28] | ((unsigned int)e[29] << 8)
  406. | ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
  407. c->attr = e[11];
  408. c->lba = lba;
  409. c->offset = offset;
  410. c->found = 1;
  411. return 1;
  412. }
  413. return 0;
  414. }
  415. /* Look up `name` (11-byte, uppercase) in directory `dir_cluster`. */
  416. static int fat_lookup(unsigned int dir_cluster, const unsigned char name[11],
  417. unsigned int *entry_cluster, unsigned int *size, unsigned char *attr,
  418. unsigned int *entry_lba, unsigned int *entry_offset)
  419. {
  420. struct lookup_ctx c;
  421. c.name = name;
  422. c.found = 0;
  423. c.cluster = c.size = 0;
  424. c.attr = 0;
  425. if (dir_each_entry(dir_cluster, lookup_cb, &c) != 0)
  426. return -2;
  427. if (!c.found)
  428. return -1;
  429. if (entry_cluster) *entry_cluster = c.cluster;
  430. if (size) *size = c.size;
  431. if (attr) *attr = c.attr;
  432. if (entry_lba) *entry_lba = c.lba;
  433. if (entry_offset) *entry_offset = c.offset;
  434. return 0;
  435. }
  436. struct free_ctx {
  437. unsigned int lba, offset;
  438. int found;
  439. };
  440. static int find_free_cb(unsigned int lba, unsigned int offset, void *vctx)
  441. {
  442. struct free_ctx *c = vctx;
  443. if (sector_buf[offset] == 0x00 || sector_buf[offset] == 0xE5) {
  444. c->lba = lba;
  445. c->offset = offset;
  446. c->found = 1;
  447. return 1;
  448. }
  449. return 0;
  450. }
  451. /* Find a free (0x00 or 0xE5) entry in `dir_cluster`. Returns 0 and fills
  452. * lba/offset, or -1 if the directory is full, -2 on disk error. */
  453. static int dir_find_free(unsigned int dir_cluster, unsigned int *lba, unsigned int *offset)
  454. {
  455. struct free_ctx c;
  456. c.found = 0;
  457. if (dir_each_entry(dir_cluster, find_free_cb, &c) != 0)
  458. return -2;
  459. if (!c.found)
  460. return -1;
  461. *lba = c.lba;
  462. *offset = c.offset;
  463. return 0;
  464. }
  465. /* Write a 32-byte directory entry at (lba, offset). */
  466. static int fat_dir_entry_write(unsigned int lba, unsigned int offset,
  467. const unsigned char name[11], unsigned char attr,
  468. unsigned int cluster, unsigned int size)
  469. {
  470. unsigned int i;
  471. if (fat_read_sector(lba) != 0)
  472. return -2;
  473. for (i = 0; i < 11; i++)
  474. sector_buf[offset + i] = name[i];
  475. sector_buf[offset + 11] = attr;
  476. for (i = 12; i < 26; i++)
  477. sector_buf[offset + i] = 0;
  478. sector_buf[offset + 26] = (unsigned char)(cluster & 0xFF);
  479. sector_buf[offset + 27] = (unsigned char)((cluster >> 8) & 0xFF);
  480. sector_buf[offset + 28] = (unsigned char)(size & 0xFF);
  481. sector_buf[offset + 29] = (unsigned char)((size >> 8) & 0xFF);
  482. sector_buf[offset + 30] = (unsigned char)((size >> 16) & 0xFF);
  483. sector_buf[offset + 31] = (unsigned char)((size >> 24) & 0xFF);
  484. return (disk_write_sectors(lba, sector_buf, 1) != 0) ? -2 : 0;
  485. }
  486. /* Mark the entry at (lba, offset) deleted (0xE5). */
  487. static int fat_mark_deleted(unsigned int lba, unsigned int offset)
  488. {
  489. if (fat_read_sector(lba) != 0)
  490. return -2;
  491. sector_buf[offset] = 0xE5;
  492. return (disk_write_sectors(lba, sector_buf, 1) != 0) ? -2 : 0;
  493. }
  494. /* ---- public directory API ---- */
  495. struct list_ctx {
  496. fat_root_callback_t callback;
  497. };
  498. static int list_cb(unsigned int lba, unsigned int offset, void *vctx)
  499. {
  500. struct list_ctx *c = vctx;
  501. unsigned char *e = sector_buf + offset;
  502. char name_buf[12];
  503. unsigned int size;
  504. (void)lba;
  505. if (e[0] == 0x00)
  506. return 1; /* end of directory */
  507. if (e[0] == 0xE5)
  508. return 0;
  509. if ((e[11] & 0x18) == 0x08)
  510. return 0; /* volume label */
  511. trim_name_8_3((const char *)e, name_buf);
  512. size = (unsigned int)e[28] | ((unsigned int)e[29] << 8)
  513. | ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
  514. c->callback(name_buf, (e[11] & 0x10) ? 1 : 0, size);
  515. return 0;
  516. }
  517. int fat_list_dir(unsigned int dir_cluster, fat_root_callback_t callback)
  518. {
  519. struct list_ctx c;
  520. c.callback = callback;
  521. if (!fat_ready || !callback)
  522. return -1;
  523. return (dir_each_entry(dir_cluster, list_cb, &c) != 0) ? -2 : 0;
  524. }
  525. int fat_read_file_in(unsigned int dir_cluster, const char *name_8_3, void *buf, unsigned int max_len)
  526. {
  527. unsigned int cluster, size;
  528. unsigned char attr;
  529. unsigned int copied = 0;
  530. unsigned char *dst = buf;
  531. int r;
  532. if (!fat_ready || !buf || !name_8_3)
  533. return -1;
  534. r = fat_lookup(dir_cluster, (const unsigned char *)name_8_3, &cluster, &size, &attr, 0, 0);
  535. if (r == -2)
  536. return -2;
  537. if (r != 0 || (attr & 0x10))
  538. return -1;
  539. if (size > max_len)
  540. size = max_len;
  541. while (cluster >= 2 && cluster < FAT12_EOC_MIN && copied < size) {
  542. unsigned int sector = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
  543. unsigned int chunk = fat_sec_per_cluster * SECTOR_SIZE;
  544. unsigned int n_sec;
  545. if (chunk > size - copied)
  546. chunk = size - copied;
  547. n_sec = (chunk + SECTOR_SIZE - 1) / SECTOR_SIZE;
  548. if (disk_read_sectors(sector, dst + copied, n_sec) != 0)
  549. return -2;
  550. copied += chunk;
  551. if (copied >= size)
  552. break;
  553. if (fat_entry_read(cluster, &cluster) != 0)
  554. return -2;
  555. }
  556. return (int)copied;
  557. }
  558. int fat_write_file_in(unsigned int dir_cluster, const char *name_8_3, const void *buf, unsigned int size)
  559. {
  560. unsigned char name[11];
  561. unsigned int i;
  562. unsigned int first_cluster = 0, prev = 0, cluster;
  563. unsigned int remaining = size;
  564. const unsigned char *src = buf;
  565. unsigned int lba = 0, offset = 0, old_cluster = 0;
  566. unsigned char old_attr = 0;
  567. int r;
  568. if (!fat_ready || !name_8_3)
  569. return -1;
  570. for (i = 0; i < 11; i++)
  571. name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
  572. ? name_8_3[i] - 32 : name_8_3[i]);
  573. r = fat_lookup(dir_cluster, name, &old_cluster, 0, &old_attr, &lba, &offset);
  574. if (r == -2)
  575. return -2;
  576. if (r == 0 && (old_attr & 0x10)) {
  577. io_putstr("write: is a directory\n");
  578. return -1;
  579. }
  580. if (r != 0) {
  581. /* not found: find a free entry */
  582. r = dir_find_free(dir_cluster, &lba, &offset);
  583. if (r == -2)
  584. return -2;
  585. if (r != 0) {
  586. io_putstr("write: directory full\n");
  587. return -1;
  588. }
  589. } else if (old_cluster >= 2) {
  590. /* overwrite: free the old chain */
  591. fat_free_chain(old_cluster);
  592. }
  593. if (remaining > 0) {
  594. unsigned int cluster_size = fat_sec_per_cluster * SECTOR_SIZE;
  595. while (remaining > 0) {
  596. int c = fat_find_free_cluster();
  597. if (c == -2)
  598. return -2;
  599. if (c < 0) {
  600. if (first_cluster)
  601. fat_free_chain(first_cluster);
  602. io_putstr("write: disk full\n");
  603. return -1;
  604. }
  605. cluster = (unsigned int)c;
  606. if (first_cluster == 0)
  607. first_cluster = cluster;
  608. else if (fat_entry_write(prev, cluster) != 0)
  609. return -2;
  610. {
  611. unsigned int chunk = (remaining < cluster_size) ? remaining : cluster_size;
  612. unsigned int lba2 = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
  613. unsigned int full_secs = chunk / SECTOR_SIZE;
  614. unsigned int rem_bytes = chunk % SECTOR_SIZE;
  615. if (full_secs > 0) {
  616. if (disk_write_sectors(lba2, src, full_secs) != 0)
  617. return -2;
  618. src += full_secs * SECTOR_SIZE;
  619. lba2 += full_secs;
  620. }
  621. if (rem_bytes > 0) {
  622. unsigned int j;
  623. for (j = 0; j < rem_bytes; j++)
  624. sector_buf[j] = src[j];
  625. for (j = rem_bytes; j < SECTOR_SIZE; j++)
  626. sector_buf[j] = 0;
  627. if (disk_write_sectors(lba2, sector_buf, 1) != 0)
  628. return -2;
  629. src += rem_bytes;
  630. }
  631. remaining -= chunk;
  632. }
  633. prev = cluster;
  634. }
  635. if (fat_entry_write(prev, 0xFFF) != 0)
  636. return -2;
  637. }
  638. if (fat_dir_entry_write(lba, offset, name, 0x20, first_cluster, size) != 0)
  639. return -2;
  640. return 0;
  641. }
  642. int fat_delete_file_in(unsigned int dir_cluster, const char *name_8_3)
  643. {
  644. unsigned char name[11];
  645. unsigned int i, cluster = 0;
  646. unsigned char attr = 0;
  647. unsigned int lba, offset;
  648. int r;
  649. if (!fat_ready || !name_8_3)
  650. return -1;
  651. for (i = 0; i < 11; i++)
  652. name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
  653. ? name_8_3[i] - 32 : name_8_3[i]);
  654. r = fat_lookup(dir_cluster, name, &cluster, 0, &attr, &lba, &offset);
  655. if (r == -2)
  656. return -2;
  657. if (r != 0)
  658. return -1;
  659. if (attr & 0x10)
  660. return -1; /* it's a directory; use rmdir */
  661. if (cluster >= 2)
  662. fat_free_chain(cluster);
  663. return fat_mark_deleted(lba, offset);
  664. }
  665. static int dir_empty_cb(unsigned int lba, unsigned int offset, void *vctx)
  666. {
  667. int *nonempty = vctx;
  668. unsigned char *e = sector_buf + offset;
  669. (void)lba;
  670. if (e[0] == 0x00 || e[0] == 0xE5)
  671. return 0;
  672. if (e[0] == '.')
  673. return 0; /* "." or ".." */
  674. *nonempty = 1;
  675. return 1;
  676. }
  677. int fat_mkdir(unsigned int parent_cluster, const char *name_8_3)
  678. {
  679. unsigned char name[11];
  680. unsigned int i, cluster;
  681. unsigned int lba, offset;
  682. int r;
  683. if (!fat_ready || !name_8_3)
  684. return -1;
  685. for (i = 0; i < 11; i++)
  686. name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
  687. ? name_8_3[i] - 32 : name_8_3[i]);
  688. r = fat_lookup(parent_cluster, name, 0, 0, 0, 0, 0);
  689. if (r == -2)
  690. return -2;
  691. if (r == 0) {
  692. io_putstr("mkdir: already exists\n");
  693. return -1;
  694. }
  695. r = fat_find_free_cluster();
  696. if (r == -2)
  697. return -2;
  698. if (r < 0) {
  699. io_putstr("mkdir: disk full\n");
  700. return -1;
  701. }
  702. cluster = (unsigned int)r;
  703. /* Mark the cluster allocated (end of chain) so it's not reused. */
  704. if (fat_entry_write(cluster, 0xFFF) != 0)
  705. return -2;
  706. /* Write "." and ".." entries into the cluster's first sector. */
  707. {
  708. unsigned int base = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
  709. for (i = 0; i < SECTOR_SIZE; i++)
  710. sector_buf[i] = 0;
  711. sector_buf[0] = '.';
  712. for (i = 1; i < 11; i++)
  713. sector_buf[i] = ' ';
  714. sector_buf[11] = 0x10;
  715. sector_buf[26] = (unsigned char)(cluster & 0xFF);
  716. sector_buf[27] = (unsigned char)((cluster >> 8) & 0xFF);
  717. sector_buf[32] = '.';
  718. sector_buf[33] = '.';
  719. for (i = 34; i < 43; i++)
  720. sector_buf[i] = ' ';
  721. sector_buf[43] = 0x10;
  722. sector_buf[58] = (unsigned char)(parent_cluster & 0xFF);
  723. sector_buf[59] = (unsigned char)((parent_cluster >> 8) & 0xFF);
  724. if (disk_write_sectors(base, sector_buf, 1) != 0)
  725. return -2;
  726. }
  727. r = dir_find_free(parent_cluster, &lba, &offset);
  728. if (r == -2)
  729. return -2;
  730. if (r != 0) {
  731. fat_free_chain(cluster);
  732. io_putstr("mkdir: directory full\n");
  733. return -1;
  734. }
  735. return fat_dir_entry_write(lba, offset, name, 0x10, cluster, 0);
  736. }
  737. int fat_rmdir(unsigned int parent_cluster, const char *name_8_3)
  738. {
  739. unsigned char name[11];
  740. unsigned int i, cluster = 0;
  741. unsigned char attr = 0;
  742. unsigned int lba, offset;
  743. int r, nonempty = 0;
  744. if (!fat_ready || !name_8_3)
  745. return -1;
  746. for (i = 0; i < 11; i++)
  747. name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
  748. ? name_8_3[i] - 32 : name_8_3[i]);
  749. r = fat_lookup(parent_cluster, name, &cluster, 0, &attr, &lba, &offset);
  750. if (r == -2)
  751. return -2;
  752. if (r != 0)
  753. return -1;
  754. if (!(attr & 0x10)) {
  755. io_putstr("rmdir: not a directory\n");
  756. return -1;
  757. }
  758. if (dir_each_entry(cluster, dir_empty_cb, &nonempty) != 0)
  759. return -2;
  760. if (nonempty) {
  761. io_putstr("rmdir: directory not empty\n");
  762. return -1;
  763. }
  764. if (cluster >= 2)
  765. fat_free_chain(cluster);
  766. return fat_mark_deleted(lba, offset);
  767. }
  768. /* Resolve a path "a/b/c" into (parent dir cluster, final 8.3 name). */
  769. int fat_resolve_path(const char *path, unsigned int *dir_cluster, char name_8_3[12])
  770. {
  771. unsigned int dir = 0;
  772. const char *p = path;
  773. if (!fat_ready || !path || !dir_cluster || !name_8_3)
  774. return -1;
  775. for (;;) {
  776. const char *start;
  777. unsigned int len;
  778. while (*p == '/')
  779. p++;
  780. if (!*p)
  781. return -1; /* empty path */
  782. start = p;
  783. len = 0;
  784. while (p[len] && p[len] != '/')
  785. len++;
  786. if (p[len] == '/') {
  787. /* intermediate component: must be an existing directory */
  788. unsigned char name[11];
  789. unsigned int cluster, size;
  790. unsigned char attr;
  791. int r;
  792. fat_name_to_8_3(start, len, name);
  793. r = fat_lookup(dir, name, &cluster, &size, &attr, 0, 0);
  794. if (r == -2)
  795. return -2;
  796. if (r != 0 || !(attr & 0x10))
  797. return -1;
  798. dir = cluster;
  799. p += len + 1;
  800. } else {
  801. fat_name_to_8_3(start, len, (unsigned char *)name_8_3);
  802. name_8_3[11] = '\0';
  803. *dir_cluster = dir;
  804. return 0;
  805. }
  806. }
  807. }
  808. /* Resolve a full path (all components must be directories) to a directory
  809. * cluster. An empty path resolves to the root (cluster 0). */
  810. int fat_resolve_dir(const char *path, unsigned int *dir_cluster)
  811. {
  812. unsigned int dir = 0;
  813. const char *p = path;
  814. if (!fat_ready || !path || !dir_cluster)
  815. return -1;
  816. if (!*p) {
  817. *dir_cluster = 0;
  818. return 0;
  819. }
  820. for (;;) {
  821. const char *start;
  822. unsigned int len;
  823. while (*p == '/')
  824. p++;
  825. if (!*p)
  826. break; /* trailing slash */
  827. start = p;
  828. len = 0;
  829. while (p[len] && p[len] != '/')
  830. len++;
  831. {
  832. unsigned char name[11];
  833. unsigned int cluster, size;
  834. unsigned char attr;
  835. int r;
  836. fat_name_to_8_3(start, len, name);
  837. r = fat_lookup(dir, name, &cluster, &size, &attr, 0, 0);
  838. if (r == -2)
  839. return -2;
  840. if (r != 0 || !(attr & 0x10))
  841. return -1;
  842. dir = cluster;
  843. }
  844. if (!p[len])
  845. break;
  846. p += len + 1;
  847. }
  848. *dir_cluster = dir;
  849. return 0;
  850. }
  851. /* Root-directory wrappers (dir_cluster = 0). */
  852. int fat_write_file(const char *name_8_3, const void *buf, unsigned int size)
  853. {
  854. return fat_write_file_in(0, name_8_3, buf, size);
  855. }
  856. int fat_delete_file(const char *name_8_3)
  857. {
  858. return fat_delete_file_in(0, name_8_3);
  859. }