fat.c 24 KB

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