4 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
6 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
7 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 #include <asm/byteorder.h>
35 #include <linux/compiler.h>
36 #include <linux/ctype.h>
38 #ifdef CONFIG_SUPPORT_VFAT
39 static const int vfat_enabled = 1;
41 static const int vfat_enabled = 0;
45 * Convert a string to lowercase.
47 static void downcase(char *str)
49 while (*str != '\0') {
55 static block_dev_desc_t *cur_dev;
56 static disk_partition_t cur_part_info;
58 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
59 #define DOS_FS_TYPE_OFFSET 0x36
60 #define DOS_FS32_TYPE_OFFSET 0x52
62 static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
64 if (!cur_dev || !cur_dev->block_read)
67 return cur_dev->block_read(cur_dev->dev,
68 cur_part_info.start + block, nr_blocks, buf);
71 int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info)
73 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
76 cur_part_info = *info;
78 /* Make sure it has a valid FAT header */
79 if (disk_read(0, 1, buffer) != 1) {
84 /* Check if it's actually a DOS volume */
85 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
90 /* Check for FAT12/FAT16/FAT32 filesystem */
91 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
93 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
100 int fat_register_device(block_dev_desc_t *dev_desc, int part_no)
102 disk_partition_t info;
104 /* First close any currently found FAT filesystem */
107 /* Read the partition table, if present */
108 if (get_partition_info(dev_desc, part_no, &info)) {
110 printf("** Partition %d not valid on device %d **\n",
111 part_no, dev_desc->dev);
116 info.size = dev_desc->lba;
117 info.blksz = dev_desc->blksz;
121 #ifdef CONFIG_PARTITION_UUIDS
126 return fat_set_blk_dev(dev_desc, &info);
130 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
131 * Return index into string if found, -1 otherwise.
133 static int dirdelim(char *str)
137 while (*str != '\0') {
138 if (ISDIRDELIM(*str))
146 * Extract zero terminated short name from a directory entry.
148 static void get_name(dir_entry *dirent, char *s_name)
152 memcpy(s_name, dirent->name, 8);
155 while (*ptr && *ptr != ' ')
157 if (dirent->ext[0] && dirent->ext[0] != ' ') {
160 memcpy(ptr, dirent->ext, 3);
162 while (*ptr && *ptr != ' ')
166 if (*s_name == DELETED_FLAG)
168 else if (*s_name == aRING)
169 *s_name = DELETED_FLAG;
174 * Get the entry at index 'entry' in a FAT (12/16/32) table.
175 * On failure 0x00 is returned.
177 static __u32 get_fatent(fsdata *mydata, __u32 entry)
184 switch (mydata->fatsize) {
186 bufnum = entry / FAT32BUFSIZE;
187 offset = entry - bufnum * FAT32BUFSIZE;
190 bufnum = entry / FAT16BUFSIZE;
191 offset = entry - bufnum * FAT16BUFSIZE;
194 bufnum = entry / FAT12BUFSIZE;
195 offset = entry - bufnum * FAT12BUFSIZE;
199 /* Unsupported FAT size */
203 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
204 mydata->fatsize, entry, entry, offset, offset);
206 /* Read a new block of FAT entries into the cache. */
207 if (bufnum != mydata->fatbufnum) {
208 __u32 getsize = FATBUFBLOCKS;
209 __u8 *bufptr = mydata->fatbuf;
210 __u32 fatlength = mydata->fatlength;
211 __u32 startblock = bufnum * FATBUFBLOCKS;
213 if (startblock + getsize > fatlength)
214 getsize = fatlength - startblock;
216 startblock += mydata->fat_sect; /* Offset from start of disk */
218 if (disk_read(startblock, getsize, bufptr) < 0) {
219 debug("Error reading FAT blocks\n");
222 mydata->fatbufnum = bufnum;
225 /* Get the actual entry from the table */
226 switch (mydata->fatsize) {
228 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
231 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
234 off16 = (offset * 3) / 4;
236 switch (offset & 0x3) {
238 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
242 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
244 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
246 ret = (val2 << 4) | (val1 >> 12);
249 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
251 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
253 ret = (val2 << 8) | (val1 >> 8);
256 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
257 ret = (ret & 0xfff0) >> 4;
264 debug("FAT%d: ret: %08x, offset: %04x\n",
265 mydata->fatsize, ret, offset);
271 * Read at most 'size' bytes from the specified cluster into 'buffer'.
272 * Return 0 on success, -1 otherwise.
275 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
282 startsect = mydata->data_begin +
283 clustnum * mydata->clust_size;
285 startsect = mydata->rootdir_sect;
288 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
290 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
291 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
293 printf("FAT: Misaligned buffer address (%p)\n", buffer);
295 while (size >= mydata->sect_size) {
296 ret = disk_read(startsect++, 1, tmpbuf);
298 debug("Error reading data (got %d)\n", ret);
302 memcpy(buffer, tmpbuf, mydata->sect_size);
303 buffer += mydata->sect_size;
304 size -= mydata->sect_size;
307 idx = size / mydata->sect_size;
308 ret = disk_read(startsect, idx, buffer);
310 debug("Error reading data (got %d)\n", ret);
314 idx *= mydata->sect_size;
319 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
321 ret = disk_read(startsect, 1, tmpbuf);
323 debug("Error reading data (got %d)\n", ret);
327 memcpy(buffer, tmpbuf, size);
334 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
336 * Return the number of bytes read or -1 on fatal errors.
338 __u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
339 __aligned(ARCH_DMA_MINALIGN);
342 get_contents(fsdata *mydata, dir_entry *dentptr, unsigned long pos,
343 __u8 *buffer, unsigned long maxsize)
345 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
346 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
347 __u32 curclust = START(dentptr);
348 __u32 endclust, newclust;
349 unsigned long actsize;
351 debug("Filesize: %ld bytes\n", filesize);
353 if (pos >= filesize) {
354 debug("Read position past EOF: %lu\n", pos);
358 if (maxsize > 0 && filesize > pos + maxsize)
359 filesize = pos + maxsize;
361 debug("%ld bytes\n", filesize);
363 actsize = bytesperclust;
365 /* go to cluster at pos */
366 while (actsize <= pos) {
367 curclust = get_fatent(mydata, curclust);
368 if (CHECK_CLUST(curclust, mydata->fatsize)) {
369 debug("curclust: 0x%x\n", curclust);
370 debug("Invalid FAT entry\n");
373 actsize += bytesperclust;
377 actsize -= bytesperclust;
381 /* align to beginning of next cluster if any */
383 actsize = min(filesize, bytesperclust);
384 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
385 (int)actsize) != 0) {
386 printf("Error reading cluster\n");
391 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
397 curclust = get_fatent(mydata, curclust);
398 if (CHECK_CLUST(curclust, mydata->fatsize)) {
399 debug("curclust: 0x%x\n", curclust);
400 debug("Invalid FAT entry\n");
405 actsize = bytesperclust;
409 /* search for consecutive clusters */
410 while (actsize < filesize) {
411 newclust = get_fatent(mydata, endclust);
412 if ((newclust - 1) != endclust)
414 if (CHECK_CLUST(newclust, mydata->fatsize)) {
415 debug("curclust: 0x%x\n", newclust);
416 debug("Invalid FAT entry\n");
420 actsize += bytesperclust;
423 /* get remaining bytes */
425 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
426 printf("Error reading cluster\n");
432 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
433 printf("Error reading cluster\n");
436 gotsize += (int)actsize;
440 curclust = get_fatent(mydata, endclust);
441 if (CHECK_CLUST(curclust, mydata->fatsize)) {
442 debug("curclust: 0x%x\n", curclust);
443 printf("Invalid FAT entry\n");
446 actsize = bytesperclust;
452 * Extract the file name information from 'slotptr' into 'l_name',
453 * starting at l_name[*idx].
454 * Return 1 if terminator (zero byte) is found, 0 otherwise.
456 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
460 for (j = 0; j <= 8; j += 2) {
461 l_name[*idx] = slotptr->name0_4[j];
462 if (l_name[*idx] == 0x00)
466 for (j = 0; j <= 10; j += 2) {
467 l_name[*idx] = slotptr->name5_10[j];
468 if (l_name[*idx] == 0x00)
472 for (j = 0; j <= 2; j += 2) {
473 l_name[*idx] = slotptr->name11_12[j];
474 if (l_name[*idx] == 0x00)
483 * Extract the full long filename starting at 'retdent' (which is really
484 * a slot) into 'l_name'. If successful also copy the real directory entry
486 * Return 0 on success, -1 otherwise.
489 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
490 dir_entry *retdent, char *l_name)
493 dir_slot *slotptr = (dir_slot *)retdent;
494 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
497 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
500 if (counter > VFAT_MAXSEQ) {
501 debug("Error: VFAT name is too long\n");
505 while ((__u8 *)slotptr < buflimit) {
508 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
514 if ((__u8 *)slotptr >= buflimit) {
519 curclust = get_fatent(mydata, curclust);
520 if (CHECK_CLUST(curclust, mydata->fatsize)) {
521 debug("curclust: 0x%x\n", curclust);
522 printf("Invalid FAT entry\n");
526 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
527 mydata->clust_size * mydata->sect_size) != 0) {
528 debug("Error: reading directory block\n");
532 slotptr2 = (dir_slot *)get_contents_vfatname_block;
533 while (counter > 0) {
534 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
541 /* Save the real directory entry */
542 realdent = (dir_entry *)slotptr2;
543 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
545 slot2str(slotptr2, l_name, &idx);
548 /* Save the real directory entry */
549 realdent = (dir_entry *)slotptr;
554 if (slot2str(slotptr, l_name, &idx))
556 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
559 if (*l_name == DELETED_FLAG)
561 else if (*l_name == aRING)
562 *l_name = DELETED_FLAG;
565 /* Return the real directory entry */
566 memcpy(retdent, realdent, sizeof(dir_entry));
571 /* Calculate short name checksum */
572 static __u8 mkcksum(const char name[8], const char ext[3])
578 for (i = 0; i < 8; i++)
579 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
580 for (i = 0; i < 3; i++)
581 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
587 * Get the directory entry associated with 'filename' from the directory
588 * starting at 'startsect'
590 __u8 get_dentfromdir_block[MAX_CLUSTSIZE]
591 __aligned(ARCH_DMA_MINALIGN);
593 static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
594 char *filename, dir_entry *retdent,
597 __u16 prevcksum = 0xffff;
598 __u32 curclust = START(retdent);
599 int files = 0, dirs = 0;
601 debug("get_dentfromdir: %s\n", filename);
608 if (get_cluster(mydata, curclust, get_dentfromdir_block,
609 mydata->clust_size * mydata->sect_size) != 0) {
610 debug("Error: reading directory block\n");
614 dentptr = (dir_entry *)get_dentfromdir_block;
616 for (i = 0; i < DIRENTSPERCLUST; i++) {
617 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
620 if (dentptr->name[0] == DELETED_FLAG) {
624 if ((dentptr->attr & ATTR_VOLUME)) {
626 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
627 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
628 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
629 get_vfatname(mydata, curclust,
630 get_dentfromdir_block,
637 isdir = (dentptr->attr & ATTR_DIR);
645 if (l_name[0] != 0) {
652 printf(" %8ld %s%c\n",
653 (long)FAT2CPU32(dentptr->size),
665 debug("vfatname: |%s|\n", l_name);
667 /* Volume label or VFAT entry */
672 if (dentptr->name[0] == 0) {
674 printf("\n%d file(s), %d dir(s)\n\n",
677 debug("Dentname == NULL - %d\n", i);
681 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
682 if (dols && csum == prevcksum) {
689 get_name(dentptr, s_name);
691 int isdir = (dentptr->attr & ATTR_DIR);
701 if (s_name[0] != 0) {
709 printf(" %8ld %s%c\n",
710 (long)FAT2CPU32(dentptr->size),
722 if (strcmp(filename, s_name)
723 && strcmp(filename, l_name)) {
724 debug("Mismatch: |%s|%s|\n", s_name, l_name);
729 memcpy(retdent, dentptr, sizeof(dir_entry));
731 debug("DentName: %s", s_name);
732 debug(", start: 0x%x", START(dentptr));
733 debug(", size: 0x%x %s\n",
734 FAT2CPU32(dentptr->size),
735 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
740 curclust = get_fatent(mydata, curclust);
741 if (CHECK_CLUST(curclust, mydata->fatsize)) {
742 debug("curclust: 0x%x\n", curclust);
743 printf("Invalid FAT entry\n");
752 * Read boot sector and volume info from a FAT filesystem
755 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
758 volume_info *vistart;
761 if (cur_dev == NULL) {
762 debug("Error: no device selected\n");
766 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
768 debug("Error: allocating block\n");
772 if (disk_read(0, 1, block) < 0) {
773 debug("Error: reading block\n");
777 memcpy(bs, block, sizeof(boot_sector));
778 bs->reserved = FAT2CPU16(bs->reserved);
779 bs->fat_length = FAT2CPU16(bs->fat_length);
780 bs->secs_track = FAT2CPU16(bs->secs_track);
781 bs->heads = FAT2CPU16(bs->heads);
782 bs->total_sect = FAT2CPU32(bs->total_sect);
785 if (bs->fat_length == 0) {
787 bs->fat32_length = FAT2CPU32(bs->fat32_length);
788 bs->flags = FAT2CPU16(bs->flags);
789 bs->root_cluster = FAT2CPU32(bs->root_cluster);
790 bs->info_sector = FAT2CPU16(bs->info_sector);
791 bs->backup_boot = FAT2CPU16(bs->backup_boot);
792 vistart = (volume_info *)(block + sizeof(boot_sector));
795 vistart = (volume_info *)&(bs->fat32_length);
798 memcpy(volinfo, vistart, sizeof(volume_info));
800 if (*fatsize == 32) {
801 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
804 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
808 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
814 debug("Error: broken fs_type sign\n");
822 __u8 do_fat_read_at_block[MAX_CLUSTSIZE]
823 __aligned(ARCH_DMA_MINALIGN);
826 do_fat_read_at(const char *filename, unsigned long pos, void *buffer,
827 unsigned long maxsize, int dols)
829 char fnamecopy[2048];
833 fsdata *mydata = &datablock;
834 dir_entry *dentptr = NULL;
835 __u16 prevcksum = 0xffff;
839 int files = 0, dirs = 0;
842 __u32 root_cluster = 0;
843 int rootdir_size = 0;
846 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
847 debug("Error: reading boot sector\n");
851 if (mydata->fatsize == 32) {
852 root_cluster = bs.root_cluster;
853 mydata->fatlength = bs.fat32_length;
855 mydata->fatlength = bs.fat_length;
858 mydata->fat_sect = bs.reserved;
860 cursect = mydata->rootdir_sect
861 = mydata->fat_sect + mydata->fatlength * bs.fats;
863 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
864 mydata->clust_size = bs.cluster_size;
865 if (mydata->sect_size != cur_part_info.blksz) {
866 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
867 mydata->sect_size, cur_part_info.blksz);
871 if (mydata->fatsize == 32) {
872 mydata->data_begin = mydata->rootdir_sect -
873 (mydata->clust_size * 2);
875 rootdir_size = ((bs.dir_entries[1] * (int)256 +
879 mydata->data_begin = mydata->rootdir_sect +
881 (mydata->clust_size * 2);
884 mydata->fatbufnum = -1;
885 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
886 if (mydata->fatbuf == NULL) {
887 debug("Error: allocating memory\n");
892 debug("VFAT Support enabled\n");
894 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
895 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
896 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
897 "Data begins at: %d\n",
899 mydata->rootdir_sect,
900 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
901 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
904 /* "cwd" is always the root... */
905 while (ISDIRDELIM(*filename))
908 /* Make a copy of the filename and convert it to lowercase */
909 strcpy(fnamecopy, filename);
912 if (*fnamecopy == '\0') {
917 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
919 fnamecopy[idx] = '\0';
920 subname = fnamecopy + idx + 1;
922 /* Handle multiple delimiters */
923 while (ISDIRDELIM(*subname))
934 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
935 cursect, mydata->clust_size, DIRENTSPERBLOCK);
937 if (disk_read(cursect,
938 (mydata->fatsize == 32) ?
939 (mydata->clust_size) :
941 do_fat_read_at_block) < 0) {
942 debug("Error: reading rootdir block\n");
946 dentptr = (dir_entry *) do_fat_read_at_block;
949 for (i = 0; i < DIRENTSPERBLOCK; i++) {
950 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
954 if (dentptr->name[0] == DELETED_FLAG) {
960 csum = mkcksum(dentptr->name, dentptr->ext);
962 if (dentptr->attr & ATTR_VOLUME) {
964 (dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
965 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
967 ((dir_slot *)dentptr)->alias_checksum;
971 do_fat_read_at_block,
974 if (dols == LS_ROOT) {
978 (dentptr->attr & ATTR_DIR);
986 if (l_name[0] != 0) {
993 printf(" %8ld %s%c\n",
994 (long)FAT2CPU32(dentptr->size),
1006 debug("Rootvfatname: |%s|\n",
1009 /* Volume label or VFAT entry */
1013 } else if (dentptr->name[0] == 0) {
1014 debug("RootDentname == NULL - %d\n", i);
1015 if (dols == LS_ROOT) {
1016 printf("\n%d file(s), %d dir(s)\n\n",
1022 else if (vfat_enabled &&
1023 dols == LS_ROOT && csum == prevcksum) {
1029 get_name(dentptr, s_name);
1031 if (dols == LS_ROOT) {
1032 int isdir = (dentptr->attr & ATTR_DIR);
1038 if (s_name[0] != 0) {
1044 if (s_name[0] != 0) {
1051 printf(" %8ld %s%c\n",
1052 (long)FAT2CPU32(dentptr->size),
1063 if (strcmp(fnamecopy, s_name)
1064 && strcmp(fnamecopy, l_name)) {
1065 debug("RootMismatch: |%s|%s|\n", s_name,
1071 if (isdir && !(dentptr->attr & ATTR_DIR))
1074 debug("RootName: %s", s_name);
1075 debug(", start: 0x%x", START(dentptr));
1076 debug(", size: 0x%x %s\n",
1077 FAT2CPU32(dentptr->size),
1078 isdir ? "(DIR)" : "");
1080 goto rootdir_done; /* We got a match */
1082 debug("END LOOP: j=%d clust_size=%d\n", j,
1083 mydata->clust_size);
1086 * On FAT32 we must fetch the FAT entries for the next
1087 * root directory clusters when a cluster has been
1088 * completely processed.
1091 int rootdir_end = 0;
1092 if (mydata->fatsize == 32) {
1093 if (j == mydata->clust_size) {
1097 nxt_clust = get_fatent(mydata, root_cluster);
1098 rootdir_end = CHECK_CLUST(nxt_clust, 32);
1100 nxtsect = mydata->data_begin +
1101 (nxt_clust * mydata->clust_size);
1103 root_cluster = nxt_clust;
1109 if (j == PREFETCH_BLOCKS)
1112 rootdir_end = (++cursect - mydata->rootdir_sect >=
1116 /* If end of rootdir reached */
1118 if (dols == LS_ROOT) {
1119 printf("\n%d file(s), %d dir(s)\n\n",
1131 int startsect = mydata->data_begin
1132 + START(dentptr) * mydata->clust_size;
1134 char *nextname = NULL;
1139 idx = dirdelim(subname);
1142 subname[idx] = '\0';
1143 nextname = subname + idx + 1;
1144 /* Handle multiple delimiters */
1145 while (ISDIRDELIM(*nextname))
1147 if (dols && *nextname == '\0')
1150 if (dols && firsttime) {
1157 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1158 isdir ? 0 : dols) == NULL) {
1164 if (isdir && !(dentptr->attr & ATTR_DIR))
1171 ret = get_contents(mydata, dentptr, pos, buffer, maxsize);
1172 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
1175 free(mydata->fatbuf);
1180 do_fat_read(const char *filename, void *buffer, unsigned long maxsize, int dols)
1182 return do_fat_read_at(filename, 0, buffer, maxsize, dols);
1185 int file_fat_detectfs(void)
1188 volume_info volinfo;
1192 if (cur_dev == NULL) {
1193 printf("No current device\n");
1197 #if defined(CONFIG_CMD_IDE) || \
1198 defined(CONFIG_CMD_SATA) || \
1199 defined(CONFIG_CMD_SCSI) || \
1200 defined(CONFIG_CMD_USB) || \
1202 printf("Interface: ");
1203 switch (cur_dev->if_type) {
1229 printf("\n Device %d: ", cur_dev->dev);
1233 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1234 printf("\nNo valid FAT fs found\n");
1238 memcpy(vol_label, volinfo.volume_label, 11);
1239 vol_label[11] = '\0';
1240 volinfo.fs_type[5] = '\0';
1242 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1247 int file_fat_ls(const char *dir)
1249 return do_fat_read(dir, NULL, 0, LS_YES);
1252 long file_fat_read_at(const char *filename, unsigned long pos, void *buffer,
1253 unsigned long maxsize)
1255 printf("reading %s\n", filename);
1256 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO);
1259 long file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1261 return file_fat_read_at(filename, 0, buffer, maxsize);
1264 int fat_read_file(const char *filename, void *buf, int offset, int len)
1268 len_read = file_fat_read_at(filename, offset, buf, len);
1269 if (len_read == -1) {
1270 printf("** Unable to read file %s **\n", filename);
1277 void fat_close(void)