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 (*)(void))ec->ec_fptr)(); 494 break; 495 case 1: 496 ec->ec_efi_status = ((register_t (*)(register_t))ec->ec_fptr) 497 (ec->ec_arg1); 498 break; 499 case 2: 500 ec->ec_efi_status = ((register_t (*)(register_t, register_t)) 501 ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2); 502 break; 503 case 3: 504 ec->ec_efi_status = ((register_t (*)(register_t, register_t, 505 register_t))ec->ec_fptr)(ec->ec_arg1, ec->ec_arg2, 506 ec->ec_arg3); 507 break; 508 case 4: 509 ec->ec_efi_status = ((register_t (*)(register_t, register_t, 510 register_t, register_t))ec->ec_fptr)(ec->ec_arg1, 511 ec->ec_arg2, ec->ec_arg3, ec->ec_arg4); 512 break; 513 case 5: 514 ec->ec_efi_status = ((register_t (*)(register_t, register_t, 515 register_t, register_t, register_t))ec->ec_fptr)( 516 ec->ec_arg1, ec->ec_arg2, ec->ec_arg3, ec->ec_arg4, 517 ec->ec_arg5); 518 break; 519 default: 520 panic("efi_rt_arch_call: %d args", (int)ec->ec_argcnt); 521 } 522 523 return (0); 524 } 525 526 static int 527 efi_call(struct efirt_callinfo *ecp) 528 { 529 int error; 530 531 error = efi_enter(); 532 if (error != 0) 533 return (error); 534 error = efi_rt_handle_faults ? efi_rt_arch_call(ecp) : 535 efi_rt_arch_call_nofault(ecp); 536 efi_leave(); 537 if (error == 0) 538 error = efi_status_to_errno(ecp->ec_efi_status); 539 else if (bootverbose) 540 printf("EFI %s call faulted, error %d\n", ecp->ec_name, error); 541 return (error); 542 } 543 544 #define EFI_RT_METHOD_PA(method) \ 545 ((uintptr_t)((struct efi_rt *)efi_phys_to_kva((uintptr_t) \ 546 efi_runtime))->method) 547 548 static int 549 efi_get_time_locked(struct efi_tm *tm, struct efi_tmcap *tmcap) 550 { 551 struct efirt_callinfo ec; 552 int error; 553 554 EFI_TIME_OWNED(); 555 if (efi_runtime == NULL) 556 return (ENXIO); 557 bzero(&ec, sizeof(ec)); 558 ec.ec_name = "rt_gettime"; 559 ec.ec_argcnt = 2; 560 ec.ec_arg1 = (uintptr_t)tm; 561 ec.ec_arg2 = (uintptr_t)tmcap; 562 ec.ec_fptr = EFI_RT_METHOD_PA(rt_gettime); 563 error = efi_call(&ec); 564 if (error == 0) 565 kmsan_mark(tm, sizeof(*tm), KMSAN_STATE_INITED); 566 return (error); 567 } 568 569 static int 570 get_time(struct efi_tm *tm) 571 { 572 struct efi_tmcap dummy; 573 int error; 574 575 if (efi_runtime == NULL) 576 return (ENXIO); 577 EFI_TIME_LOCK(); 578 /* 579 * UEFI spec states that the Capabilities argument to GetTime is 580 * optional, but some UEFI implementations choke when passed a NULL 581 * pointer. Pass a dummy efi_tmcap, even though we won't use it, 582 * to workaround such implementations. 583 */ 584 error = efi_get_time_locked(tm, &dummy); 585 EFI_TIME_UNLOCK(); 586 return (error); 587 } 588 589 static int 590 get_waketime(uint8_t *enabled, uint8_t *pending, struct efi_tm *tm) 591 { 592 struct efirt_callinfo ec; 593 int error; 594 #ifdef DEV_ACPI 595 UINT32 acpiRtcEnabled; 596 #endif 597 598 if (efi_runtime == NULL) 599 return (ENXIO); 600 601 EFI_TIME_LOCK(); 602 bzero(&ec, sizeof(ec)); 603 ec.ec_name = "rt_getwaketime"; 604 ec.ec_argcnt = 3; 605 ec.ec_arg1 = (uintptr_t)enabled; 606 ec.ec_arg2 = (uintptr_t)pending; 607 ec.ec_arg3 = (uintptr_t)tm; 608 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getwaketime); 609 error = efi_call(&ec); 610 EFI_TIME_UNLOCK(); 611 612 #ifdef DEV_ACPI 613 if (error == 0) { 614 error = AcpiReadBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE, 615 &acpiRtcEnabled); 616 if (ACPI_SUCCESS(error)) { 617 *enabled = *enabled && acpiRtcEnabled; 618 } else 619 error = EIO; 620 } 621 #endif 622 623 return (error); 624 } 625 626 static int 627 set_waketime(uint8_t enable, struct efi_tm *tm) 628 { 629 struct efirt_callinfo ec; 630 int error; 631 632 if (efi_runtime == NULL) 633 return (ENXIO); 634 635 EFI_TIME_LOCK(); 636 bzero(&ec, sizeof(ec)); 637 ec.ec_name = "rt_setwaketime"; 638 ec.ec_argcnt = 2; 639 ec.ec_arg1 = (uintptr_t)enable; 640 ec.ec_arg2 = (uintptr_t)tm; 641 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setwaketime); 642 error = efi_call(&ec); 643 EFI_TIME_UNLOCK(); 644 645 #ifdef DEV_ACPI 646 if (error == 0) { 647 error = AcpiWriteBitRegister(ACPI_BITREG_RT_CLOCK_ENABLE, 648 (enable != 0) ? 1 : 0); 649 if (ACPI_FAILURE(error)) 650 error = EIO; 651 } 652 #endif 653 654 return (error); 655 } 656 657 static int 658 get_time_capabilities(struct efi_tmcap *tmcap) 659 { 660 struct efi_tm dummy; 661 int error; 662 663 if (efi_runtime == NULL) 664 return (ENXIO); 665 EFI_TIME_LOCK(); 666 error = efi_get_time_locked(&dummy, tmcap); 667 EFI_TIME_UNLOCK(); 668 return (error); 669 } 670 671 static int 672 reset_system(enum efi_reset type) 673 { 674 struct efirt_callinfo ec; 675 676 switch (type) { 677 case EFI_RESET_COLD: 678 case EFI_RESET_WARM: 679 case EFI_RESET_SHUTDOWN: 680 break; 681 default: 682 return (EINVAL); 683 } 684 if (efi_runtime == NULL) 685 return (ENXIO); 686 bzero(&ec, sizeof(ec)); 687 ec.ec_name = "rt_reset"; 688 ec.ec_argcnt = 4; 689 ec.ec_arg1 = (uintptr_t)type; 690 ec.ec_arg2 = (uintptr_t)0; 691 ec.ec_arg3 = (uintptr_t)0; 692 ec.ec_arg4 = (uintptr_t)NULL; 693 ec.ec_fptr = EFI_RT_METHOD_PA(rt_reset); 694 return (efi_call(&ec)); 695 } 696 697 static int 698 efi_set_time_locked(struct efi_tm *tm) 699 { 700 struct efirt_callinfo ec; 701 702 EFI_TIME_OWNED(); 703 if (efi_runtime == NULL) 704 return (ENXIO); 705 bzero(&ec, sizeof(ec)); 706 ec.ec_name = "rt_settime"; 707 ec.ec_argcnt = 1; 708 ec.ec_arg1 = (uintptr_t)tm; 709 ec.ec_fptr = EFI_RT_METHOD_PA(rt_settime); 710 return (efi_call(&ec)); 711 } 712 713 static int 714 set_time(struct efi_tm *tm) 715 { 716 int error; 717 718 if (efi_runtime == NULL) 719 return (ENXIO); 720 EFI_TIME_LOCK(); 721 error = efi_set_time_locked(tm); 722 EFI_TIME_UNLOCK(); 723 return (error); 724 } 725 726 static int 727 var_get(efi_char *name, struct uuid *vendor, uint32_t *attrib, 728 size_t *datasize, void *data) 729 { 730 struct efirt_callinfo ec; 731 int error; 732 733 if (efi_runtime == NULL) 734 return (ENXIO); 735 bzero(&ec, sizeof(ec)); 736 ec.ec_argcnt = 5; 737 ec.ec_name = "rt_getvar"; 738 ec.ec_arg1 = (uintptr_t)name; 739 ec.ec_arg2 = (uintptr_t)vendor; 740 ec.ec_arg3 = (uintptr_t)attrib; 741 ec.ec_arg4 = (uintptr_t)datasize; 742 ec.ec_arg5 = (uintptr_t)data; 743 ec.ec_fptr = EFI_RT_METHOD_PA(rt_getvar); 744 error = efi_call(&ec); 745 if (error == 0) 746 kmsan_mark(data, *datasize, KMSAN_STATE_INITED); 747 return (error); 748 } 749 750 static int 751 var_nextname(size_t *namesize, efi_char *name, struct uuid *vendor) 752 { 753 struct efirt_callinfo ec; 754 int error; 755 756 if (efi_runtime == NULL) 757 return (ENXIO); 758 bzero(&ec, sizeof(ec)); 759 ec.ec_argcnt = 3; 760 ec.ec_name = "rt_scanvar"; 761 ec.ec_arg1 = (uintptr_t)namesize; 762 ec.ec_arg2 = (uintptr_t)name; 763 ec.ec_arg3 = (uintptr_t)vendor; 764 ec.ec_fptr = EFI_RT_METHOD_PA(rt_scanvar); 765 error = efi_call(&ec); 766 if (error == 0) 767 kmsan_mark(name, *namesize, KMSAN_STATE_INITED); 768 return (error); 769 } 770 771 static int 772 var_set(efi_char *name, struct uuid *vendor, uint32_t attrib, 773 size_t datasize, void *data) 774 { 775 struct efirt_callinfo ec; 776 777 if (efi_runtime == NULL) 778 return (ENXIO); 779 bzero(&ec, sizeof(ec)); 780 ec.ec_argcnt = 5; 781 ec.ec_name = "rt_setvar"; 782 ec.ec_arg1 = (uintptr_t)name; 783 ec.ec_arg2 = (uintptr_t)vendor; 784 ec.ec_arg3 = (uintptr_t)attrib; 785 ec.ec_arg4 = (uintptr_t)datasize; 786 ec.ec_arg5 = (uintptr_t)data; 787 ec.ec_fptr = EFI_RT_METHOD_PA(rt_setvar); 788 return (efi_call(&ec)); 789 } 790 791 const static struct efi_ops efi_ops = { 792 .rt_ok = rt_ok, 793 .get_table = get_table, 794 .copy_table = copy_table, 795 .get_time = get_time, 796 .get_time_capabilities = get_time_capabilities, 797 .reset_system = reset_system, 798 .set_time = set_time, 799 .get_waketime = get_waketime, 800 .set_waketime = set_waketime, 801 .var_get = var_get, 802 .var_nextname = var_nextname, 803 .var_set = var_set, 804 }; 805 const struct efi_ops *active_efi_ops = &efi_ops; 806 807 static int 808 efirt_modevents(module_t m, int event, void *arg __unused) 809 { 810 811 switch (event) { 812 case MOD_LOAD: 813 return (efi_init()); 814 815 case MOD_UNLOAD: 816 efi_uninit(); 817 return (0); 818 819 case MOD_SHUTDOWN: 820 return (0); 821 822 default: 823 return (EOPNOTSUPP); 824 } 825 } 826 827 static moduledata_t efirt_moddata = { 828 .name = "efirt", 829 .evhand = efirt_modevents, 830 .priv = NULL, 831 }; 832 /* After fpuinitstate, before efidev */ 833 DECLARE_MODULE(efirt, efirt_moddata, SI_SUB_DRIVERS, SI_ORDER_SECOND); 834 MODULE_VERSION(efirt, 1); 835