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/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
44 #include <sys/clock.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>
52 #include <sys/vmmeter.h>
54 #include <machine/fpu.h>
55 #include <machine/efi.h>
56 #include <machine/metadata.h>
57 #include <machine/vmparam.h>
61 #include <vm/vm_map.h>
63 #define EFI_TABLE_ALLOC_MAX 0x800000
65 static struct efi_systbl *efi_systbl;
66 static eventhandler_tag efi_shutdown_tag;
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.
72 static struct efi_cfgtbl *efi_cfgtbl;
73 static struct efi_rt *efi_runtime;
75 static int efi_status2err[25] = {
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 */
103 enum efi_table_type {
108 static int efi_enter(void);
109 static void efi_leave(void);
112 efi_status_to_errno(efi_status status)
116 code = status & 0x3ffffffffffffffful;
117 return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS);
120 static struct mtx efi_lock;
121 static SYSCTL_NODE(_hw, OID_AUTO, efi, CTLFLAG_RWTUN | CTLFLAG_MPSAFE, NULL,
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");
128 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr)
133 for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p,
135 if ((p->md_attr & EFI_MD_ATTR_RT) == 0)
138 if (addr >= p->md_virt &&
139 addr < p->md_virt + p->md_pages * PAGE_SIZE)
147 efi_shutdown_final(void *dummy __unused, int howto)
151 * On some systems, ACPI S5 is missing or does not function properly.
152 * When present, shutdown via EFI Runtime Services instead, unless
155 if ((howto & RB_POWEROFF) != 0 && efi_poweroff)
156 (void)efi_reset_system(EFI_RESET_SHUTDOWN);
162 struct efi_map_header *efihdr;
167 int ndesc, rt_disabled;
170 TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled);
171 if (rt_disabled == 1)
173 mtx_init(&efi_lock, "efi", NULL, MTX_DEF);
175 if (efi_systbl_phys == 0) {
177 printf("EFI systbl not available\n");
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) {
185 printf("EFI systbl signature invalid\n");
188 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL :
189 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl;
190 if (efi_cfgtbl == NULL) {
192 printf("EFI config table is not present\n");
195 kmdp = preload_search_by_type("elf kernel");
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) {
202 printf("EFI map is not present\n");
205 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
206 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
207 if (efihdr->descriptor_size == 0)
210 ndesc = efihdr->memory_size / efihdr->descriptor_size;
211 if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) {
213 printf("EFI cannot create runtime map\n");
217 efi_runtime = (efi_systbl->st_rt == 0) ? NULL :
218 (struct efi_rt *)efi_systbl->st_rt;
219 if (efi_runtime == NULL) {
221 printf("EFI runtime services table is not present\n");
222 efi_destroy_1t1_map();
226 #if defined(__aarch64__) || defined(__amd64__)
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.
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)) {
240 "EFI runtime services table has an invalid pointer\n");
242 efi_destroy_1t1_map();
248 * We use SHUTDOWN_PRI_LAST - 1 to trigger after IPMI, but before ACPI.
250 efi_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final,
251 efi_shutdown_final, NULL, SHUTDOWN_PRI_LAST - 1);
260 /* Most likely disabled by tunable */
261 if (efi_runtime == NULL)
263 if (efi_shutdown_tag != NULL)
264 EVENTHANDLER_DEREGISTER(shutdown_final, efi_shutdown_tag);
265 efi_destroy_1t1_map();
271 mtx_destroy(&efi_lock);
278 if (efi_runtime == NULL)
290 if (efi_runtime == NULL)
293 curpmap = &td->td_proc->p_vmspace->vm_pmap;
296 fpu_kern_enter(td, NULL, FPU_KERN_NOCTX);
297 error = efi_arch_enter();
299 fpu_kern_leave(td, NULL);
300 mtx_unlock(&efi_lock);
301 PMAP_UNLOCK(curpmap);
314 curpmap = &curproc->p_vmspace->vm_pmap;
316 fpu_kern_leave(td, NULL);
317 mtx_unlock(&efi_lock);
318 PMAP_UNLOCK(curpmap);
322 get_table(struct uuid *uuid, void **ptr)
324 struct efi_cfgtbl *ct;
328 if (efi_cfgtbl == NULL || efi_systbl == NULL)
333 count = efi_systbl->st_entries;
336 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) {
349 get_table_length(enum efi_table_type type, size_t *table_len, void **taddr)
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);
361 error = efi_get_table(&uuid, (void **)&esrt);
365 buf = malloc(len, M_TEMP, M_WAITOK);
366 error = physcopyout((vm_paddr_t)esrt, buf, len);
372 /* Check ESRT version */
373 if (((struct efi_esrt_table *)buf)->fw_resource_version !=
374 ESRT_FIRMWARE_RESOURCE_VERSION) {
379 fw_resource_count = ((struct efi_esrt_table *)buf)->
381 if (fw_resource_count > EFI_TABLE_ALLOC_MAX /
382 sizeof(struct efi_esrt_entry_v1)) {
387 len += fw_resource_count * sizeof(struct efi_esrt_entry_v1);
397 struct uuid uuid = EFI_PROPERTIES_TABLE;
398 struct efi_prop_table *prop;
399 size_t len = sizeof(*prop);
404 error = efi_get_table(&uuid, (void **)&prop);
408 buf = malloc(len, M_TEMP, M_WAITOK);
409 error = physcopyout((vm_paddr_t)prop, buf, len);
415 prop_len = ((struct efi_prop_table *)buf)->length;
416 if (prop_len > EFI_TABLE_ALLOC_MAX) {
420 *table_len = prop_len;
432 copy_table(struct uuid *uuid, void **buf, size_t buf_len, size_t *table_len)
434 static const struct known_table {
436 enum efi_table_type type;
438 { EFI_TABLE_ESRT, TYPE_ESRT },
439 { EFI_PROPERTIES_TABLE, TYPE_PROP }
445 for (table_idx = 0; table_idx < nitems(tables); table_idx++) {
446 if (!bcmp(&tables[table_idx].uuid, uuid, sizeof(*uuid)))
450 if (table_idx == nitems(tables))
453 rc = get_table_length(tables[table_idx].type, table_len, &taddr);
457 /* return table length to userspace */
461 *buf = malloc(*table_len, M_TEMP, M_WAITOK);
462 rc = physcopyout((vm_paddr_t)taddr, *buf, *table_len);
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");
472 efi_rt_arch_call_nofault(struct efirt_callinfo *ec)
475 switch (ec->ec_argcnt) {
477 ec->ec_efi_status = ((register_t (*)(void))ec->ec_fptr)();
480 ec->ec_efi_status = ((register_t (*)(register_t))ec->ec_fptr)
484 ec->ec_efi_status = ((register_t (*)(register_t, register_t))
485 ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2);
488 ec->ec_efi_status = ((register_t (*)(register_t, register_t,
489 register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2,
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);
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,
504 panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt);
511 efi_call(struct efirt_callinfo *ecp)
518 error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) :
519 efi_rt_arch_call_nofault(ecp);
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);
528 #define EFI_RT_METHOD_PA(method) \
529 ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t) \
530 efi_runtime))->method)
533 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap)
535 struct efirt_callinfo ec;
538 if (efi_runtime == NULL)
540 bzero(&ec, sizeof(ec));
541 ec.ec_name = "rt_gettime";
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));
550 get_time(struct efi_tm *tm)
552 struct efi_tmcap dummy;
555 if (efi_runtime == NULL)
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.
564 error = efi_get_time_locked(tm, &dummy);
570 get_time_capabilities(struct efi_tmcap *tmcap)
575 if (efi_runtime == NULL)
578 error = efi_get_time_locked(&dummy, tmcap);
584 reset_system(enum efi_reset type)
586 struct efirt_callinfo ec;
591 case EFI_RESET_SHUTDOWN:
596 if (efi_runtime == NULL)
598 bzero(&ec, sizeof(ec));
599 ec.ec_name = "rt_reset";
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));
610 efi_set_time_locked(struct efi_tm *tm)
612 struct efirt_callinfo ec;
615 if (efi_runtime == NULL)
617 bzero(&ec, sizeof(ec));
618 ec.ec_name = "rt_settime";
620 ec.ec_arg1 = (uintptr_t)tm;
621 ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime);
622 return (efi_call(&ec));
626 set_time(struct efi_tm *tm)
630 if (efi_runtime == NULL)
633 error = efi_set_time_locked(tm);
639 var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib,
640 size_t *datasize, void *data)
642 struct efirt_callinfo ec;
644 if (efi_runtime == NULL)
646 bzero(&ec, sizeof(ec));
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));
659 var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor)
661 struct efirt_callinfo ec;
663 if (efi_runtime == NULL)
665 bzero(&ec, sizeof(ec));
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));
676 var_set(efi_char *name, struct uuid *vendor, uint32_t attrib,
677 size_t datasize, void *data)
679 struct efirt_callinfo ec;
681 if (efi_runtime == NULL)
683 bzero(&ec, sizeof(ec));
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));
695 const static struct efi_ops efi_ops = {
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,
704 .var_nextname = var_nextname,
707 const struct efi_ops *active_efi_ops = &efi_ops;
710 efirt_modevents(module_t m, int event, void *arg __unused)
729 static moduledata_t efirt_moddata = {
731 .evhand = efirt_modevents,
734 /* After fpuinitstate, before efidev */
735 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND);
736 MODULE_VERSION(efirt, 1);