blob: c1879999abe76ae1f803cf768a30954464c806a9 [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>
Lee, Chun-Yi28d54022014-07-09 18:39:29 +080026#include <linux/platform_device.h>
Octavian Purdila475fb4e2016-07-08 19:13:12 +030027#include <linux/slab.h>
28#include <linux/acpi.h>
29#include <linux/ucs2_string.h>
Leif Lindholm272686b2013-09-05 11:34:54 +010030
Ard Biesheuvel0f7f2f02016-01-12 14:22:46 +010031#include <asm/early_ioremap.h>
Ard Biesheuvelf7d92482015-11-30 13:28:19 +010032
Leif Lindholm272686b2013-09-05 11:34:54 +010033struct efi __read_mostly efi = {
Ard Biesheuvelbf924862015-09-09 10:08:15 +020034 .mps = EFI_INVALID_TABLE_ADDR,
35 .acpi = EFI_INVALID_TABLE_ADDR,
36 .acpi20 = EFI_INVALID_TABLE_ADDR,
37 .smbios = EFI_INVALID_TABLE_ADDR,
38 .smbios3 = EFI_INVALID_TABLE_ADDR,
39 .sal_systab = EFI_INVALID_TABLE_ADDR,
40 .boot_info = EFI_INVALID_TABLE_ADDR,
41 .hcdp = EFI_INVALID_TABLE_ADDR,
42 .uga = EFI_INVALID_TABLE_ADDR,
43 .uv_systab = EFI_INVALID_TABLE_ADDR,
44 .fw_vendor = EFI_INVALID_TABLE_ADDR,
45 .runtime = EFI_INVALID_TABLE_ADDR,
46 .config_table = EFI_INVALID_TABLE_ADDR,
47 .esrt = EFI_INVALID_TABLE_ADDR,
48 .properties_table = EFI_INVALID_TABLE_ADDR,
Ard Biesheuvela604af02016-04-25 21:06:44 +010049 .mem_attr_table = EFI_INVALID_TABLE_ADDR,
Leif Lindholm272686b2013-09-05 11:34:54 +010050};
51EXPORT_SYMBOL(efi);
Tom Gundersena9499fa2013-02-08 15:37:06 +000052
Dave Youngb2e0a542014-08-14 17:15:26 +080053static bool disable_runtime;
54static int __init setup_noefi(char *arg)
55{
56 disable_runtime = true;
57 return 0;
58}
59early_param("noefi", setup_noefi);
60
61bool efi_runtime_disabled(void)
62{
63 return disable_runtime;
64}
65
Dave Young5ae36832014-08-14 17:15:28 +080066static int __init parse_efi_cmdline(char *str)
67{
Ricardo Neri9115c752015-07-15 19:36:03 -070068 if (!str) {
69 pr_warn("need at least one option\n");
70 return -EINVAL;
71 }
72
Leif Lindholm12dd00e2015-08-26 14:24:56 +010073 if (parse_option_str(str, "debug"))
74 set_bit(EFI_DBG, &efi.flags);
75
Dave Young5ae36832014-08-14 17:15:28 +080076 if (parse_option_str(str, "noruntime"))
77 disable_runtime = true;
78
79 return 0;
80}
81early_param("efi", parse_efi_cmdline);
82
Peter Jones0bb54902015-04-28 18:44:31 -040083struct kobject *efi_kobj;
Tom Gundersena9499fa2013-02-08 15:37:06 +000084
85/*
86 * Let's not leave out systab information that snuck into
87 * the efivars driver
88 */
89static ssize_t systab_show(struct kobject *kobj,
90 struct kobj_attribute *attr, char *buf)
91{
92 char *str = buf;
93
94 if (!kobj || !buf)
95 return -EINVAL;
96
97 if (efi.mps != EFI_INVALID_TABLE_ADDR)
98 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
99 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
100 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
101 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
102 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
Jean Delvareb119fe02015-04-30 15:23:05 +0200103 /*
104 * If both SMBIOS and SMBIOS3 entry points are implemented, the
105 * SMBIOS3 entry point shall be preferred, so we list it first to
106 * let applications stop parsing after the first match.
107 */
Ard Biesheuvele1ccbbc2014-10-14 16:34:47 +0200108 if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
109 str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
Jean Delvareb119fe02015-04-30 15:23:05 +0200110 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
111 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
Tom Gundersena9499fa2013-02-08 15:37:06 +0000112 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
113 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
114 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
115 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
116 if (efi.uga != EFI_INVALID_TABLE_ADDR)
117 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
118
119 return str - buf;
120}
121
122static struct kobj_attribute efi_attr_systab =
123 __ATTR(systab, 0400, systab_show, NULL);
124
Dave Younga0998eb2013-12-20 18:02:17 +0800125#define EFI_FIELD(var) efi.var
126
127#define EFI_ATTR_SHOW(name) \
128static ssize_t name##_show(struct kobject *kobj, \
129 struct kobj_attribute *attr, char *buf) \
130{ \
131 return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
132}
133
134EFI_ATTR_SHOW(fw_vendor);
135EFI_ATTR_SHOW(runtime);
136EFI_ATTR_SHOW(config_table);
137
Steve McIntyre2859dff2015-01-09 15:29:53 +0000138static ssize_t fw_platform_size_show(struct kobject *kobj,
139 struct kobj_attribute *attr, char *buf)
140{
141 return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32);
142}
143
Dave Younga0998eb2013-12-20 18:02:17 +0800144static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
145static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
146static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
Steve McIntyre2859dff2015-01-09 15:29:53 +0000147static struct kobj_attribute efi_attr_fw_platform_size =
148 __ATTR_RO(fw_platform_size);
Dave Younga0998eb2013-12-20 18:02:17 +0800149
Tom Gundersena9499fa2013-02-08 15:37:06 +0000150static struct attribute *efi_subsys_attrs[] = {
151 &efi_attr_systab.attr,
Dave Younga0998eb2013-12-20 18:02:17 +0800152 &efi_attr_fw_vendor.attr,
153 &efi_attr_runtime.attr,
154 &efi_attr_config_table.attr,
Steve McIntyre2859dff2015-01-09 15:29:53 +0000155 &efi_attr_fw_platform_size.attr,
Dave Younga0998eb2013-12-20 18:02:17 +0800156 NULL,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000157};
158
Dave Younga0998eb2013-12-20 18:02:17 +0800159static umode_t efi_attr_is_visible(struct kobject *kobj,
160 struct attribute *attr, int n)
161{
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200162 if (attr == &efi_attr_fw_vendor.attr) {
163 if (efi_enabled(EFI_PARAVIRT) ||
164 efi.fw_vendor == EFI_INVALID_TABLE_ADDR)
165 return 0;
166 } else if (attr == &efi_attr_runtime.attr) {
167 if (efi.runtime == EFI_INVALID_TABLE_ADDR)
168 return 0;
169 } else if (attr == &efi_attr_config_table.attr) {
170 if (efi.config_table == EFI_INVALID_TABLE_ADDR)
171 return 0;
172 }
Dave Younga0998eb2013-12-20 18:02:17 +0800173
Daniel Kiper9f27bc52014-06-30 19:52:58 +0200174 return attr->mode;
Dave Younga0998eb2013-12-20 18:02:17 +0800175}
176
Tom Gundersena9499fa2013-02-08 15:37:06 +0000177static struct attribute_group efi_subsys_attr_group = {
178 .attrs = efi_subsys_attrs,
Dave Younga0998eb2013-12-20 18:02:17 +0800179 .is_visible = efi_attr_is_visible,
Tom Gundersena9499fa2013-02-08 15:37:06 +0000180};
181
182static struct efivars generic_efivars;
183static struct efivar_operations generic_ops;
184
185static int generic_ops_register(void)
186{
187 generic_ops.get_variable = efi.get_variable;
188 generic_ops.set_variable = efi.set_variable;
Ard Biesheuvel9c6672a2016-02-01 22:06:55 +0000189 generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
Tom Gundersena9499fa2013-02-08 15:37:06 +0000190 generic_ops.get_next_variable = efi.get_next_variable;
Matt Fleminga614e192013-04-30 11:30:24 +0100191 generic_ops.query_variable_store = efi_query_variable_store;
Tom Gundersena9499fa2013-02-08 15:37:06 +0000192
193 return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
194}
195
196static void generic_ops_unregister(void)
197{
198 efivars_unregister(&generic_efivars);
199}
200
Octavian Purdila475fb4e2016-07-08 19:13:12 +0300201#if IS_ENABLED(CONFIG_ACPI)
202#define EFIVAR_SSDT_NAME_MAX 16
203static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
204static int __init efivar_ssdt_setup(char *str)
205{
206 if (strlen(str) < sizeof(efivar_ssdt))
207 memcpy(efivar_ssdt, str, strlen(str));
208 else
209 pr_warn("efivar_ssdt: name too long: %s\n", str);
210 return 0;
211}
212__setup("efivar_ssdt=", efivar_ssdt_setup);
213
214static __init int efivar_ssdt_iter(efi_char16_t *name, efi_guid_t vendor,
215 unsigned long name_size, void *data)
216{
217 struct efivar_entry *entry;
218 struct list_head *list = data;
219 char utf8_name[EFIVAR_SSDT_NAME_MAX];
220 int limit = min_t(unsigned long, EFIVAR_SSDT_NAME_MAX, name_size);
221
222 ucs2_as_utf8(utf8_name, name, limit - 1);
223 if (strncmp(utf8_name, efivar_ssdt, limit) != 0)
224 return 0;
225
226 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
227 if (!entry)
228 return 0;
229
230 memcpy(entry->var.VariableName, name, name_size);
231 memcpy(&entry->var.VendorGuid, &vendor, sizeof(efi_guid_t));
232
233 efivar_entry_add(entry, list);
234
235 return 0;
236}
237
238static __init int efivar_ssdt_load(void)
239{
240 LIST_HEAD(entries);
241 struct efivar_entry *entry, *aux;
242 unsigned long size;
243 void *data;
244 int ret;
245
246 ret = efivar_init(efivar_ssdt_iter, &entries, true, &entries);
247
248 list_for_each_entry_safe(entry, aux, &entries, list) {
249 pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt,
250 &entry->var.VendorGuid);
251
252 list_del(&entry->list);
253
254 ret = efivar_entry_size(entry, &size);
255 if (ret) {
256 pr_err("failed to get var size\n");
257 goto free_entry;
258 }
259
260 data = kmalloc(size, GFP_KERNEL);
261 if (!data)
262 goto free_entry;
263
264 ret = efivar_entry_get(entry, NULL, &size, data);
265 if (ret) {
266 pr_err("failed to get var data\n");
267 goto free_data;
268 }
269
270 ret = acpi_load_table(data);
271 if (ret) {
272 pr_err("failed to load table: %d\n", ret);
273 goto free_data;
274 }
275
276 goto free_entry;
277
278free_data:
279 kfree(data);
280
281free_entry:
282 kfree(entry);
283 }
284
285 return ret;
286}
287#else
288static inline int efivar_ssdt_load(void) { return 0; }
289#endif
290
Tom Gundersena9499fa2013-02-08 15:37:06 +0000291/*
292 * We register the efi subsystem with the firmware subsystem and the
293 * efivars subsystem with the efi subsystem, if the system was booted with
294 * EFI.
295 */
296static int __init efisubsys_init(void)
297{
298 int error;
299
300 if (!efi_enabled(EFI_BOOT))
301 return 0;
302
303 /* We register the efi directory at /sys/firmware/efi */
304 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
305 if (!efi_kobj) {
306 pr_err("efi: Firmware registration failed.\n");
307 return -ENOMEM;
308 }
309
310 error = generic_ops_register();
311 if (error)
312 goto err_put;
313
Octavian Purdila475fb4e2016-07-08 19:13:12 +0300314 if (efi_enabled(EFI_RUNTIME_SERVICES))
315 efivar_ssdt_load();
316
Tom Gundersena9499fa2013-02-08 15:37:06 +0000317 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
318 if (error) {
319 pr_err("efi: Sysfs attribute export failed with error %d.\n",
320 error);
321 goto err_unregister;
322 }
323
Dave Young926172d2013-12-20 18:02:18 +0800324 error = efi_runtime_map_init(efi_kobj);
325 if (error)
326 goto err_remove_group;
327
Tom Gundersena9499fa2013-02-08 15:37:06 +0000328 /* and the standard mountpoint for efivarfs */
Eric W. Biedermanf9bb4882015-05-13 17:35:41 -0500329 error = sysfs_create_mount_point(efi_kobj, "efivars");
330 if (error) {
Tom Gundersena9499fa2013-02-08 15:37:06 +0000331 pr_err("efivars: Subsystem registration failed.\n");
Tom Gundersena9499fa2013-02-08 15:37:06 +0000332 goto err_remove_group;
333 }
334
335 return 0;
336
337err_remove_group:
338 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
339err_unregister:
340 generic_ops_unregister();
341err_put:
342 kobject_put(efi_kobj);
343 return error;
344}
345
346subsys_initcall(efisubsys_init);
Leif Lindholm272686b2013-09-05 11:34:54 +0100347
Peter Jones0bb54902015-04-28 18:44:31 -0400348/*
349 * Find the efi memory descriptor for a given physical address. Given a
350 * physicall address, determine if it exists within an EFI Memory Map entry,
351 * and if so, populate the supplied memory descriptor with the appropriate
352 * data.
353 */
354int __init efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
355{
Matt Fleming884f4f62016-04-25 21:06:39 +0100356 struct efi_memory_map *map = &efi.memmap;
Ard Biesheuvel44511fb2015-10-23 11:48:16 +0200357 phys_addr_t p, e;
Peter Jones0bb54902015-04-28 18:44:31 -0400358
359 if (!efi_enabled(EFI_MEMMAP)) {
360 pr_err_once("EFI_MEMMAP is not enabled.\n");
361 return -EINVAL;
362 }
363
364 if (!map) {
365 pr_err_once("efi.memmap is not set.\n");
366 return -EINVAL;
367 }
368 if (!out_md) {
369 pr_err_once("out_md is null.\n");
370 return -EINVAL;
371 }
372 if (WARN_ON_ONCE(!map->phys_map))
373 return -EINVAL;
374 if (WARN_ON_ONCE(map->nr_map == 0) || WARN_ON_ONCE(map->desc_size == 0))
375 return -EINVAL;
376
377 e = map->phys_map + map->nr_map * map->desc_size;
378 for (p = map->phys_map; p < e; p += map->desc_size) {
379 efi_memory_desc_t *md;
380 u64 size;
381 u64 end;
382
383 /*
384 * If a driver calls this after efi_free_boot_services,
385 * ->map will be NULL, and the target may also not be mapped.
386 * So just always get our own virtual map on the CPU.
387 *
388 */
Ard Biesheuvel44511fb2015-10-23 11:48:16 +0200389 md = early_memremap(p, sizeof (*md));
Peter Jones0bb54902015-04-28 18:44:31 -0400390 if (!md) {
Ard Biesheuvel44511fb2015-10-23 11:48:16 +0200391 pr_err_once("early_memremap(%pa, %zu) failed.\n",
392 &p, sizeof (*md));
Peter Jones0bb54902015-04-28 18:44:31 -0400393 return -ENOMEM;
394 }
395
396 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
397 md->type != EFI_BOOT_SERVICES_DATA &&
398 md->type != EFI_RUNTIME_SERVICES_DATA) {
399 early_memunmap(md, sizeof (*md));
400 continue;
401 }
402
403 size = md->num_pages << EFI_PAGE_SHIFT;
404 end = md->phys_addr + size;
405 if (phys_addr >= md->phys_addr && phys_addr < end) {
406 memcpy(out_md, md, sizeof(*out_md));
407 early_memunmap(md, sizeof (*md));
408 return 0;
409 }
410
411 early_memunmap(md, sizeof (*md));
412 }
413 pr_err_once("requested map not found.\n");
414 return -ENOENT;
415}
416
417/*
418 * Calculate the highest address of an efi memory descriptor.
419 */
420u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
421{
422 u64 size = md->num_pages << EFI_PAGE_SHIFT;
423 u64 end = md->phys_addr + size;
424 return end;
425}
Leif Lindholm272686b2013-09-05 11:34:54 +0100426
427static __initdata efi_config_table_type_t common_tables[] = {
428 {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
429 {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
430 {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
431 {MPS_TABLE_GUID, "MPS", &efi.mps},
432 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
433 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
Ard Biesheuvele1ccbbc2014-10-14 16:34:47 +0200434 {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3},
Leif Lindholm272686b2013-09-05 11:34:54 +0100435 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
Peter Jones0bb54902015-04-28 18:44:31 -0400436 {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt},
Ard Biesheuvelbf924862015-09-09 10:08:15 +0200437 {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table},
Ard Biesheuvela604af02016-04-25 21:06:44 +0100438 {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table},
Daeseok Youn69e60842014-02-13 17:16:36 +0900439 {NULL_GUID, NULL, NULL},
Leif Lindholm272686b2013-09-05 11:34:54 +0100440};
441
442static __init int match_config_table(efi_guid_t *guid,
443 unsigned long table,
444 efi_config_table_type_t *table_types)
445{
Leif Lindholm272686b2013-09-05 11:34:54 +0100446 int i;
447
448 if (table_types) {
Leif Lindholm272686b2013-09-05 11:34:54 +0100449 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
Leif Lindholm272686b2013-09-05 11:34:54 +0100450 if (!efi_guidcmp(*guid, table_types[i].guid)) {
451 *(table_types[i].ptr) = table;
Ard Biesheuvel801820b2016-04-25 21:06:53 +0100452 if (table_types[i].name)
453 pr_cont(" %s=0x%lx ",
454 table_types[i].name, table);
Leif Lindholm272686b2013-09-05 11:34:54 +0100455 return 1;
456 }
457 }
458 }
459
460 return 0;
461}
462
Ard Biesheuvel7bb68412014-10-18 15:04:15 +0200463int __init efi_config_parse_tables(void *config_tables, int count, int sz,
464 efi_config_table_type_t *arch_tables)
465{
466 void *tablep;
467 int i;
468
469 tablep = config_tables;
470 pr_info("");
471 for (i = 0; i < count; i++) {
472 efi_guid_t guid;
473 unsigned long table;
474
475 if (efi_enabled(EFI_64BIT)) {
476 u64 table64;
477 guid = ((efi_config_table_64_t *)tablep)->guid;
478 table64 = ((efi_config_table_64_t *)tablep)->table;
479 table = table64;
480#ifndef CONFIG_64BIT
481 if (table64 >> 32) {
482 pr_cont("\n");
483 pr_err("Table located above 4GB, disabling EFI.\n");
484 return -EINVAL;
485 }
486#endif
487 } else {
488 guid = ((efi_config_table_32_t *)tablep)->guid;
489 table = ((efi_config_table_32_t *)tablep)->table;
490 }
491
492 if (!match_config_table(&guid, table, common_tables))
493 match_config_table(&guid, table, arch_tables);
494
495 tablep += sz;
496 }
497 pr_cont("\n");
498 set_bit(EFI_CONFIG_TABLES, &efi.flags);
Ard Biesheuvela1041712015-09-23 07:29:34 -0700499
500 /* Parse the EFI Properties table if it exists */
501 if (efi.properties_table != EFI_INVALID_TABLE_ADDR) {
502 efi_properties_table_t *tbl;
503
504 tbl = early_memremap(efi.properties_table, sizeof(*tbl));
505 if (tbl == NULL) {
506 pr_err("Could not map Properties table!\n");
507 return -ENOMEM;
508 }
509
510 if (tbl->memory_protection_attribute &
511 EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
512 set_bit(EFI_NX_PE_DATA, &efi.flags);
513
514 early_memunmap(tbl, sizeof(*tbl));
515 }
516
Ard Biesheuvel7bb68412014-10-18 15:04:15 +0200517 return 0;
518}
519
Leif Lindholm272686b2013-09-05 11:34:54 +0100520int __init efi_config_init(efi_config_table_type_t *arch_tables)
521{
Ard Biesheuvel7bb68412014-10-18 15:04:15 +0200522 void *config_tables;
523 int sz, ret;
Leif Lindholm272686b2013-09-05 11:34:54 +0100524
525 if (efi_enabled(EFI_64BIT))
526 sz = sizeof(efi_config_table_64_t);
527 else
528 sz = sizeof(efi_config_table_32_t);
529
530 /*
531 * Let's see what config tables the firmware passed to us.
532 */
533 config_tables = early_memremap(efi.systab->tables,
534 efi.systab->nr_tables * sz);
535 if (config_tables == NULL) {
536 pr_err("Could not map Configuration table!\n");
537 return -ENOMEM;
538 }
539
Ard Biesheuvel7bb68412014-10-18 15:04:15 +0200540 ret = efi_config_parse_tables(config_tables, efi.systab->nr_tables, sz,
541 arch_tables);
Leif Lindholm272686b2013-09-05 11:34:54 +0100542
Daniel Kiperabc93f82014-06-30 19:52:56 +0200543 early_memunmap(config_tables, efi.systab->nr_tables * sz);
Ard Biesheuvel7bb68412014-10-18 15:04:15 +0200544 return ret;
Leif Lindholm272686b2013-09-05 11:34:54 +0100545}
Mark Salter0302f712013-12-30 12:12:12 -0500546
Matt Fleming9479c7c2016-02-26 21:22:05 +0000547/**
548 * efi_memmap_init_early - Map the EFI memory map data structure
549 * @data: EFI memory map data
550 *
551 * Use early_memremap() to map the passed in EFI memory map and assign
552 * it to efi.memmap.
553 */
554int __init efi_memmap_init_early(struct efi_memory_map_data *data)
555{
556 struct efi_memory_map map;
557
558 if (efi_enabled(EFI_PARAVIRT))
559 return 0;
560
561 map.phys_map = data->phys_map;
562
563 map.map = early_memremap(data->phys_map, data->size);
564 if (!map.map) {
565 pr_err("Could not map the memory map!\n");
566 return -ENOMEM;
567 }
568
569 map.nr_map = data->size / data->desc_size;
570 map.map_end = map.map + data->size;
571
572 map.desc_version = data->desc_version;
573 map.desc_size = data->desc_size;
574
575 set_bit(EFI_MEMMAP, &efi.flags);
576
577 efi.memmap = map;
578
579 return 0;
580}
581
582void __init efi_memmap_unmap(void)
583{
584 unsigned long size;
585
586 size = efi.memmap.desc_size * efi.memmap.nr_map;
587
588 early_memunmap(efi.memmap.map, size);
589 efi.memmap.map = NULL;
590 clear_bit(EFI_MEMMAP, &efi.flags);
591}
592
Lee, Chun-Yi28d54022014-07-09 18:39:29 +0800593#ifdef CONFIG_EFI_VARS_MODULE
594static int __init efi_load_efivars(void)
595{
596 struct platform_device *pdev;
597
598 if (!efi_enabled(EFI_RUNTIME_SERVICES))
599 return 0;
600
601 pdev = platform_device_register_simple("efivars", 0, NULL, 0);
602 return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
603}
604device_initcall(efi_load_efivars);
605#endif
606
Mark Salter0302f712013-12-30 12:12:12 -0500607#ifdef CONFIG_EFI_PARAMS_FROM_FDT
608
609#define UEFI_PARAM(name, prop, field) \
610 { \
611 { name }, \
612 { prop }, \
613 offsetof(struct efi_fdt_params, field), \
614 FIELD_SIZEOF(struct efi_fdt_params, field) \
615 }
616
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800617struct params {
Mark Salter0302f712013-12-30 12:12:12 -0500618 const char name[32];
619 const char propname[32];
620 int offset;
621 int size;
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800622};
623
624static __initdata struct params fdt_params[] = {
Mark Salter0302f712013-12-30 12:12:12 -0500625 UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
626 UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
627 UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
628 UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
629 UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver)
630};
631
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800632static __initdata struct params xen_fdt_params[] = {
633 UEFI_PARAM("System Table", "xen,uefi-system-table", system_table),
634 UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap),
635 UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size),
636 UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size),
637 UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver)
638};
639
640#define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params)
641
642static __initdata struct {
643 const char *uname;
644 const char *subnode;
645 struct params *params;
646} dt_params[] = {
647 { "hypervisor", "uefi", xen_fdt_params },
648 { "chosen", NULL, fdt_params },
649};
650
Mark Salter0302f712013-12-30 12:12:12 -0500651struct param_info {
Catalin Marinas29e24352014-07-08 16:54:18 +0100652 int found;
Mark Salter0302f712013-12-30 12:12:12 -0500653 void *params;
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800654 const char *missing;
Mark Salter0302f712013-12-30 12:12:12 -0500655};
656
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800657static int __init __find_uefi_params(unsigned long node,
658 struct param_info *info,
659 struct params *params)
Mark Salter0302f712013-12-30 12:12:12 -0500660{
Catalin Marinas6fb8cc82014-06-02 11:31:06 +0100661 const void *prop;
662 void *dest;
Mark Salter0302f712013-12-30 12:12:12 -0500663 u64 val;
Catalin Marinas6fb8cc82014-06-02 11:31:06 +0100664 int i, len;
Mark Salter0302f712013-12-30 12:12:12 -0500665
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800666 for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) {
667 prop = of_get_flat_dt_prop(node, params[i].propname, &len);
668 if (!prop) {
669 info->missing = params[i].name;
Mark Salter0302f712013-12-30 12:12:12 -0500670 return 0;
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800671 }
672
673 dest = info->params + params[i].offset;
Catalin Marinas29e24352014-07-08 16:54:18 +0100674 info->found++;
Mark Salter0302f712013-12-30 12:12:12 -0500675
676 val = of_read_number(prop, len / sizeof(u32));
677
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800678 if (params[i].size == sizeof(u32))
Mark Salter0302f712013-12-30 12:12:12 -0500679 *(u32 *)dest = val;
680 else
681 *(u64 *)dest = val;
682
Leif Lindholm7968c0e2015-08-26 14:24:58 +0100683 if (efi_enabled(EFI_DBG))
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800684 pr_info(" %s: 0x%0*llx\n", params[i].name,
685 params[i].size * 2, val);
Mark Salter0302f712013-12-30 12:12:12 -0500686 }
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800687
Mark Salter0302f712013-12-30 12:12:12 -0500688 return 1;
689}
690
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800691static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
692 int depth, void *data)
693{
694 struct param_info *info = data;
695 int i;
696
697 for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
698 const char *subnode = dt_params[i].subnode;
699
700 if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) {
701 info->missing = dt_params[i].params[0].name;
702 continue;
703 }
704
705 if (subnode) {
706 node = of_get_flat_dt_subnode_by_name(node, subnode);
707 if (node < 0)
708 return 0;
709 }
710
711 return __find_uefi_params(node, info, dt_params[i].params);
712 }
713
714 return 0;
715}
716
Leif Lindholm7968c0e2015-08-26 14:24:58 +0100717int __init efi_get_fdt_params(struct efi_fdt_params *params)
Mark Salter0302f712013-12-30 12:12:12 -0500718{
719 struct param_info info;
Catalin Marinas29e24352014-07-08 16:54:18 +0100720 int ret;
721
722 pr_info("Getting EFI parameters from FDT:\n");
Mark Salter0302f712013-12-30 12:12:12 -0500723
Catalin Marinas29e24352014-07-08 16:54:18 +0100724 info.found = 0;
Mark Salter0302f712013-12-30 12:12:12 -0500725 info.params = params;
726
Catalin Marinas29e24352014-07-08 16:54:18 +0100727 ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
728 if (!info.found)
729 pr_info("UEFI not found.\n");
730 else if (!ret)
731 pr_err("Can't find '%s' in device tree!\n",
Shannon Zhao0cac5c32016-05-12 20:19:54 +0800732 info.missing);
Catalin Marinas29e24352014-07-08 16:54:18 +0100733
734 return ret;
Mark Salter0302f712013-12-30 12:12:12 -0500735}
736#endif /* CONFIG_EFI_PARAMS_FROM_FDT */
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200737
738static __initdata char memory_type_name[][20] = {
739 "Reserved",
740 "Loader Code",
741 "Loader Data",
742 "Boot Code",
743 "Boot Data",
744 "Runtime Code",
745 "Runtime Data",
746 "Conventional Memory",
747 "Unusable Memory",
748 "ACPI Reclaim Memory",
749 "ACPI Memory NVS",
750 "Memory Mapped I/O",
751 "MMIO Port Space",
Robert Elliott35575e02016-02-01 22:07:07 +0000752 "PAL Code",
753 "Persistent Memory",
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200754};
755
756char * __init efi_md_typeattr_format(char *buf, size_t size,
757 const efi_memory_desc_t *md)
758{
759 char *pos;
760 int type_len;
761 u64 attr;
762
763 pos = buf;
764 if (md->type >= ARRAY_SIZE(memory_type_name))
765 type_len = snprintf(pos, size, "[type=%u", md->type);
766 else
767 type_len = snprintf(pos, size, "[%-*s",
768 (int)(sizeof(memory_type_name[0]) - 1),
769 memory_type_name[md->type]);
770 if (type_len >= size)
771 return buf;
772
773 pos += type_len;
774 size -= type_len;
775
776 attr = md->attribute;
777 if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
Ard Biesheuvel87db73ae2015-08-07 09:36:54 +0100778 EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
779 EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
Robert Elliottc016ca02016-02-01 22:07:06 +0000780 EFI_MEMORY_NV |
Taku Izumi8be44322015-08-27 02:11:19 +0900781 EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200782 snprintf(pos, size, "|attr=0x%016llx]",
783 (unsigned long long)attr);
784 else
Robert Elliottc016ca02016-02-01 22:07:06 +0000785 snprintf(pos, size,
786 "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200787 attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
Taku Izumi8be44322015-08-27 02:11:19 +0900788 attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
Robert Elliottc016ca02016-02-01 22:07:06 +0000789 attr & EFI_MEMORY_NV ? "NV" : "",
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200790 attr & EFI_MEMORY_XP ? "XP" : "",
791 attr & EFI_MEMORY_RP ? "RP" : "",
792 attr & EFI_MEMORY_WP ? "WP" : "",
Ard Biesheuvel87db73ae2015-08-07 09:36:54 +0100793 attr & EFI_MEMORY_RO ? "RO" : "",
Laszlo Ersek98d2a6c2014-09-03 13:32:20 +0200794 attr & EFI_MEMORY_UCE ? "UCE" : "",
795 attr & EFI_MEMORY_WB ? "WB" : "",
796 attr & EFI_MEMORY_WT ? "WT" : "",
797 attr & EFI_MEMORY_WC ? "WC" : "",
798 attr & EFI_MEMORY_UC ? "UC" : "");
799 return buf;
800}
Jonathan (Zhixiong) Zhang7bf79312015-08-07 09:36:57 +0100801
802/*
803 * efi_mem_attributes - lookup memmap attributes for physical address
804 * @phys_addr: the physical address to lookup
805 *
806 * Search in the EFI memory map for the region covering
807 * @phys_addr. Returns the EFI memory attributes if the region
808 * was found in the memory map, 0 otherwise.
809 *
810 * Despite being marked __weak, most architectures should *not*
811 * override this function. It is __weak solely for the benefit
812 * of ia64 which has a funky EFI memory map that doesn't work
813 * the same way as other architectures.
814 */
815u64 __weak efi_mem_attributes(unsigned long phys_addr)
816{
817 efi_memory_desc_t *md;
Jonathan (Zhixiong) Zhang7bf79312015-08-07 09:36:57 +0100818
819 if (!efi_enabled(EFI_MEMMAP))
820 return 0;
821
Matt Fleming78ce2482016-04-25 21:06:38 +0100822 for_each_efi_memory_desc(md) {
Jonathan (Zhixiong) Zhang7bf79312015-08-07 09:36:57 +0100823 if ((md->phys_addr <= phys_addr) &&
824 (phys_addr < (md->phys_addr +
825 (md->num_pages << EFI_PAGE_SHIFT))))
826 return md->attribute;
827 }
828 return 0;
829}
Matt Fleming806b0352016-04-25 21:06:58 +0100830
831int efi_status_to_err(efi_status_t status)
832{
833 int err;
834
835 switch (status) {
836 case EFI_SUCCESS:
837 err = 0;
838 break;
839 case EFI_INVALID_PARAMETER:
840 err = -EINVAL;
841 break;
842 case EFI_OUT_OF_RESOURCES:
843 err = -ENOSPC;
844 break;
845 case EFI_DEVICE_ERROR:
846 err = -EIO;
847 break;
848 case EFI_WRITE_PROTECTED:
849 err = -EROFS;
850 break;
851 case EFI_SECURITY_VIOLATION:
852 err = -EACCES;
853 break;
854 case EFI_NOT_FOUND:
855 err = -ENOENT;
856 break;
857 default:
858 err = -EINVAL;
859 }
860
861 return err;
862}