AKASHI Takahiro | 7e0badb | 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 | aea03d6 | 2019-05-04 11:47:48 +0200 | [diff] [blame] | 9 | #include <bootm.h> |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 10 | #include <efi_loader.h> |
Heinrich Schuchardt | ee3565f | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 11 | #include <efi_variable.h> |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 12 | |
| 13 | #define OBJ_LIST_NOT_INITIALIZED 1 |
| 14 | |
Heinrich Schuchardt | de9bdc6 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 15 | efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED; |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 16 | |
Heinrich Schuchardt | aea03d6 | 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 | 3700d57 | 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 | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 34 | { |
Heinrich Schuchardt | 3700d57 | 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; |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 39 | |
| 40 | /* |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 41 | * Variable PlatformLangCodes defines the language codes that the |
| 42 | * machine can support. |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 43 | */ |
Heinrich Schuchardt | ee3565f | 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 | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 51 | if (ret != EFI_SUCCESS) |
| 52 | goto out; |
| 53 | |
| 54 | /* |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 55 | * Variable PlatformLang defines the language that the machine has been |
| 56 | * configured for. |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 57 | */ |
Heinrich Schuchardt | ee3565f | 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 | 3700d57 | 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 | ee3565f | 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 | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 81 | out: |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 82 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 83 | printf("EFI: cannot initialize platform language settings\n"); |
| 84 | return ret; |
| 85 | } |
| 86 | |
AKASHI Takahiro | 8d494f6 | 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 | e2c43da | 2020-05-03 16:29:00 +0200 | [diff] [blame] | 91 | * Return: status code |
AKASHI Takahiro | 8d494f6 | 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 | ee3565f | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 101 | ret = efi_set_variable_int(L"SignatureSupport", |
| 102 | &efi_global_variable_guid, |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 103 | EFI_VARIABLE_READ_ONLY | |
Heinrich Schuchardt | ee3565f | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 104 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 105 | EFI_VARIABLE_RUNTIME_ACCESS, |
Heinrich Schuchardt | ee3565f | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 106 | sizeof(signature_types), |
| 107 | &signature_types, false); |
AKASHI Takahiro | 8d494f6 | 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 | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 120 | /** |
AKASHI Takahiro | 473d9b3 | 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 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 158 | if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK)) |
| 159 | os_indications_supported |= |
| 160 | EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED; |
| 161 | |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 162 | if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT)) |
| 163 | os_indications_supported |= |
| 164 | EFI_OS_INDICATIONS_FMP_CAPSULE_SUPPORTED; |
| 165 | |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 166 | return efi_set_variable_int(L"OsIndicationsSupported", |
| 167 | &efi_global_variable_guid, |
| 168 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 169 | EFI_VARIABLE_RUNTIME_ACCESS | |
| 170 | EFI_VARIABLE_READ_ONLY, |
| 171 | sizeof(os_indications_supported), |
| 172 | &os_indications_supported, false); |
| 173 | } |
| 174 | |
| 175 | /** |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 176 | * efi_init_obj_list() - Initialize and populate EFI object list |
| 177 | * |
| 178 | * Return: status code |
| 179 | */ |
| 180 | efi_status_t efi_init_obj_list(void) |
| 181 | { |
| 182 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 183 | |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 184 | /* Initialize once only */ |
| 185 | if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED) |
| 186 | return efi_obj_list_initialized; |
| 187 | |
Heinrich Schuchardt | aea03d6 | 2019-05-04 11:47:48 +0200 | [diff] [blame] | 188 | /* Allow unaligned memory access */ |
| 189 | allow_unaligned(); |
| 190 | |
| 191 | /* On ARM switch from EL3 or secure mode to EL2 or non-secure mode */ |
| 192 | switch_to_non_secure_mode(); |
| 193 | |
Heinrich Schuchardt | f667a18 | 2020-05-20 21:27:29 +0200 | [diff] [blame] | 194 | /* Initialize root node */ |
| 195 | ret = efi_root_node_register(); |
| 196 | if (ret != EFI_SUCCESS) |
| 197 | goto out; |
| 198 | |
AKASHI Takahiro | 9b31c63 | 2020-06-09 14:09:31 +0900 | [diff] [blame] | 199 | ret = efi_console_register(); |
| 200 | if (ret != EFI_SUCCESS) |
| 201 | goto out; |
| 202 | |
Heinrich Schuchardt | eef4eee | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 203 | #ifdef CONFIG_PARTITIONS |
| 204 | ret = efi_disk_register(); |
| 205 | if (ret != EFI_SUCCESS) |
| 206 | goto out; |
| 207 | #endif |
Heinrich Schuchardt | c63f68f | 2020-09-25 12:50:19 +0200 | [diff] [blame] | 208 | if (IS_ENABLED(CONFIG_EFI_RNG_PROTOCOL)) { |
| 209 | ret = efi_rng_register(); |
| 210 | if (ret != EFI_SUCCESS) |
| 211 | goto out; |
| 212 | } |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 213 | |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 214 | /* Initialize variable services */ |
| 215 | ret = efi_init_variables(); |
| 216 | if (ret != EFI_SUCCESS) |
| 217 | goto out; |
| 218 | |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 219 | /* Define supported languages */ |
| 220 | ret = efi_init_platform_lang(); |
| 221 | if (ret != EFI_SUCCESS) |
| 222 | goto out; |
| 223 | |
AKASHI Takahiro | 3a48925 | 2019-04-24 15:30:38 +0900 | [diff] [blame] | 224 | /* Indicate supported features */ |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 225 | ret = efi_init_os_indications(); |
AKASHI Takahiro | 3a48925 | 2019-04-24 15:30:38 +0900 | [diff] [blame] | 226 | if (ret != EFI_SUCCESS) |
| 227 | goto out; |
| 228 | |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 229 | /* Initialize system table */ |
| 230 | ret = efi_initialize_system_table(); |
AKASHI Takahiro | 3240184 | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 231 | if (ret != EFI_SUCCESS) |
| 232 | goto out; |
| 233 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 234 | if (IS_ENABLED(CONFIG_EFI_TCG2_PROTOCOL)) { |
| 235 | ret = efi_tcg2_register(); |
| 236 | if (ret != EFI_SUCCESS) |
| 237 | goto out; |
| 238 | } |
| 239 | |
AKASHI Takahiro | 8d494f6 | 2020-04-14 11:51:45 +0900 | [diff] [blame] | 240 | /* Secure boot */ |
| 241 | ret = efi_init_secure_boot(); |
| 242 | if (ret != EFI_SUCCESS) |
| 243 | goto out; |
| 244 | |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 245 | /* Indicate supported runtime services */ |
| 246 | ret = efi_init_runtime_supported(); |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 247 | if (ret != EFI_SUCCESS) |
| 248 | goto out; |
| 249 | |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 250 | /* Initialize EFI driver uclass */ |
| 251 | ret = efi_driver_init(); |
| 252 | if (ret != EFI_SUCCESS) |
| 253 | goto out; |
| 254 | |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 255 | #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO) |
| 256 | ret = efi_gop_register(); |
| 257 | if (ret != EFI_SUCCESS) |
| 258 | goto out; |
| 259 | #endif |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 260 | #ifdef CONFIG_NET |
| 261 | ret = efi_net_register(); |
| 262 | if (ret != EFI_SUCCESS) |
| 263 | goto out; |
| 264 | #endif |
| 265 | #ifdef CONFIG_GENERATE_ACPI_TABLE |
| 266 | ret = efi_acpi_register(); |
| 267 | if (ret != EFI_SUCCESS) |
| 268 | goto out; |
| 269 | #endif |
| 270 | #ifdef CONFIG_GENERATE_SMBIOS_TABLE |
| 271 | ret = efi_smbios_register(); |
| 272 | if (ret != EFI_SUCCESS) |
| 273 | goto out; |
| 274 | #endif |
| 275 | ret = efi_watchdog_register(); |
| 276 | if (ret != EFI_SUCCESS) |
| 277 | goto out; |
| 278 | |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 279 | ret = efi_init_capsule(); |
| 280 | if (ret != EFI_SUCCESS) |
| 281 | goto out; |
| 282 | |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 283 | /* Initialize EFI runtime services */ |
| 284 | ret = efi_reset_system_init(); |
| 285 | if (ret != EFI_SUCCESS) |
| 286 | goto out; |
| 287 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 288 | /* Execute capsules after reboot */ |
| 289 | if (IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK) && |
| 290 | !IS_ENABLED(CONFIG_EFI_CAPSULE_ON_DISK_EARLY)) |
| 291 | ret = efi_launch_capsules(); |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 292 | out: |
| 293 | efi_obj_list_initialized = ret; |
| 294 | return ret; |
| 295 | } |