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 #if CONFIG_IS_ENABLED(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,
187 #if CONFIG_IS_ENABLED(FS_EXT4)
189 .fstype = FS_TYPE_EXT,
191 .null_dev_desc_ok = false,
192 .probe = ext4fs_probe,
193 .close = ext4fs_close,
195 .exists = ext4fs_exists,
197 .read = ext4_read_file,
198 #ifdef CONFIG_CMD_EXT4_WRITE
199 .write = ext4_write_file,
201 .write = fs_write_unsupported,
204 .opendir = fs_opendir_unsupported,
205 .unlink = fs_unlink_unsupported,
206 .mkdir = fs_mkdir_unsupported,
209 #ifdef CONFIG_SANDBOX
211 .fstype = FS_TYPE_SANDBOX,
213 .null_dev_desc_ok = true,
214 .probe = sandbox_fs_set_blk_dev,
215 .close = sandbox_fs_close,
217 .exists = sandbox_fs_exists,
218 .size = sandbox_fs_size,
219 .read = fs_read_sandbox,
220 .write = fs_write_sandbox,
221 .uuid = fs_uuid_unsupported,
222 .opendir = fs_opendir_unsupported,
223 .unlink = fs_unlink_unsupported,
224 .mkdir = fs_mkdir_unsupported,
227 #ifdef CONFIG_CMD_UBIFS
229 .fstype = FS_TYPE_UBIFS,
231 .null_dev_desc_ok = true,
232 .probe = ubifs_set_blk_dev,
233 .close = ubifs_close,
235 .exists = ubifs_exists,
238 .write = fs_write_unsupported,
239 .uuid = fs_uuid_unsupported,
240 .opendir = fs_opendir_unsupported,
241 .unlink = fs_unlink_unsupported,
242 .mkdir = fs_mkdir_unsupported,
245 #ifdef CONFIG_FS_BTRFS
247 .fstype = FS_TYPE_BTRFS,
249 .null_dev_desc_ok = false,
250 .probe = btrfs_probe,
251 .close = btrfs_close,
253 .exists = btrfs_exists,
256 .write = fs_write_unsupported,
258 .opendir = fs_opendir_unsupported,
259 .unlink = fs_unlink_unsupported,
260 .mkdir = fs_mkdir_unsupported,
264 .fstype = FS_TYPE_ANY,
265 .name = "unsupported",
266 .null_dev_desc_ok = true,
267 .probe = fs_probe_unsupported,
268 .close = fs_close_unsupported,
269 .ls = fs_ls_unsupported,
270 .exists = fs_exists_unsupported,
271 .size = fs_size_unsupported,
272 .read = fs_read_unsupported,
273 .write = fs_write_unsupported,
274 .uuid = fs_uuid_unsupported,
275 .opendir = fs_opendir_unsupported,
276 .unlink = fs_unlink_unsupported,
277 .mkdir = fs_mkdir_unsupported,
281 static struct fstype_info *fs_get_info(int fstype)
283 struct fstype_info *info;
286 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
287 if (fstype == info->fstype)
291 /* Return the 'unsupported' sentinel */
296 * fs_get_type_name() - Get type of current filesystem
298 * Return: Pointer to filesystem name
300 * Returns a string describing the current filesystem, or the sentinel
301 * "unsupported" for any unrecognised filesystem.
303 const char *fs_get_type_name(void)
305 return fs_get_info(fs_type)->name;
308 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
310 struct fstype_info *info;
312 #ifdef CONFIG_NEEDS_MANUAL_RELOC
313 static int relocated;
316 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
318 info->name += gd->reloc_off;
319 info->probe += gd->reloc_off;
320 info->close += gd->reloc_off;
321 info->ls += gd->reloc_off;
322 info->read += gd->reloc_off;
323 info->write += gd->reloc_off;
329 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
334 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
335 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
336 fstype != info->fstype)
339 if (!fs_dev_desc && !info->null_dev_desc_ok)
342 if (!info->probe(fs_dev_desc, &fs_partition)) {
343 fs_type = info->fstype;
352 /* set current blk device w/ blk_desc + partition # */
353 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
355 struct fstype_info *info;
359 ret = part_get_info(desc, part, &fs_partition);
361 ret = part_get_info_whole_disk(desc, &fs_partition);
366 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
367 if (!info->probe(fs_dev_desc, &fs_partition)) {
368 fs_type = info->fstype;
377 static void fs_close(void)
379 struct fstype_info *info = fs_get_info(fs_type);
383 fs_type = FS_TYPE_ANY;
386 int fs_uuid(char *uuid_str)
388 struct fstype_info *info = fs_get_info(fs_type);
390 return info->uuid(uuid_str);
393 int fs_ls(const char *dirname)
397 struct fstype_info *info = fs_get_info(fs_type);
399 ret = info->ls(dirname);
401 fs_type = FS_TYPE_ANY;
407 int fs_exists(const char *filename)
411 struct fstype_info *info = fs_get_info(fs_type);
413 ret = info->exists(filename);
420 int fs_size(const char *filename, loff_t *size)
424 struct fstype_info *info = fs_get_info(fs_type);
426 ret = info->size(filename, size);
434 /* Check if a file may be read to the given address */
435 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
436 loff_t len, struct fstype_info *info)
443 /* get the actual size of the file */
444 ret = info->size(filename, &size);
447 if (offset >= size) {
448 /* offset >= EOF, no bytes will be written */
451 read_len = size - offset;
453 /* limit to 'len' if it is smaller */
454 if (len && len < read_len)
457 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
460 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
463 printf("** Reading file would overwrite reserved memory **\n");
468 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
469 int do_lmb_check, loff_t *actread)
471 struct fstype_info *info = fs_get_info(fs_type);
477 ret = fs_read_lmb_check(filename, addr, offset, len, info);
484 * We don't actually know how many bytes are being read, since len==0
485 * means read the whole file.
487 buf = map_sysmem(addr, len);
488 ret = info->read(filename, buf, offset, len, actread);
491 /* If we requested a specific number of bytes, check we got it */
492 if (ret == 0 && len && *actread != len)
493 debug("** %s shorter than offset + len **\n", filename);
499 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
502 return _fs_read(filename, addr, offset, len, 0, actread);
505 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
508 struct fstype_info *info = fs_get_info(fs_type);
512 buf = map_sysmem(addr, len);
513 ret = info->write(filename, buf, offset, len, actwrite);
516 if (ret < 0 && len != *actwrite) {
517 printf("** Unable to write file %s **\n", filename);
525 struct fs_dir_stream *fs_opendir(const char *filename)
527 struct fstype_info *info = fs_get_info(fs_type);
528 struct fs_dir_stream *dirs = NULL;
531 ret = info->opendir(filename, &dirs);
538 dirs->desc = fs_dev_desc;
539 dirs->part = fs_dev_part;
544 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
546 struct fstype_info *info;
547 struct fs_dirent *dirent;
550 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
551 info = fs_get_info(fs_type);
553 ret = info->readdir(dirs, &dirent);
563 void fs_closedir(struct fs_dir_stream *dirs)
565 struct fstype_info *info;
570 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
571 info = fs_get_info(fs_type);
573 info->closedir(dirs);
577 int fs_unlink(const char *filename)
581 struct fstype_info *info = fs_get_info(fs_type);
583 ret = info->unlink(filename);
585 fs_type = FS_TYPE_ANY;
591 int fs_mkdir(const char *dirname)
595 struct fstype_info *info = fs_get_info(fs_type);
597 ret = info->mkdir(dirname);
599 fs_type = FS_TYPE_ANY;
605 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
611 return CMD_RET_USAGE;
613 if (fs_set_blk_dev(argv[1], argv[2], fstype))
616 if (fs_size(argv[3], &size) < 0)
617 return CMD_RET_FAILURE;
619 env_set_hex("filesize", size);
624 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
628 const char *addr_str;
629 const char *filename;
638 return CMD_RET_USAGE;
640 return CMD_RET_USAGE;
642 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
646 addr = simple_strtoul(argv[3], &ep, 16);
647 if (ep == argv[3] || *ep != '\0')
648 return CMD_RET_USAGE;
650 addr_str = env_get("loadaddr");
651 if (addr_str != NULL)
652 addr = simple_strtoul(addr_str, NULL, 16);
654 addr = CONFIG_SYS_LOAD_ADDR;
659 filename = env_get("bootfile");
661 puts("** No boot file defined **\n");
666 bytes = simple_strtoul(argv[5], NULL, 16);
670 pos = simple_strtoul(argv[6], NULL, 16);
675 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
676 time = get_timer(time);
680 printf("%llu bytes read in %lu ms", len_read, time);
683 print_size(div_u64(len_read, time) * 1000, "/s");
688 env_set_hex("fileaddr", addr);
689 env_set_hex("filesize", len_read);
694 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
698 return CMD_RET_USAGE;
700 return CMD_RET_USAGE;
702 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
705 if (fs_ls(argc >= 4 ? argv[3] : "/"))
711 int file_exists(const char *dev_type, const char *dev_part, const char *file,
714 if (fs_set_blk_dev(dev_type, dev_part, fstype))
717 return fs_exists(file);
720 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
724 const char *filename;
731 if (argc < 6 || argc > 7)
732 return CMD_RET_USAGE;
734 if (fs_set_blk_dev(argv[1], argv[2], fstype))
737 addr = simple_strtoul(argv[3], NULL, 16);
739 bytes = simple_strtoul(argv[5], NULL, 16);
741 pos = simple_strtoul(argv[6], NULL, 16);
746 ret = fs_write(filename, addr, pos, bytes, &len);
747 time = get_timer(time);
751 printf("%llu bytes written in %lu ms", len, time);
754 print_size(div_u64(len, time) * 1000, "/s");
762 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
767 memset(uuid, 0, sizeof(uuid));
769 if (argc < 3 || argc > 4)
770 return CMD_RET_USAGE;
772 if (fs_set_blk_dev(argv[1], argv[2], fstype))
777 return CMD_RET_FAILURE;
780 env_set(argv[3], uuid);
782 printf("%s\n", uuid);
784 return CMD_RET_SUCCESS;
787 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
789 struct fstype_info *info;
791 if (argc < 3 || argc > 4)
792 return CMD_RET_USAGE;
794 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
797 info = fs_get_info(fs_type);
800 env_set(argv[3], info->name);
802 printf("%s\n", info->name);
806 return CMD_RET_SUCCESS;
809 int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
813 return CMD_RET_USAGE;
815 if (fs_set_blk_dev(argv[1], argv[2], fstype))
818 if (fs_unlink(argv[3]))
824 int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
830 return CMD_RET_USAGE;
832 if (fs_set_blk_dev(argv[1], argv[2], fstype))
835 ret = fs_mkdir(argv[3]);
837 printf("** Unable to create a directory \"%s\" **\n", argv[3]);