blob: e1010d450b659fd1e11570633394726b4525fc67 [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>
Leif Lindholm272686b2013-09-05 11:34:54 +010023#include <linux/io.h>
24
25struct efi __read_mostly efi = {
26 .mps = EFI_INVALID_TABLE_ADDR,
27 .acpi = EFI_INVALID_TABLE_ADDR,
28 .acpi20 = EFI_INVALID_TABLE_ADDR,
29 .smbios = EFI_INVALID_TABLE_ADDR,
30 .sal_systab = EFI_INVALID_TABLE_ADDR,
31 .boot_info = EFI_INVALID_TABLE_ADDR,
32 .hcdp = EFI_INVALID_TABLE_ADDR,
33 .uga = EFI_INVALID_TABLE_ADDR,
34 .uv_systab = EFI_INVALID_TABLE_ADDR,
35};
36EXPORT_SYMBOL(efi);
Tom Gundersena9499fa2013-02-08 15:37:06 +000037
38static struct kobject *efi_kobj;
39static struct kobject *efivars_kobj;
40
41/*
42 * Let's not leave out systab information that snuck into
43 * the efivars driver
44 */
45static ssize_t systab_show(struct kobject *kobj,
46 struct kobj_attribute *attr, char *buf)
47{
48 char *str = buf;
49
50 if (!kobj || !buf)
51 return -EINVAL;
52
53 if (efi.mps != EFI_INVALID_TABLE_ADDR)
54 str += sprintf(str, "MPS=0x%lx\n", efi.mps);
55 if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
56 str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
57 if (efi.acpi != EFI_INVALID_TABLE_ADDR)
58 str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
59 if (efi.smbios != EFI_INVALID_TABLE_ADDR)
60 str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
61 if (efi.hcdp != EFI_INVALID_TABLE_ADDR)
62 str += sprintf(str, "HCDP=0x%lx\n", efi.hcdp);
63 if (efi.boot_info != EFI_INVALID_TABLE_ADDR)
64 str += sprintf(str, "BOOTINFO=0x%lx\n", efi.boot_info);
65 if (efi.uga != EFI_INVALID_TABLE_ADDR)
66 str += sprintf(str, "UGA=0x%lx\n", efi.uga);
67
68 return str - buf;
69}
70
71static struct kobj_attribute efi_attr_systab =
72 __ATTR(systab, 0400, systab_show, NULL);
73
74static struct attribute *efi_subsys_attrs[] = {
75 &efi_attr_systab.attr,
76 NULL, /* maybe more in the future? */
77};
78
79static struct attribute_group efi_subsys_attr_group = {
80 .attrs = efi_subsys_attrs,
81};
82
83static struct efivars generic_efivars;
84static struct efivar_operations generic_ops;
85
86static int generic_ops_register(void)
87{
88 generic_ops.get_variable = efi.get_variable;
89 generic_ops.set_variable = efi.set_variable;
90 generic_ops.get_next_variable = efi.get_next_variable;
Matt Fleminga614e192013-04-30 11:30:24 +010091 generic_ops.query_variable_store = efi_query_variable_store;
Tom Gundersena9499fa2013-02-08 15:37:06 +000092
93 return efivars_register(&generic_efivars, &generic_ops, efi_kobj);
94}
95
96static void generic_ops_unregister(void)
97{
98 efivars_unregister(&generic_efivars);
99}
100
101/*
102 * We register the efi subsystem with the firmware subsystem and the
103 * efivars subsystem with the efi subsystem, if the system was booted with
104 * EFI.
105 */
106static int __init efisubsys_init(void)
107{
108 int error;
109
110 if (!efi_enabled(EFI_BOOT))
111 return 0;
112
113 /* We register the efi directory at /sys/firmware/efi */
114 efi_kobj = kobject_create_and_add("efi", firmware_kobj);
115 if (!efi_kobj) {
116 pr_err("efi: Firmware registration failed.\n");
117 return -ENOMEM;
118 }
119
120 error = generic_ops_register();
121 if (error)
122 goto err_put;
123
124 error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
125 if (error) {
126 pr_err("efi: Sysfs attribute export failed with error %d.\n",
127 error);
128 goto err_unregister;
129 }
130
131 /* and the standard mountpoint for efivarfs */
132 efivars_kobj = kobject_create_and_add("efivars", efi_kobj);
133 if (!efivars_kobj) {
134 pr_err("efivars: Subsystem registration failed.\n");
135 error = -ENOMEM;
136 goto err_remove_group;
137 }
138
139 return 0;
140
141err_remove_group:
142 sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
143err_unregister:
144 generic_ops_unregister();
145err_put:
146 kobject_put(efi_kobj);
147 return error;
148}
149
150subsys_initcall(efisubsys_init);
Leif Lindholm272686b2013-09-05 11:34:54 +0100151
152
153static __initdata efi_config_table_type_t common_tables[] = {
154 {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20},
155 {ACPI_TABLE_GUID, "ACPI", &efi.acpi},
156 {HCDP_TABLE_GUID, "HCDP", &efi.hcdp},
157 {MPS_TABLE_GUID, "MPS", &efi.mps},
158 {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab},
159 {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios},
160 {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga},
161 {NULL_GUID, NULL, 0},
162};
163
164static __init int match_config_table(efi_guid_t *guid,
165 unsigned long table,
166 efi_config_table_type_t *table_types)
167{
168 u8 str[EFI_VARIABLE_GUID_LEN + 1];
169 int i;
170
171 if (table_types) {
172 efi_guid_unparse(guid, str);
173
174 for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
175 efi_guid_unparse(&table_types[i].guid, str);
176
177 if (!efi_guidcmp(*guid, table_types[i].guid)) {
178 *(table_types[i].ptr) = table;
179 pr_cont(" %s=0x%lx ",
180 table_types[i].name, table);
181 return 1;
182 }
183 }
184 }
185
186 return 0;
187}
188
189int __init efi_config_init(efi_config_table_type_t *arch_tables)
190{
191 void *config_tables, *tablep;
192 int i, sz;
193
194 if (efi_enabled(EFI_64BIT))
195 sz = sizeof(efi_config_table_64_t);
196 else
197 sz = sizeof(efi_config_table_32_t);
198
199 /*
200 * Let's see what config tables the firmware passed to us.
201 */
202 config_tables = early_memremap(efi.systab->tables,
203 efi.systab->nr_tables * sz);
204 if (config_tables == NULL) {
205 pr_err("Could not map Configuration table!\n");
206 return -ENOMEM;
207 }
208
209 tablep = config_tables;
210 pr_info("");
211 for (i = 0; i < efi.systab->nr_tables; i++) {
212 efi_guid_t guid;
213 unsigned long table;
214
215 if (efi_enabled(EFI_64BIT)) {
216 u64 table64;
217 guid = ((efi_config_table_64_t *)tablep)->guid;
218 table64 = ((efi_config_table_64_t *)tablep)->table;
219 table = table64;
220#ifndef CONFIG_64BIT
221 if (table64 >> 32) {
222 pr_cont("\n");
223 pr_err("Table located above 4GB, disabling EFI.\n");
224 early_iounmap(config_tables,
225 efi.systab->nr_tables * sz);
226 return -EINVAL;
227 }
228#endif
229 } else {
230 guid = ((efi_config_table_32_t *)tablep)->guid;
231 table = ((efi_config_table_32_t *)tablep)->table;
232 }
233
234 if (!match_config_table(&guid, table, common_tables))
235 match_config_table(&guid, table, arch_tables);
236
237 tablep += sz;
238 }
239 pr_cont("\n");
240 early_iounmap(config_tables, efi.systab->nr_tables * sz);
241 return 0;
242}