xref: /freebsd-12.1/sys/dev/efidev/efirt.c (revision e0eb7945)
1 /*-
2  * Copyright (c) 2004 Marcel Moolenaar
3  * Copyright (c) 2001 Doug Rabson
4  * Copyright (c) 2016, 2018 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 /*
61  * The following pointers point to tables in the EFI runtime service data pages.
62  * Care should be taken to make sure that we've properly entered the EFI runtime
63  * environment (efi_enter()) before dereferencing them.
64  */
65 static struct efi_cfgtbl *efi_cfgtbl;
66 static struct efi_rt *efi_runtime;
67 
68 static int efi_status2err[25] = {
69 	0,		/* EFI_SUCCESS */
70 	ENOEXEC,	/* EFI_LOAD_ERROR */
71 	EINVAL,		/* EFI_INVALID_PARAMETER */
72 	ENOSYS,		/* EFI_UNSUPPORTED */
73 	EMSGSIZE, 	/* EFI_BAD_BUFFER_SIZE */
74 	EOVERFLOW,	/* EFI_BUFFER_TOO_SMALL */
75 	EBUSY,		/* EFI_NOT_READY */
76 	EIO,		/* EFI_DEVICE_ERROR */
77 	EROFS,		/* EFI_WRITE_PROTECTED */
78 	EAGAIN,		/* EFI_OUT_OF_RESOURCES */
79 	EIO,		/* EFI_VOLUME_CORRUPTED */
80 	ENOSPC,		/* EFI_VOLUME_FULL */
81 	ENXIO,		/* EFI_NO_MEDIA */
82 	ESTALE,		/* EFI_MEDIA_CHANGED */
83 	ENOENT,		/* EFI_NOT_FOUND */
84 	EACCES,		/* EFI_ACCESS_DENIED */
85 	ETIMEDOUT,	/* EFI_NO_RESPONSE */
86 	EADDRNOTAVAIL,	/* EFI_NO_MAPPING */
87 	ETIMEDOUT,	/* EFI_TIMEOUT */
88 	EDOOFUS,	/* EFI_NOT_STARTED */
89 	EALREADY,	/* EFI_ALREADY_STARTED */
90 	ECANCELED,	/* EFI_ABORTED */
91 	EPROTO,		/* EFI_ICMP_ERROR */
92 	EPROTO,		/* EFI_TFTP_ERROR */
93 	EPROTO		/* EFI_PROTOCOL_ERROR */
94 };
95 
96 static int efi_enter(void);
97 static void efi_leave(void);
98 
99 static int
efi_status_to_errno(efi_status status)100 efi_status_to_errno(efi_status status)
101 {
102 	u_long code;
103 
104 	code = status & 0x3ffffffffffffffful;
105 	return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
106 }
107 
108 static struct mtx efi_lock;
109 
110 static bool
efi_is_in_map(struct efi_md * map,int ndesc,int descsz,vm_offset_t addr)111 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr)
112 {
113 	struct efi_md *p;
114 	int i;
115 
116 	for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
117 	    descsz)) {
118 		if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
119 			continue;
120 
121 		if (addr >= (uintptr_t)p->md_virt &&
122 		    addr < (uintptr_t)p->md_virt + p->md_pages * PAGE_SIZE)
123 			return (true);
124 	}
125 
126 	return (false);
127 }
128 
129 static int
efi_init(void)130 efi_init(void)
131 {
132 	struct efi_map_header *efihdr;
133 	struct efi_md *map;
134 	struct efi_rt *rtdm;
135 	caddr_t kmdp;
136 	size_t efisz;
137 	int ndesc, rt_disabled;
138 
139 	rt_disabled = 0;
140 	TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
141 	if (rt_disabled == 1)
142 		return (0);
143 	mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
144 
145 	if (efi_systbl_phys == 0) {
146 		if (bootverbose)
147 			printf("EFI systbl not available\n");
148 		return (0);
149 	}
150 
151 	efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys);
152 	if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
153 		efi_systbl = NULL;
154 		if (bootverbose)
155 			printf("EFI systbl signature invalid\n");
156 		return (0);
157 	}
158 	efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
159 	    (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
160 	if (efi_cfgtbl == NULL) {
161 		if (bootverbose)
162 			printf("EFI config table is not present\n");
163 	}
164 
165 	kmdp = preload_search_by_type("elf kernel");
166 	if (kmdp == NULL)
167 		kmdp = preload_search_by_type("elf64 kernel");
168 	efihdr = (struct efi_map_header *)preload_search_info(kmdp,
169 	    MODINFO_METADATA | MODINFOMD_EFI_MAP);
170 	if (efihdr == NULL) {
171 		if (bootverbose)
172 			printf("EFI map is not present\n");
173 		return (0);
174 	}
175 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
176 	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
177 	if (efihdr->descriptor_size == 0)
178 		return (ENOMEM);
179 
180 	ndesc = efihdr->memory_size / efihdr->descriptor_size;
181 	if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) {
182 		if (bootverbose)
183 			printf("EFI cannot create runtime map\n");
184 		return (ENOMEM);
185 	}
186 
187 	efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
188 	    (struct efi_rt *)efi_systbl->st_rt;
189 	if (efi_runtime == NULL) {
190 		if (bootverbose)
191 			printf("EFI runtime services table is not present\n");
192 		efi_destroy_1t1_map();
193 		return (ENXIO);
194 	}
195 
196 #if defined(__aarch64__) || defined(__amd64__)
197 	/*
198 	 * Some UEFI implementations have multiple implementations of the
199 	 * RS->GetTime function. They switch from one we can only use early
200 	 * in the boot process to one valid as a RunTime service only when we
201 	 * call RS->SetVirtualAddressMap. As this is not always the case, e.g.
202 	 * with an old loader.efi, check if the RS->GetTime function is within
203 	 * the EFI map, and fail to attach if not.
204 	 */
205 	rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime);
206 	if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size,
207 	    (vm_offset_t)rtdm->rt_gettime)) {
208 		if (bootverbose)
209 			printf(
210 			 "EFI runtime services table has an invalid pointer\n");
211 		efi_runtime = NULL;
212 		efi_destroy_1t1_map();
213 		return (ENXIO);
214 	}
215 #endif
216 
217 	return (0);
218 }
219 
220 static void
efi_uninit(void)221 efi_uninit(void)
222 {
223 
224 	/* Most likely disabled by tunable */
225 	if (efi_runtime == NULL)
226 		return;
227 	efi_destroy_1t1_map();
228 
229 	efi_systbl = NULL;
230 	efi_cfgtbl = NULL;
231 	efi_runtime = NULL;
232 
233 	mtx_destroy(&efi_lock);
234 }
235 
236 int
efi_rt_ok(void)237 efi_rt_ok(void)
238 {
239 
240 	if (efi_runtime == NULL)
241 		return (ENXIO);
242 	return (0);
243 }
244 
245 static int
efi_enter(void)246 efi_enter(void)
247 {
248 	struct thread *td;
249 	pmap_t curpmap;
250 	int error;
251 
252 	if (efi_runtime == NULL)
253 		return (ENXIO);
254 	td = curthread;
255 	curpmap = &td->td_proc->p_vmspace->vm_pmap;
256 	PMAP_LOCK(curpmap);
257 	mtx_lock(&efi_lock);
258 	fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
259 	error = efi_arch_enter();
260 	if (error != 0) {
261 		fpu_kern_leave(td, NULL);
262 		mtx_unlock(&efi_lock);
263 		PMAP_UNLOCK(curpmap);
264 	}
265 	return (error);
266 }
267 
268 static void
efi_leave(void)269 efi_leave(void)
270 {
271 	struct thread *td;
272 	pmap_t curpmap;
273 
274 	efi_arch_leave();
275 
276 	curpmap = &curproc->p_vmspace->vm_pmap;
277 	td = curthread;
278 	fpu_kern_leave(td, NULL);
279 	mtx_unlock(&efi_lock);
280 	PMAP_UNLOCK(curpmap);
281 }
282 
283 int
efi_get_table(struct uuid * uuid,void ** ptr)284 efi_get_table(struct uuid *uuid, void **ptr)
285 {
286 	struct efi_cfgtbl *ct;
287 	u_long count;
288 
289 	if (efi_cfgtbl == NULL || efi_systbl == NULL)
290 		return (ENXIO);
291 	count = efi_systbl->st_entries;
292 	ct = efi_cfgtbl;
293 	while (count--) {
294 		if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
295 			*ptr = (void *)efi_phys_to_kva(ct->ct_data);
296 			return (0);
297 		}
298 		ct++;
299 	}
300 	return (ENOENT);
301 }
302 
303 static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT;
304 SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN,
305     &efi_rt_handle_faults, 0,
306     "Call EFI RT methods with fault handler wrapper around");
307 
308 static int
efi_rt_arch_call_nofault(struct efirt_callinfo * ec)309 efi_rt_arch_call_nofault(struct efirt_callinfo *ec)
310 {
311 
312 	switch (ec->ec_argcnt) {
313 	case 0:
314 		ec->ec_efi_status = ((register_t (*)(void))ec->ec_fptr)();
315 		break;
316 	case 1:
317 		ec->ec_efi_status = ((register_t (*)(register_t))ec->ec_fptr)
318 		    (ec->ec_arg1);
319 		break;
320 	case 2:
321 		ec->ec_efi_status = ((register_t (*)(register_t, register_t))
322 		    ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2);
323 		break;
324 	case 3:
325 		ec->ec_efi_status = ((register_t (*)(register_t, register_t,
326 		    register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2,
327 		    ec->ec_arg3);
328 		break;
329 	case 4:
330 		ec->ec_efi_status = ((register_t (*)(register_t, register_t,
331 		    register_t, register_t))ec->ec_fptr)(ec->ec_arg1,
332 		    ec->ec_arg2, ec->ec_arg3, ec->ec_arg4);
333 		break;
334 	case 5:
335 		ec->ec_efi_status = ((register_t (*)(register_t, register_t,
336 		    register_t, register_t, register_t))ec->ec_fptr)(
337 		    ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4,
338 		    ec->ec_arg5);
339 		break;
340 	default:
341 		panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt);
342 	}
343 
344 	return (0);
345 }
346 
347 static int
efi_call(struct efirt_callinfo * ecp)348 efi_call(struct efirt_callinfo *ecp)
349 {
350 	int error;
351 
352 	error = efi_enter();
353 	if (error != 0)
354 		return (error);
355 	error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) :
356 	    efi_rt_arch_call_nofault(ecp);
357 	efi_leave();
358 	if (error == 0)
359 		error = efi_status_to_errno(ecp->ec_efi_status);
360 	else if (bootverbose)
361 		printf("EFI %s call faulted, error %d\n", ecp->ec_name, error);
362 	return (error);
363 }
364 
365 #define	EFI_RT_METHOD_PA(method)				\
366     ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t)	\
367     efi_runtime))->method)
368 
369 static int
efi_get_time_locked(struct efi_tm * tm,struct efi_tmcap * tmcap)370 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
371 {
372 	struct efirt_callinfo ec;
373 
374 	EFI_TIME_OWNED();
375 	if (efi_runtime == NULL)
376 		return (ENXIO);
377 	bzero(&ec, sizeof(ec));
378 	ec.ec_name = "rt_gettime";
379 	ec.ec_argcnt = 2;
380 	ec.ec_arg1 = (uintptr_t)tm;
381 	ec.ec_arg2 = (uintptr_t)tmcap;
382 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime);
383 	return (efi_call(&ec));
384 }
385 
386 int
efi_get_time(struct efi_tm * tm)387 efi_get_time(struct efi_tm *tm)
388 {
389 	struct efi_tmcap dummy;
390 	int error;
391 
392 	if (efi_runtime == NULL)
393 		return (ENXIO);
394 	EFI_TIME_LOCK();
395 	/*
396 	 * UEFI spec states that the Capabilities argument to GetTime is
397 	 * optional, but some UEFI implementations choke when passed a NULL
398 	 * pointer. Pass a dummy efi_tmcap, even though we won't use it,
399 	 * to workaround such implementations.
400 	 */
401 	error = efi_get_time_locked(tm, &dummy);
402 	EFI_TIME_UNLOCK();
403 	return (error);
404 }
405 
406 int
efi_get_time_capabilities(struct efi_tmcap * tmcap)407 efi_get_time_capabilities(struct efi_tmcap *tmcap)
408 {
409 	struct efi_tm dummy;
410 	int error;
411 
412 	if (efi_runtime == NULL)
413 		return (ENXIO);
414 	EFI_TIME_LOCK();
415 	error = efi_get_time_locked(&dummy, tmcap);
416 	EFI_TIME_UNLOCK();
417 	return (error);
418 }
419 
420 int
efi_reset_system(void)421 efi_reset_system(void)
422 {
423 	struct efirt_callinfo ec;
424 
425 	if (efi_runtime == NULL)
426 		return (ENXIO);
427 	bzero(&ec, sizeof(ec));
428 	ec.ec_name = "rt_reset";
429 	ec.ec_argcnt = 4;
430 	ec.ec_arg1 = (uintptr_t)EFI_RESET_WARM;
431 	ec.ec_arg2 = (uintptr_t)0;
432 	ec.ec_arg3 = (uintptr_t)0;
433 	ec.ec_arg4 = (uintptr_t)NULL;
434 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset);
435 	return (efi_call(&ec));
436 }
437 
438 static int
efi_set_time_locked(struct efi_tm * tm)439 efi_set_time_locked(struct efi_tm *tm)
440 {
441 	struct efirt_callinfo ec;
442 
443 	EFI_TIME_OWNED();
444 	if (efi_runtime == NULL)
445 		return (ENXIO);
446 	bzero(&ec, sizeof(ec));
447 	ec.ec_name = "rt_settime";
448 	ec.ec_argcnt = 1;
449 	ec.ec_arg1 = (uintptr_t)tm;
450 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime);
451 	return (efi_call(&ec));
452 }
453 
454 int
efi_set_time(struct efi_tm * tm)455 efi_set_time(struct efi_tm *tm)
456 {
457 	int error;
458 
459 	if (efi_runtime == NULL)
460 		return (ENXIO);
461 	EFI_TIME_LOCK();
462 	error = efi_set_time_locked(tm);
463 	EFI_TIME_UNLOCK();
464 	return (error);
465 }
466 
467 int
efi_var_get(efi_char * name,struct uuid * vendor,uint32_t * attrib,size_t * datasize,void * data)468 efi_var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
469     size_t *datasize, void *data)
470 {
471 	struct efirt_callinfo ec;
472 
473 	if (efi_runtime == NULL)
474 		return (ENXIO);
475 	bzero(&ec, sizeof(ec));
476 	ec.ec_argcnt = 5;
477 	ec.ec_name = "rt_getvar";
478 	ec.ec_arg1 = (uintptr_t)name;
479 	ec.ec_arg2 = (uintptr_t)vendor;
480 	ec.ec_arg3 = (uintptr_t)attrib;
481 	ec.ec_arg4 = (uintptr_t)datasize;
482 	ec.ec_arg5 = (uintptr_t)data;
483 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar);
484 	return (efi_call(&ec));
485 }
486 
487 int
efi_var_nextname(size_t * namesize,efi_char * name,struct uuid * vendor)488 efi_var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
489 {
490 	struct efirt_callinfo ec;
491 
492 	if (efi_runtime == NULL)
493 		return (ENXIO);
494 	bzero(&ec, sizeof(ec));
495 	ec.ec_argcnt = 3;
496 	ec.ec_name = "rt_scanvar";
497 	ec.ec_arg1 = (uintptr_t)namesize;
498 	ec.ec_arg2 = (uintptr_t)name;
499 	ec.ec_arg3 = (uintptr_t)vendor;
500 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar);
501 	return (efi_call(&ec));
502 }
503 
504 int
efi_var_set(efi_char * name,struct uuid * vendor,uint32_t attrib,size_t datasize,void * data)505 efi_var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
506     size_t datasize, void *data)
507 {
508 	struct efirt_callinfo ec;
509 
510 	if (efi_runtime == NULL)
511 		return (ENXIO);
512 	bzero(&ec, sizeof(ec));
513 	ec.ec_argcnt = 5;
514 	ec.ec_name = "rt_setvar";
515 	ec.ec_arg1 = (uintptr_t)name;
516 	ec.ec_arg2 = (uintptr_t)vendor;
517 	ec.ec_arg3 = (uintptr_t)attrib;
518 	ec.ec_arg4 = (uintptr_t)datasize;
519 	ec.ec_arg5 = (uintptr_t)data;
520 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar);
521 	return (efi_call(&ec));
522 }
523 
524 static int
efirt_modevents(module_t m,int event,void * arg __unused)525 efirt_modevents(module_t m, int event, void *arg __unused)
526 {
527 
528 	switch (event) {
529 	case MOD_LOAD:
530 		return (efi_init());
531 
532 	case MOD_UNLOAD:
533 		efi_uninit();
534 		return (0);
535 
536 	case MOD_SHUTDOWN:
537 		return (0);
538 
539 	default:
540 		return (EOPNOTSUPP);
541 	}
542 }
543 
544 static moduledata_t efirt_moddata = {
545 	.name = "efirt",
546 	.evhand = efirt_modevents,
547 	.priv = NULL,
548 };
549 /* After fpuinitstate, before efidev */
550 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
551 MODULE_VERSION(efirt, 1);
552