fat.c 26 KB

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