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>
27 DECLARE_GLOBAL_DATA_PTR;
29 static struct blk_desc *fs_dev_desc;
30 static int fs_dev_part;
31 static struct disk_partition fs_partition;
32 static int fs_type = FS_TYPE_ANY;
34 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
35 struct disk_partition *fs_partition)
37 printf("** Unrecognized filesystem type **\n");
41 static inline int fs_ls_unsupported(const char *dirname)
46 /* generic implementation of ls in terms of opendir/readdir/closedir */
48 static int fs_ls_generic(const char *dirname)
50 struct fs_dir_stream *dirs;
51 struct fs_dirent *dent;
52 int nfiles = 0, ndirs = 0;
54 dirs = fs_opendir(dirname);
58 while ((dent = fs_readdir(dirs))) {
59 if (dent->type == FS_DT_DIR) {
60 printf(" %s/\n", dent->name);
63 printf(" %8lld %s\n", dent->size, dent->name);
70 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
75 static inline int fs_exists_unsupported(const char *filename)
80 static inline int fs_size_unsupported(const char *filename, loff_t *size)
85 static inline int fs_read_unsupported(const char *filename, void *buf,
86 loff_t offset, loff_t len,
92 static inline int fs_write_unsupported(const char *filename, void *buf,
93 loff_t offset, loff_t len,
99 static inline int fs_ln_unsupported(const char *filename, const char *target)
104 static inline void fs_close_unsupported(void)
108 static inline int fs_uuid_unsupported(char *uuid_str)
113 static inline int fs_opendir_unsupported(const char *filename,
114 struct fs_dir_stream **dirs)
119 static inline int fs_unlink_unsupported(const char *filename)
124 static inline int fs_mkdir_unsupported(const char *dirname)
133 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
134 * should be false in most cases. For "virtual" filesystems which
135 * aren't based on a U-Boot block device (e.g. sandbox), this can be
136 * set to true. This should also be true for the dummy entry at the end
137 * of fstypes[], since that is essentially a "virtual" (non-existent)
140 bool null_dev_desc_ok;
141 int (*probe)(struct blk_desc *fs_dev_desc,
142 struct disk_partition *fs_partition);
143 int (*ls)(const char *dirname);
144 int (*exists)(const char *filename);
145 int (*size)(const char *filename, loff_t *size);
146 int (*read)(const char *filename, void *buf, loff_t offset,
147 loff_t len, loff_t *actread);
148 int (*write)(const char *filename, void *buf, loff_t offset,
149 loff_t len, loff_t *actwrite);
151 int (*uuid)(char *uuid_str);
153 * Open a directory stream. On success return 0 and directory
154 * stream pointer via 'dirsp'. On error, return -errno. See
157 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
159 * Read next entry from directory stream. On success return 0
160 * and directory entry pointer via 'dentp'. On error return
161 * -errno. See fs_readdir().
163 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
164 /* see fs_closedir() */
165 void (*closedir)(struct fs_dir_stream *dirs);
166 int (*unlink)(const char *filename);
167 int (*mkdir)(const char *dirname);
168 int (*ln)(const char *filename, const char *target);
171 static struct fstype_info fstypes[] = {
174 .fstype = FS_TYPE_FAT,
176 .null_dev_desc_ok = false,
177 .probe = fat_set_blk_dev,
180 .exists = fat_exists,
182 .read = fat_read_file,
183 #if CONFIG_IS_ENABLED(FAT_WRITE)
184 .write = file_fat_write,
185 .unlink = fat_unlink,
188 .write = fs_write_unsupported,
189 .unlink = fs_unlink_unsupported,
190 .mkdir = fs_mkdir_unsupported,
192 .uuid = fs_uuid_unsupported,
193 .opendir = fat_opendir,
194 .readdir = fat_readdir,
195 .closedir = fat_closedir,
196 .ln = fs_ln_unsupported,
200 #if CONFIG_IS_ENABLED(FS_EXT4)
202 .fstype = FS_TYPE_EXT,
204 .null_dev_desc_ok = false,
205 .probe = ext4fs_probe,
206 .close = ext4fs_close,
208 .exists = ext4fs_exists,
210 .read = ext4_read_file,
211 #ifdef CONFIG_CMD_EXT4_WRITE
212 .write = ext4_write_file,
213 .ln = ext4fs_create_link,
215 .write = fs_write_unsupported,
216 .ln = fs_ln_unsupported,
219 .opendir = fs_opendir_unsupported,
220 .unlink = fs_unlink_unsupported,
221 .mkdir = fs_mkdir_unsupported,
224 #ifdef CONFIG_SANDBOX
226 .fstype = FS_TYPE_SANDBOX,
228 .null_dev_desc_ok = true,
229 .probe = sandbox_fs_set_blk_dev,
230 .close = sandbox_fs_close,
232 .exists = sandbox_fs_exists,
233 .size = sandbox_fs_size,
234 .read = fs_read_sandbox,
235 .write = fs_write_sandbox,
236 .uuid = fs_uuid_unsupported,
237 .opendir = fs_opendir_unsupported,
238 .unlink = fs_unlink_unsupported,
239 .mkdir = fs_mkdir_unsupported,
240 .ln = fs_ln_unsupported,
243 #ifdef CONFIG_CMD_UBIFS
245 .fstype = FS_TYPE_UBIFS,
247 .null_dev_desc_ok = true,
248 .probe = ubifs_set_blk_dev,
249 .close = ubifs_close,
251 .exists = ubifs_exists,
254 .write = fs_write_unsupported,
255 .uuid = fs_uuid_unsupported,
256 .opendir = fs_opendir_unsupported,
257 .unlink = fs_unlink_unsupported,
258 .mkdir = fs_mkdir_unsupported,
259 .ln = fs_ln_unsupported,
262 #ifdef CONFIG_FS_BTRFS
264 .fstype = FS_TYPE_BTRFS,
266 .null_dev_desc_ok = false,
267 .probe = btrfs_probe,
268 .close = btrfs_close,
270 .exists = btrfs_exists,
273 .write = fs_write_unsupported,
275 .opendir = fs_opendir_unsupported,
276 .unlink = fs_unlink_unsupported,
277 .mkdir = fs_mkdir_unsupported,
278 .ln = fs_ln_unsupported,
281 #if IS_ENABLED(CONFIG_FS_SQUASHFS)
283 .fstype = FS_TYPE_SQUASHFS,
286 .opendir = sqfs_opendir,
287 .readdir = sqfs_readdir,
292 .closedir = sqfs_closedir,
296 .fstype = FS_TYPE_ANY,
297 .name = "unsupported",
298 .null_dev_desc_ok = true,
299 .probe = fs_probe_unsupported,
300 .close = fs_close_unsupported,
301 .ls = fs_ls_unsupported,
302 .exists = fs_exists_unsupported,
303 .size = fs_size_unsupported,
304 .read = fs_read_unsupported,
305 .write = fs_write_unsupported,
306 .uuid = fs_uuid_unsupported,
307 .opendir = fs_opendir_unsupported,
308 .unlink = fs_unlink_unsupported,
309 .mkdir = fs_mkdir_unsupported,
310 .ln = fs_ln_unsupported,
314 static struct fstype_info *fs_get_info(int fstype)
316 struct fstype_info *info;
319 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
320 if (fstype == info->fstype)
324 /* Return the 'unsupported' sentinel */
329 * fs_get_type() - Get type of current filesystem
331 * Return: filesystem type
333 * Returns filesystem type representing the current filesystem, or
334 * FS_TYPE_ANY for any unrecognised filesystem.
336 int fs_get_type(void)
342 * fs_get_type_name() - Get type of current filesystem
344 * Return: Pointer to filesystem name
346 * Returns a string describing the current filesystem, or the sentinel
347 * "unsupported" for any unrecognised filesystem.
349 const char *fs_get_type_name(void)
351 return fs_get_info(fs_type)->name;
354 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
356 struct fstype_info *info;
358 #ifdef CONFIG_NEEDS_MANUAL_RELOC
359 static int relocated;
362 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
364 info->name += gd->reloc_off;
365 info->probe += gd->reloc_off;
366 info->close += gd->reloc_off;
367 info->ls += gd->reloc_off;
368 info->read += gd->reloc_off;
369 info->write += gd->reloc_off;
375 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
380 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
381 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
382 fstype != info->fstype)
385 if (!fs_dev_desc && !info->null_dev_desc_ok)
388 if (!info->probe(fs_dev_desc, &fs_partition)) {
389 fs_type = info->fstype;
398 /* set current blk device w/ blk_desc + partition # */
399 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
401 struct fstype_info *info;
405 ret = part_get_info(desc, part, &fs_partition);
407 ret = part_get_info_whole_disk(desc, &fs_partition);
412 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
413 if (!info->probe(fs_dev_desc, &fs_partition)) {
414 fs_type = info->fstype;
425 struct fstype_info *info = fs_get_info(fs_type);
429 fs_type = FS_TYPE_ANY;
432 int fs_uuid(char *uuid_str)
434 struct fstype_info *info = fs_get_info(fs_type);
436 return info->uuid(uuid_str);
439 int fs_ls(const char *dirname)
443 struct fstype_info *info = fs_get_info(fs_type);
445 ret = info->ls(dirname);
452 int fs_exists(const char *filename)
456 struct fstype_info *info = fs_get_info(fs_type);
458 ret = info->exists(filename);
465 int fs_size(const char *filename, loff_t *size)
469 struct fstype_info *info = fs_get_info(fs_type);
471 ret = info->size(filename, size);
479 /* Check if a file may be read to the given address */
480 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
481 loff_t len, struct fstype_info *info)
488 /* get the actual size of the file */
489 ret = info->size(filename, &size);
492 if (offset >= size) {
493 /* offset >= EOF, no bytes will be written */
496 read_len = size - offset;
498 /* limit to 'len' if it is smaller */
499 if (len && len < read_len)
502 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
505 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
508 printf("** Reading file would overwrite reserved memory **\n");
513 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
514 int do_lmb_check, loff_t *actread)
516 struct fstype_info *info = fs_get_info(fs_type);
522 ret = fs_read_lmb_check(filename, addr, offset, len, info);
529 * We don't actually know how many bytes are being read, since len==0
530 * means read the whole file.
532 buf = map_sysmem(addr, len);
533 ret = info->read(filename, buf, offset, len, actread);
536 /* If we requested a specific number of bytes, check we got it */
537 if (ret == 0 && len && *actread != len)
538 debug("** %s shorter than offset + len **\n", filename);
544 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
547 return _fs_read(filename, addr, offset, len, 0, actread);
550 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
553 struct fstype_info *info = fs_get_info(fs_type);
557 buf = map_sysmem(addr, len);
558 ret = info->write(filename, buf, offset, len, actwrite);
561 if (ret < 0 && len != *actwrite) {
562 printf("** Unable to write file %s **\n", filename);
570 struct fs_dir_stream *fs_opendir(const char *filename)
572 struct fstype_info *info = fs_get_info(fs_type);
573 struct fs_dir_stream *dirs = NULL;
576 ret = info->opendir(filename, &dirs);
583 dirs->desc = fs_dev_desc;
584 dirs->part = fs_dev_part;
589 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
591 struct fstype_info *info;
592 struct fs_dirent *dirent;
595 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
596 info = fs_get_info(fs_type);
598 ret = info->readdir(dirs, &dirent);
608 void fs_closedir(struct fs_dir_stream *dirs)
610 struct fstype_info *info;
615 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
616 info = fs_get_info(fs_type);
618 info->closedir(dirs);
622 int fs_unlink(const char *filename)
626 struct fstype_info *info = fs_get_info(fs_type);
628 ret = info->unlink(filename);
635 int fs_mkdir(const char *dirname)
639 struct fstype_info *info = fs_get_info(fs_type);
641 ret = info->mkdir(dirname);
648 int fs_ln(const char *fname, const char *target)
650 struct fstype_info *info = fs_get_info(fs_type);
653 ret = info->ln(fname, target);
656 printf("** Unable to create link %s -> %s **\n", fname, target);
664 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
670 return CMD_RET_USAGE;
672 if (fs_set_blk_dev(argv[1], argv[2], fstype))
675 if (fs_size(argv[3], &size) < 0)
676 return CMD_RET_FAILURE;
678 env_set_hex("filesize", size);
683 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
687 const char *addr_str;
688 const char *filename;
697 return CMD_RET_USAGE;
699 return CMD_RET_USAGE;
701 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
705 addr = simple_strtoul(argv[3], &ep, 16);
706 if (ep == argv[3] || *ep != '\0')
707 return CMD_RET_USAGE;
709 addr_str = env_get("loadaddr");
710 if (addr_str != NULL)
711 addr = simple_strtoul(addr_str, NULL, 16);
713 addr = CONFIG_SYS_LOAD_ADDR;
718 filename = env_get("bootfile");
720 puts("** No boot file defined **\n");
725 bytes = simple_strtoul(argv[5], NULL, 16);
729 pos = simple_strtoul(argv[6], NULL, 16);
734 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
735 time = get_timer(time);
737 printf("Failed to load '%s'\n", filename);
741 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
742 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
743 (argc > 4) ? argv[4] : "");
745 printf("%llu bytes read in %lu ms", len_read, time);
748 print_size(div_u64(len_read, time) * 1000, "/s");
753 env_set_hex("fileaddr", addr);
754 env_set_hex("filesize", len_read);
759 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
763 return CMD_RET_USAGE;
765 return CMD_RET_USAGE;
767 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
770 if (fs_ls(argc >= 4 ? argv[3] : "/"))
776 int file_exists(const char *dev_type, const char *dev_part, const char *file,
779 if (fs_set_blk_dev(dev_type, dev_part, fstype))
782 return fs_exists(file);
785 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
789 const char *filename;
796 if (argc < 6 || argc > 7)
797 return CMD_RET_USAGE;
799 if (fs_set_blk_dev(argv[1], argv[2], fstype))
802 addr = simple_strtoul(argv[3], NULL, 16);
804 bytes = simple_strtoul(argv[5], NULL, 16);
806 pos = simple_strtoul(argv[6], NULL, 16);
811 ret = fs_write(filename, addr, pos, bytes, &len);
812 time = get_timer(time);
816 printf("%llu bytes written in %lu ms", len, time);
819 print_size(div_u64(len, time) * 1000, "/s");
827 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
832 memset(uuid, 0, sizeof(uuid));
834 if (argc < 3 || argc > 4)
835 return CMD_RET_USAGE;
837 if (fs_set_blk_dev(argv[1], argv[2], fstype))
842 return CMD_RET_FAILURE;
845 env_set(argv[3], uuid);
847 printf("%s\n", uuid);
849 return CMD_RET_SUCCESS;
852 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
854 struct fstype_info *info;
856 if (argc < 3 || argc > 4)
857 return CMD_RET_USAGE;
859 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
862 info = fs_get_info(fs_type);
865 env_set(argv[3], info->name);
867 printf("%s\n", info->name);
871 return CMD_RET_SUCCESS;
874 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
878 return CMD_RET_USAGE;
880 if (fs_set_blk_dev(argv[1], argv[2], fstype))
883 if (fs_unlink(argv[3]))
889 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
895 return CMD_RET_USAGE;
897 if (fs_set_blk_dev(argv[1], argv[2], fstype))
900 ret = fs_mkdir(argv[3]);
902 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
909 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
913 return CMD_RET_USAGE;
915 if (fs_set_blk_dev(argv[1], argv[2], fstype))
918 if (fs_ln(argv[3], argv[4]))
924 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
926 struct fstype_info *drv = fstypes;
927 const int n_ents = ARRAY_SIZE(fstypes);
928 struct fstype_info *entry;
931 puts("Supported filesystems");
932 for (entry = drv; entry != drv + n_ents; entry++) {
933 if (entry->fstype != FS_TYPE_ANY) {
934 printf("%c %s", i ? ',' : ':', entry->name);
941 return CMD_RET_SUCCESS;