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 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <asm/byteorder.h>
35 #include <linux/compiler.h>
38 * Convert a string to lowercase.
40 static void downcase (char *str
)
42 while (*str
!= '\0') {
48 static block_dev_desc_t
*cur_dev
;
49 static unsigned int cur_part_nr
;
50 static disk_partition_t cur_part_info
;
52 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
53 #define DOS_FS_TYPE_OFFSET 0x36
54 #define DOS_FS32_TYPE_OFFSET 0x52
56 static int disk_read(__u32 block
, __u32 nr_blocks
, void *buf
)
58 if (!cur_dev
|| !cur_dev
->block_read
)
61 return cur_dev
->block_read(cur_dev
->dev
,
62 cur_part_info
.start
+ block
, nr_blocks
, buf
);
65 int fat_register_device (block_dev_desc_t
* dev_desc
, int part_no
)
67 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer
, dev_desc
->blksz
);
69 /* First close any currently found FAT filesystem */
72 #if (defined(CONFIG_CMD_IDE) || \
73 defined(CONFIG_CMD_SATA) || \
74 defined(CONFIG_CMD_SCSI) || \
75 defined(CONFIG_CMD_USB) || \
76 defined(CONFIG_MMC) || \
77 defined(CONFIG_SYSTEMACE) )
79 /* Read the partition table, if present */
80 if (!get_partition_info(dev_desc
, part_no
, &cur_part_info
)) {
82 cur_part_nr
= part_no
;
86 /* Otherwise it might be a superfloppy (whole-disk FAT filesystem) */
89 printf("** Partition %d not valid on device %d **\n",
90 part_no
, dev_desc
->dev
);
96 cur_part_info
.start
= 0;
97 cur_part_info
.size
= dev_desc
->lba
;
98 cur_part_info
.blksz
= dev_desc
->blksz
;
99 memset(cur_part_info
.name
, 0, sizeof(cur_part_info
.name
));
100 memset(cur_part_info
.type
, 0, sizeof(cur_part_info
.type
));
103 /* Make sure it has a valid FAT header */
104 if (disk_read(0, 1, buffer
) != 1) {
109 /* Check if it's actually a DOS volume */
110 if (memcmp(buffer
+ DOS_BOOT_MAGIC_OFFSET
, "\x55\xAA", 2)) {
115 /* Check for FAT12/FAT16/FAT32 filesystem */
116 if (!memcmp(buffer
+ DOS_FS_TYPE_OFFSET
, "FAT", 3))
118 if (!memcmp(buffer
+ DOS_FS32_TYPE_OFFSET
, "FAT32", 5))
127 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
128 * Return index into string if found, -1 otherwise.
130 static int dirdelim (char *str
)
134 while (*str
!= '\0') {
135 if (ISDIRDELIM(*str
))
143 * Extract zero terminated short name from a directory entry.
145 static void get_name (dir_entry
*dirent
, char *s_name
)
149 memcpy(s_name
, dirent
->name
, 8);
152 while (*ptr
&& *ptr
!= ' ')
154 if (dirent
->ext
[0] && dirent
->ext
[0] != ' ') {
157 memcpy(ptr
, dirent
->ext
, 3);
159 while (*ptr
&& *ptr
!= ' ')
163 if (*s_name
== DELETED_FLAG
)
165 else if (*s_name
== aRING
)
166 *s_name
= DELETED_FLAG
;
171 * Get the entry at index 'entry' in a FAT (12/16/32) table.
172 * On failure 0x00 is returned.
174 static __u32
get_fatent (fsdata
*mydata
, __u32 entry
)
181 switch (mydata
->fatsize
) {
183 bufnum
= entry
/ FAT32BUFSIZE
;
184 offset
= entry
- bufnum
* FAT32BUFSIZE
;
187 bufnum
= entry
/ FAT16BUFSIZE
;
188 offset
= entry
- bufnum
* FAT16BUFSIZE
;
191 bufnum
= entry
/ FAT12BUFSIZE
;
192 offset
= entry
- bufnum
* FAT12BUFSIZE
;
196 /* Unsupported FAT size */
200 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
201 mydata
->fatsize
, entry
, entry
, offset
, offset
);
203 /* Read a new block of FAT entries into the cache. */
204 if (bufnum
!= mydata
->fatbufnum
) {
205 __u32 getsize
= FATBUFBLOCKS
;
206 __u8
*bufptr
= mydata
->fatbuf
;
207 __u32 fatlength
= mydata
->fatlength
;
208 __u32 startblock
= bufnum
* FATBUFBLOCKS
;
210 if (getsize
> fatlength
)
213 fatlength
*= mydata
->sect_size
; /* We want it in bytes now */
214 startblock
+= mydata
->fat_sect
; /* Offset from start of disk */
216 if (disk_read(startblock
, getsize
, bufptr
) < 0) {
217 debug("Error reading FAT blocks\n");
220 mydata
->fatbufnum
= bufnum
;
223 /* Get the actual entry from the table */
224 switch (mydata
->fatsize
) {
226 ret
= FAT2CPU32(((__u32
*) mydata
->fatbuf
)[offset
]);
229 ret
= FAT2CPU16(((__u16
*) mydata
->fatbuf
)[offset
]);
232 off16
= (offset
* 3) / 4;
234 switch (offset
& 0x3) {
236 ret
= FAT2CPU16(((__u16
*) mydata
->fatbuf
)[off16
]);
240 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
242 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+ 1]);
244 ret
= (val2
<< 4) | (val1
>> 12);
247 val1
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
249 val2
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
+ 1]);
251 ret
= (val2
<< 8) | (val1
>> 8);
254 ret
= FAT2CPU16(((__u16
*)mydata
->fatbuf
)[off16
]);
255 ret
= (ret
& 0xfff0) >> 4;
262 debug("FAT%d: ret: %08x, offset: %04x\n",
263 mydata
->fatsize
, ret
, offset
);
269 * Read at most 'size' bytes from the specified cluster into 'buffer'.
270 * Return 0 on success, -1 otherwise.
273 get_cluster (fsdata
*mydata
, __u32 clustnum
, __u8
*buffer
,
282 startsect
= mydata
->data_begin
+
283 clustnum
* mydata
->clust_size
;
285 startsect
= mydata
->rootdir_sect
;
288 debug("gc - clustnum: %d, startsect: %d\n", clustnum
, startsect
);
290 nr_sect
= size
/ mydata
->sect_size
;
291 ret
= disk_read(startsect
, nr_sect
, buffer
);
292 if (ret
!= nr_sect
) {
293 debug("Error reading data (got %d)\n", ret
);
296 if (size
% mydata
->sect_size
) {
297 ALLOC_CACHE_ALIGN_BUFFER(__u8
, tmpbuf
, mydata
->sect_size
);
299 idx
= size
/ mydata
->sect_size
;
300 ret
= disk_read(startsect
+ idx
, 1, tmpbuf
);
302 debug("Error reading data (got %d)\n", ret
);
305 buffer
+= idx
* mydata
->sect_size
;
307 memcpy(buffer
, tmpbuf
, size
% mydata
->sect_size
);
315 * Read at most 'maxsize' bytes from the file associated with 'dentptr'
317 * Return the number of bytes read or -1 on fatal errors.
320 get_contents (fsdata
*mydata
, dir_entry
*dentptr
, __u8
*buffer
,
321 unsigned long maxsize
)
323 unsigned long filesize
= FAT2CPU32(dentptr
->size
), gotsize
= 0;
324 unsigned int bytesperclust
= mydata
->clust_size
* mydata
->sect_size
;
325 __u32 curclust
= START(dentptr
);
326 __u32 endclust
, newclust
;
327 unsigned long actsize
;
329 debug("Filesize: %ld bytes\n", filesize
);
331 if (maxsize
> 0 && filesize
> maxsize
)
334 debug("%ld bytes\n", filesize
);
336 actsize
= bytesperclust
;
340 /* search for consecutive clusters */
341 while (actsize
< filesize
) {
342 newclust
= get_fatent(mydata
, endclust
);
343 if ((newclust
- 1) != endclust
)
345 if (CHECK_CLUST(newclust
, mydata
->fatsize
)) {
346 debug("curclust: 0x%x\n", newclust
);
347 debug("Invalid FAT entry\n");
351 actsize
+= bytesperclust
;
354 /* actsize >= file size */
355 actsize
-= bytesperclust
;
357 /* get remaining clusters */
358 if (get_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
359 printf("Error reading cluster\n");
363 /* get remaining bytes */
364 gotsize
+= (int)actsize
;
368 if (get_cluster(mydata
, endclust
, buffer
, (int)actsize
) != 0) {
369 printf("Error reading cluster\n");
375 if (get_cluster(mydata
, curclust
, buffer
, (int)actsize
) != 0) {
376 printf("Error reading cluster\n");
379 gotsize
+= (int)actsize
;
383 curclust
= get_fatent(mydata
, endclust
);
384 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
385 debug("curclust: 0x%x\n", curclust
);
386 printf("Invalid FAT entry\n");
389 actsize
= bytesperclust
;
394 #ifdef CONFIG_SUPPORT_VFAT
396 * Extract the file name information from 'slotptr' into 'l_name',
397 * starting at l_name[*idx].
398 * Return 1 if terminator (zero byte) is found, 0 otherwise.
400 static int slot2str (dir_slot
*slotptr
, char *l_name
, int *idx
)
404 for (j
= 0; j
<= 8; j
+= 2) {
405 l_name
[*idx
] = slotptr
->name0_4
[j
];
406 if (l_name
[*idx
] == 0x00)
410 for (j
= 0; j
<= 10; j
+= 2) {
411 l_name
[*idx
] = slotptr
->name5_10
[j
];
412 if (l_name
[*idx
] == 0x00)
416 for (j
= 0; j
<= 2; j
+= 2) {
417 l_name
[*idx
] = slotptr
->name11_12
[j
];
418 if (l_name
[*idx
] == 0x00)
427 * Extract the full long filename starting at 'retdent' (which is really
428 * a slot) into 'l_name'. If successful also copy the real directory entry
430 * Return 0 on success, -1 otherwise.
432 __u8 get_vfatname_block
[MAX_CLUSTSIZE
]
433 __aligned(ARCH_DMA_MINALIGN
);
436 get_vfatname (fsdata
*mydata
, int curclust
, __u8
*cluster
,
437 dir_entry
*retdent
, char *l_name
)
440 dir_slot
*slotptr
= (dir_slot
*)retdent
;
441 __u8
*buflimit
= cluster
+ mydata
->sect_size
* ((curclust
== 0) ?
444 __u8 counter
= (slotptr
->id
& ~LAST_LONG_ENTRY_MASK
) & 0xff;
447 if (counter
> VFAT_MAXSEQ
) {
448 debug("Error: VFAT name is too long\n");
452 while ((__u8
*)slotptr
< buflimit
) {
455 if (((slotptr
->id
& ~LAST_LONG_ENTRY_MASK
) & 0xff) != counter
)
461 if ((__u8
*)slotptr
>= buflimit
) {
466 curclust
= get_fatent(mydata
, curclust
);
467 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
468 debug("curclust: 0x%x\n", curclust
);
469 printf("Invalid FAT entry\n");
473 if (get_cluster(mydata
, curclust
, get_vfatname_block
,
474 mydata
->clust_size
* mydata
->sect_size
) != 0) {
475 debug("Error: reading directory block\n");
479 slotptr2
= (dir_slot
*)get_vfatname_block
;
480 while (counter
> 0) {
481 if (((slotptr2
->id
& ~LAST_LONG_ENTRY_MASK
)
488 /* Save the real directory entry */
489 realdent
= (dir_entry
*)slotptr2
;
490 while ((__u8
*)slotptr2
> get_vfatname_block
) {
492 slot2str(slotptr2
, l_name
, &idx
);
495 /* Save the real directory entry */
496 realdent
= (dir_entry
*)slotptr
;
501 if (slot2str(slotptr
, l_name
, &idx
))
503 } while (!(slotptr
->id
& LAST_LONG_ENTRY_MASK
));
506 if (*l_name
== DELETED_FLAG
)
508 else if (*l_name
== aRING
)
509 *l_name
= DELETED_FLAG
;
512 /* Return the real directory entry */
513 memcpy(retdent
, realdent
, sizeof(dir_entry
));
518 /* Calculate short name checksum */
519 static __u8
mkcksum (const char *str
)
525 for (i
= 0; i
< 11; i
++) {
526 ret
= (((ret
& 1) << 7) | ((ret
& 0xfe) >> 1)) + str
[i
];
531 #endif /* CONFIG_SUPPORT_VFAT */
534 * Get the directory entry associated with 'filename' from the directory
535 * starting at 'startsect'
537 __u8 get_dentfromdir_block
[MAX_CLUSTSIZE
]
538 __aligned(ARCH_DMA_MINALIGN
);
540 static dir_entry
*get_dentfromdir (fsdata
*mydata
, int startsect
,
541 char *filename
, dir_entry
*retdent
,
544 __u16 prevcksum
= 0xffff;
545 __u32 curclust
= START(retdent
);
546 int files
= 0, dirs
= 0;
548 debug("get_dentfromdir: %s\n", filename
);
555 if (get_cluster(mydata
, curclust
, get_dentfromdir_block
,
556 mydata
->clust_size
* mydata
->sect_size
) != 0) {
557 debug("Error: reading directory block\n");
561 dentptr
= (dir_entry
*)get_dentfromdir_block
;
563 for (i
= 0; i
< DIRENTSPERCLUST
; i
++) {
564 char s_name
[14], l_name
[VFAT_MAXLEN_BYTES
];
567 if (dentptr
->name
[0] == DELETED_FLAG
) {
571 if ((dentptr
->attr
& ATTR_VOLUME
)) {
572 #ifdef CONFIG_SUPPORT_VFAT
573 if ((dentptr
->attr
& ATTR_VFAT
) == ATTR_VFAT
&&
574 (dentptr
->name
[0] & LAST_LONG_ENTRY_MASK
)) {
575 prevcksum
= ((dir_slot
*)dentptr
)->alias_checksum
;
576 get_vfatname(mydata
, curclust
,
577 get_dentfromdir_block
,
584 isdir
= (dentptr
->attr
& ATTR_DIR
);
592 if (l_name
[0] != 0) {
599 printf(" %8ld %s%c\n",
600 (long)FAT2CPU32(dentptr
->size
),
612 debug("vfatname: |%s|\n", l_name
);
616 /* Volume label or VFAT entry */
621 if (dentptr
->name
[0] == 0) {
623 printf("\n%d file(s), %d dir(s)\n\n",
626 debug("Dentname == NULL - %d\n", i
);
629 #ifdef CONFIG_SUPPORT_VFAT
630 if (dols
&& mkcksum(dentptr
->name
) == prevcksum
) {
636 get_name(dentptr
, s_name
);
638 int isdir
= (dentptr
->attr
& ATTR_DIR
);
648 if (s_name
[0] != 0) {
656 printf(" %8ld %s%c\n",
657 (long)FAT2CPU32(dentptr
->size
),
669 if (strcmp(filename
, s_name
)
670 && strcmp(filename
, l_name
)) {
671 debug("Mismatch: |%s|%s|\n", s_name
, l_name
);
676 memcpy(retdent
, dentptr
, sizeof(dir_entry
));
678 debug("DentName: %s", s_name
);
679 debug(", start: 0x%x", START(dentptr
));
680 debug(", size: 0x%x %s\n",
681 FAT2CPU32(dentptr
->size
),
682 (dentptr
->attr
& ATTR_DIR
) ? "(DIR)" : "");
687 curclust
= get_fatent(mydata
, curclust
);
688 if (CHECK_CLUST(curclust
, mydata
->fatsize
)) {
689 debug("curclust: 0x%x\n", curclust
);
690 printf("Invalid FAT entry\n");
699 * Read boot sector and volume info from a FAT filesystem
702 read_bootsectandvi (boot_sector
*bs
, volume_info
*volinfo
, int *fatsize
)
705 volume_info
*vistart
;
708 if (cur_dev
== NULL
) {
709 debug("Error: no device selected\n");
713 block
= memalign(ARCH_DMA_MINALIGN
, cur_dev
->blksz
);
715 debug("Error: allocating block\n");
719 if (disk_read (0, 1, block
) < 0) {
720 debug("Error: reading block\n");
724 memcpy(bs
, block
, sizeof(boot_sector
));
725 bs
->reserved
= FAT2CPU16(bs
->reserved
);
726 bs
->fat_length
= FAT2CPU16(bs
->fat_length
);
727 bs
->secs_track
= FAT2CPU16(bs
->secs_track
);
728 bs
->heads
= FAT2CPU16(bs
->heads
);
729 bs
->total_sect
= FAT2CPU32(bs
->total_sect
);
732 if (bs
->fat_length
== 0) {
734 bs
->fat32_length
= FAT2CPU32(bs
->fat32_length
);
735 bs
->flags
= FAT2CPU16(bs
->flags
);
736 bs
->root_cluster
= FAT2CPU32(bs
->root_cluster
);
737 bs
->info_sector
= FAT2CPU16(bs
->info_sector
);
738 bs
->backup_boot
= FAT2CPU16(bs
->backup_boot
);
739 vistart
= (volume_info
*)(block
+ sizeof(boot_sector
));
742 vistart
= (volume_info
*)&(bs
->fat32_length
);
745 memcpy(volinfo
, vistart
, sizeof(volume_info
));
747 if (*fatsize
== 32) {
748 if (strncmp(FAT32_SIGN
, vistart
->fs_type
, SIGNLEN
) == 0)
751 if (strncmp(FAT12_SIGN
, vistart
->fs_type
, SIGNLEN
) == 0) {
755 if (strncmp(FAT16_SIGN
, vistart
->fs_type
, SIGNLEN
) == 0) {
761 debug("Error: broken fs_type sign\n");
769 __u8 do_fat_read_block
[MAX_CLUSTSIZE
]
770 __aligned(ARCH_DMA_MINALIGN
);
773 do_fat_read (const char *filename
, void *buffer
, unsigned long maxsize
,
776 char fnamecopy
[2048];
780 fsdata
*mydata
= &datablock
;
782 __u16 prevcksum
= 0xffff;
786 int files
= 0, dirs
= 0;
789 __u32 root_cluster
= 0;
790 int rootdir_size
= 0;
793 if (read_bootsectandvi(&bs
, &volinfo
, &mydata
->fatsize
)) {
794 debug("Error: reading boot sector\n");
798 if (mydata
->fatsize
== 32) {
799 root_cluster
= bs
.root_cluster
;
800 mydata
->fatlength
= bs
.fat32_length
;
802 mydata
->fatlength
= bs
.fat_length
;
805 mydata
->fat_sect
= bs
.reserved
;
807 cursect
= mydata
->rootdir_sect
808 = mydata
->fat_sect
+ mydata
->fatlength
* bs
.fats
;
810 mydata
->sect_size
= (bs
.sector_size
[1] << 8) + bs
.sector_size
[0];
811 mydata
->clust_size
= bs
.cluster_size
;
812 if (mydata
->sect_size
!= cur_part_info
.blksz
) {
813 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
814 mydata
->sect_size
, cur_part_info
.blksz
);
818 if (mydata
->fatsize
== 32) {
819 mydata
->data_begin
= mydata
->rootdir_sect
-
820 (mydata
->clust_size
* 2);
822 rootdir_size
= ((bs
.dir_entries
[1] * (int)256 +
826 mydata
->data_begin
= mydata
->rootdir_sect
+
828 (mydata
->clust_size
* 2);
831 mydata
->fatbufnum
= -1;
832 mydata
->fatbuf
= memalign(ARCH_DMA_MINALIGN
, FATBUFSIZE
);
833 if (mydata
->fatbuf
== NULL
) {
834 debug("Error: allocating memory\n");
838 #ifdef CONFIG_SUPPORT_VFAT
839 debug("VFAT Support enabled\n");
841 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
842 mydata
->fatsize
, mydata
->fat_sect
, mydata
->fatlength
);
843 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
844 "Data begins at: %d\n",
846 mydata
->rootdir_sect
,
847 mydata
->rootdir_sect
* mydata
->sect_size
, mydata
->data_begin
);
848 debug("Sector size: %d, cluster size: %d\n", mydata
->sect_size
,
851 /* "cwd" is always the root... */
852 while (ISDIRDELIM(*filename
))
855 /* Make a copy of the filename and convert it to lowercase */
856 strcpy(fnamecopy
, filename
);
859 if (*fnamecopy
== '\0') {
864 } else if ((idx
= dirdelim(fnamecopy
)) >= 0) {
866 fnamecopy
[idx
] = '\0';
867 subname
= fnamecopy
+ idx
+ 1;
869 /* Handle multiple delimiters */
870 while (ISDIRDELIM(*subname
))
880 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
881 cursect
, mydata
->clust_size
, DIRENTSPERBLOCK
);
883 if (disk_read(cursect
,
884 (mydata
->fatsize
== 32) ?
885 (mydata
->clust_size
) :
887 do_fat_read_block
) < 0) {
888 debug("Error: reading rootdir block\n");
892 dentptr
= (dir_entry
*) do_fat_read_block
;
894 for (i
= 0; i
< DIRENTSPERBLOCK
; i
++) {
895 char s_name
[14], l_name
[VFAT_MAXLEN_BYTES
];
898 if (dentptr
->name
[0] == DELETED_FLAG
) {
902 if ((dentptr
->attr
& ATTR_VOLUME
)) {
903 #ifdef CONFIG_SUPPORT_VFAT
904 if ((dentptr
->attr
& ATTR_VFAT
) == ATTR_VFAT
&&
905 (dentptr
->name
[0] & LAST_LONG_ENTRY_MASK
)) {
907 ((dir_slot
*)dentptr
)->alias_checksum
;
914 if (dols
== LS_ROOT
) {
918 (dentptr
->attr
& ATTR_DIR
);
926 if (l_name
[0] != 0) {
933 printf(" %8ld %s%c\n",
934 (long)FAT2CPU32(dentptr
->size
),
946 debug("Rootvfatname: |%s|\n",
951 /* Volume label or VFAT entry */
955 } else if (dentptr
->name
[0] == 0) {
956 debug("RootDentname == NULL - %d\n", i
);
957 if (dols
== LS_ROOT
) {
958 printf("\n%d file(s), %d dir(s)\n\n",
964 #ifdef CONFIG_SUPPORT_VFAT
965 else if (dols
== LS_ROOT
&&
966 mkcksum(dentptr
->name
) == prevcksum
) {
972 get_name(dentptr
, s_name
);
974 if (dols
== LS_ROOT
) {
975 int isdir
= (dentptr
->attr
& ATTR_DIR
);
981 if (s_name
[0] != 0) {
987 if (s_name
[0] != 0) {
994 printf(" %8ld %s%c\n",
995 (long)FAT2CPU32(dentptr
->size
),
1006 if (strcmp(fnamecopy
, s_name
)
1007 && strcmp(fnamecopy
, l_name
)) {
1008 debug("RootMismatch: |%s|%s|\n", s_name
,
1014 if (isdir
&& !(dentptr
->attr
& ATTR_DIR
))
1017 debug("RootName: %s", s_name
);
1018 debug(", start: 0x%x", START(dentptr
));
1019 debug(", size: 0x%x %s\n",
1020 FAT2CPU32(dentptr
->size
),
1021 isdir
? "(DIR)" : "");
1023 goto rootdir_done
; /* We got a match */
1025 debug("END LOOP: j=%d clust_size=%d\n", j
,
1026 mydata
->clust_size
);
1029 * On FAT32 we must fetch the FAT entries for the next
1030 * root directory clusters when a cluster has been
1031 * completely processed.
1035 if ((mydata
->fatsize
== 32) && (j
== mydata
->clust_size
)) {
1039 nxt_clust
= get_fatent(mydata
, root_cluster
);
1040 fat32_end
= CHECK_CLUST(nxt_clust
, 32);
1042 nxtsect
= mydata
->data_begin
+
1043 (nxt_clust
* mydata
->clust_size
);
1045 root_cluster
= nxt_clust
;
1053 /* If end of rootdir reached */
1054 if ((mydata
->fatsize
== 32 && fat32_end
) ||
1055 (mydata
->fatsize
!= 32 && j
== rootdir_size
)) {
1056 if (dols
== LS_ROOT
) {
1057 printf("\n%d file(s), %d dir(s)\n\n",
1069 int startsect
= mydata
->data_begin
1070 + START(dentptr
) * mydata
->clust_size
;
1072 char *nextname
= NULL
;
1077 idx
= dirdelim(subname
);
1080 subname
[idx
] = '\0';
1081 nextname
= subname
+ idx
+ 1;
1082 /* Handle multiple delimiters */
1083 while (ISDIRDELIM(*nextname
))
1085 if (dols
&& *nextname
== '\0')
1088 if (dols
&& firsttime
) {
1095 if (get_dentfromdir(mydata
, startsect
, subname
, dentptr
,
1096 isdir
? 0 : dols
) == NULL
) {
1103 if (!(dentptr
->attr
& ATTR_DIR
))
1109 ret
= get_contents(mydata
, dentptr
, buffer
, maxsize
);
1110 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr
->size
), ret
);
1113 free(mydata
->fatbuf
);
1117 int file_fat_detectfs (void)
1120 volume_info volinfo
;
1124 if (cur_dev
== NULL
) {
1125 printf("No current device\n");
1129 #if defined(CONFIG_CMD_IDE) || \
1130 defined(CONFIG_CMD_SATA) || \
1131 defined(CONFIG_CMD_SCSI) || \
1132 defined(CONFIG_CMD_USB) || \
1134 printf("Interface: ");
1135 switch (cur_dev
->if_type
) {
1161 printf("\n Device %d: ", cur_dev
->dev
);
1165 if (read_bootsectandvi(&bs
, &volinfo
, &fatsize
)) {
1166 printf("\nNo valid FAT fs found\n");
1170 memcpy(vol_label
, volinfo
.volume_label
, 11);
1171 vol_label
[11] = '\0';
1172 volinfo
.fs_type
[5] = '\0';
1174 printf("Partition %d: Filesystem: %s \"%s\"\n", cur_part_nr
,
1175 volinfo
.fs_type
, vol_label
);
1180 int file_fat_ls (const char *dir
)
1182 return do_fat_read(dir
, NULL
, 0, LS_YES
);
1185 long file_fat_read (const char *filename
, void *buffer
, unsigned long maxsize
)
1187 printf("reading %s\n", filename
);
1188 return do_fat_read(filename
, buffer
, maxsize
, LS_NO
);