xref: /freebsd/sys/dev/efidev/efirt.c (revision 74fe6c29fb7eef3418d7919dcd41dc1a04a982a1)
1 /*-
2  * Copyright (c) 2004 Marcel Moolenaar
3  * Copyright (c) 2001 Doug Rabson
4  * Copyright (c) 2016 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Portions of this software were developed by Konstantin Belousov
8  * under sponsorship from the FreeBSD Foundation.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/efi.h>
37 #include <sys/kernel.h>
38 #include <sys/linker.h>
39 #include <sys/lock.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/clock.h>
43 #include <sys/proc.h>
44 #include <sys/rwlock.h>
45 #include <sys/sched.h>
46 #include <sys/sysctl.h>
47 #include <sys/systm.h>
48 #include <sys/vmmeter.h>
49 
50 #include <machine/fpu.h>
51 #include <machine/efi.h>
52 #include <machine/metadata.h>
53 #include <machine/vmparam.h>
54 
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_map.h>
58 
59 static struct efi_systbl *efi_systbl;
60 static struct efi_cfgtbl *efi_cfgtbl;
61 static struct efi_rt *efi_runtime;
62 
63 static int efi_status2err[25] = {
64 	0,		/* EFI_SUCCESS */
65 	ENOEXEC,	/* EFI_LOAD_ERROR */
66 	EINVAL,		/* EFI_INVALID_PARAMETER */
67 	ENOSYS,		/* EFI_UNSUPPORTED */
68 	EMSGSIZE, 	/* EFI_BAD_BUFFER_SIZE */
69 	EOVERFLOW,	/* EFI_BUFFER_TOO_SMALL */
70 	EBUSY,		/* EFI_NOT_READY */
71 	EIO,		/* EFI_DEVICE_ERROR */
72 	EROFS,		/* EFI_WRITE_PROTECTED */
73 	EAGAIN,		/* EFI_OUT_OF_RESOURCES */
74 	EIO,		/* EFI_VOLUME_CORRUPTED */
75 	ENOSPC,		/* EFI_VOLUME_FULL */
76 	ENXIO,		/* EFI_NO_MEDIA */
77 	ESTALE,		/* EFI_MEDIA_CHANGED */
78 	ENOENT,		/* EFI_NOT_FOUND */
79 	EACCES,		/* EFI_ACCESS_DENIED */
80 	ETIMEDOUT,	/* EFI_NO_RESPONSE */
81 	EADDRNOTAVAIL,	/* EFI_NO_MAPPING */
82 	ETIMEDOUT,	/* EFI_TIMEOUT */
83 	EDOOFUS,	/* EFI_NOT_STARTED */
84 	EALREADY,	/* EFI_ALREADY_STARTED */
85 	ECANCELED,	/* EFI_ABORTED */
86 	EPROTO,		/* EFI_ICMP_ERROR */
87 	EPROTO,		/* EFI_TFTP_ERROR */
88 	EPROTO		/* EFI_PROTOCOL_ERROR */
89 };
90 
91 static int
92 efi_status_to_errno(efi_status status)
93 {
94 	u_long code;
95 
96 	code = status & 0x3ffffffffffffffful;
97 	return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
98 }
99 
100 static struct mtx efi_lock;
101 
102 static int
103 efi_init(void)
104 {
105 	struct efi_map_header *efihdr;
106 	struct efi_md *map;
107 	caddr_t kmdp;
108 	size_t efisz;
109 
110 	mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
111 
112 	if (efi_systbl_phys == 0) {
113 		if (bootverbose)
114 			printf("EFI systbl not available\n");
115 		return (0);
116 	}
117 	if (!PMAP_HAS_DMAP) {
118 		if (bootverbose)
119 			printf("EFI systbl requires direct map\n");
120 		return (0);
121 	}
122 	efi_systbl = (struct efi_systbl *)PHYS_TO_DMAP(efi_systbl_phys);
123 	if (efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
124 		efi_systbl = NULL;
125 		if (bootverbose)
126 			printf("EFI systbl signature invalid\n");
127 		return (0);
128 	}
129 	efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
130 	    (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
131 	if (efi_cfgtbl == NULL) {
132 		if (bootverbose)
133 			printf("EFI config table is not present\n");
134 	}
135 
136 	kmdp = preload_search_by_type("elf kernel");
137 	if (kmdp == NULL)
138 		kmdp = preload_search_by_type("elf64 kernel");
139 	efihdr = (struct efi_map_header *)preload_search_info(kmdp,
140 	    MODINFO_METADATA | MODINFOMD_EFI_MAP);
141 	if (efihdr == NULL) {
142 		if (bootverbose)
143 			printf("EFI map is not present\n");
144 		return (0);
145 	}
146 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
147 	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
148 	if (efihdr->descriptor_size == 0)
149 		return (ENOMEM);
150 
151 	if (!efi_create_1t1_map(map, efihdr->memory_size /
152 	    efihdr->descriptor_size, efihdr->descriptor_size)) {
153 		if (bootverbose)
154 			printf("EFI cannot create runtime map\n");
155 		return (ENOMEM);
156 	}
157 
158 	efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
159 	    (struct efi_rt *)efi_systbl->st_rt;
160 	if (efi_runtime == NULL) {
161 		if (bootverbose)
162 			printf("EFI runtime services table is not present\n");
163 		efi_destroy_1t1_map();
164 		return (ENXIO);
165 	}
166 
167 	return (0);
168 }
169 
170 static void
171 efi_uninit(void)
172 {
173 
174 	efi_destroy_1t1_map();
175 
176 	efi_systbl = NULL;
177 	efi_cfgtbl = NULL;
178 	efi_runtime = NULL;
179 
180 	mtx_destroy(&efi_lock);
181 }
182 
183 int
184 efi_rt_ok(void)
185 {
186 
187 	if (efi_runtime == NULL)
188 		return (ENXIO);
189 	return (0);
190 }
191 
192 static int
193 efi_enter(void)
194 {
195 	struct thread *td;
196 	pmap_t curpmap;
197 
198 	if (efi_runtime == NULL)
199 		return (ENXIO);
200 	td = curthread;
201 	curpmap = &td->td_proc->p_vmspace->vm_pmap;
202 	PMAP_LOCK(curpmap);
203 	mtx_lock(&efi_lock);
204 	fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
205 	return (efi_arch_enter());
206 }
207 
208 static void
209 efi_leave(void)
210 {
211 	struct thread *td;
212 	pmap_t curpmap;
213 
214 	efi_arch_leave();
215 
216 	curpmap = &curproc->p_vmspace->vm_pmap;
217 	td = curthread;
218 	fpu_kern_leave(td, NULL);
219 	mtx_unlock(&efi_lock);
220 	PMAP_UNLOCK(curpmap);
221 }
222 
223 int
224 efi_get_table(struct uuid *uuid, void **ptr)
225 {
226 	struct efi_cfgtbl *ct;
227 	u_long count;
228 
229 	if (efi_cfgtbl == NULL || efi_systbl == NULL)
230 		return (ENXIO);
231 	count = efi_systbl->st_entries;
232 	ct = efi_cfgtbl;
233 	while (count--) {
234 		if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
235 			*ptr = (void *)PHYS_TO_DMAP(ct->ct_data);
236 			return (0);
237 		}
238 		ct++;
239 	}
240 	return (ENOENT);
241 }
242 
243 static int
244 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
245 {
246 	efi_status status;
247 	int error;
248 
249 	EFI_TIME_OWNED()
250 	error = efi_enter();
251 	if (error != 0)
252 		return (error);
253 	status = efi_runtime->rt_gettime(tm, tmcap);
254 	efi_leave();
255 	error = efi_status_to_errno(status);
256 	return (error);
257 }
258 
259 int
260 efi_get_time(struct efi_tm *tm)
261 {
262 	struct efi_tmcap dummy;
263 	int error;
264 
265 	if (efi_runtime == NULL)
266 		return (ENXIO);
267 	EFI_TIME_LOCK()
268 	/*
269 	 * UEFI spec states that the Capabilities argument to GetTime is
270 	 * optional, but some UEFI implementations choke when passed a NULL
271 	 * pointer. Pass a dummy efi_tmcap, even though we won't use it,
272 	 * to workaround such implementations.
273 	 */
274 	error = efi_get_time_locked(tm, &dummy);
275 	EFI_TIME_UNLOCK()
276 	return (error);
277 }
278 
279 int
280 efi_get_time_capabilities(struct efi_tmcap *tmcap)
281 {
282 	struct efi_tm dummy;
283 	int error;
284 
285 	if (efi_runtime == NULL)
286 		return (ENXIO);
287 	EFI_TIME_LOCK()
288 	error = efi_get_time_locked(&dummy, tmcap);
289 	EFI_TIME_UNLOCK()
290 	return (error);
291 }
292 
293 int
294 efi_reset_system(void)
295 {
296 	int error;
297 
298 	error = efi_enter();
299 	if (error != 0)
300 		return (error);
301 	efi_runtime->rt_reset(EFI_RESET_WARM, 0, 0, NULL);
302 	efi_leave();
303 	return (EIO);
304 }
305 
306 static int
307 efi_set_time_locked(struct efi_tm *tm)
308 {
309 	efi_status status;
310 	int error;
311 
312 	EFI_TIME_OWNED();
313 	error = efi_enter();
314 	if (error != 0)
315 		return (error);
316 	status = efi_runtime->rt_settime(tm);
317 	efi_leave();
318 	error = efi_status_to_errno(status);
319 	return (error);
320 }
321 
322 int
323 efi_set_time(struct efi_tm *tm)
324 {
325 	int error;
326 
327 	if (efi_runtime == NULL)
328 		return (ENXIO);
329 	EFI_TIME_LOCK()
330 	error = efi_set_time_locked(tm);
331 	EFI_TIME_UNLOCK()
332 	return (error);
333 }
334 
335 int
336 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
337     size_t *datasize, void *data)
338 {
339 	efi_status status;
340 	int error;
341 
342 	error = efi_enter();
343 	if (error != 0)
344 		return (error);
345 	status = efi_runtime->rt_getvar(name, vendor, attrib, datasize, data);
346 	efi_leave();
347 	error = efi_status_to_errno(status);
348 	return (error);
349 }
350 
351 int
352 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
353 {
354 	efi_status status;
355 	int error;
356 
357 	error = efi_enter();
358 	if (error != 0)
359 		return (error);
360 	status = efi_runtime->rt_scanvar(namesize, name, vendor);
361 	efi_leave();
362 	error = efi_status_to_errno(status);
363 	return (error);
364 }
365 
366 int
367 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
368     size_t datasize, void *data)
369 {
370 	efi_status status;
371 	int error;
372 
373 	error = efi_enter();
374 	if (error != 0)
375 		return (error);
376 	status = efi_runtime->rt_setvar(name, vendor, attrib, datasize, data);
377 	efi_leave();
378 	error = efi_status_to_errno(status);
379 	return (error);
380 }
381 
382 static int
383 efirt_modevents(module_t m, int event, void *arg __unused)
384 {
385 
386 	switch (event) {
387 	case MOD_LOAD:
388 		return (efi_init());
389 
390 	case MOD_UNLOAD:
391 		efi_uninit();
392 		return (0);
393 
394 	case MOD_SHUTDOWN:
395 		return (0);
396 
397 	default:
398 		return (EOPNOTSUPP);
399 	}
400 }
401 
402 static moduledata_t efirt_moddata = {
403 	.name = "efirt",
404 	.evhand = efirt_modevents,
405 	.priv = NULL,
406 };
407 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_VM_CONF, SI_ORDER_ANY);
408 MODULE_VERSION(efirt, 1);
409