2 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
4 * SPDX-License-Identifier: GPL-2.0
15 #include <sandboxfs.h>
16 #include <ubifs_uboot.h>
19 #include <linux/math64.h>
21 DECLARE_GLOBAL_DATA_PTR
;
23 static struct blk_desc
*fs_dev_desc
;
24 static int fs_dev_part
;
25 static disk_partition_t fs_partition
;
26 static int fs_type
= FS_TYPE_ANY
;
28 static inline int fs_probe_unsupported(struct blk_desc
*fs_dev_desc
,
29 disk_partition_t
*fs_partition
)
31 printf("** Unrecognized filesystem type **\n");
35 static inline int fs_ls_unsupported(const char *dirname
)
40 /* generic implementation of ls in terms of opendir/readdir/closedir */
42 static int fs_ls_generic(const char *dirname
)
44 struct fs_dir_stream
*dirs
;
45 struct fs_dirent
*dent
;
46 int nfiles
= 0, ndirs
= 0;
48 dirs
= fs_opendir(dirname
);
52 while ((dent
= fs_readdir(dirs
))) {
53 if (dent
->type
== FS_DT_DIR
) {
54 printf(" %s/\n", dent
->name
);
57 printf(" %8lld %s\n", dent
->size
, dent
->name
);
64 printf("\n%d file(s), %d dir(s)\n\n", nfiles
, ndirs
);
69 static inline int fs_exists_unsupported(const char *filename
)
74 static inline int fs_size_unsupported(const char *filename
, loff_t
*size
)
79 static inline int fs_read_unsupported(const char *filename
, void *buf
,
80 loff_t offset
, loff_t len
,
86 static inline int fs_write_unsupported(const char *filename
, void *buf
,
87 loff_t offset
, loff_t len
,
93 static inline void fs_close_unsupported(void)
97 static inline int fs_uuid_unsupported(char *uuid_str
)
102 static inline int fs_opendir_unsupported(const char *filename
,
103 struct fs_dir_stream
**dirs
)
112 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
113 * should be false in most cases. For "virtual" filesystems which
114 * aren't based on a U-Boot block device (e.g. sandbox), this can be
115 * set to true. This should also be true for the dumm entry at the end
116 * of fstypes[], since that is essentially a "virtual" (non-existent)
119 bool null_dev_desc_ok
;
120 int (*probe
)(struct blk_desc
*fs_dev_desc
,
121 disk_partition_t
*fs_partition
);
122 int (*ls
)(const char *dirname
);
123 int (*exists
)(const char *filename
);
124 int (*size
)(const char *filename
, loff_t
*size
);
125 int (*read
)(const char *filename
, void *buf
, loff_t offset
,
126 loff_t len
, loff_t
*actread
);
127 int (*write
)(const char *filename
, void *buf
, loff_t offset
,
128 loff_t len
, loff_t
*actwrite
);
130 int (*uuid
)(char *uuid_str
);
132 * Open a directory stream. On success return 0 and directory
133 * stream pointer via 'dirsp'. On error, return -errno. See
136 int (*opendir
)(const char *filename
, struct fs_dir_stream
**dirsp
);
138 * Read next entry from directory stream. On success return 0
139 * and directory entry pointer via 'dentp'. On error return
140 * -errno. See fs_readdir().
142 int (*readdir
)(struct fs_dir_stream
*dirs
, struct fs_dirent
**dentp
);
143 /* see fs_closedir() */
144 void (*closedir
)(struct fs_dir_stream
*dirs
);
147 static struct fstype_info fstypes
[] = {
150 .fstype
= FS_TYPE_FAT
,
152 .null_dev_desc_ok
= false,
153 .probe
= fat_set_blk_dev
,
156 .exists
= fat_exists
,
158 .read
= fat_read_file
,
159 #ifdef CONFIG_FAT_WRITE
160 .write
= file_fat_write
,
162 .write
= fs_write_unsupported
,
164 .uuid
= fs_uuid_unsupported
,
165 .opendir
= fat_opendir
,
166 .readdir
= fat_readdir
,
167 .closedir
= fat_closedir
,
170 #ifdef CONFIG_FS_EXT4
172 .fstype
= FS_TYPE_EXT
,
174 .null_dev_desc_ok
= false,
175 .probe
= ext4fs_probe
,
176 .close
= ext4fs_close
,
178 .exists
= ext4fs_exists
,
180 .read
= ext4_read_file
,
181 #ifdef CONFIG_CMD_EXT4_WRITE
182 .write
= ext4_write_file
,
184 .write
= fs_write_unsupported
,
187 .opendir
= fs_opendir_unsupported
,
190 #ifdef CONFIG_SANDBOX
192 .fstype
= FS_TYPE_SANDBOX
,
194 .null_dev_desc_ok
= true,
195 .probe
= sandbox_fs_set_blk_dev
,
196 .close
= sandbox_fs_close
,
198 .exists
= sandbox_fs_exists
,
199 .size
= sandbox_fs_size
,
200 .read
= fs_read_sandbox
,
201 .write
= fs_write_sandbox
,
202 .uuid
= fs_uuid_unsupported
,
203 .opendir
= fs_opendir_unsupported
,
206 #ifdef CONFIG_CMD_UBIFS
208 .fstype
= FS_TYPE_UBIFS
,
210 .null_dev_desc_ok
= true,
211 .probe
= ubifs_set_blk_dev
,
212 .close
= ubifs_close
,
214 .exists
= ubifs_exists
,
217 .write
= fs_write_unsupported
,
218 .uuid
= fs_uuid_unsupported
,
219 .opendir
= fs_opendir_unsupported
,
223 .fstype
= FS_TYPE_ANY
,
224 .name
= "unsupported",
225 .null_dev_desc_ok
= true,
226 .probe
= fs_probe_unsupported
,
227 .close
= fs_close_unsupported
,
228 .ls
= fs_ls_unsupported
,
229 .exists
= fs_exists_unsupported
,
230 .size
= fs_size_unsupported
,
231 .read
= fs_read_unsupported
,
232 .write
= fs_write_unsupported
,
233 .uuid
= fs_uuid_unsupported
,
234 .opendir
= fs_opendir_unsupported
,
238 static struct fstype_info
*fs_get_info(int fstype
)
240 struct fstype_info
*info
;
243 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
) - 1; i
++, info
++) {
244 if (fstype
== info
->fstype
)
248 /* Return the 'unsupported' sentinel */
252 int fs_set_blk_dev(const char *ifname
, const char *dev_part_str
, int fstype
)
254 struct fstype_info
*info
;
256 #ifdef CONFIG_NEEDS_MANUAL_RELOC
257 static int relocated
;
260 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
);
262 info
->name
+= gd
->reloc_off
;
263 info
->probe
+= gd
->reloc_off
;
264 info
->close
+= gd
->reloc_off
;
265 info
->ls
+= gd
->reloc_off
;
266 info
->read
+= gd
->reloc_off
;
267 info
->write
+= gd
->reloc_off
;
273 part
= blk_get_device_part_str(ifname
, dev_part_str
, &fs_dev_desc
,
278 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
279 if (fstype
!= FS_TYPE_ANY
&& info
->fstype
!= FS_TYPE_ANY
&&
280 fstype
!= info
->fstype
)
283 if (!fs_dev_desc
&& !info
->null_dev_desc_ok
)
286 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
287 fs_type
= info
->fstype
;
296 /* set current blk device w/ blk_desc + partition # */
297 int fs_set_blk_dev_with_part(struct blk_desc
*desc
, int part
)
299 struct fstype_info
*info
;
303 ret
= part_get_info(desc
, part
, &fs_partition
);
305 ret
= part_get_info_whole_disk(desc
, &fs_partition
);
310 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
311 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
312 fs_type
= info
->fstype
;
320 static void fs_close(void)
322 struct fstype_info
*info
= fs_get_info(fs_type
);
326 fs_type
= FS_TYPE_ANY
;
329 int fs_uuid(char *uuid_str
)
331 struct fstype_info
*info
= fs_get_info(fs_type
);
333 return info
->uuid(uuid_str
);
336 int fs_ls(const char *dirname
)
340 struct fstype_info
*info
= fs_get_info(fs_type
);
342 ret
= info
->ls(dirname
);
344 fs_type
= FS_TYPE_ANY
;
350 int fs_exists(const char *filename
)
354 struct fstype_info
*info
= fs_get_info(fs_type
);
356 ret
= info
->exists(filename
);
363 int fs_size(const char *filename
, loff_t
*size
)
367 struct fstype_info
*info
= fs_get_info(fs_type
);
369 ret
= info
->size(filename
, size
);
376 int fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
379 struct fstype_info
*info
= fs_get_info(fs_type
);
384 * We don't actually know how many bytes are being read, since len==0
385 * means read the whole file.
387 buf
= map_sysmem(addr
, len
);
388 ret
= info
->read(filename
, buf
, offset
, len
, actread
);
391 /* If we requested a specific number of bytes, check we got it */
392 if (ret
== 0 && len
&& *actread
!= len
)
393 printf("** %s shorter than offset + len **\n", filename
);
399 int fs_write(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
402 struct fstype_info
*info
= fs_get_info(fs_type
);
406 buf
= map_sysmem(addr
, len
);
407 ret
= info
->write(filename
, buf
, offset
, len
, actwrite
);
410 if (ret
< 0 && len
!= *actwrite
) {
411 printf("** Unable to write file %s **\n", filename
);
419 struct fs_dir_stream
*fs_opendir(const char *filename
)
421 struct fstype_info
*info
= fs_get_info(fs_type
);
422 struct fs_dir_stream
*dirs
= NULL
;
425 ret
= info
->opendir(filename
, &dirs
);
432 dirs
->desc
= fs_dev_desc
;
433 dirs
->part
= fs_dev_part
;
438 struct fs_dirent
*fs_readdir(struct fs_dir_stream
*dirs
)
440 struct fstype_info
*info
;
441 struct fs_dirent
*dirent
;
444 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
445 info
= fs_get_info(fs_type
);
447 ret
= info
->readdir(dirs
, &dirent
);
457 void fs_closedir(struct fs_dir_stream
*dirs
)
459 struct fstype_info
*info
;
464 fs_set_blk_dev_with_part(dirs
->desc
, dirs
->part
);
465 info
= fs_get_info(fs_type
);
467 info
->closedir(dirs
);
472 int do_size(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
478 return CMD_RET_USAGE
;
480 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
483 if (fs_size(argv
[3], &size
) < 0)
484 return CMD_RET_FAILURE
;
486 env_set_hex("filesize", size
);
491 int do_load(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
495 const char *addr_str
;
496 const char *filename
;
505 return CMD_RET_USAGE
;
507 return CMD_RET_USAGE
;
509 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
513 addr
= simple_strtoul(argv
[3], &ep
, 16);
514 if (ep
== argv
[3] || *ep
!= '\0')
515 return CMD_RET_USAGE
;
517 addr_str
= env_get("loadaddr");
518 if (addr_str
!= NULL
)
519 addr
= simple_strtoul(addr_str
, NULL
, 16);
521 addr
= CONFIG_SYS_LOAD_ADDR
;
526 filename
= env_get("bootfile");
528 puts("** No boot file defined **\n");
533 bytes
= simple_strtoul(argv
[5], NULL
, 16);
537 pos
= simple_strtoul(argv
[6], NULL
, 16);
542 ret
= fs_read(filename
, addr
, pos
, bytes
, &len_read
);
543 time
= get_timer(time
);
547 printf("%llu bytes read in %lu ms", len_read
, time
);
550 print_size(div_u64(len_read
, time
) * 1000, "/s");
555 env_set_hex("fileaddr", addr
);
556 env_set_hex("filesize", len_read
);
561 int do_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
565 return CMD_RET_USAGE
;
567 return CMD_RET_USAGE
;
569 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
572 if (fs_ls(argc
>= 4 ? argv
[3] : "/"))
578 int file_exists(const char *dev_type
, const char *dev_part
, const char *file
,
581 if (fs_set_blk_dev(dev_type
, dev_part
, fstype
))
584 return fs_exists(file
);
587 int do_save(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
591 const char *filename
;
598 if (argc
< 6 || argc
> 7)
599 return CMD_RET_USAGE
;
601 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
604 addr
= simple_strtoul(argv
[3], NULL
, 16);
606 bytes
= simple_strtoul(argv
[5], NULL
, 16);
608 pos
= simple_strtoul(argv
[6], NULL
, 16);
613 ret
= fs_write(filename
, addr
, pos
, bytes
, &len
);
614 time
= get_timer(time
);
618 printf("%llu bytes written in %lu ms", len
, time
);
621 print_size(div_u64(len
, time
) * 1000, "/s");
629 int do_fs_uuid(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
634 memset(uuid
, 0, sizeof(uuid
));
636 if (argc
< 3 || argc
> 4)
637 return CMD_RET_USAGE
;
639 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
644 return CMD_RET_FAILURE
;
647 env_set(argv
[3], uuid
);
649 printf("%s\n", uuid
);
651 return CMD_RET_SUCCESS
;
654 int do_fs_type(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
656 struct fstype_info
*info
;
658 if (argc
< 3 || argc
> 4)
659 return CMD_RET_USAGE
;
661 if (fs_set_blk_dev(argv
[1], argv
[2], FS_TYPE_ANY
))
664 info
= fs_get_info(fs_type
);
667 env_set(argv
[3], info
->name
);
669 printf("%s\n", info
->name
);
671 return CMD_RET_SUCCESS
;