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>
27 DECLARE_GLOBAL_DATA_PTR
;
29 static struct blk_desc
*fs_dev_desc
;
30 static int fs_dev_part
;
31 static struct disk_partition fs_partition
;
32 static int fs_type
= FS_TYPE_ANY
;
34 static inline int fs_probe_unsupported(struct blk_desc
*fs_dev_desc
,
35 struct disk_partition
*fs_partition
)
37 printf("** Unrecognized filesystem type **\n");
41 static inline int fs_ls_unsupported(const char *dirname
)
46 /* generic implementation of ls in terms of opendir/readdir/closedir */
48 static int fs_ls_generic(const char *dirname
)
50 struct fs_dir_stream
*dirs
;
51 struct fs_dirent
*dent
;
52 int nfiles
= 0, ndirs
= 0;
54 dirs
= fs_opendir(dirname
);
58 while ((dent
= fs_readdir(dirs
))) {
59 if (dent
->type
== FS_DT_DIR
) {
60 printf(" %s/\n", dent
->name
);
62 } else if (dent
->type
== FS_DT_LNK
) {
63 printf(" <SYM> %s\n", dent
->name
);
66 printf(" %8lld %s\n", dent
->size
, dent
->name
);
73 printf("\n%d file(s), %d dir(s)\n\n", nfiles
, ndirs
);
78 static inline int fs_exists_unsupported(const char *filename
)
83 static inline int fs_size_unsupported(const char *filename
, loff_t
*size
)
88 static inline int fs_read_unsupported(const char *filename
, void *buf
,
89 loff_t offset
, loff_t len
,
95 static inline int fs_write_unsupported(const char *filename
, void *buf
,
96 loff_t offset
, loff_t len
,
102 static inline int fs_ln_unsupported(const char *filename
, const char *target
)
107 static inline void fs_close_unsupported(void)
111 static inline int fs_uuid_unsupported(char *uuid_str
)
116 static inline int fs_opendir_unsupported(const char *filename
,
117 struct fs_dir_stream
**dirs
)
122 static inline int fs_unlink_unsupported(const char *filename
)
127 static inline int fs_mkdir_unsupported(const char *dirname
)
136 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
137 * should be false in most cases. For "virtual" filesystems which
138 * aren't based on a U-Boot block device (e.g. sandbox), this can be
139 * set to true. This should also be true for the dummy entry at the end
140 * of fstypes[], since that is essentially a "virtual" (non-existent)
143 bool null_dev_desc_ok
;
144 int (*probe
)(struct blk_desc
*fs_dev_desc
,
145 struct disk_partition
*fs_partition
);
146 int (*ls
)(const char *dirname
);
147 int (*exists
)(const char *filename
);
148 int (*size
)(const char *filename
, loff_t
*size
);
149 int (*read
)(const char *filename
, void *buf
, loff_t offset
,
150 loff_t len
, loff_t
*actread
);
151 int (*write
)(const char *filename
, void *buf
, loff_t offset
,
152 loff_t len
, loff_t
*actwrite
);
154 int (*uuid
)(char *uuid_str
);
156 * Open a directory stream. On success return 0 and directory
157 * stream pointer via 'dirsp'. On error, return -errno. See
160 int (*opendir
)(const char *filename
, struct fs_dir_stream
**dirsp
);
162 * Read next entry from directory stream. On success return 0
163 * and directory entry pointer via 'dentp'. On error return
164 * -errno. See fs_readdir().
166 int (*readdir
)(struct fs_dir_stream
*dirs
, struct fs_dirent
**dentp
);
167 /* see fs_closedir() */
168 void (*closedir
)(struct fs_dir_stream
*dirs
);
169 int (*unlink
)(const char *filename
);
170 int (*mkdir
)(const char *dirname
);
171 int (*ln
)(const char *filename
, const char *target
);
174 static struct fstype_info fstypes
[] = {
177 .fstype
= FS_TYPE_FAT
,
179 .null_dev_desc_ok
= false,
180 .probe
= fat_set_blk_dev
,
183 .exists
= fat_exists
,
185 .read
= fat_read_file
,
186 #if CONFIG_IS_ENABLED(FAT_WRITE)
187 .write
= file_fat_write
,
188 .unlink
= fat_unlink
,
191 .write
= fs_write_unsupported
,
192 .unlink
= fs_unlink_unsupported
,
193 .mkdir
= fs_mkdir_unsupported
,
195 .uuid
= fs_uuid_unsupported
,
196 .opendir
= fat_opendir
,
197 .readdir
= fat_readdir
,
198 .closedir
= fat_closedir
,
199 .ln
= fs_ln_unsupported
,
203 #if CONFIG_IS_ENABLED(FS_EXT4)
205 .fstype
= FS_TYPE_EXT
,
207 .null_dev_desc_ok
= false,
208 .probe
= ext4fs_probe
,
209 .close
= ext4fs_close
,
211 .exists
= ext4fs_exists
,
213 .read
= ext4_read_file
,
214 #ifdef CONFIG_CMD_EXT4_WRITE
215 .write
= ext4_write_file
,
216 .ln
= ext4fs_create_link
,
218 .write
= fs_write_unsupported
,
219 .ln
= fs_ln_unsupported
,
222 .opendir
= fs_opendir_unsupported
,
223 .unlink
= fs_unlink_unsupported
,
224 .mkdir
= fs_mkdir_unsupported
,
227 #ifdef CONFIG_SANDBOX
229 .fstype
= FS_TYPE_SANDBOX
,
231 .null_dev_desc_ok
= true,
232 .probe
= sandbox_fs_set_blk_dev
,
233 .close
= sandbox_fs_close
,
235 .exists
= sandbox_fs_exists
,
236 .size
= sandbox_fs_size
,
237 .read
= fs_read_sandbox
,
238 .write
= fs_write_sandbox
,
239 .uuid
= fs_uuid_unsupported
,
240 .opendir
= fs_opendir_unsupported
,
241 .unlink
= fs_unlink_unsupported
,
242 .mkdir
= fs_mkdir_unsupported
,
243 .ln
= fs_ln_unsupported
,
246 #ifdef CONFIG_CMD_UBIFS
248 .fstype
= FS_TYPE_UBIFS
,
250 .null_dev_desc_ok
= true,
251 .probe
= ubifs_set_blk_dev
,
252 .close
= ubifs_close
,
254 .exists
= ubifs_exists
,
257 .write
= fs_write_unsupported
,
258 .uuid
= fs_uuid_unsupported
,
259 .opendir
= fs_opendir_unsupported
,
260 .unlink
= fs_unlink_unsupported
,
261 .mkdir
= fs_mkdir_unsupported
,
262 .ln
= fs_ln_unsupported
,
265 #ifdef CONFIG_FS_BTRFS
267 .fstype
= FS_TYPE_BTRFS
,
269 .null_dev_desc_ok
= false,
270 .probe
= btrfs_probe
,
271 .close
= btrfs_close
,
273 .exists
= btrfs_exists
,
276 .write
= fs_write_unsupported
,
278 .opendir
= fs_opendir_unsupported
,
279 .unlink
= fs_unlink_unsupported
,
280 .mkdir
= fs_mkdir_unsupported
,
281 .ln
= fs_ln_unsupported
,
284 #if IS_ENABLED(CONFIG_FS_SQUASHFS)
286 .fstype
= FS_TYPE_SQUASHFS
,
289 .opendir
= sqfs_opendir
,
290 .readdir
= sqfs_readdir
,
295 .closedir
= sqfs_closedir
,
299 .fstype
= FS_TYPE_ANY
,
300 .name
= "unsupported",
301 .null_dev_desc_ok
= true,
302 .probe
= fs_probe_unsupported
,
303 .close
= fs_close_unsupported
,
304 .ls
= fs_ls_unsupported
,
305 .exists
= fs_exists_unsupported
,
306 .size
= fs_size_unsupported
,
307 .read
= fs_read_unsupported
,
308 .write
= fs_write_unsupported
,
309 .uuid
= fs_uuid_unsupported
,
310 .opendir
= fs_opendir_unsupported
,
311 .unlink
= fs_unlink_unsupported
,
312 .mkdir
= fs_mkdir_unsupported
,
313 .ln
= fs_ln_unsupported
,
317 static struct fstype_info
*fs_get_info(int fstype
)
319 struct fstype_info
*info
;
322 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
) - 1; i
++, info
++) {
323 if (fstype
== info
->fstype
)
327 /* Return the 'unsupported' sentinel */
332 * fs_get_type() - Get type of current filesystem
334 * Return: filesystem type
336 * Returns filesystem type representing the current filesystem, or
337 * FS_TYPE_ANY for any unrecognised filesystem.
339 int fs_get_type(void)
345 * fs_get_type_name() - Get type of current filesystem
347 * Return: Pointer to filesystem name
349 * Returns a string describing the current filesystem, or the sentinel
350 * "unsupported" for any unrecognised filesystem.
352 const char *fs_get_type_name(void)
354 return fs_get_info(fs_type
)->name
;
357 int fs_set_blk_dev(const char *ifname
, const char *dev_part_str
, int fstype
)
359 struct fstype_info
*info
;
361 #ifdef CONFIG_NEEDS_MANUAL_RELOC
362 static int relocated
;
365 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
);
367 info
->name
+= gd
->reloc_off
;
368 info
->probe
+= gd
->reloc_off
;
369 info
->close
+= gd
->reloc_off
;
370 info
->ls
+= gd
->reloc_off
;
371 info
->read
+= gd
->reloc_off
;
372 info
->write
+= gd
->reloc_off
;
378 part
= blk_get_device_part_str(ifname
, dev_part_str
, &fs_dev_desc
,
383 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
384 if (fstype
!= FS_TYPE_ANY
&& info
->fstype
!= FS_TYPE_ANY
&&
385 fstype
!= info
->fstype
)
388 if (!fs_dev_desc
&& !info
->null_dev_desc_ok
)
391 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
392 fs_type
= info
->fstype
;
401 /* set current blk device w/ blk_desc + partition # */
402 int fs_set_blk_dev_with_part(struct blk_desc
*desc
, int part
)
404 struct fstype_info
*info
;
408 ret
= part_get_info(desc
, part
, &fs_partition
);
410 ret
= part_get_info_whole_disk(desc
, &fs_partition
);
415 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
416 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
417 fs_type
= info
->fstype
;
428 struct fstype_info
*info
= fs_get_info(fs_type
);
432 fs_type
= FS_TYPE_ANY
;
435 int fs_uuid(char *uuid_str
)
437 struct fstype_info
*info
= fs_get_info(fs_type
);
439 return info
->uuid(uuid_str
);
442 int fs_ls(const char *dirname
)
446 struct fstype_info
*info
= fs_get_info(fs_type
);
448 ret
= info
->ls(dirname
);
455 int fs_exists(const char *filename
)
459 struct fstype_info
*info
= fs_get_info(fs_type
);
461 ret
= info
->exists(filename
);
468 int fs_size(const char *filename
, loff_t
*size
)
472 struct fstype_info
*info
= fs_get_info(fs_type
);
474 ret
= info
->size(filename
, size
);
482 /* Check if a file may be read to the given address */
483 static int fs_read_lmb_check(const char *filename
, ulong addr
, loff_t offset
,
484 loff_t len
, struct fstype_info
*info
)
491 /* get the actual size of the file */
492 ret
= info
->size(filename
, &size
);
495 if (offset
>= size
) {
496 /* offset >= EOF, no bytes will be written */
499 read_len
= size
- offset
;
501 /* limit to 'len' if it is smaller */
502 if (len
&& len
< read_len
)
505 lmb_init_and_reserve(&lmb
, gd
->bd
, (void *)gd
->fdt_blob
);
508 if (lmb_alloc_addr(&lmb
, addr
, read_len
) == addr
)
511 printf("** Reading file would overwrite reserved memory **\n");
516 static int _fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
517 int do_lmb_check
, loff_t
*actread
)
519 struct fstype_info
*info
= fs_get_info(fs_type
);
525 ret
= fs_read_lmb_check(filename
, addr
, offset
, len
, info
);
532 * We don't actually know how many bytes are being read, since len==0
533 * means read the whole file.
535 buf
= map_sysmem(addr
, len
);
536 ret
= info
->read(filename
, buf
, offset
, len
, actread
);
539 /* If we requested a specific number of bytes, check we got it */
540 if (ret
== 0 && len
&& *actread
!= len
)
541 debug("** %s shorter than offset + len **\n", filename
);
547 int fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
550 return _fs_read(filename
, addr
, offset
, len
, 0, actread
);
553 int fs_write(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
556 struct fstype_info
*info
= fs_get_info(fs_type
);
560 buf
= map_sysmem(addr
, len
);
561 ret
= info
->write(filename
, buf
, offset
, len
, actwrite
);
564 if (ret
< 0 && len
!= *actwrite
) {
565 printf("** Unable to write file %s **\n", filename
);
573 struct fs_dir_stream
*fs_opendir(const char *filename
)
575 struct fstype_info
*info
= fs_get_info(fs_type
);
576 struct fs_dir_stream
*dirs
= NULL
;
579 ret
= info
->opendir(filename
, &dirs
);
586 dirs
->desc
= fs_dev_desc
;
587 dirs
->part
= fs_dev_part
;
592 struct fs_dirent
*fs_readdir(struct fs_dir_stream
*dirs
)
594 struct fstype_info
*info
;
595 struct fs_dirent
*dirent
;
598 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
599 info
= fs_get_info(fs_type
);
601 ret
= info
->readdir(dirs
, &dirent
);
611 void fs_closedir(struct fs_dir_stream
*dirs
)
613 struct fstype_info
*info
;
618 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
619 info
= fs_get_info(fs_type
);
621 info
->closedir(dirs
);
625 int fs_unlink(const char *filename
)
629 struct fstype_info
*info
= fs_get_info(fs_type
);
631 ret
= info
->unlink(filename
);
638 int fs_mkdir(const char *dirname
)
642 struct fstype_info
*info
= fs_get_info(fs_type
);
644 ret
= info
->mkdir(dirname
);
651 int fs_ln(const char *fname
, const char *target
)
653 struct fstype_info
*info
= fs_get_info(fs_type
);
656 ret
= info
->ln(fname
, target
);
659 printf("** Unable to create link %s -> %s **\n", fname
, target
);
667 int do_size(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
673 return CMD_RET_USAGE
;
675 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
678 if (fs_size(argv
[3], &size
) < 0)
679 return CMD_RET_FAILURE
;
681 env_set_hex("filesize", size
);
686 int do_load(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
690 const char *addr_str
;
691 const char *filename
;
700 return CMD_RET_USAGE
;
702 return CMD_RET_USAGE
;
704 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
708 addr
= simple_strtoul(argv
[3], &ep
, 16);
709 if (ep
== argv
[3] || *ep
!= '\0')
710 return CMD_RET_USAGE
;
712 addr_str
= env_get("loadaddr");
713 if (addr_str
!= NULL
)
714 addr
= simple_strtoul(addr_str
, NULL
, 16);
716 addr
= CONFIG_SYS_LOAD_ADDR
;
721 filename
= env_get("bootfile");
723 puts("** No boot file defined **\n");
728 bytes
= simple_strtoul(argv
[5], NULL
, 16);
732 pos
= simple_strtoul(argv
[6], NULL
, 16);
737 ret
= _fs_read(filename
, addr
, pos
, bytes
, 1, &len_read
);
738 time
= get_timer(time
);
740 printf("Failed to load '%s'\n", filename
);
744 if (IS_ENABLED(CONFIG_CMD_BOOTEFI
))
745 efi_set_bootdev(argv
[1], (argc
> 2) ? argv
[2] : "",
746 (argc
> 4) ? argv
[4] : "");
748 printf("%llu bytes read in %lu ms", len_read
, time
);
751 print_size(div_u64(len_read
, time
) * 1000, "/s");
756 env_set_hex("fileaddr", addr
);
757 env_set_hex("filesize", len_read
);
762 int do_ls(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
766 return CMD_RET_USAGE
;
768 return CMD_RET_USAGE
;
770 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
773 if (fs_ls(argc
>= 4 ? argv
[3] : "/"))
779 int file_exists(const char *dev_type
, const char *dev_part
, const char *file
,
782 if (fs_set_blk_dev(dev_type
, dev_part
, fstype
))
785 return fs_exists(file
);
788 int do_save(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
792 const char *filename
;
799 if (argc
< 6 || argc
> 7)
800 return CMD_RET_USAGE
;
802 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
805 addr
= simple_strtoul(argv
[3], NULL
, 16);
807 bytes
= simple_strtoul(argv
[5], NULL
, 16);
809 pos
= simple_strtoul(argv
[6], NULL
, 16);
814 ret
= fs_write(filename
, addr
, pos
, bytes
, &len
);
815 time
= get_timer(time
);
819 printf("%llu bytes written in %lu ms", len
, time
);
822 print_size(div_u64(len
, time
) * 1000, "/s");
830 int do_fs_uuid(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
835 memset(uuid
, 0, sizeof(uuid
));
837 if (argc
< 3 || argc
> 4)
838 return CMD_RET_USAGE
;
840 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
845 return CMD_RET_FAILURE
;
848 env_set(argv
[3], uuid
);
850 printf("%s\n", uuid
);
852 return CMD_RET_SUCCESS
;
855 int do_fs_type(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
857 struct fstype_info
*info
;
859 if (argc
< 3 || argc
> 4)
860 return CMD_RET_USAGE
;
862 if (fs_set_blk_dev(argv
[1], argv
[2], FS_TYPE_ANY
))
865 info
= fs_get_info(fs_type
);
868 env_set(argv
[3], info
->name
);
870 printf("%s\n", info
->name
);
874 return CMD_RET_SUCCESS
;
877 int do_rm(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
881 return CMD_RET_USAGE
;
883 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
886 if (fs_unlink(argv
[3]))
892 int do_mkdir(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
898 return CMD_RET_USAGE
;
900 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
903 ret
= fs_mkdir(argv
[3]);
905 printf("** Unable to create a directory \"%s\" **\n", argv
[3]);
912 int do_ln(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
916 return CMD_RET_USAGE
;
918 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
921 if (fs_ln(argv
[3], argv
[4]))
927 int do_fs_types(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char * const argv
[])
929 struct fstype_info
*drv
= fstypes
;
930 const int n_ents
= ARRAY_SIZE(fstypes
);
931 struct fstype_info
*entry
;
934 puts("Supported filesystems");
935 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
936 if (entry
->fstype
!= FS_TYPE_ANY
) {
937 printf("%c %s", i
? ',' : ':', entry
->name
);
944 return CMD_RET_SUCCESS
;