1
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
28#define QUARK_SECURITY_HEADER_SIZE 0x400
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62struct quark_security_header {
63 u32 csh_signature;
64 u32 version;
65 u32 modulesize;
66 u32 security_version_number_index;
67 u32 security_version_number;
68 u32 rsvd_module_id;
69 u32 rsvd_module_vendor;
70 u32 rsvd_date;
71 u32 headersize;
72 u32 hash_algo;
73 u32 cryp_algo;
74 u32 keysize;
75 u32 signaturesize;
76 u32 rsvd_next_header;
77 u32 rsvd[2];
78};
79
80static const efi_char16_t efi_dummy_name[] = L"DUMMY";
81
82static bool efi_no_storage_paranoia;
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98static int __init setup_storage_paranoia(char *arg)
99{
100 efi_no_storage_paranoia = true;
101 return 0;
102}
103early_param("efi_no_storage_paranoia", setup_storage_paranoia);
104
105
106
107
108void 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
118
119
120
121
122
123
124
125
126static efi_status_t
127query_variable_store_nonblocking(u32 attributes, unsigned long size)
128{
129 efi_status_t status;
130 u64 storage_size, remaining_size, max_size;
131
132 status = efi.query_variable_info_nonblocking(attributes, &storage_size,
133 &remaining_size,
134 &max_size);
135 if (status != EFI_SUCCESS)
136 return status;
137
138 if (remaining_size - size < EFI_MIN_RESERVE)
139 return EFI_OUT_OF_RESOURCES;
140
141 return EFI_SUCCESS;
142}
143
144
145
146
147
148
149
150
151efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
152 bool nonblocking)
153{
154 efi_status_t status;
155 u64 storage_size, remaining_size, max_size;
156
157 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
158 return 0;
159
160 if (nonblocking)
161 return query_variable_store_nonblocking(attributes, size);
162
163 status = efi.query_variable_info(attributes, &storage_size,
164 &remaining_size, &max_size);
165 if (status != EFI_SUCCESS)
166 return status;
167
168
169
170
171
172
173 if ((remaining_size - size < EFI_MIN_RESERVE) &&
174 !efi_no_storage_paranoia) {
175
176
177
178
179
180
181 unsigned long dummy_size = remaining_size + 1024;
182 void *dummy = kzalloc(dummy_size, GFP_KERNEL);
183
184 if (!dummy)
185 return EFI_OUT_OF_RESOURCES;
186
187 status = efi.set_variable((efi_char16_t *)efi_dummy_name,
188 &EFI_DUMMY_GUID,
189 EFI_VARIABLE_NON_VOLATILE |
190 EFI_VARIABLE_BOOTSERVICE_ACCESS |
191 EFI_VARIABLE_RUNTIME_ACCESS,
192 dummy_size, dummy);
193
194 if (status == EFI_SUCCESS) {
195
196
197
198
199 efi_delete_dummy_variable();
200 }
201
202 kfree(dummy);
203
204
205
206
207
208 status = efi.query_variable_info(attributes, &storage_size,
209 &remaining_size, &max_size);
210
211 if (status != EFI_SUCCESS)
212 return status;
213
214
215
216
217 if (remaining_size - size < EFI_MIN_RESERVE)
218 return EFI_OUT_OF_RESOURCES;
219 }
220
221 return EFI_SUCCESS;
222}
223EXPORT_SYMBOL_GPL(efi_query_variable_store);
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)
246{
247 struct efi_memory_map_data data = { 0 };
248 struct efi_mem_range mr;
249 efi_memory_desc_t md;
250 int num_entries;
251 void *new;
252
253 if (efi_mem_desc_lookup(addr, &md) ||
254 md.type != EFI_BOOT_SERVICES_DATA) {
255 pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);
256 return;
257 }
258
259 if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {
260 pr_err("Region spans EFI memory descriptors, %pa\n", &addr);
261 return;
262 }
263
264 size += addr % EFI_PAGE_SIZE;
265 size = round_up(size, EFI_PAGE_SIZE);
266 addr = round_down(addr, EFI_PAGE_SIZE);
267
268 mr.range.start = addr;
269 mr.range.end = addr + size - 1;
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 if (efi_memmap_alloc(num_entries, &data) != 0) {
276 pr_err("Could not allocate boot services memmap\n");
277 return;
278 }
279
280 new = early_memremap(data.phys_map, data.size);
281 if (!new) {
282 pr_err("Failed to map new boot services memmap\n");
283 return;
284 }
285
286 efi_memmap_insert(&efi.memmap, new, &mr);
287 early_memunmap(new, data.size);
288
289 efi_memmap_install(&data);
290 e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
291 e820__update_table(e820_table);
292}
293
294
295
296
297
298
299
300
301
302
303
304static __init bool can_free_region(u64 start, u64 size)
305{
306 if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
307 return false;
308
309 if (!e820__mapped_all(start, start+size, E820_TYPE_RAM))
310 return false;
311
312 return true;
313}
314
315void __init efi_reserve_boot_services(void)
316{
317 efi_memory_desc_t *md;
318
319 if (!efi_enabled(EFI_MEMMAP))
320 return;
321
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;
326
327 if (md->type != EFI_BOOT_SERVICES_CODE &&
328 md->type != EFI_BOOT_SERVICES_DATA)
329 continue;
330
331 already_reserved = memblock_is_region_reserved(start, size);
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347 if (!already_reserved) {
348 memblock_reserve(start, size);
349
350
351
352
353
354
355 if (can_free_region(start, size))
356 continue;
357 }
358
359
360
361
362
363
364
365
366
367
368 md->attribute |= EFI_MEMORY_RUNTIME;
369 }
370}
371
372
373
374
375
376
377static void __init efi_unmap_pages(efi_memory_desc_t *md)
378{
379 pgd_t *pgd = efi_mm.pgd;
380 u64 pa = md->phys_addr;
381 u64 va = md->virt_addr;
382
383
384
385
386
387
388 if (efi_have_uv1_memmap())
389 return;
390
391
392
393
394
395
396 if (efi_is_mixed())
397 return;
398
399 if (kernel_unmap_pages_in_pgd(pgd, pa, md->num_pages))
400 pr_err("Failed to unmap 1:1 mapping for 0x%llx\n", pa);
401
402 if (kernel_unmap_pages_in_pgd(pgd, va, md->num_pages))
403 pr_err("Failed to unmap VA mapping for 0x%llx\n", va);
404}
405
406void __init efi_free_boot_services(void)
407{
408 struct efi_memory_map_data data = { 0 };
409 efi_memory_desc_t *md;
410 int num_entries = 0;
411 void *new, *new_md;
412
413
414 if (efi_enabled(EFI_DBG))
415 return;
416
417 for_each_efi_memory_desc(md) {
418 unsigned long long start = md->phys_addr;
419 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
420 size_t rm_size;
421
422 if (md->type != EFI_BOOT_SERVICES_CODE &&
423 md->type != EFI_BOOT_SERVICES_DATA) {
424 num_entries++;
425 continue;
426 }
427
428
429 if (md->attribute & EFI_MEMORY_RUNTIME) {
430 num_entries++;
431 continue;
432 }
433
434
435
436
437
438
439 efi_unmap_pages(md);
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454 rm_size = real_mode_size_needed();
455 if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
456 set_real_mode_mem(start);
457 start += rm_size;
458 size -= rm_size;
459 }
460
461 memblock_free_late(start, size);
462 }
463
464 if (!num_entries)
465 return;
466
467 if (efi_memmap_alloc(num_entries, &data) != 0) {
468 pr_err("Failed to allocate new EFI memmap\n");
469 return;
470 }
471
472 new = memremap(data.phys_map, data.size, MEMREMAP_WB);
473 if (!new) {
474 pr_err("Failed to map new EFI memmap\n");
475 return;
476 }
477
478
479
480
481
482
483 new_md = new;
484 for_each_efi_memory_desc(md) {
485 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
486 (md->type == EFI_BOOT_SERVICES_CODE ||
487 md->type == EFI_BOOT_SERVICES_DATA))
488 continue;
489
490 memcpy(new_md, md, efi.memmap.desc_size);
491 new_md += efi.memmap.desc_size;
492 }
493
494 memunmap(new);
495
496 if (efi_memmap_install(&data) != 0) {
497 pr_err("Could not install new EFI memmap\n");
498 return;
499 }
500}
501
502
503
504
505
506
507
508
509
510
511int __init efi_reuse_config(u64 tables, int nr_tables)
512{
513 int i, sz, ret = 0;
514 void *p, *tablep;
515 struct efi_setup_data *data;
516
517 if (nr_tables == 0)
518 return 0;
519
520 if (!efi_setup)
521 return 0;
522
523 if (!efi_enabled(EFI_64BIT))
524 return 0;
525
526 data = early_memremap(efi_setup, sizeof(*data));
527 if (!data) {
528 ret = -ENOMEM;
529 goto out;
530 }
531
532 if (!data->smbios)
533 goto out_memremap;
534
535 sz = sizeof(efi_config_table_64_t);
536
537 p = tablep = early_memremap(tables, nr_tables * sz);
538 if (!p) {
539 pr_err("Could not map Configuration table!\n");
540 ret = -ENOMEM;
541 goto out_memremap;
542 }
543
544 for (i = 0; i < nr_tables; i++) {
545 efi_guid_t guid;
546
547 guid = ((efi_config_table_64_t *)p)->guid;
548
549 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
550 ((efi_config_table_64_t *)p)->table = data->smbios;
551 p += sz;
552 }
553 early_memunmap(tablep, nr_tables * sz);
554
555out_memremap:
556 early_memunmap(data, sizeof(*data));
557out:
558 return ret;
559}
560
561static const struct dmi_system_id sgi_uv1_dmi[] __initconst = {
562 { NULL, "SGI UV1",
563 { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"),
564 DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"),
565 DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"),
566 }
567 },
568 { }
569};
570
571void __init efi_apply_memmap_quirks(void)
572{
573
574
575
576
577
578 if (!efi_runtime_supported()) {
579 pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
580 efi_memmap_unmap();
581 }
582
583
584 if (dmi_check_system(sgi_uv1_dmi)) {
585 if (IS_ENABLED(CONFIG_X86_UV)) {
586 set_bit(EFI_UV1_MEMMAP, &efi.flags);
587 } else {
588 pr_warn("EFI runtime disabled, needs CONFIG_X86_UV=y on UV1\n");
589 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
590 efi_memmap_unmap();
591 }
592 }
593}
594
595
596
597
598
599
600
601
602
603bool efi_reboot_required(void)
604{
605 if (!acpi_gbl_reduced_hardware)
606 return false;
607
608 efi_reboot_quirk_mode = EFI_RESET_WARM;
609 return true;
610}
611
612bool efi_poweroff_required(void)
613{
614 return acpi_gbl_reduced_hardware || acpi_no_s5;
615}
616
617#ifdef CONFIG_EFI_CAPSULE_QUIRK_QUARK_CSH
618
619static int qrk_capsule_setup_info(struct capsule_info *cap_info, void **pkbuff,
620 size_t hdr_bytes)
621{
622 struct quark_security_header *csh = *pkbuff;
623
624
625 if (hdr_bytes < sizeof(struct quark_security_header))
626 return 0;
627
628 if (csh->csh_signature != QUARK_CSH_SIGNATURE ||
629 csh->headersize != QUARK_SECURITY_HEADER_SIZE)
630 return 1;
631
632
633 if (hdr_bytes < QUARK_SECURITY_HEADER_SIZE +
634 sizeof(efi_capsule_header_t))
635 return 0;
636
637 pr_debug("Quark security header detected\n");
638
639 if (csh->rsvd_next_header != 0) {
640 pr_err("multiple Quark security headers not supported\n");
641 return -EINVAL;
642 }
643
644 *pkbuff += csh->headersize;
645 cap_info->total_size = csh->headersize;
646
647
648
649
650 cap_info->phys[0] += csh->headersize;
651
652
653
654
655
656
657
658
659
660
661 cap_info->capsule = &cap_info->header;
662
663 return 1;
664}
665
666static const struct x86_cpu_id efi_capsule_quirk_ids[] = {
667 X86_MATCH_VENDOR_FAM_MODEL(INTEL, 5, INTEL_FAM5_QUARK_X1000,
668 &qrk_capsule_setup_info),
669 { }
670};
671
672int efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
673 size_t hdr_bytes)
674{
675 int (*quirk_handler)(struct capsule_info *, void **, size_t);
676 const struct x86_cpu_id *id;
677 int ret;
678
679 if (hdr_bytes < sizeof(efi_capsule_header_t))
680 return 0;
681
682 cap_info->total_size = 0;
683
684 id = x86_match_cpu(efi_capsule_quirk_ids);
685 if (id) {
686
687
688
689
690
691
692
693 quirk_handler = (typeof(quirk_handler))id->driver_data;
694 ret = quirk_handler(cap_info, &kbuff, hdr_bytes);
695 if (ret <= 0)
696 return ret;
697 }
698
699 memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));
700
701 cap_info->total_size += cap_info->header.imagesize;
702
703 return __efi_capsule_setup_info(cap_info);
704}
705
706#endif
707
708
709
710
711
712
713
714
715
716
717
718
719void efi_recover_from_page_fault(unsigned long phys_addr)
720{
721 if (!IS_ENABLED(CONFIG_X86_64))
722 return;
723
724
725
726
727
728
729 if (efi_rts_work.efi_rts_id == EFI_NONE)
730 return;
731
732
733
734
735
736 if (phys_addr <= 0x0fff)
737 return;
738
739
740
741
742
743 WARN(1, FW_BUG "Page fault caused by firmware at PA: 0x%lx\n",
744 phys_addr);
745
746
747
748
749
750
751
752
753
754 if (efi_rts_work.efi_rts_id == EFI_RESET_SYSTEM) {
755 pr_info("efi_reset_system() buggy! Reboot through BIOS\n");
756 machine_real_restart(MRR_BIOS);
757 return;
758 }
759
760
761
762
763
764 arch_efi_call_virt_teardown();
765
766
767 efi_rts_work.status = EFI_ABORTED;
768 complete(&efi_rts_work.efi_rts_comp);
769
770 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
771 pr_info("Froze efi_rts_wq and disabled EFI Runtime Services\n");
772
773
774
775
776
777 for (;;) {
778 set_current_state(TASK_IDLE);
779 schedule();
780 }
781
782 return;
783}
784