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> |
| 11 | |
| 12 | #define OBJ_LIST_NOT_INITIALIZED 1 |
| 13 | |
Heinrich Schuchardt | de9bdc6 | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 14 | efi_status_t efi_obj_list_initialized = OBJ_LIST_NOT_INITIALIZED; |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 15 | |
Heinrich Schuchardt | aea03d6 | 2019-05-04 11:47:48 +0200 | [diff] [blame] | 16 | /* |
| 17 | * Allow unaligned memory access. |
| 18 | * |
| 19 | * This routine is overridden by architectures providing this feature. |
| 20 | */ |
| 21 | void __weak allow_unaligned(void) |
| 22 | { |
| 23 | } |
| 24 | |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 25 | /** |
| 26 | * efi_init_platform_lang() - define supported languages |
| 27 | * |
| 28 | * Set the PlatformLangCodes and PlatformLang variables. |
| 29 | * |
| 30 | * Return: status code |
| 31 | */ |
| 32 | static efi_status_t efi_init_platform_lang(void) |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 33 | { |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 34 | efi_status_t ret; |
| 35 | efi_uintn_t data_size = 0; |
| 36 | char *lang = CONFIG_EFI_PLATFORM_LANG_CODES; |
| 37 | char *pos; |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 38 | |
| 39 | /* |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 40 | * Variable PlatformLangCodes defines the language codes that the |
| 41 | * machine can support. |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 42 | */ |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 43 | ret = EFI_CALL(efi_set_variable(L"PlatformLangCodes", |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 44 | &efi_global_variable_guid, |
| 45 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 46 | EFI_VARIABLE_RUNTIME_ACCESS, |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 47 | sizeof(CONFIG_EFI_PLATFORM_LANG_CODES), |
| 48 | CONFIG_EFI_PLATFORM_LANG_CODES)); |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 49 | if (ret != EFI_SUCCESS) |
| 50 | goto out; |
| 51 | |
| 52 | /* |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 53 | * Variable PlatformLang defines the language that the machine has been |
| 54 | * configured for. |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 55 | */ |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 56 | ret = EFI_CALL(efi_get_variable(L"PlatformLang", |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 57 | &efi_global_variable_guid, |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 58 | NULL, &data_size, &pos)); |
| 59 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 60 | /* The variable is already set. Do not change it. */ |
| 61 | ret = EFI_SUCCESS; |
| 62 | goto out; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * The list of supported languages is semicolon separated. Use the first |
| 67 | * language to initialize PlatformLang. |
| 68 | */ |
| 69 | pos = strchr(lang, ';'); |
| 70 | if (pos) |
| 71 | *pos = 0; |
| 72 | |
| 73 | ret = EFI_CALL(efi_set_variable(L"PlatformLang", |
| 74 | &efi_global_variable_guid, |
| 75 | EFI_VARIABLE_NON_VOLATILE | |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 76 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 77 | EFI_VARIABLE_RUNTIME_ACCESS, |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 78 | 1 + strlen(lang), lang)); |
| 79 | out: |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 80 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 81 | printf("EFI: cannot initialize platform language settings\n"); |
| 82 | return ret; |
| 83 | } |
| 84 | |
AKASHI Takahiro | 8d494f6 | 2020-04-14 11:51:45 +0900 | [diff] [blame] | 85 | #ifdef CONFIG_EFI_SECURE_BOOT |
| 86 | /** |
| 87 | * efi_init_secure_boot - initialize secure boot state |
| 88 | * |
Heinrich Schuchardt | e2c43da | 2020-05-03 16:29:00 +0200 | [diff] [blame] | 89 | * Return: status code |
AKASHI Takahiro | 8d494f6 | 2020-04-14 11:51:45 +0900 | [diff] [blame] | 90 | */ |
| 91 | static efi_status_t efi_init_secure_boot(void) |
| 92 | { |
| 93 | efi_guid_t signature_types[] = { |
| 94 | EFI_CERT_SHA256_GUID, |
| 95 | EFI_CERT_X509_GUID, |
| 96 | }; |
| 97 | efi_status_t ret; |
| 98 | |
| 99 | /* TODO: read-only */ |
| 100 | ret = EFI_CALL(efi_set_variable(L"SignatureSupport", |
| 101 | &efi_global_variable_guid, |
| 102 | EFI_VARIABLE_BOOTSERVICE_ACCESS |
| 103 | | EFI_VARIABLE_RUNTIME_ACCESS, |
| 104 | sizeof(signature_types), |
| 105 | &signature_types)); |
| 106 | if (ret != EFI_SUCCESS) |
| 107 | printf("EFI: cannot initialize SignatureSupport variable\n"); |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | #else |
| 112 | static efi_status_t efi_init_secure_boot(void) |
| 113 | { |
| 114 | return EFI_SUCCESS; |
| 115 | } |
| 116 | #endif /* CONFIG_EFI_SECURE_BOOT */ |
| 117 | |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 118 | /** |
| 119 | * efi_init_obj_list() - Initialize and populate EFI object list |
| 120 | * |
| 121 | * Return: status code |
| 122 | */ |
| 123 | efi_status_t efi_init_obj_list(void) |
| 124 | { |
AKASHI Takahiro | 3a48925 | 2019-04-24 15:30:38 +0900 | [diff] [blame] | 125 | u64 os_indications_supported = 0; /* None */ |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 126 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | f7fe45b | 2019-04-05 02:45:21 +0200 | [diff] [blame] | 127 | |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 128 | /* Initialize once only */ |
| 129 | if (efi_obj_list_initialized != OBJ_LIST_NOT_INITIALIZED) |
| 130 | return efi_obj_list_initialized; |
| 131 | |
Heinrich Schuchardt | aea03d6 | 2019-05-04 11:47:48 +0200 | [diff] [blame] | 132 | /* Allow unaligned memory access */ |
| 133 | allow_unaligned(); |
| 134 | |
| 135 | /* On ARM switch from EL3 or secure mode to EL2 or non-secure mode */ |
| 136 | switch_to_non_secure_mode(); |
| 137 | |
Heinrich Schuchardt | f667a18 | 2020-05-20 21:27:29 +0200 | [diff] [blame] | 138 | /* Initialize root node */ |
| 139 | ret = efi_root_node_register(); |
| 140 | if (ret != EFI_SUCCESS) |
| 141 | goto out; |
| 142 | |
AKASHI Takahiro | 9b31c63 | 2020-06-09 14:09:31 +0900 | [diff] [blame] | 143 | ret = efi_console_register(); |
| 144 | if (ret != EFI_SUCCESS) |
| 145 | goto out; |
| 146 | |
Heinrich Schuchardt | eef4eee | 2020-03-19 18:21:58 +0000 | [diff] [blame] | 147 | #ifdef CONFIG_PARTITIONS |
| 148 | ret = efi_disk_register(); |
| 149 | if (ret != EFI_SUCCESS) |
| 150 | goto out; |
| 151 | #endif |
Heinrich Schuchardt | cf3b118 | 2019-06-20 13:52:16 +0200 | [diff] [blame] | 152 | /* Initialize variable services */ |
| 153 | ret = efi_init_variables(); |
| 154 | if (ret != EFI_SUCCESS) |
| 155 | goto out; |
| 156 | |
Heinrich Schuchardt | 3700d57 | 2019-04-11 07:34:24 +0200 | [diff] [blame] | 157 | /* Define supported languages */ |
| 158 | ret = efi_init_platform_lang(); |
| 159 | if (ret != EFI_SUCCESS) |
| 160 | goto out; |
| 161 | |
AKASHI Takahiro | 3a48925 | 2019-04-24 15:30:38 +0900 | [diff] [blame] | 162 | /* Indicate supported features */ |
| 163 | ret = EFI_CALL(efi_set_variable(L"OsIndicationsSupported", |
| 164 | &efi_global_variable_guid, |
| 165 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 166 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 167 | sizeof(os_indications_supported), |
| 168 | &os_indications_supported)); |
| 169 | if (ret != EFI_SUCCESS) |
| 170 | goto out; |
| 171 | |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 172 | /* Initialize system table */ |
| 173 | ret = efi_initialize_system_table(); |
AKASHI Takahiro | 3240184 | 2019-06-05 13:21:38 +0900 | [diff] [blame] | 174 | if (ret != EFI_SUCCESS) |
| 175 | goto out; |
| 176 | |
AKASHI Takahiro | 8d494f6 | 2020-04-14 11:51:45 +0900 | [diff] [blame] | 177 | /* Secure boot */ |
| 178 | ret = efi_init_secure_boot(); |
| 179 | if (ret != EFI_SUCCESS) |
| 180 | goto out; |
| 181 | |
Heinrich Schuchardt | 956eff3 | 2020-02-19 20:48:49 +0100 | [diff] [blame] | 182 | /* Indicate supported runtime services */ |
| 183 | ret = efi_init_runtime_supported(); |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 184 | if (ret != EFI_SUCCESS) |
| 185 | goto out; |
| 186 | |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 187 | /* Initialize EFI driver uclass */ |
| 188 | ret = efi_driver_init(); |
| 189 | if (ret != EFI_SUCCESS) |
| 190 | goto out; |
| 191 | |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 192 | #if defined(CONFIG_LCD) || defined(CONFIG_DM_VIDEO) |
| 193 | ret = efi_gop_register(); |
| 194 | if (ret != EFI_SUCCESS) |
| 195 | goto out; |
| 196 | #endif |
Ilias Apalodimas | 3510ba7 | 2020-02-21 09:55:45 +0200 | [diff] [blame] | 197 | #ifdef CONFIG_EFI_LOAD_FILE2_INITRD |
| 198 | ret = efi_initrd_register(); |
| 199 | if (ret != EFI_SUCCESS) |
| 200 | goto out; |
| 201 | #endif |
AKASHI Takahiro | 7e0badb | 2018-12-30 15:16:55 +0100 | [diff] [blame] | 202 | #ifdef CONFIG_NET |
| 203 | ret = efi_net_register(); |
| 204 | if (ret != EFI_SUCCESS) |
| 205 | goto out; |
| 206 | #endif |
| 207 | #ifdef CONFIG_GENERATE_ACPI_TABLE |
| 208 | ret = efi_acpi_register(); |
| 209 | if (ret != EFI_SUCCESS) |
| 210 | goto out; |
| 211 | #endif |
| 212 | #ifdef CONFIG_GENERATE_SMBIOS_TABLE |
| 213 | ret = efi_smbios_register(); |
| 214 | if (ret != EFI_SUCCESS) |
| 215 | goto out; |
| 216 | #endif |
| 217 | ret = efi_watchdog_register(); |
| 218 | if (ret != EFI_SUCCESS) |
| 219 | goto out; |
| 220 | |
| 221 | /* Initialize EFI runtime services */ |
| 222 | ret = efi_reset_system_init(); |
| 223 | if (ret != EFI_SUCCESS) |
| 224 | goto out; |
| 225 | |
| 226 | out: |
| 227 | efi_obj_list_initialized = ret; |
| 228 | return ret; |
| 229 | } |