blob: 49172e357989e06188234665ca3833d11b353a81 [file] [log] [blame]
AKASHI Takahiro056b45b2018-12-30 15:16:55 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * EFI setup code
4 *
5 * Copyright (c) 2016-2018 Alexander Graf et al.
6 */
7
Heinrich Schuchardt149108a2021-06-29 13:33:09 +02008#define LOG_CATEGORY LOGC_EFI
9
AKASHI Takahiro056b45b2018-12-30 15:16:55 +010010#include <common.h>
11#include <efi_loader.h>
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +020012#include <efi_variable.h>
Heinrich Schuchardt149108a2021-06-29 13:33:09 +020013#include <log.h>
AKASHI Takahiro056b45b2018-12-30 15:16:55 +010014
15#define OBJ_LIST_NOT_INITIALIZED 1
16
Heinrich Schuchardt15b1bf12020-03-19 18:21:58 +000017efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
AKASHI Takahiro056b45b2018-12-30 15:16:55 +010018
Heinrich Schuchardt52cbac92019-05-04 11:47:48 +020019/*
20 * Allow unaligned memory access.
21 *
22 * This routine is overridden by architectures providing this feature.
23 */
24void __weak allow_unaligned(void)
25{
26}
27
Heinrich Schuchardt07805f22019-04-11 07:34:24 +020028/**
29 * efi_init_platform_lang() - define supported languages
30 *
31 * Set the PlatformLangCodes and PlatformLang variables.
32 *
33 * Return: status code
34 */
35static efi_status_t efi_init_platform_lang(void)
AKASHI Takahiro056b45b2018-12-30 15:16:55 +010036{
Heinrich Schuchardt07805f22019-04-11 07:34:24 +020037 efi_status_t ret;
38 efi_uintn_t data_size = 0;
39 char *lang = CONFIG_EFI_PLATFORM_LANG_CODES;
40 char *pos;
Heinrich Schuchardt734d3252019-04-05 02:45:21 +020041
42 /*
43 * Variable PlatformLangCodes defines the language codes that the
44 * machine can support.
45 */
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +020046 ret = efi_set_variable_int(L"PlatformLangCodes",
47 &efi_global_variable_guid,
48 EFI_VARIABLE_BOOTSERVICE_ACCESS |
49 EFI_VARIABLE_RUNTIME_ACCESS |
50 EFI_VARIABLE_READ_ONLY,
51 sizeof(CONFIG_EFI_PLATFORM_LANG_CODES),
52 CONFIG_EFI_PLATFORM_LANG_CODES, false);
Heinrich Schuchardt734d3252019-04-05 02:45:21 +020053 if (ret != EFI_SUCCESS)
54 goto out;
55
Heinrich Schuchardt07805f22019-04-11 07:34:24 +020056 /*
57 * Variable PlatformLang defines the language that the machine has been
58 * configured for.
59 */
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +020060 ret = efi_get_variable_int(L"PlatformLang",
61 &efi_global_variable_guid,
62 NULL, &data_size, &pos, NULL);
Heinrich Schuchardt07805f22019-04-11 07:34:24 +020063 if (ret == EFI_BUFFER_TOO_SMALL) {
64 /* The variable is already set. Do not change it. */
65 ret = EFI_SUCCESS;
66 goto out;
67 }
68
69 /*
70 * The list of supported languages is semicolon separated. Use the first
71 * language to initialize PlatformLang.
72 */
73 pos = strchr(lang, ';');
74 if (pos)
75 *pos = 0;
76
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +020077 ret = efi_set_variable_int(L"PlatformLang",
78 &efi_global_variable_guid,
79 EFI_VARIABLE_NON_VOLATILE |
80 EFI_VARIABLE_BOOTSERVICE_ACCESS |
81 EFI_VARIABLE_RUNTIME_ACCESS,
82 1 + strlen(lang), lang, false);
Heinrich Schuchardt07805f22019-04-11 07:34:24 +020083out:
84 if (ret != EFI_SUCCESS)
85 printf("EFI: cannot initialize platform language settings\n");
86 return ret;
87}
88
AKASHI Takahirod0f07942020-04-14 11:51:45 +090089#ifdef CONFIG_EFI_SECURE_BOOT
90/**
91 * efi_init_secure_boot - initialize secure boot state
92 *
Heinrich Schuchardt30f92ce2020-05-03 16:29:00 +020093 * Return: status code
AKASHI Takahirod0f07942020-04-14 11:51:45 +090094 */
95static efi_status_t efi_init_secure_boot(void)
96{
97 efi_guid_t signature_types[] = {
98 EFI_CERT_SHA256_GUID,
99 EFI_CERT_X509_GUID,
100 };
101 efi_status_t ret;
102
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +0200103 ret = efi_set_variable_int(L"SignatureSupport",
104 &efi_global_variable_guid,
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900105 EFI_VARIABLE_READ_ONLY |
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +0200106 EFI_VARIABLE_BOOTSERVICE_ACCESS |
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900107 EFI_VARIABLE_RUNTIME_ACCESS,
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +0200108 sizeof(signature_types),
109 &signature_types, false);
AKASHI Takahirod0f07942020-04-14 11:51:45 +0900110 if (ret != EFI_SUCCESS)
111 printf("EFI: cannot initialize SignatureSupport variable\n");
112
113 return ret;
114}
115#else
116static efi_status_t efi_init_secure_boot(void)
117{
118 return EFI_SUCCESS;
119}
120#endif /* CONFIG_EFI_SECURE_BOOT */
121
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200122/**
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900123 * efi_init_capsule - initialize capsule update state
124 *
125 * Return: status code
126 */
127static efi_status_t efi_init_capsule(void)
128{
129 efi_status_t ret = EFI_SUCCESS;
130
131 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_UPDATE)) {
132 ret = efi_set_variable_int(L"CapsuleMax",
133 &efi_guid_capsule_report,
134 EFI_VARIABLE_READ_ONLY |
135 EFI_VARIABLE_BOOTSERVICE_ACCESS |
136 EFI_VARIABLE_RUNTIME_ACCESS,
137 22, L"CapsuleFFFF", false);
138 if (ret != EFI_SUCCESS)
139 printf("EFI: cannot initialize CapsuleMax variable\n");
140 }
141
142 return ret;
143}
144
145/**
146 * efi_init_os_indications() - indicate supported features for OS requests
147 *
148 * Set the OsIndicationsSupported variable.
149 *
150 * Return: status code
151 */
152static efi_status_t efi_init_os_indications(void)
153{
154 u64 os_indications_supported = 0;
155
156 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT))
157 os_indications_supported |=
158 EFI_OS_INDICATIONS_CAPSULE_RESULT_VAR_SUPPORTED;
159
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900160 if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK))
161 os_indications_supported |=
162 EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
163
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900164 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT))
165 os_indications_supported |=
166 EFI_OS_INDICATIONS_FMP_CAPSULE_SUPPORTED;
167
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900168 return efi_set_variable_int(L"OsIndicationsSupported",
169 &efi_global_variable_guid,
170 EFI_VARIABLE_BOOTSERVICE_ACCESS |
171 EFI_VARIABLE_RUNTIME_ACCESS |
172 EFI_VARIABLE_READ_ONLY,
173 sizeof(os_indications_supported),
174 &os_indications_supported, false);
175}
176
Heinrich Schuchardt149108a2021-06-29 13:33:09 +0200177
178/**
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200179 * efi_init_obj_list() - Initialize and populate EFI object list
180 *
181 * Return: status code
182 */
183efi_status_t efi_init_obj_list(void)
184{
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +0100185 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200186
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100187 /* Initialize once only */
188 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
189 return efi_obj_list_initialized;
190
Heinrich Schuchardt52cbac92019-05-04 11:47:48 +0200191 /* Allow unaligned memory access */
192 allow_unaligned();
193
Heinrich Schuchardtc067cef2020-05-20 21:27:29 +0200194 /* Initialize root node */
195 ret = efi_root_node_register();
196 if (ret != EFI_SUCCESS)
197 goto out;
198
AKASHI Takahirof7a963c2020-06-09 14:09:31 +0900199 ret = efi_console_register();
200 if (ret != EFI_SUCCESS)
201 goto out;
202
Heinrich Schuchardt16ad9462020-03-19 18:21:58 +0000203#ifdef CONFIG_PARTITIONS
204 ret = efi_disk_register();
205 if (ret != EFI_SUCCESS)
206 goto out;
207#endif
Heinrich Schuchardtb59c13d2020-09-25 12:50:19 +0200208 if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) {
209 ret = efi_rng_register();
210 if (ret != EFI_SUCCESS)
211 goto out;
212 }
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200213
Heinrich Schuchardt88192092019-06-20 13:52:16 +0200214 /* Initialize variable services */
215 ret = efi_init_variables();
216 if (ret != EFI_SUCCESS)
217 goto out;
218
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200219 /* Define supported languages */
220 ret = efi_init_platform_lang();
221 if (ret != EFI_SUCCESS)
222 goto out;
223
AKASHI Takahirod40e05a2019-04-24 15:30:38 +0900224 /* Indicate supported features */
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900225 ret = efi_init_os_indications();
AKASHI Takahirod40e05a2019-04-24 15:30:38 +0900226 if (ret != EFI_SUCCESS)
227 goto out;
228
Heinrich Schuchardt76be6872020-02-19 20:48:49 +0100229 /* Initialize system table */
230 ret = efi_initialize_system_table();
AKASHI Takahiroe771b4b2019-06-05 13:21:38 +0900231 if (ret != EFI_SUCCESS)
232 goto out;
233
Jose Marinho64a8aae2021-03-02 17:26:38 +0000234 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
235 ret = efi_esrt_register();
236 if (ret != EFI_SUCCESS)
237 goto out;
238 }
239
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200240 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
241 ret = efi_tcg2_register();
242 if (ret != EFI_SUCCESS)
243 goto out;
Masahisa Kojima54bec172021-12-07 14:15:31 +0900244
245 ret = efi_tcg2_do_initial_measurement();
246 if (ret == EFI_SECURITY_VIOLATION)
247 goto out;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200248 }
249
AKASHI Takahirod0f07942020-04-14 11:51:45 +0900250 /* Secure boot */
251 ret = efi_init_secure_boot();
252 if (ret != EFI_SUCCESS)
253 goto out;
254
Heinrich Schuchardt76be6872020-02-19 20:48:49 +0100255 /* Indicate supported runtime services */
256 ret = efi_init_runtime_supported();
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100257 if (ret != EFI_SUCCESS)
258 goto out;
259
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100260 /* Initialize EFI driver uclass */
261 ret = efi_driver_init();
262 if (ret != EFI_SUCCESS)
263 goto out;
264
Ilias Apalodimas6e0184b2021-06-22 17:38:53 +0300265 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)) {
266 ret = efi_load_capsule_drivers();
267 if (ret != EFI_SUCCESS)
268 goto out;
269 }
270
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100271#if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
272 ret = efi_gop_register();
273 if (ret != EFI_SUCCESS)
274 goto out;
275#endif
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100276#ifdef CONFIG_NET
277 ret = efi_net_register();
278 if (ret != EFI_SUCCESS)
279 goto out;
280#endif
281#ifdef CONFIG_GENERATE_ACPI_TABLE
282 ret = efi_acpi_register();
283 if (ret != EFI_SUCCESS)
284 goto out;
285#endif
286#ifdef CONFIG_GENERATE_SMBIOS_TABLE
287 ret = efi_smbios_register();
288 if (ret != EFI_SUCCESS)
289 goto out;
290#endif
291 ret = efi_watchdog_register();
292 if (ret != EFI_SUCCESS)
293 goto out;
294
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900295 ret = efi_init_capsule();
296 if (ret != EFI_SUCCESS)
297 goto out;
298
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100299 /* Initialize EFI runtime services */
300 ret = efi_reset_system_init();
301 if (ret != EFI_SUCCESS)
302 goto out;
303
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900304 /* Execute capsules after reboot */
305 if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK) &&
306 !IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK_EARLY))
307 ret = efi_launch_capsules();
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100308out:
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100309 efi_obj_list_initialized = ret;
310 return ret;
311}