blob: cceb813044efc5dfdcbaf9fddbd076b23b2d1dc1 [file] [log] [blame]
Huang, Ying5b836832008-01-30 13:31:19 +01001/*
2 * Common EFI (Extensible Firmware Interface) support functions
3 * Based on Extensible Firmware Interface Specification version 1.0
4 *
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>
15 *
16 * Copied from efi_32.c to eliminate the duplicated code between EFI
17 * 32/64 support code. --ying 2007-10-26
18 *
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
22 *
23 * Implemented EFI runtime services and virtual mode calls. --davidm
24 *
25 * Goutham Rao: <goutham.rao@intel.com>
26 * Skip non-WB memory and ignore empty memory ranges.
27 */
28
Olof Johanssone3cb3f52012-02-12 13:24:26 -080029#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30
Huang, Ying5b836832008-01-30 13:31:19 +010031#include <linux/kernel.h>
32#include <linux/init.h>
33#include <linux/efi.h>
Josh Triplett2223af32012-09-28 17:57:05 -070034#include <linux/efi-bgrt.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040035#include <linux/export.h>
Huang, Ying5b836832008-01-30 13:31:19 +010036#include <linux/bootmem.h>
David Howells0d01ff22013-04-11 23:51:01 +010037#include <linux/slab.h>
Yinghai Lua9ce6bc2010-08-25 13:39:17 -070038#include <linux/memblock.h>
Huang, Ying5b836832008-01-30 13:31:19 +010039#include <linux/spinlock.h>
40#include <linux/uaccess.h>
41#include <linux/time.h>
42#include <linux/io.h>
43#include <linux/reboot.h>
44#include <linux/bcd.h>
45
46#include <asm/setup.h>
47#include <asm/efi.h>
48#include <asm/time.h>
Huang, Yinga2172e22008-01-30 13:33:55 +010049#include <asm/cacheflush.h>
50#include <asm/tlbflush.h>
Feng Tang7bd867d2009-09-10 10:48:56 +080051#include <asm/x86_init.h>
Prarit Bhargava3195ef52013-02-14 12:02:54 -050052#include <asm/rtc.h>
Huang, Ying5b836832008-01-30 13:31:19 +010053
54#define EFI_DEBUG 1
Huang, Ying5b836832008-01-30 13:31:19 +010055
Matthew Garrettf8b84042013-06-01 16:06:20 -040056#define EFI_MIN_RESERVE 5120
57
58#define EFI_DUMMY_GUID \
59 EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
60
61static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
Matthew Garrett31ff2f22013-04-15 13:09:47 -070062
Huang, Ying5b836832008-01-30 13:31:19 +010063struct efi_memory_map memmap;
64
Harvey Harrisonecaea422008-02-13 13:26:13 -080065static struct efi efi_phys __initdata;
Huang, Ying5b836832008-01-30 13:31:19 +010066static efi_system_table_t efi_systab __initdata;
67
Matt Fleming83e68182012-11-14 09:42:35 +000068unsigned long x86_efi_facility;
69
Leif Lindholm272686b2013-09-05 11:34:54 +010070static __initdata efi_config_table_type_t arch_tables[] = {
71#ifdef CONFIG_X86_UV
72 {UV_SYSTEM_TABLE_GUID, "UVsystab", &efi.uv_systab},
73#endif
Leif Lindholm722da9d2013-10-03 15:42:37 +010074 {NULL_GUID, NULL, NULL},
Leif Lindholm272686b2013-09-05 11:34:54 +010075};
76
Matt Fleming83e68182012-11-14 09:42:35 +000077/*
78 * Returns 1 if 'facility' is enabled, 0 otherwise.
79 */
80int efi_enabled(int facility)
81{
82 return test_bit(facility, &x86_efi_facility) != 0;
83}
84EXPORT_SYMBOL(efi_enabled);
85
Matt Fleming058e7b52013-02-22 15:03:47 +000086static bool __initdata disable_runtime = false;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010087static int __init setup_noefi(char *arg)
88{
Matt Flemingfb834c72013-02-20 20:36:12 +000089 disable_runtime = true;
Huang, Ying8b2cb7a2008-01-30 13:32:11 +010090 return 0;
91}
92early_param("noefi", setup_noefi);
93
Paul Jackson200001e2008-06-25 05:44:46 -070094int add_efi_memmap;
95EXPORT_SYMBOL(add_efi_memmap);
96
97static int __init setup_add_efi_memmap(char *arg)
98{
99 add_efi_memmap = 1;
100 return 0;
101}
102early_param("add_efi_memmap", setup_add_efi_memmap);
103
Richard Weinberger8c58bf32013-04-17 01:00:53 +0200104static bool efi_no_storage_paranoia;
105
106static int __init setup_storage_paranoia(char *arg)
107{
108 efi_no_storage_paranoia = true;
109 return 0;
110}
111early_param("efi_no_storage_paranoia", setup_storage_paranoia);
112
Paul Jackson200001e2008-06-25 05:44:46 -0700113
Huang, Ying5b836832008-01-30 13:31:19 +0100114static efi_status_t virt_efi_get_time(efi_time_t *tm, efi_time_cap_t *tc)
115{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100116 unsigned long flags;
117 efi_status_t status;
118
119 spin_lock_irqsave(&rtc_lock, flags);
120 status = efi_call_virt2(get_time, tm, tc);
121 spin_unlock_irqrestore(&rtc_lock, flags);
122 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100123}
124
125static efi_status_t virt_efi_set_time(efi_time_t *tm)
126{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100127 unsigned long flags;
128 efi_status_t status;
129
130 spin_lock_irqsave(&rtc_lock, flags);
131 status = efi_call_virt1(set_time, tm);
132 spin_unlock_irqrestore(&rtc_lock, flags);
133 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100134}
135
136static efi_status_t virt_efi_get_wakeup_time(efi_bool_t *enabled,
137 efi_bool_t *pending,
138 efi_time_t *tm)
139{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100140 unsigned long flags;
141 efi_status_t status;
142
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);
147 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100148}
149
150static efi_status_t virt_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm)
151{
Jan Beulichef68c8f2011-07-19 11:53:07 +0100152 unsigned long flags;
153 efi_status_t status;
154
155 spin_lock_irqsave(&rtc_lock, flags);
156 status = efi_call_virt2(set_wakeup_time,
157 enabled, tm);
158 spin_unlock_irqrestore(&rtc_lock, flags);
159 return status;
Huang, Ying5b836832008-01-30 13:31:19 +0100160}
161
162static efi_status_t virt_efi_get_variable(efi_char16_t *name,
163 efi_guid_t *vendor,
164 u32 *attr,
165 unsigned long *data_size,
166 void *data)
167{
168 return efi_call_virt5(get_variable,
169 name, vendor, attr,
170 data_size, data);
171}
172
173static efi_status_t virt_efi_get_next_variable(unsigned long *name_size,
174 efi_char16_t *name,
175 efi_guid_t *vendor)
176{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400177 return efi_call_virt3(get_next_variable,
178 name_size, name, vendor);
Huang, Ying5b836832008-01-30 13:31:19 +0100179}
180
181static efi_status_t virt_efi_set_variable(efi_char16_t *name,
182 efi_guid_t *vendor,
Matthew Garrettf7a2d732011-06-06 15:36:24 -0400183 u32 attr,
Huang, Ying5b836832008-01-30 13:31:19 +0100184 unsigned long data_size,
185 void *data)
186{
Matthew Garrettf8b84042013-06-01 16:06:20 -0400187 return efi_call_virt5(set_variable,
188 name, vendor, attr,
189 data_size, data);
Huang, Ying5b836832008-01-30 13:31:19 +0100190}
191
Matthew Garrett3b370232011-06-06 15:36:25 -0400192static efi_status_t virt_efi_query_variable_info(u32 attr,
193 u64 *storage_space,
194 u64 *remaining_space,
195 u64 *max_variable_size)
196{
197 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
198 return EFI_UNSUPPORTED;
199
200 return efi_call_virt4(query_variable_info, attr, storage_space,
201 remaining_space, max_variable_size);
202}
203
Huang, Ying5b836832008-01-30 13:31:19 +0100204static efi_status_t virt_efi_get_next_high_mono_count(u32 *count)
205{
206 return efi_call_virt1(get_next_high_mono_count, count);
207}
208
209static void virt_efi_reset_system(int reset_type,
210 efi_status_t status,
211 unsigned long data_size,
212 efi_char16_t *data)
213{
214 efi_call_virt4(reset_system, reset_type, status,
215 data_size, data);
216}
217
Matthew Garrett3b370232011-06-06 15:36:25 -0400218static efi_status_t virt_efi_update_capsule(efi_capsule_header_t **capsules,
219 unsigned long count,
220 unsigned long sg_list)
221{
222 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
223 return EFI_UNSUPPORTED;
224
225 return efi_call_virt3(update_capsule, capsules, count, sg_list);
226}
227
228static efi_status_t virt_efi_query_capsule_caps(efi_capsule_header_t **capsules,
229 unsigned long count,
230 u64 *max_size,
231 int *reset_type)
232{
233 if (efi.runtime_version < EFI_2_00_SYSTEM_TABLE_REVISION)
234 return EFI_UNSUPPORTED;
235
236 return efi_call_virt4(query_capsule_caps, capsules, count, max_size,
237 reset_type);
238}
239
Huang, Ying5b836832008-01-30 13:31:19 +0100240static 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)
245{
246 efi_status_t status;
247
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();
253 return status;
254}
255
Linus Torvalds11520e52012-12-15 15:15:24 -0800256static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
257 efi_time_cap_t *tc)
258{
259 unsigned long flags;
260 efi_status_t status;
261
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),
265 virt_to_phys(tc));
266 efi_call_phys_epilog();
267 spin_unlock_irqrestore(&rtc_lock, flags);
268 return status;
269}
270
David Vrabel35651842013-05-13 18:56:06 +0100271int efi_set_rtc_mmss(const struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100272{
David Vrabel35651842013-05-13 18:56:06 +0100273 unsigned long nowtime = now->tv_sec;
Huang, Ying5b836832008-01-30 13:31:19 +0100274 efi_status_t status;
275 efi_time_t eft;
276 efi_time_cap_t cap;
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500277 struct rtc_time tm;
Huang, Ying5b836832008-01-30 13:31:19 +0100278
279 status = efi.get_time(&eft, &cap);
280 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800281 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100282 return -1;
283 }
284
Prarit Bhargava3195ef52013-02-14 12:02:54 -0500285 rtc_time_to_tm(nowtime, &tm);
286 if (!rtc_valid_tm(&tm)) {
287 eft.year = tm.tm_year + 1900;
288 eft.month = tm.tm_mon + 1;
289 eft.day = tm.tm_mday;
290 eft.minute = tm.tm_min;
291 eft.second = tm.tm_sec;
292 eft.nanosecond = 0;
293 } else {
294 printk(KERN_ERR
295 "%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
296 __FUNCTION__, nowtime);
297 return -1;
298 }
Huang, Ying5b836832008-01-30 13:31:19 +0100299
300 status = efi.set_time(&eft);
301 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800302 pr_err("Oops: efitime: can't write time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100303 return -1;
304 }
305 return 0;
306}
307
David Vrabel35651842013-05-13 18:56:06 +0100308void efi_get_time(struct timespec *now)
Huang, Ying5b836832008-01-30 13:31:19 +0100309{
310 efi_status_t status;
311 efi_time_t eft;
312 efi_time_cap_t cap;
313
314 status = efi.get_time(&eft, &cap);
315 if (status != EFI_SUCCESS)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800316 pr_err("Oops: efitime: can't read time!\n");
Huang, Ying5b836832008-01-30 13:31:19 +0100317
David Vrabel35651842013-05-13 18:56:06 +0100318 now->tv_sec = mktime(eft.year, eft.month, eft.day, eft.hour,
319 eft.minute, eft.second);
320 now->tv_nsec = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100321}
322
Paul Jackson69c91892008-05-14 08:15:58 -0700323/*
324 * Tell the kernel about the EFI memory map. This might include
325 * more than the max 128 entries that can fit in the e820 legacy
326 * (zeropage) memory map.
327 */
328
Paul Jackson200001e2008-06-25 05:44:46 -0700329static void __init do_add_efi_memmap(void)
Paul Jackson69c91892008-05-14 08:15:58 -0700330{
331 void *p;
332
333 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
334 efi_memory_desc_t *md = p;
335 unsigned long long start = md->phys_addr;
336 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
337 int e820_type;
338
Cliff Wickmane2a71472009-06-16 16:43:40 -0500339 switch (md->type) {
340 case EFI_LOADER_CODE:
341 case EFI_LOADER_DATA:
342 case EFI_BOOT_SERVICES_CODE:
343 case EFI_BOOT_SERVICES_DATA:
344 case EFI_CONVENTIONAL_MEMORY:
345 if (md->attribute & EFI_MEMORY_WB)
346 e820_type = E820_RAM;
347 else
348 e820_type = E820_RESERVED;
349 break;
350 case EFI_ACPI_RECLAIM_MEMORY:
351 e820_type = E820_ACPI;
352 break;
353 case EFI_ACPI_MEMORY_NVS:
354 e820_type = E820_NVS;
355 break;
356 case EFI_UNUSABLE_MEMORY:
357 e820_type = E820_UNUSABLE;
358 break;
359 default:
360 /*
361 * EFI_RESERVED_TYPE EFI_RUNTIME_SERVICES_CODE
362 * EFI_RUNTIME_SERVICES_DATA EFI_MEMORY_MAPPED_IO
363 * EFI_MEMORY_MAPPED_IO_PORT_SPACE EFI_PAL_CODE
364 */
Paul Jackson69c91892008-05-14 08:15:58 -0700365 e820_type = E820_RESERVED;
Cliff Wickmane2a71472009-06-16 16:43:40 -0500366 break;
367 }
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700368 e820_add_region(start, size, e820_type);
Paul Jackson69c91892008-05-14 08:15:58 -0700369 }
370 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
371}
372
Olof Johansson1adbfa32012-02-12 13:24:29 -0800373int __init efi_memblock_x86_reserve_range(void)
Huang, Yingecacf092008-06-02 14:26:21 +0800374{
Borislav Petkov15b9c352013-03-01 17:07:44 +0100375 struct efi_info *e = &boot_params.efi_info;
Huang, Yingecacf092008-06-02 14:26:21 +0800376 unsigned long pmap;
377
Paul Jackson05486fa2008-06-22 07:22:02 -0700378#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800379 /* Can't handle data above 4GB at this time */
Borislav Petkov15b9c352013-03-01 17:07:44 +0100380 if (e->efi_memmap_hi) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800381 pr_err("Memory map is above 4GB, disabling EFI.\n");
382 return -EINVAL;
383 }
Borislav Petkov15b9c352013-03-01 17:07:44 +0100384 pmap = e->efi_memmap;
Paul Jackson05486fa2008-06-22 07:22:02 -0700385#else
Borislav Petkov15b9c352013-03-01 17:07:44 +0100386 pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
Huang, Yingecacf092008-06-02 14:26:21 +0800387#endif
Borislav Petkov15b9c352013-03-01 17:07:44 +0100388 memmap.phys_map = (void *)pmap;
389 memmap.nr_map = e->efi_memmap_size /
390 e->efi_memdesc_size;
391 memmap.desc_size = e->efi_memdesc_size;
392 memmap.desc_version = e->efi_memdesc_version;
393
Tejun Heo24aa0782011-07-12 11:16:06 +0200394 memblock_reserve(pmap, memmap.nr_map * memmap.desc_size);
Olof Johansson1adbfa32012-02-12 13:24:29 -0800395
Leif Lindholm258f6fd2013-09-05 11:34:55 +0100396 efi.memmap = &memmap;
397
Olof Johansson1adbfa32012-02-12 13:24:29 -0800398 return 0;
Huang, Yingecacf092008-06-02 14:26:21 +0800399}
400
Huang, Ying5b836832008-01-30 13:31:19 +0100401#if EFI_DEBUG
402static void __init print_efi_memmap(void)
403{
404 efi_memory_desc_t *md;
405 void *p;
406 int i;
407
408 for (p = memmap.map, i = 0;
409 p < memmap.map_end;
410 p += memmap.desc_size, i++) {
411 md = p;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800412 pr_info("mem%02u: type=%u, attr=0x%llx, "
Huang, Ying5b836832008-01-30 13:31:19 +0100413 "range=[0x%016llx-0x%016llx) (%lluMB)\n",
414 i, md->type, md->attribute, md->phys_addr,
415 md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT),
416 (md->num_pages >> (20 - EFI_PAGE_SHIFT)));
417 }
418}
419#endif /* EFI_DEBUG */
420
Matthew Garrett916f6762011-05-25 09:53:13 -0400421void __init efi_reserve_boot_services(void)
422{
423 void *p;
424
425 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
426 efi_memory_desc_t *md = p;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200427 u64 start = md->phys_addr;
428 u64 size = md->num_pages << EFI_PAGE_SHIFT;
Matthew Garrett916f6762011-05-25 09:53:13 -0400429
430 if (md->type != EFI_BOOT_SERVICES_CODE &&
431 md->type != EFI_BOOT_SERVICES_DATA)
432 continue;
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200433 /* Only reserve where possible:
434 * - Not within any already allocated areas
435 * - Not over any memory area (really needed, if above?)
436 * - Not within any part of the kernel
437 * - Not the bios reserved area
438 */
Alexander Duyckfc8d7822012-11-16 13:57:13 -0800439 if ((start+size >= __pa_symbol(_text)
440 && start <= __pa_symbol(_end)) ||
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200441 !e820_all_mapped(start, start+size, E820_RAM) ||
Tejun Heobf615492011-07-12 09:58:05 +0200442 memblock_is_region_reserved(start, size)) {
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200443 /* Could not reserve, skip it */
444 md->num_pages = 0;
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800445 memblock_dbg("Could not reserve boot range "
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200446 "[0x%010llx-0x%010llx]\n",
447 start, start+size-1);
448 } else
Tejun Heo24aa0782011-07-12 11:16:06 +0200449 memblock_reserve(start, size);
Matthew Garrett916f6762011-05-25 09:53:13 -0400450 }
451}
452
Olof Johansson5189c2a2012-10-24 10:00:44 -0700453void __init efi_unmap_memmap(void)
Josh Triplett78510792012-09-28 17:55:44 -0700454{
Matt Fleming83e68182012-11-14 09:42:35 +0000455 clear_bit(EFI_MEMMAP, &x86_efi_facility);
Josh Triplett78510792012-09-28 17:55:44 -0700456 if (memmap.map) {
457 early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size);
458 memmap.map = NULL;
459 }
460}
461
462void __init efi_free_boot_services(void)
Matthew Garrett916f6762011-05-25 09:53:13 -0400463{
464 void *p;
465
Olof Johansson5189c2a2012-10-24 10:00:44 -0700466 if (!efi_is_native())
Josh Triplett78510792012-09-28 17:55:44 -0700467 return;
468
Matthew Garrett916f6762011-05-25 09:53:13 -0400469 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
470 efi_memory_desc_t *md = p;
471 unsigned long long start = md->phys_addr;
472 unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
473
474 if (md->type != EFI_BOOT_SERVICES_CODE &&
475 md->type != EFI_BOOT_SERVICES_DATA)
476 continue;
477
Maarten Lankhorst7d68dc32011-06-14 19:53:09 +0200478 /* Could not reserve boot area */
479 if (!size)
480 continue;
481
Matthew Garrett916f6762011-05-25 09:53:13 -0400482 free_bootmem_late(start, size);
483 }
Josh Triplett78510792012-09-28 17:55:44 -0700484
485 efi_unmap_memmap();
Matthew Garrett916f6762011-05-25 09:53:13 -0400486}
487
Olof Johansson140bf272012-02-12 13:24:28 -0800488static int __init efi_systab_init(void *phys)
Huang, Ying5b836832008-01-30 13:31:19 +0100489{
Matt Fleming83e68182012-11-14 09:42:35 +0000490 if (efi_enabled(EFI_64BIT)) {
Olof Johansson1adbfa32012-02-12 13:24:29 -0800491 efi_system_table_64_t *systab64;
492 u64 tmp = 0;
493
494 systab64 = early_ioremap((unsigned long)phys,
495 sizeof(*systab64));
496 if (systab64 == NULL) {
497 pr_err("Couldn't map the system table!\n");
498 return -ENOMEM;
499 }
500
501 efi_systab.hdr = systab64->hdr;
502 efi_systab.fw_vendor = systab64->fw_vendor;
503 tmp |= systab64->fw_vendor;
504 efi_systab.fw_revision = systab64->fw_revision;
505 efi_systab.con_in_handle = systab64->con_in_handle;
506 tmp |= systab64->con_in_handle;
507 efi_systab.con_in = systab64->con_in;
508 tmp |= systab64->con_in;
509 efi_systab.con_out_handle = systab64->con_out_handle;
510 tmp |= systab64->con_out_handle;
511 efi_systab.con_out = systab64->con_out;
512 tmp |= systab64->con_out;
513 efi_systab.stderr_handle = systab64->stderr_handle;
514 tmp |= systab64->stderr_handle;
515 efi_systab.stderr = systab64->stderr;
516 tmp |= systab64->stderr;
517 efi_systab.runtime = (void *)(unsigned long)systab64->runtime;
518 tmp |= systab64->runtime;
519 efi_systab.boottime = (void *)(unsigned long)systab64->boottime;
520 tmp |= systab64->boottime;
521 efi_systab.nr_tables = systab64->nr_tables;
522 efi_systab.tables = systab64->tables;
523 tmp |= systab64->tables;
524
525 early_iounmap(systab64, sizeof(*systab64));
526#ifdef CONFIG_X86_32
527 if (tmp >> 32) {
528 pr_err("EFI data located above 4GB, disabling EFI.\n");
529 return -EINVAL;
530 }
531#endif
532 } else {
533 efi_system_table_32_t *systab32;
534
535 systab32 = early_ioremap((unsigned long)phys,
536 sizeof(*systab32));
537 if (systab32 == NULL) {
538 pr_err("Couldn't map the system table!\n");
539 return -ENOMEM;
540 }
541
542 efi_systab.hdr = systab32->hdr;
543 efi_systab.fw_vendor = systab32->fw_vendor;
544 efi_systab.fw_revision = systab32->fw_revision;
545 efi_systab.con_in_handle = systab32->con_in_handle;
546 efi_systab.con_in = systab32->con_in;
547 efi_systab.con_out_handle = systab32->con_out_handle;
548 efi_systab.con_out = systab32->con_out;
549 efi_systab.stderr_handle = systab32->stderr_handle;
550 efi_systab.stderr = systab32->stderr;
551 efi_systab.runtime = (void *)(unsigned long)systab32->runtime;
552 efi_systab.boottime = (void *)(unsigned long)systab32->boottime;
553 efi_systab.nr_tables = systab32->nr_tables;
554 efi_systab.tables = systab32->tables;
555
556 early_iounmap(systab32, sizeof(*systab32));
Olof Johansson140bf272012-02-12 13:24:28 -0800557 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800558
Huang, Ying5b836832008-01-30 13:31:19 +0100559 efi.systab = &efi_systab;
560
561 /*
562 * Verify the EFI Table
563 */
Olof Johansson140bf272012-02-12 13:24:28 -0800564 if (efi.systab->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800565 pr_err("System table signature incorrect!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800566 return -EINVAL;
567 }
Huang, Ying5b836832008-01-30 13:31:19 +0100568 if ((efi.systab->hdr.revision >> 16) == 0)
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800569 pr_err("Warning: System table version "
Huang, Ying5b836832008-01-30 13:31:19 +0100570 "%d.%02d, expected 1.00 or greater!\n",
571 efi.systab->hdr.revision >> 16,
572 efi.systab->hdr.revision & 0xffff);
Olof Johansson140bf272012-02-12 13:24:28 -0800573
574 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800575}
Huang, Ying5b836832008-01-30 13:31:19 +0100576
Olof Johansson140bf272012-02-12 13:24:28 -0800577static int __init efi_runtime_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800578{
579 efi_runtime_services_t *runtime;
Huang, Ying5b836832008-01-30 13:31:19 +0100580
581 /*
582 * Check out the runtime services table. We need to map
583 * the runtime services table so that we can grab the physical
584 * address of several of the EFI runtime functions, needed to
585 * set the firmware into virtual mode.
586 */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100587 runtime = early_ioremap((unsigned long)efi.systab->runtime,
588 sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800589 if (!runtime) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800590 pr_err("Could not map the runtime service table!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800591 return -ENOMEM;
592 }
593 /*
594 * We will only need *early* access to the following
Linus Torvalds11520e52012-12-15 15:15:24 -0800595 * two EFI runtime services before set_virtual_address_map
Olof Johansson140bf272012-02-12 13:24:28 -0800596 * is invoked.
597 */
Linus Torvalds11520e52012-12-15 15:15:24 -0800598 efi_phys.get_time = (efi_get_time_t *)runtime->get_time;
Olof Johansson140bf272012-02-12 13:24:28 -0800599 efi_phys.set_virtual_address_map =
600 (efi_set_virtual_address_map_t *)
601 runtime->set_virtual_address_map;
Linus Torvalds11520e52012-12-15 15:15:24 -0800602 /*
603 * Make efi_get_time can be called before entering
604 * virtual mode.
605 */
606 efi.get_time = phys_efi_get_time;
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100607 early_iounmap(runtime, sizeof(efi_runtime_services_t));
Olof Johansson140bf272012-02-12 13:24:28 -0800608
609 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800610}
Huang, Ying5b836832008-01-30 13:31:19 +0100611
Olof Johansson140bf272012-02-12 13:24:28 -0800612static int __init efi_memmap_init(void)
Olof Johansson83e7ee62012-02-12 13:24:25 -0800613{
Huang, Ying5b836832008-01-30 13:31:19 +0100614 /* Map the EFI memory map */
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100615 memmap.map = early_ioremap((unsigned long)memmap.phys_map,
616 memmap.nr_map * memmap.desc_size);
Olof Johansson140bf272012-02-12 13:24:28 -0800617 if (memmap.map == NULL) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800618 pr_err("Could not map the memory map!\n");
Olof Johansson140bf272012-02-12 13:24:28 -0800619 return -ENOMEM;
620 }
Huang, Ying5b836832008-01-30 13:31:19 +0100621 memmap.map_end = memmap.map + (memmap.nr_map * memmap.desc_size);
Russ Anderson175e4382008-10-02 17:32:06 -0500622
Paul Jackson200001e2008-06-25 05:44:46 -0700623 if (add_efi_memmap)
624 do_add_efi_memmap();
Olof Johansson140bf272012-02-12 13:24:28 -0800625
626 return 0;
Olof Johansson83e7ee62012-02-12 13:24:25 -0800627}
628
629void __init efi_init(void)
630{
631 efi_char16_t *c16;
632 char vendor[100] = "unknown";
633 int i = 0;
634 void *tmp;
635
636#ifdef CONFIG_X86_32
Olof Johansson1adbfa32012-02-12 13:24:29 -0800637 if (boot_params.efi_info.efi_systab_hi ||
638 boot_params.efi_info.efi_memmap_hi) {
639 pr_info("Table located above 4GB, disabling EFI.\n");
Olof Johansson1adbfa32012-02-12 13:24:29 -0800640 return;
641 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800642 efi_phys.systab = (efi_system_table_t *)boot_params.efi_info.efi_systab;
643#else
644 efi_phys.systab = (efi_system_table_t *)
Olof Johansson1adbfa32012-02-12 13:24:29 -0800645 (boot_params.efi_info.efi_systab |
646 ((__u64)boot_params.efi_info.efi_systab_hi<<32));
Olof Johansson83e7ee62012-02-12 13:24:25 -0800647#endif
648
Matt Fleming83e68182012-11-14 09:42:35 +0000649 if (efi_systab_init(efi_phys.systab))
Olof Johansson140bf272012-02-12 13:24:28 -0800650 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000651
652 set_bit(EFI_SYSTEM_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800653
654 /*
655 * Show what we know for posterity
656 */
657 c16 = tmp = early_ioremap(efi.systab->fw_vendor, 2);
658 if (c16) {
659 for (i = 0; i < sizeof(vendor) - 1 && *c16; ++i)
660 vendor[i] = *c16++;
661 vendor[i] = '\0';
662 } else
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800663 pr_err("Could not map the firmware vendor!\n");
Olof Johansson83e7ee62012-02-12 13:24:25 -0800664 early_iounmap(tmp, 2);
665
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800666 pr_info("EFI v%u.%.02u by %s\n",
667 efi.systab->hdr.revision >> 16,
668 efi.systab->hdr.revision & 0xffff, vendor);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800669
Leif Lindholm272686b2013-09-05 11:34:54 +0100670 if (efi_config_init(arch_tables))
Olof Johansson140bf272012-02-12 13:24:28 -0800671 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000672
673 set_bit(EFI_CONFIG_TABLES, &x86_efi_facility);
Olof Johansson83e7ee62012-02-12 13:24:25 -0800674
Olof Johansson1adbfa32012-02-12 13:24:29 -0800675 /*
676 * Note: We currently don't support runtime services on an EFI
677 * that doesn't match the kernel 32/64-bit mode.
678 */
679
Olof Johansson5189c2a2012-10-24 10:00:44 -0700680 if (!efi_is_native())
Olof Johansson1adbfa32012-02-12 13:24:29 -0800681 pr_info("No EFI runtime due to 32/64-bit mismatch with kernel\n");
Matt Fleming83e68182012-11-14 09:42:35 +0000682 else {
Matt Flemingfb834c72013-02-20 20:36:12 +0000683 if (disable_runtime || efi_runtime_init())
Matt Fleming83e68182012-11-14 09:42:35 +0000684 return;
685 set_bit(EFI_RUNTIME_SERVICES, &x86_efi_facility);
Olof Johansson140bf272012-02-12 13:24:28 -0800686 }
Olof Johansson83e7ee62012-02-12 13:24:25 -0800687
Matt Fleming83e68182012-11-14 09:42:35 +0000688 if (efi_memmap_init())
Olof Johansson140bf272012-02-12 13:24:28 -0800689 return;
Matt Fleming83e68182012-11-14 09:42:35 +0000690
691 set_bit(EFI_MEMMAP, &x86_efi_facility);
692
Huang, Ying5b836832008-01-30 13:31:19 +0100693#if EFI_DEBUG
694 print_efi_memmap();
695#endif
696}
697
Josh Triplett2223af32012-09-28 17:57:05 -0700698void __init efi_late_init(void)
699{
700 efi_bgrt_init();
701}
702
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400703void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
704{
705 u64 addr, npages;
706
707 addr = md->virt_addr;
708 npages = md->num_pages;
709
710 memrange_efi_to_native(&addr, &npages);
711
712 if (executable)
713 set_memory_x(addr, npages);
714 else
715 set_memory_nx(addr, npages);
716}
717
Huang, Yinga2172e22008-01-30 13:33:55 +0100718static void __init runtime_code_page_mkexec(void)
719{
720 efi_memory_desc_t *md;
Huang, Yinga2172e22008-01-30 13:33:55 +0100721 void *p;
722
Huang, Yinga2172e22008-01-30 13:33:55 +0100723 /* Make EFI runtime service code area executable */
724 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
725 md = p;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100726
727 if (md->type != EFI_RUNTIME_SERVICES_CODE)
728 continue;
729
Matthew Garrett9cd2b072011-05-05 15:19:43 -0400730 efi_set_executable(md, true);
Huang, Yinga2172e22008-01-30 13:33:55 +0100731 }
Huang, Yinga2172e22008-01-30 13:33:55 +0100732}
Huang, Yinga2172e22008-01-30 13:33:55 +0100733
Matt Fleming3e8fa262012-10-19 13:25:46 +0100734void efi_memory_uc(u64 addr, unsigned long size)
735{
736 unsigned long page_shift = 1UL << EFI_PAGE_SHIFT;
737 u64 npages;
738
739 npages = round_up(size, page_shift) / page_shift;
740 memrange_efi_to_native(&addr, &npages);
741 set_memory_uc(addr, npages);
742}
743
Josh Triplett7bc90e02012-09-28 17:56:08 -0700744/*
Huang, Ying5b836832008-01-30 13:31:19 +0100745 * This function will switch the EFI runtime services to virtual mode.
746 * Essentially, look through the EFI memmap and map every region that
747 * has the runtime attribute bit set in its memory descriptor and update
748 * that memory descriptor with the virtual address obtained from ioremap().
749 * This enables the runtime services to be called without having to
750 * thunk back into physical mode for every invocation.
751 */
752void __init efi_enter_virtual_mode(void)
753{
Matthew Garrett202f9d02011-05-05 15:19:44 -0400754 efi_memory_desc_t *md, *prev_md = NULL;
Huang, Ying5b836832008-01-30 13:31:19 +0100755 efi_status_t status;
Huang, Ying1c083eb2008-02-04 16:48:06 +0100756 unsigned long size;
Jacob Shindda56e12012-11-16 19:38:48 -0800757 u64 end, systab, start_pfn, end_pfn;
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400758 void *p, *va, *new_memmap = NULL;
759 int count = 0;
Huang, Ying5b836832008-01-30 13:31:19 +0100760
761 efi.systab = NULL;
Matthew Garrett202f9d02011-05-05 15:19:44 -0400762
Olof Johansson1adbfa32012-02-12 13:24:29 -0800763 /*
764 * We don't do virtual mode, since we don't do runtime services, on
765 * non-native EFI
766 */
767
Olof Johansson5189c2a2012-10-24 10:00:44 -0700768 if (!efi_is_native()) {
Josh Triplett78510792012-09-28 17:55:44 -0700769 efi_unmap_memmap();
770 return;
771 }
Olof Johansson1adbfa32012-02-12 13:24:29 -0800772
Matthew Garrett202f9d02011-05-05 15:19:44 -0400773 /* Merge contiguous regions of the same type and attribute */
774 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
775 u64 prev_size;
776 md = p;
777
778 if (!prev_md) {
779 prev_md = md;
780 continue;
781 }
782
783 if (prev_md->type != md->type ||
784 prev_md->attribute != md->attribute) {
785 prev_md = md;
786 continue;
787 }
788
789 prev_size = prev_md->num_pages << EFI_PAGE_SHIFT;
790
791 if (md->phys_addr == (prev_md->phys_addr + prev_size)) {
792 prev_md->num_pages += md->num_pages;
793 md->type = EFI_RESERVED_TYPE;
794 md->attribute = 0;
795 continue;
796 }
797 prev_md = md;
798 }
799
Huang, Ying5b836832008-01-30 13:31:19 +0100800 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
801 md = p;
Josh Boyer70087012013-04-18 07:51:34 -0700802 if (!(md->attribute & EFI_MEMORY_RUNTIME)) {
803#ifdef CONFIG_X86_64
804 if (md->type != EFI_BOOT_SERVICES_CODE &&
805 md->type != EFI_BOOT_SERVICES_DATA)
806#endif
807 continue;
808 }
Huang, Ying1c083eb2008-02-04 16:48:06 +0100809
810 size = md->num_pages << EFI_PAGE_SHIFT;
811 end = md->phys_addr + size;
812
Jacob Shindda56e12012-11-16 19:38:48 -0800813 start_pfn = PFN_DOWN(md->phys_addr);
Huang Yingdd39ecf2009-03-04 10:58:33 +0800814 end_pfn = PFN_UP(end);
Jacob Shindda56e12012-11-16 19:38:48 -0800815 if (pfn_range_is_mapped(start_pfn, end_pfn)) {
Huang, Ying1c083eb2008-02-04 16:48:06 +0100816 va = __va(md->phys_addr);
Matt Fleming3e8fa262012-10-19 13:25:46 +0100817
818 if (!(md->attribute & EFI_MEMORY_WB))
819 efi_memory_uc((u64)(unsigned long)va, size);
820 } else
821 va = efi_ioremap(md->phys_addr, size,
822 md->type, md->attribute);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100823
Huang, Ying1c083eb2008-02-04 16:48:06 +0100824 md->virt_addr = (u64) (unsigned long) va;
825
826 if (!va) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800827 pr_err("ioremap of 0x%llX failed!\n",
Huang, Ying5b836832008-01-30 13:31:19 +0100828 (unsigned long long)md->phys_addr);
Huang, Ying1c083eb2008-02-04 16:48:06 +0100829 continue;
830 }
831
832 systab = (u64) (unsigned long) efi_phys.systab;
833 if (md->phys_addr <= systab && systab < end) {
834 systab += md->virt_addr - md->phys_addr;
835 efi.systab = (efi_system_table_t *) (unsigned long) systab;
836 }
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400837 new_memmap = krealloc(new_memmap,
838 (count + 1) * memmap.desc_size,
839 GFP_KERNEL);
840 memcpy(new_memmap + (count * memmap.desc_size), md,
841 memmap.desc_size);
842 count++;
Huang, Ying5b836832008-01-30 13:31:19 +0100843 }
844
845 BUG_ON(!efi.systab);
846
847 status = phys_efi_set_virtual_address_map(
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400848 memmap.desc_size * count,
Huang, Ying5b836832008-01-30 13:31:19 +0100849 memmap.desc_size,
850 memmap.desc_version,
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400851 (efi_memory_desc_t *)__pa(new_memmap));
Huang, Ying5b836832008-01-30 13:31:19 +0100852
853 if (status != EFI_SUCCESS) {
Olof Johanssone3cb3f52012-02-12 13:24:26 -0800854 pr_alert("Unable to switch EFI into virtual mode "
855 "(status=%lx)!\n", status);
Huang, Ying5b836832008-01-30 13:31:19 +0100856 panic("EFI call to SetVirtualAddressMap() failed!");
857 }
858
859 /*
860 * Now that EFI is in virtual mode, update the function
861 * pointers in the runtime service table to the new virtual addresses.
862 *
863 * Call EFI services through wrapper functions.
864 */
Matt Fleming712ba9e2013-01-25 10:07:25 +0000865 efi.runtime_version = efi_systab.hdr.revision;
Huang, Ying5b836832008-01-30 13:31:19 +0100866 efi.get_time = virt_efi_get_time;
867 efi.set_time = virt_efi_set_time;
868 efi.get_wakeup_time = virt_efi_get_wakeup_time;
869 efi.set_wakeup_time = virt_efi_set_wakeup_time;
870 efi.get_variable = virt_efi_get_variable;
871 efi.get_next_variable = virt_efi_get_next_variable;
872 efi.set_variable = virt_efi_set_variable;
873 efi.get_next_high_mono_count = virt_efi_get_next_high_mono_count;
874 efi.reset_system = virt_efi_reset_system;
Matthew Garrett2b5e8ef2011-05-05 15:19:42 -0400875 efi.set_virtual_address_map = NULL;
Matthew Garrett3b370232011-06-06 15:36:25 -0400876 efi.query_variable_info = virt_efi_query_variable_info;
877 efi.update_capsule = virt_efi_update_capsule;
878 efi.query_capsule_caps = virt_efi_query_capsule_caps;
Huang, Ying4de0d4a2008-02-13 17:22:41 +0800879 if (__supported_pte_mask & _PAGE_NX)
880 runtime_code_page_mkexec();
Olof Johansson1adbfa32012-02-12 13:24:29 -0800881
Matthew Garrett7cb00b72011-05-05 15:19:45 -0400882 kfree(new_memmap);
Matthew Garrettf8b84042013-06-01 16:06:20 -0400883
884 /* clean DUMMY object */
885 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
886 EFI_VARIABLE_NON_VOLATILE |
887 EFI_VARIABLE_BOOTSERVICE_ACCESS |
888 EFI_VARIABLE_RUNTIME_ACCESS,
889 0, NULL);
Huang, Ying5b836832008-01-30 13:31:19 +0100890}
891
892/*
893 * Convenience functions to obtain memory types and attributes
894 */
895u32 efi_mem_type(unsigned long phys_addr)
896{
897 efi_memory_desc_t *md;
898 void *p;
899
Matt Fleming83e68182012-11-14 09:42:35 +0000900 if (!efi_enabled(EFI_MEMMAP))
901 return 0;
902
Huang, Ying5b836832008-01-30 13:31:19 +0100903 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
904 md = p;
905 if ((md->phys_addr <= phys_addr) &&
906 (phys_addr < (md->phys_addr +
907 (md->num_pages << EFI_PAGE_SHIFT))))
908 return md->type;
909 }
910 return 0;
911}
912
913u64 efi_mem_attributes(unsigned long phys_addr)
914{
915 efi_memory_desc_t *md;
916 void *p;
917
918 for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
919 md = p;
920 if ((md->phys_addr <= phys_addr) &&
921 (phys_addr < (md->phys_addr +
922 (md->num_pages << EFI_PAGE_SHIFT))))
923 return md->attribute;
924 }
925 return 0;
926}
Matt Fleminga6e4d5a2013-03-25 09:14:30 +0000927
928/*
929 * Some firmware has serious problems when using more than 50% of the EFI
930 * variable store, i.e. it triggers bugs that can brick machines. Ensure that
931 * we never use more than this safe limit.
932 *
933 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
934 * store.
935 */
936efi_status_t efi_query_variable_store(u32 attributes, unsigned long size)
937{
938 efi_status_t status;
939 u64 storage_size, remaining_size, max_size;
940
Matthew Garrettf8b84042013-06-01 16:06:20 -0400941 if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
942 return 0;
943
Matt Fleminga6e4d5a2013-03-25 09:14:30 +0000944 status = efi.query_variable_info(attributes, &storage_size,
945 &remaining_size, &max_size);
946 if (status != EFI_SUCCESS)
947 return status;
948
Matthew Garrett31ff2f22013-04-15 13:09:47 -0700949 /*
950 * Some firmware implementations refuse to boot if there's insufficient
951 * space in the variable store. We account for that by refusing the
952 * write if permitting it would reduce the available space to under
Matthew Garrettf8b84042013-06-01 16:06:20 -0400953 * 5KB. This figure was provided by Samsung, so should be safe.
Matthew Garrett31ff2f22013-04-15 13:09:47 -0700954 */
Matthew Garrettf8b84042013-06-01 16:06:20 -0400955 if ((remaining_size - size < EFI_MIN_RESERVE) &&
956 !efi_no_storage_paranoia) {
Richard Weinberger7791c842013-04-10 10:59:34 +0200957
Matthew Garrettf8b84042013-06-01 16:06:20 -0400958 /*
959 * Triggering garbage collection may require that the firmware
960 * generate a real EFI_OUT_OF_RESOURCES error. We can force
961 * that by attempting to use more space than is available.
962 */
963 unsigned long dummy_size = remaining_size + 1024;
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +0100964 void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
965
966 if (!dummy)
967 return EFI_OUT_OF_RESOURCES;
Richard Weinberger8c58bf32013-04-17 01:00:53 +0200968
Matthew Garrettf8b84042013-06-01 16:06:20 -0400969 status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
970 EFI_VARIABLE_NON_VOLATILE |
971 EFI_VARIABLE_BOOTSERVICE_ACCESS |
972 EFI_VARIABLE_RUNTIME_ACCESS,
973 dummy_size, dummy);
974
975 if (status == EFI_SUCCESS) {
976 /*
977 * This should have failed, so if it didn't make sure
978 * that we delete it...
979 */
980 efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
981 EFI_VARIABLE_NON_VOLATILE |
982 EFI_VARIABLE_BOOTSERVICE_ACCESS |
983 EFI_VARIABLE_RUNTIME_ACCESS,
984 0, dummy);
985 }
986
Ben Hutchingsb8cb62f2013-06-16 21:27:12 +0100987 kfree(dummy);
988
Matthew Garrettf8b84042013-06-01 16:06:20 -0400989 /*
990 * The runtime code may now have triggered a garbage collection
991 * run, so check the variable info again
992 */
993 status = efi.query_variable_info(attributes, &storage_size,
994 &remaining_size, &max_size);
995
996 if (status != EFI_SUCCESS)
997 return status;
998
999 /*
1000 * There still isn't enough room, so return an error
1001 */
1002 if (remaining_size - size < EFI_MIN_RESERVE)
1003 return EFI_OUT_OF_RESOURCES;
1004 }
Matt Fleminga6e4d5a2013-03-25 09:14:30 +00001005
1006 return EFI_SUCCESS;
1007}
Sergey Vlasov36680112013-04-16 18:31:09 +04001008EXPORT_SYMBOL_GPL(efi_query_variable_store);