fat.c 24 KB

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