diff --git a/sys/dev/efidev/efidev.c b/sys/dev/efidev/efidev.c index 79d98956ed24..45e571828e51 100644 --- a/sys/dev/efidev/efidev.c +++ b/sys/dev/efidev/efidev.c @@ -1,235 +1,250 @@ /*- * Copyright (c) 2016 Netflix, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * in this position and unchanged. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include static d_ioctl_t efidev_ioctl; static struct cdevsw efi_cdevsw = { .d_name = "efi", .d_version = D_VERSION, .d_ioctl = efidev_ioctl, }; static int efidev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t addr, int flags __unused, struct thread *td __unused) { int error; switch (cmd) { case EFIIOC_GET_TABLE: { struct efi_get_table_ioc *egtioc = (struct efi_get_table_ioc *)addr; void *buf = NULL; error = efi_copy_table(&egtioc->uuid, egtioc->buf ? &buf : NULL, egtioc->buf_len, &egtioc->table_len); if (error != 0 || egtioc->buf == NULL) break; if (egtioc->buf_len < egtioc->table_len) { error = EINVAL; free(buf, M_TEMP); break; } error = copyout(buf, egtioc->buf, egtioc->buf_len); free(buf, M_TEMP); break; } case EFIIOC_GET_TIME: { struct efi_tm *tm = (struct efi_tm *)addr; error = efi_get_time(tm); break; } case EFIIOC_SET_TIME: { struct efi_tm *tm = (struct efi_tm *)addr; error = efi_set_time(tm); break; } + case EFIIOC_GET_WAKETIME: + { + struct efi_waketime_ioc *wt = (struct efi_waketime_ioc *)addr; + + error = efi_get_waketime(&wt->enabled, &wt->pending, + &wt->waketime); + break; + } + case EFIIOC_SET_WAKETIME: + { + struct efi_waketime_ioc *wt = (struct efi_waketime_ioc *)addr; + + error = efi_set_waketime(wt->enabled, &wt->waketime); + break; + } case EFIIOC_VAR_GET: { struct efi_var_ioc *ev = (struct efi_var_ioc *)addr; void *data; efi_char *name; data = malloc(ev->datasize, M_TEMP, M_WAITOK); name = malloc(ev->namesize, M_TEMP, M_WAITOK); error = copyin(ev->name, name, ev->namesize); if (error) goto vg_out; if (name[ev->namesize / sizeof(efi_char) - 1] != 0) { error = EINVAL; goto vg_out; } error = efi_var_get(name, &ev->vendor, &ev->attrib, &ev->datasize, data); if (error == 0) { error = copyout(data, ev->data, ev->datasize); } else if (error == EOVERFLOW) { /* * Pass back the size we really need, but * convert the error to 0 so the copyout * happens. datasize was updated in the * efi_var_get call. */ ev->data = NULL; error = 0; } vg_out: free(data, M_TEMP); free(name, M_TEMP); break; } case EFIIOC_VAR_NEXT: { struct efi_var_ioc *ev = (struct efi_var_ioc *)addr; efi_char *name; name = malloc(ev->namesize, M_TEMP, M_WAITOK); error = copyin(ev->name, name, ev->namesize); if (error) goto vn_out; /* Note: namesize is the buffer size, not the string lenght */ error = efi_var_nextname(&ev->namesize, name, &ev->vendor); if (error == 0) { error = copyout(name, ev->name, ev->namesize); } else if (error == EOVERFLOW) { ev->name = NULL; error = 0; } vn_out: free(name, M_TEMP); break; } case EFIIOC_VAR_SET: { struct efi_var_ioc *ev = (struct efi_var_ioc *)addr; void *data = NULL; efi_char *name; /* datasize == 0 -> delete (more or less) */ if (ev->datasize > 0) data = malloc(ev->datasize, M_TEMP, M_WAITOK); name = malloc(ev->namesize, M_TEMP, M_WAITOK); if (ev->datasize) { error = copyin(ev->data, data, ev->datasize); if (error) goto vs_out; } error = copyin(ev->name, name, ev->namesize); if (error) goto vs_out; if (name[ev->namesize / sizeof(efi_char) - 1] != 0) { error = EINVAL; goto vs_out; } error = efi_var_set(name, &ev->vendor, ev->attrib, ev->datasize, data); vs_out: free(data, M_TEMP); free(name, M_TEMP); break; } default: error = ENOTTY; break; } return (error); } static struct cdev *efidev; static int efidev_modevents(module_t m, int event, void *arg __unused) { struct make_dev_args mda; int error; switch (event) { case MOD_LOAD: /* * If we have no efi environment, then don't create the device. */ if (efi_rt_ok() != 0) return (0); make_dev_args_init(&mda); mda.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME; mda.mda_devsw = &efi_cdevsw; mda.mda_uid = UID_ROOT; mda.mda_gid = GID_WHEEL; mda.mda_mode = 0700; error = make_dev_s(&mda, &efidev, "efi"); return (error); case MOD_UNLOAD: if (efidev != NULL) destroy_dev(efidev); efidev = NULL; return (0); case MOD_SHUTDOWN: return (0); default: return (EOPNOTSUPP); } } static moduledata_t efidev_moddata = { .name = "efidev", .evhand = efidev_modevents, .priv = NULL, }; DECLARE_MODULE(efidev, efidev_moddata, SI_SUB_DRIVERS, SI_ORDER_ANY); MODULE_VERSION(efidev, 1); MODULE_DEPEND(efidev, efirt, 1, 1, 1); diff --git a/sys/dev/efidev/efirt.c b/sys/dev/efidev/efirt.c index e4c47fa68741..d577396de20c 100644 --- a/sys/dev/efidev/efirt.c +++ b/sys/dev/efidev/efirt.c @@ -1,749 +1,825 @@ /*- * Copyright (c) 2004 Marcel Moolenaar * Copyright (c) 2001 Doug Rabson * Copyright (c) 2016, 2018 The FreeBSD Foundation * All rights reserved. * * Portions of this software were developed by Konstantin Belousov * under sponsorship from the FreeBSD Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); +#include "opt_acpi.h" + #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#ifdef DEV_ACPI +#include +#endif + #define EFI_TABLE_ALLOC_MAX 0x800000 static struct efi_systbl *efi_systbl; static eventhandler_tag efi_shutdown_tag; /* * The following pointers point to tables in the EFI runtime service data pages. * Care should be taken to make sure that we've properly entered the EFI runtime * environment (efi_enter()) before dereferencing them. */ static struct efi_cfgtbl *efi_cfgtbl; static struct efi_rt *efi_runtime; static int efi_status2err[25] = { 0, /* EFI_SUCCESS */ ENOEXEC, /* EFI_LOAD_ERROR */ EINVAL, /* EFI_INVALID_PARAMETER */ ENOSYS, /* EFI_UNSUPPORTED */ EMSGSIZE, /* EFI_BAD_BUFFER_SIZE */ EOVERFLOW, /* EFI_BUFFER_TOO_SMALL */ EBUSY, /* EFI_NOT_READY */ EIO, /* EFI_DEVICE_ERROR */ EROFS, /* EFI_WRITE_PROTECTED */ EAGAIN, /* EFI_OUT_OF_RESOURCES */ EIO, /* EFI_VOLUME_CORRUPTED */ ENOSPC, /* EFI_VOLUME_FULL */ ENXIO, /* EFI_NO_MEDIA */ ESTALE, /* EFI_MEDIA_CHANGED */ ENOENT, /* EFI_NOT_FOUND */ EACCES, /* EFI_ACCESS_DENIED */ ETIMEDOUT, /* EFI_NO_RESPONSE */ EADDRNOTAVAIL, /* EFI_NO_MAPPING */ ETIMEDOUT, /* EFI_TIMEOUT */ EDOOFUS, /* EFI_NOT_STARTED */ EALREADY, /* EFI_ALREADY_STARTED */ ECANCELED, /* EFI_ABORTED */ EPROTO, /* EFI_ICMP_ERROR */ EPROTO, /* EFI_TFTP_ERROR */ EPROTO /* EFI_PROTOCOL_ERROR */ }; enum efi_table_type { TYPE_ESRT = 0, TYPE_PROP }; static int efi_enter(void); static void efi_leave(void); int efi_status_to_errno(efi_status status) { u_long code; code = status & 0x3ffffffffffffffful; return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS); } static struct mtx efi_lock; static SYSCTL_NODE(_hw, OID_AUTO, efi, CTLFLAG_RWTUN | CTLFLAG_MPSAFE, NULL, "EFI"); static bool efi_poweroff = true; SYSCTL_BOOL(_hw_efi, OID_AUTO, poweroff, CTLFLAG_RWTUN, &efi_poweroff, 0, "If true, use EFI runtime services to power off in preference to ACPI"); static bool efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr) { struct efi_md *p; int i; for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p, descsz)) { if ((p->md_attr & EFI_MD_ATTR_RT) == 0) continue; if (addr >= p->md_virt && addr < p->md_virt + p->md_pages * EFI_PAGE_SIZE) return (true); } return (false); } static void efi_shutdown_final(void *dummy __unused, int howto) { /* * On some systems, ACPI S5 is missing or does not function properly. * When present, shutdown via EFI Runtime Services instead, unless * disabled. */ if ((howto & RB_POWEROFF) != 0 && efi_poweroff) (void)efi_reset_system(EFI_RESET_SHUTDOWN); } static int efi_init(void) { struct efi_map_header *efihdr; struct efi_md *map; struct efi_rt *rtdm; caddr_t kmdp; size_t efisz; int ndesc, rt_disabled; rt_disabled = 0; TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled); if (rt_disabled == 1) return (0); mtx_init(&efi_lock, "efi", NULL, MTX_DEF); if (efi_systbl_phys == 0) { if (bootverbose) printf("EFI systbl not available\n"); return (0); } efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys); if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) { efi_systbl = NULL; if (bootverbose) printf("EFI systbl signature invalid\n"); return (0); } efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL : (struct efi_cfgtbl *)efi_systbl->st_cfgtbl; if (efi_cfgtbl == NULL) { if (bootverbose) printf("EFI config table is not present\n"); } kmdp = preload_search_by_type("elf kernel"); if (kmdp == NULL) kmdp = preload_search_by_type("elf64 kernel"); efihdr = (struct efi_map_header *)preload_search_info(kmdp, MODINFO_METADATA | MODINFOMD_EFI_MAP); if (efihdr == NULL) { if (bootverbose) printf("EFI map is not present\n"); return (0); } efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; map = (struct efi_md *)((uint8_t *)efihdr + efisz); if (efihdr->descriptor_size == 0) return (ENOMEM); ndesc = efihdr->memory_size / efihdr->descriptor_size; if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) { if (bootverbose) printf("EFI cannot create runtime map\n"); return (ENOMEM); } efi_runtime = (efi_systbl->st_rt == 0) ? NULL : (struct efi_rt *)efi_systbl->st_rt; if (efi_runtime == NULL) { if (bootverbose) printf("EFI runtime services table is not present\n"); efi_destroy_1t1_map(); return (ENXIO); } #if defined(__aarch64__) || defined(__amd64__) /* * Some UEFI implementations have multiple implementations of the * RS->GetTime function. They switch from one we can only use early * in the boot process to one valid as a RunTime service only when we * call RS->SetVirtualAddressMap. As this is not always the case, e.g. * with an old loader.efi, check if the RS->GetTime function is within * the EFI map, and fail to attach if not. */ rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime); if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size, (vm_offset_t)rtdm->rt_gettime)) { if (bootverbose) printf( "EFI runtime services table has an invalid pointer\n"); efi_runtime = NULL; efi_destroy_1t1_map(); return (ENXIO); } #endif /* * We use SHUTDOWN_PRI_LAST - 1 to trigger after IPMI, but before ACPI. */ efi_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final, efi_shutdown_final, NULL, SHUTDOWN_PRI_LAST - 1); return (0); } static void efi_uninit(void) { /* Most likely disabled by tunable */ if (efi_runtime == NULL) return; if (efi_shutdown_tag != NULL) EVENTHANDLER_DEREGISTER(shutdown_final, efi_shutdown_tag); efi_destroy_1t1_map(); efi_systbl = NULL; efi_cfgtbl = NULL; efi_runtime = NULL; mtx_destroy(&efi_lock); } static int rt_ok(void) { if (efi_runtime == NULL) return (ENXIO); return (0); } static int efi_enter(void) { struct thread *td; pmap_t curpmap; int error; if (efi_runtime == NULL) return (ENXIO); td = curthread; curpmap = &td->td_proc->p_vmspace->vm_pmap; PMAP_LOCK(curpmap); mtx_lock(&efi_lock); fpu_kern_enter(td, NULL, FPU_KERN_NOCTX); error = efi_arch_enter(); if (error != 0) { fpu_kern_leave(td, NULL); mtx_unlock(&efi_lock); PMAP_UNLOCK(curpmap); } return (error); } static void efi_leave(void) { struct thread *td; pmap_t curpmap; efi_arch_leave(); curpmap = &curproc->p_vmspace->vm_pmap; td = curthread; fpu_kern_leave(td, NULL); mtx_unlock(&efi_lock); PMAP_UNLOCK(curpmap); } static int get_table(struct uuid *uuid, void **ptr) { struct efi_cfgtbl *ct; u_long count; int error; if (efi_cfgtbl == NULL || efi_systbl == NULL) return (ENXIO); error = efi_enter(); if (error != 0) return (error); count = efi_systbl->st_entries; ct = efi_cfgtbl; while (count--) { if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) { *ptr = ct->ct_data; efi_leave(); return (0); } ct++; } efi_leave(); return (ENOENT); } static int get_table_length(enum efi_table_type type, size_t *table_len, void **taddr) { switch (type) { case TYPE_ESRT: { struct efi_esrt_table *esrt = NULL; struct uuid uuid = EFI_TABLE_ESRT; uint32_t fw_resource_count = 0; size_t len = sizeof(*esrt); int error; void *buf; error = efi_get_table(&uuid, (void **)&esrt); if (error != 0) return (error); buf = malloc(len, M_TEMP, M_WAITOK); error = physcopyout((vm_paddr_t)esrt, buf, len); if (error != 0) { free(buf, M_TEMP); return (error); } /* Check ESRT version */ if (((struct efi_esrt_table *)buf)->fw_resource_version != ESRT_FIRMWARE_RESOURCE_VERSION) { free(buf, M_TEMP); return (ENODEV); } fw_resource_count = ((struct efi_esrt_table *)buf)-> fw_resource_count; if (fw_resource_count > EFI_TABLE_ALLOC_MAX / sizeof(struct efi_esrt_entry_v1)) { free(buf, M_TEMP); return (ENOMEM); } len += fw_resource_count * sizeof(struct efi_esrt_entry_v1); *table_len = len; if (taddr != NULL) *taddr = esrt; free(buf, M_TEMP); return (0); } case TYPE_PROP: { struct uuid uuid = EFI_PROPERTIES_TABLE; struct efi_prop_table *prop; size_t len = sizeof(*prop); uint32_t prop_len; int error; void *buf; error = efi_get_table(&uuid, (void **)&prop); if (error != 0) return (error); buf = malloc(len, M_TEMP, M_WAITOK); error = physcopyout((vm_paddr_t)prop, buf, len); if (error != 0) { free(buf, M_TEMP); return (error); } prop_len = ((struct efi_prop_table *)buf)->length; if (prop_len > EFI_TABLE_ALLOC_MAX) { free(buf, M_TEMP); return (ENOMEM); } *table_len = prop_len; if (taddr != NULL) *taddr = prop; free(buf, M_TEMP); return (0); } } return (ENOENT); } static int copy_table(struct uuid *uuid, void **buf, size_t buf_len, size_t *table_len) { static const struct known_table { struct uuid uuid; enum efi_table_type type; } tables[] = { { EFI_TABLE_ESRT, TYPE_ESRT }, { EFI_PROPERTIES_TABLE, TYPE_PROP } }; size_t table_idx; void *taddr; int rc; for (table_idx = 0; table_idx < nitems(tables); table_idx++) { if (!bcmp(&tables[table_idx].uuid, uuid, sizeof(*uuid))) break; } if (table_idx == nitems(tables)) return (EINVAL); rc = get_table_length(tables[table_idx].type, table_len, &taddr); if (rc != 0) return rc; /* return table length to userspace */ if (buf == NULL) return (0); *buf = malloc(*table_len, M_TEMP, M_WAITOK); rc = physcopyout((vm_paddr_t)taddr, *buf, *table_len); return (rc); } static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT; SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN, &efi_rt_handle_faults, 0, "Call EFI RT methods with fault handler wrapper around"); static int efi_rt_arch_call_nofault(struct efirt_callinfo *ec) { switch (ec->ec_argcnt) { case 0: ec->ec_efi_status = ((register_t (*)(void))ec->ec_fptr)(); break; case 1: ec->ec_efi_status = ((register_t (*)(register_t))ec->ec_fptr) (ec->ec_arg1); break; case 2: ec->ec_efi_status = ((register_t (*)(register_t, register_t)) ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2); break; case 3: ec->ec_efi_status = ((register_t (*)(register_t, register_t, register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2, ec->ec_arg3); break; case 4: ec->ec_efi_status = ((register_t (*)(register_t, register_t, register_t, register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4); break; case 5: ec->ec_efi_status = ((register_t (*)(register_t, register_t, register_t, register_t, register_t))ec->ec_fptr)( ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4, ec->ec_arg5); break; default: panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt); } return (0); } static int efi_call(struct efirt_callinfo *ecp) { int error; error = efi_enter(); if (error != 0) return (error); error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) : efi_rt_arch_call_nofault(ecp); efi_leave(); if (error == 0) error = efi_status_to_errno(ecp->ec_efi_status); else if (bootverbose) printf("EFI %s call faulted, error %d\n", ecp->ec_name, error); return (error); } #define EFI_RT_METHOD_PA(method) \ ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t) \ efi_runtime))->method) static int efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap) { struct efirt_callinfo ec; int error; EFI_TIME_OWNED(); if (efi_runtime == NULL) return (ENXIO); bzero(&ec, sizeof(ec)); ec.ec_name = "rt_gettime"; ec.ec_argcnt = 2; ec.ec_arg1 = (uintptr_t)tm; ec.ec_arg2 = (uintptr_t)tmcap; ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime); error = efi_call(&ec); if (error == 0) kmsan_mark(tm, sizeof(*tm), KMSAN_STATE_INITED); return (error); } static int get_time(struct efi_tm *tm) { struct efi_tmcap dummy; int error; if (efi_runtime == NULL) return (ENXIO); EFI_TIME_LOCK(); /* * UEFI spec states that the Capabilities argument to GetTime is * optional, but some UEFI implementations choke when passed a NULL * pointer. Pass a dummy efi_tmcap, even though we won't use it, * to workaround such implementations. */ error = efi_get_time_locked(tm, &dummy); EFI_TIME_UNLOCK(); return (error); } +static int +get_waketime(uint8_t *enabled, uint8_t *pending, struct efi_tm *tm) +{ + struct efirt_callinfo ec; + int error; +#ifdef DEV_ACPI + UINT32 acpiRtcEnabled; +#endif + + if (efi_runtime == NULL) + return (ENXIO); + + EFI_TIME_LOCK(); + bzero(&ec, sizeof(ec)); + ec.ec_name = "rt_getwaketime"; + ec.ec_argcnt = 3; + ec.ec_arg1 = (uintptr_t)enabled; + ec.ec_arg2 = (uintptr_t)pending; + ec.ec_arg3 = (uintptr_t)tm; + ec.ec_fptr = EFI_RT_METHOD_PA(rt_getwaketime); + error = efi_call(&ec); + EFI_TIME_UNLOCK(); + +#ifdef DEV_ACPI + if (error == 0) { + error = AcpiReadBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE, + &acpiRtcEnabled); + if (ACPI_SUCCESS(error)) { + *enabled = *enabled && acpiRtcEnabled; + } else + error = EIO; + } +#endif + + return (error); +} + +static int +set_waketime(uint8_t enable, struct efi_tm *tm) +{ + struct efirt_callinfo ec; + int error; + + if (efi_runtime == NULL) + return (ENXIO); + + EFI_TIME_LOCK(); + bzero(&ec, sizeof(ec)); + ec.ec_name = "rt_setwaketime"; + ec.ec_argcnt = 2; + ec.ec_arg1 = (uintptr_t)enable; + ec.ec_arg2 = (uintptr_t)tm; + ec.ec_fptr = EFI_RT_METHOD_PA(rt_setwaketime); + error = efi_call(&ec); + EFI_TIME_UNLOCK(); + +#ifdef DEV_ACPI + if (error == 0) { + error = AcpiWriteBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE, + (enable != 0) ? 1 : 0); + if (ACPI_FAILURE(error)) + error = EIO; + } +#endif + + return (error); +} + static int get_time_capabilities(struct efi_tmcap *tmcap) { struct efi_tm dummy; int error; if (efi_runtime == NULL) return (ENXIO); EFI_TIME_LOCK(); error = efi_get_time_locked(&dummy, tmcap); EFI_TIME_UNLOCK(); return (error); } static int reset_system(enum efi_reset type) { struct efirt_callinfo ec; switch (type) { case EFI_RESET_COLD: case EFI_RESET_WARM: case EFI_RESET_SHUTDOWN: break; default: return (EINVAL); } if (efi_runtime == NULL) return (ENXIO); bzero(&ec, sizeof(ec)); ec.ec_name = "rt_reset"; ec.ec_argcnt = 4; ec.ec_arg1 = (uintptr_t)type; ec.ec_arg2 = (uintptr_t)0; ec.ec_arg3 = (uintptr_t)0; ec.ec_arg4 = (uintptr_t)NULL; ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset); return (efi_call(&ec)); } static int efi_set_time_locked(struct efi_tm *tm) { struct efirt_callinfo ec; EFI_TIME_OWNED(); if (efi_runtime == NULL) return (ENXIO); bzero(&ec, sizeof(ec)); ec.ec_name = "rt_settime"; ec.ec_argcnt = 1; ec.ec_arg1 = (uintptr_t)tm; ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime); return (efi_call(&ec)); } static int set_time(struct efi_tm *tm) { int error; if (efi_runtime == NULL) return (ENXIO); EFI_TIME_LOCK(); error = efi_set_time_locked(tm); EFI_TIME_UNLOCK(); return (error); } static int var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib, size_t *datasize, void *data) { struct efirt_callinfo ec; int error; if (efi_runtime == NULL) return (ENXIO); bzero(&ec, sizeof(ec)); ec.ec_argcnt = 5; ec.ec_name = "rt_getvar"; ec.ec_arg1 = (uintptr_t)name; ec.ec_arg2 = (uintptr_t)vendor; ec.ec_arg3 = (uintptr_t)attrib; ec.ec_arg4 = (uintptr_t)datasize; ec.ec_arg5 = (uintptr_t)data; ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar); error = efi_call(&ec); if (error == 0) kmsan_mark(data, *datasize, KMSAN_STATE_INITED); return (error); } static int var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor) { struct efirt_callinfo ec; int error; if (efi_runtime == NULL) return (ENXIO); bzero(&ec, sizeof(ec)); ec.ec_argcnt = 3; ec.ec_name = "rt_scanvar"; ec.ec_arg1 = (uintptr_t)namesize; ec.ec_arg2 = (uintptr_t)name; ec.ec_arg3 = (uintptr_t)vendor; ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar); error = efi_call(&ec); if (error == 0) kmsan_mark(name, *namesize, KMSAN_STATE_INITED); return (error); } static int var_set(efi_char *name, struct uuid *vendor, uint32_t attrib, size_t datasize, void *data) { struct efirt_callinfo ec; if (efi_runtime == NULL) return (ENXIO); bzero(&ec, sizeof(ec)); ec.ec_argcnt = 5; ec.ec_name = "rt_setvar"; ec.ec_arg1 = (uintptr_t)name; ec.ec_arg2 = (uintptr_t)vendor; ec.ec_arg3 = (uintptr_t)attrib; ec.ec_arg4 = (uintptr_t)datasize; ec.ec_arg5 = (uintptr_t)data; ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar); return (efi_call(&ec)); } const static struct efi_ops efi_ops = { .rt_ok = rt_ok, .get_table = get_table, .copy_table = copy_table, .get_time = get_time, .get_time_capabilities = get_time_capabilities, .reset_system = reset_system, .set_time = set_time, + .get_waketime = get_waketime, + .set_waketime = set_waketime, .var_get = var_get, .var_nextname = var_nextname, .var_set = var_set, }; const struct efi_ops *active_efi_ops = &efi_ops; static int efirt_modevents(module_t m, int event, void *arg __unused) { switch (event) { case MOD_LOAD: return (efi_init()); case MOD_UNLOAD: efi_uninit(); return (0); case MOD_SHUTDOWN: return (0); default: return (EOPNOTSUPP); } } static moduledata_t efirt_moddata = { .name = "efirt", .evhand = efirt_modevents, .priv = NULL, }; /* After fpuinitstate, before efidev */ DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND); MODULE_VERSION(efirt, 1); diff --git a/sys/sys/efi.h b/sys/sys/efi.h index 4714dd137551..8d2d932934da 100644 --- a/sys/sys/efi.h +++ b/sys/sys/efi.h @@ -1,353 +1,371 @@ /*- * Copyright (c) 2004 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _SYS_EFI_H_ #define _SYS_EFI_H_ #include #include #define EFI_PAGE_SHIFT 12 #define EFI_PAGE_SIZE (1 << EFI_PAGE_SHIFT) #define EFI_PAGE_MASK (EFI_PAGE_SIZE - 1) #define EFI_TABLE_SMBIOS \ {0xeb9d2d31,0x2d88,0x11d3,0x9a,0x16,{0x00,0x90,0x27,0x3f,0xc1,0x4d}} #define EFI_TABLE_SMBIOS3 \ {0xf2fd1544,0x9794,0x4a2c,0x99,0x2e,{0xe5,0xbb,0xcf,0x20,0xe3,0x94}} #define EFI_TABLE_ESRT \ {0xb122a263,0x3661,0x4f68,0x99,0x29,{0x78,0xf8,0xb0,0xd6,0x21,0x80}} #define EFI_PROPERTIES_TABLE \ {0x880aaca3,0x4adc,0x4a04,0x90,0x79,{0xb7,0x47,0x34,0x08,0x25,0xe5}} #define LINUX_EFI_MEMRESERVE_TABLE \ {0x888eb0c6,0x8ede,0x4ff5,0xa8,0xf0,{0x9a,0xee,0x5c,0xb9,0x77,0xc2}} enum efi_reset { EFI_RESET_COLD = 0, EFI_RESET_WARM = 1, EFI_RESET_SHUTDOWN = 2, }; typedef uint16_t efi_char; typedef unsigned long efi_status; struct efi_cfgtbl { struct uuid ct_uuid; void *ct_data; }; #define EFI_MEMORY_DESCRIPTOR_VERSION 1 struct efi_md { uint32_t md_type; #define EFI_MD_TYPE_NULL 0 #define EFI_MD_TYPE_CODE 1 /* Loader text. */ #define EFI_MD_TYPE_DATA 2 /* Loader data. */ #define EFI_MD_TYPE_BS_CODE 3 /* Boot services text. */ #define EFI_MD_TYPE_BS_DATA 4 /* Boot services data. */ #define EFI_MD_TYPE_RT_CODE 5 /* Runtime services text. */ #define EFI_MD_TYPE_RT_DATA 6 /* Runtime services data. */ #define EFI_MD_TYPE_FREE 7 /* Unused/free memory. */ #define EFI_MD_TYPE_BAD 8 /* Bad memory */ #define EFI_MD_TYPE_RECLAIM 9 /* ACPI reclaimable memory. */ #define EFI_MD_TYPE_FIRMWARE 10 /* ACPI NV memory */ #define EFI_MD_TYPE_IOMEM 11 /* Memory-mapped I/O. */ #define EFI_MD_TYPE_IOPORT 12 /* I/O port space. */ #define EFI_MD_TYPE_PALCODE 13 /* PAL */ #define EFI_MD_TYPE_PERSISTENT 14 /* Persistent memory. */ uint32_t __pad; uint64_t md_phys; uint64_t md_virt; uint64_t md_pages; uint64_t md_attr; #define EFI_MD_ATTR_UC 0x0000000000000001UL #define EFI_MD_ATTR_WC 0x0000000000000002UL #define EFI_MD_ATTR_WT 0x0000000000000004UL #define EFI_MD_ATTR_WB 0x0000000000000008UL #define EFI_MD_ATTR_UCE 0x0000000000000010UL #define EFI_MD_ATTR_WP 0x0000000000001000UL #define EFI_MD_ATTR_RP 0x0000000000002000UL #define EFI_MD_ATTR_XP 0x0000000000004000UL #define EFI_MD_ATTR_NV 0x0000000000008000UL #define EFI_MD_ATTR_MORE_RELIABLE \ 0x0000000000010000UL #define EFI_MD_ATTR_RO 0x0000000000020000UL #define EFI_MD_ATTR_RT 0x8000000000000000UL }; #define efi_next_descriptor(ptr, size) \ ((struct efi_md *)(((uint8_t *)(ptr)) + (size))) struct efi_tm { uint16_t tm_year; /* 1998 - 20XX */ uint8_t tm_mon; /* 1 - 12 */ uint8_t tm_mday; /* 1 - 31 */ uint8_t tm_hour; /* 0 - 23 */ uint8_t tm_min; /* 0 - 59 */ uint8_t tm_sec; /* 0 - 59 */ uint8_t __pad1; uint32_t tm_nsec; /* 0 - 999,999,999 */ int16_t tm_tz; /* -1440 to 1440 or 2047 */ uint8_t tm_dst; uint8_t __pad2; }; struct efi_tmcap { uint32_t tc_res; /* 1e-6 parts per million */ uint32_t tc_prec; /* hertz */ uint8_t tc_stz; /* Set clears sub-second time */ }; struct efi_tblhdr { uint64_t th_sig; uint32_t th_rev; uint32_t th_hdrsz; uint32_t th_crc32; uint32_t __res; }; #define ESRT_FIRMWARE_RESOURCE_VERSION 1 struct efi_esrt_table { uint32_t fw_resource_count; uint32_t fw_resource_count_max; uint64_t fw_resource_version; uint8_t entries[]; }; struct efi_esrt_entry_v1 { struct uuid fw_class; uint32_t fw_type; uint32_t fw_version; uint32_t lowest_supported_fw_version; uint32_t capsule_flags; uint32_t last_attempt_version; uint32_t last_attempt_status; }; struct efi_prop_table { uint32_t version; uint32_t length; uint64_t memory_protection_attribute; }; #ifdef _KERNEL #ifdef EFIABI_ATTR struct efi_rt { struct efi_tblhdr rt_hdr; efi_status (*rt_gettime)(struct efi_tm *, struct efi_tmcap *) EFIABI_ATTR; efi_status (*rt_settime)(struct efi_tm *) EFIABI_ATTR; efi_status (*rt_getwaketime)(uint8_t *, uint8_t *, struct efi_tm *) EFIABI_ATTR; efi_status (*rt_setwaketime)(uint8_t, struct efi_tm *) EFIABI_ATTR; efi_status (*rt_setvirtual)(u_long, u_long, uint32_t, struct efi_md *) EFIABI_ATTR; efi_status (*rt_cvtptr)(u_long, void **) EFIABI_ATTR; efi_status (*rt_getvar)(efi_char *, struct uuid *, uint32_t *, u_long *, void *) EFIABI_ATTR; efi_status (*rt_scanvar)(u_long *, efi_char *, struct uuid *) EFIABI_ATTR; efi_status (*rt_setvar)(efi_char *, struct uuid *, uint32_t, u_long, void *) EFIABI_ATTR; efi_status (*rt_gethicnt)(uint32_t *) EFIABI_ATTR; efi_status (*rt_reset)(enum efi_reset, efi_status, u_long, efi_char *) EFIABI_ATTR; }; #endif struct efi_systbl { struct efi_tblhdr st_hdr; #define EFI_SYSTBL_SIG 0x5453595320494249UL efi_char *st_fwvendor; uint32_t st_fwrev; uint32_t __pad; void *st_cin; void *st_cinif; void *st_cout; void *st_coutif; void *st_cerr; void *st_cerrif; uint64_t st_rt; void *st_bs; u_long st_entries; uint64_t st_cfgtbl; }; extern vm_paddr_t efi_systbl_phys; /* * When memory is reserved for some use, Linux will add a * LINUX_EFI_MEMSERVE_TABLE to the cfgtbl array of tables to communicate * this. At present, Linux only uses this as part of its workaround for a GICv3 * issue where you can't stop the controller long enough to move it's config and * pending vectors. When the LinuxBoot environment kexec's a new kernel, the new * kernel needs to use this old memory (and not use it for any other purpose). * * Linux stores the PA of this table in the cfgtbl. And all the addresses are * the physical address of 'reserved' memory. The mr_next field creates a linked * list of these tables, and all must be walked. If mr_count is 0, that entry * should be ignored. There is no checksum for these tables, nor do they have * a efi_tblhdr. * * This table is only documented in the Linux code in drivers/firmware/efi/efi.c. */ struct linux_efi_memreserve_entry { vm_offset_t mre_base; /* PA of reserved area */ vm_offset_t mre_size; /* Size of area */ }; struct linux_efi_memreserve { uint32_t mr_size; /* Total size of table in bytes */ uint32_t mr_count; /* Count of entries used */ vm_offset_t mr_next; /* Next in chain (though unused?) */ struct linux_efi_memreserve_entry mr_entry[]; }; struct efirt_callinfo; /* Internal MD EFI functions */ int efi_arch_enter(void); void efi_arch_leave(void); vm_offset_t efi_phys_to_kva(vm_paddr_t); int efi_rt_arch_call(struct efirt_callinfo *); bool efi_create_1t1_map(struct efi_md *, int, int); void efi_destroy_1t1_map(void); struct efi_ops { /* * The EFI calls might be virtualized in some environments, requiring * FreeBSD to use a different interface (ie: hypercalls) in order to * access them. */ int (*rt_ok)(void); int (*get_table)(struct uuid *, void **); int (*copy_table)(struct uuid *, void **, size_t, size_t *); int (*get_time)(struct efi_tm *); int (*get_time_capabilities)(struct efi_tmcap *); int (*reset_system)(enum efi_reset); int (*set_time)(struct efi_tm *); + int (*get_waketime)(uint8_t *enabled, uint8_t *pending, + struct efi_tm *tm); + int (*set_waketime)(uint8_t enable, struct efi_tm *tm); int (*var_get)(uint16_t *, struct uuid *, uint32_t *, size_t *, void *); int (*var_nextname)(size_t *, uint16_t *, struct uuid *); int (*var_set)(uint16_t *, struct uuid *, uint32_t, size_t, void *); }; extern const struct efi_ops *active_efi_ops; /* Public MI EFI functions */ static inline int efi_rt_ok(void) { if (active_efi_ops->rt_ok == NULL) return (ENXIO); return (active_efi_ops->rt_ok()); } static inline int efi_get_table(struct uuid *uuid, void **ptr) { if (active_efi_ops->get_table == NULL) return (ENXIO); return (active_efi_ops->get_table(uuid, ptr)); } static inline int efi_copy_table(struct uuid *uuid, void **buf, size_t buf_len, size_t *table_len) { if (active_efi_ops->copy_table == NULL) return (ENXIO); return (active_efi_ops->copy_table(uuid, buf, buf_len, table_len)); } static inline int efi_get_time(struct efi_tm *tm) { if (active_efi_ops->get_time == NULL) return (ENXIO); return (active_efi_ops->get_time(tm)); } static inline int efi_get_time_capabilities(struct efi_tmcap *tmcap) { if (active_efi_ops->get_time_capabilities == NULL) return (ENXIO); return (active_efi_ops->get_time_capabilities(tmcap)); } static inline int efi_reset_system(enum efi_reset type) { if (active_efi_ops->reset_system == NULL) return (ENXIO); return (active_efi_ops->reset_system(type)); } static inline int efi_set_time(struct efi_tm *tm) { if (active_efi_ops->set_time == NULL) return (ENXIO); return (active_efi_ops->set_time(tm)); } +static inline int efi_get_waketime(uint8_t *enabled, uint8_t *pending, + struct efi_tm *tm) +{ + if (active_efi_ops->get_waketime == NULL) + return (ENXIO); + return (active_efi_ops->get_waketime(enabled, pending, tm)); +} + +static inline int efi_set_waketime(uint8_t enable, struct efi_tm *tm) +{ + if (active_efi_ops->set_waketime == NULL) + return (ENXIO); + return (active_efi_ops->set_waketime(enable, tm)); +} + static inline int efi_var_get(uint16_t *name, struct uuid *vendor, uint32_t *attrib, size_t *datasize, void *data) { if (active_efi_ops->var_get == NULL) return (ENXIO); return (active_efi_ops->var_get(name, vendor, attrib, datasize, data)); } static inline int efi_var_nextname(size_t *namesize, uint16_t *name, struct uuid *vendor) { if (active_efi_ops->var_nextname == NULL) return (ENXIO); return (active_efi_ops->var_nextname(namesize, name, vendor)); } static inline int efi_var_set(uint16_t *name, struct uuid *vendor, uint32_t attrib, size_t datasize, void *data) { if (active_efi_ops->var_set == NULL) return (ENXIO); return (active_efi_ops->var_set(name, vendor, attrib, datasize, data)); } int efi_status_to_errno(efi_status status); #endif /* _KERNEL */ #endif /* _SYS_EFI_H_ */ diff --git a/sys/sys/efiio.h b/sys/sys/efiio.h index 803aed6a965e..a5f0145127e3 100644 --- a/sys/sys/efiio.h +++ b/sys/sys/efiio.h @@ -1,60 +1,69 @@ /*- * Copyright (c) 2016 Netflix, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer * in this position and unchanged. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _SYS_EFIIO_H_ #define _SYS_EFIIO_H_ #include #include #include struct efi_get_table_ioc { void *buf; /* Pointer to userspace buffer */ struct uuid uuid; /* UUID to look up */ size_t table_len; /* Table size */ size_t buf_len; /* Size of the buffer */ }; struct efi_var_ioc { efi_char *name; /* User pointer to name, in wide chars */ size_t namesize; /* Number of wide characters in name */ struct uuid vendor; /* Vendor's UUID for variable */ uint32_t attrib; /* Attributes */ void *data; /* User pointer to the data */ size_t datasize; /* Number of *bytes* in the data */ }; +struct efi_waketime_ioc +{ + struct efi_tm waketime; + uint8_t enabled; + uint8_t pending; +}; + #define EFIIOC_GET_TABLE _IOWR('E', 1, struct efi_get_table_ioc) #define EFIIOC_GET_TIME _IOR('E', 2, struct efi_tm) #define EFIIOC_SET_TIME _IOW('E', 3, struct efi_tm) #define EFIIOC_VAR_GET _IOWR('E', 4, struct efi_var_ioc) #define EFIIOC_VAR_NEXT _IOWR('E', 5, struct efi_var_ioc) #define EFIIOC_VAR_SET _IOWR('E', 6, struct efi_var_ioc) +#define EFIIOC_GET_WAKETIME _IOR('E', 7, struct efi_waketime_ioc) +#define EFIIOC_SET_WAKETIME _IOW('E', 8, struct efi_waketime_ioc) #endif /* _SYS_EFIIO_H_ */