fat.c 24 KB

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