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>
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)
108 static inline int fs_unlink_unsupported(const char *filename)
113 static inline int fs_mkdir_unsupported(const char *dirname)
122 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
123 * should be false in most cases. For "virtual" filesystems which
124 * aren't based on a U-Boot block device (e.g. sandbox), this can be
125 * set to true. This should also be true for the dummy entry at the end
126 * of fstypes[], since that is essentially a "virtual" (non-existent)
129 bool null_dev_desc_ok;
130 int (*probe)(struct blk_desc *fs_dev_desc,
131 disk_partition_t *fs_partition);
132 int (*ls)(const char *dirname);
133 int (*exists)(const char *filename);
134 int (*size)(const char *filename, loff_t *size);
135 int (*read)(const char *filename, void *buf, loff_t offset,
136 loff_t len, loff_t *actread);
137 int (*write)(const char *filename, void *buf, loff_t offset,
138 loff_t len, loff_t *actwrite);
140 int (*uuid)(char *uuid_str);
142 * Open a directory stream. On success return 0 and directory
143 * stream pointer via 'dirsp'. On error, return -errno. See
146 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
148 * Read next entry from directory stream. On success return 0
149 * and directory entry pointer via 'dentp'. On error return
150 * -errno. See fs_readdir().
152 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
153 /* see fs_closedir() */
154 void (*closedir)(struct fs_dir_stream *dirs);
155 int (*unlink)(const char *filename);
156 int (*mkdir)(const char *dirname);
159 static struct fstype_info fstypes[] = {
162 .fstype = FS_TYPE_FAT,
164 .null_dev_desc_ok = false,
165 .probe = fat_set_blk_dev,
168 .exists = fat_exists,
170 .read = fat_read_file,
171 #ifdef CONFIG_FAT_WRITE
172 .write = file_fat_write,
173 .unlink = fat_unlink,
176 .write = fs_write_unsupported,
177 .unlink = fs_unlink_unsupported,
178 .mkdir = fs_mkdir_unsupported,
180 .uuid = fs_uuid_unsupported,
181 .opendir = fat_opendir,
182 .readdir = fat_readdir,
183 .closedir = fat_closedir,
186 #ifdef CONFIG_FS_EXT4
188 .fstype = FS_TYPE_EXT,
190 .null_dev_desc_ok = false,
191 .probe = ext4fs_probe,
192 .close = ext4fs_close,
194 .exists = ext4fs_exists,
196 .read = ext4_read_file,
197 #ifdef CONFIG_CMD_EXT4_WRITE
198 .write = ext4_write_file,
200 .write = fs_write_unsupported,
203 .opendir = fs_opendir_unsupported,
204 .unlink = fs_unlink_unsupported,
205 .mkdir = fs_mkdir_unsupported,
208 #ifdef CONFIG_SANDBOX
210 .fstype = FS_TYPE_SANDBOX,
212 .null_dev_desc_ok = true,
213 .probe = sandbox_fs_set_blk_dev,
214 .close = sandbox_fs_close,
216 .exists = sandbox_fs_exists,
217 .size = sandbox_fs_size,
218 .read = fs_read_sandbox,
219 .write = fs_write_sandbox,
220 .uuid = fs_uuid_unsupported,
221 .opendir = fs_opendir_unsupported,
222 .unlink = fs_unlink_unsupported,
223 .mkdir = fs_mkdir_unsupported,
226 #ifdef CONFIG_CMD_UBIFS
228 .fstype = FS_TYPE_UBIFS,
230 .null_dev_desc_ok = true,
231 .probe = ubifs_set_blk_dev,
232 .close = ubifs_close,
234 .exists = ubifs_exists,
237 .write = fs_write_unsupported,
238 .uuid = fs_uuid_unsupported,
239 .opendir = fs_opendir_unsupported,
240 .unlink = fs_unlink_unsupported,
241 .mkdir = fs_mkdir_unsupported,
244 #ifdef CONFIG_FS_BTRFS
246 .fstype = FS_TYPE_BTRFS,
248 .null_dev_desc_ok = false,
249 .probe = btrfs_probe,
250 .close = btrfs_close,
252 .exists = btrfs_exists,
255 .write = fs_write_unsupported,
257 .opendir = fs_opendir_unsupported,
258 .unlink = fs_unlink_unsupported,
259 .mkdir = fs_mkdir_unsupported,
263 .fstype = FS_TYPE_ANY,
264 .name = "unsupported",
265 .null_dev_desc_ok = true,
266 .probe = fs_probe_unsupported,
267 .close = fs_close_unsupported,
268 .ls = fs_ls_unsupported,
269 .exists = fs_exists_unsupported,
270 .size = fs_size_unsupported,
271 .read = fs_read_unsupported,
272 .write = fs_write_unsupported,
273 .uuid = fs_uuid_unsupported,
274 .opendir = fs_opendir_unsupported,
275 .unlink = fs_unlink_unsupported,
276 .mkdir = fs_mkdir_unsupported,
280 static struct fstype_info *fs_get_info(int fstype)
282 struct fstype_info *info;
285 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
286 if (fstype == info->fstype)
290 /* Return the 'unsupported' sentinel */
295 * fs_get_type_name() - Get type of current filesystem
297 * Return: Pointer to filesystem name
299 * Returns a string describing the current filesystem, or the sentinel
300 * "unsupported" for any unrecognised filesystem.
302 const char *fs_get_type_name(void)
304 return fs_get_info(fs_type)->name;
307 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
309 struct fstype_info *info;
311 #ifdef CONFIG_NEEDS_MANUAL_RELOC
312 static int relocated;
315 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
317 info->name += gd->reloc_off;
318 info->probe += gd->reloc_off;
319 info->close += gd->reloc_off;
320 info->ls += gd->reloc_off;
321 info->read += gd->reloc_off;
322 info->write += gd->reloc_off;
328 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
333 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
334 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
335 fstype != info->fstype)
338 if (!fs_dev_desc && !info->null_dev_desc_ok)
341 if (!info->probe(fs_dev_desc, &fs_partition)) {
342 fs_type = info->fstype;
351 /* set current blk device w/ blk_desc + partition # */
352 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
354 struct fstype_info *info;
358 ret = part_get_info(desc, part, &fs_partition);
360 ret = part_get_info_whole_disk(desc, &fs_partition);
365 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
366 if (!info->probe(fs_dev_desc, &fs_partition)) {
367 fs_type = info->fstype;
375 static void fs_close(void)
377 struct fstype_info *info = fs_get_info(fs_type);
381 fs_type = FS_TYPE_ANY;
384 int fs_uuid(char *uuid_str)
386 struct fstype_info *info = fs_get_info(fs_type);
388 return info->uuid(uuid_str);
391 int fs_ls(const char *dirname)
395 struct fstype_info *info = fs_get_info(fs_type);
397 ret = info->ls(dirname);
399 fs_type = FS_TYPE_ANY;
405 int fs_exists(const char *filename)
409 struct fstype_info *info = fs_get_info(fs_type);
411 ret = info->exists(filename);
418 int fs_size(const char *filename, loff_t *size)
422 struct fstype_info *info = fs_get_info(fs_type);
424 ret = info->size(filename, size);
431 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
434 struct fstype_info *info = fs_get_info(fs_type);
439 * We don't actually know how many bytes are being read, since len==0
440 * means read the whole file.
442 buf = map_sysmem(addr, len);
443 ret = info->read(filename, buf, offset, len, actread);
446 /* If we requested a specific number of bytes, check we got it */
447 if (ret == 0 && len && *actread != len)
448 debug("** %s shorter than offset + len **\n", filename);
454 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
457 struct fstype_info *info = fs_get_info(fs_type);
461 buf = map_sysmem(addr, len);
462 ret = info->write(filename, buf, offset, len, actwrite);
465 if (ret < 0 && len != *actwrite) {
466 printf("** Unable to write file %s **\n", filename);
474 struct fs_dir_stream *fs_opendir(const char *filename)
476 struct fstype_info *info = fs_get_info(fs_type);
477 struct fs_dir_stream *dirs = NULL;
480 ret = info->opendir(filename, &dirs);
487 dirs->desc = fs_dev_desc;
488 dirs->part = fs_dev_part;
493 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
495 struct fstype_info *info;
496 struct fs_dirent *dirent;
499 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
500 info = fs_get_info(fs_type);
502 ret = info->readdir(dirs, &dirent);
512 void fs_closedir(struct fs_dir_stream *dirs)
514 struct fstype_info *info;
519 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
520 info = fs_get_info(fs_type);
522 info->closedir(dirs);
526 int fs_unlink(const char *filename)
530 struct fstype_info *info = fs_get_info(fs_type);
532 ret = info->unlink(filename);
534 fs_type = FS_TYPE_ANY;
540 int fs_mkdir(const char *dirname)
544 struct fstype_info *info = fs_get_info(fs_type);
546 ret = info->mkdir(dirname);
548 fs_type = FS_TYPE_ANY;
554 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
560 return CMD_RET_USAGE;
562 if (fs_set_blk_dev(argv[1], argv[2], fstype))
565 if (fs_size(argv[3], &size) < 0)
566 return CMD_RET_FAILURE;
568 env_set_hex("filesize", size);
573 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
577 const char *addr_str;
578 const char *filename;
587 return CMD_RET_USAGE;
589 return CMD_RET_USAGE;
591 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
595 addr = simple_strtoul(argv[3], &ep, 16);
596 if (ep == argv[3] || *ep != '\0')
597 return CMD_RET_USAGE;
599 addr_str = env_get("loadaddr");
600 if (addr_str != NULL)
601 addr = simple_strtoul(addr_str, NULL, 16);
603 addr = CONFIG_SYS_LOAD_ADDR;
608 filename = env_get("bootfile");
610 puts("** No boot file defined **\n");
615 bytes = simple_strtoul(argv[5], NULL, 16);
619 pos = simple_strtoul(argv[6], NULL, 16);
624 ret = fs_read(filename, addr, pos, bytes, &len_read);
625 time = get_timer(time);
629 printf("%llu bytes read in %lu ms", len_read, time);
632 print_size(div_u64(len_read, time) * 1000, "/s");
637 env_set_hex("fileaddr", addr);
638 env_set_hex("filesize", len_read);
643 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
647 return CMD_RET_USAGE;
649 return CMD_RET_USAGE;
651 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
654 if (fs_ls(argc >= 4 ? argv[3] : "/"))
660 int file_exists(const char *dev_type, const char *dev_part, const char *file,
663 if (fs_set_blk_dev(dev_type, dev_part, fstype))
666 return fs_exists(file);
669 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
673 const char *filename;
680 if (argc < 6 || argc > 7)
681 return CMD_RET_USAGE;
683 if (fs_set_blk_dev(argv[1], argv[2], fstype))
686 addr = simple_strtoul(argv[3], NULL, 16);
688 bytes = simple_strtoul(argv[5], NULL, 16);
690 pos = simple_strtoul(argv[6], NULL, 16);
695 ret = fs_write(filename, addr, pos, bytes, &len);
696 time = get_timer(time);
700 printf("%llu bytes written in %lu ms", len, time);
703 print_size(div_u64(len, time) * 1000, "/s");
711 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
716 memset(uuid, 0, sizeof(uuid));
718 if (argc < 3 || argc > 4)
719 return CMD_RET_USAGE;
721 if (fs_set_blk_dev(argv[1], argv[2], fstype))
726 return CMD_RET_FAILURE;
729 env_set(argv[3], uuid);
731 printf("%s\n", uuid);
733 return CMD_RET_SUCCESS;
736 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
738 struct fstype_info *info;
740 if (argc < 3 || argc > 4)
741 return CMD_RET_USAGE;
743 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
746 info = fs_get_info(fs_type);
749 env_set(argv[3], info->name);
751 printf("%s\n", info->name);
753 return CMD_RET_SUCCESS;
756 int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
760 return CMD_RET_USAGE;
762 if (fs_set_blk_dev(argv[1], argv[2], fstype))
765 if (fs_unlink(argv[3]))
771 int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
777 return CMD_RET_USAGE;
779 if (fs_set_blk_dev(argv[1], argv[2], fstype))
782 ret = fs_mkdir(argv[3]);
784 printf("** Unable to create a directory \"%s\" **\n", argv[3]);