fat.c 30 KB

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