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, |
| 103 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 104 | EFI_VARIABLE_RUNTIME_ACCESS | |
| 105 | EFI_VARIABLE_READ_ONLY, |
| 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 | /** |
| 121 | * efi_init_obj_list() - Initialize and populate EFI object list |
| 122 | * |
| 123 | * Return: status code |
| 124 | */ |
| 125 | efi_status_t efi_init_obj_list(void) |
| 126 | { |
AKASHI Takahiro | 3a48925 | 2019-04-24 15:30:38 +0900 | [diff] [blame] | 127 | u64 os_indications_supported = 0; /* None */ |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 128 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 129 | |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 130 | /* Initialize once only */ |
| 131 | if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED) |
| 132 | return efi_obj_list_initialized; |
| 133 | |
Heinrich Schuchardt | aea03d6 | 2019-05-04 11:47:48 +0200 | [diff] [blame] | 134 | /* 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 Schuchardt | f667a18 | 2020-05-20 21:27:29 +0200 | [diff] [blame] | 140 | /* Initialize root node */ |
| 141 | ret = efi_root_node_register(); |
| 142 | if (ret != EFI_SUCCESS) |
| 143 | goto out; |
| 144 | |
AKASHI Takahiro | 9b31c63 | 2020-06-09 14:09:31 +0900 | [diff] [blame] | 145 | ret = efi_console_register(); |
| 146 | if (ret != EFI_SUCCESS) |
| 147 | goto out; |
| 148 | |
Heinrich Schuchardt | eef4eee | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 149 | #ifdef CONFIG_PARTITIONS |
| 150 | ret = efi_disk_register(); |
| 151 | if (ret != EFI_SUCCESS) |
| 152 | goto out; |
| 153 | #endif |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 154 | /* Initialize variable services */ |
| 155 | ret = efi_init_variables(); |
| 156 | if (ret != EFI_SUCCESS) |
| 157 | goto out; |
| 158 | |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 159 | /* Define supported languages */ |
| 160 | ret = efi_init_platform_lang(); |
| 161 | if (ret != EFI_SUCCESS) |
| 162 | goto out; |
| 163 | |
AKASHI Takahiro | 3a48925 | 2019-04-24 15:30:38 +0900 | [diff] [blame] | 164 | /* Indicate supported features */ |
Heinrich Schuchardt | ee3565f | 2020-06-24 19:38:29 +0200 | [diff] [blame] | 165 | ret = efi_set_variable_int(L"OsIndicationsSupported", |
| 166 | &efi_global_variable_guid, |
| 167 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 168 | EFI_VARIABLE_RUNTIME_ACCESS | |
| 169 | EFI_VARIABLE_READ_ONLY, |
| 170 | sizeof(os_indications_supported), |
| 171 | &os_indications_supported, false); |
AKASHI Takahiro | 3a48925 | 2019-04-24 15:30:38 +0900 | [diff] [blame] | 172 | if (ret != EFI_SUCCESS) |
| 173 | goto out; |
| 174 | |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 175 | /* Initialize system table */ |
| 176 | ret = efi_initialize_system_table(); |
AKASHI Takahiro | 3240184 | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 177 | if (ret != EFI_SUCCESS) |
| 178 | goto out; |
| 179 | |
AKASHI Takahiro | 8d494f6 | 2020-04-14 11:51:45 +0900 | [diff] [blame] | 180 | /* Secure boot */ |
| 181 | ret = efi_init_secure_boot(); |
| 182 | if (ret != EFI_SUCCESS) |
| 183 | goto out; |
| 184 | |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 185 | /* Indicate supported runtime services */ |
| 186 | ret = efi_init_runtime_supported(); |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 187 | if (ret != EFI_SUCCESS) |
| 188 | goto out; |
| 189 | |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 190 | /* Initialize EFI driver uclass */ |
| 191 | ret = efi_driver_init(); |
| 192 | if (ret != EFI_SUCCESS) |
| 193 | goto out; |
| 194 | |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 195 | #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO) |
| 196 | ret = efi_gop_register(); |
| 197 | if (ret != EFI_SUCCESS) |
| 198 | goto out; |
| 199 | #endif |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 200 | #ifdef CONFIG_EFI_LOAD_FILE2_INITRD |
| 201 | ret = efi_initrd_register(); |
| 202 | if (ret != EFI_SUCCESS) |
| 203 | goto out; |
| 204 | #endif |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 205 | #ifdef CONFIG_NET |
| 206 | ret = efi_net_register(); |
| 207 | if (ret != EFI_SUCCESS) |
| 208 | goto out; |
| 209 | #endif |
| 210 | #ifdef CONFIG_GENERATE_ACPI_TABLE |
| 211 | ret = efi_acpi_register(); |
| 212 | if (ret != EFI_SUCCESS) |
| 213 | goto out; |
| 214 | #endif |
| 215 | #ifdef CONFIG_GENERATE_SMBIOS_TABLE |
| 216 | ret = efi_smbios_register(); |
| 217 | if (ret != EFI_SUCCESS) |
| 218 | goto out; |
| 219 | #endif |
| 220 | ret = efi_watchdog_register(); |
| 221 | if (ret != EFI_SUCCESS) |
| 222 | goto out; |
| 223 | |
| 224 | /* Initialize EFI runtime services */ |
| 225 | ret = efi_reset_system_init(); |
| 226 | if (ret != EFI_SUCCESS) |
| 227 | goto out; |
| 228 | |
| 229 | out: |
| 230 | efi_obj_list_initialized = ret; |
| 231 | return ret; |
| 232 | } |