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>
39 * Convert a string to lowercase.
41 static void downcase(char *str)
43 while (*str != '\0') {
49 static block_dev_desc_t *cur_dev;
50 static disk_partition_t cur_part_info;
52 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
53 #define DOS_FS_TYPE_OFFSET 0x36
54 #define DOS_FS32_TYPE_OFFSET 0x52
56 static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
58 if (!cur_dev || !cur_dev->block_read)
61 return cur_dev->block_read(cur_dev->dev,
62 cur_part_info.start + block, nr_blocks, buf);
65 int fat_set_blk_dev(block_dev_desc_t *dev_desc, disk_partition_t *info)
67 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
70 cur_part_info = *info;
72 /* Make sure it has a valid FAT header */
73 if (disk_read(0, 1, buffer) != 1) {
78 /* Check if it's actually a DOS volume */
79 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
84 /* Check for FAT12/FAT16/FAT32 filesystem */
85 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
87 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
94 int fat_register_device(block_dev_desc_t *dev_desc, int part_no)
96 disk_partition_t info;
98 /* First close any currently found FAT filesystem */
101 /* Read the partition table, if present */
102 if (get_partition_info(dev_desc, part_no, &info)) {
104 printf("** Partition %d not valid on device %d **\n",
105 part_no, dev_desc->dev);
110 info.size = dev_desc->lba;
111 info.blksz = dev_desc->blksz;
115 #ifdef CONFIG_PARTITION_UUIDS
120 return fat_set_blk_dev(dev_desc, &info);
124 * Get the first occurence of a directory delimiter ('/' or '\') in a string.
125 * Return index into string if found, -1 otherwise.
127 static int dirdelim(char *str)
131 while (*str != '\0') {
132 if (ISDIRDELIM(*str))
140 * Extract zero terminated short name from a directory entry.
142 static void get_name(dir_entry *dirent, char *s_name)
146 memcpy(s_name, dirent->name, 8);
149 while (*ptr && *ptr != ' ')
151 if (dirent->ext[0] && dirent->ext[0] != ' ') {
154 memcpy(ptr, dirent->ext, 3);
156 while (*ptr && *ptr != ' ')
160 if (*s_name == DELETED_FLAG)
162 else if (*s_name == aRING)
163 *s_name = DELETED_FLAG;
168 * Get the entry at index 'entry' in a FAT (12/16/32) table.
169 * On failure 0x00 is returned.
171 static __u32 get_fatent(fsdata *mydata, __u32 entry)
178 switch (mydata->fatsize) {
180 bufnum = entry / FAT32BUFSIZE;
181 offset = entry - bufnum * FAT32BUFSIZE;
184 bufnum = entry / FAT16BUFSIZE;
185 offset = entry - bufnum * FAT16BUFSIZE;
188 bufnum = entry / FAT12BUFSIZE;
189 offset = entry - bufnum * FAT12BUFSIZE;
193 /* Unsupported FAT size */
197 debug("FAT%d: entry: 0x%04x = %d, offset: 0x%04x = %d\n",
198 mydata->fatsize, entry, entry, offset, offset);
200 /* Read a new block of FAT entries into the cache. */
201 if (bufnum != mydata->fatbufnum) {
202 __u32 getsize = FATBUFBLOCKS;
203 __u8 *bufptr = mydata->fatbuf;
204 __u32 fatlength = mydata->fatlength;
205 __u32 startblock = bufnum * FATBUFBLOCKS;
207 if (startblock + getsize > fatlength)
208 getsize = fatlength - startblock;
210 startblock += mydata->fat_sect; /* Offset from start of disk */
212 if (disk_read(startblock, getsize, bufptr) < 0) {
213 debug("Error reading FAT blocks\n");
216 mydata->fatbufnum = bufnum;
219 /* Get the actual entry from the table */
220 switch (mydata->fatsize) {
222 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
225 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
228 off16 = (offset * 3) / 4;
230 switch (offset & 0x3) {
232 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[off16]);
236 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
238 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
240 ret = (val2 << 4) | (val1 >> 12);
243 val1 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
245 val2 = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16 + 1]);
247 ret = (val2 << 8) | (val1 >> 8);
250 ret = FAT2CPU16(((__u16 *)mydata->fatbuf)[off16]);
251 ret = (ret & 0xfff0) >> 4;
258 debug("FAT%d: ret: %08x, offset: %04x\n",
259 mydata->fatsize, ret, offset);
265 * Read at most 'size' bytes from the specified cluster into 'buffer'.
266 * Return 0 on success, -1 otherwise.
269 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
276 startsect = mydata->data_begin +
277 clustnum * mydata->clust_size;
279 startsect = mydata->rootdir_sect;
282 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
284 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
285 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
287 printf("FAT: Misaligned buffer address (%p)\n", buffer);
289 while (size >= mydata->sect_size) {
290 ret = disk_read(startsect++, 1, tmpbuf);
292 debug("Error reading data (got %d)\n", ret);
296 memcpy(buffer, tmpbuf, mydata->sect_size);
297 buffer += mydata->sect_size;
298 size -= mydata->sect_size;
301 idx = size / mydata->sect_size;
302 ret = disk_read(startsect, idx, buffer);
304 debug("Error reading data (got %d)\n", ret);
308 idx *= mydata->sect_size;
313 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
315 ret = disk_read(startsect, 1, tmpbuf);
317 debug("Error reading data (got %d)\n", ret);
321 memcpy(buffer, tmpbuf, size);
328 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
330 * Return the number of bytes read or -1 on fatal errors.
332 __u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
333 __aligned(ARCH_DMA_MINALIGN);
336 get_contents(fsdata *mydata, dir_entry *dentptr, unsigned long pos,
337 __u8 *buffer, unsigned long maxsize)
339 unsigned long filesize = FAT2CPU32(dentptr->size), gotsize = 0;
340 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
341 __u32 curclust = START(dentptr);
342 __u32 endclust, newclust;
343 unsigned long actsize;
345 debug("Filesize: %ld bytes\n", filesize);
347 if (pos >= filesize) {
348 debug("Read position past EOF: %lu\n", pos);
352 if (maxsize > 0 && filesize > pos + maxsize)
353 filesize = pos + maxsize;
355 debug("%ld bytes\n", filesize);
357 actsize = bytesperclust;
359 /* go to cluster at pos */
360 while (actsize <= pos) {
361 curclust = get_fatent(mydata, curclust);
362 if (CHECK_CLUST(curclust, mydata->fatsize)) {
363 debug("curclust: 0x%x\n", curclust);
364 debug("Invalid FAT entry\n");
367 actsize += bytesperclust;
371 actsize -= bytesperclust;
375 /* align to beginning of next cluster if any */
377 actsize = min(filesize, bytesperclust);
378 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
379 (int)actsize) != 0) {
380 printf("Error reading cluster\n");
385 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
391 curclust = get_fatent(mydata, curclust);
392 if (CHECK_CLUST(curclust, mydata->fatsize)) {
393 debug("curclust: 0x%x\n", curclust);
394 debug("Invalid FAT entry\n");
399 actsize = bytesperclust;
403 /* search for consecutive clusters */
404 while (actsize < filesize) {
405 newclust = get_fatent(mydata, endclust);
406 if ((newclust - 1) != endclust)
408 if (CHECK_CLUST(newclust, mydata->fatsize)) {
409 debug("curclust: 0x%x\n", newclust);
410 debug("Invalid FAT entry\n");
414 actsize += bytesperclust;
417 /* get remaining bytes */
419 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
420 printf("Error reading cluster\n");
426 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
427 printf("Error reading cluster\n");
430 gotsize += (int)actsize;
434 curclust = get_fatent(mydata, endclust);
435 if (CHECK_CLUST(curclust, mydata->fatsize)) {
436 debug("curclust: 0x%x\n", curclust);
437 printf("Invalid FAT entry\n");
440 actsize = bytesperclust;
445 #ifdef CONFIG_SUPPORT_VFAT
447 * Extract the file name information from 'slotptr' into 'l_name',
448 * starting at l_name[*idx].
449 * Return 1 if terminator (zero byte) is found, 0 otherwise.
451 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
455 for (j = 0; j <= 8; j += 2) {
456 l_name[*idx] = slotptr->name0_4[j];
457 if (l_name[*idx] == 0x00)
461 for (j = 0; j <= 10; j += 2) {
462 l_name[*idx] = slotptr->name5_10[j];
463 if (l_name[*idx] == 0x00)
467 for (j = 0; j <= 2; j += 2) {
468 l_name[*idx] = slotptr->name11_12[j];
469 if (l_name[*idx] == 0x00)
478 * Extract the full long filename starting at 'retdent' (which is really
479 * a slot) into 'l_name'. If successful also copy the real directory entry
481 * Return 0 on success, -1 otherwise.
484 get_vfatname(fsdata *mydata, int curclust, __u8 *cluster,
485 dir_entry *retdent, char *l_name)
488 dir_slot *slotptr = (dir_slot *)retdent;
489 __u8 *buflimit = cluster + mydata->sect_size * ((curclust == 0) ?
492 __u8 counter = (slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff;
495 if (counter > VFAT_MAXSEQ) {
496 debug("Error: VFAT name is too long\n");
500 while ((__u8 *)slotptr < buflimit) {
503 if (((slotptr->id & ~LAST_LONG_ENTRY_MASK) & 0xff) != counter)
509 if ((__u8 *)slotptr >= buflimit) {
514 curclust = get_fatent(mydata, curclust);
515 if (CHECK_CLUST(curclust, mydata->fatsize)) {
516 debug("curclust: 0x%x\n", curclust);
517 printf("Invalid FAT entry\n");
521 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
522 mydata->clust_size * mydata->sect_size) != 0) {
523 debug("Error: reading directory block\n");
527 slotptr2 = (dir_slot *)get_contents_vfatname_block;
528 while (counter > 0) {
529 if (((slotptr2->id & ~LAST_LONG_ENTRY_MASK)
536 /* Save the real directory entry */
537 realdent = (dir_entry *)slotptr2;
538 while ((__u8 *)slotptr2 > get_contents_vfatname_block) {
540 slot2str(slotptr2, l_name, &idx);
543 /* Save the real directory entry */
544 realdent = (dir_entry *)slotptr;
549 if (slot2str(slotptr, l_name, &idx))
551 } while (!(slotptr->id & LAST_LONG_ENTRY_MASK));
554 if (*l_name == DELETED_FLAG)
556 else if (*l_name == aRING)
557 *l_name = DELETED_FLAG;
560 /* Return the real directory entry */
561 memcpy(retdent, realdent, sizeof(dir_entry));
566 /* Calculate short name checksum */
567 static __u8 mkcksum(const char name[8], const char ext[3])
573 for (i = 0; i < 8; i++)
574 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
575 for (i = 0; i < 3; i++)
576 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
580 #endif /* CONFIG_SUPPORT_VFAT */
583 * Get the directory entry associated with 'filename' from the directory
584 * starting at 'startsect'
586 __u8 get_dentfromdir_block[MAX_CLUSTSIZE]
587 __aligned(ARCH_DMA_MINALIGN);
589 static dir_entry *get_dentfromdir(fsdata *mydata, int startsect,
590 char *filename, dir_entry *retdent,
593 __u16 prevcksum = 0xffff;
594 __u32 curclust = START(retdent);
595 int files = 0, dirs = 0;
597 debug("get_dentfromdir: %s\n", filename);
604 if (get_cluster(mydata, curclust, get_dentfromdir_block,
605 mydata->clust_size * mydata->sect_size) != 0) {
606 debug("Error: reading directory block\n");
610 dentptr = (dir_entry *)get_dentfromdir_block;
612 for (i = 0; i < DIRENTSPERCLUST; i++) {
613 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
616 if (dentptr->name[0] == DELETED_FLAG) {
620 if ((dentptr->attr & ATTR_VOLUME)) {
621 #ifdef CONFIG_SUPPORT_VFAT
622 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
623 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
624 prevcksum = ((dir_slot *)dentptr)->alias_checksum;
625 get_vfatname(mydata, curclust,
626 get_dentfromdir_block,
633 isdir = (dentptr->attr & ATTR_DIR);
641 if (l_name[0] != 0) {
648 printf(" %8ld %s%c\n",
649 (long)FAT2CPU32(dentptr->size),
661 debug("vfatname: |%s|\n", l_name);
665 /* Volume label or VFAT entry */
670 if (dentptr->name[0] == 0) {
672 printf("\n%d file(s), %d dir(s)\n\n",
675 debug("Dentname == NULL - %d\n", i);
678 #ifdef CONFIG_SUPPORT_VFAT
679 __u8 csum = mkcksum(dentptr->name, dentptr->ext);
680 if (dols && csum == prevcksum) {
686 get_name(dentptr, s_name);
688 int isdir = (dentptr->attr & ATTR_DIR);
698 if (s_name[0] != 0) {
706 printf(" %8ld %s%c\n",
707 (long)FAT2CPU32(dentptr->size),
719 if (strcmp(filename, s_name)
720 && strcmp(filename, l_name)) {
721 debug("Mismatch: |%s|%s|\n", s_name, l_name);
726 memcpy(retdent, dentptr, sizeof(dir_entry));
728 debug("DentName: %s", s_name);
729 debug(", start: 0x%x", START(dentptr));
730 debug(", size: 0x%x %s\n",
731 FAT2CPU32(dentptr->size),
732 (dentptr->attr & ATTR_DIR) ? "(DIR)" : "");
737 curclust = get_fatent(mydata, curclust);
738 if (CHECK_CLUST(curclust, mydata->fatsize)) {
739 debug("curclust: 0x%x\n", curclust);
740 printf("Invalid FAT entry\n");
749 * Read boot sector and volume info from a FAT filesystem
752 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
755 volume_info *vistart;
758 if (cur_dev == NULL) {
759 debug("Error: no device selected\n");
763 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
765 debug("Error: allocating block\n");
769 if (disk_read(0, 1, block) < 0) {
770 debug("Error: reading block\n");
774 memcpy(bs, block, sizeof(boot_sector));
775 bs->reserved = FAT2CPU16(bs->reserved);
776 bs->fat_length = FAT2CPU16(bs->fat_length);
777 bs->secs_track = FAT2CPU16(bs->secs_track);
778 bs->heads = FAT2CPU16(bs->heads);
779 bs->total_sect = FAT2CPU32(bs->total_sect);
782 if (bs->fat_length == 0) {
784 bs->fat32_length = FAT2CPU32(bs->fat32_length);
785 bs->flags = FAT2CPU16(bs->flags);
786 bs->root_cluster = FAT2CPU32(bs->root_cluster);
787 bs->info_sector = FAT2CPU16(bs->info_sector);
788 bs->backup_boot = FAT2CPU16(bs->backup_boot);
789 vistart = (volume_info *)(block + sizeof(boot_sector));
792 vistart = (volume_info *)&(bs->fat32_length);
795 memcpy(volinfo, vistart, sizeof(volume_info));
797 if (*fatsize == 32) {
798 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
801 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
805 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
811 debug("Error: broken fs_type sign\n");
819 __u8 do_fat_read_at_block[MAX_CLUSTSIZE]
820 __aligned(ARCH_DMA_MINALIGN);
823 do_fat_read_at(const char *filename, unsigned long pos, void *buffer,
824 unsigned long maxsize, int dols)
826 char fnamecopy[2048];
830 fsdata *mydata = &datablock;
831 dir_entry *dentptr = NULL;
832 __u16 prevcksum = 0xffff;
836 int files = 0, dirs = 0;
839 __u32 root_cluster = 0;
840 int rootdir_size = 0;
843 if (read_bootsectandvi(&bs, &volinfo, &mydata->fatsize)) {
844 debug("Error: reading boot sector\n");
848 if (mydata->fatsize == 32) {
849 root_cluster = bs.root_cluster;
850 mydata->fatlength = bs.fat32_length;
852 mydata->fatlength = bs.fat_length;
855 mydata->fat_sect = bs.reserved;
857 cursect = mydata->rootdir_sect
858 = mydata->fat_sect + mydata->fatlength * bs.fats;
860 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
861 mydata->clust_size = bs.cluster_size;
862 if (mydata->sect_size != cur_part_info.blksz) {
863 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
864 mydata->sect_size, cur_part_info.blksz);
868 if (mydata->fatsize == 32) {
869 mydata->data_begin = mydata->rootdir_sect -
870 (mydata->clust_size * 2);
872 rootdir_size = ((bs.dir_entries[1] * (int)256 +
876 mydata->data_begin = mydata->rootdir_sect +
878 (mydata->clust_size * 2);
881 mydata->fatbufnum = -1;
882 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
883 if (mydata->fatbuf == NULL) {
884 debug("Error: allocating memory\n");
888 #ifdef CONFIG_SUPPORT_VFAT
889 debug("VFAT Support enabled\n");
891 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
892 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
893 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
894 "Data begins at: %d\n",
896 mydata->rootdir_sect,
897 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
898 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
901 /* "cwd" is always the root... */
902 while (ISDIRDELIM(*filename))
905 /* Make a copy of the filename and convert it to lowercase */
906 strcpy(fnamecopy, filename);
909 if (*fnamecopy == '\0') {
914 } else if ((idx = dirdelim(fnamecopy)) >= 0) {
916 fnamecopy[idx] = '\0';
917 subname = fnamecopy + idx + 1;
919 /* Handle multiple delimiters */
920 while (ISDIRDELIM(*subname))
931 debug("FAT read sect=%d, clust_size=%d, DIRENTSPERBLOCK=%zd\n",
932 cursect, mydata->clust_size, DIRENTSPERBLOCK);
934 if (disk_read(cursect,
935 (mydata->fatsize == 32) ?
936 (mydata->clust_size) :
938 do_fat_read_at_block) < 0) {
939 debug("Error: reading rootdir block\n");
943 dentptr = (dir_entry *) do_fat_read_at_block;
946 for (i = 0; i < DIRENTSPERBLOCK; i++) {
947 char s_name[14], l_name[VFAT_MAXLEN_BYTES];
951 if (dentptr->name[0] == DELETED_FLAG) {
956 csum = mkcksum(dentptr->name, dentptr->ext);
957 if (dentptr->attr & ATTR_VOLUME) {
958 #ifdef CONFIG_SUPPORT_VFAT
959 if ((dentptr->attr & ATTR_VFAT) == ATTR_VFAT &&
960 (dentptr->name[0] & LAST_LONG_ENTRY_MASK)) {
962 ((dir_slot *)dentptr)->alias_checksum;
966 do_fat_read_at_block,
969 if (dols == LS_ROOT) {
973 (dentptr->attr & ATTR_DIR);
981 if (l_name[0] != 0) {
988 printf(" %8ld %s%c\n",
989 (long)FAT2CPU32(dentptr->size),
1001 debug("Rootvfatname: |%s|\n",
1006 /* Volume label or VFAT entry */
1010 } else if (dentptr->name[0] == 0) {
1011 debug("RootDentname == NULL - %d\n", i);
1012 if (dols == LS_ROOT) {
1013 printf("\n%d file(s), %d dir(s)\n\n",
1019 #ifdef CONFIG_SUPPORT_VFAT
1020 else if (dols == LS_ROOT && csum == prevcksum) {
1026 get_name(dentptr, s_name);
1028 if (dols == LS_ROOT) {
1029 int isdir = (dentptr->attr & ATTR_DIR);
1035 if (s_name[0] != 0) {
1041 if (s_name[0] != 0) {
1048 printf(" %8ld %s%c\n",
1049 (long)FAT2CPU32(dentptr->size),
1060 if (strcmp(fnamecopy, s_name)
1061 && strcmp(fnamecopy, l_name)) {
1062 debug("RootMismatch: |%s|%s|\n", s_name,
1068 if (isdir && !(dentptr->attr & ATTR_DIR))
1071 debug("RootName: %s", s_name);
1072 debug(", start: 0x%x", START(dentptr));
1073 debug(", size: 0x%x %s\n",
1074 FAT2CPU32(dentptr->size),
1075 isdir ? "(DIR)" : "");
1077 goto rootdir_done; /* We got a match */
1079 debug("END LOOP: j=%d clust_size=%d\n", j,
1080 mydata->clust_size);
1083 * On FAT32 we must fetch the FAT entries for the next
1084 * root directory clusters when a cluster has been
1085 * completely processed.
1088 int rootdir_end = 0;
1089 if (mydata->fatsize == 32) {
1090 if (j == mydata->clust_size) {
1094 nxt_clust = get_fatent(mydata, root_cluster);
1095 rootdir_end = CHECK_CLUST(nxt_clust, 32);
1097 nxtsect = mydata->data_begin +
1098 (nxt_clust * mydata->clust_size);
1100 root_cluster = nxt_clust;
1106 if (j == PREFETCH_BLOCKS)
1109 rootdir_end = (++cursect - mydata->rootdir_sect >=
1113 /* If end of rootdir reached */
1115 if (dols == LS_ROOT) {
1116 printf("\n%d file(s), %d dir(s)\n\n",
1128 int startsect = mydata->data_begin
1129 + START(dentptr) * mydata->clust_size;
1131 char *nextname = NULL;
1136 idx = dirdelim(subname);
1139 subname[idx] = '\0';
1140 nextname = subname + idx + 1;
1141 /* Handle multiple delimiters */
1142 while (ISDIRDELIM(*nextname))
1144 if (dols && *nextname == '\0')
1147 if (dols && firsttime) {
1154 if (get_dentfromdir(mydata, startsect, subname, dentptr,
1155 isdir ? 0 : dols) == NULL) {
1161 if (isdir && !(dentptr->attr & ATTR_DIR))
1168 ret = get_contents(mydata, dentptr, pos, buffer, maxsize);
1169 debug("Size: %d, got: %ld\n", FAT2CPU32(dentptr->size), ret);
1172 free(mydata->fatbuf);
1177 do_fat_read(const char *filename, void *buffer, unsigned long maxsize, int dols)
1179 return do_fat_read_at(filename, 0, buffer, maxsize, dols);
1182 int file_fat_detectfs(void)
1185 volume_info volinfo;
1189 if (cur_dev == NULL) {
1190 printf("No current device\n");
1194 #if defined(CONFIG_CMD_IDE) || \
1195 defined(CONFIG_CMD_SATA) || \
1196 defined(CONFIG_CMD_SCSI) || \
1197 defined(CONFIG_CMD_USB) || \
1199 printf("Interface: ");
1200 switch (cur_dev->if_type) {
1226 printf("\n Device %d: ", cur_dev->dev);
1230 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1231 printf("\nNo valid FAT fs found\n");
1235 memcpy(vol_label, volinfo.volume_label, 11);
1236 vol_label[11] = '\0';
1237 volinfo.fs_type[5] = '\0';
1239 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1244 int file_fat_ls(const char *dir)
1246 return do_fat_read(dir, NULL, 0, LS_YES);
1249 long file_fat_read_at(const char *filename, unsigned long pos, void *buffer,
1250 unsigned long maxsize)
1252 printf("reading %s\n", filename);
1253 return do_fat_read_at(filename, pos, buffer, maxsize, LS_NO);
1256 long file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
1258 return file_fat_read_at(filename, 0, buffer, maxsize);