1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 #define LOG_CATEGORY LOGC_CORE
10 #include <display_options.h>
21 #include <sandboxfs.h>
22 #include <semihostingfs.h>
24 #include <ubifs_uboot.h>
26 #include <asm/cache.h>
27 #include <asm/global_data.h>
30 #include <linux/math64.h>
31 #include <linux/sizes.h>
32 #include <efi_loader.h>
36 DECLARE_GLOBAL_DATA_PTR;
38 static struct blk_desc *fs_dev_desc;
39 static int fs_dev_part;
40 static struct disk_partition fs_partition;
41 static int fs_type = FS_TYPE_ANY;
43 void fs_set_type(int type)
48 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
49 struct disk_partition *fs_partition)
51 log_debug("Unrecognized filesystem type\n");
55 static inline int fs_ls_unsupported(const char *dirname)
60 /* generic implementation of ls in terms of opendir/readdir/closedir */
62 static int fs_ls_generic(const char *dirname)
64 struct fs_dir_stream *dirs;
65 struct fs_dirent *dent;
66 int nfiles = 0, ndirs = 0;
68 dirs = fs_opendir(dirname);
72 while ((dent = fs_readdir(dirs))) {
73 if (dent->type == FS_DT_DIR) {
74 printf(" %s/\n", dent->name);
76 } else if (dent->type == FS_DT_LNK) {
77 printf(" <SYM> %s\n", dent->name);
80 printf(" %8lld %s\n", dent->size, dent->name);
87 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
92 static inline int fs_exists_unsupported(const char *filename)
97 static inline int fs_size_unsupported(const char *filename, loff_t *size)
102 static inline int fs_read_unsupported(const char *filename, void *buf,
103 loff_t offset, loff_t len,
109 static inline int fs_write_unsupported(const char *filename, void *buf,
110 loff_t offset, loff_t len,
116 static inline int fs_ln_unsupported(const char *filename, const char *target)
121 static inline void fs_close_unsupported(void)
125 static inline int fs_uuid_unsupported(char *uuid_str)
130 static inline int fs_opendir_unsupported(const char *filename,
131 struct fs_dir_stream **dirs)
136 static inline int fs_unlink_unsupported(const char *filename)
141 static inline int fs_mkdir_unsupported(const char *dirname)
150 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
151 * should be false in most cases. For "virtual" filesystems which
152 * aren't based on a U-Boot block device (e.g. sandbox), this can be
153 * set to true. This should also be true for the dummy entry at the end
154 * of fstypes[], since that is essentially a "virtual" (non-existent)
157 bool null_dev_desc_ok;
158 int (*probe)(struct blk_desc *fs_dev_desc,
159 struct disk_partition *fs_partition);
160 int (*ls)(const char *dirname);
161 int (*exists)(const char *filename);
162 int (*size)(const char *filename, loff_t *size);
163 int (*read)(const char *filename, void *buf, loff_t offset,
164 loff_t len, loff_t *actread);
165 int (*write)(const char *filename, void *buf, loff_t offset,
166 loff_t len, loff_t *actwrite);
168 int (*uuid)(char *uuid_str);
170 * Open a directory stream. On success return 0 and directory
171 * stream pointer via 'dirsp'. On error, return -errno. See
174 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
176 * Read next entry from directory stream. On success return 0
177 * and directory entry pointer via 'dentp'. On error return
178 * -errno. See fs_readdir().
180 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
181 /* see fs_closedir() */
182 void (*closedir)(struct fs_dir_stream *dirs);
183 int (*unlink)(const char *filename);
184 int (*mkdir)(const char *dirname);
185 int (*ln)(const char *filename, const char *target);
188 static struct fstype_info fstypes[] = {
189 #if CONFIG_IS_ENABLED(FS_FAT)
191 .fstype = FS_TYPE_FAT,
193 .null_dev_desc_ok = false,
194 .probe = fat_set_blk_dev,
197 .exists = fat_exists,
199 .read = fat_read_file,
200 #if CONFIG_IS_ENABLED(FAT_WRITE)
201 .write = file_fat_write,
202 .unlink = fat_unlink,
205 .write = fs_write_unsupported,
206 .unlink = fs_unlink_unsupported,
207 .mkdir = fs_mkdir_unsupported,
210 .opendir = fat_opendir,
211 .readdir = fat_readdir,
212 .closedir = fat_closedir,
213 .ln = fs_ln_unsupported,
217 #if CONFIG_IS_ENABLED(FS_EXT4)
219 .fstype = FS_TYPE_EXT,
221 .null_dev_desc_ok = false,
222 .probe = ext4fs_probe,
223 .close = ext4fs_close,
225 .exists = ext4fs_exists,
227 .read = ext4_read_file,
228 #ifdef CONFIG_EXT4_WRITE
229 .write = ext4_write_file,
230 .ln = ext4fs_create_link,
232 .write = fs_write_unsupported,
233 .ln = fs_ln_unsupported,
236 .opendir = ext4fs_opendir,
237 .readdir = ext4fs_readdir,
238 .closedir = ext4fs_closedir,
239 .unlink = fs_unlink_unsupported,
240 .mkdir = fs_mkdir_unsupported,
243 #if IS_ENABLED(CONFIG_SANDBOX) && !IS_ENABLED(CONFIG_XPL_BUILD)
245 .fstype = FS_TYPE_SANDBOX,
247 .null_dev_desc_ok = true,
248 .probe = sandbox_fs_set_blk_dev,
249 .close = sandbox_fs_close,
251 .exists = sandbox_fs_exists,
252 .size = sandbox_fs_size,
253 .read = fs_read_sandbox,
254 .write = fs_write_sandbox,
255 .uuid = fs_uuid_unsupported,
256 .opendir = fs_opendir_unsupported,
257 .unlink = fs_unlink_unsupported,
258 .mkdir = fs_mkdir_unsupported,
259 .ln = fs_ln_unsupported,
262 #if CONFIG_IS_ENABLED(SEMIHOSTING)
264 .fstype = FS_TYPE_SEMIHOSTING,
265 .name = "semihosting",
266 .null_dev_desc_ok = true,
267 .probe = smh_fs_set_blk_dev,
268 .close = fs_close_unsupported,
269 .ls = fs_ls_unsupported,
270 .exists = fs_exists_unsupported,
273 .write = smh_fs_write,
274 .uuid = fs_uuid_unsupported,
275 .opendir = fs_opendir_unsupported,
276 .unlink = fs_unlink_unsupported,
277 .mkdir = fs_mkdir_unsupported,
278 .ln = fs_ln_unsupported,
281 #ifndef CONFIG_XPL_BUILD
282 #ifdef CONFIG_CMD_UBIFS
284 .fstype = FS_TYPE_UBIFS,
286 .null_dev_desc_ok = true,
287 .probe = ubifs_set_blk_dev,
288 .close = ubifs_close,
290 .exists = ubifs_exists,
293 .write = fs_write_unsupported,
294 .uuid = fs_uuid_unsupported,
295 .opendir = fs_opendir_unsupported,
296 .unlink = fs_unlink_unsupported,
297 .mkdir = fs_mkdir_unsupported,
298 .ln = fs_ln_unsupported,
302 #ifndef CONFIG_XPL_BUILD
303 #ifdef CONFIG_FS_BTRFS
305 .fstype = FS_TYPE_BTRFS,
307 .null_dev_desc_ok = false,
308 .probe = btrfs_probe,
309 .close = btrfs_close,
311 .exists = btrfs_exists,
314 .write = fs_write_unsupported,
316 .opendir = fs_opendir_unsupported,
317 .unlink = fs_unlink_unsupported,
318 .mkdir = fs_mkdir_unsupported,
319 .ln = fs_ln_unsupported,
323 #if CONFIG_IS_ENABLED(FS_SQUASHFS)
325 .fstype = FS_TYPE_SQUASHFS,
327 .null_dev_desc_ok = false,
329 .opendir = sqfs_opendir,
330 .readdir = sqfs_readdir,
335 .closedir = sqfs_closedir,
336 .exists = sqfs_exists,
337 .uuid = fs_uuid_unsupported,
338 .write = fs_write_unsupported,
339 .ln = fs_ln_unsupported,
340 .unlink = fs_unlink_unsupported,
341 .mkdir = fs_mkdir_unsupported,
344 #if IS_ENABLED(CONFIG_FS_EROFS)
346 .fstype = FS_TYPE_EROFS,
348 .null_dev_desc_ok = false,
349 .probe = erofs_probe,
350 .opendir = erofs_opendir,
351 .readdir = erofs_readdir,
355 .close = erofs_close,
356 .closedir = erofs_closedir,
357 .exists = erofs_exists,
358 .uuid = fs_uuid_unsupported,
359 .write = fs_write_unsupported,
360 .ln = fs_ln_unsupported,
361 .unlink = fs_unlink_unsupported,
362 .mkdir = fs_mkdir_unsupported,
366 .fstype = FS_TYPE_ANY,
367 .name = "unsupported",
368 .null_dev_desc_ok = true,
369 .probe = fs_probe_unsupported,
370 .close = fs_close_unsupported,
371 .ls = fs_ls_unsupported,
372 .exists = fs_exists_unsupported,
373 .size = fs_size_unsupported,
374 .read = fs_read_unsupported,
375 .write = fs_write_unsupported,
376 .uuid = fs_uuid_unsupported,
377 .opendir = fs_opendir_unsupported,
378 .unlink = fs_unlink_unsupported,
379 .mkdir = fs_mkdir_unsupported,
380 .ln = fs_ln_unsupported,
384 static struct fstype_info *fs_get_info(int fstype)
386 struct fstype_info *info;
389 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
390 if (fstype == info->fstype)
394 /* Return the 'unsupported' sentinel */
399 * fs_get_type() - Get type of current filesystem
401 * Return: filesystem type
403 * Returns filesystem type representing the current filesystem, or
404 * FS_TYPE_ANY for any unrecognised filesystem.
406 int fs_get_type(void)
412 * fs_get_type_name() - Get type of current filesystem
414 * Return: Pointer to filesystem name
416 * Returns a string describing the current filesystem, or the sentinel
417 * "unsupported" for any unrecognised filesystem.
419 const char *fs_get_type_name(void)
421 return fs_get_info(fs_type)->name;
424 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
426 struct fstype_info *info;
429 part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
434 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
435 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
436 fstype != info->fstype)
439 if (!fs_dev_desc && !info->null_dev_desc_ok)
442 if (!info->probe(fs_dev_desc, &fs_partition)) {
443 fs_type = info->fstype;
452 /* set current blk device w/ blk_desc + partition # */
453 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
455 struct fstype_info *info;
459 ret = part_get_info(desc, part, &fs_partition);
461 ret = part_get_info_whole_disk(desc, &fs_partition);
466 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
467 if (!info->probe(fs_dev_desc, &fs_partition)) {
468 fs_type = info->fstype;
479 struct fstype_info *info = fs_get_info(fs_type);
483 fs_type = FS_TYPE_ANY;
486 int fs_uuid(char *uuid_str)
488 struct fstype_info *info = fs_get_info(fs_type);
490 return info->uuid(uuid_str);
493 int fs_ls(const char *dirname)
497 struct fstype_info *info = fs_get_info(fs_type);
499 ret = info->ls(dirname);
506 int fs_exists(const char *filename)
510 struct fstype_info *info = fs_get_info(fs_type);
512 ret = info->exists(filename);
519 int fs_size(const char *filename, loff_t *size)
523 struct fstype_info *info = fs_get_info(fs_type);
525 ret = info->size(filename, size);
532 #if CONFIG_IS_ENABLED(LMB)
533 /* Check if a file may be read to the given address */
534 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
535 loff_t len, struct fstype_info *info)
541 /* get the actual size of the file */
542 ret = info->size(filename, &size);
545 if (offset >= size) {
546 /* offset >= EOF, no bytes will be written */
549 read_len = size - offset;
551 /* limit to 'len' if it is smaller */
552 if (len && len < read_len)
557 if (lmb_alloc_addr(addr, read_len, LMB_NONE) == addr)
560 log_err("** Reading file would overwrite reserved memory **\n");
565 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
566 int do_lmb_check, loff_t *actread)
568 struct fstype_info *info = fs_get_info(fs_type);
572 #if CONFIG_IS_ENABLED(LMB)
574 ret = fs_read_lmb_check(filename, addr, offset, len, info);
581 * We don't actually know how many bytes are being read, since len==0
582 * means read the whole file.
584 buf = map_sysmem(addr, len);
585 ret = info->read(filename, buf, offset, len, actread);
588 /* If we requested a specific number of bytes, check we got it */
589 if (ret == 0 && len && *actread != len)
590 log_debug("** %s shorter than offset + len **\n", filename);
596 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
599 return _fs_read(filename, addr, offset, len, 0, actread);
602 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
605 struct fstype_info *info = fs_get_info(fs_type);
609 buf = map_sysmem(addr, len);
610 ret = info->write(filename, buf, offset, len, actwrite);
613 if (ret < 0 && len != *actwrite) {
614 log_err("** Unable to write file %s **\n", filename);
622 struct fs_dir_stream *fs_opendir(const char *filename)
624 struct fstype_info *info = fs_get_info(fs_type);
625 struct fs_dir_stream *dirs = NULL;
628 ret = info->opendir(filename, &dirs);
635 dirs->desc = fs_dev_desc;
636 dirs->part = fs_dev_part;
641 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
643 struct fstype_info *info;
644 struct fs_dirent *dirent;
647 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
648 info = fs_get_info(fs_type);
650 ret = info->readdir(dirs, &dirent);
660 void fs_closedir(struct fs_dir_stream *dirs)
662 struct fstype_info *info;
667 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
668 info = fs_get_info(fs_type);
670 info->closedir(dirs);
674 int fs_unlink(const char *filename)
678 struct fstype_info *info = fs_get_info(fs_type);
680 ret = info->unlink(filename);
687 int fs_mkdir(const char *dirname)
691 struct fstype_info *info = fs_get_info(fs_type);
693 ret = info->mkdir(dirname);
700 int fs_ln(const char *fname, const char *target)
702 struct fstype_info *info = fs_get_info(fs_type);
705 ret = info->ln(fname, target);
708 log_err("** Unable to create link %s -> %s **\n", fname, target);
716 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
722 return CMD_RET_USAGE;
724 if (fs_set_blk_dev(argv[1], argv[2], fstype))
727 if (fs_size(argv[3], &size) < 0)
728 return CMD_RET_FAILURE;
730 env_set_hex("filesize", size);
735 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
739 const char *addr_str;
740 const char *filename;
749 return CMD_RET_USAGE;
751 return CMD_RET_USAGE;
753 if (fs_set_blk_dev(argv[1], cmd_arg2(argc, argv), fstype)) {
754 log_err("Can't set block device\n");
759 addr = hextoul(argv[3], &ep);
760 if (ep == argv[3] || *ep != '\0')
761 return CMD_RET_USAGE;
763 addr_str = env_get("loadaddr");
764 if (addr_str != NULL)
765 addr = hextoul(addr_str, NULL);
767 addr = CONFIG_SYS_LOAD_ADDR;
772 filename = env_get("bootfile");
774 puts("** No boot file defined **\n");
779 bytes = hextoul(argv[5], NULL);
783 pos = hextoul(argv[6], NULL);
788 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
789 time = get_timer(time);
791 log_err("Failed to load '%s'\n", filename);
795 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
796 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
799 printf("%llu bytes read in %lu ms", len_read, time);
802 print_size(div_u64(len_read, time) * 1000, "/s");
807 env_set_hex("fileaddr", addr);
808 env_set_hex("filesize", len_read);
813 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
817 return CMD_RET_USAGE;
819 return CMD_RET_USAGE;
821 if (fs_set_blk_dev(argv[1], cmd_arg2(argc, argv), fstype))
824 if (fs_ls(argc >= 4 ? argv[3] : "/"))
830 int file_exists(const char *dev_type, const char *dev_part, const char *file,
833 if (fs_set_blk_dev(dev_type, dev_part, fstype))
836 return fs_exists(file);
839 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
843 const char *filename;
850 if (argc < 6 || argc > 7)
851 return CMD_RET_USAGE;
853 if (fs_set_blk_dev(argv[1], argv[2], fstype))
856 addr = hextoul(argv[3], NULL);
858 bytes = hextoul(argv[5], NULL);
860 pos = hextoul(argv[6], NULL);
865 ret = fs_write(filename, addr, pos, bytes, &len);
866 time = get_timer(time);
870 printf("%llu bytes written in %lu ms", len, time);
873 print_size(div_u64(len, time) * 1000, "/s");
881 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
886 memset(uuid, 0, sizeof(uuid));
888 if (argc < 3 || argc > 4)
889 return CMD_RET_USAGE;
891 if (fs_set_blk_dev(argv[1], argv[2], fstype))
896 return CMD_RET_FAILURE;
899 env_set(argv[3], uuid);
901 printf("%s\n", uuid);
903 return CMD_RET_SUCCESS;
906 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
908 struct fstype_info *info;
910 if (argc < 3 || argc > 4)
911 return CMD_RET_USAGE;
913 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
916 info = fs_get_info(fs_type);
919 env_set(argv[3], info->name);
921 printf("%s\n", info->name);
925 return CMD_RET_SUCCESS;
928 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
932 return CMD_RET_USAGE;
934 if (fs_set_blk_dev(argv[1], argv[2], fstype))
937 if (fs_unlink(argv[3]))
943 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
949 return CMD_RET_USAGE;
951 if (fs_set_blk_dev(argv[1], argv[2], fstype))
954 ret = fs_mkdir(argv[3]);
956 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
963 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
967 return CMD_RET_USAGE;
969 if (fs_set_blk_dev(argv[1], argv[2], fstype))
972 if (fs_ln(argv[3], argv[4]))
978 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
980 struct fstype_info *drv = fstypes;
981 const int n_ents = ARRAY_SIZE(fstypes);
982 struct fstype_info *entry;
985 puts("Supported filesystems");
986 for (entry = drv; entry != drv + n_ents; entry++) {
987 if (entry->fstype != FS_TYPE_ANY) {
988 printf("%c %s", i ? ',' : ':', entry->name);
995 return CMD_RET_SUCCESS;
998 int fs_read_alloc(const char *fname, ulong size, uint align, void **bufp)
1006 align = ARCH_DMA_MINALIGN;
1008 buf = memalign(align, size + 1);
1010 return log_msg_ret("buf", -ENOMEM);
1011 addr = map_to_sysmem(buf);
1013 ret = fs_read(fname, addr, 0, size, &bytes_read);
1016 return log_msg_ret("read", ret);
1018 if (size != bytes_read)
1019 return log_msg_ret("bread", -EIO);
1027 int fs_load_alloc(const char *ifname, const char *dev_part_str,
1028 const char *fname, ulong max_size, ulong align, void **bufp,
1035 if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY))
1036 return log_msg_ret("set", -ENOMEDIUM);
1038 ret = fs_size(fname, &size);
1040 return log_msg_ret("sz", -ENOENT);
1042 if (size >= (max_size ?: SZ_1G))
1043 return log_msg_ret("sz", -E2BIG);
1045 if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY))
1046 return log_msg_ret("set", -ENOMEDIUM);
1048 ret = fs_read_alloc(fname, size, align, &buf);
1050 return log_msg_ret("al", ret);