fat.c 31 KB

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