blob: ddf8153e09829ca1d9fe3b9474d90e7ec090e790 [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
AKASHI Takahiro1b7e4692023-07-27 09:38:00 +0900584 if (guidcmp(&capsule_data->capsule_guid,
585 &efi_guid_firmware_management_capsule_id)) {
586 log_err("Unsupported capsule type: %pUs\n",
587 &capsule_data->capsule_guid);
588 return EFI_UNSUPPORTED;
589 }
590
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900591 /* sanity check */
592 if (capsule_data->header_size < sizeof(*capsule) ||
593 capsule_data->header_size >= capsule_data->capsule_image_size)
594 return EFI_INVALID_PARAMETER;
595
596 capsule = (void *)capsule_data + capsule_data->header_size;
597 capsule_size = capsule_data->capsule_image_size
598 - capsule_data->header_size;
599
600 if (capsule->version != 0x00000001)
601 return EFI_UNSUPPORTED;
602
603 handles = NULL;
604 ret = EFI_CALL(efi_locate_handle_buffer(
605 BY_PROTOCOL,
606 &efi_guid_firmware_management_protocol,
607 NULL, &no_handles, (efi_handle_t **)&handles));
608 if (ret != EFI_SUCCESS)
609 return EFI_UNSUPPORTED;
610
611 /* Payload */
612 for (item = capsule->embedded_driver_count;
613 item < capsule->embedded_driver_count
614 + capsule->payload_item_count; item++) {
615 /* sanity check */
616 if ((capsule->item_offset_list[item] + sizeof(*image)
617 >= capsule_size)) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200618 log_err("Capsule does not have enough data\n");
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900619 ret = EFI_INVALID_PARAMETER;
620 goto out;
621 }
622
623 image = (void *)capsule + capsule->item_offset_list[item];
624
625 if (image->version != 0x00000003) {
626 ret = EFI_UNSUPPORTED;
627 goto out;
628 }
629
630 /* find a device for update firmware */
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900631 fmp = efi_fmp_find(&image->update_image_type_id,
Sughosh Ganu7cf06f02022-04-15 11:29:36 +0530632 image->update_image_index,
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900633 image->update_hardware_instance,
634 handles, no_handles);
635 if (!fmp) {
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100636 log_err("FMP driver not found for firmware type %pUs, hardware instance %lld\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900637 &image->update_image_type_id,
638 image->update_hardware_instance);
639 ret = EFI_UNSUPPORTED;
640 goto out;
641 }
642
643 /* do update */
AKASHI Takahiroc2cc60c2021-07-20 14:52:05 +0900644 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
645 !(image->image_capsule_support &
646 CAPSULE_SUPPORT_AUTHENTICATION)) {
647 /* no signature */
648 ret = EFI_SECURITY_VIOLATION;
649 goto out;
650 }
651
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900652 image_binary = (void *)image + sizeof(*image);
AKASHI Takahiroc2cc60c2021-07-20 14:52:05 +0900653 image_binary_size = image->update_image_size;
654 vendor_code = image_binary + image_binary_size;
655 if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
656 (image->image_capsule_support &
657 CAPSULE_SUPPORT_AUTHENTICATION)) {
658 ret = efi_remove_auth_hdr(&image_binary,
659 &image_binary_size);
660 if (ret != EFI_SUCCESS)
661 goto out;
662 }
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900663
664 abort_reason = NULL;
665 ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
666 image_binary,
AKASHI Takahiroc2cc60c2021-07-20 14:52:05 +0900667 image_binary_size,
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900668 vendor_code, NULL,
669 &abort_reason));
670 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200671 log_err("Firmware update failed: %ls\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900672 abort_reason);
673 efi_free_pool(abort_reason);
674 goto out;
675 }
Sughosh Ganu86794052022-10-21 18:16:03 +0530676
677 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
678 image_type_id = &image->update_image_type_id;
679 if (!fw_accept_os) {
680 /*
681 * The OS will not be accepting the firmware
682 * images. Set the accept bit of all the
683 * images contained in this capsule.
684 */
685 status = fwu_accept_image(image_type_id,
686 update_index);
687 } else {
688 status = fwu_clear_accept_image(image_type_id,
689 update_index);
690 }
691 ret = fwu_to_efi_error(status);
692 if (ret != EFI_SUCCESS) {
693 log_err("Unable to %s the accept bit for the image %pUs\n",
694 fw_accept_os ? "clear" : "set",
695 image_type_id);
696 goto out;
697 }
698
699 log_debug("%s the accepted bit for Image %pUs\n",
700 fw_accept_os ? "Cleared" : "Set",
701 image_type_id);
702 }
703
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900704 }
705
706out:
707 efi_free_pool(handles);
708
709 return ret;
710}
711#else
712static efi_status_t efi_capsule_update_firmware(
713 struct efi_capsule_header *capsule_data)
714{
715 return EFI_UNSUPPORTED;
716}
717#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
718
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900719/**
720 * efi_update_capsule() - process information from operating system
721 * @capsule_header_array: Array of virtual address pointers
722 * @capsule_count: Number of pointers in capsule_header_array
723 * @scatter_gather_list: Array of physical address pointers
724 *
725 * This function implements the UpdateCapsule() runtime service.
726 *
727 * See the Unified Extensible Firmware Interface (UEFI) specification for
728 * details.
729 *
730 * Return: status code
731 */
732efi_status_t EFIAPI efi_update_capsule(
733 struct efi_capsule_header **capsule_header_array,
734 efi_uintn_t capsule_count,
735 u64 scatter_gather_list)
736{
737 struct efi_capsule_header *capsule;
738 unsigned int i;
739 efi_status_t ret;
740
Simon Glassdf7d89a2021-02-07 14:27:02 -0700741 EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900742 scatter_gather_list);
743
744 if (!capsule_count) {
745 ret = EFI_INVALID_PARAMETER;
746 goto out;
747 }
748
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900749 ret = EFI_SUCCESS;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900750 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
751 i++, capsule = *(++capsule_header_array)) {
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900752 /* sanity check */
753 if (capsule->header_size < sizeof(*capsule) ||
754 capsule->capsule_image_size < sizeof(*capsule)) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200755 log_err("Capsule does not have enough data\n");
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900756 continue;
757 }
758
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100759 log_debug("Capsule[%d] (guid:%pUs)\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900760 i, &capsule->capsule_guid);
AKASHI Takahiro1b7e4692023-07-27 09:38:00 +0900761 ret = efi_capsule_update_firmware(capsule);
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900762 if (ret != EFI_SUCCESS)
763 goto out;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900764 }
Jose Marinho64a8aae2021-03-02 17:26:38 +0000765
766 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
767 /* Rebuild the ESRT to reflect any updated FW images. */
768 ret = efi_esrt_populate();
769 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200770 log_warning("ESRT update failed\n");
Jose Marinho64a8aae2021-03-02 17:26:38 +0000771 }
Jose Marinho3627cf42021-04-19 14:54:33 +0100772out:
Jose Marinho64a8aae2021-03-02 17:26:38 +0000773
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900774 return EFI_EXIT(ret);
775}
776
777/**
778 * efi_query_capsule_caps() - check if capsule is supported
779 * @capsule_header_array: Array of virtual pointers
780 * @capsule_count: Number of pointers in capsule_header_array
781 * @maximum_capsule_size: Maximum capsule size
782 * @reset_type: Type of reset needed for capsule update
783 *
784 * This function implements the QueryCapsuleCapabilities() runtime service.
785 *
786 * See the Unified Extensible Firmware Interface (UEFI) specification for
787 * details.
788 *
789 * Return: status code
790 */
791efi_status_t EFIAPI efi_query_capsule_caps(
792 struct efi_capsule_header **capsule_header_array,
793 efi_uintn_t capsule_count,
794 u64 *maximum_capsule_size,
795 u32 *reset_type)
796{
797 struct efi_capsule_header *capsule __attribute__((unused));
798 unsigned int i;
799 efi_status_t ret;
800
Simon Glassdf7d89a2021-02-07 14:27:02 -0700801 EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900802 maximum_capsule_size, reset_type);
803
804 if (!maximum_capsule_size) {
805 ret = EFI_INVALID_PARAMETER;
806 goto out;
807 }
808
809 *maximum_capsule_size = U64_MAX;
810 *reset_type = EFI_RESET_COLD;
811
812 ret = EFI_SUCCESS;
813 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
814 i++, capsule = *(++capsule_header_array)) {
815 /* TODO */
816 }
817out:
818 return EFI_EXIT(ret);
819}
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900820
Masami Hiramatsu39bdf112022-03-21 22:37:45 +0900821/**
822 * efi_load_capsule_drivers - initialize capsule drivers
823 *
824 * Generic FMP drivers backed by DFU
825 *
826 * Return: status code
827 */
828efi_status_t __weak efi_load_capsule_drivers(void)
829{
830 __maybe_unused efi_handle_t handle;
831 efi_status_t ret = EFI_SUCCESS;
832
833 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
834 handle = NULL;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +0300835 ret = efi_install_multiple_protocol_interfaces(&handle,
836 &efi_guid_firmware_management_protocol,
837 &efi_fmp_fit,
838 NULL);
Masami Hiramatsu39bdf112022-03-21 22:37:45 +0900839 }
840
841 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
842 handle = NULL;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +0300843 ret = efi_install_multiple_protocol_interfaces(&handle,
844 &efi_guid_firmware_management_protocol,
845 &efi_fmp_raw,
846 NULL);
Masami Hiramatsu39bdf112022-03-21 22:37:45 +0900847 }
848
849 return ret;
850}
851
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900852#ifdef CONFIG_EFI_CAPSULE_ON_DISK
853/**
854 * get_dp_device - retrieve a device path from boot variable
855 * @boot_var: Boot variable name
856 * @device_dp Device path
857 *
858 * Retrieve a device patch from boot variable, @boot_var.
859 *
860 * Return: status code
861 */
862static efi_status_t get_dp_device(u16 *boot_var,
863 struct efi_device_path **device_dp)
864{
865 void *buf = NULL;
866 efi_uintn_t size;
867 struct efi_load_option lo;
868 struct efi_device_path *file_dp;
869 efi_status_t ret;
870
871 size = 0;
872 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
873 NULL, &size, NULL, NULL);
874 if (ret == EFI_BUFFER_TOO_SMALL) {
875 buf = malloc(size);
876 if (!buf)
877 return EFI_OUT_OF_RESOURCES;
878 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
879 NULL, &size, buf, NULL);
880 }
881 if (ret != EFI_SUCCESS)
882 return ret;
883
884 efi_deserialize_load_option(&lo, buf, &size);
885
886 if (lo.attributes & LOAD_OPTION_ACTIVE) {
887 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
888 efi_free_pool(file_dp);
889
890 ret = EFI_SUCCESS;
891 } else {
892 ret = EFI_NOT_FOUND;
893 }
894
895 free(buf);
896
897 return ret;
898}
899
900/**
901 * device_is_present_and_system_part - check if a device exists
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900902 *
903 * Check if a device pointed to by the device path, @dp, exists and is
904 * located in UEFI system partition.
905 *
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100906 * @dp device path
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900907 * Return: true - yes, false - no
908 */
909static bool device_is_present_and_system_part(struct efi_device_path *dp)
910{
911 efi_handle_t handle;
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100912 struct efi_device_path *rem;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900913
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100914 /* Check device exists */
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +0100915 handle = efi_dp_find_obj(dp, NULL, NULL);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900916 if (!handle)
917 return false;
918
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100919 /* Check device is on system partition */
920 handle = efi_dp_find_obj(dp, &efi_system_partition_guid, &rem);
921 if (!handle)
922 return false;
923
924 return true;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900925}
926
927/**
928 * find_boot_device - identify the boot device
929 *
930 * Identify the boot device from boot-related variables as UEFI
931 * specification describes and put its handle into bootdev_root.
932 *
933 * Return: status code
934 */
935static efi_status_t find_boot_device(void)
936{
937 char boot_var[9];
938 u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
939 efi_uintn_t size;
940 int i, num;
941 struct efi_simple_file_system_protocol *volume;
942 struct efi_device_path *boot_dev = NULL;
943 efi_status_t ret;
944
945 /* find active boot device in BootNext */
946 bootnext = 0;
947 size = sizeof(bootnext);
Simon Glass156ccbc2022-01-23 12:55:12 -0700948 ret = efi_get_variable_int(u"BootNext",
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900949 (efi_guid_t *)&efi_global_variable_guid,
950 NULL, &size, &bootnext, NULL);
951 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
952 /* BootNext does exist here */
953 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900954 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900955 goto skip;
956 }
957 sprintf((char *)boot_var, "Boot%04X", bootnext);
958 p = boot_var16;
959 utf8_utf16_strcpy(&p, boot_var);
960
961 ret = get_dp_device(boot_var16, &boot_dev);
962 if (ret == EFI_SUCCESS) {
963 if (device_is_present_and_system_part(boot_dev)) {
Masami Hiramatsub0b14492021-07-12 18:05:17 +0900964 goto found;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900965 } else {
966 efi_free_pool(boot_dev);
967 boot_dev = NULL;
968 }
969 }
970 }
971
972skip:
973 /* find active boot device in BootOrder */
974 size = 0;
Simon Glass156ccbc2022-01-23 12:55:12 -0700975 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900976 NULL, &size, NULL, NULL);
977 if (ret == EFI_BUFFER_TOO_SMALL) {
978 boot_order = malloc(size);
979 if (!boot_order) {
980 ret = EFI_OUT_OF_RESOURCES;
981 goto out;
982 }
983
Simon Glass156ccbc2022-01-23 12:55:12 -0700984 ret = efi_get_variable_int(u"BootOrder",
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900985 &efi_global_variable_guid,
986 NULL, &size, boot_order, NULL);
987 }
988 if (ret != EFI_SUCCESS)
989 goto out;
990
991 /* check in higher order */
992 num = size / sizeof(u16);
993 for (i = 0; i < num; i++) {
994 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
995 p = boot_var16;
996 utf8_utf16_strcpy(&p, boot_var);
997 ret = get_dp_device(boot_var16, &boot_dev);
998 if (ret != EFI_SUCCESS)
999 continue;
1000
1001 if (device_is_present_and_system_part(boot_dev))
1002 break;
1003
1004 efi_free_pool(boot_dev);
1005 boot_dev = NULL;
1006 }
Masami Hiramatsub0b14492021-07-12 18:05:17 +09001007found:
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001008 if (boot_dev) {
Masami Hiramatsu484d7d82021-07-14 14:19:13 +09001009 log_debug("Boot device %pD\n", boot_dev);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001010
1011 volume = efi_fs_from_path(boot_dev);
1012 if (!volume)
1013 ret = EFI_DEVICE_ERROR;
1014 else
1015 ret = EFI_CALL(volume->open_volume(volume,
1016 &bootdev_root));
1017 efi_free_pool(boot_dev);
1018 } else {
1019 ret = EFI_NOT_FOUND;
1020 }
AKASHI Takahiro2c1a6842021-04-20 10:03:16 +09001021out:
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001022 free(boot_order);
1023
1024 return ret;
1025}
1026
1027/**
1028 * efi_capsule_scan_dir - traverse a capsule directory in boot device
1029 * @files: Array of file names
1030 * @num: Number of elements in @files
1031 *
1032 * Traverse a capsule directory in boot device.
1033 * Called by initialization code, and returns an array of capsule file
1034 * names in @files.
1035 *
1036 * Return: status code
1037 */
1038static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
1039{
1040 struct efi_file_handle *dirh;
1041 struct efi_file_info *dirent;
1042 efi_uintn_t dirent_size, tmp_size;
1043 unsigned int count;
1044 u16 **tmp_files;
1045 efi_status_t ret;
1046
1047 ret = find_boot_device();
1048 if (ret == EFI_NOT_FOUND) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001049 log_debug("Boot device is not set\n");
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001050 *num = 0;
1051 return EFI_SUCCESS;
1052 } else if (ret != EFI_SUCCESS) {
1053 return EFI_DEVICE_ERROR;
1054 }
1055
1056 /* count capsule files */
1057 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1058 EFI_CAPSULE_DIR,
1059 EFI_FILE_MODE_READ, 0));
1060 if (ret != EFI_SUCCESS) {
1061 *num = 0;
1062 return EFI_SUCCESS;
1063 }
1064
1065 dirent_size = 256;
1066 dirent = malloc(dirent_size);
1067 if (!dirent)
1068 return EFI_OUT_OF_RESOURCES;
1069
1070 count = 0;
1071 while (1) {
1072 tmp_size = dirent_size;
1073 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1074 if (ret == EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardte8287b02021-04-11 06:53:04 +02001075 struct efi_file_info *old_dirent = dirent;
1076
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001077 dirent = realloc(dirent, tmp_size);
1078 if (!dirent) {
Heinrich Schuchardte8287b02021-04-11 06:53:04 +02001079 dirent = old_dirent;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001080 ret = EFI_OUT_OF_RESOURCES;
1081 goto err;
1082 }
1083 dirent_size = tmp_size;
1084 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1085 }
1086 if (ret != EFI_SUCCESS)
1087 goto err;
1088 if (!tmp_size)
1089 break;
1090
Heinrich Schuchardt841f7a42021-02-09 17:45:33 +01001091 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001092 count++;
1093 }
1094
1095 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
1096 if (ret != EFI_SUCCESS)
1097 goto err;
1098
1099 /* make a list */
AKASHI Takahiro8f1844c2021-01-22 10:43:27 +09001100 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001101 if (!tmp_files) {
1102 ret = EFI_OUT_OF_RESOURCES;
1103 goto err;
1104 }
1105
1106 count = 0;
1107 while (1) {
1108 tmp_size = dirent_size;
1109 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1110 if (ret != EFI_SUCCESS)
1111 goto err;
1112 if (!tmp_size)
1113 break;
1114
1115 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
Simon Glass156ccbc2022-01-23 12:55:12 -07001116 u16_strcmp(dirent->file_name, u".") &&
1117 u16_strcmp(dirent->file_name, u".."))
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001118 tmp_files[count++] = u16_strdup(dirent->file_name);
1119 }
1120 /* ignore an error */
1121 EFI_CALL((*dirh->close)(dirh));
1122
Heinrich Schuchardt93cdb952022-12-29 14:44:05 +01001123 /*
1124 * Capsule files are applied in case insensitive alphabetic order
1125 *
1126 * TODO: special handling of rightmost period
1127 */
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001128 qsort(tmp_files, count, sizeof(*tmp_files),
Heinrich Schuchardt93cdb952022-12-29 14:44:05 +01001129 (int (*)(const void *, const void *))u16_strcasecmp);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001130 *files = tmp_files;
1131 *num = count;
1132 ret = EFI_SUCCESS;
1133err:
1134 free(dirent);
1135
1136 return ret;
1137}
1138
1139/**
1140 * efi_capsule_read_file - read in a capsule file
1141 * @filename: File name
1142 * @capsule: Pointer to buffer for capsule
1143 *
1144 * Read a capsule file and put its content in @capsule.
1145 *
1146 * Return: status code
1147 */
1148static efi_status_t efi_capsule_read_file(const u16 *filename,
1149 struct efi_capsule_header **capsule)
1150{
1151 struct efi_file_handle *dirh, *fh;
1152 struct efi_file_info *file_info = NULL;
1153 struct efi_capsule_header *buf = NULL;
1154 efi_uintn_t size;
1155 efi_status_t ret;
1156
1157 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1158 EFI_CAPSULE_DIR,
1159 EFI_FILE_MODE_READ, 0));
1160 if (ret != EFI_SUCCESS)
1161 return ret;
1162 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1163 EFI_FILE_MODE_READ, 0));
1164 /* ignore an error */
1165 EFI_CALL((*dirh->close)(dirh));
1166 if (ret != EFI_SUCCESS)
1167 return ret;
1168
1169 /* file size */
1170 size = 0;
1171 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1172 &size, file_info));
1173 if (ret == EFI_BUFFER_TOO_SMALL) {
1174 file_info = malloc(size);
1175 if (!file_info) {
1176 ret = EFI_OUT_OF_RESOURCES;
1177 goto err;
1178 }
1179 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1180 &size, file_info));
1181 }
1182 if (ret != EFI_SUCCESS)
1183 goto err;
1184 size = file_info->file_size;
1185 free(file_info);
1186 buf = malloc(size);
1187 if (!buf) {
1188 ret = EFI_OUT_OF_RESOURCES;
1189 goto err;
1190 }
1191
1192 /* fetch data */
1193 ret = EFI_CALL((*fh->read)(fh, &size, buf));
1194 if (ret == EFI_SUCCESS) {
1195 if (size >= buf->capsule_image_size) {
1196 *capsule = buf;
1197 } else {
1198 free(buf);
1199 ret = EFI_INVALID_PARAMETER;
1200 }
1201 } else {
1202 free(buf);
1203 }
1204err:
1205 EFI_CALL((*fh->close)(fh));
1206
1207 return ret;
1208}
1209
1210/**
1211 * efi_capsule_delete_file - delete a capsule file
1212 * @filename: File name
1213 *
1214 * Delete a capsule file from capsule directory.
1215 *
1216 * Return: status code
1217 */
1218static efi_status_t efi_capsule_delete_file(const u16 *filename)
1219{
1220 struct efi_file_handle *dirh, *fh;
1221 efi_status_t ret;
1222
1223 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1224 EFI_CAPSULE_DIR,
1225 EFI_FILE_MODE_READ, 0));
1226 if (ret != EFI_SUCCESS)
1227 return ret;
1228 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1229 EFI_FILE_MODE_READ, 0));
1230 /* ignore an error */
1231 EFI_CALL((*dirh->close)(dirh));
1232
Heinrich Schuchardte27b0ff2021-06-02 19:28:22 +02001233 if (ret == EFI_SUCCESS)
1234 ret = EFI_CALL((*fh->delete)(fh));
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001235
1236 return ret;
1237}
1238
1239/**
1240 * efi_capsule_scan_done - reset a scan help function
1241 *
1242 * Reset a scan help function
1243 */
1244static void efi_capsule_scan_done(void)
1245{
1246 EFI_CALL((*bootdev_root->close)(bootdev_root));
1247 bootdev_root = NULL;
1248}
1249
1250/**
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001251 * check_run_capsules() - check whether capsule update should run
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001252 *
1253 * The spec says OsIndications must be set in order to run the capsule update
1254 * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to
1255 * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001256 *
1257 * Return: EFI_SUCCESS if update to run, EFI_NOT_FOUND otherwise
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001258 */
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001259static efi_status_t check_run_capsules(void)
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001260{
Sughosh Ganu119fafd2022-06-01 23:30:39 +05301261 u64 os_indications = 0x0;
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001262 efi_uintn_t size;
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001263 efi_status_t r;
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001264
1265 size = sizeof(os_indications);
Simon Glass156ccbc2022-01-23 12:55:12 -07001266 r = efi_get_variable_int(u"OsIndications", &efi_global_variable_guid,
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001267 NULL, &size, &os_indications, NULL);
Sughosh Ganu119fafd2022-06-01 23:30:39 +05301268 if (!IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS) &&
1269 (r != EFI_SUCCESS || size != sizeof(os_indications)))
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001270 return EFI_NOT_FOUND;
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001271
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001272 if (os_indications &
1273 EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
1274 os_indications &=
1275 ~EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
Simon Glass156ccbc2022-01-23 12:55:12 -07001276 r = efi_set_variable_int(u"OsIndications",
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001277 &efi_global_variable_guid,
1278 EFI_VARIABLE_NON_VOLATILE |
1279 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1280 EFI_VARIABLE_RUNTIME_ACCESS,
1281 sizeof(os_indications),
1282 &os_indications, false);
1283 if (r != EFI_SUCCESS)
1284 log_err("Setting %ls failed\n", L"OsIndications");
1285 return EFI_SUCCESS;
1286 } else if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS)) {
1287 return EFI_SUCCESS;
Sughosh Ganu119fafd2022-06-01 23:30:39 +05301288 } else {
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001289 return EFI_NOT_FOUND;
1290 }
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001291}
1292
1293/**
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001294 * efi_launch_capsule - launch capsules
1295 *
1296 * Launch all the capsules in system at boot time.
1297 * Called by efi init code
1298 *
1299 * Return: status codde
1300 */
1301efi_status_t efi_launch_capsules(void)
1302{
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001303 struct efi_capsule_header *capsule = NULL;
1304 u16 **files;
Etienne Carriere44293932023-02-16 18:21:41 +01001305 unsigned int nfiles, index, index_max, i;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001306 efi_status_t ret;
Sughosh Ganu86794052022-10-21 18:16:03 +05301307 bool capsule_update = true;
1308 bool update_status = true;
1309 bool fw_accept_os = false;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001310
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001311 if (check_run_capsules() != EFI_SUCCESS)
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001312 return EFI_SUCCESS;
1313
Etienne Carriere44293932023-02-16 18:21:41 +01001314 index_max = get_max_capsule();
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001315 index = get_last_capsule();
1316
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001317 /*
1318 * Find capsules on disk.
1319 * All the capsules are collected at the beginning because
1320 * capsule files will be removed instantly.
1321 */
1322 nfiles = 0;
1323 files = NULL;
1324 ret = efi_capsule_scan_dir(&files, &nfiles);
1325 if (ret != EFI_SUCCESS)
1326 return ret;
1327 if (!nfiles)
1328 return EFI_SUCCESS;
1329
1330 /* Launch capsules */
1331 for (i = 0, ++index; i < nfiles; i++, index++) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001332 log_debug("Applying %ls\n", files[i]);
Etienne Carriere44293932023-02-16 18:21:41 +01001333 if (index > index_max)
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001334 index = 0;
1335 ret = efi_capsule_read_file(files[i], &capsule);
1336 if (ret == EFI_SUCCESS) {
Masami Hiramatsua6aafce2022-02-16 15:15:42 +09001337 ret = efi_capsule_update_firmware(capsule);
Sughosh Ganu86794052022-10-21 18:16:03 +05301338 if (ret != EFI_SUCCESS) {
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001339 log_err("Applying capsule %ls failed.\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001340 files[i]);
Sughosh Ganu86794052022-10-21 18:16:03 +05301341 update_status = false;
1342 } else {
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001343 log_info("Applying capsule %ls succeeded.\n",
1344 files[i]);
Sughosh Ganu86794052022-10-21 18:16:03 +05301345 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1346 fwu_post_update_checks(capsule,
1347 &fw_accept_os,
1348 &capsule_update);
1349 }
1350 }
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001351
Masami Hiramatsu5d49b322021-11-12 22:05:15 +09001352 /* create CapsuleXXXX */
1353 set_capsule_result(index, capsule, ret);
1354
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001355 free(capsule);
1356 } else {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001357 log_err("Reading capsule %ls failed\n", files[i]);
Sughosh Ganu86794052022-10-21 18:16:03 +05301358 update_status = false;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001359 }
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001360 /* delete a capsule either in case of success or failure */
1361 ret = efi_capsule_delete_file(files[i]);
1362 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001363 log_err("Deleting capsule %ls failed\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001364 files[i]);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001365 }
Sughosh Ganu86794052022-10-21 18:16:03 +05301366
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001367 efi_capsule_scan_done();
1368
Sughosh Ganu86794052022-10-21 18:16:03 +05301369 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1370 if (capsule_update == true && update_status == true) {
1371 ret = fwu_post_update_process(fw_accept_os);
1372 } else if (capsule_update == true && update_status == false) {
1373 log_err("All capsules were not updated. Not updating FWU metadata\n");
1374 }
1375 }
1376
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001377 for (i = 0; i < nfiles; i++)
1378 free(files[i]);
1379 free(files);
1380
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001381 /*
1382 * UEFI spec requires to reset system after complete processing capsule
1383 * update on the storage.
1384 */
Masami Hiramatsu7660cfe2022-03-21 22:37:56 +09001385 log_info("Reboot after firmware update.\n");
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001386 /* Cold reset is required for loading the new firmware. */
Masami Hiramatsu7660cfe2022-03-21 22:37:56 +09001387 sysreset_walk_halt(SYSRESET_COLD);
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001388 hang();
1389 /* not reach here */
1390
1391 return 0;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001392}
1393#endif /* CONFIG_EFI_CAPSULE_ON_DISK */