1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
17 #include <sandboxfs.h>
18 #include <ubifs_uboot.h>
22 #include <linux/math64.h>
23 #include <efi_loader.h>
25 DECLARE_GLOBAL_DATA_PTR;
27 static struct blk_desc *fs_dev_desc;
28 static int fs_dev_part;
29 static struct disk_partition fs_partition;
30 static int fs_type = FS_TYPE_ANY;
32 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
33 struct disk_partition *fs_partition)
35 printf("** Unrecognized filesystem type **\n");
39 static inline int fs_ls_unsupported(const char *dirname)
44 /* generic implementation of ls in terms of opendir/readdir/closedir */
46 static int fs_ls_generic(const char *dirname)
48 struct fs_dir_stream *dirs;
49 struct fs_dirent *dent;
50 int nfiles = 0, ndirs = 0;
52 dirs = fs_opendir(dirname);
56 while ((dent = fs_readdir(dirs))) {
57 if (dent->type == FS_DT_DIR) {
58 printf(" %s/\n", dent->name);
61 printf(" %8lld %s\n", dent->size, dent->name);
68 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
73 static inline int fs_exists_unsupported(const char *filename)
78 static inline int fs_size_unsupported(const char *filename, loff_t *size)
83 static inline int fs_read_unsupported(const char *filename, void *buf,
84 loff_t offset, loff_t len,
90 static inline int fs_write_unsupported(const char *filename, void *buf,
91 loff_t offset, loff_t len,
97 static inline int fs_ln_unsupported(const char *filename, const char *target)
102 static inline void fs_close_unsupported(void)
106 static inline int fs_uuid_unsupported(char *uuid_str)
111 static inline int fs_opendir_unsupported(const char *filename,
112 struct fs_dir_stream **dirs)
117 static inline int fs_unlink_unsupported(const char *filename)
122 static inline int fs_mkdir_unsupported(const char *dirname)
131 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
132 * should be false in most cases. For "virtual" filesystems which
133 * aren't based on a U-Boot block device (e.g. sandbox), this can be
134 * set to true. This should also be true for the dummy entry at the end
135 * of fstypes[], since that is essentially a "virtual" (non-existent)
138 bool null_dev_desc_ok;
139 int (*probe)(struct blk_desc *fs_dev_desc,
140 struct disk_partition *fs_partition);
141 int (*ls)(const char *dirname);
142 int (*exists)(const char *filename);
143 int (*size)(const char *filename, loff_t *size);
144 int (*read)(const char *filename, void *buf, loff_t offset,
145 loff_t len, loff_t *actread);
146 int (*write)(const char *filename, void *buf, loff_t offset,
147 loff_t len, loff_t *actwrite);
149 int (*uuid)(char *uuid_str);
151 * Open a directory stream. On success return 0 and directory
152 * stream pointer via 'dirsp'. On error, return -errno. See
155 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
157 * Read next entry from directory stream. On success return 0
158 * and directory entry pointer via 'dentp'. On error return
159 * -errno. See fs_readdir().
161 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
162 /* see fs_closedir() */
163 void (*closedir)(struct fs_dir_stream *dirs);
164 int (*unlink)(const char *filename);
165 int (*mkdir)(const char *dirname);
166 int (*ln)(const char *filename, const char *target);
169 static struct fstype_info fstypes[] = {
172 .fstype = FS_TYPE_FAT,
174 .null_dev_desc_ok = false,
175 .probe = fat_set_blk_dev,
178 .exists = fat_exists,
180 .read = fat_read_file,
181 #if CONFIG_IS_ENABLED(FAT_WRITE)
182 .write = file_fat_write,
183 .unlink = fat_unlink,
186 .write = fs_write_unsupported,
187 .unlink = fs_unlink_unsupported,
188 .mkdir = fs_mkdir_unsupported,
190 .uuid = fs_uuid_unsupported,
191 .opendir = fat_opendir,
192 .readdir = fat_readdir,
193 .closedir = fat_closedir,
194 .ln = fs_ln_unsupported,
198 #if CONFIG_IS_ENABLED(FS_EXT4)
200 .fstype = FS_TYPE_EXT,
202 .null_dev_desc_ok = false,
203 .probe = ext4fs_probe,
204 .close = ext4fs_close,
206 .exists = ext4fs_exists,
208 .read = ext4_read_file,
209 #ifdef CONFIG_CMD_EXT4_WRITE
210 .write = ext4_write_file,
211 .ln = ext4fs_create_link,
213 .write = fs_write_unsupported,
214 .ln = fs_ln_unsupported,
217 .opendir = fs_opendir_unsupported,
218 .unlink = fs_unlink_unsupported,
219 .mkdir = fs_mkdir_unsupported,
222 #ifdef CONFIG_SANDBOX
224 .fstype = FS_TYPE_SANDBOX,
226 .null_dev_desc_ok = true,
227 .probe = sandbox_fs_set_blk_dev,
228 .close = sandbox_fs_close,
230 .exists = sandbox_fs_exists,
231 .size = sandbox_fs_size,
232 .read = fs_read_sandbox,
233 .write = fs_write_sandbox,
234 .uuid = fs_uuid_unsupported,
235 .opendir = fs_opendir_unsupported,
236 .unlink = fs_unlink_unsupported,
237 .mkdir = fs_mkdir_unsupported,
238 .ln = fs_ln_unsupported,
241 #ifdef CONFIG_CMD_UBIFS
243 .fstype = FS_TYPE_UBIFS,
245 .null_dev_desc_ok = true,
246 .probe = ubifs_set_blk_dev,
247 .close = ubifs_close,
249 .exists = ubifs_exists,
252 .write = fs_write_unsupported,
253 .uuid = fs_uuid_unsupported,
254 .opendir = fs_opendir_unsupported,
255 .unlink = fs_unlink_unsupported,
256 .mkdir = fs_mkdir_unsupported,
257 .ln = fs_ln_unsupported,
260 #ifdef CONFIG_FS_BTRFS
262 .fstype = FS_TYPE_BTRFS,
264 .null_dev_desc_ok = false,
265 .probe = btrfs_probe,
266 .close = btrfs_close,
268 .exists = btrfs_exists,
271 .write = fs_write_unsupported,
273 .opendir = fs_opendir_unsupported,
274 .unlink = fs_unlink_unsupported,
275 .mkdir = fs_mkdir_unsupported,
276 .ln = fs_ln_unsupported,
280 .fstype = FS_TYPE_ANY,
281 .name = "unsupported",
282 .null_dev_desc_ok = true,
283 .probe = fs_probe_unsupported,
284 .close = fs_close_unsupported,
285 .ls = fs_ls_unsupported,
286 .exists = fs_exists_unsupported,
287 .size = fs_size_unsupported,
288 .read = fs_read_unsupported,
289 .write = fs_write_unsupported,
290 .uuid = fs_uuid_unsupported,
291 .opendir = fs_opendir_unsupported,
292 .unlink = fs_unlink_unsupported,
293 .mkdir = fs_mkdir_unsupported,
294 .ln = fs_ln_unsupported,
298 static struct fstype_info *fs_get_info(int fstype)
300 struct fstype_info *info;
303 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
304 if (fstype == info->fstype)
308 /* Return the 'unsupported' sentinel */
313 * fs_get_type() - Get type of current filesystem
315 * Return: filesystem type
317 * Returns filesystem type representing the current filesystem, or
318 * FS_TYPE_ANY for any unrecognised filesystem.
320 int fs_get_type(void)
326 * fs_get_type_name() - Get type of current filesystem
328 * Return: Pointer to filesystem name
330 * Returns a string describing the current filesystem, or the sentinel
331 * "unsupported" for any unrecognised filesystem.
333 const char *fs_get_type_name(void)
335 return fs_get_info(fs_type)->name;
338 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
340 struct fstype_info *info;
342 #ifdef CONFIG_NEEDS_MANUAL_RELOC
343 static int relocated;
346 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
348 info->name += gd->reloc_off;
349 info->probe += gd->reloc_off;
350 info->close += gd->reloc_off;
351 info->ls += gd->reloc_off;
352 info->read += gd->reloc_off;
353 info->write += gd->reloc_off;
359 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
364 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
365 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
366 fstype != info->fstype)
369 if (!fs_dev_desc && !info->null_dev_desc_ok)
372 if (!info->probe(fs_dev_desc, &fs_partition)) {
373 fs_type = info->fstype;
382 /* set current blk device w/ blk_desc + partition # */
383 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
385 struct fstype_info *info;
389 ret = part_get_info(desc, part, &fs_partition);
391 ret = part_get_info_whole_disk(desc, &fs_partition);
396 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
397 if (!info->probe(fs_dev_desc, &fs_partition)) {
398 fs_type = info->fstype;
409 struct fstype_info *info = fs_get_info(fs_type);
413 fs_type = FS_TYPE_ANY;
416 int fs_uuid(char *uuid_str)
418 struct fstype_info *info = fs_get_info(fs_type);
420 return info->uuid(uuid_str);
423 int fs_ls(const char *dirname)
427 struct fstype_info *info = fs_get_info(fs_type);
429 ret = info->ls(dirname);
436 int fs_exists(const char *filename)
440 struct fstype_info *info = fs_get_info(fs_type);
442 ret = info->exists(filename);
449 int fs_size(const char *filename, loff_t *size)
453 struct fstype_info *info = fs_get_info(fs_type);
455 ret = info->size(filename, size);
463 /* Check if a file may be read to the given address */
464 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
465 loff_t len, struct fstype_info *info)
472 /* get the actual size of the file */
473 ret = info->size(filename, &size);
476 if (offset >= size) {
477 /* offset >= EOF, no bytes will be written */
480 read_len = size - offset;
482 /* limit to 'len' if it is smaller */
483 if (len && len < read_len)
486 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
489 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
492 printf("** Reading file would overwrite reserved memory **\n");
497 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
498 int do_lmb_check, loff_t *actread)
500 struct fstype_info *info = fs_get_info(fs_type);
506 ret = fs_read_lmb_check(filename, addr, offset, len, info);
513 * We don't actually know how many bytes are being read, since len==0
514 * means read the whole file.
516 buf = map_sysmem(addr, len);
517 ret = info->read(filename, buf, offset, len, actread);
520 /* If we requested a specific number of bytes, check we got it */
521 if (ret == 0 && len && *actread != len)
522 debug("** %s shorter than offset + len **\n", filename);
528 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
531 return _fs_read(filename, addr, offset, len, 0, actread);
534 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
537 struct fstype_info *info = fs_get_info(fs_type);
541 buf = map_sysmem(addr, len);
542 ret = info->write(filename, buf, offset, len, actwrite);
545 if (ret < 0 && len != *actwrite) {
546 printf("** Unable to write file %s **\n", filename);
554 struct fs_dir_stream *fs_opendir(const char *filename)
556 struct fstype_info *info = fs_get_info(fs_type);
557 struct fs_dir_stream *dirs = NULL;
560 ret = info->opendir(filename, &dirs);
567 dirs->desc = fs_dev_desc;
568 dirs->part = fs_dev_part;
573 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
575 struct fstype_info *info;
576 struct fs_dirent *dirent;
579 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
580 info = fs_get_info(fs_type);
582 ret = info->readdir(dirs, &dirent);
592 void fs_closedir(struct fs_dir_stream *dirs)
594 struct fstype_info *info;
599 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
600 info = fs_get_info(fs_type);
602 info->closedir(dirs);
606 int fs_unlink(const char *filename)
610 struct fstype_info *info = fs_get_info(fs_type);
612 ret = info->unlink(filename);
619 int fs_mkdir(const char *dirname)
623 struct fstype_info *info = fs_get_info(fs_type);
625 ret = info->mkdir(dirname);
632 int fs_ln(const char *fname, const char *target)
634 struct fstype_info *info = fs_get_info(fs_type);
637 ret = info->ln(fname, target);
640 printf("** Unable to create link %s -> %s **\n", fname, target);
648 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
654 return CMD_RET_USAGE;
656 if (fs_set_blk_dev(argv[1], argv[2], fstype))
659 if (fs_size(argv[3], &size) < 0)
660 return CMD_RET_FAILURE;
662 env_set_hex("filesize", size);
667 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
671 const char *addr_str;
672 const char *filename;
681 return CMD_RET_USAGE;
683 return CMD_RET_USAGE;
685 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
689 addr = simple_strtoul(argv[3], &ep, 16);
690 if (ep == argv[3] || *ep != '\0')
691 return CMD_RET_USAGE;
693 addr_str = env_get("loadaddr");
694 if (addr_str != NULL)
695 addr = simple_strtoul(addr_str, NULL, 16);
697 addr = CONFIG_SYS_LOAD_ADDR;
702 filename = env_get("bootfile");
704 puts("** No boot file defined **\n");
709 bytes = simple_strtoul(argv[5], NULL, 16);
713 pos = simple_strtoul(argv[6], NULL, 16);
717 #ifdef CONFIG_CMD_BOOTEFI
718 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
719 (argc > 4) ? argv[4] : "");
722 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
723 time = get_timer(time);
727 printf("%llu bytes read in %lu ms", len_read, time);
730 print_size(div_u64(len_read, time) * 1000, "/s");
735 env_set_hex("fileaddr", addr);
736 env_set_hex("filesize", len_read);
741 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
745 return CMD_RET_USAGE;
747 return CMD_RET_USAGE;
749 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
752 if (fs_ls(argc >= 4 ? argv[3] : "/"))
758 int file_exists(const char *dev_type, const char *dev_part, const char *file,
761 if (fs_set_blk_dev(dev_type, dev_part, fstype))
764 return fs_exists(file);
767 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
771 const char *filename;
778 if (argc < 6 || argc > 7)
779 return CMD_RET_USAGE;
781 if (fs_set_blk_dev(argv[1], argv[2], fstype))
784 addr = simple_strtoul(argv[3], NULL, 16);
786 bytes = simple_strtoul(argv[5], NULL, 16);
788 pos = simple_strtoul(argv[6], NULL, 16);
793 ret = fs_write(filename, addr, pos, bytes, &len);
794 time = get_timer(time);
798 printf("%llu bytes written in %lu ms", len, time);
801 print_size(div_u64(len, time) * 1000, "/s");
809 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
814 memset(uuid, 0, sizeof(uuid));
816 if (argc < 3 || argc > 4)
817 return CMD_RET_USAGE;
819 if (fs_set_blk_dev(argv[1], argv[2], fstype))
824 return CMD_RET_FAILURE;
827 env_set(argv[3], uuid);
829 printf("%s\n", uuid);
831 return CMD_RET_SUCCESS;
834 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
836 struct fstype_info *info;
838 if (argc < 3 || argc > 4)
839 return CMD_RET_USAGE;
841 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
844 info = fs_get_info(fs_type);
847 env_set(argv[3], info->name);
849 printf("%s\n", info->name);
853 return CMD_RET_SUCCESS;
856 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
860 return CMD_RET_USAGE;
862 if (fs_set_blk_dev(argv[1], argv[2], fstype))
865 if (fs_unlink(argv[3]))
871 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
877 return CMD_RET_USAGE;
879 if (fs_set_blk_dev(argv[1], argv[2], fstype))
882 ret = fs_mkdir(argv[3]);
884 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
891 int do_ln(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 if (fs_ln(argv[3], argv[4]))