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 int fs_ln_unsupported(const char *filename, const char *target)
98 static inline void fs_close_unsupported(void)
102 static inline int fs_uuid_unsupported(char *uuid_str)
107 static inline int fs_opendir_unsupported(const char *filename,
108 struct fs_dir_stream **dirs)
113 static inline int fs_unlink_unsupported(const char *filename)
118 static inline int fs_mkdir_unsupported(const char *dirname)
127 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
128 * should be false in most cases. For "virtual" filesystems which
129 * aren't based on a U-Boot block device (e.g. sandbox), this can be
130 * set to true. This should also be true for the dummy entry at the end
131 * of fstypes[], since that is essentially a "virtual" (non-existent)
134 bool null_dev_desc_ok;
135 int (*probe)(struct blk_desc *fs_dev_desc,
136 disk_partition_t *fs_partition);
137 int (*ls)(const char *dirname);
138 int (*exists)(const char *filename);
139 int (*size)(const char *filename, loff_t *size);
140 int (*read)(const char *filename, void *buf, loff_t offset,
141 loff_t len, loff_t *actread);
142 int (*write)(const char *filename, void *buf, loff_t offset,
143 loff_t len, loff_t *actwrite);
145 int (*uuid)(char *uuid_str);
147 * Open a directory stream. On success return 0 and directory
148 * stream pointer via 'dirsp'. On error, return -errno. See
151 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
153 * Read next entry from directory stream. On success return 0
154 * and directory entry pointer via 'dentp'. On error return
155 * -errno. See fs_readdir().
157 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
158 /* see fs_closedir() */
159 void (*closedir)(struct fs_dir_stream *dirs);
160 int (*unlink)(const char *filename);
161 int (*mkdir)(const char *dirname);
162 int (*ln)(const char *filename, const char *target);
165 static struct fstype_info fstypes[] = {
168 .fstype = FS_TYPE_FAT,
170 .null_dev_desc_ok = false,
171 .probe = fat_set_blk_dev,
174 .exists = fat_exists,
176 .read = fat_read_file,
177 #if CONFIG_IS_ENABLED(FAT_WRITE)
178 .write = file_fat_write,
179 .unlink = fat_unlink,
182 .write = fs_write_unsupported,
183 .unlink = fs_unlink_unsupported,
184 .mkdir = fs_mkdir_unsupported,
186 .uuid = fs_uuid_unsupported,
187 .opendir = fat_opendir,
188 .readdir = fat_readdir,
189 .closedir = fat_closedir,
190 .ln = fs_ln_unsupported,
194 #if CONFIG_IS_ENABLED(FS_EXT4)
196 .fstype = FS_TYPE_EXT,
198 .null_dev_desc_ok = false,
199 .probe = ext4fs_probe,
200 .close = ext4fs_close,
202 .exists = ext4fs_exists,
204 .read = ext4_read_file,
205 #ifdef CONFIG_CMD_EXT4_WRITE
206 .write = ext4_write_file,
207 .ln = ext4fs_create_link,
209 .write = fs_write_unsupported,
210 .ln = fs_ln_unsupported,
213 .opendir = fs_opendir_unsupported,
214 .unlink = fs_unlink_unsupported,
215 .mkdir = fs_mkdir_unsupported,
218 #ifdef CONFIG_SANDBOX
220 .fstype = FS_TYPE_SANDBOX,
222 .null_dev_desc_ok = true,
223 .probe = sandbox_fs_set_blk_dev,
224 .close = sandbox_fs_close,
226 .exists = sandbox_fs_exists,
227 .size = sandbox_fs_size,
228 .read = fs_read_sandbox,
229 .write = fs_write_sandbox,
230 .uuid = fs_uuid_unsupported,
231 .opendir = fs_opendir_unsupported,
232 .unlink = fs_unlink_unsupported,
233 .mkdir = fs_mkdir_unsupported,
234 .ln = fs_ln_unsupported,
237 #ifdef CONFIG_CMD_UBIFS
239 .fstype = FS_TYPE_UBIFS,
241 .null_dev_desc_ok = true,
242 .probe = ubifs_set_blk_dev,
243 .close = ubifs_close,
245 .exists = ubifs_exists,
248 .write = fs_write_unsupported,
249 .uuid = fs_uuid_unsupported,
250 .opendir = fs_opendir_unsupported,
251 .unlink = fs_unlink_unsupported,
252 .mkdir = fs_mkdir_unsupported,
253 .ln = fs_ln_unsupported,
256 #ifdef CONFIG_FS_BTRFS
258 .fstype = FS_TYPE_BTRFS,
260 .null_dev_desc_ok = false,
261 .probe = btrfs_probe,
262 .close = btrfs_close,
264 .exists = btrfs_exists,
267 .write = fs_write_unsupported,
269 .opendir = fs_opendir_unsupported,
270 .unlink = fs_unlink_unsupported,
271 .mkdir = fs_mkdir_unsupported,
272 .ln = fs_ln_unsupported,
276 .fstype = FS_TYPE_ANY,
277 .name = "unsupported",
278 .null_dev_desc_ok = true,
279 .probe = fs_probe_unsupported,
280 .close = fs_close_unsupported,
281 .ls = fs_ls_unsupported,
282 .exists = fs_exists_unsupported,
283 .size = fs_size_unsupported,
284 .read = fs_read_unsupported,
285 .write = fs_write_unsupported,
286 .uuid = fs_uuid_unsupported,
287 .opendir = fs_opendir_unsupported,
288 .unlink = fs_unlink_unsupported,
289 .mkdir = fs_mkdir_unsupported,
290 .ln = fs_ln_unsupported,
294 static struct fstype_info *fs_get_info(int fstype)
296 struct fstype_info *info;
299 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
300 if (fstype == info->fstype)
304 /* Return the 'unsupported' sentinel */
309 * fs_get_type_name() - Get type of current filesystem
311 * Return: Pointer to filesystem name
313 * Returns a string describing the current filesystem, or the sentinel
314 * "unsupported" for any unrecognised filesystem.
316 const char *fs_get_type_name(void)
318 return fs_get_info(fs_type)->name;
321 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
323 struct fstype_info *info;
325 #ifdef CONFIG_NEEDS_MANUAL_RELOC
326 static int relocated;
329 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
331 info->name += gd->reloc_off;
332 info->probe += gd->reloc_off;
333 info->close += gd->reloc_off;
334 info->ls += gd->reloc_off;
335 info->read += gd->reloc_off;
336 info->write += gd->reloc_off;
342 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
347 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
348 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
349 fstype != info->fstype)
352 if (!fs_dev_desc && !info->null_dev_desc_ok)
355 if (!info->probe(fs_dev_desc, &fs_partition)) {
356 fs_type = info->fstype;
365 /* set current blk device w/ blk_desc + partition # */
366 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
368 struct fstype_info *info;
372 ret = part_get_info(desc, part, &fs_partition);
374 ret = part_get_info_whole_disk(desc, &fs_partition);
379 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
380 if (!info->probe(fs_dev_desc, &fs_partition)) {
381 fs_type = info->fstype;
390 static void fs_close(void)
392 struct fstype_info *info = fs_get_info(fs_type);
396 fs_type = FS_TYPE_ANY;
399 int fs_uuid(char *uuid_str)
401 struct fstype_info *info = fs_get_info(fs_type);
403 return info->uuid(uuid_str);
406 int fs_ls(const char *dirname)
410 struct fstype_info *info = fs_get_info(fs_type);
412 ret = info->ls(dirname);
414 fs_type = FS_TYPE_ANY;
420 int fs_exists(const char *filename)
424 struct fstype_info *info = fs_get_info(fs_type);
426 ret = info->exists(filename);
433 int fs_size(const char *filename, loff_t *size)
437 struct fstype_info *info = fs_get_info(fs_type);
439 ret = info->size(filename, size);
447 /* Check if a file may be read to the given address */
448 static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
449 loff_t len, struct fstype_info *info)
456 /* get the actual size of the file */
457 ret = info->size(filename, &size);
460 if (offset >= size) {
461 /* offset >= EOF, no bytes will be written */
464 read_len = size - offset;
466 /* limit to 'len' if it is smaller */
467 if (len && len < read_len)
470 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
473 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
476 printf("** Reading file would overwrite reserved memory **\n");
481 static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
482 int do_lmb_check, loff_t *actread)
484 struct fstype_info *info = fs_get_info(fs_type);
490 ret = fs_read_lmb_check(filename, addr, offset, len, info);
497 * We don't actually know how many bytes are being read, since len==0
498 * means read the whole file.
500 buf = map_sysmem(addr, len);
501 ret = info->read(filename, buf, offset, len, actread);
504 /* If we requested a specific number of bytes, check we got it */
505 if (ret == 0 && len && *actread != len)
506 debug("** %s shorter than offset + len **\n", filename);
512 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
515 return _fs_read(filename, addr, offset, len, 0, actread);
518 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
521 struct fstype_info *info = fs_get_info(fs_type);
525 buf = map_sysmem(addr, len);
526 ret = info->write(filename, buf, offset, len, actwrite);
529 if (ret < 0 && len != *actwrite) {
530 printf("** Unable to write file %s **\n", filename);
538 struct fs_dir_stream *fs_opendir(const char *filename)
540 struct fstype_info *info = fs_get_info(fs_type);
541 struct fs_dir_stream *dirs = NULL;
544 ret = info->opendir(filename, &dirs);
551 dirs->desc = fs_dev_desc;
552 dirs->part = fs_dev_part;
557 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
559 struct fstype_info *info;
560 struct fs_dirent *dirent;
563 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
564 info = fs_get_info(fs_type);
566 ret = info->readdir(dirs, &dirent);
576 void fs_closedir(struct fs_dir_stream *dirs)
578 struct fstype_info *info;
583 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
584 info = fs_get_info(fs_type);
586 info->closedir(dirs);
590 int fs_unlink(const char *filename)
594 struct fstype_info *info = fs_get_info(fs_type);
596 ret = info->unlink(filename);
598 fs_type = FS_TYPE_ANY;
604 int fs_mkdir(const char *dirname)
608 struct fstype_info *info = fs_get_info(fs_type);
610 ret = info->mkdir(dirname);
612 fs_type = FS_TYPE_ANY;
618 int fs_ln(const char *fname, const char *target)
620 struct fstype_info *info = fs_get_info(fs_type);
623 ret = info->ln(fname, target);
626 printf("** Unable to create link %s -> %s **\n", fname, target);
634 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
640 return CMD_RET_USAGE;
642 if (fs_set_blk_dev(argv[1], argv[2], fstype))
645 if (fs_size(argv[3], &size) < 0)
646 return CMD_RET_FAILURE;
648 env_set_hex("filesize", size);
653 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
657 const char *addr_str;
658 const char *filename;
667 return CMD_RET_USAGE;
669 return CMD_RET_USAGE;
671 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
675 addr = simple_strtoul(argv[3], &ep, 16);
676 if (ep == argv[3] || *ep != '\0')
677 return CMD_RET_USAGE;
679 addr_str = env_get("loadaddr");
680 if (addr_str != NULL)
681 addr = simple_strtoul(addr_str, NULL, 16);
683 addr = CONFIG_SYS_LOAD_ADDR;
688 filename = env_get("bootfile");
690 puts("** No boot file defined **\n");
695 bytes = simple_strtoul(argv[5], NULL, 16);
699 pos = simple_strtoul(argv[6], NULL, 16);
704 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
705 time = get_timer(time);
709 printf("%llu bytes read in %lu ms", len_read, time);
712 print_size(div_u64(len_read, time) * 1000, "/s");
717 env_set_hex("fileaddr", addr);
718 env_set_hex("filesize", len_read);
723 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
727 return CMD_RET_USAGE;
729 return CMD_RET_USAGE;
731 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
734 if (fs_ls(argc >= 4 ? argv[3] : "/"))
740 int file_exists(const char *dev_type, const char *dev_part, const char *file,
743 if (fs_set_blk_dev(dev_type, dev_part, fstype))
746 return fs_exists(file);
749 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
753 const char *filename;
760 if (argc < 6 || argc > 7)
761 return CMD_RET_USAGE;
763 if (fs_set_blk_dev(argv[1], argv[2], fstype))
766 addr = simple_strtoul(argv[3], NULL, 16);
768 bytes = simple_strtoul(argv[5], NULL, 16);
770 pos = simple_strtoul(argv[6], NULL, 16);
775 ret = fs_write(filename, addr, pos, bytes, &len);
776 time = get_timer(time);
780 printf("%llu bytes written in %lu ms", len, time);
783 print_size(div_u64(len, time) * 1000, "/s");
791 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
796 memset(uuid, 0, sizeof(uuid));
798 if (argc < 3 || argc > 4)
799 return CMD_RET_USAGE;
801 if (fs_set_blk_dev(argv[1], argv[2], fstype))
806 return CMD_RET_FAILURE;
809 env_set(argv[3], uuid);
811 printf("%s\n", uuid);
813 return CMD_RET_SUCCESS;
816 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
818 struct fstype_info *info;
820 if (argc < 3 || argc > 4)
821 return CMD_RET_USAGE;
823 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
826 info = fs_get_info(fs_type);
829 env_set(argv[3], info->name);
831 printf("%s\n", info->name);
835 return CMD_RET_SUCCESS;
838 int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
842 return CMD_RET_USAGE;
844 if (fs_set_blk_dev(argv[1], argv[2], fstype))
847 if (fs_unlink(argv[3]))
853 int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
859 return CMD_RET_USAGE;
861 if (fs_set_blk_dev(argv[1], argv[2], fstype))
864 ret = fs_mkdir(argv[3]);
866 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
873 int do_ln(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
877 return CMD_RET_USAGE;
879 if (fs_set_blk_dev(argv[1], argv[2], fstype))
882 if (fs_ln(argv[3], argv[4]))