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. #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 (getsize > fatlength)
  184. getsize = fatlength;
  185. fatlength *= mydata->sect_size; /* We want it in bytes now */
  186. startblock += mydata->fat_sect; /* Offset from start of disk */
  187. if (disk_read(startblock, getsize, bufptr) < 0) {
  188. debug("Error reading FAT blocks\n");
  189. return ret;
  190. }
  191. mydata->fatbufnum = bufnum;
  192. }
  193. /* Get the actual entry from the table */
  194. switch (mydata->fatsize) {
  195. case 32:
  196. ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
  197. break;
  198. case 16:
  199. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
  200. break;
  201. case 12:
  202. off16 = (offset * 3) / 4;
  203. switch (offset & 0x3) {
  204. case 0:
  205. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
  206. ret &= 0xfff;
  207. break;
  208. case 1:
  209. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  210. val1 &= 0xf000;
  211. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  212. val2 &= 0x00ff;
  213. ret = (val2 << 4) | (val1 >> 12);
  214. break;
  215. case 2:
  216. val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  217. val1 &= 0xff00;
  218. val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
  219. val2 &= 0x000f;
  220. ret = (val2 << 8) | (val1 >> 8);
  221. break;
  222. case 3:
  223. ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
  224. ret = (ret & 0xfff0) >> 4;
  225. break;
  226. default:
  227. break;
  228. }
  229. break;
  230. }
  231. debug("FAT%d: ret: %08x, offset: %04x\n",
  232. mydata->fatsize, ret, offset);
  233. return ret;
  234. }
  235. /*
  236. * Read at most 'size' bytes from the specified cluster into 'buffer'.
  237. * Return 0 on success, -1 otherwise.
  238. */
  239. static int
  240. get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
  241. {
  242. __u32 idx = 0;
  243. __u32 startsect;
  244. __u32 nr_sect;
  245. int ret;
  246. if (clustnum > 0) {
  247. startsect = mydata->data_begin +
  248. clustnum * mydata->clust_size;
  249. } else {
  250. startsect = mydata->rootdir_sect;
  251. }
  252. debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
  253. nr_sect = size / mydata->sect_size;
  254. ret = disk_read(startsect, nr_sect, buffer);
  255. if (ret != nr_sect) {
  256. debug("Error reading data (got %d)\n", ret);
  257. return -1;
  258. }
  259. if (size % mydata->sect_size) {
  260. ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
  261. idx = size / mydata->sect_size;
  262. ret = disk_read(startsect + idx, 1, tmpbuf);
  263. if (ret != 1) {
  264. debug("Error reading data (got %d)\n", ret);
  265. return -1;
  266. }
  267. buffer += idx * mydata->sect_size;
  268. memcpy(buffer, tmpbuf, size % mydata->sect_size);
  269. return 0;
  270. }
  271. return 0;
  272. }
  273. /*
  274. * Read at most 'maxsize' bytes from the file associated with 'dentptr'
  275. * into 'buffer'.
  276. * Return the number of bytes read or -1 on fatal errors.
  277. */
  278. static long
  279. get_contents(fsdata *mydata, dir_entry *dentptr, __u8 *buffer,
  280. unsigned long maxsize)
  281. {
  282. unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
  283. unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
  284. __u32 curclust = START(dentptr);
  285. __u32 endclust, newclust;
  286. unsigned long actsize;
  287. debug("Filesize: %ld bytes\n", filesize);
  288. if (maxsize > 0 && filesize > maxsize)
  289. filesize = maxsize;
  290. debug("%ld bytes\n", filesize);
  291. actsize = bytesperclust;
  292. endclust = curclust;
  293. do {
  294. /* search for consecutive clusters */
  295. while (actsize < filesize) {
  296. newclust = get_fatent(mydata, endclust);
  297. if ((newclust - 1) != endclust)
  298. goto getit;
  299. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  300. debug("curclust: 0x%x\n", newclust);
  301. debug("Invalid FAT entry\n");
  302. return gotsize;
  303. }
  304. endclust = newclust;
  305. actsize += bytesperclust;
  306. }
  307. /* actsize >= file size */
  308. actsize -= bytesperclust;
  309. /* get remaining clusters */
  310. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  311. printf("Error reading cluster\n");
  312. return -1;
  313. }
  314. /* get remaining bytes */
  315. gotsize += (int)actsize;
  316. filesize -= actsize;
  317. buffer += actsize;
  318. actsize = filesize;
  319. if (get_cluster(mydata, endclust, buffer, (int)actsize) != 0) {
  320. printf("Error reading cluster\n");
  321. return -1;
  322. }
  323. gotsize += actsize;
  324. return gotsize;
  325. getit:
  326. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  327. printf("Error reading cluster\n");
  328. return -1;
  329. }
  330. gotsize += (int)actsize;
  331. filesize -= actsize;
  332. buffer += actsize;
  333. curclust = get_fatent(mydata, endclust);
  334. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  335. debug("curclust: 0x%x\n", curclust);
  336. printf("Invalid FAT entry\n");
  337. return gotsize;
  338. }
  339. actsize = bytesperclust;
  340. endclust = curclust;
  341. } while (1);
  342. }
  343. #ifdef CONFIG_SUPPORT_VFAT
  344. /*
  345. * Extract the file name information from 'slotptr' into 'l_name',
  346. * starting at l_name[*idx].
  347. * Return 1 if terminator (zero byte) is found, 0 otherwise.
  348. */
  349. static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
  350. {
  351. int j;
  352. for (j = 0; j <= 8; j += 2) {
  353. l_name[*idx] = slotptr->name0_4[j];
  354. if (l_name[*idx] == 0x00)
  355. return 1;
  356. (*idx)++;
  357. }
  358. for (j = 0; j <= 10; j += 2) {
  359. l_name[*idx] = slotptr->name5_10[j];
  360. if (l_name[*idx] == 0x00)
  361. return 1;
  362. (*idx)++;
  363. }
  364. for (j = 0; j <= 2; j += 2) {
  365. l_name[*idx] = slotptr->name11_12[j];
  366. if (l_name[*idx] == 0x00)
  367. return 1;
  368. (*idx)++;
  369. }
  370. return 0;
  371. }
  372. /*
  373. * Extract the full long filename starting at 'retdent' (which is really
  374. * a slot) into 'l_name'. If successful also copy the real directory entry
  375. * into 'retdent'
  376. * Return 0 on success, -1 otherwise.
  377. */
  378. __u8 get_vfatname_block[MAX_CLUSTSIZE]
  379. __aligned(ARCH_DMA_MINALIGN);
  380. static int
  381. get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
  382. dir_entry *retdent, char *l_name)
  383. {
  384. dir_entry *realdent;
  385. dir_slot *slotptr = (dir_slot *)retdent;
  386. __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
  387. PREFETCH_BLOCKS :
  388. mydata->clust_size);
  389. __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
  390. int idx = 0;
  391. if (counter > VFAT_MAXSEQ) {
  392. debug("Error: VFAT name is too long\n");
  393. return -1;
  394. }
  395. while ((__u8 *)slotptr < buflimit) {
  396. if (counter == 0)
  397. break;
  398. if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
  399. return -1;
  400. slotptr++;
  401. counter--;
  402. }
  403. if ((__u8 *)slotptr >= buflimit) {
  404. dir_slot *slotptr2;
  405. if (curclust == 0)
  406. return -1;
  407. curclust = get_fatent(mydata, curclust);
  408. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  409. debug("curclust: 0x%x\n", curclust);
  410. printf("Invalid FAT entry\n");
  411. return -1;
  412. }
  413. if (get_cluster(mydata, curclust, get_vfatname_block,
  414. mydata->clust_size * mydata->sect_size) != 0) {
  415. debug("Error: reading directory block\n");
  416. return -1;
  417. }
  418. slotptr2 = (dir_slot *)get_vfatname_block;
  419. while (counter > 0) {
  420. if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
  421. & 0xff) != counter)
  422. return -1;
  423. slotptr2++;
  424. counter--;
  425. }
  426. /* Save the real directory entry */
  427. realdent = (dir_entry *)slotptr2;
  428. while ((__u8 *)slotptr2 > get_vfatname_block) {
  429. slotptr2--;
  430. slot2str(slotptr2, l_name, &idx);
  431. }
  432. } else {
  433. /* Save the real directory entry */
  434. realdent = (dir_entry *)slotptr;
  435. }
  436. do {
  437. slotptr--;
  438. if (slot2str(slotptr, l_name, &idx))
  439. break;
  440. } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
  441. l_name[idx] = '\0';
  442. if (*l_name == DELETED_FLAG)
  443. *l_name = '\0';
  444. else if (*l_name == aRING)
  445. *l_name = DELETED_FLAG;
  446. downcase(l_name);
  447. /* Return the real directory entry */
  448. memcpy(retdent, realdent, sizeof(dir_entry));
  449. return 0;
  450. }
  451. /* Calculate short name checksum */
  452. static __u8 mkcksum(const char *str)
  453. {
  454. int i;
  455. __u8 ret = 0;
  456. for (i = 0; i < 11; i++) {
  457. ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + str[i];
  458. }
  459. return ret;
  460. }
  461. #endif /* CONFIG_SUPPORT_VFAT */
  462. /*
  463. * Get the directory entry associated with 'filename' from the directory
  464. * starting at 'startsect'
  465. */
  466. __u8 get_dentfromdir_block[MAX_CLUSTSIZE]
  467. __aligned(ARCH_DMA_MINALIGN);
  468. static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
  469. char *filename, dir_entry *retdent,
  470. int dols)
  471. {
  472. __u16 prevcksum = 0xffff;
  473. __u32 curclust = START(retdent);
  474. int files = 0, dirs = 0;
  475. debug("get_dentfromdir: %s\n", filename);
  476. while (1) {
  477. dir_entry *dentptr;
  478. int i;
  479. if (get_cluster(mydata, curclust, get_dentfromdir_block,
  480. mydata->clust_size * mydata->sect_size) != 0) {
  481. debug("Error: reading directory block\n");
  482. return NULL;
  483. }
  484. dentptr = (dir_entry *)get_dentfromdir_block;
  485. for (i = 0; i < DIRENTSPERCLUST; i++) {
  486. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  487. l_name[0] = '\0';
  488. if (dentptr->name[0] == DELETED_FLAG) {
  489. dentptr++;
  490. continue;
  491. }
  492. if ((dentptr->attr & ATTR_VOLUME)) {
  493. #ifdef CONFIG_SUPPORT_VFAT
  494. if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
  495. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  496. prevcksum = ((dir_slot *)dentptr)->alias_checksum;
  497. get_vfatname(mydata, curclust,
  498. get_dentfromdir_block,
  499. dentptr, l_name);
  500. if (dols) {
  501. int isdir;
  502. char dirc;
  503. int doit = 0;
  504. isdir = (dentptr->attr & ATTR_DIR);
  505. if (isdir) {
  506. dirs++;
  507. dirc = '/';
  508. doit = 1;
  509. } else {
  510. dirc = ' ';
  511. if (l_name[0] != 0) {
  512. files++;
  513. doit = 1;
  514. }
  515. }
  516. if (doit) {
  517. if (dirc == ' ') {
  518. printf(" %8ld %s%c\n",
  519. (long)FAT2CPU32(dentptr->size),
  520. l_name,
  521. dirc);
  522. } else {
  523. printf(" %s%c\n",
  524. l_name,
  525. dirc);
  526. }
  527. }
  528. dentptr++;
  529. continue;
  530. }
  531. debug("vfatname: |%s|\n", l_name);
  532. } else
  533. #endif
  534. {
  535. /* Volume label or VFAT entry */
  536. dentptr++;
  537. continue;
  538. }
  539. }
  540. if (dentptr->name[0] == 0) {
  541. if (dols) {
  542. printf("\n%d file(s), %d dir(s)\n\n",
  543. files, dirs);
  544. }
  545. debug("Dentname == NULL - %d\n", i);
  546. return NULL;
  547. }
  548. #ifdef CONFIG_SUPPORT_VFAT
  549. if (dols && mkcksum(dentptr->name) == prevcksum) {
  550. prevcksum = 0xffff;
  551. dentptr++;
  552. continue;
  553. }
  554. #endif
  555. get_name(dentptr, s_name);
  556. if (dols) {
  557. int isdir = (dentptr->attr & ATTR_DIR);
  558. char dirc;
  559. int doit = 0;
  560. if (isdir) {
  561. dirs++;
  562. dirc = '/';
  563. doit = 1;
  564. } else {
  565. dirc = ' ';
  566. if (s_name[0] != 0) {
  567. files++;
  568. doit = 1;
  569. }
  570. }
  571. if (doit) {
  572. if (dirc == ' ') {
  573. printf(" %8ld %s%c\n",
  574. (long)FAT2CPU32(dentptr->size),
  575. s_name, dirc);
  576. } else {
  577. printf(" %s%c\n",
  578. s_name, dirc);
  579. }
  580. }
  581. dentptr++;
  582. continue;
  583. }
  584. if (strcmp(filename, s_name)
  585. && strcmp(filename, l_name)) {
  586. debug("Mismatch: |%s|%s|\n", s_name, l_name);
  587. dentptr++;
  588. continue;
  589. }
  590. memcpy(retdent, dentptr, sizeof(dir_entry));
  591. debug("DentName: %s", s_name);
  592. debug(", start: 0x%x", START(dentptr));
  593. debug(", size: 0x%x %s\n",
  594. FAT2CPU32(dentptr->size),
  595. (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
  596. return retdent;
  597. }
  598. curclust = get_fatent(mydata, curclust);
  599. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  600. debug("curclust: 0x%x\n", curclust);
  601. printf("Invalid FAT entry\n");
  602. return NULL;
  603. }
  604. }
  605. return NULL;
  606. }
  607. /*
  608. * Read boot sector and volume info from a FAT filesystem
  609. */
  610. static int
  611. read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
  612. {
  613. __u8 *block;
  614. volume_info *vistart;
  615. int ret = 0;
  616. if (cur_dev == NULL) {
  617. debug("Error: no device selected\n");
  618. return -1;
  619. }
  620. block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
  621. if (block == NULL) {
  622. debug("Error: allocating block\n");
  623. return -1;
  624. }
  625. if (disk_read(0, 1, block) < 0) {
  626. debug("Error: reading block\n");
  627. goto fail;
  628. }
  629. memcpy(bs, block, sizeof(boot_sector));
  630. bs->reserved = FAT2CPU16(bs->reserved);
  631. bs->fat_length = FAT2CPU16(bs->fat_length);
  632. bs->secs_track = FAT2CPU16(bs->secs_track);
  633. bs->heads = FAT2CPU16(bs->heads);
  634. bs->total_sect = FAT2CPU32(bs->total_sect);
  635. /* FAT32 entries */
  636. if (bs->fat_length == 0) {
  637. /* Assume FAT32 */
  638. bs->fat32_length = FAT2CPU32(bs->fat32_length);
  639. bs->flags = FAT2CPU16(bs->flags);
  640. bs->root_cluster = FAT2CPU32(bs->root_cluster);
  641. bs->info_sector = FAT2CPU16(bs->info_sector);
  642. bs->backup_boot = FAT2CPU16(bs->backup_boot);
  643. vistart = (volume_info *)(block + sizeof(boot_sector));
  644. *fatsize = 32;
  645. } else {
  646. vistart = (volume_info *)&(bs->fat32_length);
  647. *fatsize = 0;
  648. }
  649. memcpy(volinfo, vistart, sizeof(volume_info));
  650. if (*fatsize == 32) {
  651. if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
  652. goto exit;
  653. } else {
  654. if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  655. *fatsize = 12;
  656. goto exit;
  657. }
  658. if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  659. *fatsize = 16;
  660. goto exit;
  661. }
  662. }
  663. debug("Error: broken fs_type sign\n");
  664. fail:
  665. ret = -1;
  666. exit:
  667. free(block);
  668. return ret;
  669. }
  670. __u8 do_fat_read_block[MAX_CLUSTSIZE]
  671. __aligned(ARCH_DMA_MINALIGN);
  672. long
  673. do_fat_read(const char *filename, void *buffer, unsigned long maxsize, int dols)
  674. {
  675. char fnamecopy[2048];
  676. boot_sector bs;
  677. volume_info volinfo;
  678. fsdata datablock;
  679. fsdata *mydata = &datablock;
  680. dir_entry *dentptr;
  681. __u16 prevcksum = 0xffff;
  682. char *subname = "";
  683. __u32 cursect;
  684. int idx, isdir = 0;
  685. int files = 0, dirs = 0;
  686. long ret = -1;
  687. int firsttime;
  688. __u32 root_cluster = 0;
  689. int rootdir_size = 0;
  690. int j;
  691. if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
  692. debug("Error: reading boot sector\n");
  693. return -1;
  694. }
  695. if (mydata->fatsize == 32) {
  696. root_cluster = bs.root_cluster;
  697. mydata->fatlength = bs.fat32_length;
  698. } else {
  699. mydata->fatlength = bs.fat_length;
  700. }
  701. mydata->fat_sect = bs.reserved;
  702. cursect = mydata->rootdir_sect
  703. = mydata->fat_sect + mydata->fatlength * bs.fats;
  704. mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
  705. mydata->clust_size = bs.cluster_size;
  706. if (mydata->sect_size != cur_part_info.blksz) {
  707. printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
  708. mydata->sect_size, cur_part_info.blksz);
  709. return -1;
  710. }
  711. if (mydata->fatsize == 32) {
  712. mydata->data_begin = mydata->rootdir_sect -
  713. (mydata->clust_size * 2);
  714. } else {
  715. rootdir_size = ((bs.dir_entries[1] * (int)256 +
  716. bs.dir_entries[0]) *
  717. sizeof(dir_entry)) /
  718. mydata->sect_size;
  719. mydata->data_begin = mydata->rootdir_sect +
  720. rootdir_size -
  721. (mydata->clust_size * 2);
  722. }
  723. mydata->fatbufnum = -1;
  724. mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
  725. if (mydata->fatbuf == NULL) {
  726. debug("Error: allocating memory\n");
  727. return -1;
  728. }
  729. #ifdef CONFIG_SUPPORT_VFAT
  730. debug("VFAT Support enabled\n");
  731. #endif
  732. debug("FAT%d, fat_sect: %d, fatlength: %d\n",
  733. mydata->fatsize, mydata->fat_sect, mydata->fatlength);
  734. debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
  735. "Data begins at: %d\n",
  736. root_cluster,
  737. mydata->rootdir_sect,
  738. mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
  739. debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
  740. mydata->clust_size);
  741. /* "cwd" is always the root... */
  742. while (ISDIRDELIM(*filename))
  743. filename++;
  744. /* Make a copy of the filename and convert it to lowercase */
  745. strcpy(fnamecopy, filename);
  746. downcase(fnamecopy);
  747. if (*fnamecopy == '\0') {
  748. if (!dols)
  749. goto exit;
  750. dols = LS_ROOT;
  751. } else if ((idx = dirdelim(fnamecopy)) >= 0) {
  752. isdir = 1;
  753. fnamecopy[idx] = '\0';
  754. subname = fnamecopy + idx + 1;
  755. /* Handle multiple delimiters */
  756. while (ISDIRDELIM(*subname))
  757. subname++;
  758. } else if (dols) {
  759. isdir = 1;
  760. }
  761. j = 0;
  762. while (1) {
  763. int i;
  764. debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
  765. cursect, mydata->clust_size, DIRENTSPERBLOCK);
  766. if (disk_read(cursect,
  767. (mydata->fatsize == 32) ?
  768. (mydata->clust_size) :
  769. PREFETCH_BLOCKS,
  770. do_fat_read_block) < 0) {
  771. debug("Error: reading rootdir block\n");
  772. goto exit;
  773. }
  774. dentptr = (dir_entry *) do_fat_read_block;
  775. for (i = 0; i < DIRENTSPERBLOCK; i++) {
  776. char s_name[14], l_name[VFAT_MAXLEN_BYTES];
  777. l_name[0] = '\0';
  778. if (dentptr->name[0] == DELETED_FLAG) {
  779. dentptr++;
  780. continue;
  781. }
  782. if ((dentptr->attr & ATTR_VOLUME)) {
  783. #ifdef CONFIG_SUPPORT_VFAT
  784. if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
  785. (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
  786. prevcksum =
  787. ((dir_slot *)dentptr)->alias_checksum;
  788. get_vfatname(mydata,
  789. root_cluster,
  790. do_fat_read_block,
  791. dentptr, l_name);
  792. if (dols == LS_ROOT) {
  793. char dirc;
  794. int doit = 0;
  795. int isdir =
  796. (dentptr->attr & ATTR_DIR);
  797. if (isdir) {
  798. dirs++;
  799. dirc = '/';
  800. doit = 1;
  801. } else {
  802. dirc = ' ';
  803. if (l_name[0] != 0) {
  804. files++;
  805. doit = 1;
  806. }
  807. }
  808. if (doit) {
  809. if (dirc == ' ') {
  810. printf(" %8ld %s%c\n",
  811. (long)FAT2CPU32(dentptr->size),
  812. l_name,
  813. dirc);
  814. } else {
  815. printf(" %s%c\n",
  816. l_name,
  817. dirc);
  818. }
  819. }
  820. dentptr++;
  821. continue;
  822. }
  823. debug("Rootvfatname: |%s|\n",
  824. l_name);
  825. } else
  826. #endif
  827. {
  828. /* Volume label or VFAT entry */
  829. dentptr++;
  830. continue;
  831. }
  832. } else if (dentptr->name[0] == 0) {
  833. debug("RootDentname == NULL - %d\n", i);
  834. if (dols == LS_ROOT) {
  835. printf("\n%d file(s), %d dir(s)\n\n",
  836. files, dirs);
  837. ret = 0;
  838. }
  839. goto exit;
  840. }
  841. #ifdef CONFIG_SUPPORT_VFAT
  842. else if (dols == LS_ROOT &&
  843. mkcksum(dentptr->name) == prevcksum) {
  844. prevcksum = 0xffff;
  845. dentptr++;
  846. continue;
  847. }
  848. #endif
  849. get_name(dentptr, s_name);
  850. if (dols == LS_ROOT) {
  851. int isdir = (dentptr->attr & ATTR_DIR);
  852. char dirc;
  853. int doit = 0;
  854. if (isdir) {
  855. dirc = '/';
  856. if (s_name[0] != 0) {
  857. dirs++;
  858. doit = 1;
  859. }
  860. } else {
  861. dirc = ' ';
  862. if (s_name[0] != 0) {
  863. files++;
  864. doit = 1;
  865. }
  866. }
  867. if (doit) {
  868. if (dirc == ' ') {
  869. printf(" %8ld %s%c\n",
  870. (long)FAT2CPU32(dentptr->size),
  871. s_name, dirc);
  872. } else {
  873. printf(" %s%c\n",
  874. s_name, dirc);
  875. }
  876. }
  877. dentptr++;
  878. continue;
  879. }
  880. if (strcmp(fnamecopy, s_name)
  881. && strcmp(fnamecopy, l_name)) {
  882. debug("RootMismatch: |%s|%s|\n", s_name,
  883. l_name);
  884. dentptr++;
  885. continue;
  886. }
  887. if (isdir && !(dentptr->attr & ATTR_DIR))
  888. goto exit;
  889. debug("RootName: %s", s_name);
  890. debug(", start: 0x%x", START(dentptr));
  891. debug(", size: 0x%x %s\n",
  892. FAT2CPU32(dentptr->size),
  893. isdir ? "(DIR)" : "");
  894. goto rootdir_done; /* We got a match */
  895. }
  896. debug("END LOOP: j=%d clust_size=%d\n", j,
  897. mydata->clust_size);
  898. /*
  899. * On FAT32 we must fetch the FAT entries for the next
  900. * root directory clusters when a cluster has been
  901. * completely processed.
  902. */
  903. ++j;
  904. int fat32_end = 0;
  905. if ((mydata->fatsize == 32) && (j == mydata->clust_size)) {
  906. int nxtsect = 0;
  907. int nxt_clust = 0;
  908. nxt_clust = get_fatent(mydata, root_cluster);
  909. fat32_end = CHECK_CLUST(nxt_clust, 32);
  910. nxtsect = mydata->data_begin +
  911. (nxt_clust * mydata->clust_size);
  912. root_cluster = nxt_clust;
  913. cursect = nxtsect;
  914. j = 0;
  915. } else {
  916. cursect++;
  917. }
  918. /* If end of rootdir reached */
  919. if ((mydata->fatsize == 32 && fat32_end) ||
  920. (mydata->fatsize != 32 && j == rootdir_size)) {
  921. if (dols == LS_ROOT) {
  922. printf("\n%d file(s), %d dir(s)\n\n",
  923. files, dirs);
  924. ret = 0;
  925. }
  926. goto exit;
  927. }
  928. }
  929. rootdir_done:
  930. firsttime = 1;
  931. while (isdir) {
  932. int startsect = mydata->data_begin
  933. + START(dentptr) * mydata->clust_size;
  934. dir_entry dent;
  935. char *nextname = NULL;
  936. dent = *dentptr;
  937. dentptr = &dent;
  938. idx = dirdelim(subname);
  939. if (idx >= 0) {
  940. subname[idx] = '\0';
  941. nextname = subname + idx + 1;
  942. /* Handle multiple delimiters */
  943. while (ISDIRDELIM(*nextname))
  944. nextname++;
  945. if (dols && *nextname == '\0')
  946. firsttime = 0;
  947. } else {
  948. if (dols && firsttime) {
  949. firsttime = 0;
  950. } else {
  951. isdir = 0;
  952. }
  953. }
  954. if (get_dentfromdir(mydata, startsect, subname, dentptr,
  955. isdir ? 0 : dols) == NULL) {
  956. if (dols && !isdir)
  957. ret = 0;
  958. goto exit;
  959. }
  960. if (idx >= 0) {
  961. if (!(dentptr->attr & ATTR_DIR))
  962. goto exit;
  963. subname = nextname;
  964. }
  965. }
  966. ret = get_contents(mydata, dentptr, buffer, maxsize);
  967. debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
  968. exit:
  969. free(mydata->fatbuf);
  970. return ret;
  971. }
  972. int file_fat_detectfs(void)
  973. {
  974. boot_sector bs;
  975. volume_info volinfo;
  976. int fatsize;
  977. char vol_label[12];
  978. if (cur_dev == NULL) {
  979. printf("No current device\n");
  980. return 1;
  981. }
  982. #if defined(CONFIG_CMD_IDE) || \
  983. defined(CONFIG_CMD_SATA) || \
  984. defined(CONFIG_CMD_SCSI) || \
  985. defined(CONFIG_CMD_USB) || \
  986. defined(CONFIG_MMC)
  987. printf("Interface: ");
  988. switch (cur_dev->if_type) {
  989. case IF_TYPE_IDE:
  990. printf("IDE");
  991. break;
  992. case IF_TYPE_SATA:
  993. printf("SATA");
  994. break;
  995. case IF_TYPE_SCSI:
  996. printf("SCSI");
  997. break;
  998. case IF_TYPE_ATAPI:
  999. printf("ATAPI");
  1000. break;
  1001. case IF_TYPE_USB:
  1002. printf("USB");
  1003. break;
  1004. case IF_TYPE_DOC:
  1005. printf("DOC");
  1006. break;
  1007. case IF_TYPE_MMC:
  1008. printf("MMC");
  1009. break;
  1010. default:
  1011. printf("Unknown");
  1012. }
  1013. printf("\n Device %d: ", cur_dev->dev);
  1014. dev_print(cur_dev);
  1015. #endif
  1016. if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
  1017. printf("\nNo valid FAT fs found\n");
  1018. return 1;
  1019. }
  1020. memcpy(vol_label, volinfo.volume_label, 11);
  1021. vol_label[11] = '\0';
  1022. volinfo.fs_type[5] = '\0';
  1023. printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part_nr,
  1024. volinfo.fs_type, vol_label);
  1025. return 0;
  1026. }
  1027. int file_fat_ls(const char *dir)
  1028. {
  1029. return do_fat_read(dir, NULL, 0, LS_YES);
  1030. }
  1031. long file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
  1032. {
  1033. printf("reading %s\n", filename);
  1034. return do_fat_read(filename, buffer, maxsize, LS_NO);
  1035. }