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+
18 #include <asm/byteorder.h>
22 #include <linux/compiler.h>
23 #include <linux/ctype.h>
25 #ifdef CONFIG_SUPPORT_VFAT
26 static const int vfat_enabled = 1;
28 static const int vfat_enabled = 0;
32 * Convert a string to lowercase.
34 static void downcase(char *str)
36 while (*str != '\0') {
42 static struct blk_desc *cur_dev;
43 static disk_partition_t cur_part_info;
45 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
46 #define DOS_FS_TYPE_OFFSET 0x36
47 #define DOS_FS32_TYPE_OFFSET 0x52
49 static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
56 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
64 int fat_set_blk_dev(struct blk_desc *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(struct blk_desc *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 (part_get_info(dev_desc, part_no, &info)) {
103 printf("** Partition %d not valid on device %d **\n",
104 part_no, dev_desc->devnum);
109 info.size = dev_desc->lba;
110 info.blksz = dev_desc->blksz;
114 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
119 return fat_set_blk_dev(dev_desc, &info);
123 * Extract zero terminated short name from a directory entry.
125 static void get_name(dir_entry *dirent, char *s_name)
129 memcpy(s_name, dirent->name, 8);
132 while (*ptr && *ptr != ' ')
134 if (dirent->ext[0] && dirent->ext[0] != ' ') {
137 memcpy(ptr, dirent->ext, 3);
139 while (*ptr && *ptr != ' ')
143 if (*s_name == DELETED_FLAG)
145 else if (*s_name == aRING)
146 *s_name = DELETED_FLAG;
150 static int flush_dirty_fat_buffer(fsdata *mydata);
151 #if !defined(CONFIG_FAT_WRITE)
152 /* Stub for read only operation */
153 int flush_dirty_fat_buffer(fsdata *mydata)
161 * Get the entry at index 'entry' in a FAT (12/16/32) table.
162 * On failure 0x00 is returned.
164 static __u32 get_fatent(fsdata *mydata, __u32 entry)
170 if (CHECK_CLUST(entry, mydata->fatsize)) {
171 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
175 switch (mydata->fatsize) {
177 bufnum = entry / FAT32BUFSIZE;
178 offset = entry - bufnum * FAT32BUFSIZE;
181 bufnum = entry / FAT16BUFSIZE;
182 offset = entry - bufnum * FAT16BUFSIZE;
185 bufnum = entry / FAT12BUFSIZE;
186 offset = entry - bufnum * FAT12BUFSIZE;
190 /* Unsupported FAT size */
194 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
195 mydata->fatsize, entry, entry, offset, offset);
197 /* Read a new block of FAT entries into the cache. */
198 if (bufnum != mydata->fatbufnum) {
199 __u32 getsize = FATBUFBLOCKS;
200 __u8 *bufptr = mydata->fatbuf;
201 __u32 fatlength = mydata->fatlength;
202 __u32 startblock = bufnum * FATBUFBLOCKS;
204 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
205 if (startblock + getsize > fatlength)
206 getsize = fatlength - startblock;
208 startblock += mydata->fat_sect; /* Offset from start of disk */
210 /* Write back the fatbuf to the disk */
211 if (flush_dirty_fat_buffer(mydata) < 0)
214 if (disk_read(startblock, getsize, bufptr) < 0) {
215 debug("Error reading FAT blocks\n");
218 mydata->fatbufnum = bufnum;
221 /* Get the actual entry from the table */
222 switch (mydata->fatsize) {
224 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
227 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
230 off8 = (offset * 3) / 2;
231 /* fatbut + off8 may be unaligned, read in byte granularity */
232 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
238 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
239 mydata->fatsize, ret, entry, offset);
245 * Read at most 'size' bytes from the specified cluster into 'buffer'.
246 * Return 0 on success, -1 otherwise.
249 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
256 startsect = mydata->data_begin +
257 clustnum * mydata->clust_size;
259 startsect = mydata->rootdir_sect;
262 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
264 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
265 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
267 printf("FAT: Misaligned buffer address (%p)\n", buffer);
269 while (size >= mydata->sect_size) {
270 ret = disk_read(startsect++, 1, tmpbuf);
272 debug("Error reading data (got %d)\n", ret);
276 memcpy(buffer, tmpbuf, mydata->sect_size);
277 buffer += mydata->sect_size;
278 size -= mydata->sect_size;
281 idx = size / mydata->sect_size;
282 ret = disk_read(startsect, idx, buffer);
284 debug("Error reading data (got %d)\n", ret);
288 idx *= mydata->sect_size;
293 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
295 ret = disk_read(startsect, 1, tmpbuf);
297 debug("Error reading data (got %d)\n", ret);
301 memcpy(buffer, tmpbuf, size);
308 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
310 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
312 __u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
313 __aligned(ARCH_DMA_MINALIGN);
315 static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
316 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
318 loff_t filesize = FAT2CPU32(dentptr->size);
319 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
320 __u32 curclust = START(dentptr);
321 __u32 endclust, newclust;
325 debug("Filesize: %llu bytes\n", filesize);
327 if (pos >= filesize) {
328 debug("Read position past EOF: %llu\n", pos);
332 if (maxsize > 0 && filesize > pos + maxsize)
333 filesize = pos + maxsize;
335 debug("%llu bytes\n", filesize);
337 actsize = bytesperclust;
339 /* go to cluster at pos */
340 while (actsize <= pos) {
341 curclust = get_fatent(mydata, curclust);
342 if (CHECK_CLUST(curclust, mydata->fatsize)) {
343 debug("curclust: 0x%x\n", curclust);
344 debug("Invalid FAT entry\n");
347 actsize += bytesperclust;
351 actsize -= bytesperclust;
355 /* align to beginning of next cluster if any */
357 actsize = min(filesize, (loff_t)bytesperclust);
358 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
359 (int)actsize) != 0) {
360 printf("Error reading cluster\n");
365 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
371 curclust = get_fatent(mydata, curclust);
372 if (CHECK_CLUST(curclust, mydata->fatsize)) {
373 debug("curclust: 0x%x\n", curclust);
374 debug("Invalid FAT entry\n");
379 actsize = bytesperclust;
383 /* search for consecutive clusters */
384 while (actsize < filesize) {
385 newclust = get_fatent(mydata, endclust);
386 if ((newclust - 1) != endclust)
388 if (CHECK_CLUST(newclust, mydata->fatsize)) {
389 debug("curclust: 0x%x\n", newclust);
390 debug("Invalid FAT entry\n");
394 actsize += bytesperclust;
397 /* get remaining bytes */
399 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
400 printf("Error reading cluster\n");
406 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
407 printf("Error reading cluster\n");
410 *gotsize += (int)actsize;
414 curclust = get_fatent(mydata, endclust);
415 if (CHECK_CLUST(curclust, mydata->fatsize)) {
416 debug("curclust: 0x%x\n", curclust);
417 printf("Invalid FAT entry\n");
420 actsize = bytesperclust;
426 * Extract the file name information from 'slotptr' into 'l_name',
427 * starting at l_name[*idx].
428 * Return 1 if terminator (zero byte) is found, 0 otherwise.
430 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
434 for (j = 0; j <= 8; j += 2) {
435 l_name[*idx] = slotptr->name0_4[j];
436 if (l_name[*idx] == 0x00)
440 for (j = 0; j <= 10; j += 2) {
441 l_name[*idx] = slotptr->name5_10[j];
442 if (l_name[*idx] == 0x00)
446 for (j = 0; j <= 2; j += 2) {
447 l_name[*idx] = slotptr->name11_12[j];
448 if (l_name[*idx] == 0x00)
456 /* Calculate short name checksum */
457 static __u8 mkcksum(const char name[8], const char ext[3])
463 for (i = 0; i < 8; i++)
464 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
465 for (i = 0; i < 3; i++)
466 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
472 * TODO these should go away once fat_write is reworked to use the
475 __u8 get_dentfromdir_block[MAX_CLUSTSIZE]
476 __aligned(ARCH_DMA_MINALIGN);
477 __u8 do_fat_read_at_block[MAX_CLUSTSIZE]
478 __aligned(ARCH_DMA_MINALIGN);
481 * Read boot sector and volume info from a FAT filesystem
484 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
487 volume_info *vistart;
490 if (cur_dev == NULL) {
491 debug("Error: no device selected\n");
495 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
497 debug("Error: allocating block\n");
501 if (disk_read(0, 1, block) < 0) {
502 debug("Error: reading block\n");
506 memcpy(bs, block, sizeof(boot_sector));
507 bs->reserved = FAT2CPU16(bs->reserved);
508 bs->fat_length = FAT2CPU16(bs->fat_length);
509 bs->secs_track = FAT2CPU16(bs->secs_track);
510 bs->heads = FAT2CPU16(bs->heads);
511 bs->total_sect = FAT2CPU32(bs->total_sect);
514 if (bs->fat_length == 0) {
516 bs->fat32_length = FAT2CPU32(bs->fat32_length);
517 bs->flags = FAT2CPU16(bs->flags);
518 bs->root_cluster = FAT2CPU32(bs->root_cluster);
519 bs->info_sector = FAT2CPU16(bs->info_sector);
520 bs->backup_boot = FAT2CPU16(bs->backup_boot);
521 vistart = (volume_info *)(block + sizeof(boot_sector));
524 vistart = (volume_info *)&(bs->fat32_length);
527 memcpy(volinfo, vistart, sizeof(volume_info));
529 if (*fatsize == 32) {
530 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
533 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
537 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
543 debug("Error: broken fs_type sign\n");
551 static int get_fs_info(fsdata *mydata)
557 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
559 debug("Error: reading boot sector\n");
563 if (mydata->fatsize == 32) {
564 mydata->fatlength = bs.fat32_length;
566 mydata->fatlength = bs.fat_length;
569 mydata->fat_sect = bs.reserved;
571 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
573 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
574 mydata->clust_size = bs.cluster_size;
575 if (mydata->sect_size != cur_part_info.blksz) {
576 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
577 mydata->sect_size, cur_part_info.blksz);
581 if (mydata->fatsize == 32) {
582 mydata->data_begin = mydata->rootdir_sect -
583 (mydata->clust_size * 2);
584 mydata->root_cluster = bs.root_cluster;
586 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
590 mydata->data_begin = mydata->rootdir_sect +
591 mydata->rootdir_size -
592 (mydata->clust_size * 2);
593 mydata->root_cluster = (mydata->rootdir_sect -
594 mydata->data_begin) /
598 mydata->fatbufnum = -1;
599 mydata->fat_dirty = 0;
600 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
601 if (mydata->fatbuf == NULL) {
602 debug("Error: allocating memory\n");
607 debug("VFAT Support enabled\n");
609 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
610 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
611 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
612 "Data begins at: %d\n",
613 mydata->root_cluster,
614 mydata->rootdir_sect,
615 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
616 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
624 * Directory iterator, to simplify filesystem traversal
626 * Implements an iterator pattern to traverse directory tables,
627 * transparently handling directory tables split across multiple
628 * clusters, and the difference between FAT12/FAT16 root directory
629 * (contiguous) and subdirectories + FAT32 root (chained).
633 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
634 * // to traverse down to a subdirectory pointed to by
635 * // current iterator position:
636 * fat_itr_child(&itr, &itr);
639 * For more complete example, see fat_itr_resolve()
643 fsdata *fsdata; /* filesystem parameters */
644 unsigned clust; /* current cluster */
645 int last_cluster; /* set once we've read last cluster */
646 int is_root; /* is iterator at root directory */
647 int remaining; /* remaining dent's in current cluster */
649 /* current iterator position values: */
650 dir_entry *dent; /* current directory entry */
651 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
652 char s_name[14]; /* short 8.3 name */
653 char *name; /* l_name if there is one, else s_name */
655 /* storage for current cluster in memory: */
656 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
659 static int fat_itr_isdir(fat_itr *itr);
662 * fat_itr_root() - initialize an iterator to start at the root
665 * @itr: iterator to initialize
666 * @fsdata: filesystem data for the partition
667 * @return 0 on success, else -errno
669 static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
671 if (get_fs_info(fsdata))
674 itr->fsdata = fsdata;
675 itr->clust = fsdata->root_cluster;
678 itr->last_cluster = 0;
685 * fat_itr_child() - initialize an iterator to descend into a sub-
688 * Initializes 'itr' to iterate the contents of the directory at
689 * the current cursor position of 'parent'. It is an error to
690 * call this if the current cursor of 'parent' is pointing at a
693 * Note that 'itr' and 'parent' can be the same pointer if you do
694 * not need to preserve 'parent' after this call, which is useful
695 * for traversing directory structure to resolve a file/directory.
697 * @itr: iterator to initialize
698 * @parent: the iterator pointing at a directory entry in the
699 * parent directory of the directory to iterate
701 static void fat_itr_child(fat_itr *itr, fat_itr *parent)
703 fsdata *mydata = parent->fsdata; /* for silly macros */
704 unsigned clustnum = START(parent->dent);
706 assert(fat_itr_isdir(parent));
708 itr->fsdata = parent->fsdata;
710 itr->clust = clustnum;
712 itr->clust = parent->fsdata->root_cluster;
716 itr->last_cluster = 0;
720 static void *next_cluster(fat_itr *itr)
722 fsdata *mydata = itr->fsdata; /* for silly macros */
726 /* have we reached the end? */
727 if (itr->last_cluster)
730 sect = clust_to_sect(itr->fsdata, itr->clust);
732 debug("FAT read(sect=%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
733 sect, itr->fsdata->clust_size, DIRENTSPERBLOCK);
736 * NOTE: do_fat_read_at() had complicated logic to deal w/
737 * vfat names that span multiple clusters in the fat16 case,
738 * which get_dentfromdir() probably also needed (and was
739 * missing). And not entirely sure what fat32 didn't have
740 * the same issue.. We solve that by only caring about one
741 * dent at a time and iteratively constructing the vfat long
744 ret = disk_read(sect, itr->fsdata->clust_size,
747 debug("Error: reading block\n");
751 if (itr->is_root && itr->fsdata->fatsize != 32) {
753 sect = clust_to_sect(itr->fsdata, itr->clust);
754 if (sect - itr->fsdata->rootdir_sect >=
755 itr->fsdata->rootdir_size) {
756 debug("cursect: 0x%x\n", itr->clust);
757 itr->last_cluster = 1;
760 itr->clust = get_fatent(itr->fsdata, itr->clust);
761 if (CHECK_CLUST(itr->clust, itr->fsdata->fatsize)) {
762 debug("cursect: 0x%x\n", itr->clust);
763 itr->last_cluster = 1;
770 static dir_entry *next_dent(fat_itr *itr)
772 if (itr->remaining == 0) {
773 struct dir_entry *dent = next_cluster(itr);
774 unsigned nbytes = itr->fsdata->sect_size *
775 itr->fsdata->clust_size;
777 /* have we reached the last cluster? */
781 itr->remaining = nbytes / sizeof(dir_entry) - 1;
788 /* have we reached the last valid entry? */
789 if (itr->dent->name[0] == 0)
795 static dir_entry *extract_vfat_name(fat_itr *itr)
797 struct dir_entry *dent = itr->dent;
798 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
799 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
806 slot2str((dir_slot *)dent, buf, &idx);
808 /* shift accumulated long-name up and copy new part in: */
809 memmove(itr->l_name + idx, itr->l_name, n);
810 memcpy(itr->l_name, buf, idx);
813 dent = next_dent(itr);
818 itr->l_name[n] = '\0';
820 chksum = mkcksum(dent->name, dent->ext);
822 /* checksum mismatch could mean deleted file, etc.. skip it: */
823 if (chksum != alias_checksum) {
824 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
825 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
833 * fat_itr_next() - step to the next entry in a directory
835 * Must be called once on a new iterator before the cursor is valid.
837 * @itr: the iterator to iterate
838 * @return boolean, 1 if success or 0 if no more entries in the
841 static int fat_itr_next(fat_itr *itr)
848 dent = next_dent(itr);
852 if (dent->name[0] == DELETED_FLAG ||
853 dent->name[0] == aRING)
856 if (dent->attr & ATTR_VOLUME) {
858 (dent->attr & ATTR_VFAT) == ATTR_VFAT &&
859 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
860 dent = extract_vfat_name(itr);
863 itr->name = itr->l_name;
866 /* Volume label or VFAT entry, skip */
874 get_name(dent, itr->s_name);
876 itr->name = itr->s_name;
882 * fat_itr_isdir() - is current cursor position pointing to a directory
885 * @return true if cursor is at a directory
887 static int fat_itr_isdir(fat_itr *itr)
889 return !!(itr->dent->attr & ATTR_DIR);
896 #define TYPE_FILE 0x1
898 #define TYPE_ANY (TYPE_FILE | TYPE_DIR)
901 * fat_itr_resolve() - traverse directory structure to resolve the
904 * Traverse directory structure to the requested path. If the specified
905 * path is to a directory, this will descend into the directory and
906 * leave it iterator at the start of the directory. If the path is to a
907 * file, it will leave the iterator in the parent directory with current
908 * cursor at file's entry in the directory.
910 * @itr: iterator initialized to root
911 * @path: the requested path
912 * @type: bitmask of allowable file types
913 * @return 0 on success or -errno
915 static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
919 /* chomp any extra leading slashes: */
920 while (path[0] && ISDIRDELIM(path[0]))
923 /* are we at the end? */
924 if (strlen(path) == 0) {
925 if (!(type & TYPE_DIR))
930 /* find length of next path entry: */
932 while (next[0] && !ISDIRDELIM(next[0]))
935 while (fat_itr_next(itr)) {
937 unsigned n = max(strlen(itr->name), (size_t)(next - path));
939 /* check both long and short name: */
940 if (!strncasecmp(path, itr->name, n))
942 else if (itr->name != itr->s_name &&
943 !strncasecmp(path, itr->s_name, n))
949 if (fat_itr_isdir(itr)) {
950 /* recurse into directory: */
951 fat_itr_child(itr, itr);
952 return fat_itr_resolve(itr, next, type);
953 } else if (next[0]) {
955 * If next is not empty then we have a case
956 * like: /path/to/realfile/nonsense
958 debug("bad trailing path: %s\n", next);
960 } else if (!(type & TYPE_FILE)) {
970 int file_fat_detectfs(void)
977 if (cur_dev == NULL) {
978 printf("No current device\n");
982 #if defined(CONFIG_IDE) || \
983 defined(CONFIG_SATA) || \
984 defined(CONFIG_SCSI) || \
985 defined(CONFIG_CMD_USB) || \
987 printf("Interface: ");
988 switch (cur_dev->if_type) {
1014 printf("\n Device %d: ", cur_dev->devnum);
1018 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1019 printf("\nNo valid FAT fs found\n");
1023 memcpy(vol_label, volinfo.volume_label, 11);
1024 vol_label[11] = '\0';
1025 volinfo.fs_type[5] = '\0';
1027 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1032 int file_fat_ls(const char *dir)
1035 fat_itr itrblock, *itr = &itrblock;
1036 int files = 0, dirs = 0;
1039 ret = fat_itr_root(itr, &fsdata);
1043 ret = fat_itr_resolve(itr, dir, TYPE_DIR);
1047 while (fat_itr_next(itr)) {
1048 if (fat_itr_isdir(itr)) {
1049 printf(" %s/\n", itr->name);
1053 FAT2CPU32(itr->dent->size),
1059 printf("\n%d file(s), %d dir(s)\n\n", files, dirs);
1064 int fat_exists(const char *filename)
1067 fat_itr itrblock, *itr = &itrblock;
1070 ret = fat_itr_root(itr, &fsdata);
1074 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
1078 int fat_size(const char *filename, loff_t *size)
1081 fat_itr itrblock, *itr = &itrblock;
1084 ret = fat_itr_root(itr, &fsdata);
1088 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1091 * Directories don't have size, but fs_size() is not
1092 * expected to fail if passed a directory path:
1094 fat_itr_root(itr, &fsdata);
1095 if (!fat_itr_resolve(itr, filename, TYPE_DIR)) {
1102 *size = FAT2CPU32(itr->dent->size);
1107 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1108 loff_t maxsize, loff_t *actread)
1111 fat_itr itrblock, *itr = &itrblock;
1114 ret = fat_itr_root(itr, &fsdata);
1118 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1122 printf("reading %s\n", filename);
1123 return get_contents(&fsdata, itr->dent, pos, buffer, maxsize, actread);
1126 int file_fat_read(const char *filename, void *buffer, int maxsize)
1131 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1138 int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1143 ret = file_fat_read_at(filename, offset, buf, len, actread);
1145 printf("** Unable to read file %s **\n", filename);
1151 struct fs_dir_stream parent;
1152 struct fs_dirent dirent;
1157 int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1159 fat_dir *dir = malloc(sizeof(*dir));
1165 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1169 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1173 *dirsp = (struct fs_dir_stream *)dir;
1181 int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1183 fat_dir *dir = (fat_dir *)dirs;
1184 struct fs_dirent *dent = &dir->dirent;
1186 if (!fat_itr_next(&dir->itr))
1189 memset(dent, 0, sizeof(*dent));
1190 strcpy(dent->name, dir->itr.name);
1192 if (fat_itr_isdir(&dir->itr)) {
1193 dent->type = FS_DT_DIR;
1195 dent->type = FS_DT_REG;
1196 dent->size = FAT2CPU32(dir->itr.dent->size);
1204 void fat_closedir(struct fs_dir_stream *dirs)
1206 fat_dir *dir = (fat_dir *)dirs;
1210 void fat_close(void)