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$");
37 #include <sys/param.h>
39 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
41 #include <sys/linker.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/clock.h>
49 #include <sys/reboot.h>
50 #include <sys/rwlock.h>
51 #include <sys/sched.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
55 #include <sys/vmmeter.h>
57 #include <machine/fpu.h>
58 #include <machine/efi.h>
59 #include <machine/metadata.h>
60 #include <machine/vmparam.h>
64 #include <vm/vm_map.h>
67 #include <contrib/dev/acpica/include/acpi.h>
70 #define EFI_TABLE_ALLOC_MAX 0x800000
72 static struct efi_systbl *efi_systbl;
73 static eventhandler_tag efi_shutdown_tag;
75 * The following pointers point to tables in the EFI runtime service data pages.
76 * Care should be taken to make sure that we've properly entered the EFI runtime
77 * environment (efi_enter()) before dereferencing them.
79 static struct efi_cfgtbl *efi_cfgtbl;
80 static struct efi_rt *efi_runtime;
82 static int efi_status2err[25] = {
84 ENOEXEC, /* EFI_LOAD_ERROR */
85 EINVAL, /* EFI_INVALID_PARAMETER */
86 ENOSYS, /* EFI_UNSUPPORTED */
87 EMSGSIZE, /* EFI_BAD_BUFFER_SIZE */
88 EOVERFLOW, /* EFI_BUFFER_TOO_SMALL */
89 EBUSY, /* EFI_NOT_READY */
90 EIO, /* EFI_DEVICE_ERROR */
91 EROFS, /* EFI_WRITE_PROTECTED */
92 EAGAIN, /* EFI_OUT_OF_RESOURCES */
93 EIO, /* EFI_VOLUME_CORRUPTED */
94 ENOSPC, /* EFI_VOLUME_FULL */
95 ENXIO, /* EFI_NO_MEDIA */
96 ESTALE, /* EFI_MEDIA_CHANGED */
97 ENOENT, /* EFI_NOT_FOUND */
98 EACCES, /* EFI_ACCESS_DENIED */
99 ETIMEDOUT, /* EFI_NO_RESPONSE */
100 EADDRNOTAVAIL, /* EFI_NO_MAPPING */
101 ETIMEDOUT, /* EFI_TIMEOUT */
102 EDOOFUS, /* EFI_NOT_STARTED */
103 EALREADY, /* EFI_ALREADY_STARTED */
104 ECANCELED, /* EFI_ABORTED */
105 EPROTO, /* EFI_ICMP_ERROR */
106 EPROTO, /* EFI_TFTP_ERROR */
107 EPROTO /* EFI_PROTOCOL_ERROR */
110 enum efi_table_type {
115 static int efi_enter(void);
116 static void efi_leave(void);
119 efi_status_to_errno(efi_status status)
123 code = status & 0x3ffffffffffffffful;
124 return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
127 static struct mtx efi_lock;
128 static SYSCTL_NODE(_hw, OID_AUTO, efi, CTLFLAG_RWTUN | CTLFLAG_MPSAFE, NULL,
130 static bool efi_poweroff = true;
131 SYSCTL_BOOL(_hw_efi, OID_AUTO, poweroff, CTLFLAG_RWTUN, &efi_poweroff, 0,
132 "If true, use EFI runtime services to power off in preference to ACPI");
135 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr)
140 for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
142 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
145 if (addr >= p->md_virt &&
146 addr < p->md_virt + p->md_pages * EFI_PAGE_SIZE)
154 efi_shutdown_final(void *dummy __unused, int howto)
158 * On some systems, ACPI S5 is missing or does not function properly.
159 * When present, shutdown via EFI Runtime Services instead, unless
162 if ((howto & RB_POWEROFF) != 0 && efi_poweroff)
163 (void)efi_reset_system(EFI_RESET_SHUTDOWN);
169 struct efi_map_header *efihdr;
174 int ndesc, rt_disabled;
177 TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
178 if (rt_disabled == 1)
180 mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
182 if (efi_systbl_phys == 0) {
184 printf("EFI systbl not available\n");
188 efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys);
189 if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) {
192 printf("EFI systbl signature invalid\n");
195 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
196 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
197 if (efi_cfgtbl == NULL) {
199 printf("EFI config table is not present\n");
202 kmdp = preload_search_by_type("elf kernel");
204 kmdp = preload_search_by_type("elf64 kernel");
205 efihdr = (struct efi_map_header *)preload_search_info(kmdp,
206 MODINFO_METADATA | MODINFOMD_EFI_MAP);
207 if (efihdr == NULL) {
209 printf("EFI map is not present\n");
212 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
213 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
214 if (efihdr->descriptor_size == 0)
217 ndesc = efihdr->memory_size / efihdr->descriptor_size;
218 if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) {
220 printf("EFI cannot create runtime map\n");
224 efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
225 (struct efi_rt *)efi_systbl->st_rt;
226 if (efi_runtime == NULL) {
228 printf("EFI runtime services table is not present\n");
229 efi_destroy_1t1_map();
233 #if defined(__aarch64__) || defined(__amd64__)
235 * Some UEFI implementations have multiple implementations of the
236 * RS->GetTime function. They switch from one we can only use early
237 * in the boot process to one valid as a RunTime service only when we
238 * call RS->SetVirtualAddressMap. As this is not always the case, e.g.
239 * with an old loader.efi, check if the RS->GetTime function is within
240 * the EFI map, and fail to attach if not.
242 rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime);
243 if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size,
244 (vm_offset_t)rtdm->rt_gettime)) {
247 "EFI runtime services table has an invalid pointer\n");
249 efi_destroy_1t1_map();
255 * We use SHUTDOWN_PRI_LAST - 1 to trigger after IPMI, but before ACPI.
257 efi_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final,
258 efi_shutdown_final, NULL, SHUTDOWN_PRI_LAST - 1);
267 /* Most likely disabled by tunable */
268 if (efi_runtime == NULL)
270 if (efi_shutdown_tag != NULL)
271 EVENTHANDLER_DEREGISTER(shutdown_final, efi_shutdown_tag);
272 efi_destroy_1t1_map();
278 mtx_destroy(&efi_lock);
285 if (efi_runtime == NULL)
297 if (efi_runtime == NULL)
300 curpmap = &td->td_proc->p_vmspace->vm_pmap;
303 fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
304 error = efi_arch_enter();
306 fpu_kern_leave(td, NULL);
307 mtx_unlock(&efi_lock);
308 PMAP_UNLOCK(curpmap);
321 curpmap = &curproc->p_vmspace->vm_pmap;
323 fpu_kern_leave(td, NULL);
324 mtx_unlock(&efi_lock);
325 PMAP_UNLOCK(curpmap);
329 get_table(struct uuid *uuid, void **ptr)
331 struct efi_cfgtbl *ct;
335 if (efi_cfgtbl == NULL || efi_systbl == NULL)
340 count = efi_systbl->st_entries;
343 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
356 get_table_length(enum efi_table_type type, size_t *table_len, void **taddr)
361 struct efi_esrt_table *esrt = NULL;
362 struct uuid uuid = EFI_TABLE_ESRT;
363 uint32_t fw_resource_count = 0;
364 size_t len = sizeof(*esrt);
368 error = efi_get_table(&uuid, (void **)&esrt);
372 buf = malloc(len, M_TEMP, M_WAITOK);
373 error = physcopyout((vm_paddr_t)esrt, buf, len);
379 /* Check ESRT version */
380 if (((struct efi_esrt_table *)buf)->fw_resource_version !=
381 ESRT_FIRMWARE_RESOURCE_VERSION) {
386 fw_resource_count = ((struct efi_esrt_table *)buf)->
388 if (fw_resource_count > EFI_TABLE_ALLOC_MAX /
389 sizeof(struct efi_esrt_entry_v1)) {
394 len += fw_resource_count * sizeof(struct efi_esrt_entry_v1);
404 struct uuid uuid = EFI_PROPERTIES_TABLE;
405 struct efi_prop_table *prop;
406 size_t len = sizeof(*prop);
411 error = efi_get_table(&uuid, (void **)&prop);
415 buf = malloc(len, M_TEMP, M_WAITOK);
416 error = physcopyout((vm_paddr_t)prop, buf, len);
422 prop_len = ((struct efi_prop_table *)buf)->length;
423 if (prop_len > EFI_TABLE_ALLOC_MAX) {
427 *table_len = prop_len;
439 copy_table(struct uuid *uuid, void **buf, size_t buf_len, size_t *table_len)
441 static const struct known_table {
443 enum efi_table_type type;
445 { EFI_TABLE_ESRT, TYPE_ESRT },
446 { EFI_PROPERTIES_TABLE, TYPE_PROP }
452 for (table_idx = 0; table_idx < nitems(tables); table_idx++) {
453 if (!bcmp(&tables[table_idx].uuid, uuid, sizeof(*uuid)))
457 if (table_idx == nitems(tables))
460 rc = get_table_length(tables[table_idx].type, table_len, &taddr);
464 /* return table length to userspace */
468 *buf = malloc(*table_len, M_TEMP, M_WAITOK);
469 rc = physcopyout((vm_paddr_t)taddr, *buf, *table_len);
473 static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT;
474 SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN,
475 &efi_rt_handle_faults, 0,
476 "Call EFI RT methods with fault handler wrapper around");
479 efi_rt_arch_call_nofault(struct efirt_callinfo *ec)
482 switch (ec->ec_argcnt) {
484 ec->ec_efi_status = ((register_t (*)(void))ec->ec_fptr)();
487 ec->ec_efi_status = ((register_t (*)(register_t))ec->ec_fptr)
491 ec->ec_efi_status = ((register_t (*)(register_t, register_t))
492 ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2);
495 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
496 register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2,
500 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
501 register_t, register_t))ec->ec_fptr)(ec->ec_arg1,
502 ec->ec_arg2, ec->ec_arg3, ec->ec_arg4);
505 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
506 register_t, register_t, register_t))ec->ec_fptr)(
507 ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4,
511 panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt);
518 efi_call(struct efirt_callinfo *ecp)
525 error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) :
526 efi_rt_arch_call_nofault(ecp);
529 error = efi_status_to_errno(ecp->ec_efi_status);
530 else if (bootverbose)
531 printf("EFI %s call faulted, error %d\n", ecp->ec_name, error);
535 #define EFI_RT_METHOD_PA(method) \
536 ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t) \
537 efi_runtime))->method)
540 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
542 struct efirt_callinfo ec;
546 if (efi_runtime == NULL)
548 bzero(&ec, sizeof(ec));
549 ec.ec_name = "rt_gettime";
551 ec.ec_arg1 = (uintptr_t)tm;
552 ec.ec_arg2 = (uintptr_t)tmcap;
553 ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime);
554 error = efi_call(&ec);
556 kmsan_mark(tm, sizeof(*tm), KMSAN_STATE_INITED);
561 get_time(struct efi_tm *tm)
563 struct efi_tmcap dummy;
566 if (efi_runtime == NULL)
570 * UEFI spec states that the Capabilities argument to GetTime is
571 * optional, but some UEFI implementations choke when passed a NULL
572 * pointer. Pass a dummy efi_tmcap, even though we won't use it,
573 * to workaround such implementations.
575 error = efi_get_time_locked(tm, &dummy);
581 get_waketime(uint8_t *enabled, uint8_t *pending, struct efi_tm *tm)
583 struct efirt_callinfo ec;
586 UINT32 acpiRtcEnabled;
589 if (efi_runtime == NULL)
593 bzero(&ec, sizeof(ec));
594 ec.ec_name = "rt_getwaketime";
596 ec.ec_arg1 = (uintptr_t)enabled;
597 ec.ec_arg2 = (uintptr_t)pending;
598 ec.ec_arg3 = (uintptr_t)tm;
599 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getwaketime);
600 error = efi_call(&ec);
605 error = AcpiReadBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE,
607 if (ACPI_SUCCESS(error)) {
608 *enabled = *enabled && acpiRtcEnabled;
618 set_waketime(uint8_t enable, struct efi_tm *tm)
620 struct efirt_callinfo ec;
623 if (efi_runtime == NULL)
627 bzero(&ec, sizeof(ec));
628 ec.ec_name = "rt_setwaketime";
630 ec.ec_arg1 = (uintptr_t)enable;
631 ec.ec_arg2 = (uintptr_t)tm;
632 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setwaketime);
633 error = efi_call(&ec);
638 error = AcpiWriteBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE,
639 (enable != 0) ? 1 : 0);
640 if (ACPI_FAILURE(error))
649 get_time_capabilities(struct efi_tmcap *tmcap)
654 if (efi_runtime == NULL)
657 error = efi_get_time_locked(&dummy, tmcap);
663 reset_system(enum efi_reset type)
665 struct efirt_callinfo ec;
670 case EFI_RESET_SHUTDOWN:
675 if (efi_runtime == NULL)
677 bzero(&ec, sizeof(ec));
678 ec.ec_name = "rt_reset";
680 ec.ec_arg1 = (uintptr_t)type;
681 ec.ec_arg2 = (uintptr_t)0;
682 ec.ec_arg3 = (uintptr_t)0;
683 ec.ec_arg4 = (uintptr_t)NULL;
684 ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset);
685 return (efi_call(&ec));
689 efi_set_time_locked(struct efi_tm *tm)
691 struct efirt_callinfo ec;
694 if (efi_runtime == NULL)
696 bzero(&ec, sizeof(ec));
697 ec.ec_name = "rt_settime";
699 ec.ec_arg1 = (uintptr_t)tm;
700 ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime);
701 return (efi_call(&ec));
705 set_time(struct efi_tm *tm)
709 if (efi_runtime == NULL)
712 error = efi_set_time_locked(tm);
718 var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
719 size_t *datasize, void *data)
721 struct efirt_callinfo ec;
724 if (efi_runtime == NULL)
726 bzero(&ec, sizeof(ec));
728 ec.ec_name = "rt_getvar";
729 ec.ec_arg1 = (uintptr_t)name;
730 ec.ec_arg2 = (uintptr_t)vendor;
731 ec.ec_arg3 = (uintptr_t)attrib;
732 ec.ec_arg4 = (uintptr_t)datasize;
733 ec.ec_arg5 = (uintptr_t)data;
734 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar);
735 error = efi_call(&ec);
737 kmsan_mark(data, *datasize, KMSAN_STATE_INITED);
742 var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
744 struct efirt_callinfo ec;
747 if (efi_runtime == NULL)
749 bzero(&ec, sizeof(ec));
751 ec.ec_name = "rt_scanvar";
752 ec.ec_arg1 = (uintptr_t)namesize;
753 ec.ec_arg2 = (uintptr_t)name;
754 ec.ec_arg3 = (uintptr_t)vendor;
755 ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar);
756 error = efi_call(&ec);
758 kmsan_mark(name, *namesize, KMSAN_STATE_INITED);
763 var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
764 size_t datasize, void *data)
766 struct efirt_callinfo ec;
768 if (efi_runtime == NULL)
770 bzero(&ec, sizeof(ec));
772 ec.ec_name = "rt_setvar";
773 ec.ec_arg1 = (uintptr_t)name;
774 ec.ec_arg2 = (uintptr_t)vendor;
775 ec.ec_arg3 = (uintptr_t)attrib;
776 ec.ec_arg4 = (uintptr_t)datasize;
777 ec.ec_arg5 = (uintptr_t)data;
778 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar);
779 return (efi_call(&ec));
782 const static struct efi_ops efi_ops = {
784 .get_table = get_table,
785 .copy_table = copy_table,
786 .get_time = get_time,
787 .get_time_capabilities = get_time_capabilities,
788 .reset_system = reset_system,
789 .set_time = set_time,
790 .get_waketime = get_waketime,
791 .set_waketime = set_waketime,
793 .var_nextname = var_nextname,
796 const struct efi_ops *active_efi_ops = &efi_ops;
799 efirt_modevents(module_t m, int event, void *arg __unused)
818 static moduledata_t efirt_moddata = {
820 .evhand = efirt_modevents,
823 /* After fpuinitstate, before efidev */
824 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
825 MODULE_VERSION(efirt, 1);