xref: /openbmc/linux/arch/x86/xen/efi.c (revision 8bd1369b)
1 /*
2  * Copyright (c) 2014 Oracle Co., Daniel Kiper
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <linux/bitops.h>
19 #include <linux/efi.h>
20 #include <linux/init.h>
21 #include <linux/string.h>
22 
23 #include <xen/xen.h>
24 #include <xen/xen-ops.h>
25 #include <xen/interface/platform.h>
26 
27 #include <asm/page.h>
28 #include <asm/setup.h>
29 #include <asm/xen/hypercall.h>
30 
31 static efi_char16_t vendor[100] __initdata;
32 
33 static efi_system_table_t efi_systab_xen __initdata = {
34 	.hdr = {
35 		.signature	= EFI_SYSTEM_TABLE_SIGNATURE,
36 		.revision	= 0, /* Initialized later. */
37 		.headersize	= 0, /* Ignored by Linux Kernel. */
38 		.crc32		= 0, /* Ignored by Linux Kernel. */
39 		.reserved	= 0
40 	},
41 	.fw_vendor	= EFI_INVALID_TABLE_ADDR, /* Initialized later. */
42 	.fw_revision	= 0,			  /* Initialized later. */
43 	.con_in_handle	= EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
44 	.con_in		= EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
45 	.con_out_handle	= EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
46 	.con_out	= EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
47 	.stderr_handle	= EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
48 	.stderr		= EFI_INVALID_TABLE_ADDR, /* Not used under Xen. */
49 	.runtime	= (efi_runtime_services_t *)EFI_INVALID_TABLE_ADDR,
50 						  /* Not used under Xen. */
51 	.boottime	= (efi_boot_services_t *)EFI_INVALID_TABLE_ADDR,
52 						  /* Not used under Xen. */
53 	.nr_tables	= 0,			  /* Initialized later. */
54 	.tables		= EFI_INVALID_TABLE_ADDR  /* Initialized later. */
55 };
56 
57 static efi_system_table_t __init *xen_efi_probe(void)
58 {
59 	struct xen_platform_op op = {
60 		.cmd = XENPF_firmware_info,
61 		.u.firmware_info = {
62 			.type = XEN_FW_EFI_INFO,
63 			.index = XEN_FW_EFI_CONFIG_TABLE
64 		}
65 	};
66 	union xenpf_efi_info *info = &op.u.firmware_info.u.efi_info;
67 
68 	if (!xen_initial_domain() || HYPERVISOR_platform_op(&op) < 0)
69 		return NULL;
70 
71 	/* Here we know that Xen runs on EFI platform. */
72 
73 	efi.get_time                 = xen_efi_get_time;
74 	efi.set_time                 = xen_efi_set_time;
75 	efi.get_wakeup_time          = xen_efi_get_wakeup_time;
76 	efi.set_wakeup_time          = xen_efi_set_wakeup_time;
77 	efi.get_variable             = xen_efi_get_variable;
78 	efi.get_next_variable        = xen_efi_get_next_variable;
79 	efi.set_variable             = xen_efi_set_variable;
80 	efi.query_variable_info      = xen_efi_query_variable_info;
81 	efi.update_capsule           = xen_efi_update_capsule;
82 	efi.query_capsule_caps       = xen_efi_query_capsule_caps;
83 	efi.get_next_high_mono_count = xen_efi_get_next_high_mono_count;
84 	efi.reset_system             = xen_efi_reset_system;
85 
86 	efi_systab_xen.tables = info->cfg.addr;
87 	efi_systab_xen.nr_tables = info->cfg.nent;
88 
89 	op.cmd = XENPF_firmware_info;
90 	op.u.firmware_info.type = XEN_FW_EFI_INFO;
91 	op.u.firmware_info.index = XEN_FW_EFI_VENDOR;
92 	info->vendor.bufsz = sizeof(vendor);
93 	set_xen_guest_handle(info->vendor.name, vendor);
94 
95 	if (HYPERVISOR_platform_op(&op) == 0) {
96 		efi_systab_xen.fw_vendor = __pa_symbol(vendor);
97 		efi_systab_xen.fw_revision = info->vendor.revision;
98 	} else
99 		efi_systab_xen.fw_vendor = __pa_symbol(L"UNKNOWN");
100 
101 	op.cmd = XENPF_firmware_info;
102 	op.u.firmware_info.type = XEN_FW_EFI_INFO;
103 	op.u.firmware_info.index = XEN_FW_EFI_VERSION;
104 
105 	if (HYPERVISOR_platform_op(&op) == 0)
106 		efi_systab_xen.hdr.revision = info->version;
107 
108 	op.cmd = XENPF_firmware_info;
109 	op.u.firmware_info.type = XEN_FW_EFI_INFO;
110 	op.u.firmware_info.index = XEN_FW_EFI_RT_VERSION;
111 
112 	if (HYPERVISOR_platform_op(&op) == 0)
113 		efi.runtime_version = info->version;
114 
115 	return &efi_systab_xen;
116 }
117 
118 /*
119  * Determine whether we're in secure boot mode.
120  *
121  * Please keep the logic in sync with
122  * drivers/firmware/efi/libstub/secureboot.c:efi_get_secureboot().
123  */
124 static enum efi_secureboot_mode xen_efi_get_secureboot(void)
125 {
126 	static efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
127 	static efi_guid_t shim_guid = EFI_SHIM_LOCK_GUID;
128 	efi_status_t status;
129 	u8 moksbstate, secboot, setupmode;
130 	unsigned long size;
131 
132 	size = sizeof(secboot);
133 	status = efi.get_variable(L"SecureBoot", &efi_variable_guid,
134 				  NULL, &size, &secboot);
135 
136 	if (status == EFI_NOT_FOUND)
137 		return efi_secureboot_mode_disabled;
138 
139 	if (status != EFI_SUCCESS)
140 		goto out_efi_err;
141 
142 	size = sizeof(setupmode);
143 	status = efi.get_variable(L"SetupMode", &efi_variable_guid,
144 				  NULL, &size, &setupmode);
145 
146 	if (status != EFI_SUCCESS)
147 		goto out_efi_err;
148 
149 	if (secboot == 0 || setupmode == 1)
150 		return efi_secureboot_mode_disabled;
151 
152 	/* See if a user has put the shim into insecure mode. */
153 	size = sizeof(moksbstate);
154 	status = efi.get_variable(L"MokSBStateRT", &shim_guid,
155 				  NULL, &size, &moksbstate);
156 
157 	/* If it fails, we don't care why. Default to secure. */
158 	if (status != EFI_SUCCESS)
159 		goto secure_boot_enabled;
160 
161 	if (moksbstate == 1)
162 		return efi_secureboot_mode_disabled;
163 
164  secure_boot_enabled:
165 	pr_info("UEFI Secure Boot is enabled.\n");
166 	return efi_secureboot_mode_enabled;
167 
168  out_efi_err:
169 	pr_err("Could not determine UEFI Secure Boot status.\n");
170 	return efi_secureboot_mode_unknown;
171 }
172 
173 void __init xen_efi_init(void)
174 {
175 	efi_system_table_t *efi_systab_xen;
176 
177 	efi_systab_xen = xen_efi_probe();
178 
179 	if (efi_systab_xen == NULL)
180 		return;
181 
182 	strncpy((char *)&boot_params.efi_info.efi_loader_signature, "Xen",
183 			sizeof(boot_params.efi_info.efi_loader_signature));
184 	boot_params.efi_info.efi_systab = (__u32)__pa(efi_systab_xen);
185 	boot_params.efi_info.efi_systab_hi = (__u32)(__pa(efi_systab_xen) >> 32);
186 
187 	boot_params.secure_boot = xen_efi_get_secureboot();
188 
189 	set_bit(EFI_BOOT, &efi.flags);
190 	set_bit(EFI_PARAVIRT, &efi.flags);
191 	set_bit(EFI_64BIT, &efi.flags);
192 }
193