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 unsigned long x86_efi_facility;
70 static __initdata efi_config_table_type_t arch_tables[] = {
72 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
78 * Returns 1 if 'facility' is enabled, 0 otherwise.
80 int efi_enabled(int facility)
82 return test_bit(facility, &x86_efi_facility) != 0;
84 EXPORT_SYMBOL(efi_enabled);
86 static bool __initdata disable_runtime = false;
87 static int __init setup_noefi(char *arg)
89 disable_runtime = true;
92 early_param("noefi", setup_noefi);
95 EXPORT_SYMBOL(add_efi_memmap);
97 static int __init setup_add_efi_memmap(char *arg)
102 early_param("add_efi_memmap", setup_add_efi_memmap);
104 static bool efi_no_storage_paranoia;
106 static int __init setup_storage_paranoia(char *arg)
108 efi_no_storage_paranoia = true;
111 early_param("efi_no_storage_paranoia", setup_storage_paranoia);
114 static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
119 spin_lock_irqsave(&rtc_lock, flags);
120 status = efi_call_virt2(get_time, tm, tc);
121 spin_unlock_irqrestore(&rtc_lock, flags);
125 static efi_status_t virt_efi_set_time(efi_time_t *tm)
130 spin_lock_irqsave(&rtc_lock, flags);
131 status = efi_call_virt1(set_time, tm);
132 spin_unlock_irqrestore(&rtc_lock, flags);
136 static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
143 spin_lock_irqsave(&rtc_lock, flags);
144 status = efi_call_virt3(get_wakeup_time,
145 enabled, pending, tm);
146 spin_unlock_irqrestore(&rtc_lock, flags);
150 static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
155 spin_lock_irqsave(&rtc_lock, flags);
156 status = efi_call_virt2(set_wakeup_time,
158 spin_unlock_irqrestore(&rtc_lock, flags);
162 static efi_status_t virt_efi_get_variable(efi_char16_t *name,
165 unsigned long *data_size,
168 return efi_call_virt5(get_variable,
173 static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
177 return efi_call_virt3(get_next_variable,
178 name_size, name, vendor);
181 static efi_status_t virt_efi_set_variable(efi_char16_t *name,
184 unsigned long data_size,
187 return efi_call_virt5(set_variable,
192 static efi_status_t virt_efi_query_variable_info(u32 attr,
194 u64 *remaining_space,
195 u64 *max_variable_size)
197 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
198 return EFI_UNSUPPORTED;
200 return efi_call_virt4(query_variable_info, attr, storage_space,
201 remaining_space, max_variable_size);
204 static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
206 return efi_call_virt1(get_next_high_mono_count, count);
209 static void virt_efi_reset_system(int reset_type,
211 unsigned long data_size,
214 efi_call_virt4(reset_system, reset_type, status,
218 static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
220 unsigned long sg_list)
222 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
223 return EFI_UNSUPPORTED;
225 return efi_call_virt3(update_capsule, capsules, count, sg_list);
228 static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
233 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
234 return EFI_UNSUPPORTED;
236 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
240 static efi_status_t __init phys_efi_set_virtual_address_map(
241 unsigned long memory_map_size,
242 unsigned long descriptor_size,
243 u32 descriptor_version,
244 efi_memory_desc_t *virtual_map)
248 efi_call_phys_prelog();
249 status = efi_call_phys4(efi_phys.set_virtual_address_map,
250 memory_map_size, descriptor_size,
251 descriptor_version, virtual_map);
252 efi_call_phys_epilog();
256 static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
262 spin_lock_irqsave(&rtc_lock, flags);
263 efi_call_phys_prelog();
264 status = efi_call_phys2(efi_phys.get_time, virt_to_phys(tm),
266 efi_call_phys_epilog();
267 spin_unlock_irqrestore(&rtc_lock, flags);
271 int efi_set_rtc_mmss(unsigned long nowtime)
278 status = efi.get_time(&eft, &cap);
279 if (status != EFI_SUCCESS) {
280 pr_err("Oops: efitime: can't read time!\n");
284 rtc_time_to_tm(nowtime, &tm);
285 if (!rtc_valid_tm(&tm)) {
286 eft.year = tm.tm_year + 1900;
287 eft.month = tm.tm_mon + 1;
288 eft.day = tm.tm_mday;
289 eft.minute = tm.tm_min;
290 eft.second = tm.tm_sec;
294 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
295 __FUNCTION__, nowtime);
299 status = efi.set_time(&eft);
300 if (status != EFI_SUCCESS) {
301 pr_err("Oops: efitime: can't write time!\n");
307 unsigned long efi_get_time(void)
313 status = efi.get_time(&eft, &cap);
314 if (status != EFI_SUCCESS)
315 pr_err("Oops: efitime: can't read time!\n");
317 return mktime(eft.year, eft.month, eft.day, eft.hour,
318 eft.minute, eft.second);
322 * Tell the kernel about the EFI memory map. This might include
323 * more than the max 128 entries that can fit in the e820 legacy
324 * (zeropage) memory map.
327 static void __init do_add_efi_memmap(void)
331 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
332 efi_memory_desc_t *md = p;
333 unsigned long long start = md->phys_addr;
334 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
338 case EFI_LOADER_CODE:
339 case EFI_LOADER_DATA:
340 case EFI_BOOT_SERVICES_CODE:
341 case EFI_BOOT_SERVICES_DATA:
342 case EFI_CONVENTIONAL_MEMORY:
343 if (md->attribute & EFI_MEMORY_WB)
344 e820_type = E820_RAM;
346 e820_type = E820_RESERVED;
348 case EFI_ACPI_RECLAIM_MEMORY:
349 e820_type = E820_ACPI;
351 case EFI_ACPI_MEMORY_NVS:
352 e820_type = E820_NVS;
354 case EFI_UNUSABLE_MEMORY:
355 e820_type = E820_UNUSABLE;
359 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
360 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
361 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
363 e820_type = E820_RESERVED;
366 e820_add_region(start, size, e820_type);
368 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
371 int __init efi_memblock_x86_reserve_range(void)
373 struct efi_info *e = &boot_params.efi_info;
377 /* Can't handle data above 4GB at this time */
378 if (e->efi_memmap_hi) {
379 pr_err("Memory map is above 4GB, disabling EFI.\n");
382 pmap = e->efi_memmap;
384 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
386 memmap.phys_map = (void *)pmap;
387 memmap.nr_map = e->efi_memmap_size /
389 memmap.desc_size = e->efi_memdesc_size;
390 memmap.desc_version = e->efi_memdesc_version;
392 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
394 efi.memmap = &memmap;
400 static void __init print_efi_memmap(void)
402 efi_memory_desc_t *md;
406 for (p = memmap.map, i = 0;
408 p += memmap.desc_size, i++) {
410 pr_info("mem%02u: type=%u, attr=0x%llx, "
411 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
412 i, md->type, md->attribute, md->phys_addr,
413 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
414 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
417 #endif /* EFI_DEBUG */
419 void __init efi_reserve_boot_services(void)
423 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
424 efi_memory_desc_t *md = p;
425 u64 start = md->phys_addr;
426 u64 size = md->num_pages << EFI_PAGE_SHIFT;
428 if (md->type != EFI_BOOT_SERVICES_CODE &&
429 md->type != EFI_BOOT_SERVICES_DATA)
431 /* Only reserve where possible:
432 * - Not within any already allocated areas
433 * - Not over any memory area (really needed, if above?)
434 * - Not within any part of the kernel
435 * - Not the bios reserved area
437 if ((start+size >= __pa_symbol(_text)
438 && start <= __pa_symbol(_end)) ||
439 !e820_all_mapped(start, start+size, E820_RAM) ||
440 memblock_is_region_reserved(start, size)) {
441 /* Could not reserve, skip it */
443 memblock_dbg("Could not reserve boot range "
444 "[0x%010llx-0x%010llx]\n",
445 start, start+size-1);
447 memblock_reserve(start, size);
451 void __init efi_unmap_memmap(void)
453 clear_bit(EFI_MEMMAP, &x86_efi_facility);
455 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
460 void __init efi_free_boot_services(void)
464 if (!efi_is_native())
467 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
468 efi_memory_desc_t *md = p;
469 unsigned long long start = md->phys_addr;
470 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
472 if (md->type != EFI_BOOT_SERVICES_CODE &&
473 md->type != EFI_BOOT_SERVICES_DATA)
476 /* Could not reserve boot area */
480 free_bootmem_late(start, size);
486 static int __init efi_systab_init(void *phys)
488 if (efi_enabled(EFI_64BIT)) {
489 efi_system_table_64_t *systab64;
492 systab64 = early_ioremap((unsigned long)phys,
494 if (systab64 == NULL) {
495 pr_err("Couldn't map the system table!\n");
499 efi_systab.hdr = systab64->hdr;
500 efi_systab.fw_vendor = systab64->fw_vendor;
501 tmp |= systab64->fw_vendor;
502 efi_systab.fw_revision = systab64->fw_revision;
503 efi_systab.con_in_handle = systab64->con_in_handle;
504 tmp |= systab64->con_in_handle;
505 efi_systab.con_in = systab64->con_in;
506 tmp |= systab64->con_in;
507 efi_systab.con_out_handle = systab64->con_out_handle;
508 tmp |= systab64->con_out_handle;
509 efi_systab.con_out = systab64->con_out;
510 tmp |= systab64->con_out;
511 efi_systab.stderr_handle = systab64->stderr_handle;
512 tmp |= systab64->stderr_handle;
513 efi_systab.stderr = systab64->stderr;
514 tmp |= systab64->stderr;
515 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
516 tmp |= systab64->runtime;
517 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
518 tmp |= systab64->boottime;
519 efi_systab.nr_tables = systab64->nr_tables;
520 efi_systab.tables = systab64->tables;
521 tmp |= systab64->tables;
523 early_iounmap(systab64, sizeof(*systab64));
526 pr_err("EFI data located above 4GB, disabling EFI.\n");
531 efi_system_table_32_t *systab32;
533 systab32 = early_ioremap((unsigned long)phys,
535 if (systab32 == NULL) {
536 pr_err("Couldn't map the system table!\n");
540 efi_systab.hdr = systab32->hdr;
541 efi_systab.fw_vendor = systab32->fw_vendor;
542 efi_systab.fw_revision = systab32->fw_revision;
543 efi_systab.con_in_handle = systab32->con_in_handle;
544 efi_systab.con_in = systab32->con_in;
545 efi_systab.con_out_handle = systab32->con_out_handle;
546 efi_systab.con_out = systab32->con_out;
547 efi_systab.stderr_handle = systab32->stderr_handle;
548 efi_systab.stderr = systab32->stderr;
549 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
550 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
551 efi_systab.nr_tables = systab32->nr_tables;
552 efi_systab.tables = systab32->tables;
554 early_iounmap(systab32, sizeof(*systab32));
557 efi.systab = &efi_systab;
560 * Verify the EFI Table
562 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
563 pr_err("System table signature incorrect!\n");
566 if ((efi.systab->hdr.revision >> 16) == 0)
567 pr_err("Warning: System table version "
568 "%d.%02d, expected 1.00 or greater!\n",
569 efi.systab->hdr.revision >> 16,
570 efi.systab->hdr.revision & 0xffff);
575 static int __init efi_runtime_init(void)
577 efi_runtime_services_t *runtime;
580 * Check out the runtime services table. We need to map
581 * the runtime services table so that we can grab the physical
582 * address of several of the EFI runtime functions, needed to
583 * set the firmware into virtual mode.
585 runtime = early_ioremap((unsigned long)efi.systab->runtime,
586 sizeof(efi_runtime_services_t));
588 pr_err("Could not map the runtime service table!\n");
592 * We will only need *early* access to the following
593 * two EFI runtime services before set_virtual_address_map
596 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
597 efi_phys.set_virtual_address_map =
598 (efi_set_virtual_address_map_t *)
599 runtime->set_virtual_address_map;
601 * Make efi_get_time can be called before entering
604 efi.get_time = phys_efi_get_time;
605 early_iounmap(runtime, sizeof(efi_runtime_services_t));
610 static int __init efi_memmap_init(void)
612 /* Map the EFI memory map */
613 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
614 memmap.nr_map * memmap.desc_size);
615 if (memmap.map == NULL) {
616 pr_err("Could not map the memory map!\n");
619 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
627 void __init efi_init(void)
630 char vendor[100] = "unknown";
635 if (boot_params.efi_info.efi_systab_hi ||
636 boot_params.efi_info.efi_memmap_hi) {
637 pr_info("Table located above 4GB, disabling EFI.\n");
640 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
642 efi_phys.systab = (efi_system_table_t *)
643 (boot_params.efi_info.efi_systab |
644 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
647 if (efi_systab_init(efi_phys.systab))
650 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
653 * Show what we know for posterity
655 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
657 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
661 pr_err("Could not map the firmware vendor!\n");
662 early_iounmap(tmp, 2);
664 pr_info("EFI v%u.%.02u by %s\n",
665 efi.systab->hdr.revision >> 16,
666 efi.systab->hdr.revision & 0xffff, vendor);
668 if (efi_config_init(arch_tables))
671 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
674 * Note: We currently don't support runtime services on an EFI
675 * that doesn't match the kernel 32/64-bit mode.
678 if (!efi_is_native())
679 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
681 if (disable_runtime || efi_runtime_init())
683 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
686 if (efi_memmap_init())
689 set_bit(EFI_MEMMAP, &x86_efi_facility);
692 if (efi_is_native()) {
693 x86_platform.get_wallclock = efi_get_time;
694 x86_platform.set_wallclock = efi_set_rtc_mmss;
703 void __init efi_late_init(void)
708 void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
712 addr = md->virt_addr;
713 npages = md->num_pages;
715 memrange_efi_to_native(&addr, &npages);
718 set_memory_x(addr, npages);
720 set_memory_nx(addr, npages);
723 static void __init runtime_code_page_mkexec(void)
725 efi_memory_desc_t *md;
728 /* Make EFI runtime service code area executable */
729 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
732 if (md->type != EFI_RUNTIME_SERVICES_CODE)
735 efi_set_executable(md, true);
739 void efi_memory_uc(u64 addr, unsigned long size)
741 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
744 npages = round_up(size, page_shift) / page_shift;
745 memrange_efi_to_native(&addr, &npages);
746 set_memory_uc(addr, npages);
750 * This function will switch the EFI runtime services to virtual mode.
751 * Essentially, look through the EFI memmap and map every region that
752 * has the runtime attribute bit set in its memory descriptor and update
753 * that memory descriptor with the virtual address obtained from ioremap().
754 * This enables the runtime services to be called without having to
755 * thunk back into physical mode for every invocation.
757 void __init efi_enter_virtual_mode(void)
759 efi_memory_desc_t *md, *prev_md = NULL;
762 u64 end, systab, start_pfn, end_pfn;
763 void *p, *va, *new_memmap = NULL;
769 * We don't do virtual mode, since we don't do runtime services, on
773 if (!efi_is_native()) {
778 /* Merge contiguous regions of the same type and attribute */
779 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
788 if (prev_md->type != md->type ||
789 prev_md->attribute != md->attribute) {
794 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
796 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
797 prev_md->num_pages += md->num_pages;
798 md->type = EFI_RESERVED_TYPE;
805 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
807 if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
808 md->type != EFI_BOOT_SERVICES_CODE &&
809 md->type != EFI_BOOT_SERVICES_DATA)
812 size = md->num_pages << EFI_PAGE_SHIFT;
813 end = md->phys_addr + size;
815 start_pfn = PFN_DOWN(md->phys_addr);
816 end_pfn = PFN_UP(end);
817 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
818 va = __va(md->phys_addr);
820 if (!(md->attribute & EFI_MEMORY_WB))
821 efi_memory_uc((u64)(unsigned long)va, size);
823 va = efi_ioremap(md->phys_addr, size,
824 md->type, md->attribute);
826 md->virt_addr = (u64) (unsigned long) va;
829 pr_err("ioremap of 0x%llX failed!\n",
830 (unsigned long long)md->phys_addr);
834 systab = (u64) (unsigned long) efi_phys.systab;
835 if (md->phys_addr <= systab && systab < end) {
836 systab += md->virt_addr - md->phys_addr;
837 efi.systab = (efi_system_table_t *) (unsigned long) systab;
839 new_memmap = krealloc(new_memmap,
840 (count + 1) * memmap.desc_size,
842 memcpy(new_memmap + (count * memmap.desc_size), md,
849 status = phys_efi_set_virtual_address_map(
850 memmap.desc_size * count,
853 (efi_memory_desc_t *)__pa(new_memmap));
855 if (status != EFI_SUCCESS) {
856 pr_alert("Unable to switch EFI into virtual mode "
857 "(status=%lx)!\n", status);
858 panic("EFI call to SetVirtualAddressMap() failed!");
862 * Now that EFI is in virtual mode, update the function
863 * pointers in the runtime service table to the new virtual addresses.
865 * Call EFI services through wrapper functions.
867 efi.runtime_version = efi_systab.hdr.revision;
868 efi.get_time = virt_efi_get_time;
869 efi.set_time = virt_efi_set_time;
870 efi.get_wakeup_time = virt_efi_get_wakeup_time;
871 efi.set_wakeup_time = virt_efi_set_wakeup_time;
872 efi.get_variable = virt_efi_get_variable;
873 efi.get_next_variable = virt_efi_get_next_variable;
874 efi.set_variable = virt_efi_set_variable;
875 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
876 efi.reset_system = virt_efi_reset_system;
877 efi.set_virtual_address_map = NULL;
878 efi.query_variable_info = virt_efi_query_variable_info;
879 efi.update_capsule = virt_efi_update_capsule;
880 efi.query_capsule_caps = virt_efi_query_capsule_caps;
881 if (__supported_pte_mask & _PAGE_NX)
882 runtime_code_page_mkexec();
886 /* clean DUMMY object */
887 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
888 EFI_VARIABLE_NON_VOLATILE |
889 EFI_VARIABLE_BOOTSERVICE_ACCESS |
890 EFI_VARIABLE_RUNTIME_ACCESS,
895 * Convenience functions to obtain memory types and attributes
897 u32 efi_mem_type(unsigned long phys_addr)
899 efi_memory_desc_t *md;
902 if (!efi_enabled(EFI_MEMMAP))
905 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
907 if ((md->phys_addr <= phys_addr) &&
908 (phys_addr < (md->phys_addr +
909 (md->num_pages << EFI_PAGE_SHIFT))))
915 u64 efi_mem_attributes(unsigned long phys_addr)
917 efi_memory_desc_t *md;
920 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
922 if ((md->phys_addr <= phys_addr) &&
923 (phys_addr < (md->phys_addr +
924 (md->num_pages << EFI_PAGE_SHIFT))))
925 return md->attribute;
931 * Some firmware has serious problems when using more than 50% of the EFI
932 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
933 * we never use more than this safe limit.
935 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
938 efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
941 u64 storage_size, remaining_size, max_size;
943 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
946 status = efi.query_variable_info(attributes, &storage_size,
947 &remaining_size, &max_size);
948 if (status != EFI_SUCCESS)
952 * Some firmware implementations refuse to boot if there's insufficient
953 * space in the variable store. We account for that by refusing the
954 * write if permitting it would reduce the available space to under
955 * 5KB. This figure was provided by Samsung, so should be safe.
957 if ((remaining_size - size < EFI_MIN_RESERVE) &&
958 !efi_no_storage_paranoia) {
961 * Triggering garbage collection may require that the firmware
962 * generate a real EFI_OUT_OF_RESOURCES error. We can force
963 * that by attempting to use more space than is available.
965 unsigned long dummy_size = remaining_size + 1024;
966 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
969 return EFI_OUT_OF_RESOURCES;
971 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
972 EFI_VARIABLE_NON_VOLATILE |
973 EFI_VARIABLE_BOOTSERVICE_ACCESS |
974 EFI_VARIABLE_RUNTIME_ACCESS,
977 if (status == EFI_SUCCESS) {
979 * This should have failed, so if it didn't make sure
980 * that we delete it...
982 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
983 EFI_VARIABLE_NON_VOLATILE |
984 EFI_VARIABLE_BOOTSERVICE_ACCESS |
985 EFI_VARIABLE_RUNTIME_ACCESS,
992 * The runtime code may now have triggered a garbage collection
993 * run, so check the variable info again
995 status = efi.query_variable_info(attributes, &storage_size,
996 &remaining_size, &max_size);
998 if (status != EFI_SUCCESS)
1002 * There still isn't enough room, so return an error
1004 if (remaining_size - size < EFI_MIN_RESERVE)
1005 return EFI_OUT_OF_RESOURCES;
1010 EXPORT_SYMBOL_GPL(efi_query_variable_store);