]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/platform/efi/quirks.c
efi/capsule-loader: Reinstate virtual capsule mapping
[mirror_ubuntu-artful-kernel.git] / arch / x86 / platform / efi / quirks.c
CommitLineData
26d7f65f
MF
1#define pr_fmt(fmt) "efi: " fmt
2
eeb9db09
ST
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>
44be28e9 12#include <linux/acpi.h>
d394f2d9 13#include <linux/dmi.h>
5520b7e7
IM
14
15#include <asm/e820/api.h>
eeb9db09
ST
16#include <asm/efi.h>
17#include <asm/uv/uv.h>
2959c95d 18#include <asm/cpu_device_id.h>
eeb9db09
ST
19
20#define EFI_MIN_RESERVE 5120
21
22#define EFI_DUMMY_GUID \
23 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
24
2959c95d
JK
25#define QUARK_CSH_SIGNATURE 0x5f435348 /* _CSH */
26#define QUARK_SECURITY_HEADER_SIZE 0x400
27
28/*
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.
55 *
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
59 */
60struct 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];
76};
77
eeb9db09
ST
78static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
79
80static bool efi_no_storage_paranoia;
81
82/*
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.
88 *
89 * Enabling this option disables the space checks in
90 * efi_query_variable_store() and forces garbage collection.
91 *
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.
95 */
96static int __init setup_storage_paranoia(char *arg)
97{
98 efi_no_storage_paranoia = true;
99 return 0;
100}
101early_param("efi_no_storage_paranoia", setup_storage_paranoia);
102
103/*
104 * Deleting the dummy variable which kicks off garbage collection
105*/
106void efi_delete_dummy_variable(void)
107{
108 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
109 EFI_VARIABLE_NON_VOLATILE |
110 EFI_VARIABLE_BOOTSERVICE_ACCESS |
111 EFI_VARIABLE_RUNTIME_ACCESS,
112 0, NULL);
113}
114
ca0e30dc
AB
115/*
116 * In the nonblocking case we do not attempt to perform garbage
117 * collection if we do not have enough free space. Rather, we do the
118 * bare minimum check and give up immediately if the available space
119 * is below EFI_MIN_RESERVE.
120 *
121 * This function is intended to be small and simple because it is
122 * invoked from crash handler paths.
123 */
124static efi_status_t
125query_variable_store_nonblocking(u32 attributes, unsigned long size)
126{
127 efi_status_t status;
128 u64 storage_size, remaining_size, max_size;
129
130 status = efi.query_variable_info_nonblocking(attributes, &storage_size,
131 &remaining_size,
132 &max_size);
133 if (status != EFI_SUCCESS)
134 return status;
135
136 if (remaining_size - size < EFI_MIN_RESERVE)
137 return EFI_OUT_OF_RESOURCES;
138
139 return EFI_SUCCESS;
140}
141
eeb9db09
ST
142/*
143 * Some firmware implementations refuse to boot if there's insufficient space
144 * in the variable store. Ensure that we never use more than a safe limit.
145 *
146 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
147 * store.
148 */
ca0e30dc
AB
149efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
150 bool nonblocking)
eeb9db09
ST
151{
152 efi_status_t status;
153 u64 storage_size, remaining_size, max_size;
154
155 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
156 return 0;
157
ca0e30dc
AB
158 if (nonblocking)
159 return query_variable_store_nonblocking(attributes, size);
160
eeb9db09
ST
161 status = efi.query_variable_info(attributes, &storage_size,
162 &remaining_size, &max_size);
163 if (status != EFI_SUCCESS)
164 return status;
165
166 /*
167 * We account for that by refusing the write if permitting it would
168 * reduce the available space to under 5KB. This figure was provided by
169 * Samsung, so should be safe.
170 */
171 if ((remaining_size - size < EFI_MIN_RESERVE) &&
172 !efi_no_storage_paranoia) {
173
174 /*
175 * Triggering garbage collection may require that the firmware
176 * generate a real EFI_OUT_OF_RESOURCES error. We can force
177 * that by attempting to use more space than is available.
178 */
179 unsigned long dummy_size = remaining_size + 1024;
180 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
181
182 if (!dummy)
183 return EFI_OUT_OF_RESOURCES;
184
185 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
186 EFI_VARIABLE_NON_VOLATILE |
187 EFI_VARIABLE_BOOTSERVICE_ACCESS |
188 EFI_VARIABLE_RUNTIME_ACCESS,
189 dummy_size, dummy);
190
191 if (status == EFI_SUCCESS) {
192 /*
193 * This should have failed, so if it didn't make sure
194 * that we delete it...
195 */
196 efi_delete_dummy_variable();
197 }
198
199 kfree(dummy);
200
201 /*
202 * The runtime code may now have triggered a garbage collection
203 * run, so check the variable info again
204 */
205 status = efi.query_variable_info(attributes, &storage_size,
206 &remaining_size, &max_size);
207
208 if (status != EFI_SUCCESS)
209 return status;
210
211 /*
212 * There still isn't enough room, so return an error
213 */
214 if (remaining_size - size < EFI_MIN_RESERVE)
215 return EFI_OUT_OF_RESOURCES;
216 }
217
218 return EFI_SUCCESS;
219}
220EXPORT_SYMBOL_GPL(efi_query_variable_store);
221
816e7612
MF
222/*
223 * The UEFI specification makes it clear that the operating system is
224 * free to do whatever it wants with boot services code after
225 * ExitBootServices() has been called. Ignoring this recommendation a
226 * significant bunch of EFI implementations continue calling into boot
227 * services code (SetVirtualAddressMap). In order to work around such
228 * buggy implementations we reserve boot services region during EFI
229 * init and make sure it stays executable. Then, after
230 * SetVirtualAddressMap(), it is discarded.
231 *
232 * However, some boot services regions contain data that is required
233 * by drivers, so we need to track which memory ranges can never be
234 * freed. This is done by tagging those regions with the
235 * EFI_MEMORY_RUNTIME attribute.
236 *
237 * Any driver that wants to mark a region as reserved must use
238 * efi_mem_reserve() which will insert a new EFI memory descriptor
239 * into efi.memmap (splitting existing regions if necessary) and tag
240 * it with EFI_MEMORY_RUNTIME.
241 */
242void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)
243{
244 phys_addr_t new_phys, new_size;
245 struct efi_mem_range mr;
246 efi_memory_desc_t md;
247 int num_entries;
248 void *new;
249
250 if (efi_mem_desc_lookup(addr, &md)) {
251 pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);
252 return;
253 }
254
255 if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {
256 pr_err("Region spans EFI memory descriptors, %pa\n", &addr);
257 return;
258 }
259
6f6266a5
OS
260 /* No need to reserve regions that will never be freed. */
261 if (md.attribute & EFI_MEMORY_RUNTIME)
262 return;
263
92dc3350
MF
264 size += addr % EFI_PAGE_SIZE;
265 size = round_up(size, EFI_PAGE_SIZE);
266 addr = round_down(addr, EFI_PAGE_SIZE);
267
816e7612 268 mr.range.start = addr;
92dc3350 269 mr.range.end = addr + size - 1;
816e7612
MF
270 mr.attribute = md.attribute | EFI_MEMORY_RUNTIME;
271
272 num_entries = efi_memmap_split_count(&md, &mr.range);
273 num_entries += efi.memmap.nr_map;
274
275 new_size = efi.memmap.desc_size * num_entries;
276
20b1e22d 277 new_phys = efi_memmap_alloc(num_entries);
816e7612
MF
278 if (!new_phys) {
279 pr_err("Could not allocate boot services memmap\n");
280 return;
281 }
282
283 new = early_memremap(new_phys, new_size);
284 if (!new) {
285 pr_err("Failed to map new boot services memmap\n");
286 return;
287 }
288
289 efi_memmap_insert(&efi.memmap, new, &mr);
290 early_memunmap(new, new_size);
291
292 efi_memmap_install(new_phys, num_entries);
293}
294
452308de
MF
295/*
296 * Helper function for efi_reserve_boot_services() to figure out if we
297 * can free regions in efi_free_boot_services().
298 *
299 * Use this function to ensure we do not free regions owned by somebody
300 * else. We must only reserve (and then free) regions:
301 *
302 * - Not within any part of the kernel
09821ff1 303 * - Not the BIOS reserved area (E820_TYPE_RESERVED, E820_TYPE_NVS, etc)
452308de
MF
304 */
305static bool can_free_region(u64 start, u64 size)
306{
307 if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
308 return false;
309
09821ff1 310 if (!e820__mapped_all(start, start+size, E820_TYPE_RAM))
452308de
MF
311 return false;
312
313 return true;
314}
315
eeb9db09
ST
316void __init efi_reserve_boot_services(void)
317{
78ce248f 318 efi_memory_desc_t *md;
eeb9db09 319
78ce248f 320 for_each_efi_memory_desc(md) {
eeb9db09
ST
321 u64 start = md->phys_addr;
322 u64 size = md->num_pages << EFI_PAGE_SHIFT;
452308de 323 bool already_reserved;
eeb9db09
ST
324
325 if (md->type != EFI_BOOT_SERVICES_CODE &&
326 md->type != EFI_BOOT_SERVICES_DATA)
327 continue;
452308de
MF
328
329 already_reserved = memblock_is_region_reserved(start, size);
330
331 /*
332 * Because the following memblock_reserve() is paired
333 * with free_bootmem_late() for this region in
334 * efi_free_boot_services(), we must be extremely
335 * careful not to reserve, and subsequently free,
336 * critical regions of memory (like the kernel image) or
337 * those regions that somebody else has already
338 * reserved.
339 *
340 * A good example of a critical region that must not be
341 * freed is page zero (first 4Kb of memory), which may
342 * contain boot services code/data but is marked
09821ff1 343 * E820_TYPE_RESERVED by trim_bios_range().
452308de
MF
344 */
345 if (!already_reserved) {
eeb9db09 346 memblock_reserve(start, size);
452308de
MF
347
348 /*
349 * If we are the first to reserve the region, no
350 * one else cares about it. We own it and can
351 * free it later.
352 */
353 if (can_free_region(start, size))
354 continue;
355 }
356
357 /*
358 * We don't own the region. We must not free it.
359 *
360 * Setting this bit for a boot services region really
361 * doesn't make sense as far as the firmware is
362 * concerned, but it does provide us with a way to tag
363 * those regions that must not be paired with
364 * free_bootmem_late().
365 */
366 md->attribute |= EFI_MEMORY_RUNTIME;
eeb9db09
ST
367 }
368}
369
370void __init efi_free_boot_services(void)
371{
816e7612 372 phys_addr_t new_phys, new_size;
78ce248f 373 efi_memory_desc_t *md;
816e7612
MF
374 int num_entries = 0;
375 void *new, *new_md;
eeb9db09 376
78ce248f 377 for_each_efi_memory_desc(md) {
eeb9db09
ST
378 unsigned long long start = md->phys_addr;
379 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
5bc653b7 380 size_t rm_size;
eeb9db09
ST
381
382 if (md->type != EFI_BOOT_SERVICES_CODE &&
816e7612
MF
383 md->type != EFI_BOOT_SERVICES_DATA) {
384 num_entries++;
eeb9db09 385 continue;
816e7612 386 }
eeb9db09 387
452308de 388 /* Do not free, someone else owns it: */
816e7612
MF
389 if (md->attribute & EFI_MEMORY_RUNTIME) {
390 num_entries++;
eeb9db09 391 continue;
816e7612 392 }
eeb9db09 393
5bc653b7
AL
394 /*
395 * Nasty quirk: if all sub-1MB memory is used for boot
396 * services, we can get here without having allocated the
397 * real mode trampoline. It's too late to hand boot services
398 * memory back to the memblock allocator, so instead
399 * try to manually allocate the trampoline if needed.
400 *
401 * I've seen this on a Dell XPS 13 9350 with firmware
402 * 1.4.4 with SGX enabled booting Linux via Fedora 24's
403 * grub2-efi on a hard disk. (And no, I don't know why
404 * this happened, but Linux should still try to boot rather
405 * panicing early.)
406 */
407 rm_size = real_mode_size_needed();
408 if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
409 set_real_mode_mem(start, rm_size);
410 start += rm_size;
411 size -= rm_size;
412 }
413
eeb9db09
ST
414 free_bootmem_late(start, size);
415 }
816e7612 416
1ea34adb
JG
417 if (!num_entries)
418 return;
419
816e7612 420 new_size = efi.memmap.desc_size * num_entries;
20b1e22d 421 new_phys = efi_memmap_alloc(num_entries);
816e7612
MF
422 if (!new_phys) {
423 pr_err("Failed to allocate new EFI memmap\n");
424 return;
425 }
426
427 new = memremap(new_phys, new_size, MEMREMAP_WB);
428 if (!new) {
429 pr_err("Failed to map new EFI memmap\n");
430 return;
431 }
432
433 /*
434 * Build a new EFI memmap that excludes any boot services
435 * regions that are not tagged EFI_MEMORY_RUNTIME, since those
436 * regions have now been freed.
437 */
438 new_md = new;
439 for_each_efi_memory_desc(md) {
440 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
441 (md->type == EFI_BOOT_SERVICES_CODE ||
442 md->type == EFI_BOOT_SERVICES_DATA))
443 continue;
444
445 memcpy(new_md, md, efi.memmap.desc_size);
446 new_md += efi.memmap.desc_size;
447 }
448
449 memunmap(new);
450
451 if (efi_memmap_install(new_phys, num_entries)) {
452 pr_err("Could not install new EFI memmap\n");
453 return;
454 }
eeb9db09
ST
455}
456
457/*
458 * A number of config table entries get remapped to virtual addresses
459 * after entering EFI virtual mode. However, the kexec kernel requires
460 * their physical addresses therefore we pass them via setup_data and
461 * correct those entries to their respective physical addresses here.
462 *
463 * Currently only handles smbios which is necessary for some firmware
464 * implementation.
465 */
466int __init efi_reuse_config(u64 tables, int nr_tables)
467{
468 int i, sz, ret = 0;
469 void *p, *tablep;
470 struct efi_setup_data *data;
471
472 if (!efi_setup)
473 return 0;
474
475 if (!efi_enabled(EFI_64BIT))
476 return 0;
477
478 data = early_memremap(efi_setup, sizeof(*data));
479 if (!data) {
480 ret = -ENOMEM;
481 goto out;
482 }
483
484 if (!data->smbios)
485 goto out_memremap;
486
487 sz = sizeof(efi_config_table_64_t);
488
489 p = tablep = early_memremap(tables, nr_tables * sz);
490 if (!p) {
491 pr_err("Could not map Configuration table!\n");
492 ret = -ENOMEM;
493 goto out_memremap;
494 }
495
496 for (i = 0; i < efi.systab->nr_tables; i++) {
497 efi_guid_t guid;
498
499 guid = ((efi_config_table_64_t *)p)->guid;
500
501 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
502 ((efi_config_table_64_t *)p)->table = data->smbios;
503 p += sz;
504 }
98a716b6 505 early_memunmap(tablep, nr_tables * sz);
eeb9db09
ST
506
507out_memremap:
98a716b6 508 early_memunmap(data, sizeof(*data));
eeb9db09
ST
509out:
510 return ret;
511}
512
d394f2d9
AT
513static const struct dmi_system_id sgi_uv1_dmi[] = {
514 { NULL, "SGI UV1",
515 { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"),
516 DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
517 DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"),
518 }
519 },
520 { } /* NULL entry stops DMI scanning */
521};
522
eeb9db09
ST
523void __init efi_apply_memmap_quirks(void)
524{
525 /*
526 * Once setup is done earlier, unmap the EFI memory map on mismatched
527 * firmware/kernel architectures since there is no support for runtime
528 * services.
529 */
530 if (!efi_runtime_supported()) {
26d7f65f 531 pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
9479c7ce 532 efi_memmap_unmap();
eeb9db09
ST
533 }
534
d394f2d9
AT
535 /* UV2+ BIOS has a fix for this issue. UV1 still needs the quirk. */
536 if (dmi_check_system(sgi_uv1_dmi))
eeb9db09
ST
537 set_bit(EFI_OLD_MEMMAP, &efi.flags);
538}
44be28e9
MF
539
540/*
541 * For most modern platforms the preferred method of powering off is via
542 * ACPI. However, there are some that are known to require the use of
543 * EFI runtime services and for which ACPI does not work at all.
544 *
545 * Using EFI is a last resort, to be used only if no other option
546 * exists.
547 */
548bool efi_reboot_required(void)
549{
550 if (!acpi_gbl_reduced_hardware)
551 return false;
552
553 efi_reboot_quirk_mode = EFI_RESET_WARM;
554 return true;
555}
556
557bool efi_poweroff_required(void)
558{
13737181 559 return acpi_gbl_reduced_hardware || acpi_no_s5;
44be28e9 560}
2959c95d
JK
561
562#ifdef CONFIG_EFI_CAPSULE_QUIRK_QUARK_CSH
563
564static int qrk_capsule_setup_info(struct capsule_info *cap_info, void **pkbuff,
565 size_t hdr_bytes)
566{
567 struct quark_security_header *csh = *pkbuff;
568
569 /* Only process data block that is larger than the security header */
570 if (hdr_bytes < sizeof(struct quark_security_header))
571 return 0;
572
573 if (csh->csh_signature != QUARK_CSH_SIGNATURE ||
574 csh->headersize != QUARK_SECURITY_HEADER_SIZE)
575 return 1;
576
577 /* Only process data block if EFI header is included */
578 if (hdr_bytes < QUARK_SECURITY_HEADER_SIZE +
579 sizeof(efi_capsule_header_t))
580 return 0;
581
582 pr_debug("Quark security header detected\n");
583
584 if (csh->rsvd_next_header != 0) {
585 pr_err("multiple Quark security headers not supported\n");
586 return -EINVAL;
587 }
588
589 *pkbuff += csh->headersize;
590 cap_info->total_size = csh->headersize;
591
592 /*
593 * Update the first page pointer to skip over the CSH header.
594 */
20b16a3f
AB
595 cap_info->phys[0] += csh->headersize;
596
597 /*
598 * cap_info->capsule should point at a virtual mapping of the entire
599 * capsule, starting at the capsule header. Our image has the Quark
600 * security header prepended, so we cannot rely on the default vmap()
601 * mapping created by the generic capsule code.
602 * Given that the Quark firmware does not appear to care about the
603 * virtual mapping, let's just point cap_info->capsule at our copy
604 * of the capsule header.
605 */
606 cap_info->capsule = &cap_info->header;
2959c95d
JK
607
608 return 1;
609}
610
611#define ICPU(family, model, quirk_handler) \
612 { X86_VENDOR_INTEL, family, model, X86_FEATURE_ANY, \
613 (unsigned long)&quirk_handler }
614
615static const struct x86_cpu_id efi_capsule_quirk_ids[] = {
616 ICPU(5, 9, qrk_capsule_setup_info), /* Intel Quark X1000 */
617 { }
618};
619
620int efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
621 size_t hdr_bytes)
622{
623 int (*quirk_handler)(struct capsule_info *, void **, size_t);
624 const struct x86_cpu_id *id;
625 int ret;
626
627 if (hdr_bytes < sizeof(efi_capsule_header_t))
628 return 0;
629
630 cap_info->total_size = 0;
631
632 id = x86_match_cpu(efi_capsule_quirk_ids);
633 if (id) {
634 /*
635 * The quirk handler is supposed to return
636 * - a value > 0 if the setup should continue, after advancing
637 * kbuff as needed
638 * - 0 if not enough hdr_bytes are available yet
639 * - a negative error code otherwise
640 */
641 quirk_handler = (typeof(quirk_handler))id->driver_data;
642 ret = quirk_handler(cap_info, &kbuff, hdr_bytes);
643 if (ret <= 0)
644 return ret;
645 }
646
647 memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));
648
649 cap_info->total_size += cap_info->header.imagesize;
650
651 return __efi_capsule_setup_info(cap_info);
652}
653
654#endif