blob: 69aaefab633344d9dd8b824470ac0228faaef4f5 [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>
Heinrich Schuchardtbf53ea72023-02-10 09:02:56 +010014#include <asm-generic/unaligned.h>
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010015
16#define OBJ_LIST_NOT_INITIALIZED 1
17
Heinrich Schuchardtde9bdc62020-03-19 18:21:58 +000018efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010019
Heinrich Schuchardtaea03d62019-05-04 11:47:48 +020020/*
21 * Allow unaligned memory access.
22 *
23 * This routine is overridden by architectures providing this feature.
24 */
25void __weak allow_unaligned(void)
26{
27}
28
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020029/**
30 * efi_init_platform_lang() - define supported languages
31 *
32 * Set the PlatformLangCodes and PlatformLang variables.
33 *
34 * Return: status code
35 */
36static efi_status_t efi_init_platform_lang(void)
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010037{
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020038 efi_status_t ret;
39 efi_uintn_t data_size = 0;
40 char *lang = CONFIG_EFI_PLATFORM_LANG_CODES;
41 char *pos;
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010042
43 /*
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020044 * Variable PlatformLangCodes defines the language codes that the
45 * machine can support.
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +020046 */
Simon Glass90975372022-01-23 12:55:12 -070047 ret = efi_set_variable_int(u"PlatformLangCodes",
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +020048 &efi_global_variable_guid,
49 EFI_VARIABLE_BOOTSERVICE_ACCESS |
50 EFI_VARIABLE_RUNTIME_ACCESS |
51 EFI_VARIABLE_READ_ONLY,
52 sizeof(CONFIG_EFI_PLATFORM_LANG_CODES),
53 CONFIG_EFI_PLATFORM_LANG_CODES, false);
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +020054 if (ret != EFI_SUCCESS)
55 goto out;
56
57 /*
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020058 * Variable PlatformLang defines the language that the machine has been
59 * configured for.
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +020060 */
Simon Glass90975372022-01-23 12:55:12 -070061 ret = efi_get_variable_int(u"PlatformLang",
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +020062 &efi_global_variable_guid,
63 NULL, &data_size, &pos, NULL);
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020064 if (ret == EFI_BUFFER_TOO_SMALL) {
65 /* The variable is already set. Do not change it. */
66 ret = EFI_SUCCESS;
67 goto out;
68 }
69
70 /*
71 * The list of supported languages is semicolon separated. Use the first
72 * language to initialize PlatformLang.
73 */
74 pos = strchr(lang, ';');
75 if (pos)
76 *pos = 0;
77
Simon Glass90975372022-01-23 12:55:12 -070078 ret = efi_set_variable_int(u"PlatformLang",
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +020079 &efi_global_variable_guid,
80 EFI_VARIABLE_NON_VOLATILE |
81 EFI_VARIABLE_BOOTSERVICE_ACCESS |
82 EFI_VARIABLE_RUNTIME_ACCESS,
83 1 + strlen(lang), lang, false);
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020084out:
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +020085 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020086 printf("EFI: cannot initialize platform language settings\n");
87 return ret;
88}
89
AKASHI Takahiro8d494f62020-04-14 11:51:45 +090090#ifdef CONFIG_EFI_SECURE_BOOT
91/**
92 * efi_init_secure_boot - initialize secure boot state
93 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +020094 * Return: status code
AKASHI Takahiro8d494f62020-04-14 11:51:45 +090095 */
96static efi_status_t efi_init_secure_boot(void)
97{
98 efi_guid_t signature_types[] = {
99 EFI_CERT_SHA256_GUID,
100 EFI_CERT_X509_GUID,
101 };
102 efi_status_t ret;
103
Simon Glass90975372022-01-23 12:55:12 -0700104 ret = efi_set_variable_int(u"SignatureSupport",
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +0200105 &efi_global_variable_guid,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900106 EFI_VARIABLE_READ_ONLY |
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +0200107 EFI_VARIABLE_BOOTSERVICE_ACCESS |
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900108 EFI_VARIABLE_RUNTIME_ACCESS,
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +0200109 sizeof(signature_types),
110 &signature_types, false);
AKASHI Takahiro8d494f62020-04-14 11:51:45 +0900111 if (ret != EFI_SUCCESS)
112 printf("EFI: cannot initialize SignatureSupport variable\n");
113
114 return ret;
115}
116#else
117static efi_status_t efi_init_secure_boot(void)
118{
119 return EFI_SUCCESS;
120}
121#endif /* CONFIG_EFI_SECURE_BOOT */
122
Heinrich Schuchardt3700d572019-04-11 07:34:24 +0200123/**
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900124 * efi_init_capsule - initialize capsule update state
125 *
126 * Return: status code
127 */
128static efi_status_t efi_init_capsule(void)
129{
130 efi_status_t ret = EFI_SUCCESS;
131
Ilias Apalodimas21bd5ac2023-01-20 13:58:48 +0200132 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)) {
Simon Glass90975372022-01-23 12:55:12 -0700133 ret = efi_set_variable_int(u"CapsuleMax",
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900134 &efi_guid_capsule_report,
135 EFI_VARIABLE_READ_ONLY |
136 EFI_VARIABLE_BOOTSERVICE_ACCESS |
137 EFI_VARIABLE_RUNTIME_ACCESS,
Simon Glass90975372022-01-23 12:55:12 -0700138 22, u"CapsuleFFFF", false);
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900139 if (ret != EFI_SUCCESS)
140 printf("EFI: cannot initialize CapsuleMax variable\n");
141 }
142
143 return ret;
144}
145
146/**
147 * efi_init_os_indications() - indicate supported features for OS requests
148 *
149 * Set the OsIndicationsSupported variable.
150 *
151 * Return: status code
152 */
153static efi_status_t efi_init_os_indications(void)
154{
155 u64 os_indications_supported = 0;
156
157 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT))
158 os_indications_supported |=
159 EFI_OS_INDICATIONS_CAPSULE_RESULT_VAR_SUPPORTED;
160
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900161 if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK))
162 os_indications_supported |=
163 EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
164
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900165 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT))
166 os_indications_supported |=
167 EFI_OS_INDICATIONS_FMP_CAPSULE_SUPPORTED;
168
Simon Glass90975372022-01-23 12:55:12 -0700169 return efi_set_variable_int(u"OsIndicationsSupported",
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900170 &efi_global_variable_guid,
171 EFI_VARIABLE_BOOTSERVICE_ACCESS |
172 EFI_VARIABLE_RUNTIME_ACCESS |
173 EFI_VARIABLE_READ_ONLY,
174 sizeof(os_indications_supported),
175 &os_indications_supported, false);
176}
177
Heinrich Schuchardta306a122021-06-29 13:33:09 +0200178/**
AKASHI Takahiro9a84e072022-10-21 12:01:57 +0900179 * efi_init_early() - handle initialization at early stage
AKASHI Takahiroa36dc252022-04-19 10:05:11 +0900180 *
AKASHI Takahiro9a84e072022-10-21 12:01:57 +0900181 * expected to be called in board_init_r().
Heinrich Schuchardt3700d572019-04-11 07:34:24 +0200182 *
183 * Return: status code
184 */
AKASHI Takahiro9a84e072022-10-21 12:01:57 +0900185int efi_init_early(void)
Heinrich Schuchardt3700d572019-04-11 07:34:24 +0200186{
AKASHI Takahiro9a84e072022-10-21 12:01:57 +0900187 efi_status_t ret;
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();
AKASHI Takahiro9a84e072022-10-21 12:01:57 +0900203 if (ret != EFI_SUCCESS)
204 goto out;
AKASHI Takahiroa36dc252022-04-19 10:05:11 +0900205
AKASHI Takahiroa36dc252022-04-19 10:05:11 +0900206 return 0;
AKASHI Takahiro9a84e072022-10-21 12:01:57 +0900207out:
208 /* never re-init UEFI subsystem */
209 efi_obj_list_initialized = ret;
210
211 return -1;
AKASHI Takahiroa36dc252022-04-19 10:05:11 +0900212}
213
214/**
215 * efi_init_obj_list() - Initialize and populate EFI object list
216 *
217 * Return: status code
218 */
219efi_status_t efi_init_obj_list(void)
220{
221 efi_status_t ret = EFI_SUCCESS;
222
223 /* Initialize once only */
224 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
225 return efi_obj_list_initialized;
226
Heinrich Schuchardt761908e2022-06-14 08:02:03 +0200227 /* Set up console modes */
228 efi_setup_console_size();
229
Heinrich Schuchardta02228f2022-08-31 16:37:35 +0200230 /*
231 * Probe block devices to find the ESP.
232 * efi_disks_register() must be called before efi_init_variables().
233 */
234 ret = efi_disks_register();
235 if (ret != EFI_SUCCESS)
236 goto out;
237
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +0200238 /* Initialize variable services */
239 ret = efi_init_variables();
240 if (ret != EFI_SUCCESS)
241 goto out;
242
Heinrich Schuchardt3700d572019-04-11 07:34:24 +0200243 /* Define supported languages */
244 ret = efi_init_platform_lang();
245 if (ret != EFI_SUCCESS)
246 goto out;
247
AKASHI Takahiro3a489252019-04-24 15:30:38 +0900248 /* Indicate supported features */
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900249 ret = efi_init_os_indications();
AKASHI Takahiro3a489252019-04-24 15:30:38 +0900250 if (ret != EFI_SUCCESS)
251 goto out;
252
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100253 /* Initialize system table */
254 ret = efi_initialize_system_table();
AKASHI Takahiro32401842019-06-05 13:21:38 +0900255 if (ret != EFI_SUCCESS)
256 goto out;
257
Jose Marinhoff72cb32021-12-23 14:51:07 +0000258 if (IS_ENABLED(CONFIG_EFI_ECPT)) {
259 ret = efi_ecpt_register();
260 if (ret != EFI_SUCCESS)
261 goto out;
262 }
263
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000264 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
265 ret = efi_esrt_register();
266 if (ret != EFI_SUCCESS)
267 goto out;
268 }
269
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200270 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
271 ret = efi_tcg2_register();
272 if (ret != EFI_SUCCESS)
273 goto out;
Masahisa Kojima0fd43792021-12-07 14:15:31 +0900274
275 ret = efi_tcg2_do_initial_measurement();
276 if (ret == EFI_SECURITY_VIOLATION)
277 goto out;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200278 }
279
Ilias Apalodimas369b9262022-07-22 21:32:03 +0530280 /* Install EFI_RNG_PROTOCOL */
281 if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) {
282 ret = efi_rng_register();
283 if (ret != EFI_SUCCESS)
284 goto out;
285 }
286
Sunil V L279d1c82022-01-28 20:48:44 +0530287 if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
288 ret = efi_riscv_register();
289 if (ret != EFI_SUCCESS)
290 goto out;
291 }
292
AKASHI Takahiro8d494f62020-04-14 11:51:45 +0900293 /* Secure boot */
294 ret = efi_init_secure_boot();
295 if (ret != EFI_SUCCESS)
296 goto out;
297
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100298 /* Indicate supported runtime services */
299 ret = efi_init_runtime_supported();
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100300 if (ret != EFI_SUCCESS)
301 goto out;
302
Ilias Apalodimasfac773e2021-06-22 17:38:53 +0300303 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)) {
304 ret = efi_load_capsule_drivers();
305 if (ret != EFI_SUCCESS)
306 goto out;
307 }
308
Simon Glass52cb5042022-10-18 07:46:31 -0600309 if (IS_ENABLED(CONFIG_VIDEO)) {
Simon Glass26a6af22022-10-16 15:58:34 -0600310 ret = efi_gop_register();
311 if (ret != EFI_SUCCESS)
312 goto out;
313 }
Jan Kiszkaf1389822022-10-14 18:10:06 +0200314#ifdef CONFIG_NETDEVICES
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100315 ret = efi_net_register();
316 if (ret != EFI_SUCCESS)
317 goto out;
318#endif
319#ifdef CONFIG_GENERATE_ACPI_TABLE
320 ret = efi_acpi_register();
321 if (ret != EFI_SUCCESS)
322 goto out;
323#endif
324#ifdef CONFIG_GENERATE_SMBIOS_TABLE
325 ret = efi_smbios_register();
326 if (ret != EFI_SUCCESS)
327 goto out;
328#endif
329 ret = efi_watchdog_register();
330 if (ret != EFI_SUCCESS)
331 goto out;
332
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900333 ret = efi_init_capsule();
334 if (ret != EFI_SUCCESS)
335 goto out;
336
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100337 /* Initialize EFI runtime services */
338 ret = efi_reset_system_init();
339 if (ret != EFI_SUCCESS)
340 goto out;
341
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900342 /* Execute capsules after reboot */
343 if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK) &&
344 !IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK_EARLY))
345 ret = efi_launch_capsules();
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100346out:
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100347 efi_obj_list_initialized = ret;
348 return ret;
349}