1 /* 2 * efi.c - EFI subsystem 3 * 4 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com> 5 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com> 6 * Copyright (C) 2013 Tom Gundersen <teg@jklm.no> 7 * 8 * This code registers /sys/firmware/efi{,/efivars} when EFI is supported, 9 * allowing the efivarfs to be mounted or the efivars module to be loaded. 10 * The existance of /sys/firmware/efi may also be used by userspace to 11 * determine that the system supports EFI. 12 * 13 * This file is released under the GPLv2. 14 */ 15 16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 17 18 #include <linux/kobject.h> 19 #include <linux/module.h> 20 #include <linux/init.h> 21 #include <linux/device.h> 22 #include <linux/efi.h> 23 #include <linux/of.h> 24 #include <linux/of_fdt.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 35 #include <asm/early_ioremap.h> 36 37 struct efi __read_mostly efi = { 38 .mps = EFI_INVALID_TABLE_ADDR, 39 .acpi = EFI_INVALID_TABLE_ADDR, 40 .acpi20 = EFI_INVALID_TABLE_ADDR, 41 .smbios = EFI_INVALID_TABLE_ADDR, 42 .smbios3 = EFI_INVALID_TABLE_ADDR, 43 .sal_systab = EFI_INVALID_TABLE_ADDR, 44 .boot_info = EFI_INVALID_TABLE_ADDR, 45 .hcdp = EFI_INVALID_TABLE_ADDR, 46 .uga = EFI_INVALID_TABLE_ADDR, 47 .uv_systab = EFI_INVALID_TABLE_ADDR, 48 .fw_vendor = EFI_INVALID_TABLE_ADDR, 49 .runtime = EFI_INVALID_TABLE_ADDR, 50 .config_table = EFI_INVALID_TABLE_ADDR, 51 .esrt = EFI_INVALID_TABLE_ADDR, 52 .properties_table = EFI_INVALID_TABLE_ADDR, 53 .mem_attr_table = EFI_INVALID_TABLE_ADDR, 54 .rng_seed = EFI_INVALID_TABLE_ADDR, 55 .tpm_log = EFI_INVALID_TABLE_ADDR 56 }; 57 EXPORT_SYMBOL(efi); 58 59 static unsigned long *efi_tables[] = { 60 &efi.mps, 61 &efi.acpi, 62 &efi.acpi20, 63 &efi.smbios, 64 &efi.smbios3, 65 &efi.sal_systab, 66 &efi.boot_info, 67 &efi.hcdp, 68 &efi.uga, 69 &efi.uv_systab, 70 &efi.fw_vendor, 71 &efi.runtime, 72 &efi.config_table, 73 &efi.esrt, 74 &efi.properties_table, 75 &efi.mem_attr_table, 76 }; 77 78 static bool 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 static int __init parse_efi_cmdline(char *str) 92 { 93 if (!str) { 94 pr_warn("need at least one option\n"); 95 return -EINVAL; 96 } 97 98 if (parse_option_str(str, "debug")) 99 set_bit(EFI_DBG, &efi.flags); 100 101 if (parse_option_str(str, "noruntime")) 102 disable_runtime = true; 103 104 return 0; 105 } 106 early_param("efi", parse_efi_cmdline); 107 108 struct kobject *efi_kobj; 109 110 /* 111 * Let's not leave out systab information that snuck into 112 * the efivars driver 113 * Note, do not add more fields in systab sysfs file as it breaks sysfs 114 * one value per file rule! 115 */ 116 static ssize_t systab_show(struct kobject *kobj, 117 struct kobj_attribute *attr, char *buf) 118 { 119 char *str = buf; 120 121 if (!kobj || !buf) 122 return -EINVAL; 123 124 if (efi.mps != EFI_INVALID_TABLE_ADDR) 125 str += sprintf(str, "MPS=0x%lx\n", efi.mps); 126 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR) 127 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20); 128 if (efi.acpi != EFI_INVALID_TABLE_ADDR) 129 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi); 130 /* 131 * If both SMBIOS and SMBIOS3 entry points are implemented, the 132 * SMBIOS3 entry point shall be preferred, so we list it first to 133 * let applications stop parsing after the first match. 134 */ 135 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) 136 str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3); 137 if (efi.smbios != EFI_INVALID_TABLE_ADDR) 138 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios); 139 if (efi.hcdp != EFI_INVALID_TABLE_ADDR) 140 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp); 141 if (efi.boot_info != EFI_INVALID_TABLE_ADDR) 142 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info); 143 if (efi.uga != EFI_INVALID_TABLE_ADDR) 144 str += sprintf(str, "UGA=0x%lx\n", efi.uga); 145 146 return str - buf; 147 } 148 149 static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400); 150 151 #define EFI_FIELD(var) efi.var 152 153 #define EFI_ATTR_SHOW(name) \ 154 static ssize_t name##_show(struct kobject *kobj, \ 155 struct kobj_attribute *attr, char *buf) \ 156 { \ 157 return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \ 158 } 159 160 EFI_ATTR_SHOW(fw_vendor); 161 EFI_ATTR_SHOW(runtime); 162 EFI_ATTR_SHOW(config_table); 163 164 static ssize_t fw_platform_size_show(struct kobject *kobj, 165 struct kobj_attribute *attr, char *buf) 166 { 167 return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32); 168 } 169 170 static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor); 171 static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime); 172 static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table); 173 static struct kobj_attribute efi_attr_fw_platform_size = 174 __ATTR_RO(fw_platform_size); 175 176 static struct attribute *efi_subsys_attrs[] = { 177 &efi_attr_systab.attr, 178 &efi_attr_fw_vendor.attr, 179 &efi_attr_runtime.attr, 180 &efi_attr_config_table.attr, 181 &efi_attr_fw_platform_size.attr, 182 NULL, 183 }; 184 185 static umode_t efi_attr_is_visible(struct kobject *kobj, 186 struct attribute *attr, int n) 187 { 188 if (attr == &efi_attr_fw_vendor.attr) { 189 if (efi_enabled(EFI_PARAVIRT) || 190 efi.fw_vendor == EFI_INVALID_TABLE_ADDR) 191 return 0; 192 } else if (attr == &efi_attr_runtime.attr) { 193 if (efi.runtime == EFI_INVALID_TABLE_ADDR) 194 return 0; 195 } else if (attr == &efi_attr_config_table.attr) { 196 if (efi.config_table == EFI_INVALID_TABLE_ADDR) 197 return 0; 198 } 199 200 return attr->mode; 201 } 202 203 static const struct attribute_group efi_subsys_attr_group = { 204 .attrs = efi_subsys_attrs, 205 .is_visible = efi_attr_is_visible, 206 }; 207 208 static struct efivars generic_efivars; 209 static struct efivar_operations generic_ops; 210 211 static int generic_ops_register(void) 212 { 213 generic_ops.get_variable = efi.get_variable; 214 generic_ops.set_variable = efi.set_variable; 215 generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking; 216 generic_ops.get_next_variable = efi.get_next_variable; 217 generic_ops.query_variable_store = efi_query_variable_store; 218 219 return efivars_register(&generic_efivars, &generic_ops, efi_kobj); 220 } 221 222 static void generic_ops_unregister(void) 223 { 224 efivars_unregister(&generic_efivars); 225 } 226 227 #if IS_ENABLED(CONFIG_ACPI) 228 #define EFIVAR_SSDT_NAME_MAX 16 229 static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata; 230 static int __init efivar_ssdt_setup(char *str) 231 { 232 if (strlen(str) < sizeof(efivar_ssdt)) 233 memcpy(efivar_ssdt, str, strlen(str)); 234 else 235 pr_warn("efivar_ssdt: name too long: %s\n", str); 236 return 0; 237 } 238 __setup("efivar_ssdt=", efivar_ssdt_setup); 239 240 static __init int efivar_ssdt_iter(efi_char16_t *name, efi_guid_t vendor, 241 unsigned long name_size, void *data) 242 { 243 struct efivar_entry *entry; 244 struct list_head *list = data; 245 char utf8_name[EFIVAR_SSDT_NAME_MAX]; 246 int limit = min_t(unsigned long, EFIVAR_SSDT_NAME_MAX, name_size); 247 248 ucs2_as_utf8(utf8_name, name, limit - 1); 249 if (strncmp(utf8_name, efivar_ssdt, limit) != 0) 250 return 0; 251 252 entry = kmalloc(sizeof(*entry), GFP_KERNEL); 253 if (!entry) 254 return 0; 255 256 memcpy(entry->var.VariableName, name, name_size); 257 memcpy(&entry->var.VendorGuid, &vendor, sizeof(efi_guid_t)); 258 259 efivar_entry_add(entry, list); 260 261 return 0; 262 } 263 264 static __init int efivar_ssdt_load(void) 265 { 266 LIST_HEAD(entries); 267 struct efivar_entry *entry, *aux; 268 unsigned long size; 269 void *data; 270 int ret; 271 272 ret = efivar_init(efivar_ssdt_iter, &entries, true, &entries); 273 274 list_for_each_entry_safe(entry, aux, &entries, list) { 275 pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt, 276 &entry->var.VendorGuid); 277 278 list_del(&entry->list); 279 280 ret = efivar_entry_size(entry, &size); 281 if (ret) { 282 pr_err("failed to get var size\n"); 283 goto free_entry; 284 } 285 286 data = kmalloc(size, GFP_KERNEL); 287 if (!data) { 288 ret = -ENOMEM; 289 goto free_entry; 290 } 291 292 ret = efivar_entry_get(entry, NULL, &size, data); 293 if (ret) { 294 pr_err("failed to get var data\n"); 295 goto free_data; 296 } 297 298 ret = acpi_load_table(data); 299 if (ret) { 300 pr_err("failed to load table: %d\n", ret); 301 goto free_data; 302 } 303 304 goto free_entry; 305 306 free_data: 307 kfree(data); 308 309 free_entry: 310 kfree(entry); 311 } 312 313 return ret; 314 } 315 #else 316 static inline int efivar_ssdt_load(void) { return 0; } 317 #endif 318 319 /* 320 * We register the efi subsystem with the firmware subsystem and the 321 * efivars subsystem with the efi subsystem, if the system was booted with 322 * EFI. 323 */ 324 static int __init efisubsys_init(void) 325 { 326 int error; 327 328 if (!efi_enabled(EFI_BOOT)) 329 return 0; 330 331 /* We register the efi directory at /sys/firmware/efi */ 332 efi_kobj = kobject_create_and_add("efi", firmware_kobj); 333 if (!efi_kobj) { 334 pr_err("efi: Firmware registration failed.\n"); 335 return -ENOMEM; 336 } 337 338 error = generic_ops_register(); 339 if (error) 340 goto err_put; 341 342 if (efi_enabled(EFI_RUNTIME_SERVICES)) 343 efivar_ssdt_load(); 344 345 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group); 346 if (error) { 347 pr_err("efi: Sysfs attribute export failed with error %d.\n", 348 error); 349 goto err_unregister; 350 } 351 352 error = efi_runtime_map_init(efi_kobj); 353 if (error) 354 goto err_remove_group; 355 356 /* and the standard mountpoint for efivarfs */ 357 error = sysfs_create_mount_point(efi_kobj, "efivars"); 358 if (error) { 359 pr_err("efivars: Subsystem registration failed.\n"); 360 goto err_remove_group; 361 } 362 363 return 0; 364 365 err_remove_group: 366 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group); 367 err_unregister: 368 generic_ops_unregister(); 369 err_put: 370 kobject_put(efi_kobj); 371 return error; 372 } 373 374 subsys_initcall(efisubsys_init); 375 376 /* 377 * Find the efi memory descriptor for a given physical address. Given a 378 * physical address, determine if it exists within an EFI Memory Map entry, 379 * and if so, populate the supplied memory descriptor with the appropriate 380 * data. 381 */ 382 int __init efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md) 383 { 384 efi_memory_desc_t *md; 385 386 if (!efi_enabled(EFI_MEMMAP)) { 387 pr_err_once("EFI_MEMMAP is not enabled.\n"); 388 return -EINVAL; 389 } 390 391 if (!out_md) { 392 pr_err_once("out_md is null.\n"); 393 return -EINVAL; 394 } 395 396 for_each_efi_memory_desc(md) { 397 u64 size; 398 u64 end; 399 400 if (!(md->attribute & EFI_MEMORY_RUNTIME) && 401 md->type != EFI_BOOT_SERVICES_DATA && 402 md->type != EFI_RUNTIME_SERVICES_DATA) { 403 continue; 404 } 405 406 size = md->num_pages << EFI_PAGE_SHIFT; 407 end = md->phys_addr + size; 408 if (phys_addr >= md->phys_addr && phys_addr < end) { 409 memcpy(out_md, md, sizeof(*out_md)); 410 return 0; 411 } 412 } 413 return -ENOENT; 414 } 415 416 /* 417 * Calculate the highest address of an efi memory descriptor. 418 */ 419 u64 __init efi_mem_desc_end(efi_memory_desc_t *md) 420 { 421 u64 size = md->num_pages << EFI_PAGE_SHIFT; 422 u64 end = md->phys_addr + size; 423 return end; 424 } 425 426 void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {} 427 428 /** 429 * efi_mem_reserve - Reserve an EFI memory region 430 * @addr: Physical address to reserve 431 * @size: Size of reservation 432 * 433 * Mark a region as reserved from general kernel allocation and 434 * prevent it being released by efi_free_boot_services(). 435 * 436 * This function should be called drivers once they've parsed EFI 437 * configuration tables to figure out where their data lives, e.g. 438 * efi_esrt_init(). 439 */ 440 void __init efi_mem_reserve(phys_addr_t addr, u64 size) 441 { 442 if (!memblock_is_region_reserved(addr, size)) 443 memblock_reserve(addr, size); 444 445 /* 446 * Some architectures (x86) reserve all boot services ranges 447 * until efi_free_boot_services() because of buggy firmware 448 * implementations. This means the above memblock_reserve() is 449 * superfluous on x86 and instead what it needs to do is 450 * ensure the @start, @size is not freed. 451 */ 452 efi_arch_mem_reserve(addr, size); 453 } 454 455 static __initdata efi_config_table_type_t common_tables[] = { 456 {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20}, 457 {ACPI_TABLE_GUID, "ACPI", &efi.acpi}, 458 {HCDP_TABLE_GUID, "HCDP", &efi.hcdp}, 459 {MPS_TABLE_GUID, "MPS", &efi.mps}, 460 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab}, 461 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios}, 462 {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3}, 463 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga}, 464 {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt}, 465 {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table}, 466 {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table}, 467 {LINUX_EFI_RANDOM_SEED_TABLE_GUID, "RNG", &efi.rng_seed}, 468 {LINUX_EFI_TPM_EVENT_LOG_GUID, "TPMEventLog", &efi.tpm_log}, 469 {NULL_GUID, NULL, NULL}, 470 }; 471 472 static __init int match_config_table(efi_guid_t *guid, 473 unsigned long table, 474 efi_config_table_type_t *table_types) 475 { 476 int i; 477 478 if (table_types) { 479 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) { 480 if (!efi_guidcmp(*guid, table_types[i].guid)) { 481 *(table_types[i].ptr) = table; 482 if (table_types[i].name) 483 pr_cont(" %s=0x%lx ", 484 table_types[i].name, table); 485 return 1; 486 } 487 } 488 } 489 490 return 0; 491 } 492 493 int __init efi_config_parse_tables(void *config_tables, int count, int sz, 494 efi_config_table_type_t *arch_tables) 495 { 496 void *tablep; 497 int i; 498 499 tablep = config_tables; 500 pr_info(""); 501 for (i = 0; i < count; i++) { 502 efi_guid_t guid; 503 unsigned long table; 504 505 if (efi_enabled(EFI_64BIT)) { 506 u64 table64; 507 guid = ((efi_config_table_64_t *)tablep)->guid; 508 table64 = ((efi_config_table_64_t *)tablep)->table; 509 table = table64; 510 #ifndef CONFIG_64BIT 511 if (table64 >> 32) { 512 pr_cont("\n"); 513 pr_err("Table located above 4GB, disabling EFI.\n"); 514 return -EINVAL; 515 } 516 #endif 517 } else { 518 guid = ((efi_config_table_32_t *)tablep)->guid; 519 table = ((efi_config_table_32_t *)tablep)->table; 520 } 521 522 if (!match_config_table(&guid, table, common_tables)) 523 match_config_table(&guid, table, arch_tables); 524 525 tablep += sz; 526 } 527 pr_cont("\n"); 528 set_bit(EFI_CONFIG_TABLES, &efi.flags); 529 530 if (efi.rng_seed != EFI_INVALID_TABLE_ADDR) { 531 struct linux_efi_random_seed *seed; 532 u32 size = 0; 533 534 seed = early_memremap(efi.rng_seed, sizeof(*seed)); 535 if (seed != NULL) { 536 size = seed->size; 537 early_memunmap(seed, sizeof(*seed)); 538 } else { 539 pr_err("Could not map UEFI random seed!\n"); 540 } 541 if (size > 0) { 542 seed = early_memremap(efi.rng_seed, 543 sizeof(*seed) + size); 544 if (seed != NULL) { 545 add_device_randomness(seed->bits, seed->size); 546 early_memunmap(seed, sizeof(*seed) + size); 547 pr_notice("seeding entropy pool\n"); 548 } else { 549 pr_err("Could not map UEFI random seed!\n"); 550 } 551 } 552 } 553 554 if (efi_enabled(EFI_MEMMAP)) 555 efi_memattr_init(); 556 557 efi_tpm_eventlog_init(); 558 559 /* Parse the EFI Properties table if it exists */ 560 if (efi.properties_table != EFI_INVALID_TABLE_ADDR) { 561 efi_properties_table_t *tbl; 562 563 tbl = early_memremap(efi.properties_table, sizeof(*tbl)); 564 if (tbl == NULL) { 565 pr_err("Could not map Properties table!\n"); 566 return -ENOMEM; 567 } 568 569 if (tbl->memory_protection_attribute & 570 EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) 571 set_bit(EFI_NX_PE_DATA, &efi.flags); 572 573 early_memunmap(tbl, sizeof(*tbl)); 574 } 575 576 return 0; 577 } 578 579 int __init efi_config_init(efi_config_table_type_t *arch_tables) 580 { 581 void *config_tables; 582 int sz, ret; 583 584 if (efi_enabled(EFI_64BIT)) 585 sz = sizeof(efi_config_table_64_t); 586 else 587 sz = sizeof(efi_config_table_32_t); 588 589 /* 590 * Let's see what config tables the firmware passed to us. 591 */ 592 config_tables = early_memremap(efi.systab->tables, 593 efi.systab->nr_tables * sz); 594 if (config_tables == NULL) { 595 pr_err("Could not map Configuration table!\n"); 596 return -ENOMEM; 597 } 598 599 ret = efi_config_parse_tables(config_tables, efi.systab->nr_tables, sz, 600 arch_tables); 601 602 early_memunmap(config_tables, efi.systab->nr_tables * sz); 603 return ret; 604 } 605 606 #ifdef CONFIG_EFI_VARS_MODULE 607 static int __init efi_load_efivars(void) 608 { 609 struct platform_device *pdev; 610 611 if (!efi_enabled(EFI_RUNTIME_SERVICES)) 612 return 0; 613 614 pdev = platform_device_register_simple("efivars", 0, NULL, 0); 615 return PTR_ERR_OR_ZERO(pdev); 616 } 617 device_initcall(efi_load_efivars); 618 #endif 619 620 #ifdef CONFIG_EFI_PARAMS_FROM_FDT 621 622 #define UEFI_PARAM(name, prop, field) \ 623 { \ 624 { name }, \ 625 { prop }, \ 626 offsetof(struct efi_fdt_params, field), \ 627 FIELD_SIZEOF(struct efi_fdt_params, field) \ 628 } 629 630 struct params { 631 const char name[32]; 632 const char propname[32]; 633 int offset; 634 int size; 635 }; 636 637 static __initdata struct params fdt_params[] = { 638 UEFI_PARAM("System Table", "linux,uefi-system-table", system_table), 639 UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap), 640 UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size), 641 UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size), 642 UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver) 643 }; 644 645 static __initdata struct params xen_fdt_params[] = { 646 UEFI_PARAM("System Table", "xen,uefi-system-table", system_table), 647 UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap), 648 UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size), 649 UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size), 650 UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver) 651 }; 652 653 #define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params) 654 655 static __initdata struct { 656 const char *uname; 657 const char *subnode; 658 struct params *params; 659 } dt_params[] = { 660 { "hypervisor", "uefi", xen_fdt_params }, 661 { "chosen", NULL, fdt_params }, 662 }; 663 664 struct param_info { 665 int found; 666 void *params; 667 const char *missing; 668 }; 669 670 static int __init __find_uefi_params(unsigned long node, 671 struct param_info *info, 672 struct params *params) 673 { 674 const void *prop; 675 void *dest; 676 u64 val; 677 int i, len; 678 679 for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) { 680 prop = of_get_flat_dt_prop(node, params[i].propname, &len); 681 if (!prop) { 682 info->missing = params[i].name; 683 return 0; 684 } 685 686 dest = info->params + params[i].offset; 687 info->found++; 688 689 val = of_read_number(prop, len / sizeof(u32)); 690 691 if (params[i].size == sizeof(u32)) 692 *(u32 *)dest = val; 693 else 694 *(u64 *)dest = val; 695 696 if (efi_enabled(EFI_DBG)) 697 pr_info(" %s: 0x%0*llx\n", params[i].name, 698 params[i].size * 2, val); 699 } 700 701 return 1; 702 } 703 704 static int __init fdt_find_uefi_params(unsigned long node, const char *uname, 705 int depth, void *data) 706 { 707 struct param_info *info = data; 708 int i; 709 710 for (i = 0; i < ARRAY_SIZE(dt_params); i++) { 711 const char *subnode = dt_params[i].subnode; 712 713 if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) { 714 info->missing = dt_params[i].params[0].name; 715 continue; 716 } 717 718 if (subnode) { 719 int err = of_get_flat_dt_subnode_by_name(node, subnode); 720 721 if (err < 0) 722 return 0; 723 724 node = err; 725 } 726 727 return __find_uefi_params(node, info, dt_params[i].params); 728 } 729 730 return 0; 731 } 732 733 int __init efi_get_fdt_params(struct efi_fdt_params *params) 734 { 735 struct param_info info; 736 int ret; 737 738 pr_info("Getting EFI parameters from FDT:\n"); 739 740 info.found = 0; 741 info.params = params; 742 743 ret = of_scan_flat_dt(fdt_find_uefi_params, &info); 744 if (!info.found) 745 pr_info("UEFI not found.\n"); 746 else if (!ret) 747 pr_err("Can't find '%s' in device tree!\n", 748 info.missing); 749 750 return ret; 751 } 752 #endif /* CONFIG_EFI_PARAMS_FROM_FDT */ 753 754 static __initdata char memory_type_name[][20] = { 755 "Reserved", 756 "Loader Code", 757 "Loader Data", 758 "Boot Code", 759 "Boot Data", 760 "Runtime Code", 761 "Runtime Data", 762 "Conventional Memory", 763 "Unusable Memory", 764 "ACPI Reclaim Memory", 765 "ACPI Memory NVS", 766 "Memory Mapped I/O", 767 "MMIO Port Space", 768 "PAL Code", 769 "Persistent Memory", 770 }; 771 772 char * __init efi_md_typeattr_format(char *buf, size_t size, 773 const efi_memory_desc_t *md) 774 { 775 char *pos; 776 int type_len; 777 u64 attr; 778 779 pos = buf; 780 if (md->type >= ARRAY_SIZE(memory_type_name)) 781 type_len = snprintf(pos, size, "[type=%u", md->type); 782 else 783 type_len = snprintf(pos, size, "[%-*s", 784 (int)(sizeof(memory_type_name[0]) - 1), 785 memory_type_name[md->type]); 786 if (type_len >= size) 787 return buf; 788 789 pos += type_len; 790 size -= type_len; 791 792 attr = md->attribute; 793 if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT | 794 EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO | 795 EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP | 796 EFI_MEMORY_NV | 797 EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE)) 798 snprintf(pos, size, "|attr=0x%016llx]", 799 (unsigned long long)attr); 800 else 801 snprintf(pos, size, 802 "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]", 803 attr & EFI_MEMORY_RUNTIME ? "RUN" : "", 804 attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "", 805 attr & EFI_MEMORY_NV ? "NV" : "", 806 attr & EFI_MEMORY_XP ? "XP" : "", 807 attr & EFI_MEMORY_RP ? "RP" : "", 808 attr & EFI_MEMORY_WP ? "WP" : "", 809 attr & EFI_MEMORY_RO ? "RO" : "", 810 attr & EFI_MEMORY_UCE ? "UCE" : "", 811 attr & EFI_MEMORY_WB ? "WB" : "", 812 attr & EFI_MEMORY_WT ? "WT" : "", 813 attr & EFI_MEMORY_WC ? "WC" : "", 814 attr & EFI_MEMORY_UC ? "UC" : ""); 815 return buf; 816 } 817 818 /* 819 * IA64 has a funky EFI memory map that doesn't work the same way as 820 * other architectures. 821 */ 822 #ifndef CONFIG_IA64 823 /* 824 * efi_mem_attributes - lookup memmap attributes for physical address 825 * @phys_addr: the physical address to lookup 826 * 827 * Search in the EFI memory map for the region covering 828 * @phys_addr. Returns the EFI memory attributes if the region 829 * was found in the memory map, 0 otherwise. 830 */ 831 u64 efi_mem_attributes(unsigned long phys_addr) 832 { 833 efi_memory_desc_t *md; 834 835 if (!efi_enabled(EFI_MEMMAP)) 836 return 0; 837 838 for_each_efi_memory_desc(md) { 839 if ((md->phys_addr <= phys_addr) && 840 (phys_addr < (md->phys_addr + 841 (md->num_pages << EFI_PAGE_SHIFT)))) 842 return md->attribute; 843 } 844 return 0; 845 } 846 847 /* 848 * efi_mem_type - lookup memmap type for physical address 849 * @phys_addr: the physical address to lookup 850 * 851 * Search in the EFI memory map for the region covering @phys_addr. 852 * Returns the EFI memory type if the region was found in the memory 853 * map, EFI_RESERVED_TYPE (zero) otherwise. 854 */ 855 int efi_mem_type(unsigned long phys_addr) 856 { 857 const efi_memory_desc_t *md; 858 859 if (!efi_enabled(EFI_MEMMAP)) 860 return -ENOTSUPP; 861 862 for_each_efi_memory_desc(md) { 863 if ((md->phys_addr <= phys_addr) && 864 (phys_addr < (md->phys_addr + 865 (md->num_pages << EFI_PAGE_SHIFT)))) 866 return md->type; 867 } 868 return -EINVAL; 869 } 870 #endif 871 872 int efi_status_to_err(efi_status_t status) 873 { 874 int err; 875 876 switch (status) { 877 case EFI_SUCCESS: 878 err = 0; 879 break; 880 case EFI_INVALID_PARAMETER: 881 err = -EINVAL; 882 break; 883 case EFI_OUT_OF_RESOURCES: 884 err = -ENOSPC; 885 break; 886 case EFI_DEVICE_ERROR: 887 err = -EIO; 888 break; 889 case EFI_WRITE_PROTECTED: 890 err = -EROFS; 891 break; 892 case EFI_SECURITY_VIOLATION: 893 err = -EACCES; 894 break; 895 case EFI_NOT_FOUND: 896 err = -ENOENT; 897 break; 898 case EFI_ABORTED: 899 err = -EINTR; 900 break; 901 default: 902 err = -EINVAL; 903 } 904 905 return err; 906 } 907 908 bool efi_is_table_address(unsigned long phys_addr) 909 { 910 unsigned int i; 911 912 if (phys_addr == EFI_INVALID_TABLE_ADDR) 913 return false; 914 915 for (i = 0; i < ARRAY_SIZE(efi_tables); i++) 916 if (*(efi_tables[i]) == phys_addr) 917 return true; 918 919 return false; 920 } 921 922 #ifdef CONFIG_KEXEC 923 static int update_efi_random_seed(struct notifier_block *nb, 924 unsigned long code, void *unused) 925 { 926 struct linux_efi_random_seed *seed; 927 u32 size = 0; 928 929 if (!kexec_in_progress) 930 return NOTIFY_DONE; 931 932 seed = memremap(efi.rng_seed, sizeof(*seed), MEMREMAP_WB); 933 if (seed != NULL) { 934 size = min(seed->size, EFI_RANDOM_SEED_SIZE); 935 memunmap(seed); 936 } else { 937 pr_err("Could not map UEFI random seed!\n"); 938 } 939 if (size > 0) { 940 seed = memremap(efi.rng_seed, sizeof(*seed) + size, 941 MEMREMAP_WB); 942 if (seed != NULL) { 943 seed->size = size; 944 get_random_bytes(seed->bits, seed->size); 945 memunmap(seed); 946 } else { 947 pr_err("Could not map UEFI random seed!\n"); 948 } 949 } 950 return NOTIFY_DONE; 951 } 952 953 static struct notifier_block efi_random_seed_nb = { 954 .notifier_call = update_efi_random_seed, 955 }; 956 957 static int register_update_efi_random_seed(void) 958 { 959 if (efi.rng_seed == EFI_INVALID_TABLE_ADDR) 960 return 0; 961 return register_reboot_notifier(&efi_random_seed_nb); 962 } 963 late_initcall(register_update_efi_random_seed); 964 #endif 965