AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * EFI Capsule |
| 4 | * |
| 5 | * Copyright (c) 2018 Linaro Limited |
| 6 | * Author: AKASHI Takahiro |
| 7 | */ |
| 8 | |
Heinrich Schuchardt | 3e49119 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 9 | #define LOG_CATEGORY LOGC_EFI |
| 10 | |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <efi_loader.h> |
| 13 | #include <efi_variable.h> |
AKASHI Takahiro | 7a6fb28 | 2021-10-07 15:23:32 +0900 | [diff] [blame] | 14 | #include <env.h> |
| 15 | #include <fdtdec.h> |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 16 | #include <fs.h> |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 17 | #include <fwu.h> |
Masami Hiramatsu | 3e6f810 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 18 | #include <hang.h> |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 19 | #include <malloc.h> |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 20 | #include <mapmem.h> |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 21 | #include <sort.h> |
Masami Hiramatsu | 7660cfe | 2022-03-21 22:37:56 +0900 | [diff] [blame] | 22 | #include <sysreset.h> |
AKASHI Takahiro | 7a6fb28 | 2021-10-07 15:23:32 +0900 | [diff] [blame] | 23 | #include <asm/global_data.h> |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 24 | |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 25 | #include <crypto/pkcs7.h> |
| 26 | #include <crypto/pkcs7_parser.h> |
| 27 | #include <linux/err.h> |
| 28 | |
AKASHI Takahiro | 7a6fb28 | 2021-10-07 15:23:32 +0900 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 31 | const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID; |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 32 | static const efi_guid_t efi_guid_firmware_management_capsule_id = |
| 33 | EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID; |
| 34 | const efi_guid_t efi_guid_firmware_management_protocol = |
| 35 | EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID; |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 36 | const efi_guid_t fwu_guid_os_request_fw_revert = |
| 37 | FWU_OS_REQUEST_FW_REVERT_GUID; |
| 38 | const efi_guid_t fwu_guid_os_request_fw_accept = |
| 39 | FWU_OS_REQUEST_FW_ACCEPT_GUID; |
| 40 | |
| 41 | #define FW_ACCEPT_OS (u32)0x8000 |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 42 | |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 43 | #ifdef CONFIG_EFI_CAPSULE_ON_DISK |
| 44 | /* for file system access */ |
| 45 | static struct efi_file_handle *bootdev_root; |
| 46 | #endif |
| 47 | |
Etienne Carriere | 4429393 | 2023-02-16 18:21:41 +0100 | [diff] [blame] | 48 | static __maybe_unused unsigned int get_capsule_index(const u16 *variable_name) |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 49 | { |
| 50 | u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */ |
Heinrich Schuchardt | 15bbcaf | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 51 | char value[5]; |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 52 | efi_uintn_t size; |
| 53 | unsigned long index = 0xffff; |
| 54 | efi_status_t ret; |
Heinrich Schuchardt | 15bbcaf | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 55 | int i; |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 56 | |
| 57 | size = sizeof(value16); |
Etienne Carriere | 4429393 | 2023-02-16 18:21:41 +0100 | [diff] [blame] | 58 | ret = efi_get_variable_int(variable_name, &efi_guid_capsule_report, |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 59 | NULL, &size, value16, NULL); |
Heinrich Schuchardt | 15bbcaf | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 60 | if (ret != EFI_SUCCESS || size != 22 || |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 61 | u16_strncmp(value16, u"Capsule", 7)) |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 62 | goto err; |
Heinrich Schuchardt | 15bbcaf | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 63 | for (i = 0; i < 4; ++i) { |
| 64 | u16 c = value16[i + 7]; |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 65 | |
Heinrich Schuchardt | 15bbcaf | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 66 | if (!c || c > 0x7f) |
| 67 | goto err; |
| 68 | value[i] = c; |
| 69 | } |
| 70 | value[4] = 0; |
| 71 | if (strict_strtoul(value, 16, &index)) |
| 72 | index = 0xffff; |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 73 | err: |
| 74 | return index; |
| 75 | } |
| 76 | |
| 77 | /** |
Etienne Carriere | 4429393 | 2023-02-16 18:21:41 +0100 | [diff] [blame] | 78 | * get_last_capsule - get the last capsule index |
| 79 | * |
| 80 | * Retrieve the index of the capsule invoked last time from "CapsuleLast" |
| 81 | * variable. |
| 82 | * |
| 83 | * Return: |
| 84 | * * > 0 - the last capsule index invoked |
| 85 | * * 0xffff - on error, or no capsule invoked yet |
| 86 | */ |
| 87 | static __maybe_unused unsigned int get_last_capsule(void) |
| 88 | { |
| 89 | return get_capsule_index(u"CapsuleLast"); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * get_max_capsule - get the max capsule index |
| 94 | * |
| 95 | * Retrieve the max capsule index value from "CapsuleMax" variable. |
| 96 | * |
| 97 | * Return: |
| 98 | * * > 0 - the max capsule index |
| 99 | * * 0xffff - on error, or "CapsuleMax" variable does not exist |
| 100 | */ |
| 101 | static __maybe_unused unsigned int get_max_capsule(void) |
| 102 | { |
| 103 | return get_capsule_index(u"CapsuleMax"); |
| 104 | } |
| 105 | |
| 106 | /** |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 107 | * set_capsule_result - set a result variable |
| 108 | * @capsule: Capsule |
| 109 | * @return_status: Return status |
| 110 | * |
| 111 | * Create and set a result variable, "CapsuleXXXX", for the capsule, |
| 112 | * @capsule. |
| 113 | */ |
| 114 | static __maybe_unused |
| 115 | void set_capsule_result(int index, struct efi_capsule_header *capsule, |
| 116 | efi_status_t return_status) |
| 117 | { |
| 118 | u16 variable_name16[12]; |
| 119 | struct efi_capsule_result_variable_header result; |
| 120 | struct efi_time time; |
| 121 | efi_status_t ret; |
| 122 | |
Ilias Apalodimas | fe179d7 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 123 | efi_create_indexed_name(variable_name16, sizeof(variable_name16), |
| 124 | "Capsule", index); |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 125 | result.variable_total_size = sizeof(result); |
| 126 | result.capsule_guid = capsule->capsule_guid; |
| 127 | ret = EFI_CALL((*efi_runtime_services.get_time)(&time, NULL)); |
| 128 | if (ret == EFI_SUCCESS) |
| 129 | memcpy(&result.capsule_processed, &time, sizeof(time)); |
| 130 | else |
| 131 | memset(&result.capsule_processed, 0, sizeof(time)); |
| 132 | result.capsule_status = return_status; |
Heinrich Schuchardt | d7eedd9 | 2021-07-10 11:10:26 +0200 | [diff] [blame] | 133 | ret = efi_set_variable_int(variable_name16, &efi_guid_capsule_report, |
| 134 | EFI_VARIABLE_NON_VOLATILE | |
| 135 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 136 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 137 | sizeof(result), &result, false); |
Heinrich Schuchardt | 70bad54 | 2021-07-10 11:14:13 +0200 | [diff] [blame] | 138 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 3e49119 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 139 | log_err("Setting %ls failed\n", variable_name16); |
Heinrich Schuchardt | 70bad54 | 2021-07-10 11:14:13 +0200 | [diff] [blame] | 140 | return; |
| 141 | } |
| 142 | |
| 143 | /* Variable CapsuleLast must not include terminating 0x0000 */ |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 144 | ret = efi_set_variable_int(u"CapsuleLast", &efi_guid_capsule_report, |
Heinrich Schuchardt | 70bad54 | 2021-07-10 11:14:13 +0200 | [diff] [blame] | 145 | EFI_VARIABLE_READ_ONLY | |
| 146 | EFI_VARIABLE_NON_VOLATILE | |
| 147 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 148 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 149 | 22, variable_name16, false); |
| 150 | if (ret != EFI_SUCCESS) |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 151 | log_err("Setting %ls failed\n", u"CapsuleLast"); |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 152 | } |
| 153 | |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 154 | #ifdef CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT |
| 155 | /** |
| 156 | * efi_fmp_find - search for Firmware Management Protocol drivers |
| 157 | * @image_type: Image type guid |
Sughosh Ganu | 7cf06f0 | 2022-04-15 11:29:36 +0530 | [diff] [blame] | 158 | * @image_index: Image Index |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 159 | * @instance: Instance number |
| 160 | * @handles: Handles of FMP drivers |
| 161 | * @no_handles: Number of handles |
| 162 | * |
| 163 | * Search for Firmware Management Protocol drivers, matching the image |
| 164 | * type, @image_type and the machine instance, @instance, from the list, |
| 165 | * @handles. |
| 166 | * |
| 167 | * Return: |
| 168 | * * Protocol instance - on success |
| 169 | * * NULL - on failure |
| 170 | */ |
| 171 | static struct efi_firmware_management_protocol * |
Sughosh Ganu | 7cf06f0 | 2022-04-15 11:29:36 +0530 | [diff] [blame] | 172 | efi_fmp_find(efi_guid_t *image_type, u8 image_index, u64 instance, |
| 173 | efi_handle_t *handles, efi_uintn_t no_handles) |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 174 | { |
| 175 | efi_handle_t *handle; |
| 176 | struct efi_firmware_management_protocol *fmp; |
| 177 | struct efi_firmware_image_descriptor *image_info, *desc; |
| 178 | efi_uintn_t info_size, descriptor_size; |
| 179 | u32 descriptor_version; |
| 180 | u8 descriptor_count; |
| 181 | u32 package_version; |
| 182 | u16 *package_version_name; |
| 183 | bool found = false; |
| 184 | int i, j; |
| 185 | efi_status_t ret; |
| 186 | |
| 187 | for (i = 0, handle = handles; i < no_handles; i++, handle++) { |
Heinrich Schuchardt | 56c9f0c | 2022-10-07 15:29:52 +0200 | [diff] [blame] | 188 | struct efi_handler *fmp_handler; |
| 189 | |
| 190 | ret = efi_search_protocol( |
| 191 | *handle, &efi_guid_firmware_management_protocol, |
| 192 | &fmp_handler); |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 193 | if (ret != EFI_SUCCESS) |
| 194 | continue; |
Heinrich Schuchardt | 56c9f0c | 2022-10-07 15:29:52 +0200 | [diff] [blame] | 195 | fmp = fmp_handler->protocol_interface; |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 196 | |
| 197 | /* get device's image info */ |
| 198 | info_size = 0; |
| 199 | image_info = NULL; |
| 200 | descriptor_version = 0; |
| 201 | descriptor_count = 0; |
| 202 | descriptor_size = 0; |
| 203 | package_version = 0; |
| 204 | package_version_name = NULL; |
| 205 | ret = EFI_CALL(fmp->get_image_info(fmp, &info_size, |
| 206 | image_info, |
| 207 | &descriptor_version, |
| 208 | &descriptor_count, |
| 209 | &descriptor_size, |
| 210 | &package_version, |
| 211 | &package_version_name)); |
| 212 | if (ret != EFI_BUFFER_TOO_SMALL) |
| 213 | goto skip; |
| 214 | |
| 215 | image_info = malloc(info_size); |
| 216 | if (!image_info) |
| 217 | goto skip; |
| 218 | |
| 219 | ret = EFI_CALL(fmp->get_image_info(fmp, &info_size, |
| 220 | image_info, |
| 221 | &descriptor_version, |
| 222 | &descriptor_count, |
| 223 | &descriptor_size, |
| 224 | &package_version, |
| 225 | &package_version_name)); |
| 226 | if (ret != EFI_SUCCESS || |
| 227 | descriptor_version != EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION) |
| 228 | goto skip; |
| 229 | |
| 230 | /* matching */ |
| 231 | for (j = 0, desc = image_info; j < descriptor_count; |
| 232 | j++, desc = (void *)desc + descriptor_size) { |
| 233 | log_debug("+++ desc[%d] index: %d, name: %ls\n", |
| 234 | j, desc->image_index, desc->image_id_name); |
| 235 | if (!guidcmp(&desc->image_type_id, image_type) && |
Sughosh Ganu | 7cf06f0 | 2022-04-15 11:29:36 +0530 | [diff] [blame] | 236 | (desc->image_index == image_index) && |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 237 | (!instance || |
| 238 | !desc->hardware_instance || |
| 239 | desc->hardware_instance == instance)) |
| 240 | found = true; |
| 241 | } |
| 242 | |
| 243 | skip: |
| 244 | efi_free_pool(package_version_name); |
| 245 | free(image_info); |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 246 | if (found) |
| 247 | return fmp; |
| 248 | } |
| 249 | |
| 250 | return NULL; |
| 251 | } |
| 252 | |
AKASHI Takahiro | c2cc60c | 2021-07-20 14:52:05 +0900 | [diff] [blame] | 253 | /** |
| 254 | * efi_remove_auth_hdr - remove authentication data from image |
| 255 | * @image: Pointer to pointer to Image |
| 256 | * @image_size: Pointer to Image size |
| 257 | * |
| 258 | * Remove the authentication data from image if possible. |
| 259 | * Update @image and @image_size. |
| 260 | * |
| 261 | * Return: status code |
| 262 | */ |
| 263 | static efi_status_t efi_remove_auth_hdr(void **image, efi_uintn_t *image_size) |
| 264 | { |
| 265 | struct efi_firmware_image_authentication *auth_hdr; |
| 266 | efi_status_t ret = EFI_INVALID_PARAMETER; |
| 267 | |
| 268 | auth_hdr = (struct efi_firmware_image_authentication *)*image; |
| 269 | if (*image_size < sizeof(*auth_hdr)) |
| 270 | goto out; |
| 271 | |
| 272 | if (auth_hdr->auth_info.hdr.dwLength <= |
| 273 | offsetof(struct win_certificate_uefi_guid, cert_data)) |
| 274 | goto out; |
| 275 | |
| 276 | *image = (uint8_t *)*image + sizeof(auth_hdr->monotonic_count) + |
| 277 | auth_hdr->auth_info.hdr.dwLength; |
| 278 | *image_size = *image_size - auth_hdr->auth_info.hdr.dwLength - |
| 279 | sizeof(auth_hdr->monotonic_count); |
| 280 | |
| 281 | ret = EFI_SUCCESS; |
| 282 | out: |
| 283 | return ret; |
| 284 | } |
| 285 | |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 286 | #if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE) |
AKASHI Takahiro | 50b05eb | 2021-11-02 09:55:01 +0900 | [diff] [blame] | 287 | int efi_get_public_key_data(void **pkey, efi_uintn_t *pkey_len) |
AKASHI Takahiro | 7a6fb28 | 2021-10-07 15:23:32 +0900 | [diff] [blame] | 288 | { |
| 289 | const void *fdt_blob = gd->fdt_blob; |
| 290 | const void *blob; |
| 291 | const char *cnode_name = "capsule-key"; |
| 292 | const char *snode_name = "signature"; |
| 293 | int sig_node; |
| 294 | int len; |
| 295 | |
| 296 | sig_node = fdt_subnode_offset(fdt_blob, 0, snode_name); |
| 297 | if (sig_node < 0) { |
| 298 | log_err("Unable to get signature node offset\n"); |
| 299 | |
| 300 | return -FDT_ERR_NOTFOUND; |
| 301 | } |
| 302 | |
| 303 | blob = fdt_getprop(fdt_blob, sig_node, cnode_name, &len); |
| 304 | |
| 305 | if (!blob || len < 0) { |
| 306 | log_err("Unable to get capsule-key value\n"); |
| 307 | *pkey = NULL; |
| 308 | *pkey_len = 0; |
| 309 | |
| 310 | return -FDT_ERR_NOTFOUND; |
| 311 | } |
| 312 | |
| 313 | *pkey = (void *)blob; |
| 314 | *pkey_len = len; |
| 315 | |
| 316 | return 0; |
| 317 | } |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 318 | |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 319 | efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size, |
| 320 | void **image, efi_uintn_t *image_size) |
| 321 | { |
| 322 | u8 *buf; |
| 323 | int ret; |
Simon Glass | 47a25e8 | 2021-08-02 08:44:31 -0600 | [diff] [blame] | 324 | void *fdt_pkey, *pkey; |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 325 | efi_uintn_t pkey_len; |
| 326 | uint64_t monotonic_count; |
| 327 | struct efi_signature_store *truststore; |
| 328 | struct pkcs7_message *capsule_sig; |
| 329 | struct efi_image_regions *regs; |
| 330 | struct efi_firmware_image_authentication *auth_hdr; |
| 331 | efi_status_t status; |
| 332 | |
| 333 | status = EFI_SECURITY_VIOLATION; |
| 334 | capsule_sig = NULL; |
| 335 | truststore = NULL; |
| 336 | regs = NULL; |
| 337 | |
| 338 | /* Sanity checks */ |
| 339 | if (capsule == NULL || capsule_size == 0) |
| 340 | goto out; |
| 341 | |
AKASHI Takahiro | c2cc60c | 2021-07-20 14:52:05 +0900 | [diff] [blame] | 342 | *image = (uint8_t *)capsule; |
| 343 | *image_size = capsule_size; |
| 344 | if (efi_remove_auth_hdr(image, image_size) != EFI_SUCCESS) |
| 345 | goto out; |
| 346 | |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 347 | auth_hdr = (struct efi_firmware_image_authentication *)capsule; |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 348 | if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7)) |
| 349 | goto out; |
| 350 | |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 351 | memcpy(&monotonic_count, &auth_hdr->monotonic_count, |
| 352 | sizeof(monotonic_count)); |
| 353 | |
| 354 | /* data to be digested */ |
| 355 | regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 2, 1); |
| 356 | if (!regs) |
| 357 | goto out; |
| 358 | |
| 359 | regs->max = 2; |
| 360 | efi_image_region_add(regs, (uint8_t *)*image, |
| 361 | (uint8_t *)*image + *image_size, 1); |
| 362 | |
| 363 | efi_image_region_add(regs, (uint8_t *)&monotonic_count, |
| 364 | (uint8_t *)&monotonic_count + sizeof(monotonic_count), |
| 365 | 1); |
| 366 | |
| 367 | capsule_sig = efi_parse_pkcs7_header(auth_hdr->auth_info.cert_data, |
| 368 | auth_hdr->auth_info.hdr.dwLength |
| 369 | - sizeof(auth_hdr->auth_info), |
| 370 | &buf); |
| 371 | if (IS_ERR(capsule_sig)) { |
| 372 | debug("Parsing variable's pkcs7 header failed\n"); |
| 373 | capsule_sig = NULL; |
| 374 | goto out; |
| 375 | } |
| 376 | |
Simon Glass | 47a25e8 | 2021-08-02 08:44:31 -0600 | [diff] [blame] | 377 | ret = efi_get_public_key_data(&fdt_pkey, &pkey_len); |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 378 | if (ret < 0) |
| 379 | goto out; |
| 380 | |
| 381 | pkey = malloc(pkey_len); |
| 382 | if (!pkey) |
| 383 | goto out; |
| 384 | |
Simon Glass | 47a25e8 | 2021-08-02 08:44:31 -0600 | [diff] [blame] | 385 | memcpy(pkey, fdt_pkey, pkey_len); |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 386 | truststore = efi_build_signature_store(pkey, pkey_len); |
| 387 | if (!truststore) |
| 388 | goto out; |
| 389 | |
| 390 | /* verify signature */ |
| 391 | if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) { |
| 392 | debug("Verified\n"); |
| 393 | } else { |
| 394 | debug("Verifying variable's signature failed\n"); |
| 395 | goto out; |
| 396 | } |
| 397 | |
| 398 | status = EFI_SUCCESS; |
| 399 | |
| 400 | out: |
| 401 | efi_sigstore_free(truststore); |
| 402 | pkcs7_free_message(capsule_sig); |
| 403 | free(regs); |
| 404 | |
| 405 | return status; |
| 406 | } |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 407 | #endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */ |
| 408 | |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 409 | static __maybe_unused bool fwu_empty_capsule(struct efi_capsule_header *capsule) |
| 410 | { |
| 411 | return !guidcmp(&capsule->capsule_guid, |
| 412 | &fwu_guid_os_request_fw_revert) || |
| 413 | !guidcmp(&capsule->capsule_guid, |
| 414 | &fwu_guid_os_request_fw_accept); |
| 415 | } |
| 416 | |
| 417 | static __maybe_unused efi_status_t fwu_to_efi_error(int err) |
| 418 | { |
| 419 | efi_status_t ret; |
| 420 | |
| 421 | switch(err) { |
| 422 | case 0: |
| 423 | ret = EFI_SUCCESS; |
| 424 | break; |
| 425 | case -ERANGE: |
| 426 | case -EIO: |
| 427 | ret = EFI_DEVICE_ERROR; |
| 428 | break; |
| 429 | case -EINVAL: |
| 430 | ret = EFI_INVALID_PARAMETER; |
| 431 | break; |
| 432 | case -ENODEV: |
| 433 | ret = EFI_NOT_FOUND; |
| 434 | break; |
| 435 | default: |
| 436 | ret = EFI_OUT_OF_RESOURCES; |
| 437 | } |
| 438 | |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | static __maybe_unused efi_status_t fwu_empty_capsule_process( |
| 443 | struct efi_capsule_header *capsule) |
| 444 | { |
| 445 | int status; |
| 446 | u32 active_idx; |
| 447 | efi_guid_t *image_guid; |
| 448 | efi_status_t ret = EFI_INVALID_PARAMETER; |
| 449 | |
| 450 | if (!guidcmp(&capsule->capsule_guid, |
| 451 | &fwu_guid_os_request_fw_revert)) { |
| 452 | /* |
| 453 | * One of the previously updated image has |
| 454 | * failed the OS acceptance test. OS has |
| 455 | * requested to revert back to the earlier |
| 456 | * boot index |
| 457 | */ |
| 458 | status = fwu_revert_boot_index(); |
| 459 | ret = fwu_to_efi_error(status); |
| 460 | if (ret == EFI_SUCCESS) |
| 461 | log_debug("Reverted the FWU active_index. Recommend rebooting the system\n"); |
| 462 | else |
| 463 | log_err("Failed to revert the FWU boot index\n"); |
| 464 | } else if (!guidcmp(&capsule->capsule_guid, |
| 465 | &fwu_guid_os_request_fw_accept)) { |
| 466 | /* |
| 467 | * Image accepted by the OS. Set the acceptance |
| 468 | * status for the image. |
| 469 | */ |
| 470 | image_guid = (void *)(char *)capsule + |
| 471 | capsule->header_size; |
| 472 | |
| 473 | status = fwu_get_active_index(&active_idx); |
| 474 | ret = fwu_to_efi_error(status); |
| 475 | if (ret != EFI_SUCCESS) { |
| 476 | log_err("Unable to get the active_index from the FWU metadata\n"); |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | status = fwu_accept_image(image_guid, active_idx); |
| 481 | ret = fwu_to_efi_error(status); |
| 482 | if (ret != EFI_SUCCESS) |
| 483 | log_err("Unable to set the Accept bit for the image %pUs\n", |
| 484 | image_guid); |
| 485 | } |
| 486 | |
| 487 | return ret; |
| 488 | } |
| 489 | |
| 490 | static __maybe_unused void fwu_post_update_checks( |
| 491 | struct efi_capsule_header *capsule, |
| 492 | bool *fw_accept_os, bool *capsule_update) |
| 493 | { |
| 494 | if (fwu_empty_capsule(capsule)) |
| 495 | *capsule_update = false; |
| 496 | else |
| 497 | if (!*fw_accept_os) |
| 498 | *fw_accept_os = |
| 499 | capsule->flags & FW_ACCEPT_OS ? true : false; |
| 500 | } |
| 501 | |
| 502 | static __maybe_unused efi_status_t fwu_post_update_process(bool fw_accept_os) |
| 503 | { |
| 504 | int status; |
| 505 | uint update_index; |
| 506 | efi_status_t ret; |
| 507 | |
| 508 | status = fwu_plat_get_update_index(&update_index); |
| 509 | if (status < 0) { |
| 510 | log_err("Failed to get the FWU update_index value\n"); |
| 511 | return EFI_DEVICE_ERROR; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * All the capsules have been updated successfully, |
| 516 | * update the FWU metadata. |
| 517 | */ |
| 518 | log_debug("Update Complete. Now updating active_index to %u\n", |
| 519 | update_index); |
| 520 | status = fwu_set_active_index(update_index); |
| 521 | ret = fwu_to_efi_error(status); |
| 522 | if (ret != EFI_SUCCESS) { |
| 523 | log_err("Failed to update FWU metadata index values\n"); |
| 524 | } else { |
| 525 | log_debug("Successfully updated the active_index\n"); |
| 526 | if (fw_accept_os) { |
| 527 | status = fwu_trial_state_ctr_start(); |
| 528 | if (status < 0) |
| 529 | ret = EFI_DEVICE_ERROR; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return ret; |
| 534 | } |
Sughosh Ganu | 04be98b | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 535 | |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 536 | /** |
| 537 | * efi_capsule_update_firmware - update firmware from capsule |
| 538 | * @capsule_data: Capsule |
| 539 | * |
| 540 | * Update firmware, using a capsule, @capsule_data. Loading any FMP |
| 541 | * drivers embedded in a capsule is not supported. |
| 542 | * |
| 543 | * Return: status code |
| 544 | */ |
| 545 | static efi_status_t efi_capsule_update_firmware( |
| 546 | struct efi_capsule_header *capsule_data) |
| 547 | { |
| 548 | struct efi_firmware_management_capsule_header *capsule; |
| 549 | struct efi_firmware_management_capsule_image_header *image; |
AKASHI Takahiro | c2cc60c | 2021-07-20 14:52:05 +0900 | [diff] [blame] | 550 | size_t capsule_size, image_binary_size; |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 551 | void *image_binary, *vendor_code; |
| 552 | efi_handle_t *handles; |
| 553 | efi_uintn_t no_handles; |
| 554 | int item; |
| 555 | struct efi_firmware_management_protocol *fmp; |
| 556 | u16 *abort_reason; |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 557 | efi_guid_t *image_type_id; |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 558 | efi_status_t ret = EFI_SUCCESS; |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 559 | int status; |
| 560 | uint update_index; |
| 561 | bool fw_accept_os; |
| 562 | |
| 563 | if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) { |
| 564 | if (fwu_empty_capsule_checks_pass() && |
| 565 | fwu_empty_capsule(capsule_data)) |
| 566 | return fwu_empty_capsule_process(capsule_data); |
| 567 | |
| 568 | if (!fwu_update_checks_pass()) { |
| 569 | log_err("FWU checks failed. Cannot start update\n"); |
| 570 | return EFI_INVALID_PARAMETER; |
| 571 | } |
| 572 | |
| 573 | |
| 574 | /* Obtain the update_index from the platform */ |
| 575 | status = fwu_plat_get_update_index(&update_index); |
| 576 | if (status < 0) { |
| 577 | log_err("Failed to get the FWU update_index value\n"); |
| 578 | return EFI_DEVICE_ERROR; |
| 579 | } |
| 580 | |
| 581 | fw_accept_os = capsule_data->flags & FW_ACCEPT_OS ? 0x1 : 0x0; |
| 582 | } |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 583 | |
AKASHI Takahiro | 1b7e469 | 2023-07-27 09:38:00 +0900 | [diff] [blame^] | 584 | if (guidcmp(&capsule_data->capsule_guid, |
| 585 | &efi_guid_firmware_management_capsule_id)) { |
| 586 | log_err("Unsupported capsule type: %pUs\n", |
| 587 | &capsule_data->capsule_guid); |
| 588 | return EFI_UNSUPPORTED; |
| 589 | } |
| 590 | |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 591 | /* sanity check */ |
| 592 | if (capsule_data->header_size < sizeof(*capsule) || |
| 593 | capsule_data->header_size >= capsule_data->capsule_image_size) |
| 594 | return EFI_INVALID_PARAMETER; |
| 595 | |
| 596 | capsule = (void *)capsule_data + capsule_data->header_size; |
| 597 | capsule_size = capsule_data->capsule_image_size |
| 598 | - capsule_data->header_size; |
| 599 | |
| 600 | if (capsule->version != 0x00000001) |
| 601 | return EFI_UNSUPPORTED; |
| 602 | |
| 603 | handles = NULL; |
| 604 | ret = EFI_CALL(efi_locate_handle_buffer( |
| 605 | BY_PROTOCOL, |
| 606 | &efi_guid_firmware_management_protocol, |
| 607 | NULL, &no_handles, (efi_handle_t **)&handles)); |
| 608 | if (ret != EFI_SUCCESS) |
| 609 | return EFI_UNSUPPORTED; |
| 610 | |
| 611 | /* Payload */ |
| 612 | for (item = capsule->embedded_driver_count; |
| 613 | item < capsule->embedded_driver_count |
| 614 | + capsule->payload_item_count; item++) { |
| 615 | /* sanity check */ |
| 616 | if ((capsule->item_offset_list[item] + sizeof(*image) |
| 617 | >= capsule_size)) { |
Heinrich Schuchardt | 3e49119 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 618 | log_err("Capsule does not have enough data\n"); |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 619 | ret = EFI_INVALID_PARAMETER; |
| 620 | goto out; |
| 621 | } |
| 622 | |
| 623 | image = (void *)capsule + capsule->item_offset_list[item]; |
| 624 | |
| 625 | if (image->version != 0x00000003) { |
| 626 | ret = EFI_UNSUPPORTED; |
| 627 | goto out; |
| 628 | } |
| 629 | |
| 630 | /* find a device for update firmware */ |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 631 | fmp = efi_fmp_find(&image->update_image_type_id, |
Sughosh Ganu | 7cf06f0 | 2022-04-15 11:29:36 +0530 | [diff] [blame] | 632 | image->update_image_index, |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 633 | image->update_hardware_instance, |
| 634 | handles, no_handles); |
| 635 | if (!fmp) { |
Heinrich Schuchardt | ce00a74 | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 636 | log_err("FMP driver not found for firmware type %pUs, hardware instance %lld\n", |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 637 | &image->update_image_type_id, |
| 638 | image->update_hardware_instance); |
| 639 | ret = EFI_UNSUPPORTED; |
| 640 | goto out; |
| 641 | } |
| 642 | |
| 643 | /* do update */ |
AKASHI Takahiro | c2cc60c | 2021-07-20 14:52:05 +0900 | [diff] [blame] | 644 | if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) && |
| 645 | !(image->image_capsule_support & |
| 646 | CAPSULE_SUPPORT_AUTHENTICATION)) { |
| 647 | /* no signature */ |
| 648 | ret = EFI_SECURITY_VIOLATION; |
| 649 | goto out; |
| 650 | } |
| 651 | |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 652 | image_binary = (void *)image + sizeof(*image); |
AKASHI Takahiro | c2cc60c | 2021-07-20 14:52:05 +0900 | [diff] [blame] | 653 | image_binary_size = image->update_image_size; |
| 654 | vendor_code = image_binary + image_binary_size; |
| 655 | if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) && |
| 656 | (image->image_capsule_support & |
| 657 | CAPSULE_SUPPORT_AUTHENTICATION)) { |
| 658 | ret = efi_remove_auth_hdr(&image_binary, |
| 659 | &image_binary_size); |
| 660 | if (ret != EFI_SUCCESS) |
| 661 | goto out; |
| 662 | } |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 663 | |
| 664 | abort_reason = NULL; |
| 665 | ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index, |
| 666 | image_binary, |
AKASHI Takahiro | c2cc60c | 2021-07-20 14:52:05 +0900 | [diff] [blame] | 667 | image_binary_size, |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 668 | vendor_code, NULL, |
| 669 | &abort_reason)); |
| 670 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | 3e49119 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 671 | log_err("Firmware update failed: %ls\n", |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 672 | abort_reason); |
| 673 | efi_free_pool(abort_reason); |
| 674 | goto out; |
| 675 | } |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 676 | |
| 677 | if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) { |
| 678 | image_type_id = &image->update_image_type_id; |
| 679 | if (!fw_accept_os) { |
| 680 | /* |
| 681 | * The OS will not be accepting the firmware |
| 682 | * images. Set the accept bit of all the |
| 683 | * images contained in this capsule. |
| 684 | */ |
| 685 | status = fwu_accept_image(image_type_id, |
| 686 | update_index); |
| 687 | } else { |
| 688 | status = fwu_clear_accept_image(image_type_id, |
| 689 | update_index); |
| 690 | } |
| 691 | ret = fwu_to_efi_error(status); |
| 692 | if (ret != EFI_SUCCESS) { |
| 693 | log_err("Unable to %s the accept bit for the image %pUs\n", |
| 694 | fw_accept_os ? "clear" : "set", |
| 695 | image_type_id); |
| 696 | goto out; |
| 697 | } |
| 698 | |
| 699 | log_debug("%s the accepted bit for Image %pUs\n", |
| 700 | fw_accept_os ? "Cleared" : "Set", |
| 701 | image_type_id); |
| 702 | } |
| 703 | |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | out: |
| 707 | efi_free_pool(handles); |
| 708 | |
| 709 | return ret; |
| 710 | } |
| 711 | #else |
| 712 | static efi_status_t efi_capsule_update_firmware( |
| 713 | struct efi_capsule_header *capsule_data) |
| 714 | { |
| 715 | return EFI_UNSUPPORTED; |
| 716 | } |
| 717 | #endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */ |
| 718 | |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 719 | /** |
| 720 | * efi_update_capsule() - process information from operating system |
| 721 | * @capsule_header_array: Array of virtual address pointers |
| 722 | * @capsule_count: Number of pointers in capsule_header_array |
| 723 | * @scatter_gather_list: Array of physical address pointers |
| 724 | * |
| 725 | * This function implements the UpdateCapsule() runtime service. |
| 726 | * |
| 727 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 728 | * details. |
| 729 | * |
| 730 | * Return: status code |
| 731 | */ |
| 732 | efi_status_t EFIAPI efi_update_capsule( |
| 733 | struct efi_capsule_header **capsule_header_array, |
| 734 | efi_uintn_t capsule_count, |
| 735 | u64 scatter_gather_list) |
| 736 | { |
| 737 | struct efi_capsule_header *capsule; |
| 738 | unsigned int i; |
| 739 | efi_status_t ret; |
| 740 | |
Simon Glass | df7d89a | 2021-02-07 14:27:02 -0700 | [diff] [blame] | 741 | EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count, |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 742 | scatter_gather_list); |
| 743 | |
| 744 | if (!capsule_count) { |
| 745 | ret = EFI_INVALID_PARAMETER; |
| 746 | goto out; |
| 747 | } |
| 748 | |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 749 | ret = EFI_SUCCESS; |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 750 | for (i = 0, capsule = *capsule_header_array; i < capsule_count; |
| 751 | i++, capsule = *(++capsule_header_array)) { |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 752 | /* sanity check */ |
| 753 | if (capsule->header_size < sizeof(*capsule) || |
| 754 | capsule->capsule_image_size < sizeof(*capsule)) { |
Heinrich Schuchardt | 3e49119 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 755 | log_err("Capsule does not have enough data\n"); |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 756 | continue; |
| 757 | } |
| 758 | |
Heinrich Schuchardt | ce00a74 | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 759 | log_debug("Capsule[%d] (guid:%pUs)\n", |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 760 | i, &capsule->capsule_guid); |
AKASHI Takahiro | 1b7e469 | 2023-07-27 09:38:00 +0900 | [diff] [blame^] | 761 | ret = efi_capsule_update_firmware(capsule); |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 762 | if (ret != EFI_SUCCESS) |
| 763 | goto out; |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 764 | } |
Jose Marinho | 64a8aae | 2021-03-02 17:26:38 +0000 | [diff] [blame] | 765 | |
| 766 | if (IS_ENABLED(CONFIG_EFI_ESRT)) { |
| 767 | /* Rebuild the ESRT to reflect any updated FW images. */ |
| 768 | ret = efi_esrt_populate(); |
| 769 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 3e49119 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 770 | log_warning("ESRT update failed\n"); |
Jose Marinho | 64a8aae | 2021-03-02 17:26:38 +0000 | [diff] [blame] | 771 | } |
Jose Marinho | 3627cf4 | 2021-04-19 14:54:33 +0100 | [diff] [blame] | 772 | out: |
Jose Marinho | 64a8aae | 2021-03-02 17:26:38 +0000 | [diff] [blame] | 773 | |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 774 | return EFI_EXIT(ret); |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * efi_query_capsule_caps() - check if capsule is supported |
| 779 | * @capsule_header_array: Array of virtual pointers |
| 780 | * @capsule_count: Number of pointers in capsule_header_array |
| 781 | * @maximum_capsule_size: Maximum capsule size |
| 782 | * @reset_type: Type of reset needed for capsule update |
| 783 | * |
| 784 | * This function implements the QueryCapsuleCapabilities() runtime service. |
| 785 | * |
| 786 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 787 | * details. |
| 788 | * |
| 789 | * Return: status code |
| 790 | */ |
| 791 | efi_status_t EFIAPI efi_query_capsule_caps( |
| 792 | struct efi_capsule_header **capsule_header_array, |
| 793 | efi_uintn_t capsule_count, |
| 794 | u64 *maximum_capsule_size, |
| 795 | u32 *reset_type) |
| 796 | { |
| 797 | struct efi_capsule_header *capsule __attribute__((unused)); |
| 798 | unsigned int i; |
| 799 | efi_status_t ret; |
| 800 | |
Simon Glass | df7d89a | 2021-02-07 14:27:02 -0700 | [diff] [blame] | 801 | EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count, |
AKASHI Takahiro | 2bc27ca | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 802 | maximum_capsule_size, reset_type); |
| 803 | |
| 804 | if (!maximum_capsule_size) { |
| 805 | ret = EFI_INVALID_PARAMETER; |
| 806 | goto out; |
| 807 | } |
| 808 | |
| 809 | *maximum_capsule_size = U64_MAX; |
| 810 | *reset_type = EFI_RESET_COLD; |
| 811 | |
| 812 | ret = EFI_SUCCESS; |
| 813 | for (i = 0, capsule = *capsule_header_array; i < capsule_count; |
| 814 | i++, capsule = *(++capsule_header_array)) { |
| 815 | /* TODO */ |
| 816 | } |
| 817 | out: |
| 818 | return EFI_EXIT(ret); |
| 819 | } |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 820 | |
Masami Hiramatsu | 39bdf11 | 2022-03-21 22:37:45 +0900 | [diff] [blame] | 821 | /** |
| 822 | * efi_load_capsule_drivers - initialize capsule drivers |
| 823 | * |
| 824 | * Generic FMP drivers backed by DFU |
| 825 | * |
| 826 | * Return: status code |
| 827 | */ |
| 828 | efi_status_t __weak efi_load_capsule_drivers(void) |
| 829 | { |
| 830 | __maybe_unused efi_handle_t handle; |
| 831 | efi_status_t ret = EFI_SUCCESS; |
| 832 | |
| 833 | if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) { |
| 834 | handle = NULL; |
Ilias Apalodimas | 05c4c9e | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 835 | ret = efi_install_multiple_protocol_interfaces(&handle, |
| 836 | &efi_guid_firmware_management_protocol, |
| 837 | &efi_fmp_fit, |
| 838 | NULL); |
Masami Hiramatsu | 39bdf11 | 2022-03-21 22:37:45 +0900 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) { |
| 842 | handle = NULL; |
Ilias Apalodimas | 05c4c9e | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 843 | ret = efi_install_multiple_protocol_interfaces(&handle, |
| 844 | &efi_guid_firmware_management_protocol, |
| 845 | &efi_fmp_raw, |
| 846 | NULL); |
Masami Hiramatsu | 39bdf11 | 2022-03-21 22:37:45 +0900 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | return ret; |
| 850 | } |
| 851 | |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 852 | #ifdef CONFIG_EFI_CAPSULE_ON_DISK |
| 853 | /** |
| 854 | * get_dp_device - retrieve a device path from boot variable |
| 855 | * @boot_var: Boot variable name |
| 856 | * @device_dp Device path |
| 857 | * |
| 858 | * Retrieve a device patch from boot variable, @boot_var. |
| 859 | * |
| 860 | * Return: status code |
| 861 | */ |
| 862 | static efi_status_t get_dp_device(u16 *boot_var, |
| 863 | struct efi_device_path **device_dp) |
| 864 | { |
| 865 | void *buf = NULL; |
| 866 | efi_uintn_t size; |
| 867 | struct efi_load_option lo; |
| 868 | struct efi_device_path *file_dp; |
| 869 | efi_status_t ret; |
| 870 | |
| 871 | size = 0; |
| 872 | ret = efi_get_variable_int(boot_var, &efi_global_variable_guid, |
| 873 | NULL, &size, NULL, NULL); |
| 874 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 875 | buf = malloc(size); |
| 876 | if (!buf) |
| 877 | return EFI_OUT_OF_RESOURCES; |
| 878 | ret = efi_get_variable_int(boot_var, &efi_global_variable_guid, |
| 879 | NULL, &size, buf, NULL); |
| 880 | } |
| 881 | if (ret != EFI_SUCCESS) |
| 882 | return ret; |
| 883 | |
| 884 | efi_deserialize_load_option(&lo, buf, &size); |
| 885 | |
| 886 | if (lo.attributes & LOAD_OPTION_ACTIVE) { |
| 887 | efi_dp_split_file_path(lo.file_path, device_dp, &file_dp); |
| 888 | efi_free_pool(file_dp); |
| 889 | |
| 890 | ret = EFI_SUCCESS; |
| 891 | } else { |
| 892 | ret = EFI_NOT_FOUND; |
| 893 | } |
| 894 | |
| 895 | free(buf); |
| 896 | |
| 897 | return ret; |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * device_is_present_and_system_part - check if a device exists |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 902 | * |
| 903 | * Check if a device pointed to by the device path, @dp, exists and is |
| 904 | * located in UEFI system partition. |
| 905 | * |
Heinrich Schuchardt | b78631d | 2022-03-05 00:36:50 +0100 | [diff] [blame] | 906 | * @dp device path |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 907 | * Return: true - yes, false - no |
| 908 | */ |
| 909 | static bool device_is_present_and_system_part(struct efi_device_path *dp) |
| 910 | { |
| 911 | efi_handle_t handle; |
Heinrich Schuchardt | b78631d | 2022-03-05 00:36:50 +0100 | [diff] [blame] | 912 | struct efi_device_path *rem; |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 913 | |
Heinrich Schuchardt | b78631d | 2022-03-05 00:36:50 +0100 | [diff] [blame] | 914 | /* Check device exists */ |
Heinrich Schuchardt | e46ef1d | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 915 | handle = efi_dp_find_obj(dp, NULL, NULL); |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 916 | if (!handle) |
| 917 | return false; |
| 918 | |
Heinrich Schuchardt | b78631d | 2022-03-05 00:36:50 +0100 | [diff] [blame] | 919 | /* Check device is on system partition */ |
| 920 | handle = efi_dp_find_obj(dp, &efi_system_partition_guid, &rem); |
| 921 | if (!handle) |
| 922 | return false; |
| 923 | |
| 924 | return true; |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | /** |
| 928 | * find_boot_device - identify the boot device |
| 929 | * |
| 930 | * Identify the boot device from boot-related variables as UEFI |
| 931 | * specification describes and put its handle into bootdev_root. |
| 932 | * |
| 933 | * Return: status code |
| 934 | */ |
| 935 | static efi_status_t find_boot_device(void) |
| 936 | { |
| 937 | char boot_var[9]; |
| 938 | u16 boot_var16[9], *p, bootnext, *boot_order = NULL; |
| 939 | efi_uintn_t size; |
| 940 | int i, num; |
| 941 | struct efi_simple_file_system_protocol *volume; |
| 942 | struct efi_device_path *boot_dev = NULL; |
| 943 | efi_status_t ret; |
| 944 | |
| 945 | /* find active boot device in BootNext */ |
| 946 | bootnext = 0; |
| 947 | size = sizeof(bootnext); |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 948 | ret = efi_get_variable_int(u"BootNext", |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 949 | (efi_guid_t *)&efi_global_variable_guid, |
| 950 | NULL, &size, &bootnext, NULL); |
| 951 | if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) { |
| 952 | /* BootNext does exist here */ |
| 953 | if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) { |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 954 | log_err("BootNext must be 16-bit integer\n"); |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 955 | goto skip; |
| 956 | } |
| 957 | sprintf((char *)boot_var, "Boot%04X", bootnext); |
| 958 | p = boot_var16; |
| 959 | utf8_utf16_strcpy(&p, boot_var); |
| 960 | |
| 961 | ret = get_dp_device(boot_var16, &boot_dev); |
| 962 | if (ret == EFI_SUCCESS) { |
| 963 | if (device_is_present_and_system_part(boot_dev)) { |
Masami Hiramatsu | b0b1449 | 2021-07-12 18:05:17 +0900 | [diff] [blame] | 964 | goto found; |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 965 | } else { |
| 966 | efi_free_pool(boot_dev); |
| 967 | boot_dev = NULL; |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | skip: |
| 973 | /* find active boot device in BootOrder */ |
| 974 | size = 0; |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 975 | ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid, |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 976 | NULL, &size, NULL, NULL); |
| 977 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 978 | boot_order = malloc(size); |
| 979 | if (!boot_order) { |
| 980 | ret = EFI_OUT_OF_RESOURCES; |
| 981 | goto out; |
| 982 | } |
| 983 | |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 984 | ret = efi_get_variable_int(u"BootOrder", |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 985 | &efi_global_variable_guid, |
| 986 | NULL, &size, boot_order, NULL); |
| 987 | } |
| 988 | if (ret != EFI_SUCCESS) |
| 989 | goto out; |
| 990 | |
| 991 | /* check in higher order */ |
| 992 | num = size / sizeof(u16); |
| 993 | for (i = 0; i < num; i++) { |
| 994 | sprintf((char *)boot_var, "Boot%04X", boot_order[i]); |
| 995 | p = boot_var16; |
| 996 | utf8_utf16_strcpy(&p, boot_var); |
| 997 | ret = get_dp_device(boot_var16, &boot_dev); |
| 998 | if (ret != EFI_SUCCESS) |
| 999 | continue; |
| 1000 | |
| 1001 | if (device_is_present_and_system_part(boot_dev)) |
| 1002 | break; |
| 1003 | |
| 1004 | efi_free_pool(boot_dev); |
| 1005 | boot_dev = NULL; |
| 1006 | } |
Masami Hiramatsu | b0b1449 | 2021-07-12 18:05:17 +0900 | [diff] [blame] | 1007 | found: |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1008 | if (boot_dev) { |
Masami Hiramatsu | 484d7d8 | 2021-07-14 14:19:13 +0900 | [diff] [blame] | 1009 | log_debug("Boot device %pD\n", boot_dev); |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1010 | |
| 1011 | volume = efi_fs_from_path(boot_dev); |
| 1012 | if (!volume) |
| 1013 | ret = EFI_DEVICE_ERROR; |
| 1014 | else |
| 1015 | ret = EFI_CALL(volume->open_volume(volume, |
| 1016 | &bootdev_root)); |
| 1017 | efi_free_pool(boot_dev); |
| 1018 | } else { |
| 1019 | ret = EFI_NOT_FOUND; |
| 1020 | } |
AKASHI Takahiro | 2c1a684 | 2021-04-20 10:03:16 +0900 | [diff] [blame] | 1021 | out: |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1022 | free(boot_order); |
| 1023 | |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | /** |
| 1028 | * efi_capsule_scan_dir - traverse a capsule directory in boot device |
| 1029 | * @files: Array of file names |
| 1030 | * @num: Number of elements in @files |
| 1031 | * |
| 1032 | * Traverse a capsule directory in boot device. |
| 1033 | * Called by initialization code, and returns an array of capsule file |
| 1034 | * names in @files. |
| 1035 | * |
| 1036 | * Return: status code |
| 1037 | */ |
| 1038 | static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num) |
| 1039 | { |
| 1040 | struct efi_file_handle *dirh; |
| 1041 | struct efi_file_info *dirent; |
| 1042 | efi_uintn_t dirent_size, tmp_size; |
| 1043 | unsigned int count; |
| 1044 | u16 **tmp_files; |
| 1045 | efi_status_t ret; |
| 1046 | |
| 1047 | ret = find_boot_device(); |
| 1048 | if (ret == EFI_NOT_FOUND) { |
Heinrich Schuchardt | 3e49119 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1049 | log_debug("Boot device is not set\n"); |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1050 | *num = 0; |
| 1051 | return EFI_SUCCESS; |
| 1052 | } else if (ret != EFI_SUCCESS) { |
| 1053 | return EFI_DEVICE_ERROR; |
| 1054 | } |
| 1055 | |
| 1056 | /* count capsule files */ |
| 1057 | ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh, |
| 1058 | EFI_CAPSULE_DIR, |
| 1059 | EFI_FILE_MODE_READ, 0)); |
| 1060 | if (ret != EFI_SUCCESS) { |
| 1061 | *num = 0; |
| 1062 | return EFI_SUCCESS; |
| 1063 | } |
| 1064 | |
| 1065 | dirent_size = 256; |
| 1066 | dirent = malloc(dirent_size); |
| 1067 | if (!dirent) |
| 1068 | return EFI_OUT_OF_RESOURCES; |
| 1069 | |
| 1070 | count = 0; |
| 1071 | while (1) { |
| 1072 | tmp_size = dirent_size; |
| 1073 | ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent)); |
| 1074 | if (ret == EFI_BUFFER_TOO_SMALL) { |
Heinrich Schuchardt | e8287b0 | 2021-04-11 06:53:04 +0200 | [diff] [blame] | 1075 | struct efi_file_info *old_dirent = dirent; |
| 1076 | |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1077 | dirent = realloc(dirent, tmp_size); |
| 1078 | if (!dirent) { |
Heinrich Schuchardt | e8287b0 | 2021-04-11 06:53:04 +0200 | [diff] [blame] | 1079 | dirent = old_dirent; |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1080 | ret = EFI_OUT_OF_RESOURCES; |
| 1081 | goto err; |
| 1082 | } |
| 1083 | dirent_size = tmp_size; |
| 1084 | ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent)); |
| 1085 | } |
| 1086 | if (ret != EFI_SUCCESS) |
| 1087 | goto err; |
| 1088 | if (!tmp_size) |
| 1089 | break; |
| 1090 | |
Heinrich Schuchardt | 841f7a4 | 2021-02-09 17:45:33 +0100 | [diff] [blame] | 1091 | if (!(dirent->attribute & EFI_FILE_DIRECTORY)) |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1092 | count++; |
| 1093 | } |
| 1094 | |
| 1095 | ret = EFI_CALL((*dirh->setpos)(dirh, 0)); |
| 1096 | if (ret != EFI_SUCCESS) |
| 1097 | goto err; |
| 1098 | |
| 1099 | /* make a list */ |
AKASHI Takahiro | 8f1844c | 2021-01-22 10:43:27 +0900 | [diff] [blame] | 1100 | tmp_files = malloc(count * sizeof(*tmp_files)); |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1101 | if (!tmp_files) { |
| 1102 | ret = EFI_OUT_OF_RESOURCES; |
| 1103 | goto err; |
| 1104 | } |
| 1105 | |
| 1106 | count = 0; |
| 1107 | while (1) { |
| 1108 | tmp_size = dirent_size; |
| 1109 | ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent)); |
| 1110 | if (ret != EFI_SUCCESS) |
| 1111 | goto err; |
| 1112 | if (!tmp_size) |
| 1113 | break; |
| 1114 | |
| 1115 | if (!(dirent->attribute & EFI_FILE_DIRECTORY) && |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1116 | u16_strcmp(dirent->file_name, u".") && |
| 1117 | u16_strcmp(dirent->file_name, u"..")) |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1118 | tmp_files[count++] = u16_strdup(dirent->file_name); |
| 1119 | } |
| 1120 | /* ignore an error */ |
| 1121 | EFI_CALL((*dirh->close)(dirh)); |
| 1122 | |
Heinrich Schuchardt | 93cdb95 | 2022-12-29 14:44:05 +0100 | [diff] [blame] | 1123 | /* |
| 1124 | * Capsule files are applied in case insensitive alphabetic order |
| 1125 | * |
| 1126 | * TODO: special handling of rightmost period |
| 1127 | */ |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1128 | qsort(tmp_files, count, sizeof(*tmp_files), |
Heinrich Schuchardt | 93cdb95 | 2022-12-29 14:44:05 +0100 | [diff] [blame] | 1129 | (int (*)(const void *, const void *))u16_strcasecmp); |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1130 | *files = tmp_files; |
| 1131 | *num = count; |
| 1132 | ret = EFI_SUCCESS; |
| 1133 | err: |
| 1134 | free(dirent); |
| 1135 | |
| 1136 | return ret; |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * efi_capsule_read_file - read in a capsule file |
| 1141 | * @filename: File name |
| 1142 | * @capsule: Pointer to buffer for capsule |
| 1143 | * |
| 1144 | * Read a capsule file and put its content in @capsule. |
| 1145 | * |
| 1146 | * Return: status code |
| 1147 | */ |
| 1148 | static efi_status_t efi_capsule_read_file(const u16 *filename, |
| 1149 | struct efi_capsule_header **capsule) |
| 1150 | { |
| 1151 | struct efi_file_handle *dirh, *fh; |
| 1152 | struct efi_file_info *file_info = NULL; |
| 1153 | struct efi_capsule_header *buf = NULL; |
| 1154 | efi_uintn_t size; |
| 1155 | efi_status_t ret; |
| 1156 | |
| 1157 | ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh, |
| 1158 | EFI_CAPSULE_DIR, |
| 1159 | EFI_FILE_MODE_READ, 0)); |
| 1160 | if (ret != EFI_SUCCESS) |
| 1161 | return ret; |
| 1162 | ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename, |
| 1163 | EFI_FILE_MODE_READ, 0)); |
| 1164 | /* ignore an error */ |
| 1165 | EFI_CALL((*dirh->close)(dirh)); |
| 1166 | if (ret != EFI_SUCCESS) |
| 1167 | return ret; |
| 1168 | |
| 1169 | /* file size */ |
| 1170 | size = 0; |
| 1171 | ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid, |
| 1172 | &size, file_info)); |
| 1173 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 1174 | file_info = malloc(size); |
| 1175 | if (!file_info) { |
| 1176 | ret = EFI_OUT_OF_RESOURCES; |
| 1177 | goto err; |
| 1178 | } |
| 1179 | ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid, |
| 1180 | &size, file_info)); |
| 1181 | } |
| 1182 | if (ret != EFI_SUCCESS) |
| 1183 | goto err; |
| 1184 | size = file_info->file_size; |
| 1185 | free(file_info); |
| 1186 | buf = malloc(size); |
| 1187 | if (!buf) { |
| 1188 | ret = EFI_OUT_OF_RESOURCES; |
| 1189 | goto err; |
| 1190 | } |
| 1191 | |
| 1192 | /* fetch data */ |
| 1193 | ret = EFI_CALL((*fh->read)(fh, &size, buf)); |
| 1194 | if (ret == EFI_SUCCESS) { |
| 1195 | if (size >= buf->capsule_image_size) { |
| 1196 | *capsule = buf; |
| 1197 | } else { |
| 1198 | free(buf); |
| 1199 | ret = EFI_INVALID_PARAMETER; |
| 1200 | } |
| 1201 | } else { |
| 1202 | free(buf); |
| 1203 | } |
| 1204 | err: |
| 1205 | EFI_CALL((*fh->close)(fh)); |
| 1206 | |
| 1207 | return ret; |
| 1208 | } |
| 1209 | |
| 1210 | /** |
| 1211 | * efi_capsule_delete_file - delete a capsule file |
| 1212 | * @filename: File name |
| 1213 | * |
| 1214 | * Delete a capsule file from capsule directory. |
| 1215 | * |
| 1216 | * Return: status code |
| 1217 | */ |
| 1218 | static efi_status_t efi_capsule_delete_file(const u16 *filename) |
| 1219 | { |
| 1220 | struct efi_file_handle *dirh, *fh; |
| 1221 | efi_status_t ret; |
| 1222 | |
| 1223 | ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh, |
| 1224 | EFI_CAPSULE_DIR, |
| 1225 | EFI_FILE_MODE_READ, 0)); |
| 1226 | if (ret != EFI_SUCCESS) |
| 1227 | return ret; |
| 1228 | ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename, |
| 1229 | EFI_FILE_MODE_READ, 0)); |
| 1230 | /* ignore an error */ |
| 1231 | EFI_CALL((*dirh->close)(dirh)); |
| 1232 | |
Heinrich Schuchardt | e27b0ff | 2021-06-02 19:28:22 +0200 | [diff] [blame] | 1233 | if (ret == EFI_SUCCESS) |
| 1234 | ret = EFI_CALL((*fh->delete)(fh)); |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1235 | |
| 1236 | return ret; |
| 1237 | } |
| 1238 | |
| 1239 | /** |
| 1240 | * efi_capsule_scan_done - reset a scan help function |
| 1241 | * |
| 1242 | * Reset a scan help function |
| 1243 | */ |
| 1244 | static void efi_capsule_scan_done(void) |
| 1245 | { |
| 1246 | EFI_CALL((*bootdev_root->close)(bootdev_root)); |
| 1247 | bootdev_root = NULL; |
| 1248 | } |
| 1249 | |
| 1250 | /** |
Heinrich Schuchardt | 9d1564d | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1251 | * check_run_capsules() - check whether capsule update should run |
Ilias Apalodimas | 0fa5020 | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1252 | * |
| 1253 | * The spec says OsIndications must be set in order to run the capsule update |
| 1254 | * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to |
| 1255 | * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected |
Heinrich Schuchardt | 9d1564d | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1256 | * |
| 1257 | * Return: EFI_SUCCESS if update to run, EFI_NOT_FOUND otherwise |
Ilias Apalodimas | 0fa5020 | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1258 | */ |
Heinrich Schuchardt | 9d1564d | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1259 | static efi_status_t check_run_capsules(void) |
Ilias Apalodimas | 0fa5020 | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1260 | { |
Sughosh Ganu | 119fafd | 2022-06-01 23:30:39 +0530 | [diff] [blame] | 1261 | u64 os_indications = 0x0; |
Ilias Apalodimas | 0fa5020 | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1262 | efi_uintn_t size; |
Heinrich Schuchardt | 9d1564d | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1263 | efi_status_t r; |
Ilias Apalodimas | 0fa5020 | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1264 | |
| 1265 | size = sizeof(os_indications); |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1266 | r = efi_get_variable_int(u"OsIndications", &efi_global_variable_guid, |
Heinrich Schuchardt | 9d1564d | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1267 | NULL, &size, &os_indications, NULL); |
Sughosh Ganu | 119fafd | 2022-06-01 23:30:39 +0530 | [diff] [blame] | 1268 | if (!IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS) && |
| 1269 | (r != EFI_SUCCESS || size != sizeof(os_indications))) |
Heinrich Schuchardt | 9d1564d | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1270 | return EFI_NOT_FOUND; |
Ilias Apalodimas | 0fa5020 | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1271 | |
Heinrich Schuchardt | 9d1564d | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1272 | if (os_indications & |
| 1273 | EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) { |
| 1274 | os_indications &= |
| 1275 | ~EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED; |
Simon Glass | 156ccbc | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1276 | r = efi_set_variable_int(u"OsIndications", |
Heinrich Schuchardt | 9d1564d | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1277 | &efi_global_variable_guid, |
| 1278 | EFI_VARIABLE_NON_VOLATILE | |
| 1279 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1280 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1281 | sizeof(os_indications), |
| 1282 | &os_indications, false); |
| 1283 | if (r != EFI_SUCCESS) |
| 1284 | log_err("Setting %ls failed\n", L"OsIndications"); |
| 1285 | return EFI_SUCCESS; |
| 1286 | } else if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS)) { |
| 1287 | return EFI_SUCCESS; |
Sughosh Ganu | 119fafd | 2022-06-01 23:30:39 +0530 | [diff] [blame] | 1288 | } else { |
Heinrich Schuchardt | 9d1564d | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1289 | return EFI_NOT_FOUND; |
| 1290 | } |
Ilias Apalodimas | 0fa5020 | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | /** |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1294 | * efi_launch_capsule - launch capsules |
| 1295 | * |
| 1296 | * Launch all the capsules in system at boot time. |
| 1297 | * Called by efi init code |
| 1298 | * |
| 1299 | * Return: status codde |
| 1300 | */ |
| 1301 | efi_status_t efi_launch_capsules(void) |
| 1302 | { |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1303 | struct efi_capsule_header *capsule = NULL; |
| 1304 | u16 **files; |
Etienne Carriere | 4429393 | 2023-02-16 18:21:41 +0100 | [diff] [blame] | 1305 | unsigned int nfiles, index, index_max, i; |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1306 | efi_status_t ret; |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1307 | bool capsule_update = true; |
| 1308 | bool update_status = true; |
| 1309 | bool fw_accept_os = false; |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1310 | |
Heinrich Schuchardt | 9d1564d | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1311 | if (check_run_capsules() != EFI_SUCCESS) |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1312 | return EFI_SUCCESS; |
| 1313 | |
Etienne Carriere | 4429393 | 2023-02-16 18:21:41 +0100 | [diff] [blame] | 1314 | index_max = get_max_capsule(); |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1315 | index = get_last_capsule(); |
| 1316 | |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1317 | /* |
| 1318 | * Find capsules on disk. |
| 1319 | * All the capsules are collected at the beginning because |
| 1320 | * capsule files will be removed instantly. |
| 1321 | */ |
| 1322 | nfiles = 0; |
| 1323 | files = NULL; |
| 1324 | ret = efi_capsule_scan_dir(&files, &nfiles); |
| 1325 | if (ret != EFI_SUCCESS) |
| 1326 | return ret; |
| 1327 | if (!nfiles) |
| 1328 | return EFI_SUCCESS; |
| 1329 | |
| 1330 | /* Launch capsules */ |
| 1331 | for (i = 0, ++index; i < nfiles; i++, index++) { |
Heinrich Schuchardt | 3e49119 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1332 | log_debug("Applying %ls\n", files[i]); |
Etienne Carriere | 4429393 | 2023-02-16 18:21:41 +0100 | [diff] [blame] | 1333 | if (index > index_max) |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1334 | index = 0; |
| 1335 | ret = efi_capsule_read_file(files[i], &capsule); |
| 1336 | if (ret == EFI_SUCCESS) { |
Masami Hiramatsu | a6aafce | 2022-02-16 15:15:42 +0900 | [diff] [blame] | 1337 | ret = efi_capsule_update_firmware(capsule); |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1338 | if (ret != EFI_SUCCESS) { |
Masami Hiramatsu | 3e6f810 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 1339 | log_err("Applying capsule %ls failed.\n", |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 1340 | files[i]); |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1341 | update_status = false; |
| 1342 | } else { |
Masami Hiramatsu | 3e6f810 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 1343 | log_info("Applying capsule %ls succeeded.\n", |
| 1344 | files[i]); |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1345 | if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) { |
| 1346 | fwu_post_update_checks(capsule, |
| 1347 | &fw_accept_os, |
| 1348 | &capsule_update); |
| 1349 | } |
| 1350 | } |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1351 | |
Masami Hiramatsu | 5d49b32 | 2021-11-12 22:05:15 +0900 | [diff] [blame] | 1352 | /* create CapsuleXXXX */ |
| 1353 | set_capsule_result(index, capsule, ret); |
| 1354 | |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1355 | free(capsule); |
| 1356 | } else { |
Heinrich Schuchardt | 3e49119 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1357 | log_err("Reading capsule %ls failed\n", files[i]); |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1358 | update_status = false; |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1359 | } |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1360 | /* delete a capsule either in case of success or failure */ |
| 1361 | ret = efi_capsule_delete_file(files[i]); |
| 1362 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | 3e49119 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1363 | log_err("Deleting capsule %ls failed\n", |
AKASHI Takahiro | 8d99026 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 1364 | files[i]); |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1365 | } |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1366 | |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1367 | efi_capsule_scan_done(); |
| 1368 | |
Sughosh Ganu | 8679405 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1369 | if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) { |
| 1370 | if (capsule_update == true && update_status == true) { |
| 1371 | ret = fwu_post_update_process(fw_accept_os); |
| 1372 | } else if (capsule_update == true && update_status == false) { |
| 1373 | log_err("All capsules were not updated. Not updating FWU metadata\n"); |
| 1374 | } |
| 1375 | } |
| 1376 | |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1377 | for (i = 0; i < nfiles; i++) |
| 1378 | free(files[i]); |
| 1379 | free(files); |
| 1380 | |
Masami Hiramatsu | 3e6f810 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 1381 | /* |
| 1382 | * UEFI spec requires to reset system after complete processing capsule |
| 1383 | * update on the storage. |
| 1384 | */ |
Masami Hiramatsu | 7660cfe | 2022-03-21 22:37:56 +0900 | [diff] [blame] | 1385 | log_info("Reboot after firmware update.\n"); |
Masami Hiramatsu | 3e6f810 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 1386 | /* Cold reset is required for loading the new firmware. */ |
Masami Hiramatsu | 7660cfe | 2022-03-21 22:37:56 +0900 | [diff] [blame] | 1387 | sysreset_walk_halt(SYSRESET_COLD); |
Masami Hiramatsu | 3e6f810 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 1388 | hang(); |
| 1389 | /* not reach here */ |
| 1390 | |
| 1391 | return 0; |
AKASHI Takahiro | c74cd8b | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1392 | } |
| 1393 | #endif /* CONFIG_EFI_CAPSULE_ON_DISK */ |