AKASHI Takahiro | 056b45b | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 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 | #include <common.h> |
Heinrich Schuchardt | 52cbac9 | 2019-05-04 11:47:48 +0200 | [diff] [blame] | 9 | #include <bootm.h> |
AKASHI Takahiro | 056b45b | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 10 | #include <efi_loader.h> |
Heinrich Schuchardt | dc90e5c | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 11 | #include <efi_variable.h> |
AKASHI Takahiro | 056b45b | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 12 | |
| 13 | #define OBJ_LIST_NOT_INITIALIZED 1 |
| 14 | |
Heinrich Schuchardt | 15b1bf1 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 15 | efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED; |
AKASHI Takahiro | 056b45b | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 16 | |
Heinrich Schuchardt | 52cbac9 | 2019-05-04 11:47:48 +0200 | [diff] [blame] | 17 | /* |
| 18 | * Allow unaligned memory access. |
| 19 | * |
| 20 | * This routine is overridden by architectures providing this feature. |
| 21 | */ |
| 22 | void __weak allow_unaligned(void) |
| 23 | { |
| 24 | } |
| 25 | |
Heinrich Schuchardt | 07805f2 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 26 | /** |
| 27 | * efi_init_platform_lang() - define supported languages |
| 28 | * |
| 29 | * Set the PlatformLangCodes and PlatformLang variables. |
| 30 | * |
| 31 | * Return: status code |
| 32 | */ |
| 33 | static efi_status_t efi_init_platform_lang(void) |
AKASHI Takahiro | 056b45b | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 34 | { |
Heinrich Schuchardt | 07805f2 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 35 | efi_status_t ret; |
| 36 | efi_uintn_t data_size = 0; |
| 37 | char *lang = CONFIG_EFI_PLATFORM_LANG_CODES; |
| 38 | char *pos; |
Heinrich Schuchardt | 734d325 | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Variable PlatformLangCodes defines the language codes that the |
| 42 | * machine can support. |
| 43 | */ |
Heinrich Schuchardt | dc90e5c | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 44 | 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 Schuchardt | 734d325 | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 51 | if (ret != EFI_SUCCESS) |
| 52 | goto out; |
| 53 | |
Heinrich Schuchardt | 07805f2 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 54 | /* |
| 55 | * Variable PlatformLang defines the language that the machine has been |
| 56 | * configured for. |
| 57 | */ |
Heinrich Schuchardt | dc90e5c | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 58 | ret = efi_get_variable_int(L"PlatformLang", |
| 59 | &efi_global_variable_guid, |
| 60 | NULL, &data_size, &pos, NULL); |
Heinrich Schuchardt | 07805f2 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 61 | 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 Schuchardt | dc90e5c | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 75 | 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 Schuchardt | 07805f2 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 81 | out: |
| 82 | if (ret != EFI_SUCCESS) |
| 83 | printf("EFI: cannot initialize platform language settings\n"); |
| 84 | return ret; |
| 85 | } |
| 86 | |
AKASHI Takahiro | d0f0794 | 2020-04-14 11:51:45 +0900 | [diff] [blame] | 87 | #ifdef CONFIG_EFI_SECURE_BOOT |
| 88 | /** |
| 89 | * efi_init_secure_boot - initialize secure boot state |
| 90 | * |
Heinrich Schuchardt | 30f92ce | 2020-05-03 16:29:00 +0200 | [diff] [blame] | 91 | * Return: status code |
AKASHI Takahiro | d0f0794 | 2020-04-14 11:51:45 +0900 | [diff] [blame] | 92 | */ |
| 93 | static 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 Schuchardt | dc90e5c | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 101 | ret = efi_set_variable_int(L"SignatureSupport", |
| 102 | &efi_global_variable_guid, |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame^] | 103 | EFI_VARIABLE_READ_ONLY | |
Heinrich Schuchardt | dc90e5c | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 104 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame^] | 105 | EFI_VARIABLE_RUNTIME_ACCESS, |
Heinrich Schuchardt | dc90e5c | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 106 | sizeof(signature_types), |
| 107 | &signature_types, false); |
AKASHI Takahiro | d0f0794 | 2020-04-14 11:51:45 +0900 | [diff] [blame] | 108 | if (ret != EFI_SUCCESS) |
| 109 | printf("EFI: cannot initialize SignatureSupport variable\n"); |
| 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | #else |
| 114 | static efi_status_t efi_init_secure_boot(void) |
| 115 | { |
| 116 | return EFI_SUCCESS; |
| 117 | } |
| 118 | #endif /* CONFIG_EFI_SECURE_BOOT */ |
| 119 | |
Heinrich Schuchardt | 07805f2 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 120 | /** |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame^] | 121 | * efi_init_capsule - initialize capsule update state |
| 122 | * |
| 123 | * Return: status code |
| 124 | */ |
| 125 | static 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 | */ |
| 150 | static 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 | |
| 158 | return efi_set_variable_int(L"OsIndicationsSupported", |
| 159 | &efi_global_variable_guid, |
| 160 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 161 | EFI_VARIABLE_RUNTIME_ACCESS | |
| 162 | EFI_VARIABLE_READ_ONLY, |
| 163 | sizeof(os_indications_supported), |
| 164 | &os_indications_supported, false); |
| 165 | } |
| 166 | |
| 167 | /** |
Heinrich Schuchardt | 07805f2 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 168 | * efi_init_obj_list() - Initialize and populate EFI object list |
| 169 | * |
| 170 | * Return: status code |
| 171 | */ |
| 172 | efi_status_t efi_init_obj_list(void) |
| 173 | { |
| 174 | efi_status_t ret = EFI_SUCCESS; |
| 175 | |
AKASHI Takahiro | 056b45b | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 176 | /* Initialize once only */ |
| 177 | if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED) |
| 178 | return efi_obj_list_initialized; |
| 179 | |
Heinrich Schuchardt | 52cbac9 | 2019-05-04 11:47:48 +0200 | [diff] [blame] | 180 | /* Allow unaligned memory access */ |
| 181 | allow_unaligned(); |
| 182 | |
| 183 | /* On ARM switch from EL3 or secure mode to EL2 or non-secure mode */ |
| 184 | switch_to_non_secure_mode(); |
| 185 | |
Heinrich Schuchardt | c067cef | 2020-05-20 21:27:29 +0200 | [diff] [blame] | 186 | /* Initialize root node */ |
| 187 | ret = efi_root_node_register(); |
| 188 | if (ret != EFI_SUCCESS) |
| 189 | goto out; |
| 190 | |
AKASHI Takahiro | f7a963c | 2020-06-09 14:09:31 +0900 | [diff] [blame] | 191 | ret = efi_console_register(); |
| 192 | if (ret != EFI_SUCCESS) |
| 193 | goto out; |
| 194 | |
Heinrich Schuchardt | 16ad946 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 195 | #ifdef CONFIG_PARTITIONS |
| 196 | ret = efi_disk_register(); |
| 197 | if (ret != EFI_SUCCESS) |
| 198 | goto out; |
| 199 | #endif |
Heinrich Schuchardt | b59c13d | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 200 | if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) { |
| 201 | ret = efi_rng_register(); |
| 202 | if (ret != EFI_SUCCESS) |
| 203 | goto out; |
| 204 | } |
Ilias Apalodimas | c1c0210 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 205 | |
Heinrich Schuchardt | 8819209 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 206 | /* Initialize variable services */ |
| 207 | ret = efi_init_variables(); |
| 208 | if (ret != EFI_SUCCESS) |
| 209 | goto out; |
| 210 | |
Heinrich Schuchardt | 07805f2 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 211 | /* Define supported languages */ |
| 212 | ret = efi_init_platform_lang(); |
| 213 | if (ret != EFI_SUCCESS) |
| 214 | goto out; |
| 215 | |
AKASHI Takahiro | d40e05a | 2019-04-24 15:30:38 +0900 | [diff] [blame] | 216 | /* Indicate supported features */ |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame^] | 217 | ret = efi_init_os_indications(); |
AKASHI Takahiro | d40e05a | 2019-04-24 15:30:38 +0900 | [diff] [blame] | 218 | if (ret != EFI_SUCCESS) |
| 219 | goto out; |
| 220 | |
Heinrich Schuchardt | 76be687 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 221 | /* Initialize system table */ |
| 222 | ret = efi_initialize_system_table(); |
AKASHI Takahiro | e771b4b | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 223 | if (ret != EFI_SUCCESS) |
| 224 | goto out; |
| 225 | |
Ilias Apalodimas | c8d0fd5 | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 226 | if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) { |
| 227 | ret = efi_tcg2_register(); |
| 228 | if (ret != EFI_SUCCESS) |
| 229 | goto out; |
| 230 | } |
| 231 | |
AKASHI Takahiro | d0f0794 | 2020-04-14 11:51:45 +0900 | [diff] [blame] | 232 | /* Secure boot */ |
| 233 | ret = efi_init_secure_boot(); |
| 234 | if (ret != EFI_SUCCESS) |
| 235 | goto out; |
| 236 | |
Heinrich Schuchardt | 76be687 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 237 | /* Indicate supported runtime services */ |
| 238 | ret = efi_init_runtime_supported(); |
AKASHI Takahiro | 056b45b | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 239 | if (ret != EFI_SUCCESS) |
| 240 | goto out; |
| 241 | |
AKASHI Takahiro | 056b45b | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 242 | /* Initialize EFI driver uclass */ |
| 243 | ret = efi_driver_init(); |
| 244 | if (ret != EFI_SUCCESS) |
| 245 | goto out; |
| 246 | |
AKASHI Takahiro | 056b45b | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 247 | #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO) |
| 248 | ret = efi_gop_register(); |
| 249 | if (ret != EFI_SUCCESS) |
| 250 | goto out; |
| 251 | #endif |
Ilias Apalodimas | ec80b47 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 252 | #ifdef CONFIG_EFI_LOAD_FILE2_INITRD |
| 253 | ret = efi_initrd_register(); |
| 254 | if (ret != EFI_SUCCESS) |
| 255 | goto out; |
| 256 | #endif |
AKASHI Takahiro | 056b45b | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 257 | #ifdef CONFIG_NET |
| 258 | ret = efi_net_register(); |
| 259 | if (ret != EFI_SUCCESS) |
| 260 | goto out; |
| 261 | #endif |
| 262 | #ifdef CONFIG_GENERATE_ACPI_TABLE |
| 263 | ret = efi_acpi_register(); |
| 264 | if (ret != EFI_SUCCESS) |
| 265 | goto out; |
| 266 | #endif |
| 267 | #ifdef CONFIG_GENERATE_SMBIOS_TABLE |
| 268 | ret = efi_smbios_register(); |
| 269 | if (ret != EFI_SUCCESS) |
| 270 | goto out; |
| 271 | #endif |
| 272 | ret = efi_watchdog_register(); |
| 273 | if (ret != EFI_SUCCESS) |
| 274 | goto out; |
| 275 | |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame^] | 276 | ret = efi_init_capsule(); |
| 277 | if (ret != EFI_SUCCESS) |
| 278 | goto out; |
| 279 | |
AKASHI Takahiro | 056b45b | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 280 | /* Initialize EFI runtime services */ |
| 281 | ret = efi_reset_system_init(); |
| 282 | if (ret != EFI_SUCCESS) |
| 283 | goto out; |
| 284 | |
| 285 | out: |
| 286 | efi_obj_list_initialized = ret; |
| 287 | return ret; |
| 288 | } |