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> |
Lee, Chun-Yi | 28d5402 | 2014-07-09 18:39:29 +0800 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Octavian Purdila | 475fb4e | 2016-07-08 19:13:12 +0300 | [diff] [blame] | 27 | #include <linux/slab.h> |
| 28 | #include <linux/acpi.h> |
| 29 | #include <linux/ucs2_string.h> |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 30 | |
Ard Biesheuvel | 0f7f2f0 | 2016-01-12 14:22:46 +0100 | [diff] [blame] | 31 | #include <asm/early_ioremap.h> |
Ard Biesheuvel | f7d9248 | 2015-11-30 13:28:19 +0100 | [diff] [blame] | 32 | |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 33 | struct efi __read_mostly efi = { |
Ard Biesheuvel | bf92486 | 2015-09-09 10:08:15 +0200 | [diff] [blame] | 34 | .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 Biesheuvel | a604af0 | 2016-04-25 21:06:44 +0100 | [diff] [blame] | 49 | .mem_attr_table = EFI_INVALID_TABLE_ADDR, |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 50 | }; |
| 51 | EXPORT_SYMBOL(efi); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 52 | |
Dave Young | b2e0a54 | 2014-08-14 17:15:26 +0800 | [diff] [blame] | 53 | static bool disable_runtime; |
| 54 | static int __init setup_noefi(char *arg) |
| 55 | { |
| 56 | disable_runtime = true; |
| 57 | return 0; |
| 58 | } |
| 59 | early_param("noefi", setup_noefi); |
| 60 | |
| 61 | bool efi_runtime_disabled(void) |
| 62 | { |
| 63 | return disable_runtime; |
| 64 | } |
| 65 | |
Dave Young | 5ae3683 | 2014-08-14 17:15:28 +0800 | [diff] [blame] | 66 | static int __init parse_efi_cmdline(char *str) |
| 67 | { |
Ricardo Neri | 9115c75 | 2015-07-15 19:36:03 -0700 | [diff] [blame] | 68 | if (!str) { |
| 69 | pr_warn("need at least one option\n"); |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | |
Leif Lindholm | 12dd00e | 2015-08-26 14:24:56 +0100 | [diff] [blame] | 73 | if (parse_option_str(str, "debug")) |
| 74 | set_bit(EFI_DBG, &efi.flags); |
| 75 | |
Dave Young | 5ae3683 | 2014-08-14 17:15:28 +0800 | [diff] [blame] | 76 | if (parse_option_str(str, "noruntime")) |
| 77 | disable_runtime = true; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | early_param("efi", parse_efi_cmdline); |
| 82 | |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 83 | struct kobject *efi_kobj; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 84 | |
| 85 | /* |
| 86 | * Let's not leave out systab information that snuck into |
| 87 | * the efivars driver |
| 88 | */ |
| 89 | static 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 Delvare | b119fe0 | 2015-04-30 15:23:05 +0200 | [diff] [blame] | 103 | /* |
| 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 Biesheuvel | e1ccbbc | 2014-10-14 16:34:47 +0200 | [diff] [blame] | 108 | if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) |
| 109 | str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3); |
Jean Delvare | b119fe0 | 2015-04-30 15:23:05 +0200 | [diff] [blame] | 110 | if (efi.smbios != EFI_INVALID_TABLE_ADDR) |
| 111 | str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 112 | 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 | |
| 122 | static struct kobj_attribute efi_attr_systab = |
| 123 | __ATTR(systab, 0400, systab_show, NULL); |
| 124 | |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 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 | |
Steve McIntyre | 2859dff | 2015-01-09 15:29:53 +0000 | [diff] [blame] | 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 | |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 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); |
Steve McIntyre | 2859dff | 2015-01-09 15:29:53 +0000 | [diff] [blame] | 147 | static struct kobj_attribute efi_attr_fw_platform_size = |
| 148 | __ATTR_RO(fw_platform_size); |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 149 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 150 | static struct attribute *efi_subsys_attrs[] = { |
| 151 | &efi_attr_systab.attr, |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 152 | &efi_attr_fw_vendor.attr, |
| 153 | &efi_attr_runtime.attr, |
| 154 | &efi_attr_config_table.attr, |
Steve McIntyre | 2859dff | 2015-01-09 15:29:53 +0000 | [diff] [blame] | 155 | &efi_attr_fw_platform_size.attr, |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 156 | NULL, |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 159 | static umode_t efi_attr_is_visible(struct kobject *kobj, |
| 160 | struct attribute *attr, int n) |
| 161 | { |
Daniel Kiper | 9f27bc5 | 2014-06-30 19:52:58 +0200 | [diff] [blame] | 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 | } |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 173 | |
Daniel Kiper | 9f27bc5 | 2014-06-30 19:52:58 +0200 | [diff] [blame] | 174 | return attr->mode; |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 175 | } |
| 176 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 177 | static struct attribute_group efi_subsys_attr_group = { |
| 178 | .attrs = efi_subsys_attrs, |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 179 | .is_visible = efi_attr_is_visible, |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 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; |
Ard Biesheuvel | 9c6672a | 2016-02-01 22:06:55 +0000 | [diff] [blame] | 189 | generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 190 | generic_ops.get_next_variable = efi.get_next_variable; |
Matt Fleming | a614e19 | 2013-04-30 11:30:24 +0100 | [diff] [blame] | 191 | generic_ops.query_variable_store = efi_query_variable_store; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 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 | |
Octavian Purdila | 475fb4e | 2016-07-08 19:13:12 +0300 | [diff] [blame] | 201 | #if IS_ENABLED(CONFIG_ACPI) |
| 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 | 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 | |
| 278 | free_data: |
| 279 | kfree(data); |
| 280 | |
| 281 | free_entry: |
| 282 | kfree(entry); |
| 283 | } |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | #else |
| 288 | static inline int efivar_ssdt_load(void) { return 0; } |
| 289 | #endif |
| 290 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 291 | /* |
| 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 | */ |
| 296 | static 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 Purdila | 475fb4e | 2016-07-08 19:13:12 +0300 | [diff] [blame] | 314 | if (efi_enabled(EFI_RUNTIME_SERVICES)) |
| 315 | efivar_ssdt_load(); |
| 316 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 317 | 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 Young | 926172d | 2013-12-20 18:02:18 +0800 | [diff] [blame] | 324 | error = efi_runtime_map_init(efi_kobj); |
| 325 | if (error) |
| 326 | goto err_remove_group; |
| 327 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 328 | /* and the standard mountpoint for efivarfs */ |
Eric W. Biederman | f9bb488 | 2015-05-13 17:35:41 -0500 | [diff] [blame] | 329 | error = sysfs_create_mount_point(efi_kobj, "efivars"); |
| 330 | if (error) { |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 331 | pr_err("efivars: Subsystem registration failed.\n"); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 332 | goto err_remove_group; |
| 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | |
| 337 | err_remove_group: |
| 338 | sysfs_remove_group(efi_kobj, &efi_subsys_attr_group); |
| 339 | err_unregister: |
| 340 | generic_ops_unregister(); |
| 341 | err_put: |
| 342 | kobject_put(efi_kobj); |
| 343 | return error; |
| 344 | } |
| 345 | |
| 346 | subsys_initcall(efisubsys_init); |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 347 | |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 348 | /* |
| 349 | * Find the efi memory descriptor for a given physical address. Given a |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 350 | * physical address, determine if it exists within an EFI Memory Map entry, |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 351 | * and if so, populate the supplied memory descriptor with the appropriate |
| 352 | * data. |
| 353 | */ |
| 354 | int __init efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md) |
| 355 | { |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 356 | efi_memory_desc_t *md; |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 357 | |
| 358 | if (!efi_enabled(EFI_MEMMAP)) { |
| 359 | pr_err_once("EFI_MEMMAP is not enabled.\n"); |
| 360 | return -EINVAL; |
| 361 | } |
| 362 | |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 363 | if (!out_md) { |
| 364 | pr_err_once("out_md is null.\n"); |
| 365 | return -EINVAL; |
| 366 | } |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 367 | |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 368 | for_each_efi_memory_desc(md) { |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 369 | u64 size; |
| 370 | u64 end; |
| 371 | |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 372 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && |
| 373 | md->type != EFI_BOOT_SERVICES_DATA && |
| 374 | md->type != EFI_RUNTIME_SERVICES_DATA) { |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 375 | continue; |
| 376 | } |
| 377 | |
| 378 | size = md->num_pages << EFI_PAGE_SHIFT; |
| 379 | end = md->phys_addr + size; |
| 380 | if (phys_addr >= md->phys_addr && phys_addr < end) { |
| 381 | memcpy(out_md, md, sizeof(*out_md)); |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 382 | return 0; |
| 383 | } |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 384 | } |
| 385 | pr_err_once("requested map not found.\n"); |
| 386 | return -ENOENT; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Calculate the highest address of an efi memory descriptor. |
| 391 | */ |
| 392 | u64 __init efi_mem_desc_end(efi_memory_desc_t *md) |
| 393 | { |
| 394 | u64 size = md->num_pages << EFI_PAGE_SHIFT; |
| 395 | u64 end = md->phys_addr + size; |
| 396 | return end; |
| 397 | } |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 398 | |
| 399 | static __initdata efi_config_table_type_t common_tables[] = { |
| 400 | {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20}, |
| 401 | {ACPI_TABLE_GUID, "ACPI", &efi.acpi}, |
| 402 | {HCDP_TABLE_GUID, "HCDP", &efi.hcdp}, |
| 403 | {MPS_TABLE_GUID, "MPS", &efi.mps}, |
| 404 | {SAL_SYSTEM_TABLE_GUID, "SALsystab", &efi.sal_systab}, |
| 405 | {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios}, |
Ard Biesheuvel | e1ccbbc | 2014-10-14 16:34:47 +0200 | [diff] [blame] | 406 | {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3}, |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 407 | {UGA_IO_PROTOCOL_GUID, "UGA", &efi.uga}, |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 408 | {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt}, |
Ard Biesheuvel | bf92486 | 2015-09-09 10:08:15 +0200 | [diff] [blame] | 409 | {EFI_PROPERTIES_TABLE_GUID, "PROP", &efi.properties_table}, |
Ard Biesheuvel | a604af0 | 2016-04-25 21:06:44 +0100 | [diff] [blame] | 410 | {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi.mem_attr_table}, |
Daeseok Youn | 69e6084 | 2014-02-13 17:16:36 +0900 | [diff] [blame] | 411 | {NULL_GUID, NULL, NULL}, |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | static __init int match_config_table(efi_guid_t *guid, |
| 415 | unsigned long table, |
| 416 | efi_config_table_type_t *table_types) |
| 417 | { |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 418 | int i; |
| 419 | |
| 420 | if (table_types) { |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 421 | for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) { |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 422 | if (!efi_guidcmp(*guid, table_types[i].guid)) { |
| 423 | *(table_types[i].ptr) = table; |
Ard Biesheuvel | 801820b | 2016-04-25 21:06:53 +0100 | [diff] [blame] | 424 | if (table_types[i].name) |
| 425 | pr_cont(" %s=0x%lx ", |
| 426 | table_types[i].name, table); |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 427 | return 1; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 435 | int __init efi_config_parse_tables(void *config_tables, int count, int sz, |
| 436 | efi_config_table_type_t *arch_tables) |
| 437 | { |
| 438 | void *tablep; |
| 439 | int i; |
| 440 | |
| 441 | tablep = config_tables; |
| 442 | pr_info(""); |
| 443 | for (i = 0; i < count; i++) { |
| 444 | efi_guid_t guid; |
| 445 | unsigned long table; |
| 446 | |
| 447 | if (efi_enabled(EFI_64BIT)) { |
| 448 | u64 table64; |
| 449 | guid = ((efi_config_table_64_t *)tablep)->guid; |
| 450 | table64 = ((efi_config_table_64_t *)tablep)->table; |
| 451 | table = table64; |
| 452 | #ifndef CONFIG_64BIT |
| 453 | if (table64 >> 32) { |
| 454 | pr_cont("\n"); |
| 455 | pr_err("Table located above 4GB, disabling EFI.\n"); |
| 456 | return -EINVAL; |
| 457 | } |
| 458 | #endif |
| 459 | } else { |
| 460 | guid = ((efi_config_table_32_t *)tablep)->guid; |
| 461 | table = ((efi_config_table_32_t *)tablep)->table; |
| 462 | } |
| 463 | |
| 464 | if (!match_config_table(&guid, table, common_tables)) |
| 465 | match_config_table(&guid, table, arch_tables); |
| 466 | |
| 467 | tablep += sz; |
| 468 | } |
| 469 | pr_cont("\n"); |
| 470 | set_bit(EFI_CONFIG_TABLES, &efi.flags); |
Ard Biesheuvel | a104171 | 2015-09-23 07:29:34 -0700 | [diff] [blame] | 471 | |
| 472 | /* Parse the EFI Properties table if it exists */ |
| 473 | if (efi.properties_table != EFI_INVALID_TABLE_ADDR) { |
| 474 | efi_properties_table_t *tbl; |
| 475 | |
| 476 | tbl = early_memremap(efi.properties_table, sizeof(*tbl)); |
| 477 | if (tbl == NULL) { |
| 478 | pr_err("Could not map Properties table!\n"); |
| 479 | return -ENOMEM; |
| 480 | } |
| 481 | |
| 482 | if (tbl->memory_protection_attribute & |
| 483 | EFI_PROPERTIES_RUNTIME_MEMORY_PROTECTION_NON_EXECUTABLE_PE_DATA) |
| 484 | set_bit(EFI_NX_PE_DATA, &efi.flags); |
| 485 | |
| 486 | early_memunmap(tbl, sizeof(*tbl)); |
| 487 | } |
| 488 | |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 492 | int __init efi_config_init(efi_config_table_type_t *arch_tables) |
| 493 | { |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 494 | void *config_tables; |
| 495 | int sz, ret; |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 496 | |
| 497 | if (efi_enabled(EFI_64BIT)) |
| 498 | sz = sizeof(efi_config_table_64_t); |
| 499 | else |
| 500 | sz = sizeof(efi_config_table_32_t); |
| 501 | |
| 502 | /* |
| 503 | * Let's see what config tables the firmware passed to us. |
| 504 | */ |
| 505 | config_tables = early_memremap(efi.systab->tables, |
| 506 | efi.systab->nr_tables * sz); |
| 507 | if (config_tables == NULL) { |
| 508 | pr_err("Could not map Configuration table!\n"); |
| 509 | return -ENOMEM; |
| 510 | } |
| 511 | |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 512 | ret = efi_config_parse_tables(config_tables, efi.systab->nr_tables, sz, |
| 513 | arch_tables); |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 514 | |
Daniel Kiper | abc93f8 | 2014-06-30 19:52:56 +0200 | [diff] [blame] | 515 | early_memunmap(config_tables, efi.systab->nr_tables * sz); |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 516 | return ret; |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 517 | } |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 518 | |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 519 | /** |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 520 | * __efi_memmap_init - Common code for mapping the EFI memory map |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 521 | * @data: EFI memory map data |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 522 | * @late: Use early or late mapping function? |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 523 | * |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 524 | * This function takes care of figuring out which function to use to |
| 525 | * map the EFI memory map in efi.memmap based on how far into the boot |
| 526 | * we are. |
| 527 | * |
| 528 | * During bootup @late should be %false since we only have access to |
| 529 | * the early_memremap*() functions as the vmalloc space isn't setup. |
| 530 | * Once the kernel is fully booted we can fallback to the more robust |
| 531 | * memremap*() API. |
| 532 | * |
| 533 | * Returns zero on success, a negative error code on failure. |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 534 | */ |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 535 | static int __init |
| 536 | __efi_memmap_init(struct efi_memory_map_data *data, bool late) |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 537 | { |
| 538 | struct efi_memory_map map; |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 539 | phys_addr_t phys_map; |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 540 | |
| 541 | if (efi_enabled(EFI_PARAVIRT)) |
| 542 | return 0; |
| 543 | |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 544 | phys_map = data->phys_map; |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 545 | |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 546 | if (late) |
| 547 | map.map = memremap(phys_map, data->size, MEMREMAP_WB); |
| 548 | else |
| 549 | map.map = early_memremap(phys_map, data->size); |
| 550 | |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 551 | if (!map.map) { |
| 552 | pr_err("Could not map the memory map!\n"); |
| 553 | return -ENOMEM; |
| 554 | } |
| 555 | |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 556 | map.phys_map = data->phys_map; |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 557 | map.nr_map = data->size / data->desc_size; |
| 558 | map.map_end = map.map + data->size; |
| 559 | |
| 560 | map.desc_version = data->desc_version; |
| 561 | map.desc_size = data->desc_size; |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 562 | map.late = late; |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 563 | |
| 564 | set_bit(EFI_MEMMAP, &efi.flags); |
| 565 | |
| 566 | efi.memmap = map; |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 571 | /** |
| 572 | * efi_memmap_init_early - Map the EFI memory map data structure |
| 573 | * @data: EFI memory map data |
| 574 | * |
| 575 | * Use early_memremap() to map the passed in EFI memory map and assign |
| 576 | * it to efi.memmap. |
| 577 | */ |
| 578 | int __init efi_memmap_init_early(struct efi_memory_map_data *data) |
| 579 | { |
| 580 | /* Cannot go backwards */ |
| 581 | WARN_ON(efi.memmap.late); |
| 582 | |
| 583 | return __efi_memmap_init(data, false); |
| 584 | } |
| 585 | |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 586 | void __init efi_memmap_unmap(void) |
| 587 | { |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 588 | if (!efi.memmap.late) { |
| 589 | unsigned long size; |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 590 | |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 591 | size = efi.memmap.desc_size * efi.memmap.nr_map; |
| 592 | early_memunmap(efi.memmap.map, size); |
| 593 | } else { |
| 594 | memunmap(efi.memmap.map); |
| 595 | } |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 596 | |
Matt Fleming | 9479c7c | 2016-02-26 21:22:05 +0000 | [diff] [blame] | 597 | efi.memmap.map = NULL; |
| 598 | clear_bit(EFI_MEMMAP, &efi.flags); |
| 599 | } |
| 600 | |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame^] | 601 | /** |
| 602 | * efi_memmap_init_late - Map efi.memmap with memremap() |
| 603 | * @phys_addr: Physical address of the new EFI memory map |
| 604 | * @size: Size in bytes of the new EFI memory map |
| 605 | * |
| 606 | * Setup a mapping of the EFI memory map using ioremap_cache(). This |
| 607 | * function should only be called once the vmalloc space has been |
| 608 | * setup and is therefore not suitable for calling during early EFI |
| 609 | * initialise, e.g. in efi_init(). Additionally, it expects |
| 610 | * efi_memmap_init_early() to have already been called. |
| 611 | * |
| 612 | * The reason there are two EFI memmap initialisation |
| 613 | * (efi_memmap_init_early() and this late version) is because the |
| 614 | * early EFI memmap should be explicitly unmapped once EFI |
| 615 | * initialisation is complete as the fixmap space used to map the EFI |
| 616 | * memmap (via early_memremap()) is a scarce resource. |
| 617 | * |
| 618 | * This late mapping is intended to persist for the duration of |
| 619 | * runtime so that things like efi_mem_desc_lookup() and |
| 620 | * efi_mem_attributes() always work. |
| 621 | * |
| 622 | * Returns zero on success, a negative error code on failure. |
| 623 | */ |
| 624 | int __init efi_memmap_init_late(phys_addr_t addr, unsigned long size) |
| 625 | { |
| 626 | struct efi_memory_map_data data = { |
| 627 | .phys_map = addr, |
| 628 | .size = size, |
| 629 | }; |
| 630 | |
| 631 | /* Did we forget to unmap the early EFI memmap? */ |
| 632 | WARN_ON(efi.memmap.map); |
| 633 | |
| 634 | /* Were we already called? */ |
| 635 | WARN_ON(efi.memmap.late); |
| 636 | |
| 637 | /* |
| 638 | * It makes no sense to allow callers to register different |
| 639 | * values for the following fields. Copy them out of the |
| 640 | * existing early EFI memmap. |
| 641 | */ |
| 642 | data.desc_version = efi.memmap.desc_version; |
| 643 | data.desc_size = efi.memmap.desc_size; |
| 644 | |
| 645 | return __efi_memmap_init(&data, true); |
| 646 | } |
| 647 | |
Lee, Chun-Yi | 28d5402 | 2014-07-09 18:39:29 +0800 | [diff] [blame] | 648 | #ifdef CONFIG_EFI_VARS_MODULE |
| 649 | static int __init efi_load_efivars(void) |
| 650 | { |
| 651 | struct platform_device *pdev; |
| 652 | |
| 653 | if (!efi_enabled(EFI_RUNTIME_SERVICES)) |
| 654 | return 0; |
| 655 | |
| 656 | pdev = platform_device_register_simple("efivars", 0, NULL, 0); |
| 657 | return IS_ERR(pdev) ? PTR_ERR(pdev) : 0; |
| 658 | } |
| 659 | device_initcall(efi_load_efivars); |
| 660 | #endif |
| 661 | |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 662 | #ifdef CONFIG_EFI_PARAMS_FROM_FDT |
| 663 | |
| 664 | #define UEFI_PARAM(name, prop, field) \ |
| 665 | { \ |
| 666 | { name }, \ |
| 667 | { prop }, \ |
| 668 | offsetof(struct efi_fdt_params, field), \ |
| 669 | FIELD_SIZEOF(struct efi_fdt_params, field) \ |
| 670 | } |
| 671 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 672 | struct params { |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 673 | const char name[32]; |
| 674 | const char propname[32]; |
| 675 | int offset; |
| 676 | int size; |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 677 | }; |
| 678 | |
| 679 | static __initdata struct params fdt_params[] = { |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 680 | UEFI_PARAM("System Table", "linux,uefi-system-table", system_table), |
| 681 | UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap), |
| 682 | UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size), |
| 683 | UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size), |
| 684 | UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver) |
| 685 | }; |
| 686 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 687 | static __initdata struct params xen_fdt_params[] = { |
| 688 | UEFI_PARAM("System Table", "xen,uefi-system-table", system_table), |
| 689 | UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap), |
| 690 | UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size), |
| 691 | UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size), |
| 692 | UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver) |
| 693 | }; |
| 694 | |
| 695 | #define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params) |
| 696 | |
| 697 | static __initdata struct { |
| 698 | const char *uname; |
| 699 | const char *subnode; |
| 700 | struct params *params; |
| 701 | } dt_params[] = { |
| 702 | { "hypervisor", "uefi", xen_fdt_params }, |
| 703 | { "chosen", NULL, fdt_params }, |
| 704 | }; |
| 705 | |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 706 | struct param_info { |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 707 | int found; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 708 | void *params; |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 709 | const char *missing; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 710 | }; |
| 711 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 712 | static int __init __find_uefi_params(unsigned long node, |
| 713 | struct param_info *info, |
| 714 | struct params *params) |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 715 | { |
Catalin Marinas | 6fb8cc8 | 2014-06-02 11:31:06 +0100 | [diff] [blame] | 716 | const void *prop; |
| 717 | void *dest; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 718 | u64 val; |
Catalin Marinas | 6fb8cc8 | 2014-06-02 11:31:06 +0100 | [diff] [blame] | 719 | int i, len; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 720 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 721 | for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) { |
| 722 | prop = of_get_flat_dt_prop(node, params[i].propname, &len); |
| 723 | if (!prop) { |
| 724 | info->missing = params[i].name; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 725 | return 0; |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | dest = info->params + params[i].offset; |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 729 | info->found++; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 730 | |
| 731 | val = of_read_number(prop, len / sizeof(u32)); |
| 732 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 733 | if (params[i].size == sizeof(u32)) |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 734 | *(u32 *)dest = val; |
| 735 | else |
| 736 | *(u64 *)dest = val; |
| 737 | |
Leif Lindholm | 7968c0e | 2015-08-26 14:24:58 +0100 | [diff] [blame] | 738 | if (efi_enabled(EFI_DBG)) |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 739 | pr_info(" %s: 0x%0*llx\n", params[i].name, |
| 740 | params[i].size * 2, val); |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 741 | } |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 742 | |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 743 | return 1; |
| 744 | } |
| 745 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 746 | static int __init fdt_find_uefi_params(unsigned long node, const char *uname, |
| 747 | int depth, void *data) |
| 748 | { |
| 749 | struct param_info *info = data; |
| 750 | int i; |
| 751 | |
| 752 | for (i = 0; i < ARRAY_SIZE(dt_params); i++) { |
| 753 | const char *subnode = dt_params[i].subnode; |
| 754 | |
| 755 | if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) { |
| 756 | info->missing = dt_params[i].params[0].name; |
| 757 | continue; |
| 758 | } |
| 759 | |
| 760 | if (subnode) { |
| 761 | node = of_get_flat_dt_subnode_by_name(node, subnode); |
| 762 | if (node < 0) |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | return __find_uefi_params(node, info, dt_params[i].params); |
| 767 | } |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
Leif Lindholm | 7968c0e | 2015-08-26 14:24:58 +0100 | [diff] [blame] | 772 | int __init efi_get_fdt_params(struct efi_fdt_params *params) |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 773 | { |
| 774 | struct param_info info; |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 775 | int ret; |
| 776 | |
| 777 | pr_info("Getting EFI parameters from FDT:\n"); |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 778 | |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 779 | info.found = 0; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 780 | info.params = params; |
| 781 | |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 782 | ret = of_scan_flat_dt(fdt_find_uefi_params, &info); |
| 783 | if (!info.found) |
| 784 | pr_info("UEFI not found.\n"); |
| 785 | else if (!ret) |
| 786 | pr_err("Can't find '%s' in device tree!\n", |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 787 | info.missing); |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 788 | |
| 789 | return ret; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 790 | } |
| 791 | #endif /* CONFIG_EFI_PARAMS_FROM_FDT */ |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 792 | |
| 793 | static __initdata char memory_type_name[][20] = { |
| 794 | "Reserved", |
| 795 | "Loader Code", |
| 796 | "Loader Data", |
| 797 | "Boot Code", |
| 798 | "Boot Data", |
| 799 | "Runtime Code", |
| 800 | "Runtime Data", |
| 801 | "Conventional Memory", |
| 802 | "Unusable Memory", |
| 803 | "ACPI Reclaim Memory", |
| 804 | "ACPI Memory NVS", |
| 805 | "Memory Mapped I/O", |
| 806 | "MMIO Port Space", |
Robert Elliott | 35575e0 | 2016-02-01 22:07:07 +0000 | [diff] [blame] | 807 | "PAL Code", |
| 808 | "Persistent Memory", |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 809 | }; |
| 810 | |
| 811 | char * __init efi_md_typeattr_format(char *buf, size_t size, |
| 812 | const efi_memory_desc_t *md) |
| 813 | { |
| 814 | char *pos; |
| 815 | int type_len; |
| 816 | u64 attr; |
| 817 | |
| 818 | pos = buf; |
| 819 | if (md->type >= ARRAY_SIZE(memory_type_name)) |
| 820 | type_len = snprintf(pos, size, "[type=%u", md->type); |
| 821 | else |
| 822 | type_len = snprintf(pos, size, "[%-*s", |
| 823 | (int)(sizeof(memory_type_name[0]) - 1), |
| 824 | memory_type_name[md->type]); |
| 825 | if (type_len >= size) |
| 826 | return buf; |
| 827 | |
| 828 | pos += type_len; |
| 829 | size -= type_len; |
| 830 | |
| 831 | attr = md->attribute; |
| 832 | if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT | |
Ard Biesheuvel | 87db73ae | 2015-08-07 09:36:54 +0100 | [diff] [blame] | 833 | EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO | |
| 834 | EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP | |
Robert Elliott | c016ca0 | 2016-02-01 22:07:06 +0000 | [diff] [blame] | 835 | EFI_MEMORY_NV | |
Taku Izumi | 8be4432 | 2015-08-27 02:11:19 +0900 | [diff] [blame] | 836 | EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE)) |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 837 | snprintf(pos, size, "|attr=0x%016llx]", |
| 838 | (unsigned long long)attr); |
| 839 | else |
Robert Elliott | c016ca0 | 2016-02-01 22:07:06 +0000 | [diff] [blame] | 840 | snprintf(pos, size, |
| 841 | "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]", |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 842 | attr & EFI_MEMORY_RUNTIME ? "RUN" : "", |
Taku Izumi | 8be4432 | 2015-08-27 02:11:19 +0900 | [diff] [blame] | 843 | attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "", |
Robert Elliott | c016ca0 | 2016-02-01 22:07:06 +0000 | [diff] [blame] | 844 | attr & EFI_MEMORY_NV ? "NV" : "", |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 845 | attr & EFI_MEMORY_XP ? "XP" : "", |
| 846 | attr & EFI_MEMORY_RP ? "RP" : "", |
| 847 | attr & EFI_MEMORY_WP ? "WP" : "", |
Ard Biesheuvel | 87db73ae | 2015-08-07 09:36:54 +0100 | [diff] [blame] | 848 | attr & EFI_MEMORY_RO ? "RO" : "", |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 849 | attr & EFI_MEMORY_UCE ? "UCE" : "", |
| 850 | attr & EFI_MEMORY_WB ? "WB" : "", |
| 851 | attr & EFI_MEMORY_WT ? "WT" : "", |
| 852 | attr & EFI_MEMORY_WC ? "WC" : "", |
| 853 | attr & EFI_MEMORY_UC ? "UC" : ""); |
| 854 | return buf; |
| 855 | } |
Jonathan (Zhixiong) Zhang | 7bf7931 | 2015-08-07 09:36:57 +0100 | [diff] [blame] | 856 | |
| 857 | /* |
| 858 | * efi_mem_attributes - lookup memmap attributes for physical address |
| 859 | * @phys_addr: the physical address to lookup |
| 860 | * |
| 861 | * Search in the EFI memory map for the region covering |
| 862 | * @phys_addr. Returns the EFI memory attributes if the region |
| 863 | * was found in the memory map, 0 otherwise. |
| 864 | * |
| 865 | * Despite being marked __weak, most architectures should *not* |
| 866 | * override this function. It is __weak solely for the benefit |
| 867 | * of ia64 which has a funky EFI memory map that doesn't work |
| 868 | * the same way as other architectures. |
| 869 | */ |
| 870 | u64 __weak efi_mem_attributes(unsigned long phys_addr) |
| 871 | { |
| 872 | efi_memory_desc_t *md; |
Jonathan (Zhixiong) Zhang | 7bf7931 | 2015-08-07 09:36:57 +0100 | [diff] [blame] | 873 | |
| 874 | if (!efi_enabled(EFI_MEMMAP)) |
| 875 | return 0; |
| 876 | |
Matt Fleming | 78ce248 | 2016-04-25 21:06:38 +0100 | [diff] [blame] | 877 | for_each_efi_memory_desc(md) { |
Jonathan (Zhixiong) Zhang | 7bf7931 | 2015-08-07 09:36:57 +0100 | [diff] [blame] | 878 | if ((md->phys_addr <= phys_addr) && |
| 879 | (phys_addr < (md->phys_addr + |
| 880 | (md->num_pages << EFI_PAGE_SHIFT)))) |
| 881 | return md->attribute; |
| 882 | } |
| 883 | return 0; |
| 884 | } |
Matt Fleming | 806b035 | 2016-04-25 21:06:58 +0100 | [diff] [blame] | 885 | |
| 886 | int efi_status_to_err(efi_status_t status) |
| 887 | { |
| 888 | int err; |
| 889 | |
| 890 | switch (status) { |
| 891 | case EFI_SUCCESS: |
| 892 | err = 0; |
| 893 | break; |
| 894 | case EFI_INVALID_PARAMETER: |
| 895 | err = -EINVAL; |
| 896 | break; |
| 897 | case EFI_OUT_OF_RESOURCES: |
| 898 | err = -ENOSPC; |
| 899 | break; |
| 900 | case EFI_DEVICE_ERROR: |
| 901 | err = -EIO; |
| 902 | break; |
| 903 | case EFI_WRITE_PROTECTED: |
| 904 | err = -EROFS; |
| 905 | break; |
| 906 | case EFI_SECURITY_VIOLATION: |
| 907 | err = -EACCES; |
| 908 | break; |
| 909 | case EFI_NOT_FOUND: |
| 910 | err = -ENOENT; |
| 911 | break; |
| 912 | default: |
| 913 | err = -EINVAL; |
| 914 | } |
| 915 | |
| 916 | return err; |
| 917 | } |