xref: /linux/arch/x86/xen/efi.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1901d209aSJuergen Gross // SPDX-License-Identifier: GPL-2.0
2c7341d6aSDaniel Kiper /*
3c7341d6aSDaniel Kiper  * Copyright (c) 2014 Oracle Co., Daniel Kiper
4c7341d6aSDaniel Kiper  */
5c7341d6aSDaniel Kiper 
6342cd340SDaniel Kiper #include <linux/bitops.h>
7c7341d6aSDaniel Kiper #include <linux/efi.h>
8c7341d6aSDaniel Kiper #include <linux/init.h>
9c7341d6aSDaniel Kiper #include <linux/string.h>
10c7341d6aSDaniel Kiper 
11a62ed500SShannon Zhao #include <xen/xen.h>
12c7341d6aSDaniel Kiper #include <xen/xen-ops.h>
13a62ed500SShannon Zhao #include <xen/interface/platform.h>
14c7341d6aSDaniel Kiper 
15342cd340SDaniel Kiper #include <asm/page.h>
16c7341d6aSDaniel Kiper #include <asm/setup.h>
17a62ed500SShannon Zhao #include <asm/xen/hypercall.h>
18a62ed500SShannon Zhao 
19fb9b7b4bSArnd Bergmann #include "xen-ops.h"
20fb9b7b4bSArnd Bergmann 
21a62ed500SShannon Zhao static efi_char16_t vendor[100] __initdata;
22a62ed500SShannon Zhao 
23a62ed500SShannon Zhao static efi_system_table_t efi_systab_xen __initdata = {
24a62ed500SShannon Zhao 	.hdr = {
25a62ed500SShannon Zhao 		.signature	= EFI_SYSTEM_TABLE_SIGNATURE,
26a62ed500SShannon Zhao 		.revision	= 0, /* Initialized later. */
27a62ed500SShannon Zhao 		.headersize	= 0, /* Ignored by Linux Kernel. */
28a62ed500SShannon Zhao 		.crc32		= 0, /* Ignored by Linux Kernel. */
29a62ed500SShannon Zhao 		.reserved	= 0
30a62ed500SShannon Zhao 	},
31a62ed500SShannon Zhao 	.fw_vendor	= EFI_INVALID_TABLE_ADDR, /* Initialized later. */
32a62ed500SShannon Zhao 	.fw_revision	= 0,			  /* Initialized later. */
33a62ed500SShannon Zhao 	.con_in_handle	= EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
349b47c527SArvind Sankar 	.con_in		= NULL,			  /* Not used under Xen. */
35a62ed500SShannon Zhao 	.con_out_handle	= EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
36960a8d01SArd Biesheuvel 	.con_out	= NULL, 		  /* Not used under Xen. */
37a62ed500SShannon Zhao 	.stderr_handle	= EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
38a62ed500SShannon Zhao 	.stderr		= EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
39a62ed500SShannon Zhao 	.runtime	= (efi_runtime_services_t *)EFI_INVALID_TABLE_ADDR,
40a62ed500SShannon Zhao 						  /* Not used under Xen. */
41a62ed500SShannon Zhao 	.boottime	= (efi_boot_services_t *)EFI_INVALID_TABLE_ADDR,
42a62ed500SShannon Zhao 						  /* Not used under Xen. */
43a62ed500SShannon Zhao 	.nr_tables	= 0,			  /* Initialized later. */
44a62ed500SShannon Zhao 	.tables		= EFI_INVALID_TABLE_ADDR  /* Initialized later. */
45a62ed500SShannon Zhao };
46a62ed500SShannon Zhao 
xen_efi_probe(void)47a62ed500SShannon Zhao static efi_system_table_t __init *xen_efi_probe(void)
48a62ed500SShannon Zhao {
49a62ed500SShannon Zhao 	struct xen_platform_op op = {
50a62ed500SShannon Zhao 		.cmd = XENPF_firmware_info,
51a62ed500SShannon Zhao 		.u.firmware_info = {
52a62ed500SShannon Zhao 			.type = XEN_FW_EFI_INFO,
53a62ed500SShannon Zhao 			.index = XEN_FW_EFI_CONFIG_TABLE
54a62ed500SShannon Zhao 		}
55a62ed500SShannon Zhao 	};
56a62ed500SShannon Zhao 	union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
57a62ed500SShannon Zhao 
58a62ed500SShannon Zhao 	if (!xen_initial_domain() || HYPERVISOR_platform_op(&op) < 0)
59a62ed500SShannon Zhao 		return NULL;
60a62ed500SShannon Zhao 
61a62ed500SShannon Zhao 	/* Here we know that Xen runs on EFI platform. */
6209515706SJuergen Gross 	xen_efi_runtime_setup();
63a62ed500SShannon Zhao 
64a62ed500SShannon Zhao 	efi_systab_xen.tables = info->cfg.addr;
65a62ed500SShannon Zhao 	efi_systab_xen.nr_tables = info->cfg.nent;
66a62ed500SShannon Zhao 
67a62ed500SShannon Zhao 	op.cmd = XENPF_firmware_info;
68a62ed500SShannon Zhao 	op.u.firmware_info.type = XEN_FW_EFI_INFO;
69a62ed500SShannon Zhao 	op.u.firmware_info.index = XEN_FW_EFI_VENDOR;
70a62ed500SShannon Zhao 	info->vendor.bufsz = sizeof(vendor);
71a62ed500SShannon Zhao 	set_xen_guest_handle(info->vendor.name, vendor);
72a62ed500SShannon Zhao 
73a62ed500SShannon Zhao 	if (HYPERVISOR_platform_op(&op) == 0) {
74a62ed500SShannon Zhao 		efi_systab_xen.fw_vendor = __pa_symbol(vendor);
75a62ed500SShannon Zhao 		efi_systab_xen.fw_revision = info->vendor.revision;
76a62ed500SShannon Zhao 	} else
77a62ed500SShannon Zhao 		efi_systab_xen.fw_vendor = __pa_symbol(L"UNKNOWN");
78a62ed500SShannon Zhao 
79a62ed500SShannon Zhao 	op.cmd = XENPF_firmware_info;
80a62ed500SShannon Zhao 	op.u.firmware_info.type = XEN_FW_EFI_INFO;
81a62ed500SShannon Zhao 	op.u.firmware_info.index = XEN_FW_EFI_VERSION;
82a62ed500SShannon Zhao 
83a62ed500SShannon Zhao 	if (HYPERVISOR_platform_op(&op) == 0)
84a62ed500SShannon Zhao 		efi_systab_xen.hdr.revision = info->version;
85a62ed500SShannon Zhao 
86a62ed500SShannon Zhao 	op.cmd = XENPF_firmware_info;
87a62ed500SShannon Zhao 	op.u.firmware_info.type = XEN_FW_EFI_INFO;
88a62ed500SShannon Zhao 	op.u.firmware_info.index = XEN_FW_EFI_RT_VERSION;
89a62ed500SShannon Zhao 
90a62ed500SShannon Zhao 	if (HYPERVISOR_platform_op(&op) == 0)
91a62ed500SShannon Zhao 		efi.runtime_version = info->version;
92a62ed500SShannon Zhao 
93a62ed500SShannon Zhao 	return &efi_systab_xen;
94a62ed500SShannon Zhao }
95c7341d6aSDaniel Kiper 
96a7012bdbSDaniel Kiper /*
97a7012bdbSDaniel Kiper  * Determine whether we're in secure boot mode.
98a7012bdbSDaniel Kiper  */
xen_efi_get_secureboot(void)99a7012bdbSDaniel Kiper static enum efi_secureboot_mode xen_efi_get_secureboot(void)
100a7012bdbSDaniel Kiper {
101a7012bdbSDaniel Kiper 	static efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
102b283477dSArd Biesheuvel 	enum efi_secureboot_mode mode;
103a7012bdbSDaniel Kiper 	efi_status_t status;
104b283477dSArd Biesheuvel 	u8 moksbstate;
105a7012bdbSDaniel Kiper 	unsigned long size;
106a7012bdbSDaniel Kiper 
107b283477dSArd Biesheuvel 	mode = efi_get_secureboot_mode(efi.get_variable);
108b283477dSArd Biesheuvel 	if (mode == efi_secureboot_mode_unknown) {
109b283477dSArd Biesheuvel 		pr_err("Could not determine UEFI Secure Boot status.\n");
110b283477dSArd Biesheuvel 		return efi_secureboot_mode_unknown;
111b283477dSArd Biesheuvel 	}
112b283477dSArd Biesheuvel 	if (mode != efi_secureboot_mode_enabled)
113b283477dSArd Biesheuvel 		return mode;
114a7012bdbSDaniel Kiper 
115a7012bdbSDaniel Kiper 	/* See if a user has put the shim into insecure mode. */
116a7012bdbSDaniel Kiper 	size = sizeof(moksbstate);
117a7012bdbSDaniel Kiper 	status = efi.get_variable(L"MokSBStateRT", &shim_guid,
118a7012bdbSDaniel Kiper 				  NULL, &size, &moksbstate);
119a7012bdbSDaniel Kiper 
120a7012bdbSDaniel Kiper 	/* If it fails, we don't care why. Default to secure. */
121a7012bdbSDaniel Kiper 	if (status != EFI_SUCCESS)
122a7012bdbSDaniel Kiper 		goto secure_boot_enabled;
123a7012bdbSDaniel Kiper 
124a7012bdbSDaniel Kiper 	if (moksbstate == 1)
125a7012bdbSDaniel Kiper 		return efi_secureboot_mode_disabled;
126a7012bdbSDaniel Kiper 
127a7012bdbSDaniel Kiper  secure_boot_enabled:
128a7012bdbSDaniel Kiper 	pr_info("UEFI Secure Boot is enabled.\n");
129a7012bdbSDaniel Kiper 	return efi_secureboot_mode_enabled;
130a7012bdbSDaniel Kiper }
131a7012bdbSDaniel Kiper 
xen_efi_init(struct boot_params * boot_params)13272813bfbSRoger Pau Monne void __init xen_efi_init(struct boot_params *boot_params)
133c7341d6aSDaniel Kiper {
134c7341d6aSDaniel Kiper 	efi_system_table_t *efi_systab_xen;
135c7341d6aSDaniel Kiper 
136c7341d6aSDaniel Kiper 	efi_systab_xen = xen_efi_probe();
137c7341d6aSDaniel Kiper 
138c7341d6aSDaniel Kiper 	if (efi_systab_xen == NULL)
139c7341d6aSDaniel Kiper 		return;
140c7341d6aSDaniel Kiper 
141*0fc6ff5aSJustin Stitt 	strscpy((char *)&boot_params->efi_info.efi_loader_signature, "Xen",
14272813bfbSRoger Pau Monne 			sizeof(boot_params->efi_info.efi_loader_signature));
14372813bfbSRoger Pau Monne 	boot_params->efi_info.efi_systab = (__u32)__pa(efi_systab_xen);
14472813bfbSRoger Pau Monne 	boot_params->efi_info.efi_systab_hi = (__u32)(__pa(efi_systab_xen) >> 32);
145c7341d6aSDaniel Kiper 
14672813bfbSRoger Pau Monne 	boot_params->secure_boot = xen_efi_get_secureboot();
147a7012bdbSDaniel Kiper 
148c7341d6aSDaniel Kiper 	set_bit(EFI_BOOT, &efi.flags);
149c7341d6aSDaniel Kiper 	set_bit(EFI_PARAVIRT, &efi.flags);
150c7341d6aSDaniel Kiper 	set_bit(EFI_64BIT, &efi.flags);
151c7341d6aSDaniel Kiper }
152