0001
0002 #define pr_fmt(fmt) "efi: " fmt
0003
0004 #include <linux/init.h>
0005 #include <linux/kernel.h>
0006 #include <linux/string.h>
0007 #include <linux/time.h>
0008 #include <linux/types.h>
0009 #include <linux/efi.h>
0010 #include <linux/slab.h>
0011 #include <linux/memblock.h>
0012 #include <linux/acpi.h>
0013 #include <linux/dmi.h>
0014
0015 #include <asm/e820/api.h>
0016 #include <asm/efi.h>
0017 #include <asm/uv/uv.h>
0018 #include <asm/cpu_device_id.h>
0019 #include <asm/realmode.h>
0020 #include <asm/reboot.h>
0021
0022 #define EFI_MIN_RESERVE 5120
0023
0024 #define EFI_DUMMY_GUID \
0025 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
0026
0027 #define QUARK_CSH_SIGNATURE 0x5f435348
0028 #define QUARK_SECURITY_HEADER_SIZE 0x400
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062 struct quark_security_header {
0063 u32 csh_signature;
0064 u32 version;
0065 u32 modulesize;
0066 u32 security_version_number_index;
0067 u32 security_version_number;
0068 u32 rsvd_module_id;
0069 u32 rsvd_module_vendor;
0070 u32 rsvd_date;
0071 u32 headersize;
0072 u32 hash_algo;
0073 u32 cryp_algo;
0074 u32 keysize;
0075 u32 signaturesize;
0076 u32 rsvd_next_header;
0077 u32 rsvd[2];
0078 };
0079
0080 static const efi_char16_t efi_dummy_name[] = L"DUMMY";
0081
0082 static bool efi_no_storage_paranoia;
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 static int __init setup_storage_paranoia(char *arg)
0099 {
0100 efi_no_storage_paranoia = true;
0101 return 0;
0102 }
0103 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
0104
0105
0106
0107
0108 void efi_delete_dummy_variable(void)
0109 {
0110 efi.set_variable_nonblocking((efi_char16_t *)efi_dummy_name,
0111 &EFI_DUMMY_GUID,
0112 EFI_VARIABLE_NON_VOLATILE |
0113 EFI_VARIABLE_BOOTSERVICE_ACCESS |
0114 EFI_VARIABLE_RUNTIME_ACCESS, 0, NULL);
0115 }
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126 static efi_status_t
0127 query_variable_store_nonblocking(u32 attributes, unsigned long size)
0128 {
0129 efi_status_t status;
0130 u64 storage_size, remaining_size, max_size;
0131
0132 status = efi.query_variable_info_nonblocking(attributes, &storage_size,
0133 &remaining_size,
0134 &max_size);
0135 if (status != EFI_SUCCESS)
0136 return status;
0137
0138 if (remaining_size - size < EFI_MIN_RESERVE)
0139 return EFI_OUT_OF_RESOURCES;
0140
0141 return EFI_SUCCESS;
0142 }
0143
0144
0145
0146
0147
0148
0149
0150
0151 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
0152 bool nonblocking)
0153 {
0154 efi_status_t status;
0155 u64 storage_size, remaining_size, max_size;
0156
0157 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
0158 return 0;
0159
0160 if (nonblocking)
0161 return query_variable_store_nonblocking(attributes, size);
0162
0163 status = efi.query_variable_info(attributes, &storage_size,
0164 &remaining_size, &max_size);
0165 if (status != EFI_SUCCESS)
0166 return status;
0167
0168
0169
0170
0171
0172
0173 if ((remaining_size - size < EFI_MIN_RESERVE) &&
0174 !efi_no_storage_paranoia) {
0175
0176
0177
0178
0179
0180
0181 unsigned long dummy_size = remaining_size + 1024;
0182 void *dummy = kzalloc(dummy_size, GFP_KERNEL);
0183
0184 if (!dummy)
0185 return EFI_OUT_OF_RESOURCES;
0186
0187 status = efi.set_variable((efi_char16_t *)efi_dummy_name,
0188 &EFI_DUMMY_GUID,
0189 EFI_VARIABLE_NON_VOLATILE |
0190 EFI_VARIABLE_BOOTSERVICE_ACCESS |
0191 EFI_VARIABLE_RUNTIME_ACCESS,
0192 dummy_size, dummy);
0193
0194 if (status == EFI_SUCCESS) {
0195
0196
0197
0198
0199 efi_delete_dummy_variable();
0200 }
0201
0202 kfree(dummy);
0203
0204
0205
0206
0207
0208 status = efi.query_variable_info(attributes, &storage_size,
0209 &remaining_size, &max_size);
0210
0211 if (status != EFI_SUCCESS)
0212 return status;
0213
0214
0215
0216
0217 if (remaining_size - size < EFI_MIN_RESERVE)
0218 return EFI_OUT_OF_RESOURCES;
0219 }
0220
0221 return EFI_SUCCESS;
0222 }
0223 EXPORT_SYMBOL_GPL(efi_query_variable_store);
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245 void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)
0246 {
0247 struct efi_memory_map_data data = { 0 };
0248 struct efi_mem_range mr;
0249 efi_memory_desc_t md;
0250 int num_entries;
0251 void *new;
0252
0253 if (efi_mem_desc_lookup(addr, &md) ||
0254 md.type != EFI_BOOT_SERVICES_DATA) {
0255 pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);
0256 return;
0257 }
0258
0259 if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {
0260 pr_err("Region spans EFI memory descriptors, %pa\n", &addr);
0261 return;
0262 }
0263
0264 size += addr % EFI_PAGE_SIZE;
0265 size = round_up(size, EFI_PAGE_SIZE);
0266 addr = round_down(addr, EFI_PAGE_SIZE);
0267
0268 mr.range.start = addr;
0269 mr.range.end = addr + size - 1;
0270 mr.attribute = md.attribute | EFI_MEMORY_RUNTIME;
0271
0272 num_entries = efi_memmap_split_count(&md, &mr.range);
0273 num_entries += efi.memmap.nr_map;
0274
0275 if (efi_memmap_alloc(num_entries, &data) != 0) {
0276 pr_err("Could not allocate boot services memmap\n");
0277 return;
0278 }
0279
0280 new = early_memremap_prot(data.phys_map, data.size,
0281 pgprot_val(pgprot_encrypted(FIXMAP_PAGE_NORMAL)));
0282 if (!new) {
0283 pr_err("Failed to map new boot services memmap\n");
0284 return;
0285 }
0286
0287 efi_memmap_insert(&efi.memmap, new, &mr);
0288 early_memunmap(new, data.size);
0289
0290 efi_memmap_install(&data);
0291 e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
0292 e820__update_table(e820_table);
0293 }
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305 static __init bool can_free_region(u64 start, u64 size)
0306 {
0307 if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
0308 return false;
0309
0310 if (!e820__mapped_all(start, start+size, E820_TYPE_RAM))
0311 return false;
0312
0313 return true;
0314 }
0315
0316 void __init efi_reserve_boot_services(void)
0317 {
0318 efi_memory_desc_t *md;
0319
0320 if (!efi_enabled(EFI_MEMMAP))
0321 return;
0322
0323 for_each_efi_memory_desc(md) {
0324 u64 start = md->phys_addr;
0325 u64 size = md->num_pages << EFI_PAGE_SHIFT;
0326 bool already_reserved;
0327
0328 if (md->type != EFI_BOOT_SERVICES_CODE &&
0329 md->type != EFI_BOOT_SERVICES_DATA)
0330 continue;
0331
0332 already_reserved = memblock_is_region_reserved(start, size);
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348 if (!already_reserved) {
0349 memblock_reserve(start, size);
0350
0351
0352
0353
0354
0355
0356 if (can_free_region(start, size))
0357 continue;
0358 }
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369 md->attribute |= EFI_MEMORY_RUNTIME;
0370 }
0371 }
0372
0373
0374
0375
0376
0377
0378 static void __init efi_unmap_pages(efi_memory_desc_t *md)
0379 {
0380 pgd_t *pgd = efi_mm.pgd;
0381 u64 pa = md->phys_addr;
0382 u64 va = md->virt_addr;
0383
0384
0385
0386
0387
0388
0389 if (efi_is_mixed())
0390 return;
0391
0392 if (kernel_unmap_pages_in_pgd(pgd, pa, md->num_pages))
0393 pr_err("Failed to unmap 1:1 mapping for 0x%llx\n", pa);
0394
0395 if (kernel_unmap_pages_in_pgd(pgd, va, md->num_pages))
0396 pr_err("Failed to unmap VA mapping for 0x%llx\n", va);
0397 }
0398
0399 void __init efi_free_boot_services(void)
0400 {
0401 struct efi_memory_map_data data = { 0 };
0402 efi_memory_desc_t *md;
0403 int num_entries = 0;
0404 void *new, *new_md;
0405
0406
0407 if (efi_enabled(EFI_DBG))
0408 return;
0409
0410 for_each_efi_memory_desc(md) {
0411 unsigned long long start = md->phys_addr;
0412 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
0413 size_t rm_size;
0414
0415 if (md->type != EFI_BOOT_SERVICES_CODE &&
0416 md->type != EFI_BOOT_SERVICES_DATA) {
0417 num_entries++;
0418 continue;
0419 }
0420
0421
0422 if (md->attribute & EFI_MEMORY_RUNTIME) {
0423 num_entries++;
0424 continue;
0425 }
0426
0427
0428
0429
0430
0431
0432 efi_unmap_pages(md);
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447 rm_size = real_mode_size_needed();
0448 if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
0449 set_real_mode_mem(start);
0450 start += rm_size;
0451 size -= rm_size;
0452 }
0453
0454
0455
0456
0457
0458
0459 if (start + size < SZ_1M)
0460 continue;
0461 if (start < SZ_1M) {
0462 size -= (SZ_1M - start);
0463 start = SZ_1M;
0464 }
0465
0466 memblock_free_late(start, size);
0467 }
0468
0469 if (!num_entries)
0470 return;
0471
0472 if (efi_memmap_alloc(num_entries, &data) != 0) {
0473 pr_err("Failed to allocate new EFI memmap\n");
0474 return;
0475 }
0476
0477 new = memremap(data.phys_map, data.size, MEMREMAP_WB);
0478 if (!new) {
0479 pr_err("Failed to map new EFI memmap\n");
0480 return;
0481 }
0482
0483
0484
0485
0486
0487
0488 new_md = new;
0489 for_each_efi_memory_desc(md) {
0490 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
0491 (md->type == EFI_BOOT_SERVICES_CODE ||
0492 md->type == EFI_BOOT_SERVICES_DATA))
0493 continue;
0494
0495 memcpy(new_md, md, efi.memmap.desc_size);
0496 new_md += efi.memmap.desc_size;
0497 }
0498
0499 memunmap(new);
0500
0501 if (efi_memmap_install(&data) != 0) {
0502 pr_err("Could not install new EFI memmap\n");
0503 return;
0504 }
0505 }
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516 int __init efi_reuse_config(u64 tables, int nr_tables)
0517 {
0518 int i, sz, ret = 0;
0519 void *p, *tablep;
0520 struct efi_setup_data *data;
0521
0522 if (nr_tables == 0)
0523 return 0;
0524
0525 if (!efi_setup)
0526 return 0;
0527
0528 if (!efi_enabled(EFI_64BIT))
0529 return 0;
0530
0531 data = early_memremap(efi_setup, sizeof(*data));
0532 if (!data) {
0533 ret = -ENOMEM;
0534 goto out;
0535 }
0536
0537 if (!data->smbios)
0538 goto out_memremap;
0539
0540 sz = sizeof(efi_config_table_64_t);
0541
0542 p = tablep = early_memremap(tables, nr_tables * sz);
0543 if (!p) {
0544 pr_err("Could not map Configuration table!\n");
0545 ret = -ENOMEM;
0546 goto out_memremap;
0547 }
0548
0549 for (i = 0; i < nr_tables; i++) {
0550 efi_guid_t guid;
0551
0552 guid = ((efi_config_table_64_t *)p)->guid;
0553
0554 if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
0555 ((efi_config_table_64_t *)p)->table = data->smbios;
0556 p += sz;
0557 }
0558 early_memunmap(tablep, nr_tables * sz);
0559
0560 out_memremap:
0561 early_memunmap(data, sizeof(*data));
0562 out:
0563 return ret;
0564 }
0565
0566 void __init efi_apply_memmap_quirks(void)
0567 {
0568
0569
0570
0571
0572
0573 if (!efi_runtime_supported()) {
0574 pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
0575 efi_memmap_unmap();
0576 }
0577 }
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587 bool efi_reboot_required(void)
0588 {
0589 if (!acpi_gbl_reduced_hardware)
0590 return false;
0591
0592 efi_reboot_quirk_mode = EFI_RESET_WARM;
0593 return true;
0594 }
0595
0596 bool efi_poweroff_required(void)
0597 {
0598 return acpi_gbl_reduced_hardware || acpi_no_s5;
0599 }
0600
0601 #ifdef CONFIG_EFI_CAPSULE_QUIRK_QUARK_CSH
0602
0603 static int qrk_capsule_setup_info(struct capsule_info *cap_info, void **pkbuff,
0604 size_t hdr_bytes)
0605 {
0606 struct quark_security_header *csh = *pkbuff;
0607
0608
0609 if (hdr_bytes < sizeof(struct quark_security_header))
0610 return 0;
0611
0612 if (csh->csh_signature != QUARK_CSH_SIGNATURE ||
0613 csh->headersize != QUARK_SECURITY_HEADER_SIZE)
0614 return 1;
0615
0616
0617 if (hdr_bytes < QUARK_SECURITY_HEADER_SIZE +
0618 sizeof(efi_capsule_header_t))
0619 return 0;
0620
0621 pr_debug("Quark security header detected\n");
0622
0623 if (csh->rsvd_next_header != 0) {
0624 pr_err("multiple Quark security headers not supported\n");
0625 return -EINVAL;
0626 }
0627
0628 *pkbuff += csh->headersize;
0629 cap_info->total_size = csh->headersize;
0630
0631
0632
0633
0634 cap_info->phys[0] += csh->headersize;
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645 cap_info->capsule = &cap_info->header;
0646
0647 return 1;
0648 }
0649
0650 static const struct x86_cpu_id efi_capsule_quirk_ids[] = {
0651 X86_MATCH_VENDOR_FAM_MODEL(INTEL, 5, INTEL_FAM5_QUARK_X1000,
0652 &qrk_capsule_setup_info),
0653 { }
0654 };
0655
0656 int efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
0657 size_t hdr_bytes)
0658 {
0659 int (*quirk_handler)(struct capsule_info *, void **, size_t);
0660 const struct x86_cpu_id *id;
0661 int ret;
0662
0663 if (hdr_bytes < sizeof(efi_capsule_header_t))
0664 return 0;
0665
0666 cap_info->total_size = 0;
0667
0668 id = x86_match_cpu(efi_capsule_quirk_ids);
0669 if (id) {
0670
0671
0672
0673
0674
0675
0676
0677 quirk_handler = (typeof(quirk_handler))id->driver_data;
0678 ret = quirk_handler(cap_info, &kbuff, hdr_bytes);
0679 if (ret <= 0)
0680 return ret;
0681 }
0682
0683 memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));
0684
0685 cap_info->total_size += cap_info->header.imagesize;
0686
0687 return __efi_capsule_setup_info(cap_info);
0688 }
0689
0690 #endif
0691
0692
0693
0694
0695
0696
0697
0698
0699
0700
0701
0702
0703 void efi_crash_gracefully_on_page_fault(unsigned long phys_addr)
0704 {
0705 if (!IS_ENABLED(CONFIG_X86_64))
0706 return;
0707
0708
0709
0710
0711
0712 if (in_interrupt())
0713 return;
0714
0715
0716
0717
0718
0719
0720 if (READ_ONCE(efi_rts_work.efi_rts_id) == EFI_NONE ||
0721 current_work() != &efi_rts_work.work)
0722 return;
0723
0724
0725
0726
0727
0728 if (phys_addr <= 0x0fff)
0729 return;
0730
0731
0732
0733
0734
0735 WARN(1, FW_BUG "Page fault caused by firmware at PA: 0x%lx\n",
0736 phys_addr);
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746 if (efi_rts_work.efi_rts_id == EFI_RESET_SYSTEM) {
0747 pr_info("efi_reset_system() buggy! Reboot through BIOS\n");
0748 machine_real_restart(MRR_BIOS);
0749 return;
0750 }
0751
0752
0753
0754
0755
0756 arch_efi_call_virt_teardown();
0757
0758
0759 efi_rts_work.status = EFI_ABORTED;
0760 complete(&efi_rts_work.efi_rts_comp);
0761
0762 clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
0763 pr_info("Froze efi_rts_wq and disabled EFI Runtime Services\n");
0764
0765
0766
0767
0768
0769 for (;;) {
0770 set_current_state(TASK_IDLE);
0771 schedule();
0772 }
0773 }