fat.c 24 KB

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