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