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,
175 .write = fs_write_unsupported,
176 .mkdir = fs_mkdir_unsupported,
178 .uuid = fs_uuid_unsupported,
179 .opendir = fat_opendir,
180 .readdir = fat_readdir,
181 .closedir = fat_closedir,
182 .unlink = fs_unlink_unsupported,
185 #ifdef CONFIG_FS_EXT4
187 .fstype = FS_TYPE_EXT,
189 .null_dev_desc_ok = false,
190 .probe = ext4fs_probe,
191 .close = ext4fs_close,
193 .exists = ext4fs_exists,
195 .read = ext4_read_file,
196 #ifdef CONFIG_CMD_EXT4_WRITE
197 .write = ext4_write_file,
199 .write = fs_write_unsupported,
202 .opendir = fs_opendir_unsupported,
203 .unlink = fs_unlink_unsupported,
204 .mkdir = fs_mkdir_unsupported,
207 #ifdef CONFIG_SANDBOX
209 .fstype = FS_TYPE_SANDBOX,
211 .null_dev_desc_ok = true,
212 .probe = sandbox_fs_set_blk_dev,
213 .close = sandbox_fs_close,
215 .exists = sandbox_fs_exists,
216 .size = sandbox_fs_size,
217 .read = fs_read_sandbox,
218 .write = fs_write_sandbox,
219 .uuid = fs_uuid_unsupported,
220 .opendir = fs_opendir_unsupported,
221 .unlink = fs_unlink_unsupported,
222 .mkdir = fs_mkdir_unsupported,
225 #ifdef CONFIG_CMD_UBIFS
227 .fstype = FS_TYPE_UBIFS,
229 .null_dev_desc_ok = true,
230 .probe = ubifs_set_blk_dev,
231 .close = ubifs_close,
233 .exists = ubifs_exists,
236 .write = fs_write_unsupported,
237 .uuid = fs_uuid_unsupported,
238 .opendir = fs_opendir_unsupported,
239 .unlink = fs_unlink_unsupported,
240 .mkdir = fs_mkdir_unsupported,
243 #ifdef CONFIG_FS_BTRFS
245 .fstype = FS_TYPE_BTRFS,
247 .null_dev_desc_ok = false,
248 .probe = btrfs_probe,
249 .close = btrfs_close,
251 .exists = btrfs_exists,
254 .write = fs_write_unsupported,
256 .opendir = fs_opendir_unsupported,
257 .unlink = fs_unlink_unsupported,
258 .mkdir = fs_mkdir_unsupported,
262 .fstype = FS_TYPE_ANY,
263 .name = "unsupported",
264 .null_dev_desc_ok = true,
265 .probe = fs_probe_unsupported,
266 .close = fs_close_unsupported,
267 .ls = fs_ls_unsupported,
268 .exists = fs_exists_unsupported,
269 .size = fs_size_unsupported,
270 .read = fs_read_unsupported,
271 .write = fs_write_unsupported,
272 .uuid = fs_uuid_unsupported,
273 .opendir = fs_opendir_unsupported,
274 .unlink = fs_unlink_unsupported,
275 .mkdir = fs_mkdir_unsupported,
279 static struct fstype_info *fs_get_info(int fstype)
281 struct fstype_info *info;
284 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
285 if (fstype == info->fstype)
289 /* Return the 'unsupported' sentinel */
294 * fs_get_type_name() - Get type of current filesystem
296 * Return: Pointer to filesystem name
298 * Returns a string describing the current filesystem, or the sentinel
299 * "unsupported" for any unrecognised filesystem.
301 const char *fs_get_type_name(void)
303 return fs_get_info(fs_type)->name;
306 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
308 struct fstype_info *info;
310 #ifdef CONFIG_NEEDS_MANUAL_RELOC
311 static int relocated;
314 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
316 info->name += gd->reloc_off;
317 info->probe += gd->reloc_off;
318 info->close += gd->reloc_off;
319 info->ls += gd->reloc_off;
320 info->read += gd->reloc_off;
321 info->write += gd->reloc_off;
327 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
332 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
333 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
334 fstype != info->fstype)
337 if (!fs_dev_desc && !info->null_dev_desc_ok)
340 if (!info->probe(fs_dev_desc, &fs_partition)) {
341 fs_type = info->fstype;
350 /* set current blk device w/ blk_desc + partition # */
351 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
353 struct fstype_info *info;
357 ret = part_get_info(desc, part, &fs_partition);
359 ret = part_get_info_whole_disk(desc, &fs_partition);
364 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
365 if (!info->probe(fs_dev_desc, &fs_partition)) {
366 fs_type = info->fstype;
374 static void fs_close(void)
376 struct fstype_info *info = fs_get_info(fs_type);
380 fs_type = FS_TYPE_ANY;
383 int fs_uuid(char *uuid_str)
385 struct fstype_info *info = fs_get_info(fs_type);
387 return info->uuid(uuid_str);
390 int fs_ls(const char *dirname)
394 struct fstype_info *info = fs_get_info(fs_type);
396 ret = info->ls(dirname);
398 fs_type = FS_TYPE_ANY;
404 int fs_exists(const char *filename)
408 struct fstype_info *info = fs_get_info(fs_type);
410 ret = info->exists(filename);
417 int fs_size(const char *filename, loff_t *size)
421 struct fstype_info *info = fs_get_info(fs_type);
423 ret = info->size(filename, size);
430 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
433 struct fstype_info *info = fs_get_info(fs_type);
438 * We don't actually know how many bytes are being read, since len==0
439 * means read the whole file.
441 buf = map_sysmem(addr, len);
442 ret = info->read(filename, buf, offset, len, actread);
445 /* If we requested a specific number of bytes, check we got it */
446 if (ret == 0 && len && *actread != len)
447 debug("** %s shorter than offset + len **\n", filename);
453 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
456 struct fstype_info *info = fs_get_info(fs_type);
460 buf = map_sysmem(addr, len);
461 ret = info->write(filename, buf, offset, len, actwrite);
464 if (ret < 0 && len != *actwrite) {
465 printf("** Unable to write file %s **\n", filename);
473 struct fs_dir_stream *fs_opendir(const char *filename)
475 struct fstype_info *info = fs_get_info(fs_type);
476 struct fs_dir_stream *dirs = NULL;
479 ret = info->opendir(filename, &dirs);
486 dirs->desc = fs_dev_desc;
487 dirs->part = fs_dev_part;
492 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
494 struct fstype_info *info;
495 struct fs_dirent *dirent;
498 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
499 info = fs_get_info(fs_type);
501 ret = info->readdir(dirs, &dirent);
511 void fs_closedir(struct fs_dir_stream *dirs)
513 struct fstype_info *info;
518 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
519 info = fs_get_info(fs_type);
521 info->closedir(dirs);
525 int fs_unlink(const char *filename)
529 struct fstype_info *info = fs_get_info(fs_type);
531 ret = info->unlink(filename);
533 fs_type = FS_TYPE_ANY;
539 int fs_mkdir(const char *dirname)
543 struct fstype_info *info = fs_get_info(fs_type);
545 ret = info->mkdir(dirname);
547 fs_type = FS_TYPE_ANY;
553 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
559 return CMD_RET_USAGE;
561 if (fs_set_blk_dev(argv[1], argv[2], fstype))
564 if (fs_size(argv[3], &size) < 0)
565 return CMD_RET_FAILURE;
567 env_set_hex("filesize", size);
572 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
576 const char *addr_str;
577 const char *filename;
586 return CMD_RET_USAGE;
588 return CMD_RET_USAGE;
590 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
594 addr = simple_strtoul(argv[3], &ep, 16);
595 if (ep == argv[3] || *ep != '\0')
596 return CMD_RET_USAGE;
598 addr_str = env_get("loadaddr");
599 if (addr_str != NULL)
600 addr = simple_strtoul(addr_str, NULL, 16);
602 addr = CONFIG_SYS_LOAD_ADDR;
607 filename = env_get("bootfile");
609 puts("** No boot file defined **\n");
614 bytes = simple_strtoul(argv[5], NULL, 16);
618 pos = simple_strtoul(argv[6], NULL, 16);
623 ret = fs_read(filename, addr, pos, bytes, &len_read);
624 time = get_timer(time);
628 printf("%llu bytes read in %lu ms", len_read, time);
631 print_size(div_u64(len_read, time) * 1000, "/s");
636 env_set_hex("fileaddr", addr);
637 env_set_hex("filesize", len_read);
642 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
646 return CMD_RET_USAGE;
648 return CMD_RET_USAGE;
650 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
653 if (fs_ls(argc >= 4 ? argv[3] : "/"))
659 int file_exists(const char *dev_type, const char *dev_part, const char *file,
662 if (fs_set_blk_dev(dev_type, dev_part, fstype))
665 return fs_exists(file);
668 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
672 const char *filename;
679 if (argc < 6 || argc > 7)
680 return CMD_RET_USAGE;
682 if (fs_set_blk_dev(argv[1], argv[2], fstype))
685 addr = simple_strtoul(argv[3], NULL, 16);
687 bytes = simple_strtoul(argv[5], NULL, 16);
689 pos = simple_strtoul(argv[6], NULL, 16);
694 ret = fs_write(filename, addr, pos, bytes, &len);
695 time = get_timer(time);
699 printf("%llu bytes written in %lu ms", len, time);
702 print_size(div_u64(len, time) * 1000, "/s");
710 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
715 memset(uuid, 0, sizeof(uuid));
717 if (argc < 3 || argc > 4)
718 return CMD_RET_USAGE;
720 if (fs_set_blk_dev(argv[1], argv[2], fstype))
725 return CMD_RET_FAILURE;
728 env_set(argv[3], uuid);
730 printf("%s\n", uuid);
732 return CMD_RET_SUCCESS;
735 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
737 struct fstype_info *info;
739 if (argc < 3 || argc > 4)
740 return CMD_RET_USAGE;
742 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
745 info = fs_get_info(fs_type);
748 env_set(argv[3], info->name);
750 printf("%s\n", info->name);
752 return CMD_RET_SUCCESS;
755 int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
759 return CMD_RET_USAGE;
761 if (fs_set_blk_dev(argv[1], argv[2], fstype))
764 if (fs_unlink(argv[3]))
770 int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
776 return CMD_RET_USAGE;
778 if (fs_set_blk_dev(argv[1], argv[2], fstype))
781 ret = fs_mkdir(argv[3]);
783 printf("** Unable to create a directory \"%s\" **\n", argv[3]);