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>
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 static inline int fs_exists_unsupported(const char *filename)
45 static inline int fs_size_unsupported(const char *filename, loff_t *size)
50 static inline int fs_read_unsupported(const char *filename, void *buf,
51 loff_t offset, loff_t len,
57 static inline int fs_write_unsupported(const char *filename, void *buf,
58 loff_t offset, loff_t len,
64 static inline void fs_close_unsupported(void)
68 static inline int fs_uuid_unsupported(char *uuid_str)
73 static inline int fs_opendir_unsupported(const char *filename,
74 struct fs_dir_stream **dirs)
83 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
84 * should be false in most cases. For "virtual" filesystems which
85 * aren't based on a U-Boot block device (e.g. sandbox), this can be
86 * set to true. This should also be true for the dumm entry at the end
87 * of fstypes[], since that is essentially a "virtual" (non-existent)
90 bool null_dev_desc_ok;
91 int (*probe)(struct blk_desc *fs_dev_desc,
92 disk_partition_t *fs_partition);
93 int (*ls)(const char *dirname);
94 int (*exists)(const char *filename);
95 int (*size)(const char *filename, loff_t *size);
96 int (*read)(const char *filename, void *buf, loff_t offset,
97 loff_t len, loff_t *actread);
98 int (*write)(const char *filename, void *buf, loff_t offset,
99 loff_t len, loff_t *actwrite);
101 int (*uuid)(char *uuid_str);
103 * Open a directory stream. On success return 0 and directory
104 * stream pointer via 'dirsp'. On error, return -errno. See
107 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
109 * Read next entry from directory stream. On success return 0
110 * and directory entry pointer via 'dentp'. On error return
111 * -errno. See fs_readdir().
113 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
114 /* see fs_closedir() */
115 void (*closedir)(struct fs_dir_stream *dirs);
118 static struct fstype_info fstypes[] = {
121 .fstype = FS_TYPE_FAT,
123 .null_dev_desc_ok = false,
124 .probe = fat_set_blk_dev,
127 .exists = fat_exists,
129 .read = fat_read_file,
130 #ifdef CONFIG_FAT_WRITE
131 .write = file_fat_write,
133 .write = fs_write_unsupported,
135 .uuid = fs_uuid_unsupported,
136 .opendir = fs_opendir_unsupported,
139 #ifdef CONFIG_FS_EXT4
141 .fstype = FS_TYPE_EXT,
143 .null_dev_desc_ok = false,
144 .probe = ext4fs_probe,
145 .close = ext4fs_close,
147 .exists = ext4fs_exists,
149 .read = ext4_read_file,
150 #ifdef CONFIG_CMD_EXT4_WRITE
151 .write = ext4_write_file,
153 .write = fs_write_unsupported,
156 .opendir = fs_opendir_unsupported,
159 #ifdef CONFIG_SANDBOX
161 .fstype = FS_TYPE_SANDBOX,
163 .null_dev_desc_ok = true,
164 .probe = sandbox_fs_set_blk_dev,
165 .close = sandbox_fs_close,
167 .exists = sandbox_fs_exists,
168 .size = sandbox_fs_size,
169 .read = fs_read_sandbox,
170 .write = fs_write_sandbox,
171 .uuid = fs_uuid_unsupported,
172 .opendir = fs_opendir_unsupported,
175 #ifdef CONFIG_CMD_UBIFS
177 .fstype = FS_TYPE_UBIFS,
179 .null_dev_desc_ok = true,
180 .probe = ubifs_set_blk_dev,
181 .close = ubifs_close,
183 .exists = ubifs_exists,
186 .write = fs_write_unsupported,
187 .uuid = fs_uuid_unsupported,
188 .opendir = fs_opendir_unsupported,
192 .fstype = FS_TYPE_ANY,
193 .name = "unsupported",
194 .null_dev_desc_ok = true,
195 .probe = fs_probe_unsupported,
196 .close = fs_close_unsupported,
197 .ls = fs_ls_unsupported,
198 .exists = fs_exists_unsupported,
199 .size = fs_size_unsupported,
200 .read = fs_read_unsupported,
201 .write = fs_write_unsupported,
202 .uuid = fs_uuid_unsupported,
203 .opendir = fs_opendir_unsupported,
207 static struct fstype_info *fs_get_info(int fstype)
209 struct fstype_info *info;
212 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
213 if (fstype == info->fstype)
217 /* Return the 'unsupported' sentinel */
221 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
223 struct fstype_info *info;
225 #ifdef CONFIG_NEEDS_MANUAL_RELOC
226 static int relocated;
229 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
231 info->name += gd->reloc_off;
232 info->probe += gd->reloc_off;
233 info->close += gd->reloc_off;
234 info->ls += gd->reloc_off;
235 info->read += gd->reloc_off;
236 info->write += gd->reloc_off;
242 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
247 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
248 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
249 fstype != info->fstype)
252 if (!fs_dev_desc && !info->null_dev_desc_ok)
255 if (!info->probe(fs_dev_desc, &fs_partition)) {
256 fs_type = info->fstype;
265 /* set current blk device w/ blk_desc + partition # */
266 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
268 struct fstype_info *info;
272 ret = part_get_info(desc, part, &fs_partition);
274 ret = part_get_info_whole_disk(desc, &fs_partition);
279 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
280 if (!info->probe(fs_dev_desc, &fs_partition)) {
281 fs_type = info->fstype;
289 static void fs_close(void)
291 struct fstype_info *info = fs_get_info(fs_type);
295 fs_type = FS_TYPE_ANY;
298 int fs_uuid(char *uuid_str)
300 struct fstype_info *info = fs_get_info(fs_type);
302 return info->uuid(uuid_str);
305 int fs_ls(const char *dirname)
309 struct fstype_info *info = fs_get_info(fs_type);
311 ret = info->ls(dirname);
313 fs_type = FS_TYPE_ANY;
319 int fs_exists(const char *filename)
323 struct fstype_info *info = fs_get_info(fs_type);
325 ret = info->exists(filename);
332 int fs_size(const char *filename, loff_t *size)
336 struct fstype_info *info = fs_get_info(fs_type);
338 ret = info->size(filename, size);
345 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
348 struct fstype_info *info = fs_get_info(fs_type);
353 * We don't actually know how many bytes are being read, since len==0
354 * means read the whole file.
356 buf = map_sysmem(addr, len);
357 ret = info->read(filename, buf, offset, len, actread);
360 /* If we requested a specific number of bytes, check we got it */
361 if (ret == 0 && len && *actread != len)
362 printf("** %s shorter than offset + len **\n", filename);
368 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
371 struct fstype_info *info = fs_get_info(fs_type);
375 buf = map_sysmem(addr, len);
376 ret = info->write(filename, buf, offset, len, actwrite);
379 if (ret < 0 && len != *actwrite) {
380 printf("** Unable to write file %s **\n", filename);
388 struct fs_dir_stream *fs_opendir(const char *filename)
390 struct fstype_info *info = fs_get_info(fs_type);
391 struct fs_dir_stream *dirs = NULL;
394 ret = info->opendir(filename, &dirs);
401 dirs->desc = fs_dev_desc;
402 dirs->part = fs_dev_part;
407 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
409 struct fstype_info *info;
410 struct fs_dirent *dirent;
413 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
414 info = fs_get_info(fs_type);
416 ret = info->readdir(dirs, &dirent);
426 void fs_closedir(struct fs_dir_stream *dirs)
428 struct fstype_info *info;
433 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
434 info = fs_get_info(fs_type);
436 info->closedir(dirs);
441 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
447 return CMD_RET_USAGE;
449 if (fs_set_blk_dev(argv[1], argv[2], fstype))
452 if (fs_size(argv[3], &size) < 0)
453 return CMD_RET_FAILURE;
455 env_set_hex("filesize", size);
460 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
464 const char *addr_str;
465 const char *filename;
474 return CMD_RET_USAGE;
476 return CMD_RET_USAGE;
478 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
482 addr = simple_strtoul(argv[3], &ep, 16);
483 if (ep == argv[3] || *ep != '\0')
484 return CMD_RET_USAGE;
486 addr_str = env_get("loadaddr");
487 if (addr_str != NULL)
488 addr = simple_strtoul(addr_str, NULL, 16);
490 addr = CONFIG_SYS_LOAD_ADDR;
495 filename = env_get("bootfile");
497 puts("** No boot file defined **\n");
502 bytes = simple_strtoul(argv[5], NULL, 16);
506 pos = simple_strtoul(argv[6], NULL, 16);
511 ret = fs_read(filename, addr, pos, bytes, &len_read);
512 time = get_timer(time);
516 printf("%llu bytes read in %lu ms", len_read, time);
519 print_size(div_u64(len_read, time) * 1000, "/s");
524 env_set_hex("fileaddr", addr);
525 env_set_hex("filesize", len_read);
530 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
534 return CMD_RET_USAGE;
536 return CMD_RET_USAGE;
538 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
541 if (fs_ls(argc >= 4 ? argv[3] : "/"))
547 int file_exists(const char *dev_type, const char *dev_part, const char *file,
550 if (fs_set_blk_dev(dev_type, dev_part, fstype))
553 return fs_exists(file);
556 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
560 const char *filename;
567 if (argc < 6 || argc > 7)
568 return CMD_RET_USAGE;
570 if (fs_set_blk_dev(argv[1], argv[2], fstype))
573 addr = simple_strtoul(argv[3], NULL, 16);
575 bytes = simple_strtoul(argv[5], NULL, 16);
577 pos = simple_strtoul(argv[6], NULL, 16);
582 ret = fs_write(filename, addr, pos, bytes, &len);
583 time = get_timer(time);
587 printf("%llu bytes written in %lu ms", len, time);
590 print_size(div_u64(len, time) * 1000, "/s");
598 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
603 memset(uuid, 0, sizeof(uuid));
605 if (argc < 3 || argc > 4)
606 return CMD_RET_USAGE;
608 if (fs_set_blk_dev(argv[1], argv[2], fstype))
613 return CMD_RET_FAILURE;
616 env_set(argv[3], uuid);
618 printf("%s\n", uuid);
620 return CMD_RET_SUCCESS;
623 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
625 struct fstype_info *info;
627 if (argc < 3 || argc > 4)
628 return CMD_RET_USAGE;
630 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
633 info = fs_get_info(fs_type);
636 env_set(argv[3], info->name);
638 printf("%s\n", info->name);
640 return CMD_RET_SUCCESS;