fat.c 25 KB

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