2 * Copyright (c) 2015 Eric McCorkle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/queue.h>
36 #include "boot_module.h"
41 static dev_info_t *devices;
44 ldi_get_size(void *priv)
46 dev_info_t *devinfo = priv;
48 return (devinfo->dev->Media->BlockSize *
49 (devinfo->dev->Media->LastBlock + 1));
53 vdev_read(vdev_t *vdev, void *priv, off_t off, void *buf, size_t bytes)
57 size_t size, remainder, rb_size, blksz;
58 char *bouncebuf = NULL, *rb_buf;
61 devinfo = (dev_info_t *)priv;
62 lba = off / devinfo->dev->Media->BlockSize;
63 remainder = off % devinfo->dev->Media->BlockSize;
69 * If we have remainder from off, we need to add remainder part.
70 * Since buffer must be multiple of the BlockSize, round it all up.
72 size = roundup2(bytes + remainder, devinfo->dev->Media->BlockSize);
74 if (remainder != 0 || size != bytes) {
75 rb_size = devinfo->dev->Media->BlockSize;
76 bouncebuf = malloc(rb_size);
77 if (bouncebuf == NULL) {
78 printf("vdev_read: out of memory\n");
82 blksz = rb_size - remainder;
86 status = devinfo->dev->ReadBlocks(devinfo->dev,
87 devinfo->dev->Media->MediaId, lba, rb_size, rb_buf);
88 if (EFI_ERROR(status))
92 if (bouncebuf != NULL)
93 memcpy(buf, rb_buf + remainder, blksz);
94 buf = (void *)((uintptr_t)buf + blksz);
106 DPRINTF("vdev_read: failed dev: %p, id: %u, lba: %ju, size: %zu,"
107 " rb_size: %zu, status: %lu\n", devinfo->dev,
108 devinfo->dev->Media->MediaId, (uintmax_t)lba, bytes, rb_size,
109 EFI_ERROR_CODE(status));
114 probe(dev_info_t *dev)
120 /* ZFS consumes the dev on success so we need a copy. */
121 if ((status = bs->AllocatePool(EfiLoaderData, sizeof(*dev),
122 (void**)&tdev)) != EFI_SUCCESS) {
123 DPRINTF("Failed to allocate tdev (%lu)\n",
124 EFI_ERROR_CODE(status));
127 memcpy(tdev, dev, sizeof(*dev));
129 if (vdev_probe(vdev_read, tdev, &spa) != 0) {
130 (void)bs->FreePool(tdev);
131 return (EFI_UNSUPPORTED);
135 add_device(&devices, dev);
137 return (EFI_SUCCESS);
141 load(const char *filepath, dev_info_t *devinfo, void **bufp, size_t *bufsize)
144 struct zfsmount zfsmount;
151 spa = devinfo->devdata;
153 DPRINTF("load: '%s' spa: '%s', devpath: %s\n", filepath, spa->spa_name,
154 devpath_str(devinfo->devpath));
156 if ((err = zfs_spa_init(spa)) != 0) {
157 DPRINTF("Failed to load pool '%s' (%d)\n", spa->spa_name, err);
158 return (EFI_NOT_FOUND);
161 if ((err = zfs_mount(spa, 0, &zfsmount)) != 0) {
162 DPRINTF("Failed to mount pool '%s' (%d)\n", spa->spa_name, err);
163 return (EFI_NOT_FOUND);
166 if ((err = zfs_lookup(&zfsmount, filepath, &dn)) != 0) {
168 DPRINTF("Failed to find '%s' on pool '%s' (%d)\n",
169 filepath, spa->spa_name, err);
170 return (EFI_NOT_FOUND);
172 printf("Failed to lookup '%s' on pool '%s' (%d)\n", filepath,
174 return (EFI_INVALID_PARAMETER);
177 if ((err = zfs_dnode_stat(spa, &dn, &st)) != 0) {
178 printf("Failed to stat '%s' on pool '%s' (%d)\n", filepath,
180 return (EFI_INVALID_PARAMETER);
183 if ((status = bs->AllocatePool(EfiLoaderData, (UINTN)st.st_size, &buf))
185 printf("Failed to allocate load buffer %zd for pool '%s' for '%s' "
186 "(%lu)\n", st.st_size, spa->spa_name, filepath, EFI_ERROR_CODE(status));
187 return (EFI_INVALID_PARAMETER);
190 if ((err = dnode_read(spa, &dn, 0, buf, st.st_size)) != 0) {
191 printf("Failed to read node from %s (%d)\n", spa->spa_name,
193 (void)bs->FreePool(buf);
194 return (EFI_INVALID_PARAMETER);
197 *bufsize = st.st_size;
200 return (EFI_SUCCESS);
208 spa = STAILQ_FIRST(&zfs_pools);
210 printf("%s found no pools\n", zfs_module.name);
214 printf("%s found the following pools:", zfs_module.name);
215 STAILQ_FOREACH(spa, &zfs_pools, spa_link)
216 printf(" %s", spa->spa_name);
235 const boot_module_t zfs_module =