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