2 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
4 * SPDX-License-Identifier: GPL-2.0
15 #include <sandboxfs.h>
16 #include <ubifs_uboot.h>
20 #include <linux/math64.h>
22 DECLARE_GLOBAL_DATA_PTR;
24 static struct blk_desc *fs_dev_desc;
25 static int fs_dev_part;
26 static disk_partition_t fs_partition;
27 static int fs_type = FS_TYPE_ANY;
29 static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
30 disk_partition_t *fs_partition)
32 printf("** Unrecognized filesystem type **\n");
36 static inline int fs_ls_unsupported(const char *dirname)
41 /* generic implementation of ls in terms of opendir/readdir/closedir */
43 static int fs_ls_generic(const char *dirname)
45 struct fs_dir_stream *dirs;
46 struct fs_dirent *dent;
47 int nfiles = 0, ndirs = 0;
49 dirs = fs_opendir(dirname);
53 while ((dent = fs_readdir(dirs))) {
54 if (dent->type == FS_DT_DIR) {
55 printf(" %s/\n", dent->name);
58 printf(" %8lld %s\n", dent->size, dent->name);
65 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
70 static inline int fs_exists_unsupported(const char *filename)
75 static inline int fs_size_unsupported(const char *filename, loff_t *size)
80 static inline int fs_read_unsupported(const char *filename, void *buf,
81 loff_t offset, loff_t len,
87 static inline int fs_write_unsupported(const char *filename, void *buf,
88 loff_t offset, loff_t len,
94 static inline void fs_close_unsupported(void)
98 static inline int fs_uuid_unsupported(char *uuid_str)
103 static inline int fs_opendir_unsupported(const char *filename,
104 struct fs_dir_stream **dirs)
113 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
114 * should be false in most cases. For "virtual" filesystems which
115 * aren't based on a U-Boot block device (e.g. sandbox), this can be
116 * set to true. This should also be true for the dumm entry at the end
117 * of fstypes[], since that is essentially a "virtual" (non-existent)
120 bool null_dev_desc_ok;
121 int (*probe)(struct blk_desc *fs_dev_desc,
122 disk_partition_t *fs_partition);
123 int (*ls)(const char *dirname);
124 int (*exists)(const char *filename);
125 int (*size)(const char *filename, loff_t *size);
126 int (*read)(const char *filename, void *buf, loff_t offset,
127 loff_t len, loff_t *actread);
128 int (*write)(const char *filename, void *buf, loff_t offset,
129 loff_t len, loff_t *actwrite);
131 int (*uuid)(char *uuid_str);
133 * Open a directory stream. On success return 0 and directory
134 * stream pointer via 'dirsp'. On error, return -errno. See
137 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
139 * Read next entry from directory stream. On success return 0
140 * and directory entry pointer via 'dentp'. On error return
141 * -errno. See fs_readdir().
143 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
144 /* see fs_closedir() */
145 void (*closedir)(struct fs_dir_stream *dirs);
148 static struct fstype_info fstypes[] = {
151 .fstype = FS_TYPE_FAT,
153 .null_dev_desc_ok = false,
154 .probe = fat_set_blk_dev,
157 .exists = fat_exists,
159 .read = fat_read_file,
160 #ifdef CONFIG_FAT_WRITE
161 .write = file_fat_write,
163 .write = fs_write_unsupported,
165 .uuid = fs_uuid_unsupported,
166 .opendir = fat_opendir,
167 .readdir = fat_readdir,
168 .closedir = fat_closedir,
171 #ifdef CONFIG_FS_EXT4
173 .fstype = FS_TYPE_EXT,
175 .null_dev_desc_ok = false,
176 .probe = ext4fs_probe,
177 .close = ext4fs_close,
179 .exists = ext4fs_exists,
181 .read = ext4_read_file,
182 #ifdef CONFIG_CMD_EXT4_WRITE
183 .write = ext4_write_file,
185 .write = fs_write_unsupported,
188 .opendir = fs_opendir_unsupported,
191 #ifdef CONFIG_SANDBOX
193 .fstype = FS_TYPE_SANDBOX,
195 .null_dev_desc_ok = true,
196 .probe = sandbox_fs_set_blk_dev,
197 .close = sandbox_fs_close,
199 .exists = sandbox_fs_exists,
200 .size = sandbox_fs_size,
201 .read = fs_read_sandbox,
202 .write = fs_write_sandbox,
203 .uuid = fs_uuid_unsupported,
204 .opendir = fs_opendir_unsupported,
207 #ifdef CONFIG_CMD_UBIFS
209 .fstype = FS_TYPE_UBIFS,
211 .null_dev_desc_ok = true,
212 .probe = ubifs_set_blk_dev,
213 .close = ubifs_close,
215 .exists = ubifs_exists,
218 .write = fs_write_unsupported,
219 .uuid = fs_uuid_unsupported,
220 .opendir = fs_opendir_unsupported,
223 #ifdef CONFIG_FS_BTRFS
225 .fstype = FS_TYPE_BTRFS,
227 .null_dev_desc_ok = false,
228 .probe = btrfs_probe,
229 .close = btrfs_close,
231 .exists = btrfs_exists,
234 .write = fs_write_unsupported,
236 .opendir = fs_opendir_unsupported,
240 .fstype = FS_TYPE_ANY,
241 .name = "unsupported",
242 .null_dev_desc_ok = true,
243 .probe = fs_probe_unsupported,
244 .close = fs_close_unsupported,
245 .ls = fs_ls_unsupported,
246 .exists = fs_exists_unsupported,
247 .size = fs_size_unsupported,
248 .read = fs_read_unsupported,
249 .write = fs_write_unsupported,
250 .uuid = fs_uuid_unsupported,
251 .opendir = fs_opendir_unsupported,
255 static struct fstype_info *fs_get_info(int fstype)
257 struct fstype_info *info;
260 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
261 if (fstype == info->fstype)
265 /* Return the 'unsupported' sentinel */
269 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
271 struct fstype_info *info;
273 #ifdef CONFIG_NEEDS_MANUAL_RELOC
274 static int relocated;
277 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
279 info->name += gd->reloc_off;
280 info->probe += gd->reloc_off;
281 info->close += gd->reloc_off;
282 info->ls += gd->reloc_off;
283 info->read += gd->reloc_off;
284 info->write += gd->reloc_off;
290 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
295 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
296 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
297 fstype != info->fstype)
300 if (!fs_dev_desc && !info->null_dev_desc_ok)
303 if (!info->probe(fs_dev_desc, &fs_partition)) {
304 fs_type = info->fstype;
313 /* set current blk device w/ blk_desc + partition # */
314 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
316 struct fstype_info *info;
320 ret = part_get_info(desc, part, &fs_partition);
322 ret = part_get_info_whole_disk(desc, &fs_partition);
327 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
328 if (!info->probe(fs_dev_desc, &fs_partition)) {
329 fs_type = info->fstype;
337 static void fs_close(void)
339 struct fstype_info *info = fs_get_info(fs_type);
343 fs_type = FS_TYPE_ANY;
346 int fs_uuid(char *uuid_str)
348 struct fstype_info *info = fs_get_info(fs_type);
350 return info->uuid(uuid_str);
353 int fs_ls(const char *dirname)
357 struct fstype_info *info = fs_get_info(fs_type);
359 ret = info->ls(dirname);
361 fs_type = FS_TYPE_ANY;
367 int fs_exists(const char *filename)
371 struct fstype_info *info = fs_get_info(fs_type);
373 ret = info->exists(filename);
380 int fs_size(const char *filename, loff_t *size)
384 struct fstype_info *info = fs_get_info(fs_type);
386 ret = info->size(filename, size);
393 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
396 struct fstype_info *info = fs_get_info(fs_type);
401 * We don't actually know how many bytes are being read, since len==0
402 * means read the whole file.
404 buf = map_sysmem(addr, len);
405 ret = info->read(filename, buf, offset, len, actread);
408 /* If we requested a specific number of bytes, check we got it */
409 if (ret == 0 && len && *actread != len)
410 debug("** %s shorter than offset + len **\n", filename);
416 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
419 struct fstype_info *info = fs_get_info(fs_type);
423 buf = map_sysmem(addr, len);
424 ret = info->write(filename, buf, offset, len, actwrite);
427 if (ret < 0 && len != *actwrite) {
428 printf("** Unable to write file %s **\n", filename);
436 struct fs_dir_stream *fs_opendir(const char *filename)
438 struct fstype_info *info = fs_get_info(fs_type);
439 struct fs_dir_stream *dirs = NULL;
442 ret = info->opendir(filename, &dirs);
449 dirs->desc = fs_dev_desc;
450 dirs->part = fs_dev_part;
455 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
457 struct fstype_info *info;
458 struct fs_dirent *dirent;
461 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
462 info = fs_get_info(fs_type);
464 ret = info->readdir(dirs, &dirent);
474 void fs_closedir(struct fs_dir_stream *dirs)
476 struct fstype_info *info;
481 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
482 info = fs_get_info(fs_type);
484 info->closedir(dirs);
489 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
495 return CMD_RET_USAGE;
497 if (fs_set_blk_dev(argv[1], argv[2], fstype))
500 if (fs_size(argv[3], &size) < 0)
501 return CMD_RET_FAILURE;
503 env_set_hex("filesize", size);
508 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
512 const char *addr_str;
513 const char *filename;
522 return CMD_RET_USAGE;
524 return CMD_RET_USAGE;
526 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
530 addr = simple_strtoul(argv[3], &ep, 16);
531 if (ep == argv[3] || *ep != '\0')
532 return CMD_RET_USAGE;
534 addr_str = env_get("loadaddr");
535 if (addr_str != NULL)
536 addr = simple_strtoul(addr_str, NULL, 16);
538 addr = CONFIG_SYS_LOAD_ADDR;
543 filename = env_get("bootfile");
545 puts("** No boot file defined **\n");
550 bytes = simple_strtoul(argv[5], NULL, 16);
554 pos = simple_strtoul(argv[6], NULL, 16);
559 ret = fs_read(filename, addr, pos, bytes, &len_read);
560 time = get_timer(time);
564 printf("%llu bytes read in %lu ms", len_read, time);
567 print_size(div_u64(len_read, time) * 1000, "/s");
572 env_set_hex("fileaddr", addr);
573 env_set_hex("filesize", len_read);
578 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
582 return CMD_RET_USAGE;
584 return CMD_RET_USAGE;
586 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
589 if (fs_ls(argc >= 4 ? argv[3] : "/"))
595 int file_exists(const char *dev_type, const char *dev_part, const char *file,
598 if (fs_set_blk_dev(dev_type, dev_part, fstype))
601 return fs_exists(file);
604 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
608 const char *filename;
615 if (argc < 6 || argc > 7)
616 return CMD_RET_USAGE;
618 if (fs_set_blk_dev(argv[1], argv[2], fstype))
621 addr = simple_strtoul(argv[3], NULL, 16);
623 bytes = simple_strtoul(argv[5], NULL, 16);
625 pos = simple_strtoul(argv[6], NULL, 16);
630 ret = fs_write(filename, addr, pos, bytes, &len);
631 time = get_timer(time);
635 printf("%llu bytes written in %lu ms", len, time);
638 print_size(div_u64(len, time) * 1000, "/s");
646 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
651 memset(uuid, 0, sizeof(uuid));
653 if (argc < 3 || argc > 4)
654 return CMD_RET_USAGE;
656 if (fs_set_blk_dev(argv[1], argv[2], fstype))
661 return CMD_RET_FAILURE;
664 env_set(argv[3], uuid);
666 printf("%s\n", uuid);
668 return CMD_RET_SUCCESS;
671 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
673 struct fstype_info *info;
675 if (argc < 3 || argc > 4)
676 return CMD_RET_USAGE;
678 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
681 info = fs_get_info(fs_type);
684 env_set(argv[3], info->name);
686 printf("%s\n", info->name);
688 return CMD_RET_SUCCESS;