fat.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * fat.c
  4. *
  5. * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
  6. *
  7. * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
  8. * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
  9. */
  10. #define LOG_CATEGORY LOGC_FS
  11. #include <common.h>
  12. #include <blk.h>
  13. #include <config.h>
  14. #include <exports.h>
  15. #include <fat.h>
  16. #include <fs.h>
  17. #include <log.h>
  18. #include <asm/byteorder.h>
  19. #include <part.h>
  20. #include <malloc.h>
  21. #include <memalign.h>
  22. #include <asm/cache.h>
  23. #include <linux/compiler.h>
  24. #include <linux/ctype.h>
  25. /*
  26. * Convert a string to lowercase. Converts at most 'len' characters,
  27. * 'len' may be larger than the length of 'str' if 'str' is NULL
  28. * terminated.
  29. */
  30. static void downcase(char *str, size_t len)
  31. {
  32. while (*str != '\0' && len--) {
  33. *str = tolower(*str);
  34. str++;
  35. }
  36. }
  37. static struct blk_desc *cur_dev;
  38. static struct disk_partition 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, struct disk_partition *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. struct disk_partition 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. log_err("Partition %d invalid on device %d\n", part_no,
  84. 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. * Extract zero terminated short name from a directory entry.
  101. */
  102. static void get_name(dir_entry *dirent, char *s_name)
  103. {
  104. char *ptr;
  105. memcpy(s_name, dirent->nameext.name, 8);
  106. s_name[8] = '\0';
  107. ptr = s_name;
  108. while (*ptr && *ptr != ' ')
  109. ptr++;
  110. if (dirent->lcase & CASE_LOWER_BASE)
  111. downcase(s_name, (unsigned)(ptr - s_name));
  112. if (dirent->nameext.ext[0] && dirent->nameext.ext[0] != ' ') {
  113. *ptr++ = '.';
  114. memcpy(ptr, dirent->nameext.ext, 3);
  115. if (dirent->lcase & CASE_LOWER_EXT)
  116. downcase(ptr, 3);
  117. ptr[3] = '\0';
  118. while (*ptr && *ptr != ' ')
  119. ptr++;
  120. }
  121. *ptr = '\0';
  122. if (*s_name == DELETED_FLAG)
  123. *s_name = '\0';
  124. else if (*s_name == aRING)
  125. *s_name = DELETED_FLAG;
  126. }
  127. static int flush_dirty_fat_buffer(fsdata *mydata);
  128. #if !CONFIG_IS_ENABLED(FAT_WRITE)
  129. /* Stub for read only operation */
  130. int flush_dirty_fat_buffer(fsdata *mydata)
  131. {
  132. (void)(mydata);
  133. return 0;
  134. }
  135. #endif
  136. /*
  137. * Get the entry at index 'entry' in a FAT (12/16/32) table.
  138. * On failure 0x00 is returned.
  139. */
  140. static __u32 get_fatent(fsdata *mydata, __u32 entry)
  141. {
  142. __u32 bufnum;
  143. __u32 offset, off8;
  144. __u32 ret = 0x00;
  145. if (CHECK_CLUST(entry, mydata->fatsize)) {
  146. log_err("Invalid FAT entry: %#08x\n", entry);
  147. return ret;
  148. }
  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%08x = %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. /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
  175. if (startblock + getsize > fatlength)
  176. getsize = fatlength - startblock;
  177. startblock += mydata->fat_sect; /* Offset from start of disk */
  178. /* Write back the fatbuf to the disk */
  179. if (flush_dirty_fat_buffer(mydata) < 0)
  180. return -1;
  181. if (disk_read(startblock, getsize, bufptr) < 0) {
  182. debug("Error reading FAT blocks\n");
  183. return ret;
  184. }
  185. mydata->fatbufnum = bufnum;
  186. }
  187. /* Get the actual entry from the table */
  188. switch (mydata->fatsize) {
  189. case 32:
  190. ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
  191. break;
  192. case 16:
  193. ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
  194. break;
  195. case 12:
  196. off8 = (offset * 3) / 2;
  197. /* fatbut + off8 may be unaligned, read in byte granularity */
  198. ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
  199. if (offset & 0x1)
  200. ret >>= 4;
  201. ret &= 0xfff;
  202. }
  203. debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
  204. mydata->fatsize, ret, entry, offset);
  205. return ret;
  206. }
  207. /*
  208. * Read at most 'size' bytes from the specified cluster into 'buffer'.
  209. * Return 0 on success, -1 otherwise.
  210. */
  211. static int
  212. get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
  213. {
  214. __u32 startsect;
  215. int ret;
  216. if (clustnum > 0) {
  217. startsect = clust_to_sect(mydata, clustnum);
  218. } else {
  219. startsect = mydata->rootdir_sect;
  220. }
  221. debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
  222. if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
  223. ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
  224. debug("FAT: Misaligned buffer address (%p)\n", buffer);
  225. while (size >= mydata->sect_size) {
  226. ret = disk_read(startsect++, 1, tmpbuf);
  227. if (ret != 1) {
  228. debug("Error reading data (got %d)\n", ret);
  229. return -1;
  230. }
  231. memcpy(buffer, tmpbuf, mydata->sect_size);
  232. buffer += mydata->sect_size;
  233. size -= mydata->sect_size;
  234. }
  235. } else if (size >= mydata->sect_size) {
  236. __u32 bytes_read;
  237. __u32 sect_count = size / mydata->sect_size;
  238. ret = disk_read(startsect, sect_count, buffer);
  239. if (ret != sect_count) {
  240. debug("Error reading data (got %d)\n", ret);
  241. return -1;
  242. }
  243. bytes_read = sect_count * mydata->sect_size;
  244. startsect += sect_count;
  245. buffer += bytes_read;
  246. size -= bytes_read;
  247. }
  248. if (size) {
  249. ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
  250. ret = disk_read(startsect, 1, tmpbuf);
  251. if (ret != 1) {
  252. debug("Error reading data (got %d)\n", ret);
  253. return -1;
  254. }
  255. memcpy(buffer, tmpbuf, size);
  256. }
  257. return 0;
  258. }
  259. /**
  260. * get_contents() - read from file
  261. *
  262. * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
  263. * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on
  264. * fatal errors.
  265. *
  266. * @mydata: file system description
  267. * @dentprt: directory entry pointer
  268. * @pos: position from where to read
  269. * @buffer: buffer into which to read
  270. * @maxsize: maximum number of bytes to read
  271. * @gotsize: number of bytes actually read
  272. * Return: -1 on error, otherwise 0
  273. */
  274. static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
  275. __u8 *buffer, loff_t maxsize, loff_t *gotsize)
  276. {
  277. loff_t filesize = FAT2CPU32(dentptr->size);
  278. unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
  279. __u32 curclust = START(dentptr);
  280. __u32 endclust, newclust;
  281. loff_t actsize;
  282. *gotsize = 0;
  283. debug("Filesize: %llu bytes\n", filesize);
  284. if (pos >= filesize) {
  285. debug("Read position past EOF: %llu\n", pos);
  286. return 0;
  287. }
  288. if (maxsize > 0 && filesize > pos + maxsize)
  289. filesize = pos + maxsize;
  290. debug("%llu bytes\n", filesize);
  291. actsize = bytesperclust;
  292. /* go to cluster at pos */
  293. while (actsize <= pos) {
  294. curclust = get_fatent(mydata, curclust);
  295. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  296. debug("curclust: 0x%x\n", curclust);
  297. printf("Invalid FAT entry\n");
  298. return -1;
  299. }
  300. actsize += bytesperclust;
  301. }
  302. /* actsize > pos */
  303. actsize -= bytesperclust;
  304. filesize -= actsize;
  305. pos -= actsize;
  306. /* align to beginning of next cluster if any */
  307. if (pos) {
  308. __u8 *tmp_buffer;
  309. actsize = min(filesize, (loff_t)bytesperclust);
  310. tmp_buffer = malloc_cache_aligned(actsize);
  311. if (!tmp_buffer) {
  312. debug("Error: allocating buffer\n");
  313. return -1;
  314. }
  315. if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) {
  316. printf("Error reading cluster\n");
  317. free(tmp_buffer);
  318. return -1;
  319. }
  320. filesize -= actsize;
  321. actsize -= pos;
  322. memcpy(buffer, tmp_buffer + pos, actsize);
  323. free(tmp_buffer);
  324. *gotsize += actsize;
  325. if (!filesize)
  326. return 0;
  327. buffer += actsize;
  328. curclust = get_fatent(mydata, curclust);
  329. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  330. debug("curclust: 0x%x\n", curclust);
  331. printf("Invalid FAT entry\n");
  332. return -1;
  333. }
  334. }
  335. actsize = bytesperclust;
  336. endclust = curclust;
  337. do {
  338. /* search for consecutive clusters */
  339. while (actsize < filesize) {
  340. newclust = get_fatent(mydata, endclust);
  341. if ((newclust - 1) != endclust)
  342. goto getit;
  343. if (CHECK_CLUST(newclust, mydata->fatsize)) {
  344. debug("curclust: 0x%x\n", newclust);
  345. printf("Invalid FAT entry\n");
  346. return -1;
  347. }
  348. endclust = newclust;
  349. actsize += bytesperclust;
  350. }
  351. /* get remaining bytes */
  352. actsize = filesize;
  353. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  354. printf("Error reading cluster\n");
  355. return -1;
  356. }
  357. *gotsize += actsize;
  358. return 0;
  359. getit:
  360. if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
  361. printf("Error reading cluster\n");
  362. return -1;
  363. }
  364. *gotsize += (int)actsize;
  365. filesize -= actsize;
  366. buffer += actsize;
  367. curclust = get_fatent(mydata, endclust);
  368. if (CHECK_CLUST(curclust, mydata->fatsize)) {
  369. debug("curclust: 0x%x\n", curclust);
  370. printf("Invalid FAT entry\n");
  371. return -1;
  372. }
  373. actsize = bytesperclust;
  374. endclust = curclust;
  375. } while (1);
  376. }
  377. /*
  378. * Extract the file name information from 'slotptr' into 'l_name',
  379. * starting at l_name[*idx].
  380. * Return 1 if terminator (zero byte) is found, 0 otherwise.
  381. */
  382. static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
  383. {
  384. int j;
  385. for (j = 0; j <= 8; j += 2) {
  386. l_name[*idx] = slotptr->name0_4[j];
  387. if (l_name[*idx] == 0x00)
  388. return 1;
  389. (*idx)++;
  390. }
  391. for (j = 0; j <= 10; j += 2) {
  392. l_name[*idx] = slotptr->name5_10[j];
  393. if (l_name[*idx] == 0x00)
  394. return 1;
  395. (*idx)++;
  396. }
  397. for (j = 0; j <= 2; j += 2) {
  398. l_name[*idx] = slotptr->name11_12[j];
  399. if (l_name[*idx] == 0x00)
  400. return 1;
  401. (*idx)++;
  402. }
  403. return 0;
  404. }
  405. /* Calculate short name checksum */
  406. static __u8 mkcksum(struct nameext *nameext)
  407. {
  408. int i;
  409. u8 *pos = (void *)nameext;
  410. __u8 ret = 0;
  411. for (i = 0; i < 11; i++)
  412. ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + pos[i];
  413. return ret;
  414. }
  415. /*
  416. * Read boot sector and volume info from a FAT filesystem
  417. */
  418. static int
  419. read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
  420. {
  421. __u8 *block;
  422. volume_info *vistart;
  423. int ret = 0;
  424. if (cur_dev == NULL) {
  425. debug("Error: no device selected\n");
  426. return -1;
  427. }
  428. block = malloc_cache_aligned(cur_dev->blksz);
  429. if (block == NULL) {
  430. debug("Error: allocating block\n");
  431. return -1;
  432. }
  433. if (disk_read(0, 1, block) < 0) {
  434. debug("Error: reading block\n");
  435. goto fail;
  436. }
  437. memcpy(bs, block, sizeof(boot_sector));
  438. bs->reserved = FAT2CPU16(bs->reserved);
  439. bs->fat_length = FAT2CPU16(bs->fat_length);
  440. bs->secs_track = FAT2CPU16(bs->secs_track);
  441. bs->heads = FAT2CPU16(bs->heads);
  442. bs->total_sect = FAT2CPU32(bs->total_sect);
  443. /* FAT32 entries */
  444. if (bs->fat_length == 0) {
  445. /* Assume FAT32 */
  446. bs->fat32_length = FAT2CPU32(bs->fat32_length);
  447. bs->flags = FAT2CPU16(bs->flags);
  448. bs->root_cluster = FAT2CPU32(bs->root_cluster);
  449. bs->info_sector = FAT2CPU16(bs->info_sector);
  450. bs->backup_boot = FAT2CPU16(bs->backup_boot);
  451. vistart = (volume_info *)(block + sizeof(boot_sector));
  452. *fatsize = 32;
  453. } else {
  454. vistart = (volume_info *)&(bs->fat32_length);
  455. *fatsize = 0;
  456. }
  457. memcpy(volinfo, vistart, sizeof(volume_info));
  458. if (*fatsize == 32) {
  459. if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
  460. goto exit;
  461. } else {
  462. if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  463. *fatsize = 12;
  464. goto exit;
  465. }
  466. if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  467. *fatsize = 16;
  468. goto exit;
  469. }
  470. }
  471. debug("Error: broken fs_type sign\n");
  472. fail:
  473. ret = -1;
  474. exit:
  475. free(block);
  476. return ret;
  477. }
  478. static int get_fs_info(fsdata *mydata)
  479. {
  480. boot_sector bs;
  481. volume_info volinfo;
  482. int ret;
  483. ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
  484. if (ret) {
  485. debug("Error: reading boot sector\n");
  486. return ret;
  487. }
  488. if (mydata->fatsize == 32) {
  489. mydata->fatlength = bs.fat32_length;
  490. mydata->total_sect = bs.total_sect;
  491. } else {
  492. mydata->fatlength = bs.fat_length;
  493. mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
  494. if (!mydata->total_sect)
  495. mydata->total_sect = bs.total_sect;
  496. }
  497. if (!mydata->total_sect) /* unlikely */
  498. mydata->total_sect = (u32)cur_part_info.size;
  499. mydata->fats = bs.fats;
  500. mydata->fat_sect = bs.reserved;
  501. mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
  502. mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
  503. mydata->clust_size = bs.cluster_size;
  504. if (mydata->sect_size != cur_part_info.blksz) {
  505. log_err("FAT sector size mismatch (fs=%u, dev=%lu)\n",
  506. mydata->sect_size, cur_part_info.blksz);
  507. return -1;
  508. }
  509. if (mydata->clust_size == 0) {
  510. log_err("FAT cluster size not set\n");
  511. return -1;
  512. }
  513. if ((unsigned int)mydata->clust_size * mydata->sect_size >
  514. MAX_CLUSTSIZE) {
  515. log_err("FAT cluster size too big (cs=%u, max=%u)\n",
  516. (uint)mydata->clust_size * mydata->sect_size,
  517. MAX_CLUSTSIZE);
  518. return -1;
  519. }
  520. if (mydata->fatsize == 32) {
  521. mydata->data_begin = mydata->rootdir_sect -
  522. (mydata->clust_size * 2);
  523. mydata->root_cluster = bs.root_cluster;
  524. } else {
  525. mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
  526. bs.dir_entries[0]) *
  527. sizeof(dir_entry)) /
  528. mydata->sect_size;
  529. mydata->data_begin = mydata->rootdir_sect +
  530. mydata->rootdir_size -
  531. (mydata->clust_size * 2);
  532. /*
  533. * The root directory is not cluster-aligned and may be on a
  534. * "negative" cluster, this will be handled specially in
  535. * fat_next_cluster().
  536. */
  537. mydata->root_cluster = 0;
  538. }
  539. mydata->fatbufnum = -1;
  540. mydata->fat_dirty = 0;
  541. mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
  542. if (mydata->fatbuf == NULL) {
  543. debug("Error: allocating memory\n");
  544. return -1;
  545. }
  546. debug("FAT%d, fat_sect: %d, fatlength: %d\n",
  547. mydata->fatsize, mydata->fat_sect, mydata->fatlength);
  548. debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
  549. "Data begins at: %d\n",
  550. mydata->root_cluster,
  551. mydata->rootdir_sect,
  552. mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
  553. debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
  554. mydata->clust_size);
  555. return 0;
  556. }
  557. /**
  558. * struct fat_itr - directory iterator, to simplify filesystem traversal
  559. *
  560. * Implements an iterator pattern to traverse directory tables,
  561. * transparently handling directory tables split across multiple
  562. * clusters, and the difference between FAT12/FAT16 root directory
  563. * (contiguous) and subdirectories + FAT32 root (chained).
  564. *
  565. * Rough usage
  566. *
  567. * .. code-block:: c
  568. *
  569. * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
  570. * // to traverse down to a subdirectory pointed to by
  571. * // current iterator position:
  572. * fat_itr_child(&itr, &itr);
  573. * }
  574. *
  575. * For a more complete example, see fat_itr_resolve().
  576. */
  577. struct fat_itr {
  578. /**
  579. * @fsdata: filesystem parameters
  580. */
  581. fsdata *fsdata;
  582. /**
  583. * @start_clust: first cluster
  584. */
  585. unsigned int start_clust;
  586. /**
  587. * @clust: current cluster
  588. */
  589. unsigned int clust;
  590. /**
  591. * @next_clust: next cluster if remaining == 0
  592. */
  593. unsigned int next_clust;
  594. /**
  595. * @last_cluster: set if last cluster of directory reached
  596. */
  597. int last_cluster;
  598. /**
  599. * @is_root: is iterator at root directory
  600. */
  601. int is_root;
  602. /**
  603. * @remaining: remaining directory entries in current cluster
  604. */
  605. int remaining;
  606. /**
  607. * @dent: current directory entry
  608. */
  609. dir_entry *dent;
  610. /**
  611. * @dent_rem: remaining entries after long name start
  612. */
  613. int dent_rem;
  614. /**
  615. * @dent_clust: cluster of long name start
  616. */
  617. unsigned int dent_clust;
  618. /**
  619. * @dent_start: first directory entry for long name
  620. */
  621. dir_entry *dent_start;
  622. /**
  623. * @l_name: long name of current directory entry
  624. */
  625. char l_name[VFAT_MAXLEN_BYTES];
  626. /**
  627. * @s_name: short 8.3 name of current directory entry
  628. */
  629. char s_name[14];
  630. /**
  631. * @name: l_name if there is one, else s_name
  632. */
  633. char *name;
  634. /**
  635. * @block: buffer for current cluster
  636. */
  637. u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
  638. };
  639. static int fat_itr_isdir(fat_itr *itr);
  640. /**
  641. * fat_itr_root() - initialize an iterator to start at the root
  642. * directory
  643. *
  644. * @itr: iterator to initialize
  645. * @fsdata: filesystem data for the partition
  646. * Return: 0 on success, else -errno
  647. */
  648. static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
  649. {
  650. if (get_fs_info(fsdata))
  651. return -ENXIO;
  652. itr->fsdata = fsdata;
  653. itr->start_clust = fsdata->root_cluster;
  654. itr->clust = fsdata->root_cluster;
  655. itr->next_clust = fsdata->root_cluster;
  656. itr->dent = NULL;
  657. itr->remaining = 0;
  658. itr->last_cluster = 0;
  659. itr->is_root = 1;
  660. return 0;
  661. }
  662. /**
  663. * fat_itr_child() - initialize an iterator to descend into a sub-
  664. * directory
  665. *
  666. * Initializes 'itr' to iterate the contents of the directory at
  667. * the current cursor position of 'parent'. It is an error to
  668. * call this if the current cursor of 'parent' is pointing at a
  669. * regular file.
  670. *
  671. * Note that 'itr' and 'parent' can be the same pointer if you do
  672. * not need to preserve 'parent' after this call, which is useful
  673. * for traversing directory structure to resolve a file/directory.
  674. *
  675. * @itr: iterator to initialize
  676. * @parent: the iterator pointing at a directory entry in the
  677. * parent directory of the directory to iterate
  678. */
  679. static void fat_itr_child(fat_itr *itr, fat_itr *parent)
  680. {
  681. fsdata *mydata = parent->fsdata; /* for silly macros */
  682. unsigned clustnum = START(parent->dent);
  683. assert(fat_itr_isdir(parent));
  684. itr->fsdata = parent->fsdata;
  685. itr->start_clust = clustnum;
  686. if (clustnum > 0) {
  687. itr->clust = clustnum;
  688. itr->next_clust = clustnum;
  689. itr->is_root = 0;
  690. } else {
  691. itr->clust = parent->fsdata->root_cluster;
  692. itr->next_clust = parent->fsdata->root_cluster;
  693. itr->start_clust = parent->fsdata->root_cluster;
  694. itr->is_root = 1;
  695. }
  696. itr->dent = NULL;
  697. itr->remaining = 0;
  698. itr->last_cluster = 0;
  699. }
  700. /**
  701. * fat_next_cluster() - load next FAT cluster
  702. *
  703. * The function is used when iterating through directories. It loads the
  704. * next cluster with directory entries
  705. *
  706. * @itr: directory iterator
  707. * @nbytes: number of bytes read, 0 on error
  708. * Return: first directory entry, NULL on error
  709. */
  710. void *fat_next_cluster(fat_itr *itr, unsigned int *nbytes)
  711. {
  712. int ret;
  713. u32 sect;
  714. u32 read_size;
  715. /* have we reached the end? */
  716. if (itr->last_cluster)
  717. return NULL;
  718. if (itr->is_root && itr->fsdata->fatsize != 32) {
  719. /*
  720. * The root directory is located before the data area and
  721. * cannot be indexed using the regular unsigned cluster
  722. * numbers (it may start at a "negative" cluster or not at a
  723. * cluster boundary at all), so consider itr->next_clust to be
  724. * a offset in cluster-sized units from the start of rootdir.
  725. */
  726. unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size;
  727. unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset;
  728. sect = itr->fsdata->rootdir_sect + sect_offset;
  729. /* do not read past the end of rootdir */
  730. read_size = min_t(u32, itr->fsdata->clust_size,
  731. remaining_sects);
  732. } else {
  733. sect = clust_to_sect(itr->fsdata, itr->next_clust);
  734. read_size = itr->fsdata->clust_size;
  735. }
  736. log_debug("FAT read(sect=%d), clust_size=%d, read_size=%u\n",
  737. sect, itr->fsdata->clust_size, read_size);
  738. /*
  739. * NOTE: do_fat_read_at() had complicated logic to deal w/
  740. * vfat names that span multiple clusters in the fat16 case,
  741. * which get_dentfromdir() probably also needed (and was
  742. * missing). And not entirely sure what fat32 didn't have
  743. * the same issue.. We solve that by only caring about one
  744. * dent at a time and iteratively constructing the vfat long
  745. * name.
  746. */
  747. ret = disk_read(sect, read_size, itr->block);
  748. if (ret < 0) {
  749. debug("Error: reading block\n");
  750. return NULL;
  751. }
  752. *nbytes = read_size * itr->fsdata->sect_size;
  753. itr->clust = itr->next_clust;
  754. if (itr->is_root && itr->fsdata->fatsize != 32) {
  755. itr->next_clust++;
  756. if (itr->next_clust * itr->fsdata->clust_size >=
  757. itr->fsdata->rootdir_size) {
  758. debug("nextclust: 0x%x\n", itr->next_clust);
  759. itr->last_cluster = 1;
  760. }
  761. } else {
  762. itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
  763. if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
  764. debug("nextclust: 0x%x\n", itr->next_clust);
  765. itr->last_cluster = 1;
  766. }
  767. }
  768. return itr->block;
  769. }
  770. static dir_entry *next_dent(fat_itr *itr)
  771. {
  772. if (itr->remaining == 0) {
  773. unsigned nbytes;
  774. struct dir_entry *dent = fat_next_cluster(itr, &nbytes);
  775. /* have we reached the last cluster? */
  776. if (!dent) {
  777. /* a sign for no more entries left */
  778. itr->dent = NULL;
  779. return NULL;
  780. }
  781. itr->remaining = nbytes / sizeof(dir_entry) - 1;
  782. itr->dent = dent;
  783. } else {
  784. itr->remaining--;
  785. itr->dent++;
  786. }
  787. /* have we reached the last valid entry? */
  788. if (itr->dent->nameext.name[0] == 0)
  789. return NULL;
  790. return itr->dent;
  791. }
  792. static dir_entry *extract_vfat_name(fat_itr *itr)
  793. {
  794. struct dir_entry *dent = itr->dent;
  795. int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK;
  796. u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
  797. int n = 0;
  798. while (seqn--) {
  799. char buf[13];
  800. int idx = 0;
  801. slot2str((dir_slot *)dent, buf, &idx);
  802. if (n + idx >= sizeof(itr->l_name))
  803. return NULL;
  804. /* shift accumulated long-name up and copy new part in: */
  805. memmove(itr->l_name + idx, itr->l_name, n);
  806. memcpy(itr->l_name, buf, idx);
  807. n += idx;
  808. dent = next_dent(itr);
  809. if (!dent)
  810. return NULL;
  811. }
  812. /*
  813. * We are now at the short file name entry.
  814. * If it is marked as deleted, just skip it.
  815. */
  816. if (dent->nameext.name[0] == DELETED_FLAG ||
  817. dent->nameext.name[0] == aRING)
  818. return NULL;
  819. itr->l_name[n] = '\0';
  820. chksum = mkcksum(&dent->nameext);
  821. /* checksum mismatch could mean deleted file, etc.. skip it: */
  822. if (chksum != alias_checksum) {
  823. debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
  824. chksum, alias_checksum, itr->l_name, dent->nameext.name,
  825. dent->nameext.ext);
  826. return NULL;
  827. }
  828. return dent;
  829. }
  830. /**
  831. * fat_itr_next() - step to the next entry in a directory
  832. *
  833. * Must be called once on a new iterator before the cursor is valid.
  834. *
  835. * @itr: the iterator to iterate
  836. * Return: boolean, 1 if success or 0 if no more entries in the
  837. * current directory
  838. */
  839. static int fat_itr_next(fat_itr *itr)
  840. {
  841. dir_entry *dent;
  842. itr->name = NULL;
  843. /*
  844. * One logical directory entry consist of following slots:
  845. * name[0] Attributes
  846. * dent[N - N]: LFN[N - 1] N|0x40 ATTR_VFAT
  847. * ...
  848. * dent[N - 2]: LFN[1] 2 ATTR_VFAT
  849. * dent[N - 1]: LFN[0] 1 ATTR_VFAT
  850. * dent[N]: SFN ATTR_ARCH
  851. */
  852. while (1) {
  853. dent = next_dent(itr);
  854. if (!dent) {
  855. itr->dent_start = NULL;
  856. return 0;
  857. }
  858. itr->dent_rem = itr->remaining;
  859. itr->dent_start = itr->dent;
  860. itr->dent_clust = itr->clust;
  861. if (dent->nameext.name[0] == DELETED_FLAG)
  862. continue;
  863. if (dent->attr & ATTR_VOLUME) {
  864. if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
  865. (dent->nameext.name[0] & LAST_LONG_ENTRY_MASK)) {
  866. /* long file name */
  867. dent = extract_vfat_name(itr);
  868. /*
  869. * If succeeded, dent has a valid short file
  870. * name entry for the current entry.
  871. * If failed, itr points to a current bogus
  872. * entry. So after fetching a next one,
  873. * it may have a short file name entry
  874. * for this bogus entry so that we can still
  875. * check for a short name.
  876. */
  877. if (!dent)
  878. continue;
  879. itr->name = itr->l_name;
  880. break;
  881. } else {
  882. /* Volume label or VFAT entry, skip */
  883. continue;
  884. }
  885. }
  886. /* short file name */
  887. break;
  888. }
  889. get_name(dent, itr->s_name);
  890. if (!itr->name)
  891. itr->name = itr->s_name;
  892. return 1;
  893. }
  894. /**
  895. * fat_itr_isdir() - is current cursor position pointing to a directory
  896. *
  897. * @itr: the iterator
  898. * Return: true if cursor is at a directory
  899. */
  900. static int fat_itr_isdir(fat_itr *itr)
  901. {
  902. return !!(itr->dent->attr & ATTR_DIR);
  903. }
  904. /*
  905. * Helpers:
  906. */
  907. #define TYPE_FILE 0x1
  908. #define TYPE_DIR 0x2
  909. #define TYPE_ANY (TYPE_FILE | TYPE_DIR)
  910. /**
  911. * fat_itr_resolve() - traverse directory structure to resolve the
  912. * requested path.
  913. *
  914. * Traverse directory structure to the requested path. If the specified
  915. * path is to a directory, this will descend into the directory and
  916. * leave it iterator at the start of the directory. If the path is to a
  917. * file, it will leave the iterator in the parent directory with current
  918. * cursor at file's entry in the directory.
  919. *
  920. * @itr: iterator initialized to root
  921. * @path: the requested path
  922. * @type: bitmask of allowable file types
  923. * Return: 0 on success or -errno
  924. */
  925. static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
  926. {
  927. const char *next;
  928. /* chomp any extra leading slashes: */
  929. while (path[0] && ISDIRDELIM(path[0]))
  930. path++;
  931. /* are we at the end? */
  932. if (strlen(path) == 0) {
  933. if (!(type & TYPE_DIR))
  934. return -ENOENT;
  935. return 0;
  936. }
  937. /* find length of next path entry: */
  938. next = path;
  939. while (next[0] && !ISDIRDELIM(next[0]))
  940. next++;
  941. if (itr->is_root) {
  942. /* root dir doesn't have "." nor ".." */
  943. if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
  944. (((next - path) == 2) && !strncmp(path, "..", 2))) {
  945. /* point back to itself */
  946. itr->clust = itr->fsdata->root_cluster;
  947. itr->next_clust = itr->fsdata->root_cluster;
  948. itr->start_clust = itr->fsdata->root_cluster;
  949. itr->dent = NULL;
  950. itr->remaining = 0;
  951. itr->last_cluster = 0;
  952. if (next[0] == 0) {
  953. if (type & TYPE_DIR)
  954. return 0;
  955. else
  956. return -ENOENT;
  957. }
  958. return fat_itr_resolve(itr, next, type);
  959. }
  960. }
  961. while (fat_itr_next(itr)) {
  962. int match = 0;
  963. unsigned n = max(strlen(itr->name), (size_t)(next - path));
  964. /* check both long and short name: */
  965. if (!strncasecmp(path, itr->name, n))
  966. match = 1;
  967. else if (itr->name != itr->s_name &&
  968. !strncasecmp(path, itr->s_name, n))
  969. match = 1;
  970. if (!match)
  971. continue;
  972. if (fat_itr_isdir(itr)) {
  973. /* recurse into directory: */
  974. fat_itr_child(itr, itr);
  975. return fat_itr_resolve(itr, next, type);
  976. } else if (next[0]) {
  977. /*
  978. * If next is not empty then we have a case
  979. * like: /path/to/realfile/nonsense
  980. */
  981. debug("bad trailing path: %s\n", next);
  982. return -ENOENT;
  983. } else if (!(type & TYPE_FILE)) {
  984. return -ENOTDIR;
  985. } else {
  986. return 0;
  987. }
  988. }
  989. return -ENOENT;
  990. }
  991. int file_fat_detectfs(void)
  992. {
  993. boot_sector bs;
  994. volume_info volinfo;
  995. int fatsize;
  996. char vol_label[12];
  997. if (cur_dev == NULL) {
  998. printf("No current device\n");
  999. return 1;
  1000. }
  1001. if (blk_enabled()) {
  1002. printf("Interface: %s\n", blk_get_uclass_name(cur_dev->uclass_id));
  1003. printf(" Device %d: ", cur_dev->devnum);
  1004. dev_print(cur_dev);
  1005. }
  1006. if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
  1007. printf("\nNo valid FAT fs found\n");
  1008. return 1;
  1009. }
  1010. memcpy(vol_label, volinfo.volume_label, 11);
  1011. vol_label[11] = '\0';
  1012. volinfo.fs_type[5] = '\0';
  1013. printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
  1014. return 0;
  1015. }
  1016. int fat_exists(const char *filename)
  1017. {
  1018. fsdata fsdata;
  1019. fat_itr *itr;
  1020. int ret;
  1021. itr = malloc_cache_aligned(sizeof(fat_itr));
  1022. if (!itr)
  1023. return 0;
  1024. ret = fat_itr_root(itr, &fsdata);
  1025. if (ret)
  1026. goto out;
  1027. ret = fat_itr_resolve(itr, filename, TYPE_ANY);
  1028. free(fsdata.fatbuf);
  1029. out:
  1030. free(itr);
  1031. return ret == 0;
  1032. }
  1033. /**
  1034. * fat2rtc() - convert FAT time stamp to RTC file stamp
  1035. *
  1036. * @date: FAT date
  1037. * @time: FAT time
  1038. * @tm: RTC time stamp
  1039. */
  1040. static void __maybe_unused fat2rtc(u16 date, u16 time, struct rtc_time *tm)
  1041. {
  1042. tm->tm_mday = date & 0x1f;
  1043. tm->tm_mon = (date & 0x1e0) >> 4;
  1044. tm->tm_year = (date >> 9) + 1980;
  1045. tm->tm_sec = (time & 0x1f) << 1;
  1046. tm->tm_min = (time & 0x7e0) >> 5;
  1047. tm->tm_hour = time >> 11;
  1048. rtc_calc_weekday(tm);
  1049. tm->tm_yday = 0;
  1050. tm->tm_isdst = 0;
  1051. }
  1052. int fat_size(const char *filename, loff_t *size)
  1053. {
  1054. fsdata fsdata;
  1055. fat_itr *itr;
  1056. int ret;
  1057. itr = malloc_cache_aligned(sizeof(fat_itr));
  1058. if (!itr)
  1059. return -ENOMEM;
  1060. ret = fat_itr_root(itr, &fsdata);
  1061. if (ret)
  1062. goto out_free_itr;
  1063. ret = fat_itr_resolve(itr, filename, TYPE_FILE);
  1064. if (ret) {
  1065. /*
  1066. * Directories don't have size, but fs_size() is not
  1067. * expected to fail if passed a directory path:
  1068. */
  1069. free(fsdata.fatbuf);
  1070. ret = fat_itr_root(itr, &fsdata);
  1071. if (ret)
  1072. goto out_free_itr;
  1073. ret = fat_itr_resolve(itr, filename, TYPE_DIR);
  1074. if (!ret)
  1075. *size = 0;
  1076. goto out_free_both;
  1077. }
  1078. *size = FAT2CPU32(itr->dent->size);
  1079. out_free_both:
  1080. free(fsdata.fatbuf);
  1081. out_free_itr:
  1082. free(itr);
  1083. return ret;
  1084. }
  1085. int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
  1086. loff_t *actread)
  1087. {
  1088. fsdata fsdata;
  1089. fat_itr *itr;
  1090. int ret;
  1091. itr = malloc_cache_aligned(sizeof(fat_itr));
  1092. if (!itr)
  1093. return -ENOMEM;
  1094. ret = fat_itr_root(itr, &fsdata);
  1095. if (ret)
  1096. goto out_free_itr;
  1097. ret = fat_itr_resolve(itr, filename, TYPE_FILE);
  1098. if (ret)
  1099. goto out_free_both;
  1100. debug("reading %s at pos %llu\n", filename, offset);
  1101. /* For saving default max clustersize memory allocated to malloc pool */
  1102. dir_entry *dentptr = itr->dent;
  1103. ret = get_contents(&fsdata, dentptr, offset, buf, len, actread);
  1104. out_free_both:
  1105. free(fsdata.fatbuf);
  1106. out_free_itr:
  1107. free(itr);
  1108. return ret;
  1109. }
  1110. int file_fat_read(const char *filename, void *buffer, int maxsize)
  1111. {
  1112. loff_t actread;
  1113. int ret;
  1114. ret = fat_read_file(filename, buffer, 0, maxsize, &actread);
  1115. if (ret)
  1116. return ret;
  1117. else
  1118. return actread;
  1119. }
  1120. typedef struct {
  1121. struct fs_dir_stream parent;
  1122. struct fs_dirent dirent;
  1123. fsdata fsdata;
  1124. fat_itr itr;
  1125. } fat_dir;
  1126. int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
  1127. {
  1128. fat_dir *dir;
  1129. int ret;
  1130. dir = malloc_cache_aligned(sizeof(*dir));
  1131. if (!dir)
  1132. return -ENOMEM;
  1133. memset(dir, 0, sizeof(*dir));
  1134. ret = fat_itr_root(&dir->itr, &dir->fsdata);
  1135. if (ret)
  1136. goto fail_free_dir;
  1137. ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
  1138. if (ret)
  1139. goto fail_free_both;
  1140. *dirsp = (struct fs_dir_stream *)dir;
  1141. return 0;
  1142. fail_free_both:
  1143. free(dir->fsdata.fatbuf);
  1144. fail_free_dir:
  1145. free(dir);
  1146. return ret;
  1147. }
  1148. int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
  1149. {
  1150. fat_dir *dir = (fat_dir *)dirs;
  1151. struct fs_dirent *dent = &dir->dirent;
  1152. if (!fat_itr_next(&dir->itr))
  1153. return -ENOENT;
  1154. memset(dent, 0, sizeof(*dent));
  1155. strcpy(dent->name, dir->itr.name);
  1156. if (CONFIG_IS_ENABLED(EFI_LOADER)) {
  1157. dent->attr = dir->itr.dent->attr;
  1158. fat2rtc(le16_to_cpu(dir->itr.dent->cdate),
  1159. le16_to_cpu(dir->itr.dent->ctime), &dent->create_time);
  1160. fat2rtc(le16_to_cpu(dir->itr.dent->date),
  1161. le16_to_cpu(dir->itr.dent->time), &dent->change_time);
  1162. fat2rtc(le16_to_cpu(dir->itr.dent->adate),
  1163. 0, &dent->access_time);
  1164. }
  1165. if (fat_itr_isdir(&dir->itr)) {
  1166. dent->type = FS_DT_DIR;
  1167. } else {
  1168. dent->type = FS_DT_REG;
  1169. dent->size = FAT2CPU32(dir->itr.dent->size);
  1170. }
  1171. *dentp = dent;
  1172. return 0;
  1173. }
  1174. void fat_closedir(struct fs_dir_stream *dirs)
  1175. {
  1176. fat_dir *dir = (fat_dir *)dirs;
  1177. free(dir->fsdata.fatbuf);
  1178. free(dir);
  1179. }
  1180. void fat_close(void)
  1181. {
  1182. }
  1183. int fat_uuid(char *uuid_str)
  1184. {
  1185. boot_sector bs;
  1186. volume_info volinfo;
  1187. int fatsize;
  1188. int ret;
  1189. u8 *id;
  1190. ret = read_bootsectandvi(&bs, &volinfo, &fatsize);
  1191. if (ret)
  1192. return ret;
  1193. id = volinfo.volume_id;
  1194. sprintf(uuid_str, "%02X%02X-%02X%02X", id[3], id[2], id[1], id[0]);
  1195. return 0;
  1196. }