efi: Drop old LCD code
[platform/kernel/u-boot.git] / lib / efi_loader / efi_setup.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  EFI setup code
4  *
5  *  Copyright (c) 2016-2018 Alexander Graf et al.
6  */
7
8 #define LOG_CATEGORY LOGC_EFI
9
10 #include <common.h>
11 #include <efi_loader.h>
12 #include <efi_variable.h>
13 #include <log.h>
14
15 #define OBJ_LIST_NOT_INITIALIZED 1
16
17 efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED;
18
19 /*
20  * Allow unaligned memory access.
21  *
22  * This routine is overridden by architectures providing this feature.
23  */
24 void __weak allow_unaligned(void)
25 {
26 }
27
28 /**
29  * efi_init_platform_lang() - define supported languages
30  *
31  * Set the PlatformLangCodes and PlatformLang variables.
32  *
33  * Return:      status code
34  */
35 static efi_status_t efi_init_platform_lang(void)
36 {
37         efi_status_t ret;
38         efi_uintn_t data_size = 0;
39         char *lang = CONFIG_EFI_PLATFORM_LANG_CODES;
40         char *pos;
41
42         /*
43          * Variable PlatformLangCodes defines the language codes that the
44          * machine can support.
45          */
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)
54                 goto out;
55
56         /*
57          * Variable PlatformLang defines the language that the machine has been
58          * configured for.
59          */
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. */
65                 ret = EFI_SUCCESS;
66                 goto out;
67         }
68
69         /*
70          * The list of supported languages is semicolon separated. Use the first
71          * language to initialize PlatformLang.
72          */
73         pos = strchr(lang, ';');
74         if (pos)
75                 *pos = 0;
76
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);
83 out:
84         if (ret != EFI_SUCCESS)
85                 printf("EFI: cannot initialize platform language settings\n");
86         return ret;
87 }
88
89 #ifdef CONFIG_EFI_SECURE_BOOT
90 /**
91  * efi_init_secure_boot - initialize secure boot state
92  *
93  * Return:      status code
94  */
95 static efi_status_t efi_init_secure_boot(void)
96 {
97         efi_guid_t signature_types[] = {
98                 EFI_CERT_SHA256_GUID,
99                 EFI_CERT_X509_GUID,
100         };
101         efi_status_t ret;
102
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");
112
113         return ret;
114 }
115 #else
116 static efi_status_t efi_init_secure_boot(void)
117 {
118         return EFI_SUCCESS;
119 }
120 #endif /* CONFIG_EFI_SECURE_BOOT */
121
122 /**
123  * efi_init_capsule - initialize capsule update state
124  *
125  * Return:      status code
126  */
127 static efi_status_t efi_init_capsule(void)
128 {
129         efi_status_t ret = EFI_SUCCESS;
130
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");
140         }
141
142         return ret;
143 }
144
145 /**
146  * efi_init_os_indications() - indicate supported features for OS requests
147  *
148  * Set the OsIndicationsSupported variable.
149  *
150  * Return:      status code
151  */
152 static efi_status_t efi_init_os_indications(void)
153 {
154         u64 os_indications_supported = 0;
155
156         if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT))
157                 os_indications_supported |=
158                         EFI_OS_INDICATIONS_CAPSULE_RESULT_VAR_SUPPORTED;
159
160         if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK))
161                 os_indications_supported |=
162                         EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
163
164         if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT))
165                 os_indications_supported |=
166                         EFI_OS_INDICATIONS_FMP_CAPSULE_SUPPORTED;
167
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);
175 }
176
177 /**
178  * __efi_init_early() - handle initialization at early stage
179  *
180  * This function is called in efi_init_obj_list() only if
181  * !CONFIG_EFI_SETUP_EARLY.
182  *
183  * Return:      status code
184  */
185 static efi_status_t __efi_init_early(void)
186 {
187         efi_status_t ret = EFI_SUCCESS;
188
189         /* Allow unaligned memory access */
190         allow_unaligned();
191
192         /* Initialize root node */
193         ret = efi_root_node_register();
194         if (ret != EFI_SUCCESS)
195                 goto out;
196
197         ret = efi_console_register();
198         if (ret != EFI_SUCCESS)
199                 goto out;
200
201         /* Initialize EFI driver uclass */
202         ret = efi_driver_init();
203 out:
204         return ret;
205 }
206
207 /**
208  * efi_init_early() - handle initialization at early stage
209  *
210  * external version of __efi_init_early(); expected to be called in
211  * board_init_r().
212  *
213  * Return:      status code
214  */
215 int efi_init_early(void)
216 {
217         efi_status_t ret;
218
219         ret = __efi_init_early();
220         if (ret != EFI_SUCCESS) {
221                 /* never re-init UEFI subsystem */
222                 efi_obj_list_initialized = ret;
223                 return -1;
224         }
225         return 0;
226 }
227
228 /**
229  * efi_init_obj_list() - Initialize and populate EFI object list
230  *
231  * Return:      status code
232  */
233 efi_status_t efi_init_obj_list(void)
234 {
235         efi_status_t ret = EFI_SUCCESS;
236
237         /* Initialize once only */
238         if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED)
239                 return efi_obj_list_initialized;
240
241         if (!IS_ENABLED(CONFIG_EFI_SETUP_EARLY)) {
242                 ret = __efi_init_early();
243                 if (ret != EFI_SUCCESS)
244                         goto out;
245         }
246
247         /* Set up console modes */
248         efi_setup_console_size();
249
250         /*
251          * Probe block devices to find the ESP.
252          * efi_disks_register() must be called before efi_init_variables().
253          */
254         ret = efi_disks_register();
255         if (ret != EFI_SUCCESS)
256                 goto out;
257
258         /* Initialize variable services */
259         ret = efi_init_variables();
260         if (ret != EFI_SUCCESS)
261                 goto out;
262
263         /* Define supported languages */
264         ret = efi_init_platform_lang();
265         if (ret != EFI_SUCCESS)
266                 goto out;
267
268         /* Indicate supported features */
269         ret = efi_init_os_indications();
270         if (ret != EFI_SUCCESS)
271                 goto out;
272
273         /* Initialize system table */
274         ret = efi_initialize_system_table();
275         if (ret != EFI_SUCCESS)
276                 goto out;
277
278         if (IS_ENABLED(CONFIG_EFI_ECPT)) {
279                 ret = efi_ecpt_register();
280                 if (ret != EFI_SUCCESS)
281                         goto out;
282         }
283
284         if (IS_ENABLED(CONFIG_EFI_ESRT)) {
285                 ret = efi_esrt_register();
286                 if (ret != EFI_SUCCESS)
287                         goto out;
288         }
289
290         if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) {
291                 ret = efi_tcg2_register();
292                 if (ret != EFI_SUCCESS)
293                         goto out;
294
295                 ret = efi_tcg2_do_initial_measurement();
296                 if (ret == EFI_SECURITY_VIOLATION)
297                         goto out;
298         }
299
300         /* Install EFI_RNG_PROTOCOL */
301         if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) {
302                 ret = efi_rng_register();
303                 if (ret != EFI_SUCCESS)
304                         goto out;
305         }
306
307         if (IS_ENABLED(CONFIG_EFI_RISCV_BOOT_PROTOCOL)) {
308                 ret = efi_riscv_register();
309                 if (ret != EFI_SUCCESS)
310                         goto out;
311         }
312
313         /* Secure boot */
314         ret = efi_init_secure_boot();
315         if (ret != EFI_SUCCESS)
316                 goto out;
317
318         /* Indicate supported runtime services */
319         ret = efi_init_runtime_supported();
320         if (ret != EFI_SUCCESS)
321                 goto out;
322
323         if (IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)) {
324                 ret = efi_load_capsule_drivers();
325                 if (ret != EFI_SUCCESS)
326                         goto out;
327         }
328
329         if (IS_ENABLED(CONFIG_DM_VIDEO)) {
330                 ret = efi_gop_register();
331                 if (ret != EFI_SUCCESS)
332                         goto out;
333         }
334 #ifdef CONFIG_NET
335         ret = efi_net_register();
336         if (ret != EFI_SUCCESS)
337                 goto out;
338 #endif
339 #ifdef CONFIG_GENERATE_ACPI_TABLE
340         ret = efi_acpi_register();
341         if (ret != EFI_SUCCESS)
342                 goto out;
343 #endif
344 #ifdef CONFIG_GENERATE_SMBIOS_TABLE
345         ret = efi_smbios_register();
346         if (ret != EFI_SUCCESS)
347                 goto out;
348 #endif
349         ret = efi_watchdog_register();
350         if (ret != EFI_SUCCESS)
351                 goto out;
352
353         ret = efi_init_capsule();
354         if (ret != EFI_SUCCESS)
355                 goto out;
356
357         /* Initialize EFI runtime services */
358         ret = efi_reset_system_init();
359         if (ret != EFI_SUCCESS)
360                 goto out;
361
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();
366 out:
367         efi_obj_list_initialized = ret;
368         return ret;
369 }