fat.c 24 KB

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