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