1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 #define LOG_CATEGORY LOGC_CORE
20 #include <sandboxfs.h>
21 #include <semihostingfs.h>
22 #include <ubifs_uboot.h>
24 #include <asm/global_data.h>
27 #include <linux/math64.h>
28 #include <efi_loader.h>
32 DECLARE_GLOBAL_DATA_PTR;
34 static struct blk_desc *fs_dev_desc;
35 static int fs_dev_part;
36 static struct disk_partition fs_partition;
37 static int fs_type = FS_TYPE_ANY;
39 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
40 struct disk_partition *fs_partition)
42 log_debug("Unrecognized filesystem type\n");
46 static inline int fs_ls_unsupported(const char *dirname)
51 /* generic implementation of ls in terms of opendir/readdir/closedir */
53 static int fs_ls_generic(const char *dirname)
55 struct fs_dir_stream *dirs;
56 struct fs_dirent *dent;
57 int nfiles = 0, ndirs = 0;
59 dirs = fs_opendir(dirname);
63 while ((dent = fs_readdir(dirs))) {
64 if (dent->type == FS_DT_DIR) {
65 printf(" %s/\n", dent->name);
67 } else if (dent->type == FS_DT_LNK) {
68 printf(" <SYM> %s\n", dent->name);
71 printf(" %8lld %s\n", dent->size, dent->name);
78 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
83 static inline int fs_exists_unsupported(const char *filename)
88 static inline int fs_size_unsupported(const char *filename, loff_t *size)
93 static inline int fs_read_unsupported(const char *filename, void *buf,
94 loff_t offset, loff_t len,
100 static inline int fs_write_unsupported(const char *filename, void *buf,
101 loff_t offset, loff_t len,
107 static inline int fs_ln_unsupported(const char *filename, const char *target)
112 static inline void fs_close_unsupported(void)
116 static inline int fs_uuid_unsupported(char *uuid_str)
121 static inline int fs_opendir_unsupported(const char *filename,
122 struct fs_dir_stream **dirs)
127 static inline int fs_unlink_unsupported(const char *filename)
132 static inline int fs_mkdir_unsupported(const char *dirname)
141 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
142 * should be false in most cases. For "virtual" filesystems which
143 * aren't based on a U-Boot block device (e.g. sandbox), this can be
144 * set to true. This should also be true for the dummy entry at the end
145 * of fstypes[], since that is essentially a "virtual" (non-existent)
148 bool null_dev_desc_ok;
149 int (*probe)(struct blk_desc *fs_dev_desc,
150 struct disk_partition *fs_partition);
151 int (*ls)(const char *dirname);
152 int (*exists)(const char *filename);
153 int (*size)(const char *filename, loff_t *size);
154 int (*read)(const char *filename, void *buf, loff_t offset,
155 loff_t len, loff_t *actread);
156 int (*write)(const char *filename, void *buf, loff_t offset,
157 loff_t len, loff_t *actwrite);
159 int (*uuid)(char *uuid_str);
161 * Open a directory stream. On success return 0 and directory
162 * stream pointer via 'dirsp'. On error, return -errno. See
165 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
167 * Read next entry from directory stream. On success return 0
168 * and directory entry pointer via 'dentp'. On error return
169 * -errno. See fs_readdir().
171 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
172 /* see fs_closedir() */
173 void (*closedir)(struct fs_dir_stream *dirs);
174 int (*unlink)(const char *filename);
175 int (*mkdir)(const char *dirname);
176 int (*ln)(const char *filename, const char *target);
179 static struct fstype_info fstypes[] = {
180 #if CONFIG_IS_ENABLED(FS_FAT)
182 .fstype = FS_TYPE_FAT,
184 .null_dev_desc_ok = false,
185 .probe = fat_set_blk_dev,
188 .exists = fat_exists,
190 .read = fat_read_file,
191 #if CONFIG_IS_ENABLED(FAT_WRITE)
192 .write = file_fat_write,
193 .unlink = fat_unlink,
196 .write = fs_write_unsupported,
197 .unlink = fs_unlink_unsupported,
198 .mkdir = fs_mkdir_unsupported,
201 .opendir = fat_opendir,
202 .readdir = fat_readdir,
203 .closedir = fat_closedir,
204 .ln = fs_ln_unsupported,
208 #if CONFIG_IS_ENABLED(FS_EXT4)
210 .fstype = FS_TYPE_EXT,
212 .null_dev_desc_ok = false,
213 .probe = ext4fs_probe,
214 .close = ext4fs_close,
216 .exists = ext4fs_exists,
218 .read = ext4_read_file,
219 #ifdef CONFIG_CMD_EXT4_WRITE
220 .write = ext4_write_file,
221 .ln = ext4fs_create_link,
223 .write = fs_write_unsupported,
224 .ln = fs_ln_unsupported,
227 .opendir = fs_opendir_unsupported,
228 .unlink = fs_unlink_unsupported,
229 .mkdir = fs_mkdir_unsupported,
232 #ifdef CONFIG_SANDBOX
234 .fstype = FS_TYPE_SANDBOX,
236 .null_dev_desc_ok = true,
237 .probe = sandbox_fs_set_blk_dev,
238 .close = sandbox_fs_close,
240 .exists = sandbox_fs_exists,
241 .size = sandbox_fs_size,
242 .read = fs_read_sandbox,
243 .write = fs_write_sandbox,
244 .uuid = fs_uuid_unsupported,
245 .opendir = fs_opendir_unsupported,
246 .unlink = fs_unlink_unsupported,
247 .mkdir = fs_mkdir_unsupported,
248 .ln = fs_ln_unsupported,
251 #ifdef CONFIG_SEMIHOSTING
253 .fstype = FS_TYPE_SEMIHOSTING,
254 .name = "semihosting",
255 .null_dev_desc_ok = true,
256 .probe = smh_fs_set_blk_dev,
257 .close = fs_close_unsupported,
258 .ls = fs_ls_unsupported,
259 .exists = fs_exists_unsupported,
262 .write = smh_fs_write,
263 .uuid = fs_uuid_unsupported,
264 .opendir = fs_opendir_unsupported,
265 .unlink = fs_unlink_unsupported,
266 .mkdir = fs_mkdir_unsupported,
267 .ln = fs_ln_unsupported,
270 #ifndef CONFIG_SPL_BUILD
271 #ifdef CONFIG_CMD_UBIFS
273 .fstype = FS_TYPE_UBIFS,
275 .null_dev_desc_ok = true,
276 .probe = ubifs_set_blk_dev,
277 .close = ubifs_close,
279 .exists = ubifs_exists,
282 .write = fs_write_unsupported,
283 .uuid = fs_uuid_unsupported,
284 .opendir = fs_opendir_unsupported,
285 .unlink = fs_unlink_unsupported,
286 .mkdir = fs_mkdir_unsupported,
287 .ln = fs_ln_unsupported,
291 #ifdef CONFIG_FS_BTRFS
293 .fstype = FS_TYPE_BTRFS,
295 .null_dev_desc_ok = false,
296 .probe = btrfs_probe,
297 .close = btrfs_close,
299 .exists = btrfs_exists,
302 .write = fs_write_unsupported,
304 .opendir = fs_opendir_unsupported,
305 .unlink = fs_unlink_unsupported,
306 .mkdir = fs_mkdir_unsupported,
307 .ln = fs_ln_unsupported,
310 #if IS_ENABLED(CONFIG_FS_SQUASHFS)
312 .fstype = FS_TYPE_SQUASHFS,
314 .null_dev_desc_ok = false,
316 .opendir = sqfs_opendir,
317 .readdir = sqfs_readdir,
322 .closedir = sqfs_closedir,
323 .exists = sqfs_exists,
324 .uuid = fs_uuid_unsupported,
325 .write = fs_write_unsupported,
326 .ln = fs_ln_unsupported,
327 .unlink = fs_unlink_unsupported,
328 .mkdir = fs_mkdir_unsupported,
331 #if IS_ENABLED(CONFIG_FS_EROFS)
333 .fstype = FS_TYPE_EROFS,
335 .null_dev_desc_ok = false,
336 .probe = erofs_probe,
337 .opendir = erofs_opendir,
338 .readdir = erofs_readdir,
342 .close = erofs_close,
343 .closedir = erofs_closedir,
344 .exists = erofs_exists,
345 .uuid = fs_uuid_unsupported,
346 .write = fs_write_unsupported,
347 .ln = fs_ln_unsupported,
348 .unlink = fs_unlink_unsupported,
349 .mkdir = fs_mkdir_unsupported,
353 .fstype = FS_TYPE_ANY,
354 .name = "unsupported",
355 .null_dev_desc_ok = true,
356 .probe = fs_probe_unsupported,
357 .close = fs_close_unsupported,
358 .ls = fs_ls_unsupported,
359 .exists = fs_exists_unsupported,
360 .size = fs_size_unsupported,
361 .read = fs_read_unsupported,
362 .write = fs_write_unsupported,
363 .uuid = fs_uuid_unsupported,
364 .opendir = fs_opendir_unsupported,
365 .unlink = fs_unlink_unsupported,
366 .mkdir = fs_mkdir_unsupported,
367 .ln = fs_ln_unsupported,
371 static struct fstype_info *fs_get_info(int fstype)
373 struct fstype_info *info;
376 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
377 if (fstype == info->fstype)
381 /* Return the 'unsupported' sentinel */
386 * fs_get_type() - Get type of current filesystem
388 * Return: filesystem type
390 * Returns filesystem type representing the current filesystem, or
391 * FS_TYPE_ANY for any unrecognised filesystem.
393 int fs_get_type(void)
399 * fs_get_type_name() - Get type of current filesystem
401 * Return: Pointer to filesystem name
403 * Returns a string describing the current filesystem, or the sentinel
404 * "unsupported" for any unrecognised filesystem.
406 const char *fs_get_type_name(void)
408 return fs_get_info(fs_type)->name;
411 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
413 struct fstype_info *info;
415 #ifdef CONFIG_NEEDS_MANUAL_RELOC
416 static int relocated;
419 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
421 info->name += gd->reloc_off;
422 info->probe += gd->reloc_off;
423 info->close += gd->reloc_off;
424 info->ls += gd->reloc_off;
425 info->read += gd->reloc_off;
426 info->write += gd->reloc_off;
432 part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
437 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
438 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
439 fstype != info->fstype)
442 if (!fs_dev_desc && !info->null_dev_desc_ok)
445 if (!info->probe(fs_dev_desc, &fs_partition)) {
446 fs_type = info->fstype;
455 /* set current blk device w/ blk_desc + partition # */
456 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
458 struct fstype_info *info;
462 ret = part_get_info(desc, part, &fs_partition);
464 ret = part_get_info_whole_disk(desc, &fs_partition);
469 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
470 if (!info->probe(fs_dev_desc, &fs_partition)) {
471 fs_type = info->fstype;
482 struct fstype_info *info = fs_get_info(fs_type);
486 fs_type = FS_TYPE_ANY;
489 int fs_uuid(char *uuid_str)
491 struct fstype_info *info = fs_get_info(fs_type);
493 return info->uuid(uuid_str);
496 int fs_ls(const char *dirname)
500 struct fstype_info *info = fs_get_info(fs_type);
502 ret = info->ls(dirname);
509 int fs_exists(const char *filename)
513 struct fstype_info *info = fs_get_info(fs_type);
515 ret = info->exists(filename);
522 int fs_size(const char *filename, loff_t *size)
526 struct fstype_info *info = fs_get_info(fs_type);
528 ret = info->size(filename, size);
536 /* Check if a file may be read to the given address */
537 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
538 loff_t len, struct fstype_info *info)
545 /* get the actual size of the file */
546 ret = info->size(filename, &size);
549 if (offset >= size) {
550 /* offset >= EOF, no bytes will be written */
553 read_len = size - offset;
555 /* limit to 'len' if it is smaller */
556 if (len && len < read_len)
559 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
562 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
565 log_err("** Reading file would overwrite reserved memory **\n");
570 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
571 int do_lmb_check, loff_t *actread)
573 struct fstype_info *info = fs_get_info(fs_type);
579 ret = fs_read_lmb_check(filename, addr, offset, len, info);
586 * We don't actually know how many bytes are being read, since len==0
587 * means read the whole file.
589 buf = map_sysmem(addr, len);
590 ret = info->read(filename, buf, offset, len, actread);
593 /* If we requested a specific number of bytes, check we got it */
594 if (ret == 0 && len && *actread != len)
595 log_debug("** %s shorter than offset + len **\n", filename);
601 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
604 return _fs_read(filename, addr, offset, len, 0, actread);
607 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
610 struct fstype_info *info = fs_get_info(fs_type);
614 buf = map_sysmem(addr, len);
615 ret = info->write(filename, buf, offset, len, actwrite);
618 if (ret < 0 && len != *actwrite) {
619 log_err("** Unable to write file %s **\n", filename);
627 struct fs_dir_stream *fs_opendir(const char *filename)
629 struct fstype_info *info = fs_get_info(fs_type);
630 struct fs_dir_stream *dirs = NULL;
633 ret = info->opendir(filename, &dirs);
640 dirs->desc = fs_dev_desc;
641 dirs->part = fs_dev_part;
646 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
648 struct fstype_info *info;
649 struct fs_dirent *dirent;
652 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
653 info = fs_get_info(fs_type);
655 ret = info->readdir(dirs, &dirent);
665 void fs_closedir(struct fs_dir_stream *dirs)
667 struct fstype_info *info;
672 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
673 info = fs_get_info(fs_type);
675 info->closedir(dirs);
679 int fs_unlink(const char *filename)
683 struct fstype_info *info = fs_get_info(fs_type);
685 ret = info->unlink(filename);
692 int fs_mkdir(const char *dirname)
696 struct fstype_info *info = fs_get_info(fs_type);
698 ret = info->mkdir(dirname);
705 int fs_ln(const char *fname, const char *target)
707 struct fstype_info *info = fs_get_info(fs_type);
710 ret = info->ln(fname, target);
713 log_err("** Unable to create link %s -> %s **\n", fname, target);
721 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
727 return CMD_RET_USAGE;
729 if (fs_set_blk_dev(argv[1], argv[2], fstype))
732 if (fs_size(argv[3], &size) < 0)
733 return CMD_RET_FAILURE;
735 env_set_hex("filesize", size);
740 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
744 const char *addr_str;
745 const char *filename;
754 return CMD_RET_USAGE;
756 return CMD_RET_USAGE;
758 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
759 log_err("Can't set block device\n");
764 addr = hextoul(argv[3], &ep);
765 if (ep == argv[3] || *ep != '\0')
766 return CMD_RET_USAGE;
768 addr_str = env_get("loadaddr");
769 if (addr_str != NULL)
770 addr = hextoul(addr_str, NULL);
772 addr = CONFIG_SYS_LOAD_ADDR;
777 filename = env_get("bootfile");
779 puts("** No boot file defined **\n");
784 bytes = hextoul(argv[5], NULL);
788 pos = hextoul(argv[6], NULL);
793 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
794 time = get_timer(time);
796 log_err("Failed to load '%s'\n", filename);
800 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
801 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
802 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
805 printf("%llu bytes read in %lu ms", len_read, time);
808 print_size(div_u64(len_read, time) * 1000, "/s");
813 env_set_hex("fileaddr", addr);
814 env_set_hex("filesize", len_read);
819 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
823 return CMD_RET_USAGE;
825 return CMD_RET_USAGE;
827 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
830 if (fs_ls(argc >= 4 ? argv[3] : "/"))
836 int file_exists(const char *dev_type, const char *dev_part, const char *file,
839 if (fs_set_blk_dev(dev_type, dev_part, fstype))
842 return fs_exists(file);
845 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
849 const char *filename;
856 if (argc < 6 || argc > 7)
857 return CMD_RET_USAGE;
859 if (fs_set_blk_dev(argv[1], argv[2], fstype))
862 addr = hextoul(argv[3], NULL);
864 bytes = hextoul(argv[5], NULL);
866 pos = hextoul(argv[6], NULL);
871 ret = fs_write(filename, addr, pos, bytes, &len);
872 time = get_timer(time);
876 printf("%llu bytes written in %lu ms", len, time);
879 print_size(div_u64(len, time) * 1000, "/s");
887 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
892 memset(uuid, 0, sizeof(uuid));
894 if (argc < 3 || argc > 4)
895 return CMD_RET_USAGE;
897 if (fs_set_blk_dev(argv[1], argv[2], fstype))
902 return CMD_RET_FAILURE;
905 env_set(argv[3], uuid);
907 printf("%s\n", uuid);
909 return CMD_RET_SUCCESS;
912 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
914 struct fstype_info *info;
916 if (argc < 3 || argc > 4)
917 return CMD_RET_USAGE;
919 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
922 info = fs_get_info(fs_type);
925 env_set(argv[3], info->name);
927 printf("%s\n", info->name);
931 return CMD_RET_SUCCESS;
934 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
938 return CMD_RET_USAGE;
940 if (fs_set_blk_dev(argv[1], argv[2], fstype))
943 if (fs_unlink(argv[3]))
949 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
955 return CMD_RET_USAGE;
957 if (fs_set_blk_dev(argv[1], argv[2], fstype))
960 ret = fs_mkdir(argv[3]);
962 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
969 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
973 return CMD_RET_USAGE;
975 if (fs_set_blk_dev(argv[1], argv[2], fstype))
978 if (fs_ln(argv[3], argv[4]))
984 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
986 struct fstype_info *drv = fstypes;
987 const int n_ents = ARRAY_SIZE(fstypes);
988 struct fstype_info *entry;
991 puts("Supported filesystems");
992 for (entry = drv; entry != drv + n_ents; entry++) {
993 if (entry->fstype != FS_TYPE_ANY) {
994 printf("%c %s", i ? ',' : ':', entry->name);
1001 return CMD_RET_SUCCESS;