19f23ea4bSToomas Soome /*
29f23ea4bSToomas Soome  * Copyright (c) 2008-2010 Rui Paulo
39f23ea4bSToomas Soome  * Copyright (c) 2006 Marcel Moolenaar
49f23ea4bSToomas Soome  * All rights reserved.
59f23ea4bSToomas Soome  *
69f23ea4bSToomas Soome  * Redistribution and use in source and binary forms, with or without
79f23ea4bSToomas Soome  * modification, are permitted provided that the following conditions
89f23ea4bSToomas Soome  * are met:
99f23ea4bSToomas Soome  *
109f23ea4bSToomas Soome  * 1. Redistributions of source code must retain the above copyright
119f23ea4bSToomas Soome  *    notice, this list of conditions and the following disclaimer.
129f23ea4bSToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
139f23ea4bSToomas Soome  *    notice, this list of conditions and the following disclaimer in the
149f23ea4bSToomas Soome  *    documentation and/or other materials provided with the distribution.
159f23ea4bSToomas Soome  *
169f23ea4bSToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
179f23ea4bSToomas Soome  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
189f23ea4bSToomas Soome  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
199f23ea4bSToomas Soome  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
209f23ea4bSToomas Soome  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
219f23ea4bSToomas Soome  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
229f23ea4bSToomas Soome  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
239f23ea4bSToomas Soome  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
249f23ea4bSToomas Soome  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
259f23ea4bSToomas Soome  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
269f23ea4bSToomas Soome  */
279f23ea4bSToomas Soome 
289f23ea4bSToomas Soome #include <sys/cdefs.h>
299f23ea4bSToomas Soome 
309f23ea4bSToomas Soome #include <sys/param.h>
319f23ea4bSToomas Soome #include <sys/disk.h>
3276b35943SToomas Soome #include <stand.h>
339f23ea4bSToomas Soome 
349f23ea4bSToomas Soome #include <libzfs.h>
359f23ea4bSToomas Soome 
369f23ea4bSToomas Soome #include <efi.h>
379f23ea4bSToomas Soome #include <efilib.h>
389f23ea4bSToomas Soome 
399f23ea4bSToomas Soome #include "efizfs.h"
409f23ea4bSToomas Soome 
419f23ea4bSToomas Soome static zfsinfo_list_t zfsinfo;
429f23ea4bSToomas Soome 
439f23ea4bSToomas Soome uint64_t pool_guid;
449f23ea4bSToomas Soome 
459f23ea4bSToomas Soome zfsinfo_list_t *
efizfs_get_zfsinfo_list(void)469f23ea4bSToomas Soome efizfs_get_zfsinfo_list(void)
479f23ea4bSToomas Soome {
489f23ea4bSToomas Soome 	return (&zfsinfo);
499f23ea4bSToomas Soome }
509f23ea4bSToomas Soome 
519f23ea4bSToomas Soome EFI_HANDLE
efizfs_get_handle_by_guid(uint64_t guid)529f23ea4bSToomas Soome efizfs_get_handle_by_guid(uint64_t guid)
539f23ea4bSToomas Soome {
549f23ea4bSToomas Soome 	zfsinfo_t *zi;
559f23ea4bSToomas Soome 
569f23ea4bSToomas Soome 	STAILQ_FOREACH(zi, &zfsinfo, zi_link) {
579f23ea4bSToomas Soome 		if (zi->zi_pool_guid == guid) {
589f23ea4bSToomas Soome 			return (zi->zi_handle);
599f23ea4bSToomas Soome 		}
609f23ea4bSToomas Soome 	}
619f23ea4bSToomas Soome 	return (NULL);
629f23ea4bSToomas Soome }
639f23ea4bSToomas Soome 
64*e3026534SToomas Soome bool
efizfs_get_guid_by_handle(EFI_HANDLE handle,uint64_t * guid)65*e3026534SToomas Soome efizfs_get_guid_by_handle(EFI_HANDLE handle, uint64_t *guid)
66*e3026534SToomas Soome {
67*e3026534SToomas Soome 	zfsinfo_t *zi;
68*e3026534SToomas Soome 
69*e3026534SToomas Soome 	if (guid == NULL)
70*e3026534SToomas Soome 		return (false);
71*e3026534SToomas Soome 	STAILQ_FOREACH(zi, &zfsinfo, zi_link) {
72*e3026534SToomas Soome 		if (zi->zi_handle == handle) {
73*e3026534SToomas Soome 			*guid = zi->zi_pool_guid;
74*e3026534SToomas Soome 			return (true);
75*e3026534SToomas Soome 		}
76*e3026534SToomas Soome 	}
77*e3026534SToomas Soome 	return (false);
78*e3026534SToomas Soome }
79*e3026534SToomas Soome 
809f23ea4bSToomas Soome static void
insert_zfs(EFI_HANDLE handle,uint64_t guid)819f23ea4bSToomas Soome insert_zfs(EFI_HANDLE handle, uint64_t guid)
829f23ea4bSToomas Soome {
839f23ea4bSToomas Soome         zfsinfo_t *zi;
849f23ea4bSToomas Soome 
859f23ea4bSToomas Soome         zi = malloc(sizeof(zfsinfo_t));
869f23ea4bSToomas Soome         zi->zi_handle = handle;
879f23ea4bSToomas Soome         zi->zi_pool_guid = guid;
889f23ea4bSToomas Soome         STAILQ_INSERT_TAIL(&zfsinfo, zi, zi_link);
899f23ea4bSToomas Soome }
909f23ea4bSToomas Soome 
919f23ea4bSToomas Soome void
efi_zfs_probe(void)929f23ea4bSToomas Soome efi_zfs_probe(void)
939f23ea4bSToomas Soome {
949f23ea4bSToomas Soome 	pdinfo_list_t *hdi;
959f23ea4bSToomas Soome 	pdinfo_t *hd, *pd = NULL;
969f23ea4bSToomas Soome 	char devname[SPECNAMELEN + 1];
979f23ea4bSToomas Soome         uint64_t guid;
989f23ea4bSToomas Soome 
999f23ea4bSToomas Soome 	hdi = efiblk_get_pdinfo_list(&efipart_hddev);
1009f23ea4bSToomas Soome 	STAILQ_INIT(&zfsinfo);
1019f23ea4bSToomas Soome 
1029f23ea4bSToomas Soome 	/*
1039f23ea4bSToomas Soome 	 * Find the handle for the boot device. The boot1 did find the
1049f23ea4bSToomas Soome 	 * device with loader binary, now we need to search for the
1059f23ea4bSToomas Soome 	 * same device and if it is part of the zfs pool, we record the
1069f23ea4bSToomas Soome 	 * pool GUID for currdev setup.
1079f23ea4bSToomas Soome 	 */
1089f23ea4bSToomas Soome 	STAILQ_FOREACH(hd, hdi, pd_link) {
1099f23ea4bSToomas Soome 		STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
1109f23ea4bSToomas Soome 
1119f23ea4bSToomas Soome 			snprintf(devname, sizeof(devname), "%s%dp%d:",
1129f23ea4bSToomas Soome 			    efipart_hddev.dv_name, hd->pd_unit, pd->pd_unit);
1139f23ea4bSToomas Soome 
1149f23ea4bSToomas Soome                         if (zfs_probe_dev(devname, &guid) == 0) {
1159f23ea4bSToomas Soome                                 insert_zfs(pd->pd_handle, guid);
1169f23ea4bSToomas Soome 
1179f23ea4bSToomas Soome                                 if (efi_zfs_is_preferred(pd->pd_handle))
1189f23ea4bSToomas Soome                                         pool_guid = guid;
1199f23ea4bSToomas Soome                         }
1209f23ea4bSToomas Soome 
1219f23ea4bSToomas Soome 		}
1229f23ea4bSToomas Soome 	}
1239f23ea4bSToomas Soome }
124