1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright (c) 2016-2018 Alexander Graf et al.
8 #define LOG_CATEGORY LOGC_EFI
11 #include <efi_loader.h>
12 #include <efi_variable.h>
15 #define OBJ_LIST_NOT_INITIALIZED 1
17 efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
20 * Allow unaligned memory access.
22 * This routine is overridden by architectures providing this feature.
24 void __weak allow_unaligned(void)
29 * efi_init_platform_lang() - define supported languages
31 * Set the PlatformLangCodes and PlatformLang variables.
35 static efi_status_t efi_init_platform_lang(void)
38 efi_uintn_t data_size = 0;
39 char *lang = CONFIG_EFI_PLATFORM_LANG_CODES;
43 * Variable PlatformLangCodes defines the language codes that the
44 * machine can support.
46 ret = efi_set_variable_int(u"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);
53 if (ret != EFI_SUCCESS)
57 * Variable PlatformLang defines the language that the machine has been
60 ret = efi_get_variable_int(u"PlatformLang",
61 &efi_global_variable_guid,
62 NULL, &data_size, &pos, NULL);
63 if (ret == EFI_BUFFER_TOO_SMALL) {
64 /* The variable is already set. Do not change it. */
70 * The list of supported languages is semicolon separated. Use the first
71 * language to initialize PlatformLang.
73 pos = strchr(lang, ';');
77 ret = efi_set_variable_int(u"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);
84 if (ret != EFI_SUCCESS)
85 printf("EFI: cannot initialize platform language settings\n");
89 #ifdef CONFIG_EFI_SECURE_BOOT
91 * efi_init_secure_boot - initialize secure boot state
95 static efi_status_t efi_init_secure_boot(void)
97 efi_guid_t signature_types[] = {
103 ret = efi_set_variable_int(u"SignatureSupport",
104 &efi_global_variable_guid,
105 EFI_VARIABLE_READ_ONLY |
106 EFI_VARIABLE_BOOTSERVICE_ACCESS |
107 EFI_VARIABLE_RUNTIME_ACCESS,
108 sizeof(signature_types),
109 &signature_types, false);
110 if (ret != EFI_SUCCESS)
111 printf("EFI: cannot initialize SignatureSupport variable\n");
116 static efi_status_t efi_init_secure_boot(void)
120 #endif /* CONFIG_EFI_SECURE_BOOT */
123 * efi_init_capsule - initialize capsule update state
125 * Return: status code
127 static efi_status_t efi_init_capsule(void)
129 efi_status_t ret = EFI_SUCCESS;
131 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_UPDATE)) {
132 ret = efi_set_variable_int(u"CapsuleMax",
133 &efi_guid_capsule_report,
134 EFI_VARIABLE_READ_ONLY |
135 EFI_VARIABLE_BOOTSERVICE_ACCESS |
136 EFI_VARIABLE_RUNTIME_ACCESS,
137 22, u"CapsuleFFFF", false);
138 if (ret != EFI_SUCCESS)
139 printf("EFI: cannot initialize CapsuleMax variable\n");
146 * efi_init_os_indications() - indicate supported features for OS requests
148 * Set the OsIndicationsSupported variable.
150 * Return: status code
152 static efi_status_t efi_init_os_indications(void)
154 u64 os_indications_supported = 0;
156 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT))
157 os_indications_supported |=
158 EFI_OS_INDICATIONS_CAPSULE_RESULT_VAR_SUPPORTED;
160 if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK))
161 os_indications_supported |=
162 EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
164 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT))
165 os_indications_supported |=
166 EFI_OS_INDICATIONS_FMP_CAPSULE_SUPPORTED;
168 return efi_set_variable_int(u"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);
178 * __efi_init_early() - handle initialization at early stage
180 * This function is called in efi_init_obj_list() only if
181 * !CONFIG_EFI_SETUP_EARLY.
183 * Return: status code
185 static efi_status_t __efi_init_early(void)
187 efi_status_t ret = EFI_SUCCESS;
189 /* Allow unaligned memory access */
192 /* Initialize root node */
193 ret = efi_root_node_register();
194 if (ret != EFI_SUCCESS)
197 ret = efi_console_register();
198 if (ret != EFI_SUCCESS)
201 /* Initialize EFI driver uclass */
202 ret = efi_driver_init();
208 * efi_init_early() - handle initialization at early stage
210 * external version of __efi_init_early(); expected to be called in
213 * Return: status code
215 int efi_init_early(void)
219 ret = __efi_init_early();
220 if (ret != EFI_SUCCESS) {
221 /* never re-init UEFI subsystem */
222 efi_obj_list_initialized = ret;
229 * efi_init_obj_list() - Initialize and populate EFI object list
231 * Return: status code
233 efi_status_t efi_init_obj_list(void)
235 efi_status_t ret = EFI_SUCCESS;
237 /* Initialize once only */
238 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
239 return efi_obj_list_initialized;
241 if (!IS_ENABLED(CONFIG_EFI_SETUP_EARLY)) {
242 ret = __efi_init_early();
243 if (ret != EFI_SUCCESS)
247 /* Set up console modes */
248 efi_setup_console_size();
251 * Probe block devices to find the ESP.
252 * efi_disks_register() must be called before efi_init_variables().
254 ret = efi_disks_register();
255 if (ret != EFI_SUCCESS)
258 /* Initialize variable services */
259 ret = efi_init_variables();
260 if (ret != EFI_SUCCESS)
263 /* Define supported languages */
264 ret = efi_init_platform_lang();
265 if (ret != EFI_SUCCESS)
268 /* Indicate supported features */
269 ret = efi_init_os_indications();
270 if (ret != EFI_SUCCESS)
273 /* Initialize system table */
274 ret = efi_initialize_system_table();
275 if (ret != EFI_SUCCESS)
278 if (IS_ENABLED(CONFIG_EFI_ECPT)) {
279 ret = efi_ecpt_register();
280 if (ret != EFI_SUCCESS)
284 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
285 ret = efi_esrt_register();
286 if (ret != EFI_SUCCESS)
290 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
291 ret = efi_tcg2_register();
292 if (ret != EFI_SUCCESS)
295 ret = efi_tcg2_do_initial_measurement();
296 if (ret == EFI_SECURITY_VIOLATION)
300 /* Install EFI_RNG_PROTOCOL */
301 if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) {
302 ret = efi_rng_register();
303 if (ret != EFI_SUCCESS)
307 if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
308 ret = efi_riscv_register();
309 if (ret != EFI_SUCCESS)
314 ret = efi_init_secure_boot();
315 if (ret != EFI_SUCCESS)
318 /* Indicate supported runtime services */
319 ret = efi_init_runtime_supported();
320 if (ret != EFI_SUCCESS)
323 if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)) {
324 ret = efi_load_capsule_drivers();
325 if (ret != EFI_SUCCESS)
329 if (IS_ENABLED(CONFIG_VIDEO)) {
330 ret = efi_gop_register();
331 if (ret != EFI_SUCCESS)
334 #ifdef CONFIG_NETDEVICES
335 ret = efi_net_register();
336 if (ret != EFI_SUCCESS)
339 #ifdef CONFIG_GENERATE_ACPI_TABLE
340 ret = efi_acpi_register();
341 if (ret != EFI_SUCCESS)
344 #ifdef CONFIG_GENERATE_SMBIOS_TABLE
345 ret = efi_smbios_register();
346 if (ret != EFI_SUCCESS)
349 ret = efi_watchdog_register();
350 if (ret != EFI_SUCCESS)
353 ret = efi_init_capsule();
354 if (ret != EFI_SUCCESS)
357 /* Initialize EFI runtime services */
358 ret = efi_reset_system_init();
359 if (ret != EFI_SUCCESS)
362 /* Execute capsules after reboot */
363 if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK) &&
364 !IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK_EARLY))
365 ret = efi_launch_capsules();
367 efi_obj_list_initialized = ret;