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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/kernel.h>
32 #include <linux/init.h>
33 #include <linux/efi.h>
34 #include <linux/efi-bgrt.h>
35 #include <linux/export.h>
36 #include <linux/bootmem.h>
37 #include <linux/slab.h>
38 #include <linux/memblock.h>
39 #include <linux/spinlock.h>
40 #include <linux/uaccess.h>
41 #include <linux/time.h>
43 #include <linux/reboot.h>
44 #include <linux/bcd.h>
46 #include <asm/setup.h>
49 #include <asm/cacheflush.h>
50 #include <asm/tlbflush.h>
51 #include <asm/x86_init.h>
56 #define EFI_MIN_RESERVE 5120
58 #define EFI_DUMMY_GUID \
59 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
61 static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
63 struct efi_memory_map memmap;
65 static struct efi efi_phys __initdata;
66 static efi_system_table_t efi_systab __initdata;
68 static __initdata efi_config_table_type_t arch_tables[] = {
70 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
75 u64 efi_setup; /* efi setup_data physical address */
77 static bool __initdata disable_runtime = false;
78 static int __init setup_noefi(char *arg)
80 disable_runtime = true;
83 early_param("noefi", setup_noefi);
86 EXPORT_SYMBOL(add_efi_memmap);
88 static int __init setup_add_efi_memmap(char *arg)
93 early_param("add_efi_memmap", setup_add_efi_memmap);
95 static bool efi_no_storage_paranoia;
97 static int __init setup_storage_paranoia(char *arg)
99 efi_no_storage_paranoia = true;
102 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
105 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
110 spin_lock_irqsave(&rtc_lock, flags);
111 status = efi_call_virt2(get_time, tm, tc);
112 spin_unlock_irqrestore(&rtc_lock, flags);
116 static efi_status_t virt_efi_set_time(efi_time_t *tm)
121 spin_lock_irqsave(&rtc_lock, flags);
122 status = efi_call_virt1(set_time, tm);
123 spin_unlock_irqrestore(&rtc_lock, flags);
127 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
134 spin_lock_irqsave(&rtc_lock, flags);
135 status = efi_call_virt3(get_wakeup_time,
136 enabled, pending, tm);
137 spin_unlock_irqrestore(&rtc_lock, flags);
141 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
146 spin_lock_irqsave(&rtc_lock, flags);
147 status = efi_call_virt2(set_wakeup_time,
149 spin_unlock_irqrestore(&rtc_lock, flags);
153 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
156 unsigned long *data_size,
159 return efi_call_virt5(get_variable,
164 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
168 return efi_call_virt3(get_next_variable,
169 name_size, name, vendor);
172 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
175 unsigned long data_size,
178 return efi_call_virt5(set_variable,
183 static efi_status_t virt_efi_query_variable_info(u32 attr,
185 u64 *remaining_space,
186 u64 *max_variable_size)
188 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
189 return EFI_UNSUPPORTED;
191 return efi_call_virt4(query_variable_info, attr, storage_space,
192 remaining_space, max_variable_size);
195 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
197 return efi_call_virt1(get_next_high_mono_count, count);
200 static void virt_efi_reset_system(int reset_type,
202 unsigned long data_size,
205 efi_call_virt4(reset_system, reset_type, status,
209 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
211 unsigned long sg_list)
213 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
214 return EFI_UNSUPPORTED;
216 return efi_call_virt3(update_capsule, capsules, count, sg_list);
219 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
224 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
225 return EFI_UNSUPPORTED;
227 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
231 static efi_status_t __init phys_efi_set_virtual_address_map(
232 unsigned long memory_map_size,
233 unsigned long descriptor_size,
234 u32 descriptor_version,
235 efi_memory_desc_t *virtual_map)
239 efi_call_phys_prelog();
240 status = efi_call_phys4(efi_phys.set_virtual_address_map,
241 memory_map_size, descriptor_size,
242 descriptor_version, virtual_map);
243 efi_call_phys_epilog();
247 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
253 spin_lock_irqsave(&rtc_lock, flags);
254 efi_call_phys_prelog();
255 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
257 efi_call_phys_epilog();
258 spin_unlock_irqrestore(&rtc_lock, flags);
262 int efi_set_rtc_mmss(unsigned long nowtime)
269 status = efi.get_time(&eft, &cap);
270 if (status != EFI_SUCCESS) {
271 pr_err("Oops: efitime: can't read time!\n");
275 rtc_time_to_tm(nowtime, &tm);
276 if (!rtc_valid_tm(&tm)) {
277 eft.year = tm.tm_year + 1900;
278 eft.month = tm.tm_mon + 1;
279 eft.day = tm.tm_mday;
280 eft.minute = tm.tm_min;
281 eft.second = tm.tm_sec;
285 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
286 __FUNCTION__, nowtime);
290 status = efi.set_time(&eft);
291 if (status != EFI_SUCCESS) {
292 pr_err("Oops: efitime: can't write time!\n");
298 unsigned long efi_get_time(void)
304 status = efi.get_time(&eft, &cap);
305 if (status != EFI_SUCCESS)
306 pr_err("Oops: efitime: can't read time!\n");
308 return mktime(eft.year, eft.month, eft.day, eft.hour,
309 eft.minute, eft.second);
313 * Tell the kernel about the EFI memory map. This might include
314 * more than the max 128 entries that can fit in the e820 legacy
315 * (zeropage) memory map.
318 static void __init do_add_efi_memmap(void)
322 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
323 efi_memory_desc_t *md = p;
324 unsigned long long start = md->phys_addr;
325 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
329 case EFI_LOADER_CODE:
330 case EFI_LOADER_DATA:
331 case EFI_BOOT_SERVICES_CODE:
332 case EFI_BOOT_SERVICES_DATA:
333 case EFI_CONVENTIONAL_MEMORY:
334 if (md->attribute & EFI_MEMORY_WB)
335 e820_type = E820_RAM;
337 e820_type = E820_RESERVED;
339 case EFI_ACPI_RECLAIM_MEMORY:
340 e820_type = E820_ACPI;
342 case EFI_ACPI_MEMORY_NVS:
343 e820_type = E820_NVS;
345 case EFI_UNUSABLE_MEMORY:
346 e820_type = E820_UNUSABLE;
350 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
351 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
352 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
354 e820_type = E820_RESERVED;
357 e820_add_region(start, size, e820_type);
359 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
362 int __init efi_memblock_x86_reserve_range(void)
364 struct efi_info *e = &boot_params.efi_info;
368 /* Can't handle data above 4GB at this time */
369 if (e->efi_memmap_hi) {
370 pr_err("Memory map is above 4GB, disabling EFI.\n");
373 pmap = e->efi_memmap;
375 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
377 memmap.phys_map = (void *)pmap;
378 memmap.nr_map = e->efi_memmap_size /
380 memmap.desc_size = e->efi_memdesc_size;
381 memmap.desc_version = e->efi_memdesc_version;
383 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
385 efi.memmap = &memmap;
391 static void __init print_efi_memmap(void)
393 efi_memory_desc_t *md;
397 for (p = memmap.map, i = 0;
399 p += memmap.desc_size, i++) {
401 pr_info("mem%02u: type=%u, attr=0x%llx, "
402 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
403 i, md->type, md->attribute, md->phys_addr,
404 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
405 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
408 #endif /* EFI_DEBUG */
410 void __init efi_reserve_boot_services(void)
414 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
415 efi_memory_desc_t *md = p;
416 u64 start = md->phys_addr;
417 u64 size = md->num_pages << EFI_PAGE_SHIFT;
419 if (md->type != EFI_BOOT_SERVICES_CODE &&
420 md->type != EFI_BOOT_SERVICES_DATA)
422 /* Only reserve where possible:
423 * - Not within any already allocated areas
424 * - Not over any memory area (really needed, if above?)
425 * - Not within any part of the kernel
426 * - Not the bios reserved area
428 if ((start + size > __pa_symbol(_text)
429 && start <= __pa_symbol(_end)) ||
430 !e820_all_mapped(start, start+size, E820_RAM) ||
431 memblock_is_region_reserved(start, size)) {
432 /* Could not reserve, skip it */
434 memblock_dbg("Could not reserve boot range "
435 "[0x%010llx-0x%010llx]\n",
436 start, start+size-1);
438 memblock_reserve(start, size);
442 void __init efi_unmap_memmap(void)
444 clear_bit(EFI_MEMMAP, &efi.flags);
446 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
451 void __init efi_free_boot_services(void)
455 if (!efi_is_native())
458 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
459 efi_memory_desc_t *md = p;
460 unsigned long long start = md->phys_addr;
461 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
463 if (md->type != EFI_BOOT_SERVICES_CODE &&
464 md->type != EFI_BOOT_SERVICES_DATA)
467 /* Could not reserve boot area */
471 free_bootmem_late(start, size);
477 static int __init efi_systab_init(void *phys)
479 if (efi_enabled(EFI_64BIT)) {
480 efi_system_table_64_t *systab64;
483 systab64 = early_ioremap((unsigned long)phys,
485 if (systab64 == NULL) {
486 pr_err("Couldn't map the system table!\n");
490 efi_systab.hdr = systab64->hdr;
491 efi_systab.fw_vendor = systab64->fw_vendor;
492 tmp |= systab64->fw_vendor;
493 efi_systab.fw_revision = systab64->fw_revision;
494 efi_systab.con_in_handle = systab64->con_in_handle;
495 tmp |= systab64->con_in_handle;
496 efi_systab.con_in = systab64->con_in;
497 tmp |= systab64->con_in;
498 efi_systab.con_out_handle = systab64->con_out_handle;
499 tmp |= systab64->con_out_handle;
500 efi_systab.con_out = systab64->con_out;
501 tmp |= systab64->con_out;
502 efi_systab.stderr_handle = systab64->stderr_handle;
503 tmp |= systab64->stderr_handle;
504 efi_systab.stderr = systab64->stderr;
505 tmp |= systab64->stderr;
506 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
507 tmp |= systab64->runtime;
508 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
509 tmp |= systab64->boottime;
510 efi_systab.nr_tables = systab64->nr_tables;
511 efi_systab.tables = systab64->tables;
512 tmp |= systab64->tables;
514 early_iounmap(systab64, sizeof(*systab64));
517 pr_err("EFI data located above 4GB, disabling EFI.\n");
522 efi_system_table_32_t *systab32;
524 systab32 = early_ioremap((unsigned long)phys,
526 if (systab32 == NULL) {
527 pr_err("Couldn't map the system table!\n");
531 efi_systab.hdr = systab32->hdr;
532 efi_systab.fw_vendor = systab32->fw_vendor;
533 efi_systab.fw_revision = systab32->fw_revision;
534 efi_systab.con_in_handle = systab32->con_in_handle;
535 efi_systab.con_in = systab32->con_in;
536 efi_systab.con_out_handle = systab32->con_out_handle;
537 efi_systab.con_out = systab32->con_out;
538 efi_systab.stderr_handle = systab32->stderr_handle;
539 efi_systab.stderr = systab32->stderr;
540 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
541 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
542 efi_systab.nr_tables = systab32->nr_tables;
543 efi_systab.tables = systab32->tables;
545 early_iounmap(systab32, sizeof(*systab32));
548 efi.systab = &efi_systab;
551 * Verify the EFI Table
553 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
554 pr_err("System table signature incorrect!\n");
557 if ((efi.systab->hdr.revision >> 16) == 0)
558 pr_err("Warning: System table version "
559 "%d.%02d, expected 1.00 or greater!\n",
560 efi.systab->hdr.revision >> 16,
561 efi.systab->hdr.revision & 0xffff);
566 static int __init efi_runtime_init(void)
568 efi_runtime_services_t *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));
579 pr_err("Could not map the runtime service table!\n");
583 * We will only need *early* access to the following
584 * two EFI runtime services before set_virtual_address_map
587 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
588 efi_phys.set_virtual_address_map =
589 (efi_set_virtual_address_map_t *)
590 runtime->set_virtual_address_map;
592 * Make efi_get_time can be called before entering
595 efi.get_time = phys_efi_get_time;
596 early_iounmap(runtime, sizeof(efi_runtime_services_t));
601 static int __init efi_memmap_init(void)
603 /* Map the EFI memory map */
604 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
605 memmap.nr_map * memmap.desc_size);
606 if (memmap.map == NULL) {
607 pr_err("Could not map the memory map!\n");
610 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
618 void __init efi_init(void)
621 char vendor[100] = "unknown";
626 if (boot_params.efi_info.efi_systab_hi ||
627 boot_params.efi_info.efi_memmap_hi) {
628 pr_info("Table located above 4GB, disabling EFI.\n");
631 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
633 efi_phys.systab = (efi_system_table_t *)
634 (boot_params.efi_info.efi_systab |
635 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
638 if (efi_systab_init(efi_phys.systab))
641 set_bit(EFI_SYSTEM_TABLES, &efi.flags);
643 efi.config_table = (unsigned long)efi.systab->tables;
644 efi.fw_vendor = (unsigned long)efi.systab->fw_vendor;
645 efi.runtime = (unsigned long)efi.systab->runtime;
648 * Show what we know for posterity
650 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
652 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
656 pr_err("Could not map the firmware vendor!\n");
657 early_iounmap(tmp, 2);
659 pr_info("EFI v%u.%.02u by %s\n",
660 efi.systab->hdr.revision >> 16,
661 efi.systab->hdr.revision & 0xffff, vendor);
663 if (efi_config_init(arch_tables))
666 set_bit(EFI_CONFIG_TABLES, &efi.flags);
669 * Note: We currently don't support runtime services on an EFI
670 * that doesn't match the kernel 32/64-bit mode.
673 if (!efi_is_native())
674 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
676 if (disable_runtime || efi_runtime_init())
678 set_bit(EFI_RUNTIME_SERVICES, &efi.flags);
681 if (efi_memmap_init())
684 set_bit(EFI_MEMMAP, &efi.flags);
691 void __init efi_late_init(void)
696 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
700 addr = md->virt_addr;
701 npages = md->num_pages;
703 memrange_efi_to_native(&addr, &npages);
706 set_memory_x(addr, npages);
708 set_memory_nx(addr, npages);
711 static void __init runtime_code_page_mkexec(void)
713 efi_memory_desc_t *md;
716 /* Make EFI runtime service code area executable */
717 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
720 if (md->type != EFI_RUNTIME_SERVICES_CODE)
723 efi_set_executable(md, true);
727 void efi_memory_uc(u64 addr, unsigned long size)
729 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
732 npages = round_up(size, page_shift) / page_shift;
733 memrange_efi_to_native(&addr, &npages);
734 set_memory_uc(addr, npages);
738 * This function will switch the EFI runtime services to virtual mode.
739 * Essentially, look through the EFI memmap and map every region that
740 * has the runtime attribute bit set in its memory descriptor and update
741 * that memory descriptor with the virtual address obtained from ioremap().
742 * This enables the runtime services to be called without having to
743 * thunk back into physical mode for every invocation.
745 void __init efi_enter_virtual_mode(void)
747 efi_memory_desc_t *md, *prev_md = NULL;
750 u64 end, systab, start_pfn, end_pfn;
751 void *p, *va, *new_memmap = NULL;
757 * We don't do virtual mode, since we don't do runtime services, on
761 if (!efi_is_native()) {
766 /* Merge contiguous regions of the same type and attribute */
767 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
776 if (prev_md->type != md->type ||
777 prev_md->attribute != md->attribute) {
782 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
784 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
785 prev_md->num_pages += md->num_pages;
786 md->type = EFI_RESERVED_TYPE;
793 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
795 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
797 if (md->type != EFI_BOOT_SERVICES_CODE &&
798 md->type != EFI_BOOT_SERVICES_DATA)
803 size = md->num_pages << EFI_PAGE_SHIFT;
804 end = md->phys_addr + size;
806 start_pfn = PFN_DOWN(md->phys_addr);
807 end_pfn = PFN_UP(end);
808 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
809 va = __va(md->phys_addr);
811 if (!(md->attribute & EFI_MEMORY_WB))
812 efi_memory_uc((u64)(unsigned long)va, size);
814 va = efi_ioremap(md->phys_addr, size,
815 md->type, md->attribute);
817 md->virt_addr = (u64) (unsigned long) va;
820 pr_err("ioremap of 0x%llX failed!\n",
821 (unsigned long long)md->phys_addr);
825 systab = (u64) (unsigned long) efi_phys.systab;
826 if (md->phys_addr <= systab && systab < end) {
827 systab += md->virt_addr - md->phys_addr;
828 efi.systab = (efi_system_table_t *) (unsigned long) systab;
830 new_memmap = krealloc(new_memmap,
831 (count + 1) * memmap.desc_size,
833 memcpy(new_memmap + (count * memmap.desc_size), md,
840 status = phys_efi_set_virtual_address_map(
841 memmap.desc_size * count,
844 (efi_memory_desc_t *)__pa(new_memmap));
846 if (status != EFI_SUCCESS) {
847 pr_alert("Unable to switch EFI into virtual mode "
848 "(status=%lx)!\n", status);
849 panic("EFI call to SetVirtualAddressMap() failed!");
853 * Now that EFI is in virtual mode, update the function
854 * pointers in the runtime service table to the new virtual addresses.
856 * Call EFI services through wrapper functions.
858 efi.runtime_version = efi_systab.hdr.revision;
859 efi.get_time = virt_efi_get_time;
860 efi.set_time = virt_efi_set_time;
861 efi.get_wakeup_time = virt_efi_get_wakeup_time;
862 efi.set_wakeup_time = virt_efi_set_wakeup_time;
863 efi.get_variable = virt_efi_get_variable;
864 efi.get_next_variable = virt_efi_get_next_variable;
865 efi.set_variable = virt_efi_set_variable;
866 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
867 efi.reset_system = virt_efi_reset_system;
868 efi.set_virtual_address_map = NULL;
869 efi.query_variable_info = virt_efi_query_variable_info;
870 efi.update_capsule = virt_efi_update_capsule;
871 efi.query_capsule_caps = virt_efi_query_capsule_caps;
872 if (__supported_pte_mask & _PAGE_NX)
873 runtime_code_page_mkexec();
877 /* clean DUMMY object */
878 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
879 EFI_VARIABLE_NON_VOLATILE |
880 EFI_VARIABLE_BOOTSERVICE_ACCESS |
881 EFI_VARIABLE_RUNTIME_ACCESS,
886 * Convenience functions to obtain memory types and attributes
888 u32 efi_mem_type(unsigned long phys_addr)
890 efi_memory_desc_t *md;
893 if (!efi_enabled(EFI_MEMMAP))
896 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
898 if ((md->phys_addr <= phys_addr) &&
899 (phys_addr < (md->phys_addr +
900 (md->num_pages << EFI_PAGE_SHIFT))))
906 u64 efi_mem_attributes(unsigned long phys_addr)
908 efi_memory_desc_t *md;
911 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
913 if ((md->phys_addr <= phys_addr) &&
914 (phys_addr < (md->phys_addr +
915 (md->num_pages << EFI_PAGE_SHIFT))))
916 return md->attribute;
922 * Some firmware has serious problems when using more than 50% of the EFI
923 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
924 * we never use more than this safe limit.
926 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
929 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
932 u64 storage_size, remaining_size, max_size;
934 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
937 status = efi.query_variable_info(attributes, &storage_size,
938 &remaining_size, &max_size);
939 if (status != EFI_SUCCESS)
943 * Some firmware implementations refuse to boot if there's insufficient
944 * space in the variable store. We account for that by refusing the
945 * write if permitting it would reduce the available space to under
946 * 5KB. This figure was provided by Samsung, so should be safe.
948 if ((remaining_size - size < EFI_MIN_RESERVE) &&
949 !efi_no_storage_paranoia) {
952 * Triggering garbage collection may require that the firmware
953 * generate a real EFI_OUT_OF_RESOURCES error. We can force
954 * that by attempting to use more space than is available.
956 unsigned long dummy_size = remaining_size + 1024;
957 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
960 return EFI_OUT_OF_RESOURCES;
962 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
963 EFI_VARIABLE_NON_VOLATILE |
964 EFI_VARIABLE_BOOTSERVICE_ACCESS |
965 EFI_VARIABLE_RUNTIME_ACCESS,
968 if (status == EFI_SUCCESS) {
970 * This should have failed, so if it didn't make sure
971 * that we delete it...
973 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
974 EFI_VARIABLE_NON_VOLATILE |
975 EFI_VARIABLE_BOOTSERVICE_ACCESS |
976 EFI_VARIABLE_RUNTIME_ACCESS,
983 * The runtime code may now have triggered a garbage collection
984 * run, so check the variable info again
986 status = efi.query_variable_info(attributes, &storage_size,
987 &remaining_size, &max_size);
989 if (status != EFI_SUCCESS)
993 * There still isn't enough room, so return an error
995 if (remaining_size - size < EFI_MIN_RESERVE)
996 return EFI_OUT_OF_RESOURCES;
1001 EXPORT_SYMBOL_GPL(efi_query_variable_store);