From 491c47402ff8d899084b49efe96c5369b42e5a54 Mon Sep 17 00:00:00 2001 From: Toomas Soome Date: Tue, 12 Dec 2017 15:23:09 +0200 Subject: [PATCH] libefi: Move EFI ZFS functions to libefi illumos issue #9071 --- usr/src/boot/sys/boot/efi/boot1/Makefile | 3 +- usr/src/boot/sys/boot/efi/boot1/boot1.c | 4 - usr/src/boot/sys/boot/efi/boot1/boot_module.h | 4 - usr/src/boot/sys/boot/efi/include/efizfs.h | 48 +++++++++++ usr/src/boot/sys/boot/efi/libefi/Makefile | 9 +- usr/src/boot/sys/boot/efi/libefi/efizfs.c | 116 ++++++++++++++++++++++++++ usr/src/boot/sys/boot/efi/loader/Makefile | 1 - usr/src/boot/sys/boot/efi/loader/conf.c | 7 -- usr/src/boot/sys/boot/efi/loader/main.c | 73 ++++------------ 9 files changed, 186 insertions(+), 79 deletions(-) create mode 100644 usr/src/boot/sys/boot/efi/include/efizfs.h create mode 100644 usr/src/boot/sys/boot/efi/libefi/efizfs.c diff --git a/usr/src/boot/sys/boot/efi/boot1/Makefile b/usr/src/boot/sys/boot/efi/boot1/Makefile index 0d59cd1d3d..990c519d62 100644 --- a/usr/src/boot/sys/boot/efi/boot1/Makefile +++ b/usr/src/boot/sys/boot/efi/boot1/Makefile @@ -40,12 +40,11 @@ CPPFLAGS += -I./../../../../include CPPFLAGS += -I./../../../sys CPPFLAGS += -I./../../.. CPPFLAGS += -I../../../../lib/libstand -CPPFLAGS += -DEFI_UFS_BOOT -DUFS1_ONLY +CPPFLAGS += -DUFS1_ONLY # CPPFLAGS += -DEFI_DEBUG CPPFLAGS += -I./../../zfs/ CPPFLAGS += -I./../../../cddl/boot/zfs/ -CPPFLAGS += -DEFI_ZFS_BOOT # Always add MI sources and REGULAR efi loader bits CPPFLAGS += -I./../../common diff --git a/usr/src/boot/sys/boot/efi/boot1/boot1.c b/usr/src/boot/sys/boot/efi/boot1/boot1.c index 23d4c22b80..98fd3b00ff 100644 --- a/usr/src/boot/sys/boot/efi/boot1/boot1.c +++ b/usr/src/boot/sys/boot/efi/boot1/boot1.c @@ -40,12 +40,8 @@ struct fs_ops *file_system[] = { static const boot_module_t *boot_modules[] = { -#ifdef EFI_ZFS_BOOT &zfs_module, -#endif -#ifdef EFI_UFS_BOOT &ufs_module -#endif }; #define NUM_BOOT_MODULES nitems(boot_modules) diff --git a/usr/src/boot/sys/boot/efi/boot1/boot_module.h b/usr/src/boot/sys/boot/efi/boot1/boot_module.h index 59d1a7ea4a..30ecd30d9c 100644 --- a/usr/src/boot/sys/boot/efi/boot1/boot_module.h +++ b/usr/src/boot/sys/boot/efi/boot1/boot_module.h @@ -96,12 +96,8 @@ typedef struct boot_module_t } boot_module_t; /* Standard boot modules. */ -#ifdef EFI_UFS_BOOT extern const boot_module_t ufs_module; -#endif -#ifdef EFI_ZFS_BOOT extern const boot_module_t zfs_module; -#endif /* Functions available to modules. */ extern void add_device(dev_info_t **devinfop, dev_info_t *devinfo); diff --git a/usr/src/boot/sys/boot/efi/include/efizfs.h b/usr/src/boot/sys/boot/efi/include/efizfs.h new file mode 100644 index 0000000000..636951c0d6 --- /dev/null +++ b/usr/src/boot/sys/boot/efi/include/efizfs.h @@ -0,0 +1,48 @@ +/* + * 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. + */ + +#include + +#ifndef _EFIZFS_H_ +#define _EFIZFS_H_ + +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); +extern bool efi_zfs_is_preferred(EFI_HANDLE *h); +extern EFI_HANDLE efizfs_get_handle_by_guid(uint64_t); + +#endif diff --git a/usr/src/boot/sys/boot/efi/libefi/Makefile b/usr/src/boot/sys/boot/efi/libefi/Makefile index bc85a15102..0ca24675b6 100644 --- a/usr/src/boot/sys/boot/efi/libefi/Makefile +++ b/usr/src/boot/sys/boot/efi/libefi/Makefile @@ -25,7 +25,8 @@ all: lib$(LIB).a install: SRCS= delay.c devpath.c efi_console.c efi_driver_utils.c efichar.c \ - efinet.c efipart.c env.c errno.c handles.c libefi.c time.c wchar.c + efinet.c efipart.c efizfs.c env.c errno.c handles.c libefi.c \ + time.c wchar.c #.if ${MACHINE_CPUARCH} == "amd64" || ${MACHINE_CPUARCH} == "i386" #SRCS += time.c @@ -35,7 +36,7 @@ SRCS= delay.c devpath.c efi_console.c efi_driver_utils.c efichar.c \ OBJS= $(SRCS:%.c=%.o) -CPPFLAGS= -DTERM_EMU -D_STANDALONE +CPPFLAGS= -D_STANDALONE CFLAGS = -O2 #.if ${MACHINE_CPUARCH} == "aarch64" @@ -50,6 +51,8 @@ CFLAGS += -I$(SRC)/common/ficl -I../../ficl CFLAGS += -I../include CFLAGS += -I../include/${MACH64} CFLAGS += -I../../../../lib/libstand +CFLAGS += -I./../../zfs +CFLAGS += -I./../../../cddl/boot/zfs # Pick up the bootstrap header for some interface items CFLAGS += -I../../common @@ -57,7 +60,7 @@ CFLAGS += -I../../common # Handle FreeBSD specific %b and %D printf format specifiers # CFLAGS+= ${FORMAT_EXTENSIONS} # CFLAGS += -D__printf__=__freebsd_kprintf__ -CFLAGS+= -DTERM_EMU +CFLAGS += -DTERM_EMU include ../Makefile.inc diff --git a/usr/src/boot/sys/boot/efi/libefi/efizfs.c b/usr/src/boot/sys/boot/efi/libefi/efizfs.c new file mode 100644 index 0000000000..f60249221f --- /dev/null +++ b/usr/src/boot/sys/boot/efi/libefi/efizfs.c @@ -0,0 +1,116 @@ +/* + * 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 + +#include +#include + +#include + +#include +#include + +#include "efizfs.h" + +static zfsinfo_list_t zfsinfo; + +uint64_t pool_guid; + +zfsinfo_list_t * +efizfs_get_zfsinfo_list(void) +{ + return (&zfsinfo); +} + +EFI_HANDLE +efizfs_get_handle_by_guid(uint64_t guid) +{ + zfsinfo_t *zi; + + STAILQ_FOREACH(zi, &zfsinfo, zi_link) { + if (zi->zi_pool_guid == guid) { + return (zi->zi_handle); + } + } + return (NULL); +} + +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; + char devname[SPECNAMELEN + 1]; + uint64_t guid; + + 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 (efi_zfs_is_preferred(pd->pd_handle)) + pool_guid = guid; + } + + } + } +} + +uint64_t +ldi_get_size(void *priv) +{ + int fd = (uintptr_t) priv; + uint64_t size; + + ioctl(fd, DIOCGMEDIASIZE, &size); + return (size); +} diff --git a/usr/src/boot/sys/boot/efi/loader/Makefile b/usr/src/boot/sys/boot/efi/loader/Makefile index 4cb13e558f..9f3820d197 100644 --- a/usr/src/boot/sys/boot/efi/loader/Makefile +++ b/usr/src/boot/sys/boot/efi/loader/Makefile @@ -49,7 +49,6 @@ CPPFLAGS += -I./../../.. CPPFLAGS += -I./../../i386/libi386 CPPFLAGS += -I./../../zfs CPPFLAGS += -I./../../../cddl/boot/zfs -CPPFLAGS += -DEFI_ZFS_BOOT CPPFLAGS += -DNO_PCI -DEFI -DTERM_EMU # Export serial numbers, UUID, and asset tag from loader. diff --git a/usr/src/boot/sys/boot/efi/loader/conf.c b/usr/src/boot/sys/boot/efi/loader/conf.c index 6b24517c98..5a7bfede26 100644 --- a/usr/src/boot/sys/boot/efi/loader/conf.c +++ b/usr/src/boot/sys/boot/efi/loader/conf.c @@ -25,32 +25,25 @@ */ #include -__FBSDID("$FreeBSD$"); #include #include #include #include -#ifdef EFI_ZFS_BOOT #include -#endif struct devsw *devsw[] = { &efipart_fddev, &efipart_cddev, &efipart_hddev, &efinet_dev, -#ifdef EFI_ZFS_BOOT &zfs_dev, -#endif NULL }; struct fs_ops *file_system[] = { &gzipfs_fsops, -#ifdef EFI_ZFS_BOOT &zfs_fsops, -#endif &dosfs_fsops, &ufs_fsops, &cd9660_fsops, diff --git a/usr/src/boot/sys/boot/efi/loader/main.c b/usr/src/boot/sys/boot/efi/loader/main.c index c690102794..3492736f94 100644 --- a/usr/src/boot/sys/boot/efi/loader/main.c +++ b/usr/src/boot/sys/boot/efi/loader/main.c @@ -1,4 +1,4 @@ -/*- +/* * Copyright (c) 2008-2010 Rui Paulo * Copyright (c) 2006 Marcel Moolenaar * All rights reserved. @@ -46,9 +46,8 @@ #include #include -#ifdef EFI_ZFS_BOOT #include -#endif +#include #include "loader_efi.h" @@ -64,10 +63,14 @@ EFI_GUID inputid = SIMPLE_TEXT_INPUT_PROTOCOL; extern void acpi_detect(void); extern void efi_getsmap(void); -#ifdef EFI_ZFS_BOOT -static void efi_zfs_probe(void); -static uint64_t pool_guid; -#endif + +static EFI_LOADED_IMAGE *img; + +bool +efi_zfs_is_preferred(EFI_HANDLE *h) +{ + return (h == img->DeviceHandle); +} static int has_keyboard(void) @@ -77,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. @@ -134,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 */ @@ -294,7 +297,6 @@ EFI_STATUS main(int argc, CHAR16 *argv[]) { char var[128]; - EFI_LOADED_IMAGE *img; EFI_GUID *guid; int i, j, vargood, howto; void *ptr; @@ -308,10 +310,11 @@ main(int argc, CHAR16 *argv[]) archsw.arch_readin = efi_readin; archsw.arch_loadaddr = efi_loadaddr; archsw.arch_free_loadaddr = efi_free_loadaddr; -#ifdef EFI_ZFS_BOOT /* Note this needs to be set before ZFS init. */ archsw.arch_zfs_probe = efi_zfs_probe; -#endif + + /* Get our loaded image protocol interface structure. */ + BS->HandleProtocol(IH, &imgid, (VOID**)&img); /* Init the time source */ efi_time_init(); @@ -438,9 +441,6 @@ main(int argc, CHAR16 *argv[]) if (devsw[i]->dv_init != NULL) (devsw[i]->dv_init)(); - /* Get our loaded image protocol interface structure. */ - BS->HandleProtocol(IH, &imgid, (VOID**)&img); - printf("Command line arguments:"); for (i = 0; i < argc; i++) { printf(" %S", argv[i]); @@ -902,46 +902,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 -- 2.11.4.GIT