Thomas Gleixner | 55716d2 | 2019-06-01 10:08:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 2 | /* |
| 3 | * efi.c - EFI subsystem |
| 4 | * |
| 5 | * Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com> |
| 6 | * Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com> |
| 7 | * Copyright (C) 2013 Tom Gundersen <teg@jklm.no> |
| 8 | * |
| 9 | * This code registers /sys/firmware/efi{,/efivars} when EFI is supported, |
| 10 | * allowing the efivarfs to be mounted or the efivars module to be loaded. |
| 11 | * The existance of /sys/firmware/efi may also be used by userspace to |
| 12 | * determine that the system supports EFI. |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 16 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 17 | #include <linux/kobject.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/efi.h> |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 22 | #include <linux/of.h> |
| 23 | #include <linux/of_fdt.h> |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 24 | #include <linux/io.h> |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 25 | #include <linux/kexec.h> |
Lee, Chun-Yi | 28d5402 | 2014-07-09 18:39:29 +0800 | [diff] [blame] | 26 | #include <linux/platform_device.h> |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 27 | #include <linux/random.h> |
| 28 | #include <linux/reboot.h> |
Octavian Purdila | 475fb4e | 2016-07-08 19:13:12 +0300 | [diff] [blame] | 29 | #include <linux/slab.h> |
| 30 | #include <linux/acpi.h> |
| 31 | #include <linux/ucs2_string.h> |
Matt Fleming | 816e761 | 2016-02-29 21:22:52 +0000 | [diff] [blame] | 32 | #include <linux/memblock.h> |
Matthew Garrett | 1957a85b | 2019-08-19 17:18:04 -0700 | [diff] [blame] | 33 | #include <linux/security.h> |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 34 | |
Ard Biesheuvel | 0f7f2f0 | 2016-01-12 14:22:46 +0100 | [diff] [blame] | 35 | #include <asm/early_ioremap.h> |
Ard Biesheuvel | f7d9248 | 2015-11-30 13:28:19 +0100 | [diff] [blame] | 36 | |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 37 | struct efi __read_mostly efi = { |
Ard Biesheuvel | bf92486 | 2015-09-09 10:08:15 +0200 | [diff] [blame] | 38 | .acpi = EFI_INVALID_TABLE_ADDR, |
| 39 | .acpi20 = EFI_INVALID_TABLE_ADDR, |
| 40 | .smbios = EFI_INVALID_TABLE_ADDR, |
| 41 | .smbios3 = EFI_INVALID_TABLE_ADDR, |
Ard Biesheuvel | bf92486 | 2015-09-09 10:08:15 +0200 | [diff] [blame] | 42 | .esrt = EFI_INVALID_TABLE_ADDR, |
Ard Biesheuvel | 71e0940 | 2018-09-21 09:32:44 -0700 | [diff] [blame] | 43 | .tpm_log = EFI_INVALID_TABLE_ADDR, |
Matthew Garrett | c46f340 | 2019-05-20 13:54:59 -0700 | [diff] [blame] | 44 | .tpm_final_log = EFI_INVALID_TABLE_ADDR, |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 45 | }; |
| 46 | EXPORT_SYMBOL(efi); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 47 | |
Ard Biesheuvel | 5d288db | 2020-01-22 14:58:15 +0100 | [diff] [blame] | 48 | static unsigned long __ro_after_init rng_seed = EFI_INVALID_TABLE_ADDR; |
Ard Biesheuvel | b7846e6 | 2020-01-22 15:06:54 +0100 | [diff] [blame] | 49 | static unsigned long __initdata mem_reserve = EFI_INVALID_TABLE_ADDR; |
Ard Biesheuvel | 5d288db | 2020-01-22 14:58:15 +0100 | [diff] [blame] | 50 | |
Sai Praneeth | 7e904a9 | 2018-03-12 08:44:56 +0000 | [diff] [blame] | 51 | struct mm_struct efi_mm = { |
| 52 | .mm_rb = RB_ROOT, |
| 53 | .mm_users = ATOMIC_INIT(2), |
| 54 | .mm_count = ATOMIC_INIT(1), |
| 55 | .mmap_sem = __RWSEM_INITIALIZER(efi_mm.mmap_sem), |
| 56 | .page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock), |
| 57 | .mmlist = LIST_HEAD_INIT(efi_mm.mmlist), |
Rik van Riel | c1a2f7f | 2018-07-16 15:03:31 -0400 | [diff] [blame] | 58 | .cpu_bitmap = { [BITS_TO_LONGS(NR_CPUS)] = 0}, |
Sai Praneeth | 7e904a9 | 2018-03-12 08:44:56 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Sai Praneeth | 3eb420e | 2018-07-11 11:40:35 +0200 | [diff] [blame] | 61 | struct workqueue_struct *efi_rts_wq; |
| 62 | |
Dave Young | b2e0a54 | 2014-08-14 17:15:26 +0800 | [diff] [blame] | 63 | static bool disable_runtime; |
| 64 | static int __init setup_noefi(char *arg) |
| 65 | { |
| 66 | disable_runtime = true; |
| 67 | return 0; |
| 68 | } |
| 69 | early_param("noefi", setup_noefi); |
| 70 | |
| 71 | bool efi_runtime_disabled(void) |
| 72 | { |
| 73 | return disable_runtime; |
| 74 | } |
| 75 | |
Dan Williams | b617c52 | 2019-11-06 17:43:11 -0800 | [diff] [blame] | 76 | bool __pure __efi_soft_reserve_enabled(void) |
| 77 | { |
| 78 | return !efi_enabled(EFI_MEM_NO_SOFT_RESERVE); |
| 79 | } |
| 80 | |
Dave Young | 5ae3683 | 2014-08-14 17:15:28 +0800 | [diff] [blame] | 81 | static int __init parse_efi_cmdline(char *str) |
| 82 | { |
Ricardo Neri | 9115c75 | 2015-07-15 19:36:03 -0700 | [diff] [blame] | 83 | if (!str) { |
| 84 | pr_warn("need at least one option\n"); |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | |
Leif Lindholm | 12dd00e | 2015-08-26 14:24:56 +0100 | [diff] [blame] | 88 | if (parse_option_str(str, "debug")) |
| 89 | set_bit(EFI_DBG, &efi.flags); |
| 90 | |
Dave Young | 5ae3683 | 2014-08-14 17:15:28 +0800 | [diff] [blame] | 91 | if (parse_option_str(str, "noruntime")) |
| 92 | disable_runtime = true; |
| 93 | |
Dan Williams | b617c52 | 2019-11-06 17:43:11 -0800 | [diff] [blame] | 94 | if (parse_option_str(str, "nosoftreserve")) |
| 95 | set_bit(EFI_MEM_NO_SOFT_RESERVE, &efi.flags); |
| 96 | |
Dave Young | 5ae3683 | 2014-08-14 17:15:28 +0800 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | early_param("efi", parse_efi_cmdline); |
| 100 | |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 101 | struct kobject *efi_kobj; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 102 | |
| 103 | /* |
| 104 | * Let's not leave out systab information that snuck into |
| 105 | * the efivars driver |
Dave Young | 0b02e44 | 2017-12-06 09:50:10 +0000 | [diff] [blame] | 106 | * Note, do not add more fields in systab sysfs file as it breaks sysfs |
| 107 | * one value per file rule! |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 108 | */ |
| 109 | static ssize_t systab_show(struct kobject *kobj, |
| 110 | struct kobj_attribute *attr, char *buf) |
| 111 | { |
| 112 | char *str = buf; |
| 113 | |
| 114 | if (!kobj || !buf) |
| 115 | return -EINVAL; |
| 116 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 117 | if (efi.acpi20 != EFI_INVALID_TABLE_ADDR) |
| 118 | str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20); |
| 119 | if (efi.acpi != EFI_INVALID_TABLE_ADDR) |
| 120 | str += sprintf(str, "ACPI=0x%lx\n", efi.acpi); |
Jean Delvare | b119fe0 | 2015-04-30 15:23:05 +0200 | [diff] [blame] | 121 | /* |
| 122 | * If both SMBIOS and SMBIOS3 entry points are implemented, the |
| 123 | * SMBIOS3 entry point shall be preferred, so we list it first to |
| 124 | * let applications stop parsing after the first match. |
| 125 | */ |
Ard Biesheuvel | e1ccbbc | 2014-10-14 16:34:47 +0200 | [diff] [blame] | 126 | if (efi.smbios3 != EFI_INVALID_TABLE_ADDR) |
| 127 | str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3); |
Jean Delvare | b119fe0 | 2015-04-30 15:23:05 +0200 | [diff] [blame] | 128 | if (efi.smbios != EFI_INVALID_TABLE_ADDR) |
| 129 | str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 130 | |
Ard Biesheuvel | fd506e0 | 2020-01-19 16:17:59 +0100 | [diff] [blame] | 131 | if (IS_ENABLED(CONFIG_IA64) || IS_ENABLED(CONFIG_X86)) { |
Ard Biesheuvel | 120540f | 2020-01-19 15:43:53 +0100 | [diff] [blame] | 132 | extern char *efi_systab_show_arch(char *str); |
| 133 | |
| 134 | str = efi_systab_show_arch(str); |
| 135 | } |
| 136 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 137 | return str - buf; |
| 138 | } |
| 139 | |
Greg Kroah-Hartman | af97a77 | 2017-12-06 09:50:08 +0000 | [diff] [blame] | 140 | static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 141 | |
Steve McIntyre | 2859dff | 2015-01-09 15:29:53 +0000 | [diff] [blame] | 142 | static ssize_t fw_platform_size_show(struct kobject *kobj, |
| 143 | struct kobj_attribute *attr, char *buf) |
| 144 | { |
| 145 | return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32); |
| 146 | } |
| 147 | |
Ard Biesheuvel | 9cd437a | 2020-01-20 17:23:21 +0100 | [diff] [blame^] | 148 | extern __weak struct kobj_attribute efi_attr_fw_vendor; |
| 149 | extern __weak struct kobj_attribute efi_attr_runtime; |
| 150 | extern __weak struct kobj_attribute efi_attr_config_table; |
Steve McIntyre | 2859dff | 2015-01-09 15:29:53 +0000 | [diff] [blame] | 151 | static struct kobj_attribute efi_attr_fw_platform_size = |
| 152 | __ATTR_RO(fw_platform_size); |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 153 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 154 | static struct attribute *efi_subsys_attrs[] = { |
| 155 | &efi_attr_systab.attr, |
Ard Biesheuvel | 9cd437a | 2020-01-20 17:23:21 +0100 | [diff] [blame^] | 156 | &efi_attr_fw_platform_size.attr, |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 157 | &efi_attr_fw_vendor.attr, |
| 158 | &efi_attr_runtime.attr, |
| 159 | &efi_attr_config_table.attr, |
| 160 | NULL, |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
Ard Biesheuvel | 9cd437a | 2020-01-20 17:23:21 +0100 | [diff] [blame^] | 163 | umode_t __weak efi_attr_is_visible(struct kobject *kobj, struct attribute *attr, |
| 164 | int n) |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 165 | { |
Daniel Kiper | 9f27bc5 | 2014-06-30 19:52:58 +0200 | [diff] [blame] | 166 | return attr->mode; |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 167 | } |
| 168 | |
Arvind Yadav | 3ad6bd7 | 2017-08-18 20:49:46 +0100 | [diff] [blame] | 169 | static const struct attribute_group efi_subsys_attr_group = { |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 170 | .attrs = efi_subsys_attrs, |
Dave Young | a0998eb | 2013-12-20 18:02:17 +0800 | [diff] [blame] | 171 | .is_visible = efi_attr_is_visible, |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | static struct efivars generic_efivars; |
| 175 | static struct efivar_operations generic_ops; |
| 176 | |
| 177 | static int generic_ops_register(void) |
| 178 | { |
| 179 | generic_ops.get_variable = efi.get_variable; |
| 180 | generic_ops.set_variable = efi.set_variable; |
Ard Biesheuvel | 9c6672a | 2016-02-01 22:06:55 +0000 | [diff] [blame] | 181 | generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 182 | generic_ops.get_next_variable = efi.get_next_variable; |
Matt Fleming | a614e19 | 2013-04-30 11:30:24 +0100 | [diff] [blame] | 183 | generic_ops.query_variable_store = efi_query_variable_store; |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 184 | |
| 185 | return efivars_register(&generic_efivars, &generic_ops, efi_kobj); |
| 186 | } |
| 187 | |
| 188 | static void generic_ops_unregister(void) |
| 189 | { |
| 190 | efivars_unregister(&generic_efivars); |
| 191 | } |
| 192 | |
Octavian Purdila | 475fb4e | 2016-07-08 19:13:12 +0300 | [diff] [blame] | 193 | #if IS_ENABLED(CONFIG_ACPI) |
| 194 | #define EFIVAR_SSDT_NAME_MAX 16 |
| 195 | static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata; |
| 196 | static int __init efivar_ssdt_setup(char *str) |
| 197 | { |
Matthew Garrett | 1957a85b | 2019-08-19 17:18:04 -0700 | [diff] [blame] | 198 | int ret = security_locked_down(LOCKDOWN_ACPI_TABLES); |
| 199 | |
| 200 | if (ret) |
| 201 | return ret; |
| 202 | |
Octavian Purdila | 475fb4e | 2016-07-08 19:13:12 +0300 | [diff] [blame] | 203 | if (strlen(str) < sizeof(efivar_ssdt)) |
| 204 | memcpy(efivar_ssdt, str, strlen(str)); |
| 205 | else |
| 206 | pr_warn("efivar_ssdt: name too long: %s\n", str); |
| 207 | return 0; |
| 208 | } |
| 209 | __setup("efivar_ssdt=", efivar_ssdt_setup); |
| 210 | |
| 211 | static __init int efivar_ssdt_iter(efi_char16_t *name, efi_guid_t vendor, |
| 212 | unsigned long name_size, void *data) |
| 213 | { |
| 214 | struct efivar_entry *entry; |
| 215 | struct list_head *list = data; |
| 216 | char utf8_name[EFIVAR_SSDT_NAME_MAX]; |
| 217 | int limit = min_t(unsigned long, EFIVAR_SSDT_NAME_MAX, name_size); |
| 218 | |
| 219 | ucs2_as_utf8(utf8_name, name, limit - 1); |
| 220 | if (strncmp(utf8_name, efivar_ssdt, limit) != 0) |
| 221 | return 0; |
| 222 | |
| 223 | entry = kmalloc(sizeof(*entry), GFP_KERNEL); |
| 224 | if (!entry) |
| 225 | return 0; |
| 226 | |
| 227 | memcpy(entry->var.VariableName, name, name_size); |
| 228 | memcpy(&entry->var.VendorGuid, &vendor, sizeof(efi_guid_t)); |
| 229 | |
| 230 | efivar_entry_add(entry, list); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static __init int efivar_ssdt_load(void) |
| 236 | { |
| 237 | LIST_HEAD(entries); |
| 238 | struct efivar_entry *entry, *aux; |
| 239 | unsigned long size; |
| 240 | void *data; |
| 241 | int ret; |
| 242 | |
Ard Biesheuvel | c05f8f9 | 2019-10-02 18:58:59 +0200 | [diff] [blame] | 243 | if (!efivar_ssdt[0]) |
| 244 | return 0; |
| 245 | |
Octavian Purdila | 475fb4e | 2016-07-08 19:13:12 +0300 | [diff] [blame] | 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); |
Dan Carpenter | a75dcb5 | 2016-10-18 15:33:18 +0100 | [diff] [blame] | 261 | if (!data) { |
| 262 | ret = -ENOMEM; |
Octavian Purdila | 475fb4e | 2016-07-08 19:13:12 +0300 | [diff] [blame] | 263 | goto free_entry; |
Dan Carpenter | a75dcb5 | 2016-10-18 15:33:18 +0100 | [diff] [blame] | 264 | } |
Octavian Purdila | 475fb4e | 2016-07-08 19:13:12 +0300 | [diff] [blame] | 265 | |
| 266 | ret = efivar_entry_get(entry, NULL, &size, data); |
| 267 | if (ret) { |
| 268 | pr_err("failed to get var data\n"); |
| 269 | goto free_data; |
| 270 | } |
| 271 | |
Nikolaus Voss | 1770093 | 2019-10-25 14:36:53 -0700 | [diff] [blame] | 272 | ret = acpi_load_table(data, NULL); |
Octavian Purdila | 475fb4e | 2016-07-08 19:13:12 +0300 | [diff] [blame] | 273 | if (ret) { |
| 274 | pr_err("failed to load table: %d\n", ret); |
| 275 | goto free_data; |
| 276 | } |
| 277 | |
| 278 | goto free_entry; |
| 279 | |
| 280 | free_data: |
| 281 | kfree(data); |
| 282 | |
| 283 | free_entry: |
| 284 | kfree(entry); |
| 285 | } |
| 286 | |
| 287 | return ret; |
| 288 | } |
| 289 | #else |
| 290 | static inline int efivar_ssdt_load(void) { return 0; } |
| 291 | #endif |
| 292 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 293 | /* |
| 294 | * We register the efi subsystem with the firmware subsystem and the |
| 295 | * efivars subsystem with the efi subsystem, if the system was booted with |
| 296 | * EFI. |
| 297 | */ |
| 298 | static int __init efisubsys_init(void) |
| 299 | { |
| 300 | int error; |
| 301 | |
| 302 | if (!efi_enabled(EFI_BOOT)) |
| 303 | return 0; |
| 304 | |
Sai Praneeth | 3eb420e | 2018-07-11 11:40:35 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Since we process only one efi_runtime_service() at a time, an |
| 307 | * ordered workqueue (which creates only one execution context) |
| 308 | * should suffice all our needs. |
| 309 | */ |
| 310 | efi_rts_wq = alloc_ordered_workqueue("efi_rts_wq", 0); |
| 311 | if (!efi_rts_wq) { |
| 312 | pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n"); |
| 313 | clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); |
| 314 | return 0; |
| 315 | } |
| 316 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 317 | /* We register the efi directory at /sys/firmware/efi */ |
| 318 | efi_kobj = kobject_create_and_add("efi", firmware_kobj); |
| 319 | if (!efi_kobj) { |
| 320 | pr_err("efi: Firmware registration failed.\n"); |
| 321 | return -ENOMEM; |
| 322 | } |
| 323 | |
| 324 | error = generic_ops_register(); |
| 325 | if (error) |
| 326 | goto err_put; |
| 327 | |
Octavian Purdila | 475fb4e | 2016-07-08 19:13:12 +0300 | [diff] [blame] | 328 | if (efi_enabled(EFI_RUNTIME_SERVICES)) |
| 329 | efivar_ssdt_load(); |
| 330 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 331 | error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group); |
| 332 | if (error) { |
| 333 | pr_err("efi: Sysfs attribute export failed with error %d.\n", |
| 334 | error); |
| 335 | goto err_unregister; |
| 336 | } |
| 337 | |
Dave Young | 926172d | 2013-12-20 18:02:18 +0800 | [diff] [blame] | 338 | error = efi_runtime_map_init(efi_kobj); |
| 339 | if (error) |
| 340 | goto err_remove_group; |
| 341 | |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 342 | /* and the standard mountpoint for efivarfs */ |
Eric W. Biederman | f9bb488 | 2015-05-13 17:35:41 -0500 | [diff] [blame] | 343 | error = sysfs_create_mount_point(efi_kobj, "efivars"); |
| 344 | if (error) { |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 345 | pr_err("efivars: Subsystem registration failed.\n"); |
Tom Gundersen | a9499fa | 2013-02-08 15:37:06 +0000 | [diff] [blame] | 346 | goto err_remove_group; |
| 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | |
| 351 | err_remove_group: |
| 352 | sysfs_remove_group(efi_kobj, &efi_subsys_attr_group); |
| 353 | err_unregister: |
| 354 | generic_ops_unregister(); |
| 355 | err_put: |
| 356 | kobject_put(efi_kobj); |
| 357 | return error; |
| 358 | } |
| 359 | |
| 360 | subsys_initcall(efisubsys_init); |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 361 | |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 362 | /* |
| 363 | * Find the efi memory descriptor for a given physical address. Given a |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame] | 364 | * physical address, determine if it exists within an EFI Memory Map entry, |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 365 | * and if so, populate the supplied memory descriptor with the appropriate |
| 366 | * data. |
| 367 | */ |
Ard Biesheuvel | 7e1550b | 2018-07-11 11:40:39 +0200 | [diff] [blame] | 368 | int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md) |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 369 | { |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame] | 370 | efi_memory_desc_t *md; |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 371 | |
| 372 | if (!efi_enabled(EFI_MEMMAP)) { |
| 373 | pr_err_once("EFI_MEMMAP is not enabled.\n"); |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 377 | if (!out_md) { |
| 378 | pr_err_once("out_md is null.\n"); |
| 379 | return -EINVAL; |
| 380 | } |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 381 | |
Matt Fleming | dca0f97 | 2016-02-27 15:52:50 +0000 | [diff] [blame] | 382 | for_each_efi_memory_desc(md) { |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 383 | u64 size; |
| 384 | u64 end; |
| 385 | |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 386 | size = md->num_pages << EFI_PAGE_SHIFT; |
| 387 | end = md->phys_addr + size; |
| 388 | if (phys_addr >= md->phys_addr && phys_addr < end) { |
| 389 | memcpy(out_md, md, sizeof(*out_md)); |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 390 | return 0; |
| 391 | } |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 392 | } |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 393 | return -ENOENT; |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Calculate the highest address of an efi memory descriptor. |
| 398 | */ |
| 399 | u64 __init efi_mem_desc_end(efi_memory_desc_t *md) |
| 400 | { |
| 401 | u64 size = md->num_pages << EFI_PAGE_SHIFT; |
| 402 | u64 end = md->phys_addr + size; |
| 403 | return end; |
| 404 | } |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 405 | |
Matt Fleming | 816e761 | 2016-02-29 21:22:52 +0000 | [diff] [blame] | 406 | void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {} |
| 407 | |
| 408 | /** |
| 409 | * efi_mem_reserve - Reserve an EFI memory region |
| 410 | * @addr: Physical address to reserve |
| 411 | * @size: Size of reservation |
| 412 | * |
| 413 | * Mark a region as reserved from general kernel allocation and |
| 414 | * prevent it being released by efi_free_boot_services(). |
| 415 | * |
| 416 | * This function should be called drivers once they've parsed EFI |
| 417 | * configuration tables to figure out where their data lives, e.g. |
| 418 | * efi_esrt_init(). |
| 419 | */ |
| 420 | void __init efi_mem_reserve(phys_addr_t addr, u64 size) |
| 421 | { |
| 422 | if (!memblock_is_region_reserved(addr, size)) |
| 423 | memblock_reserve(addr, size); |
| 424 | |
| 425 | /* |
| 426 | * Some architectures (x86) reserve all boot services ranges |
| 427 | * until efi_free_boot_services() because of buggy firmware |
| 428 | * implementations. This means the above memblock_reserve() is |
| 429 | * superfluous on x86 and instead what it needs to do is |
| 430 | * ensure the @start, @size is not freed. |
| 431 | */ |
| 432 | efi_arch_mem_reserve(addr, size); |
| 433 | } |
| 434 | |
Ard Biesheuvel | 06c0bd9 | 2020-01-22 14:40:57 +0100 | [diff] [blame] | 435 | static const efi_config_table_type_t common_tables[] __initconst = { |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 436 | {ACPI_20_TABLE_GUID, "ACPI 2.0", &efi.acpi20}, |
| 437 | {ACPI_TABLE_GUID, "ACPI", &efi.acpi}, |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 438 | {SMBIOS_TABLE_GUID, "SMBIOS", &efi.smbios}, |
Ard Biesheuvel | e1ccbbc | 2014-10-14 16:34:47 +0200 | [diff] [blame] | 439 | {SMBIOS3_TABLE_GUID, "SMBIOS 3.0", &efi.smbios3}, |
Peter Jones | 0bb5490 | 2015-04-28 18:44:31 -0400 | [diff] [blame] | 440 | {EFI_SYSTEM_RESOURCE_TABLE_GUID, "ESRT", &efi.esrt}, |
Ard Biesheuvel | a17e809 | 2020-01-22 15:05:12 +0100 | [diff] [blame] | 441 | {EFI_MEMORY_ATTRIBUTES_TABLE_GUID, "MEMATTR", &efi_mem_attr_table}, |
Ard Biesheuvel | 5d288db | 2020-01-22 14:58:15 +0100 | [diff] [blame] | 442 | {LINUX_EFI_RANDOM_SEED_TABLE_GUID, "RNG", &rng_seed}, |
Thiebaud Weksteen | 33b6d03 | 2017-09-20 10:13:39 +0200 | [diff] [blame] | 443 | {LINUX_EFI_TPM_EVENT_LOG_GUID, "TPMEventLog", &efi.tpm_log}, |
Matthew Garrett | c46f340 | 2019-05-20 13:54:59 -0700 | [diff] [blame] | 444 | {LINUX_EFI_TPM_FINAL_LOG_GUID, "TPMFinalLog", &efi.tpm_final_log}, |
Ard Biesheuvel | b7846e6 | 2020-01-22 15:06:54 +0100 | [diff] [blame] | 445 | {LINUX_EFI_MEMRESERVE_TABLE_GUID, "MEMRESERVE", &mem_reserve}, |
Narendra K | 1c5fecb | 2019-07-10 18:59:15 +0000 | [diff] [blame] | 446 | #ifdef CONFIG_EFI_RCI2_TABLE |
| 447 | {DELLEMC_EFI_RCI2_TABLE_GUID, NULL, &rci2_table_phys}, |
| 448 | #endif |
Daeseok Youn | 69e6084 | 2014-02-13 17:16:36 +0900 | [diff] [blame] | 449 | {NULL_GUID, NULL, NULL}, |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 450 | }; |
| 451 | |
Ard Biesheuvel | 06c0bd9 | 2020-01-22 14:40:57 +0100 | [diff] [blame] | 452 | static __init int match_config_table(const efi_guid_t *guid, |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 453 | unsigned long table, |
Ard Biesheuvel | 06c0bd9 | 2020-01-22 14:40:57 +0100 | [diff] [blame] | 454 | const efi_config_table_type_t *table_types) |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 455 | { |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 456 | int i; |
| 457 | |
| 458 | if (table_types) { |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 459 | for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) { |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 460 | if (!efi_guidcmp(*guid, table_types[i].guid)) { |
| 461 | *(table_types[i].ptr) = table; |
Ard Biesheuvel | 801820b | 2016-04-25 21:06:53 +0100 | [diff] [blame] | 462 | if (table_types[i].name) |
| 463 | pr_cont(" %s=0x%lx ", |
| 464 | table_types[i].name, table); |
Leif Lindholm | 272686b | 2013-09-05 11:34:54 +0100 | [diff] [blame] | 465 | return 1; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Ard Biesheuvel | 06c0bd9 | 2020-01-22 14:40:57 +0100 | [diff] [blame] | 473 | int __init efi_config_parse_tables(const efi_config_table_t *config_tables, |
| 474 | int count, |
| 475 | const efi_config_table_type_t *arch_tables) |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 476 | { |
Ard Biesheuvel | 06c0bd9 | 2020-01-22 14:40:57 +0100 | [diff] [blame] | 477 | const efi_config_table_64_t *tbl64 = (void *)config_tables; |
| 478 | const efi_config_table_32_t *tbl32 = (void *)config_tables; |
| 479 | const efi_guid_t *guid; |
| 480 | unsigned long table; |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 481 | int i; |
| 482 | |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 483 | pr_info(""); |
| 484 | for (i = 0; i < count; i++) { |
Ard Biesheuvel | 06c0bd9 | 2020-01-22 14:40:57 +0100 | [diff] [blame] | 485 | if (!IS_ENABLED(CONFIG_X86)) { |
| 486 | guid = &config_tables[i].guid; |
| 487 | table = (unsigned long)config_tables[i].table; |
| 488 | } else if (efi_enabled(EFI_64BIT)) { |
| 489 | guid = &tbl64[i].guid; |
| 490 | table = tbl64[i].table; |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 491 | |
Ard Biesheuvel | 06c0bd9 | 2020-01-22 14:40:57 +0100 | [diff] [blame] | 492 | if (IS_ENABLED(CONFIG_X86_32) && |
| 493 | tbl64[i].table > U32_MAX) { |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 494 | pr_cont("\n"); |
| 495 | pr_err("Table located above 4GB, disabling EFI.\n"); |
| 496 | return -EINVAL; |
| 497 | } |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 498 | } else { |
Ard Biesheuvel | 06c0bd9 | 2020-01-22 14:40:57 +0100 | [diff] [blame] | 499 | guid = &tbl32[i].guid; |
| 500 | table = tbl32[i].table; |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 501 | } |
| 502 | |
Ard Biesheuvel | 06c0bd9 | 2020-01-22 14:40:57 +0100 | [diff] [blame] | 503 | if (!match_config_table(guid, table, common_tables)) |
| 504 | match_config_table(guid, table, arch_tables); |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 505 | } |
| 506 | pr_cont("\n"); |
| 507 | set_bit(EFI_CONFIG_TABLES, &efi.flags); |
Ard Biesheuvel | a104171 | 2015-09-23 07:29:34 -0700 | [diff] [blame] | 508 | |
Ard Biesheuvel | 5d288db | 2020-01-22 14:58:15 +0100 | [diff] [blame] | 509 | if (rng_seed != EFI_INVALID_TABLE_ADDR) { |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 510 | struct linux_efi_random_seed *seed; |
| 511 | u32 size = 0; |
| 512 | |
Ard Biesheuvel | 5d288db | 2020-01-22 14:58:15 +0100 | [diff] [blame] | 513 | seed = early_memremap(rng_seed, sizeof(*seed)); |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 514 | if (seed != NULL) { |
| 515 | size = seed->size; |
| 516 | early_memunmap(seed, sizeof(*seed)); |
| 517 | } else { |
| 518 | pr_err("Could not map UEFI random seed!\n"); |
| 519 | } |
| 520 | if (size > 0) { |
Ard Biesheuvel | 5d288db | 2020-01-22 14:58:15 +0100 | [diff] [blame] | 521 | seed = early_memremap(rng_seed, sizeof(*seed) + size); |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 522 | if (seed != NULL) { |
Ard Biesheuvel | 5b4e4c3 | 2018-03-08 08:00:18 +0000 | [diff] [blame] | 523 | pr_notice("seeding entropy pool\n"); |
Dominik Brodowski | 18b915a | 2019-10-29 18:37:52 +0100 | [diff] [blame] | 524 | add_bootloader_randomness(seed->bits, seed->size); |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 525 | early_memunmap(seed, sizeof(*seed) + size); |
| 526 | } else { |
| 527 | pr_err("Could not map UEFI random seed!\n"); |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Daniel Kiper | 457ea3f | 2017-06-22 12:51:36 +0200 | [diff] [blame] | 532 | if (efi_enabled(EFI_MEMMAP)) |
| 533 | efi_memattr_init(); |
Sai Praneeth | 3a6b6c6 | 2017-01-31 13:21:35 +0000 | [diff] [blame] | 534 | |
Thiebaud Weksteen | 33b6d03 | 2017-09-20 10:13:39 +0200 | [diff] [blame] | 535 | efi_tpm_eventlog_init(); |
| 536 | |
Ard Biesheuvel | b7846e6 | 2020-01-22 15:06:54 +0100 | [diff] [blame] | 537 | if (mem_reserve != EFI_INVALID_TABLE_ADDR) { |
| 538 | unsigned long prsv = mem_reserve; |
Ard Biesheuvel | 71e0940 | 2018-09-21 09:32:44 -0700 | [diff] [blame] | 539 | |
| 540 | while (prsv) { |
| 541 | struct linux_efi_memreserve *rsv; |
Ard Biesheuvel | 5f0b0ec | 2018-11-29 18:12:28 +0100 | [diff] [blame] | 542 | u8 *p; |
| 543 | int i; |
Ard Biesheuvel | 71e0940 | 2018-09-21 09:32:44 -0700 | [diff] [blame] | 544 | |
Ard Biesheuvel | 5f0b0ec | 2018-11-29 18:12:28 +0100 | [diff] [blame] | 545 | /* |
| 546 | * Just map a full page: that is what we will get |
| 547 | * anyway, and it permits us to map the entire entry |
| 548 | * before knowing its size. |
| 549 | */ |
| 550 | p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE), |
| 551 | PAGE_SIZE); |
| 552 | if (p == NULL) { |
Ard Biesheuvel | 71e0940 | 2018-09-21 09:32:44 -0700 | [diff] [blame] | 553 | pr_err("Could not map UEFI memreserve entry!\n"); |
| 554 | return -ENOMEM; |
| 555 | } |
| 556 | |
Ard Biesheuvel | 5f0b0ec | 2018-11-29 18:12:28 +0100 | [diff] [blame] | 557 | rsv = (void *)(p + prsv % PAGE_SIZE); |
| 558 | |
| 559 | /* reserve the entry itself */ |
| 560 | memblock_reserve(prsv, EFI_MEMRESERVE_SIZE(rsv->size)); |
| 561 | |
| 562 | for (i = 0; i < atomic_read(&rsv->count); i++) { |
| 563 | memblock_reserve(rsv->entry[i].base, |
| 564 | rsv->entry[i].size); |
| 565 | } |
Ard Biesheuvel | 71e0940 | 2018-09-21 09:32:44 -0700 | [diff] [blame] | 566 | |
| 567 | prsv = rsv->next; |
Ard Biesheuvel | 5f0b0ec | 2018-11-29 18:12:28 +0100 | [diff] [blame] | 568 | early_memunmap(p, PAGE_SIZE); |
Ard Biesheuvel | 71e0940 | 2018-09-21 09:32:44 -0700 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Ard Biesheuvel | 7bb6841 | 2014-10-18 15:04:15 +0200 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | |
Ard Biesheuvel | 14fb420 | 2020-01-20 10:49:11 +0100 | [diff] [blame] | 575 | int __init efi_systab_check_header(const efi_table_hdr_t *systab_hdr, |
| 576 | int min_major_version) |
| 577 | { |
| 578 | if (systab_hdr->signature != EFI_SYSTEM_TABLE_SIGNATURE) { |
| 579 | pr_err("System table signature incorrect!\n"); |
| 580 | return -EINVAL; |
| 581 | } |
| 582 | |
| 583 | if ((systab_hdr->revision >> 16) < min_major_version) |
| 584 | pr_err("Warning: System table version %d.%02d, expected %d.00 or greater!\n", |
| 585 | systab_hdr->revision >> 16, |
| 586 | systab_hdr->revision & 0xffff, |
| 587 | min_major_version); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | #ifndef CONFIG_IA64 |
| 593 | static const efi_char16_t *__init map_fw_vendor(unsigned long fw_vendor, |
| 594 | size_t size) |
| 595 | { |
| 596 | const efi_char16_t *ret; |
| 597 | |
| 598 | ret = early_memremap_ro(fw_vendor, size); |
| 599 | if (!ret) |
| 600 | pr_err("Could not map the firmware vendor!\n"); |
| 601 | return ret; |
| 602 | } |
| 603 | |
| 604 | static void __init unmap_fw_vendor(const void *fw_vendor, size_t size) |
| 605 | { |
| 606 | early_memunmap((void *)fw_vendor, size); |
| 607 | } |
| 608 | #else |
| 609 | #define map_fw_vendor(p, s) __va(p) |
| 610 | #define unmap_fw_vendor(v, s) |
| 611 | #endif |
| 612 | |
| 613 | void __init efi_systab_report_header(const efi_table_hdr_t *systab_hdr, |
| 614 | unsigned long fw_vendor) |
| 615 | { |
| 616 | char vendor[100] = "unknown"; |
| 617 | const efi_char16_t *c16; |
| 618 | size_t i; |
| 619 | |
| 620 | c16 = map_fw_vendor(fw_vendor, sizeof(vendor) * sizeof(efi_char16_t)); |
| 621 | if (c16) { |
| 622 | for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i) |
| 623 | vendor[i] = c16[i]; |
| 624 | vendor[i] = '\0'; |
| 625 | |
| 626 | unmap_fw_vendor(c16, sizeof(vendor) * sizeof(efi_char16_t)); |
| 627 | } |
| 628 | |
| 629 | pr_info("EFI v%u.%.02u by %s\n", |
| 630 | systab_hdr->revision >> 16, |
| 631 | systab_hdr->revision & 0xffff, |
| 632 | vendor); |
| 633 | } |
| 634 | |
Lee, Chun-Yi | 28d5402 | 2014-07-09 18:39:29 +0800 | [diff] [blame] | 635 | #ifdef CONFIG_EFI_VARS_MODULE |
| 636 | static int __init efi_load_efivars(void) |
| 637 | { |
| 638 | struct platform_device *pdev; |
| 639 | |
| 640 | if (!efi_enabled(EFI_RUNTIME_SERVICES)) |
| 641 | return 0; |
| 642 | |
| 643 | pdev = platform_device_register_simple("efivars", 0, NULL, 0); |
Vasyl Gomonovych | 50342b2 | 2018-01-02 18:10:40 +0000 | [diff] [blame] | 644 | return PTR_ERR_OR_ZERO(pdev); |
Lee, Chun-Yi | 28d5402 | 2014-07-09 18:39:29 +0800 | [diff] [blame] | 645 | } |
| 646 | device_initcall(efi_load_efivars); |
| 647 | #endif |
| 648 | |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 649 | #ifdef CONFIG_EFI_PARAMS_FROM_FDT |
| 650 | |
| 651 | #define UEFI_PARAM(name, prop, field) \ |
| 652 | { \ |
| 653 | { name }, \ |
| 654 | { prop }, \ |
| 655 | offsetof(struct efi_fdt_params, field), \ |
Pankaj Bharadiya | c593642 | 2019-12-09 10:31:43 -0800 | [diff] [blame] | 656 | sizeof_field(struct efi_fdt_params, field) \ |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 657 | } |
| 658 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 659 | struct params { |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 660 | const char name[32]; |
| 661 | const char propname[32]; |
| 662 | int offset; |
| 663 | int size; |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 664 | }; |
| 665 | |
| 666 | static __initdata struct params fdt_params[] = { |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 667 | UEFI_PARAM("System Table", "linux,uefi-system-table", system_table), |
| 668 | UEFI_PARAM("MemMap Address", "linux,uefi-mmap-start", mmap), |
| 669 | UEFI_PARAM("MemMap Size", "linux,uefi-mmap-size", mmap_size), |
| 670 | UEFI_PARAM("MemMap Desc. Size", "linux,uefi-mmap-desc-size", desc_size), |
| 671 | UEFI_PARAM("MemMap Desc. Version", "linux,uefi-mmap-desc-ver", desc_ver) |
| 672 | }; |
| 673 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 674 | static __initdata struct params xen_fdt_params[] = { |
| 675 | UEFI_PARAM("System Table", "xen,uefi-system-table", system_table), |
| 676 | UEFI_PARAM("MemMap Address", "xen,uefi-mmap-start", mmap), |
| 677 | UEFI_PARAM("MemMap Size", "xen,uefi-mmap-size", mmap_size), |
| 678 | UEFI_PARAM("MemMap Desc. Size", "xen,uefi-mmap-desc-size", desc_size), |
| 679 | UEFI_PARAM("MemMap Desc. Version", "xen,uefi-mmap-desc-ver", desc_ver) |
| 680 | }; |
| 681 | |
| 682 | #define EFI_FDT_PARAMS_SIZE ARRAY_SIZE(fdt_params) |
| 683 | |
| 684 | static __initdata struct { |
| 685 | const char *uname; |
| 686 | const char *subnode; |
| 687 | struct params *params; |
| 688 | } dt_params[] = { |
| 689 | { "hypervisor", "uefi", xen_fdt_params }, |
| 690 | { "chosen", NULL, fdt_params }, |
| 691 | }; |
| 692 | |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 693 | struct param_info { |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 694 | int found; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 695 | void *params; |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 696 | const char *missing; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 697 | }; |
| 698 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 699 | static int __init __find_uefi_params(unsigned long node, |
| 700 | struct param_info *info, |
| 701 | struct params *params) |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 702 | { |
Catalin Marinas | 6fb8cc8 | 2014-06-02 11:31:06 +0100 | [diff] [blame] | 703 | const void *prop; |
| 704 | void *dest; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 705 | u64 val; |
Catalin Marinas | 6fb8cc8 | 2014-06-02 11:31:06 +0100 | [diff] [blame] | 706 | int i, len; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 707 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 708 | for (i = 0; i < EFI_FDT_PARAMS_SIZE; i++) { |
| 709 | prop = of_get_flat_dt_prop(node, params[i].propname, &len); |
| 710 | if (!prop) { |
| 711 | info->missing = params[i].name; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 712 | return 0; |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | dest = info->params + params[i].offset; |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 716 | info->found++; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 717 | |
| 718 | val = of_read_number(prop, len / sizeof(u32)); |
| 719 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 720 | if (params[i].size == sizeof(u32)) |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 721 | *(u32 *)dest = val; |
| 722 | else |
| 723 | *(u64 *)dest = val; |
| 724 | |
Leif Lindholm | 7968c0e | 2015-08-26 14:24:58 +0100 | [diff] [blame] | 725 | if (efi_enabled(EFI_DBG)) |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 726 | pr_info(" %s: 0x%0*llx\n", params[i].name, |
| 727 | params[i].size * 2, val); |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 728 | } |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 729 | |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 730 | return 1; |
| 731 | } |
| 732 | |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 733 | static int __init fdt_find_uefi_params(unsigned long node, const char *uname, |
| 734 | int depth, void *data) |
| 735 | { |
| 736 | struct param_info *info = data; |
| 737 | int i; |
| 738 | |
| 739 | for (i = 0; i < ARRAY_SIZE(dt_params); i++) { |
| 740 | const char *subnode = dt_params[i].subnode; |
| 741 | |
| 742 | if (depth != 1 || strcmp(uname, dt_params[i].uname) != 0) { |
| 743 | info->missing = dt_params[i].params[0].name; |
| 744 | continue; |
| 745 | } |
| 746 | |
| 747 | if (subnode) { |
Andrzej Hajda | 4af9ed5 | 2016-08-30 12:41:37 +0200 | [diff] [blame] | 748 | int err = of_get_flat_dt_subnode_by_name(node, subnode); |
| 749 | |
| 750 | if (err < 0) |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 751 | return 0; |
Andrzej Hajda | 4af9ed5 | 2016-08-30 12:41:37 +0200 | [diff] [blame] | 752 | |
| 753 | node = err; |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | return __find_uefi_params(node, info, dt_params[i].params); |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
Leif Lindholm | 7968c0e | 2015-08-26 14:24:58 +0100 | [diff] [blame] | 762 | int __init efi_get_fdt_params(struct efi_fdt_params *params) |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 763 | { |
| 764 | struct param_info info; |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 765 | int ret; |
| 766 | |
| 767 | pr_info("Getting EFI parameters from FDT:\n"); |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 768 | |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 769 | info.found = 0; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 770 | info.params = params; |
| 771 | |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 772 | ret = of_scan_flat_dt(fdt_find_uefi_params, &info); |
| 773 | if (!info.found) |
| 774 | pr_info("UEFI not found.\n"); |
| 775 | else if (!ret) |
| 776 | pr_err("Can't find '%s' in device tree!\n", |
Shannon Zhao | 0cac5c3 | 2016-05-12 20:19:54 +0800 | [diff] [blame] | 777 | info.missing); |
Catalin Marinas | 29e2435 | 2014-07-08 16:54:18 +0100 | [diff] [blame] | 778 | |
| 779 | return ret; |
Mark Salter | 0302f71 | 2013-12-30 12:12:12 -0500 | [diff] [blame] | 780 | } |
| 781 | #endif /* CONFIG_EFI_PARAMS_FROM_FDT */ |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 782 | |
| 783 | static __initdata char memory_type_name[][20] = { |
| 784 | "Reserved", |
| 785 | "Loader Code", |
| 786 | "Loader Data", |
| 787 | "Boot Code", |
| 788 | "Boot Data", |
| 789 | "Runtime Code", |
| 790 | "Runtime Data", |
| 791 | "Conventional Memory", |
| 792 | "Unusable Memory", |
| 793 | "ACPI Reclaim Memory", |
| 794 | "ACPI Memory NVS", |
| 795 | "Memory Mapped I/O", |
| 796 | "MMIO Port Space", |
Robert Elliott | 35575e0 | 2016-02-01 22:07:07 +0000 | [diff] [blame] | 797 | "PAL Code", |
| 798 | "Persistent Memory", |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 799 | }; |
| 800 | |
| 801 | char * __init efi_md_typeattr_format(char *buf, size_t size, |
| 802 | const efi_memory_desc_t *md) |
| 803 | { |
| 804 | char *pos; |
| 805 | int type_len; |
| 806 | u64 attr; |
| 807 | |
| 808 | pos = buf; |
| 809 | if (md->type >= ARRAY_SIZE(memory_type_name)) |
| 810 | type_len = snprintf(pos, size, "[type=%u", md->type); |
| 811 | else |
| 812 | type_len = snprintf(pos, size, "[%-*s", |
| 813 | (int)(sizeof(memory_type_name[0]) - 1), |
| 814 | memory_type_name[md->type]); |
| 815 | if (type_len >= size) |
| 816 | return buf; |
| 817 | |
| 818 | pos += type_len; |
| 819 | size -= type_len; |
| 820 | |
| 821 | attr = md->attribute; |
| 822 | if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT | |
Ard Biesheuvel | 87db73ae | 2015-08-07 09:36:54 +0100 | [diff] [blame] | 823 | EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO | |
| 824 | EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP | |
Dan Williams | fe3e5e6 | 2019-11-06 17:43:00 -0800 | [diff] [blame] | 825 | EFI_MEMORY_NV | EFI_MEMORY_SP | |
Taku Izumi | 8be4432 | 2015-08-27 02:11:19 +0900 | [diff] [blame] | 826 | EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE)) |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 827 | snprintf(pos, size, "|attr=0x%016llx]", |
| 828 | (unsigned long long)attr); |
| 829 | else |
Robert Elliott | c016ca0 | 2016-02-01 22:07:06 +0000 | [diff] [blame] | 830 | snprintf(pos, size, |
Dan Williams | fe3e5e6 | 2019-11-06 17:43:00 -0800 | [diff] [blame] | 831 | "|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]", |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 832 | attr & EFI_MEMORY_RUNTIME ? "RUN" : "", |
Taku Izumi | 8be4432 | 2015-08-27 02:11:19 +0900 | [diff] [blame] | 833 | attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "", |
Dan Williams | fe3e5e6 | 2019-11-06 17:43:00 -0800 | [diff] [blame] | 834 | attr & EFI_MEMORY_SP ? "SP" : "", |
Robert Elliott | c016ca0 | 2016-02-01 22:07:06 +0000 | [diff] [blame] | 835 | attr & EFI_MEMORY_NV ? "NV" : "", |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 836 | attr & EFI_MEMORY_XP ? "XP" : "", |
| 837 | attr & EFI_MEMORY_RP ? "RP" : "", |
| 838 | attr & EFI_MEMORY_WP ? "WP" : "", |
Ard Biesheuvel | 87db73ae | 2015-08-07 09:36:54 +0100 | [diff] [blame] | 839 | attr & EFI_MEMORY_RO ? "RO" : "", |
Laszlo Ersek | 98d2a6c | 2014-09-03 13:32:20 +0200 | [diff] [blame] | 840 | attr & EFI_MEMORY_UCE ? "UCE" : "", |
| 841 | attr & EFI_MEMORY_WB ? "WB" : "", |
| 842 | attr & EFI_MEMORY_WT ? "WT" : "", |
| 843 | attr & EFI_MEMORY_WC ? "WC" : "", |
| 844 | attr & EFI_MEMORY_UC ? "UC" : ""); |
| 845 | return buf; |
| 846 | } |
Jonathan (Zhixiong) Zhang | 7bf7931 | 2015-08-07 09:36:57 +0100 | [diff] [blame] | 847 | |
| 848 | /* |
Jan Beulich | 23f0571 | 2017-08-25 16:50:18 +0100 | [diff] [blame] | 849 | * IA64 has a funky EFI memory map that doesn't work the same way as |
| 850 | * other architectures. |
| 851 | */ |
| 852 | #ifndef CONFIG_IA64 |
| 853 | /* |
Jonathan (Zhixiong) Zhang | 7bf7931 | 2015-08-07 09:36:57 +0100 | [diff] [blame] | 854 | * efi_mem_attributes - lookup memmap attributes for physical address |
| 855 | * @phys_addr: the physical address to lookup |
| 856 | * |
| 857 | * Search in the EFI memory map for the region covering |
| 858 | * @phys_addr. Returns the EFI memory attributes if the region |
| 859 | * was found in the memory map, 0 otherwise. |
Jonathan (Zhixiong) Zhang | 7bf7931 | 2015-08-07 09:36:57 +0100 | [diff] [blame] | 860 | */ |
Jan Beulich | 23f0571 | 2017-08-25 16:50:18 +0100 | [diff] [blame] | 861 | u64 efi_mem_attributes(unsigned long phys_addr) |
Jonathan (Zhixiong) Zhang | 7bf7931 | 2015-08-07 09:36:57 +0100 | [diff] [blame] | 862 | { |
| 863 | efi_memory_desc_t *md; |
Jonathan (Zhixiong) Zhang | 7bf7931 | 2015-08-07 09:36:57 +0100 | [diff] [blame] | 864 | |
| 865 | if (!efi_enabled(EFI_MEMMAP)) |
| 866 | return 0; |
| 867 | |
Matt Fleming | 78ce248 | 2016-04-25 21:06:38 +0100 | [diff] [blame] | 868 | for_each_efi_memory_desc(md) { |
Jonathan (Zhixiong) Zhang | 7bf7931 | 2015-08-07 09:36:57 +0100 | [diff] [blame] | 869 | if ((md->phys_addr <= phys_addr) && |
| 870 | (phys_addr < (md->phys_addr + |
| 871 | (md->num_pages << EFI_PAGE_SHIFT)))) |
| 872 | return md->attribute; |
| 873 | } |
| 874 | return 0; |
| 875 | } |
Matt Fleming | 806b035 | 2016-04-25 21:06:58 +0100 | [diff] [blame] | 876 | |
Jan Beulich | 23f0571 | 2017-08-25 16:50:18 +0100 | [diff] [blame] | 877 | /* |
| 878 | * efi_mem_type - lookup memmap type for physical address |
| 879 | * @phys_addr: the physical address to lookup |
| 880 | * |
| 881 | * Search in the EFI memory map for the region covering @phys_addr. |
| 882 | * Returns the EFI memory type if the region was found in the memory |
Anshuman Khandual | 62b605b | 2020-01-13 18:22:41 +0100 | [diff] [blame] | 883 | * map, -EINVAL otherwise. |
Jan Beulich | 23f0571 | 2017-08-25 16:50:18 +0100 | [diff] [blame] | 884 | */ |
| 885 | int efi_mem_type(unsigned long phys_addr) |
| 886 | { |
| 887 | const efi_memory_desc_t *md; |
| 888 | |
| 889 | if (!efi_enabled(EFI_MEMMAP)) |
| 890 | return -ENOTSUPP; |
| 891 | |
| 892 | for_each_efi_memory_desc(md) { |
| 893 | if ((md->phys_addr <= phys_addr) && |
| 894 | (phys_addr < (md->phys_addr + |
| 895 | (md->num_pages << EFI_PAGE_SHIFT)))) |
| 896 | return md->type; |
| 897 | } |
| 898 | return -EINVAL; |
| 899 | } |
| 900 | #endif |
| 901 | |
Matt Fleming | 806b035 | 2016-04-25 21:06:58 +0100 | [diff] [blame] | 902 | int efi_status_to_err(efi_status_t status) |
| 903 | { |
| 904 | int err; |
| 905 | |
| 906 | switch (status) { |
| 907 | case EFI_SUCCESS: |
| 908 | err = 0; |
| 909 | break; |
| 910 | case EFI_INVALID_PARAMETER: |
| 911 | err = -EINVAL; |
| 912 | break; |
| 913 | case EFI_OUT_OF_RESOURCES: |
| 914 | err = -ENOSPC; |
| 915 | break; |
| 916 | case EFI_DEVICE_ERROR: |
| 917 | err = -EIO; |
| 918 | break; |
| 919 | case EFI_WRITE_PROTECTED: |
| 920 | err = -EROFS; |
| 921 | break; |
| 922 | case EFI_SECURITY_VIOLATION: |
| 923 | err = -EACCES; |
| 924 | break; |
| 925 | case EFI_NOT_FOUND: |
| 926 | err = -ENOENT; |
| 927 | break; |
Ard Biesheuvel | dce48e3 | 2016-07-15 21:36:31 +0200 | [diff] [blame] | 928 | case EFI_ABORTED: |
| 929 | err = -EINTR; |
| 930 | break; |
Matt Fleming | 806b035 | 2016-04-25 21:06:58 +0100 | [diff] [blame] | 931 | default: |
| 932 | err = -EINVAL; |
| 933 | } |
| 934 | |
| 935 | return err; |
| 936 | } |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 937 | |
Ard Biesheuvel | a23d3bb | 2018-09-21 09:32:46 -0700 | [diff] [blame] | 938 | static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock); |
Ard Biesheuvel | 63eb322 | 2018-11-14 09:55:44 -0800 | [diff] [blame] | 939 | static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init; |
Ard Biesheuvel | a23d3bb | 2018-09-21 09:32:46 -0700 | [diff] [blame] | 940 | |
Ard Biesheuvel | 976b489 | 2018-11-23 22:51:32 +0100 | [diff] [blame] | 941 | static int __init efi_memreserve_map_root(void) |
| 942 | { |
Ard Biesheuvel | b7846e6 | 2020-01-22 15:06:54 +0100 | [diff] [blame] | 943 | if (mem_reserve == EFI_INVALID_TABLE_ADDR) |
Ard Biesheuvel | 976b489 | 2018-11-23 22:51:32 +0100 | [diff] [blame] | 944 | return -ENODEV; |
| 945 | |
Ard Biesheuvel | b7846e6 | 2020-01-22 15:06:54 +0100 | [diff] [blame] | 946 | efi_memreserve_root = memremap(mem_reserve, |
Ard Biesheuvel | 976b489 | 2018-11-23 22:51:32 +0100 | [diff] [blame] | 947 | sizeof(*efi_memreserve_root), |
| 948 | MEMREMAP_WB); |
| 949 | if (WARN_ON_ONCE(!efi_memreserve_root)) |
| 950 | return -ENOMEM; |
| 951 | return 0; |
| 952 | } |
| 953 | |
Ard Biesheuvel | ab0eb16 | 2019-12-06 16:55:37 +0000 | [diff] [blame] | 954 | static int efi_mem_reserve_iomem(phys_addr_t addr, u64 size) |
| 955 | { |
| 956 | struct resource *res, *parent; |
| 957 | |
| 958 | res = kzalloc(sizeof(struct resource), GFP_ATOMIC); |
| 959 | if (!res) |
| 960 | return -ENOMEM; |
| 961 | |
| 962 | res->name = "reserved"; |
| 963 | res->flags = IORESOURCE_MEM; |
| 964 | res->start = addr; |
| 965 | res->end = addr + size - 1; |
| 966 | |
| 967 | /* we expect a conflict with a 'System RAM' region */ |
| 968 | parent = request_resource_conflict(&iomem_resource, res); |
| 969 | return parent ? request_resource(parent, res) : 0; |
| 970 | } |
| 971 | |
Ard Biesheuvel | 976b489 | 2018-11-23 22:51:32 +0100 | [diff] [blame] | 972 | int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size) |
Ard Biesheuvel | a23d3bb | 2018-09-21 09:32:46 -0700 | [diff] [blame] | 973 | { |
Ard Biesheuvel | 63eb322 | 2018-11-14 09:55:44 -0800 | [diff] [blame] | 974 | struct linux_efi_memreserve *rsv; |
Ard Biesheuvel | 80424b0 | 2018-11-29 18:12:29 +0100 | [diff] [blame] | 975 | unsigned long prsv; |
| 976 | int rc, index; |
Ard Biesheuvel | a23d3bb | 2018-09-21 09:32:46 -0700 | [diff] [blame] | 977 | |
Ard Biesheuvel | 976b489 | 2018-11-23 22:51:32 +0100 | [diff] [blame] | 978 | if (efi_memreserve_root == (void *)ULONG_MAX) |
Ard Biesheuvel | a23d3bb | 2018-09-21 09:32:46 -0700 | [diff] [blame] | 979 | return -ENODEV; |
| 980 | |
Ard Biesheuvel | 976b489 | 2018-11-23 22:51:32 +0100 | [diff] [blame] | 981 | if (!efi_memreserve_root) { |
| 982 | rc = efi_memreserve_map_root(); |
| 983 | if (rc) |
| 984 | return rc; |
| 985 | } |
| 986 | |
Ard Biesheuvel | 80424b0 | 2018-11-29 18:12:29 +0100 | [diff] [blame] | 987 | /* first try to find a slot in an existing linked list entry */ |
| 988 | for (prsv = efi_memreserve_root->next; prsv; prsv = rsv->next) { |
Ard Biesheuvel | 18df757 | 2019-06-09 20:17:44 +0200 | [diff] [blame] | 989 | rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB); |
Ard Biesheuvel | 80424b0 | 2018-11-29 18:12:29 +0100 | [diff] [blame] | 990 | index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size); |
| 991 | if (index < rsv->size) { |
| 992 | rsv->entry[index].base = addr; |
| 993 | rsv->entry[index].size = size; |
| 994 | |
Ard Biesheuvel | 18df757 | 2019-06-09 20:17:44 +0200 | [diff] [blame] | 995 | memunmap(rsv); |
Ard Biesheuvel | ab0eb16 | 2019-12-06 16:55:37 +0000 | [diff] [blame] | 996 | return efi_mem_reserve_iomem(addr, size); |
Ard Biesheuvel | 80424b0 | 2018-11-29 18:12:29 +0100 | [diff] [blame] | 997 | } |
Ard Biesheuvel | 18df757 | 2019-06-09 20:17:44 +0200 | [diff] [blame] | 998 | memunmap(rsv); |
Ard Biesheuvel | 80424b0 | 2018-11-29 18:12:29 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* no slot found - allocate a new linked list entry */ |
| 1002 | rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC); |
Ard Biesheuvel | a23d3bb | 2018-09-21 09:32:46 -0700 | [diff] [blame] | 1003 | if (!rsv) |
| 1004 | return -ENOMEM; |
| 1005 | |
Ard Biesheuvel | ab0eb16 | 2019-12-06 16:55:37 +0000 | [diff] [blame] | 1006 | rc = efi_mem_reserve_iomem(__pa(rsv), SZ_4K); |
| 1007 | if (rc) { |
| 1008 | free_page((unsigned long)rsv); |
| 1009 | return rc; |
| 1010 | } |
| 1011 | |
Ard Biesheuvel | 18df757 | 2019-06-09 20:17:44 +0200 | [diff] [blame] | 1012 | /* |
| 1013 | * The memremap() call above assumes that a linux_efi_memreserve entry |
| 1014 | * never crosses a page boundary, so let's ensure that this remains true |
| 1015 | * even when kexec'ing a 4k pages kernel from a >4k pages kernel, by |
| 1016 | * using SZ_4K explicitly in the size calculation below. |
| 1017 | */ |
| 1018 | rsv->size = EFI_MEMRESERVE_COUNT(SZ_4K); |
Ard Biesheuvel | 5f0b0ec | 2018-11-29 18:12:28 +0100 | [diff] [blame] | 1019 | atomic_set(&rsv->count, 1); |
| 1020 | rsv->entry[0].base = addr; |
| 1021 | rsv->entry[0].size = size; |
Ard Biesheuvel | a23d3bb | 2018-09-21 09:32:46 -0700 | [diff] [blame] | 1022 | |
| 1023 | spin_lock(&efi_mem_reserve_persistent_lock); |
Ard Biesheuvel | 63eb322 | 2018-11-14 09:55:44 -0800 | [diff] [blame] | 1024 | rsv->next = efi_memreserve_root->next; |
| 1025 | efi_memreserve_root->next = __pa(rsv); |
Ard Biesheuvel | a23d3bb | 2018-09-21 09:32:46 -0700 | [diff] [blame] | 1026 | spin_unlock(&efi_mem_reserve_persistent_lock); |
| 1027 | |
Ard Biesheuvel | ab0eb16 | 2019-12-06 16:55:37 +0000 | [diff] [blame] | 1028 | return efi_mem_reserve_iomem(addr, size); |
Ard Biesheuvel | a23d3bb | 2018-09-21 09:32:46 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Ard Biesheuvel | 63eb322 | 2018-11-14 09:55:44 -0800 | [diff] [blame] | 1031 | static int __init efi_memreserve_root_init(void) |
| 1032 | { |
Ard Biesheuvel | 976b489 | 2018-11-23 22:51:32 +0100 | [diff] [blame] | 1033 | if (efi_memreserve_root) |
| 1034 | return 0; |
| 1035 | if (efi_memreserve_map_root()) |
| 1036 | efi_memreserve_root = (void *)ULONG_MAX; |
Ard Biesheuvel | 63eb322 | 2018-11-14 09:55:44 -0800 | [diff] [blame] | 1037 | return 0; |
| 1038 | } |
| 1039 | early_initcall(efi_memreserve_root_init); |
| 1040 | |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 1041 | #ifdef CONFIG_KEXEC |
| 1042 | static int update_efi_random_seed(struct notifier_block *nb, |
| 1043 | unsigned long code, void *unused) |
| 1044 | { |
| 1045 | struct linux_efi_random_seed *seed; |
| 1046 | u32 size = 0; |
| 1047 | |
| 1048 | if (!kexec_in_progress) |
| 1049 | return NOTIFY_DONE; |
| 1050 | |
Ard Biesheuvel | 5d288db | 2020-01-22 14:58:15 +0100 | [diff] [blame] | 1051 | seed = memremap(rng_seed, sizeof(*seed), MEMREMAP_WB); |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 1052 | if (seed != NULL) { |
Ard Biesheuvel | c2ceb5f | 2017-08-25 16:50:16 +0100 | [diff] [blame] | 1053 | size = min(seed->size, EFI_RANDOM_SEED_SIZE); |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 1054 | memunmap(seed); |
| 1055 | } else { |
| 1056 | pr_err("Could not map UEFI random seed!\n"); |
| 1057 | } |
| 1058 | if (size > 0) { |
Ard Biesheuvel | 5d288db | 2020-01-22 14:58:15 +0100 | [diff] [blame] | 1059 | seed = memremap(rng_seed, sizeof(*seed) + size, MEMREMAP_WB); |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 1060 | if (seed != NULL) { |
| 1061 | seed->size = size; |
| 1062 | get_random_bytes(seed->bits, seed->size); |
| 1063 | memunmap(seed); |
| 1064 | } else { |
| 1065 | pr_err("Could not map UEFI random seed!\n"); |
| 1066 | } |
| 1067 | } |
| 1068 | return NOTIFY_DONE; |
| 1069 | } |
| 1070 | |
| 1071 | static struct notifier_block efi_random_seed_nb = { |
| 1072 | .notifier_call = update_efi_random_seed, |
| 1073 | }; |
| 1074 | |
Ard Biesheuvel | 5d288db | 2020-01-22 14:58:15 +0100 | [diff] [blame] | 1075 | static int __init register_update_efi_random_seed(void) |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 1076 | { |
Ard Biesheuvel | 5d288db | 2020-01-22 14:58:15 +0100 | [diff] [blame] | 1077 | if (rng_seed == EFI_INVALID_TABLE_ADDR) |
Ard Biesheuvel | 6362598 | 2016-11-12 21:32:31 +0000 | [diff] [blame] | 1078 | return 0; |
| 1079 | return register_reboot_notifier(&efi_random_seed_nb); |
| 1080 | } |
| 1081 | late_initcall(register_update_efi_random_seed); |
| 1082 | #endif |