fat.c 5.4 KB

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