blob: e206b60bb82c7b1fbbba35e80ecb34df712bbe4f [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,
103 EFI_VARIABLE_BOOTSERVICE_ACCESS |
104 EFI_VARIABLE_RUNTIME_ACCESS |
105 EFI_VARIABLE_READ_ONLY,
106 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/**
121 * efi_init_obj_list() - Initialize and populate EFI object list
122 *
123 * Return: status code
124 */
125efi_status_t efi_init_obj_list(void)
126{
AKASHI Takahiro3a489252019-04-24 15:30:38 +0900127 u64 os_indications_supported = 0; /* None */
Heinrich Schuchardt3700d572019-04-11 07:34:24 +0200128 efi_status_t ret = EFI_SUCCESS;
Heinrich Schuchardtf7fe45b2019-04-05 02:45:21 +0200129
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100130 /* Initialize once only */
131 if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
132 return efi_obj_list_initialized;
133
Heinrich Schuchardtaea03d62019-05-04 11:47:48 +0200134 /* Allow unaligned memory access */
135 allow_unaligned();
136
137 /* On ARM switch from EL3 or secure mode to EL2 or non-secure mode */
138 switch_to_non_secure_mode();
139
Heinrich Schuchardtf667a182020-05-20 21:27:29 +0200140 /* Initialize root node */
141 ret = efi_root_node_register();
142 if (ret != EFI_SUCCESS)
143 goto out;
144
AKASHI Takahiro9b31c632020-06-09 14:09:31 +0900145 ret = efi_console_register();
146 if (ret != EFI_SUCCESS)
147 goto out;
148
Heinrich Schuchardteef4eee2020-03-19 18:21:58 +0000149#ifdef CONFIG_PARTITIONS
150 ret = efi_disk_register();
151 if (ret != EFI_SUCCESS)
152 goto out;
153#endif
Heinrich Schuchardtc63f68f2020-09-25 12:50:19 +0200154 if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) {
155 ret = efi_rng_register();
156 if (ret != EFI_SUCCESS)
157 goto out;
158 }
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200159
160 if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
161 ret = efi_tcg2_register();
162 if (ret != EFI_SUCCESS)
163 goto out;
164 }
165
Heinrich Schuchardtcf3b1182019-06-20 13:52:16 +0200166 /* Initialize variable services */
167 ret = efi_init_variables();
168 if (ret != EFI_SUCCESS)
169 goto out;
170
Heinrich Schuchardt3700d572019-04-11 07:34:24 +0200171 /* Define supported languages */
172 ret = efi_init_platform_lang();
173 if (ret != EFI_SUCCESS)
174 goto out;
175
AKASHI Takahiro3a489252019-04-24 15:30:38 +0900176 /* Indicate supported features */
Heinrich Schuchardtee3565f2020-06-24 19:38:29 +0200177 ret = efi_set_variable_int(L"OsIndicationsSupported",
178 &efi_global_variable_guid,
179 EFI_VARIABLE_BOOTSERVICE_ACCESS |
180 EFI_VARIABLE_RUNTIME_ACCESS |
181 EFI_VARIABLE_READ_ONLY,
182 sizeof(os_indications_supported),
183 &os_indications_supported, false);
AKASHI Takahiro3a489252019-04-24 15:30:38 +0900184 if (ret != EFI_SUCCESS)
185 goto out;
186
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100187 /* Initialize system table */
188 ret = efi_initialize_system_table();
AKASHI Takahiro32401842019-06-05 13:21:38 +0900189 if (ret != EFI_SUCCESS)
190 goto out;
191
AKASHI Takahiro8d494f62020-04-14 11:51:45 +0900192 /* Secure boot */
193 ret = efi_init_secure_boot();
194 if (ret != EFI_SUCCESS)
195 goto out;
196
Heinrich Schuchardt956eff32020-02-19 20:48:49 +0100197 /* Indicate supported runtime services */
198 ret = efi_init_runtime_supported();
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100199 if (ret != EFI_SUCCESS)
200 goto out;
201
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100202 /* Initialize EFI driver uclass */
203 ret = efi_driver_init();
204 if (ret != EFI_SUCCESS)
205 goto out;
206
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100207#if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO)
208 ret = efi_gop_register();
209 if (ret != EFI_SUCCESS)
210 goto out;
211#endif
Ilias Apalodimas3510ba72020-02-21 09:55:45 +0200212#ifdef CONFIG_EFI_LOAD_FILE2_INITRD
213 ret = efi_initrd_register();
214 if (ret != EFI_SUCCESS)
215 goto out;
216#endif
AKASHI Takahiro7e0badb2018-12-30 15:16:55 +0100217#ifdef CONFIG_NET
218 ret = efi_net_register();
219 if (ret != EFI_SUCCESS)
220 goto out;
221#endif
222#ifdef CONFIG_GENERATE_ACPI_TABLE
223 ret = efi_acpi_register();
224 if (ret != EFI_SUCCESS)
225 goto out;
226#endif
227#ifdef CONFIG_GENERATE_SMBIOS_TABLE
228 ret = efi_smbios_register();
229 if (ret != EFI_SUCCESS)
230 goto out;
231#endif
232 ret = efi_watchdog_register();
233 if (ret != EFI_SUCCESS)
234 goto out;
235
236 /* Initialize EFI runtime services */
237 ret = efi_reset_system_init();
238 if (ret != EFI_SUCCESS)
239 goto out;
240
241out:
242 efi_obj_list_initialized = ret;
243 return ret;
244}