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