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 <asm/cache.h>
22 #include <linux/compiler.h>
23 #include <linux/ctype.h>
26 * Convert a string to lowercase. Converts at most 'len' characters,
27 * 'len' may be larger than the length of 'str' if 'str' is NULL
30 static void downcase(char *str, size_t len)
32 while (*str != '\0' && len--) {
38 static struct blk_desc *cur_dev;
39 static struct disk_partition cur_part_info;
41 #define DOS_BOOT_MAGIC_OFFSET 0x1fe
42 #define DOS_FS_TYPE_OFFSET 0x36
43 #define DOS_FS32_TYPE_OFFSET 0x52
45 static int disk_read(__u32 block, __u32 nr_blocks, void *buf)
52 ret = blk_dread(cur_dev, cur_part_info.start + block, nr_blocks, buf);
60 int fat_set_blk_dev(struct blk_desc *dev_desc, struct disk_partition *info)
62 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
65 cur_part_info = *info;
67 /* Make sure it has a valid FAT header */
68 if (disk_read(0, 1, buffer) != 1) {
73 /* Check if it's actually a DOS volume */
74 if (memcmp(buffer + DOS_BOOT_MAGIC_OFFSET, "\x55\xAA", 2)) {
79 /* Check for FAT12/FAT16/FAT32 filesystem */
80 if (!memcmp(buffer + DOS_FS_TYPE_OFFSET, "FAT", 3))
82 if (!memcmp(buffer + DOS_FS32_TYPE_OFFSET, "FAT32", 5))
89 int fat_register_device(struct blk_desc *dev_desc, int part_no)
91 struct disk_partition info;
93 /* First close any currently found FAT filesystem */
96 /* Read the partition table, if present */
97 if (part_get_info(dev_desc, part_no, &info)) {
99 printf("** Partition %d not valid on device %d **\n",
100 part_no, dev_desc->devnum);
105 info.size = dev_desc->lba;
106 info.blksz = dev_desc->blksz;
110 #if CONFIG_IS_ENABLED(PARTITION_UUIDS)
115 return fat_set_blk_dev(dev_desc, &info);
119 * Extract zero terminated short name from a directory entry.
121 static void get_name(dir_entry *dirent, char *s_name)
125 memcpy(s_name, dirent->name, 8);
128 while (*ptr && *ptr != ' ')
130 if (dirent->lcase & CASE_LOWER_BASE)
131 downcase(s_name, (unsigned)(ptr - s_name));
132 if (dirent->ext[0] && dirent->ext[0] != ' ') {
134 memcpy(ptr, dirent->ext, 3);
135 if (dirent->lcase & CASE_LOWER_EXT)
138 while (*ptr && *ptr != ' ')
142 if (*s_name == DELETED_FLAG)
144 else if (*s_name == aRING)
145 *s_name = DELETED_FLAG;
148 static int flush_dirty_fat_buffer(fsdata *mydata);
150 #if !CONFIG_IS_ENABLED(FAT_WRITE)
151 /* Stub for read only operation */
152 int flush_dirty_fat_buffer(fsdata *mydata)
160 * Get the entry at index 'entry' in a FAT (12/16/32) table.
161 * On failure 0x00 is returned.
163 static __u32 get_fatent(fsdata *mydata, __u32 entry)
169 if (CHECK_CLUST(entry, mydata->fatsize)) {
170 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
174 switch (mydata->fatsize) {
176 bufnum = entry / FAT32BUFSIZE;
177 offset = entry - bufnum * FAT32BUFSIZE;
180 bufnum = entry / FAT16BUFSIZE;
181 offset = entry - bufnum * FAT16BUFSIZE;
184 bufnum = entry / FAT12BUFSIZE;
185 offset = entry - bufnum * FAT12BUFSIZE;
189 /* Unsupported FAT size */
193 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
194 mydata->fatsize, entry, entry, offset, offset);
196 /* Read a new block of FAT entries into the cache. */
197 if (bufnum != mydata->fatbufnum) {
198 __u32 getsize = FATBUFBLOCKS;
199 __u8 *bufptr = mydata->fatbuf;
200 __u32 fatlength = mydata->fatlength;
201 __u32 startblock = bufnum * FATBUFBLOCKS;
203 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
204 if (startblock + getsize > fatlength)
205 getsize = fatlength - startblock;
207 startblock += mydata->fat_sect; /* Offset from start of disk */
209 /* Write back the fatbuf to the disk */
210 if (flush_dirty_fat_buffer(mydata) < 0)
213 if (disk_read(startblock, getsize, bufptr) < 0) {
214 debug("Error reading FAT blocks\n");
217 mydata->fatbufnum = bufnum;
220 /* Get the actual entry from the table */
221 switch (mydata->fatsize) {
223 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
226 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
229 off8 = (offset * 3) / 2;
230 /* fatbut + off8 may be unaligned, read in byte granularity */
231 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
237 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
238 mydata->fatsize, ret, entry, offset);
244 * Read at most 'size' bytes from the specified cluster into 'buffer'.
245 * Return 0 on success, -1 otherwise.
248 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
255 startsect = clust_to_sect(mydata, clustnum);
257 startsect = mydata->rootdir_sect;
260 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
262 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
263 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
265 debug("FAT: Misaligned buffer address (%p)\n", buffer);
267 while (size >= mydata->sect_size) {
268 ret = disk_read(startsect++, 1, tmpbuf);
270 debug("Error reading data (got %d)\n", ret);
274 memcpy(buffer, tmpbuf, mydata->sect_size);
275 buffer += mydata->sect_size;
276 size -= mydata->sect_size;
279 idx = size / mydata->sect_size;
280 ret = disk_read(startsect, idx, buffer);
282 debug("Error reading data (got %d)\n", ret);
286 idx *= mydata->sect_size;
291 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
293 ret = disk_read(startsect, 1, tmpbuf);
295 debug("Error reading data (got %d)\n", ret);
299 memcpy(buffer, tmpbuf, size);
306 * get_contents() - read from file
308 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
309 * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on
312 * @mydata: file system description
313 * @dentprt: directory entry pointer
314 * @pos: position from where to read
315 * @buffer: buffer into which to read
316 * @maxsize: maximum number of bytes to read
317 * @gotsize: number of bytes actually read
318 * Return: -1 on error, otherwise 0
320 static int get_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos,
321 __u8 *buffer, loff_t maxsize, loff_t *gotsize)
323 loff_t filesize = FAT2CPU32(dentptr->size);
324 unsigned int bytesperclust = mydata->clust_size * mydata->sect_size;
325 __u32 curclust = START(dentptr);
326 __u32 endclust, newclust;
330 debug("Filesize: %llu bytes\n", filesize);
332 if (pos >= filesize) {
333 debug("Read position past EOF: %llu\n", pos);
337 if (maxsize > 0 && filesize > pos + maxsize)
338 filesize = pos + maxsize;
340 debug("%llu bytes\n", filesize);
342 actsize = bytesperclust;
344 /* go to cluster at pos */
345 while (actsize <= pos) {
346 curclust = get_fatent(mydata, curclust);
347 if (CHECK_CLUST(curclust, mydata->fatsize)) {
348 debug("curclust: 0x%x\n", curclust);
349 printf("Invalid FAT entry\n");
352 actsize += bytesperclust;
356 actsize -= bytesperclust;
360 /* align to beginning of next cluster if any */
364 actsize = min(filesize, (loff_t)bytesperclust);
365 tmp_buffer = malloc_cache_aligned(actsize);
367 debug("Error: allocating buffer\n");
371 if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) {
372 printf("Error reading cluster\n");
378 memcpy(buffer, tmp_buffer + pos, actsize);
385 curclust = get_fatent(mydata, curclust);
386 if (CHECK_CLUST(curclust, mydata->fatsize)) {
387 debug("curclust: 0x%x\n", curclust);
388 printf("Invalid FAT entry\n");
393 actsize = bytesperclust;
397 /* search for consecutive clusters */
398 while (actsize < filesize) {
399 newclust = get_fatent(mydata, endclust);
400 if ((newclust - 1) != endclust)
402 if (CHECK_CLUST(newclust, mydata->fatsize)) {
403 debug("curclust: 0x%x\n", newclust);
404 printf("Invalid FAT entry\n");
408 actsize += bytesperclust;
411 /* get remaining bytes */
413 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
414 printf("Error reading cluster\n");
420 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
421 printf("Error reading cluster\n");
424 *gotsize += (int)actsize;
428 curclust = get_fatent(mydata, endclust);
429 if (CHECK_CLUST(curclust, mydata->fatsize)) {
430 debug("curclust: 0x%x\n", curclust);
431 printf("Invalid FAT entry\n");
434 actsize = bytesperclust;
440 * Extract the file name information from 'slotptr' into 'l_name',
441 * starting at l_name[*idx].
442 * Return 1 if terminator (zero byte) is found, 0 otherwise.
444 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
448 for (j = 0; j <= 8; j += 2) {
449 l_name[*idx] = slotptr->name0_4[j];
450 if (l_name[*idx] == 0x00)
454 for (j = 0; j <= 10; j += 2) {
455 l_name[*idx] = slotptr->name5_10[j];
456 if (l_name[*idx] == 0x00)
460 for (j = 0; j <= 2; j += 2) {
461 l_name[*idx] = slotptr->name11_12[j];
462 if (l_name[*idx] == 0x00)
470 /* Calculate short name checksum */
471 static __u8 mkcksum(const char name[8], const char ext[3])
477 for (i = 0; i < 8; i++)
478 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
479 for (i = 0; i < 3; i++)
480 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
486 * Read boot sector and volume info from a FAT filesystem
489 read_bootsectandvi(boot_sector *bs, volume_info *volinfo, int *fatsize)
492 volume_info *vistart;
495 if (cur_dev == NULL) {
496 debug("Error: no device selected\n");
500 block = malloc_cache_aligned(cur_dev->blksz);
502 debug("Error: allocating block\n");
506 if (disk_read(0, 1, block) < 0) {
507 debug("Error: reading block\n");
511 memcpy(bs, block, sizeof(boot_sector));
512 bs->reserved = FAT2CPU16(bs->reserved);
513 bs->fat_length = FAT2CPU16(bs->fat_length);
514 bs->secs_track = FAT2CPU16(bs->secs_track);
515 bs->heads = FAT2CPU16(bs->heads);
516 bs->total_sect = FAT2CPU32(bs->total_sect);
519 if (bs->fat_length == 0) {
521 bs->fat32_length = FAT2CPU32(bs->fat32_length);
522 bs->flags = FAT2CPU16(bs->flags);
523 bs->root_cluster = FAT2CPU32(bs->root_cluster);
524 bs->info_sector = FAT2CPU16(bs->info_sector);
525 bs->backup_boot = FAT2CPU16(bs->backup_boot);
526 vistart = (volume_info *)(block + sizeof(boot_sector));
529 vistart = (volume_info *)&(bs->fat32_length);
532 memcpy(volinfo, vistart, sizeof(volume_info));
534 if (*fatsize == 32) {
535 if (strncmp(FAT32_SIGN, vistart->fs_type, SIGNLEN) == 0)
538 if (strncmp(FAT12_SIGN, vistart->fs_type, SIGNLEN) == 0) {
542 if (strncmp(FAT16_SIGN, vistart->fs_type, SIGNLEN) == 0) {
548 debug("Error: broken fs_type sign\n");
556 static int get_fs_info(fsdata *mydata)
562 ret = read_bootsectandvi(&bs, &volinfo, &mydata->fatsize);
564 debug("Error: reading boot sector\n");
568 if (mydata->fatsize == 32) {
569 mydata->fatlength = bs.fat32_length;
570 mydata->total_sect = bs.total_sect;
572 mydata->fatlength = bs.fat_length;
573 mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
574 if (!mydata->total_sect)
575 mydata->total_sect = bs.total_sect;
577 if (!mydata->total_sect) /* unlikely */
578 mydata->total_sect = (u32)cur_part_info.size;
580 mydata->fats = bs.fats;
581 mydata->fat_sect = bs.reserved;
583 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
585 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
586 mydata->clust_size = bs.cluster_size;
587 if (mydata->sect_size != cur_part_info.blksz) {
588 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
589 mydata->sect_size, cur_part_info.blksz);
592 if (mydata->clust_size == 0) {
593 printf("Error: FAT cluster size not set\n");
596 if ((unsigned int)mydata->clust_size * mydata->sect_size >
598 printf("Error: FAT cluster size too big (cs=%u, max=%u)\n",
599 (unsigned int)mydata->clust_size * mydata->sect_size,
604 if (mydata->fatsize == 32) {
605 mydata->data_begin = mydata->rootdir_sect -
606 (mydata->clust_size * 2);
607 mydata->root_cluster = bs.root_cluster;
609 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
613 mydata->data_begin = mydata->rootdir_sect +
614 mydata->rootdir_size -
615 (mydata->clust_size * 2);
618 * The root directory is not cluster-aligned and may be on a
619 * "negative" cluster, this will be handled specially in
622 mydata->root_cluster = 0;
625 mydata->fatbufnum = -1;
626 mydata->fat_dirty = 0;
627 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
628 if (mydata->fatbuf == NULL) {
629 debug("Error: allocating memory\n");
633 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
634 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
635 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
636 "Data begins at: %d\n",
637 mydata->root_cluster,
638 mydata->rootdir_sect,
639 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
640 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
648 * Directory iterator, to simplify filesystem traversal
650 * Implements an iterator pattern to traverse directory tables,
651 * transparently handling directory tables split across multiple
652 * clusters, and the difference between FAT12/FAT16 root directory
653 * (contiguous) and subdirectories + FAT32 root (chained).
657 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
658 * // to traverse down to a subdirectory pointed to by
659 * // current iterator position:
660 * fat_itr_child(&itr, &itr);
663 * For more complete example, see fat_itr_resolve()
667 fsdata *fsdata; /* filesystem parameters */
668 unsigned start_clust; /* first cluster */
669 unsigned clust; /* current cluster */
670 unsigned next_clust; /* next cluster if remaining == 0 */
671 int last_cluster; /* set once we've read last cluster */
672 int is_root; /* is iterator at root directory */
673 int remaining; /* remaining dent's in current cluster */
675 /* current iterator position values: */
676 dir_entry *dent; /* current directory entry */
677 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
678 char s_name[14]; /* short 8.3 name */
679 char *name; /* l_name if there is one, else s_name */
681 /* storage for current cluster in memory: */
682 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
685 static int fat_itr_isdir(fat_itr *itr);
688 * fat_itr_root() - initialize an iterator to start at the root
691 * @itr: iterator to initialize
692 * @fsdata: filesystem data for the partition
693 * @return 0 on success, else -errno
695 static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
697 if (get_fs_info(fsdata))
700 itr->fsdata = fsdata;
701 itr->start_clust = 0;
702 itr->clust = fsdata->root_cluster;
703 itr->next_clust = fsdata->root_cluster;
706 itr->last_cluster = 0;
713 * fat_itr_child() - initialize an iterator to descend into a sub-
716 * Initializes 'itr' to iterate the contents of the directory at
717 * the current cursor position of 'parent'. It is an error to
718 * call this if the current cursor of 'parent' is pointing at a
721 * Note that 'itr' and 'parent' can be the same pointer if you do
722 * not need to preserve 'parent' after this call, which is useful
723 * for traversing directory structure to resolve a file/directory.
725 * @itr: iterator to initialize
726 * @parent: the iterator pointing at a directory entry in the
727 * parent directory of the directory to iterate
729 static void fat_itr_child(fat_itr *itr, fat_itr *parent)
731 fsdata *mydata = parent->fsdata; /* for silly macros */
732 unsigned clustnum = START(parent->dent);
734 assert(fat_itr_isdir(parent));
736 itr->fsdata = parent->fsdata;
737 itr->start_clust = clustnum;
739 itr->clust = clustnum;
740 itr->next_clust = clustnum;
743 itr->clust = parent->fsdata->root_cluster;
744 itr->next_clust = parent->fsdata->root_cluster;
749 itr->last_cluster = 0;
752 static void *next_cluster(fat_itr *itr, unsigned *nbytes)
754 fsdata *mydata = itr->fsdata; /* for silly macros */
759 /* have we reached the end? */
760 if (itr->last_cluster)
763 if (itr->is_root && itr->fsdata->fatsize != 32) {
765 * The root directory is located before the data area and
766 * cannot be indexed using the regular unsigned cluster
767 * numbers (it may start at a "negative" cluster or not at a
768 * cluster boundary at all), so consider itr->next_clust to be
769 * a offset in cluster-sized units from the start of rootdir.
771 unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size;
772 unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset;
773 sect = itr->fsdata->rootdir_sect + sect_offset;
774 /* do not read past the end of rootdir */
775 read_size = min_t(u32, itr->fsdata->clust_size,
778 sect = clust_to_sect(itr->fsdata, itr->next_clust);
779 read_size = itr->fsdata->clust_size;
782 debug("FAT read(sect=%d), clust_size=%d, read_size=%u, DIRENTSPERBLOCK=%zd\n",
783 sect, itr->fsdata->clust_size, read_size, DIRENTSPERBLOCK);
786 * NOTE: do_fat_read_at() had complicated logic to deal w/
787 * vfat names that span multiple clusters in the fat16 case,
788 * which get_dentfromdir() probably also needed (and was
789 * missing). And not entirely sure what fat32 didn't have
790 * the same issue.. We solve that by only caring about one
791 * dent at a time and iteratively constructing the vfat long
794 ret = disk_read(sect, read_size, itr->block);
796 debug("Error: reading block\n");
800 *nbytes = read_size * itr->fsdata->sect_size;
801 itr->clust = itr->next_clust;
802 if (itr->is_root && itr->fsdata->fatsize != 32) {
804 if (itr->next_clust * itr->fsdata->clust_size >=
805 itr->fsdata->rootdir_size) {
806 debug("nextclust: 0x%x\n", itr->next_clust);
807 itr->last_cluster = 1;
810 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
811 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
812 debug("nextclust: 0x%x\n", itr->next_clust);
813 itr->last_cluster = 1;
820 static dir_entry *next_dent(fat_itr *itr)
822 if (itr->remaining == 0) {
824 struct dir_entry *dent = next_cluster(itr, &nbytes);
826 /* have we reached the last cluster? */
828 /* a sign for no more entries left */
833 itr->remaining = nbytes / sizeof(dir_entry) - 1;
840 /* have we reached the last valid entry? */
841 if (itr->dent->name[0] == 0)
847 static dir_entry *extract_vfat_name(fat_itr *itr)
849 struct dir_entry *dent = itr->dent;
850 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
851 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
858 slot2str((dir_slot *)dent, buf, &idx);
860 if (n + idx >= sizeof(itr->l_name))
863 /* shift accumulated long-name up and copy new part in: */
864 memmove(itr->l_name + idx, itr->l_name, n);
865 memcpy(itr->l_name, buf, idx);
868 dent = next_dent(itr);
874 * We are now at the short file name entry.
875 * If it is marked as deleted, just skip it.
877 if (dent->name[0] == DELETED_FLAG ||
878 dent->name[0] == aRING)
881 itr->l_name[n] = '\0';
883 chksum = mkcksum(dent->name, dent->ext);
885 /* checksum mismatch could mean deleted file, etc.. skip it: */
886 if (chksum != alias_checksum) {
887 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
888 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
896 * fat_itr_next() - step to the next entry in a directory
898 * Must be called once on a new iterator before the cursor is valid.
900 * @itr: the iterator to iterate
901 * @return boolean, 1 if success or 0 if no more entries in the
904 static int fat_itr_next(fat_itr *itr)
911 * One logical directory entry consist of following slots:
913 * dent[N - N]: LFN[N - 1] N|0x40 ATTR_VFAT
915 * dent[N - 2]: LFN[1] 2 ATTR_VFAT
916 * dent[N - 1]: LFN[0] 1 ATTR_VFAT
917 * dent[N]: SFN ATTR_ARCH
921 dent = next_dent(itr);
925 if (dent->name[0] == DELETED_FLAG ||
926 dent->name[0] == aRING)
929 if (dent->attr & ATTR_VOLUME) {
930 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
931 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
933 dent = extract_vfat_name(itr);
935 * If succeeded, dent has a valid short file
936 * name entry for the current entry.
937 * If failed, itr points to a current bogus
938 * entry. So after fetching a next one,
939 * it may have a short file name entry
940 * for this bogus entry so that we can still
941 * check for a short name.
945 itr->name = itr->l_name;
948 /* Volume label or VFAT entry, skip */
951 } else if (!(dent->attr & ATTR_ARCH) &&
952 !(dent->attr & ATTR_DIR))
955 /* short file name */
959 get_name(dent, itr->s_name);
961 itr->name = itr->s_name;
967 * fat_itr_isdir() - is current cursor position pointing to a directory
970 * @return true if cursor is at a directory
972 static int fat_itr_isdir(fat_itr *itr)
974 return !!(itr->dent->attr & ATTR_DIR);
981 #define TYPE_FILE 0x1
983 #define TYPE_ANY (TYPE_FILE | TYPE_DIR)
986 * fat_itr_resolve() - traverse directory structure to resolve the
989 * Traverse directory structure to the requested path. If the specified
990 * path is to a directory, this will descend into the directory and
991 * leave it iterator at the start of the directory. If the path is to a
992 * file, it will leave the iterator in the parent directory with current
993 * cursor at file's entry in the directory.
995 * @itr: iterator initialized to root
996 * @path: the requested path
997 * @type: bitmask of allowable file types
998 * @return 0 on success or -errno
1000 static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
1004 /* chomp any extra leading slashes: */
1005 while (path[0] && ISDIRDELIM(path[0]))
1008 /* are we at the end? */
1009 if (strlen(path) == 0) {
1010 if (!(type & TYPE_DIR))
1015 /* find length of next path entry: */
1017 while (next[0] && !ISDIRDELIM(next[0]))
1021 /* root dir doesn't have "." nor ".." */
1022 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
1023 (((next - path) == 2) && !strncmp(path, "..", 2))) {
1024 /* point back to itself */
1025 itr->clust = itr->fsdata->root_cluster;
1026 itr->next_clust = itr->fsdata->root_cluster;
1029 itr->last_cluster = 0;
1032 if (type & TYPE_DIR)
1038 return fat_itr_resolve(itr, next, type);
1042 while (fat_itr_next(itr)) {
1044 unsigned n = max(strlen(itr->name), (size_t)(next - path));
1046 /* check both long and short name: */
1047 if (!strncasecmp(path, itr->name, n))
1049 else if (itr->name != itr->s_name &&
1050 !strncasecmp(path, itr->s_name, n))
1056 if (fat_itr_isdir(itr)) {
1057 /* recurse into directory: */
1058 fat_itr_child(itr, itr);
1059 return fat_itr_resolve(itr, next, type);
1060 } else if (next[0]) {
1062 * If next is not empty then we have a case
1063 * like: /path/to/realfile/nonsense
1065 debug("bad trailing path: %s\n", next);
1067 } else if (!(type & TYPE_FILE)) {
1077 int file_fat_detectfs(void)
1080 volume_info volinfo;
1084 if (cur_dev == NULL) {
1085 printf("No current device\n");
1089 #if defined(CONFIG_IDE) || \
1090 defined(CONFIG_SATA) || \
1091 defined(CONFIG_SCSI) || \
1092 defined(CONFIG_CMD_USB) || \
1094 printf("Interface: ");
1095 switch (cur_dev->if_type) {
1121 printf("\n Device %d: ", cur_dev->devnum);
1125 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1126 printf("\nNo valid FAT fs found\n");
1130 memcpy(vol_label, volinfo.volume_label, 11);
1131 vol_label[11] = '\0';
1132 volinfo.fs_type[5] = '\0';
1134 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1139 int fat_exists(const char *filename)
1145 itr = malloc_cache_aligned(sizeof(fat_itr));
1148 ret = fat_itr_root(itr, &fsdata);
1152 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
1153 free(fsdata.fatbuf);
1159 int fat_size(const char *filename, loff_t *size)
1165 itr = malloc_cache_aligned(sizeof(fat_itr));
1168 ret = fat_itr_root(itr, &fsdata);
1172 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1175 * Directories don't have size, but fs_size() is not
1176 * expected to fail if passed a directory path:
1178 free(fsdata.fatbuf);
1179 ret = fat_itr_root(itr, &fsdata);
1182 ret = fat_itr_resolve(itr, filename, TYPE_DIR);
1188 *size = FAT2CPU32(itr->dent->size);
1190 free(fsdata.fatbuf);
1196 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1197 loff_t maxsize, loff_t *actread)
1203 itr = malloc_cache_aligned(sizeof(fat_itr));
1206 ret = fat_itr_root(itr, &fsdata);
1210 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1214 debug("reading %s at pos %llu\n", filename, pos);
1216 /* For saving default max clustersize memory allocated to malloc pool */
1217 dir_entry *dentptr = itr->dent;
1219 ret = get_contents(&fsdata, dentptr, pos, buffer, maxsize, actread);
1222 free(fsdata.fatbuf);
1228 int file_fat_read(const char *filename, void *buffer, int maxsize)
1233 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1240 int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1245 ret = file_fat_read_at(filename, offset, buf, len, actread);
1247 printf("** Unable to read file %s **\n", filename);
1253 struct fs_dir_stream parent;
1254 struct fs_dirent dirent;
1259 int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1264 dir = malloc_cache_aligned(sizeof(*dir));
1267 memset(dir, 0, sizeof(*dir));
1269 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1273 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1275 goto fail_free_both;
1277 *dirsp = (struct fs_dir_stream *)dir;
1281 free(dir->fsdata.fatbuf);
1287 int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1289 fat_dir *dir = (fat_dir *)dirs;
1290 struct fs_dirent *dent = &dir->dirent;
1292 if (!fat_itr_next(&dir->itr))
1295 memset(dent, 0, sizeof(*dent));
1296 strcpy(dent->name, dir->itr.name);
1298 if (fat_itr_isdir(&dir->itr)) {
1299 dent->type = FS_DT_DIR;
1301 dent->type = FS_DT_REG;
1302 dent->size = FAT2CPU32(dir->itr.dent->size);
1310 void fat_closedir(struct fs_dir_stream *dirs)
1312 fat_dir *dir = (fat_dir *)dirs;
1313 free(dir->fsdata.fatbuf);
1317 void fat_close(void)