fat.c 24 KB

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