9071 libefi: Move EFI ZFS functions to libefi
Reviewed by: Alexander Eremin <alexander.eremin@nexenta.com>
Reviewed by: Yuri Pankov <yuripv@yuripv.net>
Approved by: Richard Lowe <richlowe@richlowe.net>
diff --git a/usr/src/boot/sys/boot/efi/loader/main.c b/usr/src/boot/sys/boot/efi/loader/main.c
index c690102..3492736 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 <bootstrap.h>
 #include <smbios.h>
 
-#ifdef EFI_ZFS_BOOT
 #include <libzfs.h>
-#endif
+#include <efizfs.h>
 
 #include "loader_efi.h"
 
@@ -64,10 +63,14 @@
 
 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 @@
 	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 @@
 			} 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 @@
 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 @@
 	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 @@
 		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_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