blob: 0937800e588fdaf3bf3011bd16e036bfdfa8c928 [file] [log] [blame]
AKASHI Takahiro473d9b32020-11-17 09:27:55 +09001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * EFI Capsule
4 *
5 * Copyright (c) 2018 Linaro Limited
6 * Author: AKASHI Takahiro
7 */
8
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02009#define LOG_CATEGORY LOGC_EFI
10
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090011#include <efi_loader.h>
12#include <efi_variable.h>
AKASHI Takahirob71a0ae2021-10-07 15:23:32 +090013#include <env.h>
14#include <fdtdec.h>
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090015#include <fs.h>
Sughosh Ganu1cadae22022-10-21 18:16:03 +053016#include <fwu.h>
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +090017#include <hang.h>
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090018#include <malloc.h>
AKASHI Takahiro45b819542020-11-17 09:27:56 +090019#include <mapmem.h>
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090020#include <sort.h>
Masami Hiramatsuff744862022-03-21 22:37:56 +090021#include <sysreset.h>
AKASHI Takahirob71a0ae2021-10-07 15:23:32 +090022#include <asm/global_data.h>
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090023
Sughosh Ganu586bb982020-12-30 19:27:09 +053024#include <crypto/pkcs7.h>
25#include <crypto/pkcs7_parser.h>
26#include <linux/err.h>
27
AKASHI Takahirob71a0ae2021-10-07 15:23:32 +090028DECLARE_GLOBAL_DATA_PTR;
29
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090030const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
AKASHI Takahiro0d963782020-11-30 18:12:11 +090031static const efi_guid_t efi_guid_firmware_management_capsule_id =
32 EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
33const efi_guid_t efi_guid_firmware_management_protocol =
34 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID;
Sughosh Ganu1cadae22022-10-21 18:16:03 +053035const efi_guid_t fwu_guid_os_request_fw_revert =
36 FWU_OS_REQUEST_FW_REVERT_GUID;
37const efi_guid_t fwu_guid_os_request_fw_accept =
38 FWU_OS_REQUEST_FW_ACCEPT_GUID;
39
40#define FW_ACCEPT_OS (u32)0x8000
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090041
AKASHI Takahiro45b819542020-11-17 09:27:56 +090042#ifdef CONFIG_EFI_CAPSULE_ON_DISK
43/* for file system access */
44static struct efi_file_handle *bootdev_root;
45#endif
46
Etienne Carriere6326e912023-02-16 18:21:41 +010047static __maybe_unused unsigned int get_capsule_index(const u16 *variable_name)
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090048{
49 u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010050 char value[5];
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090051 efi_uintn_t size;
52 unsigned long index = 0xffff;
53 efi_status_t ret;
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010054 int i;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090055
56 size = sizeof(value16);
Etienne Carriere6326e912023-02-16 18:21:41 +010057 ret = efi_get_variable_int(variable_name, &efi_guid_capsule_report,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090058 NULL, &size, value16, NULL);
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010059 if (ret != EFI_SUCCESS || size != 22 ||
Simon Glass90975372022-01-23 12:55:12 -070060 u16_strncmp(value16, u"Capsule", 7))
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090061 goto err;
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010062 for (i = 0; i < 4; ++i) {
63 u16 c = value16[i + 7];
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090064
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010065 if (!c || c > 0x7f)
66 goto err;
67 value[i] = c;
68 }
69 value[4] = 0;
70 if (strict_strtoul(value, 16, &index))
71 index = 0xffff;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090072err:
73 return index;
74}
75
76/**
Etienne Carriere6326e912023-02-16 18:21:41 +010077 * get_last_capsule - get the last capsule index
78 *
79 * Retrieve the index of the capsule invoked last time from "CapsuleLast"
80 * variable.
81 *
82 * Return:
83 * * > 0 - the last capsule index invoked
84 * * 0xffff - on error, or no capsule invoked yet
85 */
86static __maybe_unused unsigned int get_last_capsule(void)
87{
88 return get_capsule_index(u"CapsuleLast");
89}
90
91/**
92 * get_max_capsule - get the max capsule index
93 *
94 * Retrieve the max capsule index value from "CapsuleMax" variable.
95 *
96 * Return:
97 * * > 0 - the max capsule index
98 * * 0xffff - on error, or "CapsuleMax" variable does not exist
99 */
100static __maybe_unused unsigned int get_max_capsule(void)
101{
102 return get_capsule_index(u"CapsuleMax");
103}
104
105/**
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900106 * set_capsule_result - set a result variable
107 * @capsule: Capsule
108 * @return_status: Return status
109 *
110 * Create and set a result variable, "CapsuleXXXX", for the capsule,
111 * @capsule.
112 */
113static __maybe_unused
114void set_capsule_result(int index, struct efi_capsule_header *capsule,
115 efi_status_t return_status)
116{
117 u16 variable_name16[12];
118 struct efi_capsule_result_variable_header result;
119 struct efi_time time;
120 efi_status_t ret;
121
Ilias Apalodimas21575292020-12-31 12:26:46 +0200122 efi_create_indexed_name(variable_name16, sizeof(variable_name16),
123 "Capsule", index);
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900124 result.variable_total_size = sizeof(result);
125 result.capsule_guid = capsule->capsule_guid;
126 ret = EFI_CALL((*efi_runtime_services.get_time)(&time, NULL));
127 if (ret == EFI_SUCCESS)
128 memcpy(&result.capsule_processed, &time, sizeof(time));
129 else
130 memset(&result.capsule_processed, 0, sizeof(time));
131 result.capsule_status = return_status;
Heinrich Schuchardt24adaa72021-07-10 11:10:26 +0200132 ret = efi_set_variable_int(variable_name16, &efi_guid_capsule_report,
133 EFI_VARIABLE_NON_VOLATILE |
134 EFI_VARIABLE_BOOTSERVICE_ACCESS |
135 EFI_VARIABLE_RUNTIME_ACCESS,
136 sizeof(result), &result, false);
Heinrich Schuchardtb1fae8c2021-07-10 11:14:13 +0200137 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200138 log_err("Setting %ls failed\n", variable_name16);
Heinrich Schuchardtb1fae8c2021-07-10 11:14:13 +0200139 return;
140 }
141
142 /* Variable CapsuleLast must not include terminating 0x0000 */
Simon Glass90975372022-01-23 12:55:12 -0700143 ret = efi_set_variable_int(u"CapsuleLast", &efi_guid_capsule_report,
Heinrich Schuchardtb1fae8c2021-07-10 11:14:13 +0200144 EFI_VARIABLE_READ_ONLY |
145 EFI_VARIABLE_NON_VOLATILE |
146 EFI_VARIABLE_BOOTSERVICE_ACCESS |
147 EFI_VARIABLE_RUNTIME_ACCESS,
148 22, variable_name16, false);
149 if (ret != EFI_SUCCESS)
Simon Glass90975372022-01-23 12:55:12 -0700150 log_err("Setting %ls failed\n", u"CapsuleLast");
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900151}
152
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900153#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT
154/**
155 * efi_fmp_find - search for Firmware Management Protocol drivers
156 * @image_type: Image type guid
Sughosh Ganuf55c6b62022-04-15 11:29:36 +0530157 * @image_index: Image Index
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900158 * @instance: Instance number
159 * @handles: Handles of FMP drivers
160 * @no_handles: Number of handles
161 *
162 * Search for Firmware Management Protocol drivers, matching the image
163 * type, @image_type and the machine instance, @instance, from the list,
164 * @handles.
165 *
166 * Return:
167 * * Protocol instance - on success
168 * * NULL - on failure
169 */
170static struct efi_firmware_management_protocol *
Sughosh Ganuf55c6b62022-04-15 11:29:36 +0530171efi_fmp_find(efi_guid_t *image_type, u8 image_index, u64 instance,
172 efi_handle_t *handles, efi_uintn_t no_handles)
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900173{
174 efi_handle_t *handle;
175 struct efi_firmware_management_protocol *fmp;
176 struct efi_firmware_image_descriptor *image_info, *desc;
177 efi_uintn_t info_size, descriptor_size;
178 u32 descriptor_version;
179 u8 descriptor_count;
180 u32 package_version;
181 u16 *package_version_name;
182 bool found = false;
183 int i, j;
184 efi_status_t ret;
185
186 for (i = 0, handle = handles; i < no_handles; i++, handle++) {
Heinrich Schuchardtd8dc3cb2022-10-07 15:29:52 +0200187 struct efi_handler *fmp_handler;
188
189 ret = efi_search_protocol(
190 *handle, &efi_guid_firmware_management_protocol,
191 &fmp_handler);
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900192 if (ret != EFI_SUCCESS)
193 continue;
Heinrich Schuchardtd8dc3cb2022-10-07 15:29:52 +0200194 fmp = fmp_handler->protocol_interface;
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900195
196 /* get device's image info */
197 info_size = 0;
198 image_info = NULL;
199 descriptor_version = 0;
200 descriptor_count = 0;
201 descriptor_size = 0;
202 package_version = 0;
203 package_version_name = NULL;
204 ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
205 image_info,
206 &descriptor_version,
207 &descriptor_count,
208 &descriptor_size,
209 &package_version,
210 &package_version_name));
211 if (ret != EFI_BUFFER_TOO_SMALL)
212 goto skip;
213
214 image_info = malloc(info_size);
215 if (!image_info)
216 goto skip;
217
218 ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
219 image_info,
220 &descriptor_version,
221 &descriptor_count,
222 &descriptor_size,
223 &package_version,
224 &package_version_name));
225 if (ret != EFI_SUCCESS ||
226 descriptor_version != EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION)
227 goto skip;
228
229 /* matching */
230 for (j = 0, desc = image_info; j < descriptor_count;
231 j++, desc = (void *)desc + descriptor_size) {
232 log_debug("+++ desc[%d] index: %d, name: %ls\n",
233 j, desc->image_index, desc->image_id_name);
234 if (!guidcmp(&desc->image_type_id, image_type) &&
Sughosh Ganuf55c6b62022-04-15 11:29:36 +0530235 (desc->image_index == image_index) &&
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900236 (!instance ||
237 !desc->hardware_instance ||
238 desc->hardware_instance == instance))
239 found = true;
240 }
241
242skip:
243 efi_free_pool(package_version_name);
244 free(image_info);
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900245 if (found)
246 return fmp;
247 }
248
249 return NULL;
250}
251
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900252/**
253 * efi_remove_auth_hdr - remove authentication data from image
254 * @image: Pointer to pointer to Image
255 * @image_size: Pointer to Image size
256 *
257 * Remove the authentication data from image if possible.
258 * Update @image and @image_size.
259 *
260 * Return: status code
261 */
262static efi_status_t efi_remove_auth_hdr(void **image, efi_uintn_t *image_size)
263{
264 struct efi_firmware_image_authentication *auth_hdr;
265 efi_status_t ret = EFI_INVALID_PARAMETER;
266
267 auth_hdr = (struct efi_firmware_image_authentication *)*image;
268 if (*image_size < sizeof(*auth_hdr))
269 goto out;
270
271 if (auth_hdr->auth_info.hdr.dwLength <=
272 offsetof(struct win_certificate_uefi_guid, cert_data))
273 goto out;
274
275 *image = (uint8_t *)*image + sizeof(auth_hdr->monotonic_count) +
276 auth_hdr->auth_info.hdr.dwLength;
277 *image_size = *image_size - auth_hdr->auth_info.hdr.dwLength -
278 sizeof(auth_hdr->monotonic_count);
279
280 ret = EFI_SUCCESS;
281out:
282 return ret;
283}
284
Sughosh Ganu586bb982020-12-30 19:27:09 +0530285#if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
AKASHI Takahirof155bf52021-11-02 09:55:01 +0900286int efi_get_public_key_data(void **pkey, efi_uintn_t *pkey_len)
AKASHI Takahirob71a0ae2021-10-07 15:23:32 +0900287{
288 const void *fdt_blob = gd->fdt_blob;
289 const void *blob;
290 const char *cnode_name = "capsule-key";
291 const char *snode_name = "signature";
292 int sig_node;
293 int len;
294
295 sig_node = fdt_subnode_offset(fdt_blob, 0, snode_name);
296 if (sig_node < 0) {
297 log_err("Unable to get signature node offset\n");
298
299 return -FDT_ERR_NOTFOUND;
300 }
301
302 blob = fdt_getprop(fdt_blob, sig_node, cnode_name, &len);
303
304 if (!blob || len < 0) {
305 log_err("Unable to get capsule-key value\n");
306 *pkey = NULL;
307 *pkey_len = 0;
308
309 return -FDT_ERR_NOTFOUND;
310 }
311
312 *pkey = (void *)blob;
313 *pkey_len = len;
314
315 return 0;
316}
Sughosh Ganu586bb982020-12-30 19:27:09 +0530317
Sughosh Ganu586bb982020-12-30 19:27:09 +0530318efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
319 void **image, efi_uintn_t *image_size)
320{
321 u8 *buf;
322 int ret;
Simon Glass1f78c122021-08-02 08:44:31 -0600323 void *fdt_pkey, *pkey;
Sughosh Ganu586bb982020-12-30 19:27:09 +0530324 efi_uintn_t pkey_len;
325 uint64_t monotonic_count;
326 struct efi_signature_store *truststore;
327 struct pkcs7_message *capsule_sig;
328 struct efi_image_regions *regs;
329 struct efi_firmware_image_authentication *auth_hdr;
330 efi_status_t status;
331
332 status = EFI_SECURITY_VIOLATION;
333 capsule_sig = NULL;
334 truststore = NULL;
335 regs = NULL;
336
337 /* Sanity checks */
338 if (capsule == NULL || capsule_size == 0)
339 goto out;
340
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900341 *image = (uint8_t *)capsule;
342 *image_size = capsule_size;
343 if (efi_remove_auth_hdr(image, image_size) != EFI_SUCCESS)
Sughosh Ganu586bb982020-12-30 19:27:09 +0530344 goto out;
345
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900346 auth_hdr = (struct efi_firmware_image_authentication *)capsule;
Sughosh Ganu586bb982020-12-30 19:27:09 +0530347 if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
348 goto out;
349
Sughosh Ganu586bb982020-12-30 19:27:09 +0530350 memcpy(&monotonic_count, &auth_hdr->monotonic_count,
351 sizeof(monotonic_count));
352
353 /* data to be digested */
354 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 2, 1);
355 if (!regs)
356 goto out;
357
358 regs->max = 2;
359 efi_image_region_add(regs, (uint8_t *)*image,
360 (uint8_t *)*image + *image_size, 1);
361
362 efi_image_region_add(regs, (uint8_t *)&monotonic_count,
363 (uint8_t *)&monotonic_count + sizeof(monotonic_count),
364 1);
365
366 capsule_sig = efi_parse_pkcs7_header(auth_hdr->auth_info.cert_data,
367 auth_hdr->auth_info.hdr.dwLength
368 - sizeof(auth_hdr->auth_info),
369 &buf);
Dan Carpenter15325232023-07-27 10:16:20 +0300370 if (!capsule_sig) {
Sughosh Ganu586bb982020-12-30 19:27:09 +0530371 debug("Parsing variable's pkcs7 header failed\n");
Sughosh Ganu586bb982020-12-30 19:27:09 +0530372 goto out;
373 }
374
Simon Glass1f78c122021-08-02 08:44:31 -0600375 ret = efi_get_public_key_data(&fdt_pkey, &pkey_len);
Sughosh Ganu586bb982020-12-30 19:27:09 +0530376 if (ret < 0)
377 goto out;
378
379 pkey = malloc(pkey_len);
380 if (!pkey)
381 goto out;
382
Simon Glass1f78c122021-08-02 08:44:31 -0600383 memcpy(pkey, fdt_pkey, pkey_len);
Sughosh Ganu586bb982020-12-30 19:27:09 +0530384 truststore = efi_build_signature_store(pkey, pkey_len);
385 if (!truststore)
386 goto out;
387
388 /* verify signature */
389 if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) {
390 debug("Verified\n");
391 } else {
392 debug("Verifying variable's signature failed\n");
393 goto out;
394 }
395
396 status = EFI_SUCCESS;
397
398out:
399 efi_sigstore_free(truststore);
400 pkcs7_free_message(capsule_sig);
401 free(regs);
402
403 return status;
404}
Sughosh Ganu586bb982020-12-30 19:27:09 +0530405#endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */
406
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530407static __maybe_unused bool fwu_empty_capsule(struct efi_capsule_header *capsule)
408{
409 return !guidcmp(&capsule->capsule_guid,
410 &fwu_guid_os_request_fw_revert) ||
411 !guidcmp(&capsule->capsule_guid,
412 &fwu_guid_os_request_fw_accept);
413}
414
415static __maybe_unused efi_status_t fwu_to_efi_error(int err)
416{
417 efi_status_t ret;
418
419 switch(err) {
420 case 0:
421 ret = EFI_SUCCESS;
422 break;
423 case -ERANGE:
424 case -EIO:
425 ret = EFI_DEVICE_ERROR;
426 break;
427 case -EINVAL:
428 ret = EFI_INVALID_PARAMETER;
429 break;
430 case -ENODEV:
431 ret = EFI_NOT_FOUND;
432 break;
433 default:
434 ret = EFI_OUT_OF_RESOURCES;
435 }
436
437 return ret;
438}
439
440static __maybe_unused efi_status_t fwu_empty_capsule_process(
441 struct efi_capsule_header *capsule)
442{
443 int status;
444 u32 active_idx;
445 efi_guid_t *image_guid;
446 efi_status_t ret = EFI_INVALID_PARAMETER;
447
448 if (!guidcmp(&capsule->capsule_guid,
449 &fwu_guid_os_request_fw_revert)) {
450 /*
451 * One of the previously updated image has
452 * failed the OS acceptance test. OS has
453 * requested to revert back to the earlier
454 * boot index
455 */
456 status = fwu_revert_boot_index();
457 ret = fwu_to_efi_error(status);
458 if (ret == EFI_SUCCESS)
459 log_debug("Reverted the FWU active_index. Recommend rebooting the system\n");
460 else
461 log_err("Failed to revert the FWU boot index\n");
462 } else if (!guidcmp(&capsule->capsule_guid,
463 &fwu_guid_os_request_fw_accept)) {
464 /*
465 * Image accepted by the OS. Set the acceptance
466 * status for the image.
467 */
468 image_guid = (void *)(char *)capsule +
469 capsule->header_size;
470
471 status = fwu_get_active_index(&active_idx);
472 ret = fwu_to_efi_error(status);
473 if (ret != EFI_SUCCESS) {
474 log_err("Unable to get the active_index from the FWU metadata\n");
475 return ret;
476 }
477
478 status = fwu_accept_image(image_guid, active_idx);
479 ret = fwu_to_efi_error(status);
480 if (ret != EFI_SUCCESS)
481 log_err("Unable to set the Accept bit for the image %pUs\n",
482 image_guid);
Sughosh Ganu2aa3fe52024-03-22 16:27:22 +0530483
484 status = fwu_state_machine_updates(0, active_idx);
485 if (status < 0)
486 ret = EFI_DEVICE_ERROR;
487
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530488 }
489
490 return ret;
491}
492
493static __maybe_unused void fwu_post_update_checks(
494 struct efi_capsule_header *capsule,
495 bool *fw_accept_os, bool *capsule_update)
496{
497 if (fwu_empty_capsule(capsule))
498 *capsule_update = false;
499 else
500 if (!*fw_accept_os)
501 *fw_accept_os =
502 capsule->flags & FW_ACCEPT_OS ? true : false;
503}
504
505static __maybe_unused efi_status_t fwu_post_update_process(bool fw_accept_os)
506{
507 int status;
508 uint update_index;
509 efi_status_t ret;
510
511 status = fwu_plat_get_update_index(&update_index);
512 if (status < 0) {
513 log_err("Failed to get the FWU update_index value\n");
514 return EFI_DEVICE_ERROR;
515 }
516
517 /*
518 * All the capsules have been updated successfully,
519 * update the FWU metadata.
520 */
521 log_debug("Update Complete. Now updating active_index to %u\n",
522 update_index);
523 status = fwu_set_active_index(update_index);
524 ret = fwu_to_efi_error(status);
525 if (ret != EFI_SUCCESS) {
526 log_err("Failed to update FWU metadata index values\n");
527 } else {
528 log_debug("Successfully updated the active_index\n");
Sughosh Ganu2aa3fe52024-03-22 16:27:22 +0530529 status = fwu_state_machine_updates(fw_accept_os ? 1 : 0,
530 update_index);
531 if (status < 0)
532 ret = EFI_DEVICE_ERROR;
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530533 }
534
535 return ret;
536}
Sughosh Ganu586bb982020-12-30 19:27:09 +0530537
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900538/**
539 * efi_capsule_update_firmware - update firmware from capsule
540 * @capsule_data: Capsule
541 *
542 * Update firmware, using a capsule, @capsule_data. Loading any FMP
543 * drivers embedded in a capsule is not supported.
544 *
545 * Return: status code
546 */
547static efi_status_t efi_capsule_update_firmware(
548 struct efi_capsule_header *capsule_data)
549{
550 struct efi_firmware_management_capsule_header *capsule;
551 struct efi_firmware_management_capsule_image_header *image;
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900552 size_t capsule_size, image_binary_size;
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900553 void *image_binary, *vendor_code;
554 efi_handle_t *handles;
555 efi_uintn_t no_handles;
556 int item;
557 struct efi_firmware_management_protocol *fmp;
558 u16 *abort_reason;
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530559 efi_guid_t *image_type_id;
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900560 efi_status_t ret = EFI_SUCCESS;
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530561 int status;
562 uint update_index;
563 bool fw_accept_os;
564
565 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
566 if (fwu_empty_capsule_checks_pass() &&
567 fwu_empty_capsule(capsule_data))
568 return fwu_empty_capsule_process(capsule_data);
569
570 if (!fwu_update_checks_pass()) {
571 log_err("FWU checks failed. Cannot start update\n");
572 return EFI_INVALID_PARAMETER;
573 }
574
575
576 /* Obtain the update_index from the platform */
577 status = fwu_plat_get_update_index(&update_index);
578 if (status < 0) {
579 log_err("Failed to get the FWU update_index value\n");
580 return EFI_DEVICE_ERROR;
581 }
582
583 fw_accept_os = capsule_data->flags & FW_ACCEPT_OS ? 0x1 : 0x0;
584 }
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900585
AKASHI Takahiroa8000b92023-07-27 09:38:00 +0900586 if (guidcmp(&capsule_data->capsule_guid,
587 &efi_guid_firmware_management_capsule_id)) {
588 log_err("Unsupported capsule type: %pUs\n",
589 &capsule_data->capsule_guid);
590 return EFI_UNSUPPORTED;
591 }
592
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900593 /* sanity check */
594 if (capsule_data->header_size < sizeof(*capsule) ||
595 capsule_data->header_size >= capsule_data->capsule_image_size)
596 return EFI_INVALID_PARAMETER;
597
598 capsule = (void *)capsule_data + capsule_data->header_size;
599 capsule_size = capsule_data->capsule_image_size
600 - capsule_data->header_size;
601
602 if (capsule->version != 0x00000001)
603 return EFI_UNSUPPORTED;
604
605 handles = NULL;
606 ret = EFI_CALL(efi_locate_handle_buffer(
607 BY_PROTOCOL,
608 &efi_guid_firmware_management_protocol,
609 NULL, &no_handles, (efi_handle_t **)&handles));
610 if (ret != EFI_SUCCESS)
611 return EFI_UNSUPPORTED;
612
613 /* Payload */
614 for (item = capsule->embedded_driver_count;
615 item < capsule->embedded_driver_count
616 + capsule->payload_item_count; item++) {
617 /* sanity check */
618 if ((capsule->item_offset_list[item] + sizeof(*image)
619 >= capsule_size)) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200620 log_err("Capsule does not have enough data\n");
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900621 ret = EFI_INVALID_PARAMETER;
622 goto out;
623 }
624
625 image = (void *)capsule + capsule->item_offset_list[item];
626
627 if (image->version != 0x00000003) {
628 ret = EFI_UNSUPPORTED;
629 goto out;
630 }
631
632 /* find a device for update firmware */
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900633 fmp = efi_fmp_find(&image->update_image_type_id,
Sughosh Ganuf55c6b62022-04-15 11:29:36 +0530634 image->update_image_index,
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900635 image->update_hardware_instance,
636 handles, no_handles);
637 if (!fmp) {
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100638 log_err("FMP driver not found for firmware type %pUs, hardware instance %lld\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900639 &image->update_image_type_id,
640 image->update_hardware_instance);
641 ret = EFI_UNSUPPORTED;
642 goto out;
643 }
644
645 /* do update */
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900646 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
647 !(image->image_capsule_support &
648 CAPSULE_SUPPORT_AUTHENTICATION)) {
649 /* no signature */
650 ret = EFI_SECURITY_VIOLATION;
651 goto out;
652 }
653
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900654 image_binary = (void *)image + sizeof(*image);
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900655 image_binary_size = image->update_image_size;
656 vendor_code = image_binary + image_binary_size;
657 if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
658 (image->image_capsule_support &
659 CAPSULE_SUPPORT_AUTHENTICATION)) {
660 ret = efi_remove_auth_hdr(&image_binary,
661 &image_binary_size);
662 if (ret != EFI_SUCCESS)
663 goto out;
664 }
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900665
666 abort_reason = NULL;
667 ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
668 image_binary,
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900669 image_binary_size,
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900670 vendor_code, NULL,
671 &abort_reason));
672 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200673 log_err("Firmware update failed: %ls\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900674 abort_reason);
675 efi_free_pool(abort_reason);
676 goto out;
677 }
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530678
679 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
680 image_type_id = &image->update_image_type_id;
681 if (!fw_accept_os) {
682 /*
683 * The OS will not be accepting the firmware
684 * images. Set the accept bit of all the
685 * images contained in this capsule.
686 */
687 status = fwu_accept_image(image_type_id,
688 update_index);
689 } else {
690 status = fwu_clear_accept_image(image_type_id,
691 update_index);
692 }
693 ret = fwu_to_efi_error(status);
694 if (ret != EFI_SUCCESS) {
695 log_err("Unable to %s the accept bit for the image %pUs\n",
696 fw_accept_os ? "clear" : "set",
697 image_type_id);
698 goto out;
699 }
700
701 log_debug("%s the accepted bit for Image %pUs\n",
702 fw_accept_os ? "Cleared" : "Set",
703 image_type_id);
704 }
705
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900706 }
707
708out:
709 efi_free_pool(handles);
710
711 return ret;
712}
713#else
714static efi_status_t efi_capsule_update_firmware(
715 struct efi_capsule_header *capsule_data)
716{
717 return EFI_UNSUPPORTED;
718}
719#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
720
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900721/**
722 * efi_update_capsule() - process information from operating system
723 * @capsule_header_array: Array of virtual address pointers
724 * @capsule_count: Number of pointers in capsule_header_array
725 * @scatter_gather_list: Array of physical address pointers
726 *
727 * This function implements the UpdateCapsule() runtime service.
728 *
729 * See the Unified Extensible Firmware Interface (UEFI) specification for
730 * details.
731 *
732 * Return: status code
733 */
734efi_status_t EFIAPI efi_update_capsule(
735 struct efi_capsule_header **capsule_header_array,
736 efi_uintn_t capsule_count,
737 u64 scatter_gather_list)
738{
739 struct efi_capsule_header *capsule;
740 unsigned int i;
741 efi_status_t ret;
742
Simon Glass83698b22021-02-07 14:27:02 -0700743 EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900744 scatter_gather_list);
745
746 if (!capsule_count) {
747 ret = EFI_INVALID_PARAMETER;
748 goto out;
749 }
750
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900751 ret = EFI_SUCCESS;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900752 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
753 i++, capsule = *(++capsule_header_array)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900754 /* sanity check */
755 if (capsule->header_size < sizeof(*capsule) ||
756 capsule->capsule_image_size < sizeof(*capsule)) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200757 log_err("Capsule does not have enough data\n");
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900758 continue;
759 }
760
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100761 log_debug("Capsule[%d] (guid:%pUs)\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900762 i, &capsule->capsule_guid);
AKASHI Takahiroa8000b92023-07-27 09:38:00 +0900763 ret = efi_capsule_update_firmware(capsule);
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900764 if (ret != EFI_SUCCESS)
765 goto out;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900766 }
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000767
768 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
769 /* Rebuild the ESRT to reflect any updated FW images. */
770 ret = efi_esrt_populate();
771 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200772 log_warning("ESRT update failed\n");
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000773 }
Jose Marinhoaf886ce2021-04-19 14:54:33 +0100774out:
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000775
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900776 return EFI_EXIT(ret);
777}
778
779/**
780 * efi_query_capsule_caps() - check if capsule is supported
781 * @capsule_header_array: Array of virtual pointers
782 * @capsule_count: Number of pointers in capsule_header_array
783 * @maximum_capsule_size: Maximum capsule size
784 * @reset_type: Type of reset needed for capsule update
785 *
786 * This function implements the QueryCapsuleCapabilities() runtime service.
787 *
788 * See the Unified Extensible Firmware Interface (UEFI) specification for
789 * details.
790 *
791 * Return: status code
792 */
793efi_status_t EFIAPI efi_query_capsule_caps(
794 struct efi_capsule_header **capsule_header_array,
795 efi_uintn_t capsule_count,
796 u64 *maximum_capsule_size,
797 u32 *reset_type)
798{
799 struct efi_capsule_header *capsule __attribute__((unused));
800 unsigned int i;
801 efi_status_t ret;
802
Simon Glass83698b22021-02-07 14:27:02 -0700803 EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900804 maximum_capsule_size, reset_type);
805
806 if (!maximum_capsule_size) {
807 ret = EFI_INVALID_PARAMETER;
808 goto out;
809 }
810
811 *maximum_capsule_size = U64_MAX;
812 *reset_type = EFI_RESET_COLD;
813
814 ret = EFI_SUCCESS;
815 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
816 i++, capsule = *(++capsule_header_array)) {
817 /* TODO */
818 }
819out:
820 return EFI_EXIT(ret);
821}
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900822
Masami Hiramatsud306caa2022-03-21 22:37:45 +0900823/**
824 * efi_load_capsule_drivers - initialize capsule drivers
825 *
826 * Generic FMP drivers backed by DFU
827 *
828 * Return: status code
829 */
830efi_status_t __weak efi_load_capsule_drivers(void)
831{
832 __maybe_unused efi_handle_t handle;
833 efi_status_t ret = EFI_SUCCESS;
834
835 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
836 handle = NULL;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +0300837 ret = efi_install_multiple_protocol_interfaces(&handle,
838 &efi_guid_firmware_management_protocol,
839 &efi_fmp_fit,
840 NULL);
Masami Hiramatsud306caa2022-03-21 22:37:45 +0900841 }
842
843 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
844 handle = NULL;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +0300845 ret = efi_install_multiple_protocol_interfaces(&handle,
846 &efi_guid_firmware_management_protocol,
847 &efi_fmp_raw,
848 NULL);
Masami Hiramatsud306caa2022-03-21 22:37:45 +0900849 }
850
851 return ret;
852}
853
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900854#ifdef CONFIG_EFI_CAPSULE_ON_DISK
855/**
856 * get_dp_device - retrieve a device path from boot variable
857 * @boot_var: Boot variable name
858 * @device_dp Device path
859 *
860 * Retrieve a device patch from boot variable, @boot_var.
861 *
862 * Return: status code
863 */
864static efi_status_t get_dp_device(u16 *boot_var,
865 struct efi_device_path **device_dp)
866{
867 void *buf = NULL;
868 efi_uintn_t size;
869 struct efi_load_option lo;
870 struct efi_device_path *file_dp;
871 efi_status_t ret;
872
873 size = 0;
874 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
875 NULL, &size, NULL, NULL);
876 if (ret == EFI_BUFFER_TOO_SMALL) {
877 buf = malloc(size);
878 if (!buf)
879 return EFI_OUT_OF_RESOURCES;
880 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
881 NULL, &size, buf, NULL);
882 }
883 if (ret != EFI_SUCCESS)
884 return ret;
885
886 efi_deserialize_load_option(&lo, buf, &size);
887
888 if (lo.attributes & LOAD_OPTION_ACTIVE) {
889 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
890 efi_free_pool(file_dp);
891
892 ret = EFI_SUCCESS;
893 } else {
894 ret = EFI_NOT_FOUND;
895 }
896
897 free(buf);
898
899 return ret;
900}
901
902/**
903 * device_is_present_and_system_part - check if a device exists
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900904 *
905 * Check if a device pointed to by the device path, @dp, exists and is
906 * located in UEFI system partition.
907 *
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100908 * @dp device path
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900909 * Return: true - yes, false - no
910 */
911static bool device_is_present_and_system_part(struct efi_device_path *dp)
912{
913 efi_handle_t handle;
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100914 struct efi_device_path *rem;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900915
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100916 /* Check device exists */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100917 handle = efi_dp_find_obj(dp, NULL, NULL);
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900918 if (!handle)
919 return false;
920
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100921 /* Check device is on system partition */
922 handle = efi_dp_find_obj(dp, &efi_system_partition_guid, &rem);
923 if (!handle)
924 return false;
925
926 return true;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900927}
928
929/**
930 * find_boot_device - identify the boot device
931 *
932 * Identify the boot device from boot-related variables as UEFI
933 * specification describes and put its handle into bootdev_root.
934 *
935 * Return: status code
936 */
937static efi_status_t find_boot_device(void)
938{
939 char boot_var[9];
940 u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
941 efi_uintn_t size;
942 int i, num;
943 struct efi_simple_file_system_protocol *volume;
944 struct efi_device_path *boot_dev = NULL;
945 efi_status_t ret;
946
947 /* find active boot device in BootNext */
948 bootnext = 0;
949 size = sizeof(bootnext);
Simon Glass90975372022-01-23 12:55:12 -0700950 ret = efi_get_variable_int(u"BootNext",
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900951 (efi_guid_t *)&efi_global_variable_guid,
952 NULL, &size, &bootnext, NULL);
953 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
954 /* BootNext does exist here */
955 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900956 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900957 goto skip;
958 }
959 sprintf((char *)boot_var, "Boot%04X", bootnext);
960 p = boot_var16;
961 utf8_utf16_strcpy(&p, boot_var);
962
963 ret = get_dp_device(boot_var16, &boot_dev);
964 if (ret == EFI_SUCCESS) {
965 if (device_is_present_and_system_part(boot_dev)) {
Masami Hiramatsu10165752021-07-12 18:05:17 +0900966 goto found;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900967 } else {
968 efi_free_pool(boot_dev);
969 boot_dev = NULL;
970 }
971 }
972 }
973
974skip:
975 /* find active boot device in BootOrder */
976 size = 0;
Simon Glass90975372022-01-23 12:55:12 -0700977 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900978 NULL, &size, NULL, NULL);
979 if (ret == EFI_BUFFER_TOO_SMALL) {
980 boot_order = malloc(size);
981 if (!boot_order) {
982 ret = EFI_OUT_OF_RESOURCES;
983 goto out;
984 }
985
Simon Glass90975372022-01-23 12:55:12 -0700986 ret = efi_get_variable_int(u"BootOrder",
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900987 &efi_global_variable_guid,
988 NULL, &size, boot_order, NULL);
989 }
990 if (ret != EFI_SUCCESS)
991 goto out;
992
993 /* check in higher order */
994 num = size / sizeof(u16);
995 for (i = 0; i < num; i++) {
996 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
997 p = boot_var16;
998 utf8_utf16_strcpy(&p, boot_var);
999 ret = get_dp_device(boot_var16, &boot_dev);
1000 if (ret != EFI_SUCCESS)
1001 continue;
1002
1003 if (device_is_present_and_system_part(boot_dev))
1004 break;
1005
1006 efi_free_pool(boot_dev);
1007 boot_dev = NULL;
1008 }
Masami Hiramatsu10165752021-07-12 18:05:17 +09001009found:
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001010 if (boot_dev) {
Masami Hiramatsud9763bb2021-07-14 14:19:13 +09001011 log_debug("Boot device %pD\n", boot_dev);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001012
1013 volume = efi_fs_from_path(boot_dev);
1014 if (!volume)
1015 ret = EFI_DEVICE_ERROR;
1016 else
1017 ret = EFI_CALL(volume->open_volume(volume,
1018 &bootdev_root));
1019 efi_free_pool(boot_dev);
1020 } else {
1021 ret = EFI_NOT_FOUND;
1022 }
AKASHI Takahirofa390e62021-04-20 10:03:16 +09001023out:
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001024 free(boot_order);
1025
1026 return ret;
1027}
1028
1029/**
1030 * efi_capsule_scan_dir - traverse a capsule directory in boot device
1031 * @files: Array of file names
1032 * @num: Number of elements in @files
1033 *
1034 * Traverse a capsule directory in boot device.
1035 * Called by initialization code, and returns an array of capsule file
1036 * names in @files.
1037 *
1038 * Return: status code
1039 */
1040static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
1041{
1042 struct efi_file_handle *dirh;
1043 struct efi_file_info *dirent;
1044 efi_uintn_t dirent_size, tmp_size;
1045 unsigned int count;
1046 u16 **tmp_files;
1047 efi_status_t ret;
1048
1049 ret = find_boot_device();
1050 if (ret == EFI_NOT_FOUND) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001051 log_debug("Boot device is not set\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001052 *num = 0;
1053 return EFI_SUCCESS;
1054 } else if (ret != EFI_SUCCESS) {
1055 return EFI_DEVICE_ERROR;
1056 }
1057
1058 /* count capsule files */
1059 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1060 EFI_CAPSULE_DIR,
1061 EFI_FILE_MODE_READ, 0));
1062 if (ret != EFI_SUCCESS) {
1063 *num = 0;
1064 return EFI_SUCCESS;
1065 }
1066
1067 dirent_size = 256;
1068 dirent = malloc(dirent_size);
1069 if (!dirent)
1070 return EFI_OUT_OF_RESOURCES;
1071
1072 count = 0;
1073 while (1) {
1074 tmp_size = dirent_size;
1075 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1076 if (ret == EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardtaa27e5d2021-04-11 06:53:04 +02001077 struct efi_file_info *old_dirent = dirent;
1078
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001079 dirent = realloc(dirent, tmp_size);
1080 if (!dirent) {
Heinrich Schuchardtaa27e5d2021-04-11 06:53:04 +02001081 dirent = old_dirent;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001082 ret = EFI_OUT_OF_RESOURCES;
1083 goto err;
1084 }
1085 dirent_size = tmp_size;
1086 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1087 }
1088 if (ret != EFI_SUCCESS)
1089 goto err;
1090 if (!tmp_size)
1091 break;
1092
Heinrich Schuchardt76b708a2021-02-09 17:45:33 +01001093 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001094 count++;
1095 }
1096
1097 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
1098 if (ret != EFI_SUCCESS)
1099 goto err;
1100
1101 /* make a list */
AKASHI Takahiroc8fc12f2021-01-22 10:43:27 +09001102 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001103 if (!tmp_files) {
1104 ret = EFI_OUT_OF_RESOURCES;
1105 goto err;
1106 }
1107
1108 count = 0;
1109 while (1) {
1110 tmp_size = dirent_size;
1111 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1112 if (ret != EFI_SUCCESS)
1113 goto err;
1114 if (!tmp_size)
1115 break;
1116
1117 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
Simon Glass90975372022-01-23 12:55:12 -07001118 u16_strcmp(dirent->file_name, u".") &&
1119 u16_strcmp(dirent->file_name, u".."))
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001120 tmp_files[count++] = u16_strdup(dirent->file_name);
1121 }
1122 /* ignore an error */
1123 EFI_CALL((*dirh->close)(dirh));
1124
Heinrich Schuchardtba0a2ad2022-12-29 14:44:05 +01001125 /*
1126 * Capsule files are applied in case insensitive alphabetic order
1127 *
1128 * TODO: special handling of rightmost period
1129 */
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001130 qsort(tmp_files, count, sizeof(*tmp_files),
Heinrich Schuchardtba0a2ad2022-12-29 14:44:05 +01001131 (int (*)(const void *, const void *))u16_strcasecmp);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001132 *files = tmp_files;
1133 *num = count;
1134 ret = EFI_SUCCESS;
1135err:
1136 free(dirent);
1137
1138 return ret;
1139}
1140
1141/**
1142 * efi_capsule_read_file - read in a capsule file
1143 * @filename: File name
1144 * @capsule: Pointer to buffer for capsule
1145 *
1146 * Read a capsule file and put its content in @capsule.
1147 *
1148 * Return: status code
1149 */
1150static efi_status_t efi_capsule_read_file(const u16 *filename,
1151 struct efi_capsule_header **capsule)
1152{
1153 struct efi_file_handle *dirh, *fh;
1154 struct efi_file_info *file_info = NULL;
1155 struct efi_capsule_header *buf = NULL;
1156 efi_uintn_t size;
1157 efi_status_t ret;
1158
1159 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1160 EFI_CAPSULE_DIR,
1161 EFI_FILE_MODE_READ, 0));
1162 if (ret != EFI_SUCCESS)
1163 return ret;
1164 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1165 EFI_FILE_MODE_READ, 0));
1166 /* ignore an error */
1167 EFI_CALL((*dirh->close)(dirh));
1168 if (ret != EFI_SUCCESS)
1169 return ret;
1170
1171 /* file size */
1172 size = 0;
1173 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1174 &size, file_info));
1175 if (ret == EFI_BUFFER_TOO_SMALL) {
1176 file_info = malloc(size);
1177 if (!file_info) {
1178 ret = EFI_OUT_OF_RESOURCES;
1179 goto err;
1180 }
1181 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1182 &size, file_info));
1183 }
1184 if (ret != EFI_SUCCESS)
1185 goto err;
1186 size = file_info->file_size;
1187 free(file_info);
1188 buf = malloc(size);
1189 if (!buf) {
1190 ret = EFI_OUT_OF_RESOURCES;
1191 goto err;
1192 }
1193
1194 /* fetch data */
1195 ret = EFI_CALL((*fh->read)(fh, &size, buf));
1196 if (ret == EFI_SUCCESS) {
1197 if (size >= buf->capsule_image_size) {
1198 *capsule = buf;
1199 } else {
1200 free(buf);
1201 ret = EFI_INVALID_PARAMETER;
1202 }
1203 } else {
1204 free(buf);
1205 }
1206err:
1207 EFI_CALL((*fh->close)(fh));
1208
1209 return ret;
1210}
1211
1212/**
1213 * efi_capsule_delete_file - delete a capsule file
1214 * @filename: File name
1215 *
1216 * Delete a capsule file from capsule directory.
1217 *
1218 * Return: status code
1219 */
1220static efi_status_t efi_capsule_delete_file(const u16 *filename)
1221{
1222 struct efi_file_handle *dirh, *fh;
1223 efi_status_t ret;
1224
1225 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1226 EFI_CAPSULE_DIR,
1227 EFI_FILE_MODE_READ, 0));
1228 if (ret != EFI_SUCCESS)
1229 return ret;
1230 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1231 EFI_FILE_MODE_READ, 0));
1232 /* ignore an error */
1233 EFI_CALL((*dirh->close)(dirh));
1234
Heinrich Schuchardte5c22812021-06-02 19:28:22 +02001235 if (ret == EFI_SUCCESS)
1236 ret = EFI_CALL((*fh->delete)(fh));
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001237
1238 return ret;
1239}
1240
1241/**
1242 * efi_capsule_scan_done - reset a scan help function
1243 *
1244 * Reset a scan help function
1245 */
1246static void efi_capsule_scan_done(void)
1247{
1248 EFI_CALL((*bootdev_root->close)(bootdev_root));
1249 bootdev_root = NULL;
1250}
1251
1252/**
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001253 * check_run_capsules() - check whether capsule update should run
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001254 *
1255 * The spec says OsIndications must be set in order to run the capsule update
1256 * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to
1257 * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001258 *
1259 * Return: EFI_SUCCESS if update to run, EFI_NOT_FOUND otherwise
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001260 */
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001261static efi_status_t check_run_capsules(void)
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001262{
Sughosh Ganu0f5ca5c2022-06-01 23:30:39 +05301263 u64 os_indications = 0x0;
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001264 efi_uintn_t size;
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001265 efi_status_t r;
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001266
1267 size = sizeof(os_indications);
Simon Glass90975372022-01-23 12:55:12 -07001268 r = efi_get_variable_int(u"OsIndications", &efi_global_variable_guid,
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001269 NULL, &size, &os_indications, NULL);
Sughosh Ganu0f5ca5c2022-06-01 23:30:39 +05301270 if (!IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS) &&
1271 (r != EFI_SUCCESS || size != sizeof(os_indications)))
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001272 return EFI_NOT_FOUND;
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001273
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001274 if (os_indications &
1275 EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
1276 os_indications &=
1277 ~EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
Simon Glass90975372022-01-23 12:55:12 -07001278 r = efi_set_variable_int(u"OsIndications",
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001279 &efi_global_variable_guid,
1280 EFI_VARIABLE_NON_VOLATILE |
1281 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1282 EFI_VARIABLE_RUNTIME_ACCESS,
1283 sizeof(os_indications),
1284 &os_indications, false);
1285 if (r != EFI_SUCCESS)
1286 log_err("Setting %ls failed\n", L"OsIndications");
1287 return EFI_SUCCESS;
1288 } else if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS)) {
1289 return EFI_SUCCESS;
Sughosh Ganu0f5ca5c2022-06-01 23:30:39 +05301290 } else {
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001291 return EFI_NOT_FOUND;
1292 }
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001293}
1294
1295/**
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001296 * efi_launch_capsule - launch capsules
1297 *
1298 * Launch all the capsules in system at boot time.
1299 * Called by efi init code
1300 *
1301 * Return: status codde
1302 */
1303efi_status_t efi_launch_capsules(void)
1304{
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001305 struct efi_capsule_header *capsule = NULL;
1306 u16 **files;
Etienne Carriere6326e912023-02-16 18:21:41 +01001307 unsigned int nfiles, index, index_max, i;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001308 efi_status_t ret;
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301309 bool capsule_update = true;
1310 bool update_status = true;
1311 bool fw_accept_os = false;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001312
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001313 if (check_run_capsules() != EFI_SUCCESS)
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001314 return EFI_SUCCESS;
1315
Etienne Carriere6326e912023-02-16 18:21:41 +01001316 index_max = get_max_capsule();
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001317 index = get_last_capsule();
1318
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001319 /*
1320 * Find capsules on disk.
1321 * All the capsules are collected at the beginning because
1322 * capsule files will be removed instantly.
1323 */
1324 nfiles = 0;
1325 files = NULL;
1326 ret = efi_capsule_scan_dir(&files, &nfiles);
1327 if (ret != EFI_SUCCESS)
1328 return ret;
1329 if (!nfiles)
1330 return EFI_SUCCESS;
1331
1332 /* Launch capsules */
1333 for (i = 0, ++index; i < nfiles; i++, index++) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001334 log_debug("Applying %ls\n", files[i]);
Etienne Carriere6326e912023-02-16 18:21:41 +01001335 if (index > index_max)
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001336 index = 0;
1337 ret = efi_capsule_read_file(files[i], &capsule);
1338 if (ret == EFI_SUCCESS) {
Masami Hiramatsu3454a692022-02-16 15:15:42 +09001339 ret = efi_capsule_update_firmware(capsule);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301340 if (ret != EFI_SUCCESS) {
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001341 log_err("Applying capsule %ls failed.\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001342 files[i]);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301343 update_status = false;
1344 } else {
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001345 log_info("Applying capsule %ls succeeded.\n",
1346 files[i]);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301347 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1348 fwu_post_update_checks(capsule,
1349 &fw_accept_os,
1350 &capsule_update);
1351 }
1352 }
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001353
Masami Hiramatsu1001a102021-11-12 22:05:15 +09001354 /* create CapsuleXXXX */
1355 set_capsule_result(index, capsule, ret);
1356
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001357 free(capsule);
1358 } else {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001359 log_err("Reading capsule %ls failed\n", files[i]);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301360 update_status = false;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001361 }
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001362 /* delete a capsule either in case of success or failure */
1363 ret = efi_capsule_delete_file(files[i]);
1364 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001365 log_err("Deleting capsule %ls failed\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001366 files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001367 }
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301368
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001369 efi_capsule_scan_done();
1370
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301371 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1372 if (capsule_update == true && update_status == true) {
1373 ret = fwu_post_update_process(fw_accept_os);
1374 } else if (capsule_update == true && update_status == false) {
1375 log_err("All capsules were not updated. Not updating FWU metadata\n");
1376 }
1377 }
1378
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001379 for (i = 0; i < nfiles; i++)
1380 free(files[i]);
1381 free(files);
1382
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001383 /*
1384 * UEFI spec requires to reset system after complete processing capsule
1385 * update on the storage.
1386 */
Masami Hiramatsuff744862022-03-21 22:37:56 +09001387 log_info("Reboot after firmware update.\n");
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001388 /* Cold reset is required for loading the new firmware. */
Masami Hiramatsuff744862022-03-21 22:37:56 +09001389 sysreset_walk_halt(SYSRESET_COLD);
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001390 hang();
1391 /* not reach here */
1392
1393 return 0;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001394}
1395#endif /* CONFIG_EFI_CAPSULE_ON_DISK */