1 // SPDX-License-Identifier: GPL-2.0+
4 * ZFS filesystem porting to Uboot by
5 * Jorgen Lundman <lundman at lundman.net>
8 * made from existing GRUB Sources by Sun, GNU and others.
16 #include <linux/ctype.h>
17 #include <asm/byteorder.h>
18 #include <zfs_common.h>
19 #include <linux/stat.h>
22 #if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
26 #if !CONFIG_IS_ENABLED(DOS_PARTITION) && !CONFIG_IS_ENABLED(EFI_PARTITION)
27 #error DOS or EFI partition support must be selected
30 #define DOS_PART_MAGIC_OFFSET 0x1fe
31 #define DOS_FS_TYPE_OFFSET 0x36
32 #define DOS_FS32_TYPE_OFFSET 0x52
34 static int do_zfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
36 char *filename = NULL;
40 disk_partition_t info;
41 struct blk_desc *dev_desc;
45 struct zfs_file zfile;
52 addr = simple_strtoul(argv[3], NULL, 16);
53 filename = env_get("bootfile");
56 addr_str = env_get("loadaddr");
58 addr = simple_strtoul(addr_str, NULL, 16);
60 addr = CONFIG_SYS_LOAD_ADDR;
70 count = simple_strtoul(argv[5], NULL, 16);
74 return cmd_usage(cmdtp);
78 puts("** No boot file defined **\n");
82 part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1);
86 dev = dev_desc->devnum;
87 printf("Loading file \"%s\" from %s device %d%c%c\n",
88 filename, argv[1], dev,
89 part ? ':' : ' ', part ? part + '0' : ' ');
91 zfs_set_blk_dev(dev_desc, &info);
92 vdev.part_length = info.size;
94 memset(&zfile, 0, sizeof(zfile));
96 if (zfs_open(&zfile, filename)) {
97 printf("** File not found %s **\n", filename);
101 if ((count < zfile.size) && (count != 0))
102 zfile.size = (uint64_t)count;
104 if (zfs_read(&zfile, (char *)addr, zfile.size) != zfile.size) {
105 printf("** Unable to read \"%s\" from %s %d:%d **\n",
106 filename, argv[1], dev, part);
113 /* Loading ok, update default load address */
116 printf("%llu bytes read\n", zfile.size);
117 env_set_hex("filesize", zfile.size);
123 int zfs_print(const char *entry, const struct zfs_dirhook_info *data)
126 data->dir ? "<DIR> " : " ",
128 return 0; /* 0 continue, 1 stop */
133 static int do_zfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
135 const char *filename = "/";
137 struct blk_desc *dev_desc;
138 disk_partition_t info;
139 struct device_s vdev;
142 return cmd_usage(cmdtp);
147 part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1);
151 zfs_set_blk_dev(dev_desc, &info);
152 vdev.part_length = info.size;
154 zfs_ls(&vdev, filename,
161 U_BOOT_CMD(zfsls, 4, 1, do_zfs_ls,
162 "list files in a directory (default /)",
163 "<interface> <dev[:part]> [directory]\n"
164 " - list files from 'dev' on 'interface' in a '/DATASET/@/$dir/'");
166 U_BOOT_CMD(zfsload, 6, 0, do_zfs_load,
167 "load binary file from a ZFS filesystem",
168 "<interface> <dev[:part]> [addr] [filename] [bytes]\n"
169 " - load binary file '/DATASET/@/$dir/$file' from 'dev' on 'interface'\n"
170 " to address 'addr' from ZFS filesystem");