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