fat.c 26 KB

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