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 <ubifs_uboot.h>
23 #include <asm/global_data.h>
26 #include <linux/math64.h>
27 #include <efi_loader.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 static struct blk_desc *fs_dev_desc;
33 static int fs_dev_part;
34 static struct disk_partition fs_partition;
35 static int fs_type = FS_TYPE_ANY;
37 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
38 struct disk_partition *fs_partition)
40 log_err("** Unrecognized filesystem type **\n");
44 static inline int fs_ls_unsupported(const char *dirname)
49 /* generic implementation of ls in terms of opendir/readdir/closedir */
51 static int fs_ls_generic(const char *dirname)
53 struct fs_dir_stream *dirs;
54 struct fs_dirent *dent;
55 int nfiles = 0, ndirs = 0;
57 dirs = fs_opendir(dirname);
61 while ((dent = fs_readdir(dirs))) {
62 if (dent->type == FS_DT_DIR) {
63 printf(" %s/\n", dent->name);
65 } else if (dent->type == FS_DT_LNK) {
66 printf(" <SYM> %s\n", dent->name);
69 printf(" %8lld %s\n", dent->size, dent->name);
76 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
81 static inline int fs_exists_unsupported(const char *filename)
86 static inline int fs_size_unsupported(const char *filename, loff_t *size)
91 static inline int fs_read_unsupported(const char *filename, void *buf,
92 loff_t offset, loff_t len,
98 static inline int fs_write_unsupported(const char *filename, void *buf,
99 loff_t offset, loff_t len,
105 static inline int fs_ln_unsupported(const char *filename, const char *target)
110 static inline void fs_close_unsupported(void)
114 static inline int fs_uuid_unsupported(char *uuid_str)
119 static inline int fs_opendir_unsupported(const char *filename,
120 struct fs_dir_stream **dirs)
125 static inline int fs_unlink_unsupported(const char *filename)
130 static inline int fs_mkdir_unsupported(const char *dirname)
139 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
140 * should be false in most cases. For "virtual" filesystems which
141 * aren't based on a U-Boot block device (e.g. sandbox), this can be
142 * set to true. This should also be true for the dummy entry at the end
143 * of fstypes[], since that is essentially a "virtual" (non-existent)
146 bool null_dev_desc_ok;
147 int (*probe)(struct blk_desc *fs_dev_desc,
148 struct disk_partition *fs_partition);
149 int (*ls)(const char *dirname);
150 int (*exists)(const char *filename);
151 int (*size)(const char *filename, loff_t *size);
152 int (*read)(const char *filename, void *buf, loff_t offset,
153 loff_t len, loff_t *actread);
154 int (*write)(const char *filename, void *buf, loff_t offset,
155 loff_t len, loff_t *actwrite);
157 int (*uuid)(char *uuid_str);
159 * Open a directory stream. On success return 0 and directory
160 * stream pointer via 'dirsp'. On error, return -errno. See
163 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
165 * Read next entry from directory stream. On success return 0
166 * and directory entry pointer via 'dentp'. On error return
167 * -errno. See fs_readdir().
169 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
170 /* see fs_closedir() */
171 void (*closedir)(struct fs_dir_stream *dirs);
172 int (*unlink)(const char *filename);
173 int (*mkdir)(const char *dirname);
174 int (*ln)(const char *filename, const char *target);
177 static struct fstype_info fstypes[] = {
180 .fstype = FS_TYPE_FAT,
182 .null_dev_desc_ok = false,
183 .probe = fat_set_blk_dev,
186 .exists = fat_exists,
188 .read = fat_read_file,
189 #if CONFIG_IS_ENABLED(FAT_WRITE)
190 .write = file_fat_write,
191 .unlink = fat_unlink,
194 .write = fs_write_unsupported,
195 .unlink = fs_unlink_unsupported,
196 .mkdir = fs_mkdir_unsupported,
199 .opendir = fat_opendir,
200 .readdir = fat_readdir,
201 .closedir = fat_closedir,
202 .ln = fs_ln_unsupported,
206 #if CONFIG_IS_ENABLED(FS_EXT4)
208 .fstype = FS_TYPE_EXT,
210 .null_dev_desc_ok = false,
211 .probe = ext4fs_probe,
212 .close = ext4fs_close,
214 .exists = ext4fs_exists,
216 .read = ext4_read_file,
217 #ifdef CONFIG_CMD_EXT4_WRITE
218 .write = ext4_write_file,
219 .ln = ext4fs_create_link,
221 .write = fs_write_unsupported,
222 .ln = fs_ln_unsupported,
225 .opendir = fs_opendir_unsupported,
226 .unlink = fs_unlink_unsupported,
227 .mkdir = fs_mkdir_unsupported,
230 #ifdef CONFIG_SANDBOX
232 .fstype = FS_TYPE_SANDBOX,
234 .null_dev_desc_ok = true,
235 .probe = sandbox_fs_set_blk_dev,
236 .close = sandbox_fs_close,
238 .exists = sandbox_fs_exists,
239 .size = sandbox_fs_size,
240 .read = fs_read_sandbox,
241 .write = fs_write_sandbox,
242 .uuid = fs_uuid_unsupported,
243 .opendir = fs_opendir_unsupported,
244 .unlink = fs_unlink_unsupported,
245 .mkdir = fs_mkdir_unsupported,
246 .ln = fs_ln_unsupported,
249 #ifdef CONFIG_CMD_UBIFS
251 .fstype = FS_TYPE_UBIFS,
253 .null_dev_desc_ok = true,
254 .probe = ubifs_set_blk_dev,
255 .close = ubifs_close,
257 .exists = ubifs_exists,
260 .write = fs_write_unsupported,
261 .uuid = fs_uuid_unsupported,
262 .opendir = fs_opendir_unsupported,
263 .unlink = fs_unlink_unsupported,
264 .mkdir = fs_mkdir_unsupported,
265 .ln = fs_ln_unsupported,
268 #ifdef CONFIG_FS_BTRFS
270 .fstype = FS_TYPE_BTRFS,
272 .null_dev_desc_ok = false,
273 .probe = btrfs_probe,
274 .close = btrfs_close,
276 .exists = btrfs_exists,
279 .write = fs_write_unsupported,
281 .opendir = fs_opendir_unsupported,
282 .unlink = fs_unlink_unsupported,
283 .mkdir = fs_mkdir_unsupported,
284 .ln = fs_ln_unsupported,
287 #if IS_ENABLED(CONFIG_FS_SQUASHFS)
289 .fstype = FS_TYPE_SQUASHFS,
291 .null_dev_desc_ok = false,
293 .opendir = sqfs_opendir,
294 .readdir = sqfs_readdir,
299 .closedir = sqfs_closedir,
300 .exists = sqfs_exists,
301 .uuid = fs_uuid_unsupported,
302 .write = fs_write_unsupported,
303 .ln = fs_ln_unsupported,
304 .unlink = fs_unlink_unsupported,
305 .mkdir = fs_mkdir_unsupported,
309 .fstype = FS_TYPE_ANY,
310 .name = "unsupported",
311 .null_dev_desc_ok = true,
312 .probe = fs_probe_unsupported,
313 .close = fs_close_unsupported,
314 .ls = fs_ls_unsupported,
315 .exists = fs_exists_unsupported,
316 .size = fs_size_unsupported,
317 .read = fs_read_unsupported,
318 .write = fs_write_unsupported,
319 .uuid = fs_uuid_unsupported,
320 .opendir = fs_opendir_unsupported,
321 .unlink = fs_unlink_unsupported,
322 .mkdir = fs_mkdir_unsupported,
323 .ln = fs_ln_unsupported,
327 static struct fstype_info *fs_get_info(int fstype)
329 struct fstype_info *info;
332 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
333 if (fstype == info->fstype)
337 /* Return the 'unsupported' sentinel */
342 * fs_get_type() - Get type of current filesystem
344 * Return: filesystem type
346 * Returns filesystem type representing the current filesystem, or
347 * FS_TYPE_ANY for any unrecognised filesystem.
349 int fs_get_type(void)
355 * fs_get_type_name() - Get type of current filesystem
357 * Return: Pointer to filesystem name
359 * Returns a string describing the current filesystem, or the sentinel
360 * "unsupported" for any unrecognised filesystem.
362 const char *fs_get_type_name(void)
364 return fs_get_info(fs_type)->name;
367 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
369 struct fstype_info *info;
371 #ifdef CONFIG_NEEDS_MANUAL_RELOC
372 static int relocated;
375 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
377 info->name += gd->reloc_off;
378 info->probe += gd->reloc_off;
379 info->close += gd->reloc_off;
380 info->ls += gd->reloc_off;
381 info->read += gd->reloc_off;
382 info->write += gd->reloc_off;
388 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
393 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
394 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
395 fstype != info->fstype)
398 if (!fs_dev_desc && !info->null_dev_desc_ok)
401 if (!info->probe(fs_dev_desc, &fs_partition)) {
402 fs_type = info->fstype;
411 /* set current blk device w/ blk_desc + partition # */
412 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
414 struct fstype_info *info;
418 ret = part_get_info(desc, part, &fs_partition);
420 ret = part_get_info_whole_disk(desc, &fs_partition);
425 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
426 if (!info->probe(fs_dev_desc, &fs_partition)) {
427 fs_type = info->fstype;
438 struct fstype_info *info = fs_get_info(fs_type);
442 fs_type = FS_TYPE_ANY;
445 int fs_uuid(char *uuid_str)
447 struct fstype_info *info = fs_get_info(fs_type);
449 return info->uuid(uuid_str);
452 int fs_ls(const char *dirname)
456 struct fstype_info *info = fs_get_info(fs_type);
458 ret = info->ls(dirname);
465 int fs_exists(const char *filename)
469 struct fstype_info *info = fs_get_info(fs_type);
471 ret = info->exists(filename);
478 int fs_size(const char *filename, loff_t *size)
482 struct fstype_info *info = fs_get_info(fs_type);
484 ret = info->size(filename, size);
492 /* Check if a file may be read to the given address */
493 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
494 loff_t len, struct fstype_info *info)
501 /* get the actual size of the file */
502 ret = info->size(filename, &size);
505 if (offset >= size) {
506 /* offset >= EOF, no bytes will be written */
509 read_len = size - offset;
511 /* limit to 'len' if it is smaller */
512 if (len && len < read_len)
515 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
518 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
521 log_err("** Reading file would overwrite reserved memory **\n");
526 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
527 int do_lmb_check, loff_t *actread)
529 struct fstype_info *info = fs_get_info(fs_type);
535 ret = fs_read_lmb_check(filename, addr, offset, len, info);
542 * We don't actually know how many bytes are being read, since len==0
543 * means read the whole file.
545 buf = map_sysmem(addr, len);
546 ret = info->read(filename, buf, offset, len, actread);
549 /* If we requested a specific number of bytes, check we got it */
550 if (ret == 0 && len && *actread != len)
551 log_debug("** %s shorter than offset + len **\n", filename);
557 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
560 return _fs_read(filename, addr, offset, len, 0, actread);
563 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
566 struct fstype_info *info = fs_get_info(fs_type);
570 buf = map_sysmem(addr, len);
571 ret = info->write(filename, buf, offset, len, actwrite);
574 if (ret < 0 && len != *actwrite) {
575 log_err("** Unable to write file %s **\n", filename);
583 struct fs_dir_stream *fs_opendir(const char *filename)
585 struct fstype_info *info = fs_get_info(fs_type);
586 struct fs_dir_stream *dirs = NULL;
589 ret = info->opendir(filename, &dirs);
596 dirs->desc = fs_dev_desc;
597 dirs->part = fs_dev_part;
602 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
604 struct fstype_info *info;
605 struct fs_dirent *dirent;
608 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
609 info = fs_get_info(fs_type);
611 ret = info->readdir(dirs, &dirent);
621 void fs_closedir(struct fs_dir_stream *dirs)
623 struct fstype_info *info;
628 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
629 info = fs_get_info(fs_type);
631 info->closedir(dirs);
635 int fs_unlink(const char *filename)
639 struct fstype_info *info = fs_get_info(fs_type);
641 ret = info->unlink(filename);
648 int fs_mkdir(const char *dirname)
652 struct fstype_info *info = fs_get_info(fs_type);
654 ret = info->mkdir(dirname);
661 int fs_ln(const char *fname, const char *target)
663 struct fstype_info *info = fs_get_info(fs_type);
666 ret = info->ln(fname, target);
669 log_err("** Unable to create link %s -> %s **\n", fname, target);
677 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
683 return CMD_RET_USAGE;
685 if (fs_set_blk_dev(argv[1], argv[2], fstype))
688 if (fs_size(argv[3], &size) < 0)
689 return CMD_RET_FAILURE;
691 env_set_hex("filesize", size);
696 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
700 const char *addr_str;
701 const char *filename;
710 return CMD_RET_USAGE;
712 return CMD_RET_USAGE;
714 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
715 log_err("Can't set block device\n");
720 addr = simple_strtoul(argv[3], &ep, 16);
721 if (ep == argv[3] || *ep != '\0')
722 return CMD_RET_USAGE;
724 addr_str = env_get("loadaddr");
725 if (addr_str != NULL)
726 addr = simple_strtoul(addr_str, NULL, 16);
728 addr = CONFIG_SYS_LOAD_ADDR;
733 filename = env_get("bootfile");
735 puts("** No boot file defined **\n");
740 bytes = simple_strtoul(argv[5], NULL, 16);
744 pos = simple_strtoul(argv[6], NULL, 16);
749 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
750 time = get_timer(time);
752 log_err("Failed to load '%s'\n", filename);
756 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
757 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
758 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
761 printf("%llu bytes read in %lu ms", len_read, time);
764 print_size(div_u64(len_read, time) * 1000, "/s");
769 env_set_hex("fileaddr", addr);
770 env_set_hex("filesize", len_read);
775 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
779 return CMD_RET_USAGE;
781 return CMD_RET_USAGE;
783 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
786 if (fs_ls(argc >= 4 ? argv[3] : "/"))
792 int file_exists(const char *dev_type, const char *dev_part, const char *file,
795 if (fs_set_blk_dev(dev_type, dev_part, fstype))
798 return fs_exists(file);
801 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
805 const char *filename;
812 if (argc < 6 || argc > 7)
813 return CMD_RET_USAGE;
815 if (fs_set_blk_dev(argv[1], argv[2], fstype))
818 addr = simple_strtoul(argv[3], NULL, 16);
820 bytes = simple_strtoul(argv[5], NULL, 16);
822 pos = simple_strtoul(argv[6], NULL, 16);
827 ret = fs_write(filename, addr, pos, bytes, &len);
828 time = get_timer(time);
832 printf("%llu bytes written in %lu ms", len, time);
835 print_size(div_u64(len, time) * 1000, "/s");
843 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
848 memset(uuid, 0, sizeof(uuid));
850 if (argc < 3 || argc > 4)
851 return CMD_RET_USAGE;
853 if (fs_set_blk_dev(argv[1], argv[2], fstype))
858 return CMD_RET_FAILURE;
861 env_set(argv[3], uuid);
863 printf("%s\n", uuid);
865 return CMD_RET_SUCCESS;
868 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
870 struct fstype_info *info;
872 if (argc < 3 || argc > 4)
873 return CMD_RET_USAGE;
875 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
878 info = fs_get_info(fs_type);
881 env_set(argv[3], info->name);
883 printf("%s\n", info->name);
887 return CMD_RET_SUCCESS;
890 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
894 return CMD_RET_USAGE;
896 if (fs_set_blk_dev(argv[1], argv[2], fstype))
899 if (fs_unlink(argv[3]))
905 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
911 return CMD_RET_USAGE;
913 if (fs_set_blk_dev(argv[1], argv[2], fstype))
916 ret = fs_mkdir(argv[3]);
918 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
925 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
929 return CMD_RET_USAGE;
931 if (fs_set_blk_dev(argv[1], argv[2], fstype))
934 if (fs_ln(argv[3], argv[4]))
940 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
942 struct fstype_info *drv = fstypes;
943 const int n_ents = ARRAY_SIZE(fstypes);
944 struct fstype_info *entry;
947 puts("Supported filesystems");
948 for (entry = drv; entry != drv + n_ents; entry++) {
949 if (entry->fstype != FS_TYPE_ANY) {
950 printf("%c %s", i ? ',' : ':', entry->name);
957 return CMD_RET_SUCCESS;