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>
23 #include <ubifs_uboot.h>
25 #include <asm/global_data.h>
28 #include <linux/math64.h>
29 #include <efi_loader.h>
33 DECLARE_GLOBAL_DATA_PTR;
35 static struct blk_desc *fs_dev_desc;
36 static int fs_dev_part;
37 static struct disk_partition fs_partition;
38 static int fs_type = FS_TYPE_ANY;
40 void fs_set_type(int type)
45 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
46 struct disk_partition *fs_partition)
48 log_debug("Unrecognized filesystem type\n");
52 static inline int fs_ls_unsupported(const char *dirname)
57 /* generic implementation of ls in terms of opendir/readdir/closedir */
59 static int fs_ls_generic(const char *dirname)
61 struct fs_dir_stream *dirs;
62 struct fs_dirent *dent;
63 int nfiles = 0, ndirs = 0;
65 dirs = fs_opendir(dirname);
69 while ((dent = fs_readdir(dirs))) {
70 if (dent->type == FS_DT_DIR) {
71 printf(" %s/\n", dent->name);
73 } else if (dent->type == FS_DT_LNK) {
74 printf(" <SYM> %s\n", dent->name);
77 printf(" %8lld %s\n", dent->size, dent->name);
84 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
89 static inline int fs_exists_unsupported(const char *filename)
94 static inline int fs_size_unsupported(const char *filename, loff_t *size)
99 static inline int fs_read_unsupported(const char *filename, void *buf,
100 loff_t offset, loff_t len,
106 static inline int fs_write_unsupported(const char *filename, void *buf,
107 loff_t offset, loff_t len,
113 static inline int fs_ln_unsupported(const char *filename, const char *target)
118 static inline void fs_close_unsupported(void)
122 static inline int fs_uuid_unsupported(char *uuid_str)
127 static inline int fs_opendir_unsupported(const char *filename,
128 struct fs_dir_stream **dirs)
133 static inline int fs_unlink_unsupported(const char *filename)
138 static inline int fs_mkdir_unsupported(const char *dirname)
147 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
148 * should be false in most cases. For "virtual" filesystems which
149 * aren't based on a U-Boot block device (e.g. sandbox), this can be
150 * set to true. This should also be true for the dummy entry at the end
151 * of fstypes[], since that is essentially a "virtual" (non-existent)
154 bool null_dev_desc_ok;
155 int (*probe)(struct blk_desc *fs_dev_desc,
156 struct disk_partition *fs_partition);
157 int (*ls)(const char *dirname);
158 int (*exists)(const char *filename);
159 int (*size)(const char *filename, loff_t *size);
160 int (*read)(const char *filename, void *buf, loff_t offset,
161 loff_t len, loff_t *actread);
162 int (*write)(const char *filename, void *buf, loff_t offset,
163 loff_t len, loff_t *actwrite);
165 int (*uuid)(char *uuid_str);
167 * Open a directory stream. On success return 0 and directory
168 * stream pointer via 'dirsp'. On error, return -errno. See
171 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
173 * Read next entry from directory stream. On success return 0
174 * and directory entry pointer via 'dentp'. On error return
175 * -errno. See fs_readdir().
177 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
178 /* see fs_closedir() */
179 void (*closedir)(struct fs_dir_stream *dirs);
180 int (*unlink)(const char *filename);
181 int (*mkdir)(const char *dirname);
182 int (*ln)(const char *filename, const char *target);
185 static struct fstype_info fstypes[] = {
186 #if CONFIG_IS_ENABLED(FS_FAT)
188 .fstype = FS_TYPE_FAT,
190 .null_dev_desc_ok = false,
191 .probe = fat_set_blk_dev,
194 .exists = fat_exists,
196 .read = fat_read_file,
197 #if CONFIG_IS_ENABLED(FAT_WRITE)
198 .write = file_fat_write,
199 .unlink = fat_unlink,
202 .write = fs_write_unsupported,
203 .unlink = fs_unlink_unsupported,
204 .mkdir = fs_mkdir_unsupported,
207 .opendir = fat_opendir,
208 .readdir = fat_readdir,
209 .closedir = fat_closedir,
210 .ln = fs_ln_unsupported,
214 #if CONFIG_IS_ENABLED(FS_EXT4)
216 .fstype = FS_TYPE_EXT,
218 .null_dev_desc_ok = false,
219 .probe = ext4fs_probe,
220 .close = ext4fs_close,
222 .exists = ext4fs_exists,
224 .read = ext4_read_file,
225 #ifdef CONFIG_CMD_EXT4_WRITE
226 .write = ext4_write_file,
227 .ln = ext4fs_create_link,
229 .write = fs_write_unsupported,
230 .ln = fs_ln_unsupported,
233 .opendir = fs_opendir_unsupported,
234 .unlink = fs_unlink_unsupported,
235 .mkdir = fs_mkdir_unsupported,
238 #ifdef CONFIG_SANDBOX
240 .fstype = FS_TYPE_SANDBOX,
242 .null_dev_desc_ok = true,
243 .probe = sandbox_fs_set_blk_dev,
244 .close = sandbox_fs_close,
246 .exists = sandbox_fs_exists,
247 .size = sandbox_fs_size,
248 .read = fs_read_sandbox,
249 .write = fs_write_sandbox,
250 .uuid = fs_uuid_unsupported,
251 .opendir = fs_opendir_unsupported,
252 .unlink = fs_unlink_unsupported,
253 .mkdir = fs_mkdir_unsupported,
254 .ln = fs_ln_unsupported,
257 #ifdef CONFIG_SEMIHOSTING
259 .fstype = FS_TYPE_SEMIHOSTING,
260 .name = "semihosting",
261 .null_dev_desc_ok = true,
262 .probe = smh_fs_set_blk_dev,
263 .close = fs_close_unsupported,
264 .ls = fs_ls_unsupported,
265 .exists = fs_exists_unsupported,
268 .write = smh_fs_write,
269 .uuid = fs_uuid_unsupported,
270 .opendir = fs_opendir_unsupported,
271 .unlink = fs_unlink_unsupported,
272 .mkdir = fs_mkdir_unsupported,
273 .ln = fs_ln_unsupported,
276 #ifndef CONFIG_SPL_BUILD
277 #ifdef CONFIG_CMD_UBIFS
279 .fstype = FS_TYPE_UBIFS,
281 .null_dev_desc_ok = true,
282 .probe = ubifs_set_blk_dev,
283 .close = ubifs_close,
285 .exists = ubifs_exists,
288 .write = fs_write_unsupported,
289 .uuid = fs_uuid_unsupported,
290 .opendir = fs_opendir_unsupported,
291 .unlink = fs_unlink_unsupported,
292 .mkdir = fs_mkdir_unsupported,
293 .ln = fs_ln_unsupported,
297 #ifndef CONFIG_SPL_BUILD
298 #ifdef CONFIG_FS_BTRFS
300 .fstype = FS_TYPE_BTRFS,
302 .null_dev_desc_ok = false,
303 .probe = btrfs_probe,
304 .close = btrfs_close,
306 .exists = btrfs_exists,
309 .write = fs_write_unsupported,
311 .opendir = fs_opendir_unsupported,
312 .unlink = fs_unlink_unsupported,
313 .mkdir = fs_mkdir_unsupported,
314 .ln = fs_ln_unsupported,
318 #if CONFIG_IS_ENABLED(FS_SQUASHFS)
320 .fstype = FS_TYPE_SQUASHFS,
322 .null_dev_desc_ok = false,
324 .opendir = sqfs_opendir,
325 .readdir = sqfs_readdir,
330 .closedir = sqfs_closedir,
331 .exists = sqfs_exists,
332 .uuid = fs_uuid_unsupported,
333 .write = fs_write_unsupported,
334 .ln = fs_ln_unsupported,
335 .unlink = fs_unlink_unsupported,
336 .mkdir = fs_mkdir_unsupported,
339 #if IS_ENABLED(CONFIG_FS_EROFS)
341 .fstype = FS_TYPE_EROFS,
343 .null_dev_desc_ok = false,
344 .probe = erofs_probe,
345 .opendir = erofs_opendir,
346 .readdir = erofs_readdir,
350 .close = erofs_close,
351 .closedir = erofs_closedir,
352 .exists = erofs_exists,
353 .uuid = fs_uuid_unsupported,
354 .write = fs_write_unsupported,
355 .ln = fs_ln_unsupported,
356 .unlink = fs_unlink_unsupported,
357 .mkdir = fs_mkdir_unsupported,
361 .fstype = FS_TYPE_ANY,
362 .name = "unsupported",
363 .null_dev_desc_ok = true,
364 .probe = fs_probe_unsupported,
365 .close = fs_close_unsupported,
366 .ls = fs_ls_unsupported,
367 .exists = fs_exists_unsupported,
368 .size = fs_size_unsupported,
369 .read = fs_read_unsupported,
370 .write = fs_write_unsupported,
371 .uuid = fs_uuid_unsupported,
372 .opendir = fs_opendir_unsupported,
373 .unlink = fs_unlink_unsupported,
374 .mkdir = fs_mkdir_unsupported,
375 .ln = fs_ln_unsupported,
379 static struct fstype_info *fs_get_info(int fstype)
381 struct fstype_info *info;
384 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
385 if (fstype == info->fstype)
389 /* Return the 'unsupported' sentinel */
394 * fs_get_type() - Get type of current filesystem
396 * Return: filesystem type
398 * Returns filesystem type representing the current filesystem, or
399 * FS_TYPE_ANY for any unrecognised filesystem.
401 int fs_get_type(void)
407 * fs_get_type_name() - Get type of current filesystem
409 * Return: Pointer to filesystem name
411 * Returns a string describing the current filesystem, or the sentinel
412 * "unsupported" for any unrecognised filesystem.
414 const char *fs_get_type_name(void)
416 return fs_get_info(fs_type)->name;
419 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
421 struct fstype_info *info;
423 #ifdef CONFIG_NEEDS_MANUAL_RELOC
424 static int relocated;
427 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
429 info->name += gd->reloc_off;
430 info->probe += gd->reloc_off;
431 info->close += gd->reloc_off;
432 info->ls += gd->reloc_off;
433 info->read += gd->reloc_off;
434 info->write += gd->reloc_off;
440 part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
445 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
446 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
447 fstype != info->fstype)
450 if (!fs_dev_desc && !info->null_dev_desc_ok)
453 if (!info->probe(fs_dev_desc, &fs_partition)) {
454 fs_type = info->fstype;
463 /* set current blk device w/ blk_desc + partition # */
464 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
466 struct fstype_info *info;
470 ret = part_get_info(desc, part, &fs_partition);
472 ret = part_get_info_whole_disk(desc, &fs_partition);
477 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
478 if (!info->probe(fs_dev_desc, &fs_partition)) {
479 fs_type = info->fstype;
490 struct fstype_info *info = fs_get_info(fs_type);
494 fs_type = FS_TYPE_ANY;
497 int fs_uuid(char *uuid_str)
499 struct fstype_info *info = fs_get_info(fs_type);
501 return info->uuid(uuid_str);
504 int fs_ls(const char *dirname)
508 struct fstype_info *info = fs_get_info(fs_type);
510 ret = info->ls(dirname);
517 int fs_exists(const char *filename)
521 struct fstype_info *info = fs_get_info(fs_type);
523 ret = info->exists(filename);
530 int fs_size(const char *filename, loff_t *size)
534 struct fstype_info *info = fs_get_info(fs_type);
536 ret = info->size(filename, size);
544 /* Check if a file may be read to the given address */
545 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
546 loff_t len, struct fstype_info *info)
553 /* get the actual size of the file */
554 ret = info->size(filename, &size);
557 if (offset >= size) {
558 /* offset >= EOF, no bytes will be written */
561 read_len = size - offset;
563 /* limit to 'len' if it is smaller */
564 if (len && len < read_len)
567 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
570 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
573 log_err("** Reading file would overwrite reserved memory **\n");
578 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
579 int do_lmb_check, loff_t *actread)
581 struct fstype_info *info = fs_get_info(fs_type);
587 ret = fs_read_lmb_check(filename, addr, offset, len, info);
594 * We don't actually know how many bytes are being read, since len==0
595 * means read the whole file.
597 buf = map_sysmem(addr, len);
598 ret = info->read(filename, buf, offset, len, actread);
601 /* If we requested a specific number of bytes, check we got it */
602 if (ret == 0 && len && *actread != len)
603 log_debug("** %s shorter than offset + len **\n", filename);
609 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
612 return _fs_read(filename, addr, offset, len, 0, actread);
615 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
618 struct fstype_info *info = fs_get_info(fs_type);
622 buf = map_sysmem(addr, len);
623 ret = info->write(filename, buf, offset, len, actwrite);
626 if (ret < 0 && len != *actwrite) {
627 log_err("** Unable to write file %s **\n", filename);
635 struct fs_dir_stream *fs_opendir(const char *filename)
637 struct fstype_info *info = fs_get_info(fs_type);
638 struct fs_dir_stream *dirs = NULL;
641 ret = info->opendir(filename, &dirs);
648 dirs->desc = fs_dev_desc;
649 dirs->part = fs_dev_part;
654 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
656 struct fstype_info *info;
657 struct fs_dirent *dirent;
660 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
661 info = fs_get_info(fs_type);
663 ret = info->readdir(dirs, &dirent);
673 void fs_closedir(struct fs_dir_stream *dirs)
675 struct fstype_info *info;
680 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
681 info = fs_get_info(fs_type);
683 info->closedir(dirs);
687 int fs_unlink(const char *filename)
691 struct fstype_info *info = fs_get_info(fs_type);
693 ret = info->unlink(filename);
700 int fs_mkdir(const char *dirname)
704 struct fstype_info *info = fs_get_info(fs_type);
706 ret = info->mkdir(dirname);
713 int fs_ln(const char *fname, const char *target)
715 struct fstype_info *info = fs_get_info(fs_type);
718 ret = info->ln(fname, target);
721 log_err("** Unable to create link %s -> %s **\n", fname, target);
729 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
735 return CMD_RET_USAGE;
737 if (fs_set_blk_dev(argv[1], argv[2], fstype))
740 if (fs_size(argv[3], &size) < 0)
741 return CMD_RET_FAILURE;
743 env_set_hex("filesize", size);
748 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
752 const char *addr_str;
753 const char *filename;
762 return CMD_RET_USAGE;
764 return CMD_RET_USAGE;
766 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
767 log_err("Can't set block device\n");
772 addr = hextoul(argv[3], &ep);
773 if (ep == argv[3] || *ep != '\0')
774 return CMD_RET_USAGE;
776 addr_str = env_get("loadaddr");
777 if (addr_str != NULL)
778 addr = hextoul(addr_str, NULL);
780 addr = CONFIG_SYS_LOAD_ADDR;
785 filename = env_get("bootfile");
787 puts("** No boot file defined **\n");
792 bytes = hextoul(argv[5], NULL);
796 pos = hextoul(argv[6], NULL);
801 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
802 time = get_timer(time);
804 log_err("Failed to load '%s'\n", filename);
808 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
809 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
810 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
813 printf("%llu bytes read in %lu ms", len_read, time);
816 print_size(div_u64(len_read, time) * 1000, "/s");
821 env_set_hex("fileaddr", addr);
822 env_set_hex("filesize", len_read);
827 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
831 return CMD_RET_USAGE;
833 return CMD_RET_USAGE;
835 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
838 if (fs_ls(argc >= 4 ? argv[3] : "/"))
844 int file_exists(const char *dev_type, const char *dev_part, const char *file,
847 if (fs_set_blk_dev(dev_type, dev_part, fstype))
850 return fs_exists(file);
853 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
857 const char *filename;
864 if (argc < 6 || argc > 7)
865 return CMD_RET_USAGE;
867 if (fs_set_blk_dev(argv[1], argv[2], fstype))
870 addr = hextoul(argv[3], NULL);
872 bytes = hextoul(argv[5], NULL);
874 pos = hextoul(argv[6], NULL);
879 ret = fs_write(filename, addr, pos, bytes, &len);
880 time = get_timer(time);
884 printf("%llu bytes written in %lu ms", len, time);
887 print_size(div_u64(len, time) * 1000, "/s");
895 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
900 memset(uuid, 0, sizeof(uuid));
902 if (argc < 3 || argc > 4)
903 return CMD_RET_USAGE;
905 if (fs_set_blk_dev(argv[1], argv[2], fstype))
910 return CMD_RET_FAILURE;
913 env_set(argv[3], uuid);
915 printf("%s\n", uuid);
917 return CMD_RET_SUCCESS;
920 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
922 struct fstype_info *info;
924 if (argc < 3 || argc > 4)
925 return CMD_RET_USAGE;
927 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
930 info = fs_get_info(fs_type);
933 env_set(argv[3], info->name);
935 printf("%s\n", info->name);
939 return CMD_RET_SUCCESS;
942 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
946 return CMD_RET_USAGE;
948 if (fs_set_blk_dev(argv[1], argv[2], fstype))
951 if (fs_unlink(argv[3]))
957 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
963 return CMD_RET_USAGE;
965 if (fs_set_blk_dev(argv[1], argv[2], fstype))
968 ret = fs_mkdir(argv[3]);
970 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
977 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
981 return CMD_RET_USAGE;
983 if (fs_set_blk_dev(argv[1], argv[2], fstype))
986 if (fs_ln(argv[3], argv[4]))
992 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
994 struct fstype_info *drv = fstypes;
995 const int n_ents = ARRAY_SIZE(fstypes);
996 struct fstype_info *entry;
999 puts("Supported filesystems");
1000 for (entry = drv; entry != drv + n_ents; entry++) {
1001 if (entry->fstype != FS_TYPE_ANY) {
1002 printf("%c %s", i ? ',' : ':', entry->name);
1009 return CMD_RET_SUCCESS;