fat.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* FAT12 read-only: BPB parse, root list, read file 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_SIGNATURE 0x1FE /* 0x55 0xAA at end of boot sector */
  16. static unsigned int fat_reserved;
  17. static unsigned int fat_root_start;
  18. static unsigned int fat_root_sectors;
  19. static unsigned int fat_data_start;
  20. static unsigned int fat_sec_per_cluster;
  21. static int fat_ready;
  22. static unsigned char sector_buf[SECTOR_SIZE];
  23. static int fat_read_sector(unsigned int lba)
  24. {
  25. if (disk_read_sectors(lba, sector_buf, 1) != 0)
  26. return -1;
  27. return 0;
  28. }
  29. /* Get 12-bit FAT entry at cluster index. FAT is in sector_buf (one sector). */
  30. static unsigned int fat_get_entry(unsigned int cluster_idx)
  31. {
  32. unsigned int byte_off = (cluster_idx * 3) / 2;
  33. unsigned int word_val;
  34. if (byte_off + 2 > SECTOR_SIZE)
  35. return 0xFFFF;
  36. word_val = (unsigned int)sector_buf[byte_off] | ((unsigned int)sector_buf[byte_off + 1] << 8);
  37. if (cluster_idx & 1)
  38. return word_val >> 4;
  39. return word_val & 0xFFF;
  40. }
  41. int fat_init(void)
  42. {
  43. unsigned int num_fats, root_entries, sec_per_fat;
  44. if (fat_read_sector(0) != 0) {
  45. io_putstr("run: disk read error (boot sector LBA=0)\n");
  46. return -2; /* disk I/O error */
  47. }
  48. if (sector_buf[BPB_SIGNATURE] != 0x55 || sector_buf[BPB_SIGNATURE + 1] != 0xAA)
  49. return -1; /* no valid FAT filesystem */
  50. fat_reserved = (unsigned int)sector_buf[BPB_RESERVED] | ((unsigned int)sector_buf[BPB_RESERVED + 1] << 8);
  51. num_fats = sector_buf[BPB_NUM_FATS];
  52. root_entries = (unsigned int)sector_buf[BPB_ROOT_ENTRIES] | ((unsigned int)sector_buf[BPB_ROOT_ENTRIES + 1] << 8);
  53. sec_per_fat = (unsigned int)sector_buf[BPB_SEC_PER_FAT] | ((unsigned int)sector_buf[BPB_SEC_PER_FAT + 1] << 8);
  54. fat_sec_per_cluster = sector_buf[BPB_SEC_PER_CLUSTER];
  55. fat_root_start = fat_reserved + num_fats * sec_per_fat;
  56. fat_root_sectors = (root_entries * ROOT_ENTRY_SIZE + SECTOR_SIZE - 1) / SECTOR_SIZE;
  57. fat_data_start = fat_root_start + fat_root_sectors;
  58. fat_ready = 1;
  59. return 0;
  60. }
  61. static void trim_name_8_3(const char *in, char *out)
  62. {
  63. unsigned int i;
  64. for (i = 0; i < 8 && in[i] && in[i] != ' '; i++)
  65. out[i] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  66. for (; i < 8; i++)
  67. out[i] = ' ';
  68. for (i = 0; i < 3 && in[8 + i] && in[8 + i] != ' '; i++)
  69. out[8 + i] = (char)(in[8 + i] >= 'a' && in[8 + i] <= 'z' ? in[8 + i] - 32 : in[8 + i]);
  70. for (; i < 3; i++)
  71. out[8 + i] = ' ';
  72. out[11] = '\0';
  73. }
  74. int fat_list_root(fat_root_callback_t callback)
  75. {
  76. unsigned int sec, off;
  77. char name_buf[12];
  78. if (!fat_ready || !callback)
  79. return -1;
  80. for (sec = 0; sec < fat_root_sectors; sec++) {
  81. if (fat_read_sector(fat_root_start + sec) != 0) {
  82. io_putstr("run: disk read error (root dir sec=");
  83. io_puthex(sec);
  84. io_putstr(" LBA=");
  85. io_puthex(fat_root_start + sec);
  86. io_putstr(")\n");
  87. return -2;
  88. }
  89. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
  90. unsigned char *e = sector_buf + off;
  91. if (e[0] == 0x00)
  92. return 0;
  93. if (e[0] == 0xE5)
  94. continue;
  95. if ((e[11] & 0x18) == 0x08)
  96. continue;
  97. trim_name_8_3((const char *)e, name_buf);
  98. {
  99. unsigned int size = (unsigned int)e[28] | ((unsigned int)e[29] << 8) |
  100. ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
  101. (*callback)(name_buf, (e[11] & 0x10) ? 1 : 0, size);
  102. }
  103. }
  104. }
  105. return 0;
  106. }
  107. /* Find root entry by 8.3 name; return first cluster and size, or -1. */
  108. static int fat_find_root(const char *name_8_3, unsigned int *out_cluster, unsigned int *out_size)
  109. {
  110. unsigned int sec, off;
  111. char want[12];
  112. unsigned int i;
  113. for (i = 0; i < 11; i++)
  114. want[i] = (char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z' ? name_8_3[i] - 32 : name_8_3[i]);
  115. want[11] = '\0';
  116. for (sec = 0; sec < fat_root_sectors; sec++) {
  117. if (fat_read_sector(fat_root_start + sec) != 0) {
  118. io_putstr("run: disk read error (find root sec=");
  119. io_puthex(sec);
  120. io_putstr(" LBA=");
  121. io_puthex(fat_root_start + sec);
  122. io_putstr(")\n");
  123. return -2;
  124. }
  125. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
  126. unsigned char *e = sector_buf + off;
  127. if (e[0] == 0x00)
  128. return -1;
  129. if (e[0] == 0xE5 || (e[11] & 0x10))
  130. continue;
  131. for (i = 0; i < 11; i++)
  132. if ((e[i] >= 'a' ? e[i] - 32 : e[i]) != (unsigned char)want[i])
  133. break;
  134. if (i < 11)
  135. continue;
  136. *out_cluster = (unsigned int)e[26] | ((unsigned int)e[27] << 8);
  137. *out_size = (unsigned int)e[28] | ((unsigned int)e[29] << 8) |
  138. ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
  139. return 0;
  140. }
  141. }
  142. return -1;
  143. }
  144. int fat_read_file(const char *name_8_3, void *buf, unsigned int max_len)
  145. {
  146. unsigned int cluster, size;
  147. unsigned int fat_start = fat_reserved;
  148. unsigned int copied = 0;
  149. unsigned char *dst = (unsigned char *)buf;
  150. unsigned int loaded_fat_sec;
  151. if (!fat_ready || !buf)
  152. return -1;
  153. {
  154. int fr = fat_find_root(name_8_3, &cluster, &size);
  155. if (fr == -2)
  156. return -2;
  157. if (fr != 0)
  158. return -1;
  159. }
  160. if (size > max_len)
  161. size = max_len;
  162. /* Pre-load the FAT sector covering the first cluster so we can follow
  163. * the chain without re-reading the FAT between every data cluster.
  164. * Data reads use DMA + memcpy to dst and never touch sector_buf,
  165. * so the cached FAT data stays valid throughout. */
  166. loaded_fat_sec = fat_start + (cluster * 3) / (2 * SECTOR_SIZE);
  167. if (fat_read_sector(loaded_fat_sec) != 0) {
  168. io_putstr("run: disk read error (FAT preload LBA=");
  169. io_puthex(loaded_fat_sec);
  170. io_putstr(")\n");
  171. return -2;
  172. }
  173. while (cluster >= 2 && cluster < 0xFF8 && copied < size) {
  174. unsigned int sector = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
  175. unsigned int chunk = fat_sec_per_cluster * SECTOR_SIZE;
  176. unsigned int n_sec;
  177. if (chunk > size - copied)
  178. chunk = size - copied;
  179. n_sec = (chunk + SECTOR_SIZE - 1) / SECTOR_SIZE;
  180. if (disk_read_sectors(sector, dst + copied, n_sec) != 0) {
  181. io_putstr("run: disk read error (file data cluster=");
  182. io_puthex(cluster);
  183. io_putstr(" LBA=");
  184. io_puthex(sector);
  185. io_putstr(" n_sec=");
  186. io_puthex(n_sec);
  187. io_putstr(")\n");
  188. return -2;
  189. }
  190. copied += chunk;
  191. if (copied >= size)
  192. break;
  193. /* Only re-read FAT if the chain crosses into a different FAT sector. */
  194. {
  195. unsigned int need_fat_sec = fat_start + (cluster * 3) / (2 * SECTOR_SIZE);
  196. if (need_fat_sec != loaded_fat_sec) {
  197. if (fat_read_sector(need_fat_sec) != 0) {
  198. io_putstr("run: disk read error (FAT cluster=");
  199. io_puthex(cluster);
  200. io_putstr(" LBA=");
  201. io_puthex(need_fat_sec);
  202. io_putstr(")\n");
  203. return -2;
  204. }
  205. loaded_fat_sec = need_fat_sec;
  206. }
  207. }
  208. cluster = fat_get_entry(cluster);
  209. }
  210. return (int)copied;
  211. }