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. 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 + ((curclust == 0) ?
  394. LINEAR_PREFETCH_SIZE :
  395. (mydata->clust_size * mydata->sect_size)
  396. );
  397. __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
  398. int idx = 0;
  399. if (counter > VFAT_MAXSEQ) {
  400. debug("Error: VFAT name is too long\n");
  401. return -1;
  402. }
  403. while ((__u8 *)slotptr < buflimit) {
  404. if (counter == 0)
  405. break;
  406. if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
  407. return -1;
  408. slotptr++;
  409. counter--;
  410. }
  411. if ((__u8 *)slotptr >= buflimit) {
  412. dir_slot *slotptr2;
  413. if (curclust == 0)
  414. return -1;
  415. curclust = get_fatent(mydata, curclust);
  416. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  417. debug("curclust: 0x%x\n", curclust);
  418. printf("Invalid FAT entry\n");
  419. return -1;
  420. }
  421. if (get_cluster(mydata, curclust, get_vfatname_block,
  422. mydata->clust_size * mydata->sect_size) != 0) {
  423. debug("Error: reading directory block\n");
  424. return -1;
  425. }
  426. slotptr2 = (dir_slot *)get_vfatname_block;
  427. while (counter > 0) {
  428. if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
  429. & 0xff) != counter)
  430. return -1;
  431. slotptr2++;
  432. counter--;
  433. }
  434. /* Save the real directory entry */
  435. realdent = (dir_entry *)slotptr2;
  436. while ((__u8 *)slotptr2 > get_vfatname_block) {
  437. slotptr2--;
  438. slot2str(slotptr2, l_name, &idx);
  439. }
  440. } else {
  441. /* Save the real directory entry */
  442. realdent = (dir_entry *)slotptr;
  443. }
  444. do {
  445. slotptr--;
  446. if (slot2str(slotptr, l_name, &idx))
  447. break;
  448. } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
  449. l_name[idx] = '\0';
  450. if (*l_name == DELETED_FLAG)
  451. *l_name = '\0';
  452. else if (*l_name == aRING)
  453. *l_name = DELETED_FLAG;
  454. downcase(l_name);
  455. /* Return the real directory entry */
  456. memcpy(retdent, realdent, sizeof(dir_entry));
  457. return 0;
  458. }
  459. /* Calculate short name checksum */
  460. static __u8 mkcksum (const char *str)
  461. {
  462. int i;
  463. __u8 ret = 0;
  464. for (i = 0; i < 11; i++) {
  465. ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
  466. }
  467. return ret;
  468. }
  469. #endif /* CONFIG_SUPPORT_VFAT */
  470. /*
  471. * Get the directory entry associated with 'filename' from the directory
  472. * starting at 'startsect'
  473. */
  474. __attribute__ ((__aligned__ (__alignof__ (dir_entry))))
  475. __u8 get_dentfromdir_block[MAX_CLUSTSIZE];
  476. static dir_entry *get_dentfromdir (fsdata *mydata, int startsect,
  477. char *filename, dir_entry *retdent,
  478. int dols)
  479. {
  480. __u16 prevcksum = 0xffff;
  481. __u32 curclust = START(retdent);
  482. int files = 0, dirs = 0;
  483. debug("get_dentfromdir: %s\n", filename);
  484. while (1) {
  485. dir_entry *dentptr;
  486. int i;
  487. if (get_cluster(mydata, curclust, get_dentfromdir_block,
  488. mydata->clust_size * mydata->sect_size) != 0) {
  489. debug("Error: reading directory block\n");
  490. return NULL;
  491. }
  492. dentptr = (dir_entry *)get_dentfromdir_block;
  493. for (i = 0; i < DIRENTSPERCLUST; i++) {
  494. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  495. l_name[0] = '\0';
  496. if (dentptr->name[0] == DELETED_FLAG) {
  497. dentptr++;
  498. continue;
  499. }
  500. if ((dentptr->attr & ATTR_VOLUME)) {
  501. #ifdef CONFIG_SUPPORT_VFAT
  502. if ((dentptr->attr & ATTR_VFAT) &&
  503. (dentptr-> name[0] & LAST_LONG_ENTRY_MASK)) {
  504. prevcksum = ((dir_slot *)dentptr)->alias_checksum;
  505. get_vfatname(mydata, curclust,
  506. get_dentfromdir_block,
  507. dentptr, l_name);
  508. if (dols) {
  509. int isdir;
  510. char dirc;
  511. int doit = 0;
  512. isdir = (dentptr->attr & ATTR_DIR);
  513. if (isdir) {
  514. dirs++;
  515. dirc = '/';
  516. doit = 1;
  517. } else {
  518. dirc = ' ';
  519. if (l_name[0] != 0) {
  520. files++;
  521. doit = 1;
  522. }
  523. }
  524. if (doit) {
  525. if (dirc == ' ') {
  526. printf(" %8ld %s%c\n",
  527. (long)FAT2CPU32(dentptr->size),
  528. l_name,
  529. dirc);
  530. } else {
  531. printf(" %s%c\n",
  532. l_name,
  533. dirc);
  534. }
  535. }
  536. dentptr++;
  537. continue;
  538. }
  539. debug("vfatname: |%s|\n", l_name);
  540. } else
  541. #endif
  542. {
  543. /* Volume label or VFAT entry */
  544. dentptr++;
  545. continue;
  546. }
  547. }
  548. if (dentptr->name[0] == 0) {
  549. if (dols) {
  550. printf("\n%d file(s), %d dir(s)\n\n",
  551. files, dirs);
  552. }
  553. debug("Dentname == NULL - %d\n", i);
  554. return NULL;
  555. }
  556. #ifdef CONFIG_SUPPORT_VFAT
  557. if (dols && mkcksum(dentptr->name) == prevcksum) {
  558. dentptr++;
  559. continue;
  560. }
  561. #endif
  562. get_name(dentptr, s_name);
  563. if (dols) {
  564. int isdir = (dentptr->attr & ATTR_DIR);
  565. char dirc;
  566. int doit = 0;
  567. if (isdir) {
  568. dirs++;
  569. dirc = '/';
  570. doit = 1;
  571. } else {
  572. dirc = ' ';
  573. if (s_name[0] != 0) {
  574. files++;
  575. doit = 1;
  576. }
  577. }
  578. if (doit) {
  579. if (dirc == ' ') {
  580. printf(" %8ld %s%c\n",
  581. (long)FAT2CPU32(dentptr->size),
  582. s_name, dirc);
  583. } else {
  584. printf(" %s%c\n",
  585. s_name, dirc);
  586. }
  587. }
  588. dentptr++;
  589. continue;
  590. }
  591. if (strcmp(filename, s_name)
  592. && strcmp(filename, l_name)) {
  593. debug("Mismatch: |%s|%s|\n", s_name, l_name);
  594. dentptr++;
  595. continue;
  596. }
  597. memcpy(retdent, dentptr, sizeof(dir_entry));
  598. debug("DentName: %s", s_name);
  599. debug(", start: 0x%x", START(dentptr));
  600. debug(", size: 0x%x %s\n",
  601. FAT2CPU32(dentptr->size),
  602. (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
  603. return retdent;
  604. }
  605. curclust = get_fatent(mydata, curclust);
  606. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  607. debug("curclust: 0x%x\n", curclust);
  608. printf("Invalid FAT entry\n");
  609. return NULL;
  610. }
  611. }
  612. return NULL;
  613. }
  614. /*
  615. * Read boot sector and volume info from a FAT filesystem
  616. */
  617. static int
  618. read_bootsectandvi (boot_sector *bs, volume_info *volinfo, int *fatsize)
  619. {
  620. __u8 *block;
  621. volume_info *vistart;
  622. int ret = 0;
  623. if (cur_dev == NULL) {
  624. debug("Error: no device selected\n");
  625. return -1;
  626. }
  627. block = malloc(cur_dev->blksz);
  628. if (block == NULL) {
  629. debug("Error: allocating block\n");
  630. return -1;
  631. }
  632. if (disk_read (0, 1, block) < 0) {
  633. debug("Error: reading block\n");
  634. goto fail;
  635. }
  636. memcpy(bs, block, sizeof(boot_sector));
  637. bs->reserved = FAT2CPU16(bs->reserved);
  638. bs->fat_length = FAT2CPU16(bs->fat_length);
  639. bs->secs_track = FAT2CPU16(bs->secs_track);
  640. bs->heads = FAT2CPU16(bs->heads);
  641. bs->total_sect = FAT2CPU32(bs->total_sect);
  642. /* FAT32 entries */
  643. if (bs->fat_length == 0) {
  644. /* Assume FAT32 */
  645. bs->fat32_length = FAT2CPU32(bs->fat32_length);
  646. bs->flags = FAT2CPU16(bs->flags);
  647. bs->root_cluster = FAT2CPU32(bs->root_cluster);
  648. bs->info_sector = FAT2CPU16(bs->info_sector);
  649. bs->backup_boot = FAT2CPU16(bs->backup_boot);
  650. vistart = (volume_info *)(block + sizeof(boot_sector));
  651. *fatsize = 32;
  652. } else {
  653. vistart = (volume_info *)&(bs->fat32_length);
  654. *fatsize = 0;
  655. }
  656. memcpy(volinfo, vistart, sizeof(volume_info));
  657. if (*fatsize == 32) {
  658. if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
  659. goto exit;
  660. } else {
  661. if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  662. *fatsize = 12;
  663. goto exit;
  664. }
  665. if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  666. *fatsize = 16;
  667. goto exit;
  668. }
  669. }
  670. debug("Error: broken fs_type sign\n");
  671. fail:
  672. ret = -1;
  673. exit:
  674. free(block);
  675. return ret;
  676. }
  677. __attribute__ ((__aligned__ (__alignof__ (dir_entry))))
  678. __u8 do_fat_read_block[MAX_CLUSTSIZE];
  679. long
  680. do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
  681. int dols)
  682. {
  683. char fnamecopy[2048];
  684. boot_sector bs;
  685. volume_info volinfo;
  686. fsdata datablock;
  687. fsdata *mydata = &datablock;
  688. dir_entry *dentptr;
  689. __u16 prevcksum = 0xffff;
  690. char *subname = "";
  691. __u32 cursect;
  692. int idx, isdir = 0;
  693. int files = 0, dirs = 0;
  694. long ret = -1;
  695. int firsttime;
  696. __u32 root_cluster;
  697. int rootdir_size = 0;
  698. int j;
  699. if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
  700. debug("Error: reading boot sector\n");
  701. return -1;
  702. }
  703. root_cluster = bs.root_cluster;
  704. if (mydata->fatsize == 32)
  705. mydata->fatlength = bs.fat32_length;
  706. else
  707. mydata->fatlength = bs.fat_length;
  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. LINEAR_PREFETCH_SIZE / mydata->sect_size,
  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. (mydata->fatsize == 32) ?
  792. root_cluster :
  793. 0,
  794. do_fat_read_block,
  795. dentptr, l_name);
  796. if (dols == LS_ROOT) {
  797. char dirc;
  798. int doit = 0;
  799. int isdir =
  800. (dentptr->attr & ATTR_DIR);
  801. if (isdir) {
  802. dirs++;
  803. dirc = '/';
  804. doit = 1;
  805. } else {
  806. dirc = ' ';
  807. if (l_name[0] != 0) {
  808. files++;
  809. doit = 1;
  810. }
  811. }
  812. if (doit) {
  813. if (dirc == ' ') {
  814. printf(" %8ld %s%c\n",
  815. (long)FAT2CPU32(dentptr->size),
  816. l_name,
  817. dirc);
  818. } else {
  819. printf(" %s%c\n",
  820. l_name,
  821. dirc);
  822. }
  823. }
  824. dentptr++;
  825. continue;
  826. }
  827. debug("Rootvfatname: |%s|\n",
  828. l_name);
  829. } else
  830. #endif
  831. {
  832. /* Volume label or VFAT entry */
  833. dentptr++;
  834. continue;
  835. }
  836. } else if (dentptr->name[0] == 0) {
  837. debug("RootDentname == NULL - %d\n", i);
  838. if (dols == LS_ROOT) {
  839. printf("\n%d file(s), %d dir(s)\n\n",
  840. files, dirs);
  841. ret = 0;
  842. }
  843. goto exit;
  844. }
  845. #ifdef CONFIG_SUPPORT_VFAT
  846. else if (dols == LS_ROOT &&
  847. mkcksum(dentptr->name) == prevcksum) {
  848. dentptr++;
  849. continue;
  850. }
  851. #endif
  852. get_name(dentptr, s_name);
  853. if (dols == LS_ROOT) {
  854. int isdir = (dentptr->attr & ATTR_DIR);
  855. char dirc;
  856. int doit = 0;
  857. if (isdir) {
  858. dirc = '/';
  859. if (s_name[0] != 0) {
  860. dirs++;
  861. doit = 1;
  862. }
  863. } else {
  864. dirc = ' ';
  865. if (s_name[0] != 0) {
  866. files++;
  867. doit = 1;
  868. }
  869. }
  870. if (doit) {
  871. if (dirc == ' ') {
  872. printf(" %8ld %s%c\n",
  873. (long)FAT2CPU32(dentptr->size),
  874. s_name, dirc);
  875. } else {
  876. printf(" %s%c\n",
  877. s_name, dirc);
  878. }
  879. }
  880. dentptr++;
  881. continue;
  882. }
  883. if (strcmp(fnamecopy, s_name)
  884. && strcmp(fnamecopy, l_name)) {
  885. debug("RootMismatch: |%s|%s|\n", s_name,
  886. l_name);
  887. dentptr++;
  888. continue;
  889. }
  890. if (isdir && !(dentptr->attr & ATTR_DIR))
  891. goto exit;
  892. debug("RootName: %s", s_name);
  893. debug(", start: 0x%x", START(dentptr));
  894. debug(", size: 0x%x %s\n",
  895. FAT2CPU32(dentptr->size),
  896. isdir ? "(DIR)" : "");
  897. goto rootdir_done; /* We got a match */
  898. }
  899. debug("END LOOP: j=%d clust_size=%d\n", j,
  900. mydata->clust_size);
  901. /*
  902. * On FAT32 we must fetch the FAT entries for the next
  903. * root directory clusters when a cluster has been
  904. * completely processed.
  905. */
  906. ++j;
  907. int fat32_end = 0;
  908. if ((mydata->fatsize == 32) && (j == mydata->clust_size)) {
  909. int nxtsect = 0;
  910. int nxt_clust = 0;
  911. nxt_clust = get_fatent(mydata, root_cluster);
  912. fat32_end = CHECK_CLUST(nxt_clust, 32);
  913. nxtsect = mydata->data_begin +
  914. (nxt_clust * mydata->clust_size);
  915. root_cluster = nxt_clust;
  916. cursect = nxtsect;
  917. j = 0;
  918. } else {
  919. cursect++;
  920. }
  921. /* If end of rootdir reached */
  922. if ((mydata->fatsize == 32 && fat32_end) ||
  923. (mydata->fatsize != 32 && j == rootdir_size)) {
  924. if (dols == LS_ROOT) {
  925. printf("\n%d file(s), %d dir(s)\n\n",
  926. files, dirs);
  927. ret = 0;
  928. }
  929. goto exit;
  930. }
  931. }
  932. rootdir_done:
  933. firsttime = 1;
  934. while (isdir) {
  935. int startsect = mydata->data_begin
  936. + START(dentptr) * mydata->clust_size;
  937. dir_entry dent;
  938. char *nextname = NULL;
  939. dent = *dentptr;
  940. dentptr = &dent;
  941. idx = dirdelim(subname);
  942. if (idx >= 0) {
  943. subname[idx] = '\0';
  944. nextname = subname + idx + 1;
  945. /* Handle multiple delimiters */
  946. while (ISDIRDELIM(*nextname))
  947. nextname++;
  948. if (dols && *nextname == '\0')
  949. firsttime = 0;
  950. } else {
  951. if (dols && firsttime) {
  952. firsttime = 0;
  953. } else {
  954. isdir = 0;
  955. }
  956. }
  957. if (get_dentfromdir(mydata, startsect, subname, dentptr,
  958. isdir ? 0 : dols) == NULL) {
  959. if (dols && !isdir)
  960. ret = 0;
  961. goto exit;
  962. }
  963. if (idx >= 0) {
  964. if (!(dentptr->attr & ATTR_DIR))
  965. goto exit;
  966. subname = nextname;
  967. }
  968. }
  969. ret = get_contents(mydata, dentptr, buffer, maxsize);
  970. debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
  971. exit:
  972. free(mydata->fatbuf);
  973. return ret;
  974. }
  975. int file_fat_detectfs (void)
  976. {
  977. boot_sector bs;
  978. volume_info volinfo;
  979. int fatsize;
  980. char vol_label[12];
  981. if (cur_dev == NULL) {
  982. printf("No current device\n");
  983. return 1;
  984. }
  985. #if defined(CONFIG_CMD_IDE) || \
  986. defined(CONFIG_CMD_MG_DISK) || \
  987. defined(CONFIG_CMD_SATA) || \
  988. defined(CONFIG_CMD_SCSI) || \
  989. defined(CONFIG_CMD_USB) || \
  990. defined(CONFIG_MMC)
  991. printf("Interface: ");
  992. switch (cur_dev->if_type) {
  993. case IF_TYPE_IDE:
  994. printf("IDE");
  995. break;
  996. case IF_TYPE_SATA:
  997. printf("SATA");
  998. break;
  999. case IF_TYPE_SCSI:
  1000. printf("SCSI");
  1001. break;
  1002. case IF_TYPE_ATAPI:
  1003. printf("ATAPI");
  1004. break;
  1005. case IF_TYPE_USB:
  1006. printf("USB");
  1007. break;
  1008. case IF_TYPE_DOC:
  1009. printf("DOC");
  1010. break;
  1011. case IF_TYPE_MMC:
  1012. printf("MMC");
  1013. break;
  1014. default:
  1015. printf("Unknown");
  1016. }
  1017. printf("\n Device %d: ", cur_dev->dev);
  1018. dev_print(cur_dev);
  1019. #endif
  1020. if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
  1021. printf("\nNo valid FAT fs found\n");
  1022. return 1;
  1023. }
  1024. memcpy(vol_label, volinfo.volume_label, 11);
  1025. vol_label[11] = '\0';
  1026. volinfo.fs_type[5] = '\0';
  1027. printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part,
  1028. volinfo.fs_type, vol_label);
  1029. return 0;
  1030. }
  1031. int file_fat_ls (const char *dir)
  1032. {
  1033. return do_fat_read(dir, NULL, 0, LS_YES);
  1034. }
  1035. long file_fat_read (const char *filename, void *buffer, unsigned long maxsize)
  1036. {
  1037. printf("reading %s\n", filename);
  1038. return do_fat_read(filename, buffer, maxsize, LS_NO);
  1039. }