1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
15 #include <sandboxfs.h>
16 #include <ubifs_uboot.h>
20 #include <linux/math64.h>
21 #include <efi_loader.h>
23 DECLARE_GLOBAL_DATA_PTR;
25 static struct blk_desc *fs_dev_desc;
26 static int fs_dev_part;
27 static disk_partition_t fs_partition;
28 static int fs_type = FS_TYPE_ANY;
30 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
31 disk_partition_t *fs_partition)
33 printf("** Unrecognized filesystem type **\n");
37 static inline int fs_ls_unsupported(const char *dirname)
42 /* generic implementation of ls in terms of opendir/readdir/closedir */
44 static int fs_ls_generic(const char *dirname)
46 struct fs_dir_stream *dirs;
47 struct fs_dirent *dent;
48 int nfiles = 0, ndirs = 0;
50 dirs = fs_opendir(dirname);
54 while ((dent = fs_readdir(dirs))) {
55 if (dent->type == FS_DT_DIR) {
56 printf(" %s/\n", dent->name);
59 printf(" %8lld %s\n", dent->size, dent->name);
66 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
71 static inline int fs_exists_unsupported(const char *filename)
76 static inline int fs_size_unsupported(const char *filename, loff_t *size)
81 static inline int fs_read_unsupported(const char *filename, void *buf,
82 loff_t offset, loff_t len,
88 static inline int fs_write_unsupported(const char *filename, void *buf,
89 loff_t offset, loff_t len,
95 static inline int fs_ln_unsupported(const char *filename, const char *target)
100 static inline void fs_close_unsupported(void)
104 static inline int fs_uuid_unsupported(char *uuid_str)
109 static inline int fs_opendir_unsupported(const char *filename,
110 struct fs_dir_stream **dirs)
115 static inline int fs_unlink_unsupported(const char *filename)
120 static inline int fs_mkdir_unsupported(const char *dirname)
129 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
130 * should be false in most cases. For "virtual" filesystems which
131 * aren't based on a U-Boot block device (e.g. sandbox), this can be
132 * set to true. This should also be true for the dummy entry at the end
133 * of fstypes[], since that is essentially a "virtual" (non-existent)
136 bool null_dev_desc_ok;
137 int (*probe)(struct blk_desc *fs_dev_desc,
138 disk_partition_t *fs_partition);
139 int (*ls)(const char *dirname);
140 int (*exists)(const char *filename);
141 int (*size)(const char *filename, loff_t *size);
142 int (*read)(const char *filename, void *buf, loff_t offset,
143 loff_t len, loff_t *actread);
144 int (*write)(const char *filename, void *buf, loff_t offset,
145 loff_t len, loff_t *actwrite);
147 int (*uuid)(char *uuid_str);
149 * Open a directory stream. On success return 0 and directory
150 * stream pointer via 'dirsp'. On error, return -errno. See
153 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
155 * Read next entry from directory stream. On success return 0
156 * and directory entry pointer via 'dentp'. On error return
157 * -errno. See fs_readdir().
159 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
160 /* see fs_closedir() */
161 void (*closedir)(struct fs_dir_stream *dirs);
162 int (*unlink)(const char *filename);
163 int (*mkdir)(const char *dirname);
164 int (*ln)(const char *filename, const char *target);
167 static struct fstype_info fstypes[] = {
170 .fstype = FS_TYPE_FAT,
172 .null_dev_desc_ok = false,
173 .probe = fat_set_blk_dev,
176 .exists = fat_exists,
178 .read = fat_read_file,
179 #if CONFIG_IS_ENABLED(FAT_WRITE)
180 .write = file_fat_write,
181 .unlink = fat_unlink,
184 .write = fs_write_unsupported,
185 .unlink = fs_unlink_unsupported,
186 .mkdir = fs_mkdir_unsupported,
188 .uuid = fs_uuid_unsupported,
189 .opendir = fat_opendir,
190 .readdir = fat_readdir,
191 .closedir = fat_closedir,
192 .ln = fs_ln_unsupported,
196 #if CONFIG_IS_ENABLED(FS_EXT4)
198 .fstype = FS_TYPE_EXT,
200 .null_dev_desc_ok = false,
201 .probe = ext4fs_probe,
202 .close = ext4fs_close,
204 .exists = ext4fs_exists,
206 .read = ext4_read_file,
207 #ifdef CONFIG_CMD_EXT4_WRITE
208 .write = ext4_write_file,
209 .ln = ext4fs_create_link,
211 .write = fs_write_unsupported,
212 .ln = fs_ln_unsupported,
215 .opendir = fs_opendir_unsupported,
216 .unlink = fs_unlink_unsupported,
217 .mkdir = fs_mkdir_unsupported,
220 #ifdef CONFIG_SANDBOX
222 .fstype = FS_TYPE_SANDBOX,
224 .null_dev_desc_ok = true,
225 .probe = sandbox_fs_set_blk_dev,
226 .close = sandbox_fs_close,
228 .exists = sandbox_fs_exists,
229 .size = sandbox_fs_size,
230 .read = fs_read_sandbox,
231 .write = fs_write_sandbox,
232 .uuid = fs_uuid_unsupported,
233 .opendir = fs_opendir_unsupported,
234 .unlink = fs_unlink_unsupported,
235 .mkdir = fs_mkdir_unsupported,
236 .ln = fs_ln_unsupported,
239 #ifdef CONFIG_CMD_UBIFS
241 .fstype = FS_TYPE_UBIFS,
243 .null_dev_desc_ok = true,
244 .probe = ubifs_set_blk_dev,
245 .close = ubifs_close,
247 .exists = ubifs_exists,
250 .write = fs_write_unsupported,
251 .uuid = fs_uuid_unsupported,
252 .opendir = fs_opendir_unsupported,
253 .unlink = fs_unlink_unsupported,
254 .mkdir = fs_mkdir_unsupported,
255 .ln = fs_ln_unsupported,
258 #ifdef CONFIG_FS_BTRFS
260 .fstype = FS_TYPE_BTRFS,
262 .null_dev_desc_ok = false,
263 .probe = btrfs_probe,
264 .close = btrfs_close,
266 .exists = btrfs_exists,
269 .write = fs_write_unsupported,
271 .opendir = fs_opendir_unsupported,
272 .unlink = fs_unlink_unsupported,
273 .mkdir = fs_mkdir_unsupported,
274 .ln = fs_ln_unsupported,
278 .fstype = FS_TYPE_ANY,
279 .name = "unsupported",
280 .null_dev_desc_ok = true,
281 .probe = fs_probe_unsupported,
282 .close = fs_close_unsupported,
283 .ls = fs_ls_unsupported,
284 .exists = fs_exists_unsupported,
285 .size = fs_size_unsupported,
286 .read = fs_read_unsupported,
287 .write = fs_write_unsupported,
288 .uuid = fs_uuid_unsupported,
289 .opendir = fs_opendir_unsupported,
290 .unlink = fs_unlink_unsupported,
291 .mkdir = fs_mkdir_unsupported,
292 .ln = fs_ln_unsupported,
296 static struct fstype_info *fs_get_info(int fstype)
298 struct fstype_info *info;
301 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
302 if (fstype == info->fstype)
306 /* Return the 'unsupported' sentinel */
311 * fs_get_type_name() - Get type of current filesystem
313 * Return: Pointer to filesystem name
315 * Returns a string describing the current filesystem, or the sentinel
316 * "unsupported" for any unrecognised filesystem.
318 const char *fs_get_type_name(void)
320 return fs_get_info(fs_type)->name;
323 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
325 struct fstype_info *info;
327 #ifdef CONFIG_NEEDS_MANUAL_RELOC
328 static int relocated;
331 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
333 info->name += gd->reloc_off;
334 info->probe += gd->reloc_off;
335 info->close += gd->reloc_off;
336 info->ls += gd->reloc_off;
337 info->read += gd->reloc_off;
338 info->write += gd->reloc_off;
344 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
349 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
350 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
351 fstype != info->fstype)
354 if (!fs_dev_desc && !info->null_dev_desc_ok)
357 if (!info->probe(fs_dev_desc, &fs_partition)) {
358 fs_type = info->fstype;
367 /* set current blk device w/ blk_desc + partition # */
368 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
370 struct fstype_info *info;
374 ret = part_get_info(desc, part, &fs_partition);
376 ret = part_get_info_whole_disk(desc, &fs_partition);
381 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
382 if (!info->probe(fs_dev_desc, &fs_partition)) {
383 fs_type = info->fstype;
394 struct fstype_info *info = fs_get_info(fs_type);
398 fs_type = FS_TYPE_ANY;
401 int fs_uuid(char *uuid_str)
403 struct fstype_info *info = fs_get_info(fs_type);
405 return info->uuid(uuid_str);
408 int fs_ls(const char *dirname)
412 struct fstype_info *info = fs_get_info(fs_type);
414 ret = info->ls(dirname);
421 int fs_exists(const char *filename)
425 struct fstype_info *info = fs_get_info(fs_type);
427 ret = info->exists(filename);
434 int fs_size(const char *filename, loff_t *size)
438 struct fstype_info *info = fs_get_info(fs_type);
440 ret = info->size(filename, size);
448 /* Check if a file may be read to the given address */
449 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
450 loff_t len, struct fstype_info *info)
457 /* get the actual size of the file */
458 ret = info->size(filename, &size);
461 if (offset >= size) {
462 /* offset >= EOF, no bytes will be written */
465 read_len = size - offset;
467 /* limit to 'len' if it is smaller */
468 if (len && len < read_len)
471 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
474 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
477 printf("** Reading file would overwrite reserved memory **\n");
482 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
483 int do_lmb_check, loff_t *actread)
485 struct fstype_info *info = fs_get_info(fs_type);
491 ret = fs_read_lmb_check(filename, addr, offset, len, info);
498 * We don't actually know how many bytes are being read, since len==0
499 * means read the whole file.
501 buf = map_sysmem(addr, len);
502 ret = info->read(filename, buf, offset, len, actread);
505 /* If we requested a specific number of bytes, check we got it */
506 if (ret == 0 && len && *actread != len)
507 debug("** %s shorter than offset + len **\n", filename);
513 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
516 return _fs_read(filename, addr, offset, len, 0, actread);
519 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
522 struct fstype_info *info = fs_get_info(fs_type);
526 buf = map_sysmem(addr, len);
527 ret = info->write(filename, buf, offset, len, actwrite);
530 if (ret < 0 && len != *actwrite) {
531 printf("** Unable to write file %s **\n", filename);
539 struct fs_dir_stream *fs_opendir(const char *filename)
541 struct fstype_info *info = fs_get_info(fs_type);
542 struct fs_dir_stream *dirs = NULL;
545 ret = info->opendir(filename, &dirs);
552 dirs->desc = fs_dev_desc;
553 dirs->part = fs_dev_part;
558 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
560 struct fstype_info *info;
561 struct fs_dirent *dirent;
564 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
565 info = fs_get_info(fs_type);
567 ret = info->readdir(dirs, &dirent);
577 void fs_closedir(struct fs_dir_stream *dirs)
579 struct fstype_info *info;
584 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
585 info = fs_get_info(fs_type);
587 info->closedir(dirs);
591 int fs_unlink(const char *filename)
595 struct fstype_info *info = fs_get_info(fs_type);
597 ret = info->unlink(filename);
604 int fs_mkdir(const char *dirname)
608 struct fstype_info *info = fs_get_info(fs_type);
610 ret = info->mkdir(dirname);
617 int fs_ln(const char *fname, const char *target)
619 struct fstype_info *info = fs_get_info(fs_type);
622 ret = info->ln(fname, target);
625 printf("** Unable to create link %s -> %s **\n", fname, target);
633 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
639 return CMD_RET_USAGE;
641 if (fs_set_blk_dev(argv[1], argv[2], fstype))
644 if (fs_size(argv[3], &size) < 0)
645 return CMD_RET_FAILURE;
647 env_set_hex("filesize", size);
652 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
656 const char *addr_str;
657 const char *filename;
666 return CMD_RET_USAGE;
668 return CMD_RET_USAGE;
670 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
674 addr = simple_strtoul(argv[3], &ep, 16);
675 if (ep == argv[3] || *ep != '\0')
676 return CMD_RET_USAGE;
678 addr_str = env_get("loadaddr");
679 if (addr_str != NULL)
680 addr = simple_strtoul(addr_str, NULL, 16);
682 addr = CONFIG_SYS_LOAD_ADDR;
687 filename = env_get("bootfile");
689 puts("** No boot file defined **\n");
694 bytes = simple_strtoul(argv[5], NULL, 16);
698 pos = simple_strtoul(argv[6], NULL, 16);
702 #ifdef CONFIG_CMD_BOOTEFI
703 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
704 (argc > 4) ? argv[4] : "");
707 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
708 time = get_timer(time);
712 printf("%llu bytes read in %lu ms", len_read, time);
715 print_size(div_u64(len_read, time) * 1000, "/s");
720 env_set_hex("fileaddr", addr);
721 env_set_hex("filesize", len_read);
726 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
730 return CMD_RET_USAGE;
732 return CMD_RET_USAGE;
734 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
737 if (fs_ls(argc >= 4 ? argv[3] : "/"))
743 int file_exists(const char *dev_type, const char *dev_part, const char *file,
746 if (fs_set_blk_dev(dev_type, dev_part, fstype))
749 return fs_exists(file);
752 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
756 const char *filename;
763 if (argc < 6 || argc > 7)
764 return CMD_RET_USAGE;
766 if (fs_set_blk_dev(argv[1], argv[2], fstype))
769 addr = simple_strtoul(argv[3], NULL, 16);
771 bytes = simple_strtoul(argv[5], NULL, 16);
773 pos = simple_strtoul(argv[6], NULL, 16);
778 ret = fs_write(filename, addr, pos, bytes, &len);
779 time = get_timer(time);
783 printf("%llu bytes written in %lu ms", len, time);
786 print_size(div_u64(len, time) * 1000, "/s");
794 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
799 memset(uuid, 0, sizeof(uuid));
801 if (argc < 3 || argc > 4)
802 return CMD_RET_USAGE;
804 if (fs_set_blk_dev(argv[1], argv[2], fstype))
809 return CMD_RET_FAILURE;
812 env_set(argv[3], uuid);
814 printf("%s\n", uuid);
816 return CMD_RET_SUCCESS;
819 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
821 struct fstype_info *info;
823 if (argc < 3 || argc > 4)
824 return CMD_RET_USAGE;
826 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
829 info = fs_get_info(fs_type);
832 env_set(argv[3], info->name);
834 printf("%s\n", info->name);
838 return CMD_RET_SUCCESS;
841 int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
845 return CMD_RET_USAGE;
847 if (fs_set_blk_dev(argv[1], argv[2], fstype))
850 if (fs_unlink(argv[3]))
856 int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
862 return CMD_RET_USAGE;
864 if (fs_set_blk_dev(argv[1], argv[2], fstype))
867 ret = fs_mkdir(argv[3]);
869 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
876 int do_ln(cmd_tbl_t *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 if (fs_ln(argv[3], argv[4]))