fat.c 23 KB

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