blob: f8a4a7c6ef467db7d02b2d509061c0cc34a996a3 [file] [log] [blame]
AKASHI Takahiro473d9b32020-11-17 09:27:55 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * EFI Capsule
4 *
5 * Copyright (c) 2018 Linaro Limited
6 * Author: AKASHI Takahiro
7 */
8
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02009#define LOG_CATEGORY LOGC_EFI
10
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090011#include <efi_loader.h>
12#include <efi_variable.h>
AKASHI Takahirob71a0ae2021-10-07 15:23:32 +090013#include <env.h>
14#include <fdtdec.h>
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090015#include <fs.h>
Sughosh Ganu1cadae22022-10-21 18:16:03 +053016#include <fwu.h>
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +090017#include <hang.h>
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090018#include <malloc.h>
AKASHI Takahiro45b819542020-11-17 09:27:56 +090019#include <mapmem.h>
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090020#include <sort.h>
Masami Hiramatsuff744862022-03-21 22:37:56 +090021#include <sysreset.h>
AKASHI Takahirob71a0ae2021-10-07 15:23:32 +090022#include <asm/global_data.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010023#include <u-boot/uuid.h>
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090024
Sughosh Ganu586bb982020-12-30 19:27:09 +053025#include <crypto/pkcs7.h>
26#include <crypto/pkcs7_parser.h>
27#include <linux/err.h>
28
AKASHI Takahirob71a0ae2021-10-07 15:23:32 +090029DECLARE_GLOBAL_DATA_PTR;
30
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090031const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
AKASHI Takahiro0d963782020-11-30 18:12:11 +090032static const efi_guid_t efi_guid_firmware_management_capsule_id =
33 EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
34const efi_guid_t efi_guid_firmware_management_protocol =
35 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID;
Sughosh Ganu1cadae22022-10-21 18:16:03 +053036const efi_guid_t fwu_guid_os_request_fw_revert =
37 FWU_OS_REQUEST_FW_REVERT_GUID;
38const 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 Takahiro473d9b32020-11-17 09:27:55 +090042
AKASHI Takahiro45b819542020-11-17 09:27:56 +090043#ifdef CONFIG_EFI_CAPSULE_ON_DISK
44/* for file system access */
45static struct efi_file_handle *bootdev_root;
46#endif
47
Etienne Carriere6326e912023-02-16 18:21:41 +010048static __maybe_unused unsigned int get_capsule_index(const u16 *variable_name)
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090049{
50 u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010051 char value[5];
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090052 efi_uintn_t size;
53 unsigned long index = 0xffff;
54 efi_status_t ret;
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010055 int i;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090056
57 size = sizeof(value16);
Etienne Carriere6326e912023-02-16 18:21:41 +010058 ret = efi_get_variable_int(variable_name, &efi_guid_capsule_report,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090059 NULL, &size, value16, NULL);
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010060 if (ret != EFI_SUCCESS || size != 22 ||
Simon Glass90975372022-01-23 12:55:12 -070061 u16_strncmp(value16, u"Capsule", 7))
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090062 goto err;
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010063 for (i = 0; i < 4; ++i) {
64 u16 c = value16[i + 7];
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090065
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010066 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 Takahiro473d9b32020-11-17 09:27:55 +090073err:
74 return index;
75}
76
77/**
Etienne Carriere6326e912023-02-16 18:21:41 +010078 * 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 */
87static __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 */
101static __maybe_unused unsigned int get_max_capsule(void)
102{
103 return get_capsule_index(u"CapsuleMax");
104}
105
106/**
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900107 * 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 */
114static __maybe_unused
115void 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 Apalodimas21575292020-12-31 12:26:46 +0200123 efi_create_indexed_name(variable_name16, sizeof(variable_name16),
124 "Capsule", index);
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900125 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 Schuchardt24adaa72021-07-10 11:10:26 +0200133 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 Schuchardtb1fae8c2021-07-10 11:14:13 +0200138 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200139 log_err("Setting %ls failed\n", variable_name16);
Heinrich Schuchardtb1fae8c2021-07-10 11:14:13 +0200140 return;
141 }
142
143 /* Variable CapsuleLast must not include terminating 0x0000 */
Simon Glass90975372022-01-23 12:55:12 -0700144 ret = efi_set_variable_int(u"CapsuleLast", &efi_guid_capsule_report,
Heinrich Schuchardtb1fae8c2021-07-10 11:14:13 +0200145 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 Glass90975372022-01-23 12:55:12 -0700151 log_err("Setting %ls failed\n", u"CapsuleLast");
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900152}
153
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900154#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT
155/**
156 * efi_fmp_find - search for Firmware Management Protocol drivers
157 * @image_type: Image type guid
Sughosh Ganuf55c6b62022-04-15 11:29:36 +0530158 * @image_index: Image Index
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900159 * @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 */
171static struct efi_firmware_management_protocol *
Sughosh Ganuf55c6b62022-04-15 11:29:36 +0530172efi_fmp_find(efi_guid_t *image_type, u8 image_index, u64 instance,
173 efi_handle_t *handles, efi_uintn_t no_handles)
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900174{
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 Schuchardtd8dc3cb2022-10-07 15:29:52 +0200188 struct efi_handler *fmp_handler;
189
190 ret = efi_search_protocol(
191 *handle, &efi_guid_firmware_management_protocol,
192 &fmp_handler);
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900193 if (ret != EFI_SUCCESS)
194 continue;
Heinrich Schuchardtd8dc3cb2022-10-07 15:29:52 +0200195 fmp = fmp_handler->protocol_interface;
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900196
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 Ganuf55c6b62022-04-15 11:29:36 +0530236 (desc->image_index == image_index) &&
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900237 (!instance ||
238 !desc->hardware_instance ||
239 desc->hardware_instance == instance))
240 found = true;
241 }
242
243skip:
244 efi_free_pool(package_version_name);
245 free(image_info);
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900246 if (found)
247 return fmp;
248 }
249
250 return NULL;
251}
252
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900253/**
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 */
263static 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;
282out:
283 return ret;
284}
285
Sughosh Ganu586bb982020-12-30 19:27:09 +0530286#if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
AKASHI Takahirof155bf52021-11-02 09:55:01 +0900287int efi_get_public_key_data(void **pkey, efi_uintn_t *pkey_len)
AKASHI Takahirob71a0ae2021-10-07 15:23:32 +0900288{
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 Ganu586bb982020-12-30 19:27:09 +0530318
Sughosh Ganu586bb982020-12-30 19:27:09 +0530319efi_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 Glass1f78c122021-08-02 08:44:31 -0600324 void *fdt_pkey, *pkey;
Sughosh Ganu586bb982020-12-30 19:27:09 +0530325 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 Takahiro920671c2021-07-20 14:52:05 +0900342 *image = (uint8_t *)capsule;
343 *image_size = capsule_size;
344 if (efi_remove_auth_hdr(image, image_size) != EFI_SUCCESS)
Sughosh Ganu586bb982020-12-30 19:27:09 +0530345 goto out;
346
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900347 auth_hdr = (struct efi_firmware_image_authentication *)capsule;
Sughosh Ganu586bb982020-12-30 19:27:09 +0530348 if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
349 goto out;
350
Sughosh Ganu586bb982020-12-30 19:27:09 +0530351 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 Carpenter15325232023-07-27 10:16:20 +0300371 if (!capsule_sig) {
Sughosh Ganu586bb982020-12-30 19:27:09 +0530372 debug("Parsing variable's pkcs7 header failed\n");
Sughosh Ganu586bb982020-12-30 19:27:09 +0530373 goto out;
374 }
375
Simon Glass1f78c122021-08-02 08:44:31 -0600376 ret = efi_get_public_key_data(&fdt_pkey, &pkey_len);
Sughosh Ganu586bb982020-12-30 19:27:09 +0530377 if (ret < 0)
378 goto out;
379
380 pkey = malloc(pkey_len);
381 if (!pkey)
382 goto out;
383
Simon Glass1f78c122021-08-02 08:44:31 -0600384 memcpy(pkey, fdt_pkey, pkey_len);
Sughosh Ganu586bb982020-12-30 19:27:09 +0530385 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
399out:
400 efi_sigstore_free(truststore);
401 pkcs7_free_message(capsule_sig);
402 free(regs);
403
404 return status;
405}
Sughosh Ganu586bb982020-12-30 19:27:09 +0530406#endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */
407
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530408static __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
416static __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
441static __maybe_unused efi_status_t fwu_empty_capsule_process(
442 struct efi_capsule_header *capsule)
443{
444 int status;
445 u32 active_idx;
446 efi_guid_t *image_guid;
447 efi_status_t ret = EFI_INVALID_PARAMETER;
448
449 if (!guidcmp(&capsule->capsule_guid,
450 &fwu_guid_os_request_fw_revert)) {
451 /*
452 * One of the previously updated image has
453 * failed the OS acceptance test. OS has
454 * requested to revert back to the earlier
455 * boot index
456 */
457 status = fwu_revert_boot_index();
458 ret = fwu_to_efi_error(status);
459 if (ret == EFI_SUCCESS)
460 log_debug("Reverted the FWU active_index. Recommend rebooting the system\n");
461 else
462 log_err("Failed to revert the FWU boot index\n");
463 } else if (!guidcmp(&capsule->capsule_guid,
464 &fwu_guid_os_request_fw_accept)) {
465 /*
466 * Image accepted by the OS. Set the acceptance
467 * status for the image.
468 */
469 image_guid = (void *)(char *)capsule +
470 capsule->header_size;
471
472 status = fwu_get_active_index(&active_idx);
473 ret = fwu_to_efi_error(status);
474 if (ret != EFI_SUCCESS) {
475 log_err("Unable to get the active_index from the FWU metadata\n");
476 return ret;
477 }
478
479 status = fwu_accept_image(image_guid, active_idx);
480 ret = fwu_to_efi_error(status);
481 if (ret != EFI_SUCCESS)
482 log_err("Unable to set the Accept bit for the image %pUs\n",
483 image_guid);
Sughosh Ganu2aa3fe52024-03-22 16:27:22 +0530484
485 status = fwu_state_machine_updates(0, active_idx);
486 if (status < 0)
487 ret = EFI_DEVICE_ERROR;
488
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530489 }
490
491 return ret;
492}
493
494static __maybe_unused void fwu_post_update_checks(
495 struct efi_capsule_header *capsule,
496 bool *fw_accept_os, bool *capsule_update)
497{
498 if (fwu_empty_capsule(capsule))
499 *capsule_update = false;
500 else
501 if (!*fw_accept_os)
502 *fw_accept_os =
503 capsule->flags & FW_ACCEPT_OS ? true : false;
504}
505
506static __maybe_unused efi_status_t fwu_post_update_process(bool fw_accept_os)
507{
508 int status;
509 uint update_index;
510 efi_status_t ret;
511
512 status = fwu_plat_get_update_index(&update_index);
513 if (status < 0) {
514 log_err("Failed to get the FWU update_index value\n");
515 return EFI_DEVICE_ERROR;
516 }
517
518 /*
519 * All the capsules have been updated successfully,
520 * update the FWU metadata.
521 */
522 log_debug("Update Complete. Now updating active_index to %u\n",
523 update_index);
524 status = fwu_set_active_index(update_index);
525 ret = fwu_to_efi_error(status);
526 if (ret != EFI_SUCCESS) {
527 log_err("Failed to update FWU metadata index values\n");
528 } else {
529 log_debug("Successfully updated the active_index\n");
Sughosh Ganu2aa3fe52024-03-22 16:27:22 +0530530 status = fwu_state_machine_updates(fw_accept_os ? 1 : 0,
531 update_index);
532 if (status < 0)
533 ret = EFI_DEVICE_ERROR;
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530534 }
535
536 return ret;
537}
Sughosh Ganu586bb982020-12-30 19:27:09 +0530538
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900539/**
540 * efi_capsule_update_firmware - update firmware from capsule
541 * @capsule_data: Capsule
542 *
543 * Update firmware, using a capsule, @capsule_data. Loading any FMP
544 * drivers embedded in a capsule is not supported.
545 *
546 * Return: status code
547 */
548static efi_status_t efi_capsule_update_firmware(
549 struct efi_capsule_header *capsule_data)
550{
551 struct efi_firmware_management_capsule_header *capsule;
552 struct efi_firmware_management_capsule_image_header *image;
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900553 size_t capsule_size, image_binary_size;
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900554 void *image_binary, *vendor_code;
555 efi_handle_t *handles;
556 efi_uintn_t no_handles;
557 int item;
558 struct efi_firmware_management_protocol *fmp;
559 u16 *abort_reason;
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530560 efi_guid_t *image_type_id;
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900561 efi_status_t ret = EFI_SUCCESS;
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530562 int status;
563 uint update_index;
564 bool fw_accept_os;
565
566 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
Sughosh Ganu20585cd2024-09-09 16:50:21 +0530567 if (fwu_empty_capsule(capsule_data)) {
568 if (fwu_empty_capsule_checks_pass()) {
569 return fwu_empty_capsule_process(capsule_data);
570 } else {
571 log_err("FWU empty capsule checks failed. Cannot start update\n");
572 return EFI_INVALID_PARAMETER;
573 }
574 }
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530575
576 if (!fwu_update_checks_pass()) {
577 log_err("FWU checks failed. Cannot start update\n");
578 return EFI_INVALID_PARAMETER;
579 }
580
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530581 /* Obtain the update_index from the platform */
582 status = fwu_plat_get_update_index(&update_index);
583 if (status < 0) {
584 log_err("Failed to get the FWU update_index value\n");
585 return EFI_DEVICE_ERROR;
586 }
587
588 fw_accept_os = capsule_data->flags & FW_ACCEPT_OS ? 0x1 : 0x0;
589 }
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900590
AKASHI Takahiroa8000b92023-07-27 09:38:00 +0900591 if (guidcmp(&capsule_data->capsule_guid,
592 &efi_guid_firmware_management_capsule_id)) {
593 log_err("Unsupported capsule type: %pUs\n",
594 &capsule_data->capsule_guid);
595 return EFI_UNSUPPORTED;
596 }
597
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900598 /* sanity check */
599 if (capsule_data->header_size < sizeof(*capsule) ||
600 capsule_data->header_size >= capsule_data->capsule_image_size)
601 return EFI_INVALID_PARAMETER;
602
603 capsule = (void *)capsule_data + capsule_data->header_size;
604 capsule_size = capsule_data->capsule_image_size
605 - capsule_data->header_size;
606
607 if (capsule->version != 0x00000001)
608 return EFI_UNSUPPORTED;
609
610 handles = NULL;
611 ret = EFI_CALL(efi_locate_handle_buffer(
612 BY_PROTOCOL,
613 &efi_guid_firmware_management_protocol,
614 NULL, &no_handles, (efi_handle_t **)&handles));
615 if (ret != EFI_SUCCESS)
616 return EFI_UNSUPPORTED;
617
618 /* Payload */
619 for (item = capsule->embedded_driver_count;
620 item < capsule->embedded_driver_count
621 + capsule->payload_item_count; item++) {
622 /* sanity check */
623 if ((capsule->item_offset_list[item] + sizeof(*image)
624 >= capsule_size)) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200625 log_err("Capsule does not have enough data\n");
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900626 ret = EFI_INVALID_PARAMETER;
627 goto out;
628 }
629
630 image = (void *)capsule + capsule->item_offset_list[item];
631
632 if (image->version != 0x00000003) {
633 ret = EFI_UNSUPPORTED;
634 goto out;
635 }
636
637 /* find a device for update firmware */
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900638 fmp = efi_fmp_find(&image->update_image_type_id,
Sughosh Ganuf55c6b62022-04-15 11:29:36 +0530639 image->update_image_index,
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900640 image->update_hardware_instance,
641 handles, no_handles);
642 if (!fmp) {
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100643 log_err("FMP driver not found for firmware type %pUs, hardware instance %lld\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900644 &image->update_image_type_id,
645 image->update_hardware_instance);
646 ret = EFI_UNSUPPORTED;
647 goto out;
648 }
649
650 /* do update */
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900651 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
652 !(image->image_capsule_support &
653 CAPSULE_SUPPORT_AUTHENTICATION)) {
654 /* no signature */
655 ret = EFI_SECURITY_VIOLATION;
656 goto out;
657 }
658
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900659 image_binary = (void *)image + sizeof(*image);
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900660 image_binary_size = image->update_image_size;
661 vendor_code = image_binary + image_binary_size;
662 if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
663 (image->image_capsule_support &
664 CAPSULE_SUPPORT_AUTHENTICATION)) {
665 ret = efi_remove_auth_hdr(&image_binary,
666 &image_binary_size);
667 if (ret != EFI_SUCCESS)
668 goto out;
669 }
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900670
671 abort_reason = NULL;
672 ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
673 image_binary,
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900674 image_binary_size,
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900675 vendor_code, NULL,
676 &abort_reason));
677 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200678 log_err("Firmware update failed: %ls\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900679 abort_reason);
680 efi_free_pool(abort_reason);
681 goto out;
682 }
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530683
684 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
685 image_type_id = &image->update_image_type_id;
686 if (!fw_accept_os) {
687 /*
688 * The OS will not be accepting the firmware
689 * images. Set the accept bit of all the
690 * images contained in this capsule.
691 */
692 status = fwu_accept_image(image_type_id,
693 update_index);
694 } else {
695 status = fwu_clear_accept_image(image_type_id,
696 update_index);
697 }
698 ret = fwu_to_efi_error(status);
699 if (ret != EFI_SUCCESS) {
700 log_err("Unable to %s the accept bit for the image %pUs\n",
701 fw_accept_os ? "clear" : "set",
702 image_type_id);
703 goto out;
704 }
705
706 log_debug("%s the accepted bit for Image %pUs\n",
707 fw_accept_os ? "Cleared" : "Set",
708 image_type_id);
709 }
710
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900711 }
712
713out:
714 efi_free_pool(handles);
715
716 return ret;
717}
718#else
719static efi_status_t efi_capsule_update_firmware(
720 struct efi_capsule_header *capsule_data)
721{
722 return EFI_UNSUPPORTED;
723}
724#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
725
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900726/**
727 * efi_update_capsule() - process information from operating system
728 * @capsule_header_array: Array of virtual address pointers
729 * @capsule_count: Number of pointers in capsule_header_array
730 * @scatter_gather_list: Array of physical address pointers
731 *
732 * This function implements the UpdateCapsule() runtime service.
733 *
734 * See the Unified Extensible Firmware Interface (UEFI) specification for
735 * details.
736 *
737 * Return: status code
738 */
739efi_status_t EFIAPI efi_update_capsule(
740 struct efi_capsule_header **capsule_header_array,
741 efi_uintn_t capsule_count,
742 u64 scatter_gather_list)
743{
744 struct efi_capsule_header *capsule;
745 unsigned int i;
746 efi_status_t ret;
747
Simon Glass83698b22021-02-07 14:27:02 -0700748 EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900749 scatter_gather_list);
750
751 if (!capsule_count) {
752 ret = EFI_INVALID_PARAMETER;
753 goto out;
754 }
755
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900756 ret = EFI_SUCCESS;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900757 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
758 i++, capsule = *(++capsule_header_array)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900759 /* sanity check */
760 if (capsule->header_size < sizeof(*capsule) ||
761 capsule->capsule_image_size < sizeof(*capsule)) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200762 log_err("Capsule does not have enough data\n");
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900763 continue;
764 }
765
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100766 log_debug("Capsule[%d] (guid:%pUs)\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900767 i, &capsule->capsule_guid);
AKASHI Takahiroa8000b92023-07-27 09:38:00 +0900768 ret = efi_capsule_update_firmware(capsule);
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900769 if (ret != EFI_SUCCESS)
770 goto out;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900771 }
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000772
773 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
774 /* Rebuild the ESRT to reflect any updated FW images. */
775 ret = efi_esrt_populate();
776 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200777 log_warning("ESRT update failed\n");
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000778 }
Jose Marinhoaf886ce2021-04-19 14:54:33 +0100779out:
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000780
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900781 return EFI_EXIT(ret);
782}
783
784/**
785 * efi_query_capsule_caps() - check if capsule is supported
786 * @capsule_header_array: Array of virtual pointers
787 * @capsule_count: Number of pointers in capsule_header_array
788 * @maximum_capsule_size: Maximum capsule size
789 * @reset_type: Type of reset needed for capsule update
790 *
791 * This function implements the QueryCapsuleCapabilities() runtime service.
792 *
793 * See the Unified Extensible Firmware Interface (UEFI) specification for
794 * details.
795 *
796 * Return: status code
797 */
798efi_status_t EFIAPI efi_query_capsule_caps(
799 struct efi_capsule_header **capsule_header_array,
800 efi_uintn_t capsule_count,
801 u64 *maximum_capsule_size,
802 u32 *reset_type)
803{
804 struct efi_capsule_header *capsule __attribute__((unused));
805 unsigned int i;
806 efi_status_t ret;
807
Simon Glass83698b22021-02-07 14:27:02 -0700808 EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900809 maximum_capsule_size, reset_type);
810
811 if (!maximum_capsule_size) {
812 ret = EFI_INVALID_PARAMETER;
813 goto out;
814 }
815
816 *maximum_capsule_size = U64_MAX;
817 *reset_type = EFI_RESET_COLD;
818
819 ret = EFI_SUCCESS;
820 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
821 i++, capsule = *(++capsule_header_array)) {
822 /* TODO */
823 }
824out:
825 return EFI_EXIT(ret);
826}
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900827
Masami Hiramatsud306caa2022-03-21 22:37:45 +0900828/**
829 * efi_load_capsule_drivers - initialize capsule drivers
830 *
831 * Generic FMP drivers backed by DFU
832 *
833 * Return: status code
834 */
835efi_status_t __weak efi_load_capsule_drivers(void)
836{
837 __maybe_unused efi_handle_t handle;
838 efi_status_t ret = EFI_SUCCESS;
839
840 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
841 handle = NULL;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +0300842 ret = efi_install_multiple_protocol_interfaces(&handle,
843 &efi_guid_firmware_management_protocol,
844 &efi_fmp_fit,
845 NULL);
Masami Hiramatsud306caa2022-03-21 22:37:45 +0900846 }
847
848 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
849 handle = NULL;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +0300850 ret = efi_install_multiple_protocol_interfaces(&handle,
851 &efi_guid_firmware_management_protocol,
852 &efi_fmp_raw,
853 NULL);
Masami Hiramatsud306caa2022-03-21 22:37:45 +0900854 }
855
856 return ret;
857}
858
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900859#ifdef CONFIG_EFI_CAPSULE_ON_DISK
860/**
861 * get_dp_device - retrieve a device path from boot variable
862 * @boot_var: Boot variable name
863 * @device_dp Device path
864 *
865 * Retrieve a device patch from boot variable, @boot_var.
866 *
867 * Return: status code
868 */
869static efi_status_t get_dp_device(u16 *boot_var,
870 struct efi_device_path **device_dp)
871{
872 void *buf = NULL;
873 efi_uintn_t size;
874 struct efi_load_option lo;
875 struct efi_device_path *file_dp;
876 efi_status_t ret;
877
878 size = 0;
879 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
880 NULL, &size, NULL, NULL);
881 if (ret == EFI_BUFFER_TOO_SMALL) {
882 buf = malloc(size);
883 if (!buf)
884 return EFI_OUT_OF_RESOURCES;
885 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
886 NULL, &size, buf, NULL);
887 }
888 if (ret != EFI_SUCCESS)
889 return ret;
890
891 efi_deserialize_load_option(&lo, buf, &size);
892
893 if (lo.attributes & LOAD_OPTION_ACTIVE) {
894 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
895 efi_free_pool(file_dp);
896
897 ret = EFI_SUCCESS;
898 } else {
899 ret = EFI_NOT_FOUND;
900 }
901
902 free(buf);
903
904 return ret;
905}
906
907/**
908 * device_is_present_and_system_part - check if a device exists
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900909 *
910 * Check if a device pointed to by the device path, @dp, exists and is
911 * located in UEFI system partition.
912 *
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100913 * @dp device path
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900914 * Return: true - yes, false - no
915 */
916static bool device_is_present_and_system_part(struct efi_device_path *dp)
917{
918 efi_handle_t handle;
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100919 struct efi_device_path *rem;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900920
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100921 /* Check device exists */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100922 handle = efi_dp_find_obj(dp, NULL, NULL);
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900923 if (!handle)
924 return false;
925
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100926 /* Check device is on system partition */
927 handle = efi_dp_find_obj(dp, &efi_system_partition_guid, &rem);
928 if (!handle)
929 return false;
930
931 return true;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900932}
933
934/**
935 * find_boot_device - identify the boot device
936 *
937 * Identify the boot device from boot-related variables as UEFI
938 * specification describes and put its handle into bootdev_root.
939 *
940 * Return: status code
941 */
942static efi_status_t find_boot_device(void)
943{
944 char boot_var[9];
945 u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
946 efi_uintn_t size;
947 int i, num;
948 struct efi_simple_file_system_protocol *volume;
949 struct efi_device_path *boot_dev = NULL;
950 efi_status_t ret;
951
952 /* find active boot device in BootNext */
953 bootnext = 0;
954 size = sizeof(bootnext);
Simon Glass90975372022-01-23 12:55:12 -0700955 ret = efi_get_variable_int(u"BootNext",
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900956 (efi_guid_t *)&efi_global_variable_guid,
957 NULL, &size, &bootnext, NULL);
958 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
959 /* BootNext does exist here */
960 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900961 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900962 goto skip;
963 }
964 sprintf((char *)boot_var, "Boot%04X", bootnext);
965 p = boot_var16;
966 utf8_utf16_strcpy(&p, boot_var);
967
968 ret = get_dp_device(boot_var16, &boot_dev);
969 if (ret == EFI_SUCCESS) {
970 if (device_is_present_and_system_part(boot_dev)) {
Masami Hiramatsu10165752021-07-12 18:05:17 +0900971 goto found;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900972 } else {
973 efi_free_pool(boot_dev);
974 boot_dev = NULL;
975 }
976 }
977 }
978
979skip:
980 /* find active boot device in BootOrder */
981 size = 0;
Simon Glass90975372022-01-23 12:55:12 -0700982 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900983 NULL, &size, NULL, NULL);
984 if (ret == EFI_BUFFER_TOO_SMALL) {
985 boot_order = malloc(size);
986 if (!boot_order) {
987 ret = EFI_OUT_OF_RESOURCES;
988 goto out;
989 }
990
Simon Glass90975372022-01-23 12:55:12 -0700991 ret = efi_get_variable_int(u"BootOrder",
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900992 &efi_global_variable_guid,
993 NULL, &size, boot_order, NULL);
994 }
995 if (ret != EFI_SUCCESS)
996 goto out;
997
998 /* check in higher order */
999 num = size / sizeof(u16);
1000 for (i = 0; i < num; i++) {
1001 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
1002 p = boot_var16;
1003 utf8_utf16_strcpy(&p, boot_var);
1004 ret = get_dp_device(boot_var16, &boot_dev);
1005 if (ret != EFI_SUCCESS)
1006 continue;
1007
1008 if (device_is_present_and_system_part(boot_dev))
1009 break;
1010
1011 efi_free_pool(boot_dev);
1012 boot_dev = NULL;
1013 }
Masami Hiramatsu10165752021-07-12 18:05:17 +09001014found:
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001015 if (boot_dev) {
Masami Hiramatsud9763bb2021-07-14 14:19:13 +09001016 log_debug("Boot device %pD\n", boot_dev);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001017
1018 volume = efi_fs_from_path(boot_dev);
1019 if (!volume)
1020 ret = EFI_DEVICE_ERROR;
1021 else
1022 ret = EFI_CALL(volume->open_volume(volume,
1023 &bootdev_root));
1024 efi_free_pool(boot_dev);
1025 } else {
1026 ret = EFI_NOT_FOUND;
1027 }
AKASHI Takahirofa390e62021-04-20 10:03:16 +09001028out:
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001029 free(boot_order);
1030
1031 return ret;
1032}
1033
1034/**
1035 * efi_capsule_scan_dir - traverse a capsule directory in boot device
1036 * @files: Array of file names
1037 * @num: Number of elements in @files
1038 *
1039 * Traverse a capsule directory in boot device.
1040 * Called by initialization code, and returns an array of capsule file
1041 * names in @files.
1042 *
1043 * Return: status code
1044 */
1045static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
1046{
1047 struct efi_file_handle *dirh;
1048 struct efi_file_info *dirent;
1049 efi_uintn_t dirent_size, tmp_size;
1050 unsigned int count;
1051 u16 **tmp_files;
1052 efi_status_t ret;
1053
1054 ret = find_boot_device();
1055 if (ret == EFI_NOT_FOUND) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001056 log_debug("Boot device is not set\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001057 *num = 0;
1058 return EFI_SUCCESS;
1059 } else if (ret != EFI_SUCCESS) {
1060 return EFI_DEVICE_ERROR;
1061 }
1062
1063 /* count capsule files */
1064 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1065 EFI_CAPSULE_DIR,
1066 EFI_FILE_MODE_READ, 0));
1067 if (ret != EFI_SUCCESS) {
1068 *num = 0;
1069 return EFI_SUCCESS;
1070 }
1071
1072 dirent_size = 256;
1073 dirent = malloc(dirent_size);
1074 if (!dirent)
1075 return EFI_OUT_OF_RESOURCES;
1076
1077 count = 0;
1078 while (1) {
1079 tmp_size = dirent_size;
1080 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1081 if (ret == EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardtaa27e5d2021-04-11 06:53:04 +02001082 struct efi_file_info *old_dirent = dirent;
1083
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001084 dirent = realloc(dirent, tmp_size);
1085 if (!dirent) {
Heinrich Schuchardtaa27e5d2021-04-11 06:53:04 +02001086 dirent = old_dirent;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001087 ret = EFI_OUT_OF_RESOURCES;
1088 goto err;
1089 }
1090 dirent_size = tmp_size;
1091 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1092 }
1093 if (ret != EFI_SUCCESS)
1094 goto err;
1095 if (!tmp_size)
1096 break;
1097
Heinrich Schuchardt76b708a2021-02-09 17:45:33 +01001098 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001099 count++;
1100 }
1101
1102 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
1103 if (ret != EFI_SUCCESS)
1104 goto err;
1105
1106 /* make a list */
AKASHI Takahiroc8fc12f2021-01-22 10:43:27 +09001107 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001108 if (!tmp_files) {
1109 ret = EFI_OUT_OF_RESOURCES;
1110 goto err;
1111 }
1112
1113 count = 0;
1114 while (1) {
1115 tmp_size = dirent_size;
1116 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1117 if (ret != EFI_SUCCESS)
1118 goto err;
1119 if (!tmp_size)
1120 break;
1121
1122 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
Simon Glass90975372022-01-23 12:55:12 -07001123 u16_strcmp(dirent->file_name, u".") &&
1124 u16_strcmp(dirent->file_name, u".."))
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001125 tmp_files[count++] = u16_strdup(dirent->file_name);
1126 }
1127 /* ignore an error */
1128 EFI_CALL((*dirh->close)(dirh));
1129
Heinrich Schuchardtba0a2ad2022-12-29 14:44:05 +01001130 /*
1131 * Capsule files are applied in case insensitive alphabetic order
1132 *
1133 * TODO: special handling of rightmost period
1134 */
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001135 qsort(tmp_files, count, sizeof(*tmp_files),
Heinrich Schuchardtba0a2ad2022-12-29 14:44:05 +01001136 (int (*)(const void *, const void *))u16_strcasecmp);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001137 *files = tmp_files;
1138 *num = count;
1139 ret = EFI_SUCCESS;
1140err:
1141 free(dirent);
1142
1143 return ret;
1144}
1145
1146/**
1147 * efi_capsule_read_file - read in a capsule file
1148 * @filename: File name
1149 * @capsule: Pointer to buffer for capsule
1150 *
1151 * Read a capsule file and put its content in @capsule.
1152 *
1153 * Return: status code
1154 */
1155static efi_status_t efi_capsule_read_file(const u16 *filename,
1156 struct efi_capsule_header **capsule)
1157{
1158 struct efi_file_handle *dirh, *fh;
1159 struct efi_file_info *file_info = NULL;
1160 struct efi_capsule_header *buf = NULL;
1161 efi_uintn_t size;
1162 efi_status_t ret;
1163
1164 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1165 EFI_CAPSULE_DIR,
1166 EFI_FILE_MODE_READ, 0));
1167 if (ret != EFI_SUCCESS)
1168 return ret;
1169 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1170 EFI_FILE_MODE_READ, 0));
1171 /* ignore an error */
1172 EFI_CALL((*dirh->close)(dirh));
1173 if (ret != EFI_SUCCESS)
1174 return ret;
1175
1176 /* file size */
1177 size = 0;
1178 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1179 &size, file_info));
1180 if (ret == EFI_BUFFER_TOO_SMALL) {
1181 file_info = malloc(size);
1182 if (!file_info) {
1183 ret = EFI_OUT_OF_RESOURCES;
1184 goto err;
1185 }
1186 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1187 &size, file_info));
1188 }
1189 if (ret != EFI_SUCCESS)
1190 goto err;
1191 size = file_info->file_size;
1192 free(file_info);
1193 buf = malloc(size);
1194 if (!buf) {
1195 ret = EFI_OUT_OF_RESOURCES;
1196 goto err;
1197 }
1198
1199 /* fetch data */
1200 ret = EFI_CALL((*fh->read)(fh, &size, buf));
1201 if (ret == EFI_SUCCESS) {
1202 if (size >= buf->capsule_image_size) {
1203 *capsule = buf;
1204 } else {
1205 free(buf);
1206 ret = EFI_INVALID_PARAMETER;
1207 }
1208 } else {
1209 free(buf);
1210 }
1211err:
1212 EFI_CALL((*fh->close)(fh));
1213
1214 return ret;
1215}
1216
1217/**
1218 * efi_capsule_delete_file - delete a capsule file
1219 * @filename: File name
1220 *
1221 * Delete a capsule file from capsule directory.
1222 *
1223 * Return: status code
1224 */
1225static efi_status_t efi_capsule_delete_file(const u16 *filename)
1226{
1227 struct efi_file_handle *dirh, *fh;
1228 efi_status_t ret;
1229
1230 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1231 EFI_CAPSULE_DIR,
1232 EFI_FILE_MODE_READ, 0));
1233 if (ret != EFI_SUCCESS)
1234 return ret;
1235 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1236 EFI_FILE_MODE_READ, 0));
1237 /* ignore an error */
1238 EFI_CALL((*dirh->close)(dirh));
1239
Heinrich Schuchardte5c22812021-06-02 19:28:22 +02001240 if (ret == EFI_SUCCESS)
1241 ret = EFI_CALL((*fh->delete)(fh));
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001242
1243 return ret;
1244}
1245
1246/**
1247 * efi_capsule_scan_done - reset a scan help function
1248 *
1249 * Reset a scan help function
1250 */
1251static void efi_capsule_scan_done(void)
1252{
1253 EFI_CALL((*bootdev_root->close)(bootdev_root));
1254 bootdev_root = NULL;
1255}
1256
1257/**
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001258 * check_run_capsules() - check whether capsule update should run
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001259 *
1260 * The spec says OsIndications must be set in order to run the capsule update
1261 * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to
1262 * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001263 *
1264 * Return: EFI_SUCCESS if update to run, EFI_NOT_FOUND otherwise
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001265 */
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001266static efi_status_t check_run_capsules(void)
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001267{
Sughosh Ganu0f5ca5c2022-06-01 23:30:39 +05301268 u64 os_indications = 0x0;
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001269 efi_uintn_t size;
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001270 efi_status_t r;
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001271
1272 size = sizeof(os_indications);
Simon Glass90975372022-01-23 12:55:12 -07001273 r = efi_get_variable_int(u"OsIndications", &efi_global_variable_guid,
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001274 NULL, &size, &os_indications, NULL);
Sughosh Ganu0f5ca5c2022-06-01 23:30:39 +05301275 if (!IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS) &&
1276 (r != EFI_SUCCESS || size != sizeof(os_indications)))
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001277 return EFI_NOT_FOUND;
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001278
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001279 if (os_indications &
1280 EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
1281 os_indications &=
1282 ~EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
Simon Glass90975372022-01-23 12:55:12 -07001283 r = efi_set_variable_int(u"OsIndications",
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001284 &efi_global_variable_guid,
1285 EFI_VARIABLE_NON_VOLATILE |
1286 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1287 EFI_VARIABLE_RUNTIME_ACCESS,
1288 sizeof(os_indications),
1289 &os_indications, false);
1290 if (r != EFI_SUCCESS)
1291 log_err("Setting %ls failed\n", L"OsIndications");
1292 return EFI_SUCCESS;
1293 } else if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS)) {
1294 return EFI_SUCCESS;
Sughosh Ganu0f5ca5c2022-06-01 23:30:39 +05301295 } else {
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001296 return EFI_NOT_FOUND;
1297 }
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001298}
1299
1300/**
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001301 * efi_launch_capsule - launch capsules
1302 *
1303 * Launch all the capsules in system at boot time.
1304 * Called by efi init code
1305 *
1306 * Return: status codde
1307 */
1308efi_status_t efi_launch_capsules(void)
1309{
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001310 struct efi_capsule_header *capsule = NULL;
1311 u16 **files;
Etienne Carriere6326e912023-02-16 18:21:41 +01001312 unsigned int nfiles, index, index_max, i;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001313 efi_status_t ret;
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301314 bool capsule_update = true;
1315 bool update_status = true;
1316 bool fw_accept_os = false;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001317
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001318 if (check_run_capsules() != EFI_SUCCESS)
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001319 return EFI_SUCCESS;
1320
Etienne Carriere6326e912023-02-16 18:21:41 +01001321 index_max = get_max_capsule();
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001322 index = get_last_capsule();
1323
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001324 /*
1325 * Find capsules on disk.
1326 * All the capsules are collected at the beginning because
1327 * capsule files will be removed instantly.
1328 */
1329 nfiles = 0;
1330 files = NULL;
1331 ret = efi_capsule_scan_dir(&files, &nfiles);
1332 if (ret != EFI_SUCCESS)
1333 return ret;
1334 if (!nfiles)
1335 return EFI_SUCCESS;
1336
1337 /* Launch capsules */
1338 for (i = 0, ++index; i < nfiles; i++, index++) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001339 log_debug("Applying %ls\n", files[i]);
Etienne Carriere6326e912023-02-16 18:21:41 +01001340 if (index > index_max)
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001341 index = 0;
1342 ret = efi_capsule_read_file(files[i], &capsule);
1343 if (ret == EFI_SUCCESS) {
Masami Hiramatsu3454a692022-02-16 15:15:42 +09001344 ret = efi_capsule_update_firmware(capsule);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301345 if (ret != EFI_SUCCESS) {
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001346 log_err("Applying capsule %ls failed.\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001347 files[i]);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301348 update_status = false;
1349 } else {
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001350 log_info("Applying capsule %ls succeeded.\n",
1351 files[i]);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301352 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1353 fwu_post_update_checks(capsule,
1354 &fw_accept_os,
1355 &capsule_update);
1356 }
1357 }
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001358
Masami Hiramatsu1001a102021-11-12 22:05:15 +09001359 /* create CapsuleXXXX */
1360 set_capsule_result(index, capsule, ret);
1361
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001362 free(capsule);
1363 } else {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001364 log_err("Reading capsule %ls failed\n", files[i]);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301365 update_status = false;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001366 }
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001367 /* delete a capsule either in case of success or failure */
1368 ret = efi_capsule_delete_file(files[i]);
1369 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001370 log_err("Deleting capsule %ls failed\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001371 files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001372 }
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301373
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001374 efi_capsule_scan_done();
1375
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301376 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1377 if (capsule_update == true && update_status == true) {
1378 ret = fwu_post_update_process(fw_accept_os);
1379 } else if (capsule_update == true && update_status == false) {
1380 log_err("All capsules were not updated. Not updating FWU metadata\n");
1381 }
1382 }
1383
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001384 for (i = 0; i < nfiles; i++)
1385 free(files[i]);
1386 free(files);
1387
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001388 /*
1389 * UEFI spec requires to reset system after complete processing capsule
1390 * update on the storage.
1391 */
Masami Hiramatsuff744862022-03-21 22:37:56 +09001392 log_info("Reboot after firmware update.\n");
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001393 /* Cold reset is required for loading the new firmware. */
Masami Hiramatsuff744862022-03-21 22:37:56 +09001394 sysreset_walk_halt(SYSRESET_COLD);
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001395 hang();
1396 /* not reach here */
1397
1398 return 0;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001399}
1400#endif /* CONFIG_EFI_CAPSULE_ON_DISK */