1 /*- 2 * Copyright (c) 2004 Marcel Moolenaar 3 * Copyright (c) 2001 Doug Rabson 4 * Copyright (c) 2016, 2018 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Konstantin Belousov 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #include "opt_acpi.h" 34 35 #include <sys/param.h> 36 #include <sys/efi.h> 37 #include <sys/eventhandler.h> 38 #include <sys/kernel.h> 39 #include <sys/linker.h> 40 #include <sys/lock.h> 41 #include <sys/malloc.h> 42 #include <sys/module.h> 43 #include <sys/msan.h> 44 #include <sys/mutex.h> 45 #include <sys/clock.h> 46 #include <sys/proc.h> 47 #include <sys/reboot.h> 48 #include <sys/rwlock.h> 49 #include <sys/sched.h> 50 #include <sys/sysctl.h> 51 #include <sys/systm.h> 52 #include <sys/uio.h> 53 #include <sys/vmmeter.h> 54 55 #include <machine/fpu.h> 56 #include <machine/efi.h> 57 #include <machine/metadata.h> 58 #include <machine/vmparam.h> 59 60 #include <vm/vm.h> 61 #include <vm/pmap.h> 62 #include <vm/vm_map.h> 63 64 #ifdef DEV_ACPI 65 #include <contrib/dev/acpica/include/acpi.h> 66 #endif 67 68 #define EFI_TABLE_ALLOC_MAX 0x800000 69 70 static struct efi_systbl *efi_systbl; 71 static eventhandler_tag efi_shutdown_tag; 72 /* 73 * The following pointers point to tables in the EFI runtime service data pages. 74 * Care should be taken to make sure that we've properly entered the EFI runtime 75 * environment (efi_enter()) before dereferencing them. 76 */ 77 static struct efi_cfgtbl *efi_cfgtbl; 78 static struct efi_rt *efi_runtime; 79 80 static int efi_status2err[25] = { 81 0, /* EFI_SUCCESS */ 82 ENOEXEC, /* EFI_LOAD_ERROR */ 83 EINVAL, /* EFI_INVALID_PARAMETER */ 84 ENOSYS, /* EFI_UNSUPPORTED */ 85 EMSGSIZE, /* EFI_BAD_BUFFER_SIZE */ 86 EOVERFLOW, /* EFI_BUFFER_TOO_SMALL */ 87 EBUSY, /* EFI_NOT_READY */ 88 EIO, /* EFI_DEVICE_ERROR */ 89 EROFS, /* EFI_WRITE_PROTECTED */ 90 EAGAIN, /* EFI_OUT_OF_RESOURCES */ 91 EIO, /* EFI_VOLUME_CORRUPTED */ 92 ENOSPC, /* EFI_VOLUME_FULL */ 93 ENXIO, /* EFI_NO_MEDIA */ 94 ESTALE, /* EFI_MEDIA_CHANGED */ 95 ENOENT, /* EFI_NOT_FOUND */ 96 EACCES, /* EFI_ACCESS_DENIED */ 97 ETIMEDOUT, /* EFI_NO_RESPONSE */ 98 EADDRNOTAVAIL, /* EFI_NO_MAPPING */ 99 ETIMEDOUT, /* EFI_TIMEOUT */ 100 EDOOFUS, /* EFI_NOT_STARTED */ 101 EALREADY, /* EFI_ALREADY_STARTED */ 102 ECANCELED, /* EFI_ABORTED */ 103 EPROTO, /* EFI_ICMP_ERROR */ 104 EPROTO, /* EFI_TFTP_ERROR */ 105 EPROTO /* EFI_PROTOCOL_ERROR */ 106 }; 107 108 enum efi_table_type { 109 TYPE_ESRT = 0, 110 TYPE_PROP 111 }; 112 113 static int efi_enter(void); 114 static void efi_leave(void); 115 116 int 117 efi_status_to_errno(efi_status status) 118 { 119 u_long code; 120 121 code = status & 0x3ffffffffffffffful; 122 return (code < nitems(efi_status2err) ? efi_status2err[code] : EDOOFUS); 123 } 124 125 static struct mtx efi_lock; 126 static SYSCTL_NODE(_hw, OID_AUTO, efi, CTLFLAG_RWTUN | CTLFLAG_MPSAFE, NULL, 127 "EFI"); 128 static bool efi_poweroff = true; 129 SYSCTL_BOOL(_hw_efi, OID_AUTO, poweroff, CTLFLAG_RWTUN, &efi_poweroff, 0, 130 "If true, use EFI runtime services to power off in preference to ACPI"); 131 132 static bool 133 efi_is_in_map(struct efi_md *map, int ndesc, int descsz, vm_offset_t addr) 134 { 135 struct efi_md *p; 136 int i; 137 138 for (i = 0, p = map; i < ndesc; i++, p = efi_next_descriptor(p, 139 descsz)) { 140 if ((p->md_attr & EFI_MD_ATTR_RT) == 0) 141 continue; 142 143 if (addr >= p->md_virt && 144 addr < p->md_virt + p->md_pages * EFI_PAGE_SIZE) 145 return (true); 146 } 147 148 return (false); 149 } 150 151 static void 152 efi_shutdown_final(void *dummy __unused, int howto) 153 { 154 155 /* 156 * On some systems, ACPI S5 is missing or does not function properly. 157 * When present, shutdown via EFI Runtime Services instead, unless 158 * disabled. 159 */ 160 if ((howto & RB_POWEROFF) != 0 && efi_poweroff) 161 (void)efi_reset_system(EFI_RESET_SHUTDOWN); 162 } 163 164 static int 165 efi_init(void) 166 { 167 struct efi_map_header *efihdr; 168 struct efi_md *map; 169 struct efi_rt *rtdm; 170 caddr_t kmdp; 171 size_t efisz; 172 int ndesc, rt_disabled; 173 174 rt_disabled = 0; 175 TUNABLE_INT_FETCH("efi.rt.disabled", &rt_disabled); 176 if (rt_disabled == 1) 177 return (0); 178 mtx_init(&efi_lock, "efi", NULL, MTX_DEF); 179 180 if (efi_systbl_phys == 0) { 181 if (bootverbose) 182 printf("EFI systbl not available\n"); 183 return (0); 184 } 185 186 efi_systbl = (struct efi_systbl *)efi_phys_to_kva(efi_systbl_phys); 187 if (efi_systbl == NULL || efi_systbl->st_hdr.th_sig != EFI_SYSTBL_SIG) { 188 efi_systbl = NULL; 189 if (bootverbose) 190 printf("EFI systbl signature invalid\n"); 191 return (0); 192 } 193 efi_cfgtbl = (efi_systbl->st_cfgtbl == 0) ? NULL : 194 (struct efi_cfgtbl *)efi_systbl->st_cfgtbl; 195 if (efi_cfgtbl == NULL) { 196 if (bootverbose) 197 printf("EFI config table is not present\n"); 198 } 199 200 kmdp = preload_search_by_type("elf kernel"); 201 if (kmdp == NULL) 202 kmdp = preload_search_by_type("elf64 kernel"); 203 efihdr = (struct efi_map_header *)preload_search_info(kmdp, 204 MODINFO_METADATA | MODINFOMD_EFI_MAP); 205 if (efihdr == NULL) { 206 if (bootverbose) 207 printf("EFI map is not present\n"); 208 return (0); 209 } 210 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 211 map = (struct efi_md *)((uint8_t *)efihdr + efisz); 212 if (efihdr->descriptor_size == 0) 213 return (ENOMEM); 214 215 ndesc = efihdr->memory_size / efihdr->descriptor_size; 216 if (!efi_create_1t1_map(map, ndesc, efihdr->descriptor_size)) { 217 if (bootverbose) 218 printf("EFI cannot create runtime map\n"); 219 return (ENOMEM); 220 } 221 222 efi_runtime = (efi_systbl->st_rt == 0) ? NULL : 223 (struct efi_rt *)efi_systbl->st_rt; 224 if (efi_runtime == NULL) { 225 if (bootverbose) 226 printf("EFI runtime services table is not present\n"); 227 efi_destroy_1t1_map(); 228 return (ENXIO); 229 } 230 231 #if defined(__aarch64__) || defined(__amd64__) 232 /* 233 * Some UEFI implementations have multiple implementations of the 234 * RS->GetTime function. They switch from one we can only use early 235 * in the boot process to one valid as a RunTime service only when we 236 * call RS->SetVirtualAddressMap. As this is not always the case, e.g. 237 * with an old loader.efi, check if the RS->GetTime function is within 238 * the EFI map, and fail to attach if not. 239 */ 240 rtdm = (struct efi_rt *)efi_phys_to_kva((uintptr_t)efi_runtime); 241 if (rtdm == NULL || !efi_is_in_map(map, ndesc, efihdr->descriptor_size, 242 (vm_offset_t)rtdm->rt_gettime)) { 243 if (bootverbose) 244 printf( 245 "EFI runtime services table has an invalid pointer\n"); 246 efi_runtime = NULL; 247 efi_destroy_1t1_map(); 248 return (ENXIO); 249 } 250 #endif 251 252 /* 253 * We use SHUTDOWN_PRI_LAST - 1 to trigger after IPMI, but before ACPI. 254 */ 255 efi_shutdown_tag = EVENTHANDLER_REGISTER(shutdown_final, 256 efi_shutdown_final, NULL, SHUTDOWN_PRI_LAST - 1); 257 258 return (0); 259 } 260 261 static void 262 efi_uninit(void) 263 { 264 265 /* Most likely disabled by tunable */ 266 if (efi_runtime == NULL) 267 return; 268 if (efi_shutdown_tag != NULL) 269 EVENTHANDLER_DEREGISTER(shutdown_final, efi_shutdown_tag); 270 efi_destroy_1t1_map(); 271 272 efi_systbl = NULL; 273 efi_cfgtbl = NULL; 274 efi_runtime = NULL; 275 276 mtx_destroy(&efi_lock); 277 } 278 279 static int 280 rt_ok(void) 281 { 282 283 if (efi_runtime == NULL) 284 return (ENXIO); 285 return (0); 286 } 287 288 /* 289 * The fpu_kern_enter() call in allows firmware to use FPU, as 290 * mandated by the specification. It also enters a critical section, 291 * giving us neccessary protection against context switches. 292 */ 293 static int 294 efi_enter(void) 295 { 296 struct thread *td; 297 pmap_t curpmap; 298 int error; 299 300 if (efi_runtime == NULL) 301 return (ENXIO); 302 td = curthread; 303 curpmap = &td->td_proc->p_vmspace->vm_pmap; 304 PMAP_LOCK(curpmap); 305 mtx_lock(&efi_lock); 306 fpu_kern_enter(td, NULL, FPU_KERN_NOCTX); 307 error = efi_arch_enter(); 308 if (error != 0) { 309 fpu_kern_leave(td, NULL); 310 mtx_unlock(&efi_lock); 311 PMAP_UNLOCK(curpmap); 312 } else { 313 MPASS((td->td_pflags & TDP_EFIRT) == 0); 314 td->td_pflags |= TDP_EFIRT; 315 } 316 return (error); 317 } 318 319 static void 320 efi_leave(void) 321 { 322 struct thread *td; 323 pmap_t curpmap; 324 325 td = curthread; 326 MPASS((td->td_pflags & TDP_EFIRT) != 0); 327 td->td_pflags &= ~TDP_EFIRT; 328 329 efi_arch_leave(); 330 331 curpmap = &curproc->p_vmspace->vm_pmap; 332 fpu_kern_leave(td, NULL); 333 mtx_unlock(&efi_lock); 334 PMAP_UNLOCK(curpmap); 335 } 336 337 static int 338 get_table(struct uuid *uuid, void **ptr) 339 { 340 struct efi_cfgtbl *ct; 341 u_long count; 342 int error; 343 344 if (efi_cfgtbl == NULL || efi_systbl == NULL) 345 return (ENXIO); 346 error = efi_enter(); 347 if (error != 0) 348 return (error); 349 count = efi_systbl->st_entries; 350 ct = efi_cfgtbl; 351 while (count--) { 352 if (!bcmp(&ct->ct_uuid, uuid, sizeof(*uuid))) { 353 *ptr = ct->ct_data; 354 efi_leave(); 355 return (0); 356 } 357 ct++; 358 } 359 360 efi_leave(); 361 return (ENOENT); 362 } 363 364 static int 365 get_table_length(enum efi_table_type type, size_t *table_len, void **taddr) 366 { 367 switch (type) { 368 case TYPE_ESRT: 369 { 370 struct efi_esrt_table *esrt = NULL; 371 struct uuid uuid = EFI_TABLE_ESRT; 372 uint32_t fw_resource_count = 0; 373 size_t len = sizeof(*esrt); 374 int error; 375 void *buf; 376 377 error = efi_get_table(&uuid, (void **)&esrt); 378 if (error != 0) 379 return (error); 380 381 buf = malloc(len, M_TEMP, M_WAITOK); 382 error = physcopyout((vm_paddr_t)esrt, buf, len); 383 if (error != 0) { 384 free(buf, M_TEMP); 385 return (error); 386 } 387 388 /* Check ESRT version */ 389 if (((struct efi_esrt_table *)buf)->fw_resource_version != 390 ESRT_FIRMWARE_RESOURCE_VERSION) { 391 free(buf, M_TEMP); 392 return (ENODEV); 393 } 394 395 fw_resource_count = ((struct efi_esrt_table *)buf)-> 396 fw_resource_count; 397 if (fw_resource_count > EFI_TABLE_ALLOC_MAX / 398 sizeof(struct efi_esrt_entry_v1)) { 399 free(buf, M_TEMP); 400 return (ENOMEM); 401 } 402 403 len += fw_resource_count * sizeof(struct efi_esrt_entry_v1); 404 *table_len = len; 405 406 if (taddr != NULL) 407 *taddr = esrt; 408 free(buf, M_TEMP); 409 return (0); 410 } 411 case TYPE_PROP: 412 { 413 struct uuid uuid = EFI_PROPERTIES_TABLE; 414 struct efi_prop_table *prop; 415 size_t len = sizeof(*prop); 416 uint32_t prop_len; 417 int error; 418 void *buf; 419 420 error = efi_get_table(&uuid, (void **)&prop); 421 if (error != 0) 422 return (error); 423 424 buf = malloc(len, M_TEMP, M_WAITOK); 425 error = physcopyout((vm_paddr_t)prop, buf, len); 426 if (error != 0) { 427 free(buf, M_TEMP); 428 return (error); 429 } 430 431 prop_len = ((struct efi_prop_table *)buf)->length; 432 if (prop_len > EFI_TABLE_ALLOC_MAX) { 433 free(buf, M_TEMP); 434 return (ENOMEM); 435 } 436 *table_len = prop_len; 437 438 if (taddr != NULL) 439 *taddr = prop; 440 free(buf, M_TEMP); 441 return (0); 442 } 443 } 444 return (ENOENT); 445 } 446 447 static int 448 copy_table(struct uuid *uuid, void **buf, size_t buf_len, size_t *table_len) 449 { 450 static const struct known_table { 451 struct uuid uuid; 452 enum efi_table_type type; 453 } tables[] = { 454 { EFI_TABLE_ESRT, TYPE_ESRT }, 455 { EFI_PROPERTIES_TABLE, TYPE_PROP } 456 }; 457 size_t table_idx; 458 void *taddr; 459 int rc; 460 461 for (table_idx = 0; table_idx < nitems(tables); table_idx++) { 462 if (!bcmp(&tables[table_idx].uuid, uuid, sizeof(*uuid))) 463 break; 464 } 465 466 if (table_idx == nitems(tables)) 467 return (EINVAL); 468 469 rc = get_table_length(tables[table_idx].type, table_len, &taddr); 470 if (rc != 0) 471 return rc; 472 473 /* return table length to userspace */ 474 if (buf == NULL) 475 return (0); 476 477 *buf = malloc(*table_len, M_TEMP, M_WAITOK); 478 rc = physcopyout((vm_paddr_t)taddr, *buf, *table_len); 479 return (rc); 480 } 481 482 static int efi_rt_handle_faults = EFI_RT_HANDLE_FAULTS_DEFAULT; 483 SYSCTL_INT(_machdep, OID_AUTO, efi_rt_handle_faults, CTLFLAG_RWTUN, 484 &efi_rt_handle_faults, 0, 485 "Call EFI RT methods with fault handler wrapper around"); 486 487 static int 488 efi_rt_arch_call_nofault(struct efirt_callinfo *ec) 489 { 490 491 switch (ec->ec_argcnt) { 492 case 0: 493 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(void)) 494 ec->ec_fptr)(); 495 break; 496 case 1: 497 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t)) 498 ec->ec_fptr)(ec->ec_arg1); 499 break; 500 case 2: 501 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t, 502 register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2); 503 break; 504 case 3: 505 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t, 506 register_t, register_t))ec->ec_fptr)(ec->ec_arg1, 507 ec->ec_arg2, ec->ec_arg3); 508 break; 509 case 4: 510 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t, 511 register_t, register_t, register_t))ec->ec_fptr)( 512 ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4); 513 break; 514 case 5: 515 ec->ec_efi_status = ((register_t EFIABI_ATTR (*)(register_t, 516 register_t, register_t, register_t, register_t)) 517 ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, 518 ec->ec_arg4, ec->ec_arg5); 519 break; 520 default: 521 panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt); 522 } 523 524 return (0); 525 } 526 527 static int 528 efi_call(struct efirt_callinfo *ecp) 529 { 530 int error; 531 532 error = efi_enter(); 533 if (error != 0) 534 return (error); 535 error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) : 536 efi_rt_arch_call_nofault(ecp); 537 efi_leave(); 538 if (error == 0) 539 error = efi_status_to_errno(ecp->ec_efi_status); 540 else if (bootverbose) 541 printf("EFI %s call faulted, error %d\n", ecp->ec_name, error); 542 return (error); 543 } 544 545 #define EFI_RT_METHOD_PA(method) \ 546 ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t) \ 547 efi_runtime))->method) 548 549 static int 550 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap) 551 { 552 struct efirt_callinfo ec; 553 int error; 554 555 EFI_TIME_OWNED(); 556 if (efi_runtime == NULL) 557 return (ENXIO); 558 bzero(&ec, sizeof(ec)); 559 ec.ec_name = "rt_gettime"; 560 ec.ec_argcnt = 2; 561 ec.ec_arg1 = (uintptr_t)tm; 562 ec.ec_arg2 = (uintptr_t)tmcap; 563 ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime); 564 error = efi_call(&ec); 565 if (error == 0) 566 kmsan_mark(tm, sizeof(*tm), KMSAN_STATE_INITED); 567 return (error); 568 } 569 570 static int 571 get_time(struct efi_tm *tm) 572 { 573 struct efi_tmcap dummy; 574 int error; 575 576 if (efi_runtime == NULL) 577 return (ENXIO); 578 EFI_TIME_LOCK(); 579 /* 580 * UEFI spec states that the Capabilities argument to GetTime is 581 * optional, but some UEFI implementations choke when passed a NULL 582 * pointer. Pass a dummy efi_tmcap, even though we won't use it, 583 * to workaround such implementations. 584 */ 585 error = efi_get_time_locked(tm, &dummy); 586 EFI_TIME_UNLOCK(); 587 return (error); 588 } 589 590 static int 591 get_waketime(uint8_t *enabled, uint8_t *pending, struct efi_tm *tm) 592 { 593 struct efirt_callinfo ec; 594 int error; 595 #ifdef DEV_ACPI 596 UINT32 acpiRtcEnabled; 597 #endif 598 599 if (efi_runtime == NULL) 600 return (ENXIO); 601 602 EFI_TIME_LOCK(); 603 bzero(&ec, sizeof(ec)); 604 ec.ec_name = "rt_getwaketime"; 605 ec.ec_argcnt = 3; 606 ec.ec_arg1 = (uintptr_t)enabled; 607 ec.ec_arg2 = (uintptr_t)pending; 608 ec.ec_arg3 = (uintptr_t)tm; 609 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getwaketime); 610 error = efi_call(&ec); 611 EFI_TIME_UNLOCK(); 612 613 #ifdef DEV_ACPI 614 if (error == 0) { 615 error = AcpiReadBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE, 616 &acpiRtcEnabled); 617 if (ACPI_SUCCESS(error)) { 618 *enabled = *enabled && acpiRtcEnabled; 619 } else 620 error = EIO; 621 } 622 #endif 623 624 return (error); 625 } 626 627 static int 628 set_waketime(uint8_t enable, struct efi_tm *tm) 629 { 630 struct efirt_callinfo ec; 631 int error; 632 633 if (efi_runtime == NULL) 634 return (ENXIO); 635 636 EFI_TIME_LOCK(); 637 bzero(&ec, sizeof(ec)); 638 ec.ec_name = "rt_setwaketime"; 639 ec.ec_argcnt = 2; 640 ec.ec_arg1 = (uintptr_t)enable; 641 ec.ec_arg2 = (uintptr_t)tm; 642 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setwaketime); 643 error = efi_call(&ec); 644 EFI_TIME_UNLOCK(); 645 646 #ifdef DEV_ACPI 647 if (error == 0) { 648 error = AcpiWriteBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE, 649 (enable != 0) ? 1 : 0); 650 if (ACPI_FAILURE(error)) 651 error = EIO; 652 } 653 #endif 654 655 return (error); 656 } 657 658 static int 659 get_time_capabilities(struct efi_tmcap *tmcap) 660 { 661 struct efi_tm dummy; 662 int error; 663 664 if (efi_runtime == NULL) 665 return (ENXIO); 666 EFI_TIME_LOCK(); 667 error = efi_get_time_locked(&dummy, tmcap); 668 EFI_TIME_UNLOCK(); 669 return (error); 670 } 671 672 static int 673 reset_system(enum efi_reset type) 674 { 675 struct efirt_callinfo ec; 676 677 switch (type) { 678 case EFI_RESET_COLD: 679 case EFI_RESET_WARM: 680 case EFI_RESET_SHUTDOWN: 681 break; 682 default: 683 return (EINVAL); 684 } 685 if (efi_runtime == NULL) 686 return (ENXIO); 687 bzero(&ec, sizeof(ec)); 688 ec.ec_name = "rt_reset"; 689 ec.ec_argcnt = 4; 690 ec.ec_arg1 = (uintptr_t)type; 691 ec.ec_arg2 = (uintptr_t)0; 692 ec.ec_arg3 = (uintptr_t)0; 693 ec.ec_arg4 = (uintptr_t)NULL; 694 ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset); 695 return (efi_call(&ec)); 696 } 697 698 static int 699 efi_set_time_locked(struct efi_tm *tm) 700 { 701 struct efirt_callinfo ec; 702 703 EFI_TIME_OWNED(); 704 if (efi_runtime == NULL) 705 return (ENXIO); 706 bzero(&ec, sizeof(ec)); 707 ec.ec_name = "rt_settime"; 708 ec.ec_argcnt = 1; 709 ec.ec_arg1 = (uintptr_t)tm; 710 ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime); 711 return (efi_call(&ec)); 712 } 713 714 static int 715 set_time(struct efi_tm *tm) 716 { 717 int error; 718 719 if (efi_runtime == NULL) 720 return (ENXIO); 721 EFI_TIME_LOCK(); 722 error = efi_set_time_locked(tm); 723 EFI_TIME_UNLOCK(); 724 return (error); 725 } 726 727 static int 728 var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib, 729 size_t *datasize, void *data) 730 { 731 struct efirt_callinfo ec; 732 int error; 733 734 if (efi_runtime == NULL) 735 return (ENXIO); 736 bzero(&ec, sizeof(ec)); 737 ec.ec_argcnt = 5; 738 ec.ec_name = "rt_getvar"; 739 ec.ec_arg1 = (uintptr_t)name; 740 ec.ec_arg2 = (uintptr_t)vendor; 741 ec.ec_arg3 = (uintptr_t)attrib; 742 ec.ec_arg4 = (uintptr_t)datasize; 743 ec.ec_arg5 = (uintptr_t)data; 744 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar); 745 error = efi_call(&ec); 746 if (error == 0) 747 kmsan_mark(data, *datasize, KMSAN_STATE_INITED); 748 return (error); 749 } 750 751 static int 752 var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor) 753 { 754 struct efirt_callinfo ec; 755 int error; 756 757 if (efi_runtime == NULL) 758 return (ENXIO); 759 bzero(&ec, sizeof(ec)); 760 ec.ec_argcnt = 3; 761 ec.ec_name = "rt_scanvar"; 762 ec.ec_arg1 = (uintptr_t)namesize; 763 ec.ec_arg2 = (uintptr_t)name; 764 ec.ec_arg3 = (uintptr_t)vendor; 765 ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar); 766 error = efi_call(&ec); 767 if (error == 0) 768 kmsan_mark(name, *namesize, KMSAN_STATE_INITED); 769 return (error); 770 } 771 772 static int 773 var_set(efi_char *name, struct uuid *vendor, uint32_t attrib, 774 size_t datasize, void *data) 775 { 776 struct efirt_callinfo ec; 777 778 if (efi_runtime == NULL) 779 return (ENXIO); 780 bzero(&ec, sizeof(ec)); 781 ec.ec_argcnt = 5; 782 ec.ec_name = "rt_setvar"; 783 ec.ec_arg1 = (uintptr_t)name; 784 ec.ec_arg2 = (uintptr_t)vendor; 785 ec.ec_arg3 = (uintptr_t)attrib; 786 ec.ec_arg4 = (uintptr_t)datasize; 787 ec.ec_arg5 = (uintptr_t)data; 788 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar); 789 return (efi_call(&ec)); 790 } 791 792 const static struct efi_ops efi_ops = { 793 .rt_ok = rt_ok, 794 .get_table = get_table, 795 .copy_table = copy_table, 796 .get_time = get_time, 797 .get_time_capabilities = get_time_capabilities, 798 .reset_system = reset_system, 799 .set_time = set_time, 800 .get_waketime = get_waketime, 801 .set_waketime = set_waketime, 802 .var_get = var_get, 803 .var_nextname = var_nextname, 804 .var_set = var_set, 805 }; 806 const struct efi_ops *active_efi_ops = &efi_ops; 807 808 static int 809 efirt_modevents(module_t m, int event, void *arg __unused) 810 { 811 812 switch (event) { 813 case MOD_LOAD: 814 return (efi_init()); 815 816 case MOD_UNLOAD: 817 efi_uninit(); 818 return (0); 819 820 case MOD_SHUTDOWN: 821 return (0); 822 823 default: 824 return (EOPNOTSUPP); 825 } 826 } 827 828 static moduledata_t efirt_moddata = { 829 .name = "efirt", 830 .evhand = efirt_modevents, 831 .priv = NULL, 832 }; 833 /* After fpuinitstate, before efidev */ 834 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND); 835 MODULE_VERSION(efirt, 1); 836