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>
22 #include <sandboxfs.h>
23 #include <semihostingfs.h>
24 #include <ubifs_uboot.h>
26 #include <asm/global_data.h>
29 #include <linux/math64.h>
30 #include <linux/sizes.h>
31 #include <efi_loader.h>
35 DECLARE_GLOBAL_DATA_PTR
;
37 static struct blk_desc
*fs_dev_desc
;
38 static int fs_dev_part
;
39 static struct disk_partition fs_partition
;
40 static int fs_type
= FS_TYPE_ANY
;
42 void fs_set_type(int type
)
47 static inline int fs_probe_unsupported(struct blk_desc
*fs_dev_desc
,
48 struct disk_partition
*fs_partition
)
50 log_debug("Unrecognized filesystem type\n");
54 static inline int fs_ls_unsupported(const char *dirname
)
59 /* generic implementation of ls in terms of opendir/readdir/closedir */
61 static int fs_ls_generic(const char *dirname
)
63 struct fs_dir_stream
*dirs
;
64 struct fs_dirent
*dent
;
65 int nfiles
= 0, ndirs
= 0;
67 dirs
= fs_opendir(dirname
);
71 while ((dent
= fs_readdir(dirs
))) {
72 if (dent
->type
== FS_DT_DIR
) {
73 printf(" %s/\n", dent
->name
);
75 } else if (dent
->type
== FS_DT_LNK
) {
76 printf(" <SYM> %s\n", dent
->name
);
79 printf(" %8lld %s\n", dent
->size
, dent
->name
);
86 printf("\n%d file(s), %d dir(s)\n\n", nfiles
, ndirs
);
91 static inline int fs_exists_unsupported(const char *filename
)
96 static inline int fs_size_unsupported(const char *filename
, loff_t
*size
)
101 static inline int fs_read_unsupported(const char *filename
, void *buf
,
102 loff_t offset
, loff_t len
,
108 static inline int fs_write_unsupported(const char *filename
, void *buf
,
109 loff_t offset
, loff_t len
,
115 static inline int fs_ln_unsupported(const char *filename
, const char *target
)
120 static inline void fs_close_unsupported(void)
124 static inline int fs_uuid_unsupported(char *uuid_str
)
129 static inline int fs_opendir_unsupported(const char *filename
,
130 struct fs_dir_stream
**dirs
)
135 static inline int fs_unlink_unsupported(const char *filename
)
140 static inline int fs_mkdir_unsupported(const char *dirname
)
149 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
150 * should be false in most cases. For "virtual" filesystems which
151 * aren't based on a U-Boot block device (e.g. sandbox), this can be
152 * set to true. This should also be true for the dummy entry at the end
153 * of fstypes[], since that is essentially a "virtual" (non-existent)
156 bool null_dev_desc_ok
;
157 int (*probe
)(struct blk_desc
*fs_dev_desc
,
158 struct disk_partition
*fs_partition
);
159 int (*ls
)(const char *dirname
);
160 int (*exists
)(const char *filename
);
161 int (*size
)(const char *filename
, loff_t
*size
);
162 int (*read
)(const char *filename
, void *buf
, loff_t offset
,
163 loff_t len
, loff_t
*actread
);
164 int (*write
)(const char *filename
, void *buf
, loff_t offset
,
165 loff_t len
, loff_t
*actwrite
);
167 int (*uuid
)(char *uuid_str
);
169 * Open a directory stream. On success return 0 and directory
170 * stream pointer via 'dirsp'. On error, return -errno. See
173 int (*opendir
)(const char *filename
, struct fs_dir_stream
**dirsp
);
175 * Read next entry from directory stream. On success return 0
176 * and directory entry pointer via 'dentp'. On error return
177 * -errno. See fs_readdir().
179 int (*readdir
)(struct fs_dir_stream
*dirs
, struct fs_dirent
**dentp
);
180 /* see fs_closedir() */
181 void (*closedir
)(struct fs_dir_stream
*dirs
);
182 int (*unlink
)(const char *filename
);
183 int (*mkdir
)(const char *dirname
);
184 int (*ln
)(const char *filename
, const char *target
);
187 static struct fstype_info fstypes
[] = {
188 #if CONFIG_IS_ENABLED(FS_FAT)
190 .fstype
= FS_TYPE_FAT
,
192 .null_dev_desc_ok
= false,
193 .probe
= fat_set_blk_dev
,
196 .exists
= fat_exists
,
198 .read
= fat_read_file
,
199 #if CONFIG_IS_ENABLED(FAT_WRITE)
200 .write
= file_fat_write
,
201 .unlink
= fat_unlink
,
204 .write
= fs_write_unsupported
,
205 .unlink
= fs_unlink_unsupported
,
206 .mkdir
= fs_mkdir_unsupported
,
209 .opendir
= fat_opendir
,
210 .readdir
= fat_readdir
,
211 .closedir
= fat_closedir
,
212 .ln
= fs_ln_unsupported
,
216 #if CONFIG_IS_ENABLED(FS_EXT4)
218 .fstype
= FS_TYPE_EXT
,
220 .null_dev_desc_ok
= false,
221 .probe
= ext4fs_probe
,
222 .close
= ext4fs_close
,
224 .exists
= ext4fs_exists
,
226 .read
= ext4_read_file
,
227 #ifdef CONFIG_CMD_EXT4_WRITE
228 .write
= ext4_write_file
,
229 .ln
= ext4fs_create_link
,
231 .write
= fs_write_unsupported
,
232 .ln
= fs_ln_unsupported
,
235 .opendir
= fs_opendir_unsupported
,
236 .unlink
= fs_unlink_unsupported
,
237 .mkdir
= fs_mkdir_unsupported
,
240 #ifdef CONFIG_SANDBOX
242 .fstype
= FS_TYPE_SANDBOX
,
244 .null_dev_desc_ok
= true,
245 .probe
= sandbox_fs_set_blk_dev
,
246 .close
= sandbox_fs_close
,
248 .exists
= sandbox_fs_exists
,
249 .size
= sandbox_fs_size
,
250 .read
= fs_read_sandbox
,
251 .write
= fs_write_sandbox
,
252 .uuid
= fs_uuid_unsupported
,
253 .opendir
= fs_opendir_unsupported
,
254 .unlink
= fs_unlink_unsupported
,
255 .mkdir
= fs_mkdir_unsupported
,
256 .ln
= fs_ln_unsupported
,
259 #ifdef CONFIG_SEMIHOSTING
261 .fstype
= FS_TYPE_SEMIHOSTING
,
262 .name
= "semihosting",
263 .null_dev_desc_ok
= true,
264 .probe
= smh_fs_set_blk_dev
,
265 .close
= fs_close_unsupported
,
266 .ls
= fs_ls_unsupported
,
267 .exists
= fs_exists_unsupported
,
270 .write
= smh_fs_write
,
271 .uuid
= fs_uuid_unsupported
,
272 .opendir
= fs_opendir_unsupported
,
273 .unlink
= fs_unlink_unsupported
,
274 .mkdir
= fs_mkdir_unsupported
,
275 .ln
= fs_ln_unsupported
,
278 #ifndef CONFIG_SPL_BUILD
279 #ifdef CONFIG_CMD_UBIFS
281 .fstype
= FS_TYPE_UBIFS
,
283 .null_dev_desc_ok
= true,
284 .probe
= ubifs_set_blk_dev
,
285 .close
= ubifs_close
,
287 .exists
= ubifs_exists
,
290 .write
= fs_write_unsupported
,
291 .uuid
= fs_uuid_unsupported
,
292 .opendir
= fs_opendir_unsupported
,
293 .unlink
= fs_unlink_unsupported
,
294 .mkdir
= fs_mkdir_unsupported
,
295 .ln
= fs_ln_unsupported
,
299 #ifndef CONFIG_SPL_BUILD
300 #ifdef CONFIG_FS_BTRFS
302 .fstype
= FS_TYPE_BTRFS
,
304 .null_dev_desc_ok
= false,
305 .probe
= btrfs_probe
,
306 .close
= btrfs_close
,
308 .exists
= btrfs_exists
,
311 .write
= fs_write_unsupported
,
313 .opendir
= fs_opendir_unsupported
,
314 .unlink
= fs_unlink_unsupported
,
315 .mkdir
= fs_mkdir_unsupported
,
316 .ln
= fs_ln_unsupported
,
320 #if CONFIG_IS_ENABLED(FS_SQUASHFS)
322 .fstype
= FS_TYPE_SQUASHFS
,
324 .null_dev_desc_ok
= false,
326 .opendir
= sqfs_opendir
,
327 .readdir
= sqfs_readdir
,
332 .closedir
= sqfs_closedir
,
333 .exists
= sqfs_exists
,
334 .uuid
= fs_uuid_unsupported
,
335 .write
= fs_write_unsupported
,
336 .ln
= fs_ln_unsupported
,
337 .unlink
= fs_unlink_unsupported
,
338 .mkdir
= fs_mkdir_unsupported
,
341 #if IS_ENABLED(CONFIG_FS_EROFS)
343 .fstype
= FS_TYPE_EROFS
,
345 .null_dev_desc_ok
= false,
346 .probe
= erofs_probe
,
347 .opendir
= erofs_opendir
,
348 .readdir
= erofs_readdir
,
352 .close
= erofs_close
,
353 .closedir
= erofs_closedir
,
354 .exists
= erofs_exists
,
355 .uuid
= fs_uuid_unsupported
,
356 .write
= fs_write_unsupported
,
357 .ln
= fs_ln_unsupported
,
358 .unlink
= fs_unlink_unsupported
,
359 .mkdir
= fs_mkdir_unsupported
,
363 .fstype
= FS_TYPE_ANY
,
364 .name
= "unsupported",
365 .null_dev_desc_ok
= true,
366 .probe
= fs_probe_unsupported
,
367 .close
= fs_close_unsupported
,
368 .ls
= fs_ls_unsupported
,
369 .exists
= fs_exists_unsupported
,
370 .size
= fs_size_unsupported
,
371 .read
= fs_read_unsupported
,
372 .write
= fs_write_unsupported
,
373 .uuid
= fs_uuid_unsupported
,
374 .opendir
= fs_opendir_unsupported
,
375 .unlink
= fs_unlink_unsupported
,
376 .mkdir
= fs_mkdir_unsupported
,
377 .ln
= fs_ln_unsupported
,
381 static struct fstype_info
*fs_get_info(int fstype
)
383 struct fstype_info
*info
;
386 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
) - 1; i
++, info
++) {
387 if (fstype
== info
->fstype
)
391 /* Return the 'unsupported' sentinel */
396 * fs_get_type() - Get type of current filesystem
398 * Return: filesystem type
400 * Returns filesystem type representing the current filesystem, or
401 * FS_TYPE_ANY for any unrecognised filesystem.
403 int fs_get_type(void)
409 * fs_get_type_name() - Get type of current filesystem
411 * Return: Pointer to filesystem name
413 * Returns a string describing the current filesystem, or the sentinel
414 * "unsupported" for any unrecognised filesystem.
416 const char *fs_get_type_name(void)
418 return fs_get_info(fs_type
)->name
;
421 int fs_set_blk_dev(const char *ifname
, const char *dev_part_str
, int fstype
)
423 struct fstype_info
*info
;
425 #ifdef CONFIG_NEEDS_MANUAL_RELOC
426 static int relocated
;
429 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
);
431 info
->name
+= gd
->reloc_off
;
432 info
->probe
+= gd
->reloc_off
;
433 info
->close
+= gd
->reloc_off
;
434 info
->ls
+= gd
->reloc_off
;
435 info
->read
+= gd
->reloc_off
;
436 info
->write
+= gd
->reloc_off
;
442 part
= part_get_info_by_dev_and_name_or_num(ifname
, dev_part_str
, &fs_dev_desc
,
447 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
448 if (fstype
!= FS_TYPE_ANY
&& info
->fstype
!= FS_TYPE_ANY
&&
449 fstype
!= info
->fstype
)
452 if (!fs_dev_desc
&& !info
->null_dev_desc_ok
)
455 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
456 fs_type
= info
->fstype
;
465 /* set current blk device w/ blk_desc + partition # */
466 int fs_set_blk_dev_with_part(struct blk_desc
*desc
, int part
)
468 struct fstype_info
*info
;
472 ret
= part_get_info(desc
, part
, &fs_partition
);
474 ret
= part_get_info_whole_disk(desc
, &fs_partition
);
479 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
480 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
481 fs_type
= info
->fstype
;
492 struct fstype_info
*info
= fs_get_info(fs_type
);
496 fs_type
= FS_TYPE_ANY
;
499 int fs_uuid(char *uuid_str
)
501 struct fstype_info
*info
= fs_get_info(fs_type
);
503 return info
->uuid(uuid_str
);
506 int fs_ls(const char *dirname
)
510 struct fstype_info
*info
= fs_get_info(fs_type
);
512 ret
= info
->ls(dirname
);
519 int fs_exists(const char *filename
)
523 struct fstype_info
*info
= fs_get_info(fs_type
);
525 ret
= info
->exists(filename
);
532 int fs_size(const char *filename
, loff_t
*size
)
536 struct fstype_info
*info
= fs_get_info(fs_type
);
538 ret
= info
->size(filename
, size
);
546 /* Check if a file may be read to the given address */
547 static int fs_read_lmb_check(const char *filename
, ulong addr
, loff_t offset
,
548 loff_t len
, struct fstype_info
*info
)
555 /* get the actual size of the file */
556 ret
= info
->size(filename
, &size
);
559 if (offset
>= size
) {
560 /* offset >= EOF, no bytes will be written */
563 read_len
= size
- offset
;
565 /* limit to 'len' if it is smaller */
566 if (len
&& len
< read_len
)
569 lmb_init_and_reserve(&lmb
, gd
->bd
, (void *)gd
->fdt_blob
);
572 if (lmb_alloc_addr(&lmb
, addr
, read_len
) == addr
)
575 log_err("** Reading file would overwrite reserved memory **\n");
580 static int _fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
581 int do_lmb_check
, loff_t
*actread
)
583 struct fstype_info
*info
= fs_get_info(fs_type
);
589 ret
= fs_read_lmb_check(filename
, addr
, offset
, len
, info
);
596 * We don't actually know how many bytes are being read, since len==0
597 * means read the whole file.
599 buf
= map_sysmem(addr
, len
);
600 ret
= info
->read(filename
, buf
, offset
, len
, actread
);
603 /* If we requested a specific number of bytes, check we got it */
604 if (ret
== 0 && len
&& *actread
!= len
)
605 log_debug("** %s shorter than offset + len **\n", filename
);
611 int fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
614 return _fs_read(filename
, addr
, offset
, len
, 0, actread
);
617 int fs_write(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
620 struct fstype_info
*info
= fs_get_info(fs_type
);
624 buf
= map_sysmem(addr
, len
);
625 ret
= info
->write(filename
, buf
, offset
, len
, actwrite
);
628 if (ret
< 0 && len
!= *actwrite
) {
629 log_err("** Unable to write file %s **\n", filename
);
637 struct fs_dir_stream
*fs_opendir(const char *filename
)
639 struct fstype_info
*info
= fs_get_info(fs_type
);
640 struct fs_dir_stream
*dirs
= NULL
;
643 ret
= info
->opendir(filename
, &dirs
);
650 dirs
->desc
= fs_dev_desc
;
651 dirs
->part
= fs_dev_part
;
656 struct fs_dirent
*fs_readdir(struct fs_dir_stream
*dirs
)
658 struct fstype_info
*info
;
659 struct fs_dirent
*dirent
;
662 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
663 info
= fs_get_info(fs_type
);
665 ret
= info
->readdir(dirs
, &dirent
);
675 void fs_closedir(struct fs_dir_stream
*dirs
)
677 struct fstype_info
*info
;
682 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
683 info
= fs_get_info(fs_type
);
685 info
->closedir(dirs
);
689 int fs_unlink(const char *filename
)
693 struct fstype_info
*info
= fs_get_info(fs_type
);
695 ret
= info
->unlink(filename
);
702 int fs_mkdir(const char *dirname
)
706 struct fstype_info
*info
= fs_get_info(fs_type
);
708 ret
= info
->mkdir(dirname
);
715 int fs_ln(const char *fname
, const char *target
)
717 struct fstype_info
*info
= fs_get_info(fs_type
);
720 ret
= info
->ln(fname
, target
);
723 log_err("** Unable to create link %s -> %s **\n", fname
, target
);
731 int do_size(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
737 return CMD_RET_USAGE
;
739 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
742 if (fs_size(argv
[3], &size
) < 0)
743 return CMD_RET_FAILURE
;
745 env_set_hex("filesize", size
);
750 int do_load(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
754 const char *addr_str
;
755 const char *filename
;
764 return CMD_RET_USAGE
;
766 return CMD_RET_USAGE
;
768 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
)) {
769 log_err("Can't set block device\n");
774 addr
= hextoul(argv
[3], &ep
);
775 if (ep
== argv
[3] || *ep
!= '\0')
776 return CMD_RET_USAGE
;
778 addr_str
= env_get("loadaddr");
779 if (addr_str
!= NULL
)
780 addr
= hextoul(addr_str
, NULL
);
782 addr
= CONFIG_SYS_LOAD_ADDR
;
787 filename
= env_get("bootfile");
789 puts("** No boot file defined **\n");
794 bytes
= hextoul(argv
[5], NULL
);
798 pos
= hextoul(argv
[6], NULL
);
803 ret
= _fs_read(filename
, addr
, pos
, bytes
, 1, &len_read
);
804 time
= get_timer(time
);
806 log_err("Failed to load '%s'\n", filename
);
810 if (IS_ENABLED(CONFIG_CMD_BOOTEFI
))
811 efi_set_bootdev(argv
[1], (argc
> 2) ? argv
[2] : "",
812 (argc
> 4) ? argv
[4] : "", map_sysmem(addr
, 0),
815 printf("%llu bytes read in %lu ms", len_read
, time
);
818 print_size(div_u64(len_read
, time
) * 1000, "/s");
823 env_set_hex("fileaddr", addr
);
824 env_set_hex("filesize", len_read
);
829 int do_ls(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
833 return CMD_RET_USAGE
;
835 return CMD_RET_USAGE
;
837 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
840 if (fs_ls(argc
>= 4 ? argv
[3] : "/"))
846 int file_exists(const char *dev_type
, const char *dev_part
, const char *file
,
849 if (fs_set_blk_dev(dev_type
, dev_part
, fstype
))
852 return fs_exists(file
);
855 int do_save(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
859 const char *filename
;
866 if (argc
< 6 || argc
> 7)
867 return CMD_RET_USAGE
;
869 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
872 addr
= hextoul(argv
[3], NULL
);
874 bytes
= hextoul(argv
[5], NULL
);
876 pos
= hextoul(argv
[6], NULL
);
881 ret
= fs_write(filename
, addr
, pos
, bytes
, &len
);
882 time
= get_timer(time
);
886 printf("%llu bytes written in %lu ms", len
, time
);
889 print_size(div_u64(len
, time
) * 1000, "/s");
897 int do_fs_uuid(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
902 memset(uuid
, 0, sizeof(uuid
));
904 if (argc
< 3 || argc
> 4)
905 return CMD_RET_USAGE
;
907 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
912 return CMD_RET_FAILURE
;
915 env_set(argv
[3], uuid
);
917 printf("%s\n", uuid
);
919 return CMD_RET_SUCCESS
;
922 int do_fs_type(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
924 struct fstype_info
*info
;
926 if (argc
< 3 || argc
> 4)
927 return CMD_RET_USAGE
;
929 if (fs_set_blk_dev(argv
[1], argv
[2], FS_TYPE_ANY
))
932 info
= fs_get_info(fs_type
);
935 env_set(argv
[3], info
->name
);
937 printf("%s\n", info
->name
);
941 return CMD_RET_SUCCESS
;
944 int do_rm(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
948 return CMD_RET_USAGE
;
950 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
953 if (fs_unlink(argv
[3]))
959 int do_mkdir(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
965 return CMD_RET_USAGE
;
967 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
970 ret
= fs_mkdir(argv
[3]);
972 log_err("** Unable to create a directory \"%s\" **\n", argv
[3]);
979 int do_ln(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
983 return CMD_RET_USAGE
;
985 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
988 if (fs_ln(argv
[3], argv
[4]))
994 int do_fs_types(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char * const argv
[])
996 struct fstype_info
*drv
= fstypes
;
997 const int n_ents
= ARRAY_SIZE(fstypes
);
998 struct fstype_info
*entry
;
1001 puts("Supported filesystems");
1002 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
1003 if (entry
->fstype
!= FS_TYPE_ANY
) {
1004 printf("%c %s", i
? ',' : ':', entry
->name
);
1011 return CMD_RET_SUCCESS
;
1014 int fs_read_alloc(const char *fname
, ulong size
, uint align
, void **bufp
)
1021 buf
= memalign(align
, size
+ 1);
1023 return log_msg_ret("buf", -ENOMEM
);
1024 addr
= map_to_sysmem(buf
);
1026 ret
= fs_read(fname
, addr
, 0, size
, &bytes_read
);
1029 return log_msg_ret("read", ret
);
1031 if (size
!= bytes_read
)
1032 return log_msg_ret("bread", -EIO
);
1040 int fs_load_alloc(const char *ifname
, const char *dev_part_str
,
1041 const char *fname
, ulong max_size
, ulong align
, void **bufp
,
1048 if (fs_set_blk_dev(ifname
, dev_part_str
, FS_TYPE_ANY
))
1049 return log_msg_ret("set", -ENOMEDIUM
);
1051 ret
= fs_size(fname
, &size
);
1053 return log_msg_ret("sz", -ENOENT
);
1055 if (size
>= (max_size
?: SZ_1G
))
1056 return log_msg_ret("sz", -E2BIG
);
1058 if (fs_set_blk_dev(ifname
, dev_part_str
, FS_TYPE_ANY
))
1059 return log_msg_ret("set", -ENOMEDIUM
);
1061 ret
= fs_read_alloc(fname
, size
, align
, &buf
);
1063 return log_msg_ret("al", ret
);