GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / firmware / efi / efi.c
1 /*
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
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18 #include <linux/kobject.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/device.h>
22 #include <linux/efi.h>
23 #include <linux/of.h>
24 #include <linux/of_fdt.h>
25 #include <linux/io.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/acpi.h>
29 #include <linux/ucs2_string.h>
30 #include <linux/memblock.h>
31
32 #include <asm/early_ioremap.h>
33
34 struct efi __read_mostly efi = {
35         .mps                    = EFI_INVALID_TABLE_ADDR,
36         .acpi                   = EFI_INVALID_TABLE_ADDR,
37         .acpi20                 = EFI_INVALID_TABLE_ADDR,
38         .smbios                 = EFI_INVALID_TABLE_ADDR,
39         .smbios3                = EFI_INVALID_TABLE_ADDR,
40         .sal_systab             = EFI_INVALID_TABLE_ADDR,
41         .boot_info              = EFI_INVALID_TABLE_ADDR,
42         .hcdp                   = EFI_INVALID_TABLE_ADDR,
43         .uga                    = EFI_INVALID_TABLE_ADDR,
44         .uv_systab              = EFI_INVALID_TABLE_ADDR,
45         .fw_vendor              = EFI_INVALID_TABLE_ADDR,
46         .runtime                = EFI_INVALID_TABLE_ADDR,
47         .config_table           = EFI_INVALID_TABLE_ADDR,
48         .esrt                   = EFI_INVALID_TABLE_ADDR,
49         .properties_table       = EFI_INVALID_TABLE_ADDR,
50         .mem_attr_table         = EFI_INVALID_TABLE_ADDR,
51 };
52 EXPORT_SYMBOL(efi);
53
54 static bool disable_runtime;
55 static int __init setup_noefi(char *arg)
56 {
57         disable_runtime = true;
58         return 0;
59 }
60 early_param("noefi", setup_noefi);
61
62 bool efi_runtime_disabled(void)
63 {
64         return disable_runtime;
65 }
66
67 static int __init parse_efi_cmdline(char *str)
68 {
69         if (!str) {
70                 pr_warn("need at least one option\n");
71                 return -EINVAL;
72         }
73
74         if (parse_option_str(str, "debug"))
75                 set_bit(EFI_DBG, &efi.flags);
76
77         if (parse_option_str(str, "noruntime"))
78                 disable_runtime = true;
79
80         return 0;
81 }
82 early_param("efi", parse_efi_cmdline);
83
84 struct kobject *efi_kobj;
85
86 /*
87  * Let's not leave out systab information that snuck into
88  * the efivars driver
89  */
90 static ssize_t systab_show(struct kobject *kobj,
91                            struct kobj_attribute *attr, char *buf)
92 {
93         char *str = buf;
94
95         if (!kobj || !buf)
96                 return -EINVAL;
97
98         if (efi.mps != EFI_INVALID_TABLE_ADDR)
99                 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
100         if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
101                 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
102         if (efi.acpi != EFI_INVALID_TABLE_ADDR)
103                 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
104         /*
105          * If both SMBIOS and SMBIOS3 entry points are implemented, the
106          * SMBIOS3 entry point shall be preferred, so we list it first to
107          * let applications stop parsing after the first match.
108          */
109         if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
110                 str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
111         if (efi.smbios != EFI_INVALID_TABLE_ADDR)
112                 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
113         if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
114                 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
115         if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
116                 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
117         if (efi.uga != EFI_INVALID_TABLE_ADDR)
118                 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
119
120         return str - buf;
121 }
122
123 static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
124
125 #define EFI_FIELD(var) efi.var
126
127 #define EFI_ATTR_SHOW(name) \
128 static 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
134 EFI_ATTR_SHOW(fw_vendor);
135 EFI_ATTR_SHOW(runtime);
136 EFI_ATTR_SHOW(config_table);
137
138 static 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
144 static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
145 static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
146 static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
147 static struct kobj_attribute efi_attr_fw_platform_size =
148         __ATTR_RO(fw_platform_size);
149
150 static struct attribute *efi_subsys_attrs[] = {
151         &efi_attr_systab.attr,
152         &efi_attr_fw_vendor.attr,
153         &efi_attr_runtime.attr,
154         &efi_attr_config_table.attr,
155         &efi_attr_fw_platform_size.attr,
156         NULL,
157 };
158
159 static umode_t efi_attr_is_visible(struct kobject *kobj,
160                                    struct attribute *attr, int n)
161 {
162         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         }
173
174         return attr->mode;
175 }
176
177 static struct attribute_group efi_subsys_attr_group = {
178         .attrs = efi_subsys_attrs,
179         .is_visible = efi_attr_is_visible,
180 };
181
182 static struct efivars generic_efivars;
183 static struct efivar_operations generic_ops;
184
185 static int generic_ops_register(void)
186 {
187         generic_ops.get_variable = efi.get_variable;
188         generic_ops.set_variable = efi.set_variable;
189         generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
190         generic_ops.get_next_variable = efi.get_next_variable;
191         generic_ops.query_variable_store = efi_query_variable_store;
192
193         return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
194 }
195
196 static void generic_ops_unregister(void)
197 {
198         efivars_unregister(&generic_efivars);
199 }
200
201 #ifdef CONFIG_EFI_CUSTOM_SSDT_OVERLAYS
202 #define EFIVAR_SSDT_NAME_MAX    16
203 static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
204 static 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
214 static __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
238 static __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         if (!efivar_ssdt[0])
247                 return 0;
248
249         ret = efivar_init(efivar_ssdt_iter, &entries, true, &entries);
250
251         list_for_each_entry_safe(entry, aux, &entries, list) {
252                 pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt,
253                         &entry->var.VendorGuid);
254
255                 list_del(&entry->list);
256
257                 ret = efivar_entry_size(entry, &size);
258                 if (ret) {
259                         pr_err("failed to get var size\n");
260                         goto free_entry;
261                 }
262
263                 data = kmalloc(size, GFP_KERNEL);
264                 if (!data) {
265                         ret = -ENOMEM;
266                         goto free_entry;
267                 }
268
269                 ret = efivar_entry_get(entry, NULL, &size, data);
270                 if (ret) {
271                         pr_err("failed to get var data\n");
272                         goto free_data;
273                 }
274
275                 ret = acpi_load_table(data);
276                 if (ret) {
277                         pr_err("failed to load table: %d\n", ret);
278                         goto free_data;
279                 }
280
281                 goto free_entry;
282
283 free_data:
284                 kfree(data);
285
286 free_entry:
287                 kfree(entry);
288         }
289
290         return ret;
291 }
292 #else
293 static inline int efivar_ssdt_load(void) { return 0; }
294 #endif
295
296 /*
297  * We register the efi subsystem with the firmware subsystem and the
298  * efivars subsystem with the efi subsystem, if the system was booted with
299  * EFI.
300  */
301 static int __init efisubsys_init(void)
302 {
303         int error;
304
305         if (!efi_enabled(EFI_BOOT))
306                 return 0;
307
308         /* We register the efi directory at /sys/firmware/efi */
309         efi_kobj = kobject_create_and_add("efi", firmware_kobj);
310         if (!efi_kobj) {
311                 pr_err("efi: Firmware registration failed.\n");
312                 return -ENOMEM;
313         }
314
315         error = generic_ops_register();
316         if (error)
317                 goto err_put;
318
319         if (efi_enabled(EFI_RUNTIME_SERVICES))
320                 efivar_ssdt_load();
321
322         error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
323         if (error) {
324                 pr_err("efi: Sysfs attribute export failed with error %d.\n",
325                        error);
326                 goto err_unregister;
327         }
328
329         error = efi_runtime_map_init(efi_kobj);
330         if (error)
331                 goto err_remove_group;
332
333         /* and the standard mountpoint for efivarfs */
334         error = sysfs_create_mount_point(efi_kobj, "efivars");
335         if (error) {
336                 pr_err("efivars: Subsystem registration failed.\n");
337                 goto err_remove_group;
338         }
339
340         return 0;
341
342 err_remove_group:
343         sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
344 err_unregister:
345         generic_ops_unregister();
346 err_put:
347         kobject_put(efi_kobj);
348         return error;
349 }
350
351 subsys_initcall(efisubsys_init);
352
353 /*
354  * Find the efi memory descriptor for a given physical address.  Given a
355  * physical address, determine if it exists within an EFI Memory Map entry,
356  * and if so, populate the supplied memory descriptor with the appropriate
357  * data.
358  */
359 int __init efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
360 {
361         efi_memory_desc_t *md;
362
363         if (!efi_enabled(EFI_MEMMAP)) {
364                 pr_err_once("EFI_MEMMAP is not enabled.\n");
365                 return -EINVAL;
366         }
367
368         if (!out_md) {
369                 pr_err_once("out_md is null.\n");
370                 return -EINVAL;
371         }
372
373         for_each_efi_memory_desc(md) {
374                 u64 size;
375                 u64 end;
376
377                 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
378                     md->type != EFI_BOOT_SERVICES_DATA &&
379                     md->type != EFI_RUNTIME_SERVICES_DATA) {
380                         continue;
381                 }
382
383                 size = md->num_pages << EFI_PAGE_SHIFT;
384                 end = md->phys_addr + size;
385                 if (phys_addr >= md->phys_addr && phys_addr < end) {
386                         memcpy(out_md, md, sizeof(*out_md));
387                         return 0;
388                 }
389         }
390         return -ENOENT;
391 }
392
393 /*
394  * Calculate the highest address of an efi memory descriptor.
395  */
396 u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
397 {
398         u64 size = md->num_pages << EFI_PAGE_SHIFT;
399         u64 end = md->phys_addr + size;
400         return end;
401 }
402
403 void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}
404
405 /**
406  * efi_mem_reserve - Reserve an EFI memory region
407  * @addr: Physical address to reserve
408  * @size: Size of reservation
409  *
410  * Mark a region as reserved from general kernel allocation and
411  * prevent it being released by efi_free_boot_services().
412  *
413  * This function should be called drivers once they've parsed EFI
414  * configuration tables to figure out where their data lives, e.g.
415  * efi_esrt_init().
416  */
417 void __init efi_mem_reserve(phys_addr_t addr, u64 size)
418 {
419         if (!memblock_is_region_reserved(addr, size))
420                 memblock_reserve(addr, size);
421
422         /*
423          * Some architectures (x86) reserve all boot services ranges
424          * until efi_free_boot_services() because of buggy firmware
425          * implementations. This means the above memblock_reserve() is
426          * superfluous on x86 and instead what it needs to do is
427          * ensure the @start, @size is not freed.
428          */
429         efi_arch_mem_reserve(addr, size);
430 }
431
432 static __initdata efi_config_table_type_t common_tables[] = {
433         {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
434         {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
435         {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
436         {MPS_TABLE_GUID, "MPS", &efi.mps},
437         {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
438         {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
439         {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3},
440         {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
441         {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt},
442         {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table},
443         {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table},
444         {NULL_GUID, NULL, NULL},
445 };
446
447 static __init int match_config_table(efi_guid_t *guid,
448                                      unsigned long table,
449                                      efi_config_table_type_t *table_types)
450 {
451         int i;
452
453         if (table_types) {
454                 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
455                         if (!efi_guidcmp(*guid, table_types[i].guid)) {
456                                 *(table_types[i].ptr) = table;
457                                 if (table_types[i].name)
458                                         pr_cont(" %s=0x%lx ",
459                                                 table_types[i].name, table);
460                                 return 1;
461                         }
462                 }
463         }
464
465         return 0;
466 }
467
468 int __init efi_config_parse_tables(void *config_tables, int count, int sz,
469                                    efi_config_table_type_t *arch_tables)
470 {
471         void *tablep;
472         int i;
473
474         tablep = config_tables;
475         pr_info("");
476         for (i = 0; i < count; i++) {
477                 efi_guid_t guid;
478                 unsigned long table;
479
480                 if (efi_enabled(EFI_64BIT)) {
481                         u64 table64;
482                         guid = ((efi_config_table_64_t *)tablep)->guid;
483                         table64 = ((efi_config_table_64_t *)tablep)->table;
484                         table = table64;
485 #ifndef CONFIG_64BIT
486                         if (table64 >> 32) {
487                                 pr_cont("\n");
488                                 pr_err("Table located above 4GB, disabling EFI.\n");
489                                 return -EINVAL;
490                         }
491 #endif
492                 } else {
493                         guid = ((efi_config_table_32_t *)tablep)->guid;
494                         table = ((efi_config_table_32_t *)tablep)->table;
495                 }
496
497                 if (!match_config_table(&guid, table, common_tables))
498                         match_config_table(&guid, table, arch_tables);
499
500                 tablep += sz;
501         }
502         pr_cont("\n");
503         set_bit(EFI_CONFIG_TABLES, &efi.flags);
504
505         /* Parse the EFI Properties table if it exists */
506         if (efi.properties_table != EFI_INVALID_TABLE_ADDR) {
507                 efi_properties_table_t *tbl;
508
509                 tbl = early_memremap(efi.properties_table, sizeof(*tbl));
510                 if (tbl == NULL) {
511                         pr_err("Could not map Properties table!\n");
512                         return -ENOMEM;
513                 }
514
515                 if (tbl->memory_protection_attribute &
516                     EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA)
517                         set_bit(EFI_NX_PE_DATA, &efi.flags);
518
519                 early_memunmap(tbl, sizeof(*tbl));
520         }
521
522         return 0;
523 }
524
525 int __init efi_config_init(efi_config_table_type_t *arch_tables)
526 {
527         void *config_tables;
528         int sz, ret;
529
530         if (efi_enabled(EFI_64BIT))
531                 sz = sizeof(efi_config_table_64_t);
532         else
533                 sz = sizeof(efi_config_table_32_t);
534
535         /*
536          * Let's see what config tables the firmware passed to us.
537          */
538         config_tables = early_memremap(efi.systab->tables,
539                                        efi.systab->nr_tables * sz);
540         if (config_tables == NULL) {
541                 pr_err("Could not map Configuration table!\n");
542                 return -ENOMEM;
543         }
544
545         ret = efi_config_parse_tables(config_tables, efi.systab->nr_tables, sz,
546                                       arch_tables);
547
548         early_memunmap(config_tables, efi.systab->nr_tables * sz);
549         return ret;
550 }
551
552 #ifdef CONFIG_EFI_VARS_MODULE
553 static int __init efi_load_efivars(void)
554 {
555         struct platform_device *pdev;
556
557         if (!efi_enabled(EFI_RUNTIME_SERVICES))
558                 return 0;
559
560         pdev = platform_device_register_simple("efivars", 0, NULL, 0);
561         return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
562 }
563 device_initcall(efi_load_efivars);
564 #endif
565
566 #ifdef CONFIG_EFI_PARAMS_FROM_FDT
567
568 #define UEFI_PARAM(name, prop, field)                      \
569         {                                                  \
570                 { name },                                  \
571                 { prop },                                  \
572                 offsetof(struct efi_fdt_params, field),    \
573                 FIELD_SIZEOF(struct efi_fdt_params, field) \
574         }
575
576 struct params {
577         const char name[32];
578         const char propname[32];
579         int offset;
580         int size;
581 };
582
583 static __initdata struct params fdt_params[] = {
584         UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
585         UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
586         UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
587         UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
588         UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver)
589 };
590
591 static __initdata struct params xen_fdt_params[] = {
592         UEFI_PARAM("System Table", "xen,uefi-system-table", system_table),
593         UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap),
594         UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size),
595         UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size),
596         UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver)
597 };
598
599 #define EFI_FDT_PARAMS_SIZE     ARRAY_SIZE(fdt_params)
600
601 static __initdata struct {
602         const char *uname;
603         const char *subnode;
604         struct params *params;
605 } dt_params[] = {
606         { "hypervisor", "uefi", xen_fdt_params },
607         { "chosen", NULL, fdt_params },
608 };
609
610 struct param_info {
611         int found;
612         void *params;
613         const char *missing;
614 };
615
616 static int __init __find_uefi_params(unsigned long node,
617                                      struct param_info *info,
618                                      struct params *params)
619 {
620         const void *prop;
621         void *dest;
622         u64 val;
623         int i, len;
624
625         for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) {
626                 prop = of_get_flat_dt_prop(node, params[i].propname, &len);
627                 if (!prop) {
628                         info->missing = params[i].name;
629                         return 0;
630                 }
631
632                 dest = info->params + params[i].offset;
633                 info->found++;
634
635                 val = of_read_number(prop, len / sizeof(u32));
636
637                 if (params[i].size == sizeof(u32))
638                         *(u32 *)dest = val;
639                 else
640                         *(u64 *)dest = val;
641
642                 if (efi_enabled(EFI_DBG))
643                         pr_info("  %s: 0x%0*llx\n", params[i].name,
644                                 params[i].size * 2, val);
645         }
646
647         return 1;
648 }
649
650 static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
651                                        int depth, void *data)
652 {
653         struct param_info *info = data;
654         int i;
655
656         for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
657                 const char *subnode = dt_params[i].subnode;
658
659                 if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) {
660                         info->missing = dt_params[i].params[0].name;
661                         continue;
662                 }
663
664                 if (subnode) {
665                         int err = of_get_flat_dt_subnode_by_name(node, subnode);
666
667                         if (err < 0)
668                                 return 0;
669
670                         node = err;
671                 }
672
673                 return __find_uefi_params(node, info, dt_params[i].params);
674         }
675
676         return 0;
677 }
678
679 int __init efi_get_fdt_params(struct efi_fdt_params *params)
680 {
681         struct param_info info;
682         int ret;
683
684         pr_info("Getting EFI parameters from FDT:\n");
685
686         info.found = 0;
687         info.params = params;
688
689         ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
690         if (!info.found)
691                 pr_info("UEFI not found.\n");
692         else if (!ret)
693                 pr_err("Can't find '%s' in device tree!\n",
694                        info.missing);
695
696         return ret;
697 }
698 #endif /* CONFIG_EFI_PARAMS_FROM_FDT */
699
700 static __initdata char memory_type_name[][20] = {
701         "Reserved",
702         "Loader Code",
703         "Loader Data",
704         "Boot Code",
705         "Boot Data",
706         "Runtime Code",
707         "Runtime Data",
708         "Conventional Memory",
709         "Unusable Memory",
710         "ACPI Reclaim Memory",
711         "ACPI Memory NVS",
712         "Memory Mapped I/O",
713         "MMIO Port Space",
714         "PAL Code",
715         "Persistent Memory",
716 };
717
718 char * __init efi_md_typeattr_format(char *buf, size_t size,
719                                      const efi_memory_desc_t *md)
720 {
721         char *pos;
722         int type_len;
723         u64 attr;
724
725         pos = buf;
726         if (md->type >= ARRAY_SIZE(memory_type_name))
727                 type_len = snprintf(pos, size, "[type=%u", md->type);
728         else
729                 type_len = snprintf(pos, size, "[%-*s",
730                                     (int)(sizeof(memory_type_name[0]) - 1),
731                                     memory_type_name[md->type]);
732         if (type_len >= size)
733                 return buf;
734
735         pos += type_len;
736         size -= type_len;
737
738         attr = md->attribute;
739         if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
740                      EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
741                      EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
742                      EFI_MEMORY_NV |
743                      EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
744                 snprintf(pos, size, "|attr=0x%016llx]",
745                          (unsigned long long)attr);
746         else
747                 snprintf(pos, size,
748                          "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
749                          attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
750                          attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
751                          attr & EFI_MEMORY_NV      ? "NV"  : "",
752                          attr & EFI_MEMORY_XP      ? "XP"  : "",
753                          attr & EFI_MEMORY_RP      ? "RP"  : "",
754                          attr & EFI_MEMORY_WP      ? "WP"  : "",
755                          attr & EFI_MEMORY_RO      ? "RO"  : "",
756                          attr & EFI_MEMORY_UCE     ? "UCE" : "",
757                          attr & EFI_MEMORY_WB      ? "WB"  : "",
758                          attr & EFI_MEMORY_WT      ? "WT"  : "",
759                          attr & EFI_MEMORY_WC      ? "WC"  : "",
760                          attr & EFI_MEMORY_UC      ? "UC"  : "");
761         return buf;
762 }
763
764 /*
765  * efi_mem_attributes - lookup memmap attributes for physical address
766  * @phys_addr: the physical address to lookup
767  *
768  * Search in the EFI memory map for the region covering
769  * @phys_addr. Returns the EFI memory attributes if the region
770  * was found in the memory map, 0 otherwise.
771  *
772  * Despite being marked __weak, most architectures should *not*
773  * override this function. It is __weak solely for the benefit
774  * of ia64 which has a funky EFI memory map that doesn't work
775  * the same way as other architectures.
776  */
777 u64 __weak efi_mem_attributes(unsigned long phys_addr)
778 {
779         efi_memory_desc_t *md;
780
781         if (!efi_enabled(EFI_MEMMAP))
782                 return 0;
783
784         for_each_efi_memory_desc(md) {
785                 if ((md->phys_addr <= phys_addr) &&
786                     (phys_addr < (md->phys_addr +
787                     (md->num_pages << EFI_PAGE_SHIFT))))
788                         return md->attribute;
789         }
790         return 0;
791 }
792
793 int efi_status_to_err(efi_status_t status)
794 {
795         int err;
796
797         switch (status) {
798         case EFI_SUCCESS:
799                 err = 0;
800                 break;
801         case EFI_INVALID_PARAMETER:
802                 err = -EINVAL;
803                 break;
804         case EFI_OUT_OF_RESOURCES:
805                 err = -ENOSPC;
806                 break;
807         case EFI_DEVICE_ERROR:
808                 err = -EIO;
809                 break;
810         case EFI_WRITE_PROTECTED:
811                 err = -EROFS;
812                 break;
813         case EFI_SECURITY_VIOLATION:
814                 err = -EACCES;
815                 break;
816         case EFI_NOT_FOUND:
817                 err = -ENOENT;
818                 break;
819         case EFI_ABORTED:
820                 err = -EINTR;
821                 break;
822         default:
823                 err = -EINVAL;
824         }
825
826         return err;
827 }