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