1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
18 #include <sandboxfs.h>
19 #include <ubifs_uboot.h>
23 #include <linux/math64.h>
24 #include <efi_loader.h>
26 DECLARE_GLOBAL_DATA_PTR
;
28 static struct blk_desc
*fs_dev_desc
;
29 static int fs_dev_part
;
30 static struct disk_partition fs_partition
;
31 static int fs_type
= FS_TYPE_ANY
;
33 static inline int fs_probe_unsupported(struct blk_desc
*fs_dev_desc
,
34 struct disk_partition
*fs_partition
)
36 printf("** Unrecognized filesystem type **\n");
40 static inline int fs_ls_unsupported(const char *dirname
)
45 /* generic implementation of ls in terms of opendir/readdir/closedir */
47 static int fs_ls_generic(const char *dirname
)
49 struct fs_dir_stream
*dirs
;
50 struct fs_dirent
*dent
;
51 int nfiles
= 0, ndirs
= 0;
53 dirs
= fs_opendir(dirname
);
57 while ((dent
= fs_readdir(dirs
))) {
58 if (dent
->type
== FS_DT_DIR
) {
59 printf(" %s/\n", dent
->name
);
62 printf(" %8lld %s\n", dent
->size
, dent
->name
);
69 printf("\n%d file(s), %d dir(s)\n\n", nfiles
, ndirs
);
74 static inline int fs_exists_unsupported(const char *filename
)
79 static inline int fs_size_unsupported(const char *filename
, loff_t
*size
)
84 static inline int fs_read_unsupported(const char *filename
, void *buf
,
85 loff_t offset
, loff_t len
,
91 static inline int fs_write_unsupported(const char *filename
, void *buf
,
92 loff_t offset
, loff_t len
,
98 static inline int fs_ln_unsupported(const char *filename
, const char *target
)
103 static inline void fs_close_unsupported(void)
107 static inline int fs_uuid_unsupported(char *uuid_str
)
112 static inline int fs_opendir_unsupported(const char *filename
,
113 struct fs_dir_stream
**dirs
)
118 static inline int fs_unlink_unsupported(const char *filename
)
123 static inline int fs_mkdir_unsupported(const char *dirname
)
132 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
133 * should be false in most cases. For "virtual" filesystems which
134 * aren't based on a U-Boot block device (e.g. sandbox), this can be
135 * set to true. This should also be true for the dummy entry at the end
136 * of fstypes[], since that is essentially a "virtual" (non-existent)
139 bool null_dev_desc_ok
;
140 int (*probe
)(struct blk_desc
*fs_dev_desc
,
141 struct disk_partition
*fs_partition
);
142 int (*ls
)(const char *dirname
);
143 int (*exists
)(const char *filename
);
144 int (*size
)(const char *filename
, loff_t
*size
);
145 int (*read
)(const char *filename
, void *buf
, loff_t offset
,
146 loff_t len
, loff_t
*actread
);
147 int (*write
)(const char *filename
, void *buf
, loff_t offset
,
148 loff_t len
, loff_t
*actwrite
);
150 int (*uuid
)(char *uuid_str
);
152 * Open a directory stream. On success return 0 and directory
153 * stream pointer via 'dirsp'. On error, return -errno. See
156 int (*opendir
)(const char *filename
, struct fs_dir_stream
**dirsp
);
158 * Read next entry from directory stream. On success return 0
159 * and directory entry pointer via 'dentp'. On error return
160 * -errno. See fs_readdir().
162 int (*readdir
)(struct fs_dir_stream
*dirs
, struct fs_dirent
**dentp
);
163 /* see fs_closedir() */
164 void (*closedir
)(struct fs_dir_stream
*dirs
);
165 int (*unlink
)(const char *filename
);
166 int (*mkdir
)(const char *dirname
);
167 int (*ln
)(const char *filename
, const char *target
);
170 static struct fstype_info fstypes
[] = {
173 .fstype
= FS_TYPE_FAT
,
175 .null_dev_desc_ok
= false,
176 .probe
= fat_set_blk_dev
,
179 .exists
= fat_exists
,
181 .read
= fat_read_file
,
182 #if CONFIG_IS_ENABLED(FAT_WRITE)
183 .write
= file_fat_write
,
184 .unlink
= fat_unlink
,
187 .write
= fs_write_unsupported
,
188 .unlink
= fs_unlink_unsupported
,
189 .mkdir
= fs_mkdir_unsupported
,
191 .uuid
= fs_uuid_unsupported
,
192 .opendir
= fat_opendir
,
193 .readdir
= fat_readdir
,
194 .closedir
= fat_closedir
,
195 .ln
= fs_ln_unsupported
,
199 #if CONFIG_IS_ENABLED(FS_EXT4)
201 .fstype
= FS_TYPE_EXT
,
203 .null_dev_desc_ok
= false,
204 .probe
= ext4fs_probe
,
205 .close
= ext4fs_close
,
207 .exists
= ext4fs_exists
,
209 .read
= ext4_read_file
,
210 #ifdef CONFIG_CMD_EXT4_WRITE
211 .write
= ext4_write_file
,
212 .ln
= ext4fs_create_link
,
214 .write
= fs_write_unsupported
,
215 .ln
= fs_ln_unsupported
,
218 .opendir
= fs_opendir_unsupported
,
219 .unlink
= fs_unlink_unsupported
,
220 .mkdir
= fs_mkdir_unsupported
,
223 #ifdef CONFIG_SANDBOX
225 .fstype
= FS_TYPE_SANDBOX
,
227 .null_dev_desc_ok
= true,
228 .probe
= sandbox_fs_set_blk_dev
,
229 .close
= sandbox_fs_close
,
231 .exists
= sandbox_fs_exists
,
232 .size
= sandbox_fs_size
,
233 .read
= fs_read_sandbox
,
234 .write
= fs_write_sandbox
,
235 .uuid
= fs_uuid_unsupported
,
236 .opendir
= fs_opendir_unsupported
,
237 .unlink
= fs_unlink_unsupported
,
238 .mkdir
= fs_mkdir_unsupported
,
239 .ln
= fs_ln_unsupported
,
242 #ifdef CONFIG_CMD_UBIFS
244 .fstype
= FS_TYPE_UBIFS
,
246 .null_dev_desc_ok
= true,
247 .probe
= ubifs_set_blk_dev
,
248 .close
= ubifs_close
,
250 .exists
= ubifs_exists
,
253 .write
= fs_write_unsupported
,
254 .uuid
= fs_uuid_unsupported
,
255 .opendir
= fs_opendir_unsupported
,
256 .unlink
= fs_unlink_unsupported
,
257 .mkdir
= fs_mkdir_unsupported
,
258 .ln
= fs_ln_unsupported
,
261 #ifdef CONFIG_FS_BTRFS
263 .fstype
= FS_TYPE_BTRFS
,
265 .null_dev_desc_ok
= false,
266 .probe
= btrfs_probe
,
267 .close
= btrfs_close
,
269 .exists
= btrfs_exists
,
272 .write
= fs_write_unsupported
,
274 .opendir
= fs_opendir_unsupported
,
275 .unlink
= fs_unlink_unsupported
,
276 .mkdir
= fs_mkdir_unsupported
,
277 .ln
= fs_ln_unsupported
,
281 .fstype
= FS_TYPE_ANY
,
282 .name
= "unsupported",
283 .null_dev_desc_ok
= true,
284 .probe
= fs_probe_unsupported
,
285 .close
= fs_close_unsupported
,
286 .ls
= fs_ls_unsupported
,
287 .exists
= fs_exists_unsupported
,
288 .size
= fs_size_unsupported
,
289 .read
= fs_read_unsupported
,
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 static struct fstype_info
*fs_get_info(int fstype
)
301 struct fstype_info
*info
;
304 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
) - 1; i
++, info
++) {
305 if (fstype
== info
->fstype
)
309 /* Return the 'unsupported' sentinel */
314 * fs_get_type() - Get type of current filesystem
316 * Return: filesystem type
318 * Returns filesystem type representing the current filesystem, or
319 * FS_TYPE_ANY for any unrecognised filesystem.
321 int fs_get_type(void)
327 * fs_get_type_name() - Get type of current filesystem
329 * Return: Pointer to filesystem name
331 * Returns a string describing the current filesystem, or the sentinel
332 * "unsupported" for any unrecognised filesystem.
334 const char *fs_get_type_name(void)
336 return fs_get_info(fs_type
)->name
;
339 int fs_set_blk_dev(const char *ifname
, const char *dev_part_str
, int fstype
)
341 struct fstype_info
*info
;
343 #ifdef CONFIG_NEEDS_MANUAL_RELOC
344 static int relocated
;
347 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
);
349 info
->name
+= gd
->reloc_off
;
350 info
->probe
+= gd
->reloc_off
;
351 info
->close
+= gd
->reloc_off
;
352 info
->ls
+= gd
->reloc_off
;
353 info
->read
+= gd
->reloc_off
;
354 info
->write
+= gd
->reloc_off
;
360 part
= blk_get_device_part_str(ifname
, dev_part_str
, &fs_dev_desc
,
365 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
366 if (fstype
!= FS_TYPE_ANY
&& info
->fstype
!= FS_TYPE_ANY
&&
367 fstype
!= info
->fstype
)
370 if (!fs_dev_desc
&& !info
->null_dev_desc_ok
)
373 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
374 fs_type
= info
->fstype
;
383 /* set current blk device w/ blk_desc + partition # */
384 int fs_set_blk_dev_with_part(struct blk_desc
*desc
, int part
)
386 struct fstype_info
*info
;
390 ret
= part_get_info(desc
, part
, &fs_partition
);
392 ret
= part_get_info_whole_disk(desc
, &fs_partition
);
397 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
398 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
399 fs_type
= info
->fstype
;
410 struct fstype_info
*info
= fs_get_info(fs_type
);
414 fs_type
= FS_TYPE_ANY
;
417 int fs_uuid(char *uuid_str
)
419 struct fstype_info
*info
= fs_get_info(fs_type
);
421 return info
->uuid(uuid_str
);
424 int fs_ls(const char *dirname
)
428 struct fstype_info
*info
= fs_get_info(fs_type
);
430 ret
= info
->ls(dirname
);
437 int fs_exists(const char *filename
)
441 struct fstype_info
*info
= fs_get_info(fs_type
);
443 ret
= info
->exists(filename
);
450 int fs_size(const char *filename
, loff_t
*size
)
454 struct fstype_info
*info
= fs_get_info(fs_type
);
456 ret
= info
->size(filename
, size
);
464 /* Check if a file may be read to the given address */
465 static int fs_read_lmb_check(const char *filename
, ulong addr
, loff_t offset
,
466 loff_t len
, struct fstype_info
*info
)
473 /* get the actual size of the file */
474 ret
= info
->size(filename
, &size
);
477 if (offset
>= size
) {
478 /* offset >= EOF, no bytes will be written */
481 read_len
= size
- offset
;
483 /* limit to 'len' if it is smaller */
484 if (len
&& len
< read_len
)
487 lmb_init_and_reserve(&lmb
, gd
->bd
, (void *)gd
->fdt_blob
);
490 if (lmb_alloc_addr(&lmb
, addr
, read_len
) == addr
)
493 printf("** Reading file would overwrite reserved memory **\n");
498 static int _fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
499 int do_lmb_check
, loff_t
*actread
)
501 struct fstype_info
*info
= fs_get_info(fs_type
);
507 ret
= fs_read_lmb_check(filename
, addr
, offset
, len
, info
);
514 * We don't actually know how many bytes are being read, since len==0
515 * means read the whole file.
517 buf
= map_sysmem(addr
, len
);
518 ret
= info
->read(filename
, buf
, offset
, len
, actread
);
521 /* If we requested a specific number of bytes, check we got it */
522 if (ret
== 0 && len
&& *actread
!= len
)
523 debug("** %s shorter than offset + len **\n", filename
);
529 int fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
532 return _fs_read(filename
, addr
, offset
, len
, 0, actread
);
535 int fs_write(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
538 struct fstype_info
*info
= fs_get_info(fs_type
);
542 buf
= map_sysmem(addr
, len
);
543 ret
= info
->write(filename
, buf
, offset
, len
, actwrite
);
546 if (ret
< 0 && len
!= *actwrite
) {
547 printf("** Unable to write file %s **\n", filename
);
555 struct fs_dir_stream
*fs_opendir(const char *filename
)
557 struct fstype_info
*info
= fs_get_info(fs_type
);
558 struct fs_dir_stream
*dirs
= NULL
;
561 ret
= info
->opendir(filename
, &dirs
);
568 dirs
->desc
= fs_dev_desc
;
569 dirs
->part
= fs_dev_part
;
574 struct fs_dirent
*fs_readdir(struct fs_dir_stream
*dirs
)
576 struct fstype_info
*info
;
577 struct fs_dirent
*dirent
;
580 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
581 info
= fs_get_info(fs_type
);
583 ret
= info
->readdir(dirs
, &dirent
);
593 void fs_closedir(struct fs_dir_stream
*dirs
)
595 struct fstype_info
*info
;
600 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
601 info
= fs_get_info(fs_type
);
603 info
->closedir(dirs
);
607 int fs_unlink(const char *filename
)
611 struct fstype_info
*info
= fs_get_info(fs_type
);
613 ret
= info
->unlink(filename
);
620 int fs_mkdir(const char *dirname
)
624 struct fstype_info
*info
= fs_get_info(fs_type
);
626 ret
= info
->mkdir(dirname
);
633 int fs_ln(const char *fname
, const char *target
)
635 struct fstype_info
*info
= fs_get_info(fs_type
);
638 ret
= info
->ln(fname
, target
);
641 printf("** Unable to create link %s -> %s **\n", fname
, target
);
649 int do_size(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
655 return CMD_RET_USAGE
;
657 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
660 if (fs_size(argv
[3], &size
) < 0)
661 return CMD_RET_FAILURE
;
663 env_set_hex("filesize", size
);
668 int do_load(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
672 const char *addr_str
;
673 const char *filename
;
682 return CMD_RET_USAGE
;
684 return CMD_RET_USAGE
;
686 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
690 addr
= simple_strtoul(argv
[3], &ep
, 16);
691 if (ep
== argv
[3] || *ep
!= '\0')
692 return CMD_RET_USAGE
;
694 addr_str
= env_get("loadaddr");
695 if (addr_str
!= NULL
)
696 addr
= simple_strtoul(addr_str
, NULL
, 16);
698 addr
= CONFIG_SYS_LOAD_ADDR
;
703 filename
= env_get("bootfile");
705 puts("** No boot file defined **\n");
710 bytes
= simple_strtoul(argv
[5], NULL
, 16);
714 pos
= simple_strtoul(argv
[6], NULL
, 16);
718 #ifdef CONFIG_CMD_BOOTEFI
719 efi_set_bootdev(argv
[1], (argc
> 2) ? argv
[2] : "",
720 (argc
> 4) ? argv
[4] : "");
723 ret
= _fs_read(filename
, addr
, pos
, bytes
, 1, &len_read
);
724 time
= get_timer(time
);
728 printf("%llu bytes read in %lu ms", len_read
, time
);
731 print_size(div_u64(len_read
, time
) * 1000, "/s");
736 env_set_hex("fileaddr", addr
);
737 env_set_hex("filesize", len_read
);
742 int do_ls(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
746 return CMD_RET_USAGE
;
748 return CMD_RET_USAGE
;
750 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
753 if (fs_ls(argc
>= 4 ? argv
[3] : "/"))
759 int file_exists(const char *dev_type
, const char *dev_part
, const char *file
,
762 if (fs_set_blk_dev(dev_type
, dev_part
, fstype
))
765 return fs_exists(file
);
768 int do_save(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
772 const char *filename
;
779 if (argc
< 6 || argc
> 7)
780 return CMD_RET_USAGE
;
782 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
785 addr
= simple_strtoul(argv
[3], NULL
, 16);
787 bytes
= simple_strtoul(argv
[5], NULL
, 16);
789 pos
= simple_strtoul(argv
[6], NULL
, 16);
794 ret
= fs_write(filename
, addr
, pos
, bytes
, &len
);
795 time
= get_timer(time
);
799 printf("%llu bytes written in %lu ms", len
, time
);
802 print_size(div_u64(len
, time
) * 1000, "/s");
810 int do_fs_uuid(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
815 memset(uuid
, 0, sizeof(uuid
));
817 if (argc
< 3 || argc
> 4)
818 return CMD_RET_USAGE
;
820 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
825 return CMD_RET_FAILURE
;
828 env_set(argv
[3], uuid
);
830 printf("%s\n", uuid
);
832 return CMD_RET_SUCCESS
;
835 int do_fs_type(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
837 struct fstype_info
*info
;
839 if (argc
< 3 || argc
> 4)
840 return CMD_RET_USAGE
;
842 if (fs_set_blk_dev(argv
[1], argv
[2], FS_TYPE_ANY
))
845 info
= fs_get_info(fs_type
);
848 env_set(argv
[3], info
->name
);
850 printf("%s\n", info
->name
);
854 return CMD_RET_SUCCESS
;
857 int do_rm(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
861 return CMD_RET_USAGE
;
863 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
866 if (fs_unlink(argv
[3]))
872 int do_mkdir(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
878 return CMD_RET_USAGE
;
880 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
883 ret
= fs_mkdir(argv
[3]);
885 printf("** Unable to create a directory \"%s\" **\n", argv
[3]);
892 int do_ln(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
896 return CMD_RET_USAGE
;
898 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
901 if (fs_ln(argv
[3], argv
[4]))