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