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+
17 #include <asm/byteorder.h>
21 #include <linux/compiler.h>
22 #include <linux/ctype.h>
24 #ifdef CONFIG_SUPPORT_VFAT
25 static const int vfat_enabled = 1;
27 static const int vfat_enabled = 0;
31 * Convert a string to lowercase.
33 static void downcase(char *str)
35 while (*str != '\0') {
41 static struct blk_desc *cur_dev;
42 static disk_partition_t cur_part_info;
44 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
45 #define DOS_FS_TYPE_OFFSET 0x36
46 #define DOS_FS32_TYPE_OFFSET 0x52
48 static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
55 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
57 if (nr_blocks && ret == 0)
63 int fat_set_blk_dev(struct blk_desc *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(struct blk_desc *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 (part_get_info(dev_desc, part_no, &info)) {
102 printf("** Partition %d not valid on device %d **\n",
103 part_no, dev_desc->devnum);
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 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
206 if (startblock + getsize > fatlength)
207 getsize = fatlength - startblock;
209 startblock += mydata->fat_sect; /* Offset from start of disk */
211 if (disk_read(startblock, getsize, bufptr) < 0) {
212 debug("Error reading FAT blocks\n");
215 mydata->fatbufnum = bufnum;
218 /* Get the actual entry from the table */
219 switch (mydata->fatsize) {
221 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
224 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
227 off16 = (offset * 3) / 4;
229 switch (offset & 0x3) {
231 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
235 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
237 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
239 ret = (val2 << 4) | (val1 >> 12);
242 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
244 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
246 ret = (val2 << 8) | (val1 >> 8);
249 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
250 ret = (ret & 0xfff0) >> 4;
257 debug("FAT%d: ret: %08x, offset: %04x\n",
258 mydata->fatsize, ret, offset);
264 * Read at most 'size' bytes from the specified cluster into 'buffer'.
265 * Return 0 on success, -1 otherwise.
268 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
275 startsect = mydata->data_begin +
276 clustnum * mydata->clust_size;
278 startsect = mydata->rootdir_sect;
281 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
283 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
284 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
286 printf("FAT: Misaligned buffer address (%p)\n", buffer);
288 while (size >= mydata->sect_size) {
289 ret = disk_read(startsect++, 1, tmpbuf);
291 debug("Error reading data (got %d)\n", ret);
295 memcpy(buffer, tmpbuf, mydata->sect_size);
296 buffer += mydata->sect_size;
297 size -= mydata->sect_size;
300 idx = size / mydata->sect_size;
301 ret = disk_read(startsect, idx, buffer);
303 debug("Error reading data (got %d)\n", ret);
307 idx *= mydata->sect_size;
312 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
314 ret = disk_read(startsect, 1, tmpbuf);
316 debug("Error reading data (got %d)\n", ret);
320 memcpy(buffer, tmpbuf, size);
327 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
329 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
331 __u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
332 __aligned(ARCH_DMA_MINALIGN);
334 static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
335 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
337 loff_t filesize = FAT2CPU32(dentptr->size);
338 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
339 __u32 curclust = START(dentptr);
340 __u32 endclust, newclust;
344 debug("Filesize: %llu bytes\n", filesize);
346 if (pos >= filesize) {
347 debug("Read position past EOF: %llu\n", pos);
351 if (maxsize > 0 && filesize > pos + maxsize)
352 filesize = pos + maxsize;
354 debug("%llu bytes\n", filesize);
356 actsize = bytesperclust;
358 /* go to cluster at pos */
359 while (actsize <= pos) {
360 curclust = get_fatent(mydata, curclust);
361 if (CHECK_CLUST(curclust, mydata->fatsize)) {
362 debug("curclust: 0x%x\n", curclust);
363 debug("Invalid FAT entry\n");
366 actsize += bytesperclust;
370 actsize -= bytesperclust;
374 /* align to beginning of next cluster if any */
376 actsize = min(filesize, (loff_t)bytesperclust);
377 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
378 (int)actsize) != 0) {
379 printf("Error reading cluster\n");
384 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
390 curclust = get_fatent(mydata, curclust);
391 if (CHECK_CLUST(curclust, mydata->fatsize)) {
392 debug("curclust: 0x%x\n", curclust);
393 debug("Invalid FAT entry\n");
398 actsize = bytesperclust;
402 /* search for consecutive clusters */
403 while (actsize < filesize) {
404 newclust = get_fatent(mydata, endclust);
405 if ((newclust - 1) != endclust)
407 if (CHECK_CLUST(newclust, mydata->fatsize)) {
408 debug("curclust: 0x%x\n", newclust);
409 debug("Invalid FAT entry\n");
413 actsize += bytesperclust;
416 /* get remaining bytes */
418 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
419 printf("Error reading cluster\n");
425 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
426 printf("Error reading cluster\n");
429 *gotsize += (int)actsize;
433 curclust = get_fatent(mydata, endclust);
434 if (CHECK_CLUST(curclust, mydata->fatsize)) {
435 debug("curclust: 0x%x\n", curclust);
436 printf("Invalid FAT entry\n");
439 actsize = bytesperclust;
445 * Extract the file name information from 'slotptr' into 'l_name',
446 * starting at l_name[*idx].
447 * Return 1 if terminator (zero byte) is found, 0 otherwise.
449 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
453 for (j = 0; j <= 8; j += 2) {
454 l_name[*idx] = slotptr->name0_4[j];
455 if (l_name[*idx] == 0x00)
459 for (j = 0; j <= 10; j += 2) {
460 l_name[*idx] = slotptr->name5_10[j];
461 if (l_name[*idx] == 0x00)
465 for (j = 0; j <= 2; j += 2) {
466 l_name[*idx] = slotptr->name11_12[j];
467 if (l_name[*idx] == 0x00)
476 * Extract the full long filename starting at 'retdent' (which is really
477 * a slot) into 'l_name'. If successful also copy the real directory entry
479 * Return 0 on success, -1 otherwise.
482 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
483 dir_entry *retdent, char *l_name)
486 dir_slot *slotptr = (dir_slot *)retdent;
487 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
490 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
493 if (counter > VFAT_MAXSEQ) {
494 debug("Error: VFAT name is too long\n");
498 while ((__u8 *)slotptr < buflimit) {
501 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
507 if ((__u8 *)slotptr >= buflimit) {
512 curclust = get_fatent(mydata, curclust);
513 if (CHECK_CLUST(curclust, mydata->fatsize)) {
514 debug("curclust: 0x%x\n", curclust);
515 printf("Invalid FAT entry\n");
519 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
520 mydata->clust_size * mydata->sect_size) != 0) {
521 debug("Error: reading directory block\n");
525 slotptr2 = (dir_slot *)get_contents_vfatname_block;
526 while (counter > 0) {
527 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
534 /* Save the real directory entry */
535 realdent = (dir_entry *)slotptr2;
536 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
538 slot2str(slotptr2, l_name, &idx);
541 /* Save the real directory entry */
542 realdent = (dir_entry *)slotptr;
547 if (slot2str(slotptr, l_name, &idx))
549 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
552 if (*l_name == DELETED_FLAG)
554 else if (*l_name == aRING)
555 *l_name = DELETED_FLAG;
558 /* Return the real directory entry */
559 memcpy(retdent, realdent, sizeof(dir_entry));
564 /* Calculate short name checksum */
565 static __u8 mkcksum(const char name[8], const char ext[3])
571 for (i = 0; i < 8; i++)
572 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
573 for (i = 0; i < 3; i++)
574 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
580 * Get the directory entry associated with 'filename' from the directory
581 * starting at 'startsect'
583 __u8 get_dentfromdir_block[MAX_CLUSTSIZE]
584 __aligned(ARCH_DMA_MINALIGN);
586 static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
587 char *filename, dir_entry *retdent,
590 __u16 prevcksum = 0xffff;
591 __u32 curclust = START(retdent);
592 int files = 0, dirs = 0;
594 debug("get_dentfromdir: %s\n", filename);
601 if (get_cluster(mydata, curclust, get_dentfromdir_block,
602 mydata->clust_size * mydata->sect_size) != 0) {
603 debug("Error: reading directory block\n");
607 dentptr = (dir_entry *)get_dentfromdir_block;
609 for (i = 0; i < DIRENTSPERCLUST; i++) {
610 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
613 if (dentptr->name[0] == DELETED_FLAG) {
617 if ((dentptr->attr & ATTR_VOLUME)) {
619 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
620 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
621 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
622 get_vfatname(mydata, curclust,
623 get_dentfromdir_block,
630 isdir = (dentptr->attr & ATTR_DIR);
638 if (l_name[0] != 0) {
645 printf(" %8u %s%c\n",
646 FAT2CPU32(dentptr->size),
658 debug("vfatname: |%s|\n", l_name);
660 /* Volume label or VFAT entry */
665 if (dentptr->name[0] == 0) {
667 printf("\n%d file(s), %d dir(s)\n\n",
670 debug("Dentname == NULL - %d\n", i);
674 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
675 if (dols && csum == prevcksum) {
682 get_name(dentptr, s_name);
684 int isdir = (dentptr->attr & ATTR_DIR);
694 if (s_name[0] != 0) {
702 printf(" %8u %s%c\n",
703 FAT2CPU32(dentptr->size),
715 if (strcmp(filename, s_name)
716 && strcmp(filename, l_name)) {
717 debug("Mismatch: |%s|%s|\n", s_name, l_name);
722 memcpy(retdent, dentptr, sizeof(dir_entry));
724 debug("DentName: %s", s_name);
725 debug(", start: 0x%x", START(dentptr));
726 debug(", size: 0x%x %s\n",
727 FAT2CPU32(dentptr->size),
728 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
733 curclust = get_fatent(mydata, curclust);
734 if (CHECK_CLUST(curclust, mydata->fatsize)) {
735 debug("curclust: 0x%x\n", curclust);
736 printf("Invalid FAT entry\n");
745 * Read boot sector and volume info from a FAT filesystem
748 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
751 volume_info *vistart;
754 if (cur_dev == NULL) {
755 debug("Error: no device selected\n");
759 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
761 debug("Error: allocating block\n");
765 if (disk_read(0, 1, block) < 0) {
766 debug("Error: reading block\n");
770 memcpy(bs, block, sizeof(boot_sector));
771 bs->reserved = FAT2CPU16(bs->reserved);
772 bs->fat_length = FAT2CPU16(bs->fat_length);
773 bs->secs_track = FAT2CPU16(bs->secs_track);
774 bs->heads = FAT2CPU16(bs->heads);
775 bs->total_sect = FAT2CPU32(bs->total_sect);
778 if (bs->fat_length == 0) {
780 bs->fat32_length = FAT2CPU32(bs->fat32_length);
781 bs->flags = FAT2CPU16(bs->flags);
782 bs->root_cluster = FAT2CPU32(bs->root_cluster);
783 bs->info_sector = FAT2CPU16(bs->info_sector);
784 bs->backup_boot = FAT2CPU16(bs->backup_boot);
785 vistart = (volume_info *)(block + sizeof(boot_sector));
788 vistart = (volume_info *)&(bs->fat32_length);
791 memcpy(volinfo, vistart, sizeof(volume_info));
793 if (*fatsize == 32) {
794 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
797 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
801 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
807 debug("Error: broken fs_type sign\n");
815 __u8 do_fat_read_at_block[MAX_CLUSTSIZE]
816 __aligned(ARCH_DMA_MINALIGN);
818 int do_fat_read_at(const char *filename, loff_t pos, void *buffer,
819 loff_t maxsize, int dols, int dogetsize, loff_t *size)
821 char fnamecopy[2048];
825 fsdata *mydata = &datablock;
826 dir_entry *dentptr = NULL;
827 __u16 prevcksum = 0xffff;
831 int files = 0, dirs = 0;
834 __u32 root_cluster = 0;
836 int rootdir_size = 0;
841 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
842 debug("Error: reading boot sector\n");
846 if (mydata->fatsize == 32) {
847 root_cluster = bs.root_cluster;
848 mydata->fatlength = bs.fat32_length;
850 mydata->fatlength = bs.fat_length;
853 mydata->fat_sect = bs.reserved;
855 cursect = mydata->rootdir_sect
856 = mydata->fat_sect + mydata->fatlength * bs.fats;
858 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
859 mydata->clust_size = bs.cluster_size;
860 if (mydata->sect_size != cur_part_info.blksz) {
861 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
862 mydata->sect_size, cur_part_info.blksz);
866 if (mydata->fatsize == 32) {
867 mydata->data_begin = mydata->rootdir_sect -
868 (mydata->clust_size * 2);
870 rootdir_size = ((bs.dir_entries[1] * (int)256 +
874 mydata->data_begin = mydata->rootdir_sect +
876 (mydata->clust_size * 2);
879 mydata->fatbufnum = -1;
880 mydata->fat_dirty = 0;
881 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
882 if (mydata->fatbuf == NULL) {
883 debug("Error: allocating memory\n");
888 debug("VFAT Support enabled\n");
890 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
891 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
892 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
893 "Data begins at: %d\n",
895 mydata->rootdir_sect,
896 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
897 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
900 /* "cwd" is always the root... */
901 while (ISDIRDELIM(*filename))
904 /* Make a copy of the filename and convert it to lowercase */
905 strcpy(fnamecopy, filename);
909 if (*fnamecopy == '\0') {
914 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
916 fnamecopy[idx] = '\0';
917 subname = fnamecopy + idx + 1;
919 /* Handle multiple delimiters */
920 while (ISDIRDELIM(*subname))
931 if (mydata->fatsize == 32 || firsttime) {
932 dir_ptr = do_fat_read_at_block;
936 * FAT16 sector buffer modification:
937 * Each loop, the second buffered block is moved to
938 * the buffer begin, and two next sectors are read
939 * next to the previously moved one. So the sector
940 * buffer keeps always 3 sectors for fat16.
941 * And the current sector is the buffer second sector
942 * beside the "firsttime" read, when it is the first one.
944 * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1]
945 * n = computed root dir sector
946 * loop | cursect-1 | cursect | cursect+1 |
947 * 0 | sector n+0 | sector n+1 | none |
948 * 1 | none | sector n+0 | sector n+1 |
949 * 0 | sector n+1 | sector n+2 | sector n+3 |
950 * 1 | sector n+3 | ...
952 dir_ptr = (do_fat_read_at_block + mydata->sect_size);
953 memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size);
958 if (mydata->fatsize == 32 && buffer_blk_cnt)
962 read_blk = (mydata->fatsize == 32) ?
963 mydata->clust_size : PREFETCH_BLOCKS;
965 debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
966 cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK);
968 if (disk_read(cursect, read_blk, dir_ptr) < 0) {
969 debug("Error: reading rootdir block\n");
973 dentptr = (dir_entry *)dir_ptr;
976 for (i = 0; i < DIRENTSPERBLOCK; i++) {
977 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
981 if (dentptr->name[0] == DELETED_FLAG) {
987 csum = mkcksum(dentptr->name, dentptr->ext);
989 if (dentptr->attr & ATTR_VOLUME) {
991 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
992 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
994 ((dir_slot *)dentptr)->alias_checksum;
1001 if (dols == LS_ROOT) {
1005 (dentptr->attr & ATTR_DIR);
1013 if (l_name[0] != 0) {
1020 printf(" %8u %s%c\n",
1021 FAT2CPU32(dentptr->size),
1033 debug("Rootvfatname: |%s|\n",
1036 /* Volume label or VFAT entry */
1040 } else if (dentptr->name[0] == 0) {
1041 debug("RootDentname == NULL - %d\n", i);
1042 if (dols == LS_ROOT) {
1043 printf("\n%d file(s), %d dir(s)\n\n",
1049 else if (vfat_enabled &&
1050 dols == LS_ROOT && csum == prevcksum) {
1056 get_name(dentptr, s_name);
1058 if (dols == LS_ROOT) {
1059 int isdir = (dentptr->attr & ATTR_DIR);
1065 if (s_name[0] != 0) {
1071 if (s_name[0] != 0) {
1078 printf(" %8u %s%c\n",
1079 FAT2CPU32(dentptr->size),
1090 if (strcmp(fnamecopy, s_name)
1091 && strcmp(fnamecopy, l_name)) {
1092 debug("RootMismatch: |%s|%s|\n", s_name,
1098 if (isdir && !(dentptr->attr & ATTR_DIR))
1101 debug("RootName: %s", s_name);
1102 debug(", start: 0x%x", START(dentptr));
1103 debug(", size: 0x%x %s\n",
1104 FAT2CPU32(dentptr->size),
1105 isdir ? "(DIR)" : "");
1107 goto rootdir_done; /* We got a match */
1109 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt,
1110 mydata->clust_size);
1113 * On FAT32 we must fetch the FAT entries for the next
1114 * root directory clusters when a cluster has been
1115 * completely processed.
1118 int rootdir_end = 0;
1119 if (mydata->fatsize == 32) {
1120 if (buffer_blk_cnt == mydata->clust_size) {
1124 nxt_clust = get_fatent(mydata, root_cluster);
1125 rootdir_end = CHECK_CLUST(nxt_clust, 32);
1127 nxtsect = mydata->data_begin +
1128 (nxt_clust * mydata->clust_size);
1130 root_cluster = nxt_clust;
1136 if (buffer_blk_cnt == PREFETCH_BLOCKS)
1139 rootdir_end = (++cursect - mydata->rootdir_sect >=
1143 /* If end of rootdir reached */
1145 if (dols == LS_ROOT) {
1146 printf("\n%d file(s), %d dir(s)\n\n",
1158 int startsect = mydata->data_begin
1159 + START(dentptr) * mydata->clust_size;
1161 char *nextname = NULL;
1166 idx = dirdelim(subname);
1169 subname[idx] = '\0';
1170 nextname = subname + idx + 1;
1171 /* Handle multiple delimiters */
1172 while (ISDIRDELIM(*nextname))
1174 if (dols && *nextname == '\0')
1177 if (dols && firsttime) {
1184 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1185 isdir ? 0 : dols) == NULL) {
1191 if (isdir && !(dentptr->attr & ATTR_DIR))
1195 * If we are looking for a directory, and found a directory
1196 * type entry, and the entry is for the root directory (as
1197 * denoted by a cluster number of 0), jump back to the start
1198 * of the function, since at least on FAT12/16, the root dir
1199 * lives in a hard-coded location and needs special handling
1200 * to parse, rather than simply following the cluster linked
1201 * list in the FAT, like other directories.
1203 if (isdir && (dentptr->attr & ATTR_DIR) && !START(dentptr)) {
1205 * Modify the filename to remove the prefix that gets
1206 * back to the root directory, so the initial root dir
1207 * parsing code can continue from where we are without
1210 strcpy(fnamecopy, nextname ?: "");
1212 * Set up state the same way as the function does when
1213 * first started. This is required for the root dir
1214 * parsing code operates in its expected environment.
1217 cursect = mydata->rootdir_sect;
1227 *size = FAT2CPU32(dentptr->size);
1230 ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size);
1232 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size);
1235 free(mydata->fatbuf);
1239 int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols,
1242 return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread);
1245 int file_fat_detectfs(void)
1248 volume_info volinfo;
1252 if (cur_dev == NULL) {
1253 printf("No current device\n");
1257 #if defined(CONFIG_CMD_IDE) || \
1258 defined(CONFIG_CMD_SATA) || \
1259 defined(CONFIG_SCSI) || \
1260 defined(CONFIG_CMD_USB) || \
1262 printf("Interface: ");
1263 switch (cur_dev->if_type) {
1289 printf("\n Device %d: ", cur_dev->devnum);
1293 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1294 printf("\nNo valid FAT fs found\n");
1298 memcpy(vol_label, volinfo.volume_label, 11);
1299 vol_label[11] = '\0';
1300 volinfo.fs_type[5] = '\0';
1302 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1307 int file_fat_ls(const char *dir)
1311 return do_fat_read(dir, NULL, 0, LS_YES, &size);
1314 int fat_exists(const char *filename)
1319 ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size);
1323 int fat_size(const char *filename, loff_t *size)
1325 return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
1328 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1329 loff_t maxsize, loff_t *actread)
1331 printf("reading %s\n", filename);
1332 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0,
1336 int file_fat_read(const char *filename, void *buffer, int maxsize)
1341 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1348 int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1353 ret = file_fat_read_at(filename, offset, buf, len, actread);
1355 printf("** Unable to read file %s **\n", filename);
1360 void fat_close(void)