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;
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 off16 = (offset * 3) / 2;
246 ret = FAT2CPU16(*(__u16 *)(mydata->fatbuf + off16));
252 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
253 mydata->fatsize, ret, entry, offset);
259 * Read at most 'size' bytes from the specified cluster into 'buffer'.
260 * Return 0 on success, -1 otherwise.
263 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
270 startsect = mydata->data_begin +
271 clustnum * mydata->clust_size;
273 startsect = mydata->rootdir_sect;
276 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
278 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
279 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
281 printf("FAT: Misaligned buffer address (%p)\n", buffer);
283 while (size >= mydata->sect_size) {
284 ret = disk_read(startsect++, 1, tmpbuf);
286 debug("Error reading data (got %d)\n", ret);
290 memcpy(buffer, tmpbuf, mydata->sect_size);
291 buffer += mydata->sect_size;
292 size -= mydata->sect_size;
295 idx = size / mydata->sect_size;
296 ret = disk_read(startsect, idx, buffer);
298 debug("Error reading data (got %d)\n", ret);
302 idx *= mydata->sect_size;
307 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
309 ret = disk_read(startsect, 1, tmpbuf);
311 debug("Error reading data (got %d)\n", ret);
315 memcpy(buffer, tmpbuf, size);
322 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
324 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
326 __u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
327 __aligned(ARCH_DMA_MINALIGN);
329 static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
330 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
332 loff_t filesize = FAT2CPU32(dentptr->size);
333 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
334 __u32 curclust = START(dentptr);
335 __u32 endclust, newclust;
339 debug("Filesize: %llu bytes\n", filesize);
341 if (pos >= filesize) {
342 debug("Read position past EOF: %llu\n", pos);
346 if (maxsize > 0 && filesize > pos + maxsize)
347 filesize = pos + maxsize;
349 debug("%llu bytes\n", filesize);
351 actsize = bytesperclust;
353 /* go to cluster at pos */
354 while (actsize <= pos) {
355 curclust = get_fatent(mydata, curclust);
356 if (CHECK_CLUST(curclust, mydata->fatsize)) {
357 debug("curclust: 0x%x\n", curclust);
358 debug("Invalid FAT entry\n");
361 actsize += bytesperclust;
365 actsize -= bytesperclust;
369 /* align to beginning of next cluster if any */
371 actsize = min(filesize, (loff_t)bytesperclust);
372 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
373 (int)actsize) != 0) {
374 printf("Error reading cluster\n");
379 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
385 curclust = get_fatent(mydata, curclust);
386 if (CHECK_CLUST(curclust, mydata->fatsize)) {
387 debug("curclust: 0x%x\n", curclust);
388 debug("Invalid FAT entry\n");
393 actsize = bytesperclust;
397 /* search for consecutive clusters */
398 while (actsize < filesize) {
399 newclust = get_fatent(mydata, endclust);
400 if ((newclust - 1) != endclust)
402 if (CHECK_CLUST(newclust, mydata->fatsize)) {
403 debug("curclust: 0x%x\n", newclust);
404 debug("Invalid FAT entry\n");
408 actsize += bytesperclust;
411 /* get remaining bytes */
413 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
414 printf("Error reading cluster\n");
420 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
421 printf("Error reading cluster\n");
424 *gotsize += (int)actsize;
428 curclust = get_fatent(mydata, endclust);
429 if (CHECK_CLUST(curclust, mydata->fatsize)) {
430 debug("curclust: 0x%x\n", curclust);
431 printf("Invalid FAT entry\n");
434 actsize = bytesperclust;
440 * Extract the file name information from 'slotptr' into 'l_name',
441 * starting at l_name[*idx].
442 * Return 1 if terminator (zero byte) is found, 0 otherwise.
444 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
448 for (j = 0; j <= 8; j += 2) {
449 l_name[*idx] = slotptr->name0_4[j];
450 if (l_name[*idx] == 0x00)
454 for (j = 0; j <= 10; j += 2) {
455 l_name[*idx] = slotptr->name5_10[j];
456 if (l_name[*idx] == 0x00)
460 for (j = 0; j <= 2; j += 2) {
461 l_name[*idx] = slotptr->name11_12[j];
462 if (l_name[*idx] == 0x00)
471 * Extract the full long filename starting at 'retdent' (which is really
472 * a slot) into 'l_name'. If successful also copy the real directory entry
474 * Return 0 on success, -1 otherwise.
477 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
478 dir_entry *retdent, char *l_name)
481 dir_slot *slotptr = (dir_slot *)retdent;
482 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
485 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
488 if (counter > VFAT_MAXSEQ) {
489 debug("Error: VFAT name is too long\n");
493 while ((__u8 *)slotptr < buflimit) {
496 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
502 if ((__u8 *)slotptr >= buflimit) {
507 curclust = get_fatent(mydata, curclust);
508 if (CHECK_CLUST(curclust, mydata->fatsize)) {
509 debug("curclust: 0x%x\n", curclust);
510 printf("Invalid FAT entry\n");
514 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
515 mydata->clust_size * mydata->sect_size) != 0) {
516 debug("Error: reading directory block\n");
520 slotptr2 = (dir_slot *)get_contents_vfatname_block;
521 while (counter > 0) {
522 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
529 /* Save the real directory entry */
530 realdent = (dir_entry *)slotptr2;
531 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
533 slot2str(slotptr2, l_name, &idx);
536 /* Save the real directory entry */
537 realdent = (dir_entry *)slotptr;
542 if (slot2str(slotptr, l_name, &idx))
544 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
547 if (*l_name == DELETED_FLAG)
549 else if (*l_name == aRING)
550 *l_name = DELETED_FLAG;
553 /* Return the real directory entry */
554 memcpy(retdent, realdent, sizeof(dir_entry));
559 /* Calculate short name checksum */
560 static __u8 mkcksum(const char name[8], const char ext[3])
566 for (i = 0; i < 8; i++)
567 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
568 for (i = 0; i < 3; i++)
569 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
575 * Get the directory entry associated with 'filename' from the directory
576 * starting at 'startsect'
578 __u8 get_dentfromdir_block[MAX_CLUSTSIZE]
579 __aligned(ARCH_DMA_MINALIGN);
581 static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
582 char *filename, dir_entry *retdent,
585 __u16 prevcksum = 0xffff;
586 __u32 curclust = START(retdent);
587 int files = 0, dirs = 0;
589 debug("get_dentfromdir: %s\n", filename);
596 if (get_cluster(mydata, curclust, get_dentfromdir_block,
597 mydata->clust_size * mydata->sect_size) != 0) {
598 debug("Error: reading directory block\n");
602 dentptr = (dir_entry *)get_dentfromdir_block;
604 for (i = 0; i < DIRENTSPERCLUST; i++) {
605 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
608 if (dentptr->name[0] == DELETED_FLAG) {
612 if ((dentptr->attr & ATTR_VOLUME)) {
614 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
615 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
616 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
617 get_vfatname(mydata, curclust,
618 get_dentfromdir_block,
625 isdir = (dentptr->attr & ATTR_DIR);
633 if (l_name[0] != 0) {
640 printf(" %8u %s%c\n",
641 FAT2CPU32(dentptr->size),
653 debug("vfatname: |%s|\n", l_name);
655 /* Volume label or VFAT entry */
660 if (dentptr->name[0] == 0) {
662 printf("\n%d file(s), %d dir(s)\n\n",
665 debug("Dentname == NULL - %d\n", i);
669 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
670 if (dols && csum == prevcksum) {
677 get_name(dentptr, s_name);
679 int isdir = (dentptr->attr & ATTR_DIR);
689 if (s_name[0] != 0) {
697 printf(" %8u %s%c\n",
698 FAT2CPU32(dentptr->size),
710 if (strcmp(filename, s_name)
711 && strcmp(filename, l_name)) {
712 debug("Mismatch: |%s|%s|\n", s_name, l_name);
717 memcpy(retdent, dentptr, sizeof(dir_entry));
719 debug("DentName: %s", s_name);
720 debug(", start: 0x%x", START(dentptr));
721 debug(", size: 0x%x %s\n",
722 FAT2CPU32(dentptr->size),
723 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
728 curclust = get_fatent(mydata, curclust);
729 if (CHECK_CLUST(curclust, mydata->fatsize)) {
730 debug("curclust: 0x%x\n", curclust);
731 printf("Invalid FAT entry\n");
740 * Read boot sector and volume info from a FAT filesystem
743 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
746 volume_info *vistart;
749 if (cur_dev == NULL) {
750 debug("Error: no device selected\n");
754 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
756 debug("Error: allocating block\n");
760 if (disk_read(0, 1, block) < 0) {
761 debug("Error: reading block\n");
765 memcpy(bs, block, sizeof(boot_sector));
766 bs->reserved = FAT2CPU16(bs->reserved);
767 bs->fat_length = FAT2CPU16(bs->fat_length);
768 bs->secs_track = FAT2CPU16(bs->secs_track);
769 bs->heads = FAT2CPU16(bs->heads);
770 bs->total_sect = FAT2CPU32(bs->total_sect);
773 if (bs->fat_length == 0) {
775 bs->fat32_length = FAT2CPU32(bs->fat32_length);
776 bs->flags = FAT2CPU16(bs->flags);
777 bs->root_cluster = FAT2CPU32(bs->root_cluster);
778 bs->info_sector = FAT2CPU16(bs->info_sector);
779 bs->backup_boot = FAT2CPU16(bs->backup_boot);
780 vistart = (volume_info *)(block + sizeof(boot_sector));
783 vistart = (volume_info *)&(bs->fat32_length);
786 memcpy(volinfo, vistart, sizeof(volume_info));
788 if (*fatsize == 32) {
789 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
792 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
796 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
802 debug("Error: broken fs_type sign\n");
810 __u8 do_fat_read_at_block[MAX_CLUSTSIZE]
811 __aligned(ARCH_DMA_MINALIGN);
813 int do_fat_read_at(const char *filename, loff_t pos, void *buffer,
814 loff_t maxsize, int dols, int dogetsize, loff_t *size)
816 char fnamecopy[2048];
820 fsdata *mydata = &datablock;
821 dir_entry *dentptr = NULL;
822 __u16 prevcksum = 0xffff;
826 int files = 0, dirs = 0;
829 __u32 root_cluster = 0;
831 int rootdir_size = 0;
836 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
837 debug("Error: reading boot sector\n");
841 if (mydata->fatsize == 32) {
842 root_cluster = bs.root_cluster;
843 mydata->fatlength = bs.fat32_length;
845 mydata->fatlength = bs.fat_length;
848 mydata->fat_sect = bs.reserved;
850 cursect = mydata->rootdir_sect
851 = mydata->fat_sect + mydata->fatlength * bs.fats;
853 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
854 mydata->clust_size = bs.cluster_size;
855 if (mydata->sect_size != cur_part_info.blksz) {
856 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
857 mydata->sect_size, cur_part_info.blksz);
861 if (mydata->fatsize == 32) {
862 mydata->data_begin = mydata->rootdir_sect -
863 (mydata->clust_size * 2);
865 rootdir_size = ((bs.dir_entries[1] * (int)256 +
869 mydata->data_begin = mydata->rootdir_sect +
871 (mydata->clust_size * 2);
874 mydata->fatbufnum = -1;
875 mydata->fat_dirty = 0;
876 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
877 if (mydata->fatbuf == NULL) {
878 debug("Error: allocating memory\n");
883 debug("VFAT Support enabled\n");
885 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
886 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
887 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
888 "Data begins at: %d\n",
890 mydata->rootdir_sect,
891 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
892 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
895 /* "cwd" is always the root... */
896 while (ISDIRDELIM(*filename))
899 /* Make a copy of the filename and convert it to lowercase */
900 strcpy(fnamecopy, filename);
904 if (*fnamecopy == '\0') {
909 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
911 fnamecopy[idx] = '\0';
912 subname = fnamecopy + idx + 1;
914 /* Handle multiple delimiters */
915 while (ISDIRDELIM(*subname))
926 if (mydata->fatsize == 32 || firsttime) {
927 dir_ptr = do_fat_read_at_block;
931 * FAT16 sector buffer modification:
932 * Each loop, the second buffered block is moved to
933 * the buffer begin, and two next sectors are read
934 * next to the previously moved one. So the sector
935 * buffer keeps always 3 sectors for fat16.
936 * And the current sector is the buffer second sector
937 * beside the "firsttime" read, when it is the first one.
939 * PREFETCH_BLOCKS is 2 for FAT16 == loop[0:1]
940 * n = computed root dir sector
941 * loop | cursect-1 | cursect | cursect+1 |
942 * 0 | sector n+0 | sector n+1 | none |
943 * 1 | none | sector n+0 | sector n+1 |
944 * 0 | sector n+1 | sector n+2 | sector n+3 |
945 * 1 | sector n+3 | ...
947 dir_ptr = (do_fat_read_at_block + mydata->sect_size);
948 memcpy(do_fat_read_at_block, dir_ptr, mydata->sect_size);
953 if (mydata->fatsize == 32 && buffer_blk_cnt)
957 read_blk = (mydata->fatsize == 32) ?
958 mydata->clust_size : PREFETCH_BLOCKS;
960 debug("FAT read(sect=%d, cnt:%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
961 cursect, read_blk, mydata->clust_size, DIRENTSPERBLOCK);
963 if (disk_read(cursect, read_blk, dir_ptr) < 0) {
964 debug("Error: reading rootdir block\n");
968 dentptr = (dir_entry *)dir_ptr;
971 for (i = 0; i < DIRENTSPERBLOCK; i++) {
972 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
976 if (dentptr->name[0] == DELETED_FLAG) {
982 csum = mkcksum(dentptr->name, dentptr->ext);
984 if (dentptr->attr & ATTR_VOLUME) {
986 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
987 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
989 ((dir_slot *)dentptr)->alias_checksum;
996 if (dols == LS_ROOT) {
1000 (dentptr->attr & ATTR_DIR);
1008 if (l_name[0] != 0) {
1015 printf(" %8u %s%c\n",
1016 FAT2CPU32(dentptr->size),
1028 debug("Rootvfatname: |%s|\n",
1031 /* Volume label or VFAT entry */
1035 } else if (dentptr->name[0] == 0) {
1036 debug("RootDentname == NULL - %d\n", i);
1037 if (dols == LS_ROOT) {
1038 printf("\n%d file(s), %d dir(s)\n\n",
1044 else if (vfat_enabled &&
1045 dols == LS_ROOT && csum == prevcksum) {
1051 get_name(dentptr, s_name);
1053 if (dols == LS_ROOT) {
1054 int isdir = (dentptr->attr & ATTR_DIR);
1060 if (s_name[0] != 0) {
1066 if (s_name[0] != 0) {
1073 printf(" %8u %s%c\n",
1074 FAT2CPU32(dentptr->size),
1085 if (strcmp(fnamecopy, s_name)
1086 && strcmp(fnamecopy, l_name)) {
1087 debug("RootMismatch: |%s|%s|\n", s_name,
1093 if (isdir && !(dentptr->attr & ATTR_DIR))
1096 debug("RootName: %s", s_name);
1097 debug(", start: 0x%x", START(dentptr));
1098 debug(", size: 0x%x %s\n",
1099 FAT2CPU32(dentptr->size),
1100 isdir ? "(DIR)" : "");
1102 goto rootdir_done; /* We got a match */
1104 debug("END LOOP: buffer_blk_cnt=%d clust_size=%d\n", buffer_blk_cnt,
1105 mydata->clust_size);
1108 * On FAT32 we must fetch the FAT entries for the next
1109 * root directory clusters when a cluster has been
1110 * completely processed.
1113 int rootdir_end = 0;
1114 if (mydata->fatsize == 32) {
1115 if (buffer_blk_cnt == mydata->clust_size) {
1119 nxt_clust = get_fatent(mydata, root_cluster);
1120 rootdir_end = CHECK_CLUST(nxt_clust, 32);
1122 nxtsect = mydata->data_begin +
1123 (nxt_clust * mydata->clust_size);
1125 root_cluster = nxt_clust;
1131 if (buffer_blk_cnt == PREFETCH_BLOCKS)
1134 rootdir_end = (++cursect - mydata->rootdir_sect >=
1138 /* If end of rootdir reached */
1140 if (dols == LS_ROOT) {
1141 printf("\n%d file(s), %d dir(s)\n\n",
1153 int startsect = mydata->data_begin
1154 + START(dentptr) * mydata->clust_size;
1156 char *nextname = NULL;
1161 idx = dirdelim(subname);
1164 subname[idx] = '\0';
1165 nextname = subname + idx + 1;
1166 /* Handle multiple delimiters */
1167 while (ISDIRDELIM(*nextname))
1169 if (dols && *nextname == '\0')
1172 if (dols && firsttime) {
1179 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1180 isdir ? 0 : dols) == NULL) {
1186 if (isdir && !(dentptr->attr & ATTR_DIR))
1190 * If we are looking for a directory, and found a directory
1191 * type entry, and the entry is for the root directory (as
1192 * denoted by a cluster number of 0), jump back to the start
1193 * of the function, since at least on FAT12/16, the root dir
1194 * lives in a hard-coded location and needs special handling
1195 * to parse, rather than simply following the cluster linked
1196 * list in the FAT, like other directories.
1198 if (isdir && (dentptr->attr & ATTR_DIR) && !START(dentptr)) {
1200 * Modify the filename to remove the prefix that gets
1201 * back to the root directory, so the initial root dir
1202 * parsing code can continue from where we are without
1205 strcpy(fnamecopy, nextname ?: "");
1207 * Set up state the same way as the function does when
1208 * first started. This is required for the root dir
1209 * parsing code operates in its expected environment.
1212 cursect = mydata->rootdir_sect;
1222 *size = FAT2CPU32(dentptr->size);
1225 ret = get_contents(mydata, dentptr, pos, buffer, maxsize, size);
1227 debug("Size: %u, got: %llu\n", FAT2CPU32(dentptr->size), *size);
1230 free(mydata->fatbuf);
1234 int do_fat_read(const char *filename, void *buffer, loff_t maxsize, int dols,
1237 return do_fat_read_at(filename, 0, buffer, maxsize, dols, 0, actread);
1240 int file_fat_detectfs(void)
1243 volume_info volinfo;
1247 if (cur_dev == NULL) {
1248 printf("No current device\n");
1252 #if defined(CONFIG_CMD_IDE) || \
1253 defined(CONFIG_CMD_SATA) || \
1254 defined(CONFIG_SCSI) || \
1255 defined(CONFIG_CMD_USB) || \
1257 printf("Interface: ");
1258 switch (cur_dev->if_type) {
1284 printf("\n Device %d: ", cur_dev->devnum);
1288 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1289 printf("\nNo valid FAT fs found\n");
1293 memcpy(vol_label, volinfo.volume_label, 11);
1294 vol_label[11] = '\0';
1295 volinfo.fs_type[5] = '\0';
1297 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1302 int file_fat_ls(const char *dir)
1306 return do_fat_read(dir, NULL, 0, LS_YES, &size);
1309 int fat_exists(const char *filename)
1314 ret = do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, &size);
1318 int fat_size(const char *filename, loff_t *size)
1320 return do_fat_read_at(filename, 0, NULL, 0, LS_NO, 1, size);
1323 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1324 loff_t maxsize, loff_t *actread)
1326 printf("reading %s\n", filename);
1327 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO, 0,
1331 int file_fat_read(const char *filename, void *buffer, int maxsize)
1336 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1343 int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1348 ret = file_fat_read_at(filename, offset, buf, len, actread);
1350 printf("** Unable to read file %s **\n", filename);
1355 void fat_close(void)