blob: aa59bc7779dfb165f06de737173db4c19778f66d [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 <efi_loader.h>
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +020011#include <efi_variable.h>
Heinrich Schuchardt149108a2021-06-29 13:33:09 +020012#include <log.h>
Heinrich Schuchardtb9276632023-02-10 09:02:56 +010013#include <asm-generic/unaligned.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 */
Simon Glass156ccbc2022-01-23 12:55:12 -070046 ret = efi_set_variable_int(u"PlatformLangCodes",
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +020047 &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 */
Simon Glass156ccbc2022-01-23 12:55:12 -070060 ret = efi_get_variable_int(u"PlatformLang",
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +020061 &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
Simon Glass156ccbc2022-01-23 12:55:12 -070077 ret = efi_set_variable_int(u"PlatformLang",
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +020078 &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/**
90 * efi_init_secure_boot - initialize secure boot state
91 *
Heinrich Schuchardt30f92ce2020-05-03 16:29:00 +020092 * Return: status code
AKASHI Takahirod0f07942020-04-14 11:51:45 +090093 */
94static efi_status_t efi_init_secure_boot(void)
95{
96 efi_guid_t signature_types[] = {
97 EFI_CERT_SHA256_GUID,
98 EFI_CERT_X509_GUID,
99 };
100 efi_status_t ret;
101
Simon Glass156ccbc2022-01-23 12:55:12 -0700102 ret = efi_set_variable_int(u"SignatureSupport",
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +0200103 &efi_global_variable_guid,
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900104 EFI_VARIABLE_READ_ONLY |
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +0200105 EFI_VARIABLE_BOOTSERVICE_ACCESS |
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900106 EFI_VARIABLE_RUNTIME_ACCESS,
Heinrich Schuchardtdc90e5c2020-06-24 19:38:29 +0200107 sizeof(signature_types),
108 &signature_types, false);
AKASHI Takahirod0f07942020-04-14 11:51:45 +0900109 if (ret != EFI_SUCCESS)
110 printf("EFI: cannot initialize SignatureSupport variable\n");
111
112 return ret;
113}
AKASHI Takahirod0f07942020-04-14 11:51:45 +0900114
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200115/**
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900116 * efi_init_capsule - initialize capsule update state
117 *
118 * Return: status code
119 */
120static efi_status_t efi_init_capsule(void)
121{
122 efi_status_t ret = EFI_SUCCESS;
123
Ilias Apalodimase10fffe2023-01-20 13:58:48 +0200124 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)) {
Etienne Carriere44293932023-02-16 18:21:41 +0100125 u16 var_name16[12];
126
127 efi_create_indexed_name(var_name16, sizeof(var_name16),
128 "Capsule", CONFIG_EFI_CAPSULE_MAX);
129
Simon Glass156ccbc2022-01-23 12:55:12 -0700130 ret = efi_set_variable_int(u"CapsuleMax",
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900131 &efi_guid_capsule_report,
132 EFI_VARIABLE_READ_ONLY |
133 EFI_VARIABLE_BOOTSERVICE_ACCESS |
134 EFI_VARIABLE_RUNTIME_ACCESS,
Etienne Carriere44293932023-02-16 18:21:41 +0100135 22, var_name16, false);
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900136 if (ret != EFI_SUCCESS)
137 printf("EFI: cannot initialize CapsuleMax variable\n");
138 }
139
140 return ret;
141}
142
143/**
144 * efi_init_os_indications() - indicate supported features for OS requests
145 *
146 * Set the OsIndicationsSupported variable.
147 *
148 * Return: status code
149 */
150static efi_status_t efi_init_os_indications(void)
151{
152 u64 os_indications_supported = 0;
153
154 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT))
155 os_indications_supported |=
156 EFI_OS_INDICATIONS_CAPSULE_RESULT_VAR_SUPPORTED;
157
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900158 if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK))
159 os_indications_supported |=
160 EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
161
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900162 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT))
163 os_indications_supported |=
164 EFI_OS_INDICATIONS_FMP_CAPSULE_SUPPORTED;
165
Simon Glass156ccbc2022-01-23 12:55:12 -0700166 return efi_set_variable_int(u"OsIndicationsSupported",
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900167 &efi_global_variable_guid,
168 EFI_VARIABLE_BOOTSERVICE_ACCESS |
169 EFI_VARIABLE_RUNTIME_ACCESS |
170 EFI_VARIABLE_READ_ONLY,
171 sizeof(os_indications_supported),
172 &os_indications_supported, false);
173}
174
Heinrich Schuchardt149108a2021-06-29 13:33:09 +0200175/**
AKASHI Takahiro023d9c92022-10-21 12:01:57 +0900176 * efi_init_early() - handle initialization at early stage
AKASHI Takahiroa57ad202022-04-19 10:05:11 +0900177 *
AKASHI Takahiro023d9c92022-10-21 12:01:57 +0900178 * expected to be called in board_init_r().
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200179 *
180 * Return: status code
181 */
AKASHI Takahiro023d9c92022-10-21 12:01:57 +0900182int efi_init_early(void)
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200183{
AKASHI Takahiro023d9c92022-10-21 12:01:57 +0900184 efi_status_t ret;
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200185
Heinrich Schuchardt52cbac92019-05-04 11:47:48 +0200186 /* Allow unaligned memory access */
187 allow_unaligned();
188
Heinrich Schuchardtc067cef2020-05-20 21:27:29 +0200189 /* Initialize root node */
190 ret = efi_root_node_register();
191 if (ret != EFI_SUCCESS)
192 goto out;
193
AKASHI Takahirof7a963c2020-06-09 14:09:31 +0900194 ret = efi_console_register();
195 if (ret != EFI_SUCCESS)
196 goto out;
197
Heinrich Schuchardt8f8fe1d2022-10-05 11:28:47 +0200198 /* Initialize EFI driver uclass */
199 ret = efi_driver_init();
AKASHI Takahiro023d9c92022-10-21 12:01:57 +0900200 if (ret != EFI_SUCCESS)
201 goto out;
AKASHI Takahiroa57ad202022-04-19 10:05:11 +0900202
AKASHI Takahiroa57ad202022-04-19 10:05:11 +0900203 return 0;
AKASHI Takahiro023d9c92022-10-21 12:01:57 +0900204out:
205 /* never re-init UEFI subsystem */
206 efi_obj_list_initialized = ret;
207
208 return -1;
AKASHI Takahiroa57ad202022-04-19 10:05:11 +0900209}
210
211/**
212 * efi_init_obj_list() - Initialize and populate EFI object list
213 *
214 * Return: status code
215 */
216efi_status_t efi_init_obj_list(void)
217{
218 efi_status_t ret = EFI_SUCCESS;
219
220 /* Initialize once only */
221 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
222 return efi_obj_list_initialized;
223
Heinrich Schuchardt68edbed2022-06-14 08:02:03 +0200224 /* Set up console modes */
225 efi_setup_console_size();
226
Heinrich Schuchardtd5391bf2022-08-31 16:37:35 +0200227 /*
228 * Probe block devices to find the ESP.
229 * efi_disks_register() must be called before efi_init_variables().
230 */
231 ret = efi_disks_register();
232 if (ret != EFI_SUCCESS)
233 goto out;
234
Heinrich Schuchardt88192092019-06-20 13:52:16 +0200235 /* Initialize variable services */
236 ret = efi_init_variables();
237 if (ret != EFI_SUCCESS)
238 goto out;
239
Raymond Mao550862b2023-11-10 13:25:37 +0900240 if (IS_ENABLED(CONFIG_CMD_BOOTEFI_BOOTMGR)) {
241 /* update boot option after variable service initialized */
242 ret = efi_bootmgr_update_media_device_boot_option();
243 if (ret != EFI_SUCCESS)
244 goto out;
245 }
246
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200247 /* Define supported languages */
248 ret = efi_init_platform_lang();
249 if (ret != EFI_SUCCESS)
250 goto out;
251
AKASHI Takahirod40e05a2019-04-24 15:30:38 +0900252 /* Indicate supported features */
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900253 ret = efi_init_os_indications();
AKASHI Takahirod40e05a2019-04-24 15:30:38 +0900254 if (ret != EFI_SUCCESS)
255 goto out;
256
Heinrich Schuchardt76be6872020-02-19 20:48:49 +0100257 /* Initialize system table */
258 ret = efi_initialize_system_table();
AKASHI Takahiroe771b4b2019-06-05 13:21:38 +0900259 if (ret != EFI_SUCCESS)
260 goto out;
261
Jose Marinho6b92c172021-12-23 14:51:07 +0000262 if (IS_ENABLED(CONFIG_EFI_ECPT)) {
263 ret = efi_ecpt_register();
264 if (ret != EFI_SUCCESS)
265 goto out;
266 }
267
Jose Marinho64a8aae2021-03-02 17:26:38 +0000268 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
269 ret = efi_esrt_register();
270 if (ret != EFI_SUCCESS)
271 goto out;
272 }
273
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200274 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
275 ret = efi_tcg2_register();
276 if (ret != EFI_SUCCESS)
277 goto out;
Masahisa Kojima54bec172021-12-07 14:15:31 +0900278
279 ret = efi_tcg2_do_initial_measurement();
280 if (ret == EFI_SECURITY_VIOLATION)
281 goto out;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200282 }
283
Ilias Apalodimas5d983292022-07-22 21:32:03 +0530284 /* Install EFI_RNG_PROTOCOL */
285 if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) {
286 ret = efi_rng_register();
287 if (ret != EFI_SUCCESS)
288 goto out;
289 }
290
Sunil V L1ccf8712022-01-28 20:48:44 +0530291 if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
292 ret = efi_riscv_register();
293 if (ret != EFI_SUCCESS)
294 goto out;
295 }
296
AKASHI Takahirod0f07942020-04-14 11:51:45 +0900297 /* Secure boot */
Heinrich Schuchardt3d23ded2024-10-16 12:17:39 +0200298 if (IS_ENABLED(CONFIG_EFI_SECURE_BOOT)) {
299 ret = efi_init_secure_boot();
300 if (ret != EFI_SUCCESS)
301 goto out;
302 }
AKASHI Takahirod0f07942020-04-14 11:51:45 +0900303
Heinrich Schuchardt76be6872020-02-19 20:48:49 +0100304 /* Indicate supported runtime services */
305 ret = efi_init_runtime_supported();
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100306 if (ret != EFI_SUCCESS)
307 goto out;
308
Ilias Apalodimas6e0184b2021-06-22 17:38:53 +0300309 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)) {
310 ret = efi_load_capsule_drivers();
311 if (ret != EFI_SUCCESS)
312 goto out;
313 }
314
Simon Glassb86986c2022-10-18 07:46:31 -0600315 if (IS_ENABLED(CONFIG_VIDEO)) {
Simon Glass777f3e32022-10-16 15:58:34 -0600316 ret = efi_gop_register();
317 if (ret != EFI_SUCCESS)
318 goto out;
319 }
Heinrich Schuchardt3d23ded2024-10-16 12:17:39 +0200320 if (IS_ENABLED(CONFIG_NETDEVICES)) {
321 ret = efi_net_register();
322 if (ret != EFI_SUCCESS)
323 goto out;
324 }
Simon Glass53e8e6f2023-09-19 21:00:12 -0600325 if (IS_ENABLED(CONFIG_ACPI)) {
326 ret = efi_acpi_register();
327 if (ret != EFI_SUCCESS)
328 goto out;
329 }
Simon Glass53fab132023-09-20 07:29:51 -0600330 if (IS_ENABLED(CONFIG_SMBIOS)) {
331 ret = efi_smbios_register();
332 if (ret != EFI_SUCCESS)
333 goto out;
334 }
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100335 ret = efi_watchdog_register();
336 if (ret != EFI_SUCCESS)
337 goto out;
338
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900339 ret = efi_init_capsule();
340 if (ret != EFI_SUCCESS)
341 goto out;
342
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100343 /* Initialize EFI runtime services */
344 ret = efi_reset_system_init();
345 if (ret != EFI_SUCCESS)
346 goto out;
347
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900348 /* Execute capsules after reboot */
349 if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK) &&
350 !IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK_EARLY))
351 ret = efi_launch_capsules();
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100352out:
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100353 efi_obj_list_initialized = ret;
354 return ret;
355}