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);
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 #if CONFIG_IS_ENABLED(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;
165 static int flush_dirty_fat_buffer(fsdata *mydata);
166 #if !defined(CONFIG_FAT_WRITE)
167 /* Stub for read only operation */
168 int flush_dirty_fat_buffer(fsdata *mydata)
176 * Get the entry at index 'entry' in a FAT (12/16/32) table.
177 * On failure 0x00 is returned.
179 static __u32 get_fatent(fsdata *mydata, __u32 entry)
185 if (CHECK_CLUST(entry, mydata->fatsize)) {
186 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
190 switch (mydata->fatsize) {
192 bufnum = entry / FAT32BUFSIZE;
193 offset = entry - bufnum * FAT32BUFSIZE;
196 bufnum = entry / FAT16BUFSIZE;
197 offset = entry - bufnum * FAT16BUFSIZE;
200 bufnum = entry / FAT12BUFSIZE;
201 offset = entry - bufnum * FAT12BUFSIZE;
205 /* Unsupported FAT size */
209 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
210 mydata->fatsize, entry, entry, offset, offset);
212 /* Read a new block of FAT entries into the cache. */
213 if (bufnum != mydata->fatbufnum) {
214 __u32 getsize = FATBUFBLOCKS;
215 __u8 *bufptr = mydata->fatbuf;
216 __u32 fatlength = mydata->fatlength;
217 __u32 startblock = bufnum * FATBUFBLOCKS;
219 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
220 if (startblock + getsize > fatlength)
221 getsize = fatlength - startblock;
223 startblock += mydata->fat_sect; /* Offset from start of disk */
225 /* Write back the fatbuf to the disk */
226 if (flush_dirty_fat_buffer(mydata) < 0)
229 if (disk_read(startblock, getsize, bufptr) < 0) {
230 debug("Error reading FAT blocks\n");
233 mydata->fatbufnum = bufnum;
236 /* Get the actual entry from the table */
237 switch (mydata->fatsize) {
239 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
242 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
245 off8 = (offset * 3) / 2;
246 /* fatbut + off8 may be unaligned, read in byte granularity */
247 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
253 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
254 mydata->fatsize, ret, entry, offset);
260 * Read at most 'size' bytes from the specified cluster into 'buffer'.
261 * Return 0 on success, -1 otherwise.
264 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
271 startsect = mydata->data_begin +
272 clustnum * mydata->clust_size;
274 startsect = mydata->rootdir_sect;
277 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
279 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
280 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
282 printf("FAT: Misaligned buffer address (%p)\n", buffer);
284 while (size >= mydata->sect_size) {
285 ret = disk_read(startsect++, 1, tmpbuf);
287 debug("Error reading data (got %d)\n", ret);
291 memcpy(buffer, tmpbuf, mydata->sect_size);
292 buffer += mydata->sect_size;
293 size -= mydata->sect_size;
296 idx = size / mydata->sect_size;
297 ret = disk_read(startsect, idx, buffer);
299 debug("Error reading data (got %d)\n", ret);
303 idx *= mydata->sect_size;
308 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
310 ret = disk_read(startsect, 1, tmpbuf);
312 debug("Error reading data (got %d)\n", ret);
316 memcpy(buffer, tmpbuf, size);
323 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
325 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
327 __u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
328 __aligned(ARCH_DMA_MINALIGN);
330 static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
331 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
333 loff_t filesize = FAT2CPU32(dentptr->size);
334 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
335 __u32 curclust = START(dentptr);
336 __u32 endclust, newclust;
340 debug("Filesize: %llu bytes\n", filesize);
342 if (pos >= filesize) {
343 debug("Read position past EOF: %llu\n", pos);
347 if (maxsize > 0 && filesize > pos + maxsize)
348 filesize = pos + maxsize;
350 debug("%llu bytes\n", filesize);
352 actsize = bytesperclust;
354 /* go to cluster at pos */
355 while (actsize <= pos) {
356 curclust = get_fatent(mydata, curclust);
357 if (CHECK_CLUST(curclust, mydata->fatsize)) {
358 debug("curclust: 0x%x\n", curclust);
359 debug("Invalid FAT entry\n");
362 actsize += bytesperclust;
366 actsize -= bytesperclust;
370 /* align to beginning of next cluster if any */
372 actsize = min(filesize, (loff_t)bytesperclust);
373 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
374 (int)actsize) != 0) {
375 printf("Error reading cluster\n");
380 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
386 curclust = get_fatent(mydata, curclust);
387 if (CHECK_CLUST(curclust, mydata->fatsize)) {
388 debug("curclust: 0x%x\n", curclust);
389 debug("Invalid FAT entry\n");
394 actsize = bytesperclust;
398 /* search for consecutive clusters */
399 while (actsize < filesize) {
400 newclust = get_fatent(mydata, endclust);
401 if ((newclust - 1) != endclust)
403 if (CHECK_CLUST(newclust, mydata->fatsize)) {
404 debug("curclust: 0x%x\n", newclust);
405 debug("Invalid FAT entry\n");
409 actsize += bytesperclust;
412 /* get remaining bytes */
414 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
415 printf("Error reading cluster\n");
421 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
422 printf("Error reading cluster\n");
425 *gotsize += (int)actsize;
429 curclust = get_fatent(mydata, endclust);
430 if (CHECK_CLUST(curclust, mydata->fatsize)) {
431 debug("curclust: 0x%x\n", curclust);
432 printf("Invalid FAT entry\n");
435 actsize = bytesperclust;
441 * Extract the file name information from 'slotptr' into 'l_name',
442 * starting at l_name[*idx].
443 * Return 1 if terminator (zero byte) is found, 0 otherwise.
445 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
449 for (j = 0; j <= 8; j += 2) {
450 l_name[*idx] = slotptr->name0_4[j];
451 if (l_name[*idx] == 0x00)
455 for (j = 0; j <= 10; j += 2) {
456 l_name[*idx] = slotptr->name5_10[j];
457 if (l_name[*idx] == 0x00)
461 for (j = 0; j <= 2; j += 2) {
462 l_name[*idx] = slotptr->name11_12[j];
463 if (l_name[*idx] == 0x00)
472 * Extract the full long filename starting at 'retdent' (which is really
473 * a slot) into 'l_name'. If successful also copy the real directory entry
475 * Return 0 on success, -1 otherwise.
478 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
479 dir_entry *retdent, char *l_name)
482 dir_slot *slotptr = (dir_slot *)retdent;
483 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
486 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
489 if (counter > VFAT_MAXSEQ) {
490 debug("Error: VFAT name is too long\n");
494 while ((__u8 *)slotptr < buflimit) {
497 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
503 if ((__u8 *)slotptr >= buflimit) {
508 curclust = get_fatent(mydata, curclust);
509 if (CHECK_CLUST(curclust, mydata->fatsize)) {
510 debug("curclust: 0x%x\n", curclust);
511 printf("Invalid FAT entry\n");
515 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
516 mydata->clust_size * mydata->sect_size) != 0) {
517 debug("Error: reading directory block\n");
521 slotptr2 = (dir_slot *)get_contents_vfatname_block;
522 while (counter > 0) {
523 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
530 /* Save the real directory entry */
531 realdent = (dir_entry *)slotptr2;
532 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
534 slot2str(slotptr2, l_name, &idx);
537 /* Save the real directory entry */
538 realdent = (dir_entry *)slotptr;
543 if (slot2str(slotptr, l_name, &idx))
545 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
548 if (*l_name == DELETED_FLAG)
550 else if (*l_name == aRING)
551 *l_name = DELETED_FLAG;
554 /* Return the real directory entry */
555 memcpy(retdent, realdent, sizeof(dir_entry));
560 /* Calculate short name checksum */
561 static __u8 mkcksum(const char name[8], const char ext[3])
567 for (i = 0; i < 8; i++)
568 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
569 for (i = 0; i < 3; i++)
570 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
576 * Get the directory entry associated with 'filename' from the directory
577 * starting at 'startsect'
579 __u8 get_dentfromdir_block[MAX_CLUSTSIZE]
580 __aligned(ARCH_DMA_MINALIGN);
582 static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
583 char *filename, dir_entry *retdent,
586 __u16 prevcksum = 0xffff;
587 __u32 curclust = START(retdent);
588 int files = 0, dirs = 0;
590 debug("get_dentfromdir: %s\n", filename);
597 if (get_cluster(mydata, curclust, get_dentfromdir_block,
598 mydata->clust_size * mydata->sect_size) != 0) {
599 debug("Error: reading directory block\n");
603 dentptr = (dir_entry *)get_dentfromdir_block;
605 for (i = 0; i < DIRENTSPERCLUST; i++) {
606 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
609 if (dentptr->name[0] == DELETED_FLAG) {
613 if ((dentptr->attr & ATTR_VOLUME)) {
615 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
616 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
617 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
618 get_vfatname(mydata, curclust,
619 get_dentfromdir_block,
626 isdir = (dentptr->attr & ATTR_DIR);
634 if (l_name[0] != 0) {
641 printf(" %8u %s%c\n",
642 FAT2CPU32(dentptr->size),
654 debug("vfatname: |%s|\n", l_name);
656 /* Volume label or VFAT entry */
661 if (dentptr->name[0] == 0) {
663 printf("\n%d file(s), %d dir(s)\n\n",
666 debug("Dentname == NULL - %d\n", i);
670 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
671 if (dols && csum == prevcksum) {
678 get_name(dentptr, s_name);
680 int isdir = (dentptr->attr & ATTR_DIR);
690 if (s_name[0] != 0) {
698 printf(" %8u %s%c\n",
699 FAT2CPU32(dentptr->size),
711 if (strcmp(filename, s_name)
712 && strcmp(filename, l_name)) {
713 debug("Mismatch: |%s|%s|\n", s_name, l_name);
718 memcpy(retdent, dentptr, sizeof(dir_entry));
720 debug("DentName: %s", s_name);
721 debug(", start: 0x%x", START(dentptr));
722 debug(", size: 0x%x %s\n",
723 FAT2CPU32(dentptr->size),
724 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
729 curclust = get_fatent(mydata, curclust);
730 if (CHECK_CLUST(curclust, mydata->fatsize)) {
731 debug("curclust: 0x%x\n", curclust);
732 printf("Invalid FAT entry\n");
741 * Read boot sector and volume info from a FAT filesystem
744 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
747 volume_info *vistart;
750 if (cur_dev == NULL) {
751 debug("Error: no device selected\n");
755 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
757 debug("Error: allocating block\n");
761 if (disk_read(0, 1, block) < 0) {
762 debug("Error: reading block\n");
766 memcpy(bs, block, sizeof(boot_sector));
767 bs->reserved = FAT2CPU16(bs->reserved);
768 bs->fat_length = FAT2CPU16(bs->fat_length);
769 bs->secs_track = FAT2CPU16(bs->secs_track);
770 bs->heads = FAT2CPU16(bs->heads);
771 bs->total_sect = FAT2CPU32(bs->total_sect);
774 if (bs->fat_length == 0) {
776 bs->fat32_length = FAT2CPU32(bs->fat32_length);
777 bs->flags = FAT2CPU16(bs->flags);
778 bs->root_cluster = FAT2CPU32(bs->root_cluster);
779 bs->info_sector = FAT2CPU16(bs->info_sector);
780 bs->backup_boot = FAT2CPU16(bs->backup_boot);
781 vistart = (volume_info *)(block + sizeof(boot_sector));
784 vistart = (volume_info *)&(bs->fat32_length);
787 memcpy(volinfo, vistart, sizeof(volume_info));
789 if (*fatsize == 32) {
790 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
793 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
797 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
803 debug("Error: broken fs_type sign\n");
811 __u8 do_fat_read_at_block[MAX_CLUSTSIZE]
812 __aligned(ARCH_DMA_MINALIGN);
814 int do_fat_read_at(const char *filename, loff_t pos, void *buffer,
815 loff_t maxsize, int dols, int dogetsize, loff_t *size)
817 char fnamecopy[2048];
821 fsdata *mydata = &datablock;
822 dir_entry *dentptr = NULL;
823 __u16 prevcksum = 0xffff;
827 int files = 0, dirs = 0;
830 __u32 root_cluster = 0;
832 int rootdir_size = 0;
837 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
838 debug("Error: reading boot sector\n");
842 if (mydata->fatsize == 32) {
843 root_cluster = bs.root_cluster;
844 mydata->fatlength = bs.fat32_length;
846 mydata->fatlength = bs.fat_length;
849 mydata->fat_sect = bs.reserved;
851 cursect = mydata->rootdir_sect
852 = mydata->fat_sect + mydata->fatlength * bs.fats;
854 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
855 mydata->clust_size = bs.cluster_size;
856 if (mydata->sect_size != cur_part_info.blksz) {
857 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
858 mydata->sect_size, cur_part_info.blksz);
862 if (mydata->fatsize == 32) {
863 mydata->data_begin = mydata->rootdir_sect -
864 (mydata->clust_size * 2);
866 rootdir_size = ((bs.dir_entries[1] * (int)256 +
870 mydata->data_begin = mydata->rootdir_sect +
872 (mydata->clust_size * 2);
875 mydata->fatbufnum = -1;
876 mydata->fat_dirty = 0;
877 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
878 if (mydata->fatbuf == NULL) {
879 debug("Error: allocating memory\n");
884 debug("VFAT Support enabled\n");
886 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
887 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
888 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
889 "Data begins at: %d\n",
891 mydata->rootdir_sect,
892 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
893 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
896 /* "cwd" is always the root... */
897 while (ISDIRDELIM(*filename))
900 /* Make a copy of the filename and convert it to lowercase */
901 strcpy(fnamecopy, filename);
905 if (*fnamecopy == '\0') {
910 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
912 fnamecopy[idx] = '\0';
913 subname = fnamecopy + idx + 1;
915 /* Handle multiple delimiters */
916 while (ISDIRDELIM(*subname))
927 if (mydata->fatsize == 32 || firsttime) {
928 dir_ptr = do_fat_read_at_block;
932 * FAT16 sector buffer modification:
933 * Each loop, the second buffered block is moved to
934 * the buffer begin, and two next sectors are read
935 * next to the previously moved one. So the sector
936 * buffer keeps always 3 sectors for fat16.
937 * And the current sector is the buffer second sector
938 * beside the "firsttime" read, when it is the first one.
940 * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1]
941 * n = computed root dir sector
942 * loop | cursect-1 | cursect | cursect+1 |
943 * 0 | sector n+0 | sector n+1 | none |
944 * 1 | none | sector n+0 | sector n+1 |
945 * 0 | sector n+1 | sector n+2 | sector n+3 |
946 * 1 | sector n+3 | ...
948 dir_ptr = (do_fat_read_at_block + mydata->sect_size);
949 memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size);
954 if (mydata->fatsize == 32 && buffer_blk_cnt)
958 read_blk = (mydata->fatsize == 32) ?
959 mydata->clust_size : PREFETCH_BLOCKS;
961 debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
962 cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK);
964 if (disk_read(cursect, read_blk, dir_ptr) < 0) {
965 debug("Error: reading rootdir block\n");
969 dentptr = (dir_entry *)dir_ptr;
972 for (i = 0; i < DIRENTSPERBLOCK; i++) {
973 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
977 if (dentptr->name[0] == DELETED_FLAG) {
983 csum = mkcksum(dentptr->name, dentptr->ext);
985 if (dentptr->attr & ATTR_VOLUME) {
987 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
988 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
990 ((dir_slot *)dentptr)->alias_checksum;
997 if (dols == LS_ROOT) {
1001 (dentptr->attr & ATTR_DIR);
1009 if (l_name[0] != 0) {
1016 printf(" %8u %s%c\n",
1017 FAT2CPU32(dentptr->size),
1029 debug("Rootvfatname: |%s|\n",
1032 /* Volume label or VFAT entry */
1036 } else if (dentptr->name[0] == 0) {
1037 debug("RootDentname == NULL - %d\n", i);
1038 if (dols == LS_ROOT) {
1039 printf("\n%d file(s), %d dir(s)\n\n",
1045 else if (vfat_enabled &&
1046 dols == LS_ROOT && csum == prevcksum) {
1052 get_name(dentptr, s_name);
1054 if (dols == LS_ROOT) {
1055 int isdir = (dentptr->attr & ATTR_DIR);
1061 if (s_name[0] != 0) {
1067 if (s_name[0] != 0) {
1074 printf(" %8u %s%c\n",
1075 FAT2CPU32(dentptr->size),
1086 if (strcmp(fnamecopy, s_name)
1087 && strcmp(fnamecopy, l_name)) {
1088 debug("RootMismatch: |%s|%s|\n", s_name,
1094 if (isdir && !(dentptr->attr & ATTR_DIR))
1097 debug("RootName: %s", s_name);
1098 debug(", start: 0x%x", START(dentptr));
1099 debug(", size: 0x%x %s\n",
1100 FAT2CPU32(dentptr->size),
1101 isdir ? "(DIR)" : "");
1103 goto rootdir_done; /* We got a match */
1105 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt,
1106 mydata->clust_size);
1109 * On FAT32 we must fetch the FAT entries for the next
1110 * root directory clusters when a cluster has been
1111 * completely processed.
1114 int rootdir_end = 0;
1115 if (mydata->fatsize == 32) {
1116 if (buffer_blk_cnt == mydata->clust_size) {
1120 nxt_clust = get_fatent(mydata, root_cluster);
1121 rootdir_end = CHECK_CLUST(nxt_clust, 32);
1123 nxtsect = mydata->data_begin +
1124 (nxt_clust * mydata->clust_size);
1126 root_cluster = nxt_clust;
1132 if (buffer_blk_cnt == PREFETCH_BLOCKS)
1135 rootdir_end = (++cursect - mydata->rootdir_sect >=
1139 /* If end of rootdir reached */
1141 if (dols == LS_ROOT) {
1142 printf("\n%d file(s), %d dir(s)\n\n",
1154 int startsect = mydata->data_begin
1155 + START(dentptr) * mydata->clust_size;
1157 char *nextname = NULL;
1162 idx = dirdelim(subname);
1165 subname[idx] = '\0';
1166 nextname = subname + idx + 1;
1167 /* Handle multiple delimiters */
1168 while (ISDIRDELIM(*nextname))
1170 if (dols && *nextname == '\0')
1173 if (dols && firsttime) {
1180 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1181 isdir ? 0 : dols) == NULL) {
1187 if (isdir && !(dentptr->attr & ATTR_DIR))
1191 * If we are looking for a directory, and found a directory
1192 * type entry, and the entry is for the root directory (as
1193 * denoted by a cluster number of 0), jump back to the start
1194 * of the function, since at least on FAT12/16, the root dir
1195 * lives in a hard-coded location and needs special handling
1196 * to parse, rather than simply following the cluster linked
1197 * list in the FAT, like other directories.
1199 if (isdir && (dentptr->attr & ATTR_DIR) && !START(dentptr)) {
1201 * Modify the filename to remove the prefix that gets
1202 * back to the root directory, so the initial root dir
1203 * parsing code can continue from where we are without
1206 strcpy(fnamecopy, nextname ?: "");
1208 * Set up state the same way as the function does when
1209 * first started. This is required for the root dir
1210 * parsing code operates in its expected environment.
1213 cursect = mydata->rootdir_sect;
1223 *size = FAT2CPU32(dentptr->size);
1226 ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size);
1228 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size);
1231 free(mydata->fatbuf);
1235 int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols,
1238 return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread);
1241 int file_fat_detectfs(void)
1244 volume_info volinfo;
1248 if (cur_dev == NULL) {
1249 printf("No current device\n");
1253 #if defined(CONFIG_IDE) || \
1254 defined(CONFIG_SATA) || \
1255 defined(CONFIG_SCSI) || \
1256 defined(CONFIG_CMD_USB) || \
1258 printf("Interface: ");
1259 switch (cur_dev->if_type) {
1285 printf("\n Device %d: ", cur_dev->devnum);
1289 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1290 printf("\nNo valid FAT fs found\n");
1294 memcpy(vol_label, volinfo.volume_label, 11);
1295 vol_label[11] = '\0';
1296 volinfo.fs_type[5] = '\0';
1298 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1303 int file_fat_ls(const char *dir)
1307 return do_fat_read(dir, NULL, 0, LS_YES, &size);
1310 int fat_exists(const char *filename)
1315 ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size);
1319 int fat_size(const char *filename, loff_t *size)
1321 return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
1324 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1325 loff_t maxsize, loff_t *actread)
1327 printf("reading %s\n", filename);
1328 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0,
1332 int file_fat_read(const char *filename, void *buffer, int maxsize)
1337 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1344 int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1349 ret = file_fat_read_at(filename, offset, buf, len, actread);
1351 printf("** Unable to read file %s **\n", filename);
1356 void fat_close(void)