Move EFI ZFS functions to libefi

This patch moves some EFI ZFS functions from loader to libefi,
allowing them to be used by anything that links against libefi.

Submitted by: Eric McCorkle
Differential Revision: https://reviews.freebsd.org/D11855
This commit is contained in:
Warner Losh 2017-08-04 04:20:06 +00:00
parent acf82d2659
commit 457ea3bce3
4 changed files with 174 additions and 51 deletions

View File

@ -0,0 +1,51 @@
/*-
* Copyright (c) 2016 Eric McCorkle
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <stdint.h>
#ifndef _EFIZFS_H_
#define _EFIZFS_H_
#ifdef EFI_ZFS_BOOT
typedef STAILQ_HEAD(zfsinfo_list, zfsinfo) zfsinfo_list_t;
typedef struct zfsinfo
{
STAILQ_ENTRY(zfsinfo) zi_link;
EFI_HANDLE zi_handle;
uint64_t zi_pool_guid;
} zfsinfo_t;
extern uint64_t pool_guid;
extern void efi_zfs_probe(void);
extern zfsinfo_list_t *efizfs_get_zfsinfo_list(void);
#endif
#endif

View File

@ -12,7 +12,7 @@ INTERNALLIB=
WARNS?= 2
SRCS= delay.c devpath.c efi_console.c efinet.c efipart.c env.c errno.c \
handles.c wchar.c libefi.c efi_driver_utils.c
handles.c wchar.c libefi.c efi_driver_utils.c efizfs.c
.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386"
SRCS+= time.c
@ -38,6 +38,12 @@ CFLAGS+= -fPIC -mno-red-zone
CFLAGS+= -I${.CURDIR}/../include
CFLAGS+= -I${.CURDIR}/../include/${MACHINE}
CFLAGS+= -I${.CURDIR}/../../../../lib/libstand
.if ${MK_ZFS} != "no"
CFLAGS+= -I${.CURDIR}/../../zfs
CFLAGS+= -I${.CURDIR}/../../../cddl/boot/zfs
CFLAGS+= -I${.CURDIR}/../../../crypto/skein
CFLAGS+= -DEFI_ZFS_BOOT
.endif
# Pick up the bootstrap header for some interface items
CFLAGS+= -I${.CURDIR}/../../common

View File

@ -0,0 +1,112 @@
/*-
* Copyright (c) 2008-2010 Rui Paulo
* Copyright (c) 2006 Marcel Moolenaar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/disk.h>
#include <stdint.h>
#ifdef EFI_ZFS_BOOT
#include <libzfs.h>
#endif
#include <efi.h>
#include <efilib.h>
#include "efizfs.h"
#ifdef EFI_ZFS_BOOT
static zfsinfo_list_t zfsinfo;
uint64_t pool_guid;
zfsinfo_list_t *
efizfs_get_zfsinfo_list(void)
{
return (&zfsinfo);
}
static void
insert_zfs(EFI_HANDLE handle, uint64_t guid)
{
zfsinfo_t *zi;
zi = malloc(sizeof(zfsinfo_t));
zi->zi_handle = handle;
zi->zi_pool_guid = guid;
STAILQ_INSERT_TAIL(&zfsinfo, zi, zi_link);
}
void
efi_zfs_probe(void)
{
pdinfo_list_t *hdi;
pdinfo_t *hd, *pd = NULL;
EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
EFI_LOADED_IMAGE *img;
char devname[SPECNAMELEN + 1];
uint64_t guid;
BS->HandleProtocol(IH, &imgid, (VOID**)&img);
hdi = efiblk_get_pdinfo_list(&efipart_hddev);
STAILQ_INIT(&zfsinfo);
/*
* Find the handle for the boot device. The boot1 did find the
* device with loader binary, now we need to search for the
* same device and if it is part of the zfs pool, we record the
* pool GUID for currdev setup.
*/
STAILQ_FOREACH(hd, hdi, pd_link) {
STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
snprintf(devname, sizeof(devname), "%s%dp%d:",
efipart_hddev.dv_name, hd->pd_unit, pd->pd_unit);
if (zfs_probe_dev(devname, &guid) == 0) {
insert_zfs(pd->pd_handle, guid);
if (pd->pd_handle == img->DeviceHandle)
pool_guid = guid;
}
}
}
}
uint64_t
ldi_get_size(void *priv)
{
int fd = (uintptr_t) priv;
uint64_t size;
ioctl(fd, DIOCGMEDIASIZE, &size);
return (size);
}
#endif

View File

@ -48,6 +48,8 @@ __FBSDID("$FreeBSD$");
#ifdef EFI_ZFS_BOOT
#include <libzfs.h>
#include "efizfs.h"
#endif
#include "loader_efi.h"
@ -70,11 +72,6 @@ EFI_GUID debugimg = DEBUG_IMAGE_INFO_TABLE_GUID;
EFI_GUID fdtdtb = FDT_TABLE_GUID;
EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL;
#ifdef EFI_ZFS_BOOT
static void efi_zfs_probe(void);
static uint64_t pool_guid;
#endif
static int
has_keyboard(void)
{
@ -83,7 +80,7 @@ has_keyboard(void)
EFI_HANDLE *hin, *hin_end, *walker;
UINTN sz;
int retval = 0;
/*
* Find all the handles that support the SIMPLE_TEXT_INPUT_PROTOCOL and
* do the typical dance to get the right sized buffer.
@ -140,7 +137,7 @@ has_keyboard(void)
} else if (DevicePathType(path) == MESSAGING_DEVICE_PATH &&
DevicePathSubType(path) == MSG_USB_CLASS_DP) {
USB_CLASS_DEVICE_PATH *usb;
usb = (USB_CLASS_DEVICE_PATH *)(void *)path;
if (usb->DeviceClass == 3 && /* HID */
usb->DeviceSubClass == 1 && /* Boot devices */
@ -892,46 +889,3 @@ command_chain(int argc, char *argv[])
}
COMMAND_SET(chain, "chain", "chain load file", command_chain);
#ifdef EFI_ZFS_BOOT
static void
efi_zfs_probe(void)
{
pdinfo_list_t *hdi;
pdinfo_t *hd, *pd = NULL;
EFI_GUID imgid = LOADED_IMAGE_PROTOCOL;
EFI_LOADED_IMAGE *img;
char devname[SPECNAMELEN + 1];
BS->HandleProtocol(IH, &imgid, (VOID**)&img);
hdi = efiblk_get_pdinfo_list(&efipart_hddev);
/*
* Find the handle for the boot device. The boot1 did find the
* device with loader binary, now we need to search for the
* same device and if it is part of the zfs pool, we record the
* pool GUID for currdev setup.
*/
STAILQ_FOREACH(hd, hdi, pd_link) {
STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
snprintf(devname, sizeof(devname), "%s%dp%d:",
efipart_hddev.dv_name, hd->pd_unit, pd->pd_unit);
if (pd->pd_handle == img->DeviceHandle)
(void) zfs_probe_dev(devname, &pool_guid);
else
(void) zfs_probe_dev(devname, NULL);
}
}
}
uint64_t
ldi_get_size(void *priv)
{
int fd = (uintptr_t) priv;
uint64_t size;
ioctl(fd, DIOCGMEDIASIZE, &size);
return (size);
}
#endif