fat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. if (disk_read(0, 1, block) < 0) {
  607. FAT_DPRINT("Error: reading block\n");
  608. return -1;
  609. }
  610. memcpy(bs, block, sizeof(boot_sector));
  611. bs->reserved = FAT2CPU16(bs->reserved);
  612. bs->fat_length = FAT2CPU16(bs->fat_length);
  613. bs->secs_track = FAT2CPU16(bs->secs_track);
  614. bs->heads = FAT2CPU16(bs->heads);
  615. #if 0 /* UNUSED */
  616. bs->hidden = FAT2CPU32(bs->hidden);
  617. #endif
  618. bs->total_sect = FAT2CPU32(bs->total_sect);
  619. /* FAT32 entries */
  620. if (bs->fat_length == 0) {
  621. /* Assume FAT32 */
  622. bs->fat32_length = FAT2CPU32(bs->fat32_length);
  623. bs->flags = FAT2CPU16(bs->flags);
  624. bs->root_cluster = FAT2CPU32(bs->root_cluster);
  625. bs->info_sector = FAT2CPU16(bs->info_sector);
  626. bs->backup_boot = FAT2CPU16(bs->backup_boot);
  627. vistart = (volume_info*) (block + sizeof(boot_sector));
  628. *fatsize = 32;
  629. } else {
  630. vistart = (volume_info*) &(bs->fat32_length);
  631. *fatsize = 0;
  632. }
  633. memcpy(volinfo, vistart, sizeof(volume_info));
  634. /* Terminate fs_type string. Writing past the end of vistart
  635. is ok - it's just the buffer. */
  636. vistart->fs_type[8] = '\0';
  637. if (*fatsize == 32) {
  638. if (compare_sign(FAT32_SIGN, vistart->fs_type) == 0) {
  639. return 0;
  640. }
  641. } else {
  642. if (compare_sign(FAT12_SIGN, vistart->fs_type) == 0) {
  643. *fatsize = 12;
  644. return 0;
  645. }
  646. if (compare_sign(FAT16_SIGN, vistart->fs_type) == 0) {
  647. *fatsize = 16;
  648. return 0;
  649. }
  650. }
  651. FAT_DPRINT("Error: broken fs_type sign\n");
  652. return -1;
  653. }
  654. __attribute__ ((__aligned__(__alignof__(dir_entry))))
  655. __u8 do_fat_read_block[MAX_CLUSTSIZE];
  656. long
  657. do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
  658. int dols)
  659. {
  660. #if CONFIG_NIOS /* NIOS CPU cannot access big automatic arrays */
  661. static
  662. #endif
  663. char fnamecopy[2048];
  664. boot_sector bs;
  665. volume_info volinfo;
  666. fsdata datablock;
  667. fsdata *mydata = &datablock;
  668. dir_entry *dentptr;
  669. __u16 prevcksum = 0xffff;
  670. char *subname = "";
  671. int rootdir_size, cursect;
  672. int idx, isdir = 0;
  673. int files = 0, dirs = 0;
  674. long ret = 0;
  675. int firsttime;
  676. if (read_bootsectandvi (&bs, &volinfo, &mydata->fatsize)) {
  677. FAT_DPRINT ("Error: reading boot sector\n");
  678. return -1;
  679. }
  680. if (mydata->fatsize == 32) {
  681. mydata->fatlength = bs.fat32_length;
  682. } else {
  683. mydata->fatlength = bs.fat_length;
  684. }
  685. mydata->fat_sect = bs.reserved;
  686. cursect = mydata->rootdir_sect
  687. = mydata->fat_sect + mydata->fatlength * bs.fats;
  688. mydata->clust_size = bs.cluster_size;
  689. if (mydata->fatsize == 32) {
  690. rootdir_size = mydata->clust_size;
  691. mydata->data_begin = mydata->rootdir_sect /* + rootdir_size */
  692. - (mydata->clust_size * 2);
  693. } else {
  694. rootdir_size = ((bs.dir_entries[1] * (int) 256 + bs.dir_entries[0])
  695. * sizeof (dir_entry)) / SECTOR_SIZE;
  696. mydata->data_begin = mydata->rootdir_sect + rootdir_size
  697. - (mydata->clust_size * 2);
  698. }
  699. mydata->fatbufnum = -1;
  700. FAT_DPRINT ("FAT%d, fatlength: %d\n", mydata->fatsize,
  701. mydata->fatlength);
  702. FAT_DPRINT ("Rootdir begins at sector: %d, offset: %x, size: %d\n"
  703. "Data begins at: %d\n",
  704. mydata->rootdir_sect, mydata->rootdir_sect * SECTOR_SIZE,
  705. rootdir_size, mydata->data_begin);
  706. FAT_DPRINT ("Cluster size: %d\n", mydata->clust_size);
  707. /* "cwd" is always the root... */
  708. while (ISDIRDELIM (*filename))
  709. filename++;
  710. /* Make a copy of the filename and convert it to lowercase */
  711. strcpy (fnamecopy, filename);
  712. downcase (fnamecopy);
  713. if (*fnamecopy == '\0') {
  714. if (!dols)
  715. return -1;
  716. dols = LS_ROOT;
  717. } else if ((idx = dirdelim (fnamecopy)) >= 0) {
  718. isdir = 1;
  719. fnamecopy[idx] = '\0';
  720. subname = fnamecopy + idx + 1;
  721. /* Handle multiple delimiters */
  722. while (ISDIRDELIM (*subname))
  723. subname++;
  724. } else if (dols) {
  725. isdir = 1;
  726. }
  727. while (1) {
  728. int i;
  729. if (disk_read (cursect, mydata->clust_size, do_fat_read_block) < 0) {
  730. FAT_DPRINT ("Error: reading rootdir block\n");
  731. return -1;
  732. }
  733. dentptr = (dir_entry *) do_fat_read_block;
  734. for (i = 0; i < DIRENTSPERBLOCK; i++) {
  735. char s_name[14], l_name[256];
  736. l_name[0] = '\0';
  737. if ((dentptr->attr & ATTR_VOLUME)) {
  738. #ifdef CONFIG_SUPPORT_VFAT
  739. if ((dentptr->attr & ATTR_VFAT) &&
  740. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  741. prevcksum = ((dir_slot *) dentptr)->alias_checksum;
  742. get_vfatname (mydata, 0, do_fat_read_block, dentptr, l_name);
  743. if (dols == LS_ROOT) {
  744. int isdir = (dentptr->attr & ATTR_DIR);
  745. char dirc;
  746. int doit = 0;
  747. if (isdir) {
  748. dirs++;
  749. dirc = '/';
  750. doit = 1;
  751. } else {
  752. dirc = ' ';
  753. if (l_name[0] != 0) {
  754. files++;
  755. doit = 1;
  756. }
  757. }
  758. if (doit) {
  759. if (dirc == ' ') {
  760. printf (" %8ld %s%c\n",
  761. (long) FAT2CPU32 (dentptr->size),
  762. l_name, dirc);
  763. } else {
  764. printf (" %s%c\n", l_name, dirc);
  765. }
  766. }
  767. dentptr++;
  768. continue;
  769. }
  770. FAT_DPRINT ("Rootvfatname: |%s|\n", l_name);
  771. } else
  772. #endif
  773. {
  774. /* Volume label or VFAT entry */
  775. dentptr++;
  776. continue;
  777. }
  778. } else if (dentptr->name[0] == 0) {
  779. FAT_DPRINT ("RootDentname == NULL - %d\n", i);
  780. if (dols == LS_ROOT) {
  781. printf ("\n%d file(s), %d dir(s)\n\n", files, dirs);
  782. return 0;
  783. }
  784. return -1;
  785. }
  786. #ifdef CONFIG_SUPPORT_VFAT
  787. else if (dols == LS_ROOT
  788. && mkcksum (dentptr->name) == prevcksum) {
  789. dentptr++;
  790. continue;
  791. }
  792. #endif
  793. get_name (dentptr, s_name);
  794. if (dols == LS_ROOT) {
  795. int isdir = (dentptr->attr & ATTR_DIR);
  796. char dirc;
  797. int doit = 0;
  798. if (isdir) {
  799. dirc = '/';
  800. if (s_name[0] != 0) {
  801. dirs++;
  802. doit = 1;
  803. }
  804. } else {
  805. dirc = ' ';
  806. if (s_name[0] != 0) {
  807. files++;
  808. doit = 1;
  809. }
  810. }
  811. if (doit) {
  812. if (dirc == ' ') {
  813. printf (" %8ld %s%c\n",
  814. (long) FAT2CPU32 (dentptr->size), s_name,
  815. dirc);
  816. } else {
  817. printf (" %s%c\n", s_name, dirc);
  818. }
  819. }
  820. dentptr++;
  821. continue;
  822. }
  823. if (strcmp (fnamecopy, s_name) && strcmp (fnamecopy, l_name)) {
  824. FAT_DPRINT ("RootMismatch: |%s|%s|\n", s_name, l_name);
  825. dentptr++;
  826. continue;
  827. }
  828. if (isdir && !(dentptr->attr & ATTR_DIR))
  829. return -1;
  830. FAT_DPRINT ("RootName: %s", s_name);
  831. FAT_DPRINT (", start: 0x%x", START (dentptr));
  832. FAT_DPRINT (", size: 0x%x %s\n",
  833. FAT2CPU32 (dentptr->size), isdir ? "(DIR)" : "");
  834. goto rootdir_done; /* We got a match */
  835. }
  836. cursect++;
  837. }
  838. rootdir_done:
  839. firsttime = 1;
  840. while (isdir) {
  841. int startsect = mydata->data_begin
  842. + START (dentptr) * mydata->clust_size;
  843. dir_entry dent;
  844. char *nextname = NULL;
  845. dent = *dentptr;
  846. dentptr = &dent;
  847. idx = dirdelim (subname);
  848. if (idx >= 0) {
  849. subname[idx] = '\0';
  850. nextname = subname + idx + 1;
  851. /* Handle multiple delimiters */
  852. while (ISDIRDELIM (*nextname))
  853. nextname++;
  854. if (dols && *nextname == '\0')
  855. firsttime = 0;
  856. } else {
  857. if (dols && firsttime) {
  858. firsttime = 0;
  859. } else {
  860. isdir = 0;
  861. }
  862. }
  863. if (get_dentfromdir (mydata, startsect, subname, dentptr,
  864. isdir ? 0 : dols) == NULL) {
  865. if (dols && !isdir)
  866. return 0;
  867. return -1;
  868. }
  869. if (idx >= 0) {
  870. if (!(dentptr->attr & ATTR_DIR))
  871. return -1;
  872. subname = nextname;
  873. }
  874. }
  875. ret = get_contents (mydata, dentptr, buffer, maxsize);
  876. FAT_DPRINT ("Size: %d, got: %ld\n", FAT2CPU32 (dentptr->size), ret);
  877. return ret;
  878. }
  879. int
  880. file_fat_detectfs(void)
  881. {
  882. boot_sector bs;
  883. volume_info volinfo;
  884. int fatsize;
  885. char vol_label[12];
  886. if(cur_dev==NULL) {
  887. printf("No current device\n");
  888. return 1;
  889. }
  890. #if defined(CONFIG_CMD_IDE) || \
  891. defined(CONFIG_CMD_SATA) || \
  892. defined(CONFIG_CMD_SCSI) || \
  893. defined(CONFIG_CMD_USB) || \
  894. defined(CONFIG_MMC)
  895. printf("Interface: ");
  896. switch(cur_dev->if_type) {
  897. case IF_TYPE_IDE : printf("IDE"); break;
  898. case IF_TYPE_SATA : printf("SATA"); 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. }