fat.c 26 KB

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