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>
31 DECLARE_GLOBAL_DATA_PTR;
33 static struct blk_desc *fs_dev_desc;
34 static int fs_dev_part;
35 static struct disk_partition fs_partition;
36 static int fs_type = FS_TYPE_ANY;
38 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
39 struct disk_partition *fs_partition)
41 log_debug("Unrecognized filesystem type\n");
45 static inline int fs_ls_unsupported(const char *dirname)
50 /* generic implementation of ls in terms of opendir/readdir/closedir */
52 static int fs_ls_generic(const char *dirname)
54 struct fs_dir_stream *dirs;
55 struct fs_dirent *dent;
56 int nfiles = 0, ndirs = 0;
58 dirs = fs_opendir(dirname);
62 while ((dent = fs_readdir(dirs))) {
63 if (dent->type == FS_DT_DIR) {
64 printf(" %s/\n", dent->name);
66 } else if (dent->type == FS_DT_LNK) {
67 printf(" <SYM> %s\n", dent->name);
70 printf(" %8lld %s\n", dent->size, dent->name);
77 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
82 static inline int fs_exists_unsupported(const char *filename)
87 static inline int fs_size_unsupported(const char *filename, loff_t *size)
92 static inline int fs_read_unsupported(const char *filename, void *buf,
93 loff_t offset, loff_t len,
99 static inline int fs_write_unsupported(const char *filename, void *buf,
100 loff_t offset, loff_t len,
106 static inline int fs_ln_unsupported(const char *filename, const char *target)
111 static inline void fs_close_unsupported(void)
115 static inline int fs_uuid_unsupported(char *uuid_str)
120 static inline int fs_opendir_unsupported(const char *filename,
121 struct fs_dir_stream **dirs)
126 static inline int fs_unlink_unsupported(const char *filename)
131 static inline int fs_mkdir_unsupported(const char *dirname)
140 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
141 * should be false in most cases. For "virtual" filesystems which
142 * aren't based on a U-Boot block device (e.g. sandbox), this can be
143 * set to true. This should also be true for the dummy entry at the end
144 * of fstypes[], since that is essentially a "virtual" (non-existent)
147 bool null_dev_desc_ok;
148 int (*probe)(struct blk_desc *fs_dev_desc,
149 struct disk_partition *fs_partition);
150 int (*ls)(const char *dirname);
151 int (*exists)(const char *filename);
152 int (*size)(const char *filename, loff_t *size);
153 int (*read)(const char *filename, void *buf, loff_t offset,
154 loff_t len, loff_t *actread);
155 int (*write)(const char *filename, void *buf, loff_t offset,
156 loff_t len, loff_t *actwrite);
158 int (*uuid)(char *uuid_str);
160 * Open a directory stream. On success return 0 and directory
161 * stream pointer via 'dirsp'. On error, return -errno. See
164 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
166 * Read next entry from directory stream. On success return 0
167 * and directory entry pointer via 'dentp'. On error return
168 * -errno. See fs_readdir().
170 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
171 /* see fs_closedir() */
172 void (*closedir)(struct fs_dir_stream *dirs);
173 int (*unlink)(const char *filename);
174 int (*mkdir)(const char *dirname);
175 int (*ln)(const char *filename, const char *target);
178 static struct fstype_info fstypes[] = {
181 .fstype = FS_TYPE_FAT,
183 .null_dev_desc_ok = false,
184 .probe = fat_set_blk_dev,
187 .exists = fat_exists,
189 .read = fat_read_file,
190 #if CONFIG_IS_ENABLED(FAT_WRITE)
191 .write = file_fat_write,
192 .unlink = fat_unlink,
195 .write = fs_write_unsupported,
196 .unlink = fs_unlink_unsupported,
197 .mkdir = fs_mkdir_unsupported,
200 .opendir = fat_opendir,
201 .readdir = fat_readdir,
202 .closedir = fat_closedir,
203 .ln = fs_ln_unsupported,
207 #if CONFIG_IS_ENABLED(FS_EXT4)
209 .fstype = FS_TYPE_EXT,
211 .null_dev_desc_ok = false,
212 .probe = ext4fs_probe,
213 .close = ext4fs_close,
215 .exists = ext4fs_exists,
217 .read = ext4_read_file,
218 #ifdef CONFIG_CMD_EXT4_WRITE
219 .write = ext4_write_file,
220 .ln = ext4fs_create_link,
222 .write = fs_write_unsupported,
223 .ln = fs_ln_unsupported,
226 .opendir = fs_opendir_unsupported,
227 .unlink = fs_unlink_unsupported,
228 .mkdir = fs_mkdir_unsupported,
231 #ifdef CONFIG_SANDBOX
233 .fstype = FS_TYPE_SANDBOX,
235 .null_dev_desc_ok = true,
236 .probe = sandbox_fs_set_blk_dev,
237 .close = sandbox_fs_close,
239 .exists = sandbox_fs_exists,
240 .size = sandbox_fs_size,
241 .read = fs_read_sandbox,
242 .write = fs_write_sandbox,
243 .uuid = fs_uuid_unsupported,
244 .opendir = fs_opendir_unsupported,
245 .unlink = fs_unlink_unsupported,
246 .mkdir = fs_mkdir_unsupported,
247 .ln = fs_ln_unsupported,
250 #ifdef CONFIG_CMD_UBIFS
252 .fstype = FS_TYPE_UBIFS,
254 .null_dev_desc_ok = true,
255 .probe = ubifs_set_blk_dev,
256 .close = ubifs_close,
258 .exists = ubifs_exists,
261 .write = fs_write_unsupported,
262 .uuid = fs_uuid_unsupported,
263 .opendir = fs_opendir_unsupported,
264 .unlink = fs_unlink_unsupported,
265 .mkdir = fs_mkdir_unsupported,
266 .ln = fs_ln_unsupported,
269 #ifdef CONFIG_FS_BTRFS
271 .fstype = FS_TYPE_BTRFS,
273 .null_dev_desc_ok = false,
274 .probe = btrfs_probe,
275 .close = btrfs_close,
277 .exists = btrfs_exists,
280 .write = fs_write_unsupported,
282 .opendir = fs_opendir_unsupported,
283 .unlink = fs_unlink_unsupported,
284 .mkdir = fs_mkdir_unsupported,
285 .ln = fs_ln_unsupported,
288 #if IS_ENABLED(CONFIG_FS_SQUASHFS)
290 .fstype = FS_TYPE_SQUASHFS,
292 .null_dev_desc_ok = false,
294 .opendir = sqfs_opendir,
295 .readdir = sqfs_readdir,
300 .closedir = sqfs_closedir,
301 .exists = sqfs_exists,
302 .uuid = fs_uuid_unsupported,
303 .write = fs_write_unsupported,
304 .ln = fs_ln_unsupported,
305 .unlink = fs_unlink_unsupported,
306 .mkdir = fs_mkdir_unsupported,
309 #if IS_ENABLED(CONFIG_FS_EROFS)
311 .fstype = FS_TYPE_EROFS,
313 .null_dev_desc_ok = false,
314 .probe = erofs_probe,
315 .opendir = erofs_opendir,
316 .readdir = erofs_readdir,
320 .close = erofs_close,
321 .closedir = erofs_closedir,
322 .exists = erofs_exists,
323 .uuid = fs_uuid_unsupported,
324 .write = fs_write_unsupported,
325 .ln = fs_ln_unsupported,
326 .unlink = fs_unlink_unsupported,
327 .mkdir = fs_mkdir_unsupported,
331 .fstype = FS_TYPE_ANY,
332 .name = "unsupported",
333 .null_dev_desc_ok = true,
334 .probe = fs_probe_unsupported,
335 .close = fs_close_unsupported,
336 .ls = fs_ls_unsupported,
337 .exists = fs_exists_unsupported,
338 .size = fs_size_unsupported,
339 .read = fs_read_unsupported,
340 .write = fs_write_unsupported,
341 .uuid = fs_uuid_unsupported,
342 .opendir = fs_opendir_unsupported,
343 .unlink = fs_unlink_unsupported,
344 .mkdir = fs_mkdir_unsupported,
345 .ln = fs_ln_unsupported,
349 static struct fstype_info *fs_get_info(int fstype)
351 struct fstype_info *info;
354 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
355 if (fstype == info->fstype)
359 /* Return the 'unsupported' sentinel */
364 * fs_get_type() - Get type of current filesystem
366 * Return: filesystem type
368 * Returns filesystem type representing the current filesystem, or
369 * FS_TYPE_ANY for any unrecognised filesystem.
371 int fs_get_type(void)
377 * fs_get_type_name() - Get type of current filesystem
379 * Return: Pointer to filesystem name
381 * Returns a string describing the current filesystem, or the sentinel
382 * "unsupported" for any unrecognised filesystem.
384 const char *fs_get_type_name(void)
386 return fs_get_info(fs_type)->name;
389 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
391 struct fstype_info *info;
393 #ifdef CONFIG_NEEDS_MANUAL_RELOC
394 static int relocated;
397 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
399 info->name += gd->reloc_off;
400 info->probe += gd->reloc_off;
401 info->close += gd->reloc_off;
402 info->ls += gd->reloc_off;
403 info->read += gd->reloc_off;
404 info->write += gd->reloc_off;
410 part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
415 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
416 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
417 fstype != info->fstype)
420 if (!fs_dev_desc && !info->null_dev_desc_ok)
423 if (!info->probe(fs_dev_desc, &fs_partition)) {
424 fs_type = info->fstype;
433 /* set current blk device w/ blk_desc + partition # */
434 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
436 struct fstype_info *info;
440 ret = part_get_info(desc, part, &fs_partition);
442 ret = part_get_info_whole_disk(desc, &fs_partition);
447 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
448 if (!info->probe(fs_dev_desc, &fs_partition)) {
449 fs_type = info->fstype;
460 struct fstype_info *info = fs_get_info(fs_type);
464 fs_type = FS_TYPE_ANY;
467 int fs_uuid(char *uuid_str)
469 struct fstype_info *info = fs_get_info(fs_type);
471 return info->uuid(uuid_str);
474 int fs_ls(const char *dirname)
478 struct fstype_info *info = fs_get_info(fs_type);
480 ret = info->ls(dirname);
487 int fs_exists(const char *filename)
491 struct fstype_info *info = fs_get_info(fs_type);
493 ret = info->exists(filename);
500 int fs_size(const char *filename, loff_t *size)
504 struct fstype_info *info = fs_get_info(fs_type);
506 ret = info->size(filename, size);
514 /* Check if a file may be read to the given address */
515 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
516 loff_t len, struct fstype_info *info)
523 /* get the actual size of the file */
524 ret = info->size(filename, &size);
527 if (offset >= size) {
528 /* offset >= EOF, no bytes will be written */
531 read_len = size - offset;
533 /* limit to 'len' if it is smaller */
534 if (len && len < read_len)
537 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
540 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
543 log_err("** Reading file would overwrite reserved memory **\n");
548 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
549 int do_lmb_check, loff_t *actread)
551 struct fstype_info *info = fs_get_info(fs_type);
557 ret = fs_read_lmb_check(filename, addr, offset, len, info);
564 * We don't actually know how many bytes are being read, since len==0
565 * means read the whole file.
567 buf = map_sysmem(addr, len);
568 ret = info->read(filename, buf, offset, len, actread);
571 /* If we requested a specific number of bytes, check we got it */
572 if (ret == 0 && len && *actread != len)
573 log_debug("** %s shorter than offset + len **\n", filename);
579 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
582 return _fs_read(filename, addr, offset, len, 0, actread);
585 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
588 struct fstype_info *info = fs_get_info(fs_type);
592 buf = map_sysmem(addr, len);
593 ret = info->write(filename, buf, offset, len, actwrite);
596 if (ret < 0 && len != *actwrite) {
597 log_err("** Unable to write file %s **\n", filename);
605 struct fs_dir_stream *fs_opendir(const char *filename)
607 struct fstype_info *info = fs_get_info(fs_type);
608 struct fs_dir_stream *dirs = NULL;
611 ret = info->opendir(filename, &dirs);
618 dirs->desc = fs_dev_desc;
619 dirs->part = fs_dev_part;
624 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
626 struct fstype_info *info;
627 struct fs_dirent *dirent;
630 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
631 info = fs_get_info(fs_type);
633 ret = info->readdir(dirs, &dirent);
643 void fs_closedir(struct fs_dir_stream *dirs)
645 struct fstype_info *info;
650 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
651 info = fs_get_info(fs_type);
653 info->closedir(dirs);
657 int fs_unlink(const char *filename)
661 struct fstype_info *info = fs_get_info(fs_type);
663 ret = info->unlink(filename);
670 int fs_mkdir(const char *dirname)
674 struct fstype_info *info = fs_get_info(fs_type);
676 ret = info->mkdir(dirname);
683 int fs_ln(const char *fname, const char *target)
685 struct fstype_info *info = fs_get_info(fs_type);
688 ret = info->ln(fname, target);
691 log_err("** Unable to create link %s -> %s **\n", fname, target);
699 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
705 return CMD_RET_USAGE;
707 if (fs_set_blk_dev(argv[1], argv[2], fstype))
710 if (fs_size(argv[3], &size) < 0)
711 return CMD_RET_FAILURE;
713 env_set_hex("filesize", size);
718 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
722 const char *addr_str;
723 const char *filename;
732 return CMD_RET_USAGE;
734 return CMD_RET_USAGE;
736 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
737 log_err("Can't set block device\n");
742 addr = hextoul(argv[3], &ep);
743 if (ep == argv[3] || *ep != '\0')
744 return CMD_RET_USAGE;
746 addr_str = env_get("loadaddr");
747 if (addr_str != NULL)
748 addr = hextoul(addr_str, NULL);
750 addr = CONFIG_SYS_LOAD_ADDR;
755 filename = env_get("bootfile");
757 puts("** No boot file defined **\n");
762 bytes = hextoul(argv[5], NULL);
766 pos = hextoul(argv[6], NULL);
771 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
772 time = get_timer(time);
774 log_err("Failed to load '%s'\n", filename);
778 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
779 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
780 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
783 printf("%llu bytes read in %lu ms", len_read, time);
786 print_size(div_u64(len_read, time) * 1000, "/s");
791 env_set_hex("fileaddr", addr);
792 env_set_hex("filesize", len_read);
797 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
801 return CMD_RET_USAGE;
803 return CMD_RET_USAGE;
805 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
808 if (fs_ls(argc >= 4 ? argv[3] : "/"))
814 int file_exists(const char *dev_type, const char *dev_part, const char *file,
817 if (fs_set_blk_dev(dev_type, dev_part, fstype))
820 return fs_exists(file);
823 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
827 const char *filename;
834 if (argc < 6 || argc > 7)
835 return CMD_RET_USAGE;
837 if (fs_set_blk_dev(argv[1], argv[2], fstype))
840 addr = hextoul(argv[3], NULL);
842 bytes = hextoul(argv[5], NULL);
844 pos = hextoul(argv[6], NULL);
849 ret = fs_write(filename, addr, pos, bytes, &len);
850 time = get_timer(time);
854 printf("%llu bytes written in %lu ms", len, time);
857 print_size(div_u64(len, time) * 1000, "/s");
865 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
870 memset(uuid, 0, sizeof(uuid));
872 if (argc < 3 || argc > 4)
873 return CMD_RET_USAGE;
875 if (fs_set_blk_dev(argv[1], argv[2], fstype))
880 return CMD_RET_FAILURE;
883 env_set(argv[3], uuid);
885 printf("%s\n", uuid);
887 return CMD_RET_SUCCESS;
890 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
892 struct fstype_info *info;
894 if (argc < 3 || argc > 4)
895 return CMD_RET_USAGE;
897 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
900 info = fs_get_info(fs_type);
903 env_set(argv[3], info->name);
905 printf("%s\n", info->name);
909 return CMD_RET_SUCCESS;
912 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
916 return CMD_RET_USAGE;
918 if (fs_set_blk_dev(argv[1], argv[2], fstype))
921 if (fs_unlink(argv[3]))
927 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
933 return CMD_RET_USAGE;
935 if (fs_set_blk_dev(argv[1], argv[2], fstype))
938 ret = fs_mkdir(argv[3]);
940 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
947 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
951 return CMD_RET_USAGE;
953 if (fs_set_blk_dev(argv[1], argv[2], fstype))
956 if (fs_ln(argv[3], argv[4]))
962 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
964 struct fstype_info *drv = fstypes;
965 const int n_ents = ARRAY_SIZE(fstypes);
966 struct fstype_info *entry;
969 puts("Supported filesystems");
970 for (entry = drv; entry != drv + n_ents; entry++) {
971 if (entry->fstype != FS_TYPE_ANY) {
972 printf("%c %s", i ? ',' : ':', entry->name);
979 return CMD_RET_SUCCESS;