source: src/linux/universal/linux-4.9/arch/x86/platform/efi/quirks.c @ 31885

Last change on this file since 31885 was 31885, checked in by brainslayer, 2 months ago

update

File size: 13.4 KB
Line 
1#define pr_fmt(fmt) "efi: " fmt
2
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>
12#include <linux/acpi.h>
13#include <linux/dmi.h>
14#include <asm/efi.h>
15#include <asm/uv/uv.h>
16
17#define EFI_MIN_RESERVE 5120
18
19#define EFI_DUMMY_GUID \
20        EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
21
22static efi_char16_t efi_dummy_name[6] = { 'D', 'U', 'M', 'M', 'Y', 0 };
23
24static bool efi_no_storage_paranoia;
25
26/*
27 * Some firmware implementations refuse to boot if there's insufficient
28 * space in the variable store. The implementation of garbage collection
29 * in some FW versions causes stale (deleted) variables to take up space
30 * longer than intended and space is only freed once the store becomes
31 * almost completely full.
32 *
33 * Enabling this option disables the space checks in
34 * efi_query_variable_store() and forces garbage collection.
35 *
36 * Only enable this option if deleting EFI variables does not free up
37 * space in your variable store, e.g. if despite deleting variables
38 * you're unable to create new ones.
39 */
40static int __init setup_storage_paranoia(char *arg)
41{
42        efi_no_storage_paranoia = true;
43        return 0;
44}
45early_param("efi_no_storage_paranoia", setup_storage_paranoia);
46
47/*
48 * Deleting the dummy variable which kicks off garbage collection
49*/
50void efi_delete_dummy_variable(void)
51{
52        efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
53                         EFI_VARIABLE_NON_VOLATILE |
54                         EFI_VARIABLE_BOOTSERVICE_ACCESS |
55                         EFI_VARIABLE_RUNTIME_ACCESS,
56                         0, NULL);
57}
58
59/*
60 * In the nonblocking case we do not attempt to perform garbage
61 * collection if we do not have enough free space. Rather, we do the
62 * bare minimum check and give up immediately if the available space
63 * is below EFI_MIN_RESERVE.
64 *
65 * This function is intended to be small and simple because it is
66 * invoked from crash handler paths.
67 */
68static efi_status_t
69query_variable_store_nonblocking(u32 attributes, unsigned long size)
70{
71        efi_status_t status;
72        u64 storage_size, remaining_size, max_size;
73
74        status = efi.query_variable_info_nonblocking(attributes, &storage_size,
75                                                     &remaining_size,
76                                                     &max_size);
77        if (status != EFI_SUCCESS)
78                return status;
79
80        if (remaining_size - size < EFI_MIN_RESERVE)
81                return EFI_OUT_OF_RESOURCES;
82
83        return EFI_SUCCESS;
84}
85
86/*
87 * Some firmware implementations refuse to boot if there's insufficient space
88 * in the variable store. Ensure that we never use more than a safe limit.
89 *
90 * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
91 * store.
92 */
93efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
94                                      bool nonblocking)
95{
96        efi_status_t status;
97        u64 storage_size, remaining_size, max_size;
98
99        if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
100                return 0;
101
102        if (nonblocking)
103                return query_variable_store_nonblocking(attributes, size);
104
105        status = efi.query_variable_info(attributes, &storage_size,
106                                         &remaining_size, &max_size);
107        if (status != EFI_SUCCESS)
108                return status;
109
110        /*
111         * We account for that by refusing the write if permitting it would
112         * reduce the available space to under 5KB. This figure was provided by
113         * Samsung, so should be safe.
114         */
115        if ((remaining_size - size < EFI_MIN_RESERVE) &&
116                !efi_no_storage_paranoia) {
117
118                /*
119                 * Triggering garbage collection may require that the firmware
120                 * generate a real EFI_OUT_OF_RESOURCES error. We can force
121                 * that by attempting to use more space than is available.
122                 */
123                unsigned long dummy_size = remaining_size + 1024;
124                void *dummy = kzalloc(dummy_size, GFP_ATOMIC);
125
126                if (!dummy)
127                        return EFI_OUT_OF_RESOURCES;
128
129                status = efi.set_variable(efi_dummy_name, &EFI_DUMMY_GUID,
130                                          EFI_VARIABLE_NON_VOLATILE |
131                                          EFI_VARIABLE_BOOTSERVICE_ACCESS |
132                                          EFI_VARIABLE_RUNTIME_ACCESS,
133                                          dummy_size, dummy);
134
135                if (status == EFI_SUCCESS) {
136                        /*
137                         * This should have failed, so if it didn't make sure
138                         * that we delete it...
139                         */
140                        efi_delete_dummy_variable();
141                }
142
143                kfree(dummy);
144
145                /*
146                 * The runtime code may now have triggered a garbage collection
147                 * run, so check the variable info again
148                 */
149                status = efi.query_variable_info(attributes, &storage_size,
150                                                 &remaining_size, &max_size);
151
152                if (status != EFI_SUCCESS)
153                        return status;
154
155                /*
156                 * There still isn't enough room, so return an error
157                 */
158                if (remaining_size - size < EFI_MIN_RESERVE)
159                        return EFI_OUT_OF_RESOURCES;
160        }
161
162        return EFI_SUCCESS;
163}
164EXPORT_SYMBOL_GPL(efi_query_variable_store);
165
166/*
167 * The UEFI specification makes it clear that the operating system is
168 * free to do whatever it wants with boot services code after
169 * ExitBootServices() has been called. Ignoring this recommendation a
170 * significant bunch of EFI implementations continue calling into boot
171 * services code (SetVirtualAddressMap). In order to work around such
172 * buggy implementations we reserve boot services region during EFI
173 * init and make sure it stays executable. Then, after
174 * SetVirtualAddressMap(), it is discarded.
175 *
176 * However, some boot services regions contain data that is required
177 * by drivers, so we need to track which memory ranges can never be
178 * freed. This is done by tagging those regions with the
179 * EFI_MEMORY_RUNTIME attribute.
180 *
181 * Any driver that wants to mark a region as reserved must use
182 * efi_mem_reserve() which will insert a new EFI memory descriptor
183 * into efi.memmap (splitting existing regions if necessary) and tag
184 * it with EFI_MEMORY_RUNTIME.
185 */
186void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)
187{
188        phys_addr_t new_phys, new_size;
189        struct efi_mem_range mr;
190        efi_memory_desc_t md;
191        int num_entries;
192        void *new;
193
194        if (efi_mem_desc_lookup(addr, &md)) {
195                pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);
196                return;
197        }
198
199        if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {
200                pr_err("Region spans EFI memory descriptors, %pa\n", &addr);
201                return;
202        }
203
204        /* No need to reserve regions that will never be freed. */
205        if (md.attribute & EFI_MEMORY_RUNTIME)
206                return;
207
208        size += addr % EFI_PAGE_SIZE;
209        size = round_up(size, EFI_PAGE_SIZE);
210        addr = round_down(addr, EFI_PAGE_SIZE);
211
212        mr.range.start = addr;
213        mr.range.end = addr + size - 1;
214        mr.attribute = md.attribute | EFI_MEMORY_RUNTIME;
215
216        num_entries = efi_memmap_split_count(&md, &mr.range);
217        num_entries += efi.memmap.nr_map;
218
219        new_size = efi.memmap.desc_size * num_entries;
220
221        new_phys = efi_memmap_alloc(num_entries);
222        if (!new_phys) {
223                pr_err("Could not allocate boot services memmap\n");
224                return;
225        }
226
227        new = early_memremap(new_phys, new_size);
228        if (!new) {
229                pr_err("Failed to map new boot services memmap\n");
230                return;
231        }
232
233        efi_memmap_insert(&efi.memmap, new, &mr);
234        early_memunmap(new, new_size);
235
236        efi_memmap_install(new_phys, num_entries);
237}
238
239/*
240 * Helper function for efi_reserve_boot_services() to figure out if we
241 * can free regions in efi_free_boot_services().
242 *
243 * Use this function to ensure we do not free regions owned by somebody
244 * else. We must only reserve (and then free) regions:
245 *
246 * - Not within any part of the kernel
247 * - Not the BIOS reserved area (E820_RESERVED, E820_NVS, etc)
248 */
249static bool can_free_region(u64 start, u64 size)
250{
251        if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
252                return false;
253
254        if (!e820_all_mapped(start, start+size, E820_RAM))
255                return false;
256
257        return true;
258}
259
260void __init efi_reserve_boot_services(void)
261{
262        efi_memory_desc_t *md;
263
264        for_each_efi_memory_desc(md) {
265                u64 start = md->phys_addr;
266                u64 size = md->num_pages << EFI_PAGE_SHIFT;
267                bool already_reserved;
268
269                if (md->type != EFI_BOOT_SERVICES_CODE &&
270                    md->type != EFI_BOOT_SERVICES_DATA)
271                        continue;
272
273                already_reserved = memblock_is_region_reserved(start, size);
274
275                /*
276                 * Because the following memblock_reserve() is paired
277                 * with free_bootmem_late() for this region in
278                 * efi_free_boot_services(), we must be extremely
279                 * careful not to reserve, and subsequently free,
280                 * critical regions of memory (like the kernel image) or
281                 * those regions that somebody else has already
282                 * reserved.
283                 *
284                 * A good example of a critical region that must not be
285                 * freed is page zero (first 4Kb of memory), which may
286                 * contain boot services code/data but is marked
287                 * E820_RESERVED by trim_bios_range().
288                 */
289                if (!already_reserved) {
290                        memblock_reserve(start, size);
291
292                        /*
293                         * If we are the first to reserve the region, no
294                         * one else cares about it. We own it and can
295                         * free it later.
296                         */
297                        if (can_free_region(start, size))
298                                continue;
299                }
300
301                /*
302                 * We don't own the region. We must not free it.
303                 *
304                 * Setting this bit for a boot services region really
305                 * doesn't make sense as far as the firmware is
306                 * concerned, but it does provide us with a way to tag
307                 * those regions that must not be paired with
308                 * free_bootmem_late().
309                 */
310                md->attribute |= EFI_MEMORY_RUNTIME;
311        }
312}
313
314void __init efi_free_boot_services(void)
315{
316        phys_addr_t new_phys, new_size;
317        efi_memory_desc_t *md;
318        int num_entries = 0;
319        void *new, *new_md;
320
321        for_each_efi_memory_desc(md) {
322                unsigned long long start = md->phys_addr;
323                unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
324                size_t rm_size;
325
326                if (md->type != EFI_BOOT_SERVICES_CODE &&
327                    md->type != EFI_BOOT_SERVICES_DATA) {
328                        num_entries++;
329                        continue;
330                }
331
332                /* Do not free, someone else owns it: */
333                if (md->attribute & EFI_MEMORY_RUNTIME) {
334                        num_entries++;
335                        continue;
336                }
337
338                /*
339                 * Nasty quirk: if all sub-1MB memory is used for boot
340                 * services, we can get here without having allocated the
341                 * real mode trampoline.  It's too late to hand boot services
342                 * memory back to the memblock allocator, so instead
343                 * try to manually allocate the trampoline if needed.
344                 *
345                 * I've seen this on a Dell XPS 13 9350 with firmware
346                 * 1.4.4 with SGX enabled booting Linux via Fedora 24's
347                 * grub2-efi on a hard disk.  (And no, I don't know why
348                 * this happened, but Linux should still try to boot rather
349                 * panicing early.)
350                 */
351                rm_size = real_mode_size_needed();
352                if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
353                        set_real_mode_mem(start, rm_size);
354                        start += rm_size;
355                        size -= rm_size;
356                }
357
358                free_bootmem_late(start, size);
359        }
360
361        new_size = efi.memmap.desc_size * num_entries;
362        new_phys = efi_memmap_alloc(num_entries);
363        if (!new_phys) {
364                pr_err("Failed to allocate new EFI memmap\n");
365                return;
366        }
367
368        new = memremap(new_phys, new_size, MEMREMAP_WB);
369        if (!new) {
370                pr_err("Failed to map new EFI memmap\n");
371                return;
372        }
373
374        /*
375         * Build a new EFI memmap that excludes any boot services
376         * regions that are not tagged EFI_MEMORY_RUNTIME, since those
377         * regions have now been freed.
378         */
379        new_md = new;
380        for_each_efi_memory_desc(md) {
381                if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
382                    (md->type == EFI_BOOT_SERVICES_CODE ||
383                     md->type == EFI_BOOT_SERVICES_DATA))
384                        continue;
385
386                memcpy(new_md, md, efi.memmap.desc_size);
387                new_md += efi.memmap.desc_size;
388        }
389
390        memunmap(new);
391
392        if (efi_memmap_install(new_phys, num_entries)) {
393                pr_err("Could not install new EFI memmap\n");
394                return;
395        }
396}
397
398/*
399 * A number of config table entries get remapped to virtual addresses
400 * after entering EFI virtual mode. However, the kexec kernel requires
401 * their physical addresses therefore we pass them via setup_data and
402 * correct those entries to their respective physical addresses here.
403 *
404 * Currently only handles smbios which is necessary for some firmware
405 * implementation.
406 */
407int __init efi_reuse_config(u64 tables, int nr_tables)
408{
409        int i, sz, ret = 0;
410        void *p, *tablep;
411        struct efi_setup_data *data;
412
413        if (!efi_setup)
414                return 0;
415
416        if (!efi_enabled(EFI_64BIT))
417                return 0;
418
419        data = early_memremap(efi_setup, sizeof(*data));
420        if (!data) {
421                ret = -ENOMEM;
422                goto out;
423        }
424
425        if (!data->smbios)
426                goto out_memremap;
427
428        sz = sizeof(efi_config_table_64_t);
429
430        p = tablep = early_memremap(tables, nr_tables * sz);
431        if (!p) {
432                pr_err("Could not map Configuration table!\n");
433                ret = -ENOMEM;
434                goto out_memremap;
435        }
436
437        for (i = 0; i < efi.systab->nr_tables; i++) {
438                efi_guid_t guid;
439
440                guid = ((efi_config_table_64_t *)p)->guid;
441
442                if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
443                        ((efi_config_table_64_t *)p)->table = data->smbios;
444                p += sz;
445        }
446        early_memunmap(tablep, nr_tables * sz);
447
448out_memremap:
449        early_memunmap(data, sizeof(*data));
450out:
451        return ret;
452}
453
454static const struct dmi_system_id sgi_uv1_dmi[] = {
455        { NULL, "SGI UV1",
456                {       DMI_MATCH(DMI_PRODUCT_NAME,     "Stoutland Platform"),
457                        DMI_MATCH(DMI_PRODUCT_VERSION,  "1.0"),
458                        DMI_MATCH(DMI_BIOS_VENDOR,      "SGI.COM"),
459                }
460        },
461        { } /* NULL entry stops DMI scanning */
462};
463
464void __init efi_apply_memmap_quirks(void)
465{
466        /*
467         * Once setup is done earlier, unmap the EFI memory map on mismatched
468         * firmware/kernel architectures since there is no support for runtime
469         * services.
470         */
471        if (!efi_runtime_supported()) {
472                pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
473                efi_memmap_unmap();
474        }
475
476        /* UV2+ BIOS has a fix for this issue.  UV1 still needs the quirk. */
477        if (dmi_check_system(sgi_uv1_dmi))
478                set_bit(EFI_OLD_MEMMAP, &efi.flags);
479}
480
481/*
482 * For most modern platforms the preferred method of powering off is via
483 * ACPI. However, there are some that are known to require the use of
484 * EFI runtime services and for which ACPI does not work at all.
485 *
486 * Using EFI is a last resort, to be used only if no other option
487 * exists.
488 */
489bool efi_reboot_required(void)
490{
491        if (!acpi_gbl_reduced_hardware)
492                return false;
493
494        efi_reboot_quirk_mode = EFI_RESET_WARM;
495        return true;
496}
497
498bool efi_poweroff_required(void)
499{
500        return acpi_gbl_reduced_hardware || acpi_no_s5;
501}
Note: See TracBrowser for help on using the repository browser.