blob: af8a2ee940ce84aef172194d4a12bf511c65e9c2 [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);
Dan Carpenterdae23692023-07-27 10:16:20 +0300371 if (!capsule_sig) {
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530372 debug("Parsing variable's pkcs7 header failed\n");
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530373 goto out;
374 }
375
Simon Glass47a25e82021-08-02 08:44:31 -0600376 ret = efi_get_public_key_data(&fdt_pkey, &pkey_len);
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530377 if (ret < 0)
378 goto out;
379
380 pkey = malloc(pkey_len);
381 if (!pkey)
382 goto out;
383
Simon Glass47a25e82021-08-02 08:44:31 -0600384 memcpy(pkey, fdt_pkey, pkey_len);
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530385 truststore = efi_build_signature_store(pkey, pkey_len);
386 if (!truststore)
387 goto out;
388
389 /* verify signature */
390 if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) {
391 debug("Verified\n");
392 } else {
393 debug("Verifying variable's signature failed\n");
394 goto out;
395 }
396
397 status = EFI_SUCCESS;
398
399out:
400 efi_sigstore_free(truststore);
401 pkcs7_free_message(capsule_sig);
402 free(regs);
403
404 return status;
405}
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530406#endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */
407
Sughosh Ganu86794052022-10-21 18:16:03 +0530408static __maybe_unused bool fwu_empty_capsule(struct efi_capsule_header *capsule)
409{
410 return !guidcmp(&capsule->capsule_guid,
411 &fwu_guid_os_request_fw_revert) ||
412 !guidcmp(&capsule->capsule_guid,
413 &fwu_guid_os_request_fw_accept);
414}
415
416static __maybe_unused efi_status_t fwu_to_efi_error(int err)
417{
418 efi_status_t ret;
419
420 switch(err) {
421 case 0:
422 ret = EFI_SUCCESS;
423 break;
424 case -ERANGE:
425 case -EIO:
426 ret = EFI_DEVICE_ERROR;
427 break;
428 case -EINVAL:
429 ret = EFI_INVALID_PARAMETER;
430 break;
431 case -ENODEV:
432 ret = EFI_NOT_FOUND;
433 break;
434 default:
435 ret = EFI_OUT_OF_RESOURCES;
436 }
437
438 return ret;
439}
440
441static __maybe_unused efi_status_t fwu_empty_capsule_process(
442 struct efi_capsule_header *capsule)
443{
444 int status;
445 u32 active_idx;
446 efi_guid_t *image_guid;
447 efi_status_t ret = EFI_INVALID_PARAMETER;
448
449 if (!guidcmp(&capsule->capsule_guid,
450 &fwu_guid_os_request_fw_revert)) {
451 /*
452 * One of the previously updated image has
453 * failed the OS acceptance test. OS has
454 * requested to revert back to the earlier
455 * boot index
456 */
457 status = fwu_revert_boot_index();
458 ret = fwu_to_efi_error(status);
459 if (ret == EFI_SUCCESS)
460 log_debug("Reverted the FWU active_index. Recommend rebooting the system\n");
461 else
462 log_err("Failed to revert the FWU boot index\n");
463 } else if (!guidcmp(&capsule->capsule_guid,
464 &fwu_guid_os_request_fw_accept)) {
465 /*
466 * Image accepted by the OS. Set the acceptance
467 * status for the image.
468 */
469 image_guid = (void *)(char *)capsule +
470 capsule->header_size;
471
472 status = fwu_get_active_index(&active_idx);
473 ret = fwu_to_efi_error(status);
474 if (ret != EFI_SUCCESS) {
475 log_err("Unable to get the active_index from the FWU metadata\n");
476 return ret;
477 }
478
479 status = fwu_accept_image(image_guid, active_idx);
480 ret = fwu_to_efi_error(status);
481 if (ret != EFI_SUCCESS)
482 log_err("Unable to set the Accept bit for the image %pUs\n",
483 image_guid);
484 }
485
486 return ret;
487}
488
489static __maybe_unused void fwu_post_update_checks(
490 struct efi_capsule_header *capsule,
491 bool *fw_accept_os, bool *capsule_update)
492{
493 if (fwu_empty_capsule(capsule))
494 *capsule_update = false;
495 else
496 if (!*fw_accept_os)
497 *fw_accept_os =
498 capsule->flags & FW_ACCEPT_OS ? true : false;
499}
500
501static __maybe_unused efi_status_t fwu_post_update_process(bool fw_accept_os)
502{
503 int status;
504 uint update_index;
505 efi_status_t ret;
506
507 status = fwu_plat_get_update_index(&update_index);
508 if (status < 0) {
509 log_err("Failed to get the FWU update_index value\n");
510 return EFI_DEVICE_ERROR;
511 }
512
513 /*
514 * All the capsules have been updated successfully,
515 * update the FWU metadata.
516 */
517 log_debug("Update Complete. Now updating active_index to %u\n",
518 update_index);
519 status = fwu_set_active_index(update_index);
520 ret = fwu_to_efi_error(status);
521 if (ret != EFI_SUCCESS) {
522 log_err("Failed to update FWU metadata index values\n");
523 } else {
524 log_debug("Successfully updated the active_index\n");
525 if (fw_accept_os) {
526 status = fwu_trial_state_ctr_start();
527 if (status < 0)
528 ret = EFI_DEVICE_ERROR;
529 }
530 }
531
532 return ret;
533}
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530534
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900535/**
536 * efi_capsule_update_firmware - update firmware from capsule
537 * @capsule_data: Capsule
538 *
539 * Update firmware, using a capsule, @capsule_data. Loading any FMP
540 * drivers embedded in a capsule is not supported.
541 *
542 * Return: status code
543 */
544static efi_status_t efi_capsule_update_firmware(
545 struct efi_capsule_header *capsule_data)
546{
547 struct efi_firmware_management_capsule_header *capsule;
548 struct efi_firmware_management_capsule_image_header *image;
AKASHI Takahiroc2cc60c2021-07-20 14:52:05 +0900549 size_t capsule_size, image_binary_size;
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900550 void *image_binary, *vendor_code;
551 efi_handle_t *handles;
552 efi_uintn_t no_handles;
553 int item;
554 struct efi_firmware_management_protocol *fmp;
555 u16 *abort_reason;
Sughosh Ganu86794052022-10-21 18:16:03 +0530556 efi_guid_t *image_type_id;
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900557 efi_status_t ret = EFI_SUCCESS;
Sughosh Ganu86794052022-10-21 18:16:03 +0530558 int status;
559 uint update_index;
560 bool fw_accept_os;
561
562 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
563 if (fwu_empty_capsule_checks_pass() &&
564 fwu_empty_capsule(capsule_data))
565 return fwu_empty_capsule_process(capsule_data);
566
567 if (!fwu_update_checks_pass()) {
568 log_err("FWU checks failed. Cannot start update\n");
569 return EFI_INVALID_PARAMETER;
570 }
571
572
573 /* Obtain the update_index from the platform */
574 status = fwu_plat_get_update_index(&update_index);
575 if (status < 0) {
576 log_err("Failed to get the FWU update_index value\n");
577 return EFI_DEVICE_ERROR;
578 }
579
580 fw_accept_os = capsule_data->flags & FW_ACCEPT_OS ? 0x1 : 0x0;
581 }
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900582
AKASHI Takahiro1b7e4692023-07-27 09:38:00 +0900583 if (guidcmp(&capsule_data->capsule_guid,
584 &efi_guid_firmware_management_capsule_id)) {
585 log_err("Unsupported capsule type: %pUs\n",
586 &capsule_data->capsule_guid);
587 return EFI_UNSUPPORTED;
588 }
589
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900590 /* sanity check */
591 if (capsule_data->header_size < sizeof(*capsule) ||
592 capsule_data->header_size >= capsule_data->capsule_image_size)
593 return EFI_INVALID_PARAMETER;
594
595 capsule = (void *)capsule_data + capsule_data->header_size;
596 capsule_size = capsule_data->capsule_image_size
597 - capsule_data->header_size;
598
599 if (capsule->version != 0x00000001)
600 return EFI_UNSUPPORTED;
601
602 handles = NULL;
603 ret = EFI_CALL(efi_locate_handle_buffer(
604 BY_PROTOCOL,
605 &efi_guid_firmware_management_protocol,
606 NULL, &no_handles, (efi_handle_t **)&handles));
607 if (ret != EFI_SUCCESS)
608 return EFI_UNSUPPORTED;
609
610 /* Payload */
611 for (item = capsule->embedded_driver_count;
612 item < capsule->embedded_driver_count
613 + capsule->payload_item_count; item++) {
614 /* sanity check */
615 if ((capsule->item_offset_list[item] + sizeof(*image)
616 >= capsule_size)) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200617 log_err("Capsule does not have enough data\n");
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900618 ret = EFI_INVALID_PARAMETER;
619 goto out;
620 }
621
622 image = (void *)capsule + capsule->item_offset_list[item];
623
624 if (image->version != 0x00000003) {
625 ret = EFI_UNSUPPORTED;
626 goto out;
627 }
628
629 /* find a device for update firmware */
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900630 fmp = efi_fmp_find(&image->update_image_type_id,
Sughosh Ganu7cf06f02022-04-15 11:29:36 +0530631 image->update_image_index,
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900632 image->update_hardware_instance,
633 handles, no_handles);
634 if (!fmp) {
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100635 log_err("FMP driver not found for firmware type %pUs, hardware instance %lld\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900636 &image->update_image_type_id,
637 image->update_hardware_instance);
638 ret = EFI_UNSUPPORTED;
639 goto out;
640 }
641
642 /* do update */
AKASHI Takahiroc2cc60c2021-07-20 14:52:05 +0900643 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
644 !(image->image_capsule_support &
645 CAPSULE_SUPPORT_AUTHENTICATION)) {
646 /* no signature */
647 ret = EFI_SECURITY_VIOLATION;
648 goto out;
649 }
650
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900651 image_binary = (void *)image + sizeof(*image);
AKASHI Takahiroc2cc60c2021-07-20 14:52:05 +0900652 image_binary_size = image->update_image_size;
653 vendor_code = image_binary + image_binary_size;
654 if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
655 (image->image_capsule_support &
656 CAPSULE_SUPPORT_AUTHENTICATION)) {
657 ret = efi_remove_auth_hdr(&image_binary,
658 &image_binary_size);
659 if (ret != EFI_SUCCESS)
660 goto out;
661 }
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900662
663 abort_reason = NULL;
664 ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
665 image_binary,
AKASHI Takahiroc2cc60c2021-07-20 14:52:05 +0900666 image_binary_size,
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900667 vendor_code, NULL,
668 &abort_reason));
669 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200670 log_err("Firmware update failed: %ls\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900671 abort_reason);
672 efi_free_pool(abort_reason);
673 goto out;
674 }
Sughosh Ganu86794052022-10-21 18:16:03 +0530675
676 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
677 image_type_id = &image->update_image_type_id;
678 if (!fw_accept_os) {
679 /*
680 * The OS will not be accepting the firmware
681 * images. Set the accept bit of all the
682 * images contained in this capsule.
683 */
684 status = fwu_accept_image(image_type_id,
685 update_index);
686 } else {
687 status = fwu_clear_accept_image(image_type_id,
688 update_index);
689 }
690 ret = fwu_to_efi_error(status);
691 if (ret != EFI_SUCCESS) {
692 log_err("Unable to %s the accept bit for the image %pUs\n",
693 fw_accept_os ? "clear" : "set",
694 image_type_id);
695 goto out;
696 }
697
698 log_debug("%s the accepted bit for Image %pUs\n",
699 fw_accept_os ? "Cleared" : "Set",
700 image_type_id);
701 }
702
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900703 }
704
705out:
706 efi_free_pool(handles);
707
708 return ret;
709}
710#else
711static efi_status_t efi_capsule_update_firmware(
712 struct efi_capsule_header *capsule_data)
713{
714 return EFI_UNSUPPORTED;
715}
716#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
717
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900718/**
719 * efi_update_capsule() - process information from operating system
720 * @capsule_header_array: Array of virtual address pointers
721 * @capsule_count: Number of pointers in capsule_header_array
722 * @scatter_gather_list: Array of physical address pointers
723 *
724 * This function implements the UpdateCapsule() runtime service.
725 *
726 * See the Unified Extensible Firmware Interface (UEFI) specification for
727 * details.
728 *
729 * Return: status code
730 */
731efi_status_t EFIAPI efi_update_capsule(
732 struct efi_capsule_header **capsule_header_array,
733 efi_uintn_t capsule_count,
734 u64 scatter_gather_list)
735{
736 struct efi_capsule_header *capsule;
737 unsigned int i;
738 efi_status_t ret;
739
Simon Glassdf7d89a2021-02-07 14:27:02 -0700740 EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900741 scatter_gather_list);
742
743 if (!capsule_count) {
744 ret = EFI_INVALID_PARAMETER;
745 goto out;
746 }
747
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900748 ret = EFI_SUCCESS;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900749 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
750 i++, capsule = *(++capsule_header_array)) {
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900751 /* sanity check */
752 if (capsule->header_size < sizeof(*capsule) ||
753 capsule->capsule_image_size < sizeof(*capsule)) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200754 log_err("Capsule does not have enough data\n");
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900755 continue;
756 }
757
Heinrich Schuchardtce00a742022-01-16 14:15:31 +0100758 log_debug("Capsule[%d] (guid:%pUs)\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900759 i, &capsule->capsule_guid);
AKASHI Takahiro1b7e4692023-07-27 09:38:00 +0900760 ret = efi_capsule_update_firmware(capsule);
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900761 if (ret != EFI_SUCCESS)
762 goto out;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900763 }
Jose Marinho64a8aae2021-03-02 17:26:38 +0000764
765 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
766 /* Rebuild the ESRT to reflect any updated FW images. */
767 ret = efi_esrt_populate();
768 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200769 log_warning("ESRT update failed\n");
Jose Marinho64a8aae2021-03-02 17:26:38 +0000770 }
Jose Marinho3627cf42021-04-19 14:54:33 +0100771out:
Jose Marinho64a8aae2021-03-02 17:26:38 +0000772
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900773 return EFI_EXIT(ret);
774}
775
776/**
777 * efi_query_capsule_caps() - check if capsule is supported
778 * @capsule_header_array: Array of virtual pointers
779 * @capsule_count: Number of pointers in capsule_header_array
780 * @maximum_capsule_size: Maximum capsule size
781 * @reset_type: Type of reset needed for capsule update
782 *
783 * This function implements the QueryCapsuleCapabilities() runtime service.
784 *
785 * See the Unified Extensible Firmware Interface (UEFI) specification for
786 * details.
787 *
788 * Return: status code
789 */
790efi_status_t EFIAPI efi_query_capsule_caps(
791 struct efi_capsule_header **capsule_header_array,
792 efi_uintn_t capsule_count,
793 u64 *maximum_capsule_size,
794 u32 *reset_type)
795{
796 struct efi_capsule_header *capsule __attribute__((unused));
797 unsigned int i;
798 efi_status_t ret;
799
Simon Glassdf7d89a2021-02-07 14:27:02 -0700800 EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900801 maximum_capsule_size, reset_type);
802
803 if (!maximum_capsule_size) {
804 ret = EFI_INVALID_PARAMETER;
805 goto out;
806 }
807
808 *maximum_capsule_size = U64_MAX;
809 *reset_type = EFI_RESET_COLD;
810
811 ret = EFI_SUCCESS;
812 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
813 i++, capsule = *(++capsule_header_array)) {
814 /* TODO */
815 }
816out:
817 return EFI_EXIT(ret);
818}
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900819
Masami Hiramatsu39bdf112022-03-21 22:37:45 +0900820/**
821 * efi_load_capsule_drivers - initialize capsule drivers
822 *
823 * Generic FMP drivers backed by DFU
824 *
825 * Return: status code
826 */
827efi_status_t __weak efi_load_capsule_drivers(void)
828{
829 __maybe_unused efi_handle_t handle;
830 efi_status_t ret = EFI_SUCCESS;
831
832 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
833 handle = NULL;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +0300834 ret = efi_install_multiple_protocol_interfaces(&handle,
835 &efi_guid_firmware_management_protocol,
836 &efi_fmp_fit,
837 NULL);
Masami Hiramatsu39bdf112022-03-21 22:37:45 +0900838 }
839
840 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
841 handle = NULL;
Ilias Apalodimas05c4c9e2022-10-06 16:08:46 +0300842 ret = efi_install_multiple_protocol_interfaces(&handle,
843 &efi_guid_firmware_management_protocol,
844 &efi_fmp_raw,
845 NULL);
Masami Hiramatsu39bdf112022-03-21 22:37:45 +0900846 }
847
848 return ret;
849}
850
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900851#ifdef CONFIG_EFI_CAPSULE_ON_DISK
852/**
853 * get_dp_device - retrieve a device path from boot variable
854 * @boot_var: Boot variable name
855 * @device_dp Device path
856 *
857 * Retrieve a device patch from boot variable, @boot_var.
858 *
859 * Return: status code
860 */
861static efi_status_t get_dp_device(u16 *boot_var,
862 struct efi_device_path **device_dp)
863{
864 void *buf = NULL;
865 efi_uintn_t size;
866 struct efi_load_option lo;
867 struct efi_device_path *file_dp;
868 efi_status_t ret;
869
870 size = 0;
871 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
872 NULL, &size, NULL, NULL);
873 if (ret == EFI_BUFFER_TOO_SMALL) {
874 buf = malloc(size);
875 if (!buf)
876 return EFI_OUT_OF_RESOURCES;
877 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
878 NULL, &size, buf, NULL);
879 }
880 if (ret != EFI_SUCCESS)
881 return ret;
882
883 efi_deserialize_load_option(&lo, buf, &size);
884
885 if (lo.attributes & LOAD_OPTION_ACTIVE) {
886 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
887 efi_free_pool(file_dp);
888
889 ret = EFI_SUCCESS;
890 } else {
891 ret = EFI_NOT_FOUND;
892 }
893
894 free(buf);
895
896 return ret;
897}
898
899/**
900 * device_is_present_and_system_part - check if a device exists
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900901 *
902 * Check if a device pointed to by the device path, @dp, exists and is
903 * located in UEFI system partition.
904 *
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100905 * @dp device path
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900906 * Return: true - yes, false - no
907 */
908static bool device_is_present_and_system_part(struct efi_device_path *dp)
909{
910 efi_handle_t handle;
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100911 struct efi_device_path *rem;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900912
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100913 /* Check device exists */
Heinrich Schuchardte46ef1d2022-03-19 06:35:43 +0100914 handle = efi_dp_find_obj(dp, NULL, NULL);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900915 if (!handle)
916 return false;
917
Heinrich Schuchardtb78631d2022-03-05 00:36:50 +0100918 /* Check device is on system partition */
919 handle = efi_dp_find_obj(dp, &efi_system_partition_guid, &rem);
920 if (!handle)
921 return false;
922
923 return true;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900924}
925
926/**
927 * find_boot_device - identify the boot device
928 *
929 * Identify the boot device from boot-related variables as UEFI
930 * specification describes and put its handle into bootdev_root.
931 *
932 * Return: status code
933 */
934static efi_status_t find_boot_device(void)
935{
936 char boot_var[9];
937 u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
938 efi_uintn_t size;
939 int i, num;
940 struct efi_simple_file_system_protocol *volume;
941 struct efi_device_path *boot_dev = NULL;
942 efi_status_t ret;
943
944 /* find active boot device in BootNext */
945 bootnext = 0;
946 size = sizeof(bootnext);
Simon Glass156ccbc2022-01-23 12:55:12 -0700947 ret = efi_get_variable_int(u"BootNext",
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900948 (efi_guid_t *)&efi_global_variable_guid,
949 NULL, &size, &bootnext, NULL);
950 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
951 /* BootNext does exist here */
952 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900953 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900954 goto skip;
955 }
956 sprintf((char *)boot_var, "Boot%04X", bootnext);
957 p = boot_var16;
958 utf8_utf16_strcpy(&p, boot_var);
959
960 ret = get_dp_device(boot_var16, &boot_dev);
961 if (ret == EFI_SUCCESS) {
962 if (device_is_present_and_system_part(boot_dev)) {
Masami Hiramatsub0b14492021-07-12 18:05:17 +0900963 goto found;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900964 } else {
965 efi_free_pool(boot_dev);
966 boot_dev = NULL;
967 }
968 }
969 }
970
971skip:
972 /* find active boot device in BootOrder */
973 size = 0;
Simon Glass156ccbc2022-01-23 12:55:12 -0700974 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900975 NULL, &size, NULL, NULL);
976 if (ret == EFI_BUFFER_TOO_SMALL) {
977 boot_order = malloc(size);
978 if (!boot_order) {
979 ret = EFI_OUT_OF_RESOURCES;
980 goto out;
981 }
982
Simon Glass156ccbc2022-01-23 12:55:12 -0700983 ret = efi_get_variable_int(u"BootOrder",
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900984 &efi_global_variable_guid,
985 NULL, &size, boot_order, NULL);
986 }
987 if (ret != EFI_SUCCESS)
988 goto out;
989
990 /* check in higher order */
991 num = size / sizeof(u16);
992 for (i = 0; i < num; i++) {
993 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
994 p = boot_var16;
995 utf8_utf16_strcpy(&p, boot_var);
996 ret = get_dp_device(boot_var16, &boot_dev);
997 if (ret != EFI_SUCCESS)
998 continue;
999
1000 if (device_is_present_and_system_part(boot_dev))
1001 break;
1002
1003 efi_free_pool(boot_dev);
1004 boot_dev = NULL;
1005 }
Masami Hiramatsub0b14492021-07-12 18:05:17 +09001006found:
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001007 if (boot_dev) {
Masami Hiramatsu484d7d82021-07-14 14:19:13 +09001008 log_debug("Boot device %pD\n", boot_dev);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001009
1010 volume = efi_fs_from_path(boot_dev);
1011 if (!volume)
1012 ret = EFI_DEVICE_ERROR;
1013 else
1014 ret = EFI_CALL(volume->open_volume(volume,
1015 &bootdev_root));
1016 efi_free_pool(boot_dev);
1017 } else {
1018 ret = EFI_NOT_FOUND;
1019 }
AKASHI Takahiro2c1a6842021-04-20 10:03:16 +09001020out:
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001021 free(boot_order);
1022
1023 return ret;
1024}
1025
1026/**
1027 * efi_capsule_scan_dir - traverse a capsule directory in boot device
1028 * @files: Array of file names
1029 * @num: Number of elements in @files
1030 *
1031 * Traverse a capsule directory in boot device.
1032 * Called by initialization code, and returns an array of capsule file
1033 * names in @files.
1034 *
1035 * Return: status code
1036 */
1037static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
1038{
1039 struct efi_file_handle *dirh;
1040 struct efi_file_info *dirent;
1041 efi_uintn_t dirent_size, tmp_size;
1042 unsigned int count;
1043 u16 **tmp_files;
1044 efi_status_t ret;
1045
1046 ret = find_boot_device();
1047 if (ret == EFI_NOT_FOUND) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001048 log_debug("Boot device is not set\n");
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001049 *num = 0;
1050 return EFI_SUCCESS;
1051 } else if (ret != EFI_SUCCESS) {
1052 return EFI_DEVICE_ERROR;
1053 }
1054
1055 /* count capsule files */
1056 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1057 EFI_CAPSULE_DIR,
1058 EFI_FILE_MODE_READ, 0));
1059 if (ret != EFI_SUCCESS) {
1060 *num = 0;
1061 return EFI_SUCCESS;
1062 }
1063
1064 dirent_size = 256;
1065 dirent = malloc(dirent_size);
1066 if (!dirent)
1067 return EFI_OUT_OF_RESOURCES;
1068
1069 count = 0;
1070 while (1) {
1071 tmp_size = dirent_size;
1072 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1073 if (ret == EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardte8287b02021-04-11 06:53:04 +02001074 struct efi_file_info *old_dirent = dirent;
1075
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001076 dirent = realloc(dirent, tmp_size);
1077 if (!dirent) {
Heinrich Schuchardte8287b02021-04-11 06:53:04 +02001078 dirent = old_dirent;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001079 ret = EFI_OUT_OF_RESOURCES;
1080 goto err;
1081 }
1082 dirent_size = tmp_size;
1083 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1084 }
1085 if (ret != EFI_SUCCESS)
1086 goto err;
1087 if (!tmp_size)
1088 break;
1089
Heinrich Schuchardt841f7a42021-02-09 17:45:33 +01001090 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001091 count++;
1092 }
1093
1094 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
1095 if (ret != EFI_SUCCESS)
1096 goto err;
1097
1098 /* make a list */
AKASHI Takahiro8f1844c2021-01-22 10:43:27 +09001099 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001100 if (!tmp_files) {
1101 ret = EFI_OUT_OF_RESOURCES;
1102 goto err;
1103 }
1104
1105 count = 0;
1106 while (1) {
1107 tmp_size = dirent_size;
1108 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1109 if (ret != EFI_SUCCESS)
1110 goto err;
1111 if (!tmp_size)
1112 break;
1113
1114 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
Simon Glass156ccbc2022-01-23 12:55:12 -07001115 u16_strcmp(dirent->file_name, u".") &&
1116 u16_strcmp(dirent->file_name, u".."))
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001117 tmp_files[count++] = u16_strdup(dirent->file_name);
1118 }
1119 /* ignore an error */
1120 EFI_CALL((*dirh->close)(dirh));
1121
Heinrich Schuchardt93cdb952022-12-29 14:44:05 +01001122 /*
1123 * Capsule files are applied in case insensitive alphabetic order
1124 *
1125 * TODO: special handling of rightmost period
1126 */
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001127 qsort(tmp_files, count, sizeof(*tmp_files),
Heinrich Schuchardt93cdb952022-12-29 14:44:05 +01001128 (int (*)(const void *, const void *))u16_strcasecmp);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001129 *files = tmp_files;
1130 *num = count;
1131 ret = EFI_SUCCESS;
1132err:
1133 free(dirent);
1134
1135 return ret;
1136}
1137
1138/**
1139 * efi_capsule_read_file - read in a capsule file
1140 * @filename: File name
1141 * @capsule: Pointer to buffer for capsule
1142 *
1143 * Read a capsule file and put its content in @capsule.
1144 *
1145 * Return: status code
1146 */
1147static efi_status_t efi_capsule_read_file(const u16 *filename,
1148 struct efi_capsule_header **capsule)
1149{
1150 struct efi_file_handle *dirh, *fh;
1151 struct efi_file_info *file_info = NULL;
1152 struct efi_capsule_header *buf = NULL;
1153 efi_uintn_t size;
1154 efi_status_t ret;
1155
1156 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1157 EFI_CAPSULE_DIR,
1158 EFI_FILE_MODE_READ, 0));
1159 if (ret != EFI_SUCCESS)
1160 return ret;
1161 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1162 EFI_FILE_MODE_READ, 0));
1163 /* ignore an error */
1164 EFI_CALL((*dirh->close)(dirh));
1165 if (ret != EFI_SUCCESS)
1166 return ret;
1167
1168 /* file size */
1169 size = 0;
1170 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1171 &size, file_info));
1172 if (ret == EFI_BUFFER_TOO_SMALL) {
1173 file_info = malloc(size);
1174 if (!file_info) {
1175 ret = EFI_OUT_OF_RESOURCES;
1176 goto err;
1177 }
1178 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1179 &size, file_info));
1180 }
1181 if (ret != EFI_SUCCESS)
1182 goto err;
1183 size = file_info->file_size;
1184 free(file_info);
1185 buf = malloc(size);
1186 if (!buf) {
1187 ret = EFI_OUT_OF_RESOURCES;
1188 goto err;
1189 }
1190
1191 /* fetch data */
1192 ret = EFI_CALL((*fh->read)(fh, &size, buf));
1193 if (ret == EFI_SUCCESS) {
1194 if (size >= buf->capsule_image_size) {
1195 *capsule = buf;
1196 } else {
1197 free(buf);
1198 ret = EFI_INVALID_PARAMETER;
1199 }
1200 } else {
1201 free(buf);
1202 }
1203err:
1204 EFI_CALL((*fh->close)(fh));
1205
1206 return ret;
1207}
1208
1209/**
1210 * efi_capsule_delete_file - delete a capsule file
1211 * @filename: File name
1212 *
1213 * Delete a capsule file from capsule directory.
1214 *
1215 * Return: status code
1216 */
1217static efi_status_t efi_capsule_delete_file(const u16 *filename)
1218{
1219 struct efi_file_handle *dirh, *fh;
1220 efi_status_t ret;
1221
1222 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1223 EFI_CAPSULE_DIR,
1224 EFI_FILE_MODE_READ, 0));
1225 if (ret != EFI_SUCCESS)
1226 return ret;
1227 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1228 EFI_FILE_MODE_READ, 0));
1229 /* ignore an error */
1230 EFI_CALL((*dirh->close)(dirh));
1231
Heinrich Schuchardte27b0ff2021-06-02 19:28:22 +02001232 if (ret == EFI_SUCCESS)
1233 ret = EFI_CALL((*fh->delete)(fh));
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001234
1235 return ret;
1236}
1237
1238/**
1239 * efi_capsule_scan_done - reset a scan help function
1240 *
1241 * Reset a scan help function
1242 */
1243static void efi_capsule_scan_done(void)
1244{
1245 EFI_CALL((*bootdev_root->close)(bootdev_root));
1246 bootdev_root = NULL;
1247}
1248
1249/**
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001250 * check_run_capsules() - check whether capsule update should run
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001251 *
1252 * The spec says OsIndications must be set in order to run the capsule update
1253 * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to
1254 * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001255 *
1256 * Return: EFI_SUCCESS if update to run, EFI_NOT_FOUND otherwise
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001257 */
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001258static efi_status_t check_run_capsules(void)
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001259{
Sughosh Ganu119fafd2022-06-01 23:30:39 +05301260 u64 os_indications = 0x0;
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001261 efi_uintn_t size;
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001262 efi_status_t r;
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001263
1264 size = sizeof(os_indications);
Simon Glass156ccbc2022-01-23 12:55:12 -07001265 r = efi_get_variable_int(u"OsIndications", &efi_global_variable_guid,
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001266 NULL, &size, &os_indications, NULL);
Sughosh Ganu119fafd2022-06-01 23:30:39 +05301267 if (!IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS) &&
1268 (r != EFI_SUCCESS || size != sizeof(os_indications)))
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001269 return EFI_NOT_FOUND;
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001270
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001271 if (os_indications &
1272 EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
1273 os_indications &=
1274 ~EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
Simon Glass156ccbc2022-01-23 12:55:12 -07001275 r = efi_set_variable_int(u"OsIndications",
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001276 &efi_global_variable_guid,
1277 EFI_VARIABLE_NON_VOLATILE |
1278 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1279 EFI_VARIABLE_RUNTIME_ACCESS,
1280 sizeof(os_indications),
1281 &os_indications, false);
1282 if (r != EFI_SUCCESS)
1283 log_err("Setting %ls failed\n", L"OsIndications");
1284 return EFI_SUCCESS;
1285 } else if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS)) {
1286 return EFI_SUCCESS;
Sughosh Ganu119fafd2022-06-01 23:30:39 +05301287 } else {
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001288 return EFI_NOT_FOUND;
1289 }
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001290}
1291
1292/**
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001293 * efi_launch_capsule - launch capsules
1294 *
1295 * Launch all the capsules in system at boot time.
1296 * Called by efi init code
1297 *
1298 * Return: status codde
1299 */
1300efi_status_t efi_launch_capsules(void)
1301{
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001302 struct efi_capsule_header *capsule = NULL;
1303 u16 **files;
Etienne Carriere44293932023-02-16 18:21:41 +01001304 unsigned int nfiles, index, index_max, i;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001305 efi_status_t ret;
Sughosh Ganu86794052022-10-21 18:16:03 +05301306 bool capsule_update = true;
1307 bool update_status = true;
1308 bool fw_accept_os = false;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001309
Heinrich Schuchardt9d1564d2021-11-20 11:53:12 +01001310 if (check_run_capsules() != EFI_SUCCESS)
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001311 return EFI_SUCCESS;
1312
Etienne Carriere44293932023-02-16 18:21:41 +01001313 index_max = get_max_capsule();
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001314 index = get_last_capsule();
1315
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001316 /*
1317 * Find capsules on disk.
1318 * All the capsules are collected at the beginning because
1319 * capsule files will be removed instantly.
1320 */
1321 nfiles = 0;
1322 files = NULL;
1323 ret = efi_capsule_scan_dir(&files, &nfiles);
1324 if (ret != EFI_SUCCESS)
1325 return ret;
1326 if (!nfiles)
1327 return EFI_SUCCESS;
1328
1329 /* Launch capsules */
1330 for (i = 0, ++index; i < nfiles; i++, index++) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001331 log_debug("Applying %ls\n", files[i]);
Etienne Carriere44293932023-02-16 18:21:41 +01001332 if (index > index_max)
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001333 index = 0;
1334 ret = efi_capsule_read_file(files[i], &capsule);
1335 if (ret == EFI_SUCCESS) {
Masami Hiramatsua6aafce2022-02-16 15:15:42 +09001336 ret = efi_capsule_update_firmware(capsule);
Sughosh Ganu86794052022-10-21 18:16:03 +05301337 if (ret != EFI_SUCCESS) {
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001338 log_err("Applying capsule %ls failed.\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001339 files[i]);
Sughosh Ganu86794052022-10-21 18:16:03 +05301340 update_status = false;
1341 } else {
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001342 log_info("Applying capsule %ls succeeded.\n",
1343 files[i]);
Sughosh Ganu86794052022-10-21 18:16:03 +05301344 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1345 fwu_post_update_checks(capsule,
1346 &fw_accept_os,
1347 &capsule_update);
1348 }
1349 }
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001350
Masami Hiramatsu5d49b322021-11-12 22:05:15 +09001351 /* create CapsuleXXXX */
1352 set_capsule_result(index, capsule, ret);
1353
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001354 free(capsule);
1355 } else {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001356 log_err("Reading capsule %ls failed\n", files[i]);
Sughosh Ganu86794052022-10-21 18:16:03 +05301357 update_status = false;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001358 }
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001359 /* delete a capsule either in case of success or failure */
1360 ret = efi_capsule_delete_file(files[i]);
1361 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001362 log_err("Deleting capsule %ls failed\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001363 files[i]);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001364 }
Sughosh Ganu86794052022-10-21 18:16:03 +05301365
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001366 efi_capsule_scan_done();
1367
Sughosh Ganu86794052022-10-21 18:16:03 +05301368 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1369 if (capsule_update == true && update_status == true) {
1370 ret = fwu_post_update_process(fw_accept_os);
1371 } else if (capsule_update == true && update_status == false) {
1372 log_err("All capsules were not updated. Not updating FWU metadata\n");
1373 }
1374 }
1375
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001376 for (i = 0; i < nfiles; i++)
1377 free(files[i]);
1378 free(files);
1379
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001380 /*
1381 * UEFI spec requires to reset system after complete processing capsule
1382 * update on the storage.
1383 */
Masami Hiramatsu7660cfe2022-03-21 22:37:56 +09001384 log_info("Reboot after firmware update.\n");
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001385 /* Cold reset is required for loading the new firmware. */
Masami Hiramatsu7660cfe2022-03-21 22:37:56 +09001386 sysreset_walk_halt(SYSRESET_COLD);
Masami Hiramatsu3e6f8102022-02-16 15:16:12 +09001387 hang();
1388 /* not reach here */
1389
1390 return 0;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001391}
1392#endif /* CONFIG_EFI_CAPSULE_ON_DISK */