fat.c 23 KB

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