fat.c 23 KB

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