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