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>
19 #include <linux/compiler.h>
20 #include <linux/ctype.h>
22 #ifdef CONFIG_SUPPORT_VFAT
23 static const int vfat_enabled
= 1;
25 static const int vfat_enabled
= 0;
29 * Convert a string to lowercase.
31 static void downcase(char *str
)
33 while (*str
!= '\0') {
39 static block_dev_desc_t
*cur_dev
;
40 static disk_partition_t cur_part_info
;
42 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
43 #define DOS_FS_TYPE_OFFSET 0x36
44 #define DOS_FS32_TYPE_OFFSET 0x52
46 static int disk_read(__u32 block
, __u32 nr_blocks
, void *buf
)
48 if (!cur_dev
|| !cur_dev
->block_read
)
51 return cur_dev
->block_read(cur_dev
->dev
,
52 cur_part_info
.start
+ block
, nr_blocks
, buf
);
55 int fat_set_blk_dev(block_dev_desc_t
*dev_desc
, disk_partition_t
*info
)
57 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer
, dev_desc
->blksz
);
60 cur_part_info
= *info
;
62 /* Make sure it has a valid FAT header */
63 if (disk_read(0, 1, buffer
) != 1) {
68 /* Check if it's actually a DOS volume */
69 if (memcmp(buffer
+ DOS_BOOT_MAGIC_OFFSET
, "\x55\xAA", 2)) {
74 /* Check for FAT12/FAT16/FAT32 filesystem */
75 if (!memcmp(buffer
+ DOS_FS_TYPE_OFFSET
, "FAT", 3))
77 if (!memcmp(buffer
+ DOS_FS32_TYPE_OFFSET
, "FAT32", 5))
84 int fat_register_device(block_dev_desc_t
*dev_desc
, int part_no
)
86 disk_partition_t info
;
88 /* First close any currently found FAT filesystem */
91 /* Read the partition table, if present */
92 if (get_partition_info(dev_desc
, part_no
, &info
)) {
94 printf("** Partition %d not valid on device %d **\n",
95 part_no
, dev_desc
->dev
);
100 info
.size
= dev_desc
->lba
;
101 info
.blksz
= dev_desc
->blksz
;
105 #ifdef CONFIG_PARTITION_UUIDS
110 return fat_set_blk_dev(dev_desc
, &info
);
114 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
115 * Return index into string if found, -1 otherwise.
117 static int dirdelim(char *str
)
121 while (*str
!= '\0') {
122 if (ISDIRDELIM(*str
))
130 * Extract zero terminated short name from a directory entry.
132 static void get_name(dir_entry
*dirent
, char *s_name
)
136 memcpy(s_name
, dirent
->name
, 8);
139 while (*ptr
&& *ptr
!= ' ')
141 if (dirent
->ext
[0] && dirent
->ext
[0] != ' ') {
144 memcpy(ptr
, dirent
->ext
, 3);
146 while (*ptr
&& *ptr
!= ' ')
150 if (*s_name
== DELETED_FLAG
)
152 else if (*s_name
== aRING
)
153 *s_name
= DELETED_FLAG
;
158 * Get the entry at index 'entry' in a FAT (12/16/32) table.
159 * On failure 0x00 is returned.
161 static __u32
get_fatent(fsdata
*mydata
, __u32 entry
)
168 switch (mydata
->fatsize
) {
170 bufnum
= entry
/ FAT32BUFSIZE
;
171 offset
= entry
- bufnum
* FAT32BUFSIZE
;
174 bufnum
= entry
/ FAT16BUFSIZE
;
175 offset
= entry
- bufnum
* FAT16BUFSIZE
;
178 bufnum
= entry
/ FAT12BUFSIZE
;
179 offset
= entry
- bufnum
* FAT12BUFSIZE
;
183 /* Unsupported FAT size */
187 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
188 mydata
->fatsize
, entry
, entry
, offset
, offset
);
190 /* Read a new block of FAT entries into the cache. */
191 if (bufnum
!= mydata
->fatbufnum
) {
192 __u32 getsize
= FATBUFBLOCKS
;
193 __u8
*bufptr
= mydata
->fatbuf
;
194 __u32 fatlength
= mydata
->fatlength
;
195 __u32 startblock
= bufnum
* FATBUFBLOCKS
;
197 if (startblock
+ getsize
> fatlength
)
198 getsize
= fatlength
- startblock
;
200 startblock
+= mydata
->fat_sect
; /* Offset from start of disk */
202 if (disk_read(startblock
, getsize
, bufptr
) < 0) {
203 debug("Error reading FAT blocks\n");
206 mydata
->fatbufnum
= bufnum
;
209 /* Get the actual entry from the table */
210 switch (mydata
->fatsize
) {
212 ret
= FAT2CPU32(((__u32
*) mydata
->fatbuf
)[offset
]);
215 ret
= FAT2CPU16(((__u16
*) mydata
->fatbuf
)[offset
]);
218 off16
= (offset
* 3) / 4;
220 switch (offset
& 0x3) {
222 ret
= FAT2CPU16(((__u16
*) mydata
->fatbuf
)[off16
]);
226 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
228 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+ 1]);
230 ret
= (val2
<< 4) | (val1
>> 12);
233 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
235 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+ 1]);
237 ret
= (val2
<< 8) | (val1
>> 8);
240 ret
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
241 ret
= (ret
& 0xfff0) >> 4;
248 debug("FAT%d: ret: %08x, offset: %04x\n",
249 mydata
->fatsize
, ret
, offset
);
255 * Read at most 'size' bytes from the specified cluster into 'buffer'.
256 * Return 0 on success, -1 otherwise.
259 get_cluster(fsdata
*mydata
, __u32 clustnum
, __u8
*buffer
, unsigned long size
)
266 startsect
= mydata
->data_begin
+
267 clustnum
* mydata
->clust_size
;
269 startsect
= mydata
->rootdir_sect
;
272 debug("gc - clustnum: %d, startsect: %d\n", clustnum
, startsect
);
274 if ((unsigned long)buffer
& (ARCH_DMA_MINALIGN
- 1)) {
275 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
277 printf("FAT: Misaligned buffer address (%p)\n", buffer
);
279 while (size
>= mydata
->sect_size
) {
280 ret
= disk_read(startsect
++, 1, tmpbuf
);
282 debug("Error reading data (got %d)\n", ret
);
286 memcpy(buffer
, tmpbuf
, mydata
->sect_size
);
287 buffer
+= mydata
->sect_size
;
288 size
-= mydata
->sect_size
;
291 idx
= size
/ mydata
->sect_size
;
292 ret
= disk_read(startsect
, idx
, buffer
);
294 debug("Error reading data (got %d)\n", ret
);
298 idx
*= mydata
->sect_size
;
303 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
305 ret
= disk_read(startsect
, 1, tmpbuf
);
307 debug("Error reading data (got %d)\n", ret
);
311 memcpy(buffer
, tmpbuf
, size
);
318 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
320 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
322 __u8 get_contents_vfatname_block
[MAX_CLUSTSIZE
]
323 __aligned(ARCH_DMA_MINALIGN
);
325 static int get_contents(fsdata
*mydata
, dir_entry
*dentptr
, loff_t pos
,
326 __u8
*buffer
, loff_t maxsize
, loff_t
*gotsize
)
328 loff_t filesize
= FAT2CPU32(dentptr
->size
);
329 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
330 __u32 curclust
= START(dentptr
);
331 __u32 endclust
, newclust
;
335 debug("Filesize: %llu bytes\n", filesize
);
337 if (pos
>= filesize
) {
338 debug("Read position past EOF: %llu\n", pos
);
342 if (maxsize
> 0 && filesize
> pos
+ maxsize
)
343 filesize
= pos
+ maxsize
;
345 debug("%llu bytes\n", filesize
);
347 actsize
= bytesperclust
;
349 /* go to cluster at pos */
350 while (actsize
<= pos
) {
351 curclust
= get_fatent(mydata
, curclust
);
352 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
353 debug("curclust: 0x%x\n", curclust
);
354 debug("Invalid FAT entry\n");
357 actsize
+= bytesperclust
;
361 actsize
-= bytesperclust
;
365 /* align to beginning of next cluster if any */
367 actsize
= min(filesize
, (loff_t
)bytesperclust
);
368 if (get_cluster(mydata
, curclust
, get_contents_vfatname_block
,
369 (int)actsize
) != 0) {
370 printf("Error reading cluster\n");
375 memcpy(buffer
, get_contents_vfatname_block
+ pos
, actsize
);
381 curclust
= get_fatent(mydata
, curclust
);
382 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
383 debug("curclust: 0x%x\n", curclust
);
384 debug("Invalid FAT entry\n");
389 actsize
= bytesperclust
;
393 /* search for consecutive clusters */
394 while (actsize
< filesize
) {
395 newclust
= get_fatent(mydata
, endclust
);
396 if ((newclust
- 1) != endclust
)
398 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
399 debug("curclust: 0x%x\n", newclust
);
400 debug("Invalid FAT entry\n");
404 actsize
+= bytesperclust
;
407 /* get remaining bytes */
409 if (get_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
410 printf("Error reading cluster\n");
416 if (get_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
417 printf("Error reading cluster\n");
420 *gotsize
+= (int)actsize
;
424 curclust
= get_fatent(mydata
, endclust
);
425 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
426 debug("curclust: 0x%x\n", curclust
);
427 printf("Invalid FAT entry\n");
430 actsize
= bytesperclust
;
436 * Extract the file name information from 'slotptr' into 'l_name',
437 * starting at l_name[*idx].
438 * Return 1 if terminator (zero byte) is found, 0 otherwise.
440 static int slot2str(dir_slot
*slotptr
, char *l_name
, int *idx
)
444 for (j
= 0; j
<= 8; j
+= 2) {
445 l_name
[*idx
] = slotptr
->name0_4
[j
];
446 if (l_name
[*idx
] == 0x00)
450 for (j
= 0; j
<= 10; j
+= 2) {
451 l_name
[*idx
] = slotptr
->name5_10
[j
];
452 if (l_name
[*idx
] == 0x00)
456 for (j
= 0; j
<= 2; j
+= 2) {
457 l_name
[*idx
] = slotptr
->name11_12
[j
];
458 if (l_name
[*idx
] == 0x00)
467 * Extract the full long filename starting at 'retdent' (which is really
468 * a slot) into 'l_name'. If successful also copy the real directory entry
470 * Return 0 on success, -1 otherwise.
473 get_vfatname(fsdata
*mydata
, int curclust
, __u8
*cluster
,
474 dir_entry
*retdent
, char *l_name
)
477 dir_slot
*slotptr
= (dir_slot
*)retdent
;
478 __u8
*buflimit
= cluster
+ mydata
->sect_size
* ((curclust
== 0) ?
481 __u8 counter
= (slotptr
->id
& ~LAST_LONG_ENTRY_MASK
) & 0xff;
484 if (counter
> VFAT_MAXSEQ
) {
485 debug("Error: VFAT name is too long\n");
489 while ((__u8
*)slotptr
< buflimit
) {
492 if (((slotptr
->id
& ~LAST_LONG_ENTRY_MASK
) & 0xff) != counter
)
498 if ((__u8
*)slotptr
>= buflimit
) {
503 curclust
= get_fatent(mydata
, curclust
);
504 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
505 debug("curclust: 0x%x\n", curclust
);
506 printf("Invalid FAT entry\n");
510 if (get_cluster(mydata
, curclust
, get_contents_vfatname_block
,
511 mydata
->clust_size
* mydata
->sect_size
) != 0) {
512 debug("Error: reading directory block\n");
516 slotptr2
= (dir_slot
*)get_contents_vfatname_block
;
517 while (counter
> 0) {
518 if (((slotptr2
->id
& ~LAST_LONG_ENTRY_MASK
)
525 /* Save the real directory entry */
526 realdent
= (dir_entry
*)slotptr2
;
527 while ((__u8
*)slotptr2
> get_contents_vfatname_block
) {
529 slot2str(slotptr2
, l_name
, &idx
);
532 /* Save the real directory entry */
533 realdent
= (dir_entry
*)slotptr
;
538 if (slot2str(slotptr
, l_name
, &idx
))
540 } while (!(slotptr
->id
& LAST_LONG_ENTRY_MASK
));
543 if (*l_name
== DELETED_FLAG
)
545 else if (*l_name
== aRING
)
546 *l_name
= DELETED_FLAG
;
549 /* Return the real directory entry */
550 memcpy(retdent
, realdent
, sizeof(dir_entry
));
555 /* Calculate short name checksum */
556 static __u8
mkcksum(const char name
[8], const char ext
[3])
562 for (i
= 0; i
< 8; i
++)
563 ret
= (((ret
& 1) << 7) | ((ret
& 0xfe) >> 1)) + name
[i
];
564 for (i
= 0; i
< 3; i
++)
565 ret
= (((ret
& 1) << 7) | ((ret
& 0xfe) >> 1)) + ext
[i
];
571 * Get the directory entry associated with 'filename' from the directory
572 * starting at 'startsect'
574 __u8 get_dentfromdir_block
[MAX_CLUSTSIZE
]
575 __aligned(ARCH_DMA_MINALIGN
);
577 static dir_entry
*get_dentfromdir(fsdata
*mydata
, int startsect
,
578 char *filename
, dir_entry
*retdent
,
581 __u16 prevcksum
= 0xffff;
582 __u32 curclust
= START(retdent
);
583 int files
= 0, dirs
= 0;
585 debug("get_dentfromdir: %s\n", filename
);
592 if (get_cluster(mydata
, curclust
, get_dentfromdir_block
,
593 mydata
->clust_size
* mydata
->sect_size
) != 0) {
594 debug("Error: reading directory block\n");
598 dentptr
= (dir_entry
*)get_dentfromdir_block
;
600 for (i
= 0; i
< DIRENTSPERCLUST
; i
++) {
601 char s_name
[14], l_name
[VFAT_MAXLEN_BYTES
];
604 if (dentptr
->name
[0] == DELETED_FLAG
) {
608 if ((dentptr
->attr
& ATTR_VOLUME
)) {
610 (dentptr
->attr
& ATTR_VFAT
) == ATTR_VFAT
&&
611 (dentptr
->name
[0] & LAST_LONG_ENTRY_MASK
)) {
612 prevcksum
= ((dir_slot
*)dentptr
)->alias_checksum
;
613 get_vfatname(mydata
, curclust
,
614 get_dentfromdir_block
,
621 isdir
= (dentptr
->attr
& ATTR_DIR
);
629 if (l_name
[0] != 0) {
636 printf(" %8u %s%c\n",
637 FAT2CPU32(dentptr
->size
),
649 debug("vfatname: |%s|\n", l_name
);
651 /* Volume label or VFAT entry */
656 if (dentptr
->name
[0] == 0) {
658 printf("\n%d file(s), %d dir(s)\n\n",
661 debug("Dentname == NULL - %d\n", i
);
665 __u8 csum
= mkcksum(dentptr
->name
, dentptr
->ext
);
666 if (dols
&& csum
== prevcksum
) {
673 get_name(dentptr
, s_name
);
675 int isdir
= (dentptr
->attr
& ATTR_DIR
);
685 if (s_name
[0] != 0) {
693 printf(" %8u %s%c\n",
694 FAT2CPU32(dentptr
->size
),
706 if (strcmp(filename
, s_name
)
707 && strcmp(filename
, l_name
)) {
708 debug("Mismatch: |%s|%s|\n", s_name
, l_name
);
713 memcpy(retdent
, dentptr
, sizeof(dir_entry
));
715 debug("DentName: %s", s_name
);
716 debug(", start: 0x%x", START(dentptr
));
717 debug(", size: 0x%x %s\n",
718 FAT2CPU32(dentptr
->size
),
719 (dentptr
->attr
& ATTR_DIR
) ? "(DIR)" : "");
724 curclust
= get_fatent(mydata
, curclust
);
725 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
726 debug("curclust: 0x%x\n", curclust
);
727 printf("Invalid FAT entry\n");
736 * Read boot sector and volume info from a FAT filesystem
739 read_bootsectandvi(boot_sector
*bs
, volume_info
*volinfo
, int *fatsize
)
742 volume_info
*vistart
;
745 if (cur_dev
== NULL
) {
746 debug("Error: no device selected\n");
750 block
= memalign(ARCH_DMA_MINALIGN
, cur_dev
->blksz
);
752 debug("Error: allocating block\n");
756 if (disk_read(0, 1, block
) < 0) {
757 debug("Error: reading block\n");
761 memcpy(bs
, block
, sizeof(boot_sector
));
762 bs
->reserved
= FAT2CPU16(bs
->reserved
);
763 bs
->fat_length
= FAT2CPU16(bs
->fat_length
);
764 bs
->secs_track
= FAT2CPU16(bs
->secs_track
);
765 bs
->heads
= FAT2CPU16(bs
->heads
);
766 bs
->total_sect
= FAT2CPU32(bs
->total_sect
);
769 if (bs
->fat_length
== 0) {
771 bs
->fat32_length
= FAT2CPU32(bs
->fat32_length
);
772 bs
->flags
= FAT2CPU16(bs
->flags
);
773 bs
->root_cluster
= FAT2CPU32(bs
->root_cluster
);
774 bs
->info_sector
= FAT2CPU16(bs
->info_sector
);
775 bs
->backup_boot
= FAT2CPU16(bs
->backup_boot
);
776 vistart
= (volume_info
*)(block
+ sizeof(boot_sector
));
779 vistart
= (volume_info
*)&(bs
->fat32_length
);
782 memcpy(volinfo
, vistart
, sizeof(volume_info
));
784 if (*fatsize
== 32) {
785 if (strncmp(FAT32_SIGN
, vistart
->fs_type
, SIGNLEN
) == 0)
788 if (strncmp(FAT12_SIGN
, vistart
->fs_type
, SIGNLEN
) == 0) {
792 if (strncmp(FAT16_SIGN
, vistart
->fs_type
, SIGNLEN
) == 0) {
798 debug("Error: broken fs_type sign\n");
806 __u8 do_fat_read_at_block
[MAX_CLUSTSIZE
]
807 __aligned(ARCH_DMA_MINALIGN
);
809 int do_fat_read_at(const char *filename
, loff_t pos
, void *buffer
,
810 loff_t maxsize
, int dols
, int dogetsize
, loff_t
*size
)
812 char fnamecopy
[2048];
816 fsdata
*mydata
= &datablock
;
817 dir_entry
*dentptr
= NULL
;
818 __u16 prevcksum
= 0xffff;
822 int files
= 0, dirs
= 0;
825 __u32 root_cluster
= 0;
826 int rootdir_size
= 0;
829 if (read_bootsectandvi(&bs
, &volinfo
, &mydata
->fatsize
)) {
830 debug("Error: reading boot sector\n");
834 if (mydata
->fatsize
== 32) {
835 root_cluster
= bs
.root_cluster
;
836 mydata
->fatlength
= bs
.fat32_length
;
838 mydata
->fatlength
= bs
.fat_length
;
841 mydata
->fat_sect
= bs
.reserved
;
843 cursect
= mydata
->rootdir_sect
844 = mydata
->fat_sect
+ mydata
->fatlength
* bs
.fats
;
846 mydata
->sect_size
= (bs
.sector_size
[1] << 8) + bs
.sector_size
[0];
847 mydata
->clust_size
= bs
.cluster_size
;
848 if (mydata
->sect_size
!= cur_part_info
.blksz
) {
849 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
850 mydata
->sect_size
, cur_part_info
.blksz
);
854 if (mydata
->fatsize
== 32) {
855 mydata
->data_begin
= mydata
->rootdir_sect
-
856 (mydata
->clust_size
* 2);
858 rootdir_size
= ((bs
.dir_entries
[1] * (int)256 +
862 mydata
->data_begin
= mydata
->rootdir_sect
+
864 (mydata
->clust_size
* 2);
867 mydata
->fatbufnum
= -1;
868 mydata
->fatbuf
= memalign(ARCH_DMA_MINALIGN
, FATBUFSIZE
);
869 if (mydata
->fatbuf
== NULL
) {
870 debug("Error: allocating memory\n");
875 debug("VFAT Support enabled\n");
877 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
878 mydata
->fatsize
, mydata
->fat_sect
, mydata
->fatlength
);
879 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
880 "Data begins at: %d\n",
882 mydata
->rootdir_sect
,
883 mydata
->rootdir_sect
* mydata
->sect_size
, mydata
->data_begin
);
884 debug("Sector size: %d, cluster size: %d\n", mydata
->sect_size
,
887 /* "cwd" is always the root... */
888 while (ISDIRDELIM(*filename
))
891 /* Make a copy of the filename and convert it to lowercase */
892 strcpy(fnamecopy
, filename
);
895 if (*fnamecopy
== '\0') {
900 } else if ((idx
= dirdelim(fnamecopy
)) >= 0) {
902 fnamecopy
[idx
] = '\0';
903 subname
= fnamecopy
+ idx
+ 1;
905 /* Handle multiple delimiters */
906 while (ISDIRDELIM(*subname
))
917 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
918 cursect
, mydata
->clust_size
, DIRENTSPERBLOCK
);
920 if (disk_read(cursect
,
921 (mydata
->fatsize
== 32) ?
922 (mydata
->clust_size
) :
924 do_fat_read_at_block
) < 0) {
925 debug("Error: reading rootdir block\n");
929 dentptr
= (dir_entry
*) do_fat_read_at_block
;
932 for (i
= 0; i
< DIRENTSPERBLOCK
; i
++) {
933 char s_name
[14], l_name
[VFAT_MAXLEN_BYTES
];
937 if (dentptr
->name
[0] == DELETED_FLAG
) {
943 csum
= mkcksum(dentptr
->name
, dentptr
->ext
);
945 if (dentptr
->attr
& ATTR_VOLUME
) {
947 (dentptr
->attr
& ATTR_VFAT
) == ATTR_VFAT
&&
948 (dentptr
->name
[0] & LAST_LONG_ENTRY_MASK
)) {
950 ((dir_slot
*)dentptr
)->alias_checksum
;
954 do_fat_read_at_block
,
957 if (dols
== LS_ROOT
) {
961 (dentptr
->attr
& ATTR_DIR
);
969 if (l_name
[0] != 0) {
976 printf(" %8u %s%c\n",
977 FAT2CPU32(dentptr
->size
),
989 debug("Rootvfatname: |%s|\n",
992 /* Volume label or VFAT entry */
996 } else if (dentptr
->name
[0] == 0) {
997 debug("RootDentname == NULL - %d\n", i
);
998 if (dols
== LS_ROOT
) {
999 printf("\n%d file(s), %d dir(s)\n\n",
1005 else if (vfat_enabled
&&
1006 dols
== LS_ROOT
&& csum
== prevcksum
) {
1012 get_name(dentptr
, s_name
);
1014 if (dols
== LS_ROOT
) {
1015 int isdir
= (dentptr
->attr
& ATTR_DIR
);
1021 if (s_name
[0] != 0) {
1027 if (s_name
[0] != 0) {
1034 printf(" %8u %s%c\n",
1035 FAT2CPU32(dentptr
->size
),
1046 if (strcmp(fnamecopy
, s_name
)
1047 && strcmp(fnamecopy
, l_name
)) {
1048 debug("RootMismatch: |%s|%s|\n", s_name
,
1054 if (isdir
&& !(dentptr
->attr
& ATTR_DIR
))
1057 debug("RootName: %s", s_name
);
1058 debug(", start: 0x%x", START(dentptr
));
1059 debug(", size: 0x%x %s\n",
1060 FAT2CPU32(dentptr
->size
),
1061 isdir
? "(DIR)" : "");
1063 goto rootdir_done
; /* We got a match */
1065 debug("END LOOP: j=%d clust_size=%d\n", j
,
1066 mydata
->clust_size
);
1069 * On FAT32 we must fetch the FAT entries for the next
1070 * root directory clusters when a cluster has been
1071 * completely processed.
1074 int rootdir_end
= 0;
1075 if (mydata
->fatsize
== 32) {
1076 if (j
== mydata
->clust_size
) {
1080 nxt_clust
= get_fatent(mydata
, root_cluster
);
1081 rootdir_end
= CHECK_CLUST(nxt_clust
, 32);
1083 nxtsect
= mydata
->data_begin
+
1084 (nxt_clust
* mydata
->clust_size
);
1086 root_cluster
= nxt_clust
;
1092 if (j
== PREFETCH_BLOCKS
)
1095 rootdir_end
= (++cursect
- mydata
->rootdir_sect
>=
1099 /* If end of rootdir reached */
1101 if (dols
== LS_ROOT
) {
1102 printf("\n%d file(s), %d dir(s)\n\n",
1114 int startsect
= mydata
->data_begin
1115 + START(dentptr
) * mydata
->clust_size
;
1117 char *nextname
= NULL
;
1122 idx
= dirdelim(subname
);
1125 subname
[idx
] = '\0';
1126 nextname
= subname
+ idx
+ 1;
1127 /* Handle multiple delimiters */
1128 while (ISDIRDELIM(*nextname
))
1130 if (dols
&& *nextname
== '\0')
1133 if (dols
&& firsttime
) {
1140 if (get_dentfromdir(mydata
, startsect
, subname
, dentptr
,
1141 isdir
? 0 : dols
) == NULL
) {
1147 if (isdir
&& !(dentptr
->attr
& ATTR_DIR
))
1155 *size
= FAT2CPU32(dentptr
->size
);
1158 ret
= get_contents(mydata
, dentptr
, pos
, buffer
, maxsize
, size
);
1160 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr
->size
), *size
);
1163 free(mydata
->fatbuf
);
1167 int do_fat_read(const char *filename
, void *buffer
, loff_t maxsize
, int dols
,
1170 return do_fat_read_at(filename
, 0, buffer
, maxsize
, dols
, 0, actread
);
1173 int file_fat_detectfs(void)
1176 volume_info volinfo
;
1180 if (cur_dev
== NULL
) {
1181 printf("No current device\n");
1185 #if defined(CONFIG_CMD_IDE) || \
1186 defined(CONFIG_CMD_SATA) || \
1187 defined(CONFIG_CMD_SCSI) || \
1188 defined(CONFIG_CMD_USB) || \
1190 printf("Interface: ");
1191 switch (cur_dev
->if_type
) {
1217 printf("\n Device %d: ", cur_dev
->dev
);
1221 if (read_bootsectandvi(&bs
, &volinfo
, &fatsize
)) {
1222 printf("\nNo valid FAT fs found\n");
1226 memcpy(vol_label
, volinfo
.volume_label
, 11);
1227 vol_label
[11] = '\0';
1228 volinfo
.fs_type
[5] = '\0';
1230 printf("Filesystem: %s \"%s\"\n", volinfo
.fs_type
, vol_label
);
1235 int file_fat_ls(const char *dir
)
1239 return do_fat_read(dir
, NULL
, 0, LS_YES
, &size
);
1242 int fat_exists(const char *filename
)
1247 ret
= do_fat_read_at(filename
, 0, NULL
, 0, LS_NO
, 1, &size
);
1251 int fat_size(const char *filename
, loff_t
*size
)
1253 return do_fat_read_at(filename
, 0, NULL
, 0, LS_NO
, 1, size
);
1256 int file_fat_read_at(const char *filename
, loff_t pos
, void *buffer
,
1257 loff_t maxsize
, loff_t
*actread
)
1259 printf("reading %s\n", filename
);
1260 return do_fat_read_at(filename
, pos
, buffer
, maxsize
, LS_NO
, 0,
1264 int file_fat_read(const char *filename
, void *buffer
, int maxsize
)
1269 ret
= file_fat_read_at(filename
, 0, buffer
, maxsize
, &actread
);
1276 int fat_read_file(const char *filename
, void *buf
, loff_t offset
, loff_t len
,
1281 ret
= file_fat_read_at(filename
, offset
, buf
, len
, actread
);
1283 printf("** Unable to read file %s **\n", filename
);
1288 void fat_close(void)