1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * efi.c - EFI subsystem 4 * 5 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com> 6 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com> 7 * Copyright (C) 2013 Tom Gundersen <teg@jklm.no> 8 * 9 * This code registers /sys/firmware/efi{,/efivars} when EFI is supported, 10 * allowing the efivarfs to be mounted or the efivars module to be loaded. 11 * The existence of /sys/firmware/efi may also be used by userspace to 12 * determine that the system supports EFI. 13 */ 14 15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 16 17 #include <linux/kobject.h> 18 #include <linux/module.h> 19 #include <linux/init.h> 20 #include <linux/debugfs.h> 21 #include <linux/device.h> 22 #include <linux/efi.h> 23 #include <linux/of.h> 24 #include <linux/initrd.h> 25 #include <linux/io.h> 26 #include <linux/kexec.h> 27 #include <linux/platform_device.h> 28 #include <linux/random.h> 29 #include <linux/reboot.h> 30 #include <linux/slab.h> 31 #include <linux/acpi.h> 32 #include <linux/ucs2_string.h> 33 #include <linux/memblock.h> 34 #include <linux/security.h> 35 #include <linux/notifier.h> 36 37 #include <asm/early_ioremap.h> 38 39 struct efi __read_mostly efi = { 40 .runtime_supported_mask = EFI_RT_SUPPORTED_ALL, 41 .acpi = EFI_INVALID_TABLE_ADDR, 42 .acpi20 = EFI_INVALID_TABLE_ADDR, 43 .smbios = EFI_INVALID_TABLE_ADDR, 44 .smbios3 = EFI_INVALID_TABLE_ADDR, 45 .esrt = EFI_INVALID_TABLE_ADDR, 46 .tpm_log = EFI_INVALID_TABLE_ADDR, 47 .tpm_final_log = EFI_INVALID_TABLE_ADDR, 48 .ovmf_debug_log = EFI_INVALID_TABLE_ADDR, 49 .mem_reserve = EFI_INVALID_TABLE_ADDR, 50 .mokvar_table = EFI_INVALID_TABLE_ADDR, 51 .coco_secret = EFI_INVALID_TABLE_ADDR, 52 .unaccepted = EFI_INVALID_TABLE_ADDR, 53 }; 54 EXPORT_SYMBOL(efi); 55 56 unsigned long __ro_after_init efi_rng_seed = EFI_INVALID_TABLE_ADDR; 57 static unsigned long __initdata rt_prop = EFI_INVALID_TABLE_ADDR; 58 static unsigned long __initdata initrd = EFI_INVALID_TABLE_ADDR; 59 60 extern unsigned long primary_display_table; 61 62 struct mm_struct efi_mm = { 63 .mm_mt = MTREE_INIT_EXT(mm_mt, MM_MT_FLAGS, efi_mm.mmap_lock), 64 .mm_users = ATOMIC_INIT(2), 65 .mm_count = ATOMIC_INIT(1), 66 .write_protect_seq = SEQCNT_ZERO(efi_mm.write_protect_seq), 67 MMAP_LOCK_INITIALIZER(efi_mm) 68 .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock), 69 .mmlist = LIST_HEAD_INIT(efi_mm.mmlist), 70 #ifdef CONFIG_SCHED_MM_CID 71 .mm_cid.lock = __RAW_SPIN_LOCK_UNLOCKED(efi_mm.mm_cid.lock), 72 #endif 73 .flexible_array = MM_STRUCT_FLEXIBLE_ARRAY_INIT, 74 }; 75 76 struct workqueue_struct *efi_rts_wq; 77 78 static bool disable_runtime = IS_ENABLED(CONFIG_EFI_DISABLE_RUNTIME); 79 static int __init setup_noefi(char *arg) 80 { 81 disable_runtime = true; 82 return 0; 83 } 84 early_param("noefi", setup_noefi); 85 86 bool efi_runtime_disabled(void) 87 { 88 return disable_runtime; 89 } 90 91 bool __pure __efi_soft_reserve_enabled(void) 92 { 93 return !efi_enabled(EFI_MEM_NO_SOFT_RESERVE); 94 } 95 96 static int __init parse_efi_cmdline(char *str) 97 { 98 if (!str) { 99 pr_warn("need at least one option\n"); 100 return -EINVAL; 101 } 102 103 if (parse_option_str(str, "debug")) 104 set_bit(EFI_DBG, &efi.flags); 105 106 if (parse_option_str(str, "noruntime")) 107 disable_runtime = true; 108 109 if (parse_option_str(str, "runtime")) 110 disable_runtime = false; 111 112 if (parse_option_str(str, "nosoftreserve")) 113 set_bit(EFI_MEM_NO_SOFT_RESERVE, &efi.flags); 114 115 return 0; 116 } 117 early_param("efi", parse_efi_cmdline); 118 119 struct kobject *efi_kobj; 120 121 /* 122 * Let's not leave out systab information that snuck into 123 * the efivars driver 124 * Note, do not add more fields in systab sysfs file as it breaks sysfs 125 * one value per file rule! 126 */ 127 static ssize_t systab_show(struct kobject *kobj, 128 const struct kobj_attribute *attr, char *buf) 129 { 130 int offset = 0; 131 132 if (!kobj || !buf) 133 return -EINVAL; 134 135 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR) 136 offset = sysfs_emit(buf, "ACPI20=0x%lx\n", efi.acpi20); 137 if (efi.acpi != EFI_INVALID_TABLE_ADDR) 138 offset += sysfs_emit_at(buf, offset, "ACPI=0x%lx\n", efi.acpi); 139 /* 140 * If both SMBIOS and SMBIOS3 entry points are implemented, the 141 * SMBIOS3 entry point shall be preferred, so we list it first to 142 * let applications stop parsing after the first match. 143 */ 144 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) 145 offset += sysfs_emit_at(buf, offset, "SMBIOS3=0x%lx\n", efi.smbios3); 146 if (efi.smbios != EFI_INVALID_TABLE_ADDR) 147 offset += sysfs_emit_at(buf, offset, "SMBIOS=0x%lx\n", efi.smbios); 148 149 return offset; 150 } 151 152 static const struct kobj_attribute efi_attr_systab = __KOBJ_ATTR_RO_MODE(systab, 0400); 153 154 static ssize_t fw_platform_size_show(struct kobject *kobj, 155 const struct kobj_attribute *attr, char *buf) 156 { 157 return sysfs_emit(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32); 158 } 159 160 extern __weak const struct kobj_attribute efi_attr_fw_vendor; 161 extern __weak const struct kobj_attribute efi_attr_runtime; 162 extern __weak const struct kobj_attribute efi_attr_config_table; 163 static const struct kobj_attribute efi_attr_fw_platform_size = 164 __KOBJ_ATTR_RO(fw_platform_size); 165 166 static const struct attribute *const efi_subsys_attrs[] = { 167 &efi_attr_systab.attr, 168 &efi_attr_fw_platform_size.attr, 169 &efi_attr_fw_vendor.attr, 170 &efi_attr_runtime.attr, 171 &efi_attr_config_table.attr, 172 NULL, 173 }; 174 175 umode_t __weak efi_attr_is_visible(struct kobject *kobj, 176 const struct attribute *attr, 177 int n) 178 { 179 return attr->mode; 180 } 181 182 static const struct attribute_group efi_subsys_attr_group = { 183 .attrs_const = efi_subsys_attrs, 184 .is_visible_const = efi_attr_is_visible, 185 }; 186 187 struct blocking_notifier_head efivar_ops_nh; 188 EXPORT_SYMBOL_GPL(efivar_ops_nh); 189 190 static struct efivars generic_efivars; 191 static struct efivar_operations generic_ops; 192 193 static bool generic_ops_supported(void) 194 { 195 unsigned long name_size; 196 efi_status_t status; 197 efi_char16_t name; 198 efi_guid_t guid; 199 200 name_size = sizeof(name); 201 202 if (!efi.get_next_variable) 203 return false; 204 status = efi.get_next_variable(&name_size, &name, &guid); 205 if (status == EFI_UNSUPPORTED) 206 return false; 207 208 return true; 209 } 210 211 static int generic_ops_register(void) 212 { 213 if (!generic_ops_supported()) 214 return 0; 215 216 generic_ops.get_variable = efi.get_variable; 217 generic_ops.get_next_variable = efi.get_next_variable; 218 generic_ops.query_variable_store = efi_query_variable_store; 219 generic_ops.query_variable_info = efi.query_variable_info; 220 221 if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE)) { 222 generic_ops.set_variable = efi.set_variable; 223 generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking; 224 } 225 return efivars_register(&generic_efivars, &generic_ops); 226 } 227 228 static void generic_ops_unregister(void) 229 { 230 if (!generic_ops.get_variable) 231 return; 232 233 efivars_unregister(&generic_efivars); 234 } 235 236 void efivars_generic_ops_register(void) 237 { 238 generic_ops_register(); 239 } 240 EXPORT_SYMBOL_GPL(efivars_generic_ops_register); 241 242 void efivars_generic_ops_unregister(void) 243 { 244 generic_ops_unregister(); 245 } 246 EXPORT_SYMBOL_GPL(efivars_generic_ops_unregister); 247 248 #ifdef CONFIG_EFI_CUSTOM_SSDT_OVERLAYS 249 #define EFIVAR_SSDT_NAME_MAX 16UL 250 static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata; 251 static int __init efivar_ssdt_setup(char *str) 252 { 253 int ret = security_locked_down(LOCKDOWN_ACPI_TABLES); 254 255 if (ret) 256 return ret; 257 258 if (strlen(str) < sizeof(efivar_ssdt)) 259 memcpy(efivar_ssdt, str, strlen(str)); 260 else 261 pr_warn("efivar_ssdt: name too long: %s\n", str); 262 return 1; 263 } 264 __setup("efivar_ssdt=", efivar_ssdt_setup); 265 266 static __init int efivar_ssdt_load(void) 267 { 268 unsigned long name_size = 256; 269 efi_char16_t *name = NULL; 270 efi_status_t status; 271 efi_guid_t guid; 272 int ret = 0; 273 274 if (!efivar_ssdt[0]) 275 return 0; 276 277 name = kzalloc(name_size, GFP_KERNEL); 278 if (!name) 279 return -ENOMEM; 280 281 for (;;) { 282 char utf8_name[EFIVAR_SSDT_NAME_MAX]; 283 unsigned long data_size = 0; 284 void *data; 285 int limit; 286 287 status = efi.get_next_variable(&name_size, name, &guid); 288 if (status == EFI_NOT_FOUND) { 289 break; 290 } else if (status == EFI_BUFFER_TOO_SMALL) { 291 efi_char16_t *name_tmp = 292 krealloc(name, name_size, GFP_KERNEL); 293 if (!name_tmp) { 294 ret = -ENOMEM; 295 goto out; 296 } 297 name = name_tmp; 298 continue; 299 } 300 301 limit = min(EFIVAR_SSDT_NAME_MAX, name_size); 302 ucs2_as_utf8(utf8_name, name, limit - 1); 303 if (strncmp(utf8_name, efivar_ssdt, limit) != 0) 304 continue; 305 306 pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt, &guid); 307 308 status = efi.get_variable(name, &guid, NULL, &data_size, NULL); 309 if (status != EFI_BUFFER_TOO_SMALL || !data_size) { 310 ret = -EIO; 311 goto out; 312 } 313 314 data = kmalloc(data_size, GFP_KERNEL); 315 if (!data) { 316 ret = -ENOMEM; 317 goto out; 318 } 319 320 status = efi.get_variable(name, &guid, NULL, &data_size, data); 321 if (status == EFI_SUCCESS) { 322 acpi_status acpi_ret = acpi_load_table(data, NULL); 323 if (ACPI_FAILURE(acpi_ret)) { 324 pr_err("efivar_ssdt: failed to load table: %u\n", 325 acpi_ret); 326 } else { 327 /* 328 * The @data will be in use by ACPI engine, 329 * do not free it! 330 */ 331 continue; 332 } 333 } else { 334 pr_err("efivar_ssdt: failed to get var data: 0x%lx\n", status); 335 } 336 kfree(data); 337 } 338 out: 339 kfree(name); 340 return ret; 341 } 342 #else 343 static inline int efivar_ssdt_load(void) { return 0; } 344 #endif 345 346 #ifdef CONFIG_DEBUG_FS 347 348 #define EFI_DEBUGFS_MAX_BLOBS 32 349 350 static struct debugfs_blob_wrapper debugfs_blob[EFI_DEBUGFS_MAX_BLOBS]; 351 352 static void __init efi_debugfs_init(void) 353 { 354 struct dentry *efi_debugfs; 355 efi_memory_desc_t *md; 356 char name[32]; 357 int type_count[EFI_BOOT_SERVICES_DATA + 1] = {}; 358 int i = 0; 359 360 efi_debugfs = debugfs_create_dir("efi", NULL); 361 if (IS_ERR(efi_debugfs)) 362 return; 363 364 for_each_efi_memory_desc(md) { 365 switch (md->type) { 366 case EFI_BOOT_SERVICES_CODE: 367 snprintf(name, sizeof(name), "boot_services_code%d", 368 type_count[md->type]++); 369 break; 370 case EFI_BOOT_SERVICES_DATA: 371 snprintf(name, sizeof(name), "boot_services_data%d", 372 type_count[md->type]++); 373 break; 374 default: 375 continue; 376 } 377 378 if (i >= EFI_DEBUGFS_MAX_BLOBS) { 379 pr_warn("More then %d EFI boot service segments, only showing first %d in debugfs\n", 380 EFI_DEBUGFS_MAX_BLOBS, EFI_DEBUGFS_MAX_BLOBS); 381 break; 382 } 383 384 debugfs_blob[i].size = md->num_pages << EFI_PAGE_SHIFT; 385 debugfs_blob[i].data = memremap(md->phys_addr, 386 debugfs_blob[i].size, 387 MEMREMAP_WB); 388 if (!debugfs_blob[i].data) 389 continue; 390 391 debugfs_create_blob(name, 0400, efi_debugfs, &debugfs_blob[i]); 392 i++; 393 } 394 } 395 #else 396 static inline void efi_debugfs_init(void) {} 397 #endif 398 399 static ssize_t efi_runtime_show(struct kobject *kobj, 400 const struct kobj_attribute *attr, 401 char *buf) 402 { 403 return sysfs_emit(buf, "%d\n", efi_enabled(EFI_RUNTIME_SERVICES)); 404 } 405 406 static ssize_t efi_runtime_store(struct kobject *kobj, 407 const struct kobj_attribute *attr, 408 const char *buf, size_t count) 409 { 410 int ret; 411 bool enable; 412 413 ret = kstrtobool(buf, &enable); 414 if (ret) 415 return ret; 416 417 if (efi_runtime_set_enable_flag(enable) != EFI_SUCCESS) { 418 pr_warn("unable to enable/disable efi runtime service\n"); 419 return -EAGAIN; 420 } 421 422 return count; 423 } 424 425 static const struct kobj_attribute efi_runtime_attr = 426 __KOBJ_ATTR(runtime_enable, 0644, efi_runtime_show, efi_runtime_store); 427 428 static int __init efipostcore_init(void) 429 { 430 if (!efi_enabled(EFI_RUNTIME_SERVICES)) 431 efi.runtime_supported_mask = 0; 432 433 if (efi.runtime_supported_mask) { 434 /* 435 * Since we process only one efi_runtime_service() at a time, an 436 * ordered workqueue (which creates only one execution context) 437 * should suffice for all our needs. 438 */ 439 efi_rts_wq = alloc_ordered_workqueue("efi_runtime", WQ_SYSFS); 440 if (!efi_rts_wq) { 441 pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n"); 442 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); 443 efi.runtime_supported_mask = 0; 444 } 445 } 446 return 0; 447 } 448 postcore_initcall(efipostcore_init); 449 450 /* 451 * We register the efi subsystem with the firmware subsystem and the 452 * efivars subsystem with the efi subsystem, if the system was booted with 453 * EFI. 454 */ 455 static int __init efisubsys_init(void) 456 { 457 int error; 458 459 if (!efi_enabled(EFI_BOOT)) 460 return 0; 461 462 if (efi_rt_services_supported(EFI_RT_SUPPORTED_TIME_SERVICES)) 463 platform_device_register_simple("rtc-efi", 0, NULL, 0); 464 465 /* We register the efi directory at /sys/firmware/efi */ 466 efi_kobj = kobject_create_and_add("efi", firmware_kobj); 467 if (!efi_kobj) { 468 pr_err("efi: Firmware registration failed.\n"); 469 error = -ENOMEM; 470 goto err_destroy_wq; 471 } 472 473 if (IS_ENABLED(CONFIG_PREEMPT_RT) && efi.runtime_supported_mask) { 474 if (sysfs_create_file(efi_kobj, &efi_runtime_attr.attr)) 475 pr_warn("unable to register efi dynamic sysfs interface\n"); 476 } 477 478 if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE | 479 EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME)) { 480 error = generic_ops_register(); 481 if (error) 482 goto err_put; 483 error = efivar_ssdt_load(); 484 if (error) 485 pr_err("efi: failed to load SSDT, error %d.\n", error); 486 platform_device_register_simple("efivars", 0, NULL, 0); 487 } 488 489 BLOCKING_INIT_NOTIFIER_HEAD(&efivar_ops_nh); 490 491 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group); 492 if (error) { 493 pr_err("efi: Sysfs attribute export failed with error %d.\n", 494 error); 495 goto err_unregister; 496 } 497 498 /* and the standard mountpoint for efivarfs */ 499 error = sysfs_create_mount_point(efi_kobj, "efivars"); 500 if (error) { 501 pr_err("efivars: Subsystem registration failed.\n"); 502 goto err_remove_group; 503 } 504 505 if (efi_enabled(EFI_DBG) && efi_enabled(EFI_PRESERVE_BS_REGIONS)) 506 efi_debugfs_init(); 507 508 #ifdef CONFIG_EFI_COCO_SECRET 509 if (efi.coco_secret != EFI_INVALID_TABLE_ADDR) 510 platform_device_register_simple("efi_secret", 0, NULL, 0); 511 #endif 512 513 if (IS_ENABLED(CONFIG_OVMF_DEBUG_LOG) && 514 efi.ovmf_debug_log != EFI_INVALID_TABLE_ADDR) 515 ovmf_log_probe(efi.ovmf_debug_log); 516 517 return 0; 518 519 err_remove_group: 520 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group); 521 err_unregister: 522 if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE | 523 EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME)) 524 generic_ops_unregister(); 525 err_put: 526 kobject_put(efi_kobj); 527 efi_kobj = NULL; 528 err_destroy_wq: 529 if (efi_rts_wq) 530 destroy_workqueue(efi_rts_wq); 531 532 return error; 533 } 534 535 subsys_initcall(efisubsys_init); 536 537 void __init efi_find_mirror(void) 538 { 539 efi_memory_desc_t *md; 540 u64 mirror_size = 0, total_size = 0; 541 542 if (!efi_enabled(EFI_MEMMAP)) 543 return; 544 545 for_each_efi_memory_desc(md) { 546 unsigned long long start = md->phys_addr; 547 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; 548 549 total_size += size; 550 if (md->attribute & EFI_MEMORY_MORE_RELIABLE) { 551 memblock_mark_mirror(start, size); 552 mirror_size += size; 553 } 554 } 555 if (mirror_size) 556 pr_info("Memory: %lldM/%lldM mirrored memory\n", 557 mirror_size>>20, total_size>>20); 558 } 559 560 /* 561 * Find the efi memory descriptor for a given physical address. Given a 562 * physical address, determine if it exists within an EFI Memory Map entry, 563 * and if so, populate the supplied memory descriptor with the appropriate 564 * data. 565 */ 566 int __efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md) 567 { 568 efi_memory_desc_t *md; 569 570 if (!efi_enabled(EFI_MEMMAP)) { 571 pr_err_once("EFI_MEMMAP is not enabled.\n"); 572 return -EINVAL; 573 } 574 575 if (!out_md) { 576 pr_err_once("out_md is null.\n"); 577 return -EINVAL; 578 } 579 580 for_each_efi_memory_desc(md) { 581 u64 size; 582 u64 end; 583 584 /* skip bogus entries (including empty ones) */ 585 if ((md->phys_addr & (EFI_PAGE_SIZE - 1)) || 586 (md->num_pages <= 0) || 587 (md->num_pages > (U64_MAX - md->phys_addr) >> EFI_PAGE_SHIFT)) 588 continue; 589 590 size = md->num_pages << EFI_PAGE_SHIFT; 591 end = md->phys_addr + size; 592 if (phys_addr >= md->phys_addr && phys_addr < end) { 593 memcpy(out_md, md, sizeof(*out_md)); 594 return 0; 595 } 596 } 597 return -ENOENT; 598 } 599 600 extern int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md) 601 __weak __alias(__efi_mem_desc_lookup); 602 EXPORT_SYMBOL_GPL(efi_mem_desc_lookup); 603 604 /* 605 * Calculate the highest address of an efi memory descriptor. 606 */ 607 u64 __init efi_mem_desc_end(efi_memory_desc_t *md) 608 { 609 u64 size = md->num_pages << EFI_PAGE_SHIFT; 610 u64 end = md->phys_addr + size; 611 return end; 612 } 613 614 void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {} 615 616 /** 617 * efi_mem_reserve - Reserve an EFI memory region 618 * @addr: Physical address to reserve 619 * @size: Size of reservation 620 * 621 * Mark a region as reserved from general kernel allocation and 622 * prevent it being released by efi_free_boot_services(). 623 * 624 * This function should be called drivers once they've parsed EFI 625 * configuration tables to figure out where their data lives, e.g. 626 * efi_esrt_init(). 627 */ 628 void __init efi_mem_reserve(phys_addr_t addr, u64 size) 629 { 630 /* efi_mem_reserve() does not work under Xen */ 631 if (WARN_ON_ONCE(efi_enabled(EFI_PARAVIRT))) 632 return; 633 634 if (!memblock_is_region_reserved(addr, size)) 635 memblock_reserve_kern(addr, size); 636 else 637 memblock_reserved_mark_kern(addr, size); 638 639 /* 640 * Some architectures (x86) reserve all boot services ranges 641 * until efi_free_boot_services() because of buggy firmware 642 * implementations. This means the above memblock_reserve() is 643 * superfluous on x86 and instead what it needs to do is 644 * ensure the @start, @size is not freed. 645 */ 646 efi_arch_mem_reserve(addr, size); 647 } 648 649 static const efi_config_table_type_t common_tables[] __initconst = { 650 {ACPI_20_TABLE_GUID, &efi.acpi20, "ACPI 2.0" }, 651 {ACPI_TABLE_GUID, &efi.acpi, "ACPI" }, 652 {SMBIOS_TABLE_GUID, &efi.smbios, "SMBIOS" }, 653 {SMBIOS3_TABLE_GUID, &efi.smbios3, "SMBIOS 3.0" }, 654 {EFI_SYSTEM_RESOURCE_TABLE_GUID, &efi.esrt, "ESRT" }, 655 {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, &efi_mem_attr_table, "MEMATTR" }, 656 {LINUX_EFI_RANDOM_SEED_TABLE_GUID, &efi_rng_seed, "RNG" }, 657 {LINUX_EFI_TPM_EVENT_LOG_GUID, &efi.tpm_log, "TPMEventLog" }, 658 {EFI_TCG2_FINAL_EVENTS_TABLE_GUID, &efi.tpm_final_log, "TPMFinalLog" }, 659 {EFI_CC_FINAL_EVENTS_TABLE_GUID, &efi.tpm_final_log, "CCFinalLog" }, 660 {LINUX_EFI_MEMRESERVE_TABLE_GUID, &efi.mem_reserve, "MEMRESERVE" }, 661 {LINUX_EFI_INITRD_MEDIA_GUID, &initrd, "INITRD" }, 662 {EFI_RT_PROPERTIES_TABLE_GUID, &rt_prop, "RTPROP" }, 663 #ifdef CONFIG_OVMF_DEBUG_LOG 664 {OVMF_MEMORY_LOG_TABLE_GUID, &efi.ovmf_debug_log, "OvmfDebugLog" }, 665 #endif 666 #ifdef CONFIG_EFI_RCI2_TABLE 667 {DELLEMC_EFI_RCI2_TABLE_GUID, &rci2_table_phys }, 668 #endif 669 #ifdef CONFIG_LOAD_UEFI_KEYS 670 {LINUX_EFI_MOK_VARIABLE_TABLE_GUID, &efi.mokvar_table, "MOKvar" }, 671 #endif 672 #ifdef CONFIG_EFI_COCO_SECRET 673 {LINUX_EFI_COCO_SECRET_AREA_GUID, &efi.coco_secret, "CocoSecret" }, 674 #endif 675 #ifdef CONFIG_UNACCEPTED_MEMORY 676 {LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID, &efi.unaccepted, "Unaccepted" }, 677 #endif 678 #ifdef CONFIG_EFI_GENERIC_STUB 679 {LINUX_EFI_PRIMARY_DISPLAY_TABLE_GUID, &primary_display_table }, 680 #endif 681 {}, 682 }; 683 684 static __init int match_config_table(const efi_guid_t *guid, 685 unsigned long table, 686 const efi_config_table_type_t *table_types) 687 { 688 int i; 689 690 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) { 691 if (efi_guidcmp(*guid, table_types[i].guid)) 692 continue; 693 694 if (!efi_config_table_is_usable(guid, table)) { 695 if (table_types[i].name[0]) 696 pr_cont("(%s=0x%lx unusable) ", 697 table_types[i].name, table); 698 return 1; 699 } 700 701 *(table_types[i].ptr) = table; 702 if (table_types[i].name[0]) 703 pr_cont("%s=0x%lx ", table_types[i].name, table); 704 return 1; 705 } 706 707 return 0; 708 } 709 710 /** 711 * reserve_unaccepted - Map and reserve unaccepted configuration table 712 * @unaccepted: Pointer to unaccepted memory table 713 * 714 * memblock_add() makes sure that the table is mapped in direct mapping. During 715 * normal boot it happens automatically because the table is allocated from 716 * usable memory. But during crashkernel boot only memory specifically reserved 717 * for crash scenario is mapped. memblock_add() forces the table to be mapped 718 * in crashkernel case. 719 * 720 * Align the range to the nearest page borders. Ranges smaller than page size 721 * are not going to be mapped. 722 * 723 * memblock_reserve() makes sure that future allocations will not touch the 724 * table. 725 */ 726 727 static __init void reserve_unaccepted(struct efi_unaccepted_memory *unaccepted) 728 { 729 phys_addr_t start, end; 730 731 start = PAGE_ALIGN_DOWN(efi.unaccepted); 732 end = PAGE_ALIGN(efi.unaccepted + sizeof(*unaccepted) + unaccepted->size); 733 734 memblock_add(start, end - start); 735 memblock_reserve(start, end - start); 736 } 737 738 int __init efi_config_parse_tables(const efi_config_table_t *config_tables, 739 int count, 740 const efi_config_table_type_t *arch_tables) 741 { 742 const efi_config_table_64_t *tbl64 = (void *)config_tables; 743 const efi_config_table_32_t *tbl32 = (void *)config_tables; 744 const efi_guid_t *guid; 745 unsigned long table; 746 int i; 747 748 pr_info(""); 749 for (i = 0; i < count; i++) { 750 if (!IS_ENABLED(CONFIG_X86)) { 751 guid = &config_tables[i].guid; 752 table = (unsigned long)config_tables[i].table; 753 } else if (efi_enabled(EFI_64BIT)) { 754 guid = &tbl64[i].guid; 755 table = tbl64[i].table; 756 757 if (IS_ENABLED(CONFIG_X86_32) && 758 tbl64[i].table > U32_MAX) { 759 pr_cont("\n"); 760 pr_err("Table located above 4GB, disabling EFI.\n"); 761 return -EINVAL; 762 } 763 } else { 764 guid = &tbl32[i].guid; 765 table = tbl32[i].table; 766 } 767 768 if (!match_config_table(guid, table, common_tables) && arch_tables) 769 match_config_table(guid, table, arch_tables); 770 } 771 pr_cont("\n"); 772 set_bit(EFI_CONFIG_TABLES, &efi.flags); 773 774 if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) { 775 struct linux_efi_random_seed *seed; 776 u32 size = 0; 777 778 seed = early_memremap(efi_rng_seed, sizeof(*seed)); 779 if (seed != NULL) { 780 size = min_t(u32, seed->size, SZ_1K); // sanity check 781 early_memunmap(seed, sizeof(*seed)); 782 } else { 783 pr_err("Could not map UEFI random seed!\n"); 784 } 785 if (size > 0) { 786 seed = early_memremap(efi_rng_seed, 787 sizeof(*seed) + size); 788 if (seed != NULL) { 789 add_bootloader_randomness(seed->bits, size); 790 memzero_explicit(seed->bits, size); 791 early_memunmap(seed, sizeof(*seed) + size); 792 } else { 793 pr_err("Could not map UEFI random seed!\n"); 794 } 795 } 796 } 797 798 if (!IS_ENABLED(CONFIG_X86_32) && efi_enabled(EFI_MEMMAP)) 799 efi_memattr_init(); 800 801 efi_tpm_eventlog_init(); 802 803 if (efi.mem_reserve != EFI_INVALID_TABLE_ADDR) { 804 unsigned long prsv = efi.mem_reserve; 805 806 while (prsv) { 807 struct linux_efi_memreserve *rsv; 808 u8 *p; 809 810 /* 811 * Just map a full page: that is what we will get 812 * anyway, and it permits us to map the entire entry 813 * before knowing its size. 814 */ 815 p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE), 816 PAGE_SIZE); 817 if (p == NULL) { 818 pr_err("Could not map UEFI memreserve entry!\n"); 819 return -ENOMEM; 820 } 821 822 rsv = (void *)(p + prsv % PAGE_SIZE); 823 824 /* reserve the entry itself */ 825 memblock_reserve(prsv, 826 struct_size(rsv, entry, rsv->size)); 827 828 for (i = 0; i < atomic_read(&rsv->count); i++) { 829 memblock_reserve(rsv->entry[i].base, 830 rsv->entry[i].size); 831 } 832 833 prsv = rsv->next; 834 early_memunmap(p, PAGE_SIZE); 835 } 836 } 837 838 if (rt_prop != EFI_INVALID_TABLE_ADDR) { 839 efi_rt_properties_table_t *tbl; 840 841 tbl = early_memremap(rt_prop, sizeof(*tbl)); 842 if (tbl) { 843 efi.runtime_supported_mask &= tbl->runtime_services_supported; 844 early_memunmap(tbl, sizeof(*tbl)); 845 } 846 } 847 848 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && 849 initrd != EFI_INVALID_TABLE_ADDR && phys_initrd_size == 0) { 850 struct linux_efi_initrd *tbl; 851 852 tbl = early_memremap(initrd, sizeof(*tbl)); 853 if (tbl) { 854 phys_initrd_start = tbl->base; 855 phys_initrd_size = tbl->size; 856 tbl->base = tbl->size = 0; 857 early_memunmap(tbl, sizeof(*tbl)); 858 } 859 } 860 861 if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && 862 efi.unaccepted != EFI_INVALID_TABLE_ADDR) { 863 struct efi_unaccepted_memory *unaccepted; 864 865 unaccepted = early_memremap(efi.unaccepted, sizeof(*unaccepted)); 866 if (unaccepted) { 867 868 if (unaccepted->version == 1) { 869 reserve_unaccepted(unaccepted); 870 } else { 871 efi.unaccepted = EFI_INVALID_TABLE_ADDR; 872 } 873 874 early_memunmap(unaccepted, sizeof(*unaccepted)); 875 } 876 } 877 878 return 0; 879 } 880 881 int __init efi_systab_check_header(const efi_table_hdr_t *systab_hdr) 882 { 883 if (systab_hdr->signature != EFI_SYSTEM_TABLE_SIGNATURE) { 884 pr_err("System table signature incorrect!\n"); 885 return -EINVAL; 886 } 887 888 return 0; 889 } 890 891 static const efi_char16_t *__init map_fw_vendor(unsigned long fw_vendor, 892 size_t size) 893 { 894 const efi_char16_t *ret; 895 896 ret = early_memremap_ro(fw_vendor, size); 897 if (!ret) 898 pr_err("Could not map the firmware vendor!\n"); 899 return ret; 900 } 901 902 static void __init unmap_fw_vendor(const void *fw_vendor, size_t size) 903 { 904 early_memunmap((void *)fw_vendor, size); 905 } 906 907 void __init efi_systab_report_header(const efi_table_hdr_t *systab_hdr, 908 unsigned long fw_vendor) 909 { 910 char vendor[100] = "unknown"; 911 const efi_char16_t *c16; 912 size_t i; 913 u16 rev; 914 915 c16 = map_fw_vendor(fw_vendor, sizeof(vendor) * sizeof(efi_char16_t)); 916 if (c16) { 917 for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i) 918 vendor[i] = c16[i]; 919 vendor[i] = '\0'; 920 921 unmap_fw_vendor(c16, sizeof(vendor) * sizeof(efi_char16_t)); 922 } 923 924 rev = (u16)systab_hdr->revision; 925 pr_info("EFI v%u.%u", systab_hdr->revision >> 16, rev / 10); 926 927 rev %= 10; 928 if (rev) 929 pr_cont(".%u", rev); 930 931 pr_cont(" by %s\n", vendor); 932 933 if (IS_ENABLED(CONFIG_X86_64) && 934 systab_hdr->revision > EFI_1_10_SYSTEM_TABLE_REVISION && 935 !strcmp(vendor, "Apple")) { 936 pr_info("Apple Mac detected, using EFI v1.10 runtime services only\n"); 937 efi.runtime_version = EFI_1_10_SYSTEM_TABLE_REVISION; 938 } 939 } 940 941 static __initdata char memory_type_name[][13] = { 942 "Reserved", 943 "Loader Code", 944 "Loader Data", 945 "Boot Code", 946 "Boot Data", 947 "Runtime Code", 948 "Runtime Data", 949 "Conventional", 950 "Unusable", 951 "ACPI Reclaim", 952 "ACPI Mem NVS", 953 "MMIO", 954 "MMIO Port", 955 "PAL Code", 956 "Persistent", 957 "Unaccepted", 958 }; 959 960 char * __init efi_md_typeattr_format(char *buf, size_t size, 961 const efi_memory_desc_t *md) 962 { 963 char *pos; 964 int type_len; 965 u64 attr; 966 967 pos = buf; 968 if (md->type >= ARRAY_SIZE(memory_type_name)) 969 type_len = snprintf(pos, size, "[type=%u", md->type); 970 else 971 type_len = snprintf(pos, size, "[%-*s", 972 (int)(sizeof(memory_type_name[0]) - 1), 973 memory_type_name[md->type]); 974 if (type_len >= size) 975 return buf; 976 977 pos += type_len; 978 size -= type_len; 979 980 attr = md->attribute; 981 if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT | 982 EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO | 983 EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP | 984 EFI_MEMORY_NV | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO | 985 EFI_MEMORY_MORE_RELIABLE | EFI_MEMORY_HOT_PLUGGABLE | 986 EFI_MEMORY_RUNTIME)) 987 snprintf(pos, size, "|attr=0x%016llx]", 988 (unsigned long long)attr); 989 else 990 snprintf(pos, size, 991 "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]", 992 attr & EFI_MEMORY_RUNTIME ? "RUN" : "", 993 attr & EFI_MEMORY_HOT_PLUGGABLE ? "HP" : "", 994 attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "", 995 attr & EFI_MEMORY_CPU_CRYPTO ? "CC" : "", 996 attr & EFI_MEMORY_SP ? "SP" : "", 997 attr & EFI_MEMORY_NV ? "NV" : "", 998 attr & EFI_MEMORY_XP ? "XP" : "", 999 attr & EFI_MEMORY_RP ? "RP" : "", 1000 attr & EFI_MEMORY_WP ? "WP" : "", 1001 attr & EFI_MEMORY_RO ? "RO" : "", 1002 attr & EFI_MEMORY_UCE ? "UCE" : "", 1003 attr & EFI_MEMORY_WB ? "WB" : "", 1004 attr & EFI_MEMORY_WT ? "WT" : "", 1005 attr & EFI_MEMORY_WC ? "WC" : "", 1006 attr & EFI_MEMORY_UC ? "UC" : ""); 1007 return buf; 1008 } 1009 1010 /* 1011 * efi_mem_attributes - lookup memmap attributes for physical address 1012 * @phys_addr: the physical address to lookup 1013 * 1014 * Search in the EFI memory map for the region covering 1015 * @phys_addr. Returns the EFI memory attributes if the region 1016 * was found in the memory map, 0 otherwise. 1017 */ 1018 u64 efi_mem_attributes(unsigned long phys_addr) 1019 { 1020 efi_memory_desc_t md; 1021 1022 if (efi_mem_desc_lookup(phys_addr, &md)) 1023 return 0; 1024 1025 return md.attribute; 1026 } 1027 1028 /* 1029 * efi_mem_type - lookup memmap type for physical address 1030 * @phys_addr: the physical address to lookup 1031 * 1032 * Search in the EFI memory map for the region covering @phys_addr. 1033 * Returns the EFI memory type if the region was found in the memory 1034 * map, -EINVAL otherwise. 1035 */ 1036 int efi_mem_type(unsigned long phys_addr) 1037 { 1038 efi_memory_desc_t md; 1039 1040 if (!efi_enabled(EFI_MEMMAP) && !efi_enabled(EFI_PARAVIRT)) 1041 return -ENOTSUPP; 1042 1043 if (efi_mem_desc_lookup(phys_addr, &md)) 1044 return -EINVAL; 1045 1046 return md.type; 1047 } 1048 1049 int efi_status_to_err(efi_status_t status) 1050 { 1051 int err; 1052 1053 switch (status) { 1054 case EFI_SUCCESS: 1055 err = 0; 1056 break; 1057 case EFI_INVALID_PARAMETER: 1058 err = -EINVAL; 1059 break; 1060 case EFI_OUT_OF_RESOURCES: 1061 err = -ENOSPC; 1062 break; 1063 case EFI_DEVICE_ERROR: 1064 err = -EIO; 1065 break; 1066 case EFI_WRITE_PROTECTED: 1067 err = -EROFS; 1068 break; 1069 case EFI_SECURITY_VIOLATION: 1070 err = -EACCES; 1071 break; 1072 case EFI_NOT_FOUND: 1073 err = -ENOENT; 1074 break; 1075 case EFI_ABORTED: 1076 err = -EINTR; 1077 break; 1078 default: 1079 err = -EINVAL; 1080 } 1081 1082 return err; 1083 } 1084 EXPORT_SYMBOL_GPL(efi_status_to_err); 1085 1086 static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock); 1087 static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init; 1088 1089 static int __init efi_memreserve_map_root(void) 1090 { 1091 if (efi.mem_reserve == EFI_INVALID_TABLE_ADDR) 1092 return -ENODEV; 1093 1094 efi_memreserve_root = memremap(efi.mem_reserve, 1095 sizeof(*efi_memreserve_root), 1096 MEMREMAP_WB); 1097 if (WARN_ON_ONCE(!efi_memreserve_root)) 1098 return -ENOMEM; 1099 return 0; 1100 } 1101 1102 static int efi_mem_reserve_iomem(phys_addr_t addr, u64 size) 1103 { 1104 struct resource *res, *parent; 1105 int ret; 1106 1107 res = kzalloc_obj(struct resource, GFP_ATOMIC); 1108 if (!res) 1109 return -ENOMEM; 1110 1111 res->name = "reserved"; 1112 res->flags = IORESOURCE_MEM; 1113 res->start = addr; 1114 res->end = addr + size - 1; 1115 1116 /* we expect a conflict with a 'System RAM' region */ 1117 parent = request_resource_conflict(&iomem_resource, res); 1118 ret = parent ? request_resource(parent, res) : 0; 1119 1120 /* 1121 * Given that efi_mem_reserve_iomem() can be called at any 1122 * time, only call memblock_reserve() if the architecture 1123 * keeps the infrastructure around. 1124 */ 1125 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !ret) 1126 memblock_reserve(addr, size); 1127 1128 return ret; 1129 } 1130 1131 int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size) 1132 { 1133 struct linux_efi_memreserve *rsv; 1134 unsigned long prsv; 1135 int rc, index; 1136 1137 if (efi_memreserve_root == (void *)ULONG_MAX) 1138 return -ENODEV; 1139 1140 if (!efi_memreserve_root) { 1141 rc = efi_memreserve_map_root(); 1142 if (rc) 1143 return rc; 1144 } 1145 1146 /* first try to find a slot in an existing linked list entry */ 1147 for (prsv = efi_memreserve_root->next; prsv; ) { 1148 rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB); 1149 if (!rsv) 1150 return -ENOMEM; 1151 index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size); 1152 if (index < rsv->size) { 1153 rsv->entry[index].base = addr; 1154 rsv->entry[index].size = size; 1155 1156 memunmap(rsv); 1157 return efi_mem_reserve_iomem(addr, size); 1158 } 1159 prsv = rsv->next; 1160 memunmap(rsv); 1161 } 1162 1163 /* no slot found - allocate a new linked list entry */ 1164 rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC); 1165 if (!rsv) 1166 return -ENOMEM; 1167 1168 rc = efi_mem_reserve_iomem(__pa(rsv), SZ_4K); 1169 if (rc) { 1170 free_page((unsigned long)rsv); 1171 return rc; 1172 } 1173 1174 /* 1175 * The memremap() call above assumes that a linux_efi_memreserve entry 1176 * never crosses a page boundary, so let's ensure that this remains true 1177 * even when kexec'ing a 4k pages kernel from a >4k pages kernel, by 1178 * using SZ_4K explicitly in the size calculation below. 1179 */ 1180 rsv->size = EFI_MEMRESERVE_COUNT(SZ_4K); 1181 atomic_set(&rsv->count, 1); 1182 rsv->entry[0].base = addr; 1183 rsv->entry[0].size = size; 1184 1185 spin_lock(&efi_mem_reserve_persistent_lock); 1186 rsv->next = efi_memreserve_root->next; 1187 efi_memreserve_root->next = __pa(rsv); 1188 spin_unlock(&efi_mem_reserve_persistent_lock); 1189 1190 return efi_mem_reserve_iomem(addr, size); 1191 } 1192 1193 static int __init efi_memreserve_root_init(void) 1194 { 1195 if (efi_memreserve_root) 1196 return 0; 1197 if (efi_memreserve_map_root()) 1198 efi_memreserve_root = (void *)ULONG_MAX; 1199 return 0; 1200 } 1201 early_initcall(efi_memreserve_root_init); 1202 1203 #ifdef CONFIG_KEXEC 1204 static int update_efi_random_seed(struct notifier_block *nb, 1205 unsigned long code, void *unused) 1206 { 1207 struct linux_efi_random_seed *seed; 1208 u32 size = 0; 1209 1210 if (!kexec_in_progress) 1211 return NOTIFY_DONE; 1212 1213 seed = memremap(efi_rng_seed, sizeof(*seed), MEMREMAP_WB); 1214 if (seed != NULL) { 1215 size = min(seed->size, EFI_RANDOM_SEED_SIZE); 1216 memunmap(seed); 1217 } else { 1218 pr_err("Could not map UEFI random seed!\n"); 1219 } 1220 if (size > 0) { 1221 seed = memremap(efi_rng_seed, sizeof(*seed) + size, 1222 MEMREMAP_WB); 1223 if (seed != NULL) { 1224 seed->size = size; 1225 get_random_bytes(seed->bits, seed->size); 1226 memunmap(seed); 1227 } else { 1228 pr_err("Could not map UEFI random seed!\n"); 1229 } 1230 } 1231 return NOTIFY_DONE; 1232 } 1233 1234 static struct notifier_block efi_random_seed_nb = { 1235 .notifier_call = update_efi_random_seed, 1236 }; 1237 1238 static int __init register_update_efi_random_seed(void) 1239 { 1240 if (efi_rng_seed == EFI_INVALID_TABLE_ADDR) 1241 return 0; 1242 return register_reboot_notifier(&efi_random_seed_nb); 1243 } 1244 late_initcall(register_update_efi_random_seed); 1245 #endif 1246