fat.c 24 KB

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