blob: 415d7b3a59f826f0a8dc0d76a758a4f4d8104e25 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013 */
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>
David Brazdil0f672f62019-12-10 10:32:29 +000033#include <linux/security.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034
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,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043 .boot_info = EFI_INVALID_TABLE_ADDR,
44 .hcdp = EFI_INVALID_TABLE_ADDR,
45 .uga = EFI_INVALID_TABLE_ADDR,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046 .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,
David Brazdil0f672f62019-12-10 10:32:29 +000053 .tpm_log = EFI_INVALID_TABLE_ADDR,
54 .tpm_final_log = EFI_INVALID_TABLE_ADDR,
55 .mem_reserve = EFI_INVALID_TABLE_ADDR,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056};
57EXPORT_SYMBOL(efi);
58
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000059struct 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
Olivier Deprez0e641232021-09-23 10:07:05 +0200220#ifdef CONFIG_EFI_CUSTOM_SSDT_OVERLAYS
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000221#define EFIVAR_SSDT_NAME_MAX 16
222static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
223static int __init efivar_ssdt_setup(char *str)
224{
David Brazdil0f672f62019-12-10 10:32:29 +0000225 int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
226
227 if (ret)
228 return ret;
229
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000230 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
David Brazdil0f672f62019-12-10 10:32:29 +0000270 if (!efivar_ssdt[0])
271 return 0;
272
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000273 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");
Olivier Deprez0e641232021-09-23 10:07:05 +0200348 destroy_workqueue(efi_rts_wq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000349 return -ENOMEM;
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);
Olivier Deprez0e641232021-09-23 10:07:05 +0200385 destroy_workqueue(efi_rts_wq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000386 return error;
387}
388
389subsys_initcall(efisubsys_init);
390
391/*
392 * Find the efi memory descriptor for a given physical address. Given a
393 * physical address, determine if it exists within an EFI Memory Map entry,
394 * and if so, populate the supplied memory descriptor with the appropriate
395 * data.
396 */
397int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
398{
399 efi_memory_desc_t *md;
400
401 if (!efi_enabled(EFI_MEMMAP)) {
402 pr_err_once("EFI_MEMMAP is not enabled.\n");
403 return -EINVAL;
404 }
405
406 if (!out_md) {
407 pr_err_once("out_md is null.\n");
408 return -EINVAL;
409 }
410
411 for_each_efi_memory_desc(md) {
412 u64 size;
413 u64 end;
414
415 size = md->num_pages << EFI_PAGE_SHIFT;
416 end = md->phys_addr + size;
417 if (phys_addr >= md->phys_addr && phys_addr < end) {
418 memcpy(out_md, md, sizeof(*out_md));
419 return 0;
420 }
421 }
422 return -ENOENT;
423}
424
425/*
426 * Calculate the highest address of an efi memory descriptor.
427 */
428u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
429{
430 u64 size = md->num_pages << EFI_PAGE_SHIFT;
431 u64 end = md->phys_addr + size;
432 return end;
433}
434
435void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}
436
437/**
438 * efi_mem_reserve - Reserve an EFI memory region
439 * @addr: Physical address to reserve
440 * @size: Size of reservation
441 *
442 * Mark a region as reserved from general kernel allocation and
443 * prevent it being released by efi_free_boot_services().
444 *
445 * This function should be called drivers once they've parsed EFI
446 * configuration tables to figure out where their data lives, e.g.
447 * efi_esrt_init().
448 */
449void __init efi_mem_reserve(phys_addr_t addr, u64 size)
450{
451 if (!memblock_is_region_reserved(addr, size))
452 memblock_reserve(addr, size);
453
454 /*
455 * Some architectures (x86) reserve all boot services ranges
456 * until efi_free_boot_services() because of buggy firmware
457 * implementations. This means the above memblock_reserve() is
458 * superfluous on x86 and instead what it needs to do is
459 * ensure the @start, @size is not freed.
460 */
461 efi_arch_mem_reserve(addr, size);
462}
463
464static __initdata efi_config_table_type_t common_tables[] = {
465 {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
466 {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
467 {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
468 {MPS_TABLE_GUID, "MPS", &efi.mps},
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000469 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
470 {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3},
471 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
472 {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt},
473 {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table},
474 {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table},
475 {LINUX_EFI_RANDOM_SEED_TABLE_GUID, "RNG", &efi.rng_seed},
476 {LINUX_EFI_TPM_EVENT_LOG_GUID, "TPMEventLog", &efi.tpm_log},
David Brazdil0f672f62019-12-10 10:32:29 +0000477 {LINUX_EFI_TPM_FINAL_LOG_GUID, "TPMFinalLog", &efi.tpm_final_log},
478 {LINUX_EFI_MEMRESERVE_TABLE_GUID, "MEMRESERVE", &efi.mem_reserve},
479#ifdef CONFIG_EFI_RCI2_TABLE
480 {DELLEMC_EFI_RCI2_TABLE_GUID, NULL, &rci2_table_phys},
481#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000482 {NULL_GUID, NULL, NULL},
483};
484
485static __init int match_config_table(efi_guid_t *guid,
486 unsigned long table,
487 efi_config_table_type_t *table_types)
488{
489 int i;
490
491 if (table_types) {
492 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
493 if (!efi_guidcmp(*guid, table_types[i].guid)) {
494 *(table_types[i].ptr) = table;
495 if (table_types[i].name)
496 pr_cont(" %s=0x%lx ",
497 table_types[i].name, table);
498 return 1;
499 }
500 }
501 }
502
503 return 0;
504}
505
506int __init efi_config_parse_tables(void *config_tables, int count, int sz,
507 efi_config_table_type_t *arch_tables)
508{
509 void *tablep;
510 int i;
511
512 tablep = config_tables;
513 pr_info("");
514 for (i = 0; i < count; i++) {
515 efi_guid_t guid;
516 unsigned long table;
517
518 if (efi_enabled(EFI_64BIT)) {
519 u64 table64;
520 guid = ((efi_config_table_64_t *)tablep)->guid;
521 table64 = ((efi_config_table_64_t *)tablep)->table;
522 table = table64;
523#ifndef CONFIG_64BIT
524 if (table64 >> 32) {
525 pr_cont("\n");
526 pr_err("Table located above 4GB, disabling EFI.\n");
527 return -EINVAL;
528 }
529#endif
530 } else {
531 guid = ((efi_config_table_32_t *)tablep)->guid;
532 table = ((efi_config_table_32_t *)tablep)->table;
533 }
534
535 if (!match_config_table(&guid, table, common_tables))
536 match_config_table(&guid, table, arch_tables);
537
538 tablep += sz;
539 }
540 pr_cont("\n");
541 set_bit(EFI_CONFIG_TABLES, &efi.flags);
542
543 if (efi.rng_seed != EFI_INVALID_TABLE_ADDR) {
544 struct linux_efi_random_seed *seed;
545 u32 size = 0;
546
547 seed = early_memremap(efi.rng_seed, sizeof(*seed));
548 if (seed != NULL) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200549 size = READ_ONCE(seed->size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000550 early_memunmap(seed, sizeof(*seed));
551 } else {
552 pr_err("Could not map UEFI random seed!\n");
553 }
554 if (size > 0) {
555 seed = early_memremap(efi.rng_seed,
556 sizeof(*seed) + size);
557 if (seed != NULL) {
558 pr_notice("seeding entropy pool\n");
Olivier Deprez0e641232021-09-23 10:07:05 +0200559 add_bootloader_randomness(seed->bits, size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000560 early_memunmap(seed, sizeof(*seed) + size);
561 } else {
562 pr_err("Could not map UEFI random seed!\n");
563 }
564 }
565 }
566
Olivier Deprez0e641232021-09-23 10:07:05 +0200567 if (!IS_ENABLED(CONFIG_X86_32) && efi_enabled(EFI_MEMMAP))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000568 efi_memattr_init();
569
570 efi_tpm_eventlog_init();
571
572 /* Parse the EFI Properties table if it exists */
573 if (efi.properties_table != EFI_INVALID_TABLE_ADDR) {
574 efi_properties_table_t *tbl;
575
576 tbl = early_memremap(efi.properties_table, sizeof(*tbl));
577 if (tbl == NULL) {
578 pr_err("Could not map Properties table!\n");
579 return -ENOMEM;
580 }
581
582 if (tbl->memory_protection_attribute &
583 EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
584 set_bit(EFI_NX_PE_DATA, &efi.flags);
585
586 early_memunmap(tbl, sizeof(*tbl));
587 }
588
David Brazdil0f672f62019-12-10 10:32:29 +0000589 if (efi.mem_reserve != EFI_INVALID_TABLE_ADDR) {
590 unsigned long prsv = efi.mem_reserve;
591
592 while (prsv) {
593 struct linux_efi_memreserve *rsv;
594 u8 *p;
595 int i;
596
597 /*
598 * Just map a full page: that is what we will get
599 * anyway, and it permits us to map the entire entry
600 * before knowing its size.
601 */
602 p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE),
603 PAGE_SIZE);
604 if (p == NULL) {
605 pr_err("Could not map UEFI memreserve entry!\n");
606 return -ENOMEM;
607 }
608
609 rsv = (void *)(p + prsv % PAGE_SIZE);
610
611 /* reserve the entry itself */
612 memblock_reserve(prsv, EFI_MEMRESERVE_SIZE(rsv->size));
613
614 for (i = 0; i < atomic_read(&rsv->count); i++) {
615 memblock_reserve(rsv->entry[i].base,
616 rsv->entry[i].size);
617 }
618
619 prsv = rsv->next;
620 early_memunmap(p, PAGE_SIZE);
621 }
622 }
623
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000624 return 0;
625}
626
627int __init efi_config_init(efi_config_table_type_t *arch_tables)
628{
629 void *config_tables;
630 int sz, ret;
631
David Brazdil0f672f62019-12-10 10:32:29 +0000632 if (efi.systab->nr_tables == 0)
633 return 0;
634
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000635 if (efi_enabled(EFI_64BIT))
636 sz = sizeof(efi_config_table_64_t);
637 else
638 sz = sizeof(efi_config_table_32_t);
639
640 /*
641 * Let's see what config tables the firmware passed to us.
642 */
643 config_tables = early_memremap(efi.systab->tables,
644 efi.systab->nr_tables * sz);
645 if (config_tables == NULL) {
646 pr_err("Could not map Configuration table!\n");
647 return -ENOMEM;
648 }
649
650 ret = efi_config_parse_tables(config_tables, efi.systab->nr_tables, sz,
651 arch_tables);
652
653 early_memunmap(config_tables, efi.systab->nr_tables * sz);
654 return ret;
655}
656
657#ifdef CONFIG_EFI_VARS_MODULE
658static int __init efi_load_efivars(void)
659{
660 struct platform_device *pdev;
661
662 if (!efi_enabled(EFI_RUNTIME_SERVICES))
663 return 0;
664
665 pdev = platform_device_register_simple("efivars", 0, NULL, 0);
666 return PTR_ERR_OR_ZERO(pdev);
667}
668device_initcall(efi_load_efivars);
669#endif
670
671#ifdef CONFIG_EFI_PARAMS_FROM_FDT
672
673#define UEFI_PARAM(name, prop, field) \
674 { \
675 { name }, \
676 { prop }, \
677 offsetof(struct efi_fdt_params, field), \
678 FIELD_SIZEOF(struct efi_fdt_params, field) \
679 }
680
681struct params {
682 const char name[32];
683 const char propname[32];
684 int offset;
685 int size;
686};
687
688static __initdata struct params fdt_params[] = {
689 UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
690 UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
691 UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
692 UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
693 UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver)
694};
695
696static __initdata struct params xen_fdt_params[] = {
697 UEFI_PARAM("System Table", "xen,uefi-system-table", system_table),
698 UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap),
699 UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size),
700 UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size),
701 UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver)
702};
703
704#define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params)
705
706static __initdata struct {
707 const char *uname;
708 const char *subnode;
709 struct params *params;
710} dt_params[] = {
711 { "hypervisor", "uefi", xen_fdt_params },
712 { "chosen", NULL, fdt_params },
713};
714
715struct param_info {
716 int found;
717 void *params;
718 const char *missing;
719};
720
721static int __init __find_uefi_params(unsigned long node,
722 struct param_info *info,
723 struct params *params)
724{
725 const void *prop;
726 void *dest;
727 u64 val;
728 int i, len;
729
730 for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) {
731 prop = of_get_flat_dt_prop(node, params[i].propname, &len);
732 if (!prop) {
733 info->missing = params[i].name;
734 return 0;
735 }
736
737 dest = info->params + params[i].offset;
738 info->found++;
739
740 val = of_read_number(prop, len / sizeof(u32));
741
742 if (params[i].size == sizeof(u32))
743 *(u32 *)dest = val;
744 else
745 *(u64 *)dest = val;
746
747 if (efi_enabled(EFI_DBG))
748 pr_info(" %s: 0x%0*llx\n", params[i].name,
749 params[i].size * 2, val);
750 }
751
752 return 1;
753}
754
755static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
756 int depth, void *data)
757{
758 struct param_info *info = data;
759 int i;
760
761 for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
762 const char *subnode = dt_params[i].subnode;
763
764 if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) {
765 info->missing = dt_params[i].params[0].name;
766 continue;
767 }
768
769 if (subnode) {
770 int err = of_get_flat_dt_subnode_by_name(node, subnode);
771
772 if (err < 0)
773 return 0;
774
775 node = err;
776 }
777
778 return __find_uefi_params(node, info, dt_params[i].params);
779 }
780
781 return 0;
782}
783
784int __init efi_get_fdt_params(struct efi_fdt_params *params)
785{
786 struct param_info info;
787 int ret;
788
789 pr_info("Getting EFI parameters from FDT:\n");
790
791 info.found = 0;
792 info.params = params;
793
794 ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
795 if (!info.found)
796 pr_info("UEFI not found.\n");
797 else if (!ret)
798 pr_err("Can't find '%s' in device tree!\n",
799 info.missing);
800
801 return ret;
802}
803#endif /* CONFIG_EFI_PARAMS_FROM_FDT */
804
805static __initdata char memory_type_name[][20] = {
806 "Reserved",
807 "Loader Code",
808 "Loader Data",
809 "Boot Code",
810 "Boot Data",
811 "Runtime Code",
812 "Runtime Data",
813 "Conventional Memory",
814 "Unusable Memory",
815 "ACPI Reclaim Memory",
816 "ACPI Memory NVS",
817 "Memory Mapped I/O",
818 "MMIO Port Space",
819 "PAL Code",
820 "Persistent Memory",
821};
822
823char * __init efi_md_typeattr_format(char *buf, size_t size,
824 const efi_memory_desc_t *md)
825{
826 char *pos;
827 int type_len;
828 u64 attr;
829
830 pos = buf;
831 if (md->type >= ARRAY_SIZE(memory_type_name))
832 type_len = snprintf(pos, size, "[type=%u", md->type);
833 else
834 type_len = snprintf(pos, size, "[%-*s",
835 (int)(sizeof(memory_type_name[0]) - 1),
836 memory_type_name[md->type]);
837 if (type_len >= size)
838 return buf;
839
840 pos += type_len;
841 size -= type_len;
842
843 attr = md->attribute;
844 if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
845 EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
846 EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
847 EFI_MEMORY_NV |
848 EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
849 snprintf(pos, size, "|attr=0x%016llx]",
850 (unsigned long long)attr);
851 else
852 snprintf(pos, size,
853 "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
854 attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
855 attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
856 attr & EFI_MEMORY_NV ? "NV" : "",
857 attr & EFI_MEMORY_XP ? "XP" : "",
858 attr & EFI_MEMORY_RP ? "RP" : "",
859 attr & EFI_MEMORY_WP ? "WP" : "",
860 attr & EFI_MEMORY_RO ? "RO" : "",
861 attr & EFI_MEMORY_UCE ? "UCE" : "",
862 attr & EFI_MEMORY_WB ? "WB" : "",
863 attr & EFI_MEMORY_WT ? "WT" : "",
864 attr & EFI_MEMORY_WC ? "WC" : "",
865 attr & EFI_MEMORY_UC ? "UC" : "");
866 return buf;
867}
868
869/*
870 * IA64 has a funky EFI memory map that doesn't work the same way as
871 * other architectures.
872 */
873#ifndef CONFIG_IA64
874/*
875 * efi_mem_attributes - lookup memmap attributes for physical address
876 * @phys_addr: the physical address to lookup
877 *
878 * Search in the EFI memory map for the region covering
879 * @phys_addr. Returns the EFI memory attributes if the region
880 * was found in the memory map, 0 otherwise.
881 */
882u64 efi_mem_attributes(unsigned long phys_addr)
883{
884 efi_memory_desc_t *md;
885
886 if (!efi_enabled(EFI_MEMMAP))
887 return 0;
888
889 for_each_efi_memory_desc(md) {
890 if ((md->phys_addr <= phys_addr) &&
891 (phys_addr < (md->phys_addr +
892 (md->num_pages << EFI_PAGE_SHIFT))))
893 return md->attribute;
894 }
895 return 0;
896}
897
898/*
899 * efi_mem_type - lookup memmap type for physical address
900 * @phys_addr: the physical address to lookup
901 *
902 * Search in the EFI memory map for the region covering @phys_addr.
903 * Returns the EFI memory type if the region was found in the memory
904 * map, EFI_RESERVED_TYPE (zero) otherwise.
905 */
906int efi_mem_type(unsigned long phys_addr)
907{
908 const efi_memory_desc_t *md;
909
910 if (!efi_enabled(EFI_MEMMAP))
911 return -ENOTSUPP;
912
913 for_each_efi_memory_desc(md) {
914 if ((md->phys_addr <= phys_addr) &&
915 (phys_addr < (md->phys_addr +
916 (md->num_pages << EFI_PAGE_SHIFT))))
917 return md->type;
918 }
919 return -EINVAL;
920}
921#endif
922
923int efi_status_to_err(efi_status_t status)
924{
925 int err;
926
927 switch (status) {
928 case EFI_SUCCESS:
929 err = 0;
930 break;
931 case EFI_INVALID_PARAMETER:
932 err = -EINVAL;
933 break;
934 case EFI_OUT_OF_RESOURCES:
935 err = -ENOSPC;
936 break;
937 case EFI_DEVICE_ERROR:
938 err = -EIO;
939 break;
940 case EFI_WRITE_PROTECTED:
941 err = -EROFS;
942 break;
943 case EFI_SECURITY_VIOLATION:
944 err = -EACCES;
945 break;
946 case EFI_NOT_FOUND:
947 err = -ENOENT;
948 break;
949 case EFI_ABORTED:
950 err = -EINTR;
951 break;
952 default:
953 err = -EINVAL;
954 }
955
956 return err;
957}
958
David Brazdil0f672f62019-12-10 10:32:29 +0000959static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock);
960static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
961
962static int __init efi_memreserve_map_root(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000963{
David Brazdil0f672f62019-12-10 10:32:29 +0000964 if (efi.mem_reserve == EFI_INVALID_TABLE_ADDR)
965 return -ENODEV;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000966
David Brazdil0f672f62019-12-10 10:32:29 +0000967 efi_memreserve_root = memremap(efi.mem_reserve,
968 sizeof(*efi_memreserve_root),
969 MEMREMAP_WB);
970 if (WARN_ON_ONCE(!efi_memreserve_root))
971 return -ENOMEM;
972 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000973}
974
Olivier Deprez0e641232021-09-23 10:07:05 +0200975static int efi_mem_reserve_iomem(phys_addr_t addr, u64 size)
976{
977 struct resource *res, *parent;
978 int ret;
979
980 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
981 if (!res)
982 return -ENOMEM;
983
984 res->name = "reserved";
985 res->flags = IORESOURCE_MEM;
986 res->start = addr;
987 res->end = addr + size - 1;
988
989 /* we expect a conflict with a 'System RAM' region */
990 parent = request_resource_conflict(&iomem_resource, res);
991 ret = parent ? request_resource(parent, res) : 0;
992
993 /*
994 * Given that efi_mem_reserve_iomem() can be called at any
995 * time, only call memblock_reserve() if the architecture
996 * keeps the infrastructure around.
997 */
998 if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !ret)
999 memblock_reserve(addr, size);
1000
1001 return ret;
1002}
1003
David Brazdil0f672f62019-12-10 10:32:29 +00001004int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size)
1005{
1006 struct linux_efi_memreserve *rsv;
1007 unsigned long prsv;
1008 int rc, index;
1009
1010 if (efi_memreserve_root == (void *)ULONG_MAX)
1011 return -ENODEV;
1012
1013 if (!efi_memreserve_root) {
1014 rc = efi_memreserve_map_root();
1015 if (rc)
1016 return rc;
1017 }
1018
1019 /* first try to find a slot in an existing linked list entry */
Olivier Deprez0e641232021-09-23 10:07:05 +02001020 for (prsv = efi_memreserve_root->next; prsv; ) {
David Brazdil0f672f62019-12-10 10:32:29 +00001021 rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
1022 index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size);
1023 if (index < rsv->size) {
1024 rsv->entry[index].base = addr;
1025 rsv->entry[index].size = size;
1026
1027 memunmap(rsv);
Olivier Deprez0e641232021-09-23 10:07:05 +02001028 return efi_mem_reserve_iomem(addr, size);
David Brazdil0f672f62019-12-10 10:32:29 +00001029 }
Olivier Deprez0e641232021-09-23 10:07:05 +02001030 prsv = rsv->next;
David Brazdil0f672f62019-12-10 10:32:29 +00001031 memunmap(rsv);
1032 }
1033
1034 /* no slot found - allocate a new linked list entry */
1035 rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC);
1036 if (!rsv)
1037 return -ENOMEM;
1038
Olivier Deprez0e641232021-09-23 10:07:05 +02001039 rc = efi_mem_reserve_iomem(__pa(rsv), SZ_4K);
1040 if (rc) {
1041 free_page((unsigned long)rsv);
1042 return rc;
1043 }
1044
David Brazdil0f672f62019-12-10 10:32:29 +00001045 /*
1046 * The memremap() call above assumes that a linux_efi_memreserve entry
1047 * never crosses a page boundary, so let's ensure that this remains true
1048 * even when kexec'ing a 4k pages kernel from a >4k pages kernel, by
1049 * using SZ_4K explicitly in the size calculation below.
1050 */
1051 rsv->size = EFI_MEMRESERVE_COUNT(SZ_4K);
1052 atomic_set(&rsv->count, 1);
1053 rsv->entry[0].base = addr;
1054 rsv->entry[0].size = size;
1055
1056 spin_lock(&efi_mem_reserve_persistent_lock);
1057 rsv->next = efi_memreserve_root->next;
1058 efi_memreserve_root->next = __pa(rsv);
1059 spin_unlock(&efi_mem_reserve_persistent_lock);
1060
Olivier Deprez0e641232021-09-23 10:07:05 +02001061 return efi_mem_reserve_iomem(addr, size);
David Brazdil0f672f62019-12-10 10:32:29 +00001062}
1063
1064static int __init efi_memreserve_root_init(void)
1065{
1066 if (efi_memreserve_root)
1067 return 0;
1068 if (efi_memreserve_map_root())
1069 efi_memreserve_root = (void *)ULONG_MAX;
1070 return 0;
1071}
1072early_initcall(efi_memreserve_root_init);
1073
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001074#ifdef CONFIG_KEXEC
1075static int update_efi_random_seed(struct notifier_block *nb,
1076 unsigned long code, void *unused)
1077{
1078 struct linux_efi_random_seed *seed;
1079 u32 size = 0;
1080
1081 if (!kexec_in_progress)
1082 return NOTIFY_DONE;
1083
1084 seed = memremap(efi.rng_seed, sizeof(*seed), MEMREMAP_WB);
1085 if (seed != NULL) {
1086 size = min(seed->size, EFI_RANDOM_SEED_SIZE);
1087 memunmap(seed);
1088 } else {
1089 pr_err("Could not map UEFI random seed!\n");
1090 }
1091 if (size > 0) {
1092 seed = memremap(efi.rng_seed, sizeof(*seed) + size,
1093 MEMREMAP_WB);
1094 if (seed != NULL) {
1095 seed->size = size;
1096 get_random_bytes(seed->bits, seed->size);
1097 memunmap(seed);
1098 } else {
1099 pr_err("Could not map UEFI random seed!\n");
1100 }
1101 }
1102 return NOTIFY_DONE;
1103}
1104
1105static struct notifier_block efi_random_seed_nb = {
1106 .notifier_call = update_efi_random_seed,
1107};
1108
1109static int register_update_efi_random_seed(void)
1110{
1111 if (efi.rng_seed == EFI_INVALID_TABLE_ADDR)
1112 return 0;
1113 return register_reboot_notifier(&efi_random_seed_nb);
1114}
1115late_initcall(register_update_efi_random_seed);
1116#endif