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