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 static inline int fs_probe_unsupported(struct blk_desc
*fs_dev_desc
,
40 struct disk_partition
*fs_partition
)
42 log_debug("Unrecognized filesystem type\n");
46 static inline int fs_ls_unsupported(const char *dirname
)
51 /* generic implementation of ls in terms of opendir/readdir/closedir */
53 static int fs_ls_generic(const char *dirname
)
55 struct fs_dir_stream
*dirs
;
56 struct fs_dirent
*dent
;
57 int nfiles
= 0, ndirs
= 0;
59 dirs
= fs_opendir(dirname
);
63 while ((dent
= fs_readdir(dirs
))) {
64 if (dent
->type
== FS_DT_DIR
) {
65 printf(" %s/\n", dent
->name
);
67 } else if (dent
->type
== FS_DT_LNK
) {
68 printf(" <SYM> %s\n", dent
->name
);
71 printf(" %8lld %s\n", dent
->size
, dent
->name
);
78 printf("\n%d file(s), %d dir(s)\n\n", nfiles
, ndirs
);
83 static inline int fs_exists_unsupported(const char *filename
)
88 static inline int fs_size_unsupported(const char *filename
, loff_t
*size
)
93 static inline int fs_read_unsupported(const char *filename
, void *buf
,
94 loff_t offset
, loff_t len
,
100 static inline int fs_write_unsupported(const char *filename
, void *buf
,
101 loff_t offset
, loff_t len
,
107 static inline int fs_ln_unsupported(const char *filename
, const char *target
)
112 static inline void fs_close_unsupported(void)
116 static inline int fs_uuid_unsupported(char *uuid_str
)
121 static inline int fs_opendir_unsupported(const char *filename
,
122 struct fs_dir_stream
**dirs
)
127 static inline int fs_unlink_unsupported(const char *filename
)
132 static inline int fs_mkdir_unsupported(const char *dirname
)
141 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
142 * should be false in most cases. For "virtual" filesystems which
143 * aren't based on a U-Boot block device (e.g. sandbox), this can be
144 * set to true. This should also be true for the dummy entry at the end
145 * of fstypes[], since that is essentially a "virtual" (non-existent)
148 bool null_dev_desc_ok
;
149 int (*probe
)(struct blk_desc
*fs_dev_desc
,
150 struct disk_partition
*fs_partition
);
151 int (*ls
)(const char *dirname
);
152 int (*exists
)(const char *filename
);
153 int (*size
)(const char *filename
, loff_t
*size
);
154 int (*read
)(const char *filename
, void *buf
, loff_t offset
,
155 loff_t len
, loff_t
*actread
);
156 int (*write
)(const char *filename
, void *buf
, loff_t offset
,
157 loff_t len
, loff_t
*actwrite
);
159 int (*uuid
)(char *uuid_str
);
161 * Open a directory stream. On success return 0 and directory
162 * stream pointer via 'dirsp'. On error, return -errno. See
165 int (*opendir
)(const char *filename
, struct fs_dir_stream
**dirsp
);
167 * Read next entry from directory stream. On success return 0
168 * and directory entry pointer via 'dentp'. On error return
169 * -errno. See fs_readdir().
171 int (*readdir
)(struct fs_dir_stream
*dirs
, struct fs_dirent
**dentp
);
172 /* see fs_closedir() */
173 void (*closedir
)(struct fs_dir_stream
*dirs
);
174 int (*unlink
)(const char *filename
);
175 int (*mkdir
)(const char *dirname
);
176 int (*ln
)(const char *filename
, const char *target
);
179 static struct fstype_info fstypes
[] = {
180 #if CONFIG_IS_ENABLED(FS_FAT)
182 .fstype
= FS_TYPE_FAT
,
184 .null_dev_desc_ok
= false,
185 .probe
= fat_set_blk_dev
,
188 .exists
= fat_exists
,
190 .read
= fat_read_file
,
191 #if CONFIG_IS_ENABLED(FAT_WRITE)
192 .write
= file_fat_write
,
193 .unlink
= fat_unlink
,
196 .write
= fs_write_unsupported
,
197 .unlink
= fs_unlink_unsupported
,
198 .mkdir
= fs_mkdir_unsupported
,
201 .opendir
= fat_opendir
,
202 .readdir
= fat_readdir
,
203 .closedir
= fat_closedir
,
204 .ln
= fs_ln_unsupported
,
208 #if CONFIG_IS_ENABLED(FS_EXT4)
210 .fstype
= FS_TYPE_EXT
,
212 .null_dev_desc_ok
= false,
213 .probe
= ext4fs_probe
,
214 .close
= ext4fs_close
,
216 .exists
= ext4fs_exists
,
218 .read
= ext4_read_file
,
219 #ifdef CONFIG_CMD_EXT4_WRITE
220 .write
= ext4_write_file
,
221 .ln
= ext4fs_create_link
,
223 .write
= fs_write_unsupported
,
224 .ln
= fs_ln_unsupported
,
227 .opendir
= fs_opendir_unsupported
,
228 .unlink
= fs_unlink_unsupported
,
229 .mkdir
= fs_mkdir_unsupported
,
232 #ifdef CONFIG_SANDBOX
234 .fstype
= FS_TYPE_SANDBOX
,
236 .null_dev_desc_ok
= true,
237 .probe
= sandbox_fs_set_blk_dev
,
238 .close
= sandbox_fs_close
,
240 .exists
= sandbox_fs_exists
,
241 .size
= sandbox_fs_size
,
242 .read
= fs_read_sandbox
,
243 .write
= fs_write_sandbox
,
244 .uuid
= fs_uuid_unsupported
,
245 .opendir
= fs_opendir_unsupported
,
246 .unlink
= fs_unlink_unsupported
,
247 .mkdir
= fs_mkdir_unsupported
,
248 .ln
= fs_ln_unsupported
,
251 #ifdef CONFIG_SEMIHOSTING
253 .fstype
= FS_TYPE_SEMIHOSTING
,
254 .name
= "semihosting",
255 .null_dev_desc_ok
= true,
256 .probe
= smh_fs_set_blk_dev
,
257 .close
= fs_close_unsupported
,
258 .ls
= fs_ls_unsupported
,
259 .exists
= fs_exists_unsupported
,
262 .write
= smh_fs_write
,
263 .uuid
= fs_uuid_unsupported
,
264 .opendir
= fs_opendir_unsupported
,
265 .unlink
= fs_unlink_unsupported
,
266 .mkdir
= fs_mkdir_unsupported
,
267 .ln
= fs_ln_unsupported
,
270 #ifndef CONFIG_SPL_BUILD
271 #ifdef CONFIG_CMD_UBIFS
273 .fstype
= FS_TYPE_UBIFS
,
275 .null_dev_desc_ok
= true,
276 .probe
= ubifs_set_blk_dev
,
277 .close
= ubifs_close
,
279 .exists
= ubifs_exists
,
282 .write
= fs_write_unsupported
,
283 .uuid
= fs_uuid_unsupported
,
284 .opendir
= fs_opendir_unsupported
,
285 .unlink
= fs_unlink_unsupported
,
286 .mkdir
= fs_mkdir_unsupported
,
287 .ln
= fs_ln_unsupported
,
291 #ifndef CONFIG_SPL_BUILD
292 #ifdef CONFIG_FS_BTRFS
294 .fstype
= FS_TYPE_BTRFS
,
296 .null_dev_desc_ok
= false,
297 .probe
= btrfs_probe
,
298 .close
= btrfs_close
,
300 .exists
= btrfs_exists
,
303 .write
= fs_write_unsupported
,
305 .opendir
= fs_opendir_unsupported
,
306 .unlink
= fs_unlink_unsupported
,
307 .mkdir
= fs_mkdir_unsupported
,
308 .ln
= fs_ln_unsupported
,
312 #if CONFIG_IS_ENABLED(FS_SQUASHFS)
314 .fstype
= FS_TYPE_SQUASHFS
,
316 .null_dev_desc_ok
= false,
318 .opendir
= sqfs_opendir
,
319 .readdir
= sqfs_readdir
,
324 .closedir
= sqfs_closedir
,
325 .exists
= sqfs_exists
,
326 .uuid
= fs_uuid_unsupported
,
327 .write
= fs_write_unsupported
,
328 .ln
= fs_ln_unsupported
,
329 .unlink
= fs_unlink_unsupported
,
330 .mkdir
= fs_mkdir_unsupported
,
333 #if IS_ENABLED(CONFIG_FS_EROFS)
335 .fstype
= FS_TYPE_EROFS
,
337 .null_dev_desc_ok
= false,
338 .probe
= erofs_probe
,
339 .opendir
= erofs_opendir
,
340 .readdir
= erofs_readdir
,
344 .close
= erofs_close
,
345 .closedir
= erofs_closedir
,
346 .exists
= erofs_exists
,
347 .uuid
= fs_uuid_unsupported
,
348 .write
= fs_write_unsupported
,
349 .ln
= fs_ln_unsupported
,
350 .unlink
= fs_unlink_unsupported
,
351 .mkdir
= fs_mkdir_unsupported
,
355 .fstype
= FS_TYPE_ANY
,
356 .name
= "unsupported",
357 .null_dev_desc_ok
= true,
358 .probe
= fs_probe_unsupported
,
359 .close
= fs_close_unsupported
,
360 .ls
= fs_ls_unsupported
,
361 .exists
= fs_exists_unsupported
,
362 .size
= fs_size_unsupported
,
363 .read
= fs_read_unsupported
,
364 .write
= fs_write_unsupported
,
365 .uuid
= fs_uuid_unsupported
,
366 .opendir
= fs_opendir_unsupported
,
367 .unlink
= fs_unlink_unsupported
,
368 .mkdir
= fs_mkdir_unsupported
,
369 .ln
= fs_ln_unsupported
,
373 static struct fstype_info
*fs_get_info(int fstype
)
375 struct fstype_info
*info
;
378 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
) - 1; i
++, info
++) {
379 if (fstype
== info
->fstype
)
383 /* Return the 'unsupported' sentinel */
388 * fs_get_type() - Get type of current filesystem
390 * Return: filesystem type
392 * Returns filesystem type representing the current filesystem, or
393 * FS_TYPE_ANY for any unrecognised filesystem.
395 int fs_get_type(void)
401 * fs_get_type_name() - Get type of current filesystem
403 * Return: Pointer to filesystem name
405 * Returns a string describing the current filesystem, or the sentinel
406 * "unsupported" for any unrecognised filesystem.
408 const char *fs_get_type_name(void)
410 return fs_get_info(fs_type
)->name
;
413 int fs_set_blk_dev(const char *ifname
, const char *dev_part_str
, int fstype
)
415 struct fstype_info
*info
;
417 #ifdef CONFIG_NEEDS_MANUAL_RELOC
418 static int relocated
;
421 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
);
423 info
->name
+= gd
->reloc_off
;
424 info
->probe
+= gd
->reloc_off
;
425 info
->close
+= gd
->reloc_off
;
426 info
->ls
+= gd
->reloc_off
;
427 info
->read
+= gd
->reloc_off
;
428 info
->write
+= gd
->reloc_off
;
434 part
= part_get_info_by_dev_and_name_or_num(ifname
, dev_part_str
, &fs_dev_desc
,
439 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
440 if (fstype
!= FS_TYPE_ANY
&& info
->fstype
!= FS_TYPE_ANY
&&
441 fstype
!= info
->fstype
)
444 if (!fs_dev_desc
&& !info
->null_dev_desc_ok
)
447 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
448 fs_type
= info
->fstype
;
457 /* set current blk device w/ blk_desc + partition # */
458 int fs_set_blk_dev_with_part(struct blk_desc
*desc
, int part
)
460 struct fstype_info
*info
;
464 ret
= part_get_info(desc
, part
, &fs_partition
);
466 ret
= part_get_info_whole_disk(desc
, &fs_partition
);
471 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
472 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
473 fs_type
= info
->fstype
;
484 struct fstype_info
*info
= fs_get_info(fs_type
);
488 fs_type
= FS_TYPE_ANY
;
491 int fs_uuid(char *uuid_str
)
493 struct fstype_info
*info
= fs_get_info(fs_type
);
495 return info
->uuid(uuid_str
);
498 int fs_ls(const char *dirname
)
502 struct fstype_info
*info
= fs_get_info(fs_type
);
504 ret
= info
->ls(dirname
);
511 int fs_exists(const char *filename
)
515 struct fstype_info
*info
= fs_get_info(fs_type
);
517 ret
= info
->exists(filename
);
524 int fs_size(const char *filename
, loff_t
*size
)
528 struct fstype_info
*info
= fs_get_info(fs_type
);
530 ret
= info
->size(filename
, size
);
538 /* Check if a file may be read to the given address */
539 static int fs_read_lmb_check(const char *filename
, ulong addr
, loff_t offset
,
540 loff_t len
, struct fstype_info
*info
)
547 /* get the actual size of the file */
548 ret
= info
->size(filename
, &size
);
551 if (offset
>= size
) {
552 /* offset >= EOF, no bytes will be written */
555 read_len
= size
- offset
;
557 /* limit to 'len' if it is smaller */
558 if (len
&& len
< read_len
)
561 lmb_init_and_reserve(&lmb
, gd
->bd
, (void *)gd
->fdt_blob
);
564 if (lmb_alloc_addr(&lmb
, addr
, read_len
) == addr
)
567 log_err("** Reading file would overwrite reserved memory **\n");
572 static int _fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
573 int do_lmb_check
, loff_t
*actread
)
575 struct fstype_info
*info
= fs_get_info(fs_type
);
581 ret
= fs_read_lmb_check(filename
, addr
, offset
, len
, info
);
588 * We don't actually know how many bytes are being read, since len==0
589 * means read the whole file.
591 buf
= map_sysmem(addr
, len
);
592 ret
= info
->read(filename
, buf
, offset
, len
, actread
);
595 /* If we requested a specific number of bytes, check we got it */
596 if (ret
== 0 && len
&& *actread
!= len
)
597 log_debug("** %s shorter than offset + len **\n", filename
);
603 int fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
606 return _fs_read(filename
, addr
, offset
, len
, 0, actread
);
609 int fs_write(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
612 struct fstype_info
*info
= fs_get_info(fs_type
);
616 buf
= map_sysmem(addr
, len
);
617 ret
= info
->write(filename
, buf
, offset
, len
, actwrite
);
620 if (ret
< 0 && len
!= *actwrite
) {
621 log_err("** Unable to write file %s **\n", filename
);
629 struct fs_dir_stream
*fs_opendir(const char *filename
)
631 struct fstype_info
*info
= fs_get_info(fs_type
);
632 struct fs_dir_stream
*dirs
= NULL
;
635 ret
= info
->opendir(filename
, &dirs
);
642 dirs
->desc
= fs_dev_desc
;
643 dirs
->part
= fs_dev_part
;
648 struct fs_dirent
*fs_readdir(struct fs_dir_stream
*dirs
)
650 struct fstype_info
*info
;
651 struct fs_dirent
*dirent
;
654 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
655 info
= fs_get_info(fs_type
);
657 ret
= info
->readdir(dirs
, &dirent
);
667 void fs_closedir(struct fs_dir_stream
*dirs
)
669 struct fstype_info
*info
;
674 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
675 info
= fs_get_info(fs_type
);
677 info
->closedir(dirs
);
681 int fs_unlink(const char *filename
)
685 struct fstype_info
*info
= fs_get_info(fs_type
);
687 ret
= info
->unlink(filename
);
694 int fs_mkdir(const char *dirname
)
698 struct fstype_info
*info
= fs_get_info(fs_type
);
700 ret
= info
->mkdir(dirname
);
707 int fs_ln(const char *fname
, const char *target
)
709 struct fstype_info
*info
= fs_get_info(fs_type
);
712 ret
= info
->ln(fname
, target
);
715 log_err("** Unable to create link %s -> %s **\n", fname
, target
);
723 int do_size(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
729 return CMD_RET_USAGE
;
731 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
734 if (fs_size(argv
[3], &size
) < 0)
735 return CMD_RET_FAILURE
;
737 env_set_hex("filesize", size
);
742 int do_load(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
746 const char *addr_str
;
747 const char *filename
;
756 return CMD_RET_USAGE
;
758 return CMD_RET_USAGE
;
760 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
)) {
761 log_err("Can't set block device\n");
766 addr
= hextoul(argv
[3], &ep
);
767 if (ep
== argv
[3] || *ep
!= '\0')
768 return CMD_RET_USAGE
;
770 addr_str
= env_get("loadaddr");
771 if (addr_str
!= NULL
)
772 addr
= hextoul(addr_str
, NULL
);
774 addr
= CONFIG_SYS_LOAD_ADDR
;
779 filename
= env_get("bootfile");
781 puts("** No boot file defined **\n");
786 bytes
= hextoul(argv
[5], NULL
);
790 pos
= hextoul(argv
[6], NULL
);
795 ret
= _fs_read(filename
, addr
, pos
, bytes
, 1, &len_read
);
796 time
= get_timer(time
);
798 log_err("Failed to load '%s'\n", filename
);
802 if (IS_ENABLED(CONFIG_CMD_BOOTEFI
))
803 efi_set_bootdev(argv
[1], (argc
> 2) ? argv
[2] : "",
804 (argc
> 4) ? argv
[4] : "", map_sysmem(addr
, 0),
807 printf("%llu bytes read in %lu ms", len_read
, time
);
810 print_size(div_u64(len_read
, time
) * 1000, "/s");
815 env_set_hex("fileaddr", addr
);
816 env_set_hex("filesize", len_read
);
821 int do_ls(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
825 return CMD_RET_USAGE
;
827 return CMD_RET_USAGE
;
829 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
832 if (fs_ls(argc
>= 4 ? argv
[3] : "/"))
838 int file_exists(const char *dev_type
, const char *dev_part
, const char *file
,
841 if (fs_set_blk_dev(dev_type
, dev_part
, fstype
))
844 return fs_exists(file
);
847 int do_save(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
851 const char *filename
;
858 if (argc
< 6 || argc
> 7)
859 return CMD_RET_USAGE
;
861 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
864 addr
= hextoul(argv
[3], NULL
);
866 bytes
= hextoul(argv
[5], NULL
);
868 pos
= hextoul(argv
[6], NULL
);
873 ret
= fs_write(filename
, addr
, pos
, bytes
, &len
);
874 time
= get_timer(time
);
878 printf("%llu bytes written in %lu ms", len
, time
);
881 print_size(div_u64(len
, time
) * 1000, "/s");
889 int do_fs_uuid(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
894 memset(uuid
, 0, sizeof(uuid
));
896 if (argc
< 3 || argc
> 4)
897 return CMD_RET_USAGE
;
899 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
904 return CMD_RET_FAILURE
;
907 env_set(argv
[3], uuid
);
909 printf("%s\n", uuid
);
911 return CMD_RET_SUCCESS
;
914 int do_fs_type(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
916 struct fstype_info
*info
;
918 if (argc
< 3 || argc
> 4)
919 return CMD_RET_USAGE
;
921 if (fs_set_blk_dev(argv
[1], argv
[2], FS_TYPE_ANY
))
924 info
= fs_get_info(fs_type
);
927 env_set(argv
[3], info
->name
);
929 printf("%s\n", info
->name
);
933 return CMD_RET_SUCCESS
;
936 int do_rm(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
940 return CMD_RET_USAGE
;
942 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
945 if (fs_unlink(argv
[3]))
951 int do_mkdir(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
957 return CMD_RET_USAGE
;
959 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
962 ret
= fs_mkdir(argv
[3]);
964 log_err("** Unable to create a directory \"%s\" **\n", argv
[3]);
971 int do_ln(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
975 return CMD_RET_USAGE
;
977 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
980 if (fs_ln(argv
[3], argv
[4]))
986 int do_fs_types(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char * const argv
[])
988 struct fstype_info
*drv
= fstypes
;
989 const int n_ents
= ARRAY_SIZE(fstypes
);
990 struct fstype_info
*entry
;
993 puts("Supported filesystems");
994 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
995 if (entry
->fstype
!= FS_TYPE_ANY
) {
996 printf("%c %s", i
? ',' : ':', entry
->name
);
1003 return CMD_RET_SUCCESS
;