fat.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*
  2. * fat.c
  3. *
  4. * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
  5. *
  6. * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
  7. * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <config.h>
  29. #include <fat.h>
  30. #include <asm/byteorder.h>
  31. #include <part.h>
  32. /*
  33. * Convert a string to lowercase.
  34. */
  35. static void downcase (char *str)
  36. {
  37. while (*str != '\0') {
  38. TOLOWER(*str);
  39. str++;
  40. }
  41. }
  42. static block_dev_desc_t *cur_dev = NULL;
  43. static unsigned long part_offset = 0;
  44. static int cur_part = 1;
  45. #define DOS_PART_TBL_OFFSET 0x1be
  46. #define DOS_PART_MAGIC_OFFSET 0x1fe
  47. #define DOS_FS_TYPE_OFFSET 0x36
  48. #define DOS_FS32_TYPE_OFFSET 0x52
  49. static int disk_read (__u32 startblock, __u32 getsize, __u8 * bufptr)
  50. {
  51. if (cur_dev == NULL)
  52. return -1;
  53. startblock += part_offset;
  54. if (cur_dev->block_read) {
  55. return cur_dev->block_read(cur_dev->dev, startblock, getsize,
  56. (unsigned long *) bufptr);
  57. }
  58. return -1;
  59. }
  60. int fat_register_device (block_dev_desc_t * dev_desc, int part_no)
  61. {
  62. unsigned char buffer[SECTOR_SIZE];
  63. disk_partition_t info;
  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. /* First we assume there is a MBR */
  86. if (!get_partition_info(dev_desc, part_no, &info)) {
  87. part_offset = info.start;
  88. cur_part = part_no;
  89. } else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) ||
  90. (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) {
  91. /* ok, we assume we are on a PBR only */
  92. cur_part = 1;
  93. part_offset = 0;
  94. } else {
  95. printf("** Partition %d not valid on device %d **\n",
  96. part_no, dev_desc->dev);
  97. return -1;
  98. }
  99. #else
  100. if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET], "FAT", 3) == 0) ||
  101. (strncmp((char *)&buffer[DOS_FS32_TYPE_OFFSET], "FAT32", 5) == 0)) {
  102. /* ok, we assume we are on a PBR only */
  103. cur_part = 1;
  104. part_offset = 0;
  105. info.start = part_offset;
  106. } else {
  107. /* FIXME we need to determine the start block of the
  108. * partition where the DOS FS resides. This can be done
  109. * by using the get_partition_info routine. For this
  110. * purpose the libpart must be included.
  111. */
  112. part_offset = 32;
  113. cur_part = 1;
  114. }
  115. #endif
  116. return 0;
  117. }
  118. /*
  119. * Get the first occurence of a directory delimiter ('/' or '\') in a string.
  120. * Return index into string if found, -1 otherwise.
  121. */
  122. static int dirdelim (char *str)
  123. {
  124. char *start = str;
  125. while (*str != '\0') {
  126. if (ISDIRDELIM(*str))
  127. return str - start;
  128. str++;
  129. }
  130. return -1;
  131. }
  132. /*
  133. * Extract zero terminated short name from a directory entry.
  134. */
  135. static void get_name (dir_entry *dirent, char *s_name)
  136. {
  137. char *ptr;
  138. memcpy(s_name, dirent->name, 8);
  139. s_name[8] = '\0';
  140. ptr = s_name;
  141. while (*ptr && *ptr != ' ')
  142. ptr++;
  143. if (dirent->ext[0] && dirent->ext[0] != ' ') {
  144. *ptr = '.';
  145. ptr++;
  146. memcpy(ptr, dirent->ext, 3);
  147. ptr[3] = '\0';
  148. while (*ptr && *ptr != ' ')
  149. ptr++;
  150. }
  151. *ptr = '\0';
  152. if (*s_name == DELETED_FLAG)
  153. *s_name = '\0';
  154. else if (*s_name == aRING)
  155. *s_name = DELETED_FLAG;
  156. downcase(s_name);
  157. }
  158. /*
  159. * Get the entry at index 'entry' in a FAT (12/16/32) table.
  160. * On failure 0x00 is returned.
  161. */
  162. static __u32 get_fatent (fsdata *mydata, __u32 entry)
  163. {
  164. __u32 bufnum;
  165. __u32 off16, offset;
  166. __u32 ret = 0x00;
  167. __u16 val1, val2;
  168. switch (mydata->fatsize) {
  169. case 32:
  170. bufnum = entry / FAT32BUFSIZE;
  171. offset = entry - bufnum * FAT32BUFSIZE;
  172. break;
  173. case 16:
  174. bufnum = entry / FAT16BUFSIZE;
  175. offset = entry - bufnum * FAT16BUFSIZE;
  176. break;
  177. case 12:
  178. bufnum = entry / FAT12BUFSIZE;
  179. offset = entry - bufnum * FAT12BUFSIZE;
  180. break;
  181. default:
  182. /* Unsupported FAT size */
  183. return ret;
  184. }
  185. debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
  186. mydata->fatsize, entry, entry, offset, offset);
  187. /* Read a new block of FAT entries into the cache. */
  188. if (bufnum != mydata->fatbufnum) {
  189. int getsize = FATBUFSIZE / FS_BLOCK_SIZE;
  190. __u8 *bufptr = mydata->fatbuf;
  191. __u32 fatlength = mydata->fatlength;
  192. __u32 startblock = bufnum * FATBUFBLOCKS;
  193. fatlength *= SECTOR_SIZE; /* We want it in bytes now */
  194. startblock += mydata->fat_sect; /* Offset from start of disk */
  195. if (getsize > fatlength)
  196. getsize = fatlength;
  197. if (disk_read(startblock, getsize, bufptr) < 0) {
  198. debug("Error reading FAT blocks\n");
  199. return ret;
  200. }
  201. mydata->fatbufnum = bufnum;
  202. }
  203. /* Get the actual entry from the table */
  204. switch (mydata->fatsize) {
  205. case 32:
  206. ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
  207. break;
  208. case 16:
  209. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
  210. break;
  211. case 12:
  212. off16 = (offset * 3) / 4;
  213. switch (offset & 0x3) {
  214. case 0:
  215. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
  216. ret &= 0xfff;
  217. break;
  218. case 1:
  219. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  220. val1 &= 0xf000;
  221. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  222. val2 &= 0x00ff;
  223. ret = (val2 << 4) | (val1 >> 12);
  224. break;
  225. case 2:
  226. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  227. val1 &= 0xff00;
  228. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  229. val2 &= 0x000f;
  230. ret = (val2 << 8) | (val1 >> 8);
  231. break;
  232. case 3:
  233. ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  234. ret = (ret & 0xfff0) >> 4;
  235. break;
  236. default:
  237. break;
  238. }
  239. break;
  240. }
  241. debug("FAT%d: ret: %08x, offset: %04x\n",
  242. mydata->fatsize, ret, offset);
  243. return ret;
  244. }
  245. /*
  246. * Read at most 'size' bytes from the specified cluster into 'buffer'.
  247. * Return 0 on success, -1 otherwise.
  248. */
  249. static int
  250. get_cluster (fsdata *mydata, __u32 clustnum, __u8 *buffer,
  251. unsigned long size)
  252. {
  253. int idx = 0;
  254. __u32 startsect;
  255. if (clustnum > 0) {
  256. startsect = mydata->data_begin +
  257. clustnum * mydata->clust_size;
  258. } else {
  259. startsect = mydata->rootdir_sect;
  260. }
  261. debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
  262. if (disk_read(startsect, size / FS_BLOCK_SIZE, buffer) < 0) {
  263. debug("Error reading data\n");
  264. return -1;
  265. }
  266. if (size % FS_BLOCK_SIZE) {
  267. __u8 tmpbuf[FS_BLOCK_SIZE];
  268. idx = size / FS_BLOCK_SIZE;
  269. if (disk_read(startsect + idx, 1, tmpbuf) < 0) {
  270. debug("Error reading data\n");
  271. return -1;
  272. }
  273. buffer += idx * FS_BLOCK_SIZE;
  274. memcpy(buffer, tmpbuf, size % FS_BLOCK_SIZE);
  275. return 0;
  276. }
  277. return 0;
  278. }
  279. /*
  280. * Read at most 'maxsize' bytes from the file associated with 'dentptr'
  281. * into 'buffer'.
  282. * Return the number of bytes read or -1 on fatal errors.
  283. */
  284. static long
  285. get_contents (fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
  286. unsigned long maxsize)
  287. {
  288. unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
  289. unsigned int bytesperclust = mydata->clust_size * SECTOR_SIZE;
  290. __u32 curclust = START(dentptr);
  291. __u32 endclust, newclust;
  292. unsigned long actsize;
  293. debug("Filesize: %ld bytes\n", filesize);
  294. if (maxsize > 0 && filesize > maxsize)
  295. filesize = maxsize;
  296. debug("%ld bytes\n", filesize);
  297. actsize = bytesperclust;
  298. endclust = curclust;
  299. do {
  300. /* search for consecutive clusters */
  301. while (actsize < filesize) {
  302. newclust = get_fatent(mydata, endclust);
  303. if ((newclust - 1) != endclust)
  304. goto getit;
  305. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  306. debug("curclust: 0x%x\n", newclust);
  307. debug("Invalid FAT entry\n");
  308. return gotsize;
  309. }
  310. endclust = newclust;
  311. actsize += bytesperclust;
  312. }
  313. /* actsize >= file size */
  314. actsize -= bytesperclust;
  315. /* get remaining clusters */
  316. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  317. printf("Error reading cluster\n");
  318. return -1;
  319. }
  320. /* get remaining bytes */
  321. gotsize += (int)actsize;
  322. filesize -= actsize;
  323. buffer += actsize;
  324. actsize = filesize;
  325. if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
  326. printf("Error reading cluster\n");
  327. return -1;
  328. }
  329. gotsize += actsize;
  330. return gotsize;
  331. getit:
  332. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  333. printf("Error reading cluster\n");
  334. return -1;
  335. }
  336. gotsize += (int)actsize;
  337. filesize -= actsize;
  338. buffer += actsize;
  339. curclust = get_fatent(mydata, endclust);
  340. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  341. debug("curclust: 0x%x\n", curclust);
  342. printf("Invalid FAT entry\n");
  343. return gotsize;
  344. }
  345. actsize = bytesperclust;
  346. endclust = curclust;
  347. } while (1);
  348. }
  349. #ifdef CONFIG_SUPPORT_VFAT
  350. /*
  351. * Extract the file name information from 'slotptr' into 'l_name',
  352. * starting at l_name[*idx].
  353. * Return 1 if terminator (zero byte) is found, 0 otherwise.
  354. */
  355. static int slot2str (dir_slot *slotptr, char *l_name, int *idx)
  356. {
  357. int j;
  358. for (j = 0; j <= 8; j += 2) {
  359. l_name[*idx] = slotptr->name0_4[j];
  360. if (l_name[*idx] == 0x00)
  361. return 1;
  362. (*idx)++;
  363. }
  364. for (j = 0; j <= 10; j += 2) {
  365. l_name[*idx] = slotptr->name5_10[j];
  366. if (l_name[*idx] == 0x00)
  367. return 1;
  368. (*idx)++;
  369. }
  370. for (j = 0; j <= 2; j += 2) {
  371. l_name[*idx] = slotptr->name11_12[j];
  372. if (l_name[*idx] == 0x00)
  373. return 1;
  374. (*idx)++;
  375. }
  376. return 0;
  377. }
  378. /*
  379. * Extract the full long filename starting at 'retdent' (which is really
  380. * a slot) into 'l_name'. If successful also copy the real directory entry
  381. * into 'retdent'
  382. * Return 0 on success, -1 otherwise.
  383. */
  384. __attribute__ ((__aligned__ (__alignof__ (dir_entry))))
  385. __u8 get_vfatname_block[MAX_CLUSTSIZE];
  386. static int
  387. get_vfatname (fsdata *mydata, int curclust, __u8 *cluster,
  388. dir_entry *retdent, char *l_name)
  389. {
  390. dir_entry *realdent;
  391. dir_slot *slotptr = (dir_slot *)retdent;
  392. __u8 *buflimit = cluster + ((curclust == 0) ?
  393. LINEAR_PREFETCH_SIZE :
  394. (mydata->clust_size * SECTOR_SIZE)
  395. );
  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 * SECTOR_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 * SECTOR_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[FS_BLOCK_SIZE];
  620. volume_info *vistart;
  621. if (disk_read (0, 1, block) < 0) {
  622. debug("Error: reading block\n");
  623. return -1;
  624. }
  625. memcpy(bs, block, sizeof(boot_sector));
  626. bs->reserved = FAT2CPU16(bs->reserved);
  627. bs->fat_length = FAT2CPU16(bs->fat_length);
  628. bs->secs_track = FAT2CPU16(bs->secs_track);
  629. bs->heads = FAT2CPU16(bs->heads);
  630. bs->total_sect = FAT2CPU32(bs->total_sect);
  631. /* FAT32 entries */
  632. if (bs->fat_length == 0) {
  633. /* Assume FAT32 */
  634. bs->fat32_length = FAT2CPU32(bs->fat32_length);
  635. bs->flags = FAT2CPU16(bs->flags);
  636. bs->root_cluster = FAT2CPU32(bs->root_cluster);
  637. bs->info_sector = FAT2CPU16(bs->info_sector);
  638. bs->backup_boot = FAT2CPU16(bs->backup_boot);
  639. vistart = (volume_info *)(block + sizeof(boot_sector));
  640. *fatsize = 32;
  641. } else {
  642. vistart = (volume_info *)&(bs->fat32_length);
  643. *fatsize = 0;
  644. }
  645. memcpy(volinfo, vistart, sizeof(volume_info));
  646. if (*fatsize == 32) {
  647. if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
  648. return 0;
  649. } else {
  650. if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  651. *fatsize = 12;
  652. return 0;
  653. }
  654. if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  655. *fatsize = 16;
  656. return 0;
  657. }
  658. }
  659. debug("Error: broken fs_type sign\n");
  660. return -1;
  661. }
  662. __attribute__ ((__aligned__ (__alignof__ (dir_entry))))
  663. __u8 do_fat_read_block[MAX_CLUSTSIZE];
  664. long
  665. do_fat_read (const char *filename, void *buffer, unsigned long maxsize,
  666. int dols)
  667. {
  668. char fnamecopy[2048];
  669. boot_sector bs;
  670. volume_info volinfo;
  671. fsdata datablock;
  672. fsdata *mydata = &datablock;
  673. dir_entry *dentptr;
  674. __u16 prevcksum = 0xffff;
  675. char *subname = "";
  676. int cursect;
  677. int idx, isdir = 0;
  678. int files = 0, dirs = 0;
  679. long ret = 0;
  680. int firsttime;
  681. int root_cluster;
  682. int j;
  683. if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
  684. debug("Error: reading boot sector\n");
  685. return -1;
  686. }
  687. root_cluster = bs.root_cluster;
  688. if (mydata->fatsize == 32)
  689. mydata->fatlength = bs.fat32_length;
  690. else
  691. mydata->fatlength = bs.fat_length;
  692. mydata->fat_sect = bs.reserved;
  693. cursect = mydata->rootdir_sect
  694. = mydata->fat_sect + mydata->fatlength * bs.fats;
  695. mydata->clust_size = bs.cluster_size;
  696. if (mydata->fatsize == 32) {
  697. mydata->data_begin = mydata->rootdir_sect -
  698. (mydata->clust_size * 2);
  699. } else {
  700. int rootdir_size;
  701. rootdir_size = ((bs.dir_entries[1] * (int)256 +
  702. bs.dir_entries[0]) *
  703. sizeof(dir_entry)) /
  704. SECTOR_SIZE;
  705. mydata->data_begin = mydata->rootdir_sect +
  706. rootdir_size -
  707. (mydata->clust_size * 2);
  708. }
  709. mydata->fatbufnum = -1;
  710. #ifdef CONFIG_SUPPORT_VFAT
  711. debug("VFAT Support enabled\n");
  712. #endif
  713. debug("FAT%d, fat_sect: %d, fatlength: %d\n",
  714. mydata->fatsize, mydata->fat_sect, mydata->fatlength);
  715. debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
  716. "Data begins at: %d\n",
  717. root_cluster,
  718. mydata->rootdir_sect,
  719. mydata->rootdir_sect * SECTOR_SIZE, mydata->data_begin);
  720. debug("Cluster size: %d\n", mydata->clust_size);
  721. /* "cwd" is always the root... */
  722. while (ISDIRDELIM(*filename))
  723. filename++;
  724. /* Make a copy of the filename and convert it to lowercase */
  725. strcpy(fnamecopy, filename);
  726. downcase(fnamecopy);
  727. if (*fnamecopy == '\0') {
  728. if (!dols)
  729. return -1;
  730. dols = LS_ROOT;
  731. } else if ((idx = dirdelim(fnamecopy)) >= 0) {
  732. isdir = 1;
  733. fnamecopy[idx] = '\0';
  734. subname = fnamecopy + idx + 1;
  735. /* Handle multiple delimiters */
  736. while (ISDIRDELIM(*subname))
  737. subname++;
  738. } else if (dols) {
  739. isdir = 1;
  740. }
  741. j = 0;
  742. while (1) {
  743. int i;
  744. debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%d\n",
  745. cursect, mydata->clust_size, DIRENTSPERBLOCK);
  746. if (disk_read(cursect,
  747. (mydata->fatsize == 32) ?
  748. (mydata->clust_size) :
  749. LINEAR_PREFETCH_SIZE,
  750. do_fat_read_block) < 0) {
  751. debug("Error: reading rootdir block\n");
  752. return -1;
  753. }
  754. dentptr = (dir_entry *) do_fat_read_block;
  755. for (i = 0; i < DIRENTSPERBLOCK; i++) {
  756. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  757. l_name[0] = '\0';
  758. if (dentptr->name[0] == DELETED_FLAG) {
  759. dentptr++;
  760. continue;
  761. }
  762. if ((dentptr->attr & ATTR_VOLUME)) {
  763. #ifdef CONFIG_SUPPORT_VFAT
  764. if ((dentptr->attr & ATTR_VFAT) &&
  765. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  766. prevcksum =
  767. ((dir_slot *)dentptr)->alias_checksum;
  768. get_vfatname(mydata,
  769. (mydata->fatsize == 32) ?
  770. root_cluster :
  771. 0,
  772. do_fat_read_block,
  773. dentptr, l_name);
  774. if (dols == LS_ROOT) {
  775. char dirc;
  776. int doit = 0;
  777. int isdir =
  778. (dentptr->attr & ATTR_DIR);
  779. if (isdir) {
  780. dirs++;
  781. dirc = '/';
  782. doit = 1;
  783. } else {
  784. dirc = ' ';
  785. if (l_name[0] != 0) {
  786. files++;
  787. doit = 1;
  788. }
  789. }
  790. if (doit) {
  791. if (dirc == ' ') {
  792. printf(" %8ld %s%c\n",
  793. (long)FAT2CPU32(dentptr->size),
  794. l_name,
  795. dirc);
  796. } else {
  797. printf(" %s%c\n",
  798. l_name,
  799. dirc);
  800. }
  801. }
  802. dentptr++;
  803. continue;
  804. }
  805. debug("Rootvfatname: |%s|\n",
  806. l_name);
  807. } else
  808. #endif
  809. {
  810. /* Volume label or VFAT entry */
  811. dentptr++;
  812. continue;
  813. }
  814. } else if (dentptr->name[0] == 0) {
  815. debug("RootDentname == NULL - %d\n", i);
  816. if (dols == LS_ROOT) {
  817. printf("\n%d file(s), %d dir(s)\n\n",
  818. files, dirs);
  819. return 0;
  820. }
  821. return -1;
  822. }
  823. #ifdef CONFIG_SUPPORT_VFAT
  824. else if (dols == LS_ROOT &&
  825. mkcksum(dentptr->name) == prevcksum) {
  826. dentptr++;
  827. continue;
  828. }
  829. #endif
  830. get_name(dentptr, s_name);
  831. if (dols == LS_ROOT) {
  832. int isdir = (dentptr->attr & ATTR_DIR);
  833. char dirc;
  834. int doit = 0;
  835. if (isdir) {
  836. dirc = '/';
  837. if (s_name[0] != 0) {
  838. dirs++;
  839. doit = 1;
  840. }
  841. } else {
  842. dirc = ' ';
  843. if (s_name[0] != 0) {
  844. files++;
  845. doit = 1;
  846. }
  847. }
  848. if (doit) {
  849. if (dirc == ' ') {
  850. printf(" %8ld %s%c\n",
  851. (long)FAT2CPU32(dentptr->size),
  852. s_name, dirc);
  853. } else {
  854. printf(" %s%c\n",
  855. s_name, dirc);
  856. }
  857. }
  858. dentptr++;
  859. continue;
  860. }
  861. if (strcmp(fnamecopy, s_name)
  862. && strcmp(fnamecopy, l_name)) {
  863. debug("RootMismatch: |%s|%s|\n", s_name,
  864. l_name);
  865. dentptr++;
  866. continue;
  867. }
  868. if (isdir && !(dentptr->attr & ATTR_DIR))
  869. return -1;
  870. debug("RootName: %s", s_name);
  871. debug(", start: 0x%x", START(dentptr));
  872. debug(", size: 0x%x %s\n",
  873. FAT2CPU32(dentptr->size),
  874. isdir ? "(DIR)" : "");
  875. goto rootdir_done; /* We got a match */
  876. }
  877. debug("END LOOP: j=%d clust_size=%d\n", j,
  878. mydata->clust_size);
  879. /*
  880. * On FAT32 we must fetch the FAT entries for the next
  881. * root directory clusters when a cluster has been
  882. * completely processed.
  883. */
  884. if ((mydata->fatsize == 32) && (++j == mydata->clust_size)) {
  885. int nxtsect;
  886. int nxt_clust;
  887. nxt_clust = get_fatent(mydata, root_cluster);
  888. nxtsect = mydata->data_begin +
  889. (nxt_clust * mydata->clust_size);
  890. debug("END LOOP: sect=%d, root_clust=%d, "
  891. "n_sect=%d, n_clust=%d\n",
  892. cursect, root_cluster,
  893. nxtsect, nxt_clust);
  894. root_cluster = nxt_clust;
  895. cursect = nxtsect;
  896. j = 0;
  897. } else {
  898. cursect++;
  899. }
  900. }
  901. rootdir_done:
  902. firsttime = 1;
  903. while (isdir) {
  904. int startsect = mydata->data_begin
  905. + START(dentptr) * mydata->clust_size;
  906. dir_entry dent;
  907. char *nextname = NULL;
  908. dent = *dentptr;
  909. dentptr = &dent;
  910. idx = dirdelim(subname);
  911. if (idx >= 0) {
  912. subname[idx] = '\0';
  913. nextname = subname + idx + 1;
  914. /* Handle multiple delimiters */
  915. while (ISDIRDELIM(*nextname))
  916. nextname++;
  917. if (dols && *nextname == '\0')
  918. firsttime = 0;
  919. } else {
  920. if (dols && firsttime) {
  921. firsttime = 0;
  922. } else {
  923. isdir = 0;
  924. }
  925. }
  926. if (get_dentfromdir(mydata, startsect, subname, dentptr,
  927. isdir ? 0 : dols) == NULL) {
  928. if (dols && !isdir)
  929. return 0;
  930. return -1;
  931. }
  932. if (idx >= 0) {
  933. if (!(dentptr->attr & ATTR_DIR))
  934. return -1;
  935. subname = nextname;
  936. }
  937. }
  938. ret = get_contents(mydata, dentptr, buffer, maxsize);
  939. debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
  940. return ret;
  941. }
  942. int file_fat_detectfs (void)
  943. {
  944. boot_sector bs;
  945. volume_info volinfo;
  946. int fatsize;
  947. char vol_label[12];
  948. if (cur_dev == NULL) {
  949. printf("No current device\n");
  950. return 1;
  951. }
  952. #if defined(CONFIG_CMD_IDE) || \
  953. defined(CONFIG_CMD_MG_DISK) || \
  954. defined(CONFIG_CMD_SATA) || \
  955. defined(CONFIG_CMD_SCSI) || \
  956. defined(CONFIG_CMD_USB) || \
  957. defined(CONFIG_MMC)
  958. printf("Interface: ");
  959. switch (cur_dev->if_type) {
  960. case IF_TYPE_IDE:
  961. printf("IDE");
  962. break;
  963. case IF_TYPE_SATA:
  964. printf("SATA");
  965. break;
  966. case IF_TYPE_SCSI:
  967. printf("SCSI");
  968. break;
  969. case IF_TYPE_ATAPI:
  970. printf("ATAPI");
  971. break;
  972. case IF_TYPE_USB:
  973. printf("USB");
  974. break;
  975. case IF_TYPE_DOC:
  976. printf("DOC");
  977. break;
  978. case IF_TYPE_MMC:
  979. printf("MMC");
  980. break;
  981. default:
  982. printf("Unknown");
  983. }
  984. printf("\n Device %d: ", cur_dev->dev);
  985. dev_print(cur_dev);
  986. #endif
  987. if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
  988. printf("\nNo valid FAT fs found\n");
  989. return 1;
  990. }
  991. memcpy(vol_label, volinfo.volume_label, 11);
  992. vol_label[11] = '\0';
  993. volinfo.fs_type[5] = '\0';
  994. printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part,
  995. volinfo.fs_type, vol_label);
  996. return 0;
  997. }
  998. int file_fat_ls (const char *dir)
  999. {
  1000. return do_fat_read(dir, NULL, 0, LS_YES);
  1001. }
  1002. long file_fat_read (const char *filename, void *buffer, unsigned long maxsize)
  1003. {
  1004. printf("reading %s\n", filename);
  1005. return do_fat_read(filename, buffer, maxsize, LS_NO);
  1006. }