xref: /freebsd/stand/efi/boot1/zfs_module.c (revision 9247238cc4b8835892a47701136b0fd073f8d67c)
1 /*-
2  * Copyright (c) 2015 Eric McCorkle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 #include <stddef.h>
29 #include <stdarg.h>
30 #include <stdbool.h>
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <efi.h>
35 
36 #include "boot_module.h"
37 
38 #include "libzfs.h"
39 #include "zfsimpl.c"
40 
41 static dev_info_t *devices;
42 
43 static char zfs_bootonce[VDEV_PAD_SIZE];
44 
45 uint64_t
46 ldi_get_size(void *priv)
47 {
48 	dev_info_t *devinfo = priv;
49 
50 	return (devinfo->dev->Media->BlockSize *
51 	    (devinfo->dev->Media->LastBlock + 1));
52 }
53 
54 static int
55 vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
56 {
57 	dev_info_t *devinfo;
58 	uint64_t lba;
59 	size_t size, remainder, rb_size, blksz;
60 	char *bouncebuf = NULL, *rb_buf;
61 	EFI_STATUS status;
62 
63 	devinfo = (dev_info_t *)priv;
64 	lba = off / devinfo->dev->Media->BlockSize;
65 	remainder = off % devinfo->dev->Media->BlockSize;
66 
67 	rb_buf = buf;
68 	rb_size = bytes;
69 
70 	/*
71 	 * If we have remainder from off, we need to add remainder part.
72 	 * Since buffer must be multiple of the BlockSize, round it all up.
73 	 */
74 	size = roundup2(bytes + remainder, devinfo->dev->Media->BlockSize);
75 	blksz = size;
76 	if (remainder != 0 || size != bytes) {
77 		rb_size = devinfo->dev->Media->BlockSize;
78 		bouncebuf = malloc(rb_size);
79 		if (bouncebuf == NULL) {
80 			printf("vdev_read: out of memory\n");
81 			return (-1);
82 		}
83 		rb_buf = bouncebuf;
84 		blksz = rb_size - remainder;
85 	}
86 
87 	while (bytes > 0) {
88 		status = devinfo->dev->ReadBlocks(devinfo->dev,
89 		    devinfo->dev->Media->MediaId, lba, rb_size, rb_buf);
90 		if (EFI_ERROR(status))
91 				goto error;
92 		if (bytes < blksz)
93 			blksz = bytes;
94 		if (bouncebuf != NULL)
95 			memcpy(buf, rb_buf + remainder, blksz);
96 		buf = (void *)((uintptr_t)buf + blksz);
97 		bytes -= blksz;
98 		lba++;
99 		remainder = 0;
100 		blksz = rb_size;
101 	}
102 
103 	free(bouncebuf);
104 	return (0);
105 
106 error:
107 	free(bouncebuf);
108 	DPRINTF("vdev_read: failed dev: %p, id: %u, lba: %ju, size: %zu,"
109 	    " rb_size: %zu, status: %lu\n", devinfo->dev,
110 	    devinfo->dev->Media->MediaId, (uintmax_t)lba, bytes, rb_size,
111 	    EFI_ERROR_CODE(status));
112 	return (-1);
113 }
114 
115 static EFI_STATUS
116 probe(dev_info_t *dev)
117 {
118 	spa_t *spa;
119 	dev_info_t *tdev;
120 
121 	/* ZFS consumes the dev on success so we need a copy. */
122 	tdev = malloc(sizeof(*dev));
123 	if (tdev == NULL) {
124 		DPRINTF("Failed to allocate tdev\n");
125 		return (EFI_OUT_OF_RESOURCES);
126 	}
127 	memcpy(tdev, dev, sizeof(*dev));
128 
129 	if (vdev_probe(vdev_read, NULL, tdev, &spa) != 0) {
130 		free(tdev);
131 		return (EFI_UNSUPPORTED);
132 	}
133 
134 	dev->devdata = spa;
135 	add_device(&devices, dev);
136 
137 	return (EFI_SUCCESS);
138 }
139 
140 static EFI_STATUS
141 load(const char *filepath, dev_info_t *devinfo, void **bufp, size_t *bufsize)
142 {
143 	spa_t *spa;
144 	struct zfsmount zmount;
145 	dnode_phys_t dn;
146 	struct stat st;
147 	uint64_t rootobj;
148 	int err;
149 	void *buf;
150 
151 	spa = devinfo->devdata;
152 
153 #ifdef EFI_DEBUG
154 	{
155 		CHAR16 *text = efi_devpath_name(devinfo->devpath);
156 		DPRINTF("load: '%s' spa: '%s', devpath: %S\n", filepath,
157 		    spa->spa_name, text);
158 		efi_free_devpath_name(text);
159 	}
160 #endif
161 	if ((err = zfs_spa_init(spa)) != 0) {
162 		DPRINTF("Failed to load pool '%s' (%d)\n", spa->spa_name, err);
163 		return (EFI_NOT_FOUND);
164 	}
165 
166 	if (zfs_get_bootonce_spa(spa, OS_BOOTONCE, zfs_bootonce,
167 	    sizeof(zfs_bootonce)) == 0) {
168 		/*
169 		 * If bootonce attribute is present, use it as root dataset.
170 		 * Any attempt to use it should clear the 'once' flag.  Prior
171 		 * to now, we'd not be able to clear it anyway.  We don't care
172 		 * if we can't find the files to boot, or if there's a problem
173 		 * with it: we've tried to use it once we're able to mount the
174 		 * ZFS dataset.
175 		 *
176 		 * Note: the attribute is prefixed with "zfs:" and suffixed
177 		 * with ":".
178 		 */
179 		char *dname, *end;
180 
181 		if (zfs_bootonce[0] != 'z' || zfs_bootonce[1] != 'f' ||
182 		    zfs_bootonce[2] != 's' || zfs_bootonce[3] != ':' ||
183 		    (dname = strchr(&zfs_bootonce[4], '/')) == NULL ||
184 		    (end = strrchr(&zfs_bootonce[4], ':')) == NULL) {
185 			printf("INVALID zfs bootonce: %s\n", zfs_bootonce);
186 			*zfs_bootonce = '\0';
187 			rootobj = 0;
188 		} else {
189 			dname += 1;
190 			*end = '\0';
191 			if (zfs_lookup_dataset(spa, dname, &rootobj) != 0) {
192 				printf("zfs bootonce dataset %s NOT FOUND\n",
193 				    dname);
194 				*zfs_bootonce = '\0';
195 				rootobj = 0;
196 			} else
197 				printf("zfs bootonce: %s\n", zfs_bootonce);
198 			*end = ':';
199 		}
200 	} else {
201 		*zfs_bootonce = '\0';
202 		rootobj = 0;
203 	}
204 
205 	if ((err = zfs_mount_impl(spa, rootobj, &zmount)) != 0) {
206 		printf("Failed to mount pool '%s' (%d)\n", spa->spa_name, err);
207 		return (EFI_NOT_FOUND);
208 	}
209 
210 	if ((err = zfs_lookup(&zmount, filepath, &dn)) != 0) {
211 		if (err == ENOENT) {
212 			DPRINTF("Failed to find '%s' on pool '%s' (%d)\n",
213 			    filepath, spa->spa_name, err);
214 			return (EFI_NOT_FOUND);
215 		}
216 		printf("Failed to lookup '%s' on pool '%s' (%d)\n", filepath,
217 		    spa->spa_name, err);
218 		return (EFI_INVALID_PARAMETER);
219 	}
220 
221 	if ((err = zfs_dnode_stat(spa, &dn, &st)) != 0) {
222 		printf("Failed to stat '%s' on pool '%s' (%d)\n", filepath,
223 		    spa->spa_name, err);
224 		return (EFI_INVALID_PARAMETER);
225 	}
226 
227 	buf = malloc(st.st_size);
228 	if (buf == NULL) {
229 		printf("Failed to allocate load buffer %jd for pool '%s' for '%s' ",
230 		    (intmax_t)st.st_size, spa->spa_name, filepath);
231 		return (EFI_INVALID_PARAMETER);
232 	}
233 
234 	if ((err = dnode_read(spa, &dn, 0, buf, st.st_size)) != 0) {
235 		printf("Failed to read node from %s (%d)\n", spa->spa_name,
236 		    err);
237 		free(buf);
238 		return (EFI_INVALID_PARAMETER);
239 	}
240 
241 	*bufsize = st.st_size;
242 	*bufp = buf;
243 
244 	return (EFI_SUCCESS);
245 }
246 
247 static void
248 status(void)
249 {
250 	spa_t *spa;
251 
252 	spa = STAILQ_FIRST(&zfs_pools);
253 	if (spa == NULL) {
254 		printf("%s found no pools\n", zfs_module.name);
255 		return;
256 	}
257 
258 	printf("%s found the following pools:", zfs_module.name);
259 	STAILQ_FOREACH(spa, &zfs_pools, spa_link)
260 		printf(" %s", spa->spa_name);
261 
262 	printf("\n");
263 }
264 
265 static const char *
266 extra_env(void)
267 {
268 	char *rv = NULL;	/* So we return NULL if asprintf fails */
269 
270 	if (*zfs_bootonce == '\0')
271 		return NULL;
272 	asprintf(&rv, "zfs-bootonce=%s", zfs_bootonce);
273 	return (rv);
274 }
275 
276 
277 static void
278 init(void)
279 {
280 
281 	zfs_init();
282 }
283 
284 static dev_info_t *
285 _devices(void)
286 {
287 
288 	return (devices);
289 }
290 
291 const boot_module_t zfs_module =
292 {
293 	.name = "ZFS",
294 	.init = init,
295 	.probe = probe,
296 	.load = load,
297 	.status = status,
298 	.devices = _devices,
299 	.extra_env = extra_env,
300 };
301