AKASHI Takahiro | 473d9b3 | 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 | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 9 | #define LOG_CATEGORY LOGC_EFI |
| 10 | |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 11 | #include <efi_loader.h> |
| 12 | #include <efi_variable.h> |
AKASHI Takahiro | b71a0ae | 2021-10-07 15:23:32 +0900 | [diff] [blame] | 13 | #include <env.h> |
| 14 | #include <fdtdec.h> |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 15 | #include <fs.h> |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 16 | #include <fwu.h> |
Masami Hiramatsu | 4b2f8c1 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 17 | #include <hang.h> |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 18 | #include <malloc.h> |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 19 | #include <mapmem.h> |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 20 | #include <sort.h> |
Masami Hiramatsu | ff74486 | 2022-03-21 22:37:56 +0900 | [diff] [blame] | 21 | #include <sysreset.h> |
AKASHI Takahiro | b71a0ae | 2021-10-07 15:23:32 +0900 | [diff] [blame] | 22 | #include <asm/global_data.h> |
Caleb Connolly | 29cab7c | 2024-08-30 13:34:37 +0100 | [diff] [blame] | 23 | #include <u-boot/uuid.h> |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 24 | |
Sughosh Ganu | 586bb98 | 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 | b71a0ae | 2021-10-07 15:23:32 +0900 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 31 | const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID; |
AKASHI Takahiro | 0d96378 | 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 | 1cadae2 | 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 | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 42 | |
AKASHI Takahiro | 45b81954 | 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 | 6326e91 | 2023-02-16 18:21:41 +0100 | [diff] [blame] | 48 | static __maybe_unused unsigned int get_capsule_index(const u16 *variable_name) |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 49 | { |
| 50 | u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */ |
Heinrich Schuchardt | 812f6e0 | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 51 | char value[5]; |
AKASHI Takahiro | 473d9b3 | 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 | 812f6e0 | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 55 | int i; |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 56 | |
| 57 | size = sizeof(value16); |
Etienne Carriere | 6326e91 | 2023-02-16 18:21:41 +0100 | [diff] [blame] | 58 | ret = efi_get_variable_int(variable_name, &efi_guid_capsule_report, |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 59 | NULL, &size, value16, NULL); |
Heinrich Schuchardt | 812f6e0 | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 60 | if (ret != EFI_SUCCESS || size != 22 || |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 61 | u16_strncmp(value16, u"Capsule", 7)) |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 62 | goto err; |
Heinrich Schuchardt | 812f6e0 | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 63 | for (i = 0; i < 4; ++i) { |
| 64 | u16 c = value16[i + 7]; |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 65 | |
Heinrich Schuchardt | 812f6e0 | 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 | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 73 | err: |
| 74 | return index; |
| 75 | } |
| 76 | |
| 77 | /** |
Etienne Carriere | 6326e91 | 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 | 473d9b3 | 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 | 2157529 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 123 | efi_create_indexed_name(variable_name16, sizeof(variable_name16), |
| 124 | "Capsule", index); |
AKASHI Takahiro | 473d9b3 | 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 | 24adaa7 | 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 | b1fae8c | 2021-07-10 11:14:13 +0200 | [diff] [blame] | 138 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 139 | log_err("Setting %ls failed\n", variable_name16); |
Heinrich Schuchardt | b1fae8c | 2021-07-10 11:14:13 +0200 | [diff] [blame] | 140 | return; |
| 141 | } |
| 142 | |
| 143 | /* Variable CapsuleLast must not include terminating 0x0000 */ |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 144 | ret = efi_set_variable_int(u"CapsuleLast", &efi_guid_capsule_report, |
Heinrich Schuchardt | b1fae8c | 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 | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 151 | log_err("Setting %ls failed\n", u"CapsuleLast"); |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 152 | } |
| 153 | |
AKASHI Takahiro | 0d96378 | 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 | f55c6b6 | 2022-04-15 11:29:36 +0530 | [diff] [blame] | 158 | * @image_index: Image Index |
AKASHI Takahiro | 0d96378 | 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 | f55c6b6 | 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 | 0d96378 | 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 | d8dc3cb | 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 | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 193 | if (ret != EFI_SUCCESS) |
| 194 | continue; |
Heinrich Schuchardt | d8dc3cb | 2022-10-07 15:29:52 +0200 | [diff] [blame] | 195 | fmp = fmp_handler->protocol_interface; |
AKASHI Takahiro | 0d96378 | 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 | f55c6b6 | 2022-04-15 11:29:36 +0530 | [diff] [blame] | 236 | (desc->image_index == image_index) && |
AKASHI Takahiro | 0d96378 | 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 | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 246 | if (found) |
| 247 | return fmp; |
| 248 | } |
| 249 | |
| 250 | return NULL; |
| 251 | } |
| 252 | |
AKASHI Takahiro | 920671c | 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 | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 286 | #if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE) |
AKASHI Takahiro | f155bf5 | 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 | b71a0ae | 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 | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 318 | |
Sughosh Ganu | 586bb98 | 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 | 1f78c12 | 2021-08-02 08:44:31 -0600 | [diff] [blame] | 324 | void *fdt_pkey, *pkey; |
Sughosh Ganu | 586bb98 | 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 | 920671c | 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) |
Sughosh Ganu | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 345 | goto out; |
| 346 | |
AKASHI Takahiro | 920671c | 2021-07-20 14:52:05 +0900 | [diff] [blame] | 347 | auth_hdr = (struct efi_firmware_image_authentication *)capsule; |
Sughosh Ganu | 586bb98 | 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 | 586bb98 | 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); |
Dan Carpenter | 1532523 | 2023-07-27 10:16:20 +0300 | [diff] [blame] | 371 | if (!capsule_sig) { |
Sughosh Ganu | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 372 | debug("Parsing variable's pkcs7 header failed\n"); |
Sughosh Ganu | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 373 | goto out; |
| 374 | } |
| 375 | |
Simon Glass | 1f78c12 | 2021-08-02 08:44:31 -0600 | [diff] [blame] | 376 | ret = efi_get_public_key_data(&fdt_pkey, &pkey_len); |
Sughosh Ganu | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 377 | if (ret < 0) |
| 378 | goto out; |
| 379 | |
| 380 | pkey = malloc(pkey_len); |
| 381 | if (!pkey) |
| 382 | goto out; |
| 383 | |
Simon Glass | 1f78c12 | 2021-08-02 08:44:31 -0600 | [diff] [blame] | 384 | memcpy(pkey, fdt_pkey, pkey_len); |
Sughosh Ganu | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 385 | truststore = efi_build_signature_store(pkey, pkey_len); |
| 386 | if (!truststore) |
| 387 | goto out; |
| 388 | |
| 389 | /* verify signature */ |
| 390 | if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) { |
| 391 | debug("Verified\n"); |
| 392 | } else { |
| 393 | debug("Verifying variable's signature failed\n"); |
| 394 | goto out; |
| 395 | } |
| 396 | |
| 397 | status = EFI_SUCCESS; |
| 398 | |
| 399 | out: |
| 400 | efi_sigstore_free(truststore); |
| 401 | pkcs7_free_message(capsule_sig); |
| 402 | free(regs); |
| 403 | |
| 404 | return status; |
| 405 | } |
Sughosh Ganu | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 406 | #endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */ |
| 407 | |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 408 | static __maybe_unused bool fwu_empty_capsule(struct efi_capsule_header *capsule) |
| 409 | { |
| 410 | return !guidcmp(&capsule->capsule_guid, |
| 411 | &fwu_guid_os_request_fw_revert) || |
| 412 | !guidcmp(&capsule->capsule_guid, |
| 413 | &fwu_guid_os_request_fw_accept); |
| 414 | } |
| 415 | |
| 416 | static __maybe_unused efi_status_t fwu_to_efi_error(int err) |
| 417 | { |
| 418 | efi_status_t ret; |
| 419 | |
| 420 | switch(err) { |
| 421 | case 0: |
| 422 | ret = EFI_SUCCESS; |
| 423 | break; |
| 424 | case -ERANGE: |
| 425 | case -EIO: |
| 426 | ret = EFI_DEVICE_ERROR; |
| 427 | break; |
| 428 | case -EINVAL: |
| 429 | ret = EFI_INVALID_PARAMETER; |
| 430 | break; |
| 431 | case -ENODEV: |
| 432 | ret = EFI_NOT_FOUND; |
| 433 | break; |
| 434 | default: |
| 435 | ret = EFI_OUT_OF_RESOURCES; |
| 436 | } |
| 437 | |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | static __maybe_unused efi_status_t fwu_empty_capsule_process( |
| 442 | struct efi_capsule_header *capsule) |
| 443 | { |
| 444 | int status; |
| 445 | u32 active_idx; |
| 446 | efi_guid_t *image_guid; |
| 447 | efi_status_t ret = EFI_INVALID_PARAMETER; |
| 448 | |
| 449 | if (!guidcmp(&capsule->capsule_guid, |
| 450 | &fwu_guid_os_request_fw_revert)) { |
| 451 | /* |
| 452 | * One of the previously updated image has |
| 453 | * failed the OS acceptance test. OS has |
| 454 | * requested to revert back to the earlier |
| 455 | * boot index |
| 456 | */ |
| 457 | status = fwu_revert_boot_index(); |
| 458 | ret = fwu_to_efi_error(status); |
| 459 | if (ret == EFI_SUCCESS) |
| 460 | log_debug("Reverted the FWU active_index. Recommend rebooting the system\n"); |
| 461 | else |
| 462 | log_err("Failed to revert the FWU boot index\n"); |
| 463 | } else if (!guidcmp(&capsule->capsule_guid, |
| 464 | &fwu_guid_os_request_fw_accept)) { |
| 465 | /* |
| 466 | * Image accepted by the OS. Set the acceptance |
| 467 | * status for the image. |
| 468 | */ |
| 469 | image_guid = (void *)(char *)capsule + |
| 470 | capsule->header_size; |
| 471 | |
| 472 | status = fwu_get_active_index(&active_idx); |
| 473 | ret = fwu_to_efi_error(status); |
| 474 | if (ret != EFI_SUCCESS) { |
| 475 | log_err("Unable to get the active_index from the FWU metadata\n"); |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | status = fwu_accept_image(image_guid, active_idx); |
| 480 | ret = fwu_to_efi_error(status); |
| 481 | if (ret != EFI_SUCCESS) |
| 482 | log_err("Unable to set the Accept bit for the image %pUs\n", |
| 483 | image_guid); |
Sughosh Ganu | 2aa3fe5 | 2024-03-22 16:27:22 +0530 | [diff] [blame] | 484 | |
| 485 | status = fwu_state_machine_updates(0, active_idx); |
| 486 | if (status < 0) |
| 487 | ret = EFI_DEVICE_ERROR; |
| 488 | |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | static __maybe_unused void fwu_post_update_checks( |
| 495 | struct efi_capsule_header *capsule, |
| 496 | bool *fw_accept_os, bool *capsule_update) |
| 497 | { |
| 498 | if (fwu_empty_capsule(capsule)) |
| 499 | *capsule_update = false; |
| 500 | else |
| 501 | if (!*fw_accept_os) |
| 502 | *fw_accept_os = |
| 503 | capsule->flags & FW_ACCEPT_OS ? true : false; |
| 504 | } |
| 505 | |
| 506 | static __maybe_unused efi_status_t fwu_post_update_process(bool fw_accept_os) |
| 507 | { |
| 508 | int status; |
| 509 | uint update_index; |
| 510 | efi_status_t ret; |
| 511 | |
| 512 | status = fwu_plat_get_update_index(&update_index); |
| 513 | if (status < 0) { |
| 514 | log_err("Failed to get the FWU update_index value\n"); |
| 515 | return EFI_DEVICE_ERROR; |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * All the capsules have been updated successfully, |
| 520 | * update the FWU metadata. |
| 521 | */ |
| 522 | log_debug("Update Complete. Now updating active_index to %u\n", |
| 523 | update_index); |
| 524 | status = fwu_set_active_index(update_index); |
| 525 | ret = fwu_to_efi_error(status); |
| 526 | if (ret != EFI_SUCCESS) { |
| 527 | log_err("Failed to update FWU metadata index values\n"); |
| 528 | } else { |
| 529 | log_debug("Successfully updated the active_index\n"); |
Sughosh Ganu | 2aa3fe5 | 2024-03-22 16:27:22 +0530 | [diff] [blame] | 530 | status = fwu_state_machine_updates(fw_accept_os ? 1 : 0, |
| 531 | update_index); |
| 532 | if (status < 0) |
| 533 | ret = EFI_DEVICE_ERROR; |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | return ret; |
| 537 | } |
Sughosh Ganu | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 538 | |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 539 | /** |
| 540 | * efi_capsule_update_firmware - update firmware from capsule |
| 541 | * @capsule_data: Capsule |
| 542 | * |
| 543 | * Update firmware, using a capsule, @capsule_data. Loading any FMP |
| 544 | * drivers embedded in a capsule is not supported. |
| 545 | * |
| 546 | * Return: status code |
| 547 | */ |
| 548 | static efi_status_t efi_capsule_update_firmware( |
| 549 | struct efi_capsule_header *capsule_data) |
| 550 | { |
| 551 | struct efi_firmware_management_capsule_header *capsule; |
| 552 | struct efi_firmware_management_capsule_image_header *image; |
AKASHI Takahiro | 920671c | 2021-07-20 14:52:05 +0900 | [diff] [blame] | 553 | size_t capsule_size, image_binary_size; |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 554 | void *image_binary, *vendor_code; |
| 555 | efi_handle_t *handles; |
| 556 | efi_uintn_t no_handles; |
| 557 | int item; |
| 558 | struct efi_firmware_management_protocol *fmp; |
| 559 | u16 *abort_reason; |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 560 | efi_guid_t *image_type_id; |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 561 | efi_status_t ret = EFI_SUCCESS; |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 562 | int status; |
| 563 | uint update_index; |
| 564 | bool fw_accept_os; |
| 565 | |
| 566 | if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) { |
Sughosh Ganu | 20585cd | 2024-09-09 16:50:21 +0530 | [diff] [blame] | 567 | if (fwu_empty_capsule(capsule_data)) { |
| 568 | if (fwu_empty_capsule_checks_pass()) { |
| 569 | return fwu_empty_capsule_process(capsule_data); |
| 570 | } else { |
| 571 | log_err("FWU empty capsule checks failed. Cannot start update\n"); |
| 572 | return EFI_INVALID_PARAMETER; |
| 573 | } |
| 574 | } |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 575 | |
| 576 | if (!fwu_update_checks_pass()) { |
| 577 | log_err("FWU checks failed. Cannot start update\n"); |
| 578 | return EFI_INVALID_PARAMETER; |
| 579 | } |
| 580 | |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 581 | /* Obtain the update_index from the platform */ |
| 582 | status = fwu_plat_get_update_index(&update_index); |
| 583 | if (status < 0) { |
| 584 | log_err("Failed to get the FWU update_index value\n"); |
| 585 | return EFI_DEVICE_ERROR; |
| 586 | } |
| 587 | |
| 588 | fw_accept_os = capsule_data->flags & FW_ACCEPT_OS ? 0x1 : 0x0; |
| 589 | } |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 590 | |
AKASHI Takahiro | a8000b9 | 2023-07-27 09:38:00 +0900 | [diff] [blame] | 591 | if (guidcmp(&capsule_data->capsule_guid, |
| 592 | &efi_guid_firmware_management_capsule_id)) { |
| 593 | log_err("Unsupported capsule type: %pUs\n", |
| 594 | &capsule_data->capsule_guid); |
| 595 | return EFI_UNSUPPORTED; |
| 596 | } |
| 597 | |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 598 | /* sanity check */ |
| 599 | if (capsule_data->header_size < sizeof(*capsule) || |
| 600 | capsule_data->header_size >= capsule_data->capsule_image_size) |
| 601 | return EFI_INVALID_PARAMETER; |
| 602 | |
| 603 | capsule = (void *)capsule_data + capsule_data->header_size; |
| 604 | capsule_size = capsule_data->capsule_image_size |
| 605 | - capsule_data->header_size; |
| 606 | |
| 607 | if (capsule->version != 0x00000001) |
| 608 | return EFI_UNSUPPORTED; |
| 609 | |
| 610 | handles = NULL; |
| 611 | ret = EFI_CALL(efi_locate_handle_buffer( |
| 612 | BY_PROTOCOL, |
| 613 | &efi_guid_firmware_management_protocol, |
| 614 | NULL, &no_handles, (efi_handle_t **)&handles)); |
| 615 | if (ret != EFI_SUCCESS) |
| 616 | return EFI_UNSUPPORTED; |
| 617 | |
| 618 | /* Payload */ |
| 619 | for (item = capsule->embedded_driver_count; |
| 620 | item < capsule->embedded_driver_count |
| 621 | + capsule->payload_item_count; item++) { |
| 622 | /* sanity check */ |
| 623 | if ((capsule->item_offset_list[item] + sizeof(*image) |
| 624 | >= capsule_size)) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 625 | log_err("Capsule does not have enough data\n"); |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 626 | ret = EFI_INVALID_PARAMETER; |
| 627 | goto out; |
| 628 | } |
| 629 | |
| 630 | image = (void *)capsule + capsule->item_offset_list[item]; |
| 631 | |
| 632 | if (image->version != 0x00000003) { |
| 633 | ret = EFI_UNSUPPORTED; |
| 634 | goto out; |
| 635 | } |
| 636 | |
| 637 | /* find a device for update firmware */ |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 638 | fmp = efi_fmp_find(&image->update_image_type_id, |
Sughosh Ganu | f55c6b6 | 2022-04-15 11:29:36 +0530 | [diff] [blame] | 639 | image->update_image_index, |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 640 | image->update_hardware_instance, |
| 641 | handles, no_handles); |
| 642 | if (!fmp) { |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 643 | log_err("FMP driver not found for firmware type %pUs, hardware instance %lld\n", |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 644 | &image->update_image_type_id, |
| 645 | image->update_hardware_instance); |
| 646 | ret = EFI_UNSUPPORTED; |
| 647 | goto out; |
| 648 | } |
| 649 | |
| 650 | /* do update */ |
AKASHI Takahiro | 920671c | 2021-07-20 14:52:05 +0900 | [diff] [blame] | 651 | if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) && |
| 652 | !(image->image_capsule_support & |
| 653 | CAPSULE_SUPPORT_AUTHENTICATION)) { |
| 654 | /* no signature */ |
| 655 | ret = EFI_SECURITY_VIOLATION; |
| 656 | goto out; |
| 657 | } |
| 658 | |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 659 | image_binary = (void *)image + sizeof(*image); |
AKASHI Takahiro | 920671c | 2021-07-20 14:52:05 +0900 | [diff] [blame] | 660 | image_binary_size = image->update_image_size; |
| 661 | vendor_code = image_binary + image_binary_size; |
| 662 | if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) && |
| 663 | (image->image_capsule_support & |
| 664 | CAPSULE_SUPPORT_AUTHENTICATION)) { |
| 665 | ret = efi_remove_auth_hdr(&image_binary, |
| 666 | &image_binary_size); |
| 667 | if (ret != EFI_SUCCESS) |
| 668 | goto out; |
| 669 | } |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 670 | |
| 671 | abort_reason = NULL; |
| 672 | ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index, |
| 673 | image_binary, |
AKASHI Takahiro | 920671c | 2021-07-20 14:52:05 +0900 | [diff] [blame] | 674 | image_binary_size, |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 675 | vendor_code, NULL, |
| 676 | &abort_reason)); |
| 677 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 678 | log_err("Firmware update failed: %ls\n", |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 679 | abort_reason); |
| 680 | efi_free_pool(abort_reason); |
| 681 | goto out; |
| 682 | } |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 683 | |
| 684 | if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) { |
| 685 | image_type_id = &image->update_image_type_id; |
| 686 | if (!fw_accept_os) { |
| 687 | /* |
| 688 | * The OS will not be accepting the firmware |
| 689 | * images. Set the accept bit of all the |
| 690 | * images contained in this capsule. |
| 691 | */ |
| 692 | status = fwu_accept_image(image_type_id, |
| 693 | update_index); |
| 694 | } else { |
| 695 | status = fwu_clear_accept_image(image_type_id, |
| 696 | update_index); |
| 697 | } |
| 698 | ret = fwu_to_efi_error(status); |
| 699 | if (ret != EFI_SUCCESS) { |
| 700 | log_err("Unable to %s the accept bit for the image %pUs\n", |
| 701 | fw_accept_os ? "clear" : "set", |
| 702 | image_type_id); |
| 703 | goto out; |
| 704 | } |
| 705 | |
| 706 | log_debug("%s the accepted bit for Image %pUs\n", |
| 707 | fw_accept_os ? "Cleared" : "Set", |
| 708 | image_type_id); |
| 709 | } |
| 710 | |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | out: |
| 714 | efi_free_pool(handles); |
| 715 | |
| 716 | return ret; |
| 717 | } |
| 718 | #else |
| 719 | static efi_status_t efi_capsule_update_firmware( |
| 720 | struct efi_capsule_header *capsule_data) |
| 721 | { |
| 722 | return EFI_UNSUPPORTED; |
| 723 | } |
| 724 | #endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */ |
| 725 | |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 726 | /** |
| 727 | * efi_update_capsule() - process information from operating system |
| 728 | * @capsule_header_array: Array of virtual address pointers |
| 729 | * @capsule_count: Number of pointers in capsule_header_array |
| 730 | * @scatter_gather_list: Array of physical address pointers |
| 731 | * |
| 732 | * This function implements the UpdateCapsule() runtime service. |
| 733 | * |
| 734 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 735 | * details. |
| 736 | * |
| 737 | * Return: status code |
| 738 | */ |
| 739 | efi_status_t EFIAPI efi_update_capsule( |
| 740 | struct efi_capsule_header **capsule_header_array, |
| 741 | efi_uintn_t capsule_count, |
| 742 | u64 scatter_gather_list) |
| 743 | { |
| 744 | struct efi_capsule_header *capsule; |
| 745 | unsigned int i; |
| 746 | efi_status_t ret; |
| 747 | |
Simon Glass | 83698b2 | 2021-02-07 14:27:02 -0700 | [diff] [blame] | 748 | EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count, |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 749 | scatter_gather_list); |
| 750 | |
| 751 | if (!capsule_count) { |
| 752 | ret = EFI_INVALID_PARAMETER; |
| 753 | goto out; |
| 754 | } |
| 755 | |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 756 | ret = EFI_SUCCESS; |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 757 | for (i = 0, capsule = *capsule_header_array; i < capsule_count; |
| 758 | i++, capsule = *(++capsule_header_array)) { |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 759 | /* sanity check */ |
| 760 | if (capsule->header_size < sizeof(*capsule) || |
| 761 | capsule->capsule_image_size < sizeof(*capsule)) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 762 | log_err("Capsule does not have enough data\n"); |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 763 | continue; |
| 764 | } |
| 765 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 766 | log_debug("Capsule[%d] (guid:%pUs)\n", |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 767 | i, &capsule->capsule_guid); |
AKASHI Takahiro | a8000b9 | 2023-07-27 09:38:00 +0900 | [diff] [blame] | 768 | ret = efi_capsule_update_firmware(capsule); |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 769 | if (ret != EFI_SUCCESS) |
| 770 | goto out; |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 771 | } |
Jose Marinho | ebb61ee | 2021-03-02 17:26:38 +0000 | [diff] [blame] | 772 | |
| 773 | if (IS_ENABLED(CONFIG_EFI_ESRT)) { |
| 774 | /* Rebuild the ESRT to reflect any updated FW images. */ |
| 775 | ret = efi_esrt_populate(); |
| 776 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 777 | log_warning("ESRT update failed\n"); |
Jose Marinho | ebb61ee | 2021-03-02 17:26:38 +0000 | [diff] [blame] | 778 | } |
Jose Marinho | af886ce | 2021-04-19 14:54:33 +0100 | [diff] [blame] | 779 | out: |
Jose Marinho | ebb61ee | 2021-03-02 17:26:38 +0000 | [diff] [blame] | 780 | |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 781 | return EFI_EXIT(ret); |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * efi_query_capsule_caps() - check if capsule is supported |
| 786 | * @capsule_header_array: Array of virtual pointers |
| 787 | * @capsule_count: Number of pointers in capsule_header_array |
| 788 | * @maximum_capsule_size: Maximum capsule size |
| 789 | * @reset_type: Type of reset needed for capsule update |
| 790 | * |
| 791 | * This function implements the QueryCapsuleCapabilities() runtime service. |
| 792 | * |
| 793 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 794 | * details. |
| 795 | * |
| 796 | * Return: status code |
| 797 | */ |
| 798 | efi_status_t EFIAPI efi_query_capsule_caps( |
| 799 | struct efi_capsule_header **capsule_header_array, |
| 800 | efi_uintn_t capsule_count, |
| 801 | u64 *maximum_capsule_size, |
| 802 | u32 *reset_type) |
| 803 | { |
| 804 | struct efi_capsule_header *capsule __attribute__((unused)); |
| 805 | unsigned int i; |
| 806 | efi_status_t ret; |
| 807 | |
Simon Glass | 83698b2 | 2021-02-07 14:27:02 -0700 | [diff] [blame] | 808 | EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count, |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 809 | maximum_capsule_size, reset_type); |
| 810 | |
| 811 | if (!maximum_capsule_size) { |
| 812 | ret = EFI_INVALID_PARAMETER; |
| 813 | goto out; |
| 814 | } |
| 815 | |
| 816 | *maximum_capsule_size = U64_MAX; |
| 817 | *reset_type = EFI_RESET_COLD; |
| 818 | |
| 819 | ret = EFI_SUCCESS; |
| 820 | for (i = 0, capsule = *capsule_header_array; i < capsule_count; |
| 821 | i++, capsule = *(++capsule_header_array)) { |
| 822 | /* TODO */ |
| 823 | } |
| 824 | out: |
| 825 | return EFI_EXIT(ret); |
| 826 | } |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 827 | |
Masami Hiramatsu | d306caa | 2022-03-21 22:37:45 +0900 | [diff] [blame] | 828 | /** |
| 829 | * efi_load_capsule_drivers - initialize capsule drivers |
| 830 | * |
| 831 | * Generic FMP drivers backed by DFU |
| 832 | * |
| 833 | * Return: status code |
| 834 | */ |
| 835 | efi_status_t __weak efi_load_capsule_drivers(void) |
| 836 | { |
| 837 | __maybe_unused efi_handle_t handle; |
| 838 | efi_status_t ret = EFI_SUCCESS; |
| 839 | |
| 840 | if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) { |
| 841 | handle = NULL; |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 842 | ret = efi_install_multiple_protocol_interfaces(&handle, |
| 843 | &efi_guid_firmware_management_protocol, |
| 844 | &efi_fmp_fit, |
| 845 | NULL); |
Masami Hiramatsu | d306caa | 2022-03-21 22:37:45 +0900 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) { |
| 849 | handle = NULL; |
Ilias Apalodimas | 8ac0ebe | 2022-10-06 16:08:46 +0300 | [diff] [blame] | 850 | ret = efi_install_multiple_protocol_interfaces(&handle, |
| 851 | &efi_guid_firmware_management_protocol, |
| 852 | &efi_fmp_raw, |
| 853 | NULL); |
Masami Hiramatsu | d306caa | 2022-03-21 22:37:45 +0900 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | return ret; |
| 857 | } |
| 858 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 859 | #ifdef CONFIG_EFI_CAPSULE_ON_DISK |
| 860 | /** |
| 861 | * get_dp_device - retrieve a device path from boot variable |
| 862 | * @boot_var: Boot variable name |
| 863 | * @device_dp Device path |
| 864 | * |
| 865 | * Retrieve a device patch from boot variable, @boot_var. |
| 866 | * |
| 867 | * Return: status code |
| 868 | */ |
| 869 | static efi_status_t get_dp_device(u16 *boot_var, |
| 870 | struct efi_device_path **device_dp) |
| 871 | { |
| 872 | void *buf = NULL; |
| 873 | efi_uintn_t size; |
| 874 | struct efi_load_option lo; |
| 875 | struct efi_device_path *file_dp; |
| 876 | efi_status_t ret; |
| 877 | |
| 878 | size = 0; |
| 879 | ret = efi_get_variable_int(boot_var, &efi_global_variable_guid, |
| 880 | NULL, &size, NULL, NULL); |
| 881 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 882 | buf = malloc(size); |
| 883 | if (!buf) |
| 884 | return EFI_OUT_OF_RESOURCES; |
| 885 | ret = efi_get_variable_int(boot_var, &efi_global_variable_guid, |
| 886 | NULL, &size, buf, NULL); |
| 887 | } |
| 888 | if (ret != EFI_SUCCESS) |
| 889 | return ret; |
| 890 | |
| 891 | efi_deserialize_load_option(&lo, buf, &size); |
| 892 | |
| 893 | if (lo.attributes & LOAD_OPTION_ACTIVE) { |
| 894 | efi_dp_split_file_path(lo.file_path, device_dp, &file_dp); |
| 895 | efi_free_pool(file_dp); |
| 896 | |
| 897 | ret = EFI_SUCCESS; |
| 898 | } else { |
| 899 | ret = EFI_NOT_FOUND; |
| 900 | } |
| 901 | |
| 902 | free(buf); |
| 903 | |
| 904 | return ret; |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * device_is_present_and_system_part - check if a device exists |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 909 | * |
| 910 | * Check if a device pointed to by the device path, @dp, exists and is |
| 911 | * located in UEFI system partition. |
| 912 | * |
Heinrich Schuchardt | a76fc03 | 2022-03-05 00:36:50 +0100 | [diff] [blame] | 913 | * @dp device path |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 914 | * Return: true - yes, false - no |
| 915 | */ |
| 916 | static bool device_is_present_and_system_part(struct efi_device_path *dp) |
| 917 | { |
| 918 | efi_handle_t handle; |
Heinrich Schuchardt | a76fc03 | 2022-03-05 00:36:50 +0100 | [diff] [blame] | 919 | struct efi_device_path *rem; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 920 | |
Heinrich Schuchardt | a76fc03 | 2022-03-05 00:36:50 +0100 | [diff] [blame] | 921 | /* Check device exists */ |
Heinrich Schuchardt | 0a04a41 | 2022-03-19 06:35:43 +0100 | [diff] [blame] | 922 | handle = efi_dp_find_obj(dp, NULL, NULL); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 923 | if (!handle) |
| 924 | return false; |
| 925 | |
Heinrich Schuchardt | a76fc03 | 2022-03-05 00:36:50 +0100 | [diff] [blame] | 926 | /* Check device is on system partition */ |
| 927 | handle = efi_dp_find_obj(dp, &efi_system_partition_guid, &rem); |
| 928 | if (!handle) |
| 929 | return false; |
| 930 | |
| 931 | return true; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /** |
| 935 | * find_boot_device - identify the boot device |
| 936 | * |
| 937 | * Identify the boot device from boot-related variables as UEFI |
| 938 | * specification describes and put its handle into bootdev_root. |
| 939 | * |
| 940 | * Return: status code |
| 941 | */ |
| 942 | static efi_status_t find_boot_device(void) |
| 943 | { |
| 944 | char boot_var[9]; |
| 945 | u16 boot_var16[9], *p, bootnext, *boot_order = NULL; |
| 946 | efi_uintn_t size; |
| 947 | int i, num; |
| 948 | struct efi_simple_file_system_protocol *volume; |
| 949 | struct efi_device_path *boot_dev = NULL; |
| 950 | efi_status_t ret; |
| 951 | |
| 952 | /* find active boot device in BootNext */ |
| 953 | bootnext = 0; |
| 954 | size = sizeof(bootnext); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 955 | ret = efi_get_variable_int(u"BootNext", |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 956 | (efi_guid_t *)&efi_global_variable_guid, |
| 957 | NULL, &size, &bootnext, NULL); |
| 958 | if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) { |
| 959 | /* BootNext does exist here */ |
| 960 | if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) { |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 961 | log_err("BootNext must be 16-bit integer\n"); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 962 | goto skip; |
| 963 | } |
| 964 | sprintf((char *)boot_var, "Boot%04X", bootnext); |
| 965 | p = boot_var16; |
| 966 | utf8_utf16_strcpy(&p, boot_var); |
| 967 | |
| 968 | ret = get_dp_device(boot_var16, &boot_dev); |
| 969 | if (ret == EFI_SUCCESS) { |
| 970 | if (device_is_present_and_system_part(boot_dev)) { |
Masami Hiramatsu | 1016575 | 2021-07-12 18:05:17 +0900 | [diff] [blame] | 971 | goto found; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 972 | } else { |
| 973 | efi_free_pool(boot_dev); |
| 974 | boot_dev = NULL; |
| 975 | } |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | skip: |
| 980 | /* find active boot device in BootOrder */ |
| 981 | size = 0; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 982 | ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid, |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 983 | NULL, &size, NULL, NULL); |
| 984 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 985 | boot_order = malloc(size); |
| 986 | if (!boot_order) { |
| 987 | ret = EFI_OUT_OF_RESOURCES; |
| 988 | goto out; |
| 989 | } |
| 990 | |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 991 | ret = efi_get_variable_int(u"BootOrder", |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 992 | &efi_global_variable_guid, |
| 993 | NULL, &size, boot_order, NULL); |
| 994 | } |
| 995 | if (ret != EFI_SUCCESS) |
| 996 | goto out; |
| 997 | |
| 998 | /* check in higher order */ |
| 999 | num = size / sizeof(u16); |
| 1000 | for (i = 0; i < num; i++) { |
| 1001 | sprintf((char *)boot_var, "Boot%04X", boot_order[i]); |
| 1002 | p = boot_var16; |
| 1003 | utf8_utf16_strcpy(&p, boot_var); |
| 1004 | ret = get_dp_device(boot_var16, &boot_dev); |
| 1005 | if (ret != EFI_SUCCESS) |
| 1006 | continue; |
| 1007 | |
| 1008 | if (device_is_present_and_system_part(boot_dev)) |
| 1009 | break; |
| 1010 | |
| 1011 | efi_free_pool(boot_dev); |
| 1012 | boot_dev = NULL; |
| 1013 | } |
Masami Hiramatsu | 1016575 | 2021-07-12 18:05:17 +0900 | [diff] [blame] | 1014 | found: |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1015 | if (boot_dev) { |
Masami Hiramatsu | d9763bb | 2021-07-14 14:19:13 +0900 | [diff] [blame] | 1016 | log_debug("Boot device %pD\n", boot_dev); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1017 | |
| 1018 | volume = efi_fs_from_path(boot_dev); |
| 1019 | if (!volume) |
| 1020 | ret = EFI_DEVICE_ERROR; |
| 1021 | else |
| 1022 | ret = EFI_CALL(volume->open_volume(volume, |
| 1023 | &bootdev_root)); |
| 1024 | efi_free_pool(boot_dev); |
| 1025 | } else { |
| 1026 | ret = EFI_NOT_FOUND; |
| 1027 | } |
AKASHI Takahiro | fa390e6 | 2021-04-20 10:03:16 +0900 | [diff] [blame] | 1028 | out: |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1029 | free(boot_order); |
| 1030 | |
| 1031 | return ret; |
| 1032 | } |
| 1033 | |
| 1034 | /** |
| 1035 | * efi_capsule_scan_dir - traverse a capsule directory in boot device |
| 1036 | * @files: Array of file names |
| 1037 | * @num: Number of elements in @files |
| 1038 | * |
| 1039 | * Traverse a capsule directory in boot device. |
| 1040 | * Called by initialization code, and returns an array of capsule file |
| 1041 | * names in @files. |
| 1042 | * |
| 1043 | * Return: status code |
| 1044 | */ |
| 1045 | static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num) |
| 1046 | { |
| 1047 | struct efi_file_handle *dirh; |
| 1048 | struct efi_file_info *dirent; |
| 1049 | efi_uintn_t dirent_size, tmp_size; |
| 1050 | unsigned int count; |
| 1051 | u16 **tmp_files; |
| 1052 | efi_status_t ret; |
| 1053 | |
| 1054 | ret = find_boot_device(); |
| 1055 | if (ret == EFI_NOT_FOUND) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1056 | log_debug("Boot device is not set\n"); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1057 | *num = 0; |
| 1058 | return EFI_SUCCESS; |
| 1059 | } else if (ret != EFI_SUCCESS) { |
| 1060 | return EFI_DEVICE_ERROR; |
| 1061 | } |
| 1062 | |
| 1063 | /* count capsule files */ |
| 1064 | ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh, |
| 1065 | EFI_CAPSULE_DIR, |
| 1066 | EFI_FILE_MODE_READ, 0)); |
| 1067 | if (ret != EFI_SUCCESS) { |
| 1068 | *num = 0; |
| 1069 | return EFI_SUCCESS; |
| 1070 | } |
| 1071 | |
| 1072 | dirent_size = 256; |
| 1073 | dirent = malloc(dirent_size); |
| 1074 | if (!dirent) |
| 1075 | return EFI_OUT_OF_RESOURCES; |
| 1076 | |
| 1077 | count = 0; |
| 1078 | while (1) { |
| 1079 | tmp_size = dirent_size; |
| 1080 | ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent)); |
| 1081 | if (ret == EFI_BUFFER_TOO_SMALL) { |
Heinrich Schuchardt | aa27e5d | 2021-04-11 06:53:04 +0200 | [diff] [blame] | 1082 | struct efi_file_info *old_dirent = dirent; |
| 1083 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1084 | dirent = realloc(dirent, tmp_size); |
| 1085 | if (!dirent) { |
Heinrich Schuchardt | aa27e5d | 2021-04-11 06:53:04 +0200 | [diff] [blame] | 1086 | dirent = old_dirent; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1087 | ret = EFI_OUT_OF_RESOURCES; |
| 1088 | goto err; |
| 1089 | } |
| 1090 | dirent_size = tmp_size; |
| 1091 | ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent)); |
| 1092 | } |
| 1093 | if (ret != EFI_SUCCESS) |
| 1094 | goto err; |
| 1095 | if (!tmp_size) |
| 1096 | break; |
| 1097 | |
Heinrich Schuchardt | 76b708a | 2021-02-09 17:45:33 +0100 | [diff] [blame] | 1098 | if (!(dirent->attribute & EFI_FILE_DIRECTORY)) |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1099 | count++; |
| 1100 | } |
| 1101 | |
| 1102 | ret = EFI_CALL((*dirh->setpos)(dirh, 0)); |
| 1103 | if (ret != EFI_SUCCESS) |
| 1104 | goto err; |
| 1105 | |
| 1106 | /* make a list */ |
AKASHI Takahiro | c8fc12f | 2021-01-22 10:43:27 +0900 | [diff] [blame] | 1107 | tmp_files = malloc(count * sizeof(*tmp_files)); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1108 | if (!tmp_files) { |
| 1109 | ret = EFI_OUT_OF_RESOURCES; |
| 1110 | goto err; |
| 1111 | } |
| 1112 | |
| 1113 | count = 0; |
| 1114 | while (1) { |
| 1115 | tmp_size = dirent_size; |
| 1116 | ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent)); |
| 1117 | if (ret != EFI_SUCCESS) |
| 1118 | goto err; |
| 1119 | if (!tmp_size) |
| 1120 | break; |
| 1121 | |
| 1122 | if (!(dirent->attribute & EFI_FILE_DIRECTORY) && |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1123 | u16_strcmp(dirent->file_name, u".") && |
| 1124 | u16_strcmp(dirent->file_name, u"..")) |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1125 | tmp_files[count++] = u16_strdup(dirent->file_name); |
| 1126 | } |
| 1127 | /* ignore an error */ |
| 1128 | EFI_CALL((*dirh->close)(dirh)); |
| 1129 | |
Heinrich Schuchardt | ba0a2ad | 2022-12-29 14:44:05 +0100 | [diff] [blame] | 1130 | /* |
| 1131 | * Capsule files are applied in case insensitive alphabetic order |
| 1132 | * |
| 1133 | * TODO: special handling of rightmost period |
| 1134 | */ |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1135 | qsort(tmp_files, count, sizeof(*tmp_files), |
Heinrich Schuchardt | ba0a2ad | 2022-12-29 14:44:05 +0100 | [diff] [blame] | 1136 | (int (*)(const void *, const void *))u16_strcasecmp); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1137 | *files = tmp_files; |
| 1138 | *num = count; |
| 1139 | ret = EFI_SUCCESS; |
| 1140 | err: |
| 1141 | free(dirent); |
| 1142 | |
| 1143 | return ret; |
| 1144 | } |
| 1145 | |
| 1146 | /** |
| 1147 | * efi_capsule_read_file - read in a capsule file |
| 1148 | * @filename: File name |
| 1149 | * @capsule: Pointer to buffer for capsule |
| 1150 | * |
| 1151 | * Read a capsule file and put its content in @capsule. |
| 1152 | * |
| 1153 | * Return: status code |
| 1154 | */ |
| 1155 | static efi_status_t efi_capsule_read_file(const u16 *filename, |
| 1156 | struct efi_capsule_header **capsule) |
| 1157 | { |
| 1158 | struct efi_file_handle *dirh, *fh; |
| 1159 | struct efi_file_info *file_info = NULL; |
| 1160 | struct efi_capsule_header *buf = NULL; |
| 1161 | efi_uintn_t size; |
| 1162 | efi_status_t ret; |
| 1163 | |
| 1164 | ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh, |
| 1165 | EFI_CAPSULE_DIR, |
| 1166 | EFI_FILE_MODE_READ, 0)); |
| 1167 | if (ret != EFI_SUCCESS) |
| 1168 | return ret; |
| 1169 | ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename, |
| 1170 | EFI_FILE_MODE_READ, 0)); |
| 1171 | /* ignore an error */ |
| 1172 | EFI_CALL((*dirh->close)(dirh)); |
| 1173 | if (ret != EFI_SUCCESS) |
| 1174 | return ret; |
| 1175 | |
| 1176 | /* file size */ |
| 1177 | size = 0; |
| 1178 | ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid, |
| 1179 | &size, file_info)); |
| 1180 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 1181 | file_info = malloc(size); |
| 1182 | if (!file_info) { |
| 1183 | ret = EFI_OUT_OF_RESOURCES; |
| 1184 | goto err; |
| 1185 | } |
| 1186 | ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid, |
| 1187 | &size, file_info)); |
| 1188 | } |
| 1189 | if (ret != EFI_SUCCESS) |
| 1190 | goto err; |
| 1191 | size = file_info->file_size; |
| 1192 | free(file_info); |
| 1193 | buf = malloc(size); |
| 1194 | if (!buf) { |
| 1195 | ret = EFI_OUT_OF_RESOURCES; |
| 1196 | goto err; |
| 1197 | } |
| 1198 | |
| 1199 | /* fetch data */ |
| 1200 | ret = EFI_CALL((*fh->read)(fh, &size, buf)); |
| 1201 | if (ret == EFI_SUCCESS) { |
| 1202 | if (size >= buf->capsule_image_size) { |
| 1203 | *capsule = buf; |
| 1204 | } else { |
| 1205 | free(buf); |
| 1206 | ret = EFI_INVALID_PARAMETER; |
| 1207 | } |
| 1208 | } else { |
| 1209 | free(buf); |
| 1210 | } |
| 1211 | err: |
| 1212 | EFI_CALL((*fh->close)(fh)); |
| 1213 | |
| 1214 | return ret; |
| 1215 | } |
| 1216 | |
| 1217 | /** |
| 1218 | * efi_capsule_delete_file - delete a capsule file |
| 1219 | * @filename: File name |
| 1220 | * |
| 1221 | * Delete a capsule file from capsule directory. |
| 1222 | * |
| 1223 | * Return: status code |
| 1224 | */ |
| 1225 | static efi_status_t efi_capsule_delete_file(const u16 *filename) |
| 1226 | { |
| 1227 | struct efi_file_handle *dirh, *fh; |
| 1228 | efi_status_t ret; |
| 1229 | |
| 1230 | ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh, |
| 1231 | EFI_CAPSULE_DIR, |
| 1232 | EFI_FILE_MODE_READ, 0)); |
| 1233 | if (ret != EFI_SUCCESS) |
| 1234 | return ret; |
| 1235 | ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename, |
| 1236 | EFI_FILE_MODE_READ, 0)); |
| 1237 | /* ignore an error */ |
| 1238 | EFI_CALL((*dirh->close)(dirh)); |
| 1239 | |
Heinrich Schuchardt | e5c2281 | 2021-06-02 19:28:22 +0200 | [diff] [blame] | 1240 | if (ret == EFI_SUCCESS) |
| 1241 | ret = EFI_CALL((*fh->delete)(fh)); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1242 | |
| 1243 | return ret; |
| 1244 | } |
| 1245 | |
| 1246 | /** |
| 1247 | * efi_capsule_scan_done - reset a scan help function |
| 1248 | * |
| 1249 | * Reset a scan help function |
| 1250 | */ |
| 1251 | static void efi_capsule_scan_done(void) |
| 1252 | { |
| 1253 | EFI_CALL((*bootdev_root->close)(bootdev_root)); |
| 1254 | bootdev_root = NULL; |
| 1255 | } |
| 1256 | |
| 1257 | /** |
Heinrich Schuchardt | e9e8499 | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1258 | * check_run_capsules() - check whether capsule update should run |
Ilias Apalodimas | a38d0cb | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1259 | * |
| 1260 | * The spec says OsIndications must be set in order to run the capsule update |
| 1261 | * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to |
| 1262 | * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected |
Heinrich Schuchardt | e9e8499 | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1263 | * |
| 1264 | * Return: EFI_SUCCESS if update to run, EFI_NOT_FOUND otherwise |
Ilias Apalodimas | a38d0cb | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1265 | */ |
Heinrich Schuchardt | e9e8499 | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1266 | static efi_status_t check_run_capsules(void) |
Ilias Apalodimas | a38d0cb | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1267 | { |
Sughosh Ganu | 0f5ca5c | 2022-06-01 23:30:39 +0530 | [diff] [blame] | 1268 | u64 os_indications = 0x0; |
Ilias Apalodimas | a38d0cb | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1269 | efi_uintn_t size; |
Heinrich Schuchardt | e9e8499 | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1270 | efi_status_t r; |
Ilias Apalodimas | a38d0cb | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1271 | |
| 1272 | size = sizeof(os_indications); |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1273 | r = efi_get_variable_int(u"OsIndications", &efi_global_variable_guid, |
Heinrich Schuchardt | e9e8499 | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1274 | NULL, &size, &os_indications, NULL); |
Sughosh Ganu | 0f5ca5c | 2022-06-01 23:30:39 +0530 | [diff] [blame] | 1275 | if (!IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS) && |
| 1276 | (r != EFI_SUCCESS || size != sizeof(os_indications))) |
Heinrich Schuchardt | e9e8499 | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1277 | return EFI_NOT_FOUND; |
Ilias Apalodimas | a38d0cb | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1278 | |
Heinrich Schuchardt | e9e8499 | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1279 | if (os_indications & |
| 1280 | EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) { |
| 1281 | os_indications &= |
| 1282 | ~EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1283 | r = efi_set_variable_int(u"OsIndications", |
Heinrich Schuchardt | e9e8499 | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1284 | &efi_global_variable_guid, |
| 1285 | EFI_VARIABLE_NON_VOLATILE | |
| 1286 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 1287 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 1288 | sizeof(os_indications), |
| 1289 | &os_indications, false); |
| 1290 | if (r != EFI_SUCCESS) |
| 1291 | log_err("Setting %ls failed\n", L"OsIndications"); |
| 1292 | return EFI_SUCCESS; |
| 1293 | } else if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS)) { |
| 1294 | return EFI_SUCCESS; |
Sughosh Ganu | 0f5ca5c | 2022-06-01 23:30:39 +0530 | [diff] [blame] | 1295 | } else { |
Heinrich Schuchardt | e9e8499 | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1296 | return EFI_NOT_FOUND; |
| 1297 | } |
Ilias Apalodimas | a38d0cb | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | /** |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1301 | * efi_launch_capsule - launch capsules |
| 1302 | * |
| 1303 | * Launch all the capsules in system at boot time. |
| 1304 | * Called by efi init code |
| 1305 | * |
| 1306 | * Return: status codde |
| 1307 | */ |
| 1308 | efi_status_t efi_launch_capsules(void) |
| 1309 | { |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1310 | struct efi_capsule_header *capsule = NULL; |
| 1311 | u16 **files; |
Etienne Carriere | 6326e91 | 2023-02-16 18:21:41 +0100 | [diff] [blame] | 1312 | unsigned int nfiles, index, index_max, i; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1313 | efi_status_t ret; |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1314 | bool capsule_update = true; |
| 1315 | bool update_status = true; |
| 1316 | bool fw_accept_os = false; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1317 | |
Heinrich Schuchardt | e9e8499 | 2021-11-20 11:53:12 +0100 | [diff] [blame] | 1318 | if (check_run_capsules() != EFI_SUCCESS) |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1319 | return EFI_SUCCESS; |
| 1320 | |
Etienne Carriere | 6326e91 | 2023-02-16 18:21:41 +0100 | [diff] [blame] | 1321 | index_max = get_max_capsule(); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1322 | index = get_last_capsule(); |
| 1323 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1324 | /* |
| 1325 | * Find capsules on disk. |
| 1326 | * All the capsules are collected at the beginning because |
| 1327 | * capsule files will be removed instantly. |
| 1328 | */ |
| 1329 | nfiles = 0; |
| 1330 | files = NULL; |
| 1331 | ret = efi_capsule_scan_dir(&files, &nfiles); |
| 1332 | if (ret != EFI_SUCCESS) |
| 1333 | return ret; |
| 1334 | if (!nfiles) |
| 1335 | return EFI_SUCCESS; |
| 1336 | |
| 1337 | /* Launch capsules */ |
| 1338 | for (i = 0, ++index; i < nfiles; i++, index++) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1339 | log_debug("Applying %ls\n", files[i]); |
Etienne Carriere | 6326e91 | 2023-02-16 18:21:41 +0100 | [diff] [blame] | 1340 | if (index > index_max) |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1341 | index = 0; |
| 1342 | ret = efi_capsule_read_file(files[i], &capsule); |
| 1343 | if (ret == EFI_SUCCESS) { |
Masami Hiramatsu | 3454a69 | 2022-02-16 15:15:42 +0900 | [diff] [blame] | 1344 | ret = efi_capsule_update_firmware(capsule); |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1345 | if (ret != EFI_SUCCESS) { |
Masami Hiramatsu | 4b2f8c1 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 1346 | log_err("Applying capsule %ls failed.\n", |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 1347 | files[i]); |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1348 | update_status = false; |
| 1349 | } else { |
Masami Hiramatsu | 4b2f8c1 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 1350 | log_info("Applying capsule %ls succeeded.\n", |
| 1351 | files[i]); |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1352 | if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) { |
| 1353 | fwu_post_update_checks(capsule, |
| 1354 | &fw_accept_os, |
| 1355 | &capsule_update); |
| 1356 | } |
| 1357 | } |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1358 | |
Masami Hiramatsu | 1001a10 | 2021-11-12 22:05:15 +0900 | [diff] [blame] | 1359 | /* create CapsuleXXXX */ |
| 1360 | set_capsule_result(index, capsule, ret); |
| 1361 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1362 | free(capsule); |
| 1363 | } else { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1364 | log_err("Reading capsule %ls failed\n", files[i]); |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1365 | update_status = false; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1366 | } |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1367 | /* delete a capsule either in case of success or failure */ |
| 1368 | ret = efi_capsule_delete_file(files[i]); |
| 1369 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1370 | log_err("Deleting capsule %ls failed\n", |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 1371 | files[i]); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1372 | } |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1373 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1374 | efi_capsule_scan_done(); |
| 1375 | |
Sughosh Ganu | 1cadae2 | 2022-10-21 18:16:03 +0530 | [diff] [blame] | 1376 | if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) { |
| 1377 | if (capsule_update == true && update_status == true) { |
| 1378 | ret = fwu_post_update_process(fw_accept_os); |
| 1379 | } else if (capsule_update == true && update_status == false) { |
| 1380 | log_err("All capsules were not updated. Not updating FWU metadata\n"); |
| 1381 | } |
| 1382 | } |
| 1383 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1384 | for (i = 0; i < nfiles; i++) |
| 1385 | free(files[i]); |
| 1386 | free(files); |
| 1387 | |
Masami Hiramatsu | 4b2f8c1 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 1388 | /* |
| 1389 | * UEFI spec requires to reset system after complete processing capsule |
| 1390 | * update on the storage. |
| 1391 | */ |
Masami Hiramatsu | ff74486 | 2022-03-21 22:37:56 +0900 | [diff] [blame] | 1392 | log_info("Reboot after firmware update.\n"); |
Masami Hiramatsu | 4b2f8c1 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 1393 | /* Cold reset is required for loading the new firmware. */ |
Masami Hiramatsu | ff74486 | 2022-03-21 22:37:56 +0900 | [diff] [blame] | 1394 | sysreset_walk_halt(SYSRESET_COLD); |
Masami Hiramatsu | 4b2f8c1 | 2022-02-16 15:16:12 +0900 | [diff] [blame] | 1395 | hang(); |
| 1396 | /* not reach here */ |
| 1397 | |
| 1398 | return 0; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1399 | } |
| 1400 | #endif /* CONFIG_EFI_CAPSULE_ON_DISK */ |