fat.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290
  1. /*
  2. * fat.c
  3. *
  4. * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
  5. *
  6. * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
  7. * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <config.h>
  13. #include <exports.h>
  14. #include <fat.h>
  15. #include <asm/byteorder.h>
  16. #include <part.h>
  17. #include <malloc.h>
  18. #include <linux/compiler.h>
  19. #include <linux/ctype.h>
  20. #ifdef CONFIG_SUPPORT_VFAT
  21. static const int vfat_enabled = 1;
  22. #else
  23. static const int vfat_enabled = 0;
  24. #endif
  25. /*
  26. * Convert a string to lowercase.
  27. */
  28. static void downcase(char *str)
  29. {
  30. while (*str != '\0') {
  31. *str = tolower(*str);
  32. str++;
  33. }
  34. }
  35. static block_dev_desc_t *cur_dev;
  36. static disk_partition_t cur_part_info;
  37. #define DOS_BOOT_MAGIC_OFFSET 0x1fe
  38. #define DOS_FS_TYPE_OFFSET 0x36
  39. #define DOS_FS32_TYPE_OFFSET 0x52
  40. static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
  41. {
  42. if (!cur_dev || !cur_dev->block_read)
  43. return -1;
  44. return cur_dev->block_read(cur_dev->dev,
  45. cur_part_info.start + block, nr_blocks, buf);
  46. }
  47. int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info)
  48. {
  49. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
  50. cur_dev = dev_desc;
  51. cur_part_info = *info;
  52. /* Make sure it has a valid FAT header */
  53. if (disk_read(0, 1, buffer) != 1) {
  54. cur_dev = NULL;
  55. return -1;
  56. }
  57. /* Check if it's actually a DOS volume */
  58. if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
  59. cur_dev = NULL;
  60. return -1;
  61. }
  62. /* Check for FAT12/FAT16/FAT32 filesystem */
  63. if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
  64. return 0;
  65. if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
  66. return 0;
  67. cur_dev = NULL;
  68. return -1;
  69. }
  70. int fat_register_device(block_dev_desc_t *dev_desc, int part_no)
  71. {
  72. disk_partition_t info;
  73. /* First close any currently found FAT filesystem */
  74. cur_dev = NULL;
  75. /* Read the partition table, if present */
  76. if (get_partition_info(dev_desc, part_no, &info)) {
  77. if (part_no != 0) {
  78. printf("** Partition %d not valid on device %d **\n",
  79. part_no, dev_desc->dev);
  80. return -1;
  81. }
  82. info.start = 0;
  83. info.size = dev_desc->lba;
  84. info.blksz = dev_desc->blksz;
  85. info.name[0] = 0;
  86. info.type[0] = 0;
  87. info.bootable = 0;
  88. #ifdef CONFIG_PARTITION_UUIDS
  89. info.uuid[0] = 0;
  90. #endif
  91. }
  92. return fat_set_blk_dev(dev_desc, &info);
  93. }
  94. /*
  95. * Get the first occurence of a directory delimiter ('/' or '\') in a string.
  96. * Return index into string if found, -1 otherwise.
  97. */
  98. static int dirdelim(char *str)
  99. {
  100. char *start = str;
  101. while (*str != '\0') {
  102. if (ISDIRDELIM(*str))
  103. return str - start;
  104. str++;
  105. }
  106. return -1;
  107. }
  108. /*
  109. * Extract zero terminated short name from a directory entry.
  110. */
  111. static void get_name(dir_entry *dirent, char *s_name)
  112. {
  113. char *ptr;
  114. memcpy(s_name, dirent->name, 8);
  115. s_name[8] = '\0';
  116. ptr = s_name;
  117. while (*ptr && *ptr != ' ')
  118. ptr++;
  119. if (dirent->ext[0] && dirent->ext[0] != ' ') {
  120. *ptr = '.';
  121. ptr++;
  122. memcpy(ptr, dirent->ext, 3);
  123. ptr[3] = '\0';
  124. while (*ptr && *ptr != ' ')
  125. ptr++;
  126. }
  127. *ptr = '\0';
  128. if (*s_name == DELETED_FLAG)
  129. *s_name = '\0';
  130. else if (*s_name == aRING)
  131. *s_name = DELETED_FLAG;
  132. downcase(s_name);
  133. }
  134. /*
  135. * Get the entry at index 'entry' in a FAT (12/16/32) table.
  136. * On failure 0x00 is returned.
  137. */
  138. static __u32 get_fatent(fsdata *mydata, __u32 entry)
  139. {
  140. __u32 bufnum;
  141. __u32 off16, offset;
  142. __u32 ret = 0x00;
  143. __u16 val1, val2;
  144. switch (mydata->fatsize) {
  145. case 32:
  146. bufnum = entry / FAT32BUFSIZE;
  147. offset = entry - bufnum * FAT32BUFSIZE;
  148. break;
  149. case 16:
  150. bufnum = entry / FAT16BUFSIZE;
  151. offset = entry - bufnum * FAT16BUFSIZE;
  152. break;
  153. case 12:
  154. bufnum = entry / FAT12BUFSIZE;
  155. offset = entry - bufnum * FAT12BUFSIZE;
  156. break;
  157. default:
  158. /* Unsupported FAT size */
  159. return ret;
  160. }
  161. debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
  162. mydata->fatsize, entry, entry, offset, offset);
  163. /* Read a new block of FAT entries into the cache. */
  164. if (bufnum != mydata->fatbufnum) {
  165. __u32 getsize = FATBUFBLOCKS;
  166. __u8 *bufptr = mydata->fatbuf;
  167. __u32 fatlength = mydata->fatlength;
  168. __u32 startblock = bufnum * FATBUFBLOCKS;
  169. if (startblock + getsize > fatlength)
  170. getsize = fatlength - startblock;
  171. startblock += mydata->fat_sect; /* Offset from start of disk */
  172. if (disk_read(startblock, getsize, bufptr) < 0) {
  173. debug("Error reading FAT blocks\n");
  174. return ret;
  175. }
  176. mydata->fatbufnum = bufnum;
  177. }
  178. /* Get the actual entry from the table */
  179. switch (mydata->fatsize) {
  180. case 32:
  181. ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
  182. break;
  183. case 16:
  184. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
  185. break;
  186. case 12:
  187. off16 = (offset * 3) / 4;
  188. switch (offset & 0x3) {
  189. case 0:
  190. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
  191. ret &= 0xfff;
  192. break;
  193. case 1:
  194. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  195. val1 &= 0xf000;
  196. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  197. val2 &= 0x00ff;
  198. ret = (val2 << 4) | (val1 >> 12);
  199. break;
  200. case 2:
  201. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  202. val1 &= 0xff00;
  203. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  204. val2 &= 0x000f;
  205. ret = (val2 << 8) | (val1 >> 8);
  206. break;
  207. case 3:
  208. ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  209. ret = (ret & 0xfff0) >> 4;
  210. break;
  211. default:
  212. break;
  213. }
  214. break;
  215. }
  216. debug("FAT%d: ret: %08x, offset: %04x\n",
  217. mydata->fatsize, ret, offset);
  218. return ret;
  219. }
  220. /*
  221. * Read at most 'size' bytes from the specified cluster into 'buffer'.
  222. * Return 0 on success, -1 otherwise.
  223. */
  224. static int
  225. get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
  226. {
  227. __u32 idx = 0;
  228. __u32 startsect;
  229. int ret;
  230. if (clustnum > 0) {
  231. startsect = mydata->data_begin +
  232. clustnum * mydata->clust_size;
  233. } else {
  234. startsect = mydata->rootdir_sect;
  235. }
  236. debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
  237. if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
  238. ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
  239. printf("FAT: Misaligned buffer address (%p)\n", buffer);
  240. while (size >= mydata->sect_size) {
  241. ret = disk_read(startsect++, 1, tmpbuf);
  242. if (ret != 1) {
  243. debug("Error reading data (got %d)\n", ret);
  244. return -1;
  245. }
  246. memcpy(buffer, tmpbuf, mydata->sect_size);
  247. buffer += mydata->sect_size;
  248. size -= mydata->sect_size;
  249. }
  250. } else {
  251. idx = size / mydata->sect_size;
  252. ret = disk_read(startsect, idx, buffer);
  253. if (ret != idx) {
  254. debug("Error reading data (got %d)\n", ret);
  255. return -1;
  256. }
  257. startsect += idx;
  258. idx *= mydata->sect_size;
  259. buffer += idx;
  260. size -= idx;
  261. }
  262. if (size) {
  263. ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
  264. ret = disk_read(startsect, 1, tmpbuf);
  265. if (ret != 1) {
  266. debug("Error reading data (got %d)\n", ret);
  267. return -1;
  268. }
  269. memcpy(buffer, tmpbuf, size);
  270. }
  271. return 0;
  272. }
  273. /*
  274. * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
  275. * into 'buffer'.
  276. * Update the number of bytes read in *gotsize or return -1 on fatal errors.
  277. */
  278. __u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
  279. __aligned(ARCH_DMA_MINALIGN);
  280. static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
  281. __u8 *buffer, loff_t maxsize, loff_t *gotsize)
  282. {
  283. loff_t filesize = FAT2CPU32(dentptr->size);
  284. unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
  285. __u32 curclust = START(dentptr);
  286. __u32 endclust, newclust;
  287. loff_t actsize;
  288. *gotsize = 0;
  289. debug("Filesize: %llu bytes\n", filesize);
  290. if (pos >= filesize) {
  291. debug("Read position past EOF: %llu\n", pos);
  292. return 0;
  293. }
  294. if (maxsize > 0 && filesize > pos + maxsize)
  295. filesize = pos + maxsize;
  296. debug("%llu bytes\n", filesize);
  297. actsize = bytesperclust;
  298. /* go to cluster at pos */
  299. while (actsize <= pos) {
  300. curclust = get_fatent(mydata, curclust);
  301. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  302. debug("curclust: 0x%x\n", curclust);
  303. debug("Invalid FAT entry\n");
  304. return 0;
  305. }
  306. actsize += bytesperclust;
  307. }
  308. /* actsize > pos */
  309. actsize -= bytesperclust;
  310. filesize -= actsize;
  311. pos -= actsize;
  312. /* align to beginning of next cluster if any */
  313. if (pos) {
  314. actsize = min(filesize, (loff_t)bytesperclust);
  315. if (get_cluster(mydata, curclust, get_contents_vfatname_block,
  316. (int)actsize) != 0) {
  317. printf("Error reading cluster\n");
  318. return -1;
  319. }
  320. filesize -= actsize;
  321. actsize -= pos;
  322. memcpy(buffer, get_contents_vfatname_block + pos, actsize);
  323. *gotsize += actsize;
  324. if (!filesize)
  325. return 0;
  326. buffer += actsize;
  327. curclust = get_fatent(mydata, curclust);
  328. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  329. debug("curclust: 0x%x\n", curclust);
  330. debug("Invalid FAT entry\n");
  331. return 0;
  332. }
  333. }
  334. actsize = bytesperclust;
  335. endclust = curclust;
  336. do {
  337. /* search for consecutive clusters */
  338. while (actsize < filesize) {
  339. newclust = get_fatent(mydata, endclust);
  340. if ((newclust - 1) != endclust)
  341. goto getit;
  342. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  343. debug("curclust: 0x%x\n", newclust);
  344. debug("Invalid FAT entry\n");
  345. return 0;
  346. }
  347. endclust = newclust;
  348. actsize += bytesperclust;
  349. }
  350. /* get remaining bytes */
  351. actsize = filesize;
  352. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  353. printf("Error reading cluster\n");
  354. return -1;
  355. }
  356. *gotsize += actsize;
  357. return 0;
  358. getit:
  359. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  360. printf("Error reading cluster\n");
  361. return -1;
  362. }
  363. *gotsize += (int)actsize;
  364. filesize -= actsize;
  365. buffer += actsize;
  366. curclust = get_fatent(mydata, endclust);
  367. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  368. debug("curclust: 0x%x\n", curclust);
  369. printf("Invalid FAT entry\n");
  370. return 0;
  371. }
  372. actsize = bytesperclust;
  373. endclust = curclust;
  374. } while (1);
  375. }
  376. /*
  377. * Extract the file name information from 'slotptr' into 'l_name',
  378. * starting at l_name[*idx].
  379. * Return 1 if terminator (zero byte) is found, 0 otherwise.
  380. */
  381. static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
  382. {
  383. int j;
  384. for (j = 0; j <= 8; j += 2) {
  385. l_name[*idx] = slotptr->name0_4[j];
  386. if (l_name[*idx] == 0x00)
  387. return 1;
  388. (*idx)++;
  389. }
  390. for (j = 0; j <= 10; j += 2) {
  391. l_name[*idx] = slotptr->name5_10[j];
  392. if (l_name[*idx] == 0x00)
  393. return 1;
  394. (*idx)++;
  395. }
  396. for (j = 0; j <= 2; j += 2) {
  397. l_name[*idx] = slotptr->name11_12[j];
  398. if (l_name[*idx] == 0x00)
  399. return 1;
  400. (*idx)++;
  401. }
  402. return 0;
  403. }
  404. /*
  405. * Extract the full long filename starting at 'retdent' (which is really
  406. * a slot) into 'l_name'. If successful also copy the real directory entry
  407. * into 'retdent'
  408. * Return 0 on success, -1 otherwise.
  409. */
  410. static int
  411. get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
  412. dir_entry *retdent, char *l_name)
  413. {
  414. dir_entry *realdent;
  415. dir_slot *slotptr = (dir_slot *)retdent;
  416. __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
  417. PREFETCH_BLOCKS :
  418. mydata->clust_size);
  419. __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
  420. int idx = 0;
  421. if (counter > VFAT_MAXSEQ) {
  422. debug("Error: VFAT name is too long\n");
  423. return -1;
  424. }
  425. while ((__u8 *)slotptr < buflimit) {
  426. if (counter == 0)
  427. break;
  428. if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
  429. return -1;
  430. slotptr++;
  431. counter--;
  432. }
  433. if ((__u8 *)slotptr >= buflimit) {
  434. dir_slot *slotptr2;
  435. if (curclust == 0)
  436. return -1;
  437. curclust = get_fatent(mydata, curclust);
  438. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  439. debug("curclust: 0x%x\n", curclust);
  440. printf("Invalid FAT entry\n");
  441. return -1;
  442. }
  443. if (get_cluster(mydata, curclust, get_contents_vfatname_block,
  444. mydata->clust_size * mydata->sect_size) != 0) {
  445. debug("Error: reading directory block\n");
  446. return -1;
  447. }
  448. slotptr2 = (dir_slot *)get_contents_vfatname_block;
  449. while (counter > 0) {
  450. if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
  451. & 0xff) != counter)
  452. return -1;
  453. slotptr2++;
  454. counter--;
  455. }
  456. /* Save the real directory entry */
  457. realdent = (dir_entry *)slotptr2;
  458. while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
  459. slotptr2--;
  460. slot2str(slotptr2, l_name, &idx);
  461. }
  462. } else {
  463. /* Save the real directory entry */
  464. realdent = (dir_entry *)slotptr;
  465. }
  466. do {
  467. slotptr--;
  468. if (slot2str(slotptr, l_name, &idx))
  469. break;
  470. } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
  471. l_name[idx] = '\0';
  472. if (*l_name == DELETED_FLAG)
  473. *l_name = '\0';
  474. else if (*l_name == aRING)
  475. *l_name = DELETED_FLAG;
  476. downcase(l_name);
  477. /* Return the real directory entry */
  478. memcpy(retdent, realdent, sizeof(dir_entry));
  479. return 0;
  480. }
  481. /* Calculate short name checksum */
  482. static __u8 mkcksum(const char name[8], const char ext[3])
  483. {
  484. int i;
  485. __u8 ret = 0;
  486. for (i = 0; i < 8; i++)
  487. ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
  488. for (i = 0; i < 3; i++)
  489. ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
  490. return ret;
  491. }
  492. /*
  493. * Get the directory entry associated with 'filename' from the directory
  494. * starting at 'startsect'
  495. */
  496. __u8 get_dentfromdir_block[MAX_CLUSTSIZE]
  497. __aligned(ARCH_DMA_MINALIGN);
  498. static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
  499. char *filename, dir_entry *retdent,
  500. int dols)
  501. {
  502. __u16 prevcksum = 0xffff;
  503. __u32 curclust = START(retdent);
  504. int files = 0, dirs = 0;
  505. debug("get_dentfromdir: %s\n", filename);
  506. while (1) {
  507. dir_entry *dentptr;
  508. int i;
  509. if (get_cluster(mydata, curclust, get_dentfromdir_block,
  510. mydata->clust_size * mydata->sect_size) != 0) {
  511. debug("Error: reading directory block\n");
  512. return NULL;
  513. }
  514. dentptr = (dir_entry *)get_dentfromdir_block;
  515. for (i = 0; i < DIRENTSPERCLUST; i++) {
  516. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  517. l_name[0] = '\0';
  518. if (dentptr->name[0] == DELETED_FLAG) {
  519. dentptr++;
  520. continue;
  521. }
  522. if ((dentptr->attr & ATTR_VOLUME)) {
  523. if (vfat_enabled &&
  524. (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
  525. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  526. prevcksum = ((dir_slot *)dentptr)->alias_checksum;
  527. get_vfatname(mydata, curclust,
  528. get_dentfromdir_block,
  529. dentptr, l_name);
  530. if (dols) {
  531. int isdir;
  532. char dirc;
  533. int doit = 0;
  534. isdir = (dentptr->attr & ATTR_DIR);
  535. if (isdir) {
  536. dirs++;
  537. dirc = '/';
  538. doit = 1;
  539. } else {
  540. dirc = ' ';
  541. if (l_name[0] != 0) {
  542. files++;
  543. doit = 1;
  544. }
  545. }
  546. if (doit) {
  547. if (dirc == ' ') {
  548. printf(" %8u %s%c\n",
  549. FAT2CPU32(dentptr->size),
  550. l_name,
  551. dirc);
  552. } else {
  553. printf(" %s%c\n",
  554. l_name,
  555. dirc);
  556. }
  557. }
  558. dentptr++;
  559. continue;
  560. }
  561. debug("vfatname: |%s|\n", l_name);
  562. } else {
  563. /* Volume label or VFAT entry */
  564. dentptr++;
  565. continue;
  566. }
  567. }
  568. if (dentptr->name[0] == 0) {
  569. if (dols) {
  570. printf("\n%d file(s), %d dir(s)\n\n",
  571. files, dirs);
  572. }
  573. debug("Dentname == NULL - %d\n", i);
  574. return NULL;
  575. }
  576. if (vfat_enabled) {
  577. __u8 csum = mkcksum(dentptr->name, dentptr->ext);
  578. if (dols && csum == prevcksum) {
  579. prevcksum = 0xffff;
  580. dentptr++;
  581. continue;
  582. }
  583. }
  584. get_name(dentptr, s_name);
  585. if (dols) {
  586. int isdir = (dentptr->attr & ATTR_DIR);
  587. char dirc;
  588. int doit = 0;
  589. if (isdir) {
  590. dirs++;
  591. dirc = '/';
  592. doit = 1;
  593. } else {
  594. dirc = ' ';
  595. if (s_name[0] != 0) {
  596. files++;
  597. doit = 1;
  598. }
  599. }
  600. if (doit) {
  601. if (dirc == ' ') {
  602. printf(" %8u %s%c\n",
  603. FAT2CPU32(dentptr->size),
  604. s_name, dirc);
  605. } else {
  606. printf(" %s%c\n",
  607. s_name, dirc);
  608. }
  609. }
  610. dentptr++;
  611. continue;
  612. }
  613. if (strcmp(filename, s_name)
  614. && strcmp(filename, l_name)) {
  615. debug("Mismatch: |%s|%s|\n", s_name, l_name);
  616. dentptr++;
  617. continue;
  618. }
  619. memcpy(retdent, dentptr, sizeof(dir_entry));
  620. debug("DentName: %s", s_name);
  621. debug(", start: 0x%x", START(dentptr));
  622. debug(", size: 0x%x %s\n",
  623. FAT2CPU32(dentptr->size),
  624. (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
  625. return retdent;
  626. }
  627. curclust = get_fatent(mydata, curclust);
  628. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  629. debug("curclust: 0x%x\n", curclust);
  630. printf("Invalid FAT entry\n");
  631. return NULL;
  632. }
  633. }
  634. return NULL;
  635. }
  636. /*
  637. * Read boot sector and volume info from a FAT filesystem
  638. */
  639. static int
  640. read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
  641. {
  642. __u8 *block;
  643. volume_info *vistart;
  644. int ret = 0;
  645. if (cur_dev == NULL) {
  646. debug("Error: no device selected\n");
  647. return -1;
  648. }
  649. block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
  650. if (block == NULL) {
  651. debug("Error: allocating block\n");
  652. return -1;
  653. }
  654. if (disk_read(0, 1, block) < 0) {
  655. debug("Error: reading block\n");
  656. goto fail;
  657. }
  658. memcpy(bs, block, sizeof(boot_sector));
  659. bs->reserved = FAT2CPU16(bs->reserved);
  660. bs->fat_length = FAT2CPU16(bs->fat_length);
  661. bs->secs_track = FAT2CPU16(bs->secs_track);
  662. bs->heads = FAT2CPU16(bs->heads);
  663. bs->total_sect = FAT2CPU32(bs->total_sect);
  664. /* FAT32 entries */
  665. if (bs->fat_length == 0) {
  666. /* Assume FAT32 */
  667. bs->fat32_length = FAT2CPU32(bs->fat32_length);
  668. bs->flags = FAT2CPU16(bs->flags);
  669. bs->root_cluster = FAT2CPU32(bs->root_cluster);
  670. bs->info_sector = FAT2CPU16(bs->info_sector);
  671. bs->backup_boot = FAT2CPU16(bs->backup_boot);
  672. vistart = (volume_info *)(block + sizeof(boot_sector));
  673. *fatsize = 32;
  674. } else {
  675. vistart = (volume_info *)&(bs->fat32_length);
  676. *fatsize = 0;
  677. }
  678. memcpy(volinfo, vistart, sizeof(volume_info));
  679. if (*fatsize == 32) {
  680. if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
  681. goto exit;
  682. } else {
  683. if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  684. *fatsize = 12;
  685. goto exit;
  686. }
  687. if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  688. *fatsize = 16;
  689. goto exit;
  690. }
  691. }
  692. debug("Error: broken fs_type sign\n");
  693. fail:
  694. ret = -1;
  695. exit:
  696. free(block);
  697. return ret;
  698. }
  699. __u8 do_fat_read_at_block[MAX_CLUSTSIZE]
  700. __aligned(ARCH_DMA_MINALIGN);
  701. int do_fat_read_at(const char *filename, loff_t pos, void *buffer,
  702. loff_t maxsize, int dols, int dogetsize, loff_t *size)
  703. {
  704. char fnamecopy[2048];
  705. boot_sector bs;
  706. volume_info volinfo;
  707. fsdata datablock;
  708. fsdata *mydata = &datablock;
  709. dir_entry *dentptr = NULL;
  710. __u16 prevcksum = 0xffff;
  711. char *subname = "";
  712. __u32 cursect;
  713. int idx, isdir = 0;
  714. int files = 0, dirs = 0;
  715. int ret = -1;
  716. int firsttime;
  717. __u32 root_cluster = 0;
  718. int rootdir_size = 0;
  719. int j;
  720. if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
  721. debug("Error: reading boot sector\n");
  722. return -1;
  723. }
  724. if (mydata->fatsize == 32) {
  725. root_cluster = bs.root_cluster;
  726. mydata->fatlength = bs.fat32_length;
  727. } else {
  728. mydata->fatlength = bs.fat_length;
  729. }
  730. mydata->fat_sect = bs.reserved;
  731. cursect = mydata->rootdir_sect
  732. = mydata->fat_sect + mydata->fatlength * bs.fats;
  733. mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
  734. mydata->clust_size = bs.cluster_size;
  735. if (mydata->sect_size != cur_part_info.blksz) {
  736. printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
  737. mydata->sect_size, cur_part_info.blksz);
  738. return -1;
  739. }
  740. if (mydata->fatsize == 32) {
  741. mydata->data_begin = mydata->rootdir_sect -
  742. (mydata->clust_size * 2);
  743. } else {
  744. rootdir_size = ((bs.dir_entries[1] * (int)256 +
  745. bs.dir_entries[0]) *
  746. sizeof(dir_entry)) /
  747. mydata->sect_size;
  748. mydata->data_begin = mydata->rootdir_sect +
  749. rootdir_size -
  750. (mydata->clust_size * 2);
  751. }
  752. mydata->fatbufnum = -1;
  753. mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
  754. if (mydata->fatbuf == NULL) {
  755. debug("Error: allocating memory\n");
  756. return -1;
  757. }
  758. if (vfat_enabled)
  759. debug("VFAT Support enabled\n");
  760. debug("FAT%d, fat_sect: %d, fatlength: %d\n",
  761. mydata->fatsize, mydata->fat_sect, mydata->fatlength);
  762. debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
  763. "Data begins at: %d\n",
  764. root_cluster,
  765. mydata->rootdir_sect,
  766. mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
  767. debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
  768. mydata->clust_size);
  769. /* "cwd" is always the root... */
  770. while (ISDIRDELIM(*filename))
  771. filename++;
  772. /* Make a copy of the filename and convert it to lowercase */
  773. strcpy(fnamecopy, filename);
  774. downcase(fnamecopy);
  775. if (*fnamecopy == '\0') {
  776. if (!dols)
  777. goto exit;
  778. dols = LS_ROOT;
  779. } else if ((idx = dirdelim(fnamecopy)) >= 0) {
  780. isdir = 1;
  781. fnamecopy[idx] = '\0';
  782. subname = fnamecopy + idx + 1;
  783. /* Handle multiple delimiters */
  784. while (ISDIRDELIM(*subname))
  785. subname++;
  786. } else if (dols) {
  787. isdir = 1;
  788. }
  789. j = 0;
  790. while (1) {
  791. int i;
  792. if (j == 0) {
  793. debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
  794. cursect, mydata->clust_size, DIRENTSPERBLOCK);
  795. if (disk_read(cursect,
  796. (mydata->fatsize == 32) ?
  797. (mydata->clust_size) :
  798. PREFETCH_BLOCKS,
  799. do_fat_read_at_block) < 0) {
  800. debug("Error: reading rootdir block\n");
  801. goto exit;
  802. }
  803. dentptr = (dir_entry *) do_fat_read_at_block;
  804. }
  805. for (i = 0; i < DIRENTSPERBLOCK; i++) {
  806. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  807. __u8 csum;
  808. l_name[0] = '\0';
  809. if (dentptr->name[0] == DELETED_FLAG) {
  810. dentptr++;
  811. continue;
  812. }
  813. if (vfat_enabled)
  814. csum = mkcksum(dentptr->name, dentptr->ext);
  815. if (dentptr->attr & ATTR_VOLUME) {
  816. if (vfat_enabled &&
  817. (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
  818. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  819. prevcksum =
  820. ((dir_slot *)dentptr)->alias_checksum;
  821. get_vfatname(mydata,
  822. root_cluster,
  823. do_fat_read_at_block,
  824. dentptr, l_name);
  825. if (dols == LS_ROOT) {
  826. char dirc;
  827. int doit = 0;
  828. int isdir =
  829. (dentptr->attr & ATTR_DIR);
  830. if (isdir) {
  831. dirs++;
  832. dirc = '/';
  833. doit = 1;
  834. } else {
  835. dirc = ' ';
  836. if (l_name[0] != 0) {
  837. files++;
  838. doit = 1;
  839. }
  840. }
  841. if (doit) {
  842. if (dirc == ' ') {
  843. printf(" %8u %s%c\n",
  844. FAT2CPU32(dentptr->size),
  845. l_name,
  846. dirc);
  847. } else {
  848. printf(" %s%c\n",
  849. l_name,
  850. dirc);
  851. }
  852. }
  853. dentptr++;
  854. continue;
  855. }
  856. debug("Rootvfatname: |%s|\n",
  857. l_name);
  858. } else {
  859. /* Volume label or VFAT entry */
  860. dentptr++;
  861. continue;
  862. }
  863. } else if (dentptr->name[0] == 0) {
  864. debug("RootDentname == NULL - %d\n", i);
  865. if (dols == LS_ROOT) {
  866. printf("\n%d file(s), %d dir(s)\n\n",
  867. files, dirs);
  868. ret = 0;
  869. }
  870. goto exit;
  871. }
  872. else if (vfat_enabled &&
  873. dols == LS_ROOT && csum == prevcksum) {
  874. prevcksum = 0xffff;
  875. dentptr++;
  876. continue;
  877. }
  878. get_name(dentptr, s_name);
  879. if (dols == LS_ROOT) {
  880. int isdir = (dentptr->attr & ATTR_DIR);
  881. char dirc;
  882. int doit = 0;
  883. if (isdir) {
  884. dirc = '/';
  885. if (s_name[0] != 0) {
  886. dirs++;
  887. doit = 1;
  888. }
  889. } else {
  890. dirc = ' ';
  891. if (s_name[0] != 0) {
  892. files++;
  893. doit = 1;
  894. }
  895. }
  896. if (doit) {
  897. if (dirc == ' ') {
  898. printf(" %8u %s%c\n",
  899. FAT2CPU32(dentptr->size),
  900. s_name, dirc);
  901. } else {
  902. printf(" %s%c\n",
  903. s_name, dirc);
  904. }
  905. }
  906. dentptr++;
  907. continue;
  908. }
  909. if (strcmp(fnamecopy, s_name)
  910. && strcmp(fnamecopy, l_name)) {
  911. debug("RootMismatch: |%s|%s|\n", s_name,
  912. l_name);
  913. dentptr++;
  914. continue;
  915. }
  916. if (isdir && !(dentptr->attr & ATTR_DIR))
  917. goto exit;
  918. debug("RootName: %s", s_name);
  919. debug(", start: 0x%x", START(dentptr));
  920. debug(", size: 0x%x %s\n",
  921. FAT2CPU32(dentptr->size),
  922. isdir ? "(DIR)" : "");
  923. goto rootdir_done; /* We got a match */
  924. }
  925. debug("END LOOP: j=%d clust_size=%d\n", j,
  926. mydata->clust_size);
  927. /*
  928. * On FAT32 we must fetch the FAT entries for the next
  929. * root directory clusters when a cluster has been
  930. * completely processed.
  931. */
  932. ++j;
  933. int rootdir_end = 0;
  934. if (mydata->fatsize == 32) {
  935. if (j == mydata->clust_size) {
  936. int nxtsect = 0;
  937. int nxt_clust = 0;
  938. nxt_clust = get_fatent(mydata, root_cluster);
  939. rootdir_end = CHECK_CLUST(nxt_clust, 32);
  940. nxtsect = mydata->data_begin +
  941. (nxt_clust * mydata->clust_size);
  942. root_cluster = nxt_clust;
  943. cursect = nxtsect;
  944. j = 0;
  945. }
  946. } else {
  947. if (j == PREFETCH_BLOCKS)
  948. j = 0;
  949. rootdir_end = (++cursect - mydata->rootdir_sect >=
  950. rootdir_size);
  951. }
  952. /* If end of rootdir reached */
  953. if (rootdir_end) {
  954. if (dols == LS_ROOT) {
  955. printf("\n%d file(s), %d dir(s)\n\n",
  956. files, dirs);
  957. *size = 0;
  958. }
  959. goto exit;
  960. }
  961. }
  962. rootdir_done:
  963. firsttime = 1;
  964. while (isdir) {
  965. int startsect = mydata->data_begin
  966. + START(dentptr) * mydata->clust_size;
  967. dir_entry dent;
  968. char *nextname = NULL;
  969. dent = *dentptr;
  970. dentptr = &dent;
  971. idx = dirdelim(subname);
  972. if (idx >= 0) {
  973. subname[idx] = '\0';
  974. nextname = subname + idx + 1;
  975. /* Handle multiple delimiters */
  976. while (ISDIRDELIM(*nextname))
  977. nextname++;
  978. if (dols && *nextname == '\0')
  979. firsttime = 0;
  980. } else {
  981. if (dols && firsttime) {
  982. firsttime = 0;
  983. } else {
  984. isdir = 0;
  985. }
  986. }
  987. if (get_dentfromdir(mydata, startsect, subname, dentptr,
  988. isdir ? 0 : dols) == NULL) {
  989. if (dols && !isdir)
  990. *size = 0;
  991. goto exit;
  992. }
  993. if (isdir && !(dentptr->attr & ATTR_DIR))
  994. goto exit;
  995. if (idx >= 0)
  996. subname = nextname;
  997. }
  998. if (dogetsize) {
  999. *size = FAT2CPU32(dentptr->size);
  1000. ret = 0;
  1001. } else {
  1002. ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size);
  1003. }
  1004. debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size);
  1005. exit:
  1006. free(mydata->fatbuf);
  1007. return ret;
  1008. }
  1009. int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols,
  1010. loff_t *actread)
  1011. {
  1012. return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread);
  1013. }
  1014. int file_fat_detectfs(void)
  1015. {
  1016. boot_sector bs;
  1017. volume_info volinfo;
  1018. int fatsize;
  1019. char vol_label[12];
  1020. if (cur_dev == NULL) {
  1021. printf("No current device\n");
  1022. return 1;
  1023. }
  1024. #if defined(CONFIG_CMD_IDE) || \
  1025. defined(CONFIG_CMD_SATA) || \
  1026. defined(CONFIG_CMD_SCSI) || \
  1027. defined(CONFIG_CMD_USB) || \
  1028. defined(CONFIG_MMC)
  1029. printf("Interface: ");
  1030. switch (cur_dev->if_type) {
  1031. case IF_TYPE_IDE:
  1032. printf("IDE");
  1033. break;
  1034. case IF_TYPE_SATA:
  1035. printf("SATA");
  1036. break;
  1037. case IF_TYPE_SCSI:
  1038. printf("SCSI");
  1039. break;
  1040. case IF_TYPE_ATAPI:
  1041. printf("ATAPI");
  1042. break;
  1043. case IF_TYPE_USB:
  1044. printf("USB");
  1045. break;
  1046. case IF_TYPE_DOC:
  1047. printf("DOC");
  1048. break;
  1049. case IF_TYPE_MMC:
  1050. printf("MMC");
  1051. break;
  1052. default:
  1053. printf("Unknown");
  1054. }
  1055. printf("\n Device %d: ", cur_dev->dev);
  1056. dev_print(cur_dev);
  1057. #endif
  1058. if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
  1059. printf("\nNo valid FAT fs found\n");
  1060. return 1;
  1061. }
  1062. memcpy(vol_label, volinfo.volume_label, 11);
  1063. vol_label[11] = '\0';
  1064. volinfo.fs_type[5] = '\0';
  1065. printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
  1066. return 0;
  1067. }
  1068. int file_fat_ls(const char *dir)
  1069. {
  1070. loff_t size;
  1071. return do_fat_read(dir, NULL, 0, LS_YES, &size);
  1072. }
  1073. int fat_exists(const char *filename)
  1074. {
  1075. int ret;
  1076. loff_t size;
  1077. ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size);
  1078. return ret == 0;
  1079. }
  1080. int fat_size(const char *filename, loff_t *size)
  1081. {
  1082. return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
  1083. }
  1084. int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
  1085. loff_t maxsize, loff_t *actread)
  1086. {
  1087. printf("reading %s\n", filename);
  1088. return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0,
  1089. actread);
  1090. }
  1091. int file_fat_read(const char *filename, void *buffer, int maxsize)
  1092. {
  1093. loff_t actread;
  1094. int ret;
  1095. ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
  1096. if (ret)
  1097. return ret;
  1098. else
  1099. return actread;
  1100. }
  1101. int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
  1102. loff_t *actread)
  1103. {
  1104. int ret;
  1105. ret = file_fat_read_at(filename, offset, buf, len, actread);
  1106. if (ret)
  1107. printf("** Unable to read file %s **\n", filename);
  1108. return ret;
  1109. }
  1110. void fat_close(void)
  1111. {
  1112. }