blob: 5800cbf6d46ab86065ee7b87ff9a60ced6ed248e [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
8#include <common.h>
Heinrich Schuchardtaea03d62019-05-04 11:47:48 +02009#include <bootm.h>
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010010#include <efi_loader.h>
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +020011#include <efi_variable.h>
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010012
13#define OBJ_LIST_NOT_INITIALIZED 1
14
Heinrich Schuchardtde9bdc62020-03-19 18:21:58 +000015efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010016
Heinrich Schuchardtaea03d62019-05-04 11:47:48 +020017/*
18 * Allow unaligned memory access.
19 *
20 * This routine is overridden by architectures providing this feature.
21 */
22void __weak allow_unaligned(void)
23{
24}
25
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020026/**
27 * efi_init_platform_lang() - define supported languages
28 *
29 * Set the PlatformLangCodes and PlatformLang variables.
30 *
31 * Return: status code
32 */
33static efi_status_t efi_init_platform_lang(void)
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010034{
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020035 efi_status_t ret;
36 efi_uintn_t data_size = 0;
37 char *lang = CONFIG_EFI_PLATFORM_LANG_CODES;
38 char *pos;
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +010039
40 /*
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020041 * Variable PlatformLangCodes defines the language codes that the
42 * machine can support.
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +020043 */
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +020044 ret = efi_set_variable_int(L"PlatformLangCodes",
45 &efi_global_variable_guid,
46 EFI_VARIABLE_BOOTSERVICE_ACCESS |
47 EFI_VARIABLE_RUNTIME_ACCESS |
48 EFI_VARIABLE_READ_ONLY,
49 sizeof(CONFIG_EFI_PLATFORM_LANG_CODES),
50 CONFIG_EFI_PLATFORM_LANG_CODES, false);
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +020051 if (ret != EFI_SUCCESS)
52 goto out;
53
54 /*
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020055 * Variable PlatformLang defines the language that the machine has been
56 * configured for.
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +020057 */
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +020058 ret = efi_get_variable_int(L"PlatformLang",
59 &efi_global_variable_guid,
60 NULL, &data_size, &pos, NULL);
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020061 if (ret == EFI_BUFFER_TOO_SMALL) {
62 /* The variable is already set. Do not change it. */
63 ret = EFI_SUCCESS;
64 goto out;
65 }
66
67 /*
68 * The list of supported languages is semicolon separated. Use the first
69 * language to initialize PlatformLang.
70 */
71 pos = strchr(lang, ';');
72 if (pos)
73 *pos = 0;
74
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +020075 ret = efi_set_variable_int(L"PlatformLang",
76 &efi_global_variable_guid,
77 EFI_VARIABLE_NON_VOLATILE |
78 EFI_VARIABLE_BOOTSERVICE_ACCESS |
79 EFI_VARIABLE_RUNTIME_ACCESS,
80 1 + strlen(lang), lang, false);
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020081out:
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +020082 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3700d572019-04-11 07:34:24 +020083 printf("EFI: cannot initialize platform language settings\n");
84 return ret;
85}
86
AKASHI Takahiro8d494f62020-04-14 11:51:45 +090087#ifdef CONFIG_EFI_SECURE_BOOT
88/**
89 * efi_init_secure_boot - initialize secure boot state
90 *
Heinrich Schuchardte2c43da2020-05-03 16:29:00 +020091 * Return: status code
AKASHI Takahiro8d494f62020-04-14 11:51:45 +090092 */
93static efi_status_t efi_init_secure_boot(void)
94{
95 efi_guid_t signature_types[] = {
96 EFI_CERT_SHA256_GUID,
97 EFI_CERT_X509_GUID,
98 };
99 efi_status_t ret;
100
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +0200101 ret = efi_set_variable_int(L"SignatureSupport",
102 &efi_global_variable_guid,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900103 EFI_VARIABLE_READ_ONLY |
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +0200104 EFI_VARIABLE_BOOTSERVICE_ACCESS |
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900105 EFI_VARIABLE_RUNTIME_ACCESS,
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +0200106 sizeof(signature_types),
107 &signature_types, false);
AKASHI Takahiro8d494f62020-04-14 11:51:45 +0900108 if (ret != EFI_SUCCESS)
109 printf("EFI: cannot initialize SignatureSupport variable\n");
110
111 return ret;
112}
113#else
114static efi_status_t efi_init_secure_boot(void)
115{
116 return EFI_SUCCESS;
117}
118#endif /* CONFIG_EFI_SECURE_BOOT */
119
Heinrich Schuchardt3700d572019-04-11 07:34:24 +0200120/**
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900121 * efi_init_capsule - initialize capsule update state
122 *
123 * Return: status code
124 */
125static efi_status_t efi_init_capsule(void)
126{
127 efi_status_t ret = EFI_SUCCESS;
128
129 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_UPDATE)) {
130 ret = efi_set_variable_int(L"CapsuleMax",
131 &efi_guid_capsule_report,
132 EFI_VARIABLE_READ_ONLY |
133 EFI_VARIABLE_BOOTSERVICE_ACCESS |
134 EFI_VARIABLE_RUNTIME_ACCESS,
135 22, L"CapsuleFFFF", false);
136 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 Takahiro45b819542020-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 Takahiro0d963782020-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
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900166 return efi_set_variable_int(L"OsIndicationsSupported",
167 &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
175/**
Heinrich Schuchardt3700d572019-04-11 07:34:24 +0200176 * efi_init_obj_list() - Initialize and populate EFI object list
177 *
178 * Return: status code
179 */
180efi_status_t efi_init_obj_list(void)
181{
182 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +0200183
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100184 /* Initialize once only */
185 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
186 return efi_obj_list_initialized;
187
Heinrich Schuchardtaea03d62019-05-04 11:47:48 +0200188 /* Allow unaligned memory access */
189 allow_unaligned();
190
191 /* On ARM switch from EL3 or secure mode to EL2 or non-secure mode */
192 switch_to_non_secure_mode();
193
Heinrich Schuchardtf667a182020-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 Takahiro9b31c632020-06-09 14:09:31 +0900199 ret = efi_console_register();
200 if (ret != EFI_SUCCESS)
201 goto out;
202
Heinrich Schuchardteef4eee2020-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 Schuchardtc63f68f2020-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 Apalodimas590fef62020-11-11 11:18:11 +0200213
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +0200214 /* Initialize variable services */
215 ret = efi_init_variables();
216 if (ret != EFI_SUCCESS)
217 goto out;
218
Heinrich Schuchardt3700d572019-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 Takahiro3a489252019-04-24 15:30:38 +0900224 /* Indicate supported features */
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900225 ret = efi_init_os_indications();
AKASHI Takahiro3a489252019-04-24 15:30:38 +0900226 if (ret != EFI_SUCCESS)
227 goto out;
228
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100229 /* Initialize system table */
230 ret = efi_initialize_system_table();
AKASHI Takahiro32401842019-06-05 13:21:38 +0900231 if (ret != EFI_SUCCESS)
232 goto out;
233
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200234 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
235 ret = efi_tcg2_register();
236 if (ret != EFI_SUCCESS)
237 goto out;
238 }
239
AKASHI Takahiro8d494f62020-04-14 11:51:45 +0900240 /* Secure boot */
241 ret = efi_init_secure_boot();
242 if (ret != EFI_SUCCESS)
243 goto out;
244
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100245 /* Indicate supported runtime services */
246 ret = efi_init_runtime_supported();
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100247 if (ret != EFI_SUCCESS)
248 goto out;
249
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100250 /* Initialize EFI driver uclass */
251 ret = efi_driver_init();
252 if (ret != EFI_SUCCESS)
253 goto out;
254
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100255#if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
256 ret = efi_gop_register();
257 if (ret != EFI_SUCCESS)
258 goto out;
259#endif
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100260#ifdef CONFIG_NET
261 ret = efi_net_register();
262 if (ret != EFI_SUCCESS)
263 goto out;
264#endif
265#ifdef CONFIG_GENERATE_ACPI_TABLE
266 ret = efi_acpi_register();
267 if (ret != EFI_SUCCESS)
268 goto out;
269#endif
270#ifdef CONFIG_GENERATE_SMBIOS_TABLE
271 ret = efi_smbios_register();
272 if (ret != EFI_SUCCESS)
273 goto out;
274#endif
275 ret = efi_watchdog_register();
276 if (ret != EFI_SUCCESS)
277 goto out;
278
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900279 ret = efi_init_capsule();
280 if (ret != EFI_SUCCESS)
281 goto out;
282
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100283 /* Initialize EFI runtime services */
284 ret = efi_reset_system_init();
285 if (ret != EFI_SUCCESS)
286 goto out;
287
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900288 /* Execute capsules after reboot */
289 if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK) &&
290 !IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK_EARLY))
291 ret = efi_launch_capsules();
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100292out:
293 efi_obj_list_initialized = ret;
294 return ret;
295}