1 // SPDX-License-Identifier: GPL-2.0+
5 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
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
11 #define LOG_CATEGORY LOGC_FS
20 #include <asm/byteorder.h>
21 #include <asm/unaligned.h>
25 #include <asm/cache.h>
26 #include <linux/compiler.h>
27 #include <linux/ctype.h>
29 /* maximum number of clusters for FAT12 */
30 #define MAX_FAT12 0xFF4
33 * Convert a string to lowercase. Converts at most 'len' characters,
34 * 'len' may be larger than the length of 'str' if 'str' is NULL
37 static void downcase(char *str
, size_t len
)
39 while (*str
!= '\0' && len
--) {
45 static struct blk_desc
*cur_dev
;
46 static struct disk_partition cur_part_info
;
48 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
49 #define DOS_FS_TYPE_OFFSET 0x36
50 #define DOS_FS32_TYPE_OFFSET 0x52
52 static int disk_read(__u32 block
, __u32 nr_blocks
, void *buf
)
59 ret
= blk_dread(cur_dev
, cur_part_info
.start
+ block
, nr_blocks
, buf
);
67 int fat_set_blk_dev(struct blk_desc
*dev_desc
, struct disk_partition
*info
)
69 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer
, dev_desc
->blksz
);
72 cur_part_info
= *info
;
74 /* Make sure it has a valid FAT header */
75 if (disk_read(0, 1, buffer
) != 1) {
80 /* Check if it's actually a DOS volume */
81 if (memcmp(buffer
+ DOS_BOOT_MAGIC_OFFSET
, "\x55\xAA", 2)) {
86 /* Check for FAT12/FAT16/FAT32 filesystem */
87 if (!memcmp(buffer
+ DOS_FS_TYPE_OFFSET
, "FAT", 3))
89 if (!memcmp(buffer
+ DOS_FS32_TYPE_OFFSET
, "FAT32", 5))
96 int fat_register_device(struct blk_desc
*dev_desc
, int part_no
)
98 struct disk_partition info
;
100 /* First close any currently found FAT filesystem */
103 /* Read the partition table, if present */
104 if (part_get_info(dev_desc
, part_no
, &info
)) {
106 log_err("Partition %d invalid on device %d\n", part_no
,
112 info
.size
= dev_desc
->lba
;
113 info
.blksz
= dev_desc
->blksz
;
117 disk_partition_clr_uuid(&info
);
120 return fat_set_blk_dev(dev_desc
, &info
);
124 * Extract zero terminated short name from a directory entry.
126 static void get_name(dir_entry
*dirent
, char *s_name
)
130 memcpy(s_name
, dirent
->nameext
.name
, 8);
133 while (*ptr
&& *ptr
!= ' ')
135 if (dirent
->lcase
& CASE_LOWER_BASE
)
136 downcase(s_name
, (unsigned)(ptr
- s_name
));
137 if (dirent
->nameext
.ext
[0] && dirent
->nameext
.ext
[0] != ' ') {
139 memcpy(ptr
, dirent
->nameext
.ext
, 3);
140 if (dirent
->lcase
& CASE_LOWER_EXT
)
143 while (*ptr
&& *ptr
!= ' ')
147 if (*s_name
== DELETED_FLAG
)
149 else if (*s_name
== aRING
)
150 *s_name
= DELETED_FLAG
;
153 static int flush_dirty_fat_buffer(fsdata
*mydata
);
155 #if !CONFIG_IS_ENABLED(FAT_WRITE)
156 /* Stub for read only operation */
157 int flush_dirty_fat_buffer(fsdata
*mydata
)
165 * Get the entry at index 'entry' in a FAT (12/16/32) table.
166 * On failure 0x00 is returned.
168 static __u32
get_fatent(fsdata
*mydata
, __u32 entry
)
174 if (CHECK_CLUST(entry
, mydata
->fatsize
)) {
175 log_err("Invalid FAT entry: %#08x\n", entry
);
179 switch (mydata
->fatsize
) {
181 bufnum
= entry
/ FAT32BUFSIZE
;
182 offset
= entry
- bufnum
* FAT32BUFSIZE
;
185 bufnum
= entry
/ FAT16BUFSIZE
;
186 offset
= entry
- bufnum
* FAT16BUFSIZE
;
189 bufnum
= entry
/ FAT12BUFSIZE
;
190 offset
= entry
- bufnum
* FAT12BUFSIZE
;
194 /* Unsupported FAT size */
198 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
199 mydata
->fatsize
, entry
, entry
, offset
, offset
);
201 /* Read a new block of FAT entries into the cache. */
202 if (bufnum
!= mydata
->fatbufnum
) {
203 __u32 getsize
= FATBUFBLOCKS
;
204 __u8
*bufptr
= mydata
->fatbuf
;
205 __u32 fatlength
= mydata
->fatlength
;
206 __u32 startblock
= bufnum
* FATBUFBLOCKS
;
208 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
209 if (startblock
+ getsize
> fatlength
)
210 getsize
= fatlength
- startblock
;
212 startblock
+= mydata
->fat_sect
; /* Offset from start of disk */
214 /* Write back the fatbuf to the disk */
215 if (flush_dirty_fat_buffer(mydata
) < 0)
218 if (disk_read(startblock
, getsize
, bufptr
) < 0) {
219 debug("Error reading FAT blocks\n");
222 mydata
->fatbufnum
= bufnum
;
225 /* Get the actual entry from the table */
226 switch (mydata
->fatsize
) {
228 ret
= FAT2CPU32(((__u32
*) mydata
->fatbuf
)[offset
]);
231 ret
= FAT2CPU16(((__u16
*) mydata
->fatbuf
)[offset
]);
234 off8
= (offset
* 3) / 2;
235 /* fatbut + off8 may be unaligned, read in byte granularity */
236 ret
= mydata
->fatbuf
[off8
] + (mydata
->fatbuf
[off8
+ 1] << 8);
242 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
243 mydata
->fatsize
, ret
, entry
, offset
);
249 * Read at most 'size' bytes from the specified cluster into 'buffer'.
250 * Return 0 on success, -1 otherwise.
253 get_cluster(fsdata
*mydata
, __u32 clustnum
, __u8
*buffer
, unsigned long size
)
259 startsect
= clust_to_sect(mydata
, clustnum
);
261 startsect
= mydata
->rootdir_sect
;
264 debug("gc - clustnum: %d, startsect: %d\n", clustnum
, startsect
);
266 if ((unsigned long)buffer
& (ARCH_DMA_MINALIGN
- 1)) {
267 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
269 debug("FAT: Misaligned buffer address (%p)\n", buffer
);
271 while (size
>= mydata
->sect_size
) {
272 ret
= disk_read(startsect
++, 1, tmpbuf
);
274 debug("Error reading data (got %d)\n", ret
);
278 memcpy(buffer
, tmpbuf
, mydata
->sect_size
);
279 buffer
+= mydata
->sect_size
;
280 size
-= mydata
->sect_size
;
282 } else if (size
>= mydata
->sect_size
) {
284 __u32 sect_count
= size
/ mydata
->sect_size
;
286 ret
= disk_read(startsect
, sect_count
, buffer
);
287 if (ret
!= sect_count
) {
288 debug("Error reading data (got %d)\n", ret
);
291 bytes_read
= sect_count
* mydata
->sect_size
;
292 startsect
+= sect_count
;
293 buffer
+= bytes_read
;
297 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
299 ret
= disk_read(startsect
, 1, tmpbuf
);
301 debug("Error reading data (got %d)\n", ret
);
305 memcpy(buffer
, tmpbuf
, size
);
312 * get_contents() - read from file
314 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
315 * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on
318 * @mydata: file system description
319 * @dentprt: directory entry pointer
320 * @pos: position from where to read
321 * @buffer: buffer into which to read
322 * @maxsize: maximum number of bytes to read
323 * @gotsize: number of bytes actually read
324 * Return: -1 on error, otherwise 0
326 static int get_contents(fsdata
*mydata
, dir_entry
*dentptr
, loff_t pos
,
327 __u8
*buffer
, loff_t maxsize
, loff_t
*gotsize
)
329 loff_t filesize
= FAT2CPU32(dentptr
->size
);
330 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
331 __u32 curclust
= START(dentptr
);
332 __u32 endclust
, newclust
;
336 debug("Filesize: %llu bytes\n", filesize
);
338 if (pos
>= filesize
) {
339 debug("Read position past EOF: %llu\n", pos
);
343 if (maxsize
> 0 && filesize
> pos
+ maxsize
)
344 filesize
= pos
+ maxsize
;
346 debug("%llu bytes\n", filesize
);
348 actsize
= bytesperclust
;
350 /* go to cluster at pos */
351 while (actsize
<= pos
) {
352 curclust
= get_fatent(mydata
, curclust
);
353 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
354 debug("curclust: 0x%x\n", curclust
);
355 printf("Invalid FAT entry\n");
358 actsize
+= bytesperclust
;
362 actsize
-= bytesperclust
;
366 /* align to beginning of next cluster if any */
370 actsize
= min(filesize
, (loff_t
)bytesperclust
);
371 tmp_buffer
= malloc_cache_aligned(actsize
);
373 debug("Error: allocating buffer\n");
377 if (get_cluster(mydata
, curclust
, tmp_buffer
, actsize
) != 0) {
378 printf("Error reading cluster\n");
384 memcpy(buffer
, tmp_buffer
+ pos
, actsize
);
391 curclust
= get_fatent(mydata
, curclust
);
392 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
393 debug("curclust: 0x%x\n", curclust
);
394 printf("Invalid FAT entry\n");
399 actsize
= bytesperclust
;
403 /* search for consecutive clusters */
404 while (actsize
< filesize
) {
405 newclust
= get_fatent(mydata
, endclust
);
406 if ((newclust
- 1) != endclust
)
408 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
409 debug("curclust: 0x%x\n", newclust
);
410 printf("Invalid FAT entry\n");
414 actsize
+= bytesperclust
;
417 /* get remaining bytes */
419 if (get_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
420 printf("Error reading cluster\n");
426 if (get_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
427 printf("Error reading cluster\n");
430 *gotsize
+= (int)actsize
;
434 curclust
= get_fatent(mydata
, endclust
);
435 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
436 debug("curclust: 0x%x\n", curclust
);
437 printf("Invalid FAT entry\n");
440 actsize
= bytesperclust
;
446 * Extract the file name information from 'slotptr' into 'l_name',
447 * starting at l_name[*idx].
448 * Return 1 if terminator (zero byte) is found, 0 otherwise.
450 static int slot2str(dir_slot
*slotptr
, char *l_name
, int *idx
)
454 for (j
= 0; j
<= 8; j
+= 2) {
455 l_name
[*idx
] = slotptr
->name0_4
[j
];
456 if (l_name
[*idx
] == 0x00)
460 for (j
= 0; j
<= 10; j
+= 2) {
461 l_name
[*idx
] = slotptr
->name5_10
[j
];
462 if (l_name
[*idx
] == 0x00)
466 for (j
= 0; j
<= 2; j
+= 2) {
467 l_name
[*idx
] = slotptr
->name11_12
[j
];
468 if (l_name
[*idx
] == 0x00)
476 /* Calculate short name checksum */
477 static __u8
mkcksum(struct nameext
*nameext
)
480 u8
*pos
= (void *)nameext
;
484 for (i
= 0; i
< 11; i
++)
485 ret
= (((ret
& 1) << 7) | ((ret
& 0xfe) >> 1)) + pos
[i
];
491 * Determine if the FAT type is FAT12 or FAT16
493 * Based on fat_fill_super() from the Linux kernel's fs/fat/inode.c
495 static int determine_legacy_fat_bits(const boot_sector
*bs
)
497 u16 fat_start
= bs
->reserved
;
498 u32 dir_start
= fat_start
+ bs
->fats
* bs
->fat_length
;
499 u32 rootdir_sectors
= get_unaligned_le16(bs
->dir_entries
) *
501 get_unaligned_le16(bs
->sector_size
);
502 u32 data_start
= dir_start
+ rootdir_sectors
;
503 u16 sectors
= get_unaligned_le16(bs
->sectors
);
504 u32 total_sectors
= sectors
? sectors
: bs
->total_sect
;
505 u32 total_clusters
= (total_sectors
- data_start
) /
508 return (total_clusters
> MAX_FAT12
) ? 16 : 12;
512 * Read boot sector and volume info from a FAT filesystem
515 read_bootsectandvi(boot_sector
*bs
, volume_info
*volinfo
, int *fatsize
)
518 volume_info
*vistart
;
521 if (cur_dev
== NULL
) {
522 debug("Error: no device selected\n");
526 block
= malloc_cache_aligned(cur_dev
->blksz
);
528 debug("Error: allocating block\n");
532 if (disk_read(0, 1, block
) < 0) {
533 debug("Error: reading block\n");
537 memcpy(bs
, block
, sizeof(boot_sector
));
538 bs
->reserved
= FAT2CPU16(bs
->reserved
);
539 bs
->fat_length
= FAT2CPU16(bs
->fat_length
);
540 bs
->secs_track
= FAT2CPU16(bs
->secs_track
);
541 bs
->heads
= FAT2CPU16(bs
->heads
);
542 bs
->total_sect
= FAT2CPU32(bs
->total_sect
);
545 if (!bs
->fat_length
&& bs
->fat32_length
) {
547 bs
->fat32_length
= FAT2CPU32(bs
->fat32_length
);
548 bs
->flags
= FAT2CPU16(bs
->flags
);
549 bs
->root_cluster
= FAT2CPU32(bs
->root_cluster
);
550 bs
->info_sector
= FAT2CPU16(bs
->info_sector
);
551 bs
->backup_boot
= FAT2CPU16(bs
->backup_boot
);
552 vistart
= (volume_info
*)(block
+ sizeof(boot_sector
));
555 vistart
= (volume_info
*)&(bs
->fat32_length
);
556 *fatsize
= determine_legacy_fat_bits(bs
);
558 memcpy(volinfo
, vistart
, sizeof(volume_info
));
567 static int get_fs_info(fsdata
*mydata
)
573 ret
= read_bootsectandvi(&bs
, &volinfo
, &mydata
->fatsize
);
575 debug("Error: reading boot sector\n");
579 if (mydata
->fatsize
== 32) {
580 mydata
->fatlength
= bs
.fat32_length
;
581 mydata
->total_sect
= bs
.total_sect
;
583 mydata
->fatlength
= bs
.fat_length
;
584 mydata
->total_sect
= get_unaligned_le16(bs
.sectors
);
585 if (!mydata
->total_sect
)
586 mydata
->total_sect
= bs
.total_sect
;
588 if (!mydata
->total_sect
) /* unlikely */
589 mydata
->total_sect
= (u32
)cur_part_info
.size
;
591 mydata
->fats
= bs
.fats
;
592 mydata
->fat_sect
= bs
.reserved
;
594 mydata
->rootdir_sect
= mydata
->fat_sect
+ mydata
->fatlength
* bs
.fats
;
596 mydata
->sect_size
= get_unaligned_le16(bs
.sector_size
);
597 mydata
->clust_size
= bs
.cluster_size
;
598 if (mydata
->sect_size
!= cur_part_info
.blksz
) {
599 log_err("FAT sector size mismatch (fs=%u, dev=%lu)\n",
600 mydata
->sect_size
, cur_part_info
.blksz
);
603 if (mydata
->clust_size
== 0) {
604 log_err("FAT cluster size not set\n");
607 if ((unsigned int)mydata
->clust_size
* mydata
->sect_size
>
609 log_err("FAT cluster size too big (cs=%u, max=%u)\n",
610 (uint
)mydata
->clust_size
* mydata
->sect_size
,
615 if (mydata
->fatsize
== 32) {
616 mydata
->data_begin
= mydata
->rootdir_sect
-
617 (mydata
->clust_size
* 2);
618 mydata
->root_cluster
= bs
.root_cluster
;
620 mydata
->rootdir_size
= (get_unaligned_le16(bs
.dir_entries
) *
623 mydata
->data_begin
= mydata
->rootdir_sect
+
624 mydata
->rootdir_size
-
625 (mydata
->clust_size
* 2);
628 * The root directory is not cluster-aligned and may be on a
629 * "negative" cluster, this will be handled specially in
630 * fat_next_cluster().
632 mydata
->root_cluster
= 0;
635 mydata
->fatbufnum
= -1;
636 mydata
->fat_dirty
= 0;
637 mydata
->fatbuf
= malloc_cache_aligned(FATBUFSIZE
);
638 if (mydata
->fatbuf
== NULL
) {
639 debug("Error: allocating memory\n");
643 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
644 mydata
->fatsize
, mydata
->fat_sect
, mydata
->fatlength
);
645 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
646 "Data begins at: %d\n",
647 mydata
->root_cluster
,
648 mydata
->rootdir_sect
,
649 mydata
->rootdir_sect
* mydata
->sect_size
, mydata
->data_begin
);
650 debug("Sector size: %d, cluster size: %d\n", mydata
->sect_size
,
657 * struct fat_itr - directory iterator, to simplify filesystem traversal
659 * Implements an iterator pattern to traverse directory tables,
660 * transparently handling directory tables split across multiple
661 * clusters, and the difference between FAT12/FAT16 root directory
662 * (contiguous) and subdirectories + FAT32 root (chained).
668 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
669 * // to traverse down to a subdirectory pointed to by
670 * // current iterator position:
671 * fat_itr_child(&itr, &itr);
674 * For a more complete example, see fat_itr_resolve().
678 * @fsdata: filesystem parameters
682 * @start_clust: first cluster
684 unsigned int start_clust
;
686 * @clust: current cluster
690 * @next_clust: next cluster if remaining == 0
692 unsigned int next_clust
;
694 * @last_cluster: set if last cluster of directory reached
698 * @is_root: is iterator at root directory
702 * @remaining: remaining directory entries in current cluster
706 * @dent: current directory entry
710 * @dent_rem: remaining entries after long name start
714 * @dent_clust: cluster of long name start
716 unsigned int dent_clust
;
718 * @dent_start: first directory entry for long name
720 dir_entry
*dent_start
;
722 * @l_name: long name of current directory entry
724 char l_name
[VFAT_MAXLEN_BYTES
];
726 * @s_name: short 8.3 name of current directory entry
730 * @name: l_name if there is one, else s_name
734 * @block: buffer for current cluster
736 u8 block
[MAX_CLUSTSIZE
] __aligned(ARCH_DMA_MINALIGN
);
739 static int fat_itr_isdir(fat_itr
*itr
);
742 * fat_itr_root() - initialize an iterator to start at the root
745 * @itr: iterator to initialize
746 * @fsdata: filesystem data for the partition
747 * Return: 0 on success, else -errno
749 static int fat_itr_root(fat_itr
*itr
, fsdata
*fsdata
)
751 if (get_fs_info(fsdata
))
754 itr
->fsdata
= fsdata
;
755 itr
->start_clust
= fsdata
->root_cluster
;
756 itr
->clust
= fsdata
->root_cluster
;
757 itr
->next_clust
= fsdata
->root_cluster
;
760 itr
->last_cluster
= 0;
767 * fat_itr_child() - initialize an iterator to descend into a sub-
770 * Initializes 'itr' to iterate the contents of the directory at
771 * the current cursor position of 'parent'. It is an error to
772 * call this if the current cursor of 'parent' is pointing at a
775 * Note that 'itr' and 'parent' can be the same pointer if you do
776 * not need to preserve 'parent' after this call, which is useful
777 * for traversing directory structure to resolve a file/directory.
779 * @itr: iterator to initialize
780 * @parent: the iterator pointing at a directory entry in the
781 * parent directory of the directory to iterate
783 static void fat_itr_child(fat_itr
*itr
, fat_itr
*parent
)
785 fsdata
*mydata
= parent
->fsdata
; /* for silly macros */
786 unsigned clustnum
= START(parent
->dent
);
788 assert(fat_itr_isdir(parent
));
790 itr
->fsdata
= parent
->fsdata
;
791 itr
->start_clust
= clustnum
;
793 itr
->clust
= clustnum
;
794 itr
->next_clust
= clustnum
;
797 itr
->clust
= parent
->fsdata
->root_cluster
;
798 itr
->next_clust
= parent
->fsdata
->root_cluster
;
799 itr
->start_clust
= parent
->fsdata
->root_cluster
;
804 itr
->last_cluster
= 0;
808 * fat_next_cluster() - load next FAT cluster
810 * The function is used when iterating through directories. It loads the
811 * next cluster with directory entries
813 * @itr: directory iterator
814 * @nbytes: number of bytes read, 0 on error
815 * Return: first directory entry, NULL on error
817 void *fat_next_cluster(fat_itr
*itr
, unsigned int *nbytes
)
823 /* have we reached the end? */
824 if (itr
->last_cluster
)
827 if (itr
->is_root
&& itr
->fsdata
->fatsize
!= 32) {
829 * The root directory is located before the data area and
830 * cannot be indexed using the regular unsigned cluster
831 * numbers (it may start at a "negative" cluster or not at a
832 * cluster boundary at all), so consider itr->next_clust to be
833 * a offset in cluster-sized units from the start of rootdir.
835 unsigned sect_offset
= itr
->next_clust
* itr
->fsdata
->clust_size
;
836 unsigned remaining_sects
= itr
->fsdata
->rootdir_size
- sect_offset
;
837 sect
= itr
->fsdata
->rootdir_sect
+ sect_offset
;
838 /* do not read past the end of rootdir */
839 read_size
= min_t(u32
, itr
->fsdata
->clust_size
,
842 sect
= clust_to_sect(itr
->fsdata
, itr
->next_clust
);
843 read_size
= itr
->fsdata
->clust_size
;
846 log_debug("FAT read(sect=%d), clust_size=%d, read_size=%u\n",
847 sect
, itr
->fsdata
->clust_size
, read_size
);
850 * NOTE: do_fat_read_at() had complicated logic to deal w/
851 * vfat names that span multiple clusters in the fat16 case,
852 * which get_dentfromdir() probably also needed (and was
853 * missing). And not entirely sure what fat32 didn't have
854 * the same issue.. We solve that by only caring about one
855 * dent at a time and iteratively constructing the vfat long
858 ret
= disk_read(sect
, read_size
, itr
->block
);
860 debug("Error: reading block\n");
864 *nbytes
= read_size
* itr
->fsdata
->sect_size
;
865 itr
->clust
= itr
->next_clust
;
866 if (itr
->is_root
&& itr
->fsdata
->fatsize
!= 32) {
868 if (itr
->next_clust
* itr
->fsdata
->clust_size
>=
869 itr
->fsdata
->rootdir_size
) {
870 debug("nextclust: 0x%x\n", itr
->next_clust
);
871 itr
->last_cluster
= 1;
874 itr
->next_clust
= get_fatent(itr
->fsdata
, itr
->next_clust
);
875 if (CHECK_CLUST(itr
->next_clust
, itr
->fsdata
->fatsize
)) {
876 debug("nextclust: 0x%x\n", itr
->next_clust
);
877 itr
->last_cluster
= 1;
884 static dir_entry
*next_dent(fat_itr
*itr
)
886 if (itr
->remaining
== 0) {
888 struct dir_entry
*dent
= fat_next_cluster(itr
, &nbytes
);
890 /* have we reached the last cluster? */
892 /* a sign for no more entries left */
897 itr
->remaining
= nbytes
/ sizeof(dir_entry
) - 1;
904 /* have we reached the last valid entry? */
905 if (itr
->dent
->nameext
.name
[0] == 0)
911 static dir_entry
*extract_vfat_name(fat_itr
*itr
)
913 struct dir_entry
*dent
= itr
->dent
;
914 int seqn
= itr
->dent
->nameext
.name
[0] & ~LAST_LONG_ENTRY_MASK
;
915 u8 chksum
, alias_checksum
= ((dir_slot
*)dent
)->alias_checksum
;
922 slot2str((dir_slot
*)dent
, buf
, &idx
);
924 if (n
+ idx
>= sizeof(itr
->l_name
))
927 /* shift accumulated long-name up and copy new part in: */
928 memmove(itr
->l_name
+ idx
, itr
->l_name
, n
);
929 memcpy(itr
->l_name
, buf
, idx
);
932 dent
= next_dent(itr
);
938 * We are now at the short file name entry.
939 * If it is marked as deleted, just skip it.
941 if (dent
->nameext
.name
[0] == DELETED_FLAG
||
942 dent
->nameext
.name
[0] == aRING
)
945 itr
->l_name
[n
] = '\0';
947 chksum
= mkcksum(&dent
->nameext
);
949 /* checksum mismatch could mean deleted file, etc.. skip it: */
950 if (chksum
!= alias_checksum
) {
951 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
952 chksum
, alias_checksum
, itr
->l_name
, dent
->nameext
.name
,
961 * fat_itr_next() - step to the next entry in a directory
963 * Must be called once on a new iterator before the cursor is valid.
965 * @itr: the iterator to iterate
966 * Return: boolean, 1 if success or 0 if no more entries in the
969 static int fat_itr_next(fat_itr
*itr
)
976 * One logical directory entry consist of following slots:
978 * dent[N - N]: LFN[N - 1] N|0x40 ATTR_VFAT
980 * dent[N - 2]: LFN[1] 2 ATTR_VFAT
981 * dent[N - 1]: LFN[0] 1 ATTR_VFAT
982 * dent[N]: SFN ATTR_ARCH
986 dent
= next_dent(itr
);
988 itr
->dent_start
= NULL
;
991 itr
->dent_rem
= itr
->remaining
;
992 itr
->dent_start
= itr
->dent
;
993 itr
->dent_clust
= itr
->clust
;
994 if (dent
->nameext
.name
[0] == DELETED_FLAG
)
997 if (dent
->attr
& ATTR_VOLUME
) {
998 if ((dent
->attr
& ATTR_VFAT
) == ATTR_VFAT
&&
999 (dent
->nameext
.name
[0] & LAST_LONG_ENTRY_MASK
)) {
1000 /* long file name */
1001 dent
= extract_vfat_name(itr
);
1003 * If succeeded, dent has a valid short file
1004 * name entry for the current entry.
1005 * If failed, itr points to a current bogus
1006 * entry. So after fetching a next one,
1007 * it may have a short file name entry
1008 * for this bogus entry so that we can still
1009 * check for a short name.
1013 itr
->name
= itr
->l_name
;
1016 /* Volume label or VFAT entry, skip */
1021 /* short file name */
1025 get_name(dent
, itr
->s_name
);
1027 itr
->name
= itr
->s_name
;
1033 * fat_itr_isdir() - is current cursor position pointing to a directory
1035 * @itr: the iterator
1036 * Return: true if cursor is at a directory
1038 static int fat_itr_isdir(fat_itr
*itr
)
1040 return !!(itr
->dent
->attr
& ATTR_DIR
);
1047 #define TYPE_FILE 0x1
1048 #define TYPE_DIR 0x2
1049 #define TYPE_ANY (TYPE_FILE | TYPE_DIR)
1052 * fat_itr_resolve() - traverse directory structure to resolve the
1055 * Traverse directory structure to the requested path. If the specified
1056 * path is to a directory, this will descend into the directory and
1057 * leave it iterator at the start of the directory. If the path is to a
1058 * file, it will leave the iterator in the parent directory with current
1059 * cursor at file's entry in the directory.
1061 * @itr: iterator initialized to root
1062 * @path: the requested path
1063 * @type: bitmask of allowable file types
1064 * Return: 0 on success or -errno
1066 static int fat_itr_resolve(fat_itr
*itr
, const char *path
, unsigned type
)
1070 /* chomp any extra leading slashes: */
1071 while (path
[0] && ISDIRDELIM(path
[0]))
1074 /* are we at the end? */
1075 if (strlen(path
) == 0) {
1076 if (!(type
& TYPE_DIR
))
1081 /* find length of next path entry: */
1083 while (next
[0] && !ISDIRDELIM(next
[0]))
1087 /* root dir doesn't have "." nor ".." */
1088 if ((((next
- path
) == 1) && !strncmp(path
, ".", 1)) ||
1089 (((next
- path
) == 2) && !strncmp(path
, "..", 2))) {
1090 /* point back to itself */
1091 itr
->clust
= itr
->fsdata
->root_cluster
;
1092 itr
->next_clust
= itr
->fsdata
->root_cluster
;
1093 itr
->start_clust
= itr
->fsdata
->root_cluster
;
1096 itr
->last_cluster
= 0;
1099 if (type
& TYPE_DIR
)
1105 return fat_itr_resolve(itr
, next
, type
);
1109 while (fat_itr_next(itr
)) {
1111 unsigned n
= max(strlen(itr
->name
), (size_t)(next
- path
));
1113 /* check both long and short name: */
1114 if (!strncasecmp(path
, itr
->name
, n
))
1116 else if (itr
->name
!= itr
->s_name
&&
1117 !strncasecmp(path
, itr
->s_name
, n
))
1123 if (fat_itr_isdir(itr
)) {
1124 /* recurse into directory: */
1125 fat_itr_child(itr
, itr
);
1126 return fat_itr_resolve(itr
, next
, type
);
1127 } else if (next
[0]) {
1129 * If next is not empty then we have a case
1130 * like: /path/to/realfile/nonsense
1132 debug("bad trailing path: %s\n", next
);
1134 } else if (!(type
& TYPE_FILE
)) {
1144 int file_fat_detectfs(void)
1147 volume_info volinfo
;
1151 if (cur_dev
== NULL
) {
1152 printf("No current device\n");
1156 if (blk_enabled()) {
1157 printf("Interface: %s\n", blk_get_uclass_name(cur_dev
->uclass_id
));
1158 printf(" Device %d: ", cur_dev
->devnum
);
1162 if (read_bootsectandvi(&bs
, &volinfo
, &fatsize
)) {
1163 printf("\nNo valid FAT fs found\n");
1167 memcpy(vol_label
, volinfo
.volume_label
, 11);
1168 vol_label
[11] = '\0';
1170 printf("Filesystem: FAT%d \"%s\"\n", fatsize
, vol_label
);
1175 int fat_exists(const char *filename
)
1181 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1184 ret
= fat_itr_root(itr
, &fsdata
);
1188 ret
= fat_itr_resolve(itr
, filename
, TYPE_ANY
);
1189 free(fsdata
.fatbuf
);
1196 * fat2rtc() - convert FAT time stamp to RTC file stamp
1200 * @tm: RTC time stamp
1202 static void __maybe_unused
fat2rtc(u16 date
, u16 time
, struct rtc_time
*tm
)
1204 tm
->tm_mday
= date
& 0x1f;
1205 tm
->tm_mon
= (date
& 0x1e0) >> 4;
1206 tm
->tm_year
= (date
>> 9) + 1980;
1208 tm
->tm_sec
= (time
& 0x1f) << 1;
1209 tm
->tm_min
= (time
& 0x7e0) >> 5;
1210 tm
->tm_hour
= time
>> 11;
1212 rtc_calc_weekday(tm
);
1217 int fat_size(const char *filename
, loff_t
*size
)
1223 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1226 ret
= fat_itr_root(itr
, &fsdata
);
1230 ret
= fat_itr_resolve(itr
, filename
, TYPE_FILE
);
1233 * Directories don't have size, but fs_size() is not
1234 * expected to fail if passed a directory path:
1236 free(fsdata
.fatbuf
);
1237 ret
= fat_itr_root(itr
, &fsdata
);
1240 ret
= fat_itr_resolve(itr
, filename
, TYPE_DIR
);
1246 *size
= FAT2CPU32(itr
->dent
->size
);
1248 free(fsdata
.fatbuf
);
1254 int fat_read_file(const char *filename
, void *buf
, loff_t offset
, loff_t len
,
1261 itr
= malloc_cache_aligned(sizeof(fat_itr
));
1264 ret
= fat_itr_root(itr
, &fsdata
);
1268 ret
= fat_itr_resolve(itr
, filename
, TYPE_FILE
);
1272 debug("reading %s at pos %llu\n", filename
, offset
);
1274 /* For saving default max clustersize memory allocated to malloc pool */
1275 dir_entry
*dentptr
= itr
->dent
;
1277 ret
= get_contents(&fsdata
, dentptr
, offset
, buf
, len
, actread
);
1280 free(fsdata
.fatbuf
);
1286 int file_fat_read(const char *filename
, void *buffer
, int maxsize
)
1291 ret
= fat_read_file(filename
, buffer
, 0, maxsize
, &actread
);
1299 struct fs_dir_stream parent
;
1300 struct fs_dirent dirent
;
1305 int fat_opendir(const char *filename
, struct fs_dir_stream
**dirsp
)
1310 dir
= malloc_cache_aligned(sizeof(*dir
));
1313 memset(dir
, 0, sizeof(*dir
));
1315 ret
= fat_itr_root(&dir
->itr
, &dir
->fsdata
);
1319 ret
= fat_itr_resolve(&dir
->itr
, filename
, TYPE_DIR
);
1321 goto fail_free_both
;
1323 *dirsp
= (struct fs_dir_stream
*)dir
;
1327 free(dir
->fsdata
.fatbuf
);
1333 int fat_readdir(struct fs_dir_stream
*dirs
, struct fs_dirent
**dentp
)
1335 fat_dir
*dir
= (fat_dir
*)dirs
;
1336 struct fs_dirent
*dent
= &dir
->dirent
;
1338 if (!fat_itr_next(&dir
->itr
))
1341 memset(dent
, 0, sizeof(*dent
));
1342 strcpy(dent
->name
, dir
->itr
.name
);
1343 if (CONFIG_IS_ENABLED(EFI_LOADER
)) {
1344 dent
->attr
= dir
->itr
.dent
->attr
;
1345 fat2rtc(le16_to_cpu(dir
->itr
.dent
->cdate
),
1346 le16_to_cpu(dir
->itr
.dent
->ctime
), &dent
->create_time
);
1347 fat2rtc(le16_to_cpu(dir
->itr
.dent
->date
),
1348 le16_to_cpu(dir
->itr
.dent
->time
), &dent
->change_time
);
1349 fat2rtc(le16_to_cpu(dir
->itr
.dent
->adate
),
1350 0, &dent
->access_time
);
1352 if (fat_itr_isdir(&dir
->itr
)) {
1353 dent
->type
= FS_DT_DIR
;
1355 dent
->type
= FS_DT_REG
;
1356 dent
->size
= FAT2CPU32(dir
->itr
.dent
->size
);
1364 void fat_closedir(struct fs_dir_stream
*dirs
)
1366 fat_dir
*dir
= (fat_dir
*)dirs
;
1367 free(dir
->fsdata
.fatbuf
);
1371 void fat_close(void)
1375 int fat_uuid(char *uuid_str
)
1378 volume_info volinfo
;
1383 ret
= read_bootsectandvi(&bs
, &volinfo
, &fatsize
);
1387 id
= volinfo
.volume_id
;
1388 sprintf(uuid_str
, "%02X%02X-%02X%02X", id
[3], id
[2], id
[1], id
[0]);