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