blob: 2f831532c2e8de6c73b653da1d7804174c6f32e7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +00002/*
3 *
4 * ZFS filesystem porting to Uboot by
5 * Jorgen Lundman <lundman at lundman.net>
6 *
7 * zfsfs support
8 * made from existing GRUB Sources by Sun, GNU and others.
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +00009 */
10
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000011#include <part.h>
12#include <config.h>
13#include <command.h>
Simon Glass313112a2019-08-01 09:46:46 -060014#include <env.h>
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000015#include <image.h>
16#include <linux/ctype.h>
17#include <asm/byteorder.h>
18#include <zfs_common.h>
19#include <linux/stat.h>
20#include <malloc.h>
21
22#if defined(CONFIG_CMD_USB) && defined(CONFIG_USB_STORAGE)
23#include <usb.h>
24#endif
25
Patrick Delaunay8a4f2bd2017-01-27 11:00:41 +010026#if !CONFIG_IS_ENABLED(DOS_PARTITION) && !CONFIG_IS_ENABLED(EFI_PARTITION)
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000027#error DOS or EFI partition support must be selected
28#endif
29
30#define DOS_PART_MAGIC_OFFSET 0x1fe
31#define DOS_FS_TYPE_OFFSET 0x36
32#define DOS_FS32_TYPE_OFFSET 0x52
33
Simon Glassed38aef2020-05-10 11:40:03 -060034static int do_zfs_load(struct cmd_tbl *cmdtp, int flag, int argc,
35 char *const argv[])
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000036{
37 char *filename = NULL;
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000038 int dev;
Rob Herring153817e2012-08-23 11:31:49 +000039 int part;
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000040 ulong addr = 0;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060041 struct disk_partition info;
Simon Glasse3394752016-02-29 15:25:34 -070042 struct blk_desc *dev_desc;
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000043 unsigned long count;
44 const char *addr_str;
45 struct zfs_file zfile;
46 struct device_s vdev;
47
48 if (argc < 3)
49 return CMD_RET_USAGE;
50
51 count = 0;
Simon Glass3ff49ec2021-07-24 09:03:29 -060052 addr = hextoul(argv[3], NULL);
Simon Glass64b723f2017-08-03 12:22:12 -060053 filename = env_get("bootfile");
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000054 switch (argc) {
55 case 3:
Simon Glass64b723f2017-08-03 12:22:12 -060056 addr_str = env_get("loadaddr");
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000057 if (addr_str != NULL)
Simon Glass3ff49ec2021-07-24 09:03:29 -060058 addr = hextoul(addr_str, NULL);
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000059 else
60 addr = CONFIG_SYS_LOAD_ADDR;
61
62 break;
63 case 4:
64 break;
65 case 5:
66 filename = argv[4];
67 break;
68 case 6:
69 filename = argv[4];
Simon Glass3ff49ec2021-07-24 09:03:29 -060070 count = hextoul(argv[5], NULL);
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000071 break;
72
73 default:
74 return cmd_usage(cmdtp);
75 }
76
77 if (!filename) {
78 puts("** No boot file defined **\n");
79 return 1;
80 }
81
Simon Glasse76ee972016-02-29 15:25:44 -070082 part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1);
Rob Herring153817e2012-08-23 11:31:49 +000083 if (part < 0)
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000084 return 1;
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000085
Simon Glass2f26fff2016-02-29 15:25:51 -070086 dev = dev_desc->devnum;
Rob Herring153817e2012-08-23 11:31:49 +000087 printf("Loading file \"%s\" from %s device %d%c%c\n",
88 filename, argv[1], dev,
89 part ? ':' : ' ', part ? part + '0' : ' ');
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000090
Rob Herring153817e2012-08-23 11:31:49 +000091 zfs_set_blk_dev(dev_desc, &info);
92 vdev.part_length = info.size;
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000093
94 memset(&zfile, 0, sizeof(zfile));
95 zfile.device = &vdev;
96 if (zfs_open(&zfile, filename)) {
Luka Perkove0773892013-10-10 01:32:28 +020097 printf("** File not found %s **\n", filename);
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +000098 return 1;
99 }
100
101 if ((count < zfile.size) && (count != 0))
102 zfile.size = (uint64_t)count;
103
104 if (zfs_read(&zfile, (char *)addr, zfile.size) != zfile.size) {
Rob Herring153817e2012-08-23 11:31:49 +0000105 printf("** Unable to read \"%s\" from %s %d:%d **\n",
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +0000106 filename, argv[1], dev, part);
107 zfs_close(&zfile);
108 return 1;
109 }
110
111 zfs_close(&zfile);
112
113 /* Loading ok, update default load address */
Simon Glass892265d2019-12-28 10:45:02 -0700114 image_load_addr = addr;
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +0000115
116 printf("%llu bytes read\n", zfile.size);
Simon Glass4d949a22017-08-03 12:22:10 -0600117 env_set_hex("filesize", zfile.size);
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +0000118
119 return 0;
120}
121
122
123int zfs_print(const char *entry, const struct zfs_dirhook_info *data)
124{
125 printf("%s %s\n",
126 data->dir ? "<DIR> " : " ",
127 entry);
128 return 0; /* 0 continue, 1 stop */
129}
130
131
Simon Glassed38aef2020-05-10 11:40:03 -0600132static int do_zfs_ls(struct cmd_tbl *cmdtp, int flag, int argc,
133 char *const argv[])
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +0000134{
135 const char *filename = "/";
Rob Herring153817e2012-08-23 11:31:49 +0000136 int part;
Simon Glasse3394752016-02-29 15:25:34 -0700137 struct blk_desc *dev_desc;
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600138 struct disk_partition info;
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +0000139 struct device_s vdev;
140
Rob Herring153817e2012-08-23 11:31:49 +0000141 if (argc < 2)
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +0000142 return cmd_usage(cmdtp);
143
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +0000144 if (argc == 4)
145 filename = argv[3];
146
Simon Glasse76ee972016-02-29 15:25:44 -0700147 part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1);
Rob Herring153817e2012-08-23 11:31:49 +0000148 if (part < 0)
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +0000149 return 1;
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +0000150
Rob Herring153817e2012-08-23 11:31:49 +0000151 zfs_set_blk_dev(dev_desc, &info);
152 vdev.part_length = info.size;
Jorgen Lundman9b4a1f92012-07-19 20:48:25 +0000153
154 zfs_ls(&vdev, filename,
155 zfs_print);
156
157 return 0;
158}
159
160
161U_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/'");
165
166U_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");