fat.c 30 KB

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