fat.c 28 KB

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