1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
14 #include <sandboxfs.h>
15 #include <ubifs_uboot.h>
19 #include <linux/math64.h>
20 #include <efi_loader.h>
22 DECLARE_GLOBAL_DATA_PTR
;
24 static struct blk_desc
*fs_dev_desc
;
25 static int fs_dev_part
;
26 static disk_partition_t fs_partition
;
27 static int fs_type
= FS_TYPE_ANY
;
29 static inline int fs_probe_unsupported(struct blk_desc
*fs_dev_desc
,
30 disk_partition_t
*fs_partition
)
32 printf("** Unrecognized filesystem type **\n");
36 static inline int fs_ls_unsupported(const char *dirname
)
41 /* generic implementation of ls in terms of opendir/readdir/closedir */
43 static int fs_ls_generic(const char *dirname
)
45 struct fs_dir_stream
*dirs
;
46 struct fs_dirent
*dent
;
47 int nfiles
= 0, ndirs
= 0;
49 dirs
= fs_opendir(dirname
);
53 while ((dent
= fs_readdir(dirs
))) {
54 if (dent
->type
== FS_DT_DIR
) {
55 printf(" %s/\n", dent
->name
);
58 printf(" %8lld %s\n", dent
->size
, dent
->name
);
65 printf("\n%d file(s), %d dir(s)\n\n", nfiles
, ndirs
);
70 static inline int fs_exists_unsupported(const char *filename
)
75 static inline int fs_size_unsupported(const char *filename
, loff_t
*size
)
80 static inline int fs_read_unsupported(const char *filename
, void *buf
,
81 loff_t offset
, loff_t len
,
87 static inline int fs_write_unsupported(const char *filename
, void *buf
,
88 loff_t offset
, loff_t len
,
94 static inline int fs_ln_unsupported(const char *filename
, const char *target
)
99 static inline void fs_close_unsupported(void)
103 static inline int fs_uuid_unsupported(char *uuid_str
)
108 static inline int fs_opendir_unsupported(const char *filename
,
109 struct fs_dir_stream
**dirs
)
114 static inline int fs_unlink_unsupported(const char *filename
)
119 static inline int fs_mkdir_unsupported(const char *dirname
)
128 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
129 * should be false in most cases. For "virtual" filesystems which
130 * aren't based on a U-Boot block device (e.g. sandbox), this can be
131 * set to true. This should also be true for the dummy entry at the end
132 * of fstypes[], since that is essentially a "virtual" (non-existent)
135 bool null_dev_desc_ok
;
136 int (*probe
)(struct blk_desc
*fs_dev_desc
,
137 disk_partition_t
*fs_partition
);
138 int (*ls
)(const char *dirname
);
139 int (*exists
)(const char *filename
);
140 int (*size
)(const char *filename
, loff_t
*size
);
141 int (*read
)(const char *filename
, void *buf
, loff_t offset
,
142 loff_t len
, loff_t
*actread
);
143 int (*write
)(const char *filename
, void *buf
, loff_t offset
,
144 loff_t len
, loff_t
*actwrite
);
146 int (*uuid
)(char *uuid_str
);
148 * Open a directory stream. On success return 0 and directory
149 * stream pointer via 'dirsp'. On error, return -errno. See
152 int (*opendir
)(const char *filename
, struct fs_dir_stream
**dirsp
);
154 * Read next entry from directory stream. On success return 0
155 * and directory entry pointer via 'dentp'. On error return
156 * -errno. See fs_readdir().
158 int (*readdir
)(struct fs_dir_stream
*dirs
, struct fs_dirent
**dentp
);
159 /* see fs_closedir() */
160 void (*closedir
)(struct fs_dir_stream
*dirs
);
161 int (*unlink
)(const char *filename
);
162 int (*mkdir
)(const char *dirname
);
163 int (*ln
)(const char *filename
, const char *target
);
166 static struct fstype_info fstypes
[] = {
169 .fstype
= FS_TYPE_FAT
,
171 .null_dev_desc_ok
= false,
172 .probe
= fat_set_blk_dev
,
175 .exists
= fat_exists
,
177 .read
= fat_read_file
,
178 #if CONFIG_IS_ENABLED(FAT_WRITE)
179 .write
= file_fat_write
,
180 .unlink
= fat_unlink
,
183 .write
= fs_write_unsupported
,
184 .unlink
= fs_unlink_unsupported
,
185 .mkdir
= fs_mkdir_unsupported
,
187 .uuid
= fs_uuid_unsupported
,
188 .opendir
= fat_opendir
,
189 .readdir
= fat_readdir
,
190 .closedir
= fat_closedir
,
191 .ln
= fs_ln_unsupported
,
195 #if CONFIG_IS_ENABLED(FS_EXT4)
197 .fstype
= FS_TYPE_EXT
,
199 .null_dev_desc_ok
= false,
200 .probe
= ext4fs_probe
,
201 .close
= ext4fs_close
,
203 .exists
= ext4fs_exists
,
205 .read
= ext4_read_file
,
206 #ifdef CONFIG_CMD_EXT4_WRITE
207 .write
= ext4_write_file
,
208 .ln
= ext4fs_create_link
,
210 .write
= fs_write_unsupported
,
211 .ln
= fs_ln_unsupported
,
214 .opendir
= fs_opendir_unsupported
,
215 .unlink
= fs_unlink_unsupported
,
216 .mkdir
= fs_mkdir_unsupported
,
219 #ifdef CONFIG_SANDBOX
221 .fstype
= FS_TYPE_SANDBOX
,
223 .null_dev_desc_ok
= true,
224 .probe
= sandbox_fs_set_blk_dev
,
225 .close
= sandbox_fs_close
,
227 .exists
= sandbox_fs_exists
,
228 .size
= sandbox_fs_size
,
229 .read
= fs_read_sandbox
,
230 .write
= fs_write_sandbox
,
231 .uuid
= fs_uuid_unsupported
,
232 .opendir
= fs_opendir_unsupported
,
233 .unlink
= fs_unlink_unsupported
,
234 .mkdir
= fs_mkdir_unsupported
,
235 .ln
= fs_ln_unsupported
,
238 #ifdef CONFIG_CMD_UBIFS
240 .fstype
= FS_TYPE_UBIFS
,
242 .null_dev_desc_ok
= true,
243 .probe
= ubifs_set_blk_dev
,
244 .close
= ubifs_close
,
246 .exists
= ubifs_exists
,
249 .write
= fs_write_unsupported
,
250 .uuid
= fs_uuid_unsupported
,
251 .opendir
= fs_opendir_unsupported
,
252 .unlink
= fs_unlink_unsupported
,
253 .mkdir
= fs_mkdir_unsupported
,
254 .ln
= fs_ln_unsupported
,
257 #ifdef CONFIG_FS_BTRFS
259 .fstype
= FS_TYPE_BTRFS
,
261 .null_dev_desc_ok
= false,
262 .probe
= btrfs_probe
,
263 .close
= btrfs_close
,
265 .exists
= btrfs_exists
,
268 .write
= fs_write_unsupported
,
270 .opendir
= fs_opendir_unsupported
,
271 .unlink
= fs_unlink_unsupported
,
272 .mkdir
= fs_mkdir_unsupported
,
273 .ln
= fs_ln_unsupported
,
277 .fstype
= FS_TYPE_ANY
,
278 .name
= "unsupported",
279 .null_dev_desc_ok
= true,
280 .probe
= fs_probe_unsupported
,
281 .close
= fs_close_unsupported
,
282 .ls
= fs_ls_unsupported
,
283 .exists
= fs_exists_unsupported
,
284 .size
= fs_size_unsupported
,
285 .read
= fs_read_unsupported
,
286 .write
= fs_write_unsupported
,
287 .uuid
= fs_uuid_unsupported
,
288 .opendir
= fs_opendir_unsupported
,
289 .unlink
= fs_unlink_unsupported
,
290 .mkdir
= fs_mkdir_unsupported
,
291 .ln
= fs_ln_unsupported
,
295 static struct fstype_info
*fs_get_info(int fstype
)
297 struct fstype_info
*info
;
300 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
) - 1; i
++, info
++) {
301 if (fstype
== info
->fstype
)
305 /* Return the 'unsupported' sentinel */
310 * fs_get_type_name() - Get type of current filesystem
312 * Return: Pointer to filesystem name
314 * Returns a string describing the current filesystem, or the sentinel
315 * "unsupported" for any unrecognised filesystem.
317 const char *fs_get_type_name(void)
319 return fs_get_info(fs_type
)->name
;
322 int fs_set_blk_dev(const char *ifname
, const char *dev_part_str
, int fstype
)
324 struct fstype_info
*info
;
326 #ifdef CONFIG_NEEDS_MANUAL_RELOC
327 static int relocated
;
330 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
);
332 info
->name
+= gd
->reloc_off
;
333 info
->probe
+= gd
->reloc_off
;
334 info
->close
+= gd
->reloc_off
;
335 info
->ls
+= gd
->reloc_off
;
336 info
->read
+= gd
->reloc_off
;
337 info
->write
+= gd
->reloc_off
;
343 part
= blk_get_device_part_str(ifname
, dev_part_str
, &fs_dev_desc
,
348 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
349 if (fstype
!= FS_TYPE_ANY
&& info
->fstype
!= FS_TYPE_ANY
&&
350 fstype
!= info
->fstype
)
353 if (!fs_dev_desc
&& !info
->null_dev_desc_ok
)
356 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
357 fs_type
= info
->fstype
;
366 /* set current blk device w/ blk_desc + partition # */
367 int fs_set_blk_dev_with_part(struct blk_desc
*desc
, int part
)
369 struct fstype_info
*info
;
373 ret
= part_get_info(desc
, part
, &fs_partition
);
375 ret
= part_get_info_whole_disk(desc
, &fs_partition
);
380 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
381 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
382 fs_type
= info
->fstype
;
391 static void fs_close(void)
393 struct fstype_info
*info
= fs_get_info(fs_type
);
397 fs_type
= FS_TYPE_ANY
;
400 int fs_uuid(char *uuid_str
)
402 struct fstype_info
*info
= fs_get_info(fs_type
);
404 return info
->uuid(uuid_str
);
407 int fs_ls(const char *dirname
)
411 struct fstype_info
*info
= fs_get_info(fs_type
);
413 ret
= info
->ls(dirname
);
415 fs_type
= FS_TYPE_ANY
;
421 int fs_exists(const char *filename
)
425 struct fstype_info
*info
= fs_get_info(fs_type
);
427 ret
= info
->exists(filename
);
434 int fs_size(const char *filename
, loff_t
*size
)
438 struct fstype_info
*info
= fs_get_info(fs_type
);
440 ret
= info
->size(filename
, size
);
448 /* Check if a file may be read to the given address */
449 static int fs_read_lmb_check(const char *filename
, ulong addr
, loff_t offset
,
450 loff_t len
, struct fstype_info
*info
)
457 /* get the actual size of the file */
458 ret
= info
->size(filename
, &size
);
461 if (offset
>= size
) {
462 /* offset >= EOF, no bytes will be written */
465 read_len
= size
- offset
;
467 /* limit to 'len' if it is smaller */
468 if (len
&& len
< read_len
)
471 lmb_init_and_reserve(&lmb
, gd
->bd
, (void *)gd
->fdt_blob
);
474 if (lmb_alloc_addr(&lmb
, addr
, read_len
) == addr
)
477 printf("** Reading file would overwrite reserved memory **\n");
482 static int _fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
483 int do_lmb_check
, loff_t
*actread
)
485 struct fstype_info
*info
= fs_get_info(fs_type
);
491 ret
= fs_read_lmb_check(filename
, addr
, offset
, len
, info
);
498 * We don't actually know how many bytes are being read, since len==0
499 * means read the whole file.
501 buf
= map_sysmem(addr
, len
);
502 ret
= info
->read(filename
, buf
, offset
, len
, actread
);
505 /* If we requested a specific number of bytes, check we got it */
506 if (ret
== 0 && len
&& *actread
!= len
)
507 debug("** %s shorter than offset + len **\n", filename
);
513 int fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
516 return _fs_read(filename
, addr
, offset
, len
, 0, actread
);
519 int fs_write(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
522 struct fstype_info
*info
= fs_get_info(fs_type
);
526 buf
= map_sysmem(addr
, len
);
527 ret
= info
->write(filename
, buf
, offset
, len
, actwrite
);
530 if (ret
< 0 && len
!= *actwrite
) {
531 printf("** Unable to write file %s **\n", filename
);
539 struct fs_dir_stream
*fs_opendir(const char *filename
)
541 struct fstype_info
*info
= fs_get_info(fs_type
);
542 struct fs_dir_stream
*dirs
= NULL
;
545 ret
= info
->opendir(filename
, &dirs
);
552 dirs
->desc
= fs_dev_desc
;
553 dirs
->part
= fs_dev_part
;
558 struct fs_dirent
*fs_readdir(struct fs_dir_stream
*dirs
)
560 struct fstype_info
*info
;
561 struct fs_dirent
*dirent
;
564 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
565 info
= fs_get_info(fs_type
);
567 ret
= info
->readdir(dirs
, &dirent
);
577 void fs_closedir(struct fs_dir_stream
*dirs
)
579 struct fstype_info
*info
;
584 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
585 info
= fs_get_info(fs_type
);
587 info
->closedir(dirs
);
591 int fs_unlink(const char *filename
)
595 struct fstype_info
*info
= fs_get_info(fs_type
);
597 ret
= info
->unlink(filename
);
599 fs_type
= FS_TYPE_ANY
;
605 int fs_mkdir(const char *dirname
)
609 struct fstype_info
*info
= fs_get_info(fs_type
);
611 ret
= info
->mkdir(dirname
);
613 fs_type
= FS_TYPE_ANY
;
619 int fs_ln(const char *fname
, const char *target
)
621 struct fstype_info
*info
= fs_get_info(fs_type
);
624 ret
= info
->ln(fname
, target
);
627 printf("** Unable to create link %s -> %s **\n", fname
, target
);
635 int do_size(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
641 return CMD_RET_USAGE
;
643 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
646 if (fs_size(argv
[3], &size
) < 0)
647 return CMD_RET_FAILURE
;
649 env_set_hex("filesize", size
);
654 int do_load(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
658 const char *addr_str
;
659 const char *filename
;
668 return CMD_RET_USAGE
;
670 return CMD_RET_USAGE
;
672 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
676 addr
= simple_strtoul(argv
[3], &ep
, 16);
677 if (ep
== argv
[3] || *ep
!= '\0')
678 return CMD_RET_USAGE
;
680 addr_str
= env_get("loadaddr");
681 if (addr_str
!= NULL
)
682 addr
= simple_strtoul(addr_str
, NULL
, 16);
684 addr
= CONFIG_SYS_LOAD_ADDR
;
689 filename
= env_get("bootfile");
691 puts("** No boot file defined **\n");
696 bytes
= simple_strtoul(argv
[5], NULL
, 16);
700 pos
= simple_strtoul(argv
[6], NULL
, 16);
704 #ifdef CONFIG_CMD_BOOTEFI
705 efi_set_bootdev(argv
[1], (argc
> 2) ? argv
[2] : "",
706 (argc
> 4) ? argv
[4] : "");
709 ret
= _fs_read(filename
, addr
, pos
, bytes
, 1, &len_read
);
710 time
= get_timer(time
);
714 printf("%llu bytes read in %lu ms", len_read
, time
);
717 print_size(div_u64(len_read
, time
) * 1000, "/s");
722 env_set_hex("fileaddr", addr
);
723 env_set_hex("filesize", len_read
);
728 int do_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
732 return CMD_RET_USAGE
;
734 return CMD_RET_USAGE
;
736 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
739 if (fs_ls(argc
>= 4 ? argv
[3] : "/"))
745 int file_exists(const char *dev_type
, const char *dev_part
, const char *file
,
748 if (fs_set_blk_dev(dev_type
, dev_part
, fstype
))
751 return fs_exists(file
);
754 int do_save(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
758 const char *filename
;
765 if (argc
< 6 || argc
> 7)
766 return CMD_RET_USAGE
;
768 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
771 addr
= simple_strtoul(argv
[3], NULL
, 16);
773 bytes
= simple_strtoul(argv
[5], NULL
, 16);
775 pos
= simple_strtoul(argv
[6], NULL
, 16);
780 ret
= fs_write(filename
, addr
, pos
, bytes
, &len
);
781 time
= get_timer(time
);
785 printf("%llu bytes written in %lu ms", len
, time
);
788 print_size(div_u64(len
, time
) * 1000, "/s");
796 int do_fs_uuid(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
801 memset(uuid
, 0, sizeof(uuid
));
803 if (argc
< 3 || argc
> 4)
804 return CMD_RET_USAGE
;
806 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
811 return CMD_RET_FAILURE
;
814 env_set(argv
[3], uuid
);
816 printf("%s\n", uuid
);
818 return CMD_RET_SUCCESS
;
821 int do_fs_type(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
823 struct fstype_info
*info
;
825 if (argc
< 3 || argc
> 4)
826 return CMD_RET_USAGE
;
828 if (fs_set_blk_dev(argv
[1], argv
[2], FS_TYPE_ANY
))
831 info
= fs_get_info(fs_type
);
834 env_set(argv
[3], info
->name
);
836 printf("%s\n", info
->name
);
840 return CMD_RET_SUCCESS
;
843 int do_rm(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
847 return CMD_RET_USAGE
;
849 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
852 if (fs_unlink(argv
[3]))
858 int do_mkdir(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
864 return CMD_RET_USAGE
;
866 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
869 ret
= fs_mkdir(argv
[3]);
871 printf("** Unable to create a directory \"%s\" **\n", argv
[3]);
878 int do_ln(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
882 return CMD_RET_USAGE
;
884 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
887 if (fs_ln(argv
[3], argv
[4]))