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,
168 .write = fs_write_unsupported,
170 .uuid = fs_uuid_unsupported,
171 .opendir = fat_opendir,
172 .readdir = fat_readdir,
173 .closedir = fat_closedir,
174 .mkdir = fs_mkdir_unsupported,
177 #ifdef CONFIG_FS_EXT4
179 .fstype = FS_TYPE_EXT,
181 .null_dev_desc_ok = false,
182 .probe = ext4fs_probe,
183 .close = ext4fs_close,
185 .exists = ext4fs_exists,
187 .read = ext4_read_file,
188 #ifdef CONFIG_CMD_EXT4_WRITE
189 .write = ext4_write_file,
191 .write = fs_write_unsupported,
194 .opendir = fs_opendir_unsupported,
195 .mkdir = fs_mkdir_unsupported,
198 #ifdef CONFIG_SANDBOX
200 .fstype = FS_TYPE_SANDBOX,
202 .null_dev_desc_ok = true,
203 .probe = sandbox_fs_set_blk_dev,
204 .close = sandbox_fs_close,
206 .exists = sandbox_fs_exists,
207 .size = sandbox_fs_size,
208 .read = fs_read_sandbox,
209 .write = fs_write_sandbox,
210 .uuid = fs_uuid_unsupported,
211 .opendir = fs_opendir_unsupported,
212 .mkdir = fs_mkdir_unsupported,
215 #ifdef CONFIG_CMD_UBIFS
217 .fstype = FS_TYPE_UBIFS,
219 .null_dev_desc_ok = true,
220 .probe = ubifs_set_blk_dev,
221 .close = ubifs_close,
223 .exists = ubifs_exists,
226 .write = fs_write_unsupported,
227 .uuid = fs_uuid_unsupported,
228 .opendir = fs_opendir_unsupported,
229 .mkdir = fs_mkdir_unsupported,
232 #ifdef CONFIG_FS_BTRFS
234 .fstype = FS_TYPE_BTRFS,
236 .null_dev_desc_ok = false,
237 .probe = btrfs_probe,
238 .close = btrfs_close,
240 .exists = btrfs_exists,
243 .write = fs_write_unsupported,
245 .opendir = fs_opendir_unsupported,
246 .mkdir = fs_mkdir_unsupported,
250 .fstype = FS_TYPE_ANY,
251 .name = "unsupported",
252 .null_dev_desc_ok = true,
253 .probe = fs_probe_unsupported,
254 .close = fs_close_unsupported,
255 .ls = fs_ls_unsupported,
256 .exists = fs_exists_unsupported,
257 .size = fs_size_unsupported,
258 .read = fs_read_unsupported,
259 .write = fs_write_unsupported,
260 .uuid = fs_uuid_unsupported,
261 .opendir = fs_opendir_unsupported,
262 .mkdir = fs_mkdir_unsupported,
266 static struct fstype_info *fs_get_info(int fstype)
268 struct fstype_info *info;
271 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
272 if (fstype == info->fstype)
276 /* Return the 'unsupported' sentinel */
281 * fs_get_type_name() - Get type of current filesystem
283 * Return: Pointer to filesystem name
285 * Returns a string describing the current filesystem, or the sentinel
286 * "unsupported" for any unrecognised filesystem.
288 const char *fs_get_type_name(void)
290 return fs_get_info(fs_type)->name;
293 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
295 struct fstype_info *info;
297 #ifdef CONFIG_NEEDS_MANUAL_RELOC
298 static int relocated;
301 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
303 info->name += gd->reloc_off;
304 info->probe += gd->reloc_off;
305 info->close += gd->reloc_off;
306 info->ls += gd->reloc_off;
307 info->read += gd->reloc_off;
308 info->write += gd->reloc_off;
314 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
319 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
320 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
321 fstype != info->fstype)
324 if (!fs_dev_desc && !info->null_dev_desc_ok)
327 if (!info->probe(fs_dev_desc, &fs_partition)) {
328 fs_type = info->fstype;
337 /* set current blk device w/ blk_desc + partition # */
338 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
340 struct fstype_info *info;
344 ret = part_get_info(desc, part, &fs_partition);
346 ret = part_get_info_whole_disk(desc, &fs_partition);
351 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
352 if (!info->probe(fs_dev_desc, &fs_partition)) {
353 fs_type = info->fstype;
361 static void fs_close(void)
363 struct fstype_info *info = fs_get_info(fs_type);
367 fs_type = FS_TYPE_ANY;
370 int fs_uuid(char *uuid_str)
372 struct fstype_info *info = fs_get_info(fs_type);
374 return info->uuid(uuid_str);
377 int fs_ls(const char *dirname)
381 struct fstype_info *info = fs_get_info(fs_type);
383 ret = info->ls(dirname);
385 fs_type = FS_TYPE_ANY;
391 int fs_exists(const char *filename)
395 struct fstype_info *info = fs_get_info(fs_type);
397 ret = info->exists(filename);
404 int fs_size(const char *filename, loff_t *size)
408 struct fstype_info *info = fs_get_info(fs_type);
410 ret = info->size(filename, size);
417 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
420 struct fstype_info *info = fs_get_info(fs_type);
425 * We don't actually know how many bytes are being read, since len==0
426 * means read the whole file.
428 buf = map_sysmem(addr, len);
429 ret = info->read(filename, buf, offset, len, actread);
432 /* If we requested a specific number of bytes, check we got it */
433 if (ret == 0 && len && *actread != len)
434 debug("** %s shorter than offset + len **\n", filename);
440 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
443 struct fstype_info *info = fs_get_info(fs_type);
447 buf = map_sysmem(addr, len);
448 ret = info->write(filename, buf, offset, len, actwrite);
451 if (ret < 0 && len != *actwrite) {
452 printf("** Unable to write file %s **\n", filename);
460 struct fs_dir_stream *fs_opendir(const char *filename)
462 struct fstype_info *info = fs_get_info(fs_type);
463 struct fs_dir_stream *dirs = NULL;
466 ret = info->opendir(filename, &dirs);
473 dirs->desc = fs_dev_desc;
474 dirs->part = fs_dev_part;
479 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
481 struct fstype_info *info;
482 struct fs_dirent *dirent;
485 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
486 info = fs_get_info(fs_type);
488 ret = info->readdir(dirs, &dirent);
498 void fs_closedir(struct fs_dir_stream *dirs)
500 struct fstype_info *info;
505 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
506 info = fs_get_info(fs_type);
508 info->closedir(dirs);
513 int fs_mkdir(const char *dirname)
517 struct fstype_info *info = fs_get_info(fs_type);
519 ret = info->mkdir(dirname);
521 fs_type = FS_TYPE_ANY;
527 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
533 return CMD_RET_USAGE;
535 if (fs_set_blk_dev(argv[1], argv[2], fstype))
538 if (fs_size(argv[3], &size) < 0)
539 return CMD_RET_FAILURE;
541 env_set_hex("filesize", size);
546 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
550 const char *addr_str;
551 const char *filename;
560 return CMD_RET_USAGE;
562 return CMD_RET_USAGE;
564 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
568 addr = simple_strtoul(argv[3], &ep, 16);
569 if (ep == argv[3] || *ep != '\0')
570 return CMD_RET_USAGE;
572 addr_str = env_get("loadaddr");
573 if (addr_str != NULL)
574 addr = simple_strtoul(addr_str, NULL, 16);
576 addr = CONFIG_SYS_LOAD_ADDR;
581 filename = env_get("bootfile");
583 puts("** No boot file defined **\n");
588 bytes = simple_strtoul(argv[5], NULL, 16);
592 pos = simple_strtoul(argv[6], NULL, 16);
597 ret = fs_read(filename, addr, pos, bytes, &len_read);
598 time = get_timer(time);
602 printf("%llu bytes read in %lu ms", len_read, time);
605 print_size(div_u64(len_read, time) * 1000, "/s");
610 env_set_hex("fileaddr", addr);
611 env_set_hex("filesize", len_read);
616 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
620 return CMD_RET_USAGE;
622 return CMD_RET_USAGE;
624 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
627 if (fs_ls(argc >= 4 ? argv[3] : "/"))
633 int file_exists(const char *dev_type, const char *dev_part, const char *file,
636 if (fs_set_blk_dev(dev_type, dev_part, fstype))
639 return fs_exists(file);
642 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
646 const char *filename;
653 if (argc < 6 || argc > 7)
654 return CMD_RET_USAGE;
656 if (fs_set_blk_dev(argv[1], argv[2], fstype))
659 addr = simple_strtoul(argv[3], NULL, 16);
661 bytes = simple_strtoul(argv[5], NULL, 16);
663 pos = simple_strtoul(argv[6], NULL, 16);
668 ret = fs_write(filename, addr, pos, bytes, &len);
669 time = get_timer(time);
673 printf("%llu bytes written in %lu ms", len, time);
676 print_size(div_u64(len, time) * 1000, "/s");
684 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
689 memset(uuid, 0, sizeof(uuid));
691 if (argc < 3 || argc > 4)
692 return CMD_RET_USAGE;
694 if (fs_set_blk_dev(argv[1], argv[2], fstype))
699 return CMD_RET_FAILURE;
702 env_set(argv[3], uuid);
704 printf("%s\n", uuid);
706 return CMD_RET_SUCCESS;
709 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
711 struct fstype_info *info;
713 if (argc < 3 || argc > 4)
714 return CMD_RET_USAGE;
716 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
719 info = fs_get_info(fs_type);
722 env_set(argv[3], info->name);
724 printf("%s\n", info->name);
726 return CMD_RET_SUCCESS;
729 int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
735 return CMD_RET_USAGE;
737 if (fs_set_blk_dev(argv[1], argv[2], fstype))
740 ret = fs_mkdir(argv[3]);
742 printf("** Unable to create a directory \"%s\" **\n", argv[3]);