xref: /freebsd/sys/dev/efidev/efirt.c (revision f840492b5b0d5c9e7d6d7d7dc25c260bc4d63ba2)
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 #include "opt_acpi.h"
34 
35 #include <sys/param.h>
36 #include <sys/efi.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/linker.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/msan.h>
44 #include <sys/mutex.h>
45 #include <sys/clock.h>
46 #include <sys/proc.h>
47 #include <sys/reboot.h>
48 #include <sys/rwlock.h>
49 #include <sys/sched.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52 #include <sys/uio.h>
53 #include <sys/vmmeter.h>
54 
55 #include <machine/fpu.h>
56 #include <machine/efi.h>
57 #include <machine/metadata.h>
58 #include <machine/vmparam.h>
59 
60 #include <vm/vm.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_map.h>
63 
64 #ifdef DEV_ACPI
65 #include <contrib/dev/acpica/include/acpi.h>
66 #endif
67 
68 #define EFI_TABLE_ALLOC_MAX 0x800000
69 
70 static struct efi_systbl *efi_systbl;
71 static eventhandler_tag efi_shutdown_tag;
72 /*
73  * The following pointers point to tables in the EFI runtime service data pages.
74  * Care should be taken to make sure that we've properly entered the EFI runtime
75  * environment (efi_enter()) before dereferencing them.
76  */
77 static struct efi_cfgtbl *efi_cfgtbl;
78 static struct efi_rt *efi_runtime;
79 
80 static int efi_status2err[25] = {
81 	0,		/* EFI_SUCCESS */
82 	ENOEXEC,	/* EFI_LOAD_ERROR */
83 	EINVAL,		/* EFI_INVALID_PARAMETER */
84 	ENOSYS,		/* EFI_UNSUPPORTED */
85 	EMSGSIZE, 	/* EFI_BAD_BUFFER_SIZE */
86 	EOVERFLOW,	/* EFI_BUFFER_TOO_SMALL */
87 	EBUSY,		/* EFI_NOT_READY */
88 	EIO,		/* EFI_DEVICE_ERROR */
89 	EROFS,		/* EFI_WRITE_PROTECTED */
90 	EAGAIN,		/* EFI_OUT_OF_RESOURCES */
91 	EIO,		/* EFI_VOLUME_CORRUPTED */
92 	ENOSPC,		/* EFI_VOLUME_FULL */
93 	ENXIO,		/* EFI_NO_MEDIA */
94 	ESTALE,		/* EFI_MEDIA_CHANGED */
95 	ENOENT,		/* EFI_NOT_FOUND */
96 	EACCES,		/* EFI_ACCESS_DENIED */
97 	ETIMEDOUT,	/* EFI_NO_RESPONSE */
98 	EADDRNOTAVAIL,	/* EFI_NO_MAPPING */
99 	ETIMEDOUT,	/* EFI_TIMEOUT */
100 	EDOOFUS,	/* EFI_NOT_STARTED */
101 	EALREADY,	/* EFI_ALREADY_STARTED */
102 	ECANCELED,	/* EFI_ABORTED */
103 	EPROTO,		/* EFI_ICMP_ERROR */
104 	EPROTO,		/* EFI_TFTP_ERROR */
105 	EPROTO		/* EFI_PROTOCOL_ERROR */
106 };
107 
108 enum efi_table_type {
109 	TYPE_ESRT = 0,
110 	TYPE_PROP,
111 	TYPE_MEMORY_ATTR
112 };
113 
114 static int efi_enter(void);
115 static void efi_leave(void);
116 
117 int
efi_status_to_errno(efi_status status)118 efi_status_to_errno(efi_status status)
119 {
120 	u_long code;
121 
122 	code = status & 0x3ffffffffffffffful;
123 	return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
124 }
125 
126 static struct mtx efi_lock;
127 SYSCTL_NODE(_hw, OID_AUTO, efi, CTLFLAG_RWTUN | CTLFLAG_MPSAFE, NULL,
128     "EFI");
129 static bool efi_poweroff = true;
130 SYSCTL_BOOL(_hw_efi, OID_AUTO, poweroff, CTLFLAG_RWTUN, &efi_poweroff, 0,
131     "If true, use EFI runtime services to power off in preference to ACPI");
132 extern int print_efirt_faults;
133 SYSCTL_INT(_hw_efi, OID_AUTO, print_faults, CTLFLAG_RWTUN,
134     &print_efirt_faults, 0,
135     "Print fault  information upon trap from EFIRT calls: "
136     "0 - never, 1 - once, 2 - always");
137 extern u_long cnt_efirt_faults;
138 SYSCTL_ULONG(_hw_efi, OID_AUTO, total_faults, CTLFLAG_RD,
139     &cnt_efirt_faults, 0,
140     "Total number of faults that occurred during EFIRT calls");
141 
142 static bool
efi_is_in_map(struct efi_md * map,int ndesc,int descsz,vm_offset_t addr)143 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr)
144 {
145 	struct efi_md *p;
146 	int i;
147 
148 	for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
149 	    descsz)) {
150 		if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
151 			continue;
152 
153 		if (addr >= p->md_virt &&
154 		    addr < p->md_virt + p->md_pages * EFI_PAGE_SIZE)
155 			return (true);
156 	}
157 
158 	return (false);
159 }
160 
161 static void
efi_shutdown_final(void * dummy __unused,int howto)162 efi_shutdown_final(void *dummy __unused, int howto)
163 {
164 
165 	/*
166 	 * On some systems, ACPI S5 is missing or does not function properly.
167 	 * When present, shutdown via EFI Runtime Services instead, unless
168 	 * disabled.
169 	 */
170 	if ((howto & RB_POWEROFF) != 0 && efi_poweroff)
171 		(void)efi_reset_system(EFI_RESET_SHUTDOWN);
172 }
173 
174 static int
efi_init(void)175 efi_init(void)
176 {
177 	struct efi_map_header *efihdr;
178 	struct efi_md *map;
179 	struct efi_rt *rtdm;
180 	size_t efisz;
181 	int ndesc, rt_disabled;
182 
183 	rt_disabled = 0;
184 	TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
185 	if (rt_disabled == 1)
186 		return (0);
187 	mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
188 
189 	if (efi_systbl_phys == 0) {
190 		if (bootverbose)
191 			printf("EFI systbl not available\n");
192 		return (0);
193 	}
194 
195 	efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys);
196 	if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
197 		efi_systbl = NULL;
198 		if (bootverbose)
199 			printf("EFI systbl signature invalid\n");
200 		return (0);
201 	}
202 	efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
203 	    (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
204 	if (efi_cfgtbl == NULL) {
205 		if (bootverbose)
206 			printf("EFI config table is not present\n");
207 	}
208 
209 	efihdr = (struct efi_map_header *)preload_search_info(preload_kmdp,
210 	    MODINFO_METADATA | MODINFOMD_EFI_MAP);
211 	if (efihdr == NULL) {
212 		if (bootverbose)
213 			printf("EFI map is not present\n");
214 		return (0);
215 	}
216 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
217 	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
218 	if (efihdr->descriptor_size == 0)
219 		return (ENOMEM);
220 
221 	ndesc = efihdr->memory_size / efihdr->descriptor_size;
222 	if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) {
223 		if (bootverbose)
224 			printf("EFI cannot create runtime map\n");
225 		return (ENOMEM);
226 	}
227 
228 	efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
229 	    (struct efi_rt *)efi_systbl->st_rt;
230 	if (efi_runtime == NULL) {
231 		if (bootverbose)
232 			printf("EFI runtime services table is not present\n");
233 		efi_destroy_1t1_map();
234 		return (ENXIO);
235 	}
236 
237 #if defined(__aarch64__) || defined(__amd64__)
238 	/*
239 	 * Some UEFI implementations have multiple implementations of the
240 	 * RS->GetTime function. They switch from one we can only use early
241 	 * in the boot process to one valid as a RunTime service only when we
242 	 * call RS->SetVirtualAddressMap. As this is not always the case, e.g.
243 	 * with an old loader.efi, check if the RS->GetTime function is within
244 	 * the EFI map, and fail to attach if not.
245 	 */
246 	rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime);
247 	if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size,
248 	    (vm_offset_t)rtdm->rt_gettime)) {
249 		if (bootverbose)
250 			printf(
251 			 "EFI runtime services table has an invalid pointer\n");
252 		efi_runtime = NULL;
253 		efi_destroy_1t1_map();
254 		return (ENXIO);
255 	}
256 #endif
257 
258 	/*
259 	 * We use SHUTDOWN_PRI_LAST - 1 to trigger after IPMI, but before ACPI.
260 	 */
261 	efi_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final,
262 	    efi_shutdown_final, NULL, SHUTDOWN_PRI_LAST - 1);
263 
264 	return (0);
265 }
266 
267 static void
efi_uninit(void)268 efi_uninit(void)
269 {
270 
271 	/* Most likely disabled by tunable */
272 	if (efi_runtime == NULL)
273 		return;
274 	if (efi_shutdown_tag != NULL)
275 		EVENTHANDLER_DEREGISTER(shutdown_final, efi_shutdown_tag);
276 	efi_destroy_1t1_map();
277 
278 	efi_systbl = NULL;
279 	efi_cfgtbl = NULL;
280 	efi_runtime = NULL;
281 
282 	mtx_destroy(&efi_lock);
283 }
284 
285 static int
rt_ok(void)286 rt_ok(void)
287 {
288 
289 	if (efi_runtime == NULL)
290 		return (ENXIO);
291 	return (0);
292 }
293 
294 /*
295  * The fpu_kern_enter() call in allows firmware to use FPU, as
296  * mandated by the specification.  It also enters a critical section,
297  * giving us neccessary protection against context switches.
298  */
299 static int
efi_enter(void)300 efi_enter(void)
301 {
302 	struct thread *td;
303 	pmap_t curpmap;
304 	int error;
305 
306 	if (efi_runtime == NULL)
307 		return (ENXIO);
308 	td = curthread;
309 	curpmap = &td->td_proc->p_vmspace->vm_pmap;
310 	PMAP_LOCK(curpmap);
311 	mtx_lock(&efi_lock);
312 	fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
313 	error = efi_arch_enter();
314 	if (error != 0) {
315 		fpu_kern_leave(td, NULL);
316 		mtx_unlock(&efi_lock);
317 		PMAP_UNLOCK(curpmap);
318 	} else {
319 		MPASS((td->td_pflags & TDP_EFIRT) == 0);
320 		td->td_pflags |= TDP_EFIRT;
321 	}
322 	return (error);
323 }
324 
325 static void
efi_leave(void)326 efi_leave(void)
327 {
328 	struct thread *td;
329 	pmap_t curpmap;
330 
331 	td = curthread;
332 	MPASS((td->td_pflags & TDP_EFIRT) != 0);
333 	td->td_pflags &= ~TDP_EFIRT;
334 
335 	efi_arch_leave();
336 
337 	curpmap = &curproc->p_vmspace->vm_pmap;
338 	fpu_kern_leave(td, NULL);
339 	mtx_unlock(&efi_lock);
340 	PMAP_UNLOCK(curpmap);
341 }
342 
343 static int
get_table(efi_guid_t * guid,void ** ptr)344 get_table(efi_guid_t *guid, void **ptr)
345 {
346 	struct efi_cfgtbl *ct;
347 	u_long count;
348 	int error;
349 
350 	if (efi_cfgtbl == NULL || efi_systbl == NULL)
351 		return (ENXIO);
352 	error = efi_enter();
353 	if (error != 0)
354 		return (error);
355 	count = efi_systbl->st_entries;
356 	ct = efi_cfgtbl;
357 	while (count--) {
358 		if (!bcmp(&ct->ct_guid, guid, sizeof(*guid))) {
359 			*ptr = ct->ct_data;
360 			efi_leave();
361 			return (0);
362 		}
363 		ct++;
364 	}
365 
366 	efi_leave();
367 	return (ENOENT);
368 }
369 
370 static int
get_table_length(enum efi_table_type type,size_t * table_len,void ** taddr)371 get_table_length(enum efi_table_type type, size_t *table_len, void **taddr)
372 {
373 	switch (type) {
374 	case TYPE_ESRT:
375 	{
376 		struct efi_esrt_table *esrt = NULL;
377 		efi_guid_t guid = EFI_TABLE_ESRT;
378 		uint32_t fw_resource_count = 0;
379 		size_t len = sizeof(*esrt);
380 		int error;
381 		void *buf;
382 
383 		error = efi_get_table(&guid, (void **)&esrt);
384 		if (error != 0)
385 			return (error);
386 
387 		buf = malloc(len, M_TEMP, M_WAITOK);
388 		error = physcopyout((vm_paddr_t)esrt, buf, len);
389 		if (error != 0) {
390 			free(buf, M_TEMP);
391 			return (error);
392 		}
393 
394 		/* Check ESRT version */
395 		if (((struct efi_esrt_table *)buf)->fw_resource_version !=
396 		    ESRT_FIRMWARE_RESOURCE_VERSION) {
397 			free(buf, M_TEMP);
398 			return (ENODEV);
399 		}
400 
401 		fw_resource_count = ((struct efi_esrt_table *)buf)->
402 		    fw_resource_count;
403 		if (fw_resource_count > EFI_TABLE_ALLOC_MAX /
404 		    sizeof(struct efi_esrt_entry_v1)) {
405 			free(buf, M_TEMP);
406 			return (ENOMEM);
407 		}
408 
409 		len += fw_resource_count * sizeof(struct efi_esrt_entry_v1);
410 		*table_len = len;
411 
412 		if (taddr != NULL)
413 			*taddr = esrt;
414 		free(buf, M_TEMP);
415 		return (0);
416 	}
417 	case TYPE_PROP:
418 	{
419 		efi_guid_t guid = EFI_PROPERTIES_TABLE;
420 		struct efi_prop_table *prop;
421 		size_t len = sizeof(*prop);
422 		uint32_t prop_len;
423 		int error;
424 		void *buf;
425 
426 		error = efi_get_table(&guid, (void **)&prop);
427 		if (error != 0)
428 			return (error);
429 
430 		buf = malloc(len, M_TEMP, M_WAITOK);
431 		error = physcopyout((vm_paddr_t)prop, buf, len);
432 		if (error != 0) {
433 			free(buf, M_TEMP);
434 			return (error);
435 		}
436 
437 		prop_len = ((struct efi_prop_table *)buf)->length;
438 		if (prop_len > EFI_TABLE_ALLOC_MAX) {
439 			free(buf, M_TEMP);
440 			return (ENOMEM);
441 		}
442 		*table_len = prop_len;
443 
444 		if (taddr != NULL)
445 			*taddr = prop;
446 		free(buf, M_TEMP);
447 		return (0);
448 	}
449 	case TYPE_MEMORY_ATTR:
450 	{
451 		efi_guid_t guid = EFI_MEMORY_ATTRIBUTES_TABLE;
452 		struct efi_memory_attribute_table *tbl_addr, *mem_addr;
453 		int error;
454 		void *buf;
455 		size_t len = sizeof(struct efi_memory_attribute_table);
456 
457 		error = efi_get_table(&guid, (void **)&tbl_addr);
458 		if (error)
459 			return (error);
460 
461 		buf = malloc(len, M_TEMP, M_WAITOK);
462 		error = physcopyout((vm_paddr_t)tbl_addr, buf, len);
463 		if (error) {
464 			free(buf, M_TEMP);
465 			return (error);
466 		}
467 
468 		mem_addr = (struct efi_memory_attribute_table *)buf;
469 		if (mem_addr->version != 2) {
470 			free(buf, M_TEMP);
471 			return (EINVAL);
472 		}
473 		len += mem_addr->descriptor_size * mem_addr->num_ents;
474 		if (len > EFI_TABLE_ALLOC_MAX) {
475 			free(buf, M_TEMP);
476 			return (ENOMEM);
477 		}
478 
479 		*table_len = len;
480 		if (taddr != NULL)
481 			*taddr = tbl_addr;
482 		free(buf, M_TEMP);
483 		return (0);
484 	}
485 	}
486 	return (ENOENT);
487 }
488 
489 static int
copy_table(efi_guid_t * guid,void ** buf,size_t buf_len,size_t * table_len)490 copy_table(efi_guid_t *guid, void **buf, size_t buf_len, size_t *table_len)
491 {
492 	static const struct known_table {
493 		efi_guid_t guid;
494 		enum efi_table_type type;
495 	} tables[] = {
496 		{ EFI_TABLE_ESRT,       TYPE_ESRT },
497 		{ EFI_PROPERTIES_TABLE, TYPE_PROP },
498 		{ EFI_MEMORY_ATTRIBUTES_TABLE, TYPE_MEMORY_ATTR }
499 	};
500 	size_t table_idx;
501 	void *taddr;
502 	int rc;
503 
504 	for (table_idx = 0; table_idx < nitems(tables); table_idx++) {
505 		if (!bcmp(&tables[table_idx].guid, guid, sizeof(*guid)))
506 			break;
507 	}
508 
509 	if (table_idx == nitems(tables))
510 		return (EINVAL);
511 
512 	rc = get_table_length(tables[table_idx].type, table_len, &taddr);
513 	if (rc != 0)
514 		return rc;
515 
516 	/* return table length to userspace */
517 	if (buf == NULL)
518 		return (0);
519 
520 	*buf = malloc(*table_len, M_TEMP, M_WAITOK);
521 	rc = physcopyout((vm_paddr_t)taddr, *buf, *table_len);
522 	return (rc);
523 }
524 
525 static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT;
526 SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN,
527     &efi_rt_handle_faults, 0,
528     "Call EFI RT methods with fault handler wrapper around");
529 
530 static int
efi_rt_arch_call_nofault(struct efirt_callinfo * ec)531 efi_rt_arch_call_nofault(struct efirt_callinfo *ec)
532 {
533 
534 	switch (ec->ec_argcnt) {
535 	case 0:
536 		ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(void))
537 		    ec->ec_fptr)();
538 		break;
539 	case 1:
540 		ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t))
541 		    ec->ec_fptr)(ec->ec_arg1);
542 		break;
543 	case 2:
544 		ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t,
545 		    register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2);
546 		break;
547 	case 3:
548 		ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t,
549 		    register_t, register_t))ec->ec_fptr)(ec->ec_arg1,
550 		    ec->ec_arg2, ec->ec_arg3);
551 		break;
552 	case 4:
553 		ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t,
554 		    register_t, register_t, register_t))ec->ec_fptr)(
555 		    ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4);
556 		break;
557 	case 5:
558 		ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t,
559 		    register_t, register_t, register_t, register_t))
560 		    ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2, ec->ec_arg3,
561 		    ec->ec_arg4, ec->ec_arg5);
562 		break;
563 	default:
564 		panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt);
565 	}
566 
567 	return (0);
568 }
569 
570 static int
efi_call(struct efirt_callinfo * ecp)571 efi_call(struct efirt_callinfo *ecp)
572 {
573 	int error;
574 
575 	error = efi_enter();
576 	if (error != 0)
577 		return (error);
578 	error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) :
579 	    efi_rt_arch_call_nofault(ecp);
580 	efi_leave();
581 	if (error == 0)
582 		error = efi_status_to_errno(ecp->ec_efi_status);
583 	else if (bootverbose)
584 		printf("EFI %s call faulted, error %d\n", ecp->ec_name, error);
585 	return (error);
586 }
587 
588 #define	EFI_RT_METHOD_PA(method)				\
589     ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t)	\
590     efi_runtime))->method)
591 
592 static int
efi_get_time_locked(struct efi_tm * tm,struct efi_tmcap * tmcap)593 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
594 {
595 	struct efirt_callinfo ec;
596 	int error;
597 
598 	EFI_TIME_OWNED();
599 	if (efi_runtime == NULL)
600 		return (ENXIO);
601 	bzero(&ec, sizeof(ec));
602 	ec.ec_name = "rt_gettime";
603 	ec.ec_argcnt = 2;
604 	ec.ec_arg1 = (uintptr_t)tm;
605 	ec.ec_arg2 = (uintptr_t)tmcap;
606 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime);
607 	error = efi_call(&ec);
608 	if (error == 0)
609 		kmsan_mark(tm, sizeof(*tm), KMSAN_STATE_INITED);
610 	return (error);
611 }
612 
613 static int
get_time(struct efi_tm * tm)614 get_time(struct efi_tm *tm)
615 {
616 	struct efi_tmcap dummy;
617 	int error;
618 
619 	if (efi_runtime == NULL)
620 		return (ENXIO);
621 	EFI_TIME_LOCK();
622 	/*
623 	 * UEFI spec states that the Capabilities argument to GetTime is
624 	 * optional, but some UEFI implementations choke when passed a NULL
625 	 * pointer. Pass a dummy efi_tmcap, even though we won't use it,
626 	 * to workaround such implementations.
627 	 */
628 	error = efi_get_time_locked(tm, &dummy);
629 	EFI_TIME_UNLOCK();
630 	return (error);
631 }
632 
633 static int
get_waketime(uint8_t * enabled,uint8_t * pending,struct efi_tm * tm)634 get_waketime(uint8_t *enabled, uint8_t *pending, struct efi_tm *tm)
635 {
636 	struct efirt_callinfo ec;
637 	int error;
638 #ifdef DEV_ACPI
639 	UINT32 acpiRtcEnabled;
640 #endif
641 
642 	if (efi_runtime == NULL)
643 		return (ENXIO);
644 
645 	EFI_TIME_LOCK();
646 	bzero(&ec, sizeof(ec));
647 	ec.ec_name = "rt_getwaketime";
648 	ec.ec_argcnt = 3;
649 	ec.ec_arg1 = (uintptr_t)enabled;
650 	ec.ec_arg2 = (uintptr_t)pending;
651 	ec.ec_arg3 = (uintptr_t)tm;
652 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_getwaketime);
653 	error = efi_call(&ec);
654 	EFI_TIME_UNLOCK();
655 
656 #ifdef DEV_ACPI
657 	if (error == 0) {
658 		error = AcpiReadBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE,
659 		    &acpiRtcEnabled);
660 		if (ACPI_SUCCESS(error)) {
661 			*enabled = *enabled && acpiRtcEnabled;
662 		} else
663 			error = EIO;
664 	}
665 #endif
666 
667 	return (error);
668 }
669 
670 static int
set_waketime(uint8_t enable,struct efi_tm * tm)671 set_waketime(uint8_t enable, struct efi_tm *tm)
672 {
673 	struct efirt_callinfo ec;
674 	int error;
675 
676 	if (efi_runtime == NULL)
677 		return (ENXIO);
678 
679 	EFI_TIME_LOCK();
680 	bzero(&ec, sizeof(ec));
681 	ec.ec_name = "rt_setwaketime";
682 	ec.ec_argcnt = 2;
683 	ec.ec_arg1 = (uintptr_t)enable;
684 	ec.ec_arg2 = (uintptr_t)tm;
685 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_setwaketime);
686 	error = efi_call(&ec);
687 	EFI_TIME_UNLOCK();
688 
689 #ifdef DEV_ACPI
690 	if (error == 0) {
691 		error = AcpiWriteBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE,
692 		    (enable != 0) ? 1 : 0);
693 		if (ACPI_FAILURE(error))
694 			error = EIO;
695 	}
696 #endif
697 
698 	return (error);
699 }
700 
701 static int
get_time_capabilities(struct efi_tmcap * tmcap)702 get_time_capabilities(struct efi_tmcap *tmcap)
703 {
704 	struct efi_tm dummy;
705 	int error;
706 
707 	if (efi_runtime == NULL)
708 		return (ENXIO);
709 	EFI_TIME_LOCK();
710 	error = efi_get_time_locked(&dummy, tmcap);
711 	EFI_TIME_UNLOCK();
712 	return (error);
713 }
714 
715 static int
reset_system(enum efi_reset type)716 reset_system(enum efi_reset type)
717 {
718 	struct efirt_callinfo ec;
719 
720 	switch (type) {
721 	case EFI_RESET_COLD:
722 	case EFI_RESET_WARM:
723 	case EFI_RESET_SHUTDOWN:
724 		break;
725 	default:
726 		return (EINVAL);
727 	}
728 	if (efi_runtime == NULL)
729 		return (ENXIO);
730 	bzero(&ec, sizeof(ec));
731 	ec.ec_name = "rt_reset";
732 	ec.ec_argcnt = 4;
733 	ec.ec_arg1 = (uintptr_t)type;
734 	ec.ec_arg2 = (uintptr_t)0;
735 	ec.ec_arg3 = (uintptr_t)0;
736 	ec.ec_arg4 = (uintptr_t)NULL;
737 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset);
738 	return (efi_call(&ec));
739 }
740 
741 static int
efi_set_time_locked(struct efi_tm * tm)742 efi_set_time_locked(struct efi_tm *tm)
743 {
744 	struct efirt_callinfo ec;
745 
746 	EFI_TIME_OWNED();
747 	if (efi_runtime == NULL)
748 		return (ENXIO);
749 	bzero(&ec, sizeof(ec));
750 	ec.ec_name = "rt_settime";
751 	ec.ec_argcnt = 1;
752 	ec.ec_arg1 = (uintptr_t)tm;
753 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime);
754 	return (efi_call(&ec));
755 }
756 
757 static int
set_time(struct efi_tm * tm)758 set_time(struct efi_tm *tm)
759 {
760 	int error;
761 
762 	if (efi_runtime == NULL)
763 		return (ENXIO);
764 	EFI_TIME_LOCK();
765 	error = efi_set_time_locked(tm);
766 	EFI_TIME_UNLOCK();
767 	return (error);
768 }
769 
770 static int
var_get(efi_char * name,efi_guid_t * vendor,uint32_t * attrib,size_t * datasize,void * data)771 var_get(efi_char *name, efi_guid_t *vendor, uint32_t *attrib,
772     size_t *datasize, void *data)
773 {
774 	struct efirt_callinfo ec;
775 	int error;
776 
777 	if (efi_runtime == NULL)
778 		return (ENXIO);
779 	bzero(&ec, sizeof(ec));
780 	ec.ec_argcnt = 5;
781 	ec.ec_name = "rt_getvar";
782 	ec.ec_arg1 = (uintptr_t)name;
783 	ec.ec_arg2 = (uintptr_t)vendor;
784 	ec.ec_arg3 = (uintptr_t)attrib;
785 	ec.ec_arg4 = (uintptr_t)datasize;
786 	ec.ec_arg5 = (uintptr_t)data;
787 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar);
788 	error = efi_call(&ec);
789 	if (error == 0)
790 		kmsan_mark(data, *datasize, KMSAN_STATE_INITED);
791 	return (error);
792 }
793 
794 static int
var_nextname(size_t * namesize,efi_char * name,efi_guid_t * vendor)795 var_nextname(size_t *namesize, efi_char *name, efi_guid_t *vendor)
796 {
797 	struct efirt_callinfo ec;
798 	int error;
799 
800 	if (efi_runtime == NULL)
801 		return (ENXIO);
802 	bzero(&ec, sizeof(ec));
803 	ec.ec_argcnt = 3;
804 	ec.ec_name = "rt_scanvar";
805 	ec.ec_arg1 = (uintptr_t)namesize;
806 	ec.ec_arg2 = (uintptr_t)name;
807 	ec.ec_arg3 = (uintptr_t)vendor;
808 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar);
809 	error = efi_call(&ec);
810 	if (error == 0)
811 		kmsan_mark(name, *namesize, KMSAN_STATE_INITED);
812 	return (error);
813 }
814 
815 static int
var_set(efi_char * name,efi_guid_t * vendor,uint32_t attrib,size_t datasize,void * data)816 var_set(efi_char *name, efi_guid_t *vendor, uint32_t attrib,
817     size_t datasize, void *data)
818 {
819 	struct efirt_callinfo ec;
820 
821 	if (efi_runtime == NULL)
822 		return (ENXIO);
823 	bzero(&ec, sizeof(ec));
824 	ec.ec_argcnt = 5;
825 	ec.ec_name = "rt_setvar";
826 	ec.ec_arg1 = (uintptr_t)name;
827 	ec.ec_arg2 = (uintptr_t)vendor;
828 	ec.ec_arg3 = (uintptr_t)attrib;
829 	ec.ec_arg4 = (uintptr_t)datasize;
830 	ec.ec_arg5 = (uintptr_t)data;
831 	ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar);
832 	return (efi_call(&ec));
833 }
834 
835 const static struct efi_ops efi_ops = {
836 	.rt_ok = rt_ok,
837 	.get_table = get_table,
838 	.copy_table = copy_table,
839 	.get_time = get_time,
840 	.get_time_capabilities = get_time_capabilities,
841 	.reset_system = reset_system,
842 	.set_time = set_time,
843 	.get_waketime = get_waketime,
844 	.set_waketime = set_waketime,
845 	.var_get = var_get,
846 	.var_nextname = var_nextname,
847 	.var_set = var_set,
848 };
849 const struct efi_ops *active_efi_ops = &efi_ops;
850 
851 static int
efirt_modevents(module_t m,int event,void * arg __unused)852 efirt_modevents(module_t m, int event, void *arg __unused)
853 {
854 
855 	switch (event) {
856 	case MOD_LOAD:
857 		return (efi_init());
858 
859 	case MOD_UNLOAD:
860 		efi_uninit();
861 		return (0);
862 
863 	case MOD_SHUTDOWN:
864 		return (0);
865 
866 	default:
867 		return (EOPNOTSUPP);
868 	}
869 }
870 
871 static moduledata_t efirt_moddata = {
872 	.name = "efirt",
873 	.evhand = efirt_modevents,
874 	.priv = NULL,
875 };
876 /* After fpuinitstate, before efidev */
877 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
878 MODULE_VERSION(efirt, 1);
879