xref: /freebsd/stand/efi/libefi/efizfs.c (revision 7c43148a974877188a930e4078a164f83da8e652)
1ca987d46SWarner Losh /*-
2ca987d46SWarner Losh  * Copyright (c) 2008-2010 Rui Paulo
3ca987d46SWarner Losh  * Copyright (c) 2006 Marcel Moolenaar
4ca987d46SWarner Losh  * All rights reserved.
5ca987d46SWarner Losh  *
6ca987d46SWarner Losh  * Redistribution and use in source and binary forms, with or without
7ca987d46SWarner Losh  * modification, are permitted provided that the following conditions
8ca987d46SWarner Losh  * are met:
9ca987d46SWarner Losh  *
10ca987d46SWarner Losh  * 1. Redistributions of source code must retain the above copyright
11ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer.
12ca987d46SWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
13ca987d46SWarner Losh  *    notice, this list of conditions and the following disclaimer in the
14ca987d46SWarner Losh  *    documentation and/or other materials provided with the distribution.
15ca987d46SWarner Losh  *
16ca987d46SWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17ca987d46SWarner Losh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ca987d46SWarner Losh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ca987d46SWarner Losh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20ca987d46SWarner Losh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21ca987d46SWarner Losh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22ca987d46SWarner Losh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23ca987d46SWarner Losh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24ca987d46SWarner Losh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25ca987d46SWarner Losh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ca987d46SWarner Losh  */
27ca987d46SWarner Losh 
28ca987d46SWarner Losh #include <sys/param.h>
29de04d704SWarner Losh #include <stand.h>
30ca987d46SWarner Losh 
31ca987d46SWarner Losh #ifdef EFI_ZFS_BOOT
32ca987d46SWarner Losh #include <libzfs.h>
33ca987d46SWarner Losh #endif
34ca987d46SWarner Losh 
35ca987d46SWarner Losh #include <efi.h>
36ca987d46SWarner Losh #include <efilib.h>
37ca987d46SWarner Losh 
38ca987d46SWarner Losh #include "efizfs.h"
39ca987d46SWarner Losh 
40ca987d46SWarner Losh #ifdef EFI_ZFS_BOOT
41ca987d46SWarner Losh static zfsinfo_list_t zfsinfo;
42ca987d46SWarner Losh 
43ca987d46SWarner Losh uint64_t pool_guid;
44ca987d46SWarner Losh 
45ca987d46SWarner Losh zfsinfo_list_t *
efizfs_get_zfsinfo_list(void)46ca987d46SWarner Losh efizfs_get_zfsinfo_list(void)
47ca987d46SWarner Losh {
48ca987d46SWarner Losh 	return (&zfsinfo);
49ca987d46SWarner Losh }
50ca987d46SWarner Losh 
51ca987d46SWarner Losh EFI_HANDLE
efizfs_get_handle_by_guid(uint64_t guid)52ca987d46SWarner Losh efizfs_get_handle_by_guid(uint64_t guid)
53ca987d46SWarner Losh {
54ca987d46SWarner Losh 	zfsinfo_t *zi;
55ca987d46SWarner Losh 
56ca987d46SWarner Losh 	STAILQ_FOREACH(zi, &zfsinfo, zi_link) {
57ca987d46SWarner Losh 		if (zi->zi_pool_guid == guid) {
58ca987d46SWarner Losh 			return (zi->zi_handle);
59ca987d46SWarner Losh 		}
60ca987d46SWarner Losh 	}
61ca987d46SWarner Losh 	return (NULL);
62ca987d46SWarner Losh }
63ca987d46SWarner Losh 
64fa9dc8d3SWarner Losh bool
efizfs_get_guid_by_handle(EFI_HANDLE handle,uint64_t * guid)65fa9dc8d3SWarner Losh efizfs_get_guid_by_handle(EFI_HANDLE handle, uint64_t *guid)
66fa9dc8d3SWarner Losh {
67fa9dc8d3SWarner Losh 	zfsinfo_t *zi;
68fa9dc8d3SWarner Losh 
69fa9dc8d3SWarner Losh 	if (guid == NULL)
70fa9dc8d3SWarner Losh 		return (false);
71fa9dc8d3SWarner Losh 	STAILQ_FOREACH(zi, &zfsinfo, zi_link) {
72fa9dc8d3SWarner Losh 		if (zi->zi_handle == handle) {
73fa9dc8d3SWarner Losh 			*guid = zi->zi_pool_guid;
74fa9dc8d3SWarner Losh 			return (true);
75fa9dc8d3SWarner Losh 		}
76fa9dc8d3SWarner Losh 	}
77fa9dc8d3SWarner Losh 	return (false);
78fa9dc8d3SWarner Losh }
79fa9dc8d3SWarner Losh 
80ca987d46SWarner Losh static void
insert_zfs(EFI_HANDLE handle,uint64_t guid)81ca987d46SWarner Losh insert_zfs(EFI_HANDLE handle, uint64_t guid)
82ca987d46SWarner Losh {
83ca987d46SWarner Losh         zfsinfo_t *zi;
84ca987d46SWarner Losh 
85ca987d46SWarner Losh         zi = malloc(sizeof(zfsinfo_t));
867afa0d95SToomas Soome 	if (zi != NULL) {
87ca987d46SWarner Losh         	zi->zi_handle = handle;
88ca987d46SWarner Losh         	zi->zi_pool_guid = guid;
89ca987d46SWarner Losh         	STAILQ_INSERT_TAIL(&zfsinfo, zi, zi_link);
90ca987d46SWarner Losh 	}
917afa0d95SToomas Soome }
92ca987d46SWarner Losh 
93ca987d46SWarner Losh void
efi_zfs_probe(void)94ca987d46SWarner Losh efi_zfs_probe(void)
95ca987d46SWarner Losh {
96ca987d46SWarner Losh 	pdinfo_list_t *hdi;
97ca987d46SWarner Losh 	pdinfo_t *hd, *pd = NULL;
98ca987d46SWarner Losh 	char devname[SPECNAMELEN + 1];
99ca987d46SWarner Losh 	uint64_t guid;
100ca987d46SWarner Losh 
101ca987d46SWarner Losh 	hdi = efiblk_get_pdinfo_list(&efipart_hddev);
102ca987d46SWarner Losh 	STAILQ_INIT(&zfsinfo);
103ca987d46SWarner Losh 
104ca987d46SWarner Losh 	/*
105ca987d46SWarner Losh 	 * Find the handle for the boot device. The boot1 did find the
106ca987d46SWarner Losh 	 * device with loader binary, now we need to search for the
107ca987d46SWarner Losh 	 * same device and if it is part of the zfs pool, we record the
108ca987d46SWarner Losh 	 * pool GUID for currdev setup.
109ca987d46SWarner Losh 	 */
110ca987d46SWarner Losh 	STAILQ_FOREACH(hd, hdi, pd_link) {
111ca987d46SWarner Losh 		STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
112ca987d46SWarner Losh 			snprintf(devname, sizeof(devname), "%s%dp%d:",
113ca987d46SWarner Losh 			    efipart_hddev.dv_name, hd->pd_unit, pd->pd_unit);
114867ae3c3SToomas Soome 			guid = 0;
115*71bbe6fbSWarner Losh 			if (zfs_probe_dev(devname, &guid, false) == 0) {
116ca987d46SWarner Losh 				insert_zfs(pd->pd_handle, guid);
11759a05bdfSWarner Losh 				if (pd->pd_handle == boot_img->DeviceHandle)
118ca987d46SWarner Losh 					pool_guid = guid;
119ca987d46SWarner Losh 			}
120ca987d46SWarner Losh 
121ca987d46SWarner Losh 		}
122ca987d46SWarner Losh 	}
123ca987d46SWarner Losh }
124ca987d46SWarner Losh #endif
125