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