Linux 4.18.10
[linux/fpc-iii.git] / arch / x86 / platform / efi / quirks.c
blob36c1f8b9f7e0c2df3dec1828e9f786f9d6c1ecfb
1 #define pr_fmt(fmt) "efi: " fmt
3 #include <linux/init.h>
4 #include <linux/kernel.h>
5 #include <linux/string.h>
6 #include <linux/time.h>
7 #include <linux/types.h>
8 #include <linux/efi.h>
9 #include <linux/slab.h>
10 #include <linux/memblock.h>
11 #include <linux/bootmem.h>
12 #include <linux/acpi.h>
13 #include <linux/dmi.h>
15 #include <asm/e820/api.h>
16 #include <asm/efi.h>
17 #include <asm/uv/uv.h>
18 #include <asm/cpu_device_id.h>
20 #define EFI_MIN_RESERVE 5120
22 #define EFI_DUMMY_GUID \
23 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
25 #define QUARK_CSH_SIGNATURE 0x5f435348 /* _CSH */
26 #define QUARK_SECURITY_HEADER_SIZE 0x400
29 * Header prepended to the standard EFI capsule on Quark systems the are based
30 * on Intel firmware BSP.
31 * @csh_signature: Unique identifier to sanity check signed module
32 * presence ("_CSH").
33 * @version: Current version of CSH used. Should be one for Quark A0.
34 * @modulesize: Size of the entire module including the module header
35 * and payload.
36 * @security_version_number_index: Index of SVN to use for validation of signed
37 * module.
38 * @security_version_number: Used to prevent against roll back of modules.
39 * @rsvd_module_id: Currently unused for Clanton (Quark).
40 * @rsvd_module_vendor: Vendor Identifier. For Intel products value is
41 * 0x00008086.
42 * @rsvd_date: BCD representation of build date as yyyymmdd, where
43 * yyyy=4 digit year, mm=1-12, dd=1-31.
44 * @headersize: Total length of the header including including any
45 * padding optionally added by the signing tool.
46 * @hash_algo: What Hash is used in the module signing.
47 * @cryp_algo: What Crypto is used in the module signing.
48 * @keysize: Total length of the key data including including any
49 * padding optionally added by the signing tool.
50 * @signaturesize: Total length of the signature including including any
51 * padding optionally added by the signing tool.
52 * @rsvd_next_header: 32-bit pointer to the next Secure Boot Module in the
53 * chain, if there is a next header.
54 * @rsvd: Reserved, padding structure to required size.
56 * See also QuartSecurityHeader_t in
57 * Quark_EDKII_v1.2.1.1/QuarkPlatformPkg/Include/QuarkBootRom.h
58 * from https://downloadcenter.intel.com/download/23197/Intel-Quark-SoC-X1000-Board-Support-Package-BSP
60 struct quark_security_header {
61 u32 csh_signature;
62 u32 version;
63 u32 modulesize;
64 u32 security_version_number_index;
65 u32 security_version_number;
66 u32 rsvd_module_id;
67 u32 rsvd_module_vendor;
68 u32 rsvd_date;
69 u32 headersize;
70 u32 hash_algo;
71 u32 cryp_algo;
72 u32 keysize;
73 u32 signaturesize;
74 u32 rsvd_next_header;
75 u32 rsvd[2];
78 static const efi_char16_t efi_dummy_name[] = L"DUMMY";
80 static bool efi_no_storage_paranoia;
83 * Some firmware implementations refuse to boot if there's insufficient
84 * space in the variable store. The implementation of garbage collection
85 * in some FW versions causes stale (deleted) variables to take up space
86 * longer than intended and space is only freed once the store becomes
87 * almost completely full.
89 * Enabling this option disables the space checks in
90 * efi_query_variable_store() and forces garbage collection.
92 * Only enable this option if deleting EFI variables does not free up
93 * space in your variable store, e.g. if despite deleting variables
94 * you're unable to create new ones.
96 static int __init setup_storage_paranoia(char *arg)
98 efi_no_storage_paranoia = true;
99 return 0;
101 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
104 * Deleting the dummy variable which kicks off garbage collection
106 void efi_delete_dummy_variable(void)
108 efi.set_variable((efi_char16_t *)efi_dummy_name,
109 &EFI_DUMMY_GUID,
110 EFI_VARIABLE_NON_VOLATILE |
111 EFI_VARIABLE_BOOTSERVICE_ACCESS |
112 EFI_VARIABLE_RUNTIME_ACCESS,
113 0, NULL);
117 * In the nonblocking case we do not attempt to perform garbage
118 * collection if we do not have enough free space. Rather, we do the
119 * bare minimum check and give up immediately if the available space
120 * is below EFI_MIN_RESERVE.
122 * This function is intended to be small and simple because it is
123 * invoked from crash handler paths.
125 static efi_status_t
126 query_variable_store_nonblocking(u32 attributes, unsigned long size)
128 efi_status_t status;
129 u64 storage_size, remaining_size, max_size;
131 status = efi.query_variable_info_nonblocking(attributes, &storage_size,
132 &remaining_size,
133 &max_size);
134 if (status != EFI_SUCCESS)
135 return status;
137 if (remaining_size - size < EFI_MIN_RESERVE)
138 return EFI_OUT_OF_RESOURCES;
140 return EFI_SUCCESS;
144 * Some firmware implementations refuse to boot if there's insufficient space
145 * in the variable store. Ensure that we never use more than a safe limit.
147 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
148 * store.
150 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
151 bool nonblocking)
153 efi_status_t status;
154 u64 storage_size, remaining_size, max_size;
156 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
157 return 0;
159 if (nonblocking)
160 return query_variable_store_nonblocking(attributes, size);
162 status = efi.query_variable_info(attributes, &storage_size,
163 &remaining_size, &max_size);
164 if (status != EFI_SUCCESS)
165 return status;
168 * We account for that by refusing the write if permitting it would
169 * reduce the available space to under 5KB. This figure was provided by
170 * Samsung, so should be safe.
172 if ((remaining_size - size < EFI_MIN_RESERVE) &&
173 !efi_no_storage_paranoia) {
176 * Triggering garbage collection may require that the firmware
177 * generate a real EFI_OUT_OF_RESOURCES error. We can force
178 * that by attempting to use more space than is available.
180 unsigned long dummy_size = remaining_size + 1024;
181 void *dummy = kzalloc(dummy_size, GFP_KERNEL);
183 if (!dummy)
184 return EFI_OUT_OF_RESOURCES;
186 status = efi.set_variable((efi_char16_t *)efi_dummy_name,
187 &EFI_DUMMY_GUID,
188 EFI_VARIABLE_NON_VOLATILE |
189 EFI_VARIABLE_BOOTSERVICE_ACCESS |
190 EFI_VARIABLE_RUNTIME_ACCESS,
191 dummy_size, dummy);
193 if (status == EFI_SUCCESS) {
195 * This should have failed, so if it didn't make sure
196 * that we delete it...
198 efi_delete_dummy_variable();
201 kfree(dummy);
204 * The runtime code may now have triggered a garbage collection
205 * run, so check the variable info again
207 status = efi.query_variable_info(attributes, &storage_size,
208 &remaining_size, &max_size);
210 if (status != EFI_SUCCESS)
211 return status;
214 * There still isn't enough room, so return an error
216 if (remaining_size - size < EFI_MIN_RESERVE)
217 return EFI_OUT_OF_RESOURCES;
220 return EFI_SUCCESS;
222 EXPORT_SYMBOL_GPL(efi_query_variable_store);
225 * The UEFI specification makes it clear that the operating system is
226 * free to do whatever it wants with boot services code after
227 * ExitBootServices() has been called. Ignoring this recommendation a
228 * significant bunch of EFI implementations continue calling into boot
229 * services code (SetVirtualAddressMap). In order to work around such
230 * buggy implementations we reserve boot services region during EFI
231 * init and make sure it stays executable. Then, after
232 * SetVirtualAddressMap(), it is discarded.
234 * However, some boot services regions contain data that is required
235 * by drivers, so we need to track which memory ranges can never be
236 * freed. This is done by tagging those regions with the
237 * EFI_MEMORY_RUNTIME attribute.
239 * Any driver that wants to mark a region as reserved must use
240 * efi_mem_reserve() which will insert a new EFI memory descriptor
241 * into efi.memmap (splitting existing regions if necessary) and tag
242 * it with EFI_MEMORY_RUNTIME.
244 void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)
246 phys_addr_t new_phys, new_size;
247 struct efi_mem_range mr;
248 efi_memory_desc_t md;
249 int num_entries;
250 void *new;
252 if (efi_mem_desc_lookup(addr, &md)) {
253 pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);
254 return;
257 if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {
258 pr_err("Region spans EFI memory descriptors, %pa\n", &addr);
259 return;
262 /* No need to reserve regions that will never be freed. */
263 if (md.attribute & EFI_MEMORY_RUNTIME)
264 return;
266 size += addr % EFI_PAGE_SIZE;
267 size = round_up(size, EFI_PAGE_SIZE);
268 addr = round_down(addr, EFI_PAGE_SIZE);
270 mr.range.start = addr;
271 mr.range.end = addr + size - 1;
272 mr.attribute = md.attribute | EFI_MEMORY_RUNTIME;
274 num_entries = efi_memmap_split_count(&md, &mr.range);
275 num_entries += efi.memmap.nr_map;
277 new_size = efi.memmap.desc_size * num_entries;
279 new_phys = efi_memmap_alloc(num_entries);
280 if (!new_phys) {
281 pr_err("Could not allocate boot services memmap\n");
282 return;
285 new = early_memremap(new_phys, new_size);
286 if (!new) {
287 pr_err("Failed to map new boot services memmap\n");
288 return;
291 efi_memmap_insert(&efi.memmap, new, &mr);
292 early_memunmap(new, new_size);
294 efi_memmap_install(new_phys, num_entries);
298 * Helper function for efi_reserve_boot_services() to figure out if we
299 * can free regions in efi_free_boot_services().
301 * Use this function to ensure we do not free regions owned by somebody
302 * else. We must only reserve (and then free) regions:
304 * - Not within any part of the kernel
305 * - Not the BIOS reserved area (E820_TYPE_RESERVED, E820_TYPE_NVS, etc)
307 static bool can_free_region(u64 start, u64 size)
309 if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
310 return false;
312 if (!e820__mapped_all(start, start+size, E820_TYPE_RAM))
313 return false;
315 return true;
318 void __init efi_reserve_boot_services(void)
320 efi_memory_desc_t *md;
322 for_each_efi_memory_desc(md) {
323 u64 start = md->phys_addr;
324 u64 size = md->num_pages << EFI_PAGE_SHIFT;
325 bool already_reserved;
327 if (md->type != EFI_BOOT_SERVICES_CODE &&
328 md->type != EFI_BOOT_SERVICES_DATA)
329 continue;
331 already_reserved = memblock_is_region_reserved(start, size);
334 * Because the following memblock_reserve() is paired
335 * with free_bootmem_late() for this region in
336 * efi_free_boot_services(), we must be extremely
337 * careful not to reserve, and subsequently free,
338 * critical regions of memory (like the kernel image) or
339 * those regions that somebody else has already
340 * reserved.
342 * A good example of a critical region that must not be
343 * freed is page zero (first 4Kb of memory), which may
344 * contain boot services code/data but is marked
345 * E820_TYPE_RESERVED by trim_bios_range().
347 if (!already_reserved) {
348 memblock_reserve(start, size);
351 * If we are the first to reserve the region, no
352 * one else cares about it. We own it and can
353 * free it later.
355 if (can_free_region(start, size))
356 continue;
360 * We don't own the region. We must not free it.
362 * Setting this bit for a boot services region really
363 * doesn't make sense as far as the firmware is
364 * concerned, but it does provide us with a way to tag
365 * those regions that must not be paired with
366 * free_bootmem_late().
368 md->attribute |= EFI_MEMORY_RUNTIME;
372 void __init efi_free_boot_services(void)
374 phys_addr_t new_phys, new_size;
375 efi_memory_desc_t *md;
376 int num_entries = 0;
377 void *new, *new_md;
379 for_each_efi_memory_desc(md) {
380 unsigned long long start = md->phys_addr;
381 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
382 size_t rm_size;
384 if (md->type != EFI_BOOT_SERVICES_CODE &&
385 md->type != EFI_BOOT_SERVICES_DATA) {
386 num_entries++;
387 continue;
390 /* Do not free, someone else owns it: */
391 if (md->attribute & EFI_MEMORY_RUNTIME) {
392 num_entries++;
393 continue;
397 * Nasty quirk: if all sub-1MB memory is used for boot
398 * services, we can get here without having allocated the
399 * real mode trampoline. It's too late to hand boot services
400 * memory back to the memblock allocator, so instead
401 * try to manually allocate the trampoline if needed.
403 * I've seen this on a Dell XPS 13 9350 with firmware
404 * 1.4.4 with SGX enabled booting Linux via Fedora 24's
405 * grub2-efi on a hard disk. (And no, I don't know why
406 * this happened, but Linux should still try to boot rather
407 * panicing early.)
409 rm_size = real_mode_size_needed();
410 if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
411 set_real_mode_mem(start, rm_size);
412 start += rm_size;
413 size -= rm_size;
416 free_bootmem_late(start, size);
419 if (!num_entries)
420 return;
422 new_size = efi.memmap.desc_size * num_entries;
423 new_phys = efi_memmap_alloc(num_entries);
424 if (!new_phys) {
425 pr_err("Failed to allocate new EFI memmap\n");
426 return;
429 new = memremap(new_phys, new_size, MEMREMAP_WB);
430 if (!new) {
431 pr_err("Failed to map new EFI memmap\n");
432 return;
436 * Build a new EFI memmap that excludes any boot services
437 * regions that are not tagged EFI_MEMORY_RUNTIME, since those
438 * regions have now been freed.
440 new_md = new;
441 for_each_efi_memory_desc(md) {
442 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
443 (md->type == EFI_BOOT_SERVICES_CODE ||
444 md->type == EFI_BOOT_SERVICES_DATA))
445 continue;
447 memcpy(new_md, md, efi.memmap.desc_size);
448 new_md += efi.memmap.desc_size;
451 memunmap(new);
453 if (efi_memmap_install(new_phys, num_entries)) {
454 pr_err("Could not install new EFI memmap\n");
455 return;
460 * A number of config table entries get remapped to virtual addresses
461 * after entering EFI virtual mode. However, the kexec kernel requires
462 * their physical addresses therefore we pass them via setup_data and
463 * correct those entries to their respective physical addresses here.
465 * Currently only handles smbios which is necessary for some firmware
466 * implementation.
468 int __init efi_reuse_config(u64 tables, int nr_tables)
470 int i, sz, ret = 0;
471 void *p, *tablep;
472 struct efi_setup_data *data;
474 if (!efi_setup)
475 return 0;
477 if (!efi_enabled(EFI_64BIT))
478 return 0;
480 data = early_memremap(efi_setup, sizeof(*data));
481 if (!data) {
482 ret = -ENOMEM;
483 goto out;
486 if (!data->smbios)
487 goto out_memremap;
489 sz = sizeof(efi_config_table_64_t);
491 p = tablep = early_memremap(tables, nr_tables * sz);
492 if (!p) {
493 pr_err("Could not map Configuration table!\n");
494 ret = -ENOMEM;
495 goto out_memremap;
498 for (i = 0; i < efi.systab->nr_tables; i++) {
499 efi_guid_t guid;
501 guid = ((efi_config_table_64_t *)p)->guid;
503 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
504 ((efi_config_table_64_t *)p)->table = data->smbios;
505 p += sz;
507 early_memunmap(tablep, nr_tables * sz);
509 out_memremap:
510 early_memunmap(data, sizeof(*data));
511 out:
512 return ret;
515 static const struct dmi_system_id sgi_uv1_dmi[] = {
516 { NULL, "SGI UV1",
517 { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"),
518 DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
519 DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"),
522 { } /* NULL entry stops DMI scanning */
525 void __init efi_apply_memmap_quirks(void)
528 * Once setup is done earlier, unmap the EFI memory map on mismatched
529 * firmware/kernel architectures since there is no support for runtime
530 * services.
532 if (!efi_runtime_supported()) {
533 pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
534 efi_memmap_unmap();
537 /* UV2+ BIOS has a fix for this issue. UV1 still needs the quirk. */
538 if (dmi_check_system(sgi_uv1_dmi))
539 set_bit(EFI_OLD_MEMMAP, &efi.flags);
543 * For most modern platforms the preferred method of powering off is via
544 * ACPI. However, there are some that are known to require the use of
545 * EFI runtime services and for which ACPI does not work at all.
547 * Using EFI is a last resort, to be used only if no other option
548 * exists.
550 bool efi_reboot_required(void)
552 if (!acpi_gbl_reduced_hardware)
553 return false;
555 efi_reboot_quirk_mode = EFI_RESET_WARM;
556 return true;
559 bool efi_poweroff_required(void)
561 return acpi_gbl_reduced_hardware || acpi_no_s5;
564 #ifdef CONFIG_EFI_CAPSULE_QUIRK_QUARK_CSH
566 static int qrk_capsule_setup_info(struct capsule_info *cap_info, void **pkbuff,
567 size_t hdr_bytes)
569 struct quark_security_header *csh = *pkbuff;
571 /* Only process data block that is larger than the security header */
572 if (hdr_bytes < sizeof(struct quark_security_header))
573 return 0;
575 if (csh->csh_signature != QUARK_CSH_SIGNATURE ||
576 csh->headersize != QUARK_SECURITY_HEADER_SIZE)
577 return 1;
579 /* Only process data block if EFI header is included */
580 if (hdr_bytes < QUARK_SECURITY_HEADER_SIZE +
581 sizeof(efi_capsule_header_t))
582 return 0;
584 pr_debug("Quark security header detected\n");
586 if (csh->rsvd_next_header != 0) {
587 pr_err("multiple Quark security headers not supported\n");
588 return -EINVAL;
591 *pkbuff += csh->headersize;
592 cap_info->total_size = csh->headersize;
595 * Update the first page pointer to skip over the CSH header.
597 cap_info->phys[0] += csh->headersize;
600 * cap_info->capsule should point at a virtual mapping of the entire
601 * capsule, starting at the capsule header. Our image has the Quark
602 * security header prepended, so we cannot rely on the default vmap()
603 * mapping created by the generic capsule code.
604 * Given that the Quark firmware does not appear to care about the
605 * virtual mapping, let's just point cap_info->capsule at our copy
606 * of the capsule header.
608 cap_info->capsule = &cap_info->header;
610 return 1;
613 #define ICPU(family, model, quirk_handler) \
614 { X86_VENDOR_INTEL, family, model, X86_FEATURE_ANY, \
615 (unsigned long)&quirk_handler }
617 static const struct x86_cpu_id efi_capsule_quirk_ids[] = {
618 ICPU(5, 9, qrk_capsule_setup_info), /* Intel Quark X1000 */
622 int efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
623 size_t hdr_bytes)
625 int (*quirk_handler)(struct capsule_info *, void **, size_t);
626 const struct x86_cpu_id *id;
627 int ret;
629 if (hdr_bytes < sizeof(efi_capsule_header_t))
630 return 0;
632 cap_info->total_size = 0;
634 id = x86_match_cpu(efi_capsule_quirk_ids);
635 if (id) {
637 * The quirk handler is supposed to return
638 * - a value > 0 if the setup should continue, after advancing
639 * kbuff as needed
640 * - 0 if not enough hdr_bytes are available yet
641 * - a negative error code otherwise
643 quirk_handler = (typeof(quirk_handler))id->driver_data;
644 ret = quirk_handler(cap_info, &kbuff, hdr_bytes);
645 if (ret <= 0)
646 return ret;
649 memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));
651 cap_info->total_size += cap_info->header.imagesize;
653 return __efi_capsule_setup_info(cap_info);
656 #endif