fat.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  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 <exports.h>
  30. #include <fat.h>
  31. #include <asm/byteorder.h>
  32. #include <part.h>
  33. /*
  34. * Convert a string to lowercase.
  35. */
  36. static void downcase (char *str)
  37. {
  38. while (*str != '\0') {
  39. TOLOWER(*str);
  40. str++;
  41. }
  42. }
  43. static block_dev_desc_t *cur_dev = NULL;
  44. static unsigned long part_offset = 0;
  45. static int cur_part = 1;
  46. #define DOS_PART_TBL_OFFSET 0x1be
  47. #define DOS_PART_MAGIC_OFFSET 0x1fe
  48. #define DOS_FS_TYPE_OFFSET 0x36
  49. #define DOS_FS32_TYPE_OFFSET 0x52
  50. static int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
  51. {
  52. if (cur_dev == NULL)
  53. return -1;
  54. startblock += part_offset;
  55. if (cur_dev->block_read) {
  56. return cur_dev->block_read(cur_dev->dev, startblock, getsize,
  57. (unsigned long *) bufptr);
  58. }
  59. return -1;
  60. }
  61. int fat_register_device (block_dev_desc_t * dev_desc, int part_no)
  62. {
  63. unsigned char buffer[dev_desc->blksz];
  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",
  71. dev_desc->dev);
  72. return -1;
  73. }
  74. if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
  75. buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
  76. /* no signature found */
  77. return -1;
  78. }
  79. #if (defined(CONFIG_CMD_IDE) || \
  80. defined(CONFIG_CMD_MG_DISK) || \
  81. defined(CONFIG_CMD_SATA) || \
  82. defined(CONFIG_CMD_SCSI) || \
  83. defined(CONFIG_CMD_USB) || \
  84. defined(CONFIG_MMC) || \
  85. defined(CONFIG_SYSTEMACE) )
  86. /* First we assume there is a MBR */
  87. if (!get_partition_info(dev_desc, part_no, &info)) {
  88. part_offset = info.start;
  89. cur_part = part_no;
  90. } else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) ||
  91. (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) {
  92. /* ok, we assume we are on a PBR only */
  93. cur_part = 1;
  94. part_offset = 0;
  95. } else {
  96. printf("** Partition %d not valid on device %d **\n",
  97. part_no, dev_desc->dev);
  98. return -1;
  99. }
  100. #else
  101. if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) ||
  102. (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) {
  103. /* ok, we assume we are on a PBR only */
  104. cur_part = 1;
  105. part_offset = 0;
  106. info.start = part_offset;
  107. } else {
  108. /* FIXME we need to determine the start block of the
  109. * partition where the DOS FS resides. This can be done
  110. * by using the get_partition_info routine. For this
  111. * purpose the libpart must be included.
  112. */
  113. part_offset = 32;
  114. cur_part = 1;
  115. }
  116. #endif
  117. return 0;
  118. }
  119. /*
  120. * Get the first occurence of a directory delimiter ('/' or '\') in a string.
  121. * Return index into string if found, -1 otherwise.
  122. */
  123. static int dirdelim (char *str)
  124. {
  125. char *start = str;
  126. while (*str != '\0') {
  127. if (ISDIRDELIM(*str))
  128. return str - start;
  129. str++;
  130. }
  131. return -1;
  132. }
  133. /*
  134. * Extract zero terminated short name from a directory entry.
  135. */
  136. static void get_name (dir_entry *dirent, char *s_name)
  137. {
  138. char *ptr;
  139. memcpy(s_name, dirent->name, 8);
  140. s_name[8] = '\0';
  141. ptr = s_name;
  142. while (*ptr && *ptr != ' ')
  143. ptr++;
  144. if (dirent->ext[0] && dirent->ext[0] != ' ') {
  145. *ptr = '.';
  146. ptr++;
  147. memcpy(ptr, dirent->ext, 3);
  148. ptr[3] = '\0';
  149. while (*ptr && *ptr != ' ')
  150. ptr++;
  151. }
  152. *ptr = '\0';
  153. if (*s_name == DELETED_FLAG)
  154. *s_name = '\0';
  155. else if (*s_name == aRING)
  156. *s_name = DELETED_FLAG;
  157. downcase(s_name);
  158. }
  159. /*
  160. * Get the entry at index 'entry' in a FAT (12/16/32) table.
  161. * On failure 0x00 is returned.
  162. */
  163. static __u32 get_fatent (fsdata *mydata, __u32 entry)
  164. {
  165. __u32 bufnum;
  166. __u32 off16, offset;
  167. __u32 ret = 0x00;
  168. __u16 val1, val2;
  169. switch (mydata->fatsize) {
  170. case 32:
  171. bufnum = entry / FAT32BUFSIZE;
  172. offset = entry - bufnum * FAT32BUFSIZE;
  173. break;
  174. case 16:
  175. bufnum = entry / FAT16BUFSIZE;
  176. offset = entry - bufnum * FAT16BUFSIZE;
  177. break;
  178. case 12:
  179. bufnum = entry / FAT12BUFSIZE;
  180. offset = entry - bufnum * FAT12BUFSIZE;
  181. break;
  182. default:
  183. /* Unsupported FAT size */
  184. return ret;
  185. }
  186. debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
  187. mydata->fatsize, entry, entry, offset, offset);
  188. /* Read a new block of FAT entries into the cache. */
  189. if (bufnum != mydata->fatbufnum) {
  190. __u32 getsize = FATBUFBLOCKS;
  191. __u8 *bufptr = mydata->fatbuf;
  192. __u32 fatlength = mydata->fatlength;
  193. __u32 startblock = bufnum * FATBUFBLOCKS;
  194. if (getsize > fatlength)
  195. getsize = fatlength;
  196. fatlength *= mydata->sect_size; /* We want it in bytes now */
  197. startblock += mydata->fat_sect; /* Offset from start of disk */
  198. if (disk_read(startblock, getsize, bufptr) < 0) {
  199. debug("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. off16 = (offset * 3) / 4;
  214. switch (offset & 0x3) {
  215. case 0:
  216. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
  217. ret &= 0xfff;
  218. break;
  219. case 1:
  220. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  221. val1 &= 0xf000;
  222. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  223. val2 &= 0x00ff;
  224. ret = (val2 << 4) | (val1 >> 12);
  225. break;
  226. case 2:
  227. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  228. val1 &= 0xff00;
  229. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  230. val2 &= 0x000f;
  231. ret = (val2 << 8) | (val1 >> 8);
  232. break;
  233. case 3:
  234. ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  235. ret = (ret & 0xfff0) >> 4;
  236. break;
  237. default:
  238. break;
  239. }
  240. break;
  241. }
  242. debug("FAT%d: ret: %08x, offset: %04x\n",
  243. mydata->fatsize, ret, offset);
  244. return ret;
  245. }
  246. /*
  247. * Read at most 'size' bytes from the specified cluster into 'buffer'.
  248. * Return 0 on success, -1 otherwise.
  249. */
  250. static int
  251. get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
  252. unsigned long size)
  253. {
  254. __u32 idx = 0;
  255. __u32 startsect;
  256. if (clustnum > 0) {
  257. startsect = mydata->data_begin +
  258. clustnum * mydata->clust_size;
  259. } else {
  260. startsect = mydata->rootdir_sect;
  261. }
  262. debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
  263. if (disk_read(startsect, size / mydata->sect_size, buffer) < 0) {
  264. debug("Error reading data\n");
  265. return -1;
  266. }
  267. if (size % mydata->sect_size) {
  268. __u8 tmpbuf[mydata->sect_size];
  269. idx = size / mydata->sect_size;
  270. if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
  271. debug("Error reading data\n");
  272. return -1;
  273. }
  274. buffer += idx * mydata->sect_size;
  275. memcpy(buffer, tmpbuf, size % mydata->sect_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 * mydata->sect_size;
  291. __u32 curclust = START(dentptr);
  292. __u32 endclust, newclust;
  293. unsigned long actsize;
  294. debug("Filesize: %ld bytes\n", filesize);
  295. if (maxsize > 0 && filesize > maxsize)
  296. filesize = maxsize;
  297. debug("%ld bytes\n", filesize);
  298. actsize = bytesperclust;
  299. endclust = curclust;
  300. do {
  301. /* search for consecutive clusters */
  302. while (actsize < filesize) {
  303. newclust = get_fatent(mydata, endclust);
  304. if ((newclust - 1) != endclust)
  305. goto getit;
  306. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  307. debug("curclust: 0x%x\n", newclust);
  308. debug("Invalid FAT entry\n");
  309. return gotsize;
  310. }
  311. endclust = newclust;
  312. actsize += bytesperclust;
  313. }
  314. /* actsize >= file size */
  315. actsize -= bytesperclust;
  316. /* get remaining clusters */
  317. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  318. printf("Error reading cluster\n");
  319. return -1;
  320. }
  321. /* get remaining bytes */
  322. gotsize += (int)actsize;
  323. filesize -= actsize;
  324. buffer += actsize;
  325. actsize = filesize;
  326. if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
  327. printf("Error reading cluster\n");
  328. return -1;
  329. }
  330. gotsize += actsize;
  331. return gotsize;
  332. getit:
  333. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  334. printf("Error reading cluster\n");
  335. return -1;
  336. }
  337. gotsize += (int)actsize;
  338. filesize -= actsize;
  339. buffer += actsize;
  340. curclust = get_fatent(mydata, endclust);
  341. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  342. debug("curclust: 0x%x\n", curclust);
  343. printf("Invalid FAT entry\n");
  344. return gotsize;
  345. }
  346. actsize = bytesperclust;
  347. endclust = curclust;
  348. } while (1);
  349. }
  350. #ifdef CONFIG_SUPPORT_VFAT
  351. /*
  352. * Extract the file name information from 'slotptr' into 'l_name',
  353. * starting at l_name[*idx].
  354. * Return 1 if terminator (zero byte) is found, 0 otherwise.
  355. */
  356. static int 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)
  362. return 1;
  363. (*idx)++;
  364. }
  365. for (j = 0; j <= 10; j += 2) {
  366. l_name[*idx] = slotptr->name5_10[j];
  367. if (l_name[*idx] == 0x00)
  368. return 1;
  369. (*idx)++;
  370. }
  371. for (j = 0; j <= 2; j += 2) {
  372. l_name[*idx] = slotptr->name11_12[j];
  373. if (l_name[*idx] == 0x00)
  374. return 1;
  375. (*idx)++;
  376. }
  377. return 0;
  378. }
  379. /*
  380. * Extract the full long filename starting at 'retdent' (which is really
  381. * a slot) into 'l_name'. If successful also copy the real directory entry
  382. * into 'retdent'
  383. * Return 0 on success, -1 otherwise.
  384. */
  385. __attribute__ ((__aligned__ (__alignof__ (dir_entry))))
  386. __u8 get_vfatname_block[MAX_CLUSTSIZE];
  387. static int
  388. get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
  389. dir_entry *retdent, char *l_name)
  390. {
  391. dir_entry *realdent;
  392. dir_slot *slotptr = (dir_slot *)retdent;
  393. __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
  394. PREFETCH_BLOCKS :
  395. mydata->clust_size);
  396. __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
  397. int idx = 0;
  398. if (counter > VFAT_MAXSEQ) {
  399. debug("Error: VFAT name is too long\n");
  400. return -1;
  401. }
  402. while ((__u8 *)slotptr < buflimit) {
  403. if (counter == 0)
  404. break;
  405. if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
  406. return -1;
  407. slotptr++;
  408. counter--;
  409. }
  410. if ((__u8 *)slotptr >= buflimit) {
  411. dir_slot *slotptr2;
  412. if (curclust == 0)
  413. return -1;
  414. curclust = get_fatent(mydata, curclust);
  415. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  416. debug("curclust: 0x%x\n", curclust);
  417. printf("Invalid FAT entry\n");
  418. return -1;
  419. }
  420. if (get_cluster(mydata, curclust, get_vfatname_block,
  421. mydata->clust_size * mydata->sect_size) != 0) {
  422. debug("Error: reading directory block\n");
  423. return -1;
  424. }
  425. slotptr2 = (dir_slot *)get_vfatname_block;
  426. while (counter > 0) {
  427. if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
  428. & 0xff) != counter)
  429. return -1;
  430. slotptr2++;
  431. counter--;
  432. }
  433. /* Save the real directory entry */
  434. realdent = (dir_entry *)slotptr2;
  435. while ((__u8 *)slotptr2 > get_vfatname_block) {
  436. slotptr2--;
  437. slot2str(slotptr2, l_name, &idx);
  438. }
  439. } else {
  440. /* Save the real directory entry */
  441. realdent = (dir_entry *)slotptr;
  442. }
  443. do {
  444. slotptr--;
  445. if (slot2str(slotptr, l_name, &idx))
  446. break;
  447. } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
  448. l_name[idx] = '\0';
  449. if (*l_name == DELETED_FLAG)
  450. *l_name = '\0';
  451. else if (*l_name == aRING)
  452. *l_name = DELETED_FLAG;
  453. downcase(l_name);
  454. /* Return the real directory entry */
  455. memcpy(retdent, realdent, sizeof(dir_entry));
  456. return 0;
  457. }
  458. /* Calculate short name checksum */
  459. static __u8 mkcksum (const char *str)
  460. {
  461. int i;
  462. __u8 ret = 0;
  463. for (i = 0; i < 11; i++) {
  464. ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
  465. }
  466. return ret;
  467. }
  468. #endif /* CONFIG_SUPPORT_VFAT */
  469. /*
  470. * Get the directory entry associated with 'filename' from the directory
  471. * starting at 'startsect'
  472. */
  473. __attribute__ ((__aligned__ (__alignof__ (dir_entry))))
  474. __u8 get_dentfromdir_block[MAX_CLUSTSIZE];
  475. static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
  476. char *filename, dir_entry *retdent,
  477. int dols)
  478. {
  479. __u16 prevcksum = 0xffff;
  480. __u32 curclust = START(retdent);
  481. int files = 0, dirs = 0;
  482. debug("get_dentfromdir: %s\n", filename);
  483. while (1) {
  484. dir_entry *dentptr;
  485. int i;
  486. if (get_cluster(mydata, curclust, get_dentfromdir_block,
  487. mydata->clust_size * mydata->sect_size) != 0) {
  488. debug("Error: reading directory block\n");
  489. return NULL;
  490. }
  491. dentptr = (dir_entry *)get_dentfromdir_block;
  492. for (i = 0; i < DIRENTSPERCLUST; i++) {
  493. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  494. l_name[0] = '\0';
  495. if (dentptr->name[0] == DELETED_FLAG) {
  496. dentptr++;
  497. continue;
  498. }
  499. if ((dentptr->attr & ATTR_VOLUME)) {
  500. #ifdef CONFIG_SUPPORT_VFAT
  501. if ((dentptr->attr & ATTR_VFAT) &&
  502. (dentptr-> name[0] & LAST_LONG_ENTRY_MASK)) {
  503. prevcksum = ((dir_slot *)dentptr)->alias_checksum;
  504. get_vfatname(mydata, curclust,
  505. get_dentfromdir_block,
  506. dentptr, l_name);
  507. if (dols) {
  508. int isdir;
  509. char dirc;
  510. int doit = 0;
  511. isdir = (dentptr->attr & ATTR_DIR);
  512. if (isdir) {
  513. dirs++;
  514. dirc = '/';
  515. doit = 1;
  516. } else {
  517. dirc = ' ';
  518. if (l_name[0] != 0) {
  519. files++;
  520. doit = 1;
  521. }
  522. }
  523. if (doit) {
  524. if (dirc == ' ') {
  525. printf(" %8ld %s%c\n",
  526. (long)FAT2CPU32(dentptr->size),
  527. l_name,
  528. dirc);
  529. } else {
  530. printf(" %s%c\n",
  531. l_name,
  532. dirc);
  533. }
  534. }
  535. dentptr++;
  536. continue;
  537. }
  538. debug("vfatname: |%s|\n", l_name);
  539. } else
  540. #endif
  541. {
  542. /* Volume label or VFAT entry */
  543. dentptr++;
  544. continue;
  545. }
  546. }
  547. if (dentptr->name[0] == 0) {
  548. if (dols) {
  549. printf("\n%d file(s), %d dir(s)\n\n",
  550. files, dirs);
  551. }
  552. debug("Dentname == NULL - %d\n", i);
  553. return NULL;
  554. }
  555. #ifdef CONFIG_SUPPORT_VFAT
  556. if (dols && mkcksum(dentptr->name) == prevcksum) {
  557. dentptr++;
  558. continue;
  559. }
  560. #endif
  561. get_name(dentptr, s_name);
  562. if (dols) {
  563. int isdir = (dentptr->attr & ATTR_DIR);
  564. char dirc;
  565. int doit = 0;
  566. if (isdir) {
  567. dirs++;
  568. dirc = '/';
  569. doit = 1;
  570. } else {
  571. dirc = ' ';
  572. if (s_name[0] != 0) {
  573. files++;
  574. doit = 1;
  575. }
  576. }
  577. if (doit) {
  578. if (dirc == ' ') {
  579. printf(" %8ld %s%c\n",
  580. (long)FAT2CPU32(dentptr->size),
  581. s_name, dirc);
  582. } else {
  583. printf(" %s%c\n",
  584. s_name, dirc);
  585. }
  586. }
  587. dentptr++;
  588. continue;
  589. }
  590. if (strcmp(filename, s_name)
  591. && strcmp(filename, l_name)) {
  592. debug("Mismatch: |%s|%s|\n", s_name, l_name);
  593. dentptr++;
  594. continue;
  595. }
  596. memcpy(retdent, dentptr, sizeof(dir_entry));
  597. debug("DentName: %s", s_name);
  598. debug(", start: 0x%x", START(dentptr));
  599. debug(", size: 0x%x %s\n",
  600. FAT2CPU32(dentptr->size),
  601. (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
  602. return retdent;
  603. }
  604. curclust = get_fatent(mydata, curclust);
  605. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  606. debug("curclust: 0x%x\n", curclust);
  607. printf("Invalid FAT entry\n");
  608. return NULL;
  609. }
  610. }
  611. return NULL;
  612. }
  613. /*
  614. * Read boot sector and volume info from a FAT filesystem
  615. */
  616. static int
  617. read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
  618. {
  619. __u8 *block;
  620. volume_info *vistart;
  621. int ret = 0;
  622. if (cur_dev == NULL) {
  623. debug("Error: no device selected\n");
  624. return -1;
  625. }
  626. block = malloc(cur_dev->blksz);
  627. if (block == NULL) {
  628. debug("Error: allocating block\n");
  629. return -1;
  630. }
  631. if (disk_read (0, 1, block) < 0) {
  632. debug("Error: reading block\n");
  633. goto fail;
  634. }
  635. memcpy(bs, block, sizeof(boot_sector));
  636. bs->reserved = FAT2CPU16(bs->reserved);
  637. bs->fat_length = FAT2CPU16(bs->fat_length);
  638. bs->secs_track = FAT2CPU16(bs->secs_track);
  639. bs->heads = FAT2CPU16(bs->heads);
  640. bs->total_sect = FAT2CPU32(bs->total_sect);
  641. /* FAT32 entries */
  642. if (bs->fat_length == 0) {
  643. /* Assume FAT32 */
  644. bs->fat32_length = FAT2CPU32(bs->fat32_length);
  645. bs->flags = FAT2CPU16(bs->flags);
  646. bs->root_cluster = FAT2CPU32(bs->root_cluster);
  647. bs->info_sector = FAT2CPU16(bs->info_sector);
  648. bs->backup_boot = FAT2CPU16(bs->backup_boot);
  649. vistart = (volume_info *)(block + sizeof(boot_sector));
  650. *fatsize = 32;
  651. } else {
  652. vistart = (volume_info *)&(bs->fat32_length);
  653. *fatsize = 0;
  654. }
  655. memcpy(volinfo, vistart, sizeof(volume_info));
  656. if (*fatsize == 32) {
  657. if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
  658. goto exit;
  659. } else {
  660. if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  661. *fatsize = 12;
  662. goto exit;
  663. }
  664. if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  665. *fatsize = 16;
  666. goto exit;
  667. }
  668. }
  669. debug("Error: broken fs_type sign\n");
  670. fail:
  671. ret = -1;
  672. exit:
  673. free(block);
  674. return ret;
  675. }
  676. __attribute__ ((__aligned__ (__alignof__ (dir_entry))))
  677. __u8 do_fat_read_block[MAX_CLUSTSIZE];
  678. long
  679. do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
  680. int dols)
  681. {
  682. char fnamecopy[2048];
  683. boot_sector bs;
  684. volume_info volinfo;
  685. fsdata datablock;
  686. fsdata *mydata = &datablock;
  687. dir_entry *dentptr;
  688. __u16 prevcksum = 0xffff;
  689. char *subname = "";
  690. __u32 cursect;
  691. int idx, isdir = 0;
  692. int files = 0, dirs = 0;
  693. long ret = -1;
  694. int firsttime;
  695. __u32 root_cluster = 0;
  696. int rootdir_size = 0;
  697. int j;
  698. if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
  699. debug("Error: reading boot sector\n");
  700. return -1;
  701. }
  702. if (mydata->fatsize == 32) {
  703. root_cluster = bs.root_cluster;
  704. mydata->fatlength = bs.fat32_length;
  705. } else {
  706. mydata->fatlength = bs.fat_length;
  707. }
  708. mydata->fat_sect = bs.reserved;
  709. cursect = mydata->rootdir_sect
  710. = mydata->fat_sect + mydata->fatlength * bs.fats;
  711. mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
  712. mydata->clust_size = bs.cluster_size;
  713. if (mydata->fatsize == 32) {
  714. mydata->data_begin = mydata->rootdir_sect -
  715. (mydata->clust_size * 2);
  716. } else {
  717. rootdir_size = ((bs.dir_entries[1] * (int)256 +
  718. bs.dir_entries[0]) *
  719. sizeof(dir_entry)) /
  720. mydata->sect_size;
  721. mydata->data_begin = mydata->rootdir_sect +
  722. rootdir_size -
  723. (mydata->clust_size * 2);
  724. }
  725. mydata->fatbufnum = -1;
  726. mydata->fatbuf = malloc(FATBUFSIZE);
  727. if (mydata->fatbuf == NULL) {
  728. debug("Error: allocating memory\n");
  729. return -1;
  730. }
  731. #ifdef CONFIG_SUPPORT_VFAT
  732. debug("VFAT Support enabled\n");
  733. #endif
  734. debug("FAT%d, fat_sect: %d, fatlength: %d\n",
  735. mydata->fatsize, mydata->fat_sect, mydata->fatlength);
  736. debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
  737. "Data begins at: %d\n",
  738. root_cluster,
  739. mydata->rootdir_sect,
  740. mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
  741. debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
  742. mydata->clust_size);
  743. /* "cwd" is always the root... */
  744. while (ISDIRDELIM(*filename))
  745. filename++;
  746. /* Make a copy of the filename and convert it to lowercase */
  747. strcpy(fnamecopy, filename);
  748. downcase(fnamecopy);
  749. if (*fnamecopy == '\0') {
  750. if (!dols)
  751. goto exit;
  752. dols = LS_ROOT;
  753. } else if ((idx = dirdelim(fnamecopy)) >= 0) {
  754. isdir = 1;
  755. fnamecopy[idx] = '\0';
  756. subname = fnamecopy + idx + 1;
  757. /* Handle multiple delimiters */
  758. while (ISDIRDELIM(*subname))
  759. subname++;
  760. } else if (dols) {
  761. isdir = 1;
  762. }
  763. j = 0;
  764. while (1) {
  765. int i;
  766. debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%d\n",
  767. cursect, mydata->clust_size, DIRENTSPERBLOCK);
  768. if (disk_read(cursect,
  769. (mydata->fatsize == 32) ?
  770. (mydata->clust_size) :
  771. PREFETCH_BLOCKS,
  772. do_fat_read_block) < 0) {
  773. debug("Error: reading rootdir block\n");
  774. goto exit;
  775. }
  776. dentptr = (dir_entry *) do_fat_read_block;
  777. for (i = 0; i < DIRENTSPERBLOCK; i++) {
  778. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  779. l_name[0] = '\0';
  780. if (dentptr->name[0] == DELETED_FLAG) {
  781. dentptr++;
  782. continue;
  783. }
  784. if ((dentptr->attr & ATTR_VOLUME)) {
  785. #ifdef CONFIG_SUPPORT_VFAT
  786. if ((dentptr->attr & ATTR_VFAT) &&
  787. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  788. prevcksum =
  789. ((dir_slot *)dentptr)->alias_checksum;
  790. get_vfatname(mydata,
  791. root_cluster,
  792. do_fat_read_block,
  793. dentptr, l_name);
  794. if (dols == LS_ROOT) {
  795. char dirc;
  796. int doit = 0;
  797. int isdir =
  798. (dentptr->attr & ATTR_DIR);
  799. if (isdir) {
  800. dirs++;
  801. dirc = '/';
  802. doit = 1;
  803. } else {
  804. dirc = ' ';
  805. if (l_name[0] != 0) {
  806. files++;
  807. doit = 1;
  808. }
  809. }
  810. if (doit) {
  811. if (dirc == ' ') {
  812. printf(" %8ld %s%c\n",
  813. (long)FAT2CPU32(dentptr->size),
  814. l_name,
  815. dirc);
  816. } else {
  817. printf(" %s%c\n",
  818. l_name,
  819. dirc);
  820. }
  821. }
  822. dentptr++;
  823. continue;
  824. }
  825. debug("Rootvfatname: |%s|\n",
  826. l_name);
  827. } else
  828. #endif
  829. {
  830. /* Volume label or VFAT entry */
  831. dentptr++;
  832. continue;
  833. }
  834. } else if (dentptr->name[0] == 0) {
  835. debug("RootDentname == NULL - %d\n", i);
  836. if (dols == LS_ROOT) {
  837. printf("\n%d file(s), %d dir(s)\n\n",
  838. files, dirs);
  839. ret = 0;
  840. }
  841. goto exit;
  842. }
  843. #ifdef CONFIG_SUPPORT_VFAT
  844. else if (dols == LS_ROOT &&
  845. mkcksum(dentptr->name) == prevcksum) {
  846. dentptr++;
  847. continue;
  848. }
  849. #endif
  850. get_name(dentptr, s_name);
  851. if (dols == LS_ROOT) {
  852. int isdir = (dentptr->attr & ATTR_DIR);
  853. char dirc;
  854. int doit = 0;
  855. if (isdir) {
  856. dirc = '/';
  857. if (s_name[0] != 0) {
  858. dirs++;
  859. doit = 1;
  860. }
  861. } else {
  862. dirc = ' ';
  863. if (s_name[0] != 0) {
  864. files++;
  865. doit = 1;
  866. }
  867. }
  868. if (doit) {
  869. if (dirc == ' ') {
  870. printf(" %8ld %s%c\n",
  871. (long)FAT2CPU32(dentptr->size),
  872. s_name, dirc);
  873. } else {
  874. printf(" %s%c\n",
  875. s_name, dirc);
  876. }
  877. }
  878. dentptr++;
  879. continue;
  880. }
  881. if (strcmp(fnamecopy, s_name)
  882. && strcmp(fnamecopy, l_name)) {
  883. debug("RootMismatch: |%s|%s|\n", s_name,
  884. l_name);
  885. dentptr++;
  886. continue;
  887. }
  888. if (isdir && !(dentptr->attr & ATTR_DIR))
  889. goto exit;
  890. debug("RootName: %s", s_name);
  891. debug(", start: 0x%x", START(dentptr));
  892. debug(", size: 0x%x %s\n",
  893. FAT2CPU32(dentptr->size),
  894. isdir ? "(DIR)" : "");
  895. goto rootdir_done; /* We got a match */
  896. }
  897. debug("END LOOP: j=%d clust_size=%d\n", j,
  898. mydata->clust_size);
  899. /*
  900. * On FAT32 we must fetch the FAT entries for the next
  901. * root directory clusters when a cluster has been
  902. * completely processed.
  903. */
  904. ++j;
  905. int fat32_end = 0;
  906. if ((mydata->fatsize == 32) && (j == mydata->clust_size)) {
  907. int nxtsect = 0;
  908. int nxt_clust = 0;
  909. nxt_clust = get_fatent(mydata, root_cluster);
  910. fat32_end = CHECK_CLUST(nxt_clust, 32);
  911. nxtsect = mydata->data_begin +
  912. (nxt_clust * mydata->clust_size);
  913. root_cluster = nxt_clust;
  914. cursect = nxtsect;
  915. j = 0;
  916. } else {
  917. cursect++;
  918. }
  919. /* If end of rootdir reached */
  920. if ((mydata->fatsize == 32 && fat32_end) ||
  921. (mydata->fatsize != 32 && j == rootdir_size)) {
  922. if (dols == LS_ROOT) {
  923. printf("\n%d file(s), %d dir(s)\n\n",
  924. files, dirs);
  925. ret = 0;
  926. }
  927. goto exit;
  928. }
  929. }
  930. rootdir_done:
  931. firsttime = 1;
  932. while (isdir) {
  933. int startsect = mydata->data_begin
  934. + START(dentptr) * mydata->clust_size;
  935. dir_entry dent;
  936. char *nextname = NULL;
  937. dent = *dentptr;
  938. dentptr = &dent;
  939. idx = dirdelim(subname);
  940. if (idx >= 0) {
  941. subname[idx] = '\0';
  942. nextname = subname + idx + 1;
  943. /* Handle multiple delimiters */
  944. while (ISDIRDELIM(*nextname))
  945. nextname++;
  946. if (dols && *nextname == '\0')
  947. firsttime = 0;
  948. } else {
  949. if (dols && firsttime) {
  950. firsttime = 0;
  951. } else {
  952. isdir = 0;
  953. }
  954. }
  955. if (get_dentfromdir(mydata, startsect, subname, dentptr,
  956. isdir ? 0 : dols) == NULL) {
  957. if (dols && !isdir)
  958. ret = 0;
  959. goto exit;
  960. }
  961. if (idx >= 0) {
  962. if (!(dentptr->attr & ATTR_DIR))
  963. goto exit;
  964. subname = nextname;
  965. }
  966. }
  967. ret = get_contents(mydata, dentptr, buffer, maxsize);
  968. debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
  969. exit:
  970. free(mydata->fatbuf);
  971. return ret;
  972. }
  973. int file_fat_detectfs (void)
  974. {
  975. boot_sector bs;
  976. volume_info volinfo;
  977. int fatsize;
  978. char vol_label[12];
  979. if (cur_dev == NULL) {
  980. printf("No current device\n");
  981. return 1;
  982. }
  983. #if defined(CONFIG_CMD_IDE) || \
  984. defined(CONFIG_CMD_MG_DISK) || \
  985. defined(CONFIG_CMD_SATA) || \
  986. defined(CONFIG_CMD_SCSI) || \
  987. defined(CONFIG_CMD_USB) || \
  988. defined(CONFIG_MMC)
  989. printf("Interface: ");
  990. switch (cur_dev->if_type) {
  991. case IF_TYPE_IDE:
  992. printf("IDE");
  993. break;
  994. case IF_TYPE_SATA:
  995. printf("SATA");
  996. break;
  997. case IF_TYPE_SCSI:
  998. printf("SCSI");
  999. break;
  1000. case IF_TYPE_ATAPI:
  1001. printf("ATAPI");
  1002. break;
  1003. case IF_TYPE_USB:
  1004. printf("USB");
  1005. break;
  1006. case IF_TYPE_DOC:
  1007. printf("DOC");
  1008. break;
  1009. case IF_TYPE_MMC:
  1010. printf("MMC");
  1011. break;
  1012. default:
  1013. printf("Unknown");
  1014. }
  1015. printf("\n Device %d: ", cur_dev->dev);
  1016. dev_print(cur_dev);
  1017. #endif
  1018. if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
  1019. printf("\nNo valid FAT fs found\n");
  1020. return 1;
  1021. }
  1022. memcpy(vol_label, volinfo.volume_label, 11);
  1023. vol_label[11] = '\0';
  1024. volinfo.fs_type[5] = '\0';
  1025. printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part,
  1026. volinfo.fs_type, vol_label);
  1027. return 0;
  1028. }
  1029. int file_fat_ls (const char *dir)
  1030. {
  1031. return do_fat_read(dir, NULL, 0, LS_YES);
  1032. }
  1033. long file_fat_read (const char *filename, void *buffer, unsigned long maxsize)
  1034. {
  1035. printf("reading %s\n", filename);
  1036. return do_fat_read(filename, buffer, maxsize, LS_NO);
  1037. }