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
[] = {
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 #ifdef CONFIG_CMD_UBIFS
272 .fstype
= FS_TYPE_UBIFS
,
274 .null_dev_desc_ok
= true,
275 .probe
= ubifs_set_blk_dev
,
276 .close
= ubifs_close
,
278 .exists
= ubifs_exists
,
281 .write
= fs_write_unsupported
,
282 .uuid
= fs_uuid_unsupported
,
283 .opendir
= fs_opendir_unsupported
,
284 .unlink
= fs_unlink_unsupported
,
285 .mkdir
= fs_mkdir_unsupported
,
286 .ln
= fs_ln_unsupported
,
289 #ifdef CONFIG_FS_BTRFS
291 .fstype
= FS_TYPE_BTRFS
,
293 .null_dev_desc_ok
= false,
294 .probe
= btrfs_probe
,
295 .close
= btrfs_close
,
297 .exists
= btrfs_exists
,
300 .write
= fs_write_unsupported
,
302 .opendir
= fs_opendir_unsupported
,
303 .unlink
= fs_unlink_unsupported
,
304 .mkdir
= fs_mkdir_unsupported
,
305 .ln
= fs_ln_unsupported
,
308 #if IS_ENABLED(CONFIG_FS_SQUASHFS)
310 .fstype
= FS_TYPE_SQUASHFS
,
312 .null_dev_desc_ok
= false,
314 .opendir
= sqfs_opendir
,
315 .readdir
= sqfs_readdir
,
320 .closedir
= sqfs_closedir
,
321 .exists
= sqfs_exists
,
322 .uuid
= fs_uuid_unsupported
,
323 .write
= fs_write_unsupported
,
324 .ln
= fs_ln_unsupported
,
325 .unlink
= fs_unlink_unsupported
,
326 .mkdir
= fs_mkdir_unsupported
,
329 #if IS_ENABLED(CONFIG_FS_EROFS)
331 .fstype
= FS_TYPE_EROFS
,
333 .null_dev_desc_ok
= false,
334 .probe
= erofs_probe
,
335 .opendir
= erofs_opendir
,
336 .readdir
= erofs_readdir
,
340 .close
= erofs_close
,
341 .closedir
= erofs_closedir
,
342 .exists
= erofs_exists
,
343 .uuid
= fs_uuid_unsupported
,
344 .write
= fs_write_unsupported
,
345 .ln
= fs_ln_unsupported
,
346 .unlink
= fs_unlink_unsupported
,
347 .mkdir
= fs_mkdir_unsupported
,
351 .fstype
= FS_TYPE_ANY
,
352 .name
= "unsupported",
353 .null_dev_desc_ok
= true,
354 .probe
= fs_probe_unsupported
,
355 .close
= fs_close_unsupported
,
356 .ls
= fs_ls_unsupported
,
357 .exists
= fs_exists_unsupported
,
358 .size
= fs_size_unsupported
,
359 .read
= fs_read_unsupported
,
360 .write
= fs_write_unsupported
,
361 .uuid
= fs_uuid_unsupported
,
362 .opendir
= fs_opendir_unsupported
,
363 .unlink
= fs_unlink_unsupported
,
364 .mkdir
= fs_mkdir_unsupported
,
365 .ln
= fs_ln_unsupported
,
369 static struct fstype_info
*fs_get_info(int fstype
)
371 struct fstype_info
*info
;
374 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
) - 1; i
++, info
++) {
375 if (fstype
== info
->fstype
)
379 /* Return the 'unsupported' sentinel */
384 * fs_get_type() - Get type of current filesystem
386 * Return: filesystem type
388 * Returns filesystem type representing the current filesystem, or
389 * FS_TYPE_ANY for any unrecognised filesystem.
391 int fs_get_type(void)
397 * fs_get_type_name() - Get type of current filesystem
399 * Return: Pointer to filesystem name
401 * Returns a string describing the current filesystem, or the sentinel
402 * "unsupported" for any unrecognised filesystem.
404 const char *fs_get_type_name(void)
406 return fs_get_info(fs_type
)->name
;
409 int fs_set_blk_dev(const char *ifname
, const char *dev_part_str
, int fstype
)
411 struct fstype_info
*info
;
413 #ifdef CONFIG_NEEDS_MANUAL_RELOC
414 static int relocated
;
417 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
);
419 info
->name
+= gd
->reloc_off
;
420 info
->probe
+= gd
->reloc_off
;
421 info
->close
+= gd
->reloc_off
;
422 info
->ls
+= gd
->reloc_off
;
423 info
->read
+= gd
->reloc_off
;
424 info
->write
+= gd
->reloc_off
;
430 part
= part_get_info_by_dev_and_name_or_num(ifname
, dev_part_str
, &fs_dev_desc
,
435 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
436 if (fstype
!= FS_TYPE_ANY
&& info
->fstype
!= FS_TYPE_ANY
&&
437 fstype
!= info
->fstype
)
440 if (!fs_dev_desc
&& !info
->null_dev_desc_ok
)
443 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
444 fs_type
= info
->fstype
;
453 /* set current blk device w/ blk_desc + partition # */
454 int fs_set_blk_dev_with_part(struct blk_desc
*desc
, int part
)
456 struct fstype_info
*info
;
460 ret
= part_get_info(desc
, part
, &fs_partition
);
462 ret
= part_get_info_whole_disk(desc
, &fs_partition
);
467 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
468 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
469 fs_type
= info
->fstype
;
480 struct fstype_info
*info
= fs_get_info(fs_type
);
484 fs_type
= FS_TYPE_ANY
;
487 int fs_uuid(char *uuid_str
)
489 struct fstype_info
*info
= fs_get_info(fs_type
);
491 return info
->uuid(uuid_str
);
494 int fs_ls(const char *dirname
)
498 struct fstype_info
*info
= fs_get_info(fs_type
);
500 ret
= info
->ls(dirname
);
507 int fs_exists(const char *filename
)
511 struct fstype_info
*info
= fs_get_info(fs_type
);
513 ret
= info
->exists(filename
);
520 int fs_size(const char *filename
, loff_t
*size
)
524 struct fstype_info
*info
= fs_get_info(fs_type
);
526 ret
= info
->size(filename
, size
);
534 /* Check if a file may be read to the given address */
535 static int fs_read_lmb_check(const char *filename
, ulong addr
, loff_t offset
,
536 loff_t len
, struct fstype_info
*info
)
543 /* get the actual size of the file */
544 ret
= info
->size(filename
, &size
);
547 if (offset
>= size
) {
548 /* offset >= EOF, no bytes will be written */
551 read_len
= size
- offset
;
553 /* limit to 'len' if it is smaller */
554 if (len
&& len
< read_len
)
557 lmb_init_and_reserve(&lmb
, gd
->bd
, (void *)gd
->fdt_blob
);
560 if (lmb_alloc_addr(&lmb
, addr
, read_len
) == addr
)
563 log_err("** Reading file would overwrite reserved memory **\n");
568 static int _fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
569 int do_lmb_check
, loff_t
*actread
)
571 struct fstype_info
*info
= fs_get_info(fs_type
);
577 ret
= fs_read_lmb_check(filename
, addr
, offset
, len
, info
);
584 * We don't actually know how many bytes are being read, since len==0
585 * means read the whole file.
587 buf
= map_sysmem(addr
, len
);
588 ret
= info
->read(filename
, buf
, offset
, len
, actread
);
591 /* If we requested a specific number of bytes, check we got it */
592 if (ret
== 0 && len
&& *actread
!= len
)
593 log_debug("** %s shorter than offset + len **\n", filename
);
599 int fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
602 return _fs_read(filename
, addr
, offset
, len
, 0, actread
);
605 int fs_write(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
608 struct fstype_info
*info
= fs_get_info(fs_type
);
612 buf
= map_sysmem(addr
, len
);
613 ret
= info
->write(filename
, buf
, offset
, len
, actwrite
);
616 if (ret
< 0 && len
!= *actwrite
) {
617 log_err("** Unable to write file %s **\n", filename
);
625 struct fs_dir_stream
*fs_opendir(const char *filename
)
627 struct fstype_info
*info
= fs_get_info(fs_type
);
628 struct fs_dir_stream
*dirs
= NULL
;
631 ret
= info
->opendir(filename
, &dirs
);
638 dirs
->desc
= fs_dev_desc
;
639 dirs
->part
= fs_dev_part
;
644 struct fs_dirent
*fs_readdir(struct fs_dir_stream
*dirs
)
646 struct fstype_info
*info
;
647 struct fs_dirent
*dirent
;
650 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
651 info
= fs_get_info(fs_type
);
653 ret
= info
->readdir(dirs
, &dirent
);
663 void fs_closedir(struct fs_dir_stream
*dirs
)
665 struct fstype_info
*info
;
670 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
671 info
= fs_get_info(fs_type
);
673 info
->closedir(dirs
);
677 int fs_unlink(const char *filename
)
681 struct fstype_info
*info
= fs_get_info(fs_type
);
683 ret
= info
->unlink(filename
);
690 int fs_mkdir(const char *dirname
)
694 struct fstype_info
*info
= fs_get_info(fs_type
);
696 ret
= info
->mkdir(dirname
);
703 int fs_ln(const char *fname
, const char *target
)
705 struct fstype_info
*info
= fs_get_info(fs_type
);
708 ret
= info
->ln(fname
, target
);
711 log_err("** Unable to create link %s -> %s **\n", fname
, target
);
719 int do_size(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
725 return CMD_RET_USAGE
;
727 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
730 if (fs_size(argv
[3], &size
) < 0)
731 return CMD_RET_FAILURE
;
733 env_set_hex("filesize", size
);
738 int do_load(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
742 const char *addr_str
;
743 const char *filename
;
752 return CMD_RET_USAGE
;
754 return CMD_RET_USAGE
;
756 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
)) {
757 log_err("Can't set block device\n");
762 addr
= hextoul(argv
[3], &ep
);
763 if (ep
== argv
[3] || *ep
!= '\0')
764 return CMD_RET_USAGE
;
766 addr_str
= env_get("loadaddr");
767 if (addr_str
!= NULL
)
768 addr
= hextoul(addr_str
, NULL
);
770 addr
= CONFIG_SYS_LOAD_ADDR
;
775 filename
= env_get("bootfile");
777 puts("** No boot file defined **\n");
782 bytes
= hextoul(argv
[5], NULL
);
786 pos
= hextoul(argv
[6], NULL
);
791 ret
= _fs_read(filename
, addr
, pos
, bytes
, 1, &len_read
);
792 time
= get_timer(time
);
794 log_err("Failed to load '%s'\n", filename
);
798 if (IS_ENABLED(CONFIG_CMD_BOOTEFI
))
799 efi_set_bootdev(argv
[1], (argc
> 2) ? argv
[2] : "",
800 (argc
> 4) ? argv
[4] : "", map_sysmem(addr
, 0),
803 printf("%llu bytes read in %lu ms", len_read
, time
);
806 print_size(div_u64(len_read
, time
) * 1000, "/s");
811 env_set_hex("fileaddr", addr
);
812 env_set_hex("filesize", len_read
);
817 int do_ls(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
821 return CMD_RET_USAGE
;
823 return CMD_RET_USAGE
;
825 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
828 if (fs_ls(argc
>= 4 ? argv
[3] : "/"))
834 int file_exists(const char *dev_type
, const char *dev_part
, const char *file
,
837 if (fs_set_blk_dev(dev_type
, dev_part
, fstype
))
840 return fs_exists(file
);
843 int do_save(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
847 const char *filename
;
854 if (argc
< 6 || argc
> 7)
855 return CMD_RET_USAGE
;
857 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
860 addr
= hextoul(argv
[3], NULL
);
862 bytes
= hextoul(argv
[5], NULL
);
864 pos
= hextoul(argv
[6], NULL
);
869 ret
= fs_write(filename
, addr
, pos
, bytes
, &len
);
870 time
= get_timer(time
);
874 printf("%llu bytes written in %lu ms", len
, time
);
877 print_size(div_u64(len
, time
) * 1000, "/s");
885 int do_fs_uuid(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
890 memset(uuid
, 0, sizeof(uuid
));
892 if (argc
< 3 || argc
> 4)
893 return CMD_RET_USAGE
;
895 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
900 return CMD_RET_FAILURE
;
903 env_set(argv
[3], uuid
);
905 printf("%s\n", uuid
);
907 return CMD_RET_SUCCESS
;
910 int do_fs_type(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
912 struct fstype_info
*info
;
914 if (argc
< 3 || argc
> 4)
915 return CMD_RET_USAGE
;
917 if (fs_set_blk_dev(argv
[1], argv
[2], FS_TYPE_ANY
))
920 info
= fs_get_info(fs_type
);
923 env_set(argv
[3], info
->name
);
925 printf("%s\n", info
->name
);
929 return CMD_RET_SUCCESS
;
932 int do_rm(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
936 return CMD_RET_USAGE
;
938 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
941 if (fs_unlink(argv
[3]))
947 int do_mkdir(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
953 return CMD_RET_USAGE
;
955 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
958 ret
= fs_mkdir(argv
[3]);
960 log_err("** Unable to create a directory \"%s\" **\n", argv
[3]);
967 int do_ln(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
971 return CMD_RET_USAGE
;
973 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
976 if (fs_ln(argv
[3], argv
[4]))
982 int do_fs_types(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char * const argv
[])
984 struct fstype_info
*drv
= fstypes
;
985 const int n_ents
= ARRAY_SIZE(fstypes
);
986 struct fstype_info
*entry
;
989 puts("Supported filesystems");
990 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
991 if (entry
->fstype
!= FS_TYPE_ANY
) {
992 printf("%c %s", i
? ',' : ':', entry
->name
);
999 return CMD_RET_SUCCESS
;