zfs_module.c revision 294999
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: stable/10/sys/boot/efi/boot1/zfs_module.c 294999 2016-01-28 17:24:40Z smh $
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
41static dev_info_t *devices;
42
43static int
44vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
45{
46	dev_info_t *devinfo;
47	off_t lba;
48	EFI_STATUS status;
49
50	devinfo = (dev_info_t *)priv;
51	lba = off / devinfo->dev->Media->BlockSize;
52
53	status = devinfo->dev->ReadBlocks(devinfo->dev,
54	    devinfo->dev->Media->MediaId, lba, bytes, buf);
55	if (status != EFI_SUCCESS) {
56		DPRINTF("vdev_read: failed dev: %p, id: %u, lba: %zu, size: %zu,"
57                    " status: %lu\n", devinfo->dev,
58                    devinfo->dev->Media->MediaId, lba, bytes,
59                    EFI_ERROR_CODE(status));
60		return (-1);
61	}
62
63	return (0);
64}
65
66static EFI_STATUS
67probe(dev_info_t *dev)
68{
69	spa_t *spa;
70	dev_info_t *tdev;
71	EFI_STATUS status;
72
73	/* ZFS consumes the dev on success so we need a copy. */
74	if ((status = bs->AllocatePool(EfiLoaderData, sizeof(*dev),
75	    (void**)&tdev)) != EFI_SUCCESS) {
76		DPRINTF("Failed to allocate tdev (%lu)\n",
77		    EFI_ERROR_CODE(status));
78		return (status);
79	}
80	memcpy(tdev, dev, sizeof(*dev));
81
82	if (vdev_probe(vdev_read, tdev, &spa) != 0) {
83		(void)bs->FreePool(tdev);
84		return (EFI_UNSUPPORTED);
85	}
86
87	dev->devdata = spa;
88	add_device(&devices, dev);
89
90	return (EFI_SUCCESS);
91}
92
93static EFI_STATUS
94try_load(dev_info_t *devinfo, const char *loader_path, void **bufp, size_t *bufsize)
95{
96	spa_t *spa;
97	struct zfsmount zfsmount;
98	dnode_phys_t dn;
99	struct stat st;
100	int err;
101	void *buf;
102	EFI_STATUS status;
103
104	spa = devinfo->devdata;
105	if (zfs_spa_init(spa) != 0) {
106		/* Init failed, don't report this loudly. */
107		return (EFI_NOT_FOUND);
108	}
109
110	if (zfs_mount(spa, 0, &zfsmount) != 0) {
111		/* Mount failed, don't report this loudly. */
112		return (EFI_NOT_FOUND);
113	}
114
115	if ((err = zfs_lookup(&zfsmount, loader_path, &dn)) != 0) {
116		printf("Failed to lookup %s on pool %s (%d)\n", loader_path,
117		    spa->spa_name, err);
118		return (EFI_INVALID_PARAMETER);
119	}
120
121	if ((err = zfs_dnode_stat(spa, &dn, &st)) != 0) {
122		printf("Failed to lookup %s on pool %s (%d)\n", loader_path,
123		    spa->spa_name, err);
124		return (EFI_INVALID_PARAMETER);
125	}
126
127	if ((status = bs->AllocatePool(EfiLoaderData, (UINTN)st.st_size, &buf))
128	    != EFI_SUCCESS) {
129		printf("Failed to allocate load buffer for pool %s (%lu)\n",
130		    spa->spa_name, EFI_ERROR_CODE(status));
131		return (EFI_INVALID_PARAMETER);
132	}
133
134	if ((err = dnode_read(spa, &dn, 0, buf, st.st_size)) != 0) {
135		printf("Failed to read node from %s (%d)\n", spa->spa_name,
136		    err);
137		(void)bs->FreePool(buf);
138		return (EFI_INVALID_PARAMETER);
139	}
140
141	*bufsize = st.st_size;
142	*bufp = buf;
143
144	return (EFI_SUCCESS);
145}
146
147static EFI_STATUS
148load(const char *loader_path, dev_info_t **devinfop, void **bufp,
149    size_t *bufsize)
150{
151	dev_info_t *devinfo;
152	EFI_STATUS status;
153
154	for (devinfo = devices; devinfo != NULL; devinfo = devinfo->next) {
155		status = try_load(devinfo, loader_path, bufp, bufsize);
156		if (status == EFI_SUCCESS) {
157			*devinfop = devinfo;
158			return (EFI_SUCCESS);
159		} else if (status != EFI_NOT_FOUND) {
160			return (status);
161		}
162	}
163
164	return (EFI_NOT_FOUND);
165}
166
167static void
168status()
169{
170	spa_t *spa;
171
172	spa = STAILQ_FIRST(&zfs_pools);
173	if (spa == NULL) {
174		printf("%s found no pools\n", zfs_module.name);
175		return;
176	}
177
178	printf("%s found the following pools:", zfs_module.name);
179	STAILQ_FOREACH(spa, &zfs_pools, spa_link)
180		printf(" %s", spa->spa_name);
181
182	printf("\n");
183}
184
185static void
186init()
187{
188
189	zfs_init();
190}
191
192const boot_module_t zfs_module =
193{
194	.name = "ZFS",
195	.init = init,
196	.probe = probe,
197	.load = load,
198	.status = status
199};
200