fat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 <fat.h>
  30. #include <asm/byteorder.h>
  31. #include <part.h>
  32. /*
  33. * Convert a string to lowercase.
  34. */
  35. static void
  36. downcase(char *str)
  37. {
  38. while (*str != '\0') {
  39. TOLOWER(*str);
  40. str++;
  41. }
  42. }
  43. static block_dev_desc_t *cur_dev = NULL;
  44. static unsigned long part_offset = 0;
  45. static int cur_part = 1;
  46. #define DOS_PART_TBL_OFFSET 0x1be
  47. #define DOS_PART_MAGIC_OFFSET 0x1fe
  48. #define DOS_FS_TYPE_OFFSET 0x36
  49. int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
  50. {
  51. startblock += part_offset;
  52. if (cur_dev == NULL)
  53. return -1;
  54. if (cur_dev->block_read) {
  55. return cur_dev->block_read (cur_dev->dev
  56. , startblock, getsize, (unsigned long *)bufptr);
  57. }
  58. return -1;
  59. }
  60. int
  61. fat_register_device(block_dev_desc_t *dev_desc, int part_no)
  62. {
  63. unsigned char buffer[SECTOR_SIZE];
  64. disk_partition_t info;
  65. if (!dev_desc->block_read)
  66. return -1;
  67. cur_dev = dev_desc;
  68. /* check if we have a MBR (on floppies we have only a PBR) */
  69. if (dev_desc->block_read (dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
  70. printf ("** Can't read from device %d **\n", dev_desc->dev);
  71. return -1;
  72. }
  73. if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
  74. buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
  75. /* no signature found */
  76. return -1;
  77. }
  78. #if (defined(CONFIG_CMD_IDE) || \
  79. defined(CONFIG_CMD_MG_DISK) || \
  80. defined(CONFIG_CMD_SATA) || \
  81. defined(CONFIG_CMD_SCSI) || \
  82. defined(CONFIG_CMD_USB) || \
  83. defined(CONFIG_MMC) || \
  84. defined(CONFIG_SYSTEMACE) )
  85. /* First we assume, there is a MBR */
  86. if (!get_partition_info (dev_desc, part_no, &info)) {
  87. part_offset = info.start;
  88. cur_part = part_no;
  89. } else if (!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3)) {
  90. /* ok, we assume we are on a PBR only */
  91. cur_part = 1;
  92. part_offset = 0;
  93. } else {
  94. printf ("** Partition %d not valid on device %d **\n",
  95. part_no, dev_desc->dev);
  96. return -1;
  97. }
  98. #else
  99. if (!strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],"FAT",3)) {
  100. /* ok, we assume we are on a PBR only */
  101. cur_part = 1;
  102. part_offset = 0;
  103. info.start = part_offset;
  104. } else {
  105. /* FIXME we need to determine the start block of the
  106. * partition where the DOS FS resides. This can be done
  107. * by using the get_partition_info routine. For this
  108. * purpose the libpart must be included.
  109. */
  110. part_offset = 32;
  111. cur_part = 1;
  112. }
  113. #endif
  114. return 0;
  115. }
  116. /*
  117. * Get the first occurence of a directory delimiter ('/' or '\') in a string.
  118. * Return index into string if found, -1 otherwise.
  119. */
  120. static int
  121. dirdelim(char *str)
  122. {
  123. char *start = str;
  124. while (*str != '\0') {
  125. if (ISDIRDELIM(*str)) return str - start;
  126. str++;
  127. }
  128. return -1;
  129. }
  130. /*
  131. * Extract zero terminated short name from a directory entry.
  132. */
  133. static void get_name (dir_entry *dirent, char *s_name)
  134. {
  135. char *ptr;
  136. memcpy (s_name, dirent->name, 8);
  137. s_name[8] = '\0';
  138. ptr = s_name;
  139. while (*ptr && *ptr != ' ')
  140. ptr++;
  141. if (dirent->ext[0] && dirent->ext[0] != ' ') {
  142. *ptr = '.';
  143. ptr++;
  144. memcpy (ptr, dirent->ext, 3);
  145. ptr[3] = '\0';
  146. while (*ptr && *ptr != ' ')
  147. ptr++;
  148. }
  149. *ptr = '\0';
  150. if (*s_name == DELETED_FLAG)
  151. *s_name = '\0';
  152. else if (*s_name == aRING)
  153. *s_name = DELETED_FLAG;
  154. downcase (s_name);
  155. }
  156. /*
  157. * Get the entry at index 'entry' in a FAT (12/16/32) table.
  158. * On failure 0x00 is returned.
  159. */
  160. static __u32
  161. get_fatent(fsdata *mydata, __u32 entry)
  162. {
  163. __u32 bufnum;
  164. __u32 offset;
  165. __u32 ret = 0x00;
  166. switch (mydata->fatsize) {
  167. case 32:
  168. bufnum = entry / FAT32BUFSIZE;
  169. offset = entry - bufnum * FAT32BUFSIZE;
  170. break;
  171. case 16:
  172. bufnum = entry / FAT16BUFSIZE;
  173. offset = entry - bufnum * FAT16BUFSIZE;
  174. break;
  175. case 12:
  176. bufnum = entry / FAT12BUFSIZE;
  177. offset = entry - bufnum * FAT12BUFSIZE;
  178. break;
  179. default:
  180. /* Unsupported FAT size */
  181. return ret;
  182. }
  183. /* Read a new block of FAT entries into the cache. */
  184. if (bufnum != mydata->fatbufnum) {
  185. int getsize = FATBUFSIZE/FS_BLOCK_SIZE;
  186. __u8 *bufptr = mydata->fatbuf;
  187. __u32 fatlength = mydata->fatlength;
  188. __u32 startblock = bufnum * FATBUFBLOCKS;
  189. fatlength *= SECTOR_SIZE; /* We want it in bytes now */
  190. startblock += mydata->fat_sect; /* Offset from start of disk */
  191. if (getsize > fatlength) getsize = fatlength;
  192. if (disk_read(startblock, getsize, bufptr) < 0) {
  193. FAT_DPRINT("Error reading FAT blocks\n");
  194. return ret;
  195. }
  196. mydata->fatbufnum = bufnum;
  197. }
  198. /* Get the actual entry from the table */
  199. switch (mydata->fatsize) {
  200. case 32:
  201. ret = FAT2CPU32(((__u32*)mydata->fatbuf)[offset]);
  202. break;
  203. case 16:
  204. ret = FAT2CPU16(((__u16*)mydata->fatbuf)[offset]);
  205. break;
  206. case 12: {
  207. __u32 off16 = (offset*3)/4;
  208. __u16 val1, val2;
  209. switch (offset & 0x3) {
  210. case 0:
  211. ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
  212. ret &= 0xfff;
  213. break;
  214. case 1:
  215. val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
  216. val1 &= 0xf000;
  217. val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
  218. val2 &= 0x00ff;
  219. ret = (val2 << 4) | (val1 >> 12);
  220. break;
  221. case 2:
  222. val1 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);
  223. val1 &= 0xff00;
  224. val2 = FAT2CPU16(((__u16*)mydata->fatbuf)[off16+1]);
  225. val2 &= 0x000f;
  226. ret = (val2 << 8) | (val1 >> 8);
  227. break;
  228. case 3:
  229. ret = FAT2CPU16(((__u16*)mydata->fatbuf)[off16]);;
  230. ret = (ret & 0xfff0) >> 4;
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. break;
  237. }
  238. FAT_DPRINT("ret: %d, offset: %d\n", ret, offset);
  239. return ret;
  240. }
  241. /*
  242. * Read at most 'size' bytes from the specified cluster into 'buffer'.
  243. * Return 0 on success, -1 otherwise.
  244. */
  245. static int
  246. get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
  247. {
  248. int idx = 0;
  249. __u32 startsect;
  250. if (clustnum > 0) {
  251. startsect = mydata->data_begin + clustnum*mydata->clust_size;
  252. } else {
  253. startsect = mydata->rootdir_sect;
  254. }
  255. FAT_DPRINT("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
  256. if (disk_read(startsect, size/FS_BLOCK_SIZE , buffer) < 0) {
  257. FAT_DPRINT("Error reading data\n");
  258. return -1;
  259. }
  260. if(size % FS_BLOCK_SIZE) {
  261. __u8 tmpbuf[FS_BLOCK_SIZE];
  262. idx= size/FS_BLOCK_SIZE;
  263. if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
  264. FAT_DPRINT("Error reading data\n");
  265. return -1;
  266. }
  267. buffer += idx*FS_BLOCK_SIZE;
  268. memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
  269. return 0;
  270. }
  271. return 0;
  272. }
  273. /*
  274. * Read at most 'maxsize' bytes from the file associated with 'dentptr'
  275. * into 'buffer'.
  276. * Return the number of bytes read or -1 on fatal errors.
  277. */
  278. static long
  279. get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
  280. unsigned long maxsize)
  281. {
  282. unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
  283. unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
  284. __u32 curclust = START(dentptr);
  285. __u32 endclust, newclust;
  286. unsigned long actsize;
  287. FAT_DPRINT("Filesize: %ld bytes\n", filesize);
  288. if (maxsize > 0 && filesize > maxsize) filesize = maxsize;
  289. FAT_DPRINT("Reading: %ld bytes\n", filesize);
  290. actsize=bytesperclust;
  291. endclust=curclust;
  292. do {
  293. /* search for consecutive clusters */
  294. while(actsize < filesize) {
  295. newclust = get_fatent(mydata, endclust);
  296. if((newclust -1)!=endclust)
  297. goto getit;
  298. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  299. FAT_DPRINT("curclust: 0x%x\n", newclust);
  300. FAT_DPRINT("Invalid FAT entry\n");
  301. return gotsize;
  302. }
  303. endclust=newclust;
  304. actsize+= bytesperclust;
  305. }
  306. /* actsize >= file size */
  307. actsize -= bytesperclust;
  308. /* get remaining clusters */
  309. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  310. FAT_ERROR("Error reading cluster\n");
  311. return -1;
  312. }
  313. /* get remaining bytes */
  314. gotsize += (int)actsize;
  315. filesize -= actsize;
  316. buffer += actsize;
  317. actsize= filesize;
  318. if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
  319. FAT_ERROR("Error reading cluster\n");
  320. return -1;
  321. }
  322. gotsize+=actsize;
  323. return gotsize;
  324. getit:
  325. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  326. FAT_ERROR("Error reading cluster\n");
  327. return -1;
  328. }
  329. gotsize += (int)actsize;
  330. filesize -= actsize;
  331. buffer += actsize;
  332. curclust = get_fatent(mydata, endclust);
  333. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  334. FAT_DPRINT("curclust: 0x%x\n", curclust);
  335. FAT_ERROR("Invalid FAT entry\n");
  336. return gotsize;
  337. }
  338. actsize=bytesperclust;
  339. endclust=curclust;
  340. } while (1);
  341. }
  342. #ifdef CONFIG_SUPPORT_VFAT
  343. /*
  344. * Extract the file name information from 'slotptr' into 'l_name',
  345. * starting at l_name[*idx].
  346. * Return 1 if terminator (zero byte) is found, 0 otherwise.
  347. */
  348. static int
  349. slot2str(dir_slot *slotptr, char *l_name, int *idx)
  350. {
  351. int j;
  352. for (j = 0; j <= 8; j += 2) {
  353. l_name[*idx] = slotptr->name0_4[j];
  354. if (l_name[*idx] == 0x00) return 1;
  355. (*idx)++;
  356. }
  357. for (j = 0; j <= 10; j += 2) {
  358. l_name[*idx] = slotptr->name5_10[j];
  359. if (l_name[*idx] == 0x00) return 1;
  360. (*idx)++;
  361. }
  362. for (j = 0; j <= 2; j += 2) {
  363. l_name[*idx] = slotptr->name11_12[j];
  364. if (l_name[*idx] == 0x00) return 1;
  365. (*idx)++;
  366. }
  367. return 0;
  368. }
  369. /*
  370. * Extract the full long filename starting at 'retdent' (which is really
  371. * a slot) into 'l_name'. If successful also copy the real directory entry
  372. * into 'retdent'
  373. * Return 0 on success, -1 otherwise.
  374. */
  375. __attribute__ ((__aligned__(__alignof__(dir_entry))))
  376. __u8 get_vfatname_block[MAX_CLUSTSIZE];
  377. static int
  378. get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
  379. dir_entry *retdent, char *l_name)
  380. {
  381. dir_entry *realdent;
  382. dir_slot *slotptr = (dir_slot*) retdent;
  383. __u8 *nextclust = cluster + mydata->clust_size * SECTOR_SIZE;
  384. __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
  385. int idx = 0;
  386. while ((__u8*)slotptr < nextclust) {
  387. if (counter == 0) break;
  388. if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
  389. return -1;
  390. slotptr++;
  391. counter--;
  392. }
  393. if ((__u8*)slotptr >= nextclust) {
  394. dir_slot *slotptr2;
  395. slotptr--;
  396. curclust = get_fatent(mydata, curclust);
  397. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  398. FAT_DPRINT("curclust: 0x%x\n", curclust);
  399. FAT_ERROR("Invalid FAT entry\n");
  400. return -1;
  401. }
  402. if (get_cluster(mydata, curclust, get_vfatname_block,
  403. mydata->clust_size * SECTOR_SIZE) != 0) {
  404. FAT_DPRINT("Error: reading directory block\n");
  405. return -1;
  406. }
  407. slotptr2 = (dir_slot*) get_vfatname_block;
  408. while (slotptr2->id > 0x01) {
  409. slotptr2++;
  410. }
  411. /* Save the real directory entry */
  412. realdent = (dir_entry*)slotptr2 + 1;
  413. while ((__u8*)slotptr2 >= get_vfatname_block) {
  414. slot2str(slotptr2, l_name, &idx);
  415. slotptr2--;
  416. }
  417. } else {
  418. /* Save the real directory entry */
  419. realdent = (dir_entry*)slotptr;
  420. }
  421. do {
  422. slotptr--;
  423. if (slot2str(slotptr, l_name, &idx)) break;
  424. } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
  425. l_name[idx] = '\0';
  426. if (*l_name == DELETED_FLAG) *l_name = '\0';
  427. else if (*l_name == aRING) *l_name = DELETED_FLAG;
  428. downcase(l_name);
  429. /* Return the real directory entry */
  430. memcpy(retdent, realdent, sizeof(dir_entry));
  431. return 0;
  432. }
  433. /* Calculate short name checksum */
  434. static __u8
  435. mkcksum(const char *str)
  436. {
  437. int i;
  438. __u8 ret = 0;
  439. for (i = 0; i < 11; i++) {
  440. ret = (((ret&1)<<7)|((ret&0xfe)>>1)) + str[i];
  441. }
  442. return ret;
  443. }
  444. #endif
  445. /*
  446. * Get the directory entry associated with 'filename' from the directory
  447. * starting at 'startsect'
  448. */
  449. __attribute__ ((__aligned__(__alignof__(dir_entry))))
  450. __u8 get_dentfromdir_block[MAX_CLUSTSIZE];
  451. static dir_entry *get_dentfromdir (fsdata * mydata, int startsect,
  452. char *filename, dir_entry * retdent,
  453. int dols)
  454. {
  455. __u16 prevcksum = 0xffff;
  456. __u32 curclust = START (retdent);
  457. int files = 0, dirs = 0;
  458. FAT_DPRINT ("get_dentfromdir: %s\n", filename);
  459. while (1) {
  460. dir_entry *dentptr;
  461. int i;
  462. if (get_cluster (mydata, curclust, get_dentfromdir_block,
  463. mydata->clust_size * SECTOR_SIZE) != 0) {
  464. FAT_DPRINT ("Error: reading directory block\n");
  465. return NULL;
  466. }
  467. dentptr = (dir_entry *) get_dentfromdir_block;
  468. for (i = 0; i < DIRENTSPERCLUST; i++) {
  469. char s_name[14], l_name[256];
  470. l_name[0] = '\0';
  471. if (dentptr->name[0] == DELETED_FLAG) {
  472. dentptr++;
  473. continue;
  474. }
  475. if ((dentptr->attr & ATTR_VOLUME)) {
  476. #ifdef CONFIG_SUPPORT_VFAT
  477. if ((dentptr->attr & ATTR_VFAT) &&
  478. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  479. prevcksum = ((dir_slot *) dentptr)
  480. ->alias_checksum;
  481. get_vfatname (mydata, curclust, get_dentfromdir_block,
  482. dentptr, l_name);
  483. if (dols) {
  484. int isdir = (dentptr->attr & ATTR_DIR);
  485. char dirc;
  486. int doit = 0;
  487. if (isdir) {
  488. dirs++;
  489. dirc = '/';
  490. doit = 1;
  491. } else {
  492. dirc = ' ';
  493. if (l_name[0] != 0) {
  494. files++;
  495. doit = 1;
  496. }
  497. }
  498. if (doit) {
  499. if (dirc == ' ') {
  500. printf (" %8ld %s%c\n",
  501. (long) FAT2CPU32 (dentptr->size),
  502. l_name, dirc);
  503. } else {
  504. printf (" %s%c\n", l_name, dirc);
  505. }
  506. }
  507. dentptr++;
  508. continue;
  509. }
  510. FAT_DPRINT ("vfatname: |%s|\n", l_name);
  511. } else
  512. #endif
  513. {
  514. /* Volume label or VFAT entry */
  515. dentptr++;
  516. continue;
  517. }
  518. }
  519. if (dentptr->name[0] == 0) {
  520. if (dols) {
  521. printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
  522. }
  523. FAT_DPRINT ("Dentname == NULL - %d\n", i);
  524. return NULL;
  525. }
  526. #ifdef CONFIG_SUPPORT_VFAT
  527. if (dols && mkcksum (dentptr->name) == prevcksum) {
  528. dentptr++;
  529. continue;
  530. }
  531. #endif
  532. get_name (dentptr, s_name);
  533. if (dols) {
  534. int isdir = (dentptr->attr & ATTR_DIR);
  535. char dirc;
  536. int doit = 0;
  537. if (isdir) {
  538. dirs++;
  539. dirc = '/';
  540. doit = 1;
  541. } else {
  542. dirc = ' ';
  543. if (s_name[0] != 0) {
  544. files++;
  545. doit = 1;
  546. }
  547. }
  548. if (doit) {
  549. if (dirc == ' ') {
  550. printf (" %8ld %s%c\n",
  551. (long) FAT2CPU32 (dentptr->size), s_name,
  552. dirc);
  553. } else {
  554. printf (" %s%c\n", s_name, dirc);
  555. }
  556. }
  557. dentptr++;
  558. continue;
  559. }
  560. if (strcmp (filename, s_name) && strcmp (filename, l_name)) {
  561. FAT_DPRINT ("Mismatch: |%s|%s|\n", s_name, l_name);
  562. dentptr++;
  563. continue;
  564. }
  565. memcpy (retdent, dentptr, sizeof (dir_entry));
  566. FAT_DPRINT ("DentName: %s", s_name);
  567. FAT_DPRINT (", start: 0x%x", START (dentptr));
  568. FAT_DPRINT (", size: 0x%x %s\n",
  569. FAT2CPU32 (dentptr->size),
  570. (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
  571. return retdent;
  572. }
  573. curclust = get_fatent (mydata, curclust);
  574. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  575. FAT_DPRINT ("curclust: 0x%x\n", curclust);
  576. FAT_ERROR ("Invalid FAT entry\n");
  577. return NULL;
  578. }
  579. }
  580. return NULL;
  581. }
  582. /*
  583. * Read boot sector and volume info from a FAT filesystem
  584. */
  585. static int
  586. read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
  587. {
  588. __u8 block[FS_BLOCK_SIZE];
  589. volume_info *vistart;
  590. if (disk_read(0, 1, block) < 0) {
  591. FAT_DPRINT("Error: reading block\n");
  592. return -1;
  593. }
  594. memcpy(bs, block, sizeof(boot_sector));
  595. bs->reserved = FAT2CPU16(bs->reserved);
  596. bs->fat_length = FAT2CPU16(bs->fat_length);
  597. bs->secs_track = FAT2CPU16(bs->secs_track);
  598. bs->heads = FAT2CPU16(bs->heads);
  599. #if 0 /* UNUSED */
  600. bs->hidden = FAT2CPU32(bs->hidden);
  601. #endif
  602. bs->total_sect = FAT2CPU32(bs->total_sect);
  603. /* FAT32 entries */
  604. if (bs->fat_length == 0) {
  605. /* Assume FAT32 */
  606. bs->fat32_length = FAT2CPU32(bs->fat32_length);
  607. bs->flags = FAT2CPU16(bs->flags);
  608. bs->root_cluster = FAT2CPU32(bs->root_cluster);
  609. bs->info_sector = FAT2CPU16(bs->info_sector);
  610. bs->backup_boot = FAT2CPU16(bs->backup_boot);
  611. vistart = (volume_info*) (block + sizeof(boot_sector));
  612. *fatsize = 32;
  613. } else {
  614. vistart = (volume_info*) &(bs->fat32_length);
  615. *fatsize = 0;
  616. }
  617. memcpy(volinfo, vistart, sizeof(volume_info));
  618. if (*fatsize == 32) {
  619. if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  620. return 0;
  621. }
  622. } else {
  623. if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  624. *fatsize = 12;
  625. return 0;
  626. }
  627. if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  628. *fatsize = 16;
  629. return 0;
  630. }
  631. }
  632. FAT_DPRINT("Error: broken fs_type sign\n");
  633. return -1;
  634. }
  635. __attribute__ ((__aligned__(__alignof__(dir_entry))))
  636. __u8 do_fat_read_block[MAX_CLUSTSIZE];
  637. long
  638. do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
  639. int dols)
  640. {
  641. #if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */
  642. static
  643. #endif
  644. char fnamecopy[2048];
  645. boot_sector bs;
  646. volume_info volinfo;
  647. fsdata datablock;
  648. fsdata *mydata = &datablock;
  649. dir_entry *dentptr;
  650. __u16 prevcksum = 0xffff;
  651. char *subname = "";
  652. int rootdir_size, cursect;
  653. int idx, isdir = 0;
  654. int files = 0, dirs = 0;
  655. long ret = 0;
  656. int firsttime;
  657. if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
  658. FAT_DPRINT ("Error: reading boot sector\n");
  659. return -1;
  660. }
  661. if (mydata->fatsize == 32) {
  662. mydata->fatlength = bs.fat32_length;
  663. } else {
  664. mydata->fatlength = bs.fat_length;
  665. }
  666. mydata->fat_sect = bs.reserved;
  667. cursect = mydata->rootdir_sect
  668. = mydata->fat_sect + mydata->fatlength * bs.fats;
  669. mydata->clust_size = bs.cluster_size;
  670. if (mydata->fatsize == 32) {
  671. rootdir_size = mydata->clust_size;
  672. mydata->data_begin = mydata->rootdir_sect /* + rootdir_size */
  673. - (mydata->clust_size * 2);
  674. } else {
  675. rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
  676. * sizeof (dir_entry)) / SECTOR_SIZE;
  677. mydata->data_begin = mydata->rootdir_sect + rootdir_size
  678. - (mydata->clust_size * 2);
  679. }
  680. mydata->fatbufnum = -1;
  681. FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
  682. mydata->fatlength);
  683. FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
  684. "Data begins at: %d\n",
  685. mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
  686. rootdir_size, mydata->data_begin);
  687. FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
  688. /* "cwd" is always the root... */
  689. while (ISDIRDELIM (*filename))
  690. filename++;
  691. /* Make a copy of the filename and convert it to lowercase */
  692. strcpy (fnamecopy, filename);
  693. downcase (fnamecopy);
  694. if (*fnamecopy == '\0') {
  695. if (!dols)
  696. return -1;
  697. dols = LS_ROOT;
  698. } else if ((idx = dirdelim (fnamecopy)) >= 0) {
  699. isdir = 1;
  700. fnamecopy[idx] = '\0';
  701. subname = fnamecopy + idx + 1;
  702. /* Handle multiple delimiters */
  703. while (ISDIRDELIM (*subname))
  704. subname++;
  705. } else if (dols) {
  706. isdir = 1;
  707. }
  708. while (1) {
  709. int i;
  710. if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) {
  711. FAT_DPRINT ("Error: reading rootdir block\n");
  712. return -1;
  713. }
  714. dentptr = (dir_entry *) do_fat_read_block;
  715. for (i = 0; i < DIRENTSPERBLOCK; i++) {
  716. char s_name[14], l_name[256];
  717. l_name[0] = '\0';
  718. if ((dentptr->attr & ATTR_VOLUME)) {
  719. #ifdef CONFIG_SUPPORT_VFAT
  720. if ((dentptr->attr & ATTR_VFAT) &&
  721. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  722. prevcksum = ((dir_slot *) dentptr)->alias_checksum;
  723. get_vfatname (mydata, 0, do_fat_read_block, dentptr, l_name);
  724. if (dols == LS_ROOT) {
  725. int isdir = (dentptr->attr & ATTR_DIR);
  726. char dirc;
  727. int doit = 0;
  728. if (isdir) {
  729. dirs++;
  730. dirc = '/';
  731. doit = 1;
  732. } else {
  733. dirc = ' ';
  734. if (l_name[0] != 0) {
  735. files++;
  736. doit = 1;
  737. }
  738. }
  739. if (doit) {
  740. if (dirc == ' ') {
  741. printf (" %8ld %s%c\n",
  742. (long) FAT2CPU32 (dentptr->size),
  743. l_name, dirc);
  744. } else {
  745. printf (" %s%c\n", l_name, dirc);
  746. }
  747. }
  748. dentptr++;
  749. continue;
  750. }
  751. FAT_DPRINT ("Rootvfatname: |%s|\n", l_name);
  752. } else
  753. #endif
  754. {
  755. /* Volume label or VFAT entry */
  756. dentptr++;
  757. continue;
  758. }
  759. } else if (dentptr->name[0] == 0) {
  760. FAT_DPRINT ("RootDentname == NULL - %d\n", i);
  761. if (dols == LS_ROOT) {
  762. printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
  763. return 0;
  764. }
  765. return -1;
  766. }
  767. #ifdef CONFIG_SUPPORT_VFAT
  768. else if (dols == LS_ROOT
  769. && mkcksum (dentptr->name) == prevcksum) {
  770. dentptr++;
  771. continue;
  772. }
  773. #endif
  774. get_name (dentptr, s_name);
  775. if (dols == LS_ROOT) {
  776. int isdir = (dentptr->attr & ATTR_DIR);
  777. char dirc;
  778. int doit = 0;
  779. if (isdir) {
  780. dirc = '/';
  781. if (s_name[0] != 0) {
  782. dirs++;
  783. doit = 1;
  784. }
  785. } else {
  786. dirc = ' ';
  787. if (s_name[0] != 0) {
  788. files++;
  789. doit = 1;
  790. }
  791. }
  792. if (doit) {
  793. if (dirc == ' ') {
  794. printf (" %8ld %s%c\n",
  795. (long) FAT2CPU32 (dentptr->size), s_name,
  796. dirc);
  797. } else {
  798. printf (" %s%c\n", s_name, dirc);
  799. }
  800. }
  801. dentptr++;
  802. continue;
  803. }
  804. if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) {
  805. FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
  806. dentptr++;
  807. continue;
  808. }
  809. if (isdir && !(dentptr->attr & ATTR_DIR))
  810. return -1;
  811. FAT_DPRINT ("RootName: %s", s_name);
  812. FAT_DPRINT (", start: 0x%x", START (dentptr));
  813. FAT_DPRINT (", size: 0x%x %s\n",
  814. FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
  815. goto rootdir_done; /* We got a match */
  816. }
  817. cursect++;
  818. }
  819. rootdir_done:
  820. firsttime = 1;
  821. while (isdir) {
  822. int startsect = mydata->data_begin
  823. + START (dentptr) * mydata->clust_size;
  824. dir_entry dent;
  825. char *nextname = NULL;
  826. dent = *dentptr;
  827. dentptr = &dent;
  828. idx = dirdelim (subname);
  829. if (idx >= 0) {
  830. subname[idx] = '\0';
  831. nextname = subname + idx + 1;
  832. /* Handle multiple delimiters */
  833. while (ISDIRDELIM (*nextname))
  834. nextname++;
  835. if (dols && *nextname == '\0')
  836. firsttime = 0;
  837. } else {
  838. if (dols && firsttime) {
  839. firsttime = 0;
  840. } else {
  841. isdir = 0;
  842. }
  843. }
  844. if (get_dentfromdir (mydata, startsect, subname, dentptr,
  845. isdir ? 0 : dols) == NULL) {
  846. if (dols && !isdir)
  847. return 0;
  848. return -1;
  849. }
  850. if (idx >= 0) {
  851. if (!(dentptr->attr & ATTR_DIR))
  852. return -1;
  853. subname = nextname;
  854. }
  855. }
  856. ret = get_contents (mydata, dentptr, buffer, maxsize);
  857. FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
  858. return ret;
  859. }
  860. int
  861. file_fat_detectfs(void)
  862. {
  863. boot_sector bs;
  864. volume_info volinfo;
  865. int fatsize;
  866. char vol_label[12];
  867. if(cur_dev==NULL) {
  868. printf("No current device\n");
  869. return 1;
  870. }
  871. #if defined(CONFIG_CMD_IDE) || \
  872. defined(CONFIG_CMD_MG_DISK) || \
  873. defined(CONFIG_CMD_SATA) || \
  874. defined(CONFIG_CMD_SCSI) || \
  875. defined(CONFIG_CMD_USB) || \
  876. defined(CONFIG_MMC)
  877. printf("Interface: ");
  878. switch(cur_dev->if_type) {
  879. case IF_TYPE_IDE : printf("IDE"); break;
  880. case IF_TYPE_SATA : printf("SATA"); break;
  881. case IF_TYPE_SCSI : printf("SCSI"); break;
  882. case IF_TYPE_ATAPI : printf("ATAPI"); break;
  883. case IF_TYPE_USB : printf("USB"); break;
  884. case IF_TYPE_DOC : printf("DOC"); break;
  885. case IF_TYPE_MMC : printf("MMC"); break;
  886. default : printf("Unknown");
  887. }
  888. printf("\n Device %d: ",cur_dev->dev);
  889. dev_print(cur_dev);
  890. #endif
  891. if(read_bootsectandvi(&bs, &volinfo, &fatsize)) {
  892. printf("\nNo valid FAT fs found\n");
  893. return 1;
  894. }
  895. memcpy (vol_label, volinfo.volume_label, 11);
  896. vol_label[11] = '\0';
  897. volinfo.fs_type[5]='\0';
  898. printf("Partition %d: Filesystem: %s \"%s\"\n"
  899. ,cur_part,volinfo.fs_type,vol_label);
  900. return 0;
  901. }
  902. int
  903. file_fat_ls(const char *dir)
  904. {
  905. return do_fat_read(dir, NULL, 0, LS_YES);
  906. }
  907. long
  908. file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
  909. {
  910. printf("reading %s\n",filename);
  911. return do_fat_read(filename, buffer, maxsize, LS_NO);
  912. }