| 1 | // SPDX-License-Identifier: GPL-2.0-only | 
|---|
| 2 | #define pr_fmt(fmt) "efi: " fmt | 
|---|
| 3 |  | 
|---|
| 4 | #include <linux/init.h> | 
|---|
| 5 | #include <linux/kernel.h> | 
|---|
| 6 | #include <linux/string.h> | 
|---|
| 7 | #include <linux/time.h> | 
|---|
| 8 | #include <linux/types.h> | 
|---|
| 9 | #include <linux/efi.h> | 
|---|
| 10 | #include <linux/slab.h> | 
|---|
| 11 | #include <linux/memblock.h> | 
|---|
| 12 | #include <linux/acpi.h> | 
|---|
| 13 | #include <linux/dmi.h> | 
|---|
| 14 |  | 
|---|
| 15 | #include <asm/e820/api.h> | 
|---|
| 16 | #include <asm/efi.h> | 
|---|
| 17 | #include <asm/uv/uv.h> | 
|---|
| 18 | #include <asm/cpu_device_id.h> | 
|---|
| 19 | #include <asm/realmode.h> | 
|---|
| 20 | #include <asm/reboot.h> | 
|---|
| 21 |  | 
|---|
| 22 | #define EFI_MIN_RESERVE 5120 | 
|---|
| 23 |  | 
|---|
| 24 | #define EFI_DUMMY_GUID \ | 
|---|
| 25 | EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9) | 
|---|
| 26 |  | 
|---|
| 27 | #define QUARK_CSH_SIGNATURE		0x5f435348	/* _CSH */ | 
|---|
| 28 | #define 	0x400 | 
|---|
| 29 |  | 
|---|
| 30 | /* | 
|---|
| 31 | * Header prepended to the standard EFI capsule on Quark systems the are based | 
|---|
| 32 | * on Intel firmware BSP. | 
|---|
| 33 | * @csh_signature:	Unique identifier to sanity check signed module | 
|---|
| 34 | * 			presence ("_CSH"). | 
|---|
| 35 | * @version:		Current version of CSH used. Should be one for Quark A0. | 
|---|
| 36 | * @modulesize:		Size of the entire module including the module header | 
|---|
| 37 | * 			and payload. | 
|---|
| 38 | * @security_version_number_index: Index of SVN to use for validation of signed | 
|---|
| 39 | * 			module. | 
|---|
| 40 | * @security_version_number: Used to prevent against roll back of modules. | 
|---|
| 41 | * @rsvd_module_id:	Currently unused for Clanton (Quark). | 
|---|
| 42 | * @rsvd_module_vendor:	Vendor Identifier. For Intel products value is | 
|---|
| 43 | * 			0x00008086. | 
|---|
| 44 | * @rsvd_date:		BCD representation of build date as yyyymmdd, where | 
|---|
| 45 | * 			yyyy=4 digit year, mm=1-12, dd=1-31. | 
|---|
| 46 | * @headersize:		Total length of the header including including any | 
|---|
| 47 | * 			padding optionally added by the signing tool. | 
|---|
| 48 | * @hash_algo:		What Hash is used in the module signing. | 
|---|
| 49 | * @cryp_algo:		What Crypto is used in the module signing. | 
|---|
| 50 | * @keysize:		Total length of the key data including including any | 
|---|
| 51 | * 			padding optionally added by the signing tool. | 
|---|
| 52 | * @signaturesize:	Total length of the signature including including any | 
|---|
| 53 | * 			padding optionally added by the signing tool. | 
|---|
| 54 | * @rsvd_next_header:	32-bit pointer to the next Secure Boot Module in the | 
|---|
| 55 | * 			chain, if there is a next header. | 
|---|
| 56 | * @rsvd:		Reserved, padding structure to required size. | 
|---|
| 57 | * | 
|---|
| 58 | * See also QuartSecurityHeader_t in | 
|---|
| 59 | * Quark_EDKII_v1.2.1.1/QuarkPlatformPkg/Include/QuarkBootRom.h | 
|---|
| 60 | * from https://downloadcenter.intel.com/download/23197/Intel-Quark-SoC-X1000-Board-Support-Package-BSP | 
|---|
| 61 | */ | 
|---|
| 62 | struct  { | 
|---|
| 63 | u32 ; | 
|---|
| 64 | u32 ; | 
|---|
| 65 | u32 ; | 
|---|
| 66 | u32 ; | 
|---|
| 67 | u32 ; | 
|---|
| 68 | u32 ; | 
|---|
| 69 | u32 ; | 
|---|
| 70 | u32 ; | 
|---|
| 71 | u32 ; | 
|---|
| 72 | u32 ; | 
|---|
| 73 | u32 ; | 
|---|
| 74 | u32 ; | 
|---|
| 75 | u32 ; | 
|---|
| 76 | u32 ; | 
|---|
| 77 | u32 [2]; | 
|---|
| 78 | }; | 
|---|
| 79 |  | 
|---|
| 80 | static const efi_char16_t efi_dummy_name[] = L "DUMMY"; | 
|---|
| 81 |  | 
|---|
| 82 | static bool efi_no_storage_paranoia; | 
|---|
| 83 |  | 
|---|
| 84 | /* | 
|---|
| 85 | * Some firmware implementations refuse to boot if there's insufficient | 
|---|
| 86 | * space in the variable store. The implementation of garbage collection | 
|---|
| 87 | * in some FW versions causes stale (deleted) variables to take up space | 
|---|
| 88 | * longer than intended and space is only freed once the store becomes | 
|---|
| 89 | * almost completely full. | 
|---|
| 90 | * | 
|---|
| 91 | * Enabling this option disables the space checks in | 
|---|
| 92 | * efi_query_variable_store() and forces garbage collection. | 
|---|
| 93 | * | 
|---|
| 94 | * Only enable this option if deleting EFI variables does not free up | 
|---|
| 95 | * space in your variable store, e.g. if despite deleting variables | 
|---|
| 96 | * you're unable to create new ones. | 
|---|
| 97 | */ | 
|---|
| 98 | static int __init setup_storage_paranoia(char *arg) | 
|---|
| 99 | { | 
|---|
| 100 | efi_no_storage_paranoia = true; | 
|---|
| 101 | return 0; | 
|---|
| 102 | } | 
|---|
| 103 | early_param( "efi_no_storage_paranoia", setup_storage_paranoia); | 
|---|
| 104 |  | 
|---|
| 105 | /* | 
|---|
| 106 | * Deleting the dummy variable which kicks off garbage collection | 
|---|
| 107 | */ | 
|---|
| 108 | void efi_delete_dummy_variable(void) | 
|---|
| 109 | { | 
|---|
| 110 | efi.set_variable_nonblocking((efi_char16_t *)efi_dummy_name, | 
|---|
| 111 | &EFI_DUMMY_GUID, | 
|---|
| 112 | EFI_VARIABLE_NON_VOLATILE | | 
|---|
| 113 | EFI_VARIABLE_BOOTSERVICE_ACCESS | | 
|---|
| 114 | EFI_VARIABLE_RUNTIME_ACCESS, 0, NULL); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | u64 efivar_reserved_space(void) | 
|---|
| 118 | { | 
|---|
| 119 | if (efi_no_storage_paranoia) | 
|---|
| 120 | return 0; | 
|---|
| 121 | return EFI_MIN_RESERVE; | 
|---|
| 122 | } | 
|---|
| 123 | EXPORT_SYMBOL_GPL(efivar_reserved_space); | 
|---|
| 124 |  | 
|---|
| 125 | /* | 
|---|
| 126 | * In the nonblocking case we do not attempt to perform garbage | 
|---|
| 127 | * collection if we do not have enough free space. Rather, we do the | 
|---|
| 128 | * bare minimum check and give up immediately if the available space | 
|---|
| 129 | * is below EFI_MIN_RESERVE. | 
|---|
| 130 | * | 
|---|
| 131 | * This function is intended to be small and simple because it is | 
|---|
| 132 | * invoked from crash handler paths. | 
|---|
| 133 | */ | 
|---|
| 134 | static efi_status_t | 
|---|
| 135 | query_variable_store_nonblocking(u32 attributes, unsigned long size) | 
|---|
| 136 | { | 
|---|
| 137 | efi_status_t status; | 
|---|
| 138 | u64 storage_size, remaining_size, max_size; | 
|---|
| 139 |  | 
|---|
| 140 | status = efi.query_variable_info_nonblocking(attributes, &storage_size, | 
|---|
| 141 | &remaining_size, | 
|---|
| 142 | &max_size); | 
|---|
| 143 | if (status != EFI_SUCCESS) | 
|---|
| 144 | return status; | 
|---|
| 145 |  | 
|---|
| 146 | if (remaining_size - size < EFI_MIN_RESERVE) | 
|---|
| 147 | return EFI_OUT_OF_RESOURCES; | 
|---|
| 148 |  | 
|---|
| 149 | return EFI_SUCCESS; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | /* | 
|---|
| 153 | * Some firmware implementations refuse to boot if there's insufficient space | 
|---|
| 154 | * in the variable store. Ensure that we never use more than a safe limit. | 
|---|
| 155 | * | 
|---|
| 156 | * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable | 
|---|
| 157 | * store. | 
|---|
| 158 | */ | 
|---|
| 159 | efi_status_t efi_query_variable_store(u32 attributes, unsigned long size, | 
|---|
| 160 | bool nonblocking) | 
|---|
| 161 | { | 
|---|
| 162 | efi_status_t status; | 
|---|
| 163 | u64 storage_size, remaining_size, max_size; | 
|---|
| 164 |  | 
|---|
| 165 | if (!(attributes & EFI_VARIABLE_NON_VOLATILE)) | 
|---|
| 166 | return 0; | 
|---|
| 167 |  | 
|---|
| 168 | if (nonblocking) | 
|---|
| 169 | return query_variable_store_nonblocking(attributes, size); | 
|---|
| 170 |  | 
|---|
| 171 | status = efi.query_variable_info(attributes, &storage_size, | 
|---|
| 172 | &remaining_size, &max_size); | 
|---|
| 173 | if (status != EFI_SUCCESS) | 
|---|
| 174 | return status; | 
|---|
| 175 |  | 
|---|
| 176 | /* | 
|---|
| 177 | * We account for that by refusing the write if permitting it would | 
|---|
| 178 | * reduce the available space to under 5KB. This figure was provided by | 
|---|
| 179 | * Samsung, so should be safe. | 
|---|
| 180 | */ | 
|---|
| 181 | if ((remaining_size - size < EFI_MIN_RESERVE) && | 
|---|
| 182 | !efi_no_storage_paranoia) { | 
|---|
| 183 |  | 
|---|
| 184 | /* | 
|---|
| 185 | * Triggering garbage collection may require that the firmware | 
|---|
| 186 | * generate a real EFI_OUT_OF_RESOURCES error. We can force | 
|---|
| 187 | * that by attempting to use more space than is available. | 
|---|
| 188 | */ | 
|---|
| 189 | unsigned long dummy_size = remaining_size + 1024; | 
|---|
| 190 | void *dummy = kzalloc(dummy_size, GFP_KERNEL); | 
|---|
| 191 |  | 
|---|
| 192 | if (!dummy) | 
|---|
| 193 | return EFI_OUT_OF_RESOURCES; | 
|---|
| 194 |  | 
|---|
| 195 | status = efi.set_variable((efi_char16_t *)efi_dummy_name, | 
|---|
| 196 | &EFI_DUMMY_GUID, | 
|---|
| 197 | EFI_VARIABLE_NON_VOLATILE | | 
|---|
| 198 | EFI_VARIABLE_BOOTSERVICE_ACCESS | | 
|---|
| 199 | EFI_VARIABLE_RUNTIME_ACCESS, | 
|---|
| 200 | dummy_size, dummy); | 
|---|
| 201 |  | 
|---|
| 202 | if (status == EFI_SUCCESS) { | 
|---|
| 203 | /* | 
|---|
| 204 | * This should have failed, so if it didn't make sure | 
|---|
| 205 | * that we delete it... | 
|---|
| 206 | */ | 
|---|
| 207 | efi_delete_dummy_variable(); | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | kfree(objp: dummy); | 
|---|
| 211 |  | 
|---|
| 212 | /* | 
|---|
| 213 | * The runtime code may now have triggered a garbage collection | 
|---|
| 214 | * run, so check the variable info again | 
|---|
| 215 | */ | 
|---|
| 216 | status = efi.query_variable_info(attributes, &storage_size, | 
|---|
| 217 | &remaining_size, &max_size); | 
|---|
| 218 |  | 
|---|
| 219 | if (status != EFI_SUCCESS) | 
|---|
| 220 | return status; | 
|---|
| 221 |  | 
|---|
| 222 | /* | 
|---|
| 223 | * There still isn't enough room, so return an error | 
|---|
| 224 | */ | 
|---|
| 225 | if (remaining_size - size < EFI_MIN_RESERVE) | 
|---|
| 226 | return EFI_OUT_OF_RESOURCES; | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 | return EFI_SUCCESS; | 
|---|
| 230 | } | 
|---|
| 231 | EXPORT_SYMBOL_GPL(efi_query_variable_store); | 
|---|
| 232 |  | 
|---|
| 233 | /* | 
|---|
| 234 | * The UEFI specification makes it clear that the operating system is | 
|---|
| 235 | * free to do whatever it wants with boot services code after | 
|---|
| 236 | * ExitBootServices() has been called. Ignoring this recommendation a | 
|---|
| 237 | * significant bunch of EFI implementations continue calling into boot | 
|---|
| 238 | * services code (SetVirtualAddressMap). In order to work around such | 
|---|
| 239 | * buggy implementations we reserve boot services region during EFI | 
|---|
| 240 | * init and make sure it stays executable. Then, after | 
|---|
| 241 | * SetVirtualAddressMap(), it is discarded. | 
|---|
| 242 | * | 
|---|
| 243 | * However, some boot services regions contain data that is required | 
|---|
| 244 | * by drivers, so we need to track which memory ranges can never be | 
|---|
| 245 | * freed. This is done by tagging those regions with the | 
|---|
| 246 | * EFI_MEMORY_RUNTIME attribute. | 
|---|
| 247 | * | 
|---|
| 248 | * Any driver that wants to mark a region as reserved must use | 
|---|
| 249 | * efi_mem_reserve() which will insert a new EFI memory descriptor | 
|---|
| 250 | * into efi.memmap (splitting existing regions if necessary) and tag | 
|---|
| 251 | * it with EFI_MEMORY_RUNTIME. | 
|---|
| 252 | */ | 
|---|
| 253 | void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size) | 
|---|
| 254 | { | 
|---|
| 255 | struct efi_memory_map_data data = { 0 }; | 
|---|
| 256 | struct efi_mem_range mr; | 
|---|
| 257 | efi_memory_desc_t md; | 
|---|
| 258 | int num_entries; | 
|---|
| 259 | void *new; | 
|---|
| 260 |  | 
|---|
| 261 | if (efi_mem_desc_lookup(phys_addr: addr, out_md: &md) || | 
|---|
| 262 | md.type != EFI_BOOT_SERVICES_DATA) { | 
|---|
| 263 | pr_err( "Failed to lookup EFI memory descriptor for %pa\n", &addr); | 
|---|
| 264 | return; | 
|---|
| 265 | } | 
|---|
| 266 |  | 
|---|
| 267 | if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) { | 
|---|
| 268 | pr_err( "Region spans EFI memory descriptors, %pa\n", &addr); | 
|---|
| 269 | return; | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 | size += addr % EFI_PAGE_SIZE; | 
|---|
| 273 | size = round_up(size, EFI_PAGE_SIZE); | 
|---|
| 274 | addr = round_down(addr, EFI_PAGE_SIZE); | 
|---|
| 275 |  | 
|---|
| 276 | mr.range.start = addr; | 
|---|
| 277 | mr.range.end = addr + size - 1; | 
|---|
| 278 | mr.attribute = md.attribute | EFI_MEMORY_RUNTIME; | 
|---|
| 279 |  | 
|---|
| 280 | num_entries = efi_memmap_split_count(md: &md, range: &mr.range); | 
|---|
| 281 | num_entries += efi.memmap.nr_map; | 
|---|
| 282 |  | 
|---|
| 283 | if (efi_memmap_alloc(num_entries, data: &data) != 0) { | 
|---|
| 284 | pr_err( "Could not allocate boot services memmap\n"); | 
|---|
| 285 | return; | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | new = early_memremap_prot(phys_addr: data.phys_map, size: data.size, | 
|---|
| 289 | pgprot_val(pgprot_encrypted(FIXMAP_PAGE_NORMAL))); | 
|---|
| 290 | if (!new) { | 
|---|
| 291 | pr_err( "Failed to map new boot services memmap\n"); | 
|---|
| 292 | return; | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | efi_memmap_insert(old_memmap: &efi.memmap, buf: new, mem: &mr); | 
|---|
| 296 | early_memunmap(addr: new, size: data.size); | 
|---|
| 297 |  | 
|---|
| 298 | efi_memmap_install(data: &data); | 
|---|
| 299 | e820__range_update(start: addr, size, old_type: E820_TYPE_RAM, new_type: E820_TYPE_RESERVED); | 
|---|
| 300 | e820__update_table(table: e820_table); | 
|---|
| 301 | } | 
|---|
| 302 |  | 
|---|
| 303 | /* | 
|---|
| 304 | * Helper function for efi_reserve_boot_services() to figure out if we | 
|---|
| 305 | * can free regions in efi_free_boot_services(). | 
|---|
| 306 | * | 
|---|
| 307 | * Use this function to ensure we do not free regions owned by somebody | 
|---|
| 308 | * else. We must only reserve (and then free) regions: | 
|---|
| 309 | * | 
|---|
| 310 | * - Not within any part of the kernel | 
|---|
| 311 | * - Not the BIOS reserved area (E820_TYPE_RESERVED, E820_TYPE_NVS, etc) | 
|---|
| 312 | */ | 
|---|
| 313 | static __init bool can_free_region(u64 start, u64 size) | 
|---|
| 314 | { | 
|---|
| 315 | if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end)) | 
|---|
| 316 | return false; | 
|---|
| 317 |  | 
|---|
| 318 | if (!e820__mapped_all(start, end: start+size, type: E820_TYPE_RAM)) | 
|---|
| 319 | return false; | 
|---|
| 320 |  | 
|---|
| 321 | return true; | 
|---|
| 322 | } | 
|---|
| 323 |  | 
|---|
| 324 | void __init efi_reserve_boot_services(void) | 
|---|
| 325 | { | 
|---|
| 326 | efi_memory_desc_t *md; | 
|---|
| 327 |  | 
|---|
| 328 | if (!efi_enabled(EFI_MEMMAP)) | 
|---|
| 329 | return; | 
|---|
| 330 |  | 
|---|
| 331 | for_each_efi_memory_desc(md) { | 
|---|
| 332 | u64 start = md->phys_addr; | 
|---|
| 333 | u64 size = md->num_pages << EFI_PAGE_SHIFT; | 
|---|
| 334 | bool already_reserved; | 
|---|
| 335 |  | 
|---|
| 336 | if (md->type != EFI_BOOT_SERVICES_CODE && | 
|---|
| 337 | md->type != EFI_BOOT_SERVICES_DATA) | 
|---|
| 338 | continue; | 
|---|
| 339 |  | 
|---|
| 340 | already_reserved = memblock_is_region_reserved(base: start, size); | 
|---|
| 341 |  | 
|---|
| 342 | /* | 
|---|
| 343 | * Because the following memblock_reserve() is paired | 
|---|
| 344 | * with memblock_free_late() for this region in | 
|---|
| 345 | * efi_free_boot_services(), we must be extremely | 
|---|
| 346 | * careful not to reserve, and subsequently free, | 
|---|
| 347 | * critical regions of memory (like the kernel image) or | 
|---|
| 348 | * those regions that somebody else has already | 
|---|
| 349 | * reserved. | 
|---|
| 350 | * | 
|---|
| 351 | * A good example of a critical region that must not be | 
|---|
| 352 | * freed is page zero (first 4Kb of memory), which may | 
|---|
| 353 | * contain boot services code/data but is marked | 
|---|
| 354 | * E820_TYPE_RESERVED by trim_bios_range(). | 
|---|
| 355 | */ | 
|---|
| 356 | if (!already_reserved) { | 
|---|
| 357 | memblock_reserve(base: start, size); | 
|---|
| 358 |  | 
|---|
| 359 | /* | 
|---|
| 360 | * If we are the first to reserve the region, no | 
|---|
| 361 | * one else cares about it. We own it and can | 
|---|
| 362 | * free it later. | 
|---|
| 363 | */ | 
|---|
| 364 | if (can_free_region(start, size)) | 
|---|
| 365 | continue; | 
|---|
| 366 | } | 
|---|
| 367 |  | 
|---|
| 368 | /* | 
|---|
| 369 | * We don't own the region. We must not free it. | 
|---|
| 370 | * | 
|---|
| 371 | * Setting this bit for a boot services region really | 
|---|
| 372 | * doesn't make sense as far as the firmware is | 
|---|
| 373 | * concerned, but it does provide us with a way to tag | 
|---|
| 374 | * those regions that must not be paired with | 
|---|
| 375 | * memblock_free_late(). | 
|---|
| 376 | */ | 
|---|
| 377 | md->attribute |= EFI_MEMORY_RUNTIME; | 
|---|
| 378 | } | 
|---|
| 379 | } | 
|---|
| 380 |  | 
|---|
| 381 | /* | 
|---|
| 382 | * Apart from having VA mappings for EFI boot services code/data regions, | 
|---|
| 383 | * (duplicate) 1:1 mappings were also created as a quirk for buggy firmware. So, | 
|---|
| 384 | * unmap both 1:1 and VA mappings. | 
|---|
| 385 | */ | 
|---|
| 386 | static void __init efi_unmap_pages(efi_memory_desc_t *md) | 
|---|
| 387 | { | 
|---|
| 388 | pgd_t *pgd = efi_mm.pgd; | 
|---|
| 389 | u64 pa = md->phys_addr; | 
|---|
| 390 | u64 va = md->virt_addr; | 
|---|
| 391 |  | 
|---|
| 392 | /* | 
|---|
| 393 | * EFI mixed mode has all RAM mapped to access arguments while making | 
|---|
| 394 | * EFI runtime calls, hence don't unmap EFI boot services code/data | 
|---|
| 395 | * regions. | 
|---|
| 396 | */ | 
|---|
| 397 | if (efi_is_mixed()) | 
|---|
| 398 | return; | 
|---|
| 399 |  | 
|---|
| 400 | if (kernel_unmap_pages_in_pgd(pgd, address: pa, numpages: md->num_pages)) | 
|---|
| 401 | pr_err( "Failed to unmap 1:1 mapping for 0x%llx\n", pa); | 
|---|
| 402 |  | 
|---|
| 403 | if (kernel_unmap_pages_in_pgd(pgd, address: va, numpages: md->num_pages)) | 
|---|
| 404 | pr_err( "Failed to unmap VA mapping for 0x%llx\n", va); | 
|---|
| 405 | } | 
|---|
| 406 |  | 
|---|
| 407 | void __init efi_free_boot_services(void) | 
|---|
| 408 | { | 
|---|
| 409 | struct efi_memory_map_data data = { 0 }; | 
|---|
| 410 | efi_memory_desc_t *md; | 
|---|
| 411 | int num_entries = 0; | 
|---|
| 412 | void *new, *new_md; | 
|---|
| 413 |  | 
|---|
| 414 | /* Keep all regions for /sys/kernel/debug/efi */ | 
|---|
| 415 | if (efi_enabled(EFI_DBG)) | 
|---|
| 416 | return; | 
|---|
| 417 |  | 
|---|
| 418 | for_each_efi_memory_desc(md) { | 
|---|
| 419 | unsigned long long start = md->phys_addr; | 
|---|
| 420 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; | 
|---|
| 421 | size_t rm_size; | 
|---|
| 422 |  | 
|---|
| 423 | if (md->type != EFI_BOOT_SERVICES_CODE && | 
|---|
| 424 | md->type != EFI_BOOT_SERVICES_DATA) { | 
|---|
| 425 | num_entries++; | 
|---|
| 426 | continue; | 
|---|
| 427 | } | 
|---|
| 428 |  | 
|---|
| 429 | /* Do not free, someone else owns it: */ | 
|---|
| 430 | if (md->attribute & EFI_MEMORY_RUNTIME) { | 
|---|
| 431 | num_entries++; | 
|---|
| 432 | continue; | 
|---|
| 433 | } | 
|---|
| 434 |  | 
|---|
| 435 | /* | 
|---|
| 436 | * Before calling set_virtual_address_map(), EFI boot services | 
|---|
| 437 | * code/data regions were mapped as a quirk for buggy firmware. | 
|---|
| 438 | * Unmap them from efi_pgd before freeing them up. | 
|---|
| 439 | */ | 
|---|
| 440 | efi_unmap_pages(md); | 
|---|
| 441 |  | 
|---|
| 442 | /* | 
|---|
| 443 | * Nasty quirk: if all sub-1MB memory is used for boot | 
|---|
| 444 | * services, we can get here without having allocated the | 
|---|
| 445 | * real mode trampoline.  It's too late to hand boot services | 
|---|
| 446 | * memory back to the memblock allocator, so instead | 
|---|
| 447 | * try to manually allocate the trampoline if needed. | 
|---|
| 448 | * | 
|---|
| 449 | * I've seen this on a Dell XPS 13 9350 with firmware | 
|---|
| 450 | * 1.4.4 with SGX enabled booting Linux via Fedora 24's | 
|---|
| 451 | * grub2-efi on a hard disk.  (And no, I don't know why | 
|---|
| 452 | * this happened, but Linux should still try to boot rather | 
|---|
| 453 | * panicking early.) | 
|---|
| 454 | */ | 
|---|
| 455 | rm_size = real_mode_size_needed(); | 
|---|
| 456 | if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) { | 
|---|
| 457 | set_real_mode_mem(start); | 
|---|
| 458 | start += rm_size; | 
|---|
| 459 | size -= rm_size; | 
|---|
| 460 | } | 
|---|
| 461 |  | 
|---|
| 462 | /* | 
|---|
| 463 | * Don't free memory under 1M for two reasons: | 
|---|
| 464 | * - BIOS might clobber it | 
|---|
| 465 | * - Crash kernel needs it to be reserved | 
|---|
| 466 | */ | 
|---|
| 467 | if (start + size < SZ_1M) | 
|---|
| 468 | continue; | 
|---|
| 469 | if (start < SZ_1M) { | 
|---|
| 470 | size -= (SZ_1M - start); | 
|---|
| 471 | start = SZ_1M; | 
|---|
| 472 | } | 
|---|
| 473 |  | 
|---|
| 474 | memblock_free_late(base: start, size); | 
|---|
| 475 | } | 
|---|
| 476 |  | 
|---|
| 477 | if (!num_entries) | 
|---|
| 478 | return; | 
|---|
| 479 |  | 
|---|
| 480 | if (efi_memmap_alloc(num_entries, data: &data) != 0) { | 
|---|
| 481 | pr_err( "Failed to allocate new EFI memmap\n"); | 
|---|
| 482 | return; | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 | new = memremap(offset: data.phys_map, size: data.size, flags: MEMREMAP_WB); | 
|---|
| 486 | if (!new) { | 
|---|
| 487 | pr_err( "Failed to map new EFI memmap\n"); | 
|---|
| 488 | return; | 
|---|
| 489 | } | 
|---|
| 490 |  | 
|---|
| 491 | /* | 
|---|
| 492 | * Build a new EFI memmap that excludes any boot services | 
|---|
| 493 | * regions that are not tagged EFI_MEMORY_RUNTIME, since those | 
|---|
| 494 | * regions have now been freed. | 
|---|
| 495 | */ | 
|---|
| 496 | new_md = new; | 
|---|
| 497 | for_each_efi_memory_desc(md) { | 
|---|
| 498 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && | 
|---|
| 499 | (md->type == EFI_BOOT_SERVICES_CODE || | 
|---|
| 500 | md->type == EFI_BOOT_SERVICES_DATA)) | 
|---|
| 501 | continue; | 
|---|
| 502 |  | 
|---|
| 503 | memcpy(to: new_md, from: md, len: efi.memmap.desc_size); | 
|---|
| 504 | new_md += efi.memmap.desc_size; | 
|---|
| 505 | } | 
|---|
| 506 |  | 
|---|
| 507 | memunmap(addr: new); | 
|---|
| 508 |  | 
|---|
| 509 | if (efi_memmap_install(data: &data) != 0) { | 
|---|
| 510 | pr_err( "Could not install new EFI memmap\n"); | 
|---|
| 511 | return; | 
|---|
| 512 | } | 
|---|
| 513 | } | 
|---|
| 514 |  | 
|---|
| 515 | /* | 
|---|
| 516 | * A number of config table entries get remapped to virtual addresses | 
|---|
| 517 | * after entering EFI virtual mode. However, the kexec kernel requires | 
|---|
| 518 | * their physical addresses therefore we pass them via setup_data and | 
|---|
| 519 | * correct those entries to their respective physical addresses here. | 
|---|
| 520 | * | 
|---|
| 521 | * Currently only handles smbios which is necessary for some firmware | 
|---|
| 522 | * implementation. | 
|---|
| 523 | */ | 
|---|
| 524 | int __init efi_reuse_config(u64 tables, int nr_tables) | 
|---|
| 525 | { | 
|---|
| 526 | int i, sz, ret = 0; | 
|---|
| 527 | void *p, *tablep; | 
|---|
| 528 | struct efi_setup_data *data; | 
|---|
| 529 |  | 
|---|
| 530 | if (nr_tables == 0) | 
|---|
| 531 | return 0; | 
|---|
| 532 |  | 
|---|
| 533 | if (!efi_setup) | 
|---|
| 534 | return 0; | 
|---|
| 535 |  | 
|---|
| 536 | if (!efi_enabled(EFI_64BIT)) | 
|---|
| 537 | return 0; | 
|---|
| 538 |  | 
|---|
| 539 | data = early_memremap(phys_addr: efi_setup, size: sizeof(*data)); | 
|---|
| 540 | if (!data) { | 
|---|
| 541 | ret = -ENOMEM; | 
|---|
| 542 | goto out; | 
|---|
| 543 | } | 
|---|
| 544 |  | 
|---|
| 545 | if (!data->smbios) | 
|---|
| 546 | goto out_memremap; | 
|---|
| 547 |  | 
|---|
| 548 | sz = sizeof(efi_config_table_64_t); | 
|---|
| 549 |  | 
|---|
| 550 | p = tablep = early_memremap(phys_addr: tables, size: nr_tables * sz); | 
|---|
| 551 | if (!p) { | 
|---|
| 552 | pr_err( "Could not map Configuration table!\n"); | 
|---|
| 553 | ret = -ENOMEM; | 
|---|
| 554 | goto out_memremap; | 
|---|
| 555 | } | 
|---|
| 556 |  | 
|---|
| 557 | for (i = 0; i < nr_tables; i++) { | 
|---|
| 558 | efi_guid_t guid; | 
|---|
| 559 |  | 
|---|
| 560 | guid = ((efi_config_table_64_t *)p)->guid; | 
|---|
| 561 |  | 
|---|
| 562 | if (!efi_guidcmp(left: guid, SMBIOS_TABLE_GUID)) | 
|---|
| 563 | ((efi_config_table_64_t *)p)->table = data->smbios; | 
|---|
| 564 |  | 
|---|
| 565 | /* Do not bother to play with mem attr table across kexec */ | 
|---|
| 566 | if (!efi_guidcmp(left: guid, EFI_MEMORY_ATTRIBUTES_TABLE_GUID)) | 
|---|
| 567 | ((efi_config_table_64_t *)p)->table = EFI_INVALID_TABLE_ADDR; | 
|---|
| 568 |  | 
|---|
| 569 | p += sz; | 
|---|
| 570 | } | 
|---|
| 571 | early_memunmap(addr: tablep, size: nr_tables * sz); | 
|---|
| 572 |  | 
|---|
| 573 | out_memremap: | 
|---|
| 574 | early_memunmap(addr: data, size: sizeof(*data)); | 
|---|
| 575 | out: | 
|---|
| 576 | return ret; | 
|---|
| 577 | } | 
|---|
| 578 |  | 
|---|
| 579 | void __init efi_apply_memmap_quirks(void) | 
|---|
| 580 | { | 
|---|
| 581 | /* | 
|---|
| 582 | * Once setup is done earlier, unmap the EFI memory map on mismatched | 
|---|
| 583 | * firmware/kernel architectures since there is no support for runtime | 
|---|
| 584 | * services. | 
|---|
| 585 | */ | 
|---|
| 586 | if (!efi_runtime_supported()) { | 
|---|
| 587 | pr_info( "Setup done, disabling due to 32/64-bit mismatch\n"); | 
|---|
| 588 | efi_memmap_unmap(); | 
|---|
| 589 | } | 
|---|
| 590 | } | 
|---|
| 591 |  | 
|---|
| 592 | /* | 
|---|
| 593 | * For most modern platforms the preferred method of powering off is via | 
|---|
| 594 | * ACPI. However, there are some that are known to require the use of | 
|---|
| 595 | * EFI runtime services and for which ACPI does not work at all. | 
|---|
| 596 | * | 
|---|
| 597 | * Using EFI is a last resort, to be used only if no other option | 
|---|
| 598 | * exists. | 
|---|
| 599 | */ | 
|---|
| 600 | bool efi_reboot_required(void) | 
|---|
| 601 | { | 
|---|
| 602 | if (!acpi_gbl_reduced_hardware) | 
|---|
| 603 | return false; | 
|---|
| 604 |  | 
|---|
| 605 | efi_reboot_quirk_mode = EFI_RESET_WARM; | 
|---|
| 606 | return true; | 
|---|
| 607 | } | 
|---|
| 608 |  | 
|---|
| 609 | bool efi_poweroff_required(void) | 
|---|
| 610 | { | 
|---|
| 611 | return acpi_gbl_reduced_hardware || acpi_no_s5; | 
|---|
| 612 | } | 
|---|
| 613 |  | 
|---|
| 614 | #ifdef CONFIG_EFI_CAPSULE_QUIRK_QUARK_CSH | 
|---|
| 615 |  | 
|---|
| 616 | static int qrk_capsule_setup_info(struct capsule_info *cap_info, void **pkbuff, | 
|---|
| 617 | size_t hdr_bytes) | 
|---|
| 618 | { | 
|---|
| 619 | struct quark_security_header *csh = *pkbuff; | 
|---|
| 620 |  | 
|---|
| 621 | /* Only process data block that is larger than the security header */ | 
|---|
| 622 | if (hdr_bytes < sizeof(struct quark_security_header)) | 
|---|
| 623 | return 0; | 
|---|
| 624 |  | 
|---|
| 625 | if (csh->csh_signature != QUARK_CSH_SIGNATURE || | 
|---|
| 626 | csh->headersize != QUARK_SECURITY_HEADER_SIZE) | 
|---|
| 627 | return 1; | 
|---|
| 628 |  | 
|---|
| 629 | /* Only process data block if EFI header is included */ | 
|---|
| 630 | if (hdr_bytes < QUARK_SECURITY_HEADER_SIZE + | 
|---|
| 631 | sizeof(efi_capsule_header_t)) | 
|---|
| 632 | return 0; | 
|---|
| 633 |  | 
|---|
| 634 | pr_debug( "Quark security header detected\n"); | 
|---|
| 635 |  | 
|---|
| 636 | if (csh->rsvd_next_header != 0) { | 
|---|
| 637 | pr_err( "multiple Quark security headers not supported\n"); | 
|---|
| 638 | return -EINVAL; | 
|---|
| 639 | } | 
|---|
| 640 |  | 
|---|
| 641 | *pkbuff += csh->headersize; | 
|---|
| 642 | cap_info->total_size = csh->headersize; | 
|---|
| 643 |  | 
|---|
| 644 | /* | 
|---|
| 645 | * Update the first page pointer to skip over the CSH header. | 
|---|
| 646 | */ | 
|---|
| 647 | cap_info->phys[0] += csh->headersize; | 
|---|
| 648 |  | 
|---|
| 649 | /* | 
|---|
| 650 | * cap_info->capsule should point at a virtual mapping of the entire | 
|---|
| 651 | * capsule, starting at the capsule header. Our image has the Quark | 
|---|
| 652 | * security header prepended, so we cannot rely on the default vmap() | 
|---|
| 653 | * mapping created by the generic capsule code. | 
|---|
| 654 | * Given that the Quark firmware does not appear to care about the | 
|---|
| 655 | * virtual mapping, let's just point cap_info->capsule at our copy | 
|---|
| 656 | * of the capsule header. | 
|---|
| 657 | */ | 
|---|
| 658 | cap_info->capsule = &cap_info->header; | 
|---|
| 659 |  | 
|---|
| 660 | return 1; | 
|---|
| 661 | } | 
|---|
| 662 |  | 
|---|
| 663 | static const struct x86_cpu_id efi_capsule_quirk_ids[] = { | 
|---|
| 664 | X86_MATCH_VFM(INTEL_QUARK_X1000, &qrk_capsule_setup_info), | 
|---|
| 665 | { } | 
|---|
| 666 | }; | 
|---|
| 667 |  | 
|---|
| 668 | int efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff, | 
|---|
| 669 | size_t hdr_bytes) | 
|---|
| 670 | { | 
|---|
| 671 | int (*quirk_handler)(struct capsule_info *, void **, size_t); | 
|---|
| 672 | const struct x86_cpu_id *id; | 
|---|
| 673 | int ret; | 
|---|
| 674 |  | 
|---|
| 675 | if (hdr_bytes < sizeof(efi_capsule_header_t)) | 
|---|
| 676 | return 0; | 
|---|
| 677 |  | 
|---|
| 678 | cap_info->total_size = 0; | 
|---|
| 679 |  | 
|---|
| 680 | id = x86_match_cpu(efi_capsule_quirk_ids); | 
|---|
| 681 | if (id) { | 
|---|
| 682 | /* | 
|---|
| 683 | * The quirk handler is supposed to return | 
|---|
| 684 | *  - a value > 0 if the setup should continue, after advancing | 
|---|
| 685 | *    kbuff as needed | 
|---|
| 686 | *  - 0 if not enough hdr_bytes are available yet | 
|---|
| 687 | *  - a negative error code otherwise | 
|---|
| 688 | */ | 
|---|
| 689 | quirk_handler = (typeof(quirk_handler))id->driver_data; | 
|---|
| 690 | ret = quirk_handler(cap_info, &kbuff, hdr_bytes); | 
|---|
| 691 | if (ret <= 0) | 
|---|
| 692 | return ret; | 
|---|
| 693 | } | 
|---|
| 694 |  | 
|---|
| 695 | memcpy(&cap_info->header, kbuff, sizeof(cap_info->header)); | 
|---|
| 696 |  | 
|---|
| 697 | cap_info->total_size += cap_info->header.imagesize; | 
|---|
| 698 |  | 
|---|
| 699 | return __efi_capsule_setup_info(cap_info); | 
|---|
| 700 | } | 
|---|
| 701 |  | 
|---|
| 702 | #endif | 
|---|
| 703 |  | 
|---|
| 704 | /* | 
|---|
| 705 | * If any access by any efi runtime service causes a page fault, then, | 
|---|
| 706 | * 1. If it's efi_reset_system(), reboot through BIOS. | 
|---|
| 707 | * 2. If any other efi runtime service, then | 
|---|
| 708 | *    a. Return error status to the efi caller process. | 
|---|
| 709 | *    b. Disable EFI Runtime Services forever and | 
|---|
| 710 | *    c. Freeze efi_rts_wq and schedule new process. | 
|---|
| 711 | * | 
|---|
| 712 | * @return: Returns, if the page fault is not handled. This function | 
|---|
| 713 | * will never return if the page fault is handled successfully. | 
|---|
| 714 | */ | 
|---|
| 715 | void efi_crash_gracefully_on_page_fault(unsigned long phys_addr) | 
|---|
| 716 | { | 
|---|
| 717 | if (!IS_ENABLED(CONFIG_X86_64)) | 
|---|
| 718 | return; | 
|---|
| 719 |  | 
|---|
| 720 | /* | 
|---|
| 721 | * If we get an interrupt/NMI while processing an EFI runtime service | 
|---|
| 722 | * then this is a regular OOPS, not an EFI failure. | 
|---|
| 723 | */ | 
|---|
| 724 | if (in_interrupt()) | 
|---|
| 725 | return; | 
|---|
| 726 |  | 
|---|
| 727 | /* | 
|---|
| 728 | * Make sure that an efi runtime service caused the page fault. | 
|---|
| 729 | * READ_ONCE() because we might be OOPSing in a different thread, | 
|---|
| 730 | * and we don't want to trip KTSAN while trying to OOPS. | 
|---|
| 731 | */ | 
|---|
| 732 | if (READ_ONCE(efi_rts_work.efi_rts_id) == EFI_NONE || | 
|---|
| 733 | current_work() != &efi_rts_work.work) | 
|---|
| 734 | return; | 
|---|
| 735 |  | 
|---|
| 736 | /* | 
|---|
| 737 | * Address range 0x0000 - 0x0fff is always mapped in the efi_pgd, so | 
|---|
| 738 | * page faulting on these addresses isn't expected. | 
|---|
| 739 | */ | 
|---|
| 740 | if (phys_addr <= 0x0fff) | 
|---|
| 741 | return; | 
|---|
| 742 |  | 
|---|
| 743 | /* | 
|---|
| 744 | * Print stack trace as it might be useful to know which EFI Runtime | 
|---|
| 745 | * Service is buggy. | 
|---|
| 746 | */ | 
|---|
| 747 | WARN(1, FW_BUG "Page fault caused by firmware at PA: 0x%lx\n", | 
|---|
| 748 | phys_addr); | 
|---|
| 749 |  | 
|---|
| 750 | /* | 
|---|
| 751 | * Buggy efi_reset_system() is handled differently from other EFI | 
|---|
| 752 | * Runtime Services as it doesn't use efi_rts_wq. Although, | 
|---|
| 753 | * native_machine_emergency_restart() says that machine_real_restart() | 
|---|
| 754 | * could fail, it's better not to complicate this fault handler | 
|---|
| 755 | * because this case occurs *very* rarely and hence could be improved | 
|---|
| 756 | * on a need by basis. | 
|---|
| 757 | */ | 
|---|
| 758 | if (efi_rts_work.efi_rts_id == EFI_RESET_SYSTEM) { | 
|---|
| 759 | pr_info( "efi_reset_system() buggy! Reboot through BIOS\n"); | 
|---|
| 760 | machine_real_restart(MRR_BIOS); | 
|---|
| 761 | return; | 
|---|
| 762 | } | 
|---|
| 763 |  | 
|---|
| 764 | /* | 
|---|
| 765 | * Before calling EFI Runtime Service, the kernel has switched the | 
|---|
| 766 | * calling process to efi_mm. Hence, switch back to task_mm. | 
|---|
| 767 | */ | 
|---|
| 768 | arch_efi_call_virt_teardown(); | 
|---|
| 769 |  | 
|---|
| 770 | /* Signal error status to the efi caller process */ | 
|---|
| 771 | efi_rts_work.status = EFI_ABORTED; | 
|---|
| 772 | complete(&efi_rts_work.efi_rts_comp); | 
|---|
| 773 |  | 
|---|
| 774 | clear_bit(EFI_RUNTIME_SERVICES, addr: &efi.flags); | 
|---|
| 775 | pr_info( "Froze efi_rts_wq and disabled EFI Runtime Services\n"); | 
|---|
| 776 |  | 
|---|
| 777 | /* | 
|---|
| 778 | * Call schedule() in an infinite loop, so that any spurious wake ups | 
|---|
| 779 | * will never run efi_rts_wq again. | 
|---|
| 780 | */ | 
|---|
| 781 | for (;;) { | 
|---|
| 782 | set_current_state(TASK_IDLE); | 
|---|
| 783 | schedule(); | 
|---|
| 784 | } | 
|---|
| 785 | } | 
|---|
| 786 |  | 
|---|