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_mkdir_unsupported(const char *dirname)
117 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
118 * should be false in most cases. For "virtual" filesystems which
119 * aren't based on a U-Boot block device (e.g. sandbox), this can be
120 * set to true. This should also be true for the dummy entry at the end
121 * of fstypes[], since that is essentially a "virtual" (non-existent)
124 bool null_dev_desc_ok;
125 int (*probe)(struct blk_desc *fs_dev_desc,
126 disk_partition_t *fs_partition);
127 int (*ls)(const char *dirname);
128 int (*exists)(const char *filename);
129 int (*size)(const char *filename, loff_t *size);
130 int (*read)(const char *filename, void *buf, loff_t offset,
131 loff_t len, loff_t *actread);
132 int (*write)(const char *filename, void *buf, loff_t offset,
133 loff_t len, loff_t *actwrite);
135 int (*uuid)(char *uuid_str);
137 * Open a directory stream. On success return 0 and directory
138 * stream pointer via 'dirsp'. On error, return -errno. See
141 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
143 * Read next entry from directory stream. On success return 0
144 * and directory entry pointer via 'dentp'. On error return
145 * -errno. See fs_readdir().
147 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
148 /* see fs_closedir() */
149 void (*closedir)(struct fs_dir_stream *dirs);
150 int (*mkdir)(const char *dirname);
153 static struct fstype_info fstypes[] = {
156 .fstype = FS_TYPE_FAT,
158 .null_dev_desc_ok = false,
159 .probe = fat_set_blk_dev,
162 .exists = fat_exists,
164 .read = fat_read_file,
165 #ifdef CONFIG_FAT_WRITE
166 .write = file_fat_write,
169 .write = fs_write_unsupported,
170 .mkdir = fs_mkdir_unsupported,
172 .uuid = fs_uuid_unsupported,
173 .opendir = fat_opendir,
174 .readdir = fat_readdir,
175 .closedir = fat_closedir,
178 #ifdef CONFIG_FS_EXT4
180 .fstype = FS_TYPE_EXT,
182 .null_dev_desc_ok = false,
183 .probe = ext4fs_probe,
184 .close = ext4fs_close,
186 .exists = ext4fs_exists,
188 .read = ext4_read_file,
189 #ifdef CONFIG_CMD_EXT4_WRITE
190 .write = ext4_write_file,
192 .write = fs_write_unsupported,
195 .opendir = fs_opendir_unsupported,
196 .mkdir = fs_mkdir_unsupported,
199 #ifdef CONFIG_SANDBOX
201 .fstype = FS_TYPE_SANDBOX,
203 .null_dev_desc_ok = true,
204 .probe = sandbox_fs_set_blk_dev,
205 .close = sandbox_fs_close,
207 .exists = sandbox_fs_exists,
208 .size = sandbox_fs_size,
209 .read = fs_read_sandbox,
210 .write = fs_write_sandbox,
211 .uuid = fs_uuid_unsupported,
212 .opendir = fs_opendir_unsupported,
213 .mkdir = fs_mkdir_unsupported,
216 #ifdef CONFIG_CMD_UBIFS
218 .fstype = FS_TYPE_UBIFS,
220 .null_dev_desc_ok = true,
221 .probe = ubifs_set_blk_dev,
222 .close = ubifs_close,
224 .exists = ubifs_exists,
227 .write = fs_write_unsupported,
228 .uuid = fs_uuid_unsupported,
229 .opendir = fs_opendir_unsupported,
230 .mkdir = fs_mkdir_unsupported,
233 #ifdef CONFIG_FS_BTRFS
235 .fstype = FS_TYPE_BTRFS,
237 .null_dev_desc_ok = false,
238 .probe = btrfs_probe,
239 .close = btrfs_close,
241 .exists = btrfs_exists,
244 .write = fs_write_unsupported,
246 .opendir = fs_opendir_unsupported,
247 .mkdir = fs_mkdir_unsupported,
251 .fstype = FS_TYPE_ANY,
252 .name = "unsupported",
253 .null_dev_desc_ok = true,
254 .probe = fs_probe_unsupported,
255 .close = fs_close_unsupported,
256 .ls = fs_ls_unsupported,
257 .exists = fs_exists_unsupported,
258 .size = fs_size_unsupported,
259 .read = fs_read_unsupported,
260 .write = fs_write_unsupported,
261 .uuid = fs_uuid_unsupported,
262 .opendir = fs_opendir_unsupported,
263 .mkdir = fs_mkdir_unsupported,
267 static struct fstype_info *fs_get_info(int fstype)
269 struct fstype_info *info;
272 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
273 if (fstype == info->fstype)
277 /* Return the 'unsupported' sentinel */
282 * fs_get_type_name() - Get type of current filesystem
284 * Return: Pointer to filesystem name
286 * Returns a string describing the current filesystem, or the sentinel
287 * "unsupported" for any unrecognised filesystem.
289 const char *fs_get_type_name(void)
291 return fs_get_info(fs_type)->name;
294 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
296 struct fstype_info *info;
298 #ifdef CONFIG_NEEDS_MANUAL_RELOC
299 static int relocated;
302 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
304 info->name += gd->reloc_off;
305 info->probe += gd->reloc_off;
306 info->close += gd->reloc_off;
307 info->ls += gd->reloc_off;
308 info->read += gd->reloc_off;
309 info->write += gd->reloc_off;
315 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
320 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
321 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
322 fstype != info->fstype)
325 if (!fs_dev_desc && !info->null_dev_desc_ok)
328 if (!info->probe(fs_dev_desc, &fs_partition)) {
329 fs_type = info->fstype;
338 /* set current blk device w/ blk_desc + partition # */
339 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
341 struct fstype_info *info;
345 ret = part_get_info(desc, part, &fs_partition);
347 ret = part_get_info_whole_disk(desc, &fs_partition);
352 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
353 if (!info->probe(fs_dev_desc, &fs_partition)) {
354 fs_type = info->fstype;
362 static void fs_close(void)
364 struct fstype_info *info = fs_get_info(fs_type);
368 fs_type = FS_TYPE_ANY;
371 int fs_uuid(char *uuid_str)
373 struct fstype_info *info = fs_get_info(fs_type);
375 return info->uuid(uuid_str);
378 int fs_ls(const char *dirname)
382 struct fstype_info *info = fs_get_info(fs_type);
384 ret = info->ls(dirname);
386 fs_type = FS_TYPE_ANY;
392 int fs_exists(const char *filename)
396 struct fstype_info *info = fs_get_info(fs_type);
398 ret = info->exists(filename);
405 int fs_size(const char *filename, loff_t *size)
409 struct fstype_info *info = fs_get_info(fs_type);
411 ret = info->size(filename, size);
418 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
421 struct fstype_info *info = fs_get_info(fs_type);
426 * We don't actually know how many bytes are being read, since len==0
427 * means read the whole file.
429 buf = map_sysmem(addr, len);
430 ret = info->read(filename, buf, offset, len, actread);
433 /* If we requested a specific number of bytes, check we got it */
434 if (ret == 0 && len && *actread != len)
435 debug("** %s shorter than offset + len **\n", filename);
441 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
444 struct fstype_info *info = fs_get_info(fs_type);
448 buf = map_sysmem(addr, len);
449 ret = info->write(filename, buf, offset, len, actwrite);
452 if (ret < 0 && len != *actwrite) {
453 printf("** Unable to write file %s **\n", filename);
461 struct fs_dir_stream *fs_opendir(const char *filename)
463 struct fstype_info *info = fs_get_info(fs_type);
464 struct fs_dir_stream *dirs = NULL;
467 ret = info->opendir(filename, &dirs);
474 dirs->desc = fs_dev_desc;
475 dirs->part = fs_dev_part;
480 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
482 struct fstype_info *info;
483 struct fs_dirent *dirent;
486 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
487 info = fs_get_info(fs_type);
489 ret = info->readdir(dirs, &dirent);
499 void fs_closedir(struct fs_dir_stream *dirs)
501 struct fstype_info *info;
506 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
507 info = fs_get_info(fs_type);
509 info->closedir(dirs);
514 int fs_mkdir(const char *dirname)
518 struct fstype_info *info = fs_get_info(fs_type);
520 ret = info->mkdir(dirname);
522 fs_type = FS_TYPE_ANY;
528 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
534 return CMD_RET_USAGE;
536 if (fs_set_blk_dev(argv[1], argv[2], fstype))
539 if (fs_size(argv[3], &size) < 0)
540 return CMD_RET_FAILURE;
542 env_set_hex("filesize", size);
547 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
551 const char *addr_str;
552 const char *filename;
561 return CMD_RET_USAGE;
563 return CMD_RET_USAGE;
565 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
569 addr = simple_strtoul(argv[3], &ep, 16);
570 if (ep == argv[3] || *ep != '\0')
571 return CMD_RET_USAGE;
573 addr_str = env_get("loadaddr");
574 if (addr_str != NULL)
575 addr = simple_strtoul(addr_str, NULL, 16);
577 addr = CONFIG_SYS_LOAD_ADDR;
582 filename = env_get("bootfile");
584 puts("** No boot file defined **\n");
589 bytes = simple_strtoul(argv[5], NULL, 16);
593 pos = simple_strtoul(argv[6], NULL, 16);
598 ret = fs_read(filename, addr, pos, bytes, &len_read);
599 time = get_timer(time);
603 printf("%llu bytes read in %lu ms", len_read, time);
606 print_size(div_u64(len_read, time) * 1000, "/s");
611 env_set_hex("fileaddr", addr);
612 env_set_hex("filesize", len_read);
617 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
621 return CMD_RET_USAGE;
623 return CMD_RET_USAGE;
625 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
628 if (fs_ls(argc >= 4 ? argv[3] : "/"))
634 int file_exists(const char *dev_type, const char *dev_part, const char *file,
637 if (fs_set_blk_dev(dev_type, dev_part, fstype))
640 return fs_exists(file);
643 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
647 const char *filename;
654 if (argc < 6 || argc > 7)
655 return CMD_RET_USAGE;
657 if (fs_set_blk_dev(argv[1], argv[2], fstype))
660 addr = simple_strtoul(argv[3], NULL, 16);
662 bytes = simple_strtoul(argv[5], NULL, 16);
664 pos = simple_strtoul(argv[6], NULL, 16);
669 ret = fs_write(filename, addr, pos, bytes, &len);
670 time = get_timer(time);
674 printf("%llu bytes written in %lu ms", len, time);
677 print_size(div_u64(len, time) * 1000, "/s");
685 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
690 memset(uuid, 0, sizeof(uuid));
692 if (argc < 3 || argc > 4)
693 return CMD_RET_USAGE;
695 if (fs_set_blk_dev(argv[1], argv[2], fstype))
700 return CMD_RET_FAILURE;
703 env_set(argv[3], uuid);
705 printf("%s\n", uuid);
707 return CMD_RET_SUCCESS;
710 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
712 struct fstype_info *info;
714 if (argc < 3 || argc > 4)
715 return CMD_RET_USAGE;
717 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
720 info = fs_get_info(fs_type);
723 env_set(argv[3], info->name);
725 printf("%s\n", info->name);
727 return CMD_RET_SUCCESS;
730 int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
736 return CMD_RET_USAGE;
738 if (fs_set_blk_dev(argv[1], argv[2], fstype))
741 ret = fs_mkdir(argv[3]);
743 printf("** Unable to create a directory \"%s\" **\n", argv[3]);