diff --git a/sys/dev/efidev/efidev.c b/sys/dev/efidev/efidev.c index 14712cf3c7bf..4c3570969e23 100644 --- a/sys/dev/efidev/efidev.c +++ b/sys/dev/efidev/efidev.c @@ -1,247 +1,247 @@ /*- * 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 #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, + error = efi_copy_table((efi_guid_t *)&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 9523ffc7f386..c5deb273c076 100644 --- a/sys/dev/efidev/efirt.c +++ b/sys/dev/efidev/efirt.c @@ -1,840 +1,840 @@ /*- * 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 #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; 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"); extern int print_efirt_faults; SYSCTL_INT(_hw_efi, OID_AUTO, print_faults, CTLFLAG_RWTUN, &print_efirt_faults, 0, "Print fault information upon trap from EFIRT calls: " "0 - never, 1 - once, 2 - always"); extern u_long cnt_efirt_faults; SYSCTL_ULONG(_hw_efi, OID_AUTO, total_faults, CTLFLAG_RD, &cnt_efirt_faults, 0, "Total number of faults that occurred during EFIRT calls"); 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; 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"); } efihdr = (struct efi_map_header *)preload_search_info(preload_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); } /* * The fpu_kern_enter() call in allows firmware to use FPU, as * mandated by the specification. It also enters a critical section, * giving us neccessary protection against context switches. */ 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); } else { MPASS((td->td_pflags & TDP_EFIRT) == 0); td->td_pflags |= TDP_EFIRT; } return (error); } static void efi_leave(void) { struct thread *td; pmap_t curpmap; td = curthread; MPASS((td->td_pflags & TDP_EFIRT) != 0); td->td_pflags &= ~TDP_EFIRT; efi_arch_leave(); curpmap = &curproc->p_vmspace->vm_pmap; fpu_kern_leave(td, NULL); mtx_unlock(&efi_lock); PMAP_UNLOCK(curpmap); } static int -get_table(struct uuid *uuid, void **ptr) +get_table(efi_guid_t *guid, 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))) { + if (!bcmp(&ct->ct_guid, guid, sizeof(*guid))) { *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; + efi_guid_t guid = 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); + error = efi_get_table(&guid, (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; + efi_guid_t guid = 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); + error = efi_get_table(&guid, (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) +copy_table(efi_guid_t *guid, void **buf, size_t buf_len, size_t *table_len) { static const struct known_table { - struct uuid uuid; + efi_guid_t guid; 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))) + if (!bcmp(&tables[table_idx].guid, guid, sizeof(*guid))) 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 EFIABI_ATTR (*)(void)) ec->ec_fptr)(); break; case 1: ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t)) ec->ec_fptr)(ec->ec_arg1); break; case 2: ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t, register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2); break; case 3: ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(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 EFIABI_ATTR (*)(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 EFIABI_ATTR (*)(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/dev/ipmi/ipmi_smbios.c b/sys/dev/ipmi/ipmi_smbios.c index f9fc958d9739..29aa74127041 100644 --- a/sys/dev/ipmi/ipmi_smbios.c +++ b/sys/dev/ipmi/ipmi_smbios.c @@ -1,253 +1,252 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2006 IronPort Systems Inc. * 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 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 #include #include #include #include #include #include #include #include #include #if defined(__amd64__) || defined(__i386__) #include #endif #include #ifdef LOCAL_MODULE #include #include #else #include #include #endif struct ipmi_entry { uint8_t type; uint8_t length; uint16_t handle; uint8_t interface_type; uint8_t spec_revision; uint8_t i2c_slave_address; uint8_t NV_storage_device_address; uint64_t base_address; uint8_t base_address_modifier; uint8_t interrupt_number; }; /* Fields in the base_address field of an IPMI entry. */ #define IPMI_BAR_MODE(ba) ((ba) & 0x0000000000000001) #define IPMI_BAR_ADDR(ba) ((ba) & 0xfffffffffffffffe) /* Fields in the base_address_modifier field of an IPMI entry. */ #define IPMI_BAM_IRQ_TRIGGER 0x01 #define IPMI_BAM_IRQ_POLARITY 0x02 #define IPMI_BAM_IRQ_VALID 0x08 #define IPMI_BAM_ADDR_LSB(bam) (((bam) & 0x10) >> 4) #define IPMI_BAM_REG_SPACING(bam) (((bam) & 0xc0) >> 6) #define SPACING_8 0x0 #define SPACING_32 0x1 #define SPACING_16 0x2 static struct ipmi_get_info ipmi_info; static int ipmi_probed; static struct mtx ipmi_info_mtx; MTX_SYSINIT(ipmi_info, &ipmi_info_mtx, "ipmi info", MTX_DEF); static void ipmi_smbios_probe(struct ipmi_get_info *); static int smbios_cksum(struct smbios_eps *); static void smbios_ipmi_info(struct smbios_structure_header *, void *); static void smbios_ipmi_info(struct smbios_structure_header *h, void *arg) { struct ipmi_get_info *info; struct ipmi_entry *s; if (h->type != 38 || h->length < offsetof(struct ipmi_entry, interrupt_number)) return; s = (struct ipmi_entry *)h; info = arg; bzero(info, sizeof(struct ipmi_get_info)); switch (s->interface_type) { case KCS_MODE: case SMIC_MODE: case BT_MODE: info->address = IPMI_BAR_ADDR(s->base_address) | IPMI_BAM_ADDR_LSB(s->base_address_modifier); info->io_mode = IPMI_BAR_MODE(s->base_address); switch (IPMI_BAM_REG_SPACING(s->base_address_modifier)) { case SPACING_8: info->offset = 1; break; case SPACING_32: info->offset = 4; break; case SPACING_16: info->offset = 2; break; default: printf("SMBIOS: Invalid register spacing\n"); return; } break; case SSIF_MODE: if ((s->base_address & 0xffffffffffffff00) != 0) { printf("SMBIOS: Invalid SSIF SMBus address, using BMC I2C slave address instead\n"); info->address = s->i2c_slave_address; break; } info->address = IPMI_BAR_ADDR(s->base_address); break; default: return; } if (s->length > offsetof(struct ipmi_entry, interrupt_number)) { if (s->interrupt_number > 15) printf("SMBIOS: Non-ISA IRQ %d for IPMI\n", s->interrupt_number); else info->irq = s->interrupt_number; } info->iface_type = s->interface_type; } /* * Walk the SMBIOS table looking for an IPMI (type 38) entry. If we find * one, return the parsed data in the passed in ipmi_get_info structure and * return true. If we don't find one, return false. */ static void ipmi_smbios_probe(struct ipmi_get_info *info) { #ifdef ARCH_MAY_USE_EFI - struct uuid efi_smbios; + efi_guid_t efi_smbios = EFI_TABLE_SMBIOS; void *addr_efi; #endif struct smbios_eps *header; void *table; u_int32_t addr; addr = 0; bzero(info, sizeof(struct ipmi_get_info)); #ifdef ARCH_MAY_USE_EFI - efi_smbios = (struct uuid)EFI_TABLE_SMBIOS; if (!efi_get_table(&efi_smbios, &addr_efi)) addr = (vm_paddr_t)addr_efi; #endif #if defined(__amd64__) || defined(__i386__) if (addr == 0) /* Find the SMBIOS table header. */ addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN, SMBIOS_STEP, SMBIOS_OFF); #endif if (addr == 0) return; /* * Map the header. We first map a fixed size to get the actual * length and then map it a second time with the actual length so * we can verify the checksum. */ header = pmap_mapbios(addr, sizeof(struct smbios_eps)); table = pmap_mapbios(addr, header->length); pmap_unmapbios(header, sizeof(struct smbios_eps)); header = table; if (smbios_cksum(header) != 0) { pmap_unmapbios(header, header->length); return; } /* Now map the actual table and walk it looking for an IPMI entry. */ table = pmap_mapbios(header->structure_table_address, header->structure_table_length); smbios_walk_table(table, header->number_structures, header->structure_table_length, smbios_ipmi_info, info); /* Unmap everything. */ pmap_unmapbios(table, header->structure_table_length); pmap_unmapbios(header, header->length); } /* * Return the SMBIOS IPMI table entry info to the caller. If we haven't * searched the IPMI table yet, search it. Otherwise, return a cached * copy of the data. */ int ipmi_smbios_identify(struct ipmi_get_info *info) { mtx_lock(&ipmi_info_mtx); switch (ipmi_probed) { case 0: /* Need to probe the SMBIOS table. */ ipmi_probed++; mtx_unlock(&ipmi_info_mtx); ipmi_smbios_probe(&ipmi_info); mtx_lock(&ipmi_info_mtx); ipmi_probed++; wakeup(&ipmi_info); break; case 1: /* Another thread is currently probing the table, so wait. */ while (ipmi_probed == 1) msleep(&ipmi_info, &ipmi_info_mtx, 0, "ipmi info", 0); break; default: /* The cached data is available. */ break; } bcopy(&ipmi_info, info, sizeof(ipmi_info)); mtx_unlock(&ipmi_info_mtx); return (info->iface_type != 0); } static int smbios_cksum(struct smbios_eps *e) { u_int8_t *ptr; u_int8_t cksum; int i; ptr = (u_int8_t *)e; cksum = 0; for (i = 0; i < e->length; i++) { cksum += ptr[i]; } return (cksum); } diff --git a/sys/dev/smbios/smbios.c b/sys/dev/smbios/smbios.c index 469e5ab649b6..8f2b4a56b37b 100644 --- a/sys/dev/smbios/smbios.c +++ b/sys/dev/smbios/smbios.c @@ -1,361 +1,361 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2003 Matthew N. Dodd * 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 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(__amd64__) || defined(__i386__) #include #endif #include /* * System Management BIOS Reference Specification, v2.4 Final * http://www.dmtf.org/standards/published_documents/DSP0134.pdf */ struct smbios_softc { device_t dev; union { struct smbios_eps * eps; struct smbios3_eps * eps3; }; bool is_eps3; }; static void smbios_identify (driver_t *, device_t); static int smbios_probe (device_t); static int smbios_attach (device_t); static int smbios_detach (device_t); static int smbios_modevent (module_t, int, void *); static int smbios_cksum (void *); static bool smbios_eps3 (void *); static void smbios_identify (driver_t *driver, device_t parent) { #ifdef ARCH_MAY_USE_EFI - struct uuid efi_smbios = EFI_TABLE_SMBIOS; - struct uuid efi_smbios3 = EFI_TABLE_SMBIOS3; + efi_guid_t efi_smbios = EFI_TABLE_SMBIOS; + efi_guid_t efi_smbios3 = EFI_TABLE_SMBIOS3; void *addr_efi; #endif struct smbios_eps *eps; struct smbios3_eps *eps3; void *ptr; device_t child; vm_paddr_t addr = 0; size_t map_size = sizeof(*eps); uint8_t length; if (!device_is_alive(parent)) return; #ifdef ARCH_MAY_USE_EFI if (!efi_get_table(&efi_smbios3, &addr_efi)) { addr = (vm_paddr_t)addr_efi; map_size = sizeof(*eps3); } else if (!efi_get_table(&efi_smbios, &addr_efi)) { addr = (vm_paddr_t)addr_efi; } #endif #if defined(__amd64__) || defined(__i386__) if (addr == 0) { addr = bios_sigsearch(SMBIOS_START, SMBIOS3_SIG, SMBIOS3_LEN, SMBIOS_STEP, SMBIOS_OFF); if (addr != 0) map_size = sizeof(*eps3); else addr = bios_sigsearch(SMBIOS_START, SMBIOS_SIG, SMBIOS_LEN, SMBIOS_STEP, SMBIOS_OFF); } #endif if (addr == 0) return; ptr = pmap_mapbios(addr, map_size); if (ptr == NULL) { printf("smbios: Unable to map memory.\n"); return; } if (map_size == sizeof(*eps3)) { eps3 = ptr; length = eps3->length; if (memcmp(eps3->anchor_string, SMBIOS3_SIG, SMBIOS3_LEN) != 0) goto corrupt_sig; } else { eps = ptr; length = eps->length; if (memcmp(eps->anchor_string, SMBIOS_SIG, SMBIOS_LEN) != 0) goto corrupt_sig; } if (length != map_size) { /* * SMBIOS v2.1 implementations might use 0x1e because the * standard was then erroneous. */ if (length == 0x1e && map_size == sizeof(*eps) && eps->major_version == 2 && eps->minor_version == 1) length = map_size; else { printf("smbios: %s-bit Entry Point: Invalid length: " "Got %hhu, expected %zu\n", map_size == sizeof(*eps3) ? "64" : "32", length, map_size); goto unmap_return; } } child = BUS_ADD_CHILD(parent, 5, "smbios", DEVICE_UNIT_ANY); device_set_driver(child, driver); /* smuggle the phys addr into probe and attach */ bus_set_resource(child, SYS_RES_MEMORY, 0, addr, length); device_set_desc(child, "System Management BIOS"); unmap_return: pmap_unmapbios(ptr, map_size); return; corrupt_sig: { const char *sig; const char *table_ver_str; size_t i, end; if (map_size == sizeof(*eps3)) { sig = eps3->anchor_string; table_ver_str = "64"; end = SMBIOS3_LEN; } else { sig = eps->anchor_string; table_ver_str = "32"; end = SMBIOS_LEN; } /* Space after ':' printed by the loop. */ printf("smbios: %s-bit Entry Point: Corrupt signature (hex):", table_ver_str); for (i = 0; i < end; ++i) printf(" %02hhx", sig[i]); printf("\n"); } goto unmap_return; } static int smbios_probe (device_t dev) { vm_paddr_t pa; vm_size_t size; void *va; int error; error = 0; pa = bus_get_resource_start(dev, SYS_RES_MEMORY, 0); size = bus_get_resource_count(dev, SYS_RES_MEMORY, 0); va = pmap_mapbios(pa, size); if (va == NULL) { device_printf(dev, "Unable to map memory.\n"); return (ENOMEM); } if (smbios_cksum(va)) { device_printf(dev, "SMBIOS checksum failed.\n"); error = ENXIO; } pmap_unmapbios(va, size); return (error); } static int smbios_attach (device_t dev) { struct smbios_softc *sc; void *va; vm_paddr_t pa; vm_size_t size; sc = device_get_softc(dev); sc->dev = dev; pa = bus_get_resource_start(dev, SYS_RES_MEMORY, 0); size = bus_get_resource_count(dev, SYS_RES_MEMORY, 0); va = pmap_mapbios(pa, size); if (va == NULL) { device_printf(dev, "Unable to map memory.\n"); return (ENOMEM); } sc->is_eps3 = smbios_eps3(va); if (sc->is_eps3) { sc->eps3 = va; device_printf(dev, "Entry point: v3 (64-bit), Version: %u.%u\n", sc->eps3->major_version, sc->eps3->minor_version); if (bootverbose) device_printf(dev, "Docrev: %u, Entry Point Revision: %u\n", sc->eps3->docrev, sc->eps3->entry_point_revision); } else { const struct smbios_eps *const eps = va; const uint8_t bcd = eps->BCD_revision; sc->eps = va; device_printf(dev, "Entry point: v2.1 (32-bit), Version: %u.%u", eps->major_version, eps->minor_version); if (bcd < LIBKERN_LEN_BCD2BIN && bcd2bin(bcd) != 0) printf(", BCD Revision: %u.%u\n", bcd2bin(bcd >> 4), bcd2bin(bcd & 0x0f)); else printf("\n"); if (bootverbose) device_printf(dev, "Entry Point Revision: %u\n", eps->entry_point_revision); } return (0); } static int smbios_detach (device_t dev) { struct smbios_softc *sc; vm_size_t size; void *va; sc = device_get_softc(dev); va = (sc->is_eps3 ? (void *)sc->eps3 : (void *)sc->eps); if (sc->is_eps3) va = sc->eps3; else va = sc->eps; size = bus_get_resource_count(dev, SYS_RES_MEMORY, 0); if (va != NULL) pmap_unmapbios(va, size); return (0); } static int smbios_modevent (module_t mod, int what, void *arg) { device_t * devs; int count; int i; switch (what) { case MOD_LOAD: break; case MOD_UNLOAD: devclass_get_devices(devclass_find("smbios"), &devs, &count); for (i = 0; i < count; i++) { device_delete_child(device_get_parent(devs[i]), devs[i]); } free(devs, M_TEMP); break; default: break; } return (0); } static device_method_t smbios_methods[] = { /* Device interface */ DEVMETHOD(device_identify, smbios_identify), DEVMETHOD(device_probe, smbios_probe), DEVMETHOD(device_attach, smbios_attach), DEVMETHOD(device_detach, smbios_detach), { 0, 0 } }; static driver_t smbios_driver = { "smbios", smbios_methods, sizeof(struct smbios_softc), }; DRIVER_MODULE(smbios, nexus, smbios_driver, smbios_modevent, NULL); #ifdef ARCH_MAY_USE_EFI MODULE_DEPEND(smbios, efirt, 1, 1, 1); #endif MODULE_VERSION(smbios, 1); static bool smbios_eps3 (void *v) { struct smbios3_eps *e; e = (struct smbios3_eps *)v; return (memcmp(e->anchor_string, SMBIOS3_SIG, SMBIOS3_LEN) == 0); } static int smbios_cksum (void *v) { const u_int8_t *ptr; u_int8_t cksum; u_int8_t length; int i; if (smbios_eps3(v)) { const struct smbios3_eps *eps3 = v; length = eps3->length; } else { const struct smbios_eps *eps = v; length = eps->length; } ptr = v; cksum = 0; for (i = 0; i < length; i++) cksum += ptr[i]; return (cksum); } diff --git a/sys/sys/efi.h b/sys/sys/efi.h index 4345b1636c2b..58e2c3b4df34 100644 --- a/sys/sys/efi.h +++ b/sys/sys/efi.h @@ -1,369 +1,381 @@ /*- * 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. */ #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}} + {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}} + {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}} + {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}} + {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}} + {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; +/* + * This type-puns to a struct uuid, but all the EDK2 headers use this variation, + * and we use it in the loader to specify GUIDs. We define it here so that we + * can use EDK2 definitions both places. + */ +typedef struct efi_guid { + uint32_t Data1; + uint16_t Data2; + uint16_t Data3; + uint8_t Data4[8]; +} efi_guid_t; /* Type puns with GUID and EFI_GUID */ + struct efi_cfgtbl { - struct uuid ct_uuid; + efi_guid_t ct_guid; 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_table)(efi_guid_t *, void **); + int (*copy_table)(efi_guid_t *, 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) +static inline int efi_get_table(efi_guid_t *guid, void **ptr) { if (active_efi_ops->get_table == NULL) return (ENXIO); - return (active_efi_ops->get_table(uuid, ptr)); + return (active_efi_ops->get_table(guid, ptr)); } -static inline int efi_copy_table(struct uuid *uuid, void **buf, +static inline int efi_copy_table(efi_guid_t *guid, 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)); + return (active_efi_ops->copy_table(guid, 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/usr.sbin/efitable/efitable.c b/usr.sbin/efitable/efitable.c index f283d467b01d..0eee72801592 100644 --- a/usr.sbin/efitable/efitable.c +++ b/usr.sbin/efitable/efitable.c @@ -1,248 +1,246 @@ /*- * Copyright (c) 2021 3mdeb Embedded Systems Consulting * * 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define TABLE_MAX_LEN 30 #define EFITABLE_XO_VERSION "1" static void efi_table_print_esrt(const void *data); static void efi_table_print_prop(const void *data); static void usage(void) __dead2; -typedef uuid_t efi_guid_t; /* Typedef for future efi_guid_t */ - struct efi_table_op { char name[TABLE_MAX_LEN]; void (*parse) (const void *); efi_guid_t guid; }; static const struct efi_table_op efi_table_ops[] = { { .name = "esrt", .parse = efi_table_print_esrt, .guid = EFI_TABLE_ESRT }, { .name = "prop", .parse = efi_table_print_prop, .guid = EFI_PROPERTIES_TABLE } }; int main(int argc, char **argv) { struct efi_get_table_ioc table = { .buf = NULL, .buf_len = 0, .table_len = 0 }; int efi_fd, ch, rc = 1, efi_idx = -1; bool got_table = false; bool table_set = false; bool uuid_set = false; struct option longopts[] = { { "uuid", required_argument, NULL, 'u' }, { "table", required_argument, NULL, 't' }, { NULL, 0, NULL, 0 } }; argc = xo_parse_args(argc, argv); if (argc < 0) exit(EXIT_FAILURE); while ((ch = getopt_long(argc, argv, "g:t:u:", longopts, NULL)) != -1) { switch (ch) { case 'g': case 'u': { char *uuid_str = optarg; struct uuid uuid; uint32_t status; /* * Note: we use the uuid parsing routine to parse the * guid strings. However, EFI defines a slightly * different structure to access them. We unify on * using a structure that's compatible with EDK2 * EFI_GUID structure. */ uuid_set = 1; uuid_from_string(uuid_str, &uuid, &status); if (status != uuid_s_ok) xo_errx(EX_DATAERR, "invalid UUID"); for (size_t n = 0; n < nitems(efi_table_ops); n++) { if (!memcmp(&uuid, &efi_table_ops[n].guid, sizeof(uuid))) { efi_idx = n; got_table = true; break; } } break; } case 't': { char *table_name = optarg; table_set = true; for (size_t n = 0; n < nitems(efi_table_ops); n++) { if (!strcmp(table_name, efi_table_ops[n].name)) { efi_idx = n; got_table = true; break; } } if (!got_table) xo_errx(EX_DATAERR, "unsupported efi table"); break; } default: usage(); } } if (!table_set && !uuid_set) xo_errx(EX_USAGE, "table is not set"); if (!got_table) xo_errx(EX_DATAERR, "unsupported table"); efi_fd = open("/dev/efi", O_RDWR); if (efi_fd < 0) xo_err(EX_OSFILE, "/dev/efi"); memcpy(&table.uuid, &efi_table_ops[efi_idx].guid, sizeof(struct uuid)); if (ioctl(efi_fd, EFIIOC_GET_TABLE, &table) == -1) xo_err(EX_OSERR, "EFIIOC_GET_TABLE (len == 0)"); table.buf = malloc(table.table_len); table.buf_len = table.table_len; if (ioctl(efi_fd, EFIIOC_GET_TABLE, &table) == -1) xo_err(EX_OSERR, "EFIIOC_GET_TABLE"); efi_table_ops[efi_idx].parse(table.buf); close(efi_fd); return (rc); } static void efi_table_print_esrt(const void *data) { const struct efi_esrt_entry_v1 *entries_v1; const struct efi_esrt_table *esrt; esrt = (const struct efi_esrt_table *)data; xo_set_version(EFITABLE_XO_VERSION); xo_open_container("esrt"); xo_emit("{Lwc:FwResourceCount}{:fw_resource_count/%u}\n", esrt->fw_resource_count); xo_emit("{Lwc:FwResourceCountMax}{:fw_resource_count_max/%u}\n", esrt->fw_resource_count_max); xo_emit("{Lwc:FwResourceVersion}{:fw_resource_version/%u}\n", esrt->fw_resource_version); xo_open_list("entries"); xo_emit("\nEntries:\n"); entries_v1 = (const void *) esrt->entries; for (uint32_t i = 0; i < esrt->fw_resource_count; i++) { const struct efi_esrt_entry_v1 *e = &entries_v1[i]; uint32_t status; char *uuid; uuid_to_string((const uuid_t *)&e->fw_class, &uuid, &status); if (status != uuid_s_ok) { xo_errx(EX_DATAERR, "uuid_to_string error"); } xo_open_instance("entries"); xo_emit("\n"); xo_emit("{P: }{Lwc:FwClass}{:fw_class/%s}\n", uuid); xo_emit("{P: }{Lwc:FwType}{:fw_type/%u}\n", e->fw_type); xo_emit("{P: }{Lwc:FwVersion}{:fw_version/%u}\n", e->fw_version); xo_emit("{P: }{Lwc:LowestSupportedFwVersion}" "{:lowest_supported_fw_version/%u}\n", e->lowest_supported_fw_version); xo_emit("{P: }{Lwc:CapsuleFlags}{:capsule_flags/%#x}\n", e->capsule_flags); xo_emit("{P: }{Lwc:LastAttemptVersion" "}{:last_attempt_version/%u}\n", e->last_attempt_version); xo_emit("{P: }{Lwc:LastAttemptStatus" "}{:last_attempt_status/%u}\n", e->last_attempt_status); xo_close_instance("entries"); } xo_close_list("entries"); xo_close_container("esrt"); if (xo_finish() < 0) xo_err(EX_IOERR, "stdout"); } static void efi_table_print_prop(const void *data) { const struct efi_prop_table *prop; prop = (const struct efi_prop_table *)data; xo_set_version(EFITABLE_XO_VERSION); xo_open_container("prop"); xo_emit("{Lwc:Version}{:version/%#x}\n", prop->version); xo_emit("{Lwc:Length}{:length/%u}\n", prop->length); xo_emit("{Lwc:MemoryProtectionAttribute}" "{:memory_protection_attribute/%#lx}\n", prop->memory_protection_attribute); xo_close_container("prop"); if (xo_finish() < 0) xo_err(EX_IOERR, "stdout"); } static void usage(void) { xo_error("usage: efitable [-g guid | -t name] [--libxo]\n"); exit(EX_USAGE); }