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 <ubifs_uboot.h>
25 #include <linux/math64.h>
26 #include <efi_loader.h>
29 DECLARE_GLOBAL_DATA_PTR
;
31 static struct blk_desc
*fs_dev_desc
;
32 static int fs_dev_part
;
33 static struct disk_partition fs_partition
;
34 static int fs_type
= FS_TYPE_ANY
;
36 static inline int fs_probe_unsupported(struct blk_desc
*fs_dev_desc
,
37 struct disk_partition
*fs_partition
)
39 log_err("** Unrecognized filesystem type **\n");
43 static inline int fs_ls_unsupported(const char *dirname
)
48 /* generic implementation of ls in terms of opendir/readdir/closedir */
50 static int fs_ls_generic(const char *dirname
)
52 struct fs_dir_stream
*dirs
;
53 struct fs_dirent
*dent
;
54 int nfiles
= 0, ndirs
= 0;
56 dirs
= fs_opendir(dirname
);
60 while ((dent
= fs_readdir(dirs
))) {
61 if (dent
->type
== FS_DT_DIR
) {
62 printf(" %s/\n", dent
->name
);
64 } else if (dent
->type
== FS_DT_LNK
) {
65 printf(" <SYM> %s\n", dent
->name
);
68 printf(" %8lld %s\n", dent
->size
, dent
->name
);
75 printf("\n%d file(s), %d dir(s)\n\n", nfiles
, ndirs
);
80 static inline int fs_exists_unsupported(const char *filename
)
85 static inline int fs_size_unsupported(const char *filename
, loff_t
*size
)
90 static inline int fs_read_unsupported(const char *filename
, void *buf
,
91 loff_t offset
, loff_t len
,
97 static inline int fs_write_unsupported(const char *filename
, void *buf
,
98 loff_t offset
, loff_t len
,
104 static inline int fs_ln_unsupported(const char *filename
, const char *target
)
109 static inline void fs_close_unsupported(void)
113 static inline int fs_uuid_unsupported(char *uuid_str
)
118 static inline int fs_opendir_unsupported(const char *filename
,
119 struct fs_dir_stream
**dirs
)
124 static inline int fs_unlink_unsupported(const char *filename
)
129 static inline int fs_mkdir_unsupported(const char *dirname
)
138 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
139 * should be false in most cases. For "virtual" filesystems which
140 * aren't based on a U-Boot block device (e.g. sandbox), this can be
141 * set to true. This should also be true for the dummy entry at the end
142 * of fstypes[], since that is essentially a "virtual" (non-existent)
145 bool null_dev_desc_ok
;
146 int (*probe
)(struct blk_desc
*fs_dev_desc
,
147 struct disk_partition
*fs_partition
);
148 int (*ls
)(const char *dirname
);
149 int (*exists
)(const char *filename
);
150 int (*size
)(const char *filename
, loff_t
*size
);
151 int (*read
)(const char *filename
, void *buf
, loff_t offset
,
152 loff_t len
, loff_t
*actread
);
153 int (*write
)(const char *filename
, void *buf
, loff_t offset
,
154 loff_t len
, loff_t
*actwrite
);
156 int (*uuid
)(char *uuid_str
);
158 * Open a directory stream. On success return 0 and directory
159 * stream pointer via 'dirsp'. On error, return -errno. See
162 int (*opendir
)(const char *filename
, struct fs_dir_stream
**dirsp
);
164 * Read next entry from directory stream. On success return 0
165 * and directory entry pointer via 'dentp'. On error return
166 * -errno. See fs_readdir().
168 int (*readdir
)(struct fs_dir_stream
*dirs
, struct fs_dirent
**dentp
);
169 /* see fs_closedir() */
170 void (*closedir
)(struct fs_dir_stream
*dirs
);
171 int (*unlink
)(const char *filename
);
172 int (*mkdir
)(const char *dirname
);
173 int (*ln
)(const char *filename
, const char *target
);
176 static struct fstype_info fstypes
[] = {
179 .fstype
= FS_TYPE_FAT
,
181 .null_dev_desc_ok
= false,
182 .probe
= fat_set_blk_dev
,
185 .exists
= fat_exists
,
187 .read
= fat_read_file
,
188 #if CONFIG_IS_ENABLED(FAT_WRITE)
189 .write
= file_fat_write
,
190 .unlink
= fat_unlink
,
193 .write
= fs_write_unsupported
,
194 .unlink
= fs_unlink_unsupported
,
195 .mkdir
= fs_mkdir_unsupported
,
197 .uuid
= fs_uuid_unsupported
,
198 .opendir
= fat_opendir
,
199 .readdir
= fat_readdir
,
200 .closedir
= fat_closedir
,
201 .ln
= fs_ln_unsupported
,
205 #if CONFIG_IS_ENABLED(FS_EXT4)
207 .fstype
= FS_TYPE_EXT
,
209 .null_dev_desc_ok
= false,
210 .probe
= ext4fs_probe
,
211 .close
= ext4fs_close
,
213 .exists
= ext4fs_exists
,
215 .read
= ext4_read_file
,
216 #ifdef CONFIG_CMD_EXT4_WRITE
217 .write
= ext4_write_file
,
218 .ln
= ext4fs_create_link
,
220 .write
= fs_write_unsupported
,
221 .ln
= fs_ln_unsupported
,
224 .opendir
= fs_opendir_unsupported
,
225 .unlink
= fs_unlink_unsupported
,
226 .mkdir
= fs_mkdir_unsupported
,
229 #ifdef CONFIG_SANDBOX
231 .fstype
= FS_TYPE_SANDBOX
,
233 .null_dev_desc_ok
= true,
234 .probe
= sandbox_fs_set_blk_dev
,
235 .close
= sandbox_fs_close
,
237 .exists
= sandbox_fs_exists
,
238 .size
= sandbox_fs_size
,
239 .read
= fs_read_sandbox
,
240 .write
= fs_write_sandbox
,
241 .uuid
= fs_uuid_unsupported
,
242 .opendir
= fs_opendir_unsupported
,
243 .unlink
= fs_unlink_unsupported
,
244 .mkdir
= fs_mkdir_unsupported
,
245 .ln
= fs_ln_unsupported
,
248 #ifdef CONFIG_CMD_UBIFS
250 .fstype
= FS_TYPE_UBIFS
,
252 .null_dev_desc_ok
= true,
253 .probe
= ubifs_set_blk_dev
,
254 .close
= ubifs_close
,
256 .exists
= ubifs_exists
,
259 .write
= fs_write_unsupported
,
260 .uuid
= fs_uuid_unsupported
,
261 .opendir
= fs_opendir_unsupported
,
262 .unlink
= fs_unlink_unsupported
,
263 .mkdir
= fs_mkdir_unsupported
,
264 .ln
= fs_ln_unsupported
,
267 #ifdef CONFIG_FS_BTRFS
269 .fstype
= FS_TYPE_BTRFS
,
271 .null_dev_desc_ok
= false,
272 .probe
= btrfs_probe
,
273 .close
= btrfs_close
,
275 .exists
= btrfs_exists
,
278 .write
= fs_write_unsupported
,
280 .opendir
= fs_opendir_unsupported
,
281 .unlink
= fs_unlink_unsupported
,
282 .mkdir
= fs_mkdir_unsupported
,
283 .ln
= fs_ln_unsupported
,
286 #if IS_ENABLED(CONFIG_FS_SQUASHFS)
288 .fstype
= FS_TYPE_SQUASHFS
,
291 .opendir
= sqfs_opendir
,
292 .readdir
= sqfs_readdir
,
297 .closedir
= sqfs_closedir
,
301 .fstype
= FS_TYPE_ANY
,
302 .name
= "unsupported",
303 .null_dev_desc_ok
= true,
304 .probe
= fs_probe_unsupported
,
305 .close
= fs_close_unsupported
,
306 .ls
= fs_ls_unsupported
,
307 .exists
= fs_exists_unsupported
,
308 .size
= fs_size_unsupported
,
309 .read
= fs_read_unsupported
,
310 .write
= fs_write_unsupported
,
311 .uuid
= fs_uuid_unsupported
,
312 .opendir
= fs_opendir_unsupported
,
313 .unlink
= fs_unlink_unsupported
,
314 .mkdir
= fs_mkdir_unsupported
,
315 .ln
= fs_ln_unsupported
,
319 static struct fstype_info
*fs_get_info(int fstype
)
321 struct fstype_info
*info
;
324 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
) - 1; i
++, info
++) {
325 if (fstype
== info
->fstype
)
329 /* Return the 'unsupported' sentinel */
334 * fs_get_type() - Get type of current filesystem
336 * Return: filesystem type
338 * Returns filesystem type representing the current filesystem, or
339 * FS_TYPE_ANY for any unrecognised filesystem.
341 int fs_get_type(void)
347 * fs_get_type_name() - Get type of current filesystem
349 * Return: Pointer to filesystem name
351 * Returns a string describing the current filesystem, or the sentinel
352 * "unsupported" for any unrecognised filesystem.
354 const char *fs_get_type_name(void)
356 return fs_get_info(fs_type
)->name
;
359 int fs_set_blk_dev(const char *ifname
, const char *dev_part_str
, int fstype
)
361 struct fstype_info
*info
;
363 #ifdef CONFIG_NEEDS_MANUAL_RELOC
364 static int relocated
;
367 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
);
369 info
->name
+= gd
->reloc_off
;
370 info
->probe
+= gd
->reloc_off
;
371 info
->close
+= gd
->reloc_off
;
372 info
->ls
+= gd
->reloc_off
;
373 info
->read
+= gd
->reloc_off
;
374 info
->write
+= gd
->reloc_off
;
380 part
= blk_get_device_part_str(ifname
, dev_part_str
, &fs_dev_desc
,
385 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
386 if (fstype
!= FS_TYPE_ANY
&& info
->fstype
!= FS_TYPE_ANY
&&
387 fstype
!= info
->fstype
)
390 if (!fs_dev_desc
&& !info
->null_dev_desc_ok
)
393 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
394 fs_type
= info
->fstype
;
403 /* set current blk device w/ blk_desc + partition # */
404 int fs_set_blk_dev_with_part(struct blk_desc
*desc
, int part
)
406 struct fstype_info
*info
;
410 ret
= part_get_info(desc
, part
, &fs_partition
);
412 ret
= part_get_info_whole_disk(desc
, &fs_partition
);
417 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
418 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
419 fs_type
= info
->fstype
;
430 struct fstype_info
*info
= fs_get_info(fs_type
);
434 fs_type
= FS_TYPE_ANY
;
437 int fs_uuid(char *uuid_str
)
439 struct fstype_info
*info
= fs_get_info(fs_type
);
441 return info
->uuid(uuid_str
);
444 int fs_ls(const char *dirname
)
448 struct fstype_info
*info
= fs_get_info(fs_type
);
450 ret
= info
->ls(dirname
);
457 int fs_exists(const char *filename
)
461 struct fstype_info
*info
= fs_get_info(fs_type
);
463 ret
= info
->exists(filename
);
470 int fs_size(const char *filename
, loff_t
*size
)
474 struct fstype_info
*info
= fs_get_info(fs_type
);
476 ret
= info
->size(filename
, size
);
484 /* Check if a file may be read to the given address */
485 static int fs_read_lmb_check(const char *filename
, ulong addr
, loff_t offset
,
486 loff_t len
, struct fstype_info
*info
)
493 /* get the actual size of the file */
494 ret
= info
->size(filename
, &size
);
497 if (offset
>= size
) {
498 /* offset >= EOF, no bytes will be written */
501 read_len
= size
- offset
;
503 /* limit to 'len' if it is smaller */
504 if (len
&& len
< read_len
)
507 lmb_init_and_reserve(&lmb
, gd
->bd
, (void *)gd
->fdt_blob
);
510 if (lmb_alloc_addr(&lmb
, addr
, read_len
) == addr
)
513 log_err("** Reading file would overwrite reserved memory **\n");
518 static int _fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
519 int do_lmb_check
, loff_t
*actread
)
521 struct fstype_info
*info
= fs_get_info(fs_type
);
527 ret
= fs_read_lmb_check(filename
, addr
, offset
, len
, info
);
534 * We don't actually know how many bytes are being read, since len==0
535 * means read the whole file.
537 buf
= map_sysmem(addr
, len
);
538 ret
= info
->read(filename
, buf
, offset
, len
, actread
);
541 /* If we requested a specific number of bytes, check we got it */
542 if (ret
== 0 && len
&& *actread
!= len
)
543 log_debug("** %s shorter than offset + len **\n", filename
);
549 int fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
552 return _fs_read(filename
, addr
, offset
, len
, 0, actread
);
555 int fs_write(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
558 struct fstype_info
*info
= fs_get_info(fs_type
);
562 buf
= map_sysmem(addr
, len
);
563 ret
= info
->write(filename
, buf
, offset
, len
, actwrite
);
566 if (ret
< 0 && len
!= *actwrite
) {
567 log_err("** Unable to write file %s **\n", filename
);
575 struct fs_dir_stream
*fs_opendir(const char *filename
)
577 struct fstype_info
*info
= fs_get_info(fs_type
);
578 struct fs_dir_stream
*dirs
= NULL
;
581 ret
= info
->opendir(filename
, &dirs
);
588 dirs
->desc
= fs_dev_desc
;
589 dirs
->part
= fs_dev_part
;
594 struct fs_dirent
*fs_readdir(struct fs_dir_stream
*dirs
)
596 struct fstype_info
*info
;
597 struct fs_dirent
*dirent
;
600 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
601 info
= fs_get_info(fs_type
);
603 ret
= info
->readdir(dirs
, &dirent
);
613 void fs_closedir(struct fs_dir_stream
*dirs
)
615 struct fstype_info
*info
;
620 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
621 info
= fs_get_info(fs_type
);
623 info
->closedir(dirs
);
627 int fs_unlink(const char *filename
)
631 struct fstype_info
*info
= fs_get_info(fs_type
);
633 ret
= info
->unlink(filename
);
640 int fs_mkdir(const char *dirname
)
644 struct fstype_info
*info
= fs_get_info(fs_type
);
646 ret
= info
->mkdir(dirname
);
653 int fs_ln(const char *fname
, const char *target
)
655 struct fstype_info
*info
= fs_get_info(fs_type
);
658 ret
= info
->ln(fname
, target
);
661 log_err("** Unable to create link %s -> %s **\n", fname
, target
);
669 int do_size(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
675 return CMD_RET_USAGE
;
677 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
680 if (fs_size(argv
[3], &size
) < 0)
681 return CMD_RET_FAILURE
;
683 env_set_hex("filesize", size
);
688 int do_load(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
692 const char *addr_str
;
693 const char *filename
;
702 return CMD_RET_USAGE
;
704 return CMD_RET_USAGE
;
706 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
710 addr
= simple_strtoul(argv
[3], &ep
, 16);
711 if (ep
== argv
[3] || *ep
!= '\0')
712 return CMD_RET_USAGE
;
714 addr_str
= env_get("loadaddr");
715 if (addr_str
!= NULL
)
716 addr
= simple_strtoul(addr_str
, NULL
, 16);
718 addr
= CONFIG_SYS_LOAD_ADDR
;
723 filename
= env_get("bootfile");
725 puts("** No boot file defined **\n");
730 bytes
= simple_strtoul(argv
[5], NULL
, 16);
734 pos
= simple_strtoul(argv
[6], NULL
, 16);
739 ret
= _fs_read(filename
, addr
, pos
, bytes
, 1, &len_read
);
740 time
= get_timer(time
);
742 log_err("Failed to load '%s'\n", filename
);
746 if (IS_ENABLED(CONFIG_CMD_BOOTEFI
))
747 efi_set_bootdev(argv
[1], (argc
> 2) ? argv
[2] : "",
748 (argc
> 4) ? argv
[4] : "");
750 printf("%llu bytes read in %lu ms", len_read
, time
);
753 print_size(div_u64(len_read
, time
) * 1000, "/s");
758 env_set_hex("fileaddr", addr
);
759 env_set_hex("filesize", len_read
);
764 int do_ls(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
768 return CMD_RET_USAGE
;
770 return CMD_RET_USAGE
;
772 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
775 if (fs_ls(argc
>= 4 ? argv
[3] : "/"))
781 int file_exists(const char *dev_type
, const char *dev_part
, const char *file
,
784 if (fs_set_blk_dev(dev_type
, dev_part
, fstype
))
787 return fs_exists(file
);
790 int do_save(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
794 const char *filename
;
801 if (argc
< 6 || argc
> 7)
802 return CMD_RET_USAGE
;
804 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
807 addr
= simple_strtoul(argv
[3], NULL
, 16);
809 bytes
= simple_strtoul(argv
[5], NULL
, 16);
811 pos
= simple_strtoul(argv
[6], NULL
, 16);
816 ret
= fs_write(filename
, addr
, pos
, bytes
, &len
);
817 time
= get_timer(time
);
821 printf("%llu bytes written in %lu ms", len
, time
);
824 print_size(div_u64(len
, time
) * 1000, "/s");
832 int do_fs_uuid(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
837 memset(uuid
, 0, sizeof(uuid
));
839 if (argc
< 3 || argc
> 4)
840 return CMD_RET_USAGE
;
842 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
847 return CMD_RET_FAILURE
;
850 env_set(argv
[3], uuid
);
852 printf("%s\n", uuid
);
854 return CMD_RET_SUCCESS
;
857 int do_fs_type(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[])
859 struct fstype_info
*info
;
861 if (argc
< 3 || argc
> 4)
862 return CMD_RET_USAGE
;
864 if (fs_set_blk_dev(argv
[1], argv
[2], FS_TYPE_ANY
))
867 info
= fs_get_info(fs_type
);
870 env_set(argv
[3], info
->name
);
872 printf("%s\n", info
->name
);
876 return CMD_RET_SUCCESS
;
879 int do_rm(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
883 return CMD_RET_USAGE
;
885 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
888 if (fs_unlink(argv
[3]))
894 int do_mkdir(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
900 return CMD_RET_USAGE
;
902 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
905 ret
= fs_mkdir(argv
[3]);
907 log_err("** Unable to create a directory \"%s\" **\n", argv
[3]);
914 int do_ln(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char *const argv
[],
918 return CMD_RET_USAGE
;
920 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
923 if (fs_ln(argv
[3], argv
[4]))
929 int do_fs_types(struct cmd_tbl
*cmdtp
, int flag
, int argc
, char * const argv
[])
931 struct fstype_info
*drv
= fstypes
;
932 const int n_ents
= ARRAY_SIZE(fstypes
);
933 struct fstype_info
*entry
;
936 puts("Supported filesystems");
937 for (entry
= drv
; entry
!= drv
+ n_ents
; entry
++) {
938 if (entry
->fstype
!= FS_TYPE_ANY
) {
939 printf("%c %s", i
? ',' : ':', entry
->name
);
946 return CMD_RET_SUCCESS
;