xref: /freebsd/stand/efi/libefi/efizfs.c (revision 867ae3c38d1aa97300bd35f457037b3f1a0a103f)
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/cdefs.h>
29ca987d46SWarner Losh __FBSDID("$FreeBSD$");
30ca987d46SWarner Losh 
31ca987d46SWarner Losh #include <sys/param.h>
32de04d704SWarner Losh #include <stand.h>
33ca987d46SWarner Losh 
34ca987d46SWarner Losh #ifdef EFI_ZFS_BOOT
35ca987d46SWarner Losh #include <libzfs.h>
36ca987d46SWarner Losh #endif
37ca987d46SWarner Losh 
38ca987d46SWarner Losh #include <efi.h>
39ca987d46SWarner Losh #include <efilib.h>
40ca987d46SWarner Losh 
41ca987d46SWarner Losh #include "efizfs.h"
42ca987d46SWarner Losh 
43ca987d46SWarner Losh #ifdef EFI_ZFS_BOOT
44ca987d46SWarner Losh static zfsinfo_list_t zfsinfo;
45ca987d46SWarner Losh 
46ca987d46SWarner Losh uint64_t pool_guid;
47ca987d46SWarner Losh 
48ca987d46SWarner Losh zfsinfo_list_t *
49ca987d46SWarner Losh efizfs_get_zfsinfo_list(void)
50ca987d46SWarner Losh {
51ca987d46SWarner Losh 	return (&zfsinfo);
52ca987d46SWarner Losh }
53ca987d46SWarner Losh 
54ca987d46SWarner Losh EFI_HANDLE
55ca987d46SWarner Losh efizfs_get_handle_by_guid(uint64_t guid)
56ca987d46SWarner Losh {
57ca987d46SWarner Losh 	zfsinfo_t *zi;
58ca987d46SWarner Losh 
59ca987d46SWarner Losh 	STAILQ_FOREACH(zi, &zfsinfo, zi_link) {
60ca987d46SWarner Losh 		if (zi->zi_pool_guid == guid) {
61ca987d46SWarner Losh 			return (zi->zi_handle);
62ca987d46SWarner Losh 		}
63ca987d46SWarner Losh 	}
64ca987d46SWarner Losh 	return (NULL);
65ca987d46SWarner Losh }
66ca987d46SWarner Losh 
67fa9dc8d3SWarner Losh bool
68fa9dc8d3SWarner Losh efizfs_get_guid_by_handle(EFI_HANDLE handle, uint64_t *guid)
69fa9dc8d3SWarner Losh {
70fa9dc8d3SWarner Losh 	zfsinfo_t *zi;
71fa9dc8d3SWarner Losh 
72fa9dc8d3SWarner Losh 	if (guid == NULL)
73fa9dc8d3SWarner Losh 		return (false);
74fa9dc8d3SWarner Losh 	STAILQ_FOREACH(zi, &zfsinfo, zi_link) {
75fa9dc8d3SWarner Losh 		if (zi->zi_handle == handle) {
76fa9dc8d3SWarner Losh 			*guid = zi->zi_pool_guid;
77fa9dc8d3SWarner Losh 			return (true);
78fa9dc8d3SWarner Losh 		}
79fa9dc8d3SWarner Losh 	}
80fa9dc8d3SWarner Losh 	return (false);
81fa9dc8d3SWarner Losh }
82fa9dc8d3SWarner Losh 
83ca987d46SWarner Losh static void
84ca987d46SWarner Losh insert_zfs(EFI_HANDLE handle, uint64_t guid)
85ca987d46SWarner Losh {
86ca987d46SWarner Losh         zfsinfo_t *zi;
87ca987d46SWarner Losh 
88ca987d46SWarner Losh         zi = malloc(sizeof(zfsinfo_t));
897afa0d95SToomas Soome 	if (zi != NULL) {
90ca987d46SWarner Losh         	zi->zi_handle = handle;
91ca987d46SWarner Losh         	zi->zi_pool_guid = guid;
92ca987d46SWarner Losh         	STAILQ_INSERT_TAIL(&zfsinfo, zi, zi_link);
93ca987d46SWarner Losh 	}
947afa0d95SToomas Soome }
95ca987d46SWarner Losh 
96ca987d46SWarner Losh void
97ca987d46SWarner Losh efi_zfs_probe(void)
98ca987d46SWarner Losh {
99ca987d46SWarner Losh 	pdinfo_list_t *hdi;
100ca987d46SWarner Losh 	pdinfo_t *hd, *pd = NULL;
101ca987d46SWarner Losh 	char devname[SPECNAMELEN + 1];
102ca987d46SWarner Losh 	uint64_t guid;
103ca987d46SWarner Losh 
104ca987d46SWarner Losh 	hdi = efiblk_get_pdinfo_list(&efipart_hddev);
105ca987d46SWarner Losh 	STAILQ_INIT(&zfsinfo);
106ca987d46SWarner Losh 
107ca987d46SWarner Losh 	/*
108ca987d46SWarner Losh 	 * Find the handle for the boot device. The boot1 did find the
109ca987d46SWarner Losh 	 * device with loader binary, now we need to search for the
110ca987d46SWarner Losh 	 * same device and if it is part of the zfs pool, we record the
111ca987d46SWarner Losh 	 * pool GUID for currdev setup.
112ca987d46SWarner Losh 	 */
113ca987d46SWarner Losh 	STAILQ_FOREACH(hd, hdi, pd_link) {
114ca987d46SWarner Losh 		STAILQ_FOREACH(pd, &hd->pd_part, pd_link) {
115ca987d46SWarner Losh 			snprintf(devname, sizeof(devname), "%s%dp%d:",
116ca987d46SWarner Losh 			    efipart_hddev.dv_name, hd->pd_unit, pd->pd_unit);
117*867ae3c3SToomas Soome 			guid = 0;
118ca987d46SWarner Losh 			if (zfs_probe_dev(devname, &guid) == 0) {
119ca987d46SWarner Losh 				insert_zfs(pd->pd_handle, guid);
12059a05bdfSWarner Losh 				if (pd->pd_handle == boot_img->DeviceHandle)
121ca987d46SWarner Losh 					pool_guid = guid;
122ca987d46SWarner Losh 			}
123ca987d46SWarner Losh 
124ca987d46SWarner Losh 		}
125ca987d46SWarner Losh 	}
126ca987d46SWarner Losh }
127ca987d46SWarner Losh #endif
128