xref: /freebsd/sys/dev/xen/efi/pvefi.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 /*-
2  * Copyright (c) 2021 Citrix Systems R&D
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/efi.h>
32 #include <sys/eventhandler.h>
33 #include <sys/kernel.h>
34 #include <sys/linker.h>
35 #include <sys/module.h>
36 #include <sys/clock.h>
37 #include <sys/sysctl.h>
38 #include <sys/systm.h>
39 
40 #include <xen/xen-os.h>
41 #include <xen/error.h>
42 #include <xen/hypervisor.h>
43 
44 #include <contrib/xen/platform.h>
45 
46 extern char bootmethod[16];
47 
48 static int
49 rt_ok(void)
50 {
51 
52 	return (0);
53 }
54 
55 static int
56 get_time(struct efi_tm *tm)
57 {
58 	struct xen_platform_op op = {
59 		.cmd = XENPF_efi_runtime_call,
60 		.u.efi_runtime_call.function = XEN_EFI_get_time,
61 	};
62 	struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call;
63 	int error;
64 
65 	error = HYPERVISOR_platform_op(&op);
66 	if (error != 0)
67 		return (xen_translate_error(error));
68 
69 	tm->tm_year = call->u.get_time.time.year;
70 	tm->tm_mon = call->u.get_time.time.month;
71 	tm->tm_mday = call->u.get_time.time.day;
72 	tm->tm_hour = call->u.get_time.time.hour;
73 	tm->tm_min = call->u.get_time.time.min;
74 	tm->tm_sec = call->u.get_time.time.sec;
75 	tm->tm_nsec = call->u.get_time.time.ns;
76 	tm->tm_tz = call->u.get_time.time.tz;
77 	tm->tm_dst = call->u.get_time.time.daylight;
78 
79 	return (efi_status_to_errno(call->status));
80 }
81 
82 static int
83 get_time_capabilities(struct efi_tmcap *tmcap)
84 {
85 	struct xen_platform_op op = {
86 		.cmd = XENPF_efi_runtime_call,
87 		.u.efi_runtime_call.function = XEN_EFI_get_time,
88 	};
89 	struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call;
90 	int error;
91 
92 	error = HYPERVISOR_platform_op(&op);
93 	if (error != 0)
94 		return (xen_translate_error(error));
95 
96 	tmcap->tc_res = call->u.get_time.resolution;
97 	tmcap->tc_prec = call->u.get_time.accuracy;
98 	tmcap->tc_stz = call->misc & XEN_EFI_GET_TIME_SET_CLEARS_NS;
99 
100 	return (efi_status_to_errno(call->status));
101 }
102 
103 static int
104 set_time(struct efi_tm *tm)
105 {
106 	struct xen_platform_op op = {
107 		.cmd = XENPF_efi_runtime_call,
108 		.u.efi_runtime_call.function = XEN_EFI_get_time,
109 		.u.efi_runtime_call.u.set_time.year = tm->tm_year,
110 		.u.efi_runtime_call.u.set_time.month = tm->tm_mon,
111 		.u.efi_runtime_call.u.set_time.day = tm->tm_mday,
112 		.u.efi_runtime_call.u.set_time.hour = tm->tm_hour,
113 		.u.efi_runtime_call.u.set_time.min = tm->tm_min,
114 		.u.efi_runtime_call.u.set_time.sec = tm->tm_sec,
115 		.u.efi_runtime_call.u.set_time.ns = tm->tm_nsec,
116 		.u.efi_runtime_call.u.set_time.tz = tm->tm_tz,
117 		.u.efi_runtime_call.u.set_time.daylight = tm->tm_dst,
118 	};
119 	int error;
120 
121 	error = HYPERVISOR_platform_op(&op);
122 
123 	return ((error != 0) ? xen_translate_error(error) :
124 	    efi_status_to_errno(op.u.efi_runtime_call.status));
125 }
126 
127 static int
128 var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
129     size_t *datasize, void *data)
130 {
131 	struct xen_platform_op op = {
132 		.cmd = XENPF_efi_runtime_call,
133 		.u.efi_runtime_call.function = XEN_EFI_get_variable,
134 		.u.efi_runtime_call.u.get_variable.size = *datasize,
135 	};
136 	struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call;
137 	int error;
138 
139 	CTASSERT(sizeof(*vendor) == sizeof(call->u.get_variable.vendor_guid));
140 
141 	memcpy(&call->u.get_variable.vendor_guid, vendor,
142 	    sizeof(*vendor));
143 	set_xen_guest_handle(call->u.get_variable.name, name);
144 	set_xen_guest_handle(call->u.get_variable.data, data);
145 
146 	error = HYPERVISOR_platform_op(&op);
147 	if (error != 0)
148 		return (xen_translate_error(error));
149 
150 	*attrib = call->misc;
151 	*datasize = call->u.get_variable.size;
152 
153 	return (efi_status_to_errno(call->status));
154 }
155 
156 static int
157 var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
158 {
159 	struct xen_platform_op op = {
160 		.cmd = XENPF_efi_runtime_call,
161 		.u.efi_runtime_call.function = XEN_EFI_get_next_variable_name,
162 		.u.efi_runtime_call.u.get_next_variable_name.size = *namesize,
163 	};
164 	struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call;
165 	int error;
166 
167 	memcpy(&call->u.get_next_variable_name.vendor_guid, vendor,
168 	    sizeof(*vendor));
169 	set_xen_guest_handle(call->u.get_next_variable_name.name, name);
170 
171 	error = HYPERVISOR_platform_op(&op);
172 	if (error != 0)
173 		return (xen_translate_error(error));
174 
175 	*namesize = call->u.get_next_variable_name.size;
176 	memcpy(vendor, &call->u.get_next_variable_name.vendor_guid,
177 	    sizeof(*vendor));
178 
179 	return (efi_status_to_errno(call->status));
180 }
181 
182 static int
183 var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
184     size_t datasize, void *data)
185 {
186 	struct xen_platform_op op = {
187 		.cmd = XENPF_efi_runtime_call,
188 		.u.efi_runtime_call.function = XEN_EFI_set_variable,
189 		.u.efi_runtime_call.misc = attrib,
190 		.u.efi_runtime_call.u.set_variable.size = datasize,
191 	};
192 	struct xenpf_efi_runtime_call *call = &op.u.efi_runtime_call;
193 	int error;
194 
195 	memcpy(&call->u.set_variable.vendor_guid, vendor,
196 	    sizeof(*vendor));
197 	set_xen_guest_handle(call->u.set_variable.name, name);
198 	set_xen_guest_handle(call->u.set_variable.data, data);
199 
200 	error = HYPERVISOR_platform_op(&op);
201 
202 	return ((error != 0) ? xen_translate_error(error) :
203 	    efi_status_to_errno(call->status));
204 }
205 
206 const static struct efi_ops pvefi_ops = {
207 	.rt_ok = rt_ok,
208 	.get_time = get_time,
209 	.get_time_capabilities = get_time_capabilities,
210 	.set_time = set_time,
211 	.var_get = var_get,
212 	.var_nextname = var_nextname,
213 	.var_set = var_set,
214 };
215 
216 static int
217 modevents(module_t m, int event, void *arg __unused)
218 {
219 	const static struct efi_ops *prev;
220 	int rt_disabled;
221 
222 	switch (event) {
223 	case MOD_LOAD:
224 		rt_disabled = 0;
225 		TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
226 
227 		if (!xen_initial_domain() || strcmp("UEFI", bootmethod) != 0 ||
228 		    rt_disabled == 1)
229 			return (0);
230 
231 		prev = active_efi_ops;
232 		active_efi_ops = &pvefi_ops;
233 		return (0);
234 
235 	case MOD_UNLOAD:
236 		if (prev != NULL)
237 		    active_efi_ops = prev;
238 		return (0);
239 
240 	case MOD_SHUTDOWN:
241 		return (0);
242 
243 	default:
244 		return (EOPNOTSUPP);
245 	}
246 }
247 
248 static moduledata_t moddata = {
249 	.name = "pvefirt",
250 	.evhand = modevents,
251 	.priv = NULL,
252 };
253 /* After fpuinitstate, before efidev */
254 DECLARE_MODULE(pvefirt, moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
255 MODULE_VERSION(pvefirt, 1);
256