blob: cd42f66a7c85a70801b2322ded2359fc22859ca6 [file] [log] [blame]
Tom Gundersena9499fa2013-02-08 15:37:06 +00001/*
2 * efi.c - EFI subsystem
3 *
4 * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
5 * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
6 * Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
7 *
8 * This code registers /sys/firmware/efi{,/efivars} when EFI is supported,
9 * allowing the efivarfs to be mounted or the efivars module to be loaded.
10 * The existance of /sys/firmware/efi may also be used by userspace to
11 * determine that the system supports EFI.
12 *
13 * This file is released under the GPLv2.
14 */
15
Leif Lindholm272686b2013-09-05 11:34:54 +010016#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
Tom Gundersena9499fa2013-02-08 15:37:06 +000018#include <linux/kobject.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/device.h>
22#include <linux/efi.h>
Mark Salter0302f712013-12-30 12:12:12 -050023#include <linux/of.h>
24#include <linux/of_fdt.h>
Leif Lindholm272686b2013-09-05 11:34:54 +010025#include <linux/io.h>
Ard Biesheuvel63625982016-11-12 21:32:31 +000026#include <linux/kexec.h>
Lee, Chun-Yi28d54022014-07-09 18:39:29 +080027#include <linux/platform_device.h>
Ard Biesheuvel63625982016-11-12 21:32:31 +000028#include <linux/random.h>
29#include <linux/reboot.h>
Octavian Purdila475fb4e2016-07-08 19:13:12 +030030#include <linux/slab.h>
31#include <linux/acpi.h>
32#include <linux/ucs2_string.h>
Matt Fleming816e7612016-02-29 21:22:52 +000033#include <linux/memblock.h>
Leif Lindholm272686b2013-09-05 11:34:54 +010034
Ard Biesheuvel0f7f2f02016-01-12 14:22:46 +010035#include <asm/early_ioremap.h>
Ard Biesheuvelf7d92482015-11-30 13:28:19 +010036
Leif Lindholm272686b2013-09-05 11:34:54 +010037struct efi __read_mostly efi = {
Ard Biesheuvelbf924862015-09-09 10:08:15 +020038 .mps = EFI_INVALID_TABLE_ADDR,
39 .acpi = EFI_INVALID_TABLE_ADDR,
40 .acpi20 = EFI_INVALID_TABLE_ADDR,
41 .smbios = EFI_INVALID_TABLE_ADDR,
42 .smbios3 = EFI_INVALID_TABLE_ADDR,
43 .sal_systab = EFI_INVALID_TABLE_ADDR,
44 .boot_info = EFI_INVALID_TABLE_ADDR,
45 .hcdp = EFI_INVALID_TABLE_ADDR,
46 .uga = EFI_INVALID_TABLE_ADDR,
47 .uv_systab = EFI_INVALID_TABLE_ADDR,
48 .fw_vendor = EFI_INVALID_TABLE_ADDR,
49 .runtime = EFI_INVALID_TABLE_ADDR,
50 .config_table = EFI_INVALID_TABLE_ADDR,
51 .esrt = EFI_INVALID_TABLE_ADDR,
52 .properties_table = EFI_INVALID_TABLE_ADDR,
Ard Biesheuvela604af02016-04-25 21:06:44 +010053 .mem_attr_table = EFI_INVALID_TABLE_ADDR,
Ard Biesheuvel63625982016-11-12 21:32:31 +000054 .rng_seed = EFI_INVALID_TABLE_ADDR,
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +020055 .tpm_log = EFI_INVALID_TABLE_ADDR
Leif Lindholm272686b2013-09-05 11:34:54 +010056};
57EXPORT_SYMBOL(efi);
Tom Gundersena9499fa2013-02-08 15:37:06 +000058
Tom Lendackya19d66c2017-07-17 16:10:13 -050059static unsigned long *efi_tables[] = {
60 &efi.mps,
61 &efi.acpi,
62 &efi.acpi20,
63 &efi.smbios,
64 &efi.smbios3,
65 &efi.sal_systab,
66 &efi.boot_info,
67 &efi.hcdp,
68 &efi.uga,
69 &efi.uv_systab,
70 &efi.fw_vendor,
71 &efi.runtime,
72 &efi.config_table,
73 &efi.esrt,
74 &efi.properties_table,
75 &efi.mem_attr_table,
76};
77
Dave Youngb2e0a542014-08-14 17:15:26 +080078static bool disable_runtime;
79static int __init setup_noefi(char *arg)
80{
81 disable_runtime = true;
82 return 0;
83}
84early_param("noefi", setup_noefi);
85
86bool efi_runtime_disabled(void)
87{
88 return disable_runtime;
89}
90
Dave Young5ae36832014-08-14 17:15:28 +080091static int __init parse_efi_cmdline(char *str)
92{
Ricardo Neri9115c752015-07-15 19:36:03 -070093 if (!str) {
94 pr_warn("need at least one option\n");
95 return -EINVAL;
96 }
97
Leif Lindholm12dd00e2015-08-26 14:24:56 +010098 if (parse_option_str(str, "debug"))
99 set_bit(EFI_DBG, &efi.flags);
100
Dave Young5ae36832014-08-14 17:15:28 +0800101 if (parse_option_str(str, "noruntime"))
102 disable_runtime = true;
103
104 return 0;
105}
106early_param("efi", parse_efi_cmdline);
107
Peter Jones0bb54902015-04-28 18:44:31 -0400108struct kobject *efi_kobj;
Tom Gundersena9499fa2013-02-08 15:37:06 +0000109
110/*
111 * Let's not leave out systab information that snuck into
112 * the efivars driver
Dave Young0b02e442017-12-06 09:50:10 +0000113 * Note, do not add more fields in systab sysfs file as it breaks sysfs
114 * one value per file rule!
Tom Gundersena9499fa2013-02-08 15:37:06 +0000115 */
116static ssize_t systab_show(struct kobject *kobj,
117 struct kobj_attribute *attr, char *buf)
118{
119 char *str = buf;
120
121 if (!kobj || !buf)
122 return -EINVAL;
123
124 if (efi.mps != EFI_INVALID_TABLE_ADDR)
125 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
126 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
127 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
128 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
129 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
Jean Delvareb119fe02015-04-30 15:23:05 +0200130 /*
131 * If both SMBIOS and SMBIOS3 entry points are implemented, the
132 * SMBIOS3 entry point shall be preferred, so we list it first to
133 * let applications stop parsing after the first match.
134 */
Ard Biesheuvele1ccbbc2014-10-14 16:34:47 +0200135 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
136 str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
Jean Delvareb119fe02015-04-30 15:23:05 +0200137 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
138 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
Tom Gundersena9499fa2013-02-08 15:37:06 +0000139 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
140 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
141 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
142 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
143 if (efi.uga != EFI_INVALID_TABLE_ADDR)
144 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
145
146 return str - buf;
147}
148
Greg Kroah-Hartmanaf97a772017-12-06 09:50:08 +0000149static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
Tom Gundersena9499fa2013-02-08 15:37:06 +0000150
Dave Younga0998eb2013-12-20 18:02:17 +0800151#define EFI_FIELD(var) efi.var
152
153#define EFI_ATTR_SHOW(name) \
154static ssize_t name##_show(struct kobject *kobj, \
155 struct kobj_attribute *attr, char *buf) \
156{ \
157 return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
158}
159
160EFI_ATTR_SHOW(fw_vendor);
161EFI_ATTR_SHOW(runtime);
162EFI_ATTR_SHOW(config_table);
163
Steve McIntyre2859dff2015-01-09 15:29:53 +0000164static ssize_t fw_platform_size_show(struct kobject *kobj,
165 struct kobj_attribute *attr, char *buf)
166{
167 return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32);
168}
169
Dave Younga0998eb2013-12-20 18:02:17 +0800170static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
171static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
172static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
Steve McIntyre2859dff2015-01-09 15:29:53 +0000173static struct kobj_attribute efi_attr_fw_platform_size =
174 __ATTR_RO(fw_platform_size);
Dave Younga0998eb2013-12-20 18:02:17 +0800175
Tom Gundersena9499fa2013-02-08 15:37:06 +0000176static struct attribute *efi_subsys_attrs[] = {
177 &efi_attr_systab.attr,
Dave Younga0998eb2013-12-20 18:02:17 +0800178 &efi_attr_fw_vendor.attr,
179 &efi_attr_runtime.attr,
180 &efi_attr_config_table.attr,
Steve McIntyre2859dff2015-01-09 15:29:53 +0000181 &efi_attr_fw_platform_size.attr,
Dave Younga0998eb2013-12-20 18:02:17 +0800182 NULL,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000183};
184
Dave Younga0998eb2013-12-20 18:02:17 +0800185static umode_t efi_attr_is_visible(struct kobject *kobj,
186 struct attribute *attr, int n)
187{
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200188 if (attr == &efi_attr_fw_vendor.attr) {
189 if (efi_enabled(EFI_PARAVIRT) ||
190 efi.fw_vendor == EFI_INVALID_TABLE_ADDR)
191 return 0;
192 } else if (attr == &efi_attr_runtime.attr) {
193 if (efi.runtime == EFI_INVALID_TABLE_ADDR)
194 return 0;
195 } else if (attr == &efi_attr_config_table.attr) {
196 if (efi.config_table == EFI_INVALID_TABLE_ADDR)
197 return 0;
198 }
Dave Younga0998eb2013-12-20 18:02:17 +0800199
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200200 return attr->mode;
Dave Younga0998eb2013-12-20 18:02:17 +0800201}
202
Arvind Yadav3ad6bd72017-08-18 20:49:46 +0100203static const struct attribute_group efi_subsys_attr_group = {
Tom Gundersena9499fa2013-02-08 15:37:06 +0000204 .attrs = efi_subsys_attrs,
Dave Younga0998eb2013-12-20 18:02:17 +0800205 .is_visible = efi_attr_is_visible,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000206};
207
208static struct efivars generic_efivars;
209static struct efivar_operations generic_ops;
210
211static int generic_ops_register(void)
212{
213 generic_ops.get_variable = efi.get_variable;
214 generic_ops.set_variable = efi.set_variable;
Ard Biesheuvel9c6672a2016-02-01 22:06:55 +0000215 generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
Tom Gundersena9499fa2013-02-08 15:37:06 +0000216 generic_ops.get_next_variable = efi.get_next_variable;
Matt Fleminga614e192013-04-30 11:30:24 +0100217 generic_ops.query_variable_store = efi_query_variable_store;
Tom Gundersena9499fa2013-02-08 15:37:06 +0000218
219 return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
220}
221
222static void generic_ops_unregister(void)
223{
224 efivars_unregister(&generic_efivars);
225}
226
Octavian Purdila475fb4e2016-07-08 19:13:12 +0300227#if IS_ENABLED(CONFIG_ACPI)
228#define EFIVAR_SSDT_NAME_MAX 16
229static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
230static int __init efivar_ssdt_setup(char *str)
231{
232 if (strlen(str) < sizeof(efivar_ssdt))
233 memcpy(efivar_ssdt, str, strlen(str));
234 else
235 pr_warn("efivar_ssdt: name too long: %s\n", str);
236 return 0;
237}
238__setup("efivar_ssdt=", efivar_ssdt_setup);
239
240static __init int efivar_ssdt_iter(efi_char16_t *name, efi_guid_t vendor,
241 unsigned long name_size, void *data)
242{
243 struct efivar_entry *entry;
244 struct list_head *list = data;
245 char utf8_name[EFIVAR_SSDT_NAME_MAX];
246 int limit = min_t(unsigned long, EFIVAR_SSDT_NAME_MAX, name_size);
247
248 ucs2_as_utf8(utf8_name, name, limit - 1);
249 if (strncmp(utf8_name, efivar_ssdt, limit) != 0)
250 return 0;
251
252 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
253 if (!entry)
254 return 0;
255
256 memcpy(entry->var.VariableName, name, name_size);
257 memcpy(&entry->var.VendorGuid, &vendor, sizeof(efi_guid_t));
258
259 efivar_entry_add(entry, list);
260
261 return 0;
262}
263
264static __init int efivar_ssdt_load(void)
265{
266 LIST_HEAD(entries);
267 struct efivar_entry *entry, *aux;
268 unsigned long size;
269 void *data;
270 int ret;
271
272 ret = efivar_init(efivar_ssdt_iter, &entries, true, &entries);
273
274 list_for_each_entry_safe(entry, aux, &entries, list) {
275 pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt,
276 &entry->var.VendorGuid);
277
278 list_del(&entry->list);
279
280 ret = efivar_entry_size(entry, &size);
281 if (ret) {
282 pr_err("failed to get var size\n");
283 goto free_entry;
284 }
285
286 data = kmalloc(size, GFP_KERNEL);
Dan Carpentera75dcb52016-10-18 15:33:18 +0100287 if (!data) {
288 ret = -ENOMEM;
Octavian Purdila475fb4e2016-07-08 19:13:12 +0300289 goto free_entry;
Dan Carpentera75dcb52016-10-18 15:33:18 +0100290 }
Octavian Purdila475fb4e2016-07-08 19:13:12 +0300291
292 ret = efivar_entry_get(entry, NULL, &size, data);
293 if (ret) {
294 pr_err("failed to get var data\n");
295 goto free_data;
296 }
297
298 ret = acpi_load_table(data);
299 if (ret) {
300 pr_err("failed to load table: %d\n", ret);
301 goto free_data;
302 }
303
304 goto free_entry;
305
306free_data:
307 kfree(data);
308
309free_entry:
310 kfree(entry);
311 }
312
313 return ret;
314}
315#else
316static inline int efivar_ssdt_load(void) { return 0; }
317#endif
318
Tom Gundersena9499fa2013-02-08 15:37:06 +0000319/*
320 * We register the efi subsystem with the firmware subsystem and the
321 * efivars subsystem with the efi subsystem, if the system was booted with
322 * EFI.
323 */
324static int __init efisubsys_init(void)
325{
326 int error;
327
328 if (!efi_enabled(EFI_BOOT))
329 return 0;
330
331 /* We register the efi directory at /sys/firmware/efi */
332 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
333 if (!efi_kobj) {
334 pr_err("efi: Firmware registration failed.\n");
335 return -ENOMEM;
336 }
337
338 error = generic_ops_register();
339 if (error)
340 goto err_put;
341
Octavian Purdila475fb4e2016-07-08 19:13:12 +0300342 if (efi_enabled(EFI_RUNTIME_SERVICES))
343 efivar_ssdt_load();
344
Tom Gundersena9499fa2013-02-08 15:37:06 +0000345 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
346 if (error) {
347 pr_err("efi: Sysfs attribute export failed with error %d.\n",
348 error);
349 goto err_unregister;
350 }
351
Dave Young926172d2013-12-20 18:02:18 +0800352 error = efi_runtime_map_init(efi_kobj);
353 if (error)
354 goto err_remove_group;
355
Tom Gundersena9499fa2013-02-08 15:37:06 +0000356 /* and the standard mountpoint for efivarfs */
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -0500357 error = sysfs_create_mount_point(efi_kobj, "efivars");
358 if (error) {
Tom Gundersena9499fa2013-02-08 15:37:06 +0000359 pr_err("efivars: Subsystem registration failed.\n");
Tom Gundersena9499fa2013-02-08 15:37:06 +0000360 goto err_remove_group;
361 }
362
363 return 0;
364
365err_remove_group:
366 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
367err_unregister:
368 generic_ops_unregister();
369err_put:
370 kobject_put(efi_kobj);
371 return error;
372}
373
374subsys_initcall(efisubsys_init);
Leif Lindholm272686b2013-09-05 11:34:54 +0100375
Peter Jones0bb54902015-04-28 18:44:31 -0400376/*
377 * Find the efi memory descriptor for a given physical address. Given a
Matt Flemingdca0f972016-02-27 15:52:50 +0000378 * physical address, determine if it exists within an EFI Memory Map entry,
Peter Jones0bb54902015-04-28 18:44:31 -0400379 * and if so, populate the supplied memory descriptor with the appropriate
380 * data.
381 */
382int __init efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
383{
Matt Flemingdca0f972016-02-27 15:52:50 +0000384 efi_memory_desc_t *md;
Peter Jones0bb54902015-04-28 18:44:31 -0400385
386 if (!efi_enabled(EFI_MEMMAP)) {
387 pr_err_once("EFI_MEMMAP is not enabled.\n");
388 return -EINVAL;
389 }
390
Peter Jones0bb54902015-04-28 18:44:31 -0400391 if (!out_md) {
392 pr_err_once("out_md is null.\n");
393 return -EINVAL;
394 }
Peter Jones0bb54902015-04-28 18:44:31 -0400395
Matt Flemingdca0f972016-02-27 15:52:50 +0000396 for_each_efi_memory_desc(md) {
Peter Jones0bb54902015-04-28 18:44:31 -0400397 u64 size;
398 u64 end;
399
Peter Jones0bb54902015-04-28 18:44:31 -0400400 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
401 md->type != EFI_BOOT_SERVICES_DATA &&
402 md->type != EFI_RUNTIME_SERVICES_DATA) {
Peter Jones0bb54902015-04-28 18:44:31 -0400403 continue;
404 }
405
406 size = md->num_pages << EFI_PAGE_SHIFT;
407 end = md->phys_addr + size;
408 if (phys_addr >= md->phys_addr && phys_addr < end) {
409 memcpy(out_md, md, sizeof(*out_md));
Peter Jones0bb54902015-04-28 18:44:31 -0400410 return 0;
411 }
Peter Jones0bb54902015-04-28 18:44:31 -0400412 }
Peter Jones0bb54902015-04-28 18:44:31 -0400413 return -ENOENT;
414}
415
416/*
417 * Calculate the highest address of an efi memory descriptor.
418 */
419u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
420{
421 u64 size = md->num_pages << EFI_PAGE_SHIFT;
422 u64 end = md->phys_addr + size;
423 return end;
424}
Leif Lindholm272686b2013-09-05 11:34:54 +0100425
Matt Fleming816e7612016-02-29 21:22:52 +0000426void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}
427
428/**
429 * efi_mem_reserve - Reserve an EFI memory region
430 * @addr: Physical address to reserve
431 * @size: Size of reservation
432 *
433 * Mark a region as reserved from general kernel allocation and
434 * prevent it being released by efi_free_boot_services().
435 *
436 * This function should be called drivers once they've parsed EFI
437 * configuration tables to figure out where their data lives, e.g.
438 * efi_esrt_init().
439 */
440void __init efi_mem_reserve(phys_addr_t addr, u64 size)
441{
442 if (!memblock_is_region_reserved(addr, size))
443 memblock_reserve(addr, size);
444
445 /*
446 * Some architectures (x86) reserve all boot services ranges
447 * until efi_free_boot_services() because of buggy firmware
448 * implementations. This means the above memblock_reserve() is
449 * superfluous on x86 and instead what it needs to do is
450 * ensure the @start, @size is not freed.
451 */
452 efi_arch_mem_reserve(addr, size);
453}
454
Leif Lindholm272686b2013-09-05 11:34:54 +0100455static __initdata efi_config_table_type_t common_tables[] = {
456 {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
457 {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
458 {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
459 {MPS_TABLE_GUID, "MPS", &efi.mps},
460 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
461 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
Ard Biesheuvele1ccbbc2014-10-14 16:34:47 +0200462 {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3},
Leif Lindholm272686b2013-09-05 11:34:54 +0100463 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
Peter Jones0bb54902015-04-28 18:44:31 -0400464 {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt},
Ard Biesheuvelbf924862015-09-09 10:08:15 +0200465 {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table},
Ard Biesheuvela604af02016-04-25 21:06:44 +0100466 {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table},
Ard Biesheuvel63625982016-11-12 21:32:31 +0000467 {LINUX_EFI_RANDOM_SEED_TABLE_GUID, "RNG", &efi.rng_seed},
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +0200468 {LINUX_EFI_TPM_EVENT_LOG_GUID, "TPMEventLog", &efi.tpm_log},
Daeseok Youn69e60842014-02-13 17:16:36 +0900469 {NULL_GUID, NULL, NULL},
Leif Lindholm272686b2013-09-05 11:34:54 +0100470};
471
472static __init int match_config_table(efi_guid_t *guid,
473 unsigned long table,
474 efi_config_table_type_t *table_types)
475{
Leif Lindholm272686b2013-09-05 11:34:54 +0100476 int i;
477
478 if (table_types) {
Leif Lindholm272686b2013-09-05 11:34:54 +0100479 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
Leif Lindholm272686b2013-09-05 11:34:54 +0100480 if (!efi_guidcmp(*guid, table_types[i].guid)) {
481 *(table_types[i].ptr) = table;
Ard Biesheuvel801820b2016-04-25 21:06:53 +0100482 if (table_types[i].name)
483 pr_cont(" %s=0x%lx ",
484 table_types[i].name, table);
Leif Lindholm272686b2013-09-05 11:34:54 +0100485 return 1;
486 }
487 }
488 }
489
490 return 0;
491}
492
Ard Biesheuvel7bb68412014-10-18 15:04:15 +0200493int __init efi_config_parse_tables(void *config_tables, int count, int sz,
494 efi_config_table_type_t *arch_tables)
495{
496 void *tablep;
497 int i;
498
499 tablep = config_tables;
500 pr_info("");
501 for (i = 0; i < count; i++) {
502 efi_guid_t guid;
503 unsigned long table;
504
505 if (efi_enabled(EFI_64BIT)) {
506 u64 table64;
507 guid = ((efi_config_table_64_t *)tablep)->guid;
508 table64 = ((efi_config_table_64_t *)tablep)->table;
509 table = table64;
510#ifndef CONFIG_64BIT
511 if (table64 >> 32) {
512 pr_cont("\n");
513 pr_err("Table located above 4GB, disabling EFI.\n");
514 return -EINVAL;
515 }
516#endif
517 } else {
518 guid = ((efi_config_table_32_t *)tablep)->guid;
519 table = ((efi_config_table_32_t *)tablep)->table;
520 }
521
522 if (!match_config_table(&guid, table, common_tables))
523 match_config_table(&guid, table, arch_tables);
524
525 tablep += sz;
526 }
527 pr_cont("\n");
528 set_bit(EFI_CONFIG_TABLES, &efi.flags);
Ard Biesheuvela1041712015-09-23 07:29:34 -0700529
Ard Biesheuvel63625982016-11-12 21:32:31 +0000530 if (efi.rng_seed != EFI_INVALID_TABLE_ADDR) {
531 struct linux_efi_random_seed *seed;
532 u32 size = 0;
533
534 seed = early_memremap(efi.rng_seed, sizeof(*seed));
535 if (seed != NULL) {
536 size = seed->size;
537 early_memunmap(seed, sizeof(*seed));
538 } else {
539 pr_err("Could not map UEFI random seed!\n");
540 }
541 if (size > 0) {
542 seed = early_memremap(efi.rng_seed,
543 sizeof(*seed) + size);
544 if (seed != NULL) {
545 add_device_randomness(seed->bits, seed->size);
546 early_memunmap(seed, sizeof(*seed) + size);
Ard Biesheuvelc2ceb5f2017-08-25 16:50:16 +0100547 pr_notice("seeding entropy pool\n");
Ard Biesheuvel63625982016-11-12 21:32:31 +0000548 } else {
549 pr_err("Could not map UEFI random seed!\n");
550 }
551 }
552 }
553
Daniel Kiper457ea3f2017-06-22 12:51:36 +0200554 if (efi_enabled(EFI_MEMMAP))
555 efi_memattr_init();
Sai Praneeth3a6b6c62017-01-31 13:21:35 +0000556
Thiebaud Weksteen33b6d032017-09-20 10:13:39 +0200557 efi_tpm_eventlog_init();
558
Ard Biesheuvela1041712015-09-23 07:29:34 -0700559 /* Parse the EFI Properties table if it exists */
560 if (efi.properties_table != EFI_INVALID_TABLE_ADDR) {
561 efi_properties_table_t *tbl;
562
563 tbl = early_memremap(efi.properties_table, sizeof(*tbl));
564 if (tbl == NULL) {
565 pr_err("Could not map Properties table!\n");
566 return -ENOMEM;
567 }
568
569 if (tbl->memory_protection_attribute &
570 EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
571 set_bit(EFI_NX_PE_DATA, &efi.flags);
572
573 early_memunmap(tbl, sizeof(*tbl));
574 }
575
Ard Biesheuvel7bb68412014-10-18 15:04:15 +0200576 return 0;
577}
578
Leif Lindholm272686b2013-09-05 11:34:54 +0100579int __init efi_config_init(efi_config_table_type_t *arch_tables)
580{
Ard Biesheuvel7bb68412014-10-18 15:04:15 +0200581 void *config_tables;
582 int sz, ret;
Leif Lindholm272686b2013-09-05 11:34:54 +0100583
584 if (efi_enabled(EFI_64BIT))
585 sz = sizeof(efi_config_table_64_t);
586 else
587 sz = sizeof(efi_config_table_32_t);
588
589 /*
590 * Let's see what config tables the firmware passed to us.
591 */
592 config_tables = early_memremap(efi.systab->tables,
593 efi.systab->nr_tables * sz);
594 if (config_tables == NULL) {
595 pr_err("Could not map Configuration table!\n");
596 return -ENOMEM;
597 }
598
Ard Biesheuvel7bb68412014-10-18 15:04:15 +0200599 ret = efi_config_parse_tables(config_tables, efi.systab->nr_tables, sz,
600 arch_tables);
Leif Lindholm272686b2013-09-05 11:34:54 +0100601
Daniel Kiperabc93f82014-06-30 19:52:56 +0200602 early_memunmap(config_tables, efi.systab->nr_tables * sz);
Ard Biesheuvel7bb68412014-10-18 15:04:15 +0200603 return ret;
Leif Lindholm272686b2013-09-05 11:34:54 +0100604}
Mark Salter0302f712013-12-30 12:12:12 -0500605
Lee, Chun-Yi28d54022014-07-09 18:39:29 +0800606#ifdef CONFIG_EFI_VARS_MODULE
607static int __init efi_load_efivars(void)
608{
609 struct platform_device *pdev;
610
611 if (!efi_enabled(EFI_RUNTIME_SERVICES))
612 return 0;
613
614 pdev = platform_device_register_simple("efivars", 0, NULL, 0);
Vasyl Gomonovych50342b22018-01-02 18:10:40 +0000615 return PTR_ERR_OR_ZERO(pdev);
Lee, Chun-Yi28d54022014-07-09 18:39:29 +0800616}
617device_initcall(efi_load_efivars);
618#endif
619
Mark Salter0302f712013-12-30 12:12:12 -0500620#ifdef CONFIG_EFI_PARAMS_FROM_FDT
621
622#define UEFI_PARAM(name, prop, field) \
623 { \
624 { name }, \
625 { prop }, \
626 offsetof(struct efi_fdt_params, field), \
627 FIELD_SIZEOF(struct efi_fdt_params, field) \
628 }
629
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800630struct params {
Mark Salter0302f712013-12-30 12:12:12 -0500631 const char name[32];
632 const char propname[32];
633 int offset;
634 int size;
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800635};
636
637static __initdata struct params fdt_params[] = {
Mark Salter0302f712013-12-30 12:12:12 -0500638 UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
639 UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
640 UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
641 UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
642 UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver)
643};
644
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800645static __initdata struct params xen_fdt_params[] = {
646 UEFI_PARAM("System Table", "xen,uefi-system-table", system_table),
647 UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap),
648 UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size),
649 UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size),
650 UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver)
651};
652
653#define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params)
654
655static __initdata struct {
656 const char *uname;
657 const char *subnode;
658 struct params *params;
659} dt_params[] = {
660 { "hypervisor", "uefi", xen_fdt_params },
661 { "chosen", NULL, fdt_params },
662};
663
Mark Salter0302f712013-12-30 12:12:12 -0500664struct param_info {
Catalin Marinas29e24352014-07-08 16:54:18 +0100665 int found;
Mark Salter0302f712013-12-30 12:12:12 -0500666 void *params;
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800667 const char *missing;
Mark Salter0302f712013-12-30 12:12:12 -0500668};
669
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800670static int __init __find_uefi_params(unsigned long node,
671 struct param_info *info,
672 struct params *params)
Mark Salter0302f712013-12-30 12:12:12 -0500673{
Catalin Marinas6fb8cc82014-06-02 11:31:06 +0100674 const void *prop;
675 void *dest;
Mark Salter0302f712013-12-30 12:12:12 -0500676 u64 val;
Catalin Marinas6fb8cc82014-06-02 11:31:06 +0100677 int i, len;
Mark Salter0302f712013-12-30 12:12:12 -0500678
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800679 for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) {
680 prop = of_get_flat_dt_prop(node, params[i].propname, &len);
681 if (!prop) {
682 info->missing = params[i].name;
Mark Salter0302f712013-12-30 12:12:12 -0500683 return 0;
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800684 }
685
686 dest = info->params + params[i].offset;
Catalin Marinas29e24352014-07-08 16:54:18 +0100687 info->found++;
Mark Salter0302f712013-12-30 12:12:12 -0500688
689 val = of_read_number(prop, len / sizeof(u32));
690
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800691 if (params[i].size == sizeof(u32))
Mark Salter0302f712013-12-30 12:12:12 -0500692 *(u32 *)dest = val;
693 else
694 *(u64 *)dest = val;
695
Leif Lindholm7968c0e2015-08-26 14:24:58 +0100696 if (efi_enabled(EFI_DBG))
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800697 pr_info(" %s: 0x%0*llx\n", params[i].name,
698 params[i].size * 2, val);
Mark Salter0302f712013-12-30 12:12:12 -0500699 }
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800700
Mark Salter0302f712013-12-30 12:12:12 -0500701 return 1;
702}
703
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800704static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
705 int depth, void *data)
706{
707 struct param_info *info = data;
708 int i;
709
710 for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
711 const char *subnode = dt_params[i].subnode;
712
713 if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) {
714 info->missing = dt_params[i].params[0].name;
715 continue;
716 }
717
718 if (subnode) {
Andrzej Hajda4af9ed52016-08-30 12:41:37 +0200719 int err = of_get_flat_dt_subnode_by_name(node, subnode);
720
721 if (err < 0)
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800722 return 0;
Andrzej Hajda4af9ed52016-08-30 12:41:37 +0200723
724 node = err;
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800725 }
726
727 return __find_uefi_params(node, info, dt_params[i].params);
728 }
729
730 return 0;
731}
732
Leif Lindholm7968c0e2015-08-26 14:24:58 +0100733int __init efi_get_fdt_params(struct efi_fdt_params *params)
Mark Salter0302f712013-12-30 12:12:12 -0500734{
735 struct param_info info;
Catalin Marinas29e24352014-07-08 16:54:18 +0100736 int ret;
737
738 pr_info("Getting EFI parameters from FDT:\n");
Mark Salter0302f712013-12-30 12:12:12 -0500739
Catalin Marinas29e24352014-07-08 16:54:18 +0100740 info.found = 0;
Mark Salter0302f712013-12-30 12:12:12 -0500741 info.params = params;
742
Catalin Marinas29e24352014-07-08 16:54:18 +0100743 ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
744 if (!info.found)
745 pr_info("UEFI not found.\n");
746 else if (!ret)
747 pr_err("Can't find '%s' in device tree!\n",
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800748 info.missing);
Catalin Marinas29e24352014-07-08 16:54:18 +0100749
750 return ret;
Mark Salter0302f712013-12-30 12:12:12 -0500751}
752#endif /* CONFIG_EFI_PARAMS_FROM_FDT */
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200753
754static __initdata char memory_type_name[][20] = {
755 "Reserved",
756 "Loader Code",
757 "Loader Data",
758 "Boot Code",
759 "Boot Data",
760 "Runtime Code",
761 "Runtime Data",
762 "Conventional Memory",
763 "Unusable Memory",
764 "ACPI Reclaim Memory",
765 "ACPI Memory NVS",
766 "Memory Mapped I/O",
767 "MMIO Port Space",
Robert Elliott35575e02016-02-01 22:07:07 +0000768 "PAL Code",
769 "Persistent Memory",
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200770};
771
772char * __init efi_md_typeattr_format(char *buf, size_t size,
773 const efi_memory_desc_t *md)
774{
775 char *pos;
776 int type_len;
777 u64 attr;
778
779 pos = buf;
780 if (md->type >= ARRAY_SIZE(memory_type_name))
781 type_len = snprintf(pos, size, "[type=%u", md->type);
782 else
783 type_len = snprintf(pos, size, "[%-*s",
784 (int)(sizeof(memory_type_name[0]) - 1),
785 memory_type_name[md->type]);
786 if (type_len >= size)
787 return buf;
788
789 pos += type_len;
790 size -= type_len;
791
792 attr = md->attribute;
793 if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
Ard Biesheuvel87db73ae2015-08-07 09:36:54 +0100794 EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
795 EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
Robert Elliottc016ca02016-02-01 22:07:06 +0000796 EFI_MEMORY_NV |
Taku Izumi8be44322015-08-27 02:11:19 +0900797 EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200798 snprintf(pos, size, "|attr=0x%016llx]",
799 (unsigned long long)attr);
800 else
Robert Elliottc016ca02016-02-01 22:07:06 +0000801 snprintf(pos, size,
802 "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200803 attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
Taku Izumi8be44322015-08-27 02:11:19 +0900804 attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
Robert Elliottc016ca02016-02-01 22:07:06 +0000805 attr & EFI_MEMORY_NV ? "NV" : "",
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200806 attr & EFI_MEMORY_XP ? "XP" : "",
807 attr & EFI_MEMORY_RP ? "RP" : "",
808 attr & EFI_MEMORY_WP ? "WP" : "",
Ard Biesheuvel87db73ae2015-08-07 09:36:54 +0100809 attr & EFI_MEMORY_RO ? "RO" : "",
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200810 attr & EFI_MEMORY_UCE ? "UCE" : "",
811 attr & EFI_MEMORY_WB ? "WB" : "",
812 attr & EFI_MEMORY_WT ? "WT" : "",
813 attr & EFI_MEMORY_WC ? "WC" : "",
814 attr & EFI_MEMORY_UC ? "UC" : "");
815 return buf;
816}
Jonathan (Zhixiong) Zhang7bf79312015-08-07 09:36:57 +0100817
818/*
Jan Beulich23f05712017-08-25 16:50:18 +0100819 * IA64 has a funky EFI memory map that doesn't work the same way as
820 * other architectures.
821 */
822#ifndef CONFIG_IA64
823/*
Jonathan (Zhixiong) Zhang7bf79312015-08-07 09:36:57 +0100824 * efi_mem_attributes - lookup memmap attributes for physical address
825 * @phys_addr: the physical address to lookup
826 *
827 * Search in the EFI memory map for the region covering
828 * @phys_addr. Returns the EFI memory attributes if the region
829 * was found in the memory map, 0 otherwise.
Jonathan (Zhixiong) Zhang7bf79312015-08-07 09:36:57 +0100830 */
Jan Beulich23f05712017-08-25 16:50:18 +0100831u64 efi_mem_attributes(unsigned long phys_addr)
Jonathan (Zhixiong) Zhang7bf79312015-08-07 09:36:57 +0100832{
833 efi_memory_desc_t *md;
Jonathan (Zhixiong) Zhang7bf79312015-08-07 09:36:57 +0100834
835 if (!efi_enabled(EFI_MEMMAP))
836 return 0;
837
Matt Fleming78ce2482016-04-25 21:06:38 +0100838 for_each_efi_memory_desc(md) {
Jonathan (Zhixiong) Zhang7bf79312015-08-07 09:36:57 +0100839 if ((md->phys_addr <= phys_addr) &&
840 (phys_addr < (md->phys_addr +
841 (md->num_pages << EFI_PAGE_SHIFT))))
842 return md->attribute;
843 }
844 return 0;
845}
Matt Fleming806b0352016-04-25 21:06:58 +0100846
Jan Beulich23f05712017-08-25 16:50:18 +0100847/*
848 * efi_mem_type - lookup memmap type for physical address
849 * @phys_addr: the physical address to lookup
850 *
851 * Search in the EFI memory map for the region covering @phys_addr.
852 * Returns the EFI memory type if the region was found in the memory
853 * map, EFI_RESERVED_TYPE (zero) otherwise.
854 */
855int efi_mem_type(unsigned long phys_addr)
856{
857 const efi_memory_desc_t *md;
858
859 if (!efi_enabled(EFI_MEMMAP))
860 return -ENOTSUPP;
861
862 for_each_efi_memory_desc(md) {
863 if ((md->phys_addr <= phys_addr) &&
864 (phys_addr < (md->phys_addr +
865 (md->num_pages << EFI_PAGE_SHIFT))))
866 return md->type;
867 }
868 return -EINVAL;
869}
870#endif
871
Matt Fleming806b0352016-04-25 21:06:58 +0100872int efi_status_to_err(efi_status_t status)
873{
874 int err;
875
876 switch (status) {
877 case EFI_SUCCESS:
878 err = 0;
879 break;
880 case EFI_INVALID_PARAMETER:
881 err = -EINVAL;
882 break;
883 case EFI_OUT_OF_RESOURCES:
884 err = -ENOSPC;
885 break;
886 case EFI_DEVICE_ERROR:
887 err = -EIO;
888 break;
889 case EFI_WRITE_PROTECTED:
890 err = -EROFS;
891 break;
892 case EFI_SECURITY_VIOLATION:
893 err = -EACCES;
894 break;
895 case EFI_NOT_FOUND:
896 err = -ENOENT;
897 break;
Ard Biesheuveldce48e32016-07-15 21:36:31 +0200898 case EFI_ABORTED:
899 err = -EINTR;
900 break;
Matt Fleming806b0352016-04-25 21:06:58 +0100901 default:
902 err = -EINVAL;
903 }
904
905 return err;
906}
Ard Biesheuvel63625982016-11-12 21:32:31 +0000907
Tom Lendackya19d66c2017-07-17 16:10:13 -0500908bool efi_is_table_address(unsigned long phys_addr)
909{
910 unsigned int i;
911
912 if (phys_addr == EFI_INVALID_TABLE_ADDR)
913 return false;
914
915 for (i = 0; i < ARRAY_SIZE(efi_tables); i++)
916 if (*(efi_tables[i]) == phys_addr)
917 return true;
918
919 return false;
920}
921
Ard Biesheuvel63625982016-11-12 21:32:31 +0000922#ifdef CONFIG_KEXEC
923static int update_efi_random_seed(struct notifier_block *nb,
924 unsigned long code, void *unused)
925{
926 struct linux_efi_random_seed *seed;
927 u32 size = 0;
928
929 if (!kexec_in_progress)
930 return NOTIFY_DONE;
931
932 seed = memremap(efi.rng_seed, sizeof(*seed), MEMREMAP_WB);
933 if (seed != NULL) {
Ard Biesheuvelc2ceb5f2017-08-25 16:50:16 +0100934 size = min(seed->size, EFI_RANDOM_SEED_SIZE);
Ard Biesheuvel63625982016-11-12 21:32:31 +0000935 memunmap(seed);
936 } else {
937 pr_err("Could not map UEFI random seed!\n");
938 }
939 if (size > 0) {
940 seed = memremap(efi.rng_seed, sizeof(*seed) + size,
941 MEMREMAP_WB);
942 if (seed != NULL) {
943 seed->size = size;
944 get_random_bytes(seed->bits, seed->size);
945 memunmap(seed);
946 } else {
947 pr_err("Could not map UEFI random seed!\n");
948 }
949 }
950 return NOTIFY_DONE;
951}
952
953static struct notifier_block efi_random_seed_nb = {
954 .notifier_call = update_efi_random_seed,
955};
956
957static int register_update_efi_random_seed(void)
958{
959 if (efi.rng_seed == EFI_INVALID_TABLE_ADDR)
960 return 0;
961 return register_reboot_notifier(&efi_random_seed_nb);
962}
963late_initcall(register_update_efi_random_seed);
964#endif