blob: 7a6f195cbc02264f2d2ad8858420403cc9a08dc2 [file] [log] [blame]
AKASHI Takahiro2bc27ca2020-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 Schuchardt3e491192021-07-10 11:03:27 +02009#define LOG_CATEGORY LOGC_EFI
10
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090011#include <common.h>
12#include <efi_loader.h>
13#include <efi_variable.h>
AKASHI Takahiro7a6fb282021-10-07 15:23:32 +090014#include <env.h>
15#include <fdtdec.h>
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090016#include <fs.h>
Sughosh Ganu86794052022-10-21 18:16:03 +053017#include <fwu.h>
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +090018#include <hang.h>
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090019#include <malloc.h>
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +090020#include <mapmem.h>
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090021#include <sort.h>
Masami Hiramatsu7660cfe2022-03-21 22:37:56 +090022#include <sysreset.h>
AKASHI Takahiro7a6fb282021-10-07 15:23:32 +090023#include <asm/global_data.h>
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090024
Sughosh Ganu04be98b2020-12-30 19:27:09 +053025#include <crypto/pkcs7.h>
26#include <crypto/pkcs7_parser.h>
27#include <linux/err.h>
28
AKASHI Takahiro7a6fb282021-10-07 15:23:32 +090029DECLARE_GLOBAL_DATA_PTR;
30
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090031const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
AKASHI Takahiro8d990262020-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 Ganu86794052022-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 Takahiro2bc27ca2020-11-17 09:27:55 +090042
AKASHI Takahiroc74cd8b2020-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 Carriere44293932023-02-16 18:21:41 +010048static __maybe_unused unsigned int get_capsule_index(const u16 *variable_name)
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090049{
50 u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */
Heinrich Schuchardt15bbcaf2021-02-09 20:20:34 +010051 char value[5];
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090052 efi_uintn_t size;
53 unsigned long index = 0xffff;
54 efi_status_t ret;
Heinrich Schuchardt15bbcaf2021-02-09 20:20:34 +010055 int i;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090056
57 size = sizeof(value16);
Etienne Carriere44293932023-02-16 18:21:41 +010058 ret = efi_get_variable_int(variable_name, &efi_guid_capsule_report,
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090059 NULL, &size, value16, NULL);
Heinrich Schuchardt15bbcaf2021-02-09 20:20:34 +010060 if (ret != EFI_SUCCESS || size != 22 ||
Simon Glass156ccbc2022-01-23 12:55:12 -070061 u16_strncmp(value16, u"Capsule", 7))
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090062 goto err;
Heinrich Schuchardt15bbcaf2021-02-09 20:20:34 +010063 for (i = 0; i < 4; ++i) {
64 u16 c = value16[i + 7];
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090065
Heinrich Schuchardt15bbcaf2021-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 Takahiro2bc27ca2020-11-17 09:27:55 +090073err:
74 return index;
75}
76
77/**
Etienne Carriere44293932023-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 Takahiro2bc27ca2020-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 Apalodimasfe179d72020-12-31 12:26:46 +0200123 efi_create_indexed_name(variable_name16, sizeof(variable_name16),
124 "Capsule", index);
AKASHI Takahiro2bc27ca2020-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 Schuchardtd7eedd92021-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 Schuchardt70bad542021-07-10 11:14:13 +0200138 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200139 log_err("Setting %ls failed\n", variable_name16);
Heinrich Schuchardt70bad542021-07-10 11:14:13 +0200140 return;
141 }
142
143 /* Variable CapsuleLast must not include terminating 0x0000 */
Simon Glass156ccbc2022-01-23 12:55:12 -0700144 ret = efi_set_variable_int(u"CapsuleLast", &efi_guid_capsule_report,
Heinrich Schuchardt70bad542021-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 Glass156ccbc2022-01-23 12:55:12 -0700151 log_err("Setting %ls failed\n", u"CapsuleLast");
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900152}
153
AKASHI Takahiro8d990262020-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 Ganu7cf06f02022-04-15 11:29:36 +0530158 * @image_index: Image Index
AKASHI Takahiro8d990262020-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 Ganu7cf06f02022-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 Takahiro8d990262020-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 Schuchardt56c9f0c2022-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 Takahiro8d990262020-11-30 18:12:11 +0900193 if (ret != EFI_SUCCESS)
194 continue;
Heinrich Schuchardt56c9f0c2022-10-07 15:29:52 +0200195 fmp = fmp_handler->protocol_interface;
AKASHI Takahiro8d990262020-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 Ganu7cf06f02022-04-15 11:29:36 +0530236 (desc->image_index == image_index) &&
AKASHI Takahiro8d990262020-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 Takahiro8d990262020-11-30 18:12:11 +0900246 if (found)
247 return fmp;
248 }
249
250 return NULL;
251}
252
AKASHI Takahiroc2cc60c2021-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 Ganu04be98b2020-12-30 19:27:09 +0530286#if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
AKASHI Takahiro50b05eb2021-11-02 09:55:01 +0900287int efi_get_public_key_data(void **pkey, efi_uintn_t *pkey_len)
AKASHI Takahiro7a6fb282021-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 Ganu04be98b2020-12-30 19:27:09 +0530318
Sughosh Ganu04be98b2020-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 Glass47a25e82021-08-02 08:44:31 -0600324 void *fdt_pkey, *pkey;
Sughosh Ganu04be98b2020-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 Takahiroc2cc60c2021-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)
345 goto out;
346
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530347 auth_hdr = (struct efi_firmware_image_authentication *)capsule;
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530348 if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
349 goto out;
350
Sughosh Ganu04be98b2020-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);
371 if (IS_ERR(capsule_sig)) {
372 debug("Parsing variable's pkcs7 header failed\n");
373 capsule_sig = NULL;
374 goto out;
375 }
376
Simon Glass47a25e82021-08-02 08:44:31 -0600377 ret = efi_get_public_key_data(&fdt_pkey, &pkey_len);
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530378 if (ret < 0)
379 goto out;
380
381 pkey = malloc(pkey_len);
382 if (!pkey)
383 goto out;
384
Simon Glass47a25e82021-08-02 08:44:31 -0600385 memcpy(pkey, fdt_pkey, pkey_len);
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530386 truststore = efi_build_signature_store(pkey, pkey_len);
387 if (!truststore)
388 goto out;
389
390 /* verify signature */
391 if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) {
392 debug("Verified\n");
393 } else {
394 debug("Verifying variable's signature failed\n");
395 goto out;
396 }
397
398 status = EFI_SUCCESS;
399
400out:
401 efi_sigstore_free(truststore);
402 pkcs7_free_message(capsule_sig);
403 free(regs);
404
405 return status;
406}
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530407#endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */
408
Sughosh Ganu86794052022-10-21 18:16:03 +0530409static __maybe_unused bool fwu_empty_capsule(struct efi_capsule_header *capsule)
410{
411 return !guidcmp(&capsule->capsule_guid,
412 &fwu_guid_os_request_fw_revert) ||
413 !guidcmp(&capsule->capsule_guid,
414 &fwu_guid_os_request_fw_accept);
415}
416
417static __maybe_unused efi_status_t fwu_to_efi_error(int err)
418{
419 efi_status_t ret;
420
421 switch(err) {
422 case 0:
423 ret = EFI_SUCCESS;
424 break;
425 case -ERANGE:
426 case -EIO:
427 ret = EFI_DEVICE_ERROR;
428 break;
429 case -EINVAL:
430 ret = EFI_INVALID_PARAMETER;
431 break;
432 case -ENODEV:
433 ret = EFI_NOT_FOUND;
434 break;
435 default:
436 ret = EFI_OUT_OF_RESOURCES;
437 }
438
439 return ret;
440}
441
442static __maybe_unused efi_status_t fwu_empty_capsule_process(
443 struct efi_capsule_header *capsule)
444{
445 int status;
446 u32 active_idx;
447 efi_guid_t *image_guid;
448 efi_status_t ret = EFI_INVALID_PARAMETER;
449
450 if (!guidcmp(&capsule->capsule_guid,
451 &fwu_guid_os_request_fw_revert)) {
452 /*
453 * One of the previously updated image has
454 * failed the OS acceptance test. OS has
455 * requested to revert back to the earlier
456 * boot index
457 */
458 status = fwu_revert_boot_index();
459 ret = fwu_to_efi_error(status);
460 if (ret == EFI_SUCCESS)
461 log_debug("Reverted the FWU active_index. Recommend rebooting the system\n");
462 else
463 log_err("Failed to revert the FWU boot index\n");
464 } else if (!guidcmp(&capsule->capsule_guid,
465 &fwu_guid_os_request_fw_accept)) {
466 /*
467 * Image accepted by the OS. Set the acceptance
468 * status for the image.
469 */
470 image_guid = (void *)(char *)capsule +
471 capsule->header_size;
472
473 status = fwu_get_active_index(&active_idx);
474 ret = fwu_to_efi_error(status);
475 if (ret != EFI_SUCCESS) {
476 log_err("Unable to get the active_index from the FWU metadata\n");
477 return ret;
478 }
479
480 status = fwu_accept_image(image_guid, active_idx);
481 ret = fwu_to_efi_error(status);
482 if (ret != EFI_SUCCESS)
483 log_err("Unable to set the Accept bit for the image %pUs\n",
484 image_guid);
485 }
486
487 return ret;
488}
489
490static __maybe_unused void fwu_post_update_checks(
491 struct efi_capsule_header *capsule,
492 bool *fw_accept_os, bool *capsule_update)
493{
494 if (fwu_empty_capsule(capsule))
495 *capsule_update = false;
496 else
497 if (!*fw_accept_os)
498 *fw_accept_os =
499 capsule->flags & FW_ACCEPT_OS ? true : false;
500}
501
502static __maybe_unused efi_status_t fwu_post_update_process(bool fw_accept_os)
503{
504 int status;
505 uint update_index;
506 efi_status_t ret;
507
508 status = fwu_plat_get_update_index(&update_index);
509 if (status < 0) {
510 log_err("Failed to get the FWU update_index value\n");
511 return EFI_DEVICE_ERROR;
512 }
513
514 /*
515 * All the capsules have been updated successfully,
516 * update the FWU metadata.
517 */
518 log_debug("Update Complete. Now updating active_index to %u\n",
519 update_index);
520 status = fwu_set_active_index(update_index);
521 ret = fwu_to_efi_error(status);
522 if (ret != EFI_SUCCESS) {
523 log_err("Failed to update FWU metadata index values\n");
524 } else {
525 log_debug("Successfully updated the active_index\n");
526 if (fw_accept_os) {
527 status = fwu_trial_state_ctr_start();
528 if (status < 0)
529 ret = EFI_DEVICE_ERROR;
530 }
531 }
532
533 return ret;
534}
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530535
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900536/**
537 * efi_capsule_update_firmware - update firmware from capsule
538 * @capsule_data: Capsule
539 *
540 * Update firmware, using a capsule, @capsule_data. Loading any FMP
541 * drivers embedded in a capsule is not supported.
542 *
543 * Return: status code
544 */
545static efi_status_t efi_capsule_update_firmware(
546 struct efi_capsule_header *capsule_data)
547{
548 struct efi_firmware_management_capsule_header *capsule;
549 struct efi_firmware_management_capsule_image_header *image;
AKASHI Takahiroc2cc60c2021-07-20 14:52:05 +0900550 size_t capsule_size, image_binary_size;
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900551 void *image_binary, *vendor_code;
552 efi_handle_t *handles;
553 efi_uintn_t no_handles;
554 int item;
555 struct efi_firmware_management_protocol *fmp;
556 u16 *abort_reason;
Sughosh Ganu86794052022-10-21 18:16:03 +0530557 efi_guid_t *image_type_id;
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900558 efi_status_t ret = EFI_SUCCESS;
Sughosh Ganu86794052022-10-21 18:16:03 +0530559 int status;
560 uint update_index;
561 bool fw_accept_os;
562
563 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
564 if (fwu_empty_capsule_checks_pass() &&
565 fwu_empty_capsule(capsule_data))
566 return fwu_empty_capsule_process(capsule_data);
567
568 if (!fwu_update_checks_pass()) {
569 log_err("FWU checks failed. Cannot start update\n");
570 return EFI_INVALID_PARAMETER;
571 }
572
573
574 /* Obtain the update_index from the platform */
575 status = fwu_plat_get_update_index(&update_index);
576 if (status < 0) {
577 log_err("Failed to get the FWU update_index value\n");
578 return EFI_DEVICE_ERROR;
579 }
580
581 fw_accept_os = capsule_data->flags & FW_ACCEPT_OS ? 0x1 : 0x0;
582 }
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900583
584 /* sanity check */
585 if (capsule_data->header_size < sizeof(*capsule) ||
586 capsule_data->header_size >= capsule_data->capsule_image_size)
587 return EFI_INVALID_PARAMETER;
588
589 capsule = (void *)capsule_data + capsule_data->header_size;
590 capsule_size = capsule_data->capsule_image_size
591 - capsule_data->header_size;
592
593 if (capsule->version != 0x00000001)
594 return EFI_UNSUPPORTED;
595
596 handles = NULL;
597 ret = EFI_CALL(efi_locate_handle_buffer(
598 BY_PROTOCOL,
599 &efi_guid_firmware_management_protocol,
600 NULL, &no_handles, (efi_handle_t **)&handles));
601 if (ret != EFI_SUCCESS)
602 return EFI_UNSUPPORTED;
603
604 /* Payload */
605 for (item = capsule->embedded_driver_count;
606 item < capsule->embedded_driver_count
607 + capsule->payload_item_count; item++) {
608 /* sanity check */
609 if ((capsule->item_offset_list[item] + sizeof(*image)
610 >= capsule_size)) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200611 log_err("Capsule does not have enough data\n");
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900612 ret = EFI_INVALID_PARAMETER;
613 goto out;
614 }
615
616 image = (void *)capsule + capsule->item_offset_list[item];
617
618 if (image->version != 0x00000003) {
619 ret = EFI_UNSUPPORTED;
620 goto out;
621 }
622
623 /* find a device for update firmware */
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900624 fmp = efi_fmp_find(&image->update_image_type_id,
Sughosh Ganu7cf06f02022-04-15 11:29:36 +0530625 image->update_image_index,
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900626 image->update_hardware_instance,
627 handles, no_handles);
628 if (!fmp) {
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100629 log_err("FMP driver not found for firmware type %pUs, hardware instance %lld\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900630 &image->update_image_type_id,
631 image->update_hardware_instance);
632 ret = EFI_UNSUPPORTED;
633 goto out;
634 }
635
636 /* do update */
AKASHI Takahiroc2cc60c2021-07-20 14:52:05 +0900637 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
638 !(image->image_capsule_support &
639 CAPSULE_SUPPORT_AUTHENTICATION)) {
640 /* no signature */
641 ret = EFI_SECURITY_VIOLATION;
642 goto out;
643 }
644
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900645 image_binary = (void *)image + sizeof(*image);
AKASHI Takahiroc2cc60c2021-07-20 14:52:05 +0900646 image_binary_size = image->update_image_size;
647 vendor_code = image_binary + image_binary_size;
648 if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
649 (image->image_capsule_support &
650 CAPSULE_SUPPORT_AUTHENTICATION)) {
651 ret = efi_remove_auth_hdr(&image_binary,
652 &image_binary_size);
653 if (ret != EFI_SUCCESS)
654 goto out;
655 }
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900656
657 abort_reason = NULL;
658 ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
659 image_binary,
AKASHI Takahiroc2cc60c2021-07-20 14:52:05 +0900660 image_binary_size,
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900661 vendor_code, NULL,
662 &abort_reason));
663 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200664 log_err("Firmware update failed: %ls\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900665 abort_reason);
666 efi_free_pool(abort_reason);
667 goto out;
668 }
Sughosh Ganu86794052022-10-21 18:16:03 +0530669
670 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
671 image_type_id = &image->update_image_type_id;
672 if (!fw_accept_os) {
673 /*
674 * The OS will not be accepting the firmware
675 * images. Set the accept bit of all the
676 * images contained in this capsule.
677 */
678 status = fwu_accept_image(image_type_id,
679 update_index);
680 } else {
681 status = fwu_clear_accept_image(image_type_id,
682 update_index);
683 }
684 ret = fwu_to_efi_error(status);
685 if (ret != EFI_SUCCESS) {
686 log_err("Unable to %s the accept bit for the image %pUs\n",
687 fw_accept_os ? "clear" : "set",
688 image_type_id);
689 goto out;
690 }
691
692 log_debug("%s the accepted bit for Image %pUs\n",
693 fw_accept_os ? "Cleared" : "Set",
694 image_type_id);
695 }
696
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900697 }
698
699out:
700 efi_free_pool(handles);
701
702 return ret;
703}
704#else
705static efi_status_t efi_capsule_update_firmware(
706 struct efi_capsule_header *capsule_data)
707{
708 return EFI_UNSUPPORTED;
709}
710#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
711
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900712/**
713 * efi_update_capsule() - process information from operating system
714 * @capsule_header_array: Array of virtual address pointers
715 * @capsule_count: Number of pointers in capsule_header_array
716 * @scatter_gather_list: Array of physical address pointers
717 *
718 * This function implements the UpdateCapsule() runtime service.
719 *
720 * See the Unified Extensible Firmware Interface (UEFI) specification for
721 * details.
722 *
723 * Return: status code
724 */
725efi_status_t EFIAPI efi_update_capsule(
726 struct efi_capsule_header **capsule_header_array,
727 efi_uintn_t capsule_count,
728 u64 scatter_gather_list)
729{
730 struct efi_capsule_header *capsule;
731 unsigned int i;
732 efi_status_t ret;
733
Simon Glassdf7d89a2021-02-07 14:27:02 -0700734 EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900735 scatter_gather_list);
736
737 if (!capsule_count) {
738 ret = EFI_INVALID_PARAMETER;
739 goto out;
740 }
741
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900742 ret = EFI_SUCCESS;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900743 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
744 i++, capsule = *(++capsule_header_array)) {
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900745 /* sanity check */
746 if (capsule->header_size < sizeof(*capsule) ||
747 capsule->capsule_image_size < sizeof(*capsule)) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200748 log_err("Capsule does not have enough data\n");
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900749 continue;
750 }
751
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100752 log_debug("Capsule[%d] (guid:%pUs)\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900753 i, &capsule->capsule_guid);
754 if (!guidcmp(&capsule->capsule_guid,
755 &efi_guid_firmware_management_capsule_id)) {
756 ret = efi_capsule_update_firmware(capsule);
757 } else {
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100758 log_err("Unsupported capsule type: %pUs\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900759 &capsule->capsule_guid);
760 ret = EFI_UNSUPPORTED;
761 }
762
763 if (ret != EFI_SUCCESS)
764 goto out;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900765 }
Jose Marinho64a8aae2021-03-02 17:26:38 +0000766
767 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
768 /* Rebuild the ESRT to reflect any updated FW images. */
769 ret = efi_esrt_populate();
770 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200771 log_warning("ESRT update failed\n");
Jose Marinho64a8aae2021-03-02 17:26:38 +0000772 }
Jose Marinho3627cf42021-04-19 14:54:33 +0100773out:
Jose Marinho64a8aae2021-03-02 17:26:38 +0000774
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900775 return EFI_EXIT(ret);
776}
777
778/**
779 * efi_query_capsule_caps() - check if capsule is supported
780 * @capsule_header_array: Array of virtual pointers
781 * @capsule_count: Number of pointers in capsule_header_array
782 * @maximum_capsule_size: Maximum capsule size
783 * @reset_type: Type of reset needed for capsule update
784 *
785 * This function implements the QueryCapsuleCapabilities() runtime service.
786 *
787 * See the Unified Extensible Firmware Interface (UEFI) specification for
788 * details.
789 *
790 * Return: status code
791 */
792efi_status_t EFIAPI efi_query_capsule_caps(
793 struct efi_capsule_header **capsule_header_array,
794 efi_uintn_t capsule_count,
795 u64 *maximum_capsule_size,
796 u32 *reset_type)
797{
798 struct efi_capsule_header *capsule __attribute__((unused));
799 unsigned int i;
800 efi_status_t ret;
801
Simon Glassdf7d89a2021-02-07 14:27:02 -0700802 EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900803 maximum_capsule_size, reset_type);
804
805 if (!maximum_capsule_size) {
806 ret = EFI_INVALID_PARAMETER;
807 goto out;
808 }
809
810 *maximum_capsule_size = U64_MAX;
811 *reset_type = EFI_RESET_COLD;
812
813 ret = EFI_SUCCESS;
814 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
815 i++, capsule = *(++capsule_header_array)) {
816 /* TODO */
817 }
818out:
819 return EFI_EXIT(ret);
820}
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900821
Masami Hiramatsu39bdf112022-03-21 22:37:45 +0900822/**
823 * efi_load_capsule_drivers - initialize capsule drivers
824 *
825 * Generic FMP drivers backed by DFU
826 *
827 * Return: status code
828 */
829efi_status_t __weak efi_load_capsule_drivers(void)
830{
831 __maybe_unused efi_handle_t handle;
832 efi_status_t ret = EFI_SUCCESS;
833
834 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
835 handle = NULL;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +0300836 ret = efi_install_multiple_protocol_interfaces(&handle,
837 &efi_guid_firmware_management_protocol,
838 &efi_fmp_fit,
839 NULL);
Masami Hiramatsu39bdf112022-03-21 22:37:45 +0900840 }
841
842 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
843 handle = NULL;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +0300844 ret = efi_install_multiple_protocol_interfaces(&handle,
845 &efi_guid_firmware_management_protocol,
846 &efi_fmp_raw,
847 NULL);
Masami Hiramatsu39bdf112022-03-21 22:37:45 +0900848 }
849
850 return ret;
851}
852
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900853#ifdef CONFIG_EFI_CAPSULE_ON_DISK
854/**
855 * get_dp_device - retrieve a device path from boot variable
856 * @boot_var: Boot variable name
857 * @device_dp Device path
858 *
859 * Retrieve a device patch from boot variable, @boot_var.
860 *
861 * Return: status code
862 */
863static efi_status_t get_dp_device(u16 *boot_var,
864 struct efi_device_path **device_dp)
865{
866 void *buf = NULL;
867 efi_uintn_t size;
868 struct efi_load_option lo;
869 struct efi_device_path *file_dp;
870 efi_status_t ret;
871
872 size = 0;
873 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
874 NULL, &size, NULL, NULL);
875 if (ret == EFI_BUFFER_TOO_SMALL) {
876 buf = malloc(size);
877 if (!buf)
878 return EFI_OUT_OF_RESOURCES;
879 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
880 NULL, &size, buf, NULL);
881 }
882 if (ret != EFI_SUCCESS)
883 return ret;
884
885 efi_deserialize_load_option(&lo, buf, &size);
886
887 if (lo.attributes & LOAD_OPTION_ACTIVE) {
888 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
889 efi_free_pool(file_dp);
890
891 ret = EFI_SUCCESS;
892 } else {
893 ret = EFI_NOT_FOUND;
894 }
895
896 free(buf);
897
898 return ret;
899}
900
901/**
902 * device_is_present_and_system_part - check if a device exists
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900903 *
904 * Check if a device pointed to by the device path, @dp, exists and is
905 * located in UEFI system partition.
906 *
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100907 * @dp device path
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900908 * Return: true - yes, false - no
909 */
910static bool device_is_present_and_system_part(struct efi_device_path *dp)
911{
912 efi_handle_t handle;
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100913 struct efi_device_path *rem;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900914
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100915 /* Check device exists */
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +0100916 handle = efi_dp_find_obj(dp, NULL, NULL);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900917 if (!handle)
918 return false;
919
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100920 /* Check device is on system partition */
921 handle = efi_dp_find_obj(dp, &efi_system_partition_guid, &rem);
922 if (!handle)
923 return false;
924
925 return true;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900926}
927
928/**
929 * find_boot_device - identify the boot device
930 *
931 * Identify the boot device from boot-related variables as UEFI
932 * specification describes and put its handle into bootdev_root.
933 *
934 * Return: status code
935 */
936static efi_status_t find_boot_device(void)
937{
938 char boot_var[9];
939 u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
940 efi_uintn_t size;
941 int i, num;
942 struct efi_simple_file_system_protocol *volume;
943 struct efi_device_path *boot_dev = NULL;
944 efi_status_t ret;
945
946 /* find active boot device in BootNext */
947 bootnext = 0;
948 size = sizeof(bootnext);
Simon Glass156ccbc2022-01-23 12:55:12 -0700949 ret = efi_get_variable_int(u"BootNext",
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900950 (efi_guid_t *)&efi_global_variable_guid,
951 NULL, &size, &bootnext, NULL);
952 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
953 /* BootNext does exist here */
954 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900955 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900956 goto skip;
957 }
958 sprintf((char *)boot_var, "Boot%04X", bootnext);
959 p = boot_var16;
960 utf8_utf16_strcpy(&p, boot_var);
961
962 ret = get_dp_device(boot_var16, &boot_dev);
963 if (ret == EFI_SUCCESS) {
964 if (device_is_present_and_system_part(boot_dev)) {
Masami Hiramatsub0b14492021-07-12 18:05:17 +0900965 goto found;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900966 } else {
967 efi_free_pool(boot_dev);
968 boot_dev = NULL;
969 }
970 }
971 }
972
973skip:
974 /* find active boot device in BootOrder */
975 size = 0;
Simon Glass156ccbc2022-01-23 12:55:12 -0700976 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900977 NULL, &size, NULL, NULL);
978 if (ret == EFI_BUFFER_TOO_SMALL) {
979 boot_order = malloc(size);
980 if (!boot_order) {
981 ret = EFI_OUT_OF_RESOURCES;
982 goto out;
983 }
984
Simon Glass156ccbc2022-01-23 12:55:12 -0700985 ret = efi_get_variable_int(u"BootOrder",
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900986 &efi_global_variable_guid,
987 NULL, &size, boot_order, NULL);
988 }
989 if (ret != EFI_SUCCESS)
990 goto out;
991
992 /* check in higher order */
993 num = size / sizeof(u16);
994 for (i = 0; i < num; i++) {
995 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
996 p = boot_var16;
997 utf8_utf16_strcpy(&p, boot_var);
998 ret = get_dp_device(boot_var16, &boot_dev);
999 if (ret != EFI_SUCCESS)
1000 continue;
1001
1002 if (device_is_present_and_system_part(boot_dev))
1003 break;
1004
1005 efi_free_pool(boot_dev);
1006 boot_dev = NULL;
1007 }
Masami Hiramatsub0b14492021-07-12 18:05:17 +09001008found:
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001009 if (boot_dev) {
Masami Hiramatsu484d7d82021-07-14 14:19:13 +09001010 log_debug("Boot device %pD\n", boot_dev);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001011
1012 volume = efi_fs_from_path(boot_dev);
1013 if (!volume)
1014 ret = EFI_DEVICE_ERROR;
1015 else
1016 ret = EFI_CALL(volume->open_volume(volume,
1017 &bootdev_root));
1018 efi_free_pool(boot_dev);
1019 } else {
1020 ret = EFI_NOT_FOUND;
1021 }
AKASHI Takahiro2c1a6842021-04-20 10:03:16 +09001022out:
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001023 free(boot_order);
1024
1025 return ret;
1026}
1027
1028/**
1029 * efi_capsule_scan_dir - traverse a capsule directory in boot device
1030 * @files: Array of file names
1031 * @num: Number of elements in @files
1032 *
1033 * Traverse a capsule directory in boot device.
1034 * Called by initialization code, and returns an array of capsule file
1035 * names in @files.
1036 *
1037 * Return: status code
1038 */
1039static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
1040{
1041 struct efi_file_handle *dirh;
1042 struct efi_file_info *dirent;
1043 efi_uintn_t dirent_size, tmp_size;
1044 unsigned int count;
1045 u16 **tmp_files;
1046 efi_status_t ret;
1047
1048 ret = find_boot_device();
1049 if (ret == EFI_NOT_FOUND) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001050 log_debug("Boot device is not set\n");
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001051 *num = 0;
1052 return EFI_SUCCESS;
1053 } else if (ret != EFI_SUCCESS) {
1054 return EFI_DEVICE_ERROR;
1055 }
1056
1057 /* count capsule files */
1058 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1059 EFI_CAPSULE_DIR,
1060 EFI_FILE_MODE_READ, 0));
1061 if (ret != EFI_SUCCESS) {
1062 *num = 0;
1063 return EFI_SUCCESS;
1064 }
1065
1066 dirent_size = 256;
1067 dirent = malloc(dirent_size);
1068 if (!dirent)
1069 return EFI_OUT_OF_RESOURCES;
1070
1071 count = 0;
1072 while (1) {
1073 tmp_size = dirent_size;
1074 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1075 if (ret == EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardte8287b02021-04-11 06:53:04 +02001076 struct efi_file_info *old_dirent = dirent;
1077
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001078 dirent = realloc(dirent, tmp_size);
1079 if (!dirent) {
Heinrich Schuchardte8287b02021-04-11 06:53:04 +02001080 dirent = old_dirent;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001081 ret = EFI_OUT_OF_RESOURCES;
1082 goto err;
1083 }
1084 dirent_size = tmp_size;
1085 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1086 }
1087 if (ret != EFI_SUCCESS)
1088 goto err;
1089 if (!tmp_size)
1090 break;
1091
Heinrich Schuchardt841f7a42021-02-09 17:45:33 +01001092 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001093 count++;
1094 }
1095
1096 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
1097 if (ret != EFI_SUCCESS)
1098 goto err;
1099
1100 /* make a list */
AKASHI Takahiro8f1844c2021-01-22 10:43:27 +09001101 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001102 if (!tmp_files) {
1103 ret = EFI_OUT_OF_RESOURCES;
1104 goto err;
1105 }
1106
1107 count = 0;
1108 while (1) {
1109 tmp_size = dirent_size;
1110 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1111 if (ret != EFI_SUCCESS)
1112 goto err;
1113 if (!tmp_size)
1114 break;
1115
1116 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
Simon Glass156ccbc2022-01-23 12:55:12 -07001117 u16_strcmp(dirent->file_name, u".") &&
1118 u16_strcmp(dirent->file_name, u".."))
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001119 tmp_files[count++] = u16_strdup(dirent->file_name);
1120 }
1121 /* ignore an error */
1122 EFI_CALL((*dirh->close)(dirh));
1123
Heinrich Schuchardt93cdb952022-12-29 14:44:05 +01001124 /*
1125 * Capsule files are applied in case insensitive alphabetic order
1126 *
1127 * TODO: special handling of rightmost period
1128 */
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001129 qsort(tmp_files, count, sizeof(*tmp_files),
Heinrich Schuchardt93cdb952022-12-29 14:44:05 +01001130 (int (*)(const void *, const void *))u16_strcasecmp);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001131 *files = tmp_files;
1132 *num = count;
1133 ret = EFI_SUCCESS;
1134err:
1135 free(dirent);
1136
1137 return ret;
1138}
1139
1140/**
1141 * efi_capsule_read_file - read in a capsule file
1142 * @filename: File name
1143 * @capsule: Pointer to buffer for capsule
1144 *
1145 * Read a capsule file and put its content in @capsule.
1146 *
1147 * Return: status code
1148 */
1149static efi_status_t efi_capsule_read_file(const u16 *filename,
1150 struct efi_capsule_header **capsule)
1151{
1152 struct efi_file_handle *dirh, *fh;
1153 struct efi_file_info *file_info = NULL;
1154 struct efi_capsule_header *buf = NULL;
1155 efi_uintn_t size;
1156 efi_status_t ret;
1157
1158 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1159 EFI_CAPSULE_DIR,
1160 EFI_FILE_MODE_READ, 0));
1161 if (ret != EFI_SUCCESS)
1162 return ret;
1163 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1164 EFI_FILE_MODE_READ, 0));
1165 /* ignore an error */
1166 EFI_CALL((*dirh->close)(dirh));
1167 if (ret != EFI_SUCCESS)
1168 return ret;
1169
1170 /* file size */
1171 size = 0;
1172 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1173 &size, file_info));
1174 if (ret == EFI_BUFFER_TOO_SMALL) {
1175 file_info = malloc(size);
1176 if (!file_info) {
1177 ret = EFI_OUT_OF_RESOURCES;
1178 goto err;
1179 }
1180 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1181 &size, file_info));
1182 }
1183 if (ret != EFI_SUCCESS)
1184 goto err;
1185 size = file_info->file_size;
1186 free(file_info);
1187 buf = malloc(size);
1188 if (!buf) {
1189 ret = EFI_OUT_OF_RESOURCES;
1190 goto err;
1191 }
1192
1193 /* fetch data */
1194 ret = EFI_CALL((*fh->read)(fh, &size, buf));
1195 if (ret == EFI_SUCCESS) {
1196 if (size >= buf->capsule_image_size) {
1197 *capsule = buf;
1198 } else {
1199 free(buf);
1200 ret = EFI_INVALID_PARAMETER;
1201 }
1202 } else {
1203 free(buf);
1204 }
1205err:
1206 EFI_CALL((*fh->close)(fh));
1207
1208 return ret;
1209}
1210
1211/**
1212 * efi_capsule_delete_file - delete a capsule file
1213 * @filename: File name
1214 *
1215 * Delete a capsule file from capsule directory.
1216 *
1217 * Return: status code
1218 */
1219static efi_status_t efi_capsule_delete_file(const u16 *filename)
1220{
1221 struct efi_file_handle *dirh, *fh;
1222 efi_status_t ret;
1223
1224 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1225 EFI_CAPSULE_DIR,
1226 EFI_FILE_MODE_READ, 0));
1227 if (ret != EFI_SUCCESS)
1228 return ret;
1229 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1230 EFI_FILE_MODE_READ, 0));
1231 /* ignore an error */
1232 EFI_CALL((*dirh->close)(dirh));
1233
Heinrich Schuchardte27b0ff2021-06-02 19:28:22 +02001234 if (ret == EFI_SUCCESS)
1235 ret = EFI_CALL((*fh->delete)(fh));
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001236
1237 return ret;
1238}
1239
1240/**
1241 * efi_capsule_scan_done - reset a scan help function
1242 *
1243 * Reset a scan help function
1244 */
1245static void efi_capsule_scan_done(void)
1246{
1247 EFI_CALL((*bootdev_root->close)(bootdev_root));
1248 bootdev_root = NULL;
1249}
1250
1251/**
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001252 * check_run_capsules() - check whether capsule update should run
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001253 *
1254 * The spec says OsIndications must be set in order to run the capsule update
1255 * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to
1256 * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001257 *
1258 * Return: EFI_SUCCESS if update to run, EFI_NOT_FOUND otherwise
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001259 */
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001260static efi_status_t check_run_capsules(void)
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001261{
Sughosh Ganu119fafd2022-06-01 23:30:39 +05301262 u64 os_indications = 0x0;
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001263 efi_uintn_t size;
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001264 efi_status_t r;
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001265
1266 size = sizeof(os_indications);
Simon Glass156ccbc2022-01-23 12:55:12 -07001267 r = efi_get_variable_int(u"OsIndications", &efi_global_variable_guid,
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001268 NULL, &size, &os_indications, NULL);
Sughosh Ganu119fafd2022-06-01 23:30:39 +05301269 if (!IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS) &&
1270 (r != EFI_SUCCESS || size != sizeof(os_indications)))
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001271 return EFI_NOT_FOUND;
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001272
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001273 if (os_indications &
1274 EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
1275 os_indications &=
1276 ~EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
Simon Glass156ccbc2022-01-23 12:55:12 -07001277 r = efi_set_variable_int(u"OsIndications",
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001278 &efi_global_variable_guid,
1279 EFI_VARIABLE_NON_VOLATILE |
1280 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1281 EFI_VARIABLE_RUNTIME_ACCESS,
1282 sizeof(os_indications),
1283 &os_indications, false);
1284 if (r != EFI_SUCCESS)
1285 log_err("Setting %ls failed\n", L"OsIndications");
1286 return EFI_SUCCESS;
1287 } else if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS)) {
1288 return EFI_SUCCESS;
Sughosh Ganu119fafd2022-06-01 23:30:39 +05301289 } else {
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001290 return EFI_NOT_FOUND;
1291 }
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001292}
1293
1294/**
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001295 * efi_launch_capsule - launch capsules
1296 *
1297 * Launch all the capsules in system at boot time.
1298 * Called by efi init code
1299 *
1300 * Return: status codde
1301 */
1302efi_status_t efi_launch_capsules(void)
1303{
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001304 struct efi_capsule_header *capsule = NULL;
1305 u16 **files;
Etienne Carriere44293932023-02-16 18:21:41 +01001306 unsigned int nfiles, index, index_max, i;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001307 efi_status_t ret;
Sughosh Ganu86794052022-10-21 18:16:03 +05301308 bool capsule_update = true;
1309 bool update_status = true;
1310 bool fw_accept_os = false;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001311
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001312 if (check_run_capsules() != EFI_SUCCESS)
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001313 return EFI_SUCCESS;
1314
Etienne Carriere44293932023-02-16 18:21:41 +01001315 index_max = get_max_capsule();
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001316 index = get_last_capsule();
1317
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001318 /*
1319 * Find capsules on disk.
1320 * All the capsules are collected at the beginning because
1321 * capsule files will be removed instantly.
1322 */
1323 nfiles = 0;
1324 files = NULL;
1325 ret = efi_capsule_scan_dir(&files, &nfiles);
1326 if (ret != EFI_SUCCESS)
1327 return ret;
1328 if (!nfiles)
1329 return EFI_SUCCESS;
1330
1331 /* Launch capsules */
1332 for (i = 0, ++index; i < nfiles; i++, index++) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001333 log_debug("Applying %ls\n", files[i]);
Etienne Carriere44293932023-02-16 18:21:41 +01001334 if (index > index_max)
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001335 index = 0;
1336 ret = efi_capsule_read_file(files[i], &capsule);
1337 if (ret == EFI_SUCCESS) {
Masami Hiramatsua6aafce2022-02-16 15:15:42 +09001338 ret = efi_capsule_update_firmware(capsule);
Sughosh Ganu86794052022-10-21 18:16:03 +05301339 if (ret != EFI_SUCCESS) {
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001340 log_err("Applying capsule %ls failed.\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001341 files[i]);
Sughosh Ganu86794052022-10-21 18:16:03 +05301342 update_status = false;
1343 } else {
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001344 log_info("Applying capsule %ls succeeded.\n",
1345 files[i]);
Sughosh Ganu86794052022-10-21 18:16:03 +05301346 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1347 fwu_post_update_checks(capsule,
1348 &fw_accept_os,
1349 &capsule_update);
1350 }
1351 }
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001352
Masami Hiramatsu5d49b322021-11-12 22:05:15 +09001353 /* create CapsuleXXXX */
1354 set_capsule_result(index, capsule, ret);
1355
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001356 free(capsule);
1357 } else {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001358 log_err("Reading capsule %ls failed\n", files[i]);
Sughosh Ganu86794052022-10-21 18:16:03 +05301359 update_status = false;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001360 }
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001361 /* delete a capsule either in case of success or failure */
1362 ret = efi_capsule_delete_file(files[i]);
1363 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001364 log_err("Deleting capsule %ls failed\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001365 files[i]);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001366 }
Sughosh Ganu86794052022-10-21 18:16:03 +05301367
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001368 efi_capsule_scan_done();
1369
Sughosh Ganu86794052022-10-21 18:16:03 +05301370 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1371 if (capsule_update == true && update_status == true) {
1372 ret = fwu_post_update_process(fw_accept_os);
1373 } else if (capsule_update == true && update_status == false) {
1374 log_err("All capsules were not updated. Not updating FWU metadata\n");
1375 }
1376 }
1377
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001378 for (i = 0; i < nfiles; i++)
1379 free(files[i]);
1380 free(files);
1381
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001382 /*
1383 * UEFI spec requires to reset system after complete processing capsule
1384 * update on the storage.
1385 */
Masami Hiramatsu7660cfe2022-03-21 22:37:56 +09001386 log_info("Reboot after firmware update.\n");
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001387 /* Cold reset is required for loading the new firmware. */
Masami Hiramatsu7660cfe2022-03-21 22:37:56 +09001388 sysreset_walk_halt(SYSRESET_COLD);
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001389 hang();
1390 /* not reach here */
1391
1392 return 0;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001393}
1394#endif /* CONFIG_EFI_CAPSULE_ON_DISK */