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 <semihostingfs.h>
22 #include <ubifs_uboot.h>
24 #include <asm/global_data.h>
27 #include <linux/math64.h>
28 #include <efi_loader.h>
32 DECLARE_GLOBAL_DATA_PTR;
34 static struct blk_desc *fs_dev_desc;
35 static int fs_dev_part;
36 static struct disk_partition fs_partition;
37 static int fs_type = FS_TYPE_ANY;
39 void fs_set_type(int type)
44 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
45 struct disk_partition *fs_partition)
47 log_debug("Unrecognized filesystem type\n");
51 static inline int fs_ls_unsupported(const char *dirname)
56 /* generic implementation of ls in terms of opendir/readdir/closedir */
58 static int fs_ls_generic(const char *dirname)
60 struct fs_dir_stream *dirs;
61 struct fs_dirent *dent;
62 int nfiles = 0, ndirs = 0;
64 dirs = fs_opendir(dirname);
68 while ((dent = fs_readdir(dirs))) {
69 if (dent->type == FS_DT_DIR) {
70 printf(" %s/\n", dent->name);
72 } else if (dent->type == FS_DT_LNK) {
73 printf(" <SYM> %s\n", dent->name);
76 printf(" %8lld %s\n", dent->size, dent->name);
83 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
88 static inline int fs_exists_unsupported(const char *filename)
93 static inline int fs_size_unsupported(const char *filename, loff_t *size)
98 static inline int fs_read_unsupported(const char *filename, void *buf,
99 loff_t offset, loff_t len,
105 static inline int fs_write_unsupported(const char *filename, void *buf,
106 loff_t offset, loff_t len,
112 static inline int fs_ln_unsupported(const char *filename, const char *target)
117 static inline void fs_close_unsupported(void)
121 static inline int fs_uuid_unsupported(char *uuid_str)
126 static inline int fs_opendir_unsupported(const char *filename,
127 struct fs_dir_stream **dirs)
132 static inline int fs_unlink_unsupported(const char *filename)
137 static inline int fs_mkdir_unsupported(const char *dirname)
146 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
147 * should be false in most cases. For "virtual" filesystems which
148 * aren't based on a U-Boot block device (e.g. sandbox), this can be
149 * set to true. This should also be true for the dummy entry at the end
150 * of fstypes[], since that is essentially a "virtual" (non-existent)
153 bool null_dev_desc_ok;
154 int (*probe)(struct blk_desc *fs_dev_desc,
155 struct disk_partition *fs_partition);
156 int (*ls)(const char *dirname);
157 int (*exists)(const char *filename);
158 int (*size)(const char *filename, loff_t *size);
159 int (*read)(const char *filename, void *buf, loff_t offset,
160 loff_t len, loff_t *actread);
161 int (*write)(const char *filename, void *buf, loff_t offset,
162 loff_t len, loff_t *actwrite);
164 int (*uuid)(char *uuid_str);
166 * Open a directory stream. On success return 0 and directory
167 * stream pointer via 'dirsp'. On error, return -errno. See
170 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
172 * Read next entry from directory stream. On success return 0
173 * and directory entry pointer via 'dentp'. On error return
174 * -errno. See fs_readdir().
176 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
177 /* see fs_closedir() */
178 void (*closedir)(struct fs_dir_stream *dirs);
179 int (*unlink)(const char *filename);
180 int (*mkdir)(const char *dirname);
181 int (*ln)(const char *filename, const char *target);
184 static struct fstype_info fstypes[] = {
185 #if CONFIG_IS_ENABLED(FS_FAT)
187 .fstype = FS_TYPE_FAT,
189 .null_dev_desc_ok = false,
190 .probe = fat_set_blk_dev,
193 .exists = fat_exists,
195 .read = fat_read_file,
196 #if CONFIG_IS_ENABLED(FAT_WRITE)
197 .write = file_fat_write,
198 .unlink = fat_unlink,
201 .write = fs_write_unsupported,
202 .unlink = fs_unlink_unsupported,
203 .mkdir = fs_mkdir_unsupported,
206 .opendir = fat_opendir,
207 .readdir = fat_readdir,
208 .closedir = fat_closedir,
209 .ln = fs_ln_unsupported,
213 #if CONFIG_IS_ENABLED(FS_EXT4)
215 .fstype = FS_TYPE_EXT,
217 .null_dev_desc_ok = false,
218 .probe = ext4fs_probe,
219 .close = ext4fs_close,
221 .exists = ext4fs_exists,
223 .read = ext4_read_file,
224 #ifdef CONFIG_CMD_EXT4_WRITE
225 .write = ext4_write_file,
226 .ln = ext4fs_create_link,
228 .write = fs_write_unsupported,
229 .ln = fs_ln_unsupported,
232 .opendir = fs_opendir_unsupported,
233 .unlink = fs_unlink_unsupported,
234 .mkdir = fs_mkdir_unsupported,
237 #ifdef CONFIG_SANDBOX
239 .fstype = FS_TYPE_SANDBOX,
241 .null_dev_desc_ok = true,
242 .probe = sandbox_fs_set_blk_dev,
243 .close = sandbox_fs_close,
245 .exists = sandbox_fs_exists,
246 .size = sandbox_fs_size,
247 .read = fs_read_sandbox,
248 .write = fs_write_sandbox,
249 .uuid = fs_uuid_unsupported,
250 .opendir = fs_opendir_unsupported,
251 .unlink = fs_unlink_unsupported,
252 .mkdir = fs_mkdir_unsupported,
253 .ln = fs_ln_unsupported,
256 #ifdef CONFIG_SEMIHOSTING
258 .fstype = FS_TYPE_SEMIHOSTING,
259 .name = "semihosting",
260 .null_dev_desc_ok = true,
261 .probe = smh_fs_set_blk_dev,
262 .close = fs_close_unsupported,
263 .ls = fs_ls_unsupported,
264 .exists = fs_exists_unsupported,
267 .write = smh_fs_write,
268 .uuid = fs_uuid_unsupported,
269 .opendir = fs_opendir_unsupported,
270 .unlink = fs_unlink_unsupported,
271 .mkdir = fs_mkdir_unsupported,
272 .ln = fs_ln_unsupported,
275 #ifndef CONFIG_SPL_BUILD
276 #ifdef CONFIG_CMD_UBIFS
278 .fstype = FS_TYPE_UBIFS,
280 .null_dev_desc_ok = true,
281 .probe = ubifs_set_blk_dev,
282 .close = ubifs_close,
284 .exists = ubifs_exists,
287 .write = fs_write_unsupported,
288 .uuid = fs_uuid_unsupported,
289 .opendir = fs_opendir_unsupported,
290 .unlink = fs_unlink_unsupported,
291 .mkdir = fs_mkdir_unsupported,
292 .ln = fs_ln_unsupported,
296 #ifndef CONFIG_SPL_BUILD
297 #ifdef CONFIG_FS_BTRFS
299 .fstype = FS_TYPE_BTRFS,
301 .null_dev_desc_ok = false,
302 .probe = btrfs_probe,
303 .close = btrfs_close,
305 .exists = btrfs_exists,
308 .write = fs_write_unsupported,
310 .opendir = fs_opendir_unsupported,
311 .unlink = fs_unlink_unsupported,
312 .mkdir = fs_mkdir_unsupported,
313 .ln = fs_ln_unsupported,
317 #if CONFIG_IS_ENABLED(FS_SQUASHFS)
319 .fstype = FS_TYPE_SQUASHFS,
321 .null_dev_desc_ok = false,
323 .opendir = sqfs_opendir,
324 .readdir = sqfs_readdir,
329 .closedir = sqfs_closedir,
330 .exists = sqfs_exists,
331 .uuid = fs_uuid_unsupported,
332 .write = fs_write_unsupported,
333 .ln = fs_ln_unsupported,
334 .unlink = fs_unlink_unsupported,
335 .mkdir = fs_mkdir_unsupported,
338 #if IS_ENABLED(CONFIG_FS_EROFS)
340 .fstype = FS_TYPE_EROFS,
342 .null_dev_desc_ok = false,
343 .probe = erofs_probe,
344 .opendir = erofs_opendir,
345 .readdir = erofs_readdir,
349 .close = erofs_close,
350 .closedir = erofs_closedir,
351 .exists = erofs_exists,
352 .uuid = fs_uuid_unsupported,
353 .write = fs_write_unsupported,
354 .ln = fs_ln_unsupported,
355 .unlink = fs_unlink_unsupported,
356 .mkdir = fs_mkdir_unsupported,
360 .fstype = FS_TYPE_ANY,
361 .name = "unsupported",
362 .null_dev_desc_ok = true,
363 .probe = fs_probe_unsupported,
364 .close = fs_close_unsupported,
365 .ls = fs_ls_unsupported,
366 .exists = fs_exists_unsupported,
367 .size = fs_size_unsupported,
368 .read = fs_read_unsupported,
369 .write = fs_write_unsupported,
370 .uuid = fs_uuid_unsupported,
371 .opendir = fs_opendir_unsupported,
372 .unlink = fs_unlink_unsupported,
373 .mkdir = fs_mkdir_unsupported,
374 .ln = fs_ln_unsupported,
378 static struct fstype_info *fs_get_info(int fstype)
380 struct fstype_info *info;
383 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
384 if (fstype == info->fstype)
388 /* Return the 'unsupported' sentinel */
393 * fs_get_type() - Get type of current filesystem
395 * Return: filesystem type
397 * Returns filesystem type representing the current filesystem, or
398 * FS_TYPE_ANY for any unrecognised filesystem.
400 int fs_get_type(void)
406 * fs_get_type_name() - Get type of current filesystem
408 * Return: Pointer to filesystem name
410 * Returns a string describing the current filesystem, or the sentinel
411 * "unsupported" for any unrecognised filesystem.
413 const char *fs_get_type_name(void)
415 return fs_get_info(fs_type)->name;
418 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
420 struct fstype_info *info;
422 #ifdef CONFIG_NEEDS_MANUAL_RELOC
423 static int relocated;
426 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
428 info->name += gd->reloc_off;
429 info->probe += gd->reloc_off;
430 info->close += gd->reloc_off;
431 info->ls += gd->reloc_off;
432 info->read += gd->reloc_off;
433 info->write += gd->reloc_off;
439 part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc,
444 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
445 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
446 fstype != info->fstype)
449 if (!fs_dev_desc && !info->null_dev_desc_ok)
452 if (!info->probe(fs_dev_desc, &fs_partition)) {
453 fs_type = info->fstype;
462 /* set current blk device w/ blk_desc + partition # */
463 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
465 struct fstype_info *info;
469 ret = part_get_info(desc, part, &fs_partition);
471 ret = part_get_info_whole_disk(desc, &fs_partition);
476 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
477 if (!info->probe(fs_dev_desc, &fs_partition)) {
478 fs_type = info->fstype;
489 struct fstype_info *info = fs_get_info(fs_type);
493 fs_type = FS_TYPE_ANY;
496 int fs_uuid(char *uuid_str)
498 struct fstype_info *info = fs_get_info(fs_type);
500 return info->uuid(uuid_str);
503 int fs_ls(const char *dirname)
507 struct fstype_info *info = fs_get_info(fs_type);
509 ret = info->ls(dirname);
516 int fs_exists(const char *filename)
520 struct fstype_info *info = fs_get_info(fs_type);
522 ret = info->exists(filename);
529 int fs_size(const char *filename, loff_t *size)
533 struct fstype_info *info = fs_get_info(fs_type);
535 ret = info->size(filename, size);
543 /* Check if a file may be read to the given address */
544 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
545 loff_t len, struct fstype_info *info)
552 /* get the actual size of the file */
553 ret = info->size(filename, &size);
556 if (offset >= size) {
557 /* offset >= EOF, no bytes will be written */
560 read_len = size - offset;
562 /* limit to 'len' if it is smaller */
563 if (len && len < read_len)
566 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
569 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
572 log_err("** Reading file would overwrite reserved memory **\n");
577 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
578 int do_lmb_check, loff_t *actread)
580 struct fstype_info *info = fs_get_info(fs_type);
586 ret = fs_read_lmb_check(filename, addr, offset, len, info);
593 * We don't actually know how many bytes are being read, since len==0
594 * means read the whole file.
596 buf = map_sysmem(addr, len);
597 ret = info->read(filename, buf, offset, len, actread);
600 /* If we requested a specific number of bytes, check we got it */
601 if (ret == 0 && len && *actread != len)
602 log_debug("** %s shorter than offset + len **\n", filename);
608 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
611 return _fs_read(filename, addr, offset, len, 0, actread);
614 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
617 struct fstype_info *info = fs_get_info(fs_type);
621 buf = map_sysmem(addr, len);
622 ret = info->write(filename, buf, offset, len, actwrite);
625 if (ret < 0 && len != *actwrite) {
626 log_err("** Unable to write file %s **\n", filename);
634 struct fs_dir_stream *fs_opendir(const char *filename)
636 struct fstype_info *info = fs_get_info(fs_type);
637 struct fs_dir_stream *dirs = NULL;
640 ret = info->opendir(filename, &dirs);
647 dirs->desc = fs_dev_desc;
648 dirs->part = fs_dev_part;
653 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
655 struct fstype_info *info;
656 struct fs_dirent *dirent;
659 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
660 info = fs_get_info(fs_type);
662 ret = info->readdir(dirs, &dirent);
672 void fs_closedir(struct fs_dir_stream *dirs)
674 struct fstype_info *info;
679 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
680 info = fs_get_info(fs_type);
682 info->closedir(dirs);
686 int fs_unlink(const char *filename)
690 struct fstype_info *info = fs_get_info(fs_type);
692 ret = info->unlink(filename);
699 int fs_mkdir(const char *dirname)
703 struct fstype_info *info = fs_get_info(fs_type);
705 ret = info->mkdir(dirname);
712 int fs_ln(const char *fname, const char *target)
714 struct fstype_info *info = fs_get_info(fs_type);
717 ret = info->ln(fname, target);
720 log_err("** Unable to create link %s -> %s **\n", fname, target);
728 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
734 return CMD_RET_USAGE;
736 if (fs_set_blk_dev(argv[1], argv[2], fstype))
739 if (fs_size(argv[3], &size) < 0)
740 return CMD_RET_FAILURE;
742 env_set_hex("filesize", size);
747 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
751 const char *addr_str;
752 const char *filename;
761 return CMD_RET_USAGE;
763 return CMD_RET_USAGE;
765 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) {
766 log_err("Can't set block device\n");
771 addr = hextoul(argv[3], &ep);
772 if (ep == argv[3] || *ep != '\0')
773 return CMD_RET_USAGE;
775 addr_str = env_get("loadaddr");
776 if (addr_str != NULL)
777 addr = hextoul(addr_str, NULL);
779 addr = CONFIG_SYS_LOAD_ADDR;
784 filename = env_get("bootfile");
786 puts("** No boot file defined **\n");
791 bytes = hextoul(argv[5], NULL);
795 pos = hextoul(argv[6], NULL);
800 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
801 time = get_timer(time);
803 log_err("Failed to load '%s'\n", filename);
807 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
808 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
809 (argc > 4) ? argv[4] : "", map_sysmem(addr, 0),
812 printf("%llu bytes read in %lu ms", len_read, time);
815 print_size(div_u64(len_read, time) * 1000, "/s");
820 env_set_hex("fileaddr", addr);
821 env_set_hex("filesize", len_read);
826 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
830 return CMD_RET_USAGE;
832 return CMD_RET_USAGE;
834 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
837 if (fs_ls(argc >= 4 ? argv[3] : "/"))
843 int file_exists(const char *dev_type, const char *dev_part, const char *file,
846 if (fs_set_blk_dev(dev_type, dev_part, fstype))
849 return fs_exists(file);
852 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
856 const char *filename;
863 if (argc < 6 || argc > 7)
864 return CMD_RET_USAGE;
866 if (fs_set_blk_dev(argv[1], argv[2], fstype))
869 addr = hextoul(argv[3], NULL);
871 bytes = hextoul(argv[5], NULL);
873 pos = hextoul(argv[6], NULL);
878 ret = fs_write(filename, addr, pos, bytes, &len);
879 time = get_timer(time);
883 printf("%llu bytes written in %lu ms", len, time);
886 print_size(div_u64(len, time) * 1000, "/s");
894 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
899 memset(uuid, 0, sizeof(uuid));
901 if (argc < 3 || argc > 4)
902 return CMD_RET_USAGE;
904 if (fs_set_blk_dev(argv[1], argv[2], fstype))
909 return CMD_RET_FAILURE;
912 env_set(argv[3], uuid);
914 printf("%s\n", uuid);
916 return CMD_RET_SUCCESS;
919 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
921 struct fstype_info *info;
923 if (argc < 3 || argc > 4)
924 return CMD_RET_USAGE;
926 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
929 info = fs_get_info(fs_type);
932 env_set(argv[3], info->name);
934 printf("%s\n", info->name);
938 return CMD_RET_SUCCESS;
941 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
945 return CMD_RET_USAGE;
947 if (fs_set_blk_dev(argv[1], argv[2], fstype))
950 if (fs_unlink(argv[3]))
956 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
962 return CMD_RET_USAGE;
964 if (fs_set_blk_dev(argv[1], argv[2], fstype))
967 ret = fs_mkdir(argv[3]);
969 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
976 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
980 return CMD_RET_USAGE;
982 if (fs_set_blk_dev(argv[1], argv[2], fstype))
985 if (fs_ln(argv[3], argv[4]))
991 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
993 struct fstype_info *drv = fstypes;
994 const int n_ents = ARRAY_SIZE(fstypes);
995 struct fstype_info *entry;
998 puts("Supported filesystems");
999 for (entry = drv; entry != drv + n_ents; entry++) {
1000 if (entry->fstype != FS_TYPE_ANY) {
1001 printf("%c %s", i ? ',' : ':', entry->name);
1008 return CMD_RET_SUCCESS;