blob: 113d5d5d56af7f381d52024556f594a1c67e7229 [file] [log] [blame]
AKASHI Takahiro7e0badb2018-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 Schuchardta306a122021-06-29 13:33:09 +02008#define LOG_CATEGORY LOGC_EFI
9
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010010#include <common.h>
11#include <efi_loader.h>
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +020012#include <efi_variable.h>
Heinrich Schuchardta306a122021-06-29 13:33:09 +020013#include <log.h>
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010014
15#define OBJ_LIST_NOT_INITIALIZED 1
16
Heinrich Schuchardtde9bdc62020-03-19 18:21:58 +000017efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010018
Heinrich Schuchardtaea03d62019-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 Schuchardt3700d572019-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 Takahiro7e0badb2018-12-30 15:16:55 +010036{
Heinrich Schuchardt3700d572019-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;
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010041
42 /*
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020043 * Variable PlatformLangCodes defines the language codes that the
44 * machine can support.
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +020045 */
Simon Glass90975372022-01-23 12:55:12 -070046 ret = efi_set_variable_int(u"PlatformLangCodes",
Heinrich Schuchardtee3565f2020-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 Schuchardtf7fe45b2019-04-05 02:45:21 +020053 if (ret != EFI_SUCCESS)
54 goto out;
55
56 /*
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020057 * Variable PlatformLang defines the language that the machine has been
58 * configured for.
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +020059 */
Simon Glass90975372022-01-23 12:55:12 -070060 ret = efi_get_variable_int(u"PlatformLang",
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +020061 &efi_global_variable_guid,
62 NULL, &data_size, &pos, NULL);
Heinrich Schuchardt3700d572019-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 Glass90975372022-01-23 12:55:12 -070077 ret = efi_set_variable_int(u"PlatformLang",
Heinrich Schuchardtee3565f2020-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 Schuchardt3700d572019-04-11 07:34:24 +020083out:
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +020084 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020085 printf("EFI: cannot initialize platform language settings\n");
86 return ret;
87}
88
AKASHI Takahiro8d494f62020-04-14 11:51:45 +090089#ifdef CONFIG_EFI_SECURE_BOOT
90/**
91 * efi_init_secure_boot - initialize secure boot state
92 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +020093 * Return: status code
AKASHI Takahiro8d494f62020-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
Simon Glass90975372022-01-23 12:55:12 -0700103 ret = efi_set_variable_int(u"SignatureSupport",
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +0200104 &efi_global_variable_guid,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900105 EFI_VARIABLE_READ_ONLY |
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +0200106 EFI_VARIABLE_BOOTSERVICE_ACCESS |
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900107 EFI_VARIABLE_RUNTIME_ACCESS,
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +0200108 sizeof(signature_types),
109 &signature_types, false);
AKASHI Takahiro8d494f62020-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 Schuchardt3700d572019-04-11 07:34:24 +0200122/**
AKASHI Takahiro473d9b32020-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)) {
Simon Glass90975372022-01-23 12:55:12 -0700132 ret = efi_set_variable_int(u"CapsuleMax",
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900133 &efi_guid_capsule_report,
134 EFI_VARIABLE_READ_ONLY |
135 EFI_VARIABLE_BOOTSERVICE_ACCESS |
136 EFI_VARIABLE_RUNTIME_ACCESS,
Simon Glass90975372022-01-23 12:55:12 -0700137 22, u"CapsuleFFFF", false);
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900138 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 Takahiro45b819542020-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 Takahiro0d963782020-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
Simon Glass90975372022-01-23 12:55:12 -0700168 return efi_set_variable_int(u"OsIndicationsSupported",
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900169 &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 Schuchardta306a122021-06-29 13:33:09 +0200177/**
AKASHI Takahiroa36dc252022-04-19 10:05:11 +0900178 * __efi_init_early() - handle initialization at early stage
179 *
180 * This function is called in efi_init_obj_list() only if
181 * !CONFIG_EFI_SETUP_EARLY.
Heinrich Schuchardt3700d572019-04-11 07:34:24 +0200182 *
183 * Return: status code
184 */
AKASHI Takahiroa36dc252022-04-19 10:05:11 +0900185static efi_status_t __efi_init_early(void)
Heinrich Schuchardt3700d572019-04-11 07:34:24 +0200186{
Heinrich Schuchardte9e84992021-11-20 11:53:12 +0100187 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +0200188
Heinrich Schuchardtaea03d62019-05-04 11:47:48 +0200189 /* Allow unaligned memory access */
190 allow_unaligned();
191
Heinrich Schuchardtf667a182020-05-20 21:27:29 +0200192 /* Initialize root node */
193 ret = efi_root_node_register();
194 if (ret != EFI_SUCCESS)
195 goto out;
196
AKASHI Takahiro9b31c632020-06-09 14:09:31 +0900197 ret = efi_console_register();
198 if (ret != EFI_SUCCESS)
199 goto out;
200
Heinrich Schuchardt6c5fd212022-10-05 11:28:47 +0200201 /* Initialize EFI driver uclass */
202 ret = efi_driver_init();
203 if (ret != EFI_SUCCESS)
204 goto out;
205
AKASHI Takahiro2381f2e2022-04-19 10:05:12 +0900206 ret = efi_disk_init();
AKASHI Takahiroa36dc252022-04-19 10:05:11 +0900207out:
208 return ret;
209}
210
211/**
212 * efi_init_early() - handle initialization at early stage
213 *
214 * external version of __efi_init_early(); expected to be called in
215 * board_init_r().
216 *
217 * Return: status code
218 */
219int efi_init_early(void)
220{
221 efi_status_t ret;
222
223 ret = __efi_init_early();
224 if (ret != EFI_SUCCESS) {
225 /* never re-init UEFI subsystem */
226 efi_obj_list_initialized = ret;
227 return -1;
228 }
229 return 0;
230}
231
232/**
233 * efi_init_obj_list() - Initialize and populate EFI object list
234 *
235 * Return: status code
236 */
237efi_status_t efi_init_obj_list(void)
238{
239 efi_status_t ret = EFI_SUCCESS;
240
241 /* Initialize once only */
242 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
243 return efi_obj_list_initialized;
244
245 if (!IS_ENABLED(CONFIG_EFI_SETUP_EARLY)) {
246 ret = __efi_init_early();
247 if (ret != EFI_SUCCESS)
248 goto out;
249 }
250
Heinrich Schuchardt761908e2022-06-14 08:02:03 +0200251 /* Set up console modes */
252 efi_setup_console_size();
253
Heinrich Schuchardta02228f2022-08-31 16:37:35 +0200254 /*
255 * Probe block devices to find the ESP.
256 * efi_disks_register() must be called before efi_init_variables().
257 */
258 ret = efi_disks_register();
259 if (ret != EFI_SUCCESS)
260 goto out;
261
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +0200262 /* Initialize variable services */
263 ret = efi_init_variables();
264 if (ret != EFI_SUCCESS)
265 goto out;
266
Heinrich Schuchardt3700d572019-04-11 07:34:24 +0200267 /* Define supported languages */
268 ret = efi_init_platform_lang();
269 if (ret != EFI_SUCCESS)
270 goto out;
271
AKASHI Takahiro3a489252019-04-24 15:30:38 +0900272 /* Indicate supported features */
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900273 ret = efi_init_os_indications();
AKASHI Takahiro3a489252019-04-24 15:30:38 +0900274 if (ret != EFI_SUCCESS)
275 goto out;
276
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100277 /* Initialize system table */
278 ret = efi_initialize_system_table();
AKASHI Takahiro32401842019-06-05 13:21:38 +0900279 if (ret != EFI_SUCCESS)
280 goto out;
281
Jose Marinhoff72cb32021-12-23 14:51:07 +0000282 if (IS_ENABLED(CONFIG_EFI_ECPT)) {
283 ret = efi_ecpt_register();
284 if (ret != EFI_SUCCESS)
285 goto out;
286 }
287
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000288 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
289 ret = efi_esrt_register();
290 if (ret != EFI_SUCCESS)
291 goto out;
292 }
293
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200294 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
295 ret = efi_tcg2_register();
296 if (ret != EFI_SUCCESS)
297 goto out;
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900298
299 ret = efi_tcg2_do_initial_measurement();
300 if (ret == EFI_SECURITY_VIOLATION)
301 goto out;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200302 }
303
Ilias Apalodimas369b9262022-07-22 21:32:03 +0530304 /* Install EFI_RNG_PROTOCOL */
305 if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) {
306 ret = efi_rng_register();
307 if (ret != EFI_SUCCESS)
308 goto out;
309 }
310
Sunil V L279d1c82022-01-28 20:48:44 +0530311 if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
312 ret = efi_riscv_register();
313 if (ret != EFI_SUCCESS)
314 goto out;
315 }
316
AKASHI Takahiro8d494f62020-04-14 11:51:45 +0900317 /* Secure boot */
318 ret = efi_init_secure_boot();
319 if (ret != EFI_SUCCESS)
320 goto out;
321
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100322 /* Indicate supported runtime services */
323 ret = efi_init_runtime_supported();
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100324 if (ret != EFI_SUCCESS)
325 goto out;
326
Ilias Apalodimasfac773e2021-06-22 17:38:53 +0300327 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)) {
328 ret = efi_load_capsule_drivers();
329 if (ret != EFI_SUCCESS)
330 goto out;
331 }
332
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100333#if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
334 ret = efi_gop_register();
335 if (ret != EFI_SUCCESS)
336 goto out;
337#endif
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100338#ifdef CONFIG_NET
339 ret = efi_net_register();
340 if (ret != EFI_SUCCESS)
341 goto out;
342#endif
343#ifdef CONFIG_GENERATE_ACPI_TABLE
344 ret = efi_acpi_register();
345 if (ret != EFI_SUCCESS)
346 goto out;
347#endif
348#ifdef CONFIG_GENERATE_SMBIOS_TABLE
349 ret = efi_smbios_register();
350 if (ret != EFI_SUCCESS)
351 goto out;
352#endif
353 ret = efi_watchdog_register();
354 if (ret != EFI_SUCCESS)
355 goto out;
356
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900357 ret = efi_init_capsule();
358 if (ret != EFI_SUCCESS)
359 goto out;
360
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100361 /* Initialize EFI runtime services */
362 ret = efi_reset_system_init();
363 if (ret != EFI_SUCCESS)
364 goto out;
365
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900366 /* Execute capsules after reboot */
367 if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK) &&
368 !IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK_EARLY))
369 ret = efi_launch_capsules();
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100370out:
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100371 efi_obj_list_initialized = ret;
372 return ret;
373}