1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
18 #include <sandboxfs.h>
19 #include <ubifs_uboot.h>
23 #include <linux/math64.h>
24 #include <efi_loader.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 static struct blk_desc *fs_dev_desc;
29 static int fs_dev_part;
30 static struct disk_partition fs_partition;
31 static int fs_type = FS_TYPE_ANY;
33 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
34 struct disk_partition *fs_partition)
36 printf("** Unrecognized filesystem type **\n");
40 static inline int fs_ls_unsupported(const char *dirname)
45 /* generic implementation of ls in terms of opendir/readdir/closedir */
47 static int fs_ls_generic(const char *dirname)
49 struct fs_dir_stream *dirs;
50 struct fs_dirent *dent;
51 int nfiles = 0, ndirs = 0;
53 dirs = fs_opendir(dirname);
57 while ((dent = fs_readdir(dirs))) {
58 if (dent->type == FS_DT_DIR) {
59 printf(" %s/\n", dent->name);
62 printf(" %8lld %s\n", dent->size, dent->name);
69 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
74 static inline int fs_exists_unsupported(const char *filename)
79 static inline int fs_size_unsupported(const char *filename, loff_t *size)
84 static inline int fs_read_unsupported(const char *filename, void *buf,
85 loff_t offset, loff_t len,
91 static inline int fs_write_unsupported(const char *filename, void *buf,
92 loff_t offset, loff_t len,
98 static inline int fs_ln_unsupported(const char *filename, const char *target)
103 static inline void fs_close_unsupported(void)
107 static inline int fs_uuid_unsupported(char *uuid_str)
112 static inline int fs_opendir_unsupported(const char *filename,
113 struct fs_dir_stream **dirs)
118 static inline int fs_unlink_unsupported(const char *filename)
123 static inline int fs_mkdir_unsupported(const char *dirname)
132 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
133 * should be false in most cases. For "virtual" filesystems which
134 * aren't based on a U-Boot block device (e.g. sandbox), this can be
135 * set to true. This should also be true for the dummy entry at the end
136 * of fstypes[], since that is essentially a "virtual" (non-existent)
139 bool null_dev_desc_ok;
140 int (*probe)(struct blk_desc *fs_dev_desc,
141 struct disk_partition *fs_partition);
142 int (*ls)(const char *dirname);
143 int (*exists)(const char *filename);
144 int (*size)(const char *filename, loff_t *size);
145 int (*read)(const char *filename, void *buf, loff_t offset,
146 loff_t len, loff_t *actread);
147 int (*write)(const char *filename, void *buf, loff_t offset,
148 loff_t len, loff_t *actwrite);
150 int (*uuid)(char *uuid_str);
152 * Open a directory stream. On success return 0 and directory
153 * stream pointer via 'dirsp'. On error, return -errno. See
156 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
158 * Read next entry from directory stream. On success return 0
159 * and directory entry pointer via 'dentp'. On error return
160 * -errno. See fs_readdir().
162 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
163 /* see fs_closedir() */
164 void (*closedir)(struct fs_dir_stream *dirs);
165 int (*unlink)(const char *filename);
166 int (*mkdir)(const char *dirname);
167 int (*ln)(const char *filename, const char *target);
170 static struct fstype_info fstypes[] = {
173 .fstype = FS_TYPE_FAT,
175 .null_dev_desc_ok = false,
176 .probe = fat_set_blk_dev,
179 .exists = fat_exists,
181 .read = fat_read_file,
182 #if CONFIG_IS_ENABLED(FAT_WRITE)
183 .write = file_fat_write,
184 .unlink = fat_unlink,
187 .write = fs_write_unsupported,
188 .unlink = fs_unlink_unsupported,
189 .mkdir = fs_mkdir_unsupported,
191 .uuid = fs_uuid_unsupported,
192 .opendir = fat_opendir,
193 .readdir = fat_readdir,
194 .closedir = fat_closedir,
195 .ln = fs_ln_unsupported,
199 #if CONFIG_IS_ENABLED(FS_EXT4)
201 .fstype = FS_TYPE_EXT,
203 .null_dev_desc_ok = false,
204 .probe = ext4fs_probe,
205 .close = ext4fs_close,
207 .exists = ext4fs_exists,
209 .read = ext4_read_file,
210 #ifdef CONFIG_CMD_EXT4_WRITE
211 .write = ext4_write_file,
212 .ln = ext4fs_create_link,
214 .write = fs_write_unsupported,
215 .ln = fs_ln_unsupported,
218 .opendir = fs_opendir_unsupported,
219 .unlink = fs_unlink_unsupported,
220 .mkdir = fs_mkdir_unsupported,
223 #ifdef CONFIG_SANDBOX
225 .fstype = FS_TYPE_SANDBOX,
227 .null_dev_desc_ok = true,
228 .probe = sandbox_fs_set_blk_dev,
229 .close = sandbox_fs_close,
231 .exists = sandbox_fs_exists,
232 .size = sandbox_fs_size,
233 .read = fs_read_sandbox,
234 .write = fs_write_sandbox,
235 .uuid = fs_uuid_unsupported,
236 .opendir = fs_opendir_unsupported,
237 .unlink = fs_unlink_unsupported,
238 .mkdir = fs_mkdir_unsupported,
239 .ln = fs_ln_unsupported,
242 #ifdef CONFIG_CMD_UBIFS
244 .fstype = FS_TYPE_UBIFS,
246 .null_dev_desc_ok = true,
247 .probe = ubifs_set_blk_dev,
248 .close = ubifs_close,
250 .exists = ubifs_exists,
253 .write = fs_write_unsupported,
254 .uuid = fs_uuid_unsupported,
255 .opendir = fs_opendir_unsupported,
256 .unlink = fs_unlink_unsupported,
257 .mkdir = fs_mkdir_unsupported,
258 .ln = fs_ln_unsupported,
261 #ifdef CONFIG_FS_BTRFS
263 .fstype = FS_TYPE_BTRFS,
265 .null_dev_desc_ok = false,
266 .probe = btrfs_probe,
267 .close = btrfs_close,
269 .exists = btrfs_exists,
272 .write = fs_write_unsupported,
274 .opendir = fs_opendir_unsupported,
275 .unlink = fs_unlink_unsupported,
276 .mkdir = fs_mkdir_unsupported,
277 .ln = fs_ln_unsupported,
281 .fstype = FS_TYPE_ANY,
282 .name = "unsupported",
283 .null_dev_desc_ok = true,
284 .probe = fs_probe_unsupported,
285 .close = fs_close_unsupported,
286 .ls = fs_ls_unsupported,
287 .exists = fs_exists_unsupported,
288 .size = fs_size_unsupported,
289 .read = fs_read_unsupported,
290 .write = fs_write_unsupported,
291 .uuid = fs_uuid_unsupported,
292 .opendir = fs_opendir_unsupported,
293 .unlink = fs_unlink_unsupported,
294 .mkdir = fs_mkdir_unsupported,
295 .ln = fs_ln_unsupported,
299 static struct fstype_info *fs_get_info(int fstype)
301 struct fstype_info *info;
304 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
305 if (fstype == info->fstype)
309 /* Return the 'unsupported' sentinel */
314 * fs_get_type() - Get type of current filesystem
316 * Return: filesystem type
318 * Returns filesystem type representing the current filesystem, or
319 * FS_TYPE_ANY for any unrecognised filesystem.
321 int fs_get_type(void)
327 * fs_get_type_name() - Get type of current filesystem
329 * Return: Pointer to filesystem name
331 * Returns a string describing the current filesystem, or the sentinel
332 * "unsupported" for any unrecognised filesystem.
334 const char *fs_get_type_name(void)
336 return fs_get_info(fs_type)->name;
339 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
341 struct fstype_info *info;
343 #ifdef CONFIG_NEEDS_MANUAL_RELOC
344 static int relocated;
347 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
349 info->name += gd->reloc_off;
350 info->probe += gd->reloc_off;
351 info->close += gd->reloc_off;
352 info->ls += gd->reloc_off;
353 info->read += gd->reloc_off;
354 info->write += gd->reloc_off;
360 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
365 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
366 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
367 fstype != info->fstype)
370 if (!fs_dev_desc && !info->null_dev_desc_ok)
373 if (!info->probe(fs_dev_desc, &fs_partition)) {
374 fs_type = info->fstype;
383 /* set current blk device w/ blk_desc + partition # */
384 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
386 struct fstype_info *info;
390 ret = part_get_info(desc, part, &fs_partition);
392 ret = part_get_info_whole_disk(desc, &fs_partition);
397 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
398 if (!info->probe(fs_dev_desc, &fs_partition)) {
399 fs_type = info->fstype;
410 struct fstype_info *info = fs_get_info(fs_type);
414 fs_type = FS_TYPE_ANY;
417 int fs_uuid(char *uuid_str)
419 struct fstype_info *info = fs_get_info(fs_type);
421 return info->uuid(uuid_str);
424 int fs_ls(const char *dirname)
428 struct fstype_info *info = fs_get_info(fs_type);
430 ret = info->ls(dirname);
437 int fs_exists(const char *filename)
441 struct fstype_info *info = fs_get_info(fs_type);
443 ret = info->exists(filename);
450 int fs_size(const char *filename, loff_t *size)
454 struct fstype_info *info = fs_get_info(fs_type);
456 ret = info->size(filename, size);
464 /* Check if a file may be read to the given address */
465 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
466 loff_t len, struct fstype_info *info)
473 /* get the actual size of the file */
474 ret = info->size(filename, &size);
477 if (offset >= size) {
478 /* offset >= EOF, no bytes will be written */
481 read_len = size - offset;
483 /* limit to 'len' if it is smaller */
484 if (len && len < read_len)
487 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
490 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
493 printf("** Reading file would overwrite reserved memory **\n");
498 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
499 int do_lmb_check, loff_t *actread)
501 struct fstype_info *info = fs_get_info(fs_type);
507 ret = fs_read_lmb_check(filename, addr, offset, len, info);
514 * We don't actually know how many bytes are being read, since len==0
515 * means read the whole file.
517 buf = map_sysmem(addr, len);
518 ret = info->read(filename, buf, offset, len, actread);
521 /* If we requested a specific number of bytes, check we got it */
522 if (ret == 0 && len && *actread != len)
523 debug("** %s shorter than offset + len **\n", filename);
529 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
532 return _fs_read(filename, addr, offset, len, 0, actread);
535 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
538 struct fstype_info *info = fs_get_info(fs_type);
542 buf = map_sysmem(addr, len);
543 ret = info->write(filename, buf, offset, len, actwrite);
546 if (ret < 0 && len != *actwrite) {
547 printf("** Unable to write file %s **\n", filename);
555 struct fs_dir_stream *fs_opendir(const char *filename)
557 struct fstype_info *info = fs_get_info(fs_type);
558 struct fs_dir_stream *dirs = NULL;
561 ret = info->opendir(filename, &dirs);
568 dirs->desc = fs_dev_desc;
569 dirs->part = fs_dev_part;
574 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
576 struct fstype_info *info;
577 struct fs_dirent *dirent;
580 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
581 info = fs_get_info(fs_type);
583 ret = info->readdir(dirs, &dirent);
593 void fs_closedir(struct fs_dir_stream *dirs)
595 struct fstype_info *info;
600 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
601 info = fs_get_info(fs_type);
603 info->closedir(dirs);
607 int fs_unlink(const char *filename)
611 struct fstype_info *info = fs_get_info(fs_type);
613 ret = info->unlink(filename);
620 int fs_mkdir(const char *dirname)
624 struct fstype_info *info = fs_get_info(fs_type);
626 ret = info->mkdir(dirname);
633 int fs_ln(const char *fname, const char *target)
635 struct fstype_info *info = fs_get_info(fs_type);
638 ret = info->ln(fname, target);
641 printf("** Unable to create link %s -> %s **\n", fname, target);
649 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
655 return CMD_RET_USAGE;
657 if (fs_set_blk_dev(argv[1], argv[2], fstype))
660 if (fs_size(argv[3], &size) < 0)
661 return CMD_RET_FAILURE;
663 env_set_hex("filesize", size);
668 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
672 const char *addr_str;
673 const char *filename;
682 return CMD_RET_USAGE;
684 return CMD_RET_USAGE;
686 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
690 addr = simple_strtoul(argv[3], &ep, 16);
691 if (ep == argv[3] || *ep != '\0')
692 return CMD_RET_USAGE;
694 addr_str = env_get("loadaddr");
695 if (addr_str != NULL)
696 addr = simple_strtoul(addr_str, NULL, 16);
698 addr = CONFIG_SYS_LOAD_ADDR;
703 filename = env_get("bootfile");
705 puts("** No boot file defined **\n");
710 bytes = simple_strtoul(argv[5], NULL, 16);
714 pos = simple_strtoul(argv[6], NULL, 16);
719 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
720 time = get_timer(time);
722 printf("Failed to load '%s'\n", filename);
726 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
727 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
728 (argc > 4) ? argv[4] : "");
730 printf("%llu bytes read in %lu ms", len_read, time);
733 print_size(div_u64(len_read, time) * 1000, "/s");
738 env_set_hex("fileaddr", addr);
739 env_set_hex("filesize", len_read);
744 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
748 return CMD_RET_USAGE;
750 return CMD_RET_USAGE;
752 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
755 if (fs_ls(argc >= 4 ? argv[3] : "/"))
761 int file_exists(const char *dev_type, const char *dev_part, const char *file,
764 if (fs_set_blk_dev(dev_type, dev_part, fstype))
767 return fs_exists(file);
770 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
774 const char *filename;
781 if (argc < 6 || argc > 7)
782 return CMD_RET_USAGE;
784 if (fs_set_blk_dev(argv[1], argv[2], fstype))
787 addr = simple_strtoul(argv[3], NULL, 16);
789 bytes = simple_strtoul(argv[5], NULL, 16);
791 pos = simple_strtoul(argv[6], NULL, 16);
796 ret = fs_write(filename, addr, pos, bytes, &len);
797 time = get_timer(time);
801 printf("%llu bytes written in %lu ms", len, time);
804 print_size(div_u64(len, time) * 1000, "/s");
812 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
817 memset(uuid, 0, sizeof(uuid));
819 if (argc < 3 || argc > 4)
820 return CMD_RET_USAGE;
822 if (fs_set_blk_dev(argv[1], argv[2], fstype))
827 return CMD_RET_FAILURE;
830 env_set(argv[3], uuid);
832 printf("%s\n", uuid);
834 return CMD_RET_SUCCESS;
837 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
839 struct fstype_info *info;
841 if (argc < 3 || argc > 4)
842 return CMD_RET_USAGE;
844 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
847 info = fs_get_info(fs_type);
850 env_set(argv[3], info->name);
852 printf("%s\n", info->name);
856 return CMD_RET_SUCCESS;
859 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
863 return CMD_RET_USAGE;
865 if (fs_set_blk_dev(argv[1], argv[2], fstype))
868 if (fs_unlink(argv[3]))
874 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
880 return CMD_RET_USAGE;
882 if (fs_set_blk_dev(argv[1], argv[2], fstype))
885 ret = fs_mkdir(argv[3]);
887 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
894 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
898 return CMD_RET_USAGE;
900 if (fs_set_blk_dev(argv[1], argv[2], fstype))
903 if (fs_ln(argv[3], argv[4]))
909 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
911 struct fstype_info *drv = fstypes;
912 const int n_ents = ARRAY_SIZE(fstypes);
913 struct fstype_info *entry;
916 puts("Supported filesystems");
917 for (entry = drv; entry != drv + n_ents; entry++) {
918 if (entry->fstype != FS_TYPE_ANY) {
919 printf("%c %s", i ? ',' : ':', entry->name);
926 return CMD_RET_SUCCESS;