2 * Common EFI (Extensible Firmware Interface) support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
5 * Copyright (C) 1999 VA Linux Systems
6 * Copyright (C) 1999 Walt Drummond <drummond@valinux.com>
7 * Copyright (C) 1999-2002 Hewlett-Packard Co.
8 * David Mosberger-Tang <davidm@hpl.hp.com>
9 * Stephane Eranian <eranian@hpl.hp.com>
10 * Copyright (C) 2005-2008 Intel Co.
11 * Fenghua Yu <fenghua.yu@intel.com>
12 * Bibo Mao <bibo.mao@intel.com>
13 * Chandramouli Narayanan <mouli@linux.intel.com>
14 * Huang Ying <ying.huang@intel.com>
16 * Copied from efi_32.c to eliminate the duplicated code between EFI
17 * 32/64 support code. --ying 2007-10-26
19 * All EFI Runtime Services are not implemented yet as EFI only
20 * supports physical mode addressing on SoftSDV. This is to be fixed
21 * in a future version. --drummond 1999-07-20
23 * Implemented EFI runtime services and virtual mode calls. --davidm
25 * Goutham Rao: <goutham.rao@intel.com>
26 * Skip non-WB memory and ignore empty memory ranges.
29 #include <linux/kernel.h>
30 #include <linux/init.h>
31 #include <linux/efi.h>
32 #include <linux/export.h>
33 #include <linux/bootmem.h>
34 #include <linux/memblock.h>
35 #include <linux/spinlock.h>
36 #include <linux/uaccess.h>
37 #include <linux/time.h>
39 #include <linux/reboot.h>
40 #include <linux/bcd.h>
42 #include <asm/setup.h>
45 #include <asm/cacheflush.h>
46 #include <asm/tlbflush.h>
47 #include <asm/x86_init.h>
52 #define EFI_MIN_RESERVE 5120
54 #define EFI_DUMMY_GUID \
55 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
57 static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
59 struct efi __read_mostly efi = {
60 .mps = EFI_INVALID_TABLE_ADDR,
61 .acpi = EFI_INVALID_TABLE_ADDR,
62 .acpi20 = EFI_INVALID_TABLE_ADDR,
63 .smbios = EFI_INVALID_TABLE_ADDR,
64 .sal_systab = EFI_INVALID_TABLE_ADDR,
65 .boot_info = EFI_INVALID_TABLE_ADDR,
66 .hcdp = EFI_INVALID_TABLE_ADDR,
67 .uga = EFI_INVALID_TABLE_ADDR,
68 .uv_systab = EFI_INVALID_TABLE_ADDR,
72 struct efi_memory_map memmap;
74 static struct efi efi_phys __initdata;
75 static efi_system_table_t efi_systab __initdata;
77 static inline bool efi_is_native(void)
79 return IS_ENABLED(CONFIG_X86_64) == efi_enabled(EFI_64BIT);
82 unsigned long x86_efi_facility;
85 * Returns 1 if 'facility' is enabled, 0 otherwise.
87 int efi_enabled(int facility)
89 return test_bit(facility, &x86_efi_facility) != 0;
91 EXPORT_SYMBOL(efi_enabled);
93 static bool disable_runtime = false;
94 static int __init setup_noefi(char *arg)
96 disable_runtime = true;
99 early_param("noefi", setup_noefi);
102 EXPORT_SYMBOL(add_efi_memmap);
104 static int __init setup_add_efi_memmap(char *arg)
109 early_param("add_efi_memmap", setup_add_efi_memmap);
111 static bool efi_no_storage_paranoia;
113 static int __init setup_storage_paranoia(char *arg)
115 efi_no_storage_paranoia = true;
118 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
121 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
126 spin_lock_irqsave(&rtc_lock, flags);
127 status = efi_call_virt2(get_time, tm, tc);
128 spin_unlock_irqrestore(&rtc_lock, flags);
132 static efi_status_t virt_efi_set_time(efi_time_t *tm)
137 spin_lock_irqsave(&rtc_lock, flags);
138 status = efi_call_virt1(set_time, tm);
139 spin_unlock_irqrestore(&rtc_lock, flags);
143 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
150 spin_lock_irqsave(&rtc_lock, flags);
151 status = efi_call_virt3(get_wakeup_time,
152 enabled, pending, tm);
153 spin_unlock_irqrestore(&rtc_lock, flags);
157 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
162 spin_lock_irqsave(&rtc_lock, flags);
163 status = efi_call_virt2(set_wakeup_time,
165 spin_unlock_irqrestore(&rtc_lock, flags);
169 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
172 unsigned long *data_size,
175 return efi_call_virt5(get_variable,
180 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
184 return efi_call_virt3(get_next_variable,
185 name_size, name, vendor);
188 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
191 unsigned long data_size,
194 return efi_call_virt5(set_variable,
199 static efi_status_t virt_efi_query_variable_info(u32 attr,
201 u64 *remaining_space,
202 u64 *max_variable_size)
204 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
205 return EFI_UNSUPPORTED;
207 return efi_call_virt4(query_variable_info, attr, storage_space,
208 remaining_space, max_variable_size);
211 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
213 return efi_call_virt1(get_next_high_mono_count, count);
216 static void virt_efi_reset_system(int reset_type,
218 unsigned long data_size,
221 efi_call_virt4(reset_system, reset_type, status,
225 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
227 unsigned long sg_list)
229 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
230 return EFI_UNSUPPORTED;
232 return efi_call_virt3(update_capsule, capsules, count, sg_list);
235 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
240 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
241 return EFI_UNSUPPORTED;
243 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
247 static efi_status_t __init phys_efi_set_virtual_address_map(
248 unsigned long memory_map_size,
249 unsigned long descriptor_size,
250 u32 descriptor_version,
251 efi_memory_desc_t *virtual_map)
255 efi_call_phys_prelog();
256 status = efi_call_phys4(efi_phys.set_virtual_address_map,
257 memory_map_size, descriptor_size,
258 descriptor_version, virtual_map);
259 efi_call_phys_epilog();
263 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
269 spin_lock_irqsave(&rtc_lock, flags);
270 efi_call_phys_prelog();
271 status = efi_call_phys2(efi_phys.get_time, tm, tc);
272 efi_call_phys_epilog();
273 spin_unlock_irqrestore(&rtc_lock, flags);
277 int efi_set_rtc_mmss(unsigned long nowtime)
279 int real_seconds, real_minutes;
284 status = efi.get_time(&eft, &cap);
285 if (status != EFI_SUCCESS) {
286 printk(KERN_ERR "Oops: efitime: can't read time!\n");
290 real_seconds = nowtime % 60;
291 real_minutes = nowtime / 60;
292 if (((abs(real_minutes - eft.minute) + 15)/30) & 1)
295 eft.minute = real_minutes;
296 eft.second = real_seconds;
298 status = efi.set_time(&eft);
299 if (status != EFI_SUCCESS) {
300 printk(KERN_ERR "Oops: efitime: can't write time!\n");
306 unsigned long efi_get_time(void)
312 status = efi.get_time(&eft, &cap);
313 if (status != EFI_SUCCESS)
314 printk(KERN_ERR "Oops: efitime: can't read time!\n");
316 return mktime(eft.year, eft.month, eft.day, eft.hour,
317 eft.minute, eft.second);
321 * Tell the kernel about the EFI memory map. This might include
322 * more than the max 128 entries that can fit in the e820 legacy
323 * (zeropage) memory map.
326 static void __init do_add_efi_memmap(void)
330 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
331 efi_memory_desc_t *md = p;
332 unsigned long long start = md->phys_addr;
333 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
337 case EFI_LOADER_CODE:
338 case EFI_LOADER_DATA:
339 case EFI_BOOT_SERVICES_CODE:
340 case EFI_BOOT_SERVICES_DATA:
341 case EFI_CONVENTIONAL_MEMORY:
342 if (md->attribute & EFI_MEMORY_WB)
343 e820_type = E820_RAM;
345 e820_type = E820_RESERVED;
347 case EFI_ACPI_RECLAIM_MEMORY:
348 e820_type = E820_ACPI;
350 case EFI_ACPI_MEMORY_NVS:
351 e820_type = E820_NVS;
353 case EFI_UNUSABLE_MEMORY:
354 e820_type = E820_UNUSABLE;
358 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
359 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
360 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
362 e820_type = E820_RESERVED;
365 e820_add_region(start, size, e820_type);
367 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
370 void __init efi_memblock_x86_reserve_range(void)
375 pmap = boot_params.efi_info.efi_memmap;
377 pmap = (boot_params.efi_info.efi_memmap |
378 ((__u64)boot_params.efi_info.efi_memmap_hi<<32));
380 memmap.phys_map = (void *)pmap;
381 memmap.nr_map = boot_params.efi_info.efi_memmap_size /
382 boot_params.efi_info.efi_memdesc_size;
383 memmap.desc_version = boot_params.efi_info.efi_memdesc_version;
384 memmap.desc_size = boot_params.efi_info.efi_memdesc_size;
385 memblock_x86_reserve_range(pmap, pmap + memmap.nr_map * memmap.desc_size,
390 static void __init print_efi_memmap(void)
392 efi_memory_desc_t *md;
396 for (p = memmap.map, i = 0;
398 p += memmap.desc_size, i++) {
400 printk(KERN_INFO PFX "mem%02u: type=%u, attr=0x%llx, "
401 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
402 i, md->type, md->attribute, md->phys_addr,
403 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
404 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
407 #endif /* EFI_DEBUG */
409 void __init efi_reserve_boot_services(void)
413 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
414 efi_memory_desc_t *md = p;
415 u64 start = md->phys_addr;
416 u64 size = md->num_pages << EFI_PAGE_SHIFT;
418 if (md->type != EFI_BOOT_SERVICES_CODE &&
419 md->type != EFI_BOOT_SERVICES_DATA)
421 /* Only reserve where possible:
422 * - Not within any already allocated areas
423 * - Not over any memory area (really needed, if above?)
424 * - Not within any part of the kernel
425 * - Not the bios reserved area
427 if ((start+size >= virt_to_phys(_text)
428 && start <= virt_to_phys(_end)) ||
429 !e820_all_mapped(start, start+size, E820_RAM) ||
430 memblock_x86_check_reserved_size(&start, &size,
431 1<<EFI_PAGE_SHIFT)) {
432 /* Could not reserve, skip it */
434 memblock_dbg(PFX "Could not reserve boot range "
435 "[0x%010llx-0x%010llx]\n",
436 start, start+size-1);
438 memblock_x86_reserve_range(start, start+size,
443 static void __init efi_free_boot_services(void)
447 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
448 efi_memory_desc_t *md = p;
449 unsigned long long start = md->phys_addr;
450 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
452 if (md->type != EFI_BOOT_SERVICES_CODE &&
453 md->type != EFI_BOOT_SERVICES_DATA)
456 /* Could not reserve boot area */
460 free_bootmem_late(start, size);
464 void __init efi_init(void)
466 efi_config_table_t *config_tables;
467 efi_runtime_services_t *runtime;
469 char vendor[100] = "unknown";
473 if (!efi_is_native())
477 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
479 efi_phys.systab = (efi_system_table_t *)
480 (boot_params.efi_info.efi_systab |
481 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
484 efi.systab = early_ioremap((unsigned long)efi_phys.systab,
485 sizeof(efi_system_table_t));
486 if (efi.systab == NULL)
487 printk(KERN_ERR "Couldn't map the EFI system table!\n");
488 memcpy(&efi_systab, efi.systab, sizeof(efi_system_table_t));
489 early_iounmap(efi.systab, sizeof(efi_system_table_t));
490 efi.systab = &efi_systab;
493 * Verify the EFI Table
495 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
496 printk(KERN_ERR "EFI system table signature incorrect!\n");
497 if ((efi.systab->hdr.revision >> 16) == 0)
498 printk(KERN_ERR "Warning: EFI system table version "
499 "%d.%02d, expected 1.00 or greater!\n",
500 efi.systab->hdr.revision >> 16,
501 efi.systab->hdr.revision & 0xffff);
503 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
506 * Show what we know for posterity
508 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
510 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
514 printk(KERN_ERR PFX "Could not map the firmware vendor!\n");
515 early_iounmap(tmp, 2);
517 printk(KERN_INFO "EFI v%u.%.02u by %s\n",
518 efi.systab->hdr.revision >> 16,
519 efi.systab->hdr.revision & 0xffff, vendor);
522 * Let's see what config tables the firmware passed to us.
524 config_tables = early_ioremap(
526 efi.systab->nr_tables * sizeof(efi_config_table_t));
527 if (config_tables == NULL)
528 printk(KERN_ERR "Could not map EFI Configuration Table!\n");
531 for (i = 0; i < efi.systab->nr_tables; i++) {
532 if (!efi_guidcmp(config_tables[i].guid, MPS_TABLE_GUID)) {
533 efi.mps = config_tables[i].table;
534 printk(" MPS=0x%lx ", config_tables[i].table);
535 } else if (!efi_guidcmp(config_tables[i].guid,
536 ACPI_20_TABLE_GUID)) {
537 efi.acpi20 = config_tables[i].table;
538 printk(" ACPI 2.0=0x%lx ", config_tables[i].table);
539 } else if (!efi_guidcmp(config_tables[i].guid,
541 efi.acpi = config_tables[i].table;
542 printk(" ACPI=0x%lx ", config_tables[i].table);
543 } else if (!efi_guidcmp(config_tables[i].guid,
544 SMBIOS_TABLE_GUID)) {
545 efi.smbios = config_tables[i].table;
546 printk(" SMBIOS=0x%lx ", config_tables[i].table);
548 } else if (!efi_guidcmp(config_tables[i].guid,
549 UV_SYSTEM_TABLE_GUID)) {
550 efi.uv_systab = config_tables[i].table;
551 printk(" UVsystab=0x%lx ", config_tables[i].table);
553 } else if (!efi_guidcmp(config_tables[i].guid,
555 efi.hcdp = config_tables[i].table;
556 printk(" HCDP=0x%lx ", config_tables[i].table);
557 } else if (!efi_guidcmp(config_tables[i].guid,
558 UGA_IO_PROTOCOL_GUID)) {
559 efi.uga = config_tables[i].table;
560 printk(" UGA=0x%lx ", config_tables[i].table);
564 early_iounmap(config_tables,
565 efi.systab->nr_tables * sizeof(efi_config_table_t));
567 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
569 if (!disable_runtime) {
571 * Check out the runtime services table. We need to map
572 * the runtime services table so that we can grab the physical
573 * address of several of the EFI runtime functions, needed to
574 * set the firmware into virtual mode.
576 runtime = early_ioremap((unsigned long)efi.systab->runtime,
577 sizeof(efi_runtime_services_t));
578 if (runtime != NULL) {
580 * We will only need *early* access to the following
581 * two EFI runtime services before set_virtual_address_map
584 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
585 efi_phys.set_virtual_address_map =
586 (efi_set_virtual_address_map_t *)
587 runtime->set_virtual_address_map;
589 * Make efi_get_time can be called before entering
592 efi.get_time = phys_efi_get_time;
594 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
596 printk(KERN_ERR "Could not map the EFI runtime service "
598 early_iounmap(runtime, sizeof(efi_runtime_services_t));
601 /* Map the EFI memory map */
602 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
603 memmap.nr_map * memmap.desc_size);
604 if (memmap.map == NULL)
605 printk(KERN_ERR "Could not map the EFI memory map!\n");
606 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
608 if (memmap.desc_size != sizeof(efi_memory_desc_t))
610 "Kernel-defined memdesc doesn't match the one from EFI!\n");
615 set_bit(EFI_MEMMAP, &x86_efi_facility);
618 x86_platform.get_wallclock = efi_get_time;
619 x86_platform.set_wallclock = efi_set_rtc_mmss;
627 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
631 addr = md->virt_addr;
632 npages = md->num_pages;
634 memrange_efi_to_native(&addr, &npages);
637 set_memory_x(addr, npages);
639 set_memory_nx(addr, npages);
642 static void __init runtime_code_page_mkexec(void)
644 efi_memory_desc_t *md;
647 /* Make EFI runtime service code area executable */
648 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
651 if (md->type != EFI_RUNTIME_SERVICES_CODE)
654 efi_set_executable(md, true);
659 * This function will switch the EFI runtime services to virtual mode.
660 * Essentially, look through the EFI memmap and map every region that
661 * has the runtime attribute bit set in its memory descriptor and update
662 * that memory descriptor with the virtual address obtained from ioremap().
663 * This enables the runtime services to be called without having to
664 * thunk back into physical mode for every invocation.
666 void __init efi_enter_virtual_mode(void)
668 efi_memory_desc_t *md, *prev_md = NULL;
671 u64 end, systab, addr, npages, end_pfn;
672 void *p, *va, *new_memmap = NULL;
677 /* Merge contiguous regions of the same type and attribute */
678 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
687 if (prev_md->type != md->type ||
688 prev_md->attribute != md->attribute) {
693 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
695 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
696 prev_md->num_pages += md->num_pages;
697 md->type = EFI_RESERVED_TYPE;
704 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
706 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
708 if (md->type != EFI_BOOT_SERVICES_CODE &&
709 md->type != EFI_BOOT_SERVICES_DATA)
714 size = md->num_pages << EFI_PAGE_SHIFT;
715 end = md->phys_addr + size;
717 end_pfn = PFN_UP(end);
718 if (end_pfn <= max_low_pfn_mapped
719 || (end_pfn > (1UL << (32 - PAGE_SHIFT))
720 && end_pfn <= max_pfn_mapped))
721 va = __va(md->phys_addr);
723 va = efi_ioremap(md->phys_addr, size, md->type);
725 md->virt_addr = (u64) (unsigned long) va;
728 printk(KERN_ERR PFX "ioremap of 0x%llX failed!\n",
729 (unsigned long long)md->phys_addr);
733 if (!(md->attribute & EFI_MEMORY_WB)) {
734 addr = md->virt_addr;
735 npages = md->num_pages;
736 memrange_efi_to_native(&addr, &npages);
737 set_memory_uc(addr, npages);
740 systab = (u64) (unsigned long) efi_phys.systab;
741 if (md->phys_addr <= systab && systab < end) {
742 systab += md->virt_addr - md->phys_addr;
743 efi.systab = (efi_system_table_t *) (unsigned long) systab;
745 new_memmap = krealloc(new_memmap,
746 (count + 1) * memmap.desc_size,
748 memcpy(new_memmap + (count * memmap.desc_size), md,
755 status = phys_efi_set_virtual_address_map(
756 memmap.desc_size * count,
759 (efi_memory_desc_t *)__pa(new_memmap));
761 if (status != EFI_SUCCESS) {
762 printk(KERN_ALERT "Unable to switch EFI into virtual mode "
763 "(status=%lx)!\n", status);
764 panic("EFI call to SetVirtualAddressMap() failed!");
768 * Thankfully, it does seem that no runtime services other than
769 * SetVirtualAddressMap() will touch boot services code, so we can
770 * get rid of it all at this point
772 efi_free_boot_services();
775 * Now that EFI is in virtual mode, update the function
776 * pointers in the runtime service table to the new virtual addresses.
778 * Call EFI services through wrapper functions.
780 efi.runtime_version = efi_systab.hdr.revision;
781 efi.get_time = virt_efi_get_time;
782 efi.set_time = virt_efi_set_time;
783 efi.get_wakeup_time = virt_efi_get_wakeup_time;
784 efi.set_wakeup_time = virt_efi_set_wakeup_time;
785 efi.get_variable = virt_efi_get_variable;
786 efi.get_next_variable = virt_efi_get_next_variable;
787 efi.set_variable = virt_efi_set_variable;
788 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
789 efi.reset_system = virt_efi_reset_system;
790 efi.set_virtual_address_map = NULL;
791 efi.query_variable_info = virt_efi_query_variable_info;
792 efi.update_capsule = virt_efi_update_capsule;
793 efi.query_capsule_caps = virt_efi_query_capsule_caps;
794 if (__supported_pte_mask & _PAGE_NX)
795 runtime_code_page_mkexec();
796 clear_bit(EFI_MEMMAP, &x86_efi_facility);
797 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
801 /* clean DUMMY object */
802 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
803 EFI_VARIABLE_NON_VOLATILE |
804 EFI_VARIABLE_BOOTSERVICE_ACCESS |
805 EFI_VARIABLE_RUNTIME_ACCESS,
810 * Convenience functions to obtain memory types and attributes
812 u32 efi_mem_type(unsigned long phys_addr)
814 efi_memory_desc_t *md;
817 if (!efi_enabled(EFI_MEMMAP))
820 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
822 if ((md->phys_addr <= phys_addr) &&
823 (phys_addr < (md->phys_addr +
824 (md->num_pages << EFI_PAGE_SHIFT))))
830 u64 efi_mem_attributes(unsigned long phys_addr)
832 efi_memory_desc_t *md;
835 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
837 if ((md->phys_addr <= phys_addr) &&
838 (phys_addr < (md->phys_addr +
839 (md->num_pages << EFI_PAGE_SHIFT))))
840 return md->attribute;
846 * Some firmware has serious problems when using more than 50% of the EFI
847 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
848 * we never use more than this safe limit.
850 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
853 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
856 u64 storage_size, remaining_size, max_size;
858 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
861 status = efi.query_variable_info(attributes, &storage_size,
862 &remaining_size, &max_size);
863 if (status != EFI_SUCCESS)
867 * Some firmware implementations refuse to boot if there's insufficient
868 * space in the variable store. We account for that by refusing the
869 * write if permitting it would reduce the available space to under
870 * 5KB. This figure was provided by Samsung, so should be safe.
872 if ((remaining_size - size < EFI_MIN_RESERVE) &&
873 !efi_no_storage_paranoia) {
876 * Triggering garbage collection may require that the firmware
877 * generate a real EFI_OUT_OF_RESOURCES error. We can force
878 * that by attempting to use more space than is available.
880 unsigned long dummy_size = remaining_size + 1024;
881 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
884 return EFI_OUT_OF_RESOURCES;
886 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
887 EFI_VARIABLE_NON_VOLATILE |
888 EFI_VARIABLE_BOOTSERVICE_ACCESS |
889 EFI_VARIABLE_RUNTIME_ACCESS,
892 if (status == EFI_SUCCESS) {
894 * This should have failed, so if it didn't make sure
895 * that we delete it...
897 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
898 EFI_VARIABLE_NON_VOLATILE |
899 EFI_VARIABLE_BOOTSERVICE_ACCESS |
900 EFI_VARIABLE_RUNTIME_ACCESS,
907 * The runtime code may now have triggered a garbage collection
908 * run, so check the variable info again
910 status = efi.query_variable_info(attributes, &storage_size,
911 &remaining_size, &max_size);
913 if (status != EFI_SUCCESS)
917 * There still isn't enough room, so return an error
919 if (remaining_size - size < EFI_MIN_RESERVE)
920 return EFI_OUT_OF_RESOURCES;
925 EXPORT_SYMBOL_GPL(efi_query_variable_store);