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. Converts at most 'len' characters,
33 * 'len' may be larger than the length of 'str' if 'str' is NULL
36 static void downcase(char *str, size_t len)
38 while (*str != '\0' && len--) {
44 static struct blk_desc *cur_dev;
45 static disk_partition_t cur_part_info;
47 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
48 #define DOS_FS_TYPE_OFFSET 0x36
49 #define DOS_FS32_TYPE_OFFSET 0x52
51 static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
58 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
66 int fat_set_blk_dev(struct blk_desc *dev_desc, disk_partition_t *info)
68 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
71 cur_part_info = *info;
73 /* Make sure it has a valid FAT header */
74 if (disk_read(0, 1, buffer) != 1) {
79 /* Check if it's actually a DOS volume */
80 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
85 /* Check for FAT12/FAT16/FAT32 filesystem */
86 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
88 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
95 int fat_register_device(struct blk_desc *dev_desc, int part_no)
97 disk_partition_t info;
99 /* First close any currently found FAT filesystem */
102 /* Read the partition table, if present */
103 if (part_get_info(dev_desc, part_no, &info)) {
105 printf("** Partition %d not valid on device %d **\n",
106 part_no, dev_desc->devnum);
111 info.size = dev_desc->lba;
112 info.blksz = dev_desc->blksz;
116 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
121 return fat_set_blk_dev(dev_desc, &info);
125 * Extract zero terminated short name from a directory entry.
127 static void get_name(dir_entry *dirent, char *s_name)
131 memcpy(s_name, dirent->name, 8);
134 while (*ptr && *ptr != ' ')
136 if (dirent->lcase & CASE_LOWER_BASE)
137 downcase(s_name, (unsigned)(ptr - s_name));
138 if (dirent->ext[0] && dirent->ext[0] != ' ') {
140 memcpy(ptr, dirent->ext, 3);
141 if (dirent->lcase & CASE_LOWER_EXT)
144 while (*ptr && *ptr != ' ')
148 if (*s_name == DELETED_FLAG)
150 else if (*s_name == aRING)
151 *s_name = DELETED_FLAG;
154 static int flush_dirty_fat_buffer(fsdata *mydata);
155 #if !defined(CONFIG_FAT_WRITE)
156 /* Stub for read only operation */
157 int flush_dirty_fat_buffer(fsdata *mydata)
165 * Get the entry at index 'entry' in a FAT (12/16/32) table.
166 * On failure 0x00 is returned.
168 static __u32 get_fatent(fsdata *mydata, __u32 entry)
174 if (CHECK_CLUST(entry, mydata->fatsize)) {
175 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
179 switch (mydata->fatsize) {
181 bufnum = entry / FAT32BUFSIZE;
182 offset = entry - bufnum * FAT32BUFSIZE;
185 bufnum = entry / FAT16BUFSIZE;
186 offset = entry - bufnum * FAT16BUFSIZE;
189 bufnum = entry / FAT12BUFSIZE;
190 offset = entry - bufnum * FAT12BUFSIZE;
194 /* Unsupported FAT size */
198 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
199 mydata->fatsize, entry, entry, offset, offset);
201 /* Read a new block of FAT entries into the cache. */
202 if (bufnum != mydata->fatbufnum) {
203 __u32 getsize = FATBUFBLOCKS;
204 __u8 *bufptr = mydata->fatbuf;
205 __u32 fatlength = mydata->fatlength;
206 __u32 startblock = bufnum * FATBUFBLOCKS;
208 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
209 if (startblock + getsize > fatlength)
210 getsize = fatlength - startblock;
212 startblock += mydata->fat_sect; /* Offset from start of disk */
214 /* Write back the fatbuf to the disk */
215 if (flush_dirty_fat_buffer(mydata) < 0)
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 off8 = (offset * 3) / 2;
235 /* fatbut + off8 may be unaligned, read in byte granularity */
236 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
242 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
243 mydata->fatsize, ret, entry, offset);
249 * Read at most 'size' bytes from the specified cluster into 'buffer'.
250 * Return 0 on success, -1 otherwise.
253 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
260 startsect = mydata->data_begin +
261 clustnum * mydata->clust_size;
263 startsect = mydata->rootdir_sect;
266 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
268 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
269 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
271 printf("FAT: Misaligned buffer address (%p)\n", buffer);
273 while (size >= mydata->sect_size) {
274 ret = disk_read(startsect++, 1, tmpbuf);
276 debug("Error reading data (got %d)\n", ret);
280 memcpy(buffer, tmpbuf, mydata->sect_size);
281 buffer += mydata->sect_size;
282 size -= mydata->sect_size;
285 idx = size / mydata->sect_size;
286 ret = disk_read(startsect, idx, buffer);
288 debug("Error reading data (got %d)\n", ret);
292 idx *= mydata->sect_size;
297 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
299 ret = disk_read(startsect, 1, tmpbuf);
301 debug("Error reading data (got %d)\n", ret);
305 memcpy(buffer, tmpbuf, size);
312 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
314 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
316 __u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
317 __aligned(ARCH_DMA_MINALIGN);
319 static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
320 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
322 loff_t filesize = FAT2CPU32(dentptr->size);
323 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
324 __u32 curclust = START(dentptr);
325 __u32 endclust, newclust;
329 debug("Filesize: %llu bytes\n", filesize);
331 if (pos >= filesize) {
332 debug("Read position past EOF: %llu\n", pos);
336 if (maxsize > 0 && filesize > pos + maxsize)
337 filesize = pos + maxsize;
339 debug("%llu bytes\n", filesize);
341 actsize = bytesperclust;
343 /* go to cluster at pos */
344 while (actsize <= pos) {
345 curclust = get_fatent(mydata, curclust);
346 if (CHECK_CLUST(curclust, mydata->fatsize)) {
347 debug("curclust: 0x%x\n", curclust);
348 debug("Invalid FAT entry\n");
351 actsize += bytesperclust;
355 actsize -= bytesperclust;
359 /* align to beginning of next cluster if any */
361 actsize = min(filesize, (loff_t)bytesperclust);
362 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
363 (int)actsize) != 0) {
364 printf("Error reading cluster\n");
369 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
375 curclust = get_fatent(mydata, curclust);
376 if (CHECK_CLUST(curclust, mydata->fatsize)) {
377 debug("curclust: 0x%x\n", curclust);
378 debug("Invalid FAT entry\n");
383 actsize = bytesperclust;
387 /* search for consecutive clusters */
388 while (actsize < filesize) {
389 newclust = get_fatent(mydata, endclust);
390 if ((newclust - 1) != endclust)
392 if (CHECK_CLUST(newclust, mydata->fatsize)) {
393 debug("curclust: 0x%x\n", newclust);
394 debug("Invalid FAT entry\n");
398 actsize += bytesperclust;
401 /* get remaining bytes */
403 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
404 printf("Error reading cluster\n");
410 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
411 printf("Error reading cluster\n");
414 *gotsize += (int)actsize;
418 curclust = get_fatent(mydata, endclust);
419 if (CHECK_CLUST(curclust, mydata->fatsize)) {
420 debug("curclust: 0x%x\n", curclust);
421 printf("Invalid FAT entry\n");
424 actsize = bytesperclust;
430 * Extract the file name information from 'slotptr' into 'l_name',
431 * starting at l_name[*idx].
432 * Return 1 if terminator (zero byte) is found, 0 otherwise.
434 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
438 for (j = 0; j <= 8; j += 2) {
439 l_name[*idx] = slotptr->name0_4[j];
440 if (l_name[*idx] == 0x00)
444 for (j = 0; j <= 10; j += 2) {
445 l_name[*idx] = slotptr->name5_10[j];
446 if (l_name[*idx] == 0x00)
450 for (j = 0; j <= 2; j += 2) {
451 l_name[*idx] = slotptr->name11_12[j];
452 if (l_name[*idx] == 0x00)
460 /* Calculate short name checksum */
461 static __u8 mkcksum(const char name[8], const char ext[3])
467 for (i = 0; i < 8; i++)
468 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
469 for (i = 0; i < 3; i++)
470 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
476 * TODO these should go away once fat_write is reworked to use the
479 __u8 get_dentfromdir_block[MAX_CLUSTSIZE]
480 __aligned(ARCH_DMA_MINALIGN);
481 __u8 do_fat_read_at_block[MAX_CLUSTSIZE]
482 __aligned(ARCH_DMA_MINALIGN);
485 * Read boot sector and volume info from a FAT filesystem
488 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
491 volume_info *vistart;
494 if (cur_dev == NULL) {
495 debug("Error: no device selected\n");
499 block = memalign(ARCH_DMA_MINALIGN, cur_dev->blksz);
501 debug("Error: allocating block\n");
505 if (disk_read(0, 1, block) < 0) {
506 debug("Error: reading block\n");
510 memcpy(bs, block, sizeof(boot_sector));
511 bs->reserved = FAT2CPU16(bs->reserved);
512 bs->fat_length = FAT2CPU16(bs->fat_length);
513 bs->secs_track = FAT2CPU16(bs->secs_track);
514 bs->heads = FAT2CPU16(bs->heads);
515 bs->total_sect = FAT2CPU32(bs->total_sect);
518 if (bs->fat_length == 0) {
520 bs->fat32_length = FAT2CPU32(bs->fat32_length);
521 bs->flags = FAT2CPU16(bs->flags);
522 bs->root_cluster = FAT2CPU32(bs->root_cluster);
523 bs->info_sector = FAT2CPU16(bs->info_sector);
524 bs->backup_boot = FAT2CPU16(bs->backup_boot);
525 vistart = (volume_info *)(block + sizeof(boot_sector));
528 vistart = (volume_info *)&(bs->fat32_length);
531 memcpy(volinfo, vistart, sizeof(volume_info));
533 if (*fatsize == 32) {
534 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
537 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
541 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
547 debug("Error: broken fs_type sign\n");
555 static int get_fs_info(fsdata *mydata)
561 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
563 debug("Error: reading boot sector\n");
567 if (mydata->fatsize == 32) {
568 mydata->fatlength = bs.fat32_length;
570 mydata->fatlength = bs.fat_length;
573 mydata->fat_sect = bs.reserved;
575 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
577 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
578 mydata->clust_size = bs.cluster_size;
579 if (mydata->sect_size != cur_part_info.blksz) {
580 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
581 mydata->sect_size, cur_part_info.blksz);
585 if (mydata->fatsize == 32) {
586 mydata->data_begin = mydata->rootdir_sect -
587 (mydata->clust_size * 2);
588 mydata->root_cluster = bs.root_cluster;
590 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
594 mydata->data_begin = mydata->rootdir_sect +
595 mydata->rootdir_size -
596 (mydata->clust_size * 2);
597 mydata->root_cluster = (mydata->rootdir_sect -
598 mydata->data_begin) /
602 mydata->fatbufnum = -1;
603 mydata->fat_dirty = 0;
604 mydata->fatbuf = memalign(ARCH_DMA_MINALIGN, FATBUFSIZE);
605 if (mydata->fatbuf == NULL) {
606 debug("Error: allocating memory\n");
611 debug("VFAT Support enabled\n");
613 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
614 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
615 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
616 "Data begins at: %d\n",
617 mydata->root_cluster,
618 mydata->rootdir_sect,
619 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
620 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
628 * Directory iterator, to simplify filesystem traversal
630 * Implements an iterator pattern to traverse directory tables,
631 * transparently handling directory tables split across multiple
632 * clusters, and the difference between FAT12/FAT16 root directory
633 * (contiguous) and subdirectories + FAT32 root (chained).
637 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
638 * // to traverse down to a subdirectory pointed to by
639 * // current iterator position:
640 * fat_itr_child(&itr, &itr);
643 * For more complete example, see fat_itr_resolve()
647 fsdata *fsdata; /* filesystem parameters */
648 unsigned clust; /* current cluster */
649 int last_cluster; /* set once we've read last cluster */
650 int is_root; /* is iterator at root directory */
651 int remaining; /* remaining dent's in current cluster */
653 /* current iterator position values: */
654 dir_entry *dent; /* current directory entry */
655 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
656 char s_name[14]; /* short 8.3 name */
657 char *name; /* l_name if there is one, else s_name */
659 /* storage for current cluster in memory: */
660 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
663 static int fat_itr_isdir(fat_itr *itr);
666 * fat_itr_root() - initialize an iterator to start at the root
669 * @itr: iterator to initialize
670 * @fsdata: filesystem data for the partition
671 * @return 0 on success, else -errno
673 static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
675 if (get_fs_info(fsdata))
678 itr->fsdata = fsdata;
679 itr->clust = fsdata->root_cluster;
682 itr->last_cluster = 0;
689 * fat_itr_child() - initialize an iterator to descend into a sub-
692 * Initializes 'itr' to iterate the contents of the directory at
693 * the current cursor position of 'parent'. It is an error to
694 * call this if the current cursor of 'parent' is pointing at a
697 * Note that 'itr' and 'parent' can be the same pointer if you do
698 * not need to preserve 'parent' after this call, which is useful
699 * for traversing directory structure to resolve a file/directory.
701 * @itr: iterator to initialize
702 * @parent: the iterator pointing at a directory entry in the
703 * parent directory of the directory to iterate
705 static void fat_itr_child(fat_itr *itr, fat_itr *parent)
707 fsdata *mydata = parent->fsdata; /* for silly macros */
708 unsigned clustnum = START(parent->dent);
710 assert(fat_itr_isdir(parent));
712 itr->fsdata = parent->fsdata;
714 itr->clust = clustnum;
716 itr->clust = parent->fsdata->root_cluster;
720 itr->last_cluster = 0;
724 static void *next_cluster(fat_itr *itr)
726 fsdata *mydata = itr->fsdata; /* for silly macros */
730 /* have we reached the end? */
731 if (itr->last_cluster)
734 sect = clust_to_sect(itr->fsdata, itr->clust);
736 debug("FAT read(sect=%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
737 sect, itr->fsdata->clust_size, DIRENTSPERBLOCK);
740 * NOTE: do_fat_read_at() had complicated logic to deal w/
741 * vfat names that span multiple clusters in the fat16 case,
742 * which get_dentfromdir() probably also needed (and was
743 * missing). And not entirely sure what fat32 didn't have
744 * the same issue.. We solve that by only caring about one
745 * dent at a time and iteratively constructing the vfat long
748 ret = disk_read(sect, itr->fsdata->clust_size,
751 debug("Error: reading block\n");
755 if (itr->is_root && itr->fsdata->fatsize != 32) {
757 sect = clust_to_sect(itr->fsdata, itr->clust);
758 if (sect - itr->fsdata->rootdir_sect >=
759 itr->fsdata->rootdir_size) {
760 debug("cursect: 0x%x\n", itr->clust);
761 itr->last_cluster = 1;
764 itr->clust = get_fatent(itr->fsdata, itr->clust);
765 if (CHECK_CLUST(itr->clust, itr->fsdata->fatsize)) {
766 debug("cursect: 0x%x\n", itr->clust);
767 itr->last_cluster = 1;
774 static dir_entry *next_dent(fat_itr *itr)
776 if (itr->remaining == 0) {
777 struct dir_entry *dent = next_cluster(itr);
778 unsigned nbytes = itr->fsdata->sect_size *
779 itr->fsdata->clust_size;
781 /* have we reached the last cluster? */
785 itr->remaining = nbytes / sizeof(dir_entry) - 1;
792 /* have we reached the last valid entry? */
793 if (itr->dent->name[0] == 0)
799 static dir_entry *extract_vfat_name(fat_itr *itr)
801 struct dir_entry *dent = itr->dent;
802 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
803 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
810 slot2str((dir_slot *)dent, buf, &idx);
812 /* shift accumulated long-name up and copy new part in: */
813 memmove(itr->l_name + idx, itr->l_name, n);
814 memcpy(itr->l_name, buf, idx);
817 dent = next_dent(itr);
822 itr->l_name[n] = '\0';
824 chksum = mkcksum(dent->name, dent->ext);
826 /* checksum mismatch could mean deleted file, etc.. skip it: */
827 if (chksum != alias_checksum) {
828 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
829 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
837 * fat_itr_next() - step to the next entry in a directory
839 * Must be called once on a new iterator before the cursor is valid.
841 * @itr: the iterator to iterate
842 * @return boolean, 1 if success or 0 if no more entries in the
845 static int fat_itr_next(fat_itr *itr)
852 dent = next_dent(itr);
856 if (dent->name[0] == DELETED_FLAG ||
857 dent->name[0] == aRING)
860 if (dent->attr & ATTR_VOLUME) {
862 (dent->attr & ATTR_VFAT) == ATTR_VFAT &&
863 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
864 dent = extract_vfat_name(itr);
867 itr->name = itr->l_name;
870 /* Volume label or VFAT entry, skip */
878 get_name(dent, itr->s_name);
880 itr->name = itr->s_name;
886 * fat_itr_isdir() - is current cursor position pointing to a directory
889 * @return true if cursor is at a directory
891 static int fat_itr_isdir(fat_itr *itr)
893 return !!(itr->dent->attr & ATTR_DIR);
900 #define TYPE_FILE 0x1
902 #define TYPE_ANY (TYPE_FILE | TYPE_DIR)
905 * fat_itr_resolve() - traverse directory structure to resolve the
908 * Traverse directory structure to the requested path. If the specified
909 * path is to a directory, this will descend into the directory and
910 * leave it iterator at the start of the directory. If the path is to a
911 * file, it will leave the iterator in the parent directory with current
912 * cursor at file's entry in the directory.
914 * @itr: iterator initialized to root
915 * @path: the requested path
916 * @type: bitmask of allowable file types
917 * @return 0 on success or -errno
919 static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
923 /* chomp any extra leading slashes: */
924 while (path[0] && ISDIRDELIM(path[0]))
927 /* are we at the end? */
928 if (strlen(path) == 0) {
929 if (!(type & TYPE_DIR))
934 /* find length of next path entry: */
936 while (next[0] && !ISDIRDELIM(next[0]))
939 while (fat_itr_next(itr)) {
941 unsigned n = max(strlen(itr->name), (size_t)(next - path));
943 /* check both long and short name: */
944 if (!strncasecmp(path, itr->name, n))
946 else if (itr->name != itr->s_name &&
947 !strncasecmp(path, itr->s_name, n))
953 if (fat_itr_isdir(itr)) {
954 /* recurse into directory: */
955 fat_itr_child(itr, itr);
956 return fat_itr_resolve(itr, next, type);
957 } else if (next[0]) {
959 * If next is not empty then we have a case
960 * like: /path/to/realfile/nonsense
962 debug("bad trailing path: %s\n", next);
964 } else if (!(type & TYPE_FILE)) {
974 int file_fat_detectfs(void)
981 if (cur_dev == NULL) {
982 printf("No current device\n");
986 #if defined(CONFIG_IDE) || \
987 defined(CONFIG_SATA) || \
988 defined(CONFIG_SCSI) || \
989 defined(CONFIG_CMD_USB) || \
991 printf("Interface: ");
992 switch (cur_dev->if_type) {
1018 printf("\n Device %d: ", cur_dev->devnum);
1022 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1023 printf("\nNo valid FAT fs found\n");
1027 memcpy(vol_label, volinfo.volume_label, 11);
1028 vol_label[11] = '\0';
1029 volinfo.fs_type[5] = '\0';
1031 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1036 int fat_exists(const char *filename)
1039 fat_itr itrblock, *itr = &itrblock;
1042 ret = fat_itr_root(itr, &fsdata);
1046 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
1050 int fat_size(const char *filename, loff_t *size)
1053 fat_itr itrblock, *itr = &itrblock;
1056 ret = fat_itr_root(itr, &fsdata);
1060 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1063 * Directories don't have size, but fs_size() is not
1064 * expected to fail if passed a directory path:
1066 fat_itr_root(itr, &fsdata);
1067 if (!fat_itr_resolve(itr, filename, TYPE_DIR)) {
1074 *size = FAT2CPU32(itr->dent->size);
1079 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1080 loff_t maxsize, loff_t *actread)
1083 fat_itr itrblock, *itr = &itrblock;
1086 ret = fat_itr_root(itr, &fsdata);
1090 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1094 printf("reading %s\n", filename);
1095 return get_contents(&fsdata, itr->dent, pos, buffer, maxsize, actread);
1098 int file_fat_read(const char *filename, void *buffer, int maxsize)
1103 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1110 int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1115 ret = file_fat_read_at(filename, offset, buf, len, actread);
1117 printf("** Unable to read file %s **\n", filename);
1123 struct fs_dir_stream parent;
1124 struct fs_dirent dirent;
1129 int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1131 fat_dir *dir = malloc(sizeof(*dir));
1137 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1141 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1145 *dirsp = (struct fs_dir_stream *)dir;
1153 int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1155 fat_dir *dir = (fat_dir *)dirs;
1156 struct fs_dirent *dent = &dir->dirent;
1158 if (!fat_itr_next(&dir->itr))
1161 memset(dent, 0, sizeof(*dent));
1162 strcpy(dent->name, dir->itr.name);
1164 if (fat_itr_isdir(&dir->itr)) {
1165 dent->type = FS_DT_DIR;
1167 dent->type = FS_DT_REG;
1168 dent->size = FAT2CPU32(dir->itr.dent->size);
1176 void fat_closedir(struct fs_dir_stream *dirs)
1178 fat_dir *dir = (fat_dir *)dirs;
1182 void fat_close(void)