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