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>
38 * Convert a string to lowercase.
40 static void downcase(char *str)
42 while (*str != '\0') {
48 static block_dev_desc_t *cur_dev;
49 static disk_partition_t cur_part_info;
51 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
52 #define DOS_FS_TYPE_OFFSET 0x36
53 #define DOS_FS32_TYPE_OFFSET 0x52
55 static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
57 if (!cur_dev || !cur_dev->block_read)
60 return cur_dev->block_read(cur_dev->dev,
61 cur_part_info.start + block, nr_blocks, buf);
64 int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info)
66 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
69 cur_part_info = *info;
71 /* Make sure it has a valid FAT header */
72 if (disk_read(0, 1, buffer) != 1) {
77 /* Check if it's actually a DOS volume */
78 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
83 /* Check for FAT12/FAT16/FAT32 filesystem */
84 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
86 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
93 int fat_register_device(block_dev_desc_t *dev_desc, int part_no)
95 disk_partition_t info;
97 /* First close any currently found FAT filesystem */
100 /* Read the partition table, if present */
101 if (get_partition_info(dev_desc, part_no, &info)) {
103 printf("** Partition %d not valid on device %d **\n",
104 part_no, dev_desc->dev);
109 info.size = dev_desc->lba;
110 info.blksz = dev_desc->blksz;
114 #ifdef CONFIG_PARTITION_UUIDS
119 return fat_set_blk_dev(dev_desc, &info);
123 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
124 * Return index into string if found, -1 otherwise.
126 static int dirdelim(char *str)
130 while (*str != '\0') {
131 if (ISDIRDELIM(*str))
139 * Extract zero terminated short name from a directory entry.
141 static void get_name(dir_entry *dirent, char *s_name)
145 memcpy(s_name, dirent->name, 8);
148 while (*ptr && *ptr != ' ')
150 if (dirent->ext[0] && dirent->ext[0] != ' ') {
153 memcpy(ptr, dirent->ext, 3);
155 while (*ptr && *ptr != ' ')
159 if (*s_name == DELETED_FLAG)
161 else if (*s_name == aRING)
162 *s_name = DELETED_FLAG;
167 * Get the entry at index 'entry' in a FAT (12/16/32) table.
168 * On failure 0x00 is returned.
170 static __u32 get_fatent(fsdata *mydata, __u32 entry)
177 switch (mydata->fatsize) {
179 bufnum = entry / FAT32BUFSIZE;
180 offset = entry - bufnum * FAT32BUFSIZE;
183 bufnum = entry / FAT16BUFSIZE;
184 offset = entry - bufnum * FAT16BUFSIZE;
187 bufnum = entry / FAT12BUFSIZE;
188 offset = entry - bufnum * FAT12BUFSIZE;
192 /* Unsupported FAT size */
196 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
197 mydata->fatsize, entry, entry, offset, offset);
199 /* Read a new block of FAT entries into the cache. */
200 if (bufnum != mydata->fatbufnum) {
201 __u32 getsize = FATBUFBLOCKS;
202 __u8 *bufptr = mydata->fatbuf;
203 __u32 fatlength = mydata->fatlength;
204 __u32 startblock = bufnum * 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 * Return the number of bytes read or -1 on fatal errors.
331 __u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
332 __aligned(ARCH_DMA_MINALIGN);
335 get_contents(fsdata *mydata, dir_entry *dentptr, unsigned long pos,
336 __u8 *buffer, unsigned long maxsize)
338 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
339 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
340 __u32 curclust = START(dentptr);
341 __u32 endclust, newclust;
342 unsigned long actsize;
344 debug("Filesize: %ld bytes\n", filesize);
346 if (pos >= filesize) {
347 debug("Read position past EOF: %lu\n", pos);
351 if (maxsize > 0 && filesize > pos + maxsize)
352 filesize = pos + maxsize;
354 debug("%ld 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, 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;
444 #ifdef CONFIG_SUPPORT_VFAT
446 * Extract the file name information from 'slotptr' into 'l_name',
447 * starting at l_name[*idx].
448 * Return 1 if terminator (zero byte) is found, 0 otherwise.
450 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
454 for (j = 0; j <= 8; j += 2) {
455 l_name[*idx] = slotptr->name0_4[j];
456 if (l_name[*idx] == 0x00)
460 for (j = 0; j <= 10; j += 2) {
461 l_name[*idx] = slotptr->name5_10[j];
462 if (l_name[*idx] == 0x00)
466 for (j = 0; j <= 2; j += 2) {
467 l_name[*idx] = slotptr->name11_12[j];
468 if (l_name[*idx] == 0x00)
477 * Extract the full long filename starting at 'retdent' (which is really
478 * a slot) into 'l_name'. If successful also copy the real directory entry
480 * Return 0 on success, -1 otherwise.
483 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
484 dir_entry *retdent, char *l_name)
487 dir_slot *slotptr = (dir_slot *)retdent;
488 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
491 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
494 if (counter > VFAT_MAXSEQ) {
495 debug("Error: VFAT name is too long\n");
499 while ((__u8 *)slotptr < buflimit) {
502 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
508 if ((__u8 *)slotptr >= buflimit) {
513 curclust = get_fatent(mydata, curclust);
514 if (CHECK_CLUST(curclust, mydata->fatsize)) {
515 debug("curclust: 0x%x\n", curclust);
516 printf("Invalid FAT entry\n");
520 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
521 mydata->clust_size * mydata->sect_size) != 0) {
522 debug("Error: reading directory block\n");
526 slotptr2 = (dir_slot *)get_contents_vfatname_block;
527 while (counter > 0) {
528 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
535 /* Save the real directory entry */
536 realdent = (dir_entry *)slotptr2;
537 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
539 slot2str(slotptr2, l_name, &idx);
542 /* Save the real directory entry */
543 realdent = (dir_entry *)slotptr;
548 if (slot2str(slotptr, l_name, &idx))
550 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
553 if (*l_name == DELETED_FLAG)
555 else if (*l_name == aRING)
556 *l_name = DELETED_FLAG;
559 /* Return the real directory entry */
560 memcpy(retdent, realdent, sizeof(dir_entry));
565 /* Calculate short name checksum */
566 static __u8 mkcksum(const char name[8], const char ext[3])
572 for (i = 0; i < sizeof(name); i++)
573 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
574 for (i = 0; i < sizeof(ext); i++)
575 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
579 #endif /* CONFIG_SUPPORT_VFAT */
582 * Get the directory entry associated with 'filename' from the directory
583 * starting at 'startsect'
585 __u8 get_dentfromdir_block[MAX_CLUSTSIZE]
586 __aligned(ARCH_DMA_MINALIGN);
588 static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
589 char *filename, dir_entry *retdent,
592 __u16 prevcksum = 0xffff;
593 __u32 curclust = START(retdent);
594 int files = 0, dirs = 0;
596 debug("get_dentfromdir: %s\n", filename);
603 if (get_cluster(mydata, curclust, get_dentfromdir_block,
604 mydata->clust_size * mydata->sect_size) != 0) {
605 debug("Error: reading directory block\n");
609 dentptr = (dir_entry *)get_dentfromdir_block;
611 for (i = 0; i < DIRENTSPERCLUST; i++) {
612 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
615 if (dentptr->name[0] == DELETED_FLAG) {
619 if ((dentptr->attr & ATTR_VOLUME)) {
620 #ifdef CONFIG_SUPPORT_VFAT
621 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
622 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
623 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
624 get_vfatname(mydata, curclust,
625 get_dentfromdir_block,
632 isdir = (dentptr->attr & ATTR_DIR);
640 if (l_name[0] != 0) {
647 printf(" %8ld %s%c\n",
648 (long)FAT2CPU32(dentptr->size),
660 debug("vfatname: |%s|\n", l_name);
664 /* Volume label or VFAT entry */
669 if (dentptr->name[0] == 0) {
671 printf("\n%d file(s), %d dir(s)\n\n",
674 debug("Dentname == NULL - %d\n", i);
677 #ifdef CONFIG_SUPPORT_VFAT
678 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
679 if (dols && csum == prevcksum) {
685 get_name(dentptr, s_name);
687 int isdir = (dentptr->attr & ATTR_DIR);
697 if (s_name[0] != 0) {
705 printf(" %8ld %s%c\n",
706 (long)FAT2CPU32(dentptr->size),
718 if (strcmp(filename, s_name)
719 && strcmp(filename, l_name)) {
720 debug("Mismatch: |%s|%s|\n", s_name, l_name);
725 memcpy(retdent, dentptr, sizeof(dir_entry));
727 debug("DentName: %s", s_name);
728 debug(", start: 0x%x", START(dentptr));
729 debug(", size: 0x%x %s\n",
730 FAT2CPU32(dentptr->size),
731 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
736 curclust = get_fatent(mydata, curclust);
737 if (CHECK_CLUST(curclust, mydata->fatsize)) {
738 debug("curclust: 0x%x\n", curclust);
739 printf("Invalid FAT entry\n");
748 * Read boot sector and volume info from a FAT filesystem
751 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
754 volume_info *vistart;
757 if (cur_dev == NULL) {
758 debug("Error: no device selected\n");
762 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
764 debug("Error: allocating block\n");
768 if (disk_read(0, 1, block) < 0) {
769 debug("Error: reading block\n");
773 memcpy(bs, block, sizeof(boot_sector));
774 bs->reserved = FAT2CPU16(bs->reserved);
775 bs->fat_length = FAT2CPU16(bs->fat_length);
776 bs->secs_track = FAT2CPU16(bs->secs_track);
777 bs->heads = FAT2CPU16(bs->heads);
778 bs->total_sect = FAT2CPU32(bs->total_sect);
781 if (bs->fat_length == 0) {
783 bs->fat32_length = FAT2CPU32(bs->fat32_length);
784 bs->flags = FAT2CPU16(bs->flags);
785 bs->root_cluster = FAT2CPU32(bs->root_cluster);
786 bs->info_sector = FAT2CPU16(bs->info_sector);
787 bs->backup_boot = FAT2CPU16(bs->backup_boot);
788 vistart = (volume_info *)(block + sizeof(boot_sector));
791 vistart = (volume_info *)&(bs->fat32_length);
794 memcpy(volinfo, vistart, sizeof(volume_info));
796 if (*fatsize == 32) {
797 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
800 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
804 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
810 debug("Error: broken fs_type sign\n");
818 __u8 do_fat_read_at_block[MAX_CLUSTSIZE]
819 __aligned(ARCH_DMA_MINALIGN);
822 do_fat_read_at(const char *filename, unsigned long pos, void *buffer,
823 unsigned long maxsize, int dols)
825 char fnamecopy[2048];
829 fsdata *mydata = &datablock;
830 dir_entry *dentptr = NULL;
831 __u16 prevcksum = 0xffff;
835 int files = 0, dirs = 0;
838 __u32 root_cluster = 0;
839 int rootdir_size = 0;
842 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
843 debug("Error: reading boot sector\n");
847 if (mydata->fatsize == 32) {
848 root_cluster = bs.root_cluster;
849 mydata->fatlength = bs.fat32_length;
851 mydata->fatlength = bs.fat_length;
854 mydata->fat_sect = bs.reserved;
856 cursect = mydata->rootdir_sect
857 = mydata->fat_sect + mydata->fatlength * bs.fats;
859 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
860 mydata->clust_size = bs.cluster_size;
861 if (mydata->sect_size != cur_part_info.blksz) {
862 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
863 mydata->sect_size, cur_part_info.blksz);
867 if (mydata->fatsize == 32) {
868 mydata->data_begin = mydata->rootdir_sect -
869 (mydata->clust_size * 2);
871 rootdir_size = ((bs.dir_entries[1] * (int)256 +
875 mydata->data_begin = mydata->rootdir_sect +
877 (mydata->clust_size * 2);
880 mydata->fatbufnum = -1;
881 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
882 if (mydata->fatbuf == NULL) {
883 debug("Error: allocating memory\n");
887 #ifdef CONFIG_SUPPORT_VFAT
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);
908 if (*fnamecopy == '\0') {
913 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
915 fnamecopy[idx] = '\0';
916 subname = fnamecopy + idx + 1;
918 /* Handle multiple delimiters */
919 while (ISDIRDELIM(*subname))
930 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
931 cursect, mydata->clust_size, DIRENTSPERBLOCK);
933 if (disk_read(cursect,
934 (mydata->fatsize == 32) ?
935 (mydata->clust_size) :
937 do_fat_read_at_block) < 0) {
938 debug("Error: reading rootdir block\n");
942 dentptr = (dir_entry *) do_fat_read_at_block;
945 for (i = 0; i < DIRENTSPERBLOCK; i++) {
946 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
950 if (dentptr->name[0] == DELETED_FLAG) {
955 csum = mkcksum(dentptr->name, dentptr->ext);
956 if (dentptr->attr & ATTR_VOLUME) {
957 #ifdef CONFIG_SUPPORT_VFAT
958 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
959 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
961 ((dir_slot *)dentptr)->alias_checksum;
965 do_fat_read_at_block,
968 if (dols == LS_ROOT) {
972 (dentptr->attr & ATTR_DIR);
980 if (l_name[0] != 0) {
987 printf(" %8ld %s%c\n",
988 (long)FAT2CPU32(dentptr->size),
1000 debug("Rootvfatname: |%s|\n",
1005 /* Volume label or VFAT entry */
1009 } else if (dentptr->name[0] == 0) {
1010 debug("RootDentname == NULL - %d\n", i);
1011 if (dols == LS_ROOT) {
1012 printf("\n%d file(s), %d dir(s)\n\n",
1018 #ifdef CONFIG_SUPPORT_VFAT
1019 else if (dols == LS_ROOT && csum == prevcksum) {
1025 get_name(dentptr, s_name);
1027 if (dols == LS_ROOT) {
1028 int isdir = (dentptr->attr & ATTR_DIR);
1034 if (s_name[0] != 0) {
1040 if (s_name[0] != 0) {
1047 printf(" %8ld %s%c\n",
1048 (long)FAT2CPU32(dentptr->size),
1059 if (strcmp(fnamecopy, s_name)
1060 && strcmp(fnamecopy, l_name)) {
1061 debug("RootMismatch: |%s|%s|\n", s_name,
1067 if (isdir && !(dentptr->attr & ATTR_DIR))
1070 debug("RootName: %s", s_name);
1071 debug(", start: 0x%x", START(dentptr));
1072 debug(", size: 0x%x %s\n",
1073 FAT2CPU32(dentptr->size),
1074 isdir ? "(DIR)" : "");
1076 goto rootdir_done; /* We got a match */
1078 debug("END LOOP: j=%d clust_size=%d\n", j,
1079 mydata->clust_size);
1082 * On FAT32 we must fetch the FAT entries for the next
1083 * root directory clusters when a cluster has been
1084 * completely processed.
1087 int rootdir_end = 0;
1088 if (mydata->fatsize == 32) {
1089 if (j == mydata->clust_size) {
1093 nxt_clust = get_fatent(mydata, root_cluster);
1094 rootdir_end = CHECK_CLUST(nxt_clust, 32);
1096 nxtsect = mydata->data_begin +
1097 (nxt_clust * mydata->clust_size);
1099 root_cluster = nxt_clust;
1105 if (j == PREFETCH_BLOCKS)
1108 rootdir_end = (++cursect - mydata->rootdir_sect >=
1112 /* If end of rootdir reached */
1114 if (dols == LS_ROOT) {
1115 printf("\n%d file(s), %d dir(s)\n\n",
1127 int startsect = mydata->data_begin
1128 + START(dentptr) * mydata->clust_size;
1130 char *nextname = NULL;
1135 idx = dirdelim(subname);
1138 subname[idx] = '\0';
1139 nextname = subname + idx + 1;
1140 /* Handle multiple delimiters */
1141 while (ISDIRDELIM(*nextname))
1143 if (dols && *nextname == '\0')
1146 if (dols && firsttime) {
1153 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1154 isdir ? 0 : dols) == NULL) {
1160 if (isdir && !(dentptr->attr & ATTR_DIR))
1167 ret = get_contents(mydata, dentptr, pos, buffer, maxsize);
1168 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
1171 free(mydata->fatbuf);
1176 do_fat_read(const char *filename, void *buffer, unsigned long maxsize, int dols)
1178 return do_fat_read_at(filename, 0, buffer, maxsize, dols);
1181 int file_fat_detectfs(void)
1184 volume_info volinfo;
1188 if (cur_dev == NULL) {
1189 printf("No current device\n");
1193 #if defined(CONFIG_CMD_IDE) || \
1194 defined(CONFIG_CMD_SATA) || \
1195 defined(CONFIG_CMD_SCSI) || \
1196 defined(CONFIG_CMD_USB) || \
1198 printf("Interface: ");
1199 switch (cur_dev->if_type) {
1225 printf("\n Device %d: ", cur_dev->dev);
1229 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1230 printf("\nNo valid FAT fs found\n");
1234 memcpy(vol_label, volinfo.volume_label, 11);
1235 vol_label[11] = '\0';
1236 volinfo.fs_type[5] = '\0';
1238 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1243 int file_fat_ls(const char *dir)
1245 return do_fat_read(dir, NULL, 0, LS_YES);
1248 long file_fat_read_at(const char *filename, unsigned long pos, void *buffer,
1249 unsigned long maxsize)
1251 printf("reading %s\n", filename);
1252 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO);
1255 long file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1257 return file_fat_read_at(filename, 0, buffer, maxsize);