1 // SPDX-License-Identifier: GPL-2.0+
5 * R/O (V)FAT 12/16/32 filesystem implementation by Marcus Sundberg
7 * 2002-07-28 - rjones@nexus-tech.net - ported to ppcboot v1.1.6
8 * 2003-03-10 - kharris@nexus-tech.net - ported to uboot
17 #include <asm/byteorder.h>
21 #include <linux/compiler.h>
22 #include <linux/ctype.h>
25 * Convert a string to lowercase. Converts at most 'len' characters,
26 * 'len' may be larger than the length of 'str' if 'str' is NULL
29 static void downcase(char *str, size_t len)
31 while (*str != '\0' && len--) {
37 static struct blk_desc *cur_dev;
38 static disk_partition_t cur_part_info;
40 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
41 #define DOS_FS_TYPE_OFFSET 0x36
42 #define DOS_FS32_TYPE_OFFSET 0x52
44 static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
51 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
59 int fat_set_blk_dev(struct blk_desc *dev_desc, disk_partition_t *info)
61 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
64 cur_part_info = *info;
66 /* Make sure it has a valid FAT header */
67 if (disk_read(0, 1, buffer) != 1) {
72 /* Check if it's actually a DOS volume */
73 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
78 /* Check for FAT12/FAT16/FAT32 filesystem */
79 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
81 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
88 int fat_register_device(struct blk_desc *dev_desc, int part_no)
90 disk_partition_t info;
92 /* First close any currently found FAT filesystem */
95 /* Read the partition table, if present */
96 if (part_get_info(dev_desc, part_no, &info)) {
98 printf("** Partition %d not valid on device %d **\n",
99 part_no, dev_desc->devnum);
104 info.size = dev_desc->lba;
105 info.blksz = dev_desc->blksz;
109 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
114 return fat_set_blk_dev(dev_desc, &info);
118 * Extract zero terminated short name from a directory entry.
120 static void get_name(dir_entry *dirent, char *s_name)
124 memcpy(s_name, dirent->name, 8);
127 while (*ptr && *ptr != ' ')
129 if (dirent->lcase & CASE_LOWER_BASE)
130 downcase(s_name, (unsigned)(ptr - s_name));
131 if (dirent->ext[0] && dirent->ext[0] != ' ') {
133 memcpy(ptr, dirent->ext, 3);
134 if (dirent->lcase & CASE_LOWER_EXT)
137 while (*ptr && *ptr != ' ')
141 if (*s_name == DELETED_FLAG)
143 else if (*s_name == aRING)
144 *s_name = DELETED_FLAG;
147 static int flush_dirty_fat_buffer(fsdata *mydata);
148 #if !defined(CONFIG_FAT_WRITE)
149 /* Stub for read only operation */
150 int flush_dirty_fat_buffer(fsdata *mydata)
158 * Get the entry at index 'entry' in a FAT (12/16/32) table.
159 * On failure 0x00 is returned.
161 static __u32 get_fatent(fsdata *mydata, __u32 entry)
167 if (CHECK_CLUST(entry, mydata->fatsize)) {
168 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
172 switch (mydata->fatsize) {
174 bufnum = entry / FAT32BUFSIZE;
175 offset = entry - bufnum * FAT32BUFSIZE;
178 bufnum = entry / FAT16BUFSIZE;
179 offset = entry - bufnum * FAT16BUFSIZE;
182 bufnum = entry / FAT12BUFSIZE;
183 offset = entry - bufnum * FAT12BUFSIZE;
187 /* Unsupported FAT size */
191 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
192 mydata->fatsize, entry, entry, offset, offset);
194 /* Read a new block of FAT entries into the cache. */
195 if (bufnum != mydata->fatbufnum) {
196 __u32 getsize = FATBUFBLOCKS;
197 __u8 *bufptr = mydata->fatbuf;
198 __u32 fatlength = mydata->fatlength;
199 __u32 startblock = bufnum * FATBUFBLOCKS;
201 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
202 if (startblock + getsize > fatlength)
203 getsize = fatlength - startblock;
205 startblock += mydata->fat_sect; /* Offset from start of disk */
207 /* Write back the fatbuf to the disk */
208 if (flush_dirty_fat_buffer(mydata) < 0)
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 off8 = (offset * 3) / 2;
228 /* fatbut + off8 may be unaligned, read in byte granularity */
229 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
235 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
236 mydata->fatsize, ret, entry, offset);
242 * Read at most 'size' bytes from the specified cluster into 'buffer'.
243 * Return 0 on success, -1 otherwise.
246 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
253 startsect = clust_to_sect(mydata, clustnum);
255 startsect = mydata->rootdir_sect;
258 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
260 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
261 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
263 debug("FAT: Misaligned buffer address (%p)\n", buffer);
265 while (size >= mydata->sect_size) {
266 ret = disk_read(startsect++, 1, tmpbuf);
268 debug("Error reading data (got %d)\n", ret);
272 memcpy(buffer, tmpbuf, mydata->sect_size);
273 buffer += mydata->sect_size;
274 size -= mydata->sect_size;
277 idx = size / mydata->sect_size;
278 ret = disk_read(startsect, idx, buffer);
280 debug("Error reading data (got %d)\n", ret);
284 idx *= mydata->sect_size;
289 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
291 ret = disk_read(startsect, 1, tmpbuf);
293 debug("Error reading data (got %d)\n", ret);
297 memcpy(buffer, tmpbuf, size);
304 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
306 * Update the number of bytes read in *gotsize or return -1 on fatal errors.
308 __u8 get_contents_vfatname_block[MAX_CLUSTSIZE]
309 __aligned(ARCH_DMA_MINALIGN);
311 static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
312 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
314 loff_t filesize = FAT2CPU32(dentptr->size);
315 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
316 __u32 curclust = START(dentptr);
317 __u32 endclust, newclust;
321 debug("Filesize: %llu bytes\n", filesize);
323 if (pos >= filesize) {
324 debug("Read position past EOF: %llu\n", pos);
328 if (maxsize > 0 && filesize > pos + maxsize)
329 filesize = pos + maxsize;
331 debug("%llu bytes\n", filesize);
333 actsize = bytesperclust;
335 /* go to cluster at pos */
336 while (actsize <= pos) {
337 curclust = get_fatent(mydata, curclust);
338 if (CHECK_CLUST(curclust, mydata->fatsize)) {
339 debug("curclust: 0x%x\n", curclust);
340 debug("Invalid FAT entry\n");
343 actsize += bytesperclust;
347 actsize -= bytesperclust;
351 /* align to beginning of next cluster if any */
353 actsize = min(filesize, (loff_t)bytesperclust);
354 if (get_cluster(mydata, curclust, get_contents_vfatname_block,
355 (int)actsize) != 0) {
356 printf("Error reading cluster\n");
361 memcpy(buffer, get_contents_vfatname_block + pos, actsize);
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");
375 actsize = bytesperclust;
379 /* search for consecutive clusters */
380 while (actsize < filesize) {
381 newclust = get_fatent(mydata, endclust);
382 if ((newclust - 1) != endclust)
384 if (CHECK_CLUST(newclust, mydata->fatsize)) {
385 debug("curclust: 0x%x\n", newclust);
386 debug("Invalid FAT entry\n");
390 actsize += bytesperclust;
393 /* get remaining bytes */
395 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
396 printf("Error reading cluster\n");
402 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
403 printf("Error reading cluster\n");
406 *gotsize += (int)actsize;
410 curclust = get_fatent(mydata, endclust);
411 if (CHECK_CLUST(curclust, mydata->fatsize)) {
412 debug("curclust: 0x%x\n", curclust);
413 printf("Invalid FAT entry\n");
416 actsize = bytesperclust;
422 * Extract the file name information from 'slotptr' into 'l_name',
423 * starting at l_name[*idx].
424 * Return 1 if terminator (zero byte) is found, 0 otherwise.
426 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
430 for (j = 0; j <= 8; j += 2) {
431 l_name[*idx] = slotptr->name0_4[j];
432 if (l_name[*idx] == 0x00)
436 for (j = 0; j <= 10; j += 2) {
437 l_name[*idx] = slotptr->name5_10[j];
438 if (l_name[*idx] == 0x00)
442 for (j = 0; j <= 2; j += 2) {
443 l_name[*idx] = slotptr->name11_12[j];
444 if (l_name[*idx] == 0x00)
452 /* Calculate short name checksum */
453 static __u8 mkcksum(const char name[8], const char ext[3])
459 for (i = 0; i < 8; i++)
460 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
461 for (i = 0; i < 3; i++)
462 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
468 * Read boot sector and volume info from a FAT filesystem
471 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
474 volume_info *vistart;
477 if (cur_dev == NULL) {
478 debug("Error: no device selected\n");
482 block = malloc_cache_aligned(cur_dev->blksz);
484 debug("Error: allocating block\n");
488 if (disk_read(0, 1, block) < 0) {
489 debug("Error: reading block\n");
493 memcpy(bs, block, sizeof(boot_sector));
494 bs->reserved = FAT2CPU16(bs->reserved);
495 bs->fat_length = FAT2CPU16(bs->fat_length);
496 bs->secs_track = FAT2CPU16(bs->secs_track);
497 bs->heads = FAT2CPU16(bs->heads);
498 bs->total_sect = FAT2CPU32(bs->total_sect);
501 if (bs->fat_length == 0) {
503 bs->fat32_length = FAT2CPU32(bs->fat32_length);
504 bs->flags = FAT2CPU16(bs->flags);
505 bs->root_cluster = FAT2CPU32(bs->root_cluster);
506 bs->info_sector = FAT2CPU16(bs->info_sector);
507 bs->backup_boot = FAT2CPU16(bs->backup_boot);
508 vistart = (volume_info *)(block + sizeof(boot_sector));
511 vistart = (volume_info *)&(bs->fat32_length);
514 memcpy(volinfo, vistart, sizeof(volume_info));
516 if (*fatsize == 32) {
517 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
520 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
524 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
530 debug("Error: broken fs_type sign\n");
538 static int get_fs_info(fsdata *mydata)
544 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
546 debug("Error: reading boot sector\n");
550 if (mydata->fatsize == 32) {
551 mydata->fatlength = bs.fat32_length;
552 mydata->total_sect = bs.total_sect;
554 mydata->fatlength = bs.fat_length;
555 mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
556 if (!mydata->total_sect)
557 mydata->total_sect = bs.total_sect;
559 if (!mydata->total_sect) /* unlikely */
560 mydata->total_sect = (u32)cur_part_info.size;
562 mydata->fats = bs.fats;
563 mydata->fat_sect = bs.reserved;
565 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
567 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
568 mydata->clust_size = bs.cluster_size;
569 if (mydata->sect_size != cur_part_info.blksz) {
570 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
571 mydata->sect_size, cur_part_info.blksz);
574 if (mydata->clust_size == 0) {
575 printf("Error: FAT cluster size not set\n");
578 if ((unsigned int)mydata->clust_size * mydata->sect_size >
580 printf("Error: FAT cluster size too big (cs=%u, max=%u)\n",
581 (unsigned int)mydata->clust_size * mydata->sect_size,
586 if (mydata->fatsize == 32) {
587 mydata->data_begin = mydata->rootdir_sect -
588 (mydata->clust_size * 2);
589 mydata->root_cluster = bs.root_cluster;
591 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
595 mydata->data_begin = mydata->rootdir_sect +
596 mydata->rootdir_size -
597 (mydata->clust_size * 2);
598 mydata->root_cluster =
599 sect_to_clust(mydata, mydata->rootdir_sect);
602 mydata->fatbufnum = -1;
603 mydata->fat_dirty = 0;
604 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
605 if (mydata->fatbuf == NULL) {
606 debug("Error: allocating memory\n");
610 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
611 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
612 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
613 "Data begins at: %d\n",
614 mydata->root_cluster,
615 mydata->rootdir_sect,
616 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
617 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
625 * Directory iterator, to simplify filesystem traversal
627 * Implements an iterator pattern to traverse directory tables,
628 * transparently handling directory tables split across multiple
629 * clusters, and the difference between FAT12/FAT16 root directory
630 * (contiguous) and subdirectories + FAT32 root (chained).
634 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
635 * // to traverse down to a subdirectory pointed to by
636 * // current iterator position:
637 * fat_itr_child(&itr, &itr);
640 * For more complete example, see fat_itr_resolve()
644 fsdata *fsdata; /* filesystem parameters */
645 unsigned start_clust; /* first cluster */
646 unsigned clust; /* current cluster */
647 unsigned next_clust; /* next cluster if remaining == 0 */
648 int last_cluster; /* set once we've read last cluster */
649 int is_root; /* is iterator at root directory */
650 int remaining; /* remaining dent's in current cluster */
652 /* current iterator position values: */
653 dir_entry *dent; /* current directory entry */
654 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
655 char s_name[14]; /* short 8.3 name */
656 char *name; /* l_name if there is one, else s_name */
658 /* storage for current cluster in memory: */
659 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
662 static int fat_itr_isdir(fat_itr *itr);
665 * fat_itr_root() - initialize an iterator to start at the root
668 * @itr: iterator to initialize
669 * @fsdata: filesystem data for the partition
670 * @return 0 on success, else -errno
672 static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
674 if (get_fs_info(fsdata))
677 itr->fsdata = fsdata;
678 itr->start_clust = 0;
679 itr->clust = fsdata->root_cluster;
680 itr->next_clust = fsdata->root_cluster;
683 itr->last_cluster = 0;
690 * fat_itr_child() - initialize an iterator to descend into a sub-
693 * Initializes 'itr' to iterate the contents of the directory at
694 * the current cursor position of 'parent'. It is an error to
695 * call this if the current cursor of 'parent' is pointing at a
698 * Note that 'itr' and 'parent' can be the same pointer if you do
699 * not need to preserve 'parent' after this call, which is useful
700 * for traversing directory structure to resolve a file/directory.
702 * @itr: iterator to initialize
703 * @parent: the iterator pointing at a directory entry in the
704 * parent directory of the directory to iterate
706 static void fat_itr_child(fat_itr *itr, fat_itr *parent)
708 fsdata *mydata = parent->fsdata; /* for silly macros */
709 unsigned clustnum = START(parent->dent);
711 assert(fat_itr_isdir(parent));
713 itr->fsdata = parent->fsdata;
714 itr->start_clust = clustnum;
716 itr->clust = clustnum;
717 itr->next_clust = clustnum;
720 itr->clust = parent->fsdata->root_cluster;
721 itr->next_clust = parent->fsdata->root_cluster;
726 itr->last_cluster = 0;
729 static void *next_cluster(fat_itr *itr)
731 fsdata *mydata = itr->fsdata; /* for silly macros */
735 /* have we reached the end? */
736 if (itr->last_cluster)
739 sect = clust_to_sect(itr->fsdata, itr->next_clust);
741 debug("FAT read(sect=%d), clust_size=%d, DIRENTSPERBLOCK=%zd\n",
742 sect, itr->fsdata->clust_size, DIRENTSPERBLOCK);
745 * NOTE: do_fat_read_at() had complicated logic to deal w/
746 * vfat names that span multiple clusters in the fat16 case,
747 * which get_dentfromdir() probably also needed (and was
748 * missing). And not entirely sure what fat32 didn't have
749 * the same issue.. We solve that by only caring about one
750 * dent at a time and iteratively constructing the vfat long
753 ret = disk_read(sect, itr->fsdata->clust_size,
756 debug("Error: reading block\n");
760 itr->clust = itr->next_clust;
761 if (itr->is_root && itr->fsdata->fatsize != 32) {
763 sect = clust_to_sect(itr->fsdata, itr->next_clust);
764 if (sect - itr->fsdata->rootdir_sect >=
765 itr->fsdata->rootdir_size) {
766 debug("nextclust: 0x%x\n", itr->next_clust);
767 itr->last_cluster = 1;
770 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
771 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
772 debug("nextclust: 0x%x\n", itr->next_clust);
773 itr->last_cluster = 1;
780 static dir_entry *next_dent(fat_itr *itr)
782 if (itr->remaining == 0) {
783 struct dir_entry *dent = next_cluster(itr);
784 unsigned nbytes = itr->fsdata->sect_size *
785 itr->fsdata->clust_size;
787 /* have we reached the last cluster? */
789 /* a sign for no more entries left */
794 itr->remaining = nbytes / sizeof(dir_entry) - 1;
801 /* have we reached the last valid entry? */
802 if (itr->dent->name[0] == 0)
808 static dir_entry *extract_vfat_name(fat_itr *itr)
810 struct dir_entry *dent = itr->dent;
811 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
812 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
819 slot2str((dir_slot *)dent, buf, &idx);
821 if (n + idx >= sizeof(itr->l_name))
824 /* shift accumulated long-name up and copy new part in: */
825 memmove(itr->l_name + idx, itr->l_name, n);
826 memcpy(itr->l_name, buf, idx);
829 dent = next_dent(itr);
834 itr->l_name[n] = '\0';
836 chksum = mkcksum(dent->name, dent->ext);
838 /* checksum mismatch could mean deleted file, etc.. skip it: */
839 if (chksum != alias_checksum) {
840 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
841 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
849 * fat_itr_next() - step to the next entry in a directory
851 * Must be called once on a new iterator before the cursor is valid.
853 * @itr: the iterator to iterate
854 * @return boolean, 1 if success or 0 if no more entries in the
857 static int fat_itr_next(fat_itr *itr)
864 dent = next_dent(itr);
868 if (dent->name[0] == DELETED_FLAG ||
869 dent->name[0] == aRING)
872 if (dent->attr & ATTR_VOLUME) {
873 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
874 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
875 dent = extract_vfat_name(itr);
878 itr->name = itr->l_name;
881 /* Volume label or VFAT entry, skip */
889 get_name(dent, itr->s_name);
891 itr->name = itr->s_name;
897 * fat_itr_isdir() - is current cursor position pointing to a directory
900 * @return true if cursor is at a directory
902 static int fat_itr_isdir(fat_itr *itr)
904 return !!(itr->dent->attr & ATTR_DIR);
911 #define TYPE_FILE 0x1
913 #define TYPE_ANY (TYPE_FILE | TYPE_DIR)
916 * fat_itr_resolve() - traverse directory structure to resolve the
919 * Traverse directory structure to the requested path. If the specified
920 * path is to a directory, this will descend into the directory and
921 * leave it iterator at the start of the directory. If the path is to a
922 * file, it will leave the iterator in the parent directory with current
923 * cursor at file's entry in the directory.
925 * @itr: iterator initialized to root
926 * @path: the requested path
927 * @type: bitmask of allowable file types
928 * @return 0 on success or -errno
930 static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
934 /* chomp any extra leading slashes: */
935 while (path[0] && ISDIRDELIM(path[0]))
938 /* are we at the end? */
939 if (strlen(path) == 0) {
940 if (!(type & TYPE_DIR))
945 /* find length of next path entry: */
947 while (next[0] && !ISDIRDELIM(next[0]))
951 /* root dir doesn't have "." nor ".." */
952 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
953 (((next - path) == 2) && !strncmp(path, "..", 2))) {
954 /* point back to itself */
955 itr->clust = itr->fsdata->root_cluster;
956 itr->next_clust = itr->fsdata->root_cluster;
959 itr->last_cluster = 0;
968 return fat_itr_resolve(itr, next, type);
972 while (fat_itr_next(itr)) {
974 unsigned n = max(strlen(itr->name), (size_t)(next - path));
976 /* check both long and short name: */
977 if (!strncasecmp(path, itr->name, n))
979 else if (itr->name != itr->s_name &&
980 !strncasecmp(path, itr->s_name, n))
986 if (fat_itr_isdir(itr)) {
987 /* recurse into directory: */
988 fat_itr_child(itr, itr);
989 return fat_itr_resolve(itr, next, type);
990 } else if (next[0]) {
992 * If next is not empty then we have a case
993 * like: /path/to/realfile/nonsense
995 debug("bad trailing path: %s\n", next);
997 } else if (!(type & TYPE_FILE)) {
1007 int file_fat_detectfs(void)
1010 volume_info volinfo;
1014 if (cur_dev == NULL) {
1015 printf("No current device\n");
1019 #if defined(CONFIG_IDE) || \
1020 defined(CONFIG_SATA) || \
1021 defined(CONFIG_SCSI) || \
1022 defined(CONFIG_CMD_USB) || \
1024 printf("Interface: ");
1025 switch (cur_dev->if_type) {
1051 printf("\n Device %d: ", cur_dev->devnum);
1055 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1056 printf("\nNo valid FAT fs found\n");
1060 memcpy(vol_label, volinfo.volume_label, 11);
1061 vol_label[11] = '\0';
1062 volinfo.fs_type[5] = '\0';
1064 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1069 int fat_exists(const char *filename)
1075 itr = malloc_cache_aligned(sizeof(fat_itr));
1078 ret = fat_itr_root(itr, &fsdata);
1082 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
1083 free(fsdata.fatbuf);
1089 int fat_size(const char *filename, loff_t *size)
1095 itr = malloc_cache_aligned(sizeof(fat_itr));
1098 ret = fat_itr_root(itr, &fsdata);
1102 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1105 * Directories don't have size, but fs_size() is not
1106 * expected to fail if passed a directory path:
1108 free(fsdata.fatbuf);
1109 fat_itr_root(itr, &fsdata);
1110 if (!fat_itr_resolve(itr, filename, TYPE_DIR)) {
1117 *size = FAT2CPU32(itr->dent->size);
1119 free(fsdata.fatbuf);
1125 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1126 loff_t maxsize, loff_t *actread)
1132 itr = malloc_cache_aligned(sizeof(fat_itr));
1135 ret = fat_itr_root(itr, &fsdata);
1139 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1143 debug("reading %s at pos %llu\n", filename, pos);
1144 ret = get_contents(&fsdata, itr->dent, pos, buffer, maxsize, actread);
1147 free(fsdata.fatbuf);
1153 int file_fat_read(const char *filename, void *buffer, int maxsize)
1158 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1165 int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1170 ret = file_fat_read_at(filename, offset, buf, len, actread);
1172 printf("** Unable to read file %s **\n", filename);
1178 struct fs_dir_stream parent;
1179 struct fs_dirent dirent;
1184 int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1189 dir = malloc_cache_aligned(sizeof(*dir));
1192 memset(dir, 0, sizeof(*dir));
1194 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1198 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1200 goto fail_free_both;
1202 *dirsp = (struct fs_dir_stream *)dir;
1206 free(dir->fsdata.fatbuf);
1212 int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1214 fat_dir *dir = (fat_dir *)dirs;
1215 struct fs_dirent *dent = &dir->dirent;
1217 if (!fat_itr_next(&dir->itr))
1220 memset(dent, 0, sizeof(*dent));
1221 strcpy(dent->name, dir->itr.name);
1223 if (fat_itr_isdir(&dir->itr)) {
1224 dent->type = FS_DT_DIR;
1226 dent->type = FS_DT_REG;
1227 dent->size = FAT2CPU32(dir->itr.dent->size);
1235 void fat_closedir(struct fs_dir_stream *dirs)
1237 fat_dir *dir = (fat_dir *)dirs;
1238 free(dir->fsdata.fatbuf);
1242 void fat_close(void)