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 <common.h> |
| 12 | #include <efi_loader.h> |
| 13 | #include <efi_variable.h> |
| 14 | #include <fs.h> |
| 15 | #include <malloc.h> |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 16 | #include <mapmem.h> |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 17 | #include <sort.h> |
| 18 | |
Sughosh Ganu | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 19 | #include <crypto/pkcs7.h> |
| 20 | #include <crypto/pkcs7_parser.h> |
| 21 | #include <linux/err.h> |
| 22 | |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 23 | const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID; |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 24 | static const efi_guid_t efi_guid_firmware_management_capsule_id = |
| 25 | EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID; |
| 26 | const efi_guid_t efi_guid_firmware_management_protocol = |
| 27 | EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID; |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 28 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 29 | #ifdef CONFIG_EFI_CAPSULE_ON_DISK |
| 30 | /* for file system access */ |
| 31 | static struct efi_file_handle *bootdev_root; |
| 32 | #endif |
| 33 | |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 34 | /** |
| 35 | * get_last_capsule - get the last capsule index |
| 36 | * |
| 37 | * Retrieve the index of the capsule invoked last time from "CapsuleLast" |
| 38 | * variable. |
| 39 | * |
| 40 | * Return: |
| 41 | * * > 0 - the last capsule index invoked |
| 42 | * * 0xffff - on error, or no capsule invoked yet |
| 43 | */ |
| 44 | static __maybe_unused unsigned int get_last_capsule(void) |
| 45 | { |
| 46 | u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */ |
Heinrich Schuchardt | 812f6e0 | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 47 | char value[5]; |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 48 | efi_uintn_t size; |
| 49 | unsigned long index = 0xffff; |
| 50 | efi_status_t ret; |
Heinrich Schuchardt | 812f6e0 | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 51 | int i; |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 52 | |
| 53 | size = sizeof(value16); |
| 54 | ret = efi_get_variable_int(L"CapsuleLast", &efi_guid_capsule_report, |
| 55 | NULL, &size, value16, NULL); |
Heinrich Schuchardt | 812f6e0 | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 56 | if (ret != EFI_SUCCESS || size != 22 || |
| 57 | u16_strncmp(value16, L"Capsule", 7)) |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 58 | goto err; |
Heinrich Schuchardt | 812f6e0 | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 59 | for (i = 0; i < 4; ++i) { |
| 60 | u16 c = value16[i + 7]; |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 61 | |
Heinrich Schuchardt | 812f6e0 | 2021-02-09 20:20:34 +0100 | [diff] [blame] | 62 | if (!c || c > 0x7f) |
| 63 | goto err; |
| 64 | value[i] = c; |
| 65 | } |
| 66 | value[4] = 0; |
| 67 | if (strict_strtoul(value, 16, &index)) |
| 68 | index = 0xffff; |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 69 | err: |
| 70 | return index; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * set_capsule_result - set a result variable |
| 75 | * @capsule: Capsule |
| 76 | * @return_status: Return status |
| 77 | * |
| 78 | * Create and set a result variable, "CapsuleXXXX", for the capsule, |
| 79 | * @capsule. |
| 80 | */ |
| 81 | static __maybe_unused |
| 82 | void set_capsule_result(int index, struct efi_capsule_header *capsule, |
| 83 | efi_status_t return_status) |
| 84 | { |
| 85 | u16 variable_name16[12]; |
| 86 | struct efi_capsule_result_variable_header result; |
| 87 | struct efi_time time; |
| 88 | efi_status_t ret; |
| 89 | |
Ilias Apalodimas | 2157529 | 2020-12-31 12:26:46 +0200 | [diff] [blame] | 90 | efi_create_indexed_name(variable_name16, sizeof(variable_name16), |
| 91 | "Capsule", index); |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 92 | result.variable_total_size = sizeof(result); |
| 93 | result.capsule_guid = capsule->capsule_guid; |
| 94 | ret = EFI_CALL((*efi_runtime_services.get_time)(&time, NULL)); |
| 95 | if (ret == EFI_SUCCESS) |
| 96 | memcpy(&result.capsule_processed, &time, sizeof(time)); |
| 97 | else |
| 98 | memset(&result.capsule_processed, 0, sizeof(time)); |
| 99 | result.capsule_status = return_status; |
Heinrich Schuchardt | 24adaa7 | 2021-07-10 11:10:26 +0200 | [diff] [blame] | 100 | ret = efi_set_variable_int(variable_name16, &efi_guid_capsule_report, |
| 101 | EFI_VARIABLE_NON_VOLATILE | |
| 102 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 103 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 104 | sizeof(result), &result, false); |
Heinrich Schuchardt | b1fae8c | 2021-07-10 11:14:13 +0200 | [diff] [blame] | 105 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 106 | log_err("Setting %ls failed\n", variable_name16); |
Heinrich Schuchardt | b1fae8c | 2021-07-10 11:14:13 +0200 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | |
| 110 | /* Variable CapsuleLast must not include terminating 0x0000 */ |
| 111 | ret = efi_set_variable_int(L"CapsuleLast", &efi_guid_capsule_report, |
| 112 | EFI_VARIABLE_READ_ONLY | |
| 113 | EFI_VARIABLE_NON_VOLATILE | |
| 114 | EFI_VARIABLE_BOOTSERVICE_ACCESS | |
| 115 | EFI_VARIABLE_RUNTIME_ACCESS, |
| 116 | 22, variable_name16, false); |
| 117 | if (ret != EFI_SUCCESS) |
| 118 | log_err("Setting %ls failed\n", L"CapsuleLast"); |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 119 | } |
| 120 | |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 121 | #ifdef CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT |
| 122 | /** |
| 123 | * efi_fmp_find - search for Firmware Management Protocol drivers |
| 124 | * @image_type: Image type guid |
| 125 | * @instance: Instance number |
| 126 | * @handles: Handles of FMP drivers |
| 127 | * @no_handles: Number of handles |
| 128 | * |
| 129 | * Search for Firmware Management Protocol drivers, matching the image |
| 130 | * type, @image_type and the machine instance, @instance, from the list, |
| 131 | * @handles. |
| 132 | * |
| 133 | * Return: |
| 134 | * * Protocol instance - on success |
| 135 | * * NULL - on failure |
| 136 | */ |
| 137 | static struct efi_firmware_management_protocol * |
| 138 | efi_fmp_find(efi_guid_t *image_type, u64 instance, efi_handle_t *handles, |
| 139 | efi_uintn_t no_handles) |
| 140 | { |
| 141 | efi_handle_t *handle; |
| 142 | struct efi_firmware_management_protocol *fmp; |
| 143 | struct efi_firmware_image_descriptor *image_info, *desc; |
| 144 | efi_uintn_t info_size, descriptor_size; |
| 145 | u32 descriptor_version; |
| 146 | u8 descriptor_count; |
| 147 | u32 package_version; |
| 148 | u16 *package_version_name; |
| 149 | bool found = false; |
| 150 | int i, j; |
| 151 | efi_status_t ret; |
| 152 | |
| 153 | for (i = 0, handle = handles; i < no_handles; i++, handle++) { |
| 154 | ret = EFI_CALL(efi_handle_protocol( |
| 155 | *handle, |
| 156 | &efi_guid_firmware_management_protocol, |
| 157 | (void **)&fmp)); |
| 158 | if (ret != EFI_SUCCESS) |
| 159 | continue; |
| 160 | |
| 161 | /* get device's image info */ |
| 162 | info_size = 0; |
| 163 | image_info = NULL; |
| 164 | descriptor_version = 0; |
| 165 | descriptor_count = 0; |
| 166 | descriptor_size = 0; |
| 167 | package_version = 0; |
| 168 | package_version_name = NULL; |
| 169 | ret = EFI_CALL(fmp->get_image_info(fmp, &info_size, |
| 170 | image_info, |
| 171 | &descriptor_version, |
| 172 | &descriptor_count, |
| 173 | &descriptor_size, |
| 174 | &package_version, |
| 175 | &package_version_name)); |
| 176 | if (ret != EFI_BUFFER_TOO_SMALL) |
| 177 | goto skip; |
| 178 | |
| 179 | image_info = malloc(info_size); |
| 180 | if (!image_info) |
| 181 | goto skip; |
| 182 | |
| 183 | ret = EFI_CALL(fmp->get_image_info(fmp, &info_size, |
| 184 | image_info, |
| 185 | &descriptor_version, |
| 186 | &descriptor_count, |
| 187 | &descriptor_size, |
| 188 | &package_version, |
| 189 | &package_version_name)); |
| 190 | if (ret != EFI_SUCCESS || |
| 191 | descriptor_version != EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION) |
| 192 | goto skip; |
| 193 | |
| 194 | /* matching */ |
| 195 | for (j = 0, desc = image_info; j < descriptor_count; |
| 196 | j++, desc = (void *)desc + descriptor_size) { |
| 197 | log_debug("+++ desc[%d] index: %d, name: %ls\n", |
| 198 | j, desc->image_index, desc->image_id_name); |
| 199 | if (!guidcmp(&desc->image_type_id, image_type) && |
| 200 | (!instance || |
| 201 | !desc->hardware_instance || |
| 202 | desc->hardware_instance == instance)) |
| 203 | found = true; |
| 204 | } |
| 205 | |
| 206 | skip: |
| 207 | efi_free_pool(package_version_name); |
| 208 | free(image_info); |
| 209 | EFI_CALL(efi_close_protocol( |
| 210 | (efi_handle_t)fmp, |
| 211 | &efi_guid_firmware_management_protocol, |
| 212 | NULL, NULL)); |
| 213 | if (found) |
| 214 | return fmp; |
| 215 | } |
| 216 | |
| 217 | return NULL; |
| 218 | } |
| 219 | |
Sughosh Ganu | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 220 | #if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE) |
| 221 | |
| 222 | const efi_guid_t efi_guid_capsule_root_cert_guid = |
| 223 | EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID; |
| 224 | |
Sughosh Ganu | 586bb98 | 2020-12-30 19:27:09 +0530 | [diff] [blame] | 225 | efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size, |
| 226 | void **image, efi_uintn_t *image_size) |
| 227 | { |
| 228 | u8 *buf; |
| 229 | int ret; |
| 230 | void *fdt_pkey, *pkey; |
| 231 | efi_uintn_t pkey_len; |
| 232 | uint64_t monotonic_count; |
| 233 | struct efi_signature_store *truststore; |
| 234 | struct pkcs7_message *capsule_sig; |
| 235 | struct efi_image_regions *regs; |
| 236 | struct efi_firmware_image_authentication *auth_hdr; |
| 237 | efi_status_t status; |
| 238 | |
| 239 | status = EFI_SECURITY_VIOLATION; |
| 240 | capsule_sig = NULL; |
| 241 | truststore = NULL; |
| 242 | regs = NULL; |
| 243 | |
| 244 | /* Sanity checks */ |
| 245 | if (capsule == NULL || capsule_size == 0) |
| 246 | goto out; |
| 247 | |
| 248 | auth_hdr = (struct efi_firmware_image_authentication *)capsule; |
| 249 | if (capsule_size < sizeof(*auth_hdr)) |
| 250 | goto out; |
| 251 | |
| 252 | if (auth_hdr->auth_info.hdr.dwLength <= |
| 253 | offsetof(struct win_certificate_uefi_guid, cert_data)) |
| 254 | goto out; |
| 255 | |
| 256 | if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7)) |
| 257 | goto out; |
| 258 | |
| 259 | *image = (uint8_t *)capsule + sizeof(auth_hdr->monotonic_count) + |
| 260 | auth_hdr->auth_info.hdr.dwLength; |
| 261 | *image_size = capsule_size - auth_hdr->auth_info.hdr.dwLength - |
| 262 | sizeof(auth_hdr->monotonic_count); |
| 263 | memcpy(&monotonic_count, &auth_hdr->monotonic_count, |
| 264 | sizeof(monotonic_count)); |
| 265 | |
| 266 | /* data to be digested */ |
| 267 | regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 2, 1); |
| 268 | if (!regs) |
| 269 | goto out; |
| 270 | |
| 271 | regs->max = 2; |
| 272 | efi_image_region_add(regs, (uint8_t *)*image, |
| 273 | (uint8_t *)*image + *image_size, 1); |
| 274 | |
| 275 | efi_image_region_add(regs, (uint8_t *)&monotonic_count, |
| 276 | (uint8_t *)&monotonic_count + sizeof(monotonic_count), |
| 277 | 1); |
| 278 | |
| 279 | capsule_sig = efi_parse_pkcs7_header(auth_hdr->auth_info.cert_data, |
| 280 | auth_hdr->auth_info.hdr.dwLength |
| 281 | - sizeof(auth_hdr->auth_info), |
| 282 | &buf); |
| 283 | if (IS_ERR(capsule_sig)) { |
| 284 | debug("Parsing variable's pkcs7 header failed\n"); |
| 285 | capsule_sig = NULL; |
| 286 | goto out; |
| 287 | } |
| 288 | |
| 289 | ret = efi_get_public_key_data(&fdt_pkey, &pkey_len); |
| 290 | if (ret < 0) |
| 291 | goto out; |
| 292 | |
| 293 | pkey = malloc(pkey_len); |
| 294 | if (!pkey) |
| 295 | goto out; |
| 296 | |
| 297 | memcpy(pkey, fdt_pkey, pkey_len); |
| 298 | truststore = efi_build_signature_store(pkey, pkey_len); |
| 299 | if (!truststore) |
| 300 | goto out; |
| 301 | |
| 302 | /* verify signature */ |
| 303 | if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) { |
| 304 | debug("Verified\n"); |
| 305 | } else { |
| 306 | debug("Verifying variable's signature failed\n"); |
| 307 | goto out; |
| 308 | } |
| 309 | |
| 310 | status = EFI_SUCCESS; |
| 311 | |
| 312 | out: |
| 313 | efi_sigstore_free(truststore); |
| 314 | pkcs7_free_message(capsule_sig); |
| 315 | free(regs); |
| 316 | |
| 317 | return status; |
| 318 | } |
| 319 | #else |
| 320 | efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size, |
| 321 | void **image, efi_uintn_t *image_size) |
| 322 | { |
| 323 | return EFI_UNSUPPORTED; |
| 324 | } |
| 325 | #endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */ |
| 326 | |
| 327 | |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 328 | /** |
| 329 | * efi_capsule_update_firmware - update firmware from capsule |
| 330 | * @capsule_data: Capsule |
| 331 | * |
| 332 | * Update firmware, using a capsule, @capsule_data. Loading any FMP |
| 333 | * drivers embedded in a capsule is not supported. |
| 334 | * |
| 335 | * Return: status code |
| 336 | */ |
| 337 | static efi_status_t efi_capsule_update_firmware( |
| 338 | struct efi_capsule_header *capsule_data) |
| 339 | { |
| 340 | struct efi_firmware_management_capsule_header *capsule; |
| 341 | struct efi_firmware_management_capsule_image_header *image; |
| 342 | size_t capsule_size; |
| 343 | void *image_binary, *vendor_code; |
| 344 | efi_handle_t *handles; |
| 345 | efi_uintn_t no_handles; |
| 346 | int item; |
| 347 | struct efi_firmware_management_protocol *fmp; |
| 348 | u16 *abort_reason; |
| 349 | efi_status_t ret = EFI_SUCCESS; |
| 350 | |
| 351 | /* sanity check */ |
| 352 | if (capsule_data->header_size < sizeof(*capsule) || |
| 353 | capsule_data->header_size >= capsule_data->capsule_image_size) |
| 354 | return EFI_INVALID_PARAMETER; |
| 355 | |
| 356 | capsule = (void *)capsule_data + capsule_data->header_size; |
| 357 | capsule_size = capsule_data->capsule_image_size |
| 358 | - capsule_data->header_size; |
| 359 | |
| 360 | if (capsule->version != 0x00000001) |
| 361 | return EFI_UNSUPPORTED; |
| 362 | |
| 363 | handles = NULL; |
| 364 | ret = EFI_CALL(efi_locate_handle_buffer( |
| 365 | BY_PROTOCOL, |
| 366 | &efi_guid_firmware_management_protocol, |
| 367 | NULL, &no_handles, (efi_handle_t **)&handles)); |
| 368 | if (ret != EFI_SUCCESS) |
| 369 | return EFI_UNSUPPORTED; |
| 370 | |
| 371 | /* Payload */ |
| 372 | for (item = capsule->embedded_driver_count; |
| 373 | item < capsule->embedded_driver_count |
| 374 | + capsule->payload_item_count; item++) { |
| 375 | /* sanity check */ |
| 376 | if ((capsule->item_offset_list[item] + sizeof(*image) |
| 377 | >= capsule_size)) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 378 | log_err("Capsule does not have enough data\n"); |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 379 | ret = EFI_INVALID_PARAMETER; |
| 380 | goto out; |
| 381 | } |
| 382 | |
| 383 | image = (void *)capsule + capsule->item_offset_list[item]; |
| 384 | |
| 385 | if (image->version != 0x00000003) { |
| 386 | ret = EFI_UNSUPPORTED; |
| 387 | goto out; |
| 388 | } |
| 389 | |
| 390 | /* find a device for update firmware */ |
| 391 | /* TODO: should we pass index as well, or nothing but type? */ |
| 392 | fmp = efi_fmp_find(&image->update_image_type_id, |
| 393 | image->update_hardware_instance, |
| 394 | handles, no_handles); |
| 395 | if (!fmp) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 396 | log_err("FMP driver not found for firmware type %pUl, hardware instance %lld\n", |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 397 | &image->update_image_type_id, |
| 398 | image->update_hardware_instance); |
| 399 | ret = EFI_UNSUPPORTED; |
| 400 | goto out; |
| 401 | } |
| 402 | |
| 403 | /* do update */ |
| 404 | image_binary = (void *)image + sizeof(*image); |
| 405 | vendor_code = image_binary + image->update_image_size; |
| 406 | |
| 407 | abort_reason = NULL; |
| 408 | ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index, |
| 409 | image_binary, |
| 410 | image->update_image_size, |
| 411 | vendor_code, NULL, |
| 412 | &abort_reason)); |
| 413 | if (ret != EFI_SUCCESS) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 414 | log_err("Firmware update failed: %ls\n", |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 415 | abort_reason); |
| 416 | efi_free_pool(abort_reason); |
| 417 | goto out; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | out: |
| 422 | efi_free_pool(handles); |
| 423 | |
| 424 | return ret; |
| 425 | } |
| 426 | #else |
| 427 | static efi_status_t efi_capsule_update_firmware( |
| 428 | struct efi_capsule_header *capsule_data) |
| 429 | { |
| 430 | return EFI_UNSUPPORTED; |
| 431 | } |
| 432 | #endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */ |
| 433 | |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 434 | /** |
| 435 | * efi_update_capsule() - process information from operating system |
| 436 | * @capsule_header_array: Array of virtual address pointers |
| 437 | * @capsule_count: Number of pointers in capsule_header_array |
| 438 | * @scatter_gather_list: Array of physical address pointers |
| 439 | * |
| 440 | * This function implements the UpdateCapsule() runtime service. |
| 441 | * |
| 442 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 443 | * details. |
| 444 | * |
| 445 | * Return: status code |
| 446 | */ |
| 447 | efi_status_t EFIAPI efi_update_capsule( |
| 448 | struct efi_capsule_header **capsule_header_array, |
| 449 | efi_uintn_t capsule_count, |
| 450 | u64 scatter_gather_list) |
| 451 | { |
| 452 | struct efi_capsule_header *capsule; |
| 453 | unsigned int i; |
| 454 | efi_status_t ret; |
| 455 | |
Simon Glass | 83698b2 | 2021-02-07 14:27:02 -0700 | [diff] [blame] | 456 | EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count, |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 457 | scatter_gather_list); |
| 458 | |
| 459 | if (!capsule_count) { |
| 460 | ret = EFI_INVALID_PARAMETER; |
| 461 | goto out; |
| 462 | } |
| 463 | |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 464 | ret = EFI_SUCCESS; |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 465 | for (i = 0, capsule = *capsule_header_array; i < capsule_count; |
| 466 | i++, capsule = *(++capsule_header_array)) { |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 467 | /* sanity check */ |
| 468 | if (capsule->header_size < sizeof(*capsule) || |
| 469 | capsule->capsule_image_size < sizeof(*capsule)) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 470 | log_err("Capsule does not have enough data\n"); |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 471 | continue; |
| 472 | } |
| 473 | |
| 474 | log_debug("Capsule[%d] (guid:%pUl)\n", |
| 475 | i, &capsule->capsule_guid); |
| 476 | if (!guidcmp(&capsule->capsule_guid, |
| 477 | &efi_guid_firmware_management_capsule_id)) { |
| 478 | ret = efi_capsule_update_firmware(capsule); |
| 479 | } else { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 480 | log_err("Unsupported capsule type: %pUl\n", |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 481 | &capsule->capsule_guid); |
| 482 | ret = EFI_UNSUPPORTED; |
| 483 | } |
| 484 | |
| 485 | if (ret != EFI_SUCCESS) |
| 486 | goto out; |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 487 | } |
Jose Marinho | ebb61ee | 2021-03-02 17:26:38 +0000 | [diff] [blame] | 488 | |
| 489 | if (IS_ENABLED(CONFIG_EFI_ESRT)) { |
| 490 | /* Rebuild the ESRT to reflect any updated FW images. */ |
| 491 | ret = efi_esrt_populate(); |
| 492 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 493 | log_warning("ESRT update failed\n"); |
Jose Marinho | ebb61ee | 2021-03-02 17:26:38 +0000 | [diff] [blame] | 494 | } |
Jose Marinho | af886ce | 2021-04-19 14:54:33 +0100 | [diff] [blame] | 495 | out: |
Jose Marinho | ebb61ee | 2021-03-02 17:26:38 +0000 | [diff] [blame] | 496 | |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 497 | return EFI_EXIT(ret); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * efi_query_capsule_caps() - check if capsule is supported |
| 502 | * @capsule_header_array: Array of virtual pointers |
| 503 | * @capsule_count: Number of pointers in capsule_header_array |
| 504 | * @maximum_capsule_size: Maximum capsule size |
| 505 | * @reset_type: Type of reset needed for capsule update |
| 506 | * |
| 507 | * This function implements the QueryCapsuleCapabilities() runtime service. |
| 508 | * |
| 509 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 510 | * details. |
| 511 | * |
| 512 | * Return: status code |
| 513 | */ |
| 514 | efi_status_t EFIAPI efi_query_capsule_caps( |
| 515 | struct efi_capsule_header **capsule_header_array, |
| 516 | efi_uintn_t capsule_count, |
| 517 | u64 *maximum_capsule_size, |
| 518 | u32 *reset_type) |
| 519 | { |
| 520 | struct efi_capsule_header *capsule __attribute__((unused)); |
| 521 | unsigned int i; |
| 522 | efi_status_t ret; |
| 523 | |
Simon Glass | 83698b2 | 2021-02-07 14:27:02 -0700 | [diff] [blame] | 524 | EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count, |
AKASHI Takahiro | 473d9b3 | 2020-11-17 09:27:55 +0900 | [diff] [blame] | 525 | maximum_capsule_size, reset_type); |
| 526 | |
| 527 | if (!maximum_capsule_size) { |
| 528 | ret = EFI_INVALID_PARAMETER; |
| 529 | goto out; |
| 530 | } |
| 531 | |
| 532 | *maximum_capsule_size = U64_MAX; |
| 533 | *reset_type = EFI_RESET_COLD; |
| 534 | |
| 535 | ret = EFI_SUCCESS; |
| 536 | for (i = 0, capsule = *capsule_header_array; i < capsule_count; |
| 537 | i++, capsule = *(++capsule_header_array)) { |
| 538 | /* TODO */ |
| 539 | } |
| 540 | out: |
| 541 | return EFI_EXIT(ret); |
| 542 | } |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 543 | |
| 544 | #ifdef CONFIG_EFI_CAPSULE_ON_DISK |
| 545 | /** |
| 546 | * get_dp_device - retrieve a device path from boot variable |
| 547 | * @boot_var: Boot variable name |
| 548 | * @device_dp Device path |
| 549 | * |
| 550 | * Retrieve a device patch from boot variable, @boot_var. |
| 551 | * |
| 552 | * Return: status code |
| 553 | */ |
| 554 | static efi_status_t get_dp_device(u16 *boot_var, |
| 555 | struct efi_device_path **device_dp) |
| 556 | { |
| 557 | void *buf = NULL; |
| 558 | efi_uintn_t size; |
| 559 | struct efi_load_option lo; |
| 560 | struct efi_device_path *file_dp; |
| 561 | efi_status_t ret; |
| 562 | |
| 563 | size = 0; |
| 564 | ret = efi_get_variable_int(boot_var, &efi_global_variable_guid, |
| 565 | NULL, &size, NULL, NULL); |
| 566 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 567 | buf = malloc(size); |
| 568 | if (!buf) |
| 569 | return EFI_OUT_OF_RESOURCES; |
| 570 | ret = efi_get_variable_int(boot_var, &efi_global_variable_guid, |
| 571 | NULL, &size, buf, NULL); |
| 572 | } |
| 573 | if (ret != EFI_SUCCESS) |
| 574 | return ret; |
| 575 | |
| 576 | efi_deserialize_load_option(&lo, buf, &size); |
| 577 | |
| 578 | if (lo.attributes & LOAD_OPTION_ACTIVE) { |
| 579 | efi_dp_split_file_path(lo.file_path, device_dp, &file_dp); |
| 580 | efi_free_pool(file_dp); |
| 581 | |
| 582 | ret = EFI_SUCCESS; |
| 583 | } else { |
| 584 | ret = EFI_NOT_FOUND; |
| 585 | } |
| 586 | |
| 587 | free(buf); |
| 588 | |
| 589 | return ret; |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * device_is_present_and_system_part - check if a device exists |
| 594 | * @dp Device path |
| 595 | * |
| 596 | * Check if a device pointed to by the device path, @dp, exists and is |
| 597 | * located in UEFI system partition. |
| 598 | * |
| 599 | * Return: true - yes, false - no |
| 600 | */ |
| 601 | static bool device_is_present_and_system_part(struct efi_device_path *dp) |
| 602 | { |
| 603 | efi_handle_t handle; |
| 604 | |
| 605 | handle = efi_dp_find_obj(dp, NULL); |
| 606 | if (!handle) |
| 607 | return false; |
| 608 | |
| 609 | return efi_disk_is_system_part(handle); |
| 610 | } |
| 611 | |
| 612 | /** |
| 613 | * find_boot_device - identify the boot device |
| 614 | * |
| 615 | * Identify the boot device from boot-related variables as UEFI |
| 616 | * specification describes and put its handle into bootdev_root. |
| 617 | * |
| 618 | * Return: status code |
| 619 | */ |
| 620 | static efi_status_t find_boot_device(void) |
| 621 | { |
| 622 | char boot_var[9]; |
| 623 | u16 boot_var16[9], *p, bootnext, *boot_order = NULL; |
| 624 | efi_uintn_t size; |
| 625 | int i, num; |
| 626 | struct efi_simple_file_system_protocol *volume; |
| 627 | struct efi_device_path *boot_dev = NULL; |
| 628 | efi_status_t ret; |
| 629 | |
| 630 | /* find active boot device in BootNext */ |
| 631 | bootnext = 0; |
| 632 | size = sizeof(bootnext); |
| 633 | ret = efi_get_variable_int(L"BootNext", |
| 634 | (efi_guid_t *)&efi_global_variable_guid, |
| 635 | NULL, &size, &bootnext, NULL); |
| 636 | if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) { |
| 637 | /* BootNext does exist here */ |
| 638 | if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) { |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 639 | log_err("BootNext must be 16-bit integer\n"); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 640 | goto skip; |
| 641 | } |
| 642 | sprintf((char *)boot_var, "Boot%04X", bootnext); |
| 643 | p = boot_var16; |
| 644 | utf8_utf16_strcpy(&p, boot_var); |
| 645 | |
| 646 | ret = get_dp_device(boot_var16, &boot_dev); |
| 647 | if (ret == EFI_SUCCESS) { |
| 648 | if (device_is_present_and_system_part(boot_dev)) { |
Masami Hiramatsu | 1016575 | 2021-07-12 18:05:17 +0900 | [diff] [blame] | 649 | goto found; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 650 | } else { |
| 651 | efi_free_pool(boot_dev); |
| 652 | boot_dev = NULL; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | skip: |
| 658 | /* find active boot device in BootOrder */ |
| 659 | size = 0; |
| 660 | ret = efi_get_variable_int(L"BootOrder", &efi_global_variable_guid, |
| 661 | NULL, &size, NULL, NULL); |
| 662 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 663 | boot_order = malloc(size); |
| 664 | if (!boot_order) { |
| 665 | ret = EFI_OUT_OF_RESOURCES; |
| 666 | goto out; |
| 667 | } |
| 668 | |
| 669 | ret = efi_get_variable_int(L"BootOrder", |
| 670 | &efi_global_variable_guid, |
| 671 | NULL, &size, boot_order, NULL); |
| 672 | } |
| 673 | if (ret != EFI_SUCCESS) |
| 674 | goto out; |
| 675 | |
| 676 | /* check in higher order */ |
| 677 | num = size / sizeof(u16); |
| 678 | for (i = 0; i < num; i++) { |
| 679 | sprintf((char *)boot_var, "Boot%04X", boot_order[i]); |
| 680 | p = boot_var16; |
| 681 | utf8_utf16_strcpy(&p, boot_var); |
| 682 | ret = get_dp_device(boot_var16, &boot_dev); |
| 683 | if (ret != EFI_SUCCESS) |
| 684 | continue; |
| 685 | |
| 686 | if (device_is_present_and_system_part(boot_dev)) |
| 687 | break; |
| 688 | |
| 689 | efi_free_pool(boot_dev); |
| 690 | boot_dev = NULL; |
| 691 | } |
Masami Hiramatsu | 1016575 | 2021-07-12 18:05:17 +0900 | [diff] [blame] | 692 | found: |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 693 | if (boot_dev) { |
Masami Hiramatsu | d9763bb | 2021-07-14 14:19:13 +0900 | [diff] [blame^] | 694 | log_debug("Boot device %pD\n", boot_dev); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 695 | |
| 696 | volume = efi_fs_from_path(boot_dev); |
| 697 | if (!volume) |
| 698 | ret = EFI_DEVICE_ERROR; |
| 699 | else |
| 700 | ret = EFI_CALL(volume->open_volume(volume, |
| 701 | &bootdev_root)); |
| 702 | efi_free_pool(boot_dev); |
| 703 | } else { |
| 704 | ret = EFI_NOT_FOUND; |
| 705 | } |
AKASHI Takahiro | fa390e6 | 2021-04-20 10:03:16 +0900 | [diff] [blame] | 706 | out: |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 707 | free(boot_order); |
| 708 | |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * efi_capsule_scan_dir - traverse a capsule directory in boot device |
| 714 | * @files: Array of file names |
| 715 | * @num: Number of elements in @files |
| 716 | * |
| 717 | * Traverse a capsule directory in boot device. |
| 718 | * Called by initialization code, and returns an array of capsule file |
| 719 | * names in @files. |
| 720 | * |
| 721 | * Return: status code |
| 722 | */ |
| 723 | static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num) |
| 724 | { |
| 725 | struct efi_file_handle *dirh; |
| 726 | struct efi_file_info *dirent; |
| 727 | efi_uintn_t dirent_size, tmp_size; |
| 728 | unsigned int count; |
| 729 | u16 **tmp_files; |
| 730 | efi_status_t ret; |
| 731 | |
| 732 | ret = find_boot_device(); |
| 733 | if (ret == EFI_NOT_FOUND) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 734 | log_debug("Boot device is not set\n"); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 735 | *num = 0; |
| 736 | return EFI_SUCCESS; |
| 737 | } else if (ret != EFI_SUCCESS) { |
| 738 | return EFI_DEVICE_ERROR; |
| 739 | } |
| 740 | |
| 741 | /* count capsule files */ |
| 742 | ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh, |
| 743 | EFI_CAPSULE_DIR, |
| 744 | EFI_FILE_MODE_READ, 0)); |
| 745 | if (ret != EFI_SUCCESS) { |
| 746 | *num = 0; |
| 747 | return EFI_SUCCESS; |
| 748 | } |
| 749 | |
| 750 | dirent_size = 256; |
| 751 | dirent = malloc(dirent_size); |
| 752 | if (!dirent) |
| 753 | return EFI_OUT_OF_RESOURCES; |
| 754 | |
| 755 | count = 0; |
| 756 | while (1) { |
| 757 | tmp_size = dirent_size; |
| 758 | ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent)); |
| 759 | if (ret == EFI_BUFFER_TOO_SMALL) { |
Heinrich Schuchardt | aa27e5d | 2021-04-11 06:53:04 +0200 | [diff] [blame] | 760 | struct efi_file_info *old_dirent = dirent; |
| 761 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 762 | dirent = realloc(dirent, tmp_size); |
| 763 | if (!dirent) { |
Heinrich Schuchardt | aa27e5d | 2021-04-11 06:53:04 +0200 | [diff] [blame] | 764 | dirent = old_dirent; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 765 | ret = EFI_OUT_OF_RESOURCES; |
| 766 | goto err; |
| 767 | } |
| 768 | dirent_size = tmp_size; |
| 769 | ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent)); |
| 770 | } |
| 771 | if (ret != EFI_SUCCESS) |
| 772 | goto err; |
| 773 | if (!tmp_size) |
| 774 | break; |
| 775 | |
Heinrich Schuchardt | 76b708a | 2021-02-09 17:45:33 +0100 | [diff] [blame] | 776 | if (!(dirent->attribute & EFI_FILE_DIRECTORY)) |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 777 | count++; |
| 778 | } |
| 779 | |
| 780 | ret = EFI_CALL((*dirh->setpos)(dirh, 0)); |
| 781 | if (ret != EFI_SUCCESS) |
| 782 | goto err; |
| 783 | |
| 784 | /* make a list */ |
AKASHI Takahiro | c8fc12f | 2021-01-22 10:43:27 +0900 | [diff] [blame] | 785 | tmp_files = malloc(count * sizeof(*tmp_files)); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 786 | if (!tmp_files) { |
| 787 | ret = EFI_OUT_OF_RESOURCES; |
| 788 | goto err; |
| 789 | } |
| 790 | |
| 791 | count = 0; |
| 792 | while (1) { |
| 793 | tmp_size = dirent_size; |
| 794 | ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent)); |
| 795 | if (ret != EFI_SUCCESS) |
| 796 | goto err; |
| 797 | if (!tmp_size) |
| 798 | break; |
| 799 | |
| 800 | if (!(dirent->attribute & EFI_FILE_DIRECTORY) && |
| 801 | u16_strcmp(dirent->file_name, L".") && |
| 802 | u16_strcmp(dirent->file_name, L"..")) |
| 803 | tmp_files[count++] = u16_strdup(dirent->file_name); |
| 804 | } |
| 805 | /* ignore an error */ |
| 806 | EFI_CALL((*dirh->close)(dirh)); |
| 807 | |
| 808 | /* in ascii order */ |
| 809 | /* FIXME: u16 version of strcasecmp */ |
| 810 | qsort(tmp_files, count, sizeof(*tmp_files), |
| 811 | (int (*)(const void *, const void *))strcasecmp); |
| 812 | *files = tmp_files; |
| 813 | *num = count; |
| 814 | ret = EFI_SUCCESS; |
| 815 | err: |
| 816 | free(dirent); |
| 817 | |
| 818 | return ret; |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * efi_capsule_read_file - read in a capsule file |
| 823 | * @filename: File name |
| 824 | * @capsule: Pointer to buffer for capsule |
| 825 | * |
| 826 | * Read a capsule file and put its content in @capsule. |
| 827 | * |
| 828 | * Return: status code |
| 829 | */ |
| 830 | static efi_status_t efi_capsule_read_file(const u16 *filename, |
| 831 | struct efi_capsule_header **capsule) |
| 832 | { |
| 833 | struct efi_file_handle *dirh, *fh; |
| 834 | struct efi_file_info *file_info = NULL; |
| 835 | struct efi_capsule_header *buf = NULL; |
| 836 | efi_uintn_t size; |
| 837 | efi_status_t ret; |
| 838 | |
| 839 | ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh, |
| 840 | EFI_CAPSULE_DIR, |
| 841 | EFI_FILE_MODE_READ, 0)); |
| 842 | if (ret != EFI_SUCCESS) |
| 843 | return ret; |
| 844 | ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename, |
| 845 | EFI_FILE_MODE_READ, 0)); |
| 846 | /* ignore an error */ |
| 847 | EFI_CALL((*dirh->close)(dirh)); |
| 848 | if (ret != EFI_SUCCESS) |
| 849 | return ret; |
| 850 | |
| 851 | /* file size */ |
| 852 | size = 0; |
| 853 | ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid, |
| 854 | &size, file_info)); |
| 855 | if (ret == EFI_BUFFER_TOO_SMALL) { |
| 856 | file_info = malloc(size); |
| 857 | if (!file_info) { |
| 858 | ret = EFI_OUT_OF_RESOURCES; |
| 859 | goto err; |
| 860 | } |
| 861 | ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid, |
| 862 | &size, file_info)); |
| 863 | } |
| 864 | if (ret != EFI_SUCCESS) |
| 865 | goto err; |
| 866 | size = file_info->file_size; |
| 867 | free(file_info); |
| 868 | buf = malloc(size); |
| 869 | if (!buf) { |
| 870 | ret = EFI_OUT_OF_RESOURCES; |
| 871 | goto err; |
| 872 | } |
| 873 | |
| 874 | /* fetch data */ |
| 875 | ret = EFI_CALL((*fh->read)(fh, &size, buf)); |
| 876 | if (ret == EFI_SUCCESS) { |
| 877 | if (size >= buf->capsule_image_size) { |
| 878 | *capsule = buf; |
| 879 | } else { |
| 880 | free(buf); |
| 881 | ret = EFI_INVALID_PARAMETER; |
| 882 | } |
| 883 | } else { |
| 884 | free(buf); |
| 885 | } |
| 886 | err: |
| 887 | EFI_CALL((*fh->close)(fh)); |
| 888 | |
| 889 | return ret; |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * efi_capsule_delete_file - delete a capsule file |
| 894 | * @filename: File name |
| 895 | * |
| 896 | * Delete a capsule file from capsule directory. |
| 897 | * |
| 898 | * Return: status code |
| 899 | */ |
| 900 | static efi_status_t efi_capsule_delete_file(const u16 *filename) |
| 901 | { |
| 902 | struct efi_file_handle *dirh, *fh; |
| 903 | efi_status_t ret; |
| 904 | |
| 905 | ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh, |
| 906 | EFI_CAPSULE_DIR, |
| 907 | EFI_FILE_MODE_READ, 0)); |
| 908 | if (ret != EFI_SUCCESS) |
| 909 | return ret; |
| 910 | ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename, |
| 911 | EFI_FILE_MODE_READ, 0)); |
| 912 | /* ignore an error */ |
| 913 | EFI_CALL((*dirh->close)(dirh)); |
| 914 | |
Heinrich Schuchardt | e5c2281 | 2021-06-02 19:28:22 +0200 | [diff] [blame] | 915 | if (ret == EFI_SUCCESS) |
| 916 | ret = EFI_CALL((*fh->delete)(fh)); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 917 | |
| 918 | return ret; |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * efi_capsule_scan_done - reset a scan help function |
| 923 | * |
| 924 | * Reset a scan help function |
| 925 | */ |
| 926 | static void efi_capsule_scan_done(void) |
| 927 | { |
| 928 | EFI_CALL((*bootdev_root->close)(bootdev_root)); |
| 929 | bootdev_root = NULL; |
| 930 | } |
| 931 | |
| 932 | /** |
Ilias Apalodimas | fac773e | 2021-06-22 17:38:53 +0300 | [diff] [blame] | 933 | * efi_load_capsule_drivers - initialize capsule drivers |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 934 | * |
Ilias Apalodimas | fac773e | 2021-06-22 17:38:53 +0300 | [diff] [blame] | 935 | * Generic FMP drivers backed by DFU |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 936 | * |
| 937 | * Return: status code |
| 938 | */ |
Ilias Apalodimas | fac773e | 2021-06-22 17:38:53 +0300 | [diff] [blame] | 939 | efi_status_t __weak efi_load_capsule_drivers(void) |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 940 | { |
AKASHI Takahiro | f4818e6 | 2020-11-30 18:12:12 +0900 | [diff] [blame] | 941 | __maybe_unused efi_handle_t handle; |
| 942 | efi_status_t ret = EFI_SUCCESS; |
| 943 | |
| 944 | if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) { |
| 945 | handle = NULL; |
| 946 | ret = EFI_CALL(efi_install_multiple_protocol_interfaces( |
| 947 | &handle, &efi_guid_firmware_management_protocol, |
| 948 | &efi_fmp_fit, NULL)); |
| 949 | } |
| 950 | |
AKASHI Takahiro | 7ff3f3c | 2020-11-17 09:28:00 +0900 | [diff] [blame] | 951 | if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) { |
| 952 | handle = NULL; |
| 953 | ret = EFI_CALL(efi_install_multiple_protocol_interfaces( |
Masami Hiramatsu | f538852 | 2021-06-22 17:38:51 +0300 | [diff] [blame] | 954 | &handle, |
AKASHI Takahiro | 7ff3f3c | 2020-11-17 09:28:00 +0900 | [diff] [blame] | 955 | &efi_guid_firmware_management_protocol, |
| 956 | &efi_fmp_raw, NULL)); |
| 957 | } |
| 958 | |
AKASHI Takahiro | f4818e6 | 2020-11-30 18:12:12 +0900 | [diff] [blame] | 959 | return ret; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /** |
Ilias Apalodimas | a38d0cb | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 963 | * check_run_capsules - Check whether capsule update should run |
| 964 | * |
| 965 | * The spec says OsIndications must be set in order to run the capsule update |
| 966 | * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to |
| 967 | * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected |
| 968 | */ |
| 969 | static bool check_run_capsules(void) |
| 970 | { |
| 971 | u64 os_indications; |
| 972 | efi_uintn_t size; |
| 973 | efi_status_t ret; |
| 974 | |
| 975 | if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS)) |
| 976 | return true; |
| 977 | |
| 978 | size = sizeof(os_indications); |
| 979 | ret = efi_get_variable_int(L"OsIndications", &efi_global_variable_guid, |
| 980 | NULL, &size, &os_indications, NULL); |
| 981 | if (ret == EFI_SUCCESS && |
| 982 | (os_indications |
| 983 | & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED)) |
| 984 | return true; |
| 985 | |
| 986 | return false; |
| 987 | } |
| 988 | |
| 989 | /** |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 990 | * efi_launch_capsule - launch capsules |
| 991 | * |
| 992 | * Launch all the capsules in system at boot time. |
| 993 | * Called by efi init code |
| 994 | * |
| 995 | * Return: status codde |
| 996 | */ |
| 997 | efi_status_t efi_launch_capsules(void) |
| 998 | { |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 999 | struct efi_capsule_header *capsule = NULL; |
| 1000 | u16 **files; |
| 1001 | unsigned int nfiles, index, i; |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1002 | efi_status_t ret; |
| 1003 | |
Ilias Apalodimas | a38d0cb | 2021-06-29 07:55:51 +0300 | [diff] [blame] | 1004 | if (!check_run_capsules()) |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1005 | return EFI_SUCCESS; |
| 1006 | |
| 1007 | index = get_last_capsule(); |
| 1008 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1009 | /* |
| 1010 | * Find capsules on disk. |
| 1011 | * All the capsules are collected at the beginning because |
| 1012 | * capsule files will be removed instantly. |
| 1013 | */ |
| 1014 | nfiles = 0; |
| 1015 | files = NULL; |
| 1016 | ret = efi_capsule_scan_dir(&files, &nfiles); |
| 1017 | if (ret != EFI_SUCCESS) |
| 1018 | return ret; |
| 1019 | if (!nfiles) |
| 1020 | return EFI_SUCCESS; |
| 1021 | |
| 1022 | /* Launch capsules */ |
| 1023 | for (i = 0, ++index; i < nfiles; i++, index++) { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1024 | log_debug("Applying %ls\n", files[i]); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1025 | if (index > 0xffff) |
| 1026 | index = 0; |
| 1027 | ret = efi_capsule_read_file(files[i], &capsule); |
| 1028 | if (ret == EFI_SUCCESS) { |
| 1029 | ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0)); |
| 1030 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1031 | log_err("Applying capsule %ls failed\n", |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 1032 | files[i]); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1033 | |
| 1034 | free(capsule); |
| 1035 | } else { |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1036 | log_err("Reading capsule %ls failed\n", files[i]); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1037 | } |
| 1038 | /* create CapsuleXXXX */ |
| 1039 | set_capsule_result(index, capsule, ret); |
| 1040 | |
| 1041 | /* delete a capsule either in case of success or failure */ |
| 1042 | ret = efi_capsule_delete_file(files[i]); |
| 1043 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | e3087a1 | 2021-07-10 11:03:27 +0200 | [diff] [blame] | 1044 | log_err("Deleting capsule %ls failed\n", |
AKASHI Takahiro | 0d96378 | 2020-11-30 18:12:11 +0900 | [diff] [blame] | 1045 | files[i]); |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1046 | } |
| 1047 | efi_capsule_scan_done(); |
| 1048 | |
| 1049 | for (i = 0; i < nfiles; i++) |
| 1050 | free(files[i]); |
| 1051 | free(files); |
| 1052 | |
AKASHI Takahiro | 45b81954 | 2020-11-17 09:27:56 +0900 | [diff] [blame] | 1053 | return ret; |
| 1054 | } |
| 1055 | #endif /* CONFIG_EFI_CAPSULE_ON_DISK */ |