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