2 * Copyright (c) 2004 Marcel Moolenaar
3 * Copyright (c) 2001 Doug Rabson
4 * Copyright (c) 2016, 2018 The FreeBSD Foundation
7 * Portions of this software were developed by Konstantin Belousov
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/linker.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 #include <sys/clock.h>
45 #include <sys/reboot.h>
46 #include <sys/rwlock.h>
47 #include <sys/sched.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 #include <sys/vmmeter.h>
52 #include <machine/fpu.h>
53 #include <machine/efi.h>
54 #include <machine/metadata.h>
55 #include <machine/vmparam.h>
59 #include <vm/vm_map.h>
61 static struct efi_systbl *efi_systbl;
62 static eventhandler_tag efi_shutdown_tag;
64 * The following pointers point to tables in the EFI runtime service data pages.
65 * Care should be taken to make sure that we've properly entered the EFI runtime
66 * environment (efi_enter()) before dereferencing them.
68 static struct efi_cfgtbl *efi_cfgtbl;
69 static struct efi_rt *efi_runtime;
71 static int efi_status2err[25] = {
73 ENOEXEC, /* EFI_LOAD_ERROR */
74 EINVAL, /* EFI_INVALID_PARAMETER */
75 ENOSYS, /* EFI_UNSUPPORTED */
76 EMSGSIZE, /* EFI_BAD_BUFFER_SIZE */
77 EOVERFLOW, /* EFI_BUFFER_TOO_SMALL */
78 EBUSY, /* EFI_NOT_READY */
79 EIO, /* EFI_DEVICE_ERROR */
80 EROFS, /* EFI_WRITE_PROTECTED */
81 EAGAIN, /* EFI_OUT_OF_RESOURCES */
82 EIO, /* EFI_VOLUME_CORRUPTED */
83 ENOSPC, /* EFI_VOLUME_FULL */
84 ENXIO, /* EFI_NO_MEDIA */
85 ESTALE, /* EFI_MEDIA_CHANGED */
86 ENOENT, /* EFI_NOT_FOUND */
87 EACCES, /* EFI_ACCESS_DENIED */
88 ETIMEDOUT, /* EFI_NO_RESPONSE */
89 EADDRNOTAVAIL, /* EFI_NO_MAPPING */
90 ETIMEDOUT, /* EFI_TIMEOUT */
91 EDOOFUS, /* EFI_NOT_STARTED */
92 EALREADY, /* EFI_ALREADY_STARTED */
93 ECANCELED, /* EFI_ABORTED */
94 EPROTO, /* EFI_ICMP_ERROR */
95 EPROTO, /* EFI_TFTP_ERROR */
96 EPROTO /* EFI_PROTOCOL_ERROR */
99 static int efi_enter(void);
100 static void efi_leave(void);
103 efi_status_to_errno(efi_status status)
107 code = status & 0x3ffffffffffffffful;
108 return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
111 static struct mtx efi_lock;
112 static SYSCTL_NODE(_hw, OID_AUTO, efi, CTLFLAG_RWTUN | CTLFLAG_MPSAFE, NULL,
114 static bool efi_poweroff = true;
115 SYSCTL_BOOL(_hw_efi, OID_AUTO, poweroff, CTLFLAG_RWTUN, &efi_poweroff, 0,
116 "If true, use EFI runtime services to power off in preference to ACPI");
119 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr)
124 for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
126 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
129 if (addr >= p->md_virt &&
130 addr < p->md_virt + p->md_pages * PAGE_SIZE)
138 efi_shutdown_final(void *dummy __unused, int howto)
142 * On some systems, ACPI S5 is missing or does not function properly.
143 * When present, shutdown via EFI Runtime Services instead, unless
146 if ((howto & RB_POWEROFF) != 0 && efi_poweroff)
147 (void)efi_reset_system(EFI_RESET_SHUTDOWN);
153 struct efi_map_header *efihdr;
158 int ndesc, rt_disabled;
161 TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
162 if (rt_disabled == 1)
164 mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
166 if (efi_systbl_phys == 0) {
168 printf("EFI systbl not available\n");
172 efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys);
173 if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
176 printf("EFI systbl signature invalid\n");
179 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
180 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
181 if (efi_cfgtbl == NULL) {
183 printf("EFI config table is not present\n");
186 kmdp = preload_search_by_type("elf kernel");
188 kmdp = preload_search_by_type("elf64 kernel");
189 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
190 MODINFO_METADATA | MODINFOMD_EFI_MAP);
191 if (efihdr == NULL) {
193 printf("EFI map is not present\n");
196 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
197 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
198 if (efihdr->descriptor_size == 0)
201 ndesc = efihdr->memory_size / efihdr->descriptor_size;
202 if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) {
204 printf("EFI cannot create runtime map\n");
208 efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
209 (struct efi_rt *)efi_systbl->st_rt;
210 if (efi_runtime == NULL) {
212 printf("EFI runtime services table is not present\n");
213 efi_destroy_1t1_map();
217 #if defined(__aarch64__) || defined(__amd64__)
219 * Some UEFI implementations have multiple implementations of the
220 * RS->GetTime function. They switch from one we can only use early
221 * in the boot process to one valid as a RunTime service only when we
222 * call RS->SetVirtualAddressMap. As this is not always the case, e.g.
223 * with an old loader.efi, check if the RS->GetTime function is within
224 * the EFI map, and fail to attach if not.
226 rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime);
227 if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size,
228 (vm_offset_t)rtdm->rt_gettime)) {
231 "EFI runtime services table has an invalid pointer\n");
233 efi_destroy_1t1_map();
239 * We use SHUTDOWN_PRI_LAST - 1 to trigger after IPMI, but before ACPI.
241 efi_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final,
242 efi_shutdown_final, NULL, SHUTDOWN_PRI_LAST - 1);
251 /* Most likely disabled by tunable */
252 if (efi_runtime == NULL)
254 if (efi_shutdown_tag != NULL)
255 EVENTHANDLER_DEREGISTER(shutdown_final, efi_shutdown_tag);
256 efi_destroy_1t1_map();
262 mtx_destroy(&efi_lock);
269 if (efi_runtime == NULL)
281 if (efi_runtime == NULL)
284 curpmap = &td->td_proc->p_vmspace->vm_pmap;
287 fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
288 error = efi_arch_enter();
290 fpu_kern_leave(td, NULL);
291 mtx_unlock(&efi_lock);
292 PMAP_UNLOCK(curpmap);
305 curpmap = &curproc->p_vmspace->vm_pmap;
307 fpu_kern_leave(td, NULL);
308 mtx_unlock(&efi_lock);
309 PMAP_UNLOCK(curpmap);
313 get_table(struct uuid *uuid, void **ptr)
315 struct efi_cfgtbl *ct;
319 if (efi_cfgtbl == NULL || efi_systbl == NULL)
324 count = efi_systbl->st_entries;
327 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
339 static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT;
340 SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN,
341 &efi_rt_handle_faults, 0,
342 "Call EFI RT methods with fault handler wrapper around");
345 efi_rt_arch_call_nofault(struct efirt_callinfo *ec)
348 switch (ec->ec_argcnt) {
350 ec->ec_efi_status = ((register_t (*)(void))ec->ec_fptr)();
353 ec->ec_efi_status = ((register_t (*)(register_t))ec->ec_fptr)
357 ec->ec_efi_status = ((register_t (*)(register_t, register_t))
358 ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2);
361 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
362 register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2,
366 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
367 register_t, register_t))ec->ec_fptr)(ec->ec_arg1,
368 ec->ec_arg2, ec->ec_arg3, ec->ec_arg4);
371 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
372 register_t, register_t, register_t))ec->ec_fptr)(
373 ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4,
377 panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt);
384 efi_call(struct efirt_callinfo *ecp)
391 error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) :
392 efi_rt_arch_call_nofault(ecp);
395 error = efi_status_to_errno(ecp->ec_efi_status);
396 else if (bootverbose)
397 printf("EFI %s call faulted, error %d\n", ecp->ec_name, error);
401 #define EFI_RT_METHOD_PA(method) \
402 ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t) \
403 efi_runtime))->method)
406 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
408 struct efirt_callinfo ec;
411 if (efi_runtime == NULL)
413 bzero(&ec, sizeof(ec));
414 ec.ec_name = "rt_gettime";
416 ec.ec_arg1 = (uintptr_t)tm;
417 ec.ec_arg2 = (uintptr_t)tmcap;
418 ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime);
419 return (efi_call(&ec));
423 get_time(struct efi_tm *tm)
425 struct efi_tmcap dummy;
428 if (efi_runtime == NULL)
432 * UEFI spec states that the Capabilities argument to GetTime is
433 * optional, but some UEFI implementations choke when passed a NULL
434 * pointer. Pass a dummy efi_tmcap, even though we won't use it,
435 * to workaround such implementations.
437 error = efi_get_time_locked(tm, &dummy);
443 get_time_capabilities(struct efi_tmcap *tmcap)
448 if (efi_runtime == NULL)
451 error = efi_get_time_locked(&dummy, tmcap);
457 reset_system(enum efi_reset type)
459 struct efirt_callinfo ec;
464 case EFI_RESET_SHUTDOWN:
469 if (efi_runtime == NULL)
471 bzero(&ec, sizeof(ec));
472 ec.ec_name = "rt_reset";
474 ec.ec_arg1 = (uintptr_t)type;
475 ec.ec_arg2 = (uintptr_t)0;
476 ec.ec_arg3 = (uintptr_t)0;
477 ec.ec_arg4 = (uintptr_t)NULL;
478 ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset);
479 return (efi_call(&ec));
483 efi_set_time_locked(struct efi_tm *tm)
485 struct efirt_callinfo ec;
488 if (efi_runtime == NULL)
490 bzero(&ec, sizeof(ec));
491 ec.ec_name = "rt_settime";
493 ec.ec_arg1 = (uintptr_t)tm;
494 ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime);
495 return (efi_call(&ec));
499 set_time(struct efi_tm *tm)
503 if (efi_runtime == NULL)
506 error = efi_set_time_locked(tm);
512 var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
513 size_t *datasize, void *data)
515 struct efirt_callinfo ec;
517 if (efi_runtime == NULL)
519 bzero(&ec, sizeof(ec));
521 ec.ec_name = "rt_getvar";
522 ec.ec_arg1 = (uintptr_t)name;
523 ec.ec_arg2 = (uintptr_t)vendor;
524 ec.ec_arg3 = (uintptr_t)attrib;
525 ec.ec_arg4 = (uintptr_t)datasize;
526 ec.ec_arg5 = (uintptr_t)data;
527 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar);
528 return (efi_call(&ec));
532 var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
534 struct efirt_callinfo ec;
536 if (efi_runtime == NULL)
538 bzero(&ec, sizeof(ec));
540 ec.ec_name = "rt_scanvar";
541 ec.ec_arg1 = (uintptr_t)namesize;
542 ec.ec_arg2 = (uintptr_t)name;
543 ec.ec_arg3 = (uintptr_t)vendor;
544 ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar);
545 return (efi_call(&ec));
549 var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
550 size_t datasize, void *data)
552 struct efirt_callinfo ec;
554 if (efi_runtime == NULL)
556 bzero(&ec, sizeof(ec));
558 ec.ec_name = "rt_setvar";
559 ec.ec_arg1 = (uintptr_t)name;
560 ec.ec_arg2 = (uintptr_t)vendor;
561 ec.ec_arg3 = (uintptr_t)attrib;
562 ec.ec_arg4 = (uintptr_t)datasize;
563 ec.ec_arg5 = (uintptr_t)data;
564 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar);
565 return (efi_call(&ec));
568 const static struct efi_ops efi_ops = {
570 .get_table = get_table,
571 .get_time = get_time,
572 .get_time_capabilities = get_time_capabilities,
573 .reset_system = reset_system,
574 .set_time = set_time,
576 .var_nextname = var_nextname,
579 const struct efi_ops *active_efi_ops = &efi_ops;
582 efirt_modevents(module_t m, int event, void *arg __unused)
601 static moduledata_t efirt_moddata = {
603 .evhand = efirt_modevents,
606 /* After fpuinitstate, before efidev */
607 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
608 MODULE_VERSION(efirt, 1);