fat.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  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->nameext.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->nameext.ext[0] && dirent->nameext.ext[0] != ' ') {
  112. *ptr++ = '.';
  113. memcpy(ptr, dirent->nameext.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 startsect;
  214. int ret;
  215. if (clustnum > 0) {
  216. startsect = clust_to_sect(mydata, clustnum);
  217. } else {
  218. startsect = mydata->rootdir_sect;
  219. }
  220. debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
  221. if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
  222. ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
  223. debug("FAT: Misaligned buffer address (%p)\n", buffer);
  224. while (size >= mydata->sect_size) {
  225. ret = disk_read(startsect++, 1, tmpbuf);
  226. if (ret != 1) {
  227. debug("Error reading data (got %d)\n", ret);
  228. return -1;
  229. }
  230. memcpy(buffer, tmpbuf, mydata->sect_size);
  231. buffer += mydata->sect_size;
  232. size -= mydata->sect_size;
  233. }
  234. } else {
  235. __u32 idx;
  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(struct nameext *nameext)
  409. {
  410. int i;
  411. u8 *pos = (void *)nameext;
  412. __u8 ret = 0;
  413. for (i = 0; i < 11; i++)
  414. ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + pos[i];
  415. return ret;
  416. }
  417. /*
  418. * Read boot sector and volume info from a FAT filesystem
  419. */
  420. static int
  421. read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
  422. {
  423. __u8 *block;
  424. volume_info *vistart;
  425. int ret = 0;
  426. if (cur_dev == NULL) {
  427. debug("Error: no device selected\n");
  428. return -1;
  429. }
  430. block = malloc_cache_aligned(cur_dev->blksz);
  431. if (block == NULL) {
  432. debug("Error: allocating block\n");
  433. return -1;
  434. }
  435. if (disk_read(0, 1, block) < 0) {
  436. debug("Error: reading block\n");
  437. goto fail;
  438. }
  439. memcpy(bs, block, sizeof(boot_sector));
  440. bs->reserved = FAT2CPU16(bs->reserved);
  441. bs->fat_length = FAT2CPU16(bs->fat_length);
  442. bs->secs_track = FAT2CPU16(bs->secs_track);
  443. bs->heads = FAT2CPU16(bs->heads);
  444. bs->total_sect = FAT2CPU32(bs->total_sect);
  445. /* FAT32 entries */
  446. if (bs->fat_length == 0) {
  447. /* Assume FAT32 */
  448. bs->fat32_length = FAT2CPU32(bs->fat32_length);
  449. bs->flags = FAT2CPU16(bs->flags);
  450. bs->root_cluster = FAT2CPU32(bs->root_cluster);
  451. bs->info_sector = FAT2CPU16(bs->info_sector);
  452. bs->backup_boot = FAT2CPU16(bs->backup_boot);
  453. vistart = (volume_info *)(block + sizeof(boot_sector));
  454. *fatsize = 32;
  455. } else {
  456. vistart = (volume_info *)&(bs->fat32_length);
  457. *fatsize = 0;
  458. }
  459. memcpy(volinfo, vistart, sizeof(volume_info));
  460. if (*fatsize == 32) {
  461. if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
  462. goto exit;
  463. } else {
  464. if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  465. *fatsize = 12;
  466. goto exit;
  467. }
  468. if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
  469. *fatsize = 16;
  470. goto exit;
  471. }
  472. }
  473. debug("Error: broken fs_type sign\n");
  474. fail:
  475. ret = -1;
  476. exit:
  477. free(block);
  478. return ret;
  479. }
  480. static int get_fs_info(fsdata *mydata)
  481. {
  482. boot_sector bs;
  483. volume_info volinfo;
  484. int ret;
  485. ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
  486. if (ret) {
  487. debug("Error: reading boot sector\n");
  488. return ret;
  489. }
  490. if (mydata->fatsize == 32) {
  491. mydata->fatlength = bs.fat32_length;
  492. mydata->total_sect = bs.total_sect;
  493. } else {
  494. mydata->fatlength = bs.fat_length;
  495. mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
  496. if (!mydata->total_sect)
  497. mydata->total_sect = bs.total_sect;
  498. }
  499. if (!mydata->total_sect) /* unlikely */
  500. mydata->total_sect = (u32)cur_part_info.size;
  501. mydata->fats = bs.fats;
  502. mydata->fat_sect = bs.reserved;
  503. mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
  504. mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
  505. mydata->clust_size = bs.cluster_size;
  506. if (mydata->sect_size != cur_part_info.blksz) {
  507. printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
  508. mydata->sect_size, cur_part_info.blksz);
  509. return -1;
  510. }
  511. if (mydata->clust_size == 0) {
  512. printf("Error: FAT cluster size not set\n");
  513. return -1;
  514. }
  515. if ((unsigned int)mydata->clust_size * mydata->sect_size >
  516. MAX_CLUSTSIZE) {
  517. printf("Error: FAT cluster size too big (cs=%u, max=%u)\n",
  518. (unsigned int)mydata->clust_size * mydata->sect_size,
  519. MAX_CLUSTSIZE);
  520. return -1;
  521. }
  522. if (mydata->fatsize == 32) {
  523. mydata->data_begin = mydata->rootdir_sect -
  524. (mydata->clust_size * 2);
  525. mydata->root_cluster = bs.root_cluster;
  526. } else {
  527. mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
  528. bs.dir_entries[0]) *
  529. sizeof(dir_entry)) /
  530. mydata->sect_size;
  531. mydata->data_begin = mydata->rootdir_sect +
  532. mydata->rootdir_size -
  533. (mydata->clust_size * 2);
  534. /*
  535. * The root directory is not cluster-aligned and may be on a
  536. * "negative" cluster, this will be handled specially in
  537. * fat_next_cluster().
  538. */
  539. mydata->root_cluster = 0;
  540. }
  541. mydata->fatbufnum = -1;
  542. mydata->fat_dirty = 0;
  543. mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
  544. if (mydata->fatbuf == NULL) {
  545. debug("Error: allocating memory\n");
  546. return -1;
  547. }
  548. debug("FAT%d, fat_sect: %d, fatlength: %d\n",
  549. mydata->fatsize, mydata->fat_sect, mydata->fatlength);
  550. debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
  551. "Data begins at: %d\n",
  552. mydata->root_cluster,
  553. mydata->rootdir_sect,
  554. mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
  555. debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
  556. mydata->clust_size);
  557. return 0;
  558. }
  559. /**
  560. * struct fat_itr - directory iterator, to simplify filesystem traversal
  561. *
  562. * Implements an iterator pattern to traverse directory tables,
  563. * transparently handling directory tables split across multiple
  564. * clusters, and the difference between FAT12/FAT16 root directory
  565. * (contiguous) and subdirectories + FAT32 root (chained).
  566. *
  567. * Rough usage
  568. *
  569. * .. code-block:: c
  570. *
  571. * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
  572. * // to traverse down to a subdirectory pointed to by
  573. * // current iterator position:
  574. * fat_itr_child(&itr, &itr);
  575. * }
  576. *
  577. * For a more complete example, see fat_itr_resolve().
  578. */
  579. struct fat_itr {
  580. /**
  581. * @fsdata: filesystem parameters
  582. */
  583. fsdata *fsdata;
  584. /**
  585. * @start_clust: first cluster
  586. */
  587. unsigned int start_clust;
  588. /**
  589. * @clust: current cluster
  590. */
  591. unsigned int clust;
  592. /**
  593. * @next_clust: next cluster if remaining == 0
  594. */
  595. unsigned int next_clust;
  596. /**
  597. * @last_cluster: set if last cluster of directory reached
  598. */
  599. int last_cluster;
  600. /**
  601. * @is_root: is iterator at root directory
  602. */
  603. int is_root;
  604. /**
  605. * @remaining: remaining directory entries in current cluster
  606. */
  607. int remaining;
  608. /**
  609. * @dent: current directory entry
  610. */
  611. dir_entry *dent;
  612. /**
  613. * @dent_rem: remaining entries after long name start
  614. */
  615. int dent_rem;
  616. /**
  617. * @dent_clust: cluster of long name start
  618. */
  619. unsigned int dent_clust;
  620. /**
  621. * @dent_start: first directory entry for long name
  622. */
  623. dir_entry *dent_start;
  624. /**
  625. * @l_name: long name of current directory entry
  626. */
  627. char l_name[VFAT_MAXLEN_BYTES];
  628. /**
  629. * @s_name: short 8.3 name of current directory entry
  630. */
  631. char s_name[14];
  632. /**
  633. * @name: l_name if there is one, else s_name
  634. */
  635. char *name;
  636. /**
  637. * @block: buffer for current cluster
  638. */
  639. u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
  640. };
  641. static int fat_itr_isdir(fat_itr *itr);
  642. /**
  643. * fat_itr_root() - initialize an iterator to start at the root
  644. * directory
  645. *
  646. * @itr: iterator to initialize
  647. * @fsdata: filesystem data for the partition
  648. * @return 0 on success, else -errno
  649. */
  650. static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
  651. {
  652. if (get_fs_info(fsdata))
  653. return -ENXIO;
  654. itr->fsdata = fsdata;
  655. itr->start_clust = fsdata->root_cluster;
  656. itr->clust = fsdata->root_cluster;
  657. itr->next_clust = fsdata->root_cluster;
  658. itr->dent = NULL;
  659. itr->remaining = 0;
  660. itr->last_cluster = 0;
  661. itr->is_root = 1;
  662. return 0;
  663. }
  664. /**
  665. * fat_itr_child() - initialize an iterator to descend into a sub-
  666. * directory
  667. *
  668. * Initializes 'itr' to iterate the contents of the directory at
  669. * the current cursor position of 'parent'. It is an error to
  670. * call this if the current cursor of 'parent' is pointing at a
  671. * regular file.
  672. *
  673. * Note that 'itr' and 'parent' can be the same pointer if you do
  674. * not need to preserve 'parent' after this call, which is useful
  675. * for traversing directory structure to resolve a file/directory.
  676. *
  677. * @itr: iterator to initialize
  678. * @parent: the iterator pointing at a directory entry in the
  679. * parent directory of the directory to iterate
  680. */
  681. static void fat_itr_child(fat_itr *itr, fat_itr *parent)
  682. {
  683. fsdata *mydata = parent->fsdata; /* for silly macros */
  684. unsigned clustnum = START(parent->dent);
  685. assert(fat_itr_isdir(parent));
  686. itr->fsdata = parent->fsdata;
  687. itr->start_clust = clustnum;
  688. if (clustnum > 0) {
  689. itr->clust = clustnum;
  690. itr->next_clust = clustnum;
  691. itr->is_root = 0;
  692. } else {
  693. itr->clust = parent->fsdata->root_cluster;
  694. itr->next_clust = parent->fsdata->root_cluster;
  695. itr->start_clust = parent->fsdata->root_cluster;
  696. itr->is_root = 1;
  697. }
  698. itr->dent = NULL;
  699. itr->remaining = 0;
  700. itr->last_cluster = 0;
  701. }
  702. /**
  703. * fat_next_cluster() - load next FAT cluster
  704. *
  705. * The function is used when iterating through directories. It loads the
  706. * next cluster with directory entries
  707. *
  708. * @itr: directory iterator
  709. * @nbytes: number of bytes read, 0 on error
  710. * Return: first directory entry, NULL on error
  711. */
  712. void *fat_next_cluster(fat_itr *itr, unsigned int *nbytes)
  713. {
  714. int ret;
  715. u32 sect;
  716. u32 read_size;
  717. /* have we reached the end? */
  718. if (itr->last_cluster)
  719. return NULL;
  720. if (itr->is_root && itr->fsdata->fatsize != 32) {
  721. /*
  722. * The root directory is located before the data area and
  723. * cannot be indexed using the regular unsigned cluster
  724. * numbers (it may start at a "negative" cluster or not at a
  725. * cluster boundary at all), so consider itr->next_clust to be
  726. * a offset in cluster-sized units from the start of rootdir.
  727. */
  728. unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size;
  729. unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset;
  730. sect = itr->fsdata->rootdir_sect + sect_offset;
  731. /* do not read past the end of rootdir */
  732. read_size = min_t(u32, itr->fsdata->clust_size,
  733. remaining_sects);
  734. } else {
  735. sect = clust_to_sect(itr->fsdata, itr->next_clust);
  736. read_size = itr->fsdata->clust_size;
  737. }
  738. log_debug("FAT read(sect=%d), clust_size=%d, read_size=%u\n",
  739. sect, itr->fsdata->clust_size, read_size);
  740. /*
  741. * NOTE: do_fat_read_at() had complicated logic to deal w/
  742. * vfat names that span multiple clusters in the fat16 case,
  743. * which get_dentfromdir() probably also needed (and was
  744. * missing). And not entirely sure what fat32 didn't have
  745. * the same issue.. We solve that by only caring about one
  746. * dent at a time and iteratively constructing the vfat long
  747. * name.
  748. */
  749. ret = disk_read(sect, read_size, itr->block);
  750. if (ret < 0) {
  751. debug("Error: reading block\n");
  752. return NULL;
  753. }
  754. *nbytes = read_size * itr->fsdata->sect_size;
  755. itr->clust = itr->next_clust;
  756. if (itr->is_root && itr->fsdata->fatsize != 32) {
  757. itr->next_clust++;
  758. if (itr->next_clust * itr->fsdata->clust_size >=
  759. itr->fsdata->rootdir_size) {
  760. debug("nextclust: 0x%x\n", itr->next_clust);
  761. itr->last_cluster = 1;
  762. }
  763. } else {
  764. itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
  765. if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
  766. debug("nextclust: 0x%x\n", itr->next_clust);
  767. itr->last_cluster = 1;
  768. }
  769. }
  770. return itr->block;
  771. }
  772. static dir_entry *next_dent(fat_itr *itr)
  773. {
  774. if (itr->remaining == 0) {
  775. unsigned nbytes;
  776. struct dir_entry *dent = fat_next_cluster(itr, &nbytes);
  777. /* have we reached the last cluster? */
  778. if (!dent) {
  779. /* a sign for no more entries left */
  780. itr->dent = NULL;
  781. return NULL;
  782. }
  783. itr->remaining = nbytes / sizeof(dir_entry) - 1;
  784. itr->dent = dent;
  785. } else {
  786. itr->remaining--;
  787. itr->dent++;
  788. }
  789. /* have we reached the last valid entry? */
  790. if (itr->dent->nameext.name[0] == 0)
  791. return NULL;
  792. return itr->dent;
  793. }
  794. static dir_entry *extract_vfat_name(fat_itr *itr)
  795. {
  796. struct dir_entry *dent = itr->dent;
  797. int seqn = itr->dent->nameext.name[0] & ~LAST_LONG_ENTRY_MASK;
  798. u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
  799. int n = 0;
  800. while (seqn--) {
  801. char buf[13];
  802. int idx = 0;
  803. slot2str((dir_slot *)dent, buf, &idx);
  804. if (n + idx >= sizeof(itr->l_name))
  805. return NULL;
  806. /* shift accumulated long-name up and copy new part in: */
  807. memmove(itr->l_name + idx, itr->l_name, n);
  808. memcpy(itr->l_name, buf, idx);
  809. n += idx;
  810. dent = next_dent(itr);
  811. if (!dent)
  812. return NULL;
  813. }
  814. /*
  815. * We are now at the short file name entry.
  816. * If it is marked as deleted, just skip it.
  817. */
  818. if (dent->nameext.name[0] == DELETED_FLAG ||
  819. dent->nameext.name[0] == aRING)
  820. return NULL;
  821. itr->l_name[n] = '\0';
  822. chksum = mkcksum(&dent->nameext);
  823. /* checksum mismatch could mean deleted file, etc.. skip it: */
  824. if (chksum != alias_checksum) {
  825. debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
  826. chksum, alias_checksum, itr->l_name, dent->nameext.name,
  827. dent->nameext.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->nameext.name[0] == DELETED_FLAG)
  864. continue;
  865. if (dent->attr & ATTR_VOLUME) {
  866. if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
  867. (dent->nameext.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 (IS_ENABLED(CONFIG_HAVE_BLOCK_DEVICE)) {
  1004. printf("Interface: %s\n", blk_get_if_type_name(cur_dev->if_type));
  1005. printf(" Device %d: ", cur_dev->devnum);
  1006. dev_print(cur_dev);
  1007. }
  1008. if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
  1009. printf("\nNo valid FAT fs found\n");
  1010. return 1;
  1011. }
  1012. memcpy(vol_label, volinfo.volume_label, 11);
  1013. vol_label[11] = '\0';
  1014. volinfo.fs_type[5] = '\0';
  1015. printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
  1016. return 0;
  1017. }
  1018. int fat_exists(const char *filename)
  1019. {
  1020. fsdata fsdata;
  1021. fat_itr *itr;
  1022. int ret;
  1023. itr = malloc_cache_aligned(sizeof(fat_itr));
  1024. if (!itr)
  1025. return 0;
  1026. ret = fat_itr_root(itr, &fsdata);
  1027. if (ret)
  1028. goto out;
  1029. ret = fat_itr_resolve(itr, filename, TYPE_ANY);
  1030. free(fsdata.fatbuf);
  1031. out:
  1032. free(itr);
  1033. return ret == 0;
  1034. }
  1035. /**
  1036. * fat2rtc() - convert FAT time stamp to RTC file stamp
  1037. *
  1038. * @date: FAT date
  1039. * @time: FAT time
  1040. * @tm: RTC time stamp
  1041. */
  1042. static void __maybe_unused fat2rtc(u16 date, u16 time, struct rtc_time *tm)
  1043. {
  1044. tm->tm_mday = date & 0x1f;
  1045. tm->tm_mon = (date & 0x1e0) >> 4;
  1046. tm->tm_year = (date >> 9) + 1980;
  1047. tm->tm_sec = (time & 0x1f) << 1;
  1048. tm->tm_min = (time & 0x7e0) >> 5;
  1049. tm->tm_hour = time >> 11;
  1050. rtc_calc_weekday(tm);
  1051. tm->tm_yday = 0;
  1052. tm->tm_isdst = 0;
  1053. }
  1054. int fat_size(const char *filename, loff_t *size)
  1055. {
  1056. fsdata fsdata;
  1057. fat_itr *itr;
  1058. int ret;
  1059. itr = malloc_cache_aligned(sizeof(fat_itr));
  1060. if (!itr)
  1061. return -ENOMEM;
  1062. ret = fat_itr_root(itr, &fsdata);
  1063. if (ret)
  1064. goto out_free_itr;
  1065. ret = fat_itr_resolve(itr, filename, TYPE_FILE);
  1066. if (ret) {
  1067. /*
  1068. * Directories don't have size, but fs_size() is not
  1069. * expected to fail if passed a directory path:
  1070. */
  1071. free(fsdata.fatbuf);
  1072. ret = fat_itr_root(itr, &fsdata);
  1073. if (ret)
  1074. goto out_free_itr;
  1075. ret = fat_itr_resolve(itr, filename, TYPE_DIR);
  1076. if (!ret)
  1077. *size = 0;
  1078. goto out_free_both;
  1079. }
  1080. *size = FAT2CPU32(itr->dent->size);
  1081. out_free_both:
  1082. free(fsdata.fatbuf);
  1083. out_free_itr:
  1084. free(itr);
  1085. return ret;
  1086. }
  1087. int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
  1088. loff_t maxsize, loff_t *actread)
  1089. {
  1090. fsdata fsdata;
  1091. fat_itr *itr;
  1092. int ret;
  1093. itr = malloc_cache_aligned(sizeof(fat_itr));
  1094. if (!itr)
  1095. return -ENOMEM;
  1096. ret = fat_itr_root(itr, &fsdata);
  1097. if (ret)
  1098. goto out_free_itr;
  1099. ret = fat_itr_resolve(itr, filename, TYPE_FILE);
  1100. if (ret)
  1101. goto out_free_both;
  1102. debug("reading %s at pos %llu\n", filename, pos);
  1103. /* For saving default max clustersize memory allocated to malloc pool */
  1104. dir_entry *dentptr = itr->dent;
  1105. ret = get_contents(&fsdata, dentptr, pos, buffer, maxsize, actread);
  1106. out_free_both:
  1107. free(fsdata.fatbuf);
  1108. out_free_itr:
  1109. free(itr);
  1110. return ret;
  1111. }
  1112. int file_fat_read(const char *filename, void *buffer, int maxsize)
  1113. {
  1114. loff_t actread;
  1115. int ret;
  1116. ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
  1117. if (ret)
  1118. return ret;
  1119. else
  1120. return actread;
  1121. }
  1122. int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
  1123. loff_t *actread)
  1124. {
  1125. int ret;
  1126. ret = file_fat_read_at(filename, offset, buf, len, actread);
  1127. if (ret)
  1128. printf("** Unable to read file %s **\n", filename);
  1129. return ret;
  1130. }
  1131. typedef struct {
  1132. struct fs_dir_stream parent;
  1133. struct fs_dirent dirent;
  1134. fsdata fsdata;
  1135. fat_itr itr;
  1136. } fat_dir;
  1137. int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
  1138. {
  1139. fat_dir *dir;
  1140. int ret;
  1141. dir = malloc_cache_aligned(sizeof(*dir));
  1142. if (!dir)
  1143. return -ENOMEM;
  1144. memset(dir, 0, sizeof(*dir));
  1145. ret = fat_itr_root(&dir->itr, &dir->fsdata);
  1146. if (ret)
  1147. goto fail_free_dir;
  1148. ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
  1149. if (ret)
  1150. goto fail_free_both;
  1151. *dirsp = (struct fs_dir_stream *)dir;
  1152. return 0;
  1153. fail_free_both:
  1154. free(dir->fsdata.fatbuf);
  1155. fail_free_dir:
  1156. free(dir);
  1157. return ret;
  1158. }
  1159. int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
  1160. {
  1161. fat_dir *dir = (fat_dir *)dirs;
  1162. struct fs_dirent *dent = &dir->dirent;
  1163. if (!fat_itr_next(&dir->itr))
  1164. return -ENOENT;
  1165. memset(dent, 0, sizeof(*dent));
  1166. strcpy(dent->name, dir->itr.name);
  1167. if (CONFIG_IS_ENABLED(EFI_LOADER)) {
  1168. dent->attr = dir->itr.dent->attr;
  1169. fat2rtc(le16_to_cpu(dir->itr.dent->cdate),
  1170. le16_to_cpu(dir->itr.dent->ctime), &dent->create_time);
  1171. fat2rtc(le16_to_cpu(dir->itr.dent->date),
  1172. le16_to_cpu(dir->itr.dent->time), &dent->change_time);
  1173. fat2rtc(le16_to_cpu(dir->itr.dent->adate),
  1174. 0, &dent->access_time);
  1175. }
  1176. if (fat_itr_isdir(&dir->itr)) {
  1177. dent->type = FS_DT_DIR;
  1178. } else {
  1179. dent->type = FS_DT_REG;
  1180. dent->size = FAT2CPU32(dir->itr.dent->size);
  1181. }
  1182. *dentp = dent;
  1183. return 0;
  1184. }
  1185. void fat_closedir(struct fs_dir_stream *dirs)
  1186. {
  1187. fat_dir *dir = (fat_dir *)dirs;
  1188. free(dir->fsdata.fatbuf);
  1189. free(dir);
  1190. }
  1191. void fat_close(void)
  1192. {
  1193. }
  1194. int fat_uuid(char *uuid_str)
  1195. {
  1196. boot_sector bs;
  1197. volume_info volinfo;
  1198. int fatsize;
  1199. int ret;
  1200. u8 *id;
  1201. ret = read_bootsectandvi(&bs, &volinfo, &fatsize);
  1202. if (ret)
  1203. return ret;
  1204. id = volinfo.volume_id;
  1205. sprintf(uuid_str, "%02X%02X-%02X%02X", id[3], id[2], id[1], id[0]);
  1206. return 0;
  1207. }