fat.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* FAT12 read-only: BPB parse, root list, read file by following FAT chain. */
  2. #include "disk.h"
  3. #include "fat.h"
  4. #define SECTOR_SIZE 512
  5. #define ROOT_ENTRY_SIZE 32
  6. #define FAT12_EOC_MIN 0xFF8
  7. /* BPB offsets (same for FAT12 1.44MB floppy) */
  8. #define BPB_BYTES_PER_SEC 0x0B
  9. #define BPB_SEC_PER_CLUSTER 0x0D
  10. #define BPB_RESERVED 0x0E
  11. #define BPB_NUM_FATS 0x10
  12. #define BPB_ROOT_ENTRIES 0x11
  13. #define BPB_SEC_PER_FAT 0x16
  14. static unsigned int fat_reserved;
  15. static unsigned int fat_root_start;
  16. static unsigned int fat_root_sectors;
  17. static unsigned int fat_data_start;
  18. static unsigned int fat_sec_per_cluster;
  19. static int fat_ready;
  20. static unsigned char sector_buf[SECTOR_SIZE];
  21. static int fat_read_sector(unsigned int lba)
  22. {
  23. if (disk_read_sectors(lba, sector_buf, 1) != 0)
  24. return -1;
  25. return 0;
  26. }
  27. /* Get 12-bit FAT entry at cluster index. FAT is in sector_buf (one sector). */
  28. static unsigned int fat_get_entry(unsigned int cluster_idx)
  29. {
  30. unsigned int byte_off = (cluster_idx * 3) / 2;
  31. unsigned int word_val;
  32. if (byte_off + 2 > SECTOR_SIZE)
  33. return 0xFFFF;
  34. word_val = (unsigned int)sector_buf[byte_off] | ((unsigned int)sector_buf[byte_off + 1] << 8);
  35. if (cluster_idx & 1)
  36. return word_val >> 4;
  37. return word_val & 0xFFF;
  38. }
  39. int fat_init(void)
  40. {
  41. unsigned int num_fats, root_entries, sec_per_fat;
  42. if (fat_read_sector(0) != 0)
  43. return -1;
  44. fat_reserved = (unsigned int)sector_buf[BPB_RESERVED] | ((unsigned int)sector_buf[BPB_RESERVED + 1] << 8);
  45. num_fats = sector_buf[BPB_NUM_FATS];
  46. root_entries = (unsigned int)sector_buf[BPB_ROOT_ENTRIES] | ((unsigned int)sector_buf[BPB_ROOT_ENTRIES + 1] << 8);
  47. sec_per_fat = (unsigned int)sector_buf[BPB_SEC_PER_FAT] | ((unsigned int)sector_buf[BPB_SEC_PER_FAT + 1] << 8);
  48. fat_sec_per_cluster = sector_buf[BPB_SEC_PER_CLUSTER];
  49. fat_root_start = fat_reserved + num_fats * sec_per_fat;
  50. fat_root_sectors = (root_entries * ROOT_ENTRY_SIZE + SECTOR_SIZE - 1) / SECTOR_SIZE;
  51. fat_data_start = fat_root_start + fat_root_sectors;
  52. fat_ready = 1;
  53. return 0;
  54. }
  55. static void trim_name_8_3(const char *in, char *out)
  56. {
  57. unsigned int i;
  58. for (i = 0; i < 8 && in[i] && in[i] != ' '; i++)
  59. out[i] = (char)(in[i] >= 'a' && in[i] <= 'z' ? in[i] - 32 : in[i]);
  60. for (; i < 8; i++)
  61. out[i] = ' ';
  62. for (i = 0; i < 3 && in[8 + i] && in[8 + i] != ' '; i++)
  63. out[8 + i] = (char)(in[8 + i] >= 'a' && in[8 + i] <= 'z' ? in[8 + i] - 32 : in[8 + i]);
  64. for (; i < 3; i++)
  65. out[8 + i] = ' ';
  66. out[11] = '\0';
  67. }
  68. int fat_list_root(fat_root_callback_t callback)
  69. {
  70. unsigned int sec, off;
  71. char name_buf[12];
  72. if (!fat_ready || !callback)
  73. return -1;
  74. for (sec = 0; sec < fat_root_sectors; sec++) {
  75. if (fat_read_sector(fat_root_start + sec) != 0)
  76. return -2;
  77. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
  78. unsigned char *e = sector_buf + off;
  79. if (e[0] == 0x00)
  80. return 0;
  81. if (e[0] == 0xE5)
  82. continue;
  83. if ((e[11] & 0x18) == 0x08)
  84. continue;
  85. trim_name_8_3((const char *)e, name_buf);
  86. {
  87. unsigned int size = (unsigned int)e[28] | ((unsigned int)e[29] << 8) |
  88. ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
  89. (*callback)(name_buf, (e[11] & 0x10) ? 1 : 0, size);
  90. }
  91. }
  92. }
  93. return 0;
  94. }
  95. /* Find root entry by 8.3 name; return first cluster and size, or -1. */
  96. static int fat_find_root(const char *name_8_3, unsigned int *out_cluster, unsigned int *out_size)
  97. {
  98. unsigned int sec, off;
  99. char want[12];
  100. unsigned int i;
  101. for (i = 0; i < 11; i++)
  102. want[i] = (char)(name_8_3[i] >= 'a' && name_8_3[i] <= 'z' ? name_8_3[i] - 32 : name_8_3[i]);
  103. want[11] = '\0';
  104. for (sec = 0; sec < fat_root_sectors; sec++) {
  105. if (fat_read_sector(fat_root_start + sec) != 0)
  106. return -2;
  107. for (off = 0; off < SECTOR_SIZE; off += ROOT_ENTRY_SIZE) {
  108. unsigned char *e = sector_buf + off;
  109. if (e[0] == 0x00)
  110. return -1;
  111. if (e[0] == 0xE5 || (e[11] & 0x10))
  112. continue;
  113. for (i = 0; i < 11; i++)
  114. if ((e[i] >= 'a' ? e[i] - 32 : e[i]) != (unsigned char)want[i])
  115. break;
  116. if (i < 11)
  117. continue;
  118. *out_cluster = (unsigned int)e[26] | ((unsigned int)e[27] << 8);
  119. *out_size = (unsigned int)e[28] | ((unsigned int)e[29] << 8) |
  120. ((unsigned int)e[30] << 16) | ((unsigned int)e[31] << 24);
  121. return 0;
  122. }
  123. }
  124. return -1;
  125. }
  126. int fat_read_file(const char *name_8_3, void *buf, unsigned int max_len)
  127. {
  128. unsigned int cluster, size;
  129. unsigned int fat_start = fat_reserved;
  130. unsigned int copied = 0;
  131. unsigned char *dst = (unsigned char *)buf;
  132. if (!fat_ready || !buf)
  133. return -1;
  134. {
  135. int fr = fat_find_root(name_8_3, &cluster, &size);
  136. if (fr == -2)
  137. return -2;
  138. if (fr != 0)
  139. return -1;
  140. }
  141. if (size > max_len)
  142. size = max_len;
  143. while (cluster >= 2 && cluster < 0xFF8 && copied < size) {
  144. unsigned int sector = fat_data_start + (cluster - 2) * fat_sec_per_cluster;
  145. unsigned int chunk = fat_sec_per_cluster * SECTOR_SIZE;
  146. unsigned int n_sec;
  147. if (chunk > size - copied)
  148. chunk = size - copied;
  149. n_sec = (chunk + SECTOR_SIZE - 1) / SECTOR_SIZE;
  150. if (disk_read_sectors(sector, dst + copied, n_sec) != 0)
  151. return -2;
  152. copied += chunk;
  153. if (copied >= size)
  154. break;
  155. /* Next cluster from FAT */
  156. if (fat_read_sector(fat_start + (cluster * 3) / (2 * SECTOR_SIZE)) != 0)
  157. return -2;
  158. cluster = fat_get_entry(cluster);
  159. }
  160. return (int)copied;
  161. }