fat.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. /* Read cluster/size from a 32-byte directory entry at absolute byte offset. */
  324. static int fat_dir_entry_read(unsigned int byte_off, unsigned int *cluster, unsigned int *size)
  325. {
  326. unsigned int lba = byte_off / SECTOR_SIZE;
  327. unsigned int within = byte_off % SECTOR_SIZE;
  328. if (fat_read_sector(lba) != 0)
  329. return -2;
  330. if (cluster)
  331. *cluster = (unsigned int)sector_buf[within + 26] | ((unsigned int)sector_buf[within + 27] << 8);
  332. if (size)
  333. *size = (unsigned int)sector_buf[within + 28] | ((unsigned int)sector_buf[within + 29] << 8)
  334. | ((unsigned int)sector_buf[within + 30] << 16) | ((unsigned int)sector_buf[within + 31] << 24);
  335. return 0;
  336. }
  337. /* Write a 32-byte directory entry at absolute byte offset. */
  338. static int fat_dir_entry_write(unsigned int byte_off, const unsigned char name[11],
  339. unsigned char attr, unsigned int cluster, unsigned int size)
  340. {
  341. unsigned int lba = byte_off / SECTOR_SIZE;
  342. unsigned int within = byte_off % SECTOR_SIZE;
  343. unsigned int i;
  344. if (fat_read_sector(lba) != 0)
  345. return -2;
  346. for (i = 0; i < 11; i++)
  347. sector_buf[within + i] = name[i];
  348. sector_buf[within + 11] = attr;
  349. for (i = 12; i < 26; i++)
  350. sector_buf[within + i] = 0;
  351. sector_buf[within + 26] = (unsigned char)(cluster & 0xFF);
  352. sector_buf[within + 27] = (unsigned char)((cluster >> 8) & 0xFF);
  353. sector_buf[within + 28] = (unsigned char)(size & 0xFF);
  354. sector_buf[within + 29] = (unsigned char)((size >> 8) & 0xFF);
  355. sector_buf[within + 30] = (unsigned char)((size >> 16) & 0xFF);
  356. sector_buf[within + 31] = (unsigned char)((size >> 24) & 0xFF);
  357. return (disk_write_sectors(lba, sector_buf, 1) != 0) ? -2 : 0;
  358. }
  359. /* Find the byte offset of a root entry matching `name`, or -1 / -2. */
  360. static int fat_find_root_entry_off(const unsigned char name[11])
  361. {
  362. unsigned int sec, off;
  363. for (sec = 0; sec < fat_root_sectors; sec++) {
  364. if (fat_read_sector(fat_root_start + sec) != 0)
  365. return -2;
  366. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
  367. unsigned char *e = sector_buf + off;
  368. unsigned int i;
  369. if (e[0] == 0x00)
  370. return -1;
  371. if (e[0] == 0xE5 || (e[11] & 0x10))
  372. continue;
  373. for (i = 0; i < 11; i++)
  374. if ((e[i] >= 'a' ? e[i] - 32 : e[i]) != name[i])
  375. break;
  376. if (i == 11)
  377. return (int)((fat_root_start + sec) * SECTOR_SIZE + off);
  378. }
  379. }
  380. return -1;
  381. }
  382. /* Find a free (0x00 or 0xE5) root entry; return its byte offset, -1, or -2. */
  383. static int fat_find_free_root_entry(void)
  384. {
  385. unsigned int sec, off;
  386. for (sec = 0; sec < fat_root_sectors; sec++) {
  387. if (fat_read_sector(fat_root_start + sec) != 0)
  388. return -2;
  389. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
  390. if (sector_buf[off] == 0x00 || sector_buf[off] == 0xE5)
  391. return (int)((fat_root_start + sec) * SECTOR_SIZE + off);
  392. }
  393. }
  394. return -1;
  395. }
  396. int fat_write_file(const char *name_8_3, const void *buf, unsigned int size)
  397. {
  398. unsigned char name[11];
  399. unsigned int i;
  400. int byte_off;
  401. unsigned int first_cluster = 0, prev = 0, cluster;
  402. unsigned int remaining = size;
  403. const unsigned char *src = (const unsigned char *)buf;
  404. if (!fat_ready || !name_8_3)
  405. return -1;
  406. for (i = 0; i < 11; i++)
  407. name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
  408. ? name_8_3[i] - 32 : name_8_3[i]);
  409. byte_off = fat_find_root_entry_off(name);
  410. if (byte_off == -2)
  411. return -2;
  412. if (byte_off >= 0) {
  413. /* Overwrite: free the existing chain first. */
  414. unsigned int old_cluster = 0;
  415. if (fat_dir_entry_read((unsigned int)byte_off, &old_cluster, 0) != 0)
  416. return -2;
  417. if (old_cluster >= 2)
  418. fat_free_chain(old_cluster);
  419. } else {
  420. byte_off = fat_find_free_root_entry();
  421. if (byte_off == -2)
  422. return -2;
  423. if (byte_off < 0) {
  424. io_putstr("write: root directory full\n");
  425. return -1;
  426. }
  427. }
  428. /* Allocate clusters, link the chain, and write the data. */
  429. if (remaining > 0) {
  430. unsigned int cluster_size = fat_sec_per_cluster * SECTOR_SIZE;
  431. while (remaining > 0) {
  432. int c = fat_find_free_cluster();
  433. if (c == -2)
  434. return -2;
  435. if (c < 0) {
  436. if (first_cluster)
  437. fat_free_chain(first_cluster);
  438. io_putstr("write: disk full\n");
  439. return -1;
  440. }
  441. cluster = (unsigned int)c;
  442. if (first_cluster == 0)
  443. first_cluster = cluster;
  444. else if (fat_entry_write(prev, cluster) != 0)
  445. return -2;
  446. {
  447. unsigned int chunk = (remaining < cluster_size) ? remaining : cluster_size;
  448. unsigned int lba = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
  449. unsigned int full_secs = chunk / SECTOR_SIZE;
  450. unsigned int rem_bytes = chunk % SECTOR_SIZE;
  451. if (full_secs > 0) {
  452. if (disk_write_sectors(lba, src, full_secs) != 0)
  453. return -2;
  454. src += full_secs * SECTOR_SIZE;
  455. lba += full_secs;
  456. }
  457. if (rem_bytes > 0) {
  458. unsigned int j;
  459. for (j = 0; j < rem_bytes; j++)
  460. sector_buf[j] = src[j];
  461. for (j = rem_bytes; j < SECTOR_SIZE; j++)
  462. sector_buf[j] = 0;
  463. if (disk_write_sectors(lba, sector_buf, 1) != 0)
  464. return -2;
  465. src += rem_bytes;
  466. }
  467. remaining -= chunk;
  468. }
  469. prev = cluster;
  470. }
  471. if (fat_entry_write(prev, 0xFFF) != 0)
  472. return -2;
  473. }
  474. if (fat_dir_entry_write((unsigned int)byte_off, name, 0x20, first_cluster, size) != 0)
  475. return -2;
  476. return 0;
  477. }
  478. int fat_delete_file(const char *name_8_3)
  479. {
  480. unsigned char name[11];
  481. unsigned int i, cluster = 0;
  482. int byte_off;
  483. if (!fat_ready || !name_8_3)
  484. return -1;
  485. for (i = 0; i < 11; i++)
  486. name[i] = (unsigned char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z'
  487. ? name_8_3[i] - 32 : name_8_3[i]);
  488. byte_off = fat_find_root_entry_off(name);
  489. if (byte_off == -2)
  490. return -2;
  491. if (byte_off < 0)
  492. return -1;
  493. if (fat_dir_entry_read((unsigned int)byte_off, &cluster, 0) != 0)
  494. return -2;
  495. if (cluster >= 2)
  496. fat_free_chain(cluster);
  497. {
  498. unsigned int lba = (unsigned int)byte_off / SECTOR_SIZE;
  499. unsigned int within = (unsigned int)byte_off % SECTOR_SIZE;
  500. if (fat_read_sector(lba) != 0)
  501. return -2;
  502. sector_buf[within] = 0xE5;
  503. if (disk_write_sectors(lba, sector_buf, 1) != 0)
  504. return -2;
  505. }
  506. return 0;
  507. }