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)
49 if (!cur_dev || !cur_dev->block_read)
52 return cur_dev->block_read(cur_dev->dev,
53 cur_part_info.start + block, nr_blocks, buf);
56 int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info)
58 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
61 cur_part_info = *info;
63 /* Make sure it has a valid FAT header */
64 if (disk_read(0, 1, buffer) != 1) {
69 /* Check if it's actually a DOS volume */
70 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
75 /* Check for FAT12/FAT16/FAT32 filesystem */
76 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
78 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
85 int fat_register_device(block_dev_desc_t *dev_desc, int part_no)
87 disk_partition_t info;
89 /* First close any currently found FAT filesystem */
92 /* Read the partition table, if present */
93 if (get_partition_info(dev_desc, part_no, &info)) {
95 printf("** Partition %d not valid on device %d **\n",
96 part_no, dev_desc->dev);
101 info.size = dev_desc->lba;
102 info.blksz = dev_desc->blksz;
106 #ifdef CONFIG_PARTITION_UUIDS
111 return fat_set_blk_dev(dev_desc, &info);
115 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
116 * Return index into string if found, -1 otherwise.
118 static int dirdelim(char *str)
122 while (*str != '\0') {
123 if (ISDIRDELIM(*str))
131 * Extract zero terminated short name from a directory entry.
133 static void get_name(dir_entry *dirent, char *s_name)
137 memcpy(s_name, dirent->name, 8);
140 while (*ptr && *ptr != ' ')
142 if (dirent->ext[0] && dirent->ext[0] != ' ') {
145 memcpy(ptr, dirent->ext, 3);
147 while (*ptr && *ptr != ' ')
151 if (*s_name == DELETED_FLAG)
153 else if (*s_name == aRING)
154 *s_name = DELETED_FLAG;
159 * Get the entry at index 'entry' in a FAT (12/16/32) table.
160 * On failure 0x00 is returned.
162 static __u32 get_fatent(fsdata *mydata, __u32 entry)
169 switch (mydata->fatsize) {
171 bufnum = entry / FAT32BUFSIZE;
172 offset = entry - bufnum * FAT32BUFSIZE;
175 bufnum = entry / FAT16BUFSIZE;
176 offset = entry - bufnum * FAT16BUFSIZE;
179 bufnum = entry / FAT12BUFSIZE;
180 offset = entry - bufnum * FAT12BUFSIZE;
184 /* Unsupported FAT size */
188 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
189 mydata->fatsize, entry, entry, offset, offset);
191 /* Read a new block of FAT entries into the cache. */
192 if (bufnum != mydata->fatbufnum) {
193 __u32 getsize = FATBUFBLOCKS;
194 __u8 *bufptr = mydata->fatbuf;
195 __u32 fatlength = mydata->fatlength;
196 __u32 startblock = bufnum * FATBUFBLOCKS;
198 if (startblock + getsize > fatlength)
199 getsize = fatlength - startblock;
201 startblock += mydata->fat_sect; /* Offset from start of disk */
203 if (disk_read(startblock, getsize, bufptr) < 0) {
204 debug("Error reading FAT blocks\n");
207 mydata->fatbufnum = bufnum;
210 /* Get the actual entry from the table */
211 switch (mydata->fatsize) {
213 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
216 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
219 off16 = (offset * 3) / 4;
221 switch (offset & 0x3) {
223 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
227 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
229 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
231 ret = (val2 << 4) | (val1 >> 12);
234 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
236 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
238 ret = (val2 << 8) | (val1 >> 8);
241 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
242 ret = (ret & 0xfff0) >> 4;
249 debug("FAT%d: ret: %08x, offset: %04x\n",
250 mydata->fatsize, ret, offset);
256 * Read at most 'size' bytes from the specified cluster into 'buffer'.
257 * Return 0 on success, -1 otherwise.
260 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
267 startsect = mydata->data_begin +
268 clustnum * mydata->clust_size;
270 startsect = mydata->rootdir_sect;
273 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
275 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
276 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
278 printf("FAT: Misaligned buffer address (%p)\n", buffer);
280 while (size >= mydata->sect_size) {
281 ret = disk_read(startsect++, 1, tmpbuf);
283 debug("Error reading data (got %d)\n", ret);
287 memcpy(buffer, tmpbuf, mydata->sect_size);
288 buffer += mydata->sect_size;
289 size -= mydata->sect_size;
292 idx = size / mydata->sect_size;
293 ret = disk_read(startsect, idx, buffer);
295 debug("Error reading data (got %d)\n", ret);
299 idx *= mydata->sect_size;
304 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
306 ret = disk_read(startsect, 1, tmpbuf);
308 debug("Error reading data (got %d)\n", ret);
312 memcpy(buffer, tmpbuf, size);
319 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
321 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
323 __u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
324 __aligned(ARCH_DMA_MINALIGN);
326 static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
327 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
329 loff_t filesize = FAT2CPU32(dentptr->size);
330 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
331 __u32 curclust = START(dentptr);
332 __u32 endclust, newclust;
336 debug("Filesize: %llu bytes\n", filesize);
338 if (pos >= filesize) {
339 debug("Read position past EOF: %llu\n", pos);
343 if (maxsize > 0 && filesize > pos + maxsize)
344 filesize = pos + maxsize;
346 debug("%llu bytes\n", filesize);
348 actsize = bytesperclust;
350 /* go to cluster at pos */
351 while (actsize <= pos) {
352 curclust = get_fatent(mydata, curclust);
353 if (CHECK_CLUST(curclust, mydata->fatsize)) {
354 debug("curclust: 0x%x\n", curclust);
355 debug("Invalid FAT entry\n");
358 actsize += bytesperclust;
362 actsize -= bytesperclust;
366 /* align to beginning of next cluster if any */
368 actsize = min(filesize, (loff_t)bytesperclust);
369 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
370 (int)actsize) != 0) {
371 printf("Error reading cluster\n");
376 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
382 curclust = get_fatent(mydata, curclust);
383 if (CHECK_CLUST(curclust, mydata->fatsize)) {
384 debug("curclust: 0x%x\n", curclust);
385 debug("Invalid FAT entry\n");
390 actsize = bytesperclust;
394 /* search for consecutive clusters */
395 while (actsize < filesize) {
396 newclust = get_fatent(mydata, endclust);
397 if ((newclust - 1) != endclust)
399 if (CHECK_CLUST(newclust, mydata->fatsize)) {
400 debug("curclust: 0x%x\n", newclust);
401 debug("Invalid FAT entry\n");
405 actsize += bytesperclust;
408 /* get remaining bytes */
410 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
411 printf("Error reading cluster\n");
417 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
418 printf("Error reading cluster\n");
421 *gotsize += (int)actsize;
425 curclust = get_fatent(mydata, endclust);
426 if (CHECK_CLUST(curclust, mydata->fatsize)) {
427 debug("curclust: 0x%x\n", curclust);
428 printf("Invalid FAT entry\n");
431 actsize = bytesperclust;
437 * Extract the file name information from 'slotptr' into 'l_name',
438 * starting at l_name[*idx].
439 * Return 1 if terminator (zero byte) is found, 0 otherwise.
441 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
445 for (j = 0; j <= 8; j += 2) {
446 l_name[*idx] = slotptr->name0_4[j];
447 if (l_name[*idx] == 0x00)
451 for (j = 0; j <= 10; j += 2) {
452 l_name[*idx] = slotptr->name5_10[j];
453 if (l_name[*idx] == 0x00)
457 for (j = 0; j <= 2; j += 2) {
458 l_name[*idx] = slotptr->name11_12[j];
459 if (l_name[*idx] == 0x00)
468 * Extract the full long filename starting at 'retdent' (which is really
469 * a slot) into 'l_name'. If successful also copy the real directory entry
471 * Return 0 on success, -1 otherwise.
474 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
475 dir_entry *retdent, char *l_name)
478 dir_slot *slotptr = (dir_slot *)retdent;
479 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
482 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
485 if (counter > VFAT_MAXSEQ) {
486 debug("Error: VFAT name is too long\n");
490 while ((__u8 *)slotptr < buflimit) {
493 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
499 if ((__u8 *)slotptr >= buflimit) {
504 curclust = get_fatent(mydata, curclust);
505 if (CHECK_CLUST(curclust, mydata->fatsize)) {
506 debug("curclust: 0x%x\n", curclust);
507 printf("Invalid FAT entry\n");
511 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
512 mydata->clust_size * mydata->sect_size) != 0) {
513 debug("Error: reading directory block\n");
517 slotptr2 = (dir_slot *)get_contents_vfatname_block;
518 while (counter > 0) {
519 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
526 /* Save the real directory entry */
527 realdent = (dir_entry *)slotptr2;
528 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
530 slot2str(slotptr2, l_name, &idx);
533 /* Save the real directory entry */
534 realdent = (dir_entry *)slotptr;
539 if (slot2str(slotptr, l_name, &idx))
541 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
544 if (*l_name == DELETED_FLAG)
546 else if (*l_name == aRING)
547 *l_name = DELETED_FLAG;
550 /* Return the real directory entry */
551 memcpy(retdent, realdent, sizeof(dir_entry));
556 /* Calculate short name checksum */
557 static __u8 mkcksum(const char name[8], const char ext[3])
563 for (i = 0; i < 8; i++)
564 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
565 for (i = 0; i < 3; i++)
566 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
572 * Get the directory entry associated with 'filename' from the directory
573 * starting at 'startsect'
575 __u8 get_dentfromdir_block[MAX_CLUSTSIZE]
576 __aligned(ARCH_DMA_MINALIGN);
578 static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
579 char *filename, dir_entry *retdent,
582 __u16 prevcksum = 0xffff;
583 __u32 curclust = START(retdent);
584 int files = 0, dirs = 0;
586 debug("get_dentfromdir: %s\n", filename);
593 if (get_cluster(mydata, curclust, get_dentfromdir_block,
594 mydata->clust_size * mydata->sect_size) != 0) {
595 debug("Error: reading directory block\n");
599 dentptr = (dir_entry *)get_dentfromdir_block;
601 for (i = 0; i < DIRENTSPERCLUST; i++) {
602 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
605 if (dentptr->name[0] == DELETED_FLAG) {
609 if ((dentptr->attr & ATTR_VOLUME)) {
611 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
612 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
613 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
614 get_vfatname(mydata, curclust,
615 get_dentfromdir_block,
622 isdir = (dentptr->attr & ATTR_DIR);
630 if (l_name[0] != 0) {
637 printf(" %8u %s%c\n",
638 FAT2CPU32(dentptr->size),
650 debug("vfatname: |%s|\n", l_name);
652 /* Volume label or VFAT entry */
657 if (dentptr->name[0] == 0) {
659 printf("\n%d file(s), %d dir(s)\n\n",
662 debug("Dentname == NULL - %d\n", i);
666 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
667 if (dols && csum == prevcksum) {
674 get_name(dentptr, s_name);
676 int isdir = (dentptr->attr & ATTR_DIR);
686 if (s_name[0] != 0) {
694 printf(" %8u %s%c\n",
695 FAT2CPU32(dentptr->size),
707 if (strcmp(filename, s_name)
708 && strcmp(filename, l_name)) {
709 debug("Mismatch: |%s|%s|\n", s_name, l_name);
714 memcpy(retdent, dentptr, sizeof(dir_entry));
716 debug("DentName: %s", s_name);
717 debug(", start: 0x%x", START(dentptr));
718 debug(", size: 0x%x %s\n",
719 FAT2CPU32(dentptr->size),
720 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
725 curclust = get_fatent(mydata, curclust);
726 if (CHECK_CLUST(curclust, mydata->fatsize)) {
727 debug("curclust: 0x%x\n", curclust);
728 printf("Invalid FAT entry\n");
737 * Read boot sector and volume info from a FAT filesystem
740 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
743 volume_info *vistart;
746 if (cur_dev == NULL) {
747 debug("Error: no device selected\n");
751 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
753 debug("Error: allocating block\n");
757 if (disk_read(0, 1, block) < 0) {
758 debug("Error: reading block\n");
762 memcpy(bs, block, sizeof(boot_sector));
763 bs->reserved = FAT2CPU16(bs->reserved);
764 bs->fat_length = FAT2CPU16(bs->fat_length);
765 bs->secs_track = FAT2CPU16(bs->secs_track);
766 bs->heads = FAT2CPU16(bs->heads);
767 bs->total_sect = FAT2CPU32(bs->total_sect);
770 if (bs->fat_length == 0) {
772 bs->fat32_length = FAT2CPU32(bs->fat32_length);
773 bs->flags = FAT2CPU16(bs->flags);
774 bs->root_cluster = FAT2CPU32(bs->root_cluster);
775 bs->info_sector = FAT2CPU16(bs->info_sector);
776 bs->backup_boot = FAT2CPU16(bs->backup_boot);
777 vistart = (volume_info *)(block + sizeof(boot_sector));
780 vistart = (volume_info *)&(bs->fat32_length);
783 memcpy(volinfo, vistart, sizeof(volume_info));
785 if (*fatsize == 32) {
786 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
789 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
793 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
799 debug("Error: broken fs_type sign\n");
807 __u8 do_fat_read_at_block[MAX_CLUSTSIZE]
808 __aligned(ARCH_DMA_MINALIGN);
810 int do_fat_read_at(const char *filename, loff_t pos, void *buffer,
811 loff_t maxsize, int dols, int dogetsize, loff_t *size)
813 char fnamecopy[2048];
817 fsdata *mydata = &datablock;
818 dir_entry *dentptr = NULL;
819 __u16 prevcksum = 0xffff;
823 int files = 0, dirs = 0;
826 __u32 root_cluster = 0;
828 int rootdir_size = 0;
833 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
834 debug("Error: reading boot sector\n");
838 if (mydata->fatsize == 32) {
839 root_cluster = bs.root_cluster;
840 mydata->fatlength = bs.fat32_length;
842 mydata->fatlength = bs.fat_length;
845 mydata->fat_sect = bs.reserved;
847 cursect = mydata->rootdir_sect
848 = mydata->fat_sect + mydata->fatlength * bs.fats;
850 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
851 mydata->clust_size = bs.cluster_size;
852 if (mydata->sect_size != cur_part_info.blksz) {
853 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
854 mydata->sect_size, cur_part_info.blksz);
858 if (mydata->fatsize == 32) {
859 mydata->data_begin = mydata->rootdir_sect -
860 (mydata->clust_size * 2);
862 rootdir_size = ((bs.dir_entries[1] * (int)256 +
866 mydata->data_begin = mydata->rootdir_sect +
868 (mydata->clust_size * 2);
871 mydata->fatbufnum = -1;
872 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
873 if (mydata->fatbuf == NULL) {
874 debug("Error: allocating memory\n");
879 debug("VFAT Support enabled\n");
881 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
882 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
883 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
884 "Data begins at: %d\n",
886 mydata->rootdir_sect,
887 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
888 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
891 /* "cwd" is always the root... */
892 while (ISDIRDELIM(*filename))
895 /* Make a copy of the filename and convert it to lowercase */
896 strcpy(fnamecopy, filename);
900 if (*fnamecopy == '\0') {
905 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
907 fnamecopy[idx] = '\0';
908 subname = fnamecopy + idx + 1;
910 /* Handle multiple delimiters */
911 while (ISDIRDELIM(*subname))
922 if (mydata->fatsize == 32 || firsttime) {
923 dir_ptr = do_fat_read_at_block;
927 * FAT16 sector buffer modification:
928 * Each loop, the second buffered block is moved to
929 * the buffer begin, and two next sectors are read
930 * next to the previously moved one. So the sector
931 * buffer keeps always 3 sectors for fat16.
932 * And the current sector is the buffer second sector
933 * beside the "firsttime" read, when it is the first one.
935 * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1]
936 * n = computed root dir sector
937 * loop | cursect-1 | cursect | cursect+1 |
938 * 0 | sector n+0 | sector n+1 | none |
939 * 1 | none | sector n+0 | sector n+1 |
940 * 0 | sector n+1 | sector n+2 | sector n+3 |
941 * 1 | sector n+3 | ...
943 dir_ptr = (do_fat_read_at_block + mydata->sect_size);
944 memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size);
949 if (mydata->fatsize == 32 && buffer_blk_cnt)
953 read_blk = (mydata->fatsize == 32) ?
954 mydata->clust_size : PREFETCH_BLOCKS;
956 debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
957 cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK);
959 if (disk_read(cursect, read_blk, dir_ptr) < 0) {
960 debug("Error: reading rootdir block\n");
964 dentptr = (dir_entry *)dir_ptr;
967 for (i = 0; i < DIRENTSPERBLOCK; i++) {
968 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
972 if (dentptr->name[0] == DELETED_FLAG) {
978 csum = mkcksum(dentptr->name, dentptr->ext);
980 if (dentptr->attr & ATTR_VOLUME) {
982 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
983 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
985 ((dir_slot *)dentptr)->alias_checksum;
992 if (dols == LS_ROOT) {
996 (dentptr->attr & ATTR_DIR);
1004 if (l_name[0] != 0) {
1011 printf(" %8u %s%c\n",
1012 FAT2CPU32(dentptr->size),
1024 debug("Rootvfatname: |%s|\n",
1027 /* Volume label or VFAT entry */
1031 } else if (dentptr->name[0] == 0) {
1032 debug("RootDentname == NULL - %d\n", i);
1033 if (dols == LS_ROOT) {
1034 printf("\n%d file(s), %d dir(s)\n\n",
1040 else if (vfat_enabled &&
1041 dols == LS_ROOT && csum == prevcksum) {
1047 get_name(dentptr, s_name);
1049 if (dols == LS_ROOT) {
1050 int isdir = (dentptr->attr & ATTR_DIR);
1056 if (s_name[0] != 0) {
1062 if (s_name[0] != 0) {
1069 printf(" %8u %s%c\n",
1070 FAT2CPU32(dentptr->size),
1081 if (strcmp(fnamecopy, s_name)
1082 && strcmp(fnamecopy, l_name)) {
1083 debug("RootMismatch: |%s|%s|\n", s_name,
1089 if (isdir && !(dentptr->attr & ATTR_DIR))
1092 debug("RootName: %s", s_name);
1093 debug(", start: 0x%x", START(dentptr));
1094 debug(", size: 0x%x %s\n",
1095 FAT2CPU32(dentptr->size),
1096 isdir ? "(DIR)" : "");
1098 goto rootdir_done; /* We got a match */
1100 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt,
1101 mydata->clust_size);
1104 * On FAT32 we must fetch the FAT entries for the next
1105 * root directory clusters when a cluster has been
1106 * completely processed.
1109 int rootdir_end = 0;
1110 if (mydata->fatsize == 32) {
1111 if (buffer_blk_cnt == mydata->clust_size) {
1115 nxt_clust = get_fatent(mydata, root_cluster);
1116 rootdir_end = CHECK_CLUST(nxt_clust, 32);
1118 nxtsect = mydata->data_begin +
1119 (nxt_clust * mydata->clust_size);
1121 root_cluster = nxt_clust;
1127 if (buffer_blk_cnt == PREFETCH_BLOCKS)
1130 rootdir_end = (++cursect - mydata->rootdir_sect >=
1134 /* If end of rootdir reached */
1136 if (dols == LS_ROOT) {
1137 printf("\n%d file(s), %d dir(s)\n\n",
1149 int startsect = mydata->data_begin
1150 + START(dentptr) * mydata->clust_size;
1152 char *nextname = NULL;
1157 idx = dirdelim(subname);
1160 subname[idx] = '\0';
1161 nextname = subname + idx + 1;
1162 /* Handle multiple delimiters */
1163 while (ISDIRDELIM(*nextname))
1165 if (dols && *nextname == '\0')
1168 if (dols && firsttime) {
1175 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1176 isdir ? 0 : dols) == NULL) {
1182 if (isdir && !(dentptr->attr & ATTR_DIR))
1186 * If we are looking for a directory, and found a directory
1187 * type entry, and the entry is for the root directory (as
1188 * denoted by a cluster number of 0), jump back to the start
1189 * of the function, since at least on FAT12/16, the root dir
1190 * lives in a hard-coded location and needs special handling
1191 * to parse, rather than simply following the cluster linked
1192 * list in the FAT, like other directories.
1194 if (isdir && (dentptr->attr & ATTR_DIR) && !START(dentptr)) {
1196 * Modify the filename to remove the prefix that gets
1197 * back to the root directory, so the initial root dir
1198 * parsing code can continue from where we are without
1201 strcpy(fnamecopy, nextname ?: "");
1203 * Set up state the same way as the function does when
1204 * first started. This is required for the root dir
1205 * parsing code operates in its expected environment.
1208 cursect = mydata->rootdir_sect;
1218 *size = FAT2CPU32(dentptr->size);
1221 ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size);
1223 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size);
1226 free(mydata->fatbuf);
1230 int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols,
1233 return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread);
1236 int file_fat_detectfs(void)
1239 volume_info volinfo;
1243 if (cur_dev == NULL) {
1244 printf("No current device\n");
1248 #if defined(CONFIG_CMD_IDE) || \
1249 defined(CONFIG_CMD_SATA) || \
1250 defined(CONFIG_CMD_SCSI) || \
1251 defined(CONFIG_CMD_USB) || \
1253 printf("Interface: ");
1254 switch (cur_dev->if_type) {
1280 printf("\n Device %d: ", cur_dev->dev);
1284 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1285 printf("\nNo valid FAT fs found\n");
1289 memcpy(vol_label, volinfo.volume_label, 11);
1290 vol_label[11] = '\0';
1291 volinfo.fs_type[5] = '\0';
1293 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1298 int file_fat_ls(const char *dir)
1302 return do_fat_read(dir, NULL, 0, LS_YES, &size);
1305 int fat_exists(const char *filename)
1310 ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size);
1314 int fat_size(const char *filename, loff_t *size)
1316 return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
1319 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1320 loff_t maxsize, loff_t *actread)
1322 printf("reading %s\n", filename);
1323 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0,
1327 int file_fat_read(const char *filename, void *buffer, int maxsize)
1332 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1339 int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1344 ret = file_fat_read_at(filename, offset, buf, len, actread);
1346 printf("** Unable to read file %s **\n", filename);
1351 void fat_close(void)