fat.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C) 2018 bzt (bztsrc@github)
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. #include "sd.h"
  26. #include "uart.h"
  27. // add memory compare, gcc has a built-in for that, clang needs implementation
  28. #ifdef __clang__
  29. int memcmp(void *s1, void *s2, int n)
  30. {
  31. unsigned char *a=s1,*b=s2;
  32. while(n-->0){ if(*a!=*b) { return *a-*b; } a++; b++; }
  33. return 0;
  34. }
  35. #else
  36. #define memcmp __builtin_memcmp
  37. #endif
  38. // get the end of bss segment from linker
  39. extern unsigned char _end;
  40. static unsigned int partitionlba = 0;
  41. // the BIOS Parameter Block (in Volume Boot Record)
  42. typedef struct {
  43. char jmp[3];
  44. char oem[8];
  45. unsigned char bps0;
  46. unsigned char bps1;
  47. unsigned char spc;
  48. unsigned short rsc;
  49. unsigned char nf;
  50. unsigned char nr0;
  51. unsigned char nr1;
  52. unsigned short ts16;
  53. unsigned char media;
  54. unsigned short spf16;
  55. unsigned short spt;
  56. unsigned short nh;
  57. unsigned int hs;
  58. unsigned int ts32;
  59. unsigned int spf32;
  60. unsigned int flg;
  61. unsigned int rc;
  62. char vol[6];
  63. char fst[8];
  64. char dmy[20];
  65. char fst2[8];
  66. } __attribute__((packed)) bpb_t;
  67. // directory entry structure
  68. typedef struct {
  69. char name[8];
  70. char ext[3];
  71. char attr[9];
  72. unsigned short ch;
  73. unsigned int attr2;
  74. unsigned short cl;
  75. unsigned int size;
  76. } __attribute__((packed)) fatdir_t;
  77. /**
  78. * Get the starting LBA address of the first partition
  79. * so that we know where our FAT file system starts, and
  80. * read that volume's BIOS Parameter Block
  81. */
  82. int fat_getpartition(void)
  83. {
  84. unsigned char *mbr=&_end;
  85. bpb_t *bpb=(bpb_t*)&_end;
  86. // read the partitioning table
  87. if(sd_readblock(0,&_end,1)) {
  88. // check magic
  89. if(mbr[510]!=0x55 || mbr[511]!=0xAA) {
  90. uart_puts("ERROR: Bad magic in MBR\n");
  91. return 0;
  92. }
  93. // check partition type
  94. if(mbr[0x1C2]!=0xE/*FAT16 LBA*/ && mbr[0x1C2]!=0xC/*FAT32 LBA*/) {
  95. uart_puts("ERROR: Wrong partition type\n");
  96. return 0;
  97. }
  98. // should be this, but compiler generates bad code...
  99. //partitionlba=*((unsigned int*)((unsigned long)&_end+0x1C6));
  100. partitionlba=mbr[0x1C6] + (mbr[0x1C7]<<8) + (mbr[0x1C8]<<16) + (mbr[0x1C9]<<24);
  101. // read the boot record
  102. if(!sd_readblock(partitionlba,&_end,1)) {
  103. uart_puts("ERROR: Unable to read boot record\n");
  104. return 0;
  105. }
  106. // check file system type. We don't use cluster numbers for that, but magic bytes
  107. if( !(bpb->fst[0]=='F' && bpb->fst[1]=='A' && bpb->fst[2]=='T') &&
  108. !(bpb->fst2[0]=='F' && bpb->fst2[1]=='A' && bpb->fst2[2]=='T')) {
  109. uart_puts("ERROR: Unknown file system type\n");
  110. return 0;
  111. }
  112. return 1;
  113. }
  114. return 0;
  115. }
  116. /**
  117. * Find a file in root directory entries
  118. */
  119. unsigned int fat_getcluster(char *fn)
  120. {
  121. bpb_t *bpb=(bpb_t*)&_end;
  122. fatdir_t *dir=(fatdir_t*)(&_end+512);
  123. unsigned int root_sec, s;
  124. // find the root directory's LBA
  125. root_sec=((bpb->spf16?bpb->spf16:bpb->spf32)*bpb->nf)+bpb->rsc;
  126. s = (bpb->nr0 + (bpb->nr1 << 8)) * sizeof(fatdir_t);
  127. if(bpb->spf16==0) {
  128. // adjust for FAT32
  129. root_sec+=(bpb->rc-2)*bpb->spc;
  130. }
  131. // add partition LBA
  132. root_sec+=partitionlba;
  133. // load the root directory
  134. if(sd_readblock(root_sec,(unsigned char*)dir,s/512+1)) {
  135. // iterate on each entry and check if it's the one we're looking for
  136. for(;dir->name[0]!=0;dir++) {
  137. // is it a valid entry?
  138. if(dir->name[0]==0xE5 || dir->attr[0]==0xF) continue;
  139. // filename match?
  140. if(!memcmp(dir->name,fn,11)) {
  141. uart_puts("FAT File ");
  142. uart_puts(fn);
  143. uart_puts(" starts at cluster: ");
  144. uart_hex(((unsigned int)dir->ch)<<16|dir->cl);
  145. uart_puts("\n");
  146. // if so, return starting cluster
  147. return ((unsigned int)dir->ch)<<16|dir->cl;
  148. }
  149. }
  150. uart_puts("ERROR: file not found\n");
  151. } else {
  152. uart_puts("ERROR: Unable to load root directory\n");
  153. }
  154. return 0;
  155. }
  156. /**
  157. * Read a file into memory
  158. */
  159. char *fat_readfile(unsigned int cluster)
  160. {
  161. // BIOS Parameter Block
  162. bpb_t *bpb=(bpb_t*)&_end;
  163. // File allocation tables. We choose between FAT16 and FAT32 dynamically
  164. unsigned int *fat32=(unsigned int*)(&_end+bpb->rsc*512);
  165. unsigned short *fat16=(unsigned short*)fat32;
  166. // Data pointers
  167. unsigned int data_sec, s;
  168. unsigned char *data, *ptr;
  169. // find the LBA of the first data sector
  170. data_sec=((bpb->spf16?bpb->spf16:bpb->spf32)*bpb->nf)+bpb->rsc;
  171. s = (bpb->nr0 + (bpb->nr1 << 8)) * sizeof(fatdir_t);
  172. if(bpb->spf16>0) {
  173. // adjust for FAT16
  174. data_sec+=(s+511)>>9;
  175. }
  176. // add partition LBA
  177. data_sec+=partitionlba;
  178. // dump important properties
  179. uart_puts("FAT Bytes per Sector: ");
  180. uart_hex(bpb->bps0 + (bpb->bps1 << 8));
  181. uart_puts("\nFAT Sectors per Cluster: ");
  182. uart_hex(bpb->spc);
  183. uart_puts("\nFAT Number of FAT: ");
  184. uart_hex(bpb->nf);
  185. uart_puts("\nFAT Sectors per FAT: ");
  186. uart_hex((bpb->spf16?bpb->spf16:bpb->spf32));
  187. uart_puts("\nFAT Reserved Sectors Count: ");
  188. uart_hex(bpb->rsc);
  189. uart_puts("\nFAT First data sector: ");
  190. uart_hex(data_sec);
  191. uart_puts("\n");
  192. // load FAT table
  193. s=sd_readblock(partitionlba+1,(unsigned char*)&_end+512,(bpb->spf16?bpb->spf16:bpb->spf32)+bpb->rsc);
  194. // end of FAT in memory
  195. data=ptr=&_end+512+s;
  196. // iterate on cluster chain
  197. // (Yep, MS is full of lies. FAT32 is actually FAT28 only, no mistake, the upper 4 bits must be zero)
  198. while(cluster>1 && cluster<(bpb->spf16>0?0xFFF8:0x0FFFFFF8)) {
  199. // load all sectors in a cluster
  200. sd_readblock((cluster-2)*bpb->spc+data_sec,ptr,bpb->spc);
  201. // move pointer, sector per cluster * bytes per sector
  202. ptr+=bpb->spc*(bpb->bps0 + (bpb->bps1 << 8));
  203. // get the next cluster in chain
  204. cluster=bpb->spf16>0?fat16[cluster]:fat32[cluster];
  205. }
  206. return (char*)data;
  207. }