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>
25 #include <linux/math64.h>
26 #include <efi_loader.h>
29 DECLARE_GLOBAL_DATA_PTR;
31 static struct blk_desc *fs_dev_desc;
32 static int fs_dev_part;
33 static struct disk_partition fs_partition;
34 static int fs_type = FS_TYPE_ANY;
36 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
37 struct disk_partition *fs_partition)
39 log_err("** Unrecognized filesystem type **\n");
43 static inline int fs_ls_unsupported(const char *dirname)
48 /* generic implementation of ls in terms of opendir/readdir/closedir */
50 static int fs_ls_generic(const char *dirname)
52 struct fs_dir_stream *dirs;
53 struct fs_dirent *dent;
54 int nfiles = 0, ndirs = 0;
56 dirs = fs_opendir(dirname);
60 while ((dent = fs_readdir(dirs))) {
61 if (dent->type == FS_DT_DIR) {
62 printf(" %s/\n", dent->name);
64 } else if (dent->type == FS_DT_LNK) {
65 printf(" <SYM> %s\n", dent->name);
68 printf(" %8lld %s\n", dent->size, dent->name);
75 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
80 static inline int fs_exists_unsupported(const char *filename)
85 static inline int fs_size_unsupported(const char *filename, loff_t *size)
90 static inline int fs_read_unsupported(const char *filename, void *buf,
91 loff_t offset, loff_t len,
97 static inline int fs_write_unsupported(const char *filename, void *buf,
98 loff_t offset, loff_t len,
104 static inline int fs_ln_unsupported(const char *filename, const char *target)
109 static inline void fs_close_unsupported(void)
113 static inline int fs_uuid_unsupported(char *uuid_str)
118 static inline int fs_opendir_unsupported(const char *filename,
119 struct fs_dir_stream **dirs)
124 static inline int fs_unlink_unsupported(const char *filename)
129 static inline int fs_mkdir_unsupported(const char *dirname)
138 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
139 * should be false in most cases. For "virtual" filesystems which
140 * aren't based on a U-Boot block device (e.g. sandbox), this can be
141 * set to true. This should also be true for the dummy entry at the end
142 * of fstypes[], since that is essentially a "virtual" (non-existent)
145 bool null_dev_desc_ok;
146 int (*probe)(struct blk_desc *fs_dev_desc,
147 struct disk_partition *fs_partition);
148 int (*ls)(const char *dirname);
149 int (*exists)(const char *filename);
150 int (*size)(const char *filename, loff_t *size);
151 int (*read)(const char *filename, void *buf, loff_t offset,
152 loff_t len, loff_t *actread);
153 int (*write)(const char *filename, void *buf, loff_t offset,
154 loff_t len, loff_t *actwrite);
156 int (*uuid)(char *uuid_str);
158 * Open a directory stream. On success return 0 and directory
159 * stream pointer via 'dirsp'. On error, return -errno. See
162 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
164 * Read next entry from directory stream. On success return 0
165 * and directory entry pointer via 'dentp'. On error return
166 * -errno. See fs_readdir().
168 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
169 /* see fs_closedir() */
170 void (*closedir)(struct fs_dir_stream *dirs);
171 int (*unlink)(const char *filename);
172 int (*mkdir)(const char *dirname);
173 int (*ln)(const char *filename, const char *target);
176 static struct fstype_info fstypes[] = {
179 .fstype = FS_TYPE_FAT,
181 .null_dev_desc_ok = false,
182 .probe = fat_set_blk_dev,
185 .exists = fat_exists,
187 .read = fat_read_file,
188 #if CONFIG_IS_ENABLED(FAT_WRITE)
189 .write = file_fat_write,
190 .unlink = fat_unlink,
193 .write = fs_write_unsupported,
194 .unlink = fs_unlink_unsupported,
195 .mkdir = fs_mkdir_unsupported,
198 .opendir = fat_opendir,
199 .readdir = fat_readdir,
200 .closedir = fat_closedir,
201 .ln = fs_ln_unsupported,
205 #if CONFIG_IS_ENABLED(FS_EXT4)
207 .fstype = FS_TYPE_EXT,
209 .null_dev_desc_ok = false,
210 .probe = ext4fs_probe,
211 .close = ext4fs_close,
213 .exists = ext4fs_exists,
215 .read = ext4_read_file,
216 #ifdef CONFIG_CMD_EXT4_WRITE
217 .write = ext4_write_file,
218 .ln = ext4fs_create_link,
220 .write = fs_write_unsupported,
221 .ln = fs_ln_unsupported,
224 .opendir = fs_opendir_unsupported,
225 .unlink = fs_unlink_unsupported,
226 .mkdir = fs_mkdir_unsupported,
229 #ifdef CONFIG_SANDBOX
231 .fstype = FS_TYPE_SANDBOX,
233 .null_dev_desc_ok = true,
234 .probe = sandbox_fs_set_blk_dev,
235 .close = sandbox_fs_close,
237 .exists = sandbox_fs_exists,
238 .size = sandbox_fs_size,
239 .read = fs_read_sandbox,
240 .write = fs_write_sandbox,
241 .uuid = fs_uuid_unsupported,
242 .opendir = fs_opendir_unsupported,
243 .unlink = fs_unlink_unsupported,
244 .mkdir = fs_mkdir_unsupported,
245 .ln = fs_ln_unsupported,
248 #ifdef CONFIG_CMD_UBIFS
250 .fstype = FS_TYPE_UBIFS,
252 .null_dev_desc_ok = true,
253 .probe = ubifs_set_blk_dev,
254 .close = ubifs_close,
256 .exists = ubifs_exists,
259 .write = fs_write_unsupported,
260 .uuid = fs_uuid_unsupported,
261 .opendir = fs_opendir_unsupported,
262 .unlink = fs_unlink_unsupported,
263 .mkdir = fs_mkdir_unsupported,
264 .ln = fs_ln_unsupported,
267 #ifdef CONFIG_FS_BTRFS
269 .fstype = FS_TYPE_BTRFS,
271 .null_dev_desc_ok = false,
272 .probe = btrfs_probe,
273 .close = btrfs_close,
275 .exists = btrfs_exists,
278 .write = fs_write_unsupported,
280 .opendir = fs_opendir_unsupported,
281 .unlink = fs_unlink_unsupported,
282 .mkdir = fs_mkdir_unsupported,
283 .ln = fs_ln_unsupported,
286 #if IS_ENABLED(CONFIG_FS_SQUASHFS)
288 .fstype = FS_TYPE_SQUASHFS,
290 .null_dev_desc_ok = false,
292 .opendir = sqfs_opendir,
293 .readdir = sqfs_readdir,
298 .closedir = sqfs_closedir,
299 .exists = sqfs_exists,
300 .uuid = fs_uuid_unsupported,
301 .write = fs_write_unsupported,
302 .ln = fs_ln_unsupported,
303 .unlink = fs_unlink_unsupported,
304 .mkdir = fs_mkdir_unsupported,
308 .fstype = FS_TYPE_ANY,
309 .name = "unsupported",
310 .null_dev_desc_ok = true,
311 .probe = fs_probe_unsupported,
312 .close = fs_close_unsupported,
313 .ls = fs_ls_unsupported,
314 .exists = fs_exists_unsupported,
315 .size = fs_size_unsupported,
316 .read = fs_read_unsupported,
317 .write = fs_write_unsupported,
318 .uuid = fs_uuid_unsupported,
319 .opendir = fs_opendir_unsupported,
320 .unlink = fs_unlink_unsupported,
321 .mkdir = fs_mkdir_unsupported,
322 .ln = fs_ln_unsupported,
326 static struct fstype_info *fs_get_info(int fstype)
328 struct fstype_info *info;
331 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
332 if (fstype == info->fstype)
336 /* Return the 'unsupported' sentinel */
341 * fs_get_type() - Get type of current filesystem
343 * Return: filesystem type
345 * Returns filesystem type representing the current filesystem, or
346 * FS_TYPE_ANY for any unrecognised filesystem.
348 int fs_get_type(void)
354 * fs_get_type_name() - Get type of current filesystem
356 * Return: Pointer to filesystem name
358 * Returns a string describing the current filesystem, or the sentinel
359 * "unsupported" for any unrecognised filesystem.
361 const char *fs_get_type_name(void)
363 return fs_get_info(fs_type)->name;
366 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
368 struct fstype_info *info;
370 #ifdef CONFIG_NEEDS_MANUAL_RELOC
371 static int relocated;
374 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
376 info->name += gd->reloc_off;
377 info->probe += gd->reloc_off;
378 info->close += gd->reloc_off;
379 info->ls += gd->reloc_off;
380 info->read += gd->reloc_off;
381 info->write += gd->reloc_off;
387 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
392 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
393 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
394 fstype != info->fstype)
397 if (!fs_dev_desc && !info->null_dev_desc_ok)
400 if (!info->probe(fs_dev_desc, &fs_partition)) {
401 fs_type = info->fstype;
410 /* set current blk device w/ blk_desc + partition # */
411 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
413 struct fstype_info *info;
417 ret = part_get_info(desc, part, &fs_partition);
419 ret = part_get_info_whole_disk(desc, &fs_partition);
424 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
425 if (!info->probe(fs_dev_desc, &fs_partition)) {
426 fs_type = info->fstype;
437 struct fstype_info *info = fs_get_info(fs_type);
441 fs_type = FS_TYPE_ANY;
444 int fs_uuid(char *uuid_str)
446 struct fstype_info *info = fs_get_info(fs_type);
448 return info->uuid(uuid_str);
451 int fs_ls(const char *dirname)
455 struct fstype_info *info = fs_get_info(fs_type);
457 ret = info->ls(dirname);
464 int fs_exists(const char *filename)
468 struct fstype_info *info = fs_get_info(fs_type);
470 ret = info->exists(filename);
477 int fs_size(const char *filename, loff_t *size)
481 struct fstype_info *info = fs_get_info(fs_type);
483 ret = info->size(filename, size);
491 /* Check if a file may be read to the given address */
492 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
493 loff_t len, struct fstype_info *info)
500 /* get the actual size of the file */
501 ret = info->size(filename, &size);
504 if (offset >= size) {
505 /* offset >= EOF, no bytes will be written */
508 read_len = size - offset;
510 /* limit to 'len' if it is smaller */
511 if (len && len < read_len)
514 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
517 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
520 log_err("** Reading file would overwrite reserved memory **\n");
525 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
526 int do_lmb_check, loff_t *actread)
528 struct fstype_info *info = fs_get_info(fs_type);
534 ret = fs_read_lmb_check(filename, addr, offset, len, info);
541 * We don't actually know how many bytes are being read, since len==0
542 * means read the whole file.
544 buf = map_sysmem(addr, len);
545 ret = info->read(filename, buf, offset, len, actread);
548 /* If we requested a specific number of bytes, check we got it */
549 if (ret == 0 && len && *actread != len)
550 log_debug("** %s shorter than offset + len **\n", filename);
556 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
559 return _fs_read(filename, addr, offset, len, 0, actread);
562 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
565 struct fstype_info *info = fs_get_info(fs_type);
569 buf = map_sysmem(addr, len);
570 ret = info->write(filename, buf, offset, len, actwrite);
573 if (ret < 0 && len != *actwrite) {
574 log_err("** Unable to write file %s **\n", filename);
582 struct fs_dir_stream *fs_opendir(const char *filename)
584 struct fstype_info *info = fs_get_info(fs_type);
585 struct fs_dir_stream *dirs = NULL;
588 ret = info->opendir(filename, &dirs);
595 dirs->desc = fs_dev_desc;
596 dirs->part = fs_dev_part;
601 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
603 struct fstype_info *info;
604 struct fs_dirent *dirent;
607 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
608 info = fs_get_info(fs_type);
610 ret = info->readdir(dirs, &dirent);
620 void fs_closedir(struct fs_dir_stream *dirs)
622 struct fstype_info *info;
627 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
628 info = fs_get_info(fs_type);
630 info->closedir(dirs);
634 int fs_unlink(const char *filename)
638 struct fstype_info *info = fs_get_info(fs_type);
640 ret = info->unlink(filename);
647 int fs_mkdir(const char *dirname)
651 struct fstype_info *info = fs_get_info(fs_type);
653 ret = info->mkdir(dirname);
660 int fs_ln(const char *fname, const char *target)
662 struct fstype_info *info = fs_get_info(fs_type);
665 ret = info->ln(fname, target);
668 log_err("** Unable to create link %s -> %s **\n", fname, target);
676 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
682 return CMD_RET_USAGE;
684 if (fs_set_blk_dev(argv[1], argv[2], fstype))
687 if (fs_size(argv[3], &size) < 0)
688 return CMD_RET_FAILURE;
690 env_set_hex("filesize", size);
695 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
699 const char *addr_str;
700 const char *filename;
709 return CMD_RET_USAGE;
711 return CMD_RET_USAGE;
713 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
717 addr = simple_strtoul(argv[3], &ep, 16);
718 if (ep == argv[3] || *ep != '\0')
719 return CMD_RET_USAGE;
721 addr_str = env_get("loadaddr");
722 if (addr_str != NULL)
723 addr = simple_strtoul(addr_str, NULL, 16);
725 addr = CONFIG_SYS_LOAD_ADDR;
730 filename = env_get("bootfile");
732 puts("** No boot file defined **\n");
737 bytes = simple_strtoul(argv[5], NULL, 16);
741 pos = simple_strtoul(argv[6], NULL, 16);
746 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
747 time = get_timer(time);
749 log_err("Failed to load '%s'\n", filename);
753 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
754 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
755 (argc > 4) ? argv[4] : "");
757 printf("%llu bytes read in %lu ms", len_read, time);
760 print_size(div_u64(len_read, time) * 1000, "/s");
765 env_set_hex("fileaddr", addr);
766 env_set_hex("filesize", len_read);
771 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
775 return CMD_RET_USAGE;
777 return CMD_RET_USAGE;
779 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
782 if (fs_ls(argc >= 4 ? argv[3] : "/"))
788 int file_exists(const char *dev_type, const char *dev_part, const char *file,
791 if (fs_set_blk_dev(dev_type, dev_part, fstype))
794 return fs_exists(file);
797 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
801 const char *filename;
808 if (argc < 6 || argc > 7)
809 return CMD_RET_USAGE;
811 if (fs_set_blk_dev(argv[1], argv[2], fstype))
814 addr = simple_strtoul(argv[3], NULL, 16);
816 bytes = simple_strtoul(argv[5], NULL, 16);
818 pos = simple_strtoul(argv[6], NULL, 16);
823 ret = fs_write(filename, addr, pos, bytes, &len);
824 time = get_timer(time);
828 printf("%llu bytes written in %lu ms", len, time);
831 print_size(div_u64(len, time) * 1000, "/s");
839 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
844 memset(uuid, 0, sizeof(uuid));
846 if (argc < 3 || argc > 4)
847 return CMD_RET_USAGE;
849 if (fs_set_blk_dev(argv[1], argv[2], fstype))
854 return CMD_RET_FAILURE;
857 env_set(argv[3], uuid);
859 printf("%s\n", uuid);
861 return CMD_RET_SUCCESS;
864 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
866 struct fstype_info *info;
868 if (argc < 3 || argc > 4)
869 return CMD_RET_USAGE;
871 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
874 info = fs_get_info(fs_type);
877 env_set(argv[3], info->name);
879 printf("%s\n", info->name);
883 return CMD_RET_SUCCESS;
886 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
890 return CMD_RET_USAGE;
892 if (fs_set_blk_dev(argv[1], argv[2], fstype))
895 if (fs_unlink(argv[3]))
901 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
907 return CMD_RET_USAGE;
909 if (fs_set_blk_dev(argv[1], argv[2], fstype))
912 ret = fs_mkdir(argv[3]);
914 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
921 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
925 return CMD_RET_USAGE;
927 if (fs_set_blk_dev(argv[1], argv[2], fstype))
930 if (fs_ln(argv[3], argv[4]))
936 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
938 struct fstype_info *drv = fstypes;
939 const int n_ents = ARRAY_SIZE(fstypes);
940 struct fstype_info *entry;
943 puts("Supported filesystems");
944 for (entry = drv; entry != drv + n_ents; entry++) {
945 if (entry->fstype != FS_TYPE_ANY) {
946 printf("%c %s", i ? ',' : ':', entry->name);
953 return CMD_RET_SUCCESS;