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);
149 #if !CONFIG_IS_ENABLED(FAT_WRITE)
150 /* Stub for read only operation */
151 int flush_dirty_fat_buffer(fsdata *mydata)
159 * Get the entry at index 'entry' in a FAT (12/16/32) table.
160 * On failure 0x00 is returned.
162 static __u32 get_fatent(fsdata *mydata, __u32 entry)
168 if (CHECK_CLUST(entry, mydata->fatsize)) {
169 printf("Error: Invalid FAT entry: 0x%08x\n", entry);
173 switch (mydata->fatsize) {
175 bufnum = entry / FAT32BUFSIZE;
176 offset = entry - bufnum * FAT32BUFSIZE;
179 bufnum = entry / FAT16BUFSIZE;
180 offset = entry - bufnum * FAT16BUFSIZE;
183 bufnum = entry / FAT12BUFSIZE;
184 offset = entry - bufnum * FAT12BUFSIZE;
188 /* Unsupported FAT size */
192 debug("FAT%d: entry: 0x%08x = %d, offset: 0x%04x = %d\n",
193 mydata->fatsize, entry, entry, offset, offset);
195 /* Read a new block of FAT entries into the cache. */
196 if (bufnum != mydata->fatbufnum) {
197 __u32 getsize = FATBUFBLOCKS;
198 __u8 *bufptr = mydata->fatbuf;
199 __u32 fatlength = mydata->fatlength;
200 __u32 startblock = bufnum * FATBUFBLOCKS;
202 /* Cap length if fatlength is not a multiple of FATBUFBLOCKS */
203 if (startblock + getsize > fatlength)
204 getsize = fatlength - startblock;
206 startblock += mydata->fat_sect; /* Offset from start of disk */
208 /* Write back the fatbuf to the disk */
209 if (flush_dirty_fat_buffer(mydata) < 0)
212 if (disk_read(startblock, getsize, bufptr) < 0) {
213 debug("Error reading FAT blocks\n");
216 mydata->fatbufnum = bufnum;
219 /* Get the actual entry from the table */
220 switch (mydata->fatsize) {
222 ret = FAT2CPU32(((__u32 *) mydata->fatbuf)[offset]);
225 ret = FAT2CPU16(((__u16 *) mydata->fatbuf)[offset]);
228 off8 = (offset * 3) / 2;
229 /* fatbut + off8 may be unaligned, read in byte granularity */
230 ret = mydata->fatbuf[off8] + (mydata->fatbuf[off8 + 1] << 8);
236 debug("FAT%d: ret: 0x%08x, entry: 0x%08x, offset: 0x%04x\n",
237 mydata->fatsize, ret, entry, offset);
243 * Read at most 'size' bytes from the specified cluster into 'buffer'.
244 * Return 0 on success, -1 otherwise.
247 get_cluster(fsdata *mydata, __u32 clustnum, __u8 *buffer, unsigned long size)
254 startsect = clust_to_sect(mydata, clustnum);
256 startsect = mydata->rootdir_sect;
259 debug("gc - clustnum: %d, startsect: %d\n", clustnum, startsect);
261 if ((unsigned long)buffer & (ARCH_DMA_MINALIGN - 1)) {
262 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
264 debug("FAT: Misaligned buffer address (%p)\n", buffer);
266 while (size >= mydata->sect_size) {
267 ret = disk_read(startsect++, 1, tmpbuf);
269 debug("Error reading data (got %d)\n", ret);
273 memcpy(buffer, tmpbuf, mydata->sect_size);
274 buffer += mydata->sect_size;
275 size -= mydata->sect_size;
278 idx = size / mydata->sect_size;
279 ret = disk_read(startsect, idx, buffer);
281 debug("Error reading data (got %d)\n", ret);
285 idx *= mydata->sect_size;
290 ALLOC_CACHE_ALIGN_BUFFER(__u8, tmpbuf, mydata->sect_size);
292 ret = disk_read(startsect, 1, tmpbuf);
294 debug("Error reading data (got %d)\n", ret);
298 memcpy(buffer, tmpbuf, size);
305 * get_contents() - read from file
307 * Read at most 'maxsize' bytes from 'pos' in the file associated with 'dentptr'
308 * into 'buffer'. Update the number of bytes read in *gotsize or return -1 on
311 * @mydata: file system description
312 * @dentprt: directory entry pointer
313 * @pos: position from where to read
314 * @buffer: buffer into which to read
315 * @maxsize: maximum number of bytes to read
316 * @gotsize: number of bytes actually read
317 * Return: -1 on error, otherwise 0
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 printf("Invalid FAT entry\n");
351 actsize += bytesperclust;
355 actsize -= bytesperclust;
359 /* align to beginning of next cluster if any */
363 actsize = min(filesize, (loff_t)bytesperclust);
364 tmp_buffer = malloc_cache_aligned(actsize);
366 debug("Error: allocating buffer\n");
370 if (get_cluster(mydata, curclust, tmp_buffer, actsize) != 0) {
371 printf("Error reading cluster\n");
377 memcpy(buffer, tmp_buffer + pos, actsize);
384 curclust = get_fatent(mydata, curclust);
385 if (CHECK_CLUST(curclust, mydata->fatsize)) {
386 debug("curclust: 0x%x\n", curclust);
387 printf("Invalid FAT entry\n");
392 actsize = bytesperclust;
396 /* search for consecutive clusters */
397 while (actsize < filesize) {
398 newclust = get_fatent(mydata, endclust);
399 if ((newclust - 1) != endclust)
401 if (CHECK_CLUST(newclust, mydata->fatsize)) {
402 debug("curclust: 0x%x\n", newclust);
403 printf("Invalid FAT entry\n");
407 actsize += bytesperclust;
410 /* get remaining bytes */
412 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
413 printf("Error reading cluster\n");
419 if (get_cluster(mydata, curclust, buffer, (int)actsize) != 0) {
420 printf("Error reading cluster\n");
423 *gotsize += (int)actsize;
427 curclust = get_fatent(mydata, endclust);
428 if (CHECK_CLUST(curclust, mydata->fatsize)) {
429 debug("curclust: 0x%x\n", curclust);
430 printf("Invalid FAT entry\n");
433 actsize = bytesperclust;
439 * Extract the file name information from 'slotptr' into 'l_name',
440 * starting at l_name[*idx].
441 * Return 1 if terminator (zero byte) is found, 0 otherwise.
443 static int slot2str(dir_slot *slotptr, char *l_name, int *idx)
447 for (j = 0; j <= 8; j += 2) {
448 l_name[*idx] = slotptr->name0_4[j];
449 if (l_name[*idx] == 0x00)
453 for (j = 0; j <= 10; j += 2) {
454 l_name[*idx] = slotptr->name5_10[j];
455 if (l_name[*idx] == 0x00)
459 for (j = 0; j <= 2; j += 2) {
460 l_name[*idx] = slotptr->name11_12[j];
461 if (l_name[*idx] == 0x00)
469 /* Calculate short name checksum */
470 static __u8 mkcksum(const char name[8], const char ext[3])
476 for (i = 0; i < 8; i++)
477 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + name[i];
478 for (i = 0; i < 3; i++)
479 ret = (((ret & 1) << 7) | ((ret & 0xfe) >> 1)) + ext[i];
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 = malloc_cache_aligned(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;
569 mydata->total_sect = bs.total_sect;
571 mydata->fatlength = bs.fat_length;
572 mydata->total_sect = (bs.sectors[1] << 8) + bs.sectors[0];
573 if (!mydata->total_sect)
574 mydata->total_sect = bs.total_sect;
576 if (!mydata->total_sect) /* unlikely */
577 mydata->total_sect = (u32)cur_part_info.size;
579 mydata->fats = bs.fats;
580 mydata->fat_sect = bs.reserved;
582 mydata->rootdir_sect = mydata->fat_sect + mydata->fatlength * bs.fats;
584 mydata->sect_size = (bs.sector_size[1] << 8) + bs.sector_size[0];
585 mydata->clust_size = bs.cluster_size;
586 if (mydata->sect_size != cur_part_info.blksz) {
587 printf("Error: FAT sector size mismatch (fs=%hu, dev=%lu)\n",
588 mydata->sect_size, cur_part_info.blksz);
591 if (mydata->clust_size == 0) {
592 printf("Error: FAT cluster size not set\n");
595 if ((unsigned int)mydata->clust_size * mydata->sect_size >
597 printf("Error: FAT cluster size too big (cs=%u, max=%u)\n",
598 (unsigned int)mydata->clust_size * mydata->sect_size,
603 if (mydata->fatsize == 32) {
604 mydata->data_begin = mydata->rootdir_sect -
605 (mydata->clust_size * 2);
606 mydata->root_cluster = bs.root_cluster;
608 mydata->rootdir_size = ((bs.dir_entries[1] * (int)256 +
612 mydata->data_begin = mydata->rootdir_sect +
613 mydata->rootdir_size -
614 (mydata->clust_size * 2);
617 * The root directory is not cluster-aligned and may be on a
618 * "negative" cluster, this will be handled specially in
621 mydata->root_cluster = 0;
624 mydata->fatbufnum = -1;
625 mydata->fat_dirty = 0;
626 mydata->fatbuf = malloc_cache_aligned(FATBUFSIZE);
627 if (mydata->fatbuf == NULL) {
628 debug("Error: allocating memory\n");
632 debug("FAT%d, fat_sect: %d, fatlength: %d\n",
633 mydata->fatsize, mydata->fat_sect, mydata->fatlength);
634 debug("Rootdir begins at cluster: %d, sector: %d, offset: %x\n"
635 "Data begins at: %d\n",
636 mydata->root_cluster,
637 mydata->rootdir_sect,
638 mydata->rootdir_sect * mydata->sect_size, mydata->data_begin);
639 debug("Sector size: %d, cluster size: %d\n", mydata->sect_size,
647 * Directory iterator, to simplify filesystem traversal
649 * Implements an iterator pattern to traverse directory tables,
650 * transparently handling directory tables split across multiple
651 * clusters, and the difference between FAT12/FAT16 root directory
652 * (contiguous) and subdirectories + FAT32 root (chained).
656 * for (fat_itr_root(&itr, fsdata); fat_itr_next(&itr); ) {
657 * // to traverse down to a subdirectory pointed to by
658 * // current iterator position:
659 * fat_itr_child(&itr, &itr);
662 * For more complete example, see fat_itr_resolve()
666 fsdata *fsdata; /* filesystem parameters */
667 unsigned start_clust; /* first cluster */
668 unsigned clust; /* current cluster */
669 unsigned next_clust; /* next cluster if remaining == 0 */
670 int last_cluster; /* set once we've read last cluster */
671 int is_root; /* is iterator at root directory */
672 int remaining; /* remaining dent's in current cluster */
674 /* current iterator position values: */
675 dir_entry *dent; /* current directory entry */
676 char l_name[VFAT_MAXLEN_BYTES]; /* long (vfat) name */
677 char s_name[14]; /* short 8.3 name */
678 char *name; /* l_name if there is one, else s_name */
680 /* storage for current cluster in memory: */
681 u8 block[MAX_CLUSTSIZE] __aligned(ARCH_DMA_MINALIGN);
684 static int fat_itr_isdir(fat_itr *itr);
687 * fat_itr_root() - initialize an iterator to start at the root
690 * @itr: iterator to initialize
691 * @fsdata: filesystem data for the partition
692 * @return 0 on success, else -errno
694 static int fat_itr_root(fat_itr *itr, fsdata *fsdata)
696 if (get_fs_info(fsdata))
699 itr->fsdata = fsdata;
700 itr->start_clust = 0;
701 itr->clust = fsdata->root_cluster;
702 itr->next_clust = fsdata->root_cluster;
705 itr->last_cluster = 0;
712 * fat_itr_child() - initialize an iterator to descend into a sub-
715 * Initializes 'itr' to iterate the contents of the directory at
716 * the current cursor position of 'parent'. It is an error to
717 * call this if the current cursor of 'parent' is pointing at a
720 * Note that 'itr' and 'parent' can be the same pointer if you do
721 * not need to preserve 'parent' after this call, which is useful
722 * for traversing directory structure to resolve a file/directory.
724 * @itr: iterator to initialize
725 * @parent: the iterator pointing at a directory entry in the
726 * parent directory of the directory to iterate
728 static void fat_itr_child(fat_itr *itr, fat_itr *parent)
730 fsdata *mydata = parent->fsdata; /* for silly macros */
731 unsigned clustnum = START(parent->dent);
733 assert(fat_itr_isdir(parent));
735 itr->fsdata = parent->fsdata;
736 itr->start_clust = clustnum;
738 itr->clust = clustnum;
739 itr->next_clust = clustnum;
742 itr->clust = parent->fsdata->root_cluster;
743 itr->next_clust = parent->fsdata->root_cluster;
748 itr->last_cluster = 0;
751 static void *next_cluster(fat_itr *itr, unsigned *nbytes)
753 fsdata *mydata = itr->fsdata; /* for silly macros */
758 /* have we reached the end? */
759 if (itr->last_cluster)
762 if (itr->is_root && itr->fsdata->fatsize != 32) {
764 * The root directory is located before the data area and
765 * cannot be indexed using the regular unsigned cluster
766 * numbers (it may start at a "negative" cluster or not at a
767 * cluster boundary at all), so consider itr->next_clust to be
768 * a offset in cluster-sized units from the start of rootdir.
770 unsigned sect_offset = itr->next_clust * itr->fsdata->clust_size;
771 unsigned remaining_sects = itr->fsdata->rootdir_size - sect_offset;
772 sect = itr->fsdata->rootdir_sect + sect_offset;
773 /* do not read past the end of rootdir */
774 read_size = min_t(u32, itr->fsdata->clust_size,
777 sect = clust_to_sect(itr->fsdata, itr->next_clust);
778 read_size = itr->fsdata->clust_size;
781 debug("FAT read(sect=%d), clust_size=%d, read_size=%u, DIRENTSPERBLOCK=%zd\n",
782 sect, itr->fsdata->clust_size, read_size, DIRENTSPERBLOCK);
785 * NOTE: do_fat_read_at() had complicated logic to deal w/
786 * vfat names that span multiple clusters in the fat16 case,
787 * which get_dentfromdir() probably also needed (and was
788 * missing). And not entirely sure what fat32 didn't have
789 * the same issue.. We solve that by only caring about one
790 * dent at a time and iteratively constructing the vfat long
793 ret = disk_read(sect, read_size, itr->block);
795 debug("Error: reading block\n");
799 *nbytes = read_size * itr->fsdata->sect_size;
800 itr->clust = itr->next_clust;
801 if (itr->is_root && itr->fsdata->fatsize != 32) {
803 if (itr->next_clust * itr->fsdata->clust_size >=
804 itr->fsdata->rootdir_size) {
805 debug("nextclust: 0x%x\n", itr->next_clust);
806 itr->last_cluster = 1;
809 itr->next_clust = get_fatent(itr->fsdata, itr->next_clust);
810 if (CHECK_CLUST(itr->next_clust, itr->fsdata->fatsize)) {
811 debug("nextclust: 0x%x\n", itr->next_clust);
812 itr->last_cluster = 1;
819 static dir_entry *next_dent(fat_itr *itr)
821 if (itr->remaining == 0) {
823 struct dir_entry *dent = next_cluster(itr, &nbytes);
825 /* have we reached the last cluster? */
827 /* a sign for no more entries left */
832 itr->remaining = nbytes / sizeof(dir_entry) - 1;
839 /* have we reached the last valid entry? */
840 if (itr->dent->name[0] == 0)
846 static dir_entry *extract_vfat_name(fat_itr *itr)
848 struct dir_entry *dent = itr->dent;
849 int seqn = itr->dent->name[0] & ~LAST_LONG_ENTRY_MASK;
850 u8 chksum, alias_checksum = ((dir_slot *)dent)->alias_checksum;
857 slot2str((dir_slot *)dent, buf, &idx);
859 if (n + idx >= sizeof(itr->l_name))
862 /* shift accumulated long-name up and copy new part in: */
863 memmove(itr->l_name + idx, itr->l_name, n);
864 memcpy(itr->l_name, buf, idx);
867 dent = next_dent(itr);
873 * We are now at the short file name entry.
874 * If it is marked as deleted, just skip it.
876 if (dent->name[0] == DELETED_FLAG ||
877 dent->name[0] == aRING)
880 itr->l_name[n] = '\0';
882 chksum = mkcksum(dent->name, dent->ext);
884 /* checksum mismatch could mean deleted file, etc.. skip it: */
885 if (chksum != alias_checksum) {
886 debug("** chksum=%x, alias_checksum=%x, l_name=%s, s_name=%8s.%3s\n",
887 chksum, alias_checksum, itr->l_name, dent->name, dent->ext);
895 * fat_itr_next() - step to the next entry in a directory
897 * Must be called once on a new iterator before the cursor is valid.
899 * @itr: the iterator to iterate
900 * @return boolean, 1 if success or 0 if no more entries in the
903 static int fat_itr_next(fat_itr *itr)
910 * One logical directory entry consist of following slots:
912 * dent[N - N]: LFN[N - 1] N|0x40 ATTR_VFAT
914 * dent[N - 2]: LFN[1] 2 ATTR_VFAT
915 * dent[N - 1]: LFN[0] 1 ATTR_VFAT
916 * dent[N]: SFN ATTR_ARCH
920 dent = next_dent(itr);
924 if (dent->name[0] == DELETED_FLAG ||
925 dent->name[0] == aRING)
928 if (dent->attr & ATTR_VOLUME) {
929 if ((dent->attr & ATTR_VFAT) == ATTR_VFAT &&
930 (dent->name[0] & LAST_LONG_ENTRY_MASK)) {
932 dent = extract_vfat_name(itr);
934 * If succeeded, dent has a valid short file
935 * name entry for the current entry.
936 * If failed, itr points to a current bogus
937 * entry. So after fetching a next one,
938 * it may have a short file name entry
939 * for this bogus entry so that we can still
940 * check for a short name.
944 itr->name = itr->l_name;
947 /* Volume label or VFAT entry, skip */
950 } else if (!(dent->attr & ATTR_ARCH) &&
951 !(dent->attr & ATTR_DIR))
954 /* short file name */
958 get_name(dent, itr->s_name);
960 itr->name = itr->s_name;
966 * fat_itr_isdir() - is current cursor position pointing to a directory
969 * @return true if cursor is at a directory
971 static int fat_itr_isdir(fat_itr *itr)
973 return !!(itr->dent->attr & ATTR_DIR);
980 #define TYPE_FILE 0x1
982 #define TYPE_ANY (TYPE_FILE | TYPE_DIR)
985 * fat_itr_resolve() - traverse directory structure to resolve the
988 * Traverse directory structure to the requested path. If the specified
989 * path is to a directory, this will descend into the directory and
990 * leave it iterator at the start of the directory. If the path is to a
991 * file, it will leave the iterator in the parent directory with current
992 * cursor at file's entry in the directory.
994 * @itr: iterator initialized to root
995 * @path: the requested path
996 * @type: bitmask of allowable file types
997 * @return 0 on success or -errno
999 static int fat_itr_resolve(fat_itr *itr, const char *path, unsigned type)
1003 /* chomp any extra leading slashes: */
1004 while (path[0] && ISDIRDELIM(path[0]))
1007 /* are we at the end? */
1008 if (strlen(path) == 0) {
1009 if (!(type & TYPE_DIR))
1014 /* find length of next path entry: */
1016 while (next[0] && !ISDIRDELIM(next[0]))
1020 /* root dir doesn't have "." nor ".." */
1021 if ((((next - path) == 1) && !strncmp(path, ".", 1)) ||
1022 (((next - path) == 2) && !strncmp(path, "..", 2))) {
1023 /* point back to itself */
1024 itr->clust = itr->fsdata->root_cluster;
1025 itr->next_clust = itr->fsdata->root_cluster;
1028 itr->last_cluster = 0;
1031 if (type & TYPE_DIR)
1037 return fat_itr_resolve(itr, next, type);
1041 while (fat_itr_next(itr)) {
1043 unsigned n = max(strlen(itr->name), (size_t)(next - path));
1045 /* check both long and short name: */
1046 if (!strncasecmp(path, itr->name, n))
1048 else if (itr->name != itr->s_name &&
1049 !strncasecmp(path, itr->s_name, n))
1055 if (fat_itr_isdir(itr)) {
1056 /* recurse into directory: */
1057 fat_itr_child(itr, itr);
1058 return fat_itr_resolve(itr, next, type);
1059 } else if (next[0]) {
1061 * If next is not empty then we have a case
1062 * like: /path/to/realfile/nonsense
1064 debug("bad trailing path: %s\n", next);
1066 } else if (!(type & TYPE_FILE)) {
1076 int file_fat_detectfs(void)
1079 volume_info volinfo;
1083 if (cur_dev == NULL) {
1084 printf("No current device\n");
1088 #if defined(CONFIG_IDE) || \
1089 defined(CONFIG_SATA) || \
1090 defined(CONFIG_SCSI) || \
1091 defined(CONFIG_CMD_USB) || \
1093 printf("Interface: ");
1094 switch (cur_dev->if_type) {
1120 printf("\n Device %d: ", cur_dev->devnum);
1124 if (read_bootsectandvi(&bs, &volinfo, &fatsize)) {
1125 printf("\nNo valid FAT fs found\n");
1129 memcpy(vol_label, volinfo.volume_label, 11);
1130 vol_label[11] = '\0';
1131 volinfo.fs_type[5] = '\0';
1133 printf("Filesystem: %s \"%s\"\n", volinfo.fs_type, vol_label);
1138 int fat_exists(const char *filename)
1144 itr = malloc_cache_aligned(sizeof(fat_itr));
1147 ret = fat_itr_root(itr, &fsdata);
1151 ret = fat_itr_resolve(itr, filename, TYPE_ANY);
1152 free(fsdata.fatbuf);
1158 int fat_size(const char *filename, loff_t *size)
1164 itr = malloc_cache_aligned(sizeof(fat_itr));
1167 ret = fat_itr_root(itr, &fsdata);
1171 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1174 * Directories don't have size, but fs_size() is not
1175 * expected to fail if passed a directory path:
1177 free(fsdata.fatbuf);
1178 ret = fat_itr_root(itr, &fsdata);
1181 ret = fat_itr_resolve(itr, filename, TYPE_DIR);
1187 *size = FAT2CPU32(itr->dent->size);
1189 free(fsdata.fatbuf);
1195 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
1196 loff_t maxsize, loff_t *actread)
1202 itr = malloc_cache_aligned(sizeof(fat_itr));
1205 ret = fat_itr_root(itr, &fsdata);
1209 ret = fat_itr_resolve(itr, filename, TYPE_FILE);
1213 debug("reading %s at pos %llu\n", filename, pos);
1215 /* For saving default max clustersize memory allocated to malloc pool */
1216 dir_entry *dentptr = itr->dent;
1218 ret = get_contents(&fsdata, dentptr, pos, buffer, maxsize, actread);
1221 free(fsdata.fatbuf);
1227 int file_fat_read(const char *filename, void *buffer, int maxsize)
1232 ret = file_fat_read_at(filename, 0, buffer, maxsize, &actread);
1239 int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
1244 ret = file_fat_read_at(filename, offset, buf, len, actread);
1246 printf("** Unable to read file %s **\n", filename);
1252 struct fs_dir_stream parent;
1253 struct fs_dirent dirent;
1258 int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
1263 dir = malloc_cache_aligned(sizeof(*dir));
1266 memset(dir, 0, sizeof(*dir));
1268 ret = fat_itr_root(&dir->itr, &dir->fsdata);
1272 ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
1274 goto fail_free_both;
1276 *dirsp = (struct fs_dir_stream *)dir;
1280 free(dir->fsdata.fatbuf);
1286 int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
1288 fat_dir *dir = (fat_dir *)dirs;
1289 struct fs_dirent *dent = &dir->dirent;
1291 if (!fat_itr_next(&dir->itr))
1294 memset(dent, 0, sizeof(*dent));
1295 strcpy(dent->name, dir->itr.name);
1297 if (fat_itr_isdir(&dir->itr)) {
1298 dent->type = FS_DT_DIR;
1300 dent->type = FS_DT_REG;
1301 dent->size = FAT2CPU32(dir->itr.dent->size);
1309 void fat_closedir(struct fs_dir_stream *dirs)
1311 fat_dir *dir = (fat_dir *)dirs;
1312 free(dir->fsdata.fatbuf);
1316 void fat_close(void)