4 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
6 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
7 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
9 * SPDX-License-Identifier: GPL-2.0+
16 #include <asm/byteorder.h>
20 #include <linux/compiler.h>
21 #include <linux/ctype.h>
23 #ifdef CONFIG_SUPPORT_VFAT
24 static const int vfat_enabled
= 1;
26 static const int vfat_enabled
= 0;
30 * Convert a string to lowercase.
32 static void downcase(char *str
)
34 while (*str
!= '\0') {
40 static block_dev_desc_t
*cur_dev
;
41 static disk_partition_t cur_part_info
;
43 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
44 #define DOS_FS_TYPE_OFFSET 0x36
45 #define DOS_FS32_TYPE_OFFSET 0x52
47 static int disk_read(__u32 block
, __u32 nr_blocks
, void *buf
)
51 if (!cur_dev
|| !cur_dev
->block_read
)
54 ret
= cur_dev
->block_read(cur_dev
, cur_part_info
.start
+ block
,
57 if (nr_blocks
&& ret
== 0)
63 int fat_set_blk_dev(block_dev_desc_t
*dev_desc
, disk_partition_t
*info
)
65 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer
, dev_desc
->blksz
);
68 cur_part_info
= *info
;
70 /* Make sure it has a valid FAT header */
71 if (disk_read(0, 1, buffer
) != 1) {
76 /* Check if it's actually a DOS volume */
77 if (memcmp(buffer
+ DOS_BOOT_MAGIC_OFFSET
, "\x55\xAA", 2)) {
82 /* Check for FAT12/FAT16/FAT32 filesystem */
83 if (!memcmp(buffer
+ DOS_FS_TYPE_OFFSET
, "FAT", 3))
85 if (!memcmp(buffer
+ DOS_FS32_TYPE_OFFSET
, "FAT32", 5))
92 int fat_register_device(block_dev_desc_t
*dev_desc
, int part_no
)
94 disk_partition_t info
;
96 /* First close any currently found FAT filesystem */
99 /* Read the partition table, if present */
100 if (get_partition_info(dev_desc
, part_no
, &info
)) {
102 printf("** Partition %d not valid on device %d **\n",
103 part_no
, dev_desc
->dev
);
108 info
.size
= dev_desc
->lba
;
109 info
.blksz
= dev_desc
->blksz
;
113 #ifdef CONFIG_PARTITION_UUIDS
118 return fat_set_blk_dev(dev_desc
, &info
);
122 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
123 * Return index into string if found, -1 otherwise.
125 static int dirdelim(char *str
)
129 while (*str
!= '\0') {
130 if (ISDIRDELIM(*str
))
138 * Extract zero terminated short name from a directory entry.
140 static void get_name(dir_entry
*dirent
, char *s_name
)
144 memcpy(s_name
, dirent
->name
, 8);
147 while (*ptr
&& *ptr
!= ' ')
149 if (dirent
->ext
[0] && dirent
->ext
[0] != ' ') {
152 memcpy(ptr
, dirent
->ext
, 3);
154 while (*ptr
&& *ptr
!= ' ')
158 if (*s_name
== DELETED_FLAG
)
160 else if (*s_name
== aRING
)
161 *s_name
= DELETED_FLAG
;
166 * Get the entry at index 'entry' in a FAT (12/16/32) table.
167 * On failure 0x00 is returned.
169 static __u32
get_fatent(fsdata
*mydata
, __u32 entry
)
176 switch (mydata
->fatsize
) {
178 bufnum
= entry
/ FAT32BUFSIZE
;
179 offset
= entry
- bufnum
* FAT32BUFSIZE
;
182 bufnum
= entry
/ FAT16BUFSIZE
;
183 offset
= entry
- bufnum
* FAT16BUFSIZE
;
186 bufnum
= entry
/ FAT12BUFSIZE
;
187 offset
= entry
- bufnum
* FAT12BUFSIZE
;
191 /* Unsupported FAT size */
195 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
196 mydata
->fatsize
, entry
, entry
, offset
, offset
);
198 /* Read a new block of FAT entries into the cache. */
199 if (bufnum
!= mydata
->fatbufnum
) {
200 __u32 getsize
= FATBUFBLOCKS
;
201 __u8
*bufptr
= mydata
->fatbuf
;
202 __u32 fatlength
= mydata
->fatlength
;
203 __u32 startblock
= bufnum
* FATBUFBLOCKS
;
205 if (startblock
+ getsize
> fatlength
)
206 getsize
= fatlength
- startblock
;
208 startblock
+= mydata
->fat_sect
; /* Offset from start of disk */
210 if (disk_read(startblock
, getsize
, bufptr
) < 0) {
211 debug("Error reading FAT blocks\n");
214 mydata
->fatbufnum
= bufnum
;
217 /* Get the actual entry from the table */
218 switch (mydata
->fatsize
) {
220 ret
= FAT2CPU32(((__u32
*) mydata
->fatbuf
)[offset
]);
223 ret
= FAT2CPU16(((__u16
*) mydata
->fatbuf
)[offset
]);
226 off16
= (offset
* 3) / 4;
228 switch (offset
& 0x3) {
230 ret
= FAT2CPU16(((__u16
*) mydata
->fatbuf
)[off16
]);
234 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
236 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+ 1]);
238 ret
= (val2
<< 4) | (val1
>> 12);
241 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
243 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+ 1]);
245 ret
= (val2
<< 8) | (val1
>> 8);
248 ret
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
249 ret
= (ret
& 0xfff0) >> 4;
256 debug("FAT%d: ret: %08x, offset: %04x\n",
257 mydata
->fatsize
, ret
, offset
);
263 * Read at most 'size' bytes from the specified cluster into 'buffer'.
264 * Return 0 on success, -1 otherwise.
267 get_cluster(fsdata
*mydata
, __u32 clustnum
, __u8
*buffer
, unsigned long size
)
274 startsect
= mydata
->data_begin
+
275 clustnum
* mydata
->clust_size
;
277 startsect
= mydata
->rootdir_sect
;
280 debug("gc - clustnum: %d, startsect: %d\n", clustnum
, startsect
);
282 if ((unsigned long)buffer
& (ARCH_DMA_MINALIGN
- 1)) {
283 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
285 printf("FAT: Misaligned buffer address (%p)\n", buffer
);
287 while (size
>= mydata
->sect_size
) {
288 ret
= disk_read(startsect
++, 1, tmpbuf
);
290 debug("Error reading data (got %d)\n", ret
);
294 memcpy(buffer
, tmpbuf
, mydata
->sect_size
);
295 buffer
+= mydata
->sect_size
;
296 size
-= mydata
->sect_size
;
299 idx
= size
/ mydata
->sect_size
;
300 ret
= disk_read(startsect
, idx
, buffer
);
302 debug("Error reading data (got %d)\n", ret
);
306 idx
*= mydata
->sect_size
;
311 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
313 ret
= disk_read(startsect
, 1, tmpbuf
);
315 debug("Error reading data (got %d)\n", ret
);
319 memcpy(buffer
, tmpbuf
, size
);
326 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
328 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
330 __u8 get_contents_vfatname_block
[MAX_CLUSTSIZE
]
331 __aligned(ARCH_DMA_MINALIGN
);
333 static int get_contents(fsdata
*mydata
, dir_entry
*dentptr
, loff_t pos
,
334 __u8
*buffer
, loff_t maxsize
, loff_t
*gotsize
)
336 loff_t filesize
= FAT2CPU32(dentptr
->size
);
337 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
338 __u32 curclust
= START(dentptr
);
339 __u32 endclust
, newclust
;
343 debug("Filesize: %llu bytes\n", filesize
);
345 if (pos
>= filesize
) {
346 debug("Read position past EOF: %llu\n", pos
);
350 if (maxsize
> 0 && filesize
> pos
+ maxsize
)
351 filesize
= pos
+ maxsize
;
353 debug("%llu bytes\n", filesize
);
355 actsize
= bytesperclust
;
357 /* go to cluster at pos */
358 while (actsize
<= pos
) {
359 curclust
= get_fatent(mydata
, curclust
);
360 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
361 debug("curclust: 0x%x\n", curclust
);
362 debug("Invalid FAT entry\n");
365 actsize
+= bytesperclust
;
369 actsize
-= bytesperclust
;
373 /* align to beginning of next cluster if any */
375 actsize
= min(filesize
, (loff_t
)bytesperclust
);
376 if (get_cluster(mydata
, curclust
, get_contents_vfatname_block
,
377 (int)actsize
) != 0) {
378 printf("Error reading cluster\n");
383 memcpy(buffer
, get_contents_vfatname_block
+ pos
, actsize
);
389 curclust
= get_fatent(mydata
, curclust
);
390 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
391 debug("curclust: 0x%x\n", curclust
);
392 debug("Invalid FAT entry\n");
397 actsize
= bytesperclust
;
401 /* search for consecutive clusters */
402 while (actsize
< filesize
) {
403 newclust
= get_fatent(mydata
, endclust
);
404 if ((newclust
- 1) != endclust
)
406 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
407 debug("curclust: 0x%x\n", newclust
);
408 debug("Invalid FAT entry\n");
412 actsize
+= bytesperclust
;
415 /* get remaining bytes */
417 if (get_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
418 printf("Error reading cluster\n");
424 if (get_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
425 printf("Error reading cluster\n");
428 *gotsize
+= (int)actsize
;
432 curclust
= get_fatent(mydata
, endclust
);
433 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
434 debug("curclust: 0x%x\n", curclust
);
435 printf("Invalid FAT entry\n");
438 actsize
= bytesperclust
;
444 * Extract the file name information from 'slotptr' into 'l_name',
445 * starting at l_name[*idx].
446 * Return 1 if terminator (zero byte) is found, 0 otherwise.
448 static int slot2str(dir_slot
*slotptr
, char *l_name
, int *idx
)
452 for (j
= 0; j
<= 8; j
+= 2) {
453 l_name
[*idx
] = slotptr
->name0_4
[j
];
454 if (l_name
[*idx
] == 0x00)
458 for (j
= 0; j
<= 10; j
+= 2) {
459 l_name
[*idx
] = slotptr
->name5_10
[j
];
460 if (l_name
[*idx
] == 0x00)
464 for (j
= 0; j
<= 2; j
+= 2) {
465 l_name
[*idx
] = slotptr
->name11_12
[j
];
466 if (l_name
[*idx
] == 0x00)
475 * Extract the full long filename starting at 'retdent' (which is really
476 * a slot) into 'l_name'. If successful also copy the real directory entry
478 * Return 0 on success, -1 otherwise.
481 get_vfatname(fsdata
*mydata
, int curclust
, __u8
*cluster
,
482 dir_entry
*retdent
, char *l_name
)
485 dir_slot
*slotptr
= (dir_slot
*)retdent
;
486 __u8
*buflimit
= cluster
+ mydata
->sect_size
* ((curclust
== 0) ?
489 __u8 counter
= (slotptr
->id
& ~LAST_LONG_ENTRY_MASK
) & 0xff;
492 if (counter
> VFAT_MAXSEQ
) {
493 debug("Error: VFAT name is too long\n");
497 while ((__u8
*)slotptr
< buflimit
) {
500 if (((slotptr
->id
& ~LAST_LONG_ENTRY_MASK
) & 0xff) != counter
)
506 if ((__u8
*)slotptr
>= buflimit
) {
511 curclust
= get_fatent(mydata
, curclust
);
512 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
513 debug("curclust: 0x%x\n", curclust
);
514 printf("Invalid FAT entry\n");
518 if (get_cluster(mydata
, curclust
, get_contents_vfatname_block
,
519 mydata
->clust_size
* mydata
->sect_size
) != 0) {
520 debug("Error: reading directory block\n");
524 slotptr2
= (dir_slot
*)get_contents_vfatname_block
;
525 while (counter
> 0) {
526 if (((slotptr2
->id
& ~LAST_LONG_ENTRY_MASK
)
533 /* Save the real directory entry */
534 realdent
= (dir_entry
*)slotptr2
;
535 while ((__u8
*)slotptr2
> get_contents_vfatname_block
) {
537 slot2str(slotptr2
, l_name
, &idx
);
540 /* Save the real directory entry */
541 realdent
= (dir_entry
*)slotptr
;
546 if (slot2str(slotptr
, l_name
, &idx
))
548 } while (!(slotptr
->id
& LAST_LONG_ENTRY_MASK
));
551 if (*l_name
== DELETED_FLAG
)
553 else if (*l_name
== aRING
)
554 *l_name
= DELETED_FLAG
;
557 /* Return the real directory entry */
558 memcpy(retdent
, realdent
, sizeof(dir_entry
));
563 /* Calculate short name checksum */
564 static __u8
mkcksum(const char name
[8], const char ext
[3])
570 for (i
= 0; i
< 8; i
++)
571 ret
= (((ret
& 1) << 7) | ((ret
& 0xfe) >> 1)) + name
[i
];
572 for (i
= 0; i
< 3; i
++)
573 ret
= (((ret
& 1) << 7) | ((ret
& 0xfe) >> 1)) + ext
[i
];
579 * Get the directory entry associated with 'filename' from the directory
580 * starting at 'startsect'
582 __u8 get_dentfromdir_block
[MAX_CLUSTSIZE
]
583 __aligned(ARCH_DMA_MINALIGN
);
585 static dir_entry
*get_dentfromdir(fsdata
*mydata
, int startsect
,
586 char *filename
, dir_entry
*retdent
,
589 __u16 prevcksum
= 0xffff;
590 __u32 curclust
= START(retdent
);
591 int files
= 0, dirs
= 0;
593 debug("get_dentfromdir: %s\n", filename
);
600 if (get_cluster(mydata
, curclust
, get_dentfromdir_block
,
601 mydata
->clust_size
* mydata
->sect_size
) != 0) {
602 debug("Error: reading directory block\n");
606 dentptr
= (dir_entry
*)get_dentfromdir_block
;
608 for (i
= 0; i
< DIRENTSPERCLUST
; i
++) {
609 char s_name
[14], l_name
[VFAT_MAXLEN_BYTES
];
612 if (dentptr
->name
[0] == DELETED_FLAG
) {
616 if ((dentptr
->attr
& ATTR_VOLUME
)) {
618 (dentptr
->attr
& ATTR_VFAT
) == ATTR_VFAT
&&
619 (dentptr
->name
[0] & LAST_LONG_ENTRY_MASK
)) {
620 prevcksum
= ((dir_slot
*)dentptr
)->alias_checksum
;
621 get_vfatname(mydata
, curclust
,
622 get_dentfromdir_block
,
629 isdir
= (dentptr
->attr
& ATTR_DIR
);
637 if (l_name
[0] != 0) {
644 printf(" %8u %s%c\n",
645 FAT2CPU32(dentptr
->size
),
657 debug("vfatname: |%s|\n", l_name
);
659 /* Volume label or VFAT entry */
664 if (dentptr
->name
[0] == 0) {
666 printf("\n%d file(s), %d dir(s)\n\n",
669 debug("Dentname == NULL - %d\n", i
);
673 __u8 csum
= mkcksum(dentptr
->name
, dentptr
->ext
);
674 if (dols
&& csum
== prevcksum
) {
681 get_name(dentptr
, s_name
);
683 int isdir
= (dentptr
->attr
& ATTR_DIR
);
693 if (s_name
[0] != 0) {
701 printf(" %8u %s%c\n",
702 FAT2CPU32(dentptr
->size
),
714 if (strcmp(filename
, s_name
)
715 && strcmp(filename
, l_name
)) {
716 debug("Mismatch: |%s|%s|\n", s_name
, l_name
);
721 memcpy(retdent
, dentptr
, sizeof(dir_entry
));
723 debug("DentName: %s", s_name
);
724 debug(", start: 0x%x", START(dentptr
));
725 debug(", size: 0x%x %s\n",
726 FAT2CPU32(dentptr
->size
),
727 (dentptr
->attr
& ATTR_DIR
) ? "(DIR)" : "");
732 curclust
= get_fatent(mydata
, curclust
);
733 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
734 debug("curclust: 0x%x\n", curclust
);
735 printf("Invalid FAT entry\n");
744 * Read boot sector and volume info from a FAT filesystem
747 read_bootsectandvi(boot_sector
*bs
, volume_info
*volinfo
, int *fatsize
)
750 volume_info
*vistart
;
753 if (cur_dev
== NULL
) {
754 debug("Error: no device selected\n");
758 block
= memalign(ARCH_DMA_MINALIGN
, cur_dev
->blksz
);
760 debug("Error: allocating block\n");
764 if (disk_read(0, 1, block
) < 0) {
765 debug("Error: reading block\n");
769 memcpy(bs
, block
, sizeof(boot_sector
));
770 bs
->reserved
= FAT2CPU16(bs
->reserved
);
771 bs
->fat_length
= FAT2CPU16(bs
->fat_length
);
772 bs
->secs_track
= FAT2CPU16(bs
->secs_track
);
773 bs
->heads
= FAT2CPU16(bs
->heads
);
774 bs
->total_sect
= FAT2CPU32(bs
->total_sect
);
777 if (bs
->fat_length
== 0) {
779 bs
->fat32_length
= FAT2CPU32(bs
->fat32_length
);
780 bs
->flags
= FAT2CPU16(bs
->flags
);
781 bs
->root_cluster
= FAT2CPU32(bs
->root_cluster
);
782 bs
->info_sector
= FAT2CPU16(bs
->info_sector
);
783 bs
->backup_boot
= FAT2CPU16(bs
->backup_boot
);
784 vistart
= (volume_info
*)(block
+ sizeof(boot_sector
));
787 vistart
= (volume_info
*)&(bs
->fat32_length
);
790 memcpy(volinfo
, vistart
, sizeof(volume_info
));
792 if (*fatsize
== 32) {
793 if (strncmp(FAT32_SIGN
, vistart
->fs_type
, SIGNLEN
) == 0)
796 if (strncmp(FAT12_SIGN
, vistart
->fs_type
, SIGNLEN
) == 0) {
800 if (strncmp(FAT16_SIGN
, vistart
->fs_type
, SIGNLEN
) == 0) {
806 debug("Error: broken fs_type sign\n");
814 __u8 do_fat_read_at_block
[MAX_CLUSTSIZE
]
815 __aligned(ARCH_DMA_MINALIGN
);
817 int do_fat_read_at(const char *filename
, loff_t pos
, void *buffer
,
818 loff_t maxsize
, int dols
, int dogetsize
, loff_t
*size
)
820 char fnamecopy
[2048];
824 fsdata
*mydata
= &datablock
;
825 dir_entry
*dentptr
= NULL
;
826 __u16 prevcksum
= 0xffff;
830 int files
= 0, dirs
= 0;
833 __u32 root_cluster
= 0;
835 int rootdir_size
= 0;
840 if (read_bootsectandvi(&bs
, &volinfo
, &mydata
->fatsize
)) {
841 debug("Error: reading boot sector\n");
845 if (mydata
->fatsize
== 32) {
846 root_cluster
= bs
.root_cluster
;
847 mydata
->fatlength
= bs
.fat32_length
;
849 mydata
->fatlength
= bs
.fat_length
;
852 mydata
->fat_sect
= bs
.reserved
;
854 cursect
= mydata
->rootdir_sect
855 = mydata
->fat_sect
+ mydata
->fatlength
* bs
.fats
;
857 mydata
->sect_size
= (bs
.sector_size
[1] << 8) + bs
.sector_size
[0];
858 mydata
->clust_size
= bs
.cluster_size
;
859 if (mydata
->sect_size
!= cur_part_info
.blksz
) {
860 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
861 mydata
->sect_size
, cur_part_info
.blksz
);
865 if (mydata
->fatsize
== 32) {
866 mydata
->data_begin
= mydata
->rootdir_sect
-
867 (mydata
->clust_size
* 2);
869 rootdir_size
= ((bs
.dir_entries
[1] * (int)256 +
873 mydata
->data_begin
= mydata
->rootdir_sect
+
875 (mydata
->clust_size
* 2);
878 mydata
->fatbufnum
= -1;
879 mydata
->fatbuf
= memalign(ARCH_DMA_MINALIGN
, FATBUFSIZE
);
880 if (mydata
->fatbuf
== NULL
) {
881 debug("Error: allocating memory\n");
886 debug("VFAT Support enabled\n");
888 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
889 mydata
->fatsize
, mydata
->fat_sect
, mydata
->fatlength
);
890 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
891 "Data begins at: %d\n",
893 mydata
->rootdir_sect
,
894 mydata
->rootdir_sect
* mydata
->sect_size
, mydata
->data_begin
);
895 debug("Sector size: %d, cluster size: %d\n", mydata
->sect_size
,
898 /* "cwd" is always the root... */
899 while (ISDIRDELIM(*filename
))
902 /* Make a copy of the filename and convert it to lowercase */
903 strcpy(fnamecopy
, filename
);
907 if (*fnamecopy
== '\0') {
912 } else if ((idx
= dirdelim(fnamecopy
)) >= 0) {
914 fnamecopy
[idx
] = '\0';
915 subname
= fnamecopy
+ idx
+ 1;
917 /* Handle multiple delimiters */
918 while (ISDIRDELIM(*subname
))
929 if (mydata
->fatsize
== 32 || firsttime
) {
930 dir_ptr
= do_fat_read_at_block
;
934 * FAT16 sector buffer modification:
935 * Each loop, the second buffered block is moved to
936 * the buffer begin, and two next sectors are read
937 * next to the previously moved one. So the sector
938 * buffer keeps always 3 sectors for fat16.
939 * And the current sector is the buffer second sector
940 * beside the "firsttime" read, when it is the first one.
942 * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1]
943 * n = computed root dir sector
944 * loop | cursect-1 | cursect | cursect+1 |
945 * 0 | sector n+0 | sector n+1 | none |
946 * 1 | none | sector n+0 | sector n+1 |
947 * 0 | sector n+1 | sector n+2 | sector n+3 |
948 * 1 | sector n+3 | ...
950 dir_ptr
= (do_fat_read_at_block
+ mydata
->sect_size
);
951 memcpy(do_fat_read_at_block
, dir_ptr
, mydata
->sect_size
);
956 if (mydata
->fatsize
== 32 && buffer_blk_cnt
)
960 read_blk
= (mydata
->fatsize
== 32) ?
961 mydata
->clust_size
: PREFETCH_BLOCKS
;
963 debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
964 cursect
, read_blk
, mydata
->clust_size
, DIRENTSPERBLOCK
);
966 if (disk_read(cursect
, read_blk
, dir_ptr
) < 0) {
967 debug("Error: reading rootdir block\n");
971 dentptr
= (dir_entry
*)dir_ptr
;
974 for (i
= 0; i
< DIRENTSPERBLOCK
; i
++) {
975 char s_name
[14], l_name
[VFAT_MAXLEN_BYTES
];
979 if (dentptr
->name
[0] == DELETED_FLAG
) {
985 csum
= mkcksum(dentptr
->name
, dentptr
->ext
);
987 if (dentptr
->attr
& ATTR_VOLUME
) {
989 (dentptr
->attr
& ATTR_VFAT
) == ATTR_VFAT
&&
990 (dentptr
->name
[0] & LAST_LONG_ENTRY_MASK
)) {
992 ((dir_slot
*)dentptr
)->alias_checksum
;
999 if (dols
== LS_ROOT
) {
1003 (dentptr
->attr
& ATTR_DIR
);
1011 if (l_name
[0] != 0) {
1018 printf(" %8u %s%c\n",
1019 FAT2CPU32(dentptr
->size
),
1031 debug("Rootvfatname: |%s|\n",
1034 /* Volume label or VFAT entry */
1038 } else if (dentptr
->name
[0] == 0) {
1039 debug("RootDentname == NULL - %d\n", i
);
1040 if (dols
== LS_ROOT
) {
1041 printf("\n%d file(s), %d dir(s)\n\n",
1047 else if (vfat_enabled
&&
1048 dols
== LS_ROOT
&& csum
== prevcksum
) {
1054 get_name(dentptr
, s_name
);
1056 if (dols
== LS_ROOT
) {
1057 int isdir
= (dentptr
->attr
& ATTR_DIR
);
1063 if (s_name
[0] != 0) {
1069 if (s_name
[0] != 0) {
1076 printf(" %8u %s%c\n",
1077 FAT2CPU32(dentptr
->size
),
1088 if (strcmp(fnamecopy
, s_name
)
1089 && strcmp(fnamecopy
, l_name
)) {
1090 debug("RootMismatch: |%s|%s|\n", s_name
,
1096 if (isdir
&& !(dentptr
->attr
& ATTR_DIR
))
1099 debug("RootName: %s", s_name
);
1100 debug(", start: 0x%x", START(dentptr
));
1101 debug(", size: 0x%x %s\n",
1102 FAT2CPU32(dentptr
->size
),
1103 isdir
? "(DIR)" : "");
1105 goto rootdir_done
; /* We got a match */
1107 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt
,
1108 mydata
->clust_size
);
1111 * On FAT32 we must fetch the FAT entries for the next
1112 * root directory clusters when a cluster has been
1113 * completely processed.
1116 int rootdir_end
= 0;
1117 if (mydata
->fatsize
== 32) {
1118 if (buffer_blk_cnt
== mydata
->clust_size
) {
1122 nxt_clust
= get_fatent(mydata
, root_cluster
);
1123 rootdir_end
= CHECK_CLUST(nxt_clust
, 32);
1125 nxtsect
= mydata
->data_begin
+
1126 (nxt_clust
* mydata
->clust_size
);
1128 root_cluster
= nxt_clust
;
1134 if (buffer_blk_cnt
== PREFETCH_BLOCKS
)
1137 rootdir_end
= (++cursect
- mydata
->rootdir_sect
>=
1141 /* If end of rootdir reached */
1143 if (dols
== LS_ROOT
) {
1144 printf("\n%d file(s), %d dir(s)\n\n",
1156 int startsect
= mydata
->data_begin
1157 + START(dentptr
) * mydata
->clust_size
;
1159 char *nextname
= NULL
;
1164 idx
= dirdelim(subname
);
1167 subname
[idx
] = '\0';
1168 nextname
= subname
+ idx
+ 1;
1169 /* Handle multiple delimiters */
1170 while (ISDIRDELIM(*nextname
))
1172 if (dols
&& *nextname
== '\0')
1175 if (dols
&& firsttime
) {
1182 if (get_dentfromdir(mydata
, startsect
, subname
, dentptr
,
1183 isdir
? 0 : dols
) == NULL
) {
1189 if (isdir
&& !(dentptr
->attr
& ATTR_DIR
))
1193 * If we are looking for a directory, and found a directory
1194 * type entry, and the entry is for the root directory (as
1195 * denoted by a cluster number of 0), jump back to the start
1196 * of the function, since at least on FAT12/16, the root dir
1197 * lives in a hard-coded location and needs special handling
1198 * to parse, rather than simply following the cluster linked
1199 * list in the FAT, like other directories.
1201 if (isdir
&& (dentptr
->attr
& ATTR_DIR
) && !START(dentptr
)) {
1203 * Modify the filename to remove the prefix that gets
1204 * back to the root directory, so the initial root dir
1205 * parsing code can continue from where we are without
1208 strcpy(fnamecopy
, nextname
?: "");
1210 * Set up state the same way as the function does when
1211 * first started. This is required for the root dir
1212 * parsing code operates in its expected environment.
1215 cursect
= mydata
->rootdir_sect
;
1225 *size
= FAT2CPU32(dentptr
->size
);
1228 ret
= get_contents(mydata
, dentptr
, pos
, buffer
, maxsize
, size
);
1230 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr
->size
), *size
);
1233 free(mydata
->fatbuf
);
1237 int do_fat_read(const char *filename
, void *buffer
, loff_t maxsize
, int dols
,
1240 return do_fat_read_at(filename
, 0, buffer
, maxsize
, dols
, 0, actread
);
1243 int file_fat_detectfs(void)
1246 volume_info volinfo
;
1250 if (cur_dev
== NULL
) {
1251 printf("No current device\n");
1255 #if defined(CONFIG_CMD_IDE) || \
1256 defined(CONFIG_CMD_SATA) || \
1257 defined(CONFIG_CMD_SCSI) || \
1258 defined(CONFIG_CMD_USB) || \
1260 printf("Interface: ");
1261 switch (cur_dev
->if_type
) {
1287 printf("\n Device %d: ", cur_dev
->dev
);
1291 if (read_bootsectandvi(&bs
, &volinfo
, &fatsize
)) {
1292 printf("\nNo valid FAT fs found\n");
1296 memcpy(vol_label
, volinfo
.volume_label
, 11);
1297 vol_label
[11] = '\0';
1298 volinfo
.fs_type
[5] = '\0';
1300 printf("Filesystem: %s \"%s\"\n", volinfo
.fs_type
, vol_label
);
1305 int file_fat_ls(const char *dir
)
1309 return do_fat_read(dir
, NULL
, 0, LS_YES
, &size
);
1312 int fat_exists(const char *filename
)
1317 ret
= do_fat_read_at(filename
, 0, NULL
, 0, LS_NO
, 1, &size
);
1321 int fat_size(const char *filename
, loff_t
*size
)
1323 return do_fat_read_at(filename
, 0, NULL
, 0, LS_NO
, 1, size
);
1326 int file_fat_read_at(const char *filename
, loff_t pos
, void *buffer
,
1327 loff_t maxsize
, loff_t
*actread
)
1329 printf("reading %s\n", filename
);
1330 return do_fat_read_at(filename
, pos
, buffer
, maxsize
, LS_NO
, 0,
1334 int file_fat_read(const char *filename
, void *buffer
, int maxsize
)
1339 ret
= file_fat_read_at(filename
, 0, buffer
, maxsize
, &actread
);
1346 int fat_read_file(const char *filename
, void *buf
, loff_t offset
, loff_t len
,
1351 ret
= file_fat_read_at(filename
, offset
, buf
, len
, actread
);
1353 printf("** Unable to read file %s **\n", filename
);
1358 void fat_close(void)