Commit | Line | Data |
---|---|---|
26d7f65f MF |
1 | #define pr_fmt(fmt) "efi: " fmt |
2 | ||
eeb9db09 ST |
3 | #include <linux/init.h> |
4 | #include <linux/kernel.h> | |
5 | #include <linux/string.h> | |
6 | #include <linux/time.h> | |
7 | #include <linux/types.h> | |
8 | #include <linux/efi.h> | |
9 | #include <linux/slab.h> | |
10 | #include <linux/memblock.h> | |
11 | #include <linux/bootmem.h> | |
44be28e9 | 12 | #include <linux/acpi.h> |
d394f2d9 | 13 | #include <linux/dmi.h> |
5520b7e7 IM |
14 | |
15 | #include <asm/e820/api.h> | |
eeb9db09 ST |
16 | #include <asm/efi.h> |
17 | #include <asm/uv/uv.h> | |
2959c95d | 18 | #include <asm/cpu_device_id.h> |
3425d934 | 19 | #include <asm/reboot.h> |
eeb9db09 ST |
20 | |
21 | #define EFI_MIN_RESERVE 5120 | |
22 | ||
23 | #define EFI_DUMMY_GUID \ | |
24 | EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9) | |
25 | ||
2959c95d JK |
26 | #define QUARK_CSH_SIGNATURE 0x5f435348 /* _CSH */ |
27 | #define QUARK_SECURITY_HEADER_SIZE 0x400 | |
28 | ||
29 | /* | |
30 | * Header prepended to the standard EFI capsule on Quark systems the are based | |
31 | * on Intel firmware BSP. | |
32 | * @csh_signature: Unique identifier to sanity check signed module | |
33 | * presence ("_CSH"). | |
34 | * @version: Current version of CSH used. Should be one for Quark A0. | |
35 | * @modulesize: Size of the entire module including the module header | |
36 | * and payload. | |
37 | * @security_version_number_index: Index of SVN to use for validation of signed | |
38 | * module. | |
39 | * @security_version_number: Used to prevent against roll back of modules. | |
40 | * @rsvd_module_id: Currently unused for Clanton (Quark). | |
41 | * @rsvd_module_vendor: Vendor Identifier. For Intel products value is | |
42 | * 0x00008086. | |
43 | * @rsvd_date: BCD representation of build date as yyyymmdd, where | |
44 | * yyyy=4 digit year, mm=1-12, dd=1-31. | |
45 | * @headersize: Total length of the header including including any | |
46 | * padding optionally added by the signing tool. | |
47 | * @hash_algo: What Hash is used in the module signing. | |
48 | * @cryp_algo: What Crypto is used in the module signing. | |
49 | * @keysize: Total length of the key data including including any | |
50 | * padding optionally added by the signing tool. | |
51 | * @signaturesize: Total length of the signature including including any | |
52 | * padding optionally added by the signing tool. | |
53 | * @rsvd_next_header: 32-bit pointer to the next Secure Boot Module in the | |
54 | * chain, if there is a next header. | |
55 | * @rsvd: Reserved, padding structure to required size. | |
56 | * | |
57 | * See also QuartSecurityHeader_t in | |
58 | * Quark_EDKII_v1.2.1.1/QuarkPlatformPkg/Include/QuarkBootRom.h | |
59 | * from https://downloadcenter.intel.com/download/23197/Intel-Quark-SoC-X1000-Board-Support-Package-BSP | |
60 | */ | |
61 | struct quark_security_header { | |
62 | u32 csh_signature; | |
63 | u32 version; | |
64 | u32 modulesize; | |
65 | u32 security_version_number_index; | |
66 | u32 security_version_number; | |
67 | u32 rsvd_module_id; | |
68 | u32 rsvd_module_vendor; | |
69 | u32 rsvd_date; | |
70 | u32 headersize; | |
71 | u32 hash_algo; | |
72 | u32 cryp_algo; | |
73 | u32 keysize; | |
74 | u32 signaturesize; | |
75 | u32 rsvd_next_header; | |
76 | u32 rsvd[2]; | |
77 | }; | |
78 | ||
36b64976 | 79 | static const efi_char16_t efi_dummy_name[] = L"DUMMY"; |
eeb9db09 ST |
80 | |
81 | static bool efi_no_storage_paranoia; | |
82 | ||
83 | /* | |
84 | * Some firmware implementations refuse to boot if there's insufficient | |
85 | * space in the variable store. The implementation of garbage collection | |
86 | * in some FW versions causes stale (deleted) variables to take up space | |
87 | * longer than intended and space is only freed once the store becomes | |
88 | * almost completely full. | |
89 | * | |
90 | * Enabling this option disables the space checks in | |
91 | * efi_query_variable_store() and forces garbage collection. | |
92 | * | |
93 | * Only enable this option if deleting EFI variables does not free up | |
94 | * space in your variable store, e.g. if despite deleting variables | |
95 | * you're unable to create new ones. | |
96 | */ | |
97 | static int __init setup_storage_paranoia(char *arg) | |
98 | { | |
99 | efi_no_storage_paranoia = true; | |
100 | return 0; | |
101 | } | |
102 | early_param("efi_no_storage_paranoia", setup_storage_paranoia); | |
103 | ||
104 | /* | |
105 | * Deleting the dummy variable which kicks off garbage collection | |
106 | */ | |
107 | void efi_delete_dummy_variable(void) | |
108 | { | |
5a58bc1b SP |
109 | efi.set_variable_nonblocking((efi_char16_t *)efi_dummy_name, |
110 | &EFI_DUMMY_GUID, | |
111 | EFI_VARIABLE_NON_VOLATILE | | |
112 | EFI_VARIABLE_BOOTSERVICE_ACCESS | | |
113 | EFI_VARIABLE_RUNTIME_ACCESS, 0, NULL); | |
eeb9db09 ST |
114 | } |
115 | ||
ca0e30dc AB |
116 | /* |
117 | * In the nonblocking case we do not attempt to perform garbage | |
118 | * collection if we do not have enough free space. Rather, we do the | |
119 | * bare minimum check and give up immediately if the available space | |
120 | * is below EFI_MIN_RESERVE. | |
121 | * | |
122 | * This function is intended to be small and simple because it is | |
123 | * invoked from crash handler paths. | |
124 | */ | |
125 | static efi_status_t | |
126 | query_variable_store_nonblocking(u32 attributes, unsigned long size) | |
127 | { | |
128 | efi_status_t status; | |
129 | u64 storage_size, remaining_size, max_size; | |
130 | ||
131 | status = efi.query_variable_info_nonblocking(attributes, &storage_size, | |
132 | &remaining_size, | |
133 | &max_size); | |
134 | if (status != EFI_SUCCESS) | |
135 | return status; | |
136 | ||
137 | if (remaining_size - size < EFI_MIN_RESERVE) | |
138 | return EFI_OUT_OF_RESOURCES; | |
139 | ||
140 | return EFI_SUCCESS; | |
141 | } | |
142 | ||
eeb9db09 ST |
143 | /* |
144 | * Some firmware implementations refuse to boot if there's insufficient space | |
145 | * in the variable store. Ensure that we never use more than a safe limit. | |
146 | * | |
147 | * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable | |
148 | * store. | |
149 | */ | |
ca0e30dc AB |
150 | efi_status_t efi_query_variable_store(u32 attributes, unsigned long size, |
151 | bool nonblocking) | |
eeb9db09 ST |
152 | { |
153 | efi_status_t status; | |
154 | u64 storage_size, remaining_size, max_size; | |
155 | ||
156 | if (!(attributes & EFI_VARIABLE_NON_VOLATILE)) | |
157 | return 0; | |
158 | ||
ca0e30dc AB |
159 | if (nonblocking) |
160 | return query_variable_store_nonblocking(attributes, size); | |
161 | ||
eeb9db09 ST |
162 | status = efi.query_variable_info(attributes, &storage_size, |
163 | &remaining_size, &max_size); | |
164 | if (status != EFI_SUCCESS) | |
165 | return status; | |
166 | ||
167 | /* | |
168 | * We account for that by refusing the write if permitting it would | |
169 | * reduce the available space to under 5KB. This figure was provided by | |
170 | * Samsung, so should be safe. | |
171 | */ | |
172 | if ((remaining_size - size < EFI_MIN_RESERVE) && | |
173 | !efi_no_storage_paranoia) { | |
174 | ||
175 | /* | |
176 | * Triggering garbage collection may require that the firmware | |
177 | * generate a real EFI_OUT_OF_RESOURCES error. We can force | |
178 | * that by attempting to use more space than is available. | |
179 | */ | |
180 | unsigned long dummy_size = remaining_size + 1024; | |
9f66d8d7 | 181 | void *dummy = kzalloc(dummy_size, GFP_KERNEL); |
eeb9db09 ST |
182 | |
183 | if (!dummy) | |
184 | return EFI_OUT_OF_RESOURCES; | |
185 | ||
36b64976 AB |
186 | status = efi.set_variable((efi_char16_t *)efi_dummy_name, |
187 | &EFI_DUMMY_GUID, | |
eeb9db09 ST |
188 | EFI_VARIABLE_NON_VOLATILE | |
189 | EFI_VARIABLE_BOOTSERVICE_ACCESS | | |
190 | EFI_VARIABLE_RUNTIME_ACCESS, | |
191 | dummy_size, dummy); | |
192 | ||
193 | if (status == EFI_SUCCESS) { | |
194 | /* | |
195 | * This should have failed, so if it didn't make sure | |
196 | * that we delete it... | |
197 | */ | |
198 | efi_delete_dummy_variable(); | |
199 | } | |
200 | ||
201 | kfree(dummy); | |
202 | ||
203 | /* | |
204 | * The runtime code may now have triggered a garbage collection | |
205 | * run, so check the variable info again | |
206 | */ | |
207 | status = efi.query_variable_info(attributes, &storage_size, | |
208 | &remaining_size, &max_size); | |
209 | ||
210 | if (status != EFI_SUCCESS) | |
211 | return status; | |
212 | ||
213 | /* | |
214 | * There still isn't enough room, so return an error | |
215 | */ | |
216 | if (remaining_size - size < EFI_MIN_RESERVE) | |
217 | return EFI_OUT_OF_RESOURCES; | |
218 | } | |
219 | ||
220 | return EFI_SUCCESS; | |
221 | } | |
222 | EXPORT_SYMBOL_GPL(efi_query_variable_store); | |
223 | ||
816e7612 MF |
224 | /* |
225 | * The UEFI specification makes it clear that the operating system is | |
226 | * free to do whatever it wants with boot services code after | |
227 | * ExitBootServices() has been called. Ignoring this recommendation a | |
228 | * significant bunch of EFI implementations continue calling into boot | |
229 | * services code (SetVirtualAddressMap). In order to work around such | |
230 | * buggy implementations we reserve boot services region during EFI | |
231 | * init and make sure it stays executable. Then, after | |
232 | * SetVirtualAddressMap(), it is discarded. | |
233 | * | |
234 | * However, some boot services regions contain data that is required | |
235 | * by drivers, so we need to track which memory ranges can never be | |
236 | * freed. This is done by tagging those regions with the | |
237 | * EFI_MEMORY_RUNTIME attribute. | |
238 | * | |
239 | * Any driver that wants to mark a region as reserved must use | |
240 | * efi_mem_reserve() which will insert a new EFI memory descriptor | |
241 | * into efi.memmap (splitting existing regions if necessary) and tag | |
242 | * it with EFI_MEMORY_RUNTIME. | |
243 | */ | |
244 | void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size) | |
245 | { | |
246 | phys_addr_t new_phys, new_size; | |
247 | struct efi_mem_range mr; | |
248 | efi_memory_desc_t md; | |
249 | int num_entries; | |
250 | void *new; | |
251 | ||
7e1550b8 AB |
252 | if (efi_mem_desc_lookup(addr, &md) || |
253 | md.type != EFI_BOOT_SERVICES_DATA) { | |
816e7612 MF |
254 | pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr); |
255 | return; | |
256 | } | |
257 | ||
258 | if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) { | |
259 | pr_err("Region spans EFI memory descriptors, %pa\n", &addr); | |
260 | return; | |
261 | } | |
262 | ||
6f6266a5 OS |
263 | /* No need to reserve regions that will never be freed. */ |
264 | if (md.attribute & EFI_MEMORY_RUNTIME) | |
265 | return; | |
266 | ||
92dc3350 MF |
267 | size += addr % EFI_PAGE_SIZE; |
268 | size = round_up(size, EFI_PAGE_SIZE); | |
269 | addr = round_down(addr, EFI_PAGE_SIZE); | |
270 | ||
816e7612 | 271 | mr.range.start = addr; |
92dc3350 | 272 | mr.range.end = addr + size - 1; |
816e7612 MF |
273 | mr.attribute = md.attribute | EFI_MEMORY_RUNTIME; |
274 | ||
275 | num_entries = efi_memmap_split_count(&md, &mr.range); | |
276 | num_entries += efi.memmap.nr_map; | |
277 | ||
278 | new_size = efi.memmap.desc_size * num_entries; | |
279 | ||
20b1e22d | 280 | new_phys = efi_memmap_alloc(num_entries); |
816e7612 MF |
281 | if (!new_phys) { |
282 | pr_err("Could not allocate boot services memmap\n"); | |
283 | return; | |
284 | } | |
285 | ||
286 | new = early_memremap(new_phys, new_size); | |
287 | if (!new) { | |
288 | pr_err("Failed to map new boot services memmap\n"); | |
289 | return; | |
290 | } | |
291 | ||
292 | efi_memmap_insert(&efi.memmap, new, &mr); | |
293 | early_memunmap(new, new_size); | |
294 | ||
295 | efi_memmap_install(new_phys, num_entries); | |
296 | } | |
297 | ||
452308de MF |
298 | /* |
299 | * Helper function for efi_reserve_boot_services() to figure out if we | |
300 | * can free regions in efi_free_boot_services(). | |
301 | * | |
302 | * Use this function to ensure we do not free regions owned by somebody | |
303 | * else. We must only reserve (and then free) regions: | |
304 | * | |
305 | * - Not within any part of the kernel | |
09821ff1 | 306 | * - Not the BIOS reserved area (E820_TYPE_RESERVED, E820_TYPE_NVS, etc) |
452308de MF |
307 | */ |
308 | static bool can_free_region(u64 start, u64 size) | |
309 | { | |
310 | if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end)) | |
311 | return false; | |
312 | ||
09821ff1 | 313 | if (!e820__mapped_all(start, start+size, E820_TYPE_RAM)) |
452308de MF |
314 | return false; |
315 | ||
316 | return true; | |
317 | } | |
318 | ||
eeb9db09 ST |
319 | void __init efi_reserve_boot_services(void) |
320 | { | |
78ce248f | 321 | efi_memory_desc_t *md; |
eeb9db09 | 322 | |
78ce248f | 323 | for_each_efi_memory_desc(md) { |
eeb9db09 ST |
324 | u64 start = md->phys_addr; |
325 | u64 size = md->num_pages << EFI_PAGE_SHIFT; | |
452308de | 326 | bool already_reserved; |
eeb9db09 ST |
327 | |
328 | if (md->type != EFI_BOOT_SERVICES_CODE && | |
329 | md->type != EFI_BOOT_SERVICES_DATA) | |
330 | continue; | |
452308de MF |
331 | |
332 | already_reserved = memblock_is_region_reserved(start, size); | |
333 | ||
334 | /* | |
335 | * Because the following memblock_reserve() is paired | |
53ab85eb | 336 | * with memblock_free_late() for this region in |
452308de MF |
337 | * efi_free_boot_services(), we must be extremely |
338 | * careful not to reserve, and subsequently free, | |
339 | * critical regions of memory (like the kernel image) or | |
340 | * those regions that somebody else has already | |
341 | * reserved. | |
342 | * | |
343 | * A good example of a critical region that must not be | |
344 | * freed is page zero (first 4Kb of memory), which may | |
345 | * contain boot services code/data but is marked | |
09821ff1 | 346 | * E820_TYPE_RESERVED by trim_bios_range(). |
452308de MF |
347 | */ |
348 | if (!already_reserved) { | |
eeb9db09 | 349 | memblock_reserve(start, size); |
452308de MF |
350 | |
351 | /* | |
352 | * If we are the first to reserve the region, no | |
353 | * one else cares about it. We own it and can | |
354 | * free it later. | |
355 | */ | |
356 | if (can_free_region(start, size)) | |
357 | continue; | |
358 | } | |
359 | ||
360 | /* | |
361 | * We don't own the region. We must not free it. | |
362 | * | |
363 | * Setting this bit for a boot services region really | |
364 | * doesn't make sense as far as the firmware is | |
365 | * concerned, but it does provide us with a way to tag | |
366 | * those regions that must not be paired with | |
53ab85eb | 367 | * memblock_free_late(). |
452308de MF |
368 | */ |
369 | md->attribute |= EFI_MEMORY_RUNTIME; | |
eeb9db09 ST |
370 | } |
371 | } | |
372 | ||
373 | void __init efi_free_boot_services(void) | |
374 | { | |
816e7612 | 375 | phys_addr_t new_phys, new_size; |
78ce248f | 376 | efi_memory_desc_t *md; |
816e7612 MF |
377 | int num_entries = 0; |
378 | void *new, *new_md; | |
eeb9db09 | 379 | |
78ce248f | 380 | for_each_efi_memory_desc(md) { |
eeb9db09 ST |
381 | unsigned long long start = md->phys_addr; |
382 | unsigned long long size = md->num_pages << EFI_PAGE_SHIFT; | |
5bc653b7 | 383 | size_t rm_size; |
eeb9db09 ST |
384 | |
385 | if (md->type != EFI_BOOT_SERVICES_CODE && | |
816e7612 MF |
386 | md->type != EFI_BOOT_SERVICES_DATA) { |
387 | num_entries++; | |
eeb9db09 | 388 | continue; |
816e7612 | 389 | } |
eeb9db09 | 390 | |
452308de | 391 | /* Do not free, someone else owns it: */ |
816e7612 MF |
392 | if (md->attribute & EFI_MEMORY_RUNTIME) { |
393 | num_entries++; | |
eeb9db09 | 394 | continue; |
816e7612 | 395 | } |
eeb9db09 | 396 | |
5bc653b7 AL |
397 | /* |
398 | * Nasty quirk: if all sub-1MB memory is used for boot | |
399 | * services, we can get here without having allocated the | |
400 | * real mode trampoline. It's too late to hand boot services | |
401 | * memory back to the memblock allocator, so instead | |
402 | * try to manually allocate the trampoline if needed. | |
403 | * | |
404 | * I've seen this on a Dell XPS 13 9350 with firmware | |
405 | * 1.4.4 with SGX enabled booting Linux via Fedora 24's | |
406 | * grub2-efi on a hard disk. (And no, I don't know why | |
407 | * this happened, but Linux should still try to boot rather | |
408 | * panicing early.) | |
409 | */ | |
410 | rm_size = real_mode_size_needed(); | |
411 | if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) { | |
412 | set_real_mode_mem(start, rm_size); | |
413 | start += rm_size; | |
414 | size -= rm_size; | |
415 | } | |
416 | ||
53ab85eb | 417 | memblock_free_late(start, size); |
eeb9db09 | 418 | } |
816e7612 | 419 | |
1ea34adb JG |
420 | if (!num_entries) |
421 | return; | |
422 | ||
816e7612 | 423 | new_size = efi.memmap.desc_size * num_entries; |
20b1e22d | 424 | new_phys = efi_memmap_alloc(num_entries); |
816e7612 MF |
425 | if (!new_phys) { |
426 | pr_err("Failed to allocate new EFI memmap\n"); | |
427 | return; | |
428 | } | |
429 | ||
430 | new = memremap(new_phys, new_size, MEMREMAP_WB); | |
431 | if (!new) { | |
432 | pr_err("Failed to map new EFI memmap\n"); | |
433 | return; | |
434 | } | |
435 | ||
436 | /* | |
437 | * Build a new EFI memmap that excludes any boot services | |
438 | * regions that are not tagged EFI_MEMORY_RUNTIME, since those | |
439 | * regions have now been freed. | |
440 | */ | |
441 | new_md = new; | |
442 | for_each_efi_memory_desc(md) { | |
443 | if (!(md->attribute & EFI_MEMORY_RUNTIME) && | |
444 | (md->type == EFI_BOOT_SERVICES_CODE || | |
445 | md->type == EFI_BOOT_SERVICES_DATA)) | |
446 | continue; | |
447 | ||
448 | memcpy(new_md, md, efi.memmap.desc_size); | |
449 | new_md += efi.memmap.desc_size; | |
450 | } | |
451 | ||
452 | memunmap(new); | |
453 | ||
454 | if (efi_memmap_install(new_phys, num_entries)) { | |
455 | pr_err("Could not install new EFI memmap\n"); | |
456 | return; | |
457 | } | |
eeb9db09 ST |
458 | } |
459 | ||
460 | /* | |
461 | * A number of config table entries get remapped to virtual addresses | |
462 | * after entering EFI virtual mode. However, the kexec kernel requires | |
463 | * their physical addresses therefore we pass them via setup_data and | |
464 | * correct those entries to their respective physical addresses here. | |
465 | * | |
466 | * Currently only handles smbios which is necessary for some firmware | |
467 | * implementation. | |
468 | */ | |
469 | int __init efi_reuse_config(u64 tables, int nr_tables) | |
470 | { | |
471 | int i, sz, ret = 0; | |
472 | void *p, *tablep; | |
473 | struct efi_setup_data *data; | |
474 | ||
475 | if (!efi_setup) | |
476 | return 0; | |
477 | ||
478 | if (!efi_enabled(EFI_64BIT)) | |
479 | return 0; | |
480 | ||
481 | data = early_memremap(efi_setup, sizeof(*data)); | |
482 | if (!data) { | |
483 | ret = -ENOMEM; | |
484 | goto out; | |
485 | } | |
486 | ||
487 | if (!data->smbios) | |
488 | goto out_memremap; | |
489 | ||
490 | sz = sizeof(efi_config_table_64_t); | |
491 | ||
492 | p = tablep = early_memremap(tables, nr_tables * sz); | |
493 | if (!p) { | |
494 | pr_err("Could not map Configuration table!\n"); | |
495 | ret = -ENOMEM; | |
496 | goto out_memremap; | |
497 | } | |
498 | ||
499 | for (i = 0; i < efi.systab->nr_tables; i++) { | |
500 | efi_guid_t guid; | |
501 | ||
502 | guid = ((efi_config_table_64_t *)p)->guid; | |
503 | ||
504 | if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID)) | |
505 | ((efi_config_table_64_t *)p)->table = data->smbios; | |
506 | p += sz; | |
507 | } | |
98a716b6 | 508 | early_memunmap(tablep, nr_tables * sz); |
eeb9db09 ST |
509 | |
510 | out_memremap: | |
98a716b6 | 511 | early_memunmap(data, sizeof(*data)); |
eeb9db09 ST |
512 | out: |
513 | return ret; | |
514 | } | |
515 | ||
d394f2d9 AT |
516 | static const struct dmi_system_id sgi_uv1_dmi[] = { |
517 | { NULL, "SGI UV1", | |
518 | { DMI_MATCH(DMI_PRODUCT_NAME, "Stoutland Platform"), | |
519 | DMI_MATCH(DMI_PRODUCT_VERSION, "1.0"), | |
520 | DMI_MATCH(DMI_BIOS_VENDOR, "SGI.COM"), | |
521 | } | |
522 | }, | |
523 | { } /* NULL entry stops DMI scanning */ | |
524 | }; | |
525 | ||
eeb9db09 ST |
526 | void __init efi_apply_memmap_quirks(void) |
527 | { | |
528 | /* | |
529 | * Once setup is done earlier, unmap the EFI memory map on mismatched | |
530 | * firmware/kernel architectures since there is no support for runtime | |
531 | * services. | |
532 | */ | |
533 | if (!efi_runtime_supported()) { | |
26d7f65f | 534 | pr_info("Setup done, disabling due to 32/64-bit mismatch\n"); |
9479c7ce | 535 | efi_memmap_unmap(); |
eeb9db09 ST |
536 | } |
537 | ||
d394f2d9 AT |
538 | /* UV2+ BIOS has a fix for this issue. UV1 still needs the quirk. */ |
539 | if (dmi_check_system(sgi_uv1_dmi)) | |
eeb9db09 ST |
540 | set_bit(EFI_OLD_MEMMAP, &efi.flags); |
541 | } | |
44be28e9 MF |
542 | |
543 | /* | |
544 | * For most modern platforms the preferred method of powering off is via | |
545 | * ACPI. However, there are some that are known to require the use of | |
546 | * EFI runtime services and for which ACPI does not work at all. | |
547 | * | |
548 | * Using EFI is a last resort, to be used only if no other option | |
549 | * exists. | |
550 | */ | |
551 | bool efi_reboot_required(void) | |
552 | { | |
553 | if (!acpi_gbl_reduced_hardware) | |
554 | return false; | |
555 | ||
556 | efi_reboot_quirk_mode = EFI_RESET_WARM; | |
557 | return true; | |
558 | } | |
559 | ||
560 | bool efi_poweroff_required(void) | |
561 | { | |
13737181 | 562 | return acpi_gbl_reduced_hardware || acpi_no_s5; |
44be28e9 | 563 | } |
2959c95d JK |
564 | |
565 | #ifdef CONFIG_EFI_CAPSULE_QUIRK_QUARK_CSH | |
566 | ||
567 | static int qrk_capsule_setup_info(struct capsule_info *cap_info, void **pkbuff, | |
568 | size_t hdr_bytes) | |
569 | { | |
570 | struct quark_security_header *csh = *pkbuff; | |
571 | ||
572 | /* Only process data block that is larger than the security header */ | |
573 | if (hdr_bytes < sizeof(struct quark_security_header)) | |
574 | return 0; | |
575 | ||
576 | if (csh->csh_signature != QUARK_CSH_SIGNATURE || | |
577 | csh->headersize != QUARK_SECURITY_HEADER_SIZE) | |
578 | return 1; | |
579 | ||
580 | /* Only process data block if EFI header is included */ | |
581 | if (hdr_bytes < QUARK_SECURITY_HEADER_SIZE + | |
582 | sizeof(efi_capsule_header_t)) | |
583 | return 0; | |
584 | ||
585 | pr_debug("Quark security header detected\n"); | |
586 | ||
587 | if (csh->rsvd_next_header != 0) { | |
588 | pr_err("multiple Quark security headers not supported\n"); | |
589 | return -EINVAL; | |
590 | } | |
591 | ||
592 | *pkbuff += csh->headersize; | |
593 | cap_info->total_size = csh->headersize; | |
594 | ||
595 | /* | |
596 | * Update the first page pointer to skip over the CSH header. | |
597 | */ | |
f24c4d47 AB |
598 | cap_info->phys[0] += csh->headersize; |
599 | ||
600 | /* | |
601 | * cap_info->capsule should point at a virtual mapping of the entire | |
602 | * capsule, starting at the capsule header. Our image has the Quark | |
603 | * security header prepended, so we cannot rely on the default vmap() | |
604 | * mapping created by the generic capsule code. | |
605 | * Given that the Quark firmware does not appear to care about the | |
606 | * virtual mapping, let's just point cap_info->capsule at our copy | |
607 | * of the capsule header. | |
608 | */ | |
609 | cap_info->capsule = &cap_info->header; | |
2959c95d JK |
610 | |
611 | return 1; | |
612 | } | |
613 | ||
614 | #define ICPU(family, model, quirk_handler) \ | |
615 | { X86_VENDOR_INTEL, family, model, X86_FEATURE_ANY, \ | |
616 | (unsigned long)&quirk_handler } | |
617 | ||
618 | static const struct x86_cpu_id efi_capsule_quirk_ids[] = { | |
619 | ICPU(5, 9, qrk_capsule_setup_info), /* Intel Quark X1000 */ | |
620 | { } | |
621 | }; | |
622 | ||
623 | int efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff, | |
624 | size_t hdr_bytes) | |
625 | { | |
626 | int (*quirk_handler)(struct capsule_info *, void **, size_t); | |
627 | const struct x86_cpu_id *id; | |
628 | int ret; | |
629 | ||
630 | if (hdr_bytes < sizeof(efi_capsule_header_t)) | |
631 | return 0; | |
632 | ||
633 | cap_info->total_size = 0; | |
634 | ||
635 | id = x86_match_cpu(efi_capsule_quirk_ids); | |
636 | if (id) { | |
637 | /* | |
638 | * The quirk handler is supposed to return | |
639 | * - a value > 0 if the setup should continue, after advancing | |
640 | * kbuff as needed | |
641 | * - 0 if not enough hdr_bytes are available yet | |
642 | * - a negative error code otherwise | |
643 | */ | |
644 | quirk_handler = (typeof(quirk_handler))id->driver_data; | |
645 | ret = quirk_handler(cap_info, &kbuff, hdr_bytes); | |
646 | if (ret <= 0) | |
647 | return ret; | |
648 | } | |
649 | ||
650 | memcpy(&cap_info->header, kbuff, sizeof(cap_info->header)); | |
651 | ||
652 | cap_info->total_size += cap_info->header.imagesize; | |
653 | ||
654 | return __efi_capsule_setup_info(cap_info); | |
655 | } | |
656 | ||
657 | #endif | |
3425d934 SP |
658 | |
659 | /* | |
660 | * If any access by any efi runtime service causes a page fault, then, | |
661 | * 1. If it's efi_reset_system(), reboot through BIOS. | |
662 | * 2. If any other efi runtime service, then | |
663 | * a. Return error status to the efi caller process. | |
664 | * b. Disable EFI Runtime Services forever and | |
665 | * c. Freeze efi_rts_wq and schedule new process. | |
666 | * | |
667 | * @return: Returns, if the page fault is not handled. This function | |
668 | * will never return if the page fault is handled successfully. | |
669 | */ | |
670 | void efi_recover_from_page_fault(unsigned long phys_addr) | |
671 | { | |
672 | if (!IS_ENABLED(CONFIG_X86_64)) | |
673 | return; | |
674 | ||
675 | /* | |
676 | * Make sure that an efi runtime service caused the page fault. | |
677 | * "efi_mm" cannot be used to check if the page fault had occurred | |
678 | * in the firmware context because efi=old_map doesn't use efi_pgd. | |
679 | */ | |
680 | if (efi_rts_work.efi_rts_id == NONE) | |
681 | return; | |
682 | ||
683 | /* | |
684 | * Address range 0x0000 - 0x0fff is always mapped in the efi_pgd, so | |
685 | * page faulting on these addresses isn't expected. | |
686 | */ | |
687 | if (phys_addr >= 0x0000 && phys_addr <= 0x0fff) | |
688 | return; | |
689 | ||
690 | /* | |
691 | * Print stack trace as it might be useful to know which EFI Runtime | |
692 | * Service is buggy. | |
693 | */ | |
694 | WARN(1, FW_BUG "Page fault caused by firmware at PA: 0x%lx\n", | |
695 | phys_addr); | |
696 | ||
697 | /* | |
698 | * Buggy efi_reset_system() is handled differently from other EFI | |
699 | * Runtime Services as it doesn't use efi_rts_wq. Although, | |
700 | * native_machine_emergency_restart() says that machine_real_restart() | |
701 | * could fail, it's better not to compilcate this fault handler | |
702 | * because this case occurs *very* rarely and hence could be improved | |
703 | * on a need by basis. | |
704 | */ | |
705 | if (efi_rts_work.efi_rts_id == RESET_SYSTEM) { | |
706 | pr_info("efi_reset_system() buggy! Reboot through BIOS\n"); | |
707 | machine_real_restart(MRR_BIOS); | |
708 | return; | |
709 | } | |
710 | ||
711 | /* | |
712 | * Before calling EFI Runtime Service, the kernel has switched the | |
713 | * calling process to efi_mm. Hence, switch back to task_mm. | |
714 | */ | |
715 | arch_efi_call_virt_teardown(); | |
716 | ||
717 | /* Signal error status to the efi caller process */ | |
718 | efi_rts_work.status = EFI_ABORTED; | |
719 | complete(&efi_rts_work.efi_rts_comp); | |
720 | ||
721 | clear_bit(EFI_RUNTIME_SERVICES, &efi.flags); | |
722 | pr_info("Froze efi_rts_wq and disabled EFI Runtime Services\n"); | |
723 | ||
724 | /* | |
725 | * Call schedule() in an infinite loop, so that any spurious wake ups | |
726 | * will never run efi_rts_wq again. | |
727 | */ | |
728 | for (;;) { | |
729 | set_current_state(TASK_IDLE); | |
730 | schedule(); | |
731 | } | |
732 | ||
733 | return; | |
734 | } |