fat.c 28 KB

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