2019-06-01 16:08:42 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2013-02-08 23:37:06 +08:00
|
|
|
/*
|
|
|
|
* efi.c - EFI subsystem
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001,2003,2004 Dell <Matt_Domsch@dell.com>
|
|
|
|
* Copyright (C) 2004 Intel Corporation <matthew.e.tolentino@intel.com>
|
|
|
|
* Copyright (C) 2013 Tom Gundersen <teg@jklm.no>
|
|
|
|
*
|
|
|
|
* This code registers /sys/firmware/efi{,/efivars} when EFI is supported,
|
|
|
|
* allowing the efivarfs to be mounted or the efivars module to be loaded.
|
|
|
|
* The existance of /sys/firmware/efi may also be used by userspace to
|
|
|
|
* determine that the system supports EFI.
|
|
|
|
*/
|
|
|
|
|
2013-09-05 18:34:54 +08:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
#include <linux/kobject.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
2020-01-16 00:35:45 +08:00
|
|
|
#include <linux/debugfs.h>
|
2013-02-08 23:37:06 +08:00
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/efi.h>
|
2013-12-31 01:12:12 +08:00
|
|
|
#include <linux/of.h>
|
2022-09-16 20:03:06 +08:00
|
|
|
#include <linux/initrd.h>
|
2013-09-05 18:34:54 +08:00
|
|
|
#include <linux/io.h>
|
2016-11-13 05:32:31 +08:00
|
|
|
#include <linux/kexec.h>
|
2014-07-09 18:39:29 +08:00
|
|
|
#include <linux/platform_device.h>
|
2016-11-13 05:32:31 +08:00
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/reboot.h>
|
2016-07-09 00:13:12 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/acpi.h>
|
|
|
|
#include <linux/ucs2_string.h>
|
2016-03-01 05:22:52 +08:00
|
|
|
#include <linux/memblock.h>
|
2019-08-20 08:18:04 +08:00
|
|
|
#include <linux/security.h>
|
2023-11-07 13:40:55 +08:00
|
|
|
#include <linux/notifier.h>
|
2013-09-05 18:34:54 +08:00
|
|
|
|
2016-01-12 21:22:46 +08:00
|
|
|
#include <asm/early_ioremap.h>
|
2015-11-30 20:28:19 +08:00
|
|
|
|
2013-09-05 18:34:54 +08:00
|
|
|
struct efi __read_mostly efi = {
|
2020-01-21 18:17:47 +08:00
|
|
|
.runtime_supported_mask = EFI_RT_SUPPORTED_ALL,
|
2015-09-09 16:08:15 +08:00
|
|
|
.acpi = EFI_INVALID_TABLE_ADDR,
|
|
|
|
.acpi20 = EFI_INVALID_TABLE_ADDR,
|
|
|
|
.smbios = EFI_INVALID_TABLE_ADDR,
|
|
|
|
.smbios3 = EFI_INVALID_TABLE_ADDR,
|
|
|
|
.esrt = EFI_INVALID_TABLE_ADDR,
|
2018-09-22 00:32:44 +08:00
|
|
|
.tpm_log = EFI_INVALID_TABLE_ADDR,
|
2019-05-21 04:54:59 +08:00
|
|
|
.tpm_final_log = EFI_INVALID_TABLE_ADDR,
|
2020-09-05 09:31:05 +08:00
|
|
|
#ifdef CONFIG_LOAD_UEFI_KEYS
|
|
|
|
.mokvar_table = EFI_INVALID_TABLE_ADDR,
|
|
|
|
#endif
|
2022-04-13 05:21:24 +08:00
|
|
|
#ifdef CONFIG_EFI_COCO_SECRET
|
|
|
|
.coco_secret = EFI_INVALID_TABLE_ADDR,
|
|
|
|
#endif
|
2023-06-06 22:26:33 +08:00
|
|
|
#ifdef CONFIG_UNACCEPTED_MEMORY
|
|
|
|
.unaccepted = EFI_INVALID_TABLE_ADDR,
|
|
|
|
#endif
|
2013-09-05 18:34:54 +08:00
|
|
|
};
|
|
|
|
EXPORT_SYMBOL(efi);
|
2013-02-08 23:37:06 +08:00
|
|
|
|
2020-02-28 20:14:04 +08:00
|
|
|
unsigned long __ro_after_init efi_rng_seed = EFI_INVALID_TABLE_ADDR;
|
2020-01-22 22:06:54 +08:00
|
|
|
static unsigned long __initdata mem_reserve = EFI_INVALID_TABLE_ADDR;
|
2020-01-23 20:10:25 +08:00
|
|
|
static unsigned long __initdata rt_prop = EFI_INVALID_TABLE_ADDR;
|
2022-09-16 20:03:06 +08:00
|
|
|
static unsigned long __initdata initrd = EFI_INVALID_TABLE_ADDR;
|
2020-01-22 21:58:15 +08:00
|
|
|
|
2022-10-11 23:10:39 +08:00
|
|
|
extern unsigned long screen_info_table;
|
|
|
|
|
2018-03-12 16:44:56 +08:00
|
|
|
struct mm_struct efi_mm = {
|
2022-09-07 03:48:45 +08:00
|
|
|
.mm_mt = MTREE_INIT_EXT(mm_mt, MM_MT_FLAGS, efi_mm.mmap_lock),
|
2018-03-12 16:44:56 +08:00
|
|
|
.mm_users = ATOMIC_INIT(2),
|
|
|
|
.mm_count = ATOMIC_INIT(1),
|
2020-12-15 11:05:44 +08:00
|
|
|
.write_protect_seq = SEQCNT_ZERO(efi_mm.write_protect_seq),
|
2020-06-09 12:33:40 +08:00
|
|
|
MMAP_LOCK_INITIALIZER(efi_mm)
|
2018-03-12 16:44:56 +08:00
|
|
|
.page_table_lock = __SPIN_LOCK_UNLOCKED(efi_mm.page_table_lock),
|
|
|
|
.mmlist = LIST_HEAD_INIT(efi_mm.mmlist),
|
2018-07-17 03:03:31 +08:00
|
|
|
.cpu_bitmap = { [BITS_TO_LONGS(NR_CPUS)] = 0},
|
2018-03-12 16:44:56 +08:00
|
|
|
};
|
|
|
|
|
efi: Use a work queue to invoke EFI Runtime Services
Presently, when a user process requests the kernel to execute any
UEFI runtime service, the kernel temporarily switches to a separate
set of page tables that describe the virtual mapping of the UEFI
runtime services regions in memory. Since UEFI runtime services are
typically invoked with interrupts enabled, any code that may be called
during this time, will have an incorrect view of the process's address
space. Although it is unusual for code running in interrupt context to
make assumptions about the process context it runs in, there are cases
(such as the perf subsystem taking samples) where this causes problems.
So let's set up a work queue for calling UEFI runtime services, so that
the actual calls are made when the work queue items are dispatched by a
work queue worker running in a separate kernel thread. Such threads are
not expected to have userland mappings in the first place, and so the
additional mappings created for the UEFI runtime services can never
clash with any.
The ResetSystem() runtime service is not covered by the work queue
handling, since it is not expected to return, and may be called at a
time when the kernel is torn down to the point where we cannot expect
work queues to still be operational.
The non-blocking variants of SetVariable() and QueryVariableInfo()
are also excluded: these are intended to be used from atomic context,
which obviously rules out waiting for a completion to be signalled by
another thread. Note that these variants are currently only used for
UEFI runtime services calls that occur very early in the boot, and
for ones that occur in critical conditions, e.g., to flush kernel logs
to UEFI variables via efi-pstore.
Suggested-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>
[ardb: exclude ResetSystem() from the workqueue treatment
merge from 2 separate patches and rewrite commit log]
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: http://lkml.kernel.org/r/20180711094040.12506-4-ard.biesheuvel@linaro.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2018-07-11 17:40:35 +08:00
|
|
|
struct workqueue_struct *efi_rts_wq;
|
|
|
|
|
2022-03-31 23:16:54 +08:00
|
|
|
static bool disable_runtime = IS_ENABLED(CONFIG_EFI_DISABLE_RUNTIME);
|
2014-08-14 17:15:26 +08:00
|
|
|
static int __init setup_noefi(char *arg)
|
|
|
|
{
|
|
|
|
disable_runtime = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
early_param("noefi", setup_noefi);
|
|
|
|
|
|
|
|
bool efi_runtime_disabled(void)
|
|
|
|
{
|
|
|
|
return disable_runtime;
|
|
|
|
}
|
|
|
|
|
2019-11-07 09:43:11 +08:00
|
|
|
bool __pure __efi_soft_reserve_enabled(void)
|
|
|
|
{
|
|
|
|
return !efi_enabled(EFI_MEM_NO_SOFT_RESERVE);
|
|
|
|
}
|
|
|
|
|
2014-08-14 17:15:28 +08:00
|
|
|
static int __init parse_efi_cmdline(char *str)
|
|
|
|
{
|
2015-07-16 10:36:03 +08:00
|
|
|
if (!str) {
|
|
|
|
pr_warn("need at least one option\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2015-08-26 21:24:56 +08:00
|
|
|
if (parse_option_str(str, "debug"))
|
|
|
|
set_bit(EFI_DBG, &efi.flags);
|
|
|
|
|
2014-08-14 17:15:28 +08:00
|
|
|
if (parse_option_str(str, "noruntime"))
|
|
|
|
disable_runtime = true;
|
|
|
|
|
2021-09-24 21:49:19 +08:00
|
|
|
if (parse_option_str(str, "runtime"))
|
|
|
|
disable_runtime = false;
|
|
|
|
|
2019-11-07 09:43:11 +08:00
|
|
|
if (parse_option_str(str, "nosoftreserve"))
|
|
|
|
set_bit(EFI_MEM_NO_SOFT_RESERVE, &efi.flags);
|
2014-08-14 17:15:28 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
early_param("efi", parse_efi_cmdline);
|
|
|
|
|
2015-04-29 06:44:31 +08:00
|
|
|
struct kobject *efi_kobj;
|
2013-02-08 23:37:06 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Let's not leave out systab information that snuck into
|
|
|
|
* the efivars driver
|
2017-12-06 17:50:10 +08:00
|
|
|
* Note, do not add more fields in systab sysfs file as it breaks sysfs
|
|
|
|
* one value per file rule!
|
2013-02-08 23:37:06 +08:00
|
|
|
*/
|
|
|
|
static ssize_t systab_show(struct kobject *kobj,
|
|
|
|
struct kobj_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
char *str = buf;
|
|
|
|
|
|
|
|
if (!kobj || !buf)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
|
|
|
|
str += sprintf(str, "ACPI20=0x%lx\n", efi.acpi20);
|
|
|
|
if (efi.acpi != EFI_INVALID_TABLE_ADDR)
|
|
|
|
str += sprintf(str, "ACPI=0x%lx\n", efi.acpi);
|
2015-04-30 21:23:05 +08:00
|
|
|
/*
|
|
|
|
* If both SMBIOS and SMBIOS3 entry points are implemented, the
|
|
|
|
* SMBIOS3 entry point shall be preferred, so we list it first to
|
|
|
|
* let applications stop parsing after the first match.
|
|
|
|
*/
|
2014-10-14 22:34:47 +08:00
|
|
|
if (efi.smbios3 != EFI_INVALID_TABLE_ADDR)
|
|
|
|
str += sprintf(str, "SMBIOS3=0x%lx\n", efi.smbios3);
|
2015-04-30 21:23:05 +08:00
|
|
|
if (efi.smbios != EFI_INVALID_TABLE_ADDR)
|
|
|
|
str += sprintf(str, "SMBIOS=0x%lx\n", efi.smbios);
|
2013-02-08 23:37:06 +08:00
|
|
|
|
arch: Remove Itanium (IA-64) architecture
The Itanium architecture is obsolete, and an informal survey [0] reveals
that any residual use of Itanium hardware in production is mostly HP-UX
or OpenVMS based. The use of Linux on Itanium appears to be limited to
enthusiasts that occasionally boot a fresh Linux kernel to see whether
things are still working as intended, and perhaps to churn out some
distro packages that are rarely used in practice.
None of the original companies behind Itanium still produce or support
any hardware or software for the architecture, and it is listed as
'Orphaned' in the MAINTAINERS file, as apparently, none of the engineers
that contributed on behalf of those companies (nor anyone else, for that
matter) have been willing to support or maintain the architecture
upstream or even be responsible for applying the odd fix. The Intel
firmware team removed all IA-64 support from the Tianocore/EDK2
reference implementation of EFI in 2018. (Itanium is the original
architecture for which EFI was developed, and the way Linux supports it
deviates significantly from other architectures.) Some distros, such as
Debian and Gentoo, still maintain [unofficial] ia64 ports, but many have
dropped support years ago.
While the argument is being made [1] that there is a 'for the common
good' angle to being able to build and run existing projects such as the
Grid Community Toolkit [2] on Itanium for interoperability testing, the
fact remains that none of those projects are known to be deployed on
Linux/ia64, and very few people actually have access to such a system in
the first place. Even if there were ways imaginable in which Linux/ia64
could be put to good use today, what matters is whether anyone is
actually doing that, and this does not appear to be the case.
There are no emulators widely available, and so boot testing Itanium is
generally infeasible for ordinary contributors. GCC still supports IA-64
but its compile farm [3] no longer has any IA-64 machines. GLIBC would
like to get rid of IA-64 [4] too because it would permit some overdue
code cleanups. In summary, the benefits to the ecosystem of having IA-64
be part of it are mostly theoretical, whereas the maintenance overhead
of keeping it supported is real.
So let's rip off the band aid, and remove the IA-64 arch code entirely.
This follows the timeline proposed by the Debian/ia64 maintainer [5],
which removes support in a controlled manner, leaving IA-64 in a known
good state in the most recent LTS release. Other projects will follow
once the kernel support is removed.
[0] https://lore.kernel.org/all/CAMj1kXFCMh_578jniKpUtx_j8ByHnt=s7S+yQ+vGbKt9ud7+kQ@mail.gmail.com/
[1] https://lore.kernel.org/all/0075883c-7c51-00f5-2c2d-5119c1820410@web.de/
[2] https://gridcf.org/gct-docs/latest/index.html
[3] https://cfarm.tetaneutral.net/machines/list/
[4] https://lore.kernel.org/all/87bkiilpc4.fsf@mid.deneb.enyo.de/
[5] https://lore.kernel.org/all/ff58a3e76e5102c94bb5946d99187b358def688a.camel@physik.fu-berlin.de/
Acked-by: Tony Luck <tony.luck@intel.com>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-10-20 21:54:33 +08:00
|
|
|
if (IS_ENABLED(CONFIG_X86))
|
2020-01-19 22:43:53 +08:00
|
|
|
str = efi_systab_show_arch(str);
|
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
return str - buf;
|
|
|
|
}
|
|
|
|
|
2017-12-06 17:50:08 +08:00
|
|
|
static struct kobj_attribute efi_attr_systab = __ATTR_RO_MODE(systab, 0400);
|
2013-02-08 23:37:06 +08:00
|
|
|
|
2015-01-09 23:29:53 +08:00
|
|
|
static ssize_t fw_platform_size_show(struct kobject *kobj,
|
|
|
|
struct kobj_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
return sprintf(buf, "%d\n", efi_enabled(EFI_64BIT) ? 64 : 32);
|
|
|
|
}
|
|
|
|
|
2020-01-21 00:23:21 +08:00
|
|
|
extern __weak struct kobj_attribute efi_attr_fw_vendor;
|
|
|
|
extern __weak struct kobj_attribute efi_attr_runtime;
|
|
|
|
extern __weak struct kobj_attribute efi_attr_config_table;
|
2015-01-09 23:29:53 +08:00
|
|
|
static struct kobj_attribute efi_attr_fw_platform_size =
|
|
|
|
__ATTR_RO(fw_platform_size);
|
2013-12-20 18:02:17 +08:00
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
static struct attribute *efi_subsys_attrs[] = {
|
|
|
|
&efi_attr_systab.attr,
|
2020-01-21 00:23:21 +08:00
|
|
|
&efi_attr_fw_platform_size.attr,
|
2013-12-20 18:02:17 +08:00
|
|
|
&efi_attr_fw_vendor.attr,
|
|
|
|
&efi_attr_runtime.attr,
|
|
|
|
&efi_attr_config_table.attr,
|
|
|
|
NULL,
|
2013-02-08 23:37:06 +08:00
|
|
|
};
|
|
|
|
|
2020-01-21 00:23:21 +08:00
|
|
|
umode_t __weak efi_attr_is_visible(struct kobject *kobj, struct attribute *attr,
|
|
|
|
int n)
|
2013-12-20 18:02:17 +08:00
|
|
|
{
|
2014-07-01 01:52:58 +08:00
|
|
|
return attr->mode;
|
2013-12-20 18:02:17 +08:00
|
|
|
}
|
|
|
|
|
2017-08-19 03:49:46 +08:00
|
|
|
static const struct attribute_group efi_subsys_attr_group = {
|
2013-02-08 23:37:06 +08:00
|
|
|
.attrs = efi_subsys_attrs,
|
2013-12-20 18:02:17 +08:00
|
|
|
.is_visible = efi_attr_is_visible,
|
2013-02-08 23:37:06 +08:00
|
|
|
};
|
|
|
|
|
2023-11-07 13:40:55 +08:00
|
|
|
struct blocking_notifier_head efivar_ops_nh;
|
|
|
|
EXPORT_SYMBOL_GPL(efivar_ops_nh);
|
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
static struct efivars generic_efivars;
|
|
|
|
static struct efivar_operations generic_ops;
|
|
|
|
|
2023-01-20 00:42:54 +08:00
|
|
|
static bool generic_ops_supported(void)
|
|
|
|
{
|
|
|
|
unsigned long name_size;
|
|
|
|
efi_status_t status;
|
|
|
|
efi_char16_t name;
|
|
|
|
efi_guid_t guid;
|
|
|
|
|
|
|
|
name_size = sizeof(name);
|
|
|
|
|
2024-03-23 14:33:33 +08:00
|
|
|
if (!efi.get_next_variable)
|
|
|
|
return false;
|
2023-01-20 00:42:54 +08:00
|
|
|
status = efi.get_next_variable(&name_size, &name, &guid);
|
|
|
|
if (status == EFI_UNSUPPORTED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
static int generic_ops_register(void)
|
|
|
|
{
|
2023-01-20 00:42:54 +08:00
|
|
|
if (!generic_ops_supported())
|
|
|
|
return 0;
|
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
generic_ops.get_variable = efi.get_variable;
|
|
|
|
generic_ops.get_next_variable = efi.get_next_variable;
|
2013-04-30 18:30:24 +08:00
|
|
|
generic_ops.query_variable_store = efi_query_variable_store;
|
2023-05-17 23:38:12 +08:00
|
|
|
generic_ops.query_variable_info = efi.query_variable_info;
|
2013-02-08 23:37:06 +08:00
|
|
|
|
2020-07-08 18:01:57 +08:00
|
|
|
if (efi_rt_services_supported(EFI_RT_SUPPORTED_SET_VARIABLE)) {
|
|
|
|
generic_ops.set_variable = efi.set_variable;
|
|
|
|
generic_ops.set_variable_nonblocking = efi.set_variable_nonblocking;
|
|
|
|
}
|
2023-01-17 20:43:09 +08:00
|
|
|
return efivars_register(&generic_efivars, &generic_ops);
|
2013-02-08 23:37:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void generic_ops_unregister(void)
|
|
|
|
{
|
2023-01-20 00:42:54 +08:00
|
|
|
if (!generic_ops.get_variable)
|
|
|
|
return;
|
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
efivars_unregister(&generic_efivars);
|
|
|
|
}
|
|
|
|
|
2023-11-07 13:40:52 +08:00
|
|
|
void efivars_generic_ops_register(void)
|
|
|
|
{
|
|
|
|
generic_ops_register();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(efivars_generic_ops_register);
|
|
|
|
|
|
|
|
void efivars_generic_ops_unregister(void)
|
|
|
|
{
|
|
|
|
generic_ops_unregister();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(efivars_generic_ops_unregister);
|
|
|
|
|
2020-06-16 04:24:08 +08:00
|
|
|
#ifdef CONFIG_EFI_CUSTOM_SSDT_OVERLAYS
|
2022-06-17 16:34:48 +08:00
|
|
|
#define EFIVAR_SSDT_NAME_MAX 16UL
|
2016-07-09 00:13:12 +08:00
|
|
|
static char efivar_ssdt[EFIVAR_SSDT_NAME_MAX] __initdata;
|
|
|
|
static int __init efivar_ssdt_setup(char *str)
|
|
|
|
{
|
2019-08-20 08:18:04 +08:00
|
|
|
int ret = security_locked_down(LOCKDOWN_ACPI_TABLES);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2016-07-09 00:13:12 +08:00
|
|
|
if (strlen(str) < sizeof(efivar_ssdt))
|
|
|
|
memcpy(efivar_ssdt, str, strlen(str));
|
|
|
|
else
|
|
|
|
pr_warn("efivar_ssdt: name too long: %s\n", str);
|
2022-03-01 12:18:51 +08:00
|
|
|
return 1;
|
2016-07-09 00:13:12 +08:00
|
|
|
}
|
|
|
|
__setup("efivar_ssdt=", efivar_ssdt_setup);
|
|
|
|
|
|
|
|
static __init int efivar_ssdt_load(void)
|
|
|
|
{
|
2022-06-17 16:34:48 +08:00
|
|
|
unsigned long name_size = 256;
|
|
|
|
efi_char16_t *name = NULL;
|
|
|
|
efi_status_t status;
|
|
|
|
efi_guid_t guid;
|
2016-07-09 00:13:12 +08:00
|
|
|
|
2019-10-03 00:58:59 +08:00
|
|
|
if (!efivar_ssdt[0])
|
|
|
|
return 0;
|
|
|
|
|
2022-06-17 16:34:48 +08:00
|
|
|
name = kzalloc(name_size, GFP_KERNEL);
|
|
|
|
if (!name)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
char utf8_name[EFIVAR_SSDT_NAME_MAX];
|
|
|
|
unsigned long data_size = 0;
|
|
|
|
void *data;
|
|
|
|
int limit;
|
|
|
|
|
|
|
|
status = efi.get_next_variable(&name_size, name, &guid);
|
|
|
|
if (status == EFI_NOT_FOUND) {
|
|
|
|
break;
|
|
|
|
} else if (status == EFI_BUFFER_TOO_SMALL) {
|
2023-09-24 22:26:33 +08:00
|
|
|
efi_char16_t *name_tmp =
|
|
|
|
krealloc(name, name_size, GFP_KERNEL);
|
|
|
|
if (!name_tmp) {
|
|
|
|
kfree(name);
|
2022-06-17 16:34:48 +08:00
|
|
|
return -ENOMEM;
|
2023-09-24 22:26:33 +08:00
|
|
|
}
|
|
|
|
name = name_tmp;
|
2022-06-17 16:34:48 +08:00
|
|
|
continue;
|
2016-07-09 00:13:12 +08:00
|
|
|
}
|
|
|
|
|
2022-06-17 16:34:48 +08:00
|
|
|
limit = min(EFIVAR_SSDT_NAME_MAX, name_size);
|
|
|
|
ucs2_as_utf8(utf8_name, name, limit - 1);
|
|
|
|
if (strncmp(utf8_name, efivar_ssdt, limit) != 0)
|
|
|
|
continue;
|
2016-07-09 00:13:12 +08:00
|
|
|
|
2022-06-17 16:34:48 +08:00
|
|
|
pr_info("loading SSDT from variable %s-%pUl\n", efivar_ssdt, &guid);
|
2016-07-09 00:13:12 +08:00
|
|
|
|
2022-06-17 16:34:48 +08:00
|
|
|
status = efi.get_variable(name, &guid, NULL, &data_size, NULL);
|
|
|
|
if (status != EFI_BUFFER_TOO_SMALL || !data_size)
|
|
|
|
return -EIO;
|
2016-07-09 00:13:12 +08:00
|
|
|
|
2022-06-17 16:34:48 +08:00
|
|
|
data = kmalloc(data_size, GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2016-07-09 00:13:12 +08:00
|
|
|
|
2022-06-17 16:34:48 +08:00
|
|
|
status = efi.get_variable(name, &guid, NULL, &data_size, data);
|
|
|
|
if (status == EFI_SUCCESS) {
|
|
|
|
acpi_status ret = acpi_load_table(data, NULL);
|
|
|
|
if (ret)
|
|
|
|
pr_err("failed to load table: %u\n", ret);
|
2022-10-14 18:25:52 +08:00
|
|
|
else
|
|
|
|
continue;
|
2022-06-17 16:34:48 +08:00
|
|
|
} else {
|
|
|
|
pr_err("failed to get var data: 0x%lx\n", status);
|
|
|
|
}
|
2016-07-09 00:13:12 +08:00
|
|
|
kfree(data);
|
|
|
|
}
|
2022-06-17 16:34:48 +08:00
|
|
|
return 0;
|
2016-07-09 00:13:12 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline int efivar_ssdt_load(void) { return 0; }
|
|
|
|
#endif
|
|
|
|
|
2020-01-16 00:35:45 +08:00
|
|
|
#ifdef CONFIG_DEBUG_FS
|
|
|
|
|
|
|
|
#define EFI_DEBUGFS_MAX_BLOBS 32
|
|
|
|
|
|
|
|
static struct debugfs_blob_wrapper debugfs_blob[EFI_DEBUGFS_MAX_BLOBS];
|
|
|
|
|
|
|
|
static void __init efi_debugfs_init(void)
|
|
|
|
{
|
|
|
|
struct dentry *efi_debugfs;
|
|
|
|
efi_memory_desc_t *md;
|
|
|
|
char name[32];
|
|
|
|
int type_count[EFI_BOOT_SERVICES_DATA + 1] = {};
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
efi_debugfs = debugfs_create_dir("efi", NULL);
|
|
|
|
if (IS_ERR_OR_NULL(efi_debugfs))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for_each_efi_memory_desc(md) {
|
|
|
|
switch (md->type) {
|
|
|
|
case EFI_BOOT_SERVICES_CODE:
|
|
|
|
snprintf(name, sizeof(name), "boot_services_code%d",
|
|
|
|
type_count[md->type]++);
|
|
|
|
break;
|
|
|
|
case EFI_BOOT_SERVICES_DATA:
|
|
|
|
snprintf(name, sizeof(name), "boot_services_data%d",
|
|
|
|
type_count[md->type]++);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= EFI_DEBUGFS_MAX_BLOBS) {
|
|
|
|
pr_warn("More then %d EFI boot service segments, only showing first %d in debugfs\n",
|
|
|
|
EFI_DEBUGFS_MAX_BLOBS, EFI_DEBUGFS_MAX_BLOBS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
debugfs_blob[i].size = md->num_pages << EFI_PAGE_SHIFT;
|
|
|
|
debugfs_blob[i].data = memremap(md->phys_addr,
|
|
|
|
debugfs_blob[i].size,
|
|
|
|
MEMREMAP_WB);
|
|
|
|
if (!debugfs_blob[i].data)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
debugfs_create_blob(name, 0400, efi_debugfs, &debugfs_blob[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline void efi_debugfs_init(void) {}
|
|
|
|
#endif
|
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
/*
|
|
|
|
* We register the efi subsystem with the firmware subsystem and the
|
|
|
|
* efivars subsystem with the efi subsystem, if the system was booted with
|
|
|
|
* EFI.
|
|
|
|
*/
|
|
|
|
static int __init efisubsys_init(void)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2020-01-21 18:17:47 +08:00
|
|
|
if (!efi_enabled(EFI_RUNTIME_SERVICES))
|
|
|
|
efi.runtime_supported_mask = 0;
|
|
|
|
|
2020-02-28 20:14:08 +08:00
|
|
|
if (!efi_enabled(EFI_BOOT))
|
|
|
|
return 0;
|
|
|
|
|
2020-01-21 18:17:47 +08:00
|
|
|
if (efi.runtime_supported_mask) {
|
|
|
|
/*
|
|
|
|
* Since we process only one efi_runtime_service() at a time, an
|
|
|
|
* ordered workqueue (which creates only one execution context)
|
|
|
|
* should suffice for all our needs.
|
|
|
|
*/
|
|
|
|
efi_rts_wq = alloc_ordered_workqueue("efi_rts_wq", 0);
|
|
|
|
if (!efi_rts_wq) {
|
|
|
|
pr_err("Creating efi_rts_wq failed, EFI runtime services disabled.\n");
|
|
|
|
clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
|
|
|
|
efi.runtime_supported_mask = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
efi: Use a work queue to invoke EFI Runtime Services
Presently, when a user process requests the kernel to execute any
UEFI runtime service, the kernel temporarily switches to a separate
set of page tables that describe the virtual mapping of the UEFI
runtime services regions in memory. Since UEFI runtime services are
typically invoked with interrupts enabled, any code that may be called
during this time, will have an incorrect view of the process's address
space. Although it is unusual for code running in interrupt context to
make assumptions about the process context it runs in, there are cases
(such as the perf subsystem taking samples) where this causes problems.
So let's set up a work queue for calling UEFI runtime services, so that
the actual calls are made when the work queue items are dispatched by a
work queue worker running in a separate kernel thread. Such threads are
not expected to have userland mappings in the first place, and so the
additional mappings created for the UEFI runtime services can never
clash with any.
The ResetSystem() runtime service is not covered by the work queue
handling, since it is not expected to return, and may be called at a
time when the kernel is torn down to the point where we cannot expect
work queues to still be operational.
The non-blocking variants of SetVariable() and QueryVariableInfo()
are also excluded: these are intended to be used from atomic context,
which obviously rules out waiting for a completion to be signalled by
another thread. Note that these variants are currently only used for
UEFI runtime services calls that occur very early in the boot, and
for ones that occur in critical conditions, e.g., to flush kernel logs
to UEFI variables via efi-pstore.
Suggested-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>
[ardb: exclude ResetSystem() from the workqueue treatment
merge from 2 separate patches and rewrite commit log]
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: linux-efi@vger.kernel.org
Link: http://lkml.kernel.org/r/20180711094040.12506-4-ard.biesheuvel@linaro.org
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2018-07-11 17:40:35 +08:00
|
|
|
}
|
|
|
|
|
2020-01-23 16:14:09 +08:00
|
|
|
if (efi_rt_services_supported(EFI_RT_SUPPORTED_TIME_SERVICES))
|
|
|
|
platform_device_register_simple("rtc-efi", 0, NULL, 0);
|
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
/* We register the efi directory at /sys/firmware/efi */
|
|
|
|
efi_kobj = kobject_create_and_add("efi", firmware_kobj);
|
|
|
|
if (!efi_kobj) {
|
|
|
|
pr_err("efi: Firmware registration failed.\n");
|
2022-12-19 17:10:04 +08:00
|
|
|
error = -ENOMEM;
|
|
|
|
goto err_destroy_wq;
|
2013-02-08 23:37:06 +08:00
|
|
|
}
|
|
|
|
|
2020-07-08 18:01:57 +08:00
|
|
|
if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
|
|
|
|
EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME)) {
|
2020-01-23 16:12:00 +08:00
|
|
|
error = generic_ops_register();
|
|
|
|
if (error)
|
|
|
|
goto err_put;
|
2020-11-24 01:28:17 +08:00
|
|
|
efivar_ssdt_load();
|
2020-01-23 16:12:00 +08:00
|
|
|
platform_device_register_simple("efivars", 0, NULL, 0);
|
|
|
|
}
|
2016-07-09 00:13:12 +08:00
|
|
|
|
2023-11-07 13:40:55 +08:00
|
|
|
BLOCKING_INIT_NOTIFIER_HEAD(&efivar_ops_nh);
|
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
error = sysfs_create_group(efi_kobj, &efi_subsys_attr_group);
|
|
|
|
if (error) {
|
|
|
|
pr_err("efi: Sysfs attribute export failed with error %d.\n",
|
|
|
|
error);
|
|
|
|
goto err_unregister;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and the standard mountpoint for efivarfs */
|
2015-05-14 06:35:41 +08:00
|
|
|
error = sysfs_create_mount_point(efi_kobj, "efivars");
|
|
|
|
if (error) {
|
2013-02-08 23:37:06 +08:00
|
|
|
pr_err("efivars: Subsystem registration failed.\n");
|
|
|
|
goto err_remove_group;
|
|
|
|
}
|
|
|
|
|
2020-01-16 00:35:45 +08:00
|
|
|
if (efi_enabled(EFI_DBG) && efi_enabled(EFI_PRESERVE_BS_REGIONS))
|
|
|
|
efi_debugfs_init();
|
|
|
|
|
2022-04-13 05:21:26 +08:00
|
|
|
#ifdef CONFIG_EFI_COCO_SECRET
|
|
|
|
if (efi.coco_secret != EFI_INVALID_TABLE_ADDR)
|
|
|
|
platform_device_register_simple("efi_secret", 0, NULL, 0);
|
|
|
|
#endif
|
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_remove_group:
|
|
|
|
sysfs_remove_group(efi_kobj, &efi_subsys_attr_group);
|
|
|
|
err_unregister:
|
2020-07-08 18:01:57 +08:00
|
|
|
if (efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE |
|
|
|
|
EFI_RT_SUPPORTED_GET_NEXT_VARIABLE_NAME))
|
2020-01-23 16:12:00 +08:00
|
|
|
generic_ops_unregister();
|
2013-02-08 23:37:06 +08:00
|
|
|
err_put:
|
|
|
|
kobject_put(efi_kobj);
|
2022-11-07 16:17:16 +08:00
|
|
|
efi_kobj = NULL;
|
2022-12-19 17:10:04 +08:00
|
|
|
err_destroy_wq:
|
|
|
|
if (efi_rts_wq)
|
|
|
|
destroy_workqueue(efi_rts_wq);
|
|
|
|
|
2013-02-08 23:37:06 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
subsys_initcall(efisubsys_init);
|
2013-09-05 18:34:54 +08:00
|
|
|
|
2022-06-14 17:21:52 +08:00
|
|
|
void __init efi_find_mirror(void)
|
|
|
|
{
|
|
|
|
efi_memory_desc_t *md;
|
|
|
|
u64 mirror_size = 0, total_size = 0;
|
|
|
|
|
|
|
|
if (!efi_enabled(EFI_MEMMAP))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for_each_efi_memory_desc(md) {
|
|
|
|
unsigned long long start = md->phys_addr;
|
|
|
|
unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
|
|
|
|
|
|
|
|
total_size += size;
|
|
|
|
if (md->attribute & EFI_MEMORY_MORE_RELIABLE) {
|
|
|
|
memblock_mark_mirror(start, size);
|
|
|
|
mirror_size += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mirror_size)
|
|
|
|
pr_info("Memory: %lldM/%lldM mirrored memory\n",
|
|
|
|
mirror_size>>20, total_size>>20);
|
|
|
|
}
|
|
|
|
|
2015-04-29 06:44:31 +08:00
|
|
|
/*
|
|
|
|
* Find the efi memory descriptor for a given physical address. Given a
|
2016-02-27 23:52:50 +08:00
|
|
|
* physical address, determine if it exists within an EFI Memory Map entry,
|
2015-04-29 06:44:31 +08:00
|
|
|
* and if so, populate the supplied memory descriptor with the appropriate
|
|
|
|
* data.
|
|
|
|
*/
|
2023-01-20 03:03:57 +08:00
|
|
|
int __efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
|
2015-04-29 06:44:31 +08:00
|
|
|
{
|
2016-02-27 23:52:50 +08:00
|
|
|
efi_memory_desc_t *md;
|
2015-04-29 06:44:31 +08:00
|
|
|
|
|
|
|
if (!efi_enabled(EFI_MEMMAP)) {
|
|
|
|
pr_err_once("EFI_MEMMAP is not enabled.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!out_md) {
|
|
|
|
pr_err_once("out_md is null.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-02-27 23:52:50 +08:00
|
|
|
for_each_efi_memory_desc(md) {
|
2015-04-29 06:44:31 +08:00
|
|
|
u64 size;
|
|
|
|
u64 end;
|
|
|
|
|
2023-01-20 03:03:56 +08:00
|
|
|
/* skip bogus entries (including empty ones) */
|
|
|
|
if ((md->phys_addr & (EFI_PAGE_SIZE - 1)) ||
|
|
|
|
(md->num_pages <= 0) ||
|
|
|
|
(md->num_pages > (U64_MAX - md->phys_addr) >> EFI_PAGE_SHIFT))
|
|
|
|
continue;
|
|
|
|
|
2015-04-29 06:44:31 +08:00
|
|
|
size = md->num_pages << EFI_PAGE_SHIFT;
|
|
|
|
end = md->phys_addr + size;
|
|
|
|
if (phys_addr >= md->phys_addr && phys_addr < end) {
|
|
|
|
memcpy(out_md, md, sizeof(*out_md));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2023-01-20 03:03:57 +08:00
|
|
|
extern int efi_mem_desc_lookup(u64 phys_addr, efi_memory_desc_t *out_md)
|
|
|
|
__weak __alias(__efi_mem_desc_lookup);
|
|
|
|
|
2015-04-29 06:44:31 +08:00
|
|
|
/*
|
|
|
|
* Calculate the highest address of an efi memory descriptor.
|
|
|
|
*/
|
|
|
|
u64 __init efi_mem_desc_end(efi_memory_desc_t *md)
|
|
|
|
{
|
|
|
|
u64 size = md->num_pages << EFI_PAGE_SHIFT;
|
|
|
|
u64 end = md->phys_addr + size;
|
|
|
|
return end;
|
|
|
|
}
|
2013-09-05 18:34:54 +08:00
|
|
|
|
2016-03-01 05:22:52 +08:00
|
|
|
void __init __weak efi_arch_mem_reserve(phys_addr_t addr, u64 size) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* efi_mem_reserve - Reserve an EFI memory region
|
|
|
|
* @addr: Physical address to reserve
|
|
|
|
* @size: Size of reservation
|
|
|
|
*
|
|
|
|
* Mark a region as reserved from general kernel allocation and
|
|
|
|
* prevent it being released by efi_free_boot_services().
|
|
|
|
*
|
|
|
|
* This function should be called drivers once they've parsed EFI
|
|
|
|
* configuration tables to figure out where their data lives, e.g.
|
|
|
|
* efi_esrt_init().
|
|
|
|
*/
|
|
|
|
void __init efi_mem_reserve(phys_addr_t addr, u64 size)
|
|
|
|
{
|
2023-01-20 03:04:00 +08:00
|
|
|
/* efi_mem_reserve() does not work under Xen */
|
|
|
|
if (WARN_ON_ONCE(efi_enabled(EFI_PARAVIRT)))
|
|
|
|
return;
|
|
|
|
|
2016-03-01 05:22:52 +08:00
|
|
|
if (!memblock_is_region_reserved(addr, size))
|
|
|
|
memblock_reserve(addr, size);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Some architectures (x86) reserve all boot services ranges
|
|
|
|
* until efi_free_boot_services() because of buggy firmware
|
|
|
|
* implementations. This means the above memblock_reserve() is
|
|
|
|
* superfluous on x86 and instead what it needs to do is
|
|
|
|
* ensure the @start, @size is not freed.
|
|
|
|
*/
|
|
|
|
efi_arch_mem_reserve(addr, size);
|
|
|
|
}
|
|
|
|
|
2020-01-22 21:40:57 +08:00
|
|
|
static const efi_config_table_type_t common_tables[] __initconst = {
|
2020-03-26 16:24:14 +08:00
|
|
|
{ACPI_20_TABLE_GUID, &efi.acpi20, "ACPI 2.0" },
|
|
|
|
{ACPI_TABLE_GUID, &efi.acpi, "ACPI" },
|
|
|
|
{SMBIOS_TABLE_GUID, &efi.smbios, "SMBIOS" },
|
|
|
|
{SMBIOS3_TABLE_GUID, &efi.smbios3, "SMBIOS 3.0" },
|
|
|
|
{EFI_SYSTEM_RESOURCE_TABLE_GUID, &efi.esrt, "ESRT" },
|
|
|
|
{EFI_MEMORY_ATTRIBUTES_TABLE_GUID, &efi_mem_attr_table, "MEMATTR" },
|
|
|
|
{LINUX_EFI_RANDOM_SEED_TABLE_GUID, &efi_rng_seed, "RNG" },
|
|
|
|
{LINUX_EFI_TPM_EVENT_LOG_GUID, &efi.tpm_log, "TPMEventLog" },
|
2024-03-07 22:56:10 +08:00
|
|
|
{EFI_TCG2_FINAL_EVENTS_TABLE_GUID, &efi.tpm_final_log, "TPMFinalLog" },
|
2024-02-15 11:00:02 +08:00
|
|
|
{EFI_CC_FINAL_EVENTS_TABLE_GUID, &efi.tpm_final_log, "CCFinalLog" },
|
2020-03-26 16:24:14 +08:00
|
|
|
{LINUX_EFI_MEMRESERVE_TABLE_GUID, &mem_reserve, "MEMRESERVE" },
|
2022-09-16 20:03:06 +08:00
|
|
|
{LINUX_EFI_INITRD_MEDIA_GUID, &initrd, "INITRD" },
|
2020-03-26 16:24:14 +08:00
|
|
|
{EFI_RT_PROPERTIES_TABLE_GUID, &rt_prop, "RTPROP" },
|
2019-07-11 02:59:15 +08:00
|
|
|
#ifdef CONFIG_EFI_RCI2_TABLE
|
2020-03-26 16:24:14 +08:00
|
|
|
{DELLEMC_EFI_RCI2_TABLE_GUID, &rci2_table_phys },
|
2020-09-05 09:31:05 +08:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_LOAD_UEFI_KEYS
|
|
|
|
{LINUX_EFI_MOK_VARIABLE_TABLE_GUID, &efi.mokvar_table, "MOKvar" },
|
2022-04-13 05:21:24 +08:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_EFI_COCO_SECRET
|
|
|
|
{LINUX_EFI_COCO_SECRET_AREA_GUID, &efi.coco_secret, "CocoSecret" },
|
2022-10-11 23:10:39 +08:00
|
|
|
#endif
|
2023-06-06 22:26:33 +08:00
|
|
|
#ifdef CONFIG_UNACCEPTED_MEMORY
|
|
|
|
{LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID, &efi.unaccepted, "Unaccepted" },
|
|
|
|
#endif
|
2022-10-11 23:10:39 +08:00
|
|
|
#ifdef CONFIG_EFI_GENERIC_STUB
|
|
|
|
{LINUX_EFI_SCREEN_INFO_TABLE_GUID, &screen_info_table },
|
2019-07-11 02:59:15 +08:00
|
|
|
#endif
|
2020-03-26 16:24:14 +08:00
|
|
|
{},
|
2013-09-05 18:34:54 +08:00
|
|
|
};
|
|
|
|
|
2020-01-22 21:40:57 +08:00
|
|
|
static __init int match_config_table(const efi_guid_t *guid,
|
2013-09-05 18:34:54 +08:00
|
|
|
unsigned long table,
|
2020-01-22 21:40:57 +08:00
|
|
|
const efi_config_table_type_t *table_types)
|
2013-09-05 18:34:54 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2020-03-26 16:34:35 +08:00
|
|
|
for (i = 0; efi_guidcmp(table_types[i].guid, NULL_GUID); i++) {
|
2023-01-20 03:03:58 +08:00
|
|
|
if (efi_guidcmp(*guid, table_types[i].guid))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!efi_config_table_is_usable(guid, table)) {
|
2020-03-26 16:34:35 +08:00
|
|
|
if (table_types[i].name[0])
|
2023-01-20 03:03:58 +08:00
|
|
|
pr_cont("(%s=0x%lx unusable) ",
|
2020-03-26 16:34:35 +08:00
|
|
|
table_types[i].name, table);
|
|
|
|
return 1;
|
2013-09-05 18:34:54 +08:00
|
|
|
}
|
2023-01-20 03:03:58 +08:00
|
|
|
|
|
|
|
*(table_types[i].ptr) = table;
|
|
|
|
if (table_types[i].name[0])
|
|
|
|
pr_cont("%s=0x%lx ", table_types[i].name, table);
|
|
|
|
return 1;
|
2013-09-05 18:34:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-08-15 00:12:47 +08:00
|
|
|
/**
|
|
|
|
* reserve_unaccepted - Map and reserve unaccepted configuration table
|
|
|
|
* @unaccepted: Pointer to unaccepted memory table
|
|
|
|
*
|
|
|
|
* memblock_add() makes sure that the table is mapped in direct mapping. During
|
|
|
|
* normal boot it happens automatically because the table is allocated from
|
|
|
|
* usable memory. But during crashkernel boot only memory specifically reserved
|
|
|
|
* for crash scenario is mapped. memblock_add() forces the table to be mapped
|
|
|
|
* in crashkernel case.
|
|
|
|
*
|
|
|
|
* Align the range to the nearest page borders. Ranges smaller than page size
|
|
|
|
* are not going to be mapped.
|
|
|
|
*
|
|
|
|
* memblock_reserve() makes sure that future allocations will not touch the
|
|
|
|
* table.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static __init void reserve_unaccepted(struct efi_unaccepted_memory *unaccepted)
|
|
|
|
{
|
|
|
|
phys_addr_t start, size;
|
|
|
|
|
|
|
|
start = PAGE_ALIGN_DOWN(efi.unaccepted);
|
|
|
|
size = PAGE_ALIGN(sizeof(*unaccepted) + unaccepted->size);
|
|
|
|
|
|
|
|
memblock_add(start, size);
|
|
|
|
memblock_reserve(start, size);
|
|
|
|
}
|
|
|
|
|
2020-01-22 21:40:57 +08:00
|
|
|
int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
|
|
|
|
int count,
|
|
|
|
const efi_config_table_type_t *arch_tables)
|
2013-09-05 18:34:54 +08:00
|
|
|
{
|
2020-01-22 21:40:57 +08:00
|
|
|
const efi_config_table_64_t *tbl64 = (void *)config_tables;
|
|
|
|
const efi_config_table_32_t *tbl32 = (void *)config_tables;
|
|
|
|
const efi_guid_t *guid;
|
|
|
|
unsigned long table;
|
2014-10-18 21:04:15 +08:00
|
|
|
int i;
|
2013-09-05 18:34:54 +08:00
|
|
|
|
|
|
|
pr_info("");
|
2014-10-18 21:04:15 +08:00
|
|
|
for (i = 0; i < count; i++) {
|
2020-01-22 21:40:57 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_X86)) {
|
|
|
|
guid = &config_tables[i].guid;
|
|
|
|
table = (unsigned long)config_tables[i].table;
|
|
|
|
} else if (efi_enabled(EFI_64BIT)) {
|
|
|
|
guid = &tbl64[i].guid;
|
|
|
|
table = tbl64[i].table;
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_X86_32) &&
|
|
|
|
tbl64[i].table > U32_MAX) {
|
2013-09-05 18:34:54 +08:00
|
|
|
pr_cont("\n");
|
|
|
|
pr_err("Table located above 4GB, disabling EFI.\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-22 21:40:57 +08:00
|
|
|
guid = &tbl32[i].guid;
|
|
|
|
table = tbl32[i].table;
|
2013-09-05 18:34:54 +08:00
|
|
|
}
|
|
|
|
|
2020-03-26 16:34:35 +08:00
|
|
|
if (!match_config_table(guid, table, common_tables) && arch_tables)
|
2020-01-22 21:40:57 +08:00
|
|
|
match_config_table(guid, table, arch_tables);
|
2013-09-05 18:34:54 +08:00
|
|
|
}
|
|
|
|
pr_cont("\n");
|
2014-01-15 21:36:33 +08:00
|
|
|
set_bit(EFI_CONFIG_TABLES, &efi.flags);
|
2015-09-23 22:29:34 +08:00
|
|
|
|
2020-02-28 20:14:04 +08:00
|
|
|
if (efi_rng_seed != EFI_INVALID_TABLE_ADDR) {
|
2016-11-13 05:32:31 +08:00
|
|
|
struct linux_efi_random_seed *seed;
|
|
|
|
u32 size = 0;
|
|
|
|
|
2020-02-28 20:14:04 +08:00
|
|
|
seed = early_memremap(efi_rng_seed, sizeof(*seed));
|
2016-11-13 05:32:31 +08:00
|
|
|
if (seed != NULL) {
|
efi: random: combine bootloader provided RNG seed with RNG protocol output
Instead of blindly creating the EFI random seed configuration table if
the RNG protocol is implemented and works, check whether such a EFI
configuration table was provided by an earlier boot stage and if so,
concatenate the existing and the new seeds, leaving it up to the core
code to mix it in and credit it the way it sees fit.
This can be used for, e.g., systemd-boot, to pass an additional seed to
Linux in a way that can be consumed by the kernel very early. In that
case, the following definitions should be used to pass the seed to the
EFI stub:
struct linux_efi_random_seed {
u32 size; // of the 'seed' array in bytes
u8 seed[];
};
The memory for the struct must be allocated as EFI_ACPI_RECLAIM_MEMORY
pool memory, and the address of the struct in memory should be installed
as a EFI configuration table using the following GUID:
LINUX_EFI_RANDOM_SEED_TABLE_GUID 1ce1e5bc-7ceb-42f2-81e5-8aadf180f57b
Note that doing so is safe even on kernels that were built without this
patch applied, but the seed will simply be overwritten with a seed
derived from the EFI RNG protocol, if available. The recommended seed
size is 32 bytes, and seeds larger than 512 bytes are considered
corrupted and ignored entirely.
In order to preserve forward secrecy, seeds from previous bootloaders
are memzero'd out, and in order to preserve memory, those older seeds
are also freed from memory. Freeing from memory without first memzeroing
is not safe to do, as it's possible that nothing else will ever
overwrite those pages used by EFI.
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
[ardb: incorporate Jason's followup changes to extend the maximum seed
size on the consumer end, memzero() it and drop a needless printk]
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-10-20 16:39:10 +08:00
|
|
|
size = min_t(u32, seed->size, SZ_1K); // sanity check
|
2016-11-13 05:32:31 +08:00
|
|
|
early_memunmap(seed, sizeof(*seed));
|
|
|
|
} else {
|
|
|
|
pr_err("Could not map UEFI random seed!\n");
|
|
|
|
}
|
|
|
|
if (size > 0) {
|
2020-02-28 20:14:04 +08:00
|
|
|
seed = early_memremap(efi_rng_seed,
|
|
|
|
sizeof(*seed) + size);
|
2016-11-13 05:32:31 +08:00
|
|
|
if (seed != NULL) {
|
2020-02-21 16:48:49 +08:00
|
|
|
add_bootloader_randomness(seed->bits, size);
|
efi: random: combine bootloader provided RNG seed with RNG protocol output
Instead of blindly creating the EFI random seed configuration table if
the RNG protocol is implemented and works, check whether such a EFI
configuration table was provided by an earlier boot stage and if so,
concatenate the existing and the new seeds, leaving it up to the core
code to mix it in and credit it the way it sees fit.
This can be used for, e.g., systemd-boot, to pass an additional seed to
Linux in a way that can be consumed by the kernel very early. In that
case, the following definitions should be used to pass the seed to the
EFI stub:
struct linux_efi_random_seed {
u32 size; // of the 'seed' array in bytes
u8 seed[];
};
The memory for the struct must be allocated as EFI_ACPI_RECLAIM_MEMORY
pool memory, and the address of the struct in memory should be installed
as a EFI configuration table using the following GUID:
LINUX_EFI_RANDOM_SEED_TABLE_GUID 1ce1e5bc-7ceb-42f2-81e5-8aadf180f57b
Note that doing so is safe even on kernels that were built without this
patch applied, but the seed will simply be overwritten with a seed
derived from the EFI RNG protocol, if available. The recommended seed
size is 32 bytes, and seeds larger than 512 bytes are considered
corrupted and ignored entirely.
In order to preserve forward secrecy, seeds from previous bootloaders
are memzero'd out, and in order to preserve memory, those older seeds
are also freed from memory. Freeing from memory without first memzeroing
is not safe to do, as it's possible that nothing else will ever
overwrite those pages used by EFI.
Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com>
[ardb: incorporate Jason's followup changes to extend the maximum seed
size on the consumer end, memzero() it and drop a needless printk]
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2022-10-20 16:39:10 +08:00
|
|
|
memzero_explicit(seed->bits, size);
|
2016-11-13 05:32:31 +08:00
|
|
|
early_memunmap(seed, sizeof(*seed) + size);
|
|
|
|
} else {
|
|
|
|
pr_err("Could not map UEFI random seed!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:08:51 +08:00
|
|
|
if (!IS_ENABLED(CONFIG_X86_32) && efi_enabled(EFI_MEMMAP))
|
2017-06-22 18:51:36 +08:00
|
|
|
efi_memattr_init();
|
2017-01-31 21:21:35 +08:00
|
|
|
|
2017-09-20 16:13:39 +08:00
|
|
|
efi_tpm_eventlog_init();
|
|
|
|
|
2020-01-22 22:06:54 +08:00
|
|
|
if (mem_reserve != EFI_INVALID_TABLE_ADDR) {
|
|
|
|
unsigned long prsv = mem_reserve;
|
2018-09-22 00:32:44 +08:00
|
|
|
|
|
|
|
while (prsv) {
|
|
|
|
struct linux_efi_memreserve *rsv;
|
2018-11-30 01:12:28 +08:00
|
|
|
u8 *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Just map a full page: that is what we will get
|
|
|
|
* anyway, and it permits us to map the entire entry
|
|
|
|
* before knowing its size.
|
|
|
|
*/
|
|
|
|
p = early_memremap(ALIGN_DOWN(prsv, PAGE_SIZE),
|
|
|
|
PAGE_SIZE);
|
|
|
|
if (p == NULL) {
|
2018-09-22 00:32:44 +08:00
|
|
|
pr_err("Could not map UEFI memreserve entry!\n");
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2018-11-30 01:12:28 +08:00
|
|
|
rsv = (void *)(p + prsv % PAGE_SIZE);
|
|
|
|
|
|
|
|
/* reserve the entry itself */
|
efi: Replace zero-length array and use struct_size() helper
The current codebase makes use of the zero-length array language
extension to the C90 standard, but the preferred mechanism to declare
variable-length types such as these ones is a flexible array member[1][2],
introduced in C99:
struct foo {
int stuff;
struct boo array[];
};
By making use of the mechanism above, we will get a compiler warning
in case the flexible array does not occur last in the structure, which
will help us prevent some kind of undefined behavior bugs from being
inadvertently introduced[3] to the codebase from now on.
Also, notice that, dynamic memory allocations won't be affected by
this change:
"Flexible array members have incomplete type, and so the sizeof operator
may not be applied. As a quirk of the original implementation of
zero-length arrays, sizeof evaluates to zero."[1]
sizeof(flexible-array-member) triggers a warning because flexible array
members have incomplete type[1]. There are some instances of code in
which the sizeof operator is being incorrectly/erroneously applied to
zero-length arrays and the result is zero. Such instances may be hiding
some bugs. So, this work (flexible-array member conversions) will also
help to get completely rid of those sorts of issues.
Lastly, make use of the sizeof_field() helper instead of an open-coded
version.
This issue was found with the help of Coccinelle and audited _manually_.
[1] https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
[2] https://github.com/KSPP/linux/issues/21
[3] commit 76497732932f ("cxgb3/l2t: Fix undefined behaviour")
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/20200527171425.GA4053@embeddedor
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
2020-05-28 01:14:25 +08:00
|
|
|
memblock_reserve(prsv,
|
|
|
|
struct_size(rsv, entry, rsv->size));
|
2018-11-30 01:12:28 +08:00
|
|
|
|
|
|
|
for (i = 0; i < atomic_read(&rsv->count); i++) {
|
|
|
|
memblock_reserve(rsv->entry[i].base,
|
|
|
|
rsv->entry[i].size);
|
|
|
|
}
|
2018-09-22 00:32:44 +08:00
|
|
|
|
|
|
|
prsv = rsv->next;
|
2018-11-30 01:12:28 +08:00
|
|
|
early_memunmap(p, PAGE_SIZE);
|
2018-09-22 00:32:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 20:10:25 +08:00
|
|
|
if (rt_prop != EFI_INVALID_TABLE_ADDR) {
|
|
|
|
efi_rt_properties_table_t *tbl;
|
|
|
|
|
|
|
|
tbl = early_memremap(rt_prop, sizeof(*tbl));
|
|
|
|
if (tbl) {
|
|
|
|
efi.runtime_supported_mask &= tbl->runtime_services_supported;
|
|
|
|
early_memunmap(tbl, sizeof(*tbl));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-16 20:03:06 +08:00
|
|
|
if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) &&
|
|
|
|
initrd != EFI_INVALID_TABLE_ADDR && phys_initrd_size == 0) {
|
|
|
|
struct linux_efi_initrd *tbl;
|
|
|
|
|
|
|
|
tbl = early_memremap(initrd, sizeof(*tbl));
|
|
|
|
if (tbl) {
|
|
|
|
phys_initrd_start = tbl->base;
|
|
|
|
phys_initrd_size = tbl->size;
|
|
|
|
early_memunmap(tbl, sizeof(*tbl));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-06 22:26:33 +08:00
|
|
|
if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) &&
|
|
|
|
efi.unaccepted != EFI_INVALID_TABLE_ADDR) {
|
|
|
|
struct efi_unaccepted_memory *unaccepted;
|
|
|
|
|
|
|
|
unaccepted = early_memremap(efi.unaccepted, sizeof(*unaccepted));
|
|
|
|
if (unaccepted) {
|
|
|
|
|
|
|
|
if (unaccepted->version == 1) {
|
2023-08-15 00:12:47 +08:00
|
|
|
reserve_unaccepted(unaccepted);
|
2023-06-06 22:26:33 +08:00
|
|
|
} else {
|
|
|
|
efi.unaccepted = EFI_INVALID_TABLE_ADDR;
|
|
|
|
}
|
|
|
|
|
|
|
|
early_memunmap(unaccepted, sizeof(*unaccepted));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 18:34:54 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-12-31 01:12:12 +08:00
|
|
|
|
2023-02-04 00:39:38 +08:00
|
|
|
int __init efi_systab_check_header(const efi_table_hdr_t *systab_hdr)
|
2020-01-20 17:49:11 +08:00
|
|
|
{
|
|
|
|
if (systab_hdr->signature != EFI_SYSTEM_TABLE_SIGNATURE) {
|
|
|
|
pr_err("System table signature incorrect!\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const efi_char16_t *__init map_fw_vendor(unsigned long fw_vendor,
|
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
const efi_char16_t *ret;
|
|
|
|
|
|
|
|
ret = early_memremap_ro(fw_vendor, size);
|
|
|
|
if (!ret)
|
|
|
|
pr_err("Could not map the firmware vendor!\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init unmap_fw_vendor(const void *fw_vendor, size_t size)
|
|
|
|
{
|
|
|
|
early_memunmap((void *)fw_vendor, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __init efi_systab_report_header(const efi_table_hdr_t *systab_hdr,
|
|
|
|
unsigned long fw_vendor)
|
|
|
|
{
|
|
|
|
char vendor[100] = "unknown";
|
|
|
|
const efi_char16_t *c16;
|
|
|
|
size_t i;
|
2023-01-31 02:50:55 +08:00
|
|
|
u16 rev;
|
2020-01-20 17:49:11 +08:00
|
|
|
|
|
|
|
c16 = map_fw_vendor(fw_vendor, sizeof(vendor) * sizeof(efi_char16_t));
|
|
|
|
if (c16) {
|
|
|
|
for (i = 0; i < sizeof(vendor) - 1 && c16[i]; ++i)
|
|
|
|
vendor[i] = c16[i];
|
|
|
|
vendor[i] = '\0';
|
|
|
|
|
|
|
|
unmap_fw_vendor(c16, sizeof(vendor) * sizeof(efi_char16_t));
|
|
|
|
}
|
|
|
|
|
2023-01-31 02:50:55 +08:00
|
|
|
rev = (u16)systab_hdr->revision;
|
|
|
|
pr_info("EFI v%u.%u", systab_hdr->revision >> 16, rev / 10);
|
|
|
|
|
|
|
|
rev %= 10;
|
|
|
|
if (rev)
|
|
|
|
pr_cont(".%u", rev);
|
|
|
|
|
|
|
|
pr_cont(" by %s\n", vendor);
|
efi: runtime: avoid EFIv2 runtime services on Apple x86 machines
Aditya reports [0] that his recent MacbookPro crashes in the firmware
when using the variable services at runtime. The culprit appears to be a
call to QueryVariableInfo(), which we did not use to call on Apple x86
machines in the past as they only upgraded from EFI v1.10 to EFI v2.40
firmware fairly recently, and QueryVariableInfo() (along with
UpdateCapsule() et al) was added in EFI v2.00.
The only runtime service introduced in EFI v2.00 that we actually use in
Linux is QueryVariableInfo(), as the capsule based ones are optional,
generally not used at runtime (all the LVFS/fwupd firmware update
infrastructure uses helper EFI programs that invoke capsule update at
boot time, not runtime), and not implemented by Apple machines in the
first place. QueryVariableInfo() is used to 'safely' set variables,
i.e., only when there is enough space. This prevents machines with buggy
firmwares from corrupting their NVRAMs when they run out of space.
Given that Apple machines have been using EFI v1.10 services only for
the longest time (the EFI v2.0 spec was released in 2006, and Linux
support for the newly introduced runtime services was added in 2011, but
the MacbookPro12,1 released in 2015 still claims to be EFI v1.10 only),
let's avoid the EFI v2.0 ones on all Apple x86 machines.
[0] https://lore.kernel.org/all/6D757C75-65B1-468B-842D-10410081A8E4@live.com/
Cc: <stable@vger.kernel.org>
Cc: Jeremy Kerr <jk@ozlabs.org>
Cc: Matthew Garrett <mjg59@srcf.ucam.org>
Reported-by: Aditya Garg <gargaditya08@live.com>
Tested-by: Orlando Chamberlain <redecorating@protonmail.com>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Tested-by: Aditya Garg <gargaditya08@live.com>
Link: https://bugzilla.kernel.org/show_bug.cgi?id=215277
2022-01-12 18:14:13 +08:00
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_X86_64) &&
|
|
|
|
systab_hdr->revision > EFI_1_10_SYSTEM_TABLE_REVISION &&
|
|
|
|
!strcmp(vendor, "Apple")) {
|
|
|
|
pr_info("Apple Mac detected, using EFI v1.10 runtime services only\n");
|
|
|
|
efi.runtime_version = EFI_1_10_SYSTEM_TABLE_REVISION;
|
|
|
|
}
|
2020-01-20 17:49:11 +08:00
|
|
|
}
|
|
|
|
|
2020-09-24 19:52:24 +08:00
|
|
|
static __initdata char memory_type_name[][13] = {
|
2014-09-03 19:32:20 +08:00
|
|
|
"Reserved",
|
|
|
|
"Loader Code",
|
|
|
|
"Loader Data",
|
|
|
|
"Boot Code",
|
|
|
|
"Boot Data",
|
|
|
|
"Runtime Code",
|
|
|
|
"Runtime Data",
|
2020-09-24 19:52:24 +08:00
|
|
|
"Conventional",
|
|
|
|
"Unusable",
|
|
|
|
"ACPI Reclaim",
|
|
|
|
"ACPI Mem NVS",
|
|
|
|
"MMIO",
|
|
|
|
"MMIO Port",
|
2016-02-02 06:07:07 +08:00
|
|
|
"PAL Code",
|
2020-09-24 19:52:24 +08:00
|
|
|
"Persistent",
|
2023-06-06 22:26:31 +08:00
|
|
|
"Unaccepted",
|
2014-09-03 19:32:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
char * __init efi_md_typeattr_format(char *buf, size_t size,
|
|
|
|
const efi_memory_desc_t *md)
|
|
|
|
{
|
|
|
|
char *pos;
|
|
|
|
int type_len;
|
|
|
|
u64 attr;
|
|
|
|
|
|
|
|
pos = buf;
|
|
|
|
if (md->type >= ARRAY_SIZE(memory_type_name))
|
|
|
|
type_len = snprintf(pos, size, "[type=%u", md->type);
|
|
|
|
else
|
|
|
|
type_len = snprintf(pos, size, "[%-*s",
|
|
|
|
(int)(sizeof(memory_type_name[0]) - 1),
|
|
|
|
memory_type_name[md->type]);
|
|
|
|
if (type_len >= size)
|
|
|
|
return buf;
|
|
|
|
|
|
|
|
pos += type_len;
|
|
|
|
size -= type_len;
|
|
|
|
|
|
|
|
attr = md->attribute;
|
|
|
|
if (attr & ~(EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT |
|
2015-08-07 16:36:54 +08:00
|
|
|
EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_RO |
|
|
|
|
EFI_MEMORY_WP | EFI_MEMORY_RP | EFI_MEMORY_XP |
|
2020-09-24 19:52:24 +08:00
|
|
|
EFI_MEMORY_NV | EFI_MEMORY_SP | EFI_MEMORY_CPU_CRYPTO |
|
2015-08-27 01:11:19 +08:00
|
|
|
EFI_MEMORY_RUNTIME | EFI_MEMORY_MORE_RELIABLE))
|
2014-09-03 19:32:20 +08:00
|
|
|
snprintf(pos, size, "|attr=0x%016llx]",
|
|
|
|
(unsigned long long)attr);
|
|
|
|
else
|
2016-02-02 06:07:06 +08:00
|
|
|
snprintf(pos, size,
|
2020-09-24 19:52:24 +08:00
|
|
|
"|%3s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%2s|%3s|%2s|%2s|%2s|%2s]",
|
|
|
|
attr & EFI_MEMORY_RUNTIME ? "RUN" : "",
|
|
|
|
attr & EFI_MEMORY_MORE_RELIABLE ? "MR" : "",
|
|
|
|
attr & EFI_MEMORY_CPU_CRYPTO ? "CC" : "",
|
|
|
|
attr & EFI_MEMORY_SP ? "SP" : "",
|
|
|
|
attr & EFI_MEMORY_NV ? "NV" : "",
|
|
|
|
attr & EFI_MEMORY_XP ? "XP" : "",
|
|
|
|
attr & EFI_MEMORY_RP ? "RP" : "",
|
|
|
|
attr & EFI_MEMORY_WP ? "WP" : "",
|
|
|
|
attr & EFI_MEMORY_RO ? "RO" : "",
|
|
|
|
attr & EFI_MEMORY_UCE ? "UCE" : "",
|
|
|
|
attr & EFI_MEMORY_WB ? "WB" : "",
|
|
|
|
attr & EFI_MEMORY_WT ? "WT" : "",
|
|
|
|
attr & EFI_MEMORY_WC ? "WC" : "",
|
|
|
|
attr & EFI_MEMORY_UC ? "UC" : "");
|
2014-09-03 19:32:20 +08:00
|
|
|
return buf;
|
|
|
|
}
|
2015-08-07 16:36:57 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* efi_mem_attributes - lookup memmap attributes for physical address
|
|
|
|
* @phys_addr: the physical address to lookup
|
|
|
|
*
|
|
|
|
* Search in the EFI memory map for the region covering
|
|
|
|
* @phys_addr. Returns the EFI memory attributes if the region
|
|
|
|
* was found in the memory map, 0 otherwise.
|
|
|
|
*/
|
2017-08-25 23:50:18 +08:00
|
|
|
u64 efi_mem_attributes(unsigned long phys_addr)
|
2015-08-07 16:36:57 +08:00
|
|
|
{
|
|
|
|
efi_memory_desc_t *md;
|
|
|
|
|
|
|
|
if (!efi_enabled(EFI_MEMMAP))
|
|
|
|
return 0;
|
|
|
|
|
2016-04-26 04:06:38 +08:00
|
|
|
for_each_efi_memory_desc(md) {
|
2015-08-07 16:36:57 +08:00
|
|
|
if ((md->phys_addr <= phys_addr) &&
|
|
|
|
(phys_addr < (md->phys_addr +
|
|
|
|
(md->num_pages << EFI_PAGE_SHIFT))))
|
|
|
|
return md->attribute;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-26 04:06:58 +08:00
|
|
|
|
2017-08-25 23:50:18 +08:00
|
|
|
/*
|
|
|
|
* efi_mem_type - lookup memmap type for physical address
|
|
|
|
* @phys_addr: the physical address to lookup
|
|
|
|
*
|
|
|
|
* Search in the EFI memory map for the region covering @phys_addr.
|
|
|
|
* Returns the EFI memory type if the region was found in the memory
|
2020-01-14 01:22:41 +08:00
|
|
|
* map, -EINVAL otherwise.
|
2017-08-25 23:50:18 +08:00
|
|
|
*/
|
|
|
|
int efi_mem_type(unsigned long phys_addr)
|
|
|
|
{
|
|
|
|
const efi_memory_desc_t *md;
|
|
|
|
|
|
|
|
if (!efi_enabled(EFI_MEMMAP))
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
|
|
|
for_each_efi_memory_desc(md) {
|
|
|
|
if ((md->phys_addr <= phys_addr) &&
|
|
|
|
(phys_addr < (md->phys_addr +
|
|
|
|
(md->num_pages << EFI_PAGE_SHIFT))))
|
|
|
|
return md->type;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2016-04-26 04:06:58 +08:00
|
|
|
int efi_status_to_err(efi_status_t status)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
switch (status) {
|
|
|
|
case EFI_SUCCESS:
|
|
|
|
err = 0;
|
|
|
|
break;
|
|
|
|
case EFI_INVALID_PARAMETER:
|
|
|
|
err = -EINVAL;
|
|
|
|
break;
|
|
|
|
case EFI_OUT_OF_RESOURCES:
|
|
|
|
err = -ENOSPC;
|
|
|
|
break;
|
|
|
|
case EFI_DEVICE_ERROR:
|
|
|
|
err = -EIO;
|
|
|
|
break;
|
|
|
|
case EFI_WRITE_PROTECTED:
|
|
|
|
err = -EROFS;
|
|
|
|
break;
|
|
|
|
case EFI_SECURITY_VIOLATION:
|
|
|
|
err = -EACCES;
|
|
|
|
break;
|
|
|
|
case EFI_NOT_FOUND:
|
|
|
|
err = -ENOENT;
|
|
|
|
break;
|
2016-07-16 03:36:31 +08:00
|
|
|
case EFI_ABORTED:
|
|
|
|
err = -EINTR;
|
|
|
|
break;
|
2016-04-26 04:06:58 +08:00
|
|
|
default:
|
|
|
|
err = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2017-07-18 05:10:13 +08:00
|
|
|
}
|
2022-06-21 00:19:43 +08:00
|
|
|
EXPORT_SYMBOL_GPL(efi_status_to_err);
|
2017-07-18 05:10:13 +08:00
|
|
|
|
2018-09-22 00:32:46 +08:00
|
|
|
static DEFINE_SPINLOCK(efi_mem_reserve_persistent_lock);
|
2018-11-15 01:55:44 +08:00
|
|
|
static struct linux_efi_memreserve *efi_memreserve_root __ro_after_init;
|
2018-09-22 00:32:46 +08:00
|
|
|
|
2018-11-24 05:51:32 +08:00
|
|
|
static int __init efi_memreserve_map_root(void)
|
|
|
|
{
|
2020-01-22 22:06:54 +08:00
|
|
|
if (mem_reserve == EFI_INVALID_TABLE_ADDR)
|
2018-11-24 05:51:32 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
2020-01-22 22:06:54 +08:00
|
|
|
efi_memreserve_root = memremap(mem_reserve,
|
2018-11-24 05:51:32 +08:00
|
|
|
sizeof(*efi_memreserve_root),
|
|
|
|
MEMREMAP_WB);
|
|
|
|
if (WARN_ON_ONCE(!efi_memreserve_root))
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-07 00:55:37 +08:00
|
|
|
static int efi_mem_reserve_iomem(phys_addr_t addr, u64 size)
|
|
|
|
{
|
|
|
|
struct resource *res, *parent;
|
2021-07-14 02:43:26 +08:00
|
|
|
int ret;
|
2019-12-07 00:55:37 +08:00
|
|
|
|
|
|
|
res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
|
|
|
|
if (!res)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
res->name = "reserved";
|
|
|
|
res->flags = IORESOURCE_MEM;
|
|
|
|
res->start = addr;
|
|
|
|
res->end = addr + size - 1;
|
|
|
|
|
|
|
|
/* we expect a conflict with a 'System RAM' region */
|
|
|
|
parent = request_resource_conflict(&iomem_resource, res);
|
2021-07-14 02:43:26 +08:00
|
|
|
ret = parent ? request_resource(parent, res) : 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given that efi_mem_reserve_iomem() can be called at any
|
|
|
|
* time, only call memblock_reserve() if the architecture
|
|
|
|
* keeps the infrastructure around.
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK) && !ret)
|
|
|
|
memblock_reserve(addr, size);
|
|
|
|
|
|
|
|
return ret;
|
2019-12-07 00:55:37 +08:00
|
|
|
}
|
|
|
|
|
2018-11-24 05:51:32 +08:00
|
|
|
int __ref efi_mem_reserve_persistent(phys_addr_t addr, u64 size)
|
2018-09-22 00:32:46 +08:00
|
|
|
{
|
2018-11-15 01:55:44 +08:00
|
|
|
struct linux_efi_memreserve *rsv;
|
2018-11-30 01:12:29 +08:00
|
|
|
unsigned long prsv;
|
|
|
|
int rc, index;
|
2018-09-22 00:32:46 +08:00
|
|
|
|
2018-11-24 05:51:32 +08:00
|
|
|
if (efi_memreserve_root == (void *)ULONG_MAX)
|
2018-09-22 00:32:46 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
2018-11-24 05:51:32 +08:00
|
|
|
if (!efi_memreserve_root) {
|
|
|
|
rc = efi_memreserve_map_root();
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-11-30 01:12:29 +08:00
|
|
|
/* first try to find a slot in an existing linked list entry */
|
2021-03-10 16:31:27 +08:00
|
|
|
for (prsv = efi_memreserve_root->next; prsv; ) {
|
2019-06-10 02:17:44 +08:00
|
|
|
rsv = memremap(prsv, sizeof(*rsv), MEMREMAP_WB);
|
2023-02-03 21:22:13 +08:00
|
|
|
if (!rsv)
|
|
|
|
return -ENOMEM;
|
2018-11-30 01:12:29 +08:00
|
|
|
index = atomic_fetch_add_unless(&rsv->count, 1, rsv->size);
|
|
|
|
if (index < rsv->size) {
|
|
|
|
rsv->entry[index].base = addr;
|
|
|
|
rsv->entry[index].size = size;
|
|
|
|
|
2019-06-10 02:17:44 +08:00
|
|
|
memunmap(rsv);
|
2019-12-07 00:55:37 +08:00
|
|
|
return efi_mem_reserve_iomem(addr, size);
|
2018-11-30 01:12:29 +08:00
|
|
|
}
|
2021-03-10 16:31:27 +08:00
|
|
|
prsv = rsv->next;
|
2019-06-10 02:17:44 +08:00
|
|
|
memunmap(rsv);
|
2018-11-30 01:12:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* no slot found - allocate a new linked list entry */
|
|
|
|
rsv = (struct linux_efi_memreserve *)__get_free_page(GFP_ATOMIC);
|
2018-09-22 00:32:46 +08:00
|
|
|
if (!rsv)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2019-12-07 00:55:37 +08:00
|
|
|
rc = efi_mem_reserve_iomem(__pa(rsv), SZ_4K);
|
|
|
|
if (rc) {
|
|
|
|
free_page((unsigned long)rsv);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2019-06-10 02:17:44 +08:00
|
|
|
/*
|
|
|
|
* The memremap() call above assumes that a linux_efi_memreserve entry
|
|
|
|
* never crosses a page boundary, so let's ensure that this remains true
|
|
|
|
* even when kexec'ing a 4k pages kernel from a >4k pages kernel, by
|
|
|
|
* using SZ_4K explicitly in the size calculation below.
|
|
|
|
*/
|
|
|
|
rsv->size = EFI_MEMRESERVE_COUNT(SZ_4K);
|
2018-11-30 01:12:28 +08:00
|
|
|
atomic_set(&rsv->count, 1);
|
|
|
|
rsv->entry[0].base = addr;
|
|
|
|
rsv->entry[0].size = size;
|
2018-09-22 00:32:46 +08:00
|
|
|
|
|
|
|
spin_lock(&efi_mem_reserve_persistent_lock);
|
2018-11-15 01:55:44 +08:00
|
|
|
rsv->next = efi_memreserve_root->next;
|
|
|
|
efi_memreserve_root->next = __pa(rsv);
|
2018-09-22 00:32:46 +08:00
|
|
|
spin_unlock(&efi_mem_reserve_persistent_lock);
|
|
|
|
|
2019-12-07 00:55:37 +08:00
|
|
|
return efi_mem_reserve_iomem(addr, size);
|
2018-11-15 01:55:44 +08:00
|
|
|
}
|
2018-09-22 00:32:46 +08:00
|
|
|
|
2018-11-15 01:55:44 +08:00
|
|
|
static int __init efi_memreserve_root_init(void)
|
|
|
|
{
|
2018-11-24 05:51:32 +08:00
|
|
|
if (efi_memreserve_root)
|
|
|
|
return 0;
|
|
|
|
if (efi_memreserve_map_root())
|
|
|
|
efi_memreserve_root = (void *)ULONG_MAX;
|
2018-09-22 00:32:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-11-15 01:55:44 +08:00
|
|
|
early_initcall(efi_memreserve_root_init);
|
2018-09-22 00:32:46 +08:00
|
|
|
|
2016-11-13 05:32:31 +08:00
|
|
|
#ifdef CONFIG_KEXEC
|
|
|
|
static int update_efi_random_seed(struct notifier_block *nb,
|
|
|
|
unsigned long code, void *unused)
|
|
|
|
{
|
|
|
|
struct linux_efi_random_seed *seed;
|
|
|
|
u32 size = 0;
|
|
|
|
|
|
|
|
if (!kexec_in_progress)
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
|
2020-02-28 20:14:04 +08:00
|
|
|
seed = memremap(efi_rng_seed, sizeof(*seed), MEMREMAP_WB);
|
2016-11-13 05:32:31 +08:00
|
|
|
if (seed != NULL) {
|
2017-08-25 23:50:16 +08:00
|
|
|
size = min(seed->size, EFI_RANDOM_SEED_SIZE);
|
2016-11-13 05:32:31 +08:00
|
|
|
memunmap(seed);
|
|
|
|
} else {
|
|
|
|
pr_err("Could not map UEFI random seed!\n");
|
|
|
|
}
|
|
|
|
if (size > 0) {
|
2020-02-28 20:14:04 +08:00
|
|
|
seed = memremap(efi_rng_seed, sizeof(*seed) + size,
|
|
|
|
MEMREMAP_WB);
|
2016-11-13 05:32:31 +08:00
|
|
|
if (seed != NULL) {
|
|
|
|
seed->size = size;
|
|
|
|
get_random_bytes(seed->bits, seed->size);
|
|
|
|
memunmap(seed);
|
|
|
|
} else {
|
|
|
|
pr_err("Could not map UEFI random seed!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block efi_random_seed_nb = {
|
|
|
|
.notifier_call = update_efi_random_seed,
|
|
|
|
};
|
|
|
|
|
2020-01-22 21:58:15 +08:00
|
|
|
static int __init register_update_efi_random_seed(void)
|
2016-11-13 05:32:31 +08:00
|
|
|
{
|
2020-02-28 20:14:04 +08:00
|
|
|
if (efi_rng_seed == EFI_INVALID_TABLE_ADDR)
|
2016-11-13 05:32:31 +08:00
|
|
|
return 0;
|
|
|
|
return register_reboot_notifier(&efi_random_seed_nb);
|
|
|
|
}
|
|
|
|
late_initcall(register_update_efi_random_seed);
|
|
|
|
#endif
|