]> git.ipfire.org Git - people/ms/linux.git/blame - drivers/firmware/efi/efi.c
lib: Add a generic cmdline parse function parse_option_str
[people/ms/linux.git] / drivers / firmware / efi / efi.c
CommitLineData
a9499fa7
TG
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
272686bf
LL
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
a9499fa7
TG
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>
0302f71c
MS
23#include <linux/of.h>
24#include <linux/of_fdt.h>
272686bf 25#include <linux/io.h>
28d54022 26#include <linux/platform_device.h>
272686bf
LL
27
28struct efi __read_mostly efi = {
29 .mps = EFI_INVALID_TABLE_ADDR,
30 .acpi = EFI_INVALID_TABLE_ADDR,
31 .acpi20 = EFI_INVALID_TABLE_ADDR,
32 .smbios = EFI_INVALID_TABLE_ADDR,
33 .sal_systab = EFI_INVALID_TABLE_ADDR,
34 .boot_info = EFI_INVALID_TABLE_ADDR,
35 .hcdp = EFI_INVALID_TABLE_ADDR,
36 .uga = EFI_INVALID_TABLE_ADDR,
37 .uv_systab = EFI_INVALID_TABLE_ADDR,
a0998eb1
DY
38 .fw_vendor = EFI_INVALID_TABLE_ADDR,
39 .runtime = EFI_INVALID_TABLE_ADDR,
40 .config_table = EFI_INVALID_TABLE_ADDR,
272686bf
LL
41};
42EXPORT_SYMBOL(efi);
a9499fa7 43
b2e0a54a
DY
44static bool disable_runtime;
45static int __init setup_noefi(char *arg)
46{
47 disable_runtime = true;
48 return 0;
49}
50early_param("noefi", setup_noefi);
51
52bool efi_runtime_disabled(void)
53{
54 return disable_runtime;
55}
56
a9499fa7
TG
57static struct kobject *efi_kobj;
58static struct kobject *efivars_kobj;
59
60/*
61 * Let's not leave out systab information that snuck into
62 * the efivars driver
63 */
64static ssize_t systab_show(struct kobject *kobj,
65 struct kobj_attribute *attr, char *buf)
66{
67 char *str = buf;
68
69 if (!kobj || !buf)
70 return -EINVAL;
71
72 if (efi.mps != EFI_INVALID_TABLE_ADDR)
73 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
74 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
75 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
76 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
77 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
78 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
79 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
80 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
81 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
82 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
83 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
84 if (efi.uga != EFI_INVALID_TABLE_ADDR)
85 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
86
87 return str - buf;
88}
89
90static struct kobj_attribute efi_attr_systab =
91 __ATTR(systab, 0400, systab_show, NULL);
92
a0998eb1
DY
93#define EFI_FIELD(var) efi.var
94
95#define EFI_ATTR_SHOW(name) \
96static ssize_t name##_show(struct kobject *kobj, \
97 struct kobj_attribute *attr, char *buf) \
98{ \
99 return sprintf(buf, "0x%lx\n", EFI_FIELD(name)); \
100}
101
102EFI_ATTR_SHOW(fw_vendor);
103EFI_ATTR_SHOW(runtime);
104EFI_ATTR_SHOW(config_table);
105
106static struct kobj_attribute efi_attr_fw_vendor = __ATTR_RO(fw_vendor);
107static struct kobj_attribute efi_attr_runtime = __ATTR_RO(runtime);
108static struct kobj_attribute efi_attr_config_table = __ATTR_RO(config_table);
109
a9499fa7
TG
110static struct attribute *efi_subsys_attrs[] = {
111 &efi_attr_systab.attr,
a0998eb1
DY
112 &efi_attr_fw_vendor.attr,
113 &efi_attr_runtime.attr,
114 &efi_attr_config_table.attr,
115 NULL,
a9499fa7
TG
116};
117
a0998eb1
DY
118static umode_t efi_attr_is_visible(struct kobject *kobj,
119 struct attribute *attr, int n)
120{
9f27bc54
DK
121 if (attr == &efi_attr_fw_vendor.attr) {
122 if (efi_enabled(EFI_PARAVIRT) ||
123 efi.fw_vendor == EFI_INVALID_TABLE_ADDR)
124 return 0;
125 } else if (attr == &efi_attr_runtime.attr) {
126 if (efi.runtime == EFI_INVALID_TABLE_ADDR)
127 return 0;
128 } else if (attr == &efi_attr_config_table.attr) {
129 if (efi.config_table == EFI_INVALID_TABLE_ADDR)
130 return 0;
131 }
a0998eb1 132
9f27bc54 133 return attr->mode;
a0998eb1
DY
134}
135
a9499fa7
TG
136static struct attribute_group efi_subsys_attr_group = {
137 .attrs = efi_subsys_attrs,
a0998eb1 138 .is_visible = efi_attr_is_visible,
a9499fa7
TG
139};
140
141static struct efivars generic_efivars;
142static struct efivar_operations generic_ops;
143
144static int generic_ops_register(void)
145{
146 generic_ops.get_variable = efi.get_variable;
147 generic_ops.set_variable = efi.set_variable;
148 generic_ops.get_next_variable = efi.get_next_variable;
a614e192 149 generic_ops.query_variable_store = efi_query_variable_store;
a9499fa7
TG
150
151 return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
152}
153
154static void generic_ops_unregister(void)
155{
156 efivars_unregister(&generic_efivars);
157}
158
159/*
160 * We register the efi subsystem with the firmware subsystem and the
161 * efivars subsystem with the efi subsystem, if the system was booted with
162 * EFI.
163 */
164static int __init efisubsys_init(void)
165{
166 int error;
167
168 if (!efi_enabled(EFI_BOOT))
169 return 0;
170
171 /* We register the efi directory at /sys/firmware/efi */
172 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
173 if (!efi_kobj) {
174 pr_err("efi: Firmware registration failed.\n");
175 return -ENOMEM;
176 }
177
178 error = generic_ops_register();
179 if (error)
180 goto err_put;
181
182 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
183 if (error) {
184 pr_err("efi: Sysfs attribute export failed with error %d.\n",
185 error);
186 goto err_unregister;
187 }
188
926172d4
DY
189 error = efi_runtime_map_init(efi_kobj);
190 if (error)
191 goto err_remove_group;
192
a9499fa7
TG
193 /* and the standard mountpoint for efivarfs */
194 efivars_kobj = kobject_create_and_add("efivars", efi_kobj);
195 if (!efivars_kobj) {
196 pr_err("efivars: Subsystem registration failed.\n");
197 error = -ENOMEM;
198 goto err_remove_group;
199 }
200
201 return 0;
202
203err_remove_group:
204 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
205err_unregister:
206 generic_ops_unregister();
207err_put:
208 kobject_put(efi_kobj);
209 return error;
210}
211
212subsys_initcall(efisubsys_init);
272686bf
LL
213
214
258f6fd7
LL
215/*
216 * We can't ioremap data in EFI boot services RAM, because we've already mapped
217 * it as RAM. So, look it up in the existing EFI memory map instead. Only
218 * callable after efi_enter_virtual_mode and before efi_free_boot_services.
219 */
220void __iomem *efi_lookup_mapped_addr(u64 phys_addr)
221{
222 struct efi_memory_map *map;
223 void *p;
224 map = efi.memmap;
225 if (!map)
226 return NULL;
227 if (WARN_ON(!map->map))
228 return NULL;
229 for (p = map->map; p < map->map_end; p += map->desc_size) {
230 efi_memory_desc_t *md = p;
231 u64 size = md->num_pages << EFI_PAGE_SHIFT;
232 u64 end = md->phys_addr + size;
233 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
234 md->type != EFI_BOOT_SERVICES_CODE &&
235 md->type != EFI_BOOT_SERVICES_DATA)
236 continue;
237 if (!md->virt_addr)
238 continue;
239 if (phys_addr >= md->phys_addr && phys_addr < end) {
240 phys_addr += md->virt_addr - md->phys_addr;
241 return (__force void __iomem *)(unsigned long)phys_addr;
242 }
243 }
244 return NULL;
245}
246
272686bf
LL
247static __initdata efi_config_table_type_t common_tables[] = {
248 {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
249 {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
250 {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
251 {MPS_TABLE_GUID, "MPS", &efi.mps},
252 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
253 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
254 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
69e60841 255 {NULL_GUID, NULL, NULL},
272686bf
LL
256};
257
258static __init int match_config_table(efi_guid_t *guid,
259 unsigned long table,
260 efi_config_table_type_t *table_types)
261{
262 u8 str[EFI_VARIABLE_GUID_LEN + 1];
263 int i;
264
265 if (table_types) {
266 efi_guid_unparse(guid, str);
267
268 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
269 efi_guid_unparse(&table_types[i].guid, str);
270
271 if (!efi_guidcmp(*guid, table_types[i].guid)) {
272 *(table_types[i].ptr) = table;
273 pr_cont(" %s=0x%lx ",
274 table_types[i].name, table);
275 return 1;
276 }
277 }
278 }
279
280 return 0;
281}
282
283int __init efi_config_init(efi_config_table_type_t *arch_tables)
284{
285 void *config_tables, *tablep;
286 int i, sz;
287
288 if (efi_enabled(EFI_64BIT))
289 sz = sizeof(efi_config_table_64_t);
290 else
291 sz = sizeof(efi_config_table_32_t);
292
293 /*
294 * Let's see what config tables the firmware passed to us.
295 */
296 config_tables = early_memremap(efi.systab->tables,
297 efi.systab->nr_tables * sz);
298 if (config_tables == NULL) {
299 pr_err("Could not map Configuration table!\n");
300 return -ENOMEM;
301 }
302
303 tablep = config_tables;
304 pr_info("");
305 for (i = 0; i < efi.systab->nr_tables; i++) {
306 efi_guid_t guid;
307 unsigned long table;
308
309 if (efi_enabled(EFI_64BIT)) {
310 u64 table64;
311 guid = ((efi_config_table_64_t *)tablep)->guid;
312 table64 = ((efi_config_table_64_t *)tablep)->table;
313 table = table64;
314#ifndef CONFIG_64BIT
315 if (table64 >> 32) {
316 pr_cont("\n");
317 pr_err("Table located above 4GB, disabling EFI.\n");
abc93f8e 318 early_memunmap(config_tables,
272686bf
LL
319 efi.systab->nr_tables * sz);
320 return -EINVAL;
321 }
322#endif
323 } else {
324 guid = ((efi_config_table_32_t *)tablep)->guid;
325 table = ((efi_config_table_32_t *)tablep)->table;
326 }
327
328 if (!match_config_table(&guid, table, common_tables))
329 match_config_table(&guid, table, arch_tables);
330
331 tablep += sz;
332 }
333 pr_cont("\n");
abc93f8e 334 early_memunmap(config_tables, efi.systab->nr_tables * sz);
0f8093a9
MF
335
336 set_bit(EFI_CONFIG_TABLES, &efi.flags);
337
272686bf
LL
338 return 0;
339}
0302f71c 340
28d54022
LCY
341#ifdef CONFIG_EFI_VARS_MODULE
342static int __init efi_load_efivars(void)
343{
344 struct platform_device *pdev;
345
346 if (!efi_enabled(EFI_RUNTIME_SERVICES))
347 return 0;
348
349 pdev = platform_device_register_simple("efivars", 0, NULL, 0);
350 return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
351}
352device_initcall(efi_load_efivars);
353#endif
354
0302f71c
MS
355#ifdef CONFIG_EFI_PARAMS_FROM_FDT
356
357#define UEFI_PARAM(name, prop, field) \
358 { \
359 { name }, \
360 { prop }, \
361 offsetof(struct efi_fdt_params, field), \
362 FIELD_SIZEOF(struct efi_fdt_params, field) \
363 }
364
365static __initdata struct {
366 const char name[32];
367 const char propname[32];
368 int offset;
369 int size;
370} dt_params[] = {
371 UEFI_PARAM("System Table", "linux,uefi-system-table", system_table),
372 UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap),
373 UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size),
374 UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size),
375 UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver)
376};
377
378struct param_info {
379 int verbose;
29e2435f 380 int found;
0302f71c
MS
381 void *params;
382};
383
384static int __init fdt_find_uefi_params(unsigned long node, const char *uname,
385 int depth, void *data)
386{
387 struct param_info *info = data;
6fb8cc82
CM
388 const void *prop;
389 void *dest;
0302f71c 390 u64 val;
6fb8cc82 391 int i, len;
0302f71c
MS
392
393 if (depth != 1 ||
394 (strcmp(uname, "chosen") != 0 && strcmp(uname, "chosen@0") != 0))
395 return 0;
396
0302f71c
MS
397 for (i = 0; i < ARRAY_SIZE(dt_params); i++) {
398 prop = of_get_flat_dt_prop(node, dt_params[i].propname, &len);
29e2435f 399 if (!prop)
0302f71c 400 return 0;
0302f71c 401 dest = info->params + dt_params[i].offset;
29e2435f 402 info->found++;
0302f71c
MS
403
404 val = of_read_number(prop, len / sizeof(u32));
405
406 if (dt_params[i].size == sizeof(u32))
407 *(u32 *)dest = val;
408 else
409 *(u64 *)dest = val;
410
411 if (info->verbose)
412 pr_info(" %s: 0x%0*llx\n", dt_params[i].name,
413 dt_params[i].size * 2, val);
414 }
415 return 1;
416}
417
418int __init efi_get_fdt_params(struct efi_fdt_params *params, int verbose)
419{
420 struct param_info info;
29e2435f
CM
421 int ret;
422
423 pr_info("Getting EFI parameters from FDT:\n");
0302f71c
MS
424
425 info.verbose = verbose;
29e2435f 426 info.found = 0;
0302f71c
MS
427 info.params = params;
428
29e2435f
CM
429 ret = of_scan_flat_dt(fdt_find_uefi_params, &info);
430 if (!info.found)
431 pr_info("UEFI not found.\n");
432 else if (!ret)
433 pr_err("Can't find '%s' in device tree!\n",
434 dt_params[info.found].name);
435
436 return ret;
0302f71c
MS
437}
438#endif /* CONFIG_EFI_PARAMS_FROM_FDT */