blob: 65739aca49334203b81128bccb19f112fe037179 [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
8#include <common.h>
Heinrich Schuchardt52cbac92019-05-04 11:47:48 +02009#include <bootm.h>
AKASHI Takahiro056b45b2018-12-30 15:16:55 +010010#include <efi_loader.h>
11
12#define OBJ_LIST_NOT_INITIALIZED 1
13
14static efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
15
Heinrich Schuchardt52cbac92019-05-04 11:47:48 +020016/*
17 * Allow unaligned memory access.
18 *
19 * This routine is overridden by architectures providing this feature.
20 */
21void __weak allow_unaligned(void)
22{
23}
24
Heinrich Schuchardt07805f22019-04-11 07:34:24 +020025/**
26 * efi_init_platform_lang() - define supported languages
27 *
28 * Set the PlatformLangCodes and PlatformLang variables.
29 *
30 * Return: status code
31 */
32static efi_status_t efi_init_platform_lang(void)
AKASHI Takahiro056b45b2018-12-30 15:16:55 +010033{
Heinrich Schuchardt07805f22019-04-11 07:34:24 +020034 efi_status_t ret;
35 efi_uintn_t data_size = 0;
36 char *lang = CONFIG_EFI_PLATFORM_LANG_CODES;
37 char *pos;
Heinrich Schuchardt734d3252019-04-05 02:45:21 +020038
39 /*
40 * Variable PlatformLangCodes defines the language codes that the
41 * machine can support.
42 */
43 ret = EFI_CALL(efi_set_variable(L"PlatformLangCodes",
44 &efi_global_variable_guid,
45 EFI_VARIABLE_BOOTSERVICE_ACCESS |
46 EFI_VARIABLE_RUNTIME_ACCESS,
Heinrich Schuchardt07805f22019-04-11 07:34:24 +020047 sizeof(CONFIG_EFI_PLATFORM_LANG_CODES),
48 CONFIG_EFI_PLATFORM_LANG_CODES));
Heinrich Schuchardt734d3252019-04-05 02:45:21 +020049 if (ret != EFI_SUCCESS)
50 goto out;
51
Heinrich Schuchardt07805f22019-04-11 07:34:24 +020052 /*
53 * Variable PlatformLang defines the language that the machine has been
54 * configured for.
55 */
56 ret = EFI_CALL(efi_get_variable(L"PlatformLang",
57 &efi_global_variable_guid,
58 NULL, &data_size, &pos));
59 if (ret == EFI_BUFFER_TOO_SMALL) {
60 /* The variable is already set. Do not change it. */
61 ret = EFI_SUCCESS;
62 goto out;
63 }
64
65 /*
66 * The list of supported languages is semicolon separated. Use the first
67 * language to initialize PlatformLang.
68 */
69 pos = strchr(lang, ';');
70 if (pos)
71 *pos = 0;
72
73 ret = EFI_CALL(efi_set_variable(L"PlatformLang",
74 &efi_global_variable_guid,
75 EFI_VARIABLE_NON_VOLATILE |
76 EFI_VARIABLE_BOOTSERVICE_ACCESS |
77 EFI_VARIABLE_RUNTIME_ACCESS,
78 1 + strlen(lang), lang));
79out:
80 if (ret != EFI_SUCCESS)
81 printf("EFI: cannot initialize platform language settings\n");
82 return ret;
83}
84
AKASHI Takahirod0f07942020-04-14 11:51:45 +090085#ifdef CONFIG_EFI_SECURE_BOOT
86/**
87 * efi_init_secure_boot - initialize secure boot state
88 *
Heinrich Schuchardt30f92ce2020-05-03 16:29:00 +020089 * Return: status code
AKASHI Takahirod0f07942020-04-14 11:51:45 +090090 */
91static efi_status_t efi_init_secure_boot(void)
92{
93 efi_guid_t signature_types[] = {
94 EFI_CERT_SHA256_GUID,
95 EFI_CERT_X509_GUID,
96 };
97 efi_status_t ret;
98
99 /* TODO: read-only */
100 ret = EFI_CALL(efi_set_variable(L"SignatureSupport",
101 &efi_global_variable_guid,
102 EFI_VARIABLE_BOOTSERVICE_ACCESS
103 | EFI_VARIABLE_RUNTIME_ACCESS,
104 sizeof(signature_types),
105 &signature_types));
106 if (ret != EFI_SUCCESS)
107 printf("EFI: cannot initialize SignatureSupport variable\n");
108
109 return ret;
110}
111#else
112static efi_status_t efi_init_secure_boot(void)
113{
114 return EFI_SUCCESS;
115}
116#endif /* CONFIG_EFI_SECURE_BOOT */
117
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200118/**
119 * efi_init_obj_list() - Initialize and populate EFI object list
120 *
121 * Return: status code
122 */
123efi_status_t efi_init_obj_list(void)
124{
AKASHI Takahirod40e05a2019-04-24 15:30:38 +0900125 u64 os_indications_supported = 0; /* None */
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200126 efi_status_t ret = EFI_SUCCESS;
127
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100128 /* Initialize once only */
129 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
130 return efi_obj_list_initialized;
131
Heinrich Schuchardt52cbac92019-05-04 11:47:48 +0200132 /* Allow unaligned memory access */
133 allow_unaligned();
134
135 /* On ARM switch from EL3 or secure mode to EL2 or non-secure mode */
136 switch_to_non_secure_mode();
137
Heinrich Schuchardt88192092019-06-20 13:52:16 +0200138 /* Initialize variable services */
139 ret = efi_init_variables();
140 if (ret != EFI_SUCCESS)
141 goto out;
142
Heinrich Schuchardt07805f22019-04-11 07:34:24 +0200143 /* Define supported languages */
144 ret = efi_init_platform_lang();
145 if (ret != EFI_SUCCESS)
146 goto out;
147
AKASHI Takahirod40e05a2019-04-24 15:30:38 +0900148 /* Indicate supported features */
149 ret = EFI_CALL(efi_set_variable(L"OsIndicationsSupported",
150 &efi_global_variable_guid,
151 EFI_VARIABLE_BOOTSERVICE_ACCESS |
152 EFI_VARIABLE_RUNTIME_ACCESS,
153 sizeof(os_indications_supported),
154 &os_indications_supported));
155 if (ret != EFI_SUCCESS)
156 goto out;
157
Heinrich Schuchardt76be6872020-02-19 20:48:49 +0100158 /* Initialize system table */
159 ret = efi_initialize_system_table();
AKASHI Takahiroe771b4b2019-06-05 13:21:38 +0900160 if (ret != EFI_SUCCESS)
161 goto out;
162
AKASHI Takahirod0f07942020-04-14 11:51:45 +0900163 /* Secure boot */
164 ret = efi_init_secure_boot();
165 if (ret != EFI_SUCCESS)
166 goto out;
167
Heinrich Schuchardt76be6872020-02-19 20:48:49 +0100168 /* Indicate supported runtime services */
169 ret = efi_init_runtime_supported();
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100170 if (ret != EFI_SUCCESS)
171 goto out;
172
173 /* Initialize root node */
174 ret = efi_root_node_register();
175 if (ret != EFI_SUCCESS)
176 goto out;
177
178 /* Initialize EFI driver uclass */
179 ret = efi_driver_init();
180 if (ret != EFI_SUCCESS)
181 goto out;
182
183 ret = efi_console_register();
184 if (ret != EFI_SUCCESS)
185 goto out;
186#ifdef CONFIG_PARTITIONS
187 ret = efi_disk_register();
188 if (ret != EFI_SUCCESS)
189 goto out;
190#endif
191#if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
192 ret = efi_gop_register();
193 if (ret != EFI_SUCCESS)
194 goto out;
195#endif
Ilias Apalodimasec80b472020-02-21 09:55:45 +0200196#ifdef CONFIG_EFI_LOAD_FILE2_INITRD
197 ret = efi_initrd_register();
198 if (ret != EFI_SUCCESS)
199 goto out;
200#endif
AKASHI Takahiro056b45b2018-12-30 15:16:55 +0100201#ifdef CONFIG_NET
202 ret = efi_net_register();
203 if (ret != EFI_SUCCESS)
204 goto out;
205#endif
206#ifdef CONFIG_GENERATE_ACPI_TABLE
207 ret = efi_acpi_register();
208 if (ret != EFI_SUCCESS)
209 goto out;
210#endif
211#ifdef CONFIG_GENERATE_SMBIOS_TABLE
212 ret = efi_smbios_register();
213 if (ret != EFI_SUCCESS)
214 goto out;
215#endif
216 ret = efi_watchdog_register();
217 if (ret != EFI_SUCCESS)
218 goto out;
219
220 /* Initialize EFI runtime services */
221 ret = efi_reset_system_init();
222 if (ret != EFI_SUCCESS)
223 goto out;
224
225out:
226 efi_obj_list_initialized = ret;
227 return ret;
228}