blob: 635088f25a13a28284d8daa45fb0b0f48634f448 [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
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530575 /* Obtain the update_index from the platform */
576 status = fwu_plat_get_update_index(&update_index);
577 if (status < 0) {
578 log_err("Failed to get the FWU update_index value\n");
579 return EFI_DEVICE_ERROR;
580 }
581
582 fw_accept_os = capsule_data->flags & FW_ACCEPT_OS ? 0x1 : 0x0;
583 }
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900584
AKASHI Takahiroa8000b92023-07-27 09:38:00 +0900585 if (guidcmp(&capsule_data->capsule_guid,
586 &efi_guid_firmware_management_capsule_id)) {
587 log_err("Unsupported capsule type: %pUs\n",
588 &capsule_data->capsule_guid);
589 return EFI_UNSUPPORTED;
590 }
591
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900592 /* sanity check */
593 if (capsule_data->header_size < sizeof(*capsule) ||
594 capsule_data->header_size >= capsule_data->capsule_image_size)
595 return EFI_INVALID_PARAMETER;
596
597 capsule = (void *)capsule_data + capsule_data->header_size;
598 capsule_size = capsule_data->capsule_image_size
599 - capsule_data->header_size;
600
601 if (capsule->version != 0x00000001)
602 return EFI_UNSUPPORTED;
603
604 handles = NULL;
605 ret = EFI_CALL(efi_locate_handle_buffer(
606 BY_PROTOCOL,
607 &efi_guid_firmware_management_protocol,
608 NULL, &no_handles, (efi_handle_t **)&handles));
609 if (ret != EFI_SUCCESS)
610 return EFI_UNSUPPORTED;
611
612 /* Payload */
613 for (item = capsule->embedded_driver_count;
614 item < capsule->embedded_driver_count
615 + capsule->payload_item_count; item++) {
616 /* sanity check */
617 if ((capsule->item_offset_list[item] + sizeof(*image)
618 >= capsule_size)) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200619 log_err("Capsule does not have enough data\n");
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900620 ret = EFI_INVALID_PARAMETER;
621 goto out;
622 }
623
624 image = (void *)capsule + capsule->item_offset_list[item];
625
626 if (image->version != 0x00000003) {
627 ret = EFI_UNSUPPORTED;
628 goto out;
629 }
630
631 /* find a device for update firmware */
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900632 fmp = efi_fmp_find(&image->update_image_type_id,
Sughosh Ganuf55c6b62022-04-15 11:29:36 +0530633 image->update_image_index,
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900634 image->update_hardware_instance,
635 handles, no_handles);
636 if (!fmp) {
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100637 log_err("FMP driver not found for firmware type %pUs, hardware instance %lld\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900638 &image->update_image_type_id,
639 image->update_hardware_instance);
640 ret = EFI_UNSUPPORTED;
641 goto out;
642 }
643
644 /* do update */
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900645 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
646 !(image->image_capsule_support &
647 CAPSULE_SUPPORT_AUTHENTICATION)) {
648 /* no signature */
649 ret = EFI_SECURITY_VIOLATION;
650 goto out;
651 }
652
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900653 image_binary = (void *)image + sizeof(*image);
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900654 image_binary_size = image->update_image_size;
655 vendor_code = image_binary + image_binary_size;
656 if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
657 (image->image_capsule_support &
658 CAPSULE_SUPPORT_AUTHENTICATION)) {
659 ret = efi_remove_auth_hdr(&image_binary,
660 &image_binary_size);
661 if (ret != EFI_SUCCESS)
662 goto out;
663 }
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900664
665 abort_reason = NULL;
666 ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
667 image_binary,
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900668 image_binary_size,
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900669 vendor_code, NULL,
670 &abort_reason));
671 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200672 log_err("Firmware update failed: %ls\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900673 abort_reason);
674 efi_free_pool(abort_reason);
675 goto out;
676 }
Sughosh Ganu1cadae22022-10-21 18:16:03 +0530677
678 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
679 image_type_id = &image->update_image_type_id;
680 if (!fw_accept_os) {
681 /*
682 * The OS will not be accepting the firmware
683 * images. Set the accept bit of all the
684 * images contained in this capsule.
685 */
686 status = fwu_accept_image(image_type_id,
687 update_index);
688 } else {
689 status = fwu_clear_accept_image(image_type_id,
690 update_index);
691 }
692 ret = fwu_to_efi_error(status);
693 if (ret != EFI_SUCCESS) {
694 log_err("Unable to %s the accept bit for the image %pUs\n",
695 fw_accept_os ? "clear" : "set",
696 image_type_id);
697 goto out;
698 }
699
700 log_debug("%s the accepted bit for Image %pUs\n",
701 fw_accept_os ? "Cleared" : "Set",
702 image_type_id);
703 }
704
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900705 }
706
707out:
708 efi_free_pool(handles);
709
710 return ret;
711}
712#else
713static efi_status_t efi_capsule_update_firmware(
714 struct efi_capsule_header *capsule_data)
715{
716 return EFI_UNSUPPORTED;
717}
718#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
719
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900720/**
721 * efi_update_capsule() - process information from operating system
722 * @capsule_header_array: Array of virtual address pointers
723 * @capsule_count: Number of pointers in capsule_header_array
724 * @scatter_gather_list: Array of physical address pointers
725 *
726 * This function implements the UpdateCapsule() runtime service.
727 *
728 * See the Unified Extensible Firmware Interface (UEFI) specification for
729 * details.
730 *
731 * Return: status code
732 */
733efi_status_t EFIAPI efi_update_capsule(
734 struct efi_capsule_header **capsule_header_array,
735 efi_uintn_t capsule_count,
736 u64 scatter_gather_list)
737{
738 struct efi_capsule_header *capsule;
739 unsigned int i;
740 efi_status_t ret;
741
Simon Glass83698b22021-02-07 14:27:02 -0700742 EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900743 scatter_gather_list);
744
745 if (!capsule_count) {
746 ret = EFI_INVALID_PARAMETER;
747 goto out;
748 }
749
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900750 ret = EFI_SUCCESS;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900751 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
752 i++, capsule = *(++capsule_header_array)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900753 /* sanity check */
754 if (capsule->header_size < sizeof(*capsule) ||
755 capsule->capsule_image_size < sizeof(*capsule)) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200756 log_err("Capsule does not have enough data\n");
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900757 continue;
758 }
759
Heinrich Schuchardt282249d2022-01-16 14:15:31 +0100760 log_debug("Capsule[%d] (guid:%pUs)\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900761 i, &capsule->capsule_guid);
AKASHI Takahiroa8000b92023-07-27 09:38:00 +0900762 ret = efi_capsule_update_firmware(capsule);
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900763 if (ret != EFI_SUCCESS)
764 goto out;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900765 }
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000766
767 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
768 /* Rebuild the ESRT to reflect any updated FW images. */
769 ret = efi_esrt_populate();
770 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200771 log_warning("ESRT update failed\n");
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000772 }
Jose Marinhoaf886ce2021-04-19 14:54:33 +0100773out:
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000774
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900775 return EFI_EXIT(ret);
776}
777
778/**
779 * efi_query_capsule_caps() - check if capsule is supported
780 * @capsule_header_array: Array of virtual pointers
781 * @capsule_count: Number of pointers in capsule_header_array
782 * @maximum_capsule_size: Maximum capsule size
783 * @reset_type: Type of reset needed for capsule update
784 *
785 * This function implements the QueryCapsuleCapabilities() runtime service.
786 *
787 * See the Unified Extensible Firmware Interface (UEFI) specification for
788 * details.
789 *
790 * Return: status code
791 */
792efi_status_t EFIAPI efi_query_capsule_caps(
793 struct efi_capsule_header **capsule_header_array,
794 efi_uintn_t capsule_count,
795 u64 *maximum_capsule_size,
796 u32 *reset_type)
797{
798 struct efi_capsule_header *capsule __attribute__((unused));
799 unsigned int i;
800 efi_status_t ret;
801
Simon Glass83698b22021-02-07 14:27:02 -0700802 EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900803 maximum_capsule_size, reset_type);
804
805 if (!maximum_capsule_size) {
806 ret = EFI_INVALID_PARAMETER;
807 goto out;
808 }
809
810 *maximum_capsule_size = U64_MAX;
811 *reset_type = EFI_RESET_COLD;
812
813 ret = EFI_SUCCESS;
814 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
815 i++, capsule = *(++capsule_header_array)) {
816 /* TODO */
817 }
818out:
819 return EFI_EXIT(ret);
820}
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900821
Masami Hiramatsud306caa2022-03-21 22:37:45 +0900822/**
823 * efi_load_capsule_drivers - initialize capsule drivers
824 *
825 * Generic FMP drivers backed by DFU
826 *
827 * Return: status code
828 */
829efi_status_t __weak efi_load_capsule_drivers(void)
830{
831 __maybe_unused efi_handle_t handle;
832 efi_status_t ret = EFI_SUCCESS;
833
834 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
835 handle = NULL;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +0300836 ret = efi_install_multiple_protocol_interfaces(&handle,
837 &efi_guid_firmware_management_protocol,
838 &efi_fmp_fit,
839 NULL);
Masami Hiramatsud306caa2022-03-21 22:37:45 +0900840 }
841
842 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
843 handle = NULL;
Ilias Apalodimas8ac0ebe2022-10-06 16:08:46 +0300844 ret = efi_install_multiple_protocol_interfaces(&handle,
845 &efi_guid_firmware_management_protocol,
846 &efi_fmp_raw,
847 NULL);
Masami Hiramatsud306caa2022-03-21 22:37:45 +0900848 }
849
850 return ret;
851}
852
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900853#ifdef CONFIG_EFI_CAPSULE_ON_DISK
854/**
855 * get_dp_device - retrieve a device path from boot variable
856 * @boot_var: Boot variable name
857 * @device_dp Device path
858 *
859 * Retrieve a device patch from boot variable, @boot_var.
860 *
861 * Return: status code
862 */
863static efi_status_t get_dp_device(u16 *boot_var,
864 struct efi_device_path **device_dp)
865{
866 void *buf = NULL;
867 efi_uintn_t size;
868 struct efi_load_option lo;
869 struct efi_device_path *file_dp;
870 efi_status_t ret;
871
872 size = 0;
873 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
874 NULL, &size, NULL, NULL);
875 if (ret == EFI_BUFFER_TOO_SMALL) {
876 buf = malloc(size);
877 if (!buf)
878 return EFI_OUT_OF_RESOURCES;
879 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
880 NULL, &size, buf, NULL);
881 }
882 if (ret != EFI_SUCCESS)
883 return ret;
884
885 efi_deserialize_load_option(&lo, buf, &size);
886
887 if (lo.attributes & LOAD_OPTION_ACTIVE) {
888 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
889 efi_free_pool(file_dp);
890
891 ret = EFI_SUCCESS;
892 } else {
893 ret = EFI_NOT_FOUND;
894 }
895
896 free(buf);
897
898 return ret;
899}
900
901/**
902 * device_is_present_and_system_part - check if a device exists
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900903 *
904 * Check if a device pointed to by the device path, @dp, exists and is
905 * located in UEFI system partition.
906 *
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100907 * @dp device path
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900908 * Return: true - yes, false - no
909 */
910static bool device_is_present_and_system_part(struct efi_device_path *dp)
911{
912 efi_handle_t handle;
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100913 struct efi_device_path *rem;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900914
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100915 /* Check device exists */
Heinrich Schuchardt0a04a412022-03-19 06:35:43 +0100916 handle = efi_dp_find_obj(dp, NULL, NULL);
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900917 if (!handle)
918 return false;
919
Heinrich Schuchardta76fc032022-03-05 00:36:50 +0100920 /* Check device is on system partition */
921 handle = efi_dp_find_obj(dp, &efi_system_partition_guid, &rem);
922 if (!handle)
923 return false;
924
925 return true;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900926}
927
928/**
929 * find_boot_device - identify the boot device
930 *
931 * Identify the boot device from boot-related variables as UEFI
932 * specification describes and put its handle into bootdev_root.
933 *
934 * Return: status code
935 */
936static efi_status_t find_boot_device(void)
937{
938 char boot_var[9];
939 u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
940 efi_uintn_t size;
941 int i, num;
942 struct efi_simple_file_system_protocol *volume;
943 struct efi_device_path *boot_dev = NULL;
944 efi_status_t ret;
945
946 /* find active boot device in BootNext */
947 bootnext = 0;
948 size = sizeof(bootnext);
Simon Glass90975372022-01-23 12:55:12 -0700949 ret = efi_get_variable_int(u"BootNext",
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900950 (efi_guid_t *)&efi_global_variable_guid,
951 NULL, &size, &bootnext, NULL);
952 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
953 /* BootNext does exist here */
954 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900955 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900956 goto skip;
957 }
958 sprintf((char *)boot_var, "Boot%04X", bootnext);
959 p = boot_var16;
960 utf8_utf16_strcpy(&p, boot_var);
961
962 ret = get_dp_device(boot_var16, &boot_dev);
963 if (ret == EFI_SUCCESS) {
964 if (device_is_present_and_system_part(boot_dev)) {
Masami Hiramatsu10165752021-07-12 18:05:17 +0900965 goto found;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900966 } else {
967 efi_free_pool(boot_dev);
968 boot_dev = NULL;
969 }
970 }
971 }
972
973skip:
974 /* find active boot device in BootOrder */
975 size = 0;
Simon Glass90975372022-01-23 12:55:12 -0700976 ret = efi_get_variable_int(u"BootOrder", &efi_global_variable_guid,
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900977 NULL, &size, NULL, NULL);
978 if (ret == EFI_BUFFER_TOO_SMALL) {
979 boot_order = malloc(size);
980 if (!boot_order) {
981 ret = EFI_OUT_OF_RESOURCES;
982 goto out;
983 }
984
Simon Glass90975372022-01-23 12:55:12 -0700985 ret = efi_get_variable_int(u"BootOrder",
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900986 &efi_global_variable_guid,
987 NULL, &size, boot_order, NULL);
988 }
989 if (ret != EFI_SUCCESS)
990 goto out;
991
992 /* check in higher order */
993 num = size / sizeof(u16);
994 for (i = 0; i < num; i++) {
995 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
996 p = boot_var16;
997 utf8_utf16_strcpy(&p, boot_var);
998 ret = get_dp_device(boot_var16, &boot_dev);
999 if (ret != EFI_SUCCESS)
1000 continue;
1001
1002 if (device_is_present_and_system_part(boot_dev))
1003 break;
1004
1005 efi_free_pool(boot_dev);
1006 boot_dev = NULL;
1007 }
Masami Hiramatsu10165752021-07-12 18:05:17 +09001008found:
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001009 if (boot_dev) {
Masami Hiramatsud9763bb2021-07-14 14:19:13 +09001010 log_debug("Boot device %pD\n", boot_dev);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001011
1012 volume = efi_fs_from_path(boot_dev);
1013 if (!volume)
1014 ret = EFI_DEVICE_ERROR;
1015 else
1016 ret = EFI_CALL(volume->open_volume(volume,
1017 &bootdev_root));
1018 efi_free_pool(boot_dev);
1019 } else {
1020 ret = EFI_NOT_FOUND;
1021 }
AKASHI Takahirofa390e62021-04-20 10:03:16 +09001022out:
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001023 free(boot_order);
1024
1025 return ret;
1026}
1027
1028/**
1029 * efi_capsule_scan_dir - traverse a capsule directory in boot device
1030 * @files: Array of file names
1031 * @num: Number of elements in @files
1032 *
1033 * Traverse a capsule directory in boot device.
1034 * Called by initialization code, and returns an array of capsule file
1035 * names in @files.
1036 *
1037 * Return: status code
1038 */
1039static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
1040{
1041 struct efi_file_handle *dirh;
1042 struct efi_file_info *dirent;
1043 efi_uintn_t dirent_size, tmp_size;
1044 unsigned int count;
1045 u16 **tmp_files;
1046 efi_status_t ret;
1047
1048 ret = find_boot_device();
1049 if (ret == EFI_NOT_FOUND) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001050 log_debug("Boot device is not set\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001051 *num = 0;
1052 return EFI_SUCCESS;
1053 } else if (ret != EFI_SUCCESS) {
1054 return EFI_DEVICE_ERROR;
1055 }
1056
1057 /* count capsule files */
1058 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1059 EFI_CAPSULE_DIR,
1060 EFI_FILE_MODE_READ, 0));
1061 if (ret != EFI_SUCCESS) {
1062 *num = 0;
1063 return EFI_SUCCESS;
1064 }
1065
1066 dirent_size = 256;
1067 dirent = malloc(dirent_size);
1068 if (!dirent)
1069 return EFI_OUT_OF_RESOURCES;
1070
1071 count = 0;
1072 while (1) {
1073 tmp_size = dirent_size;
1074 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1075 if (ret == EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardtaa27e5d2021-04-11 06:53:04 +02001076 struct efi_file_info *old_dirent = dirent;
1077
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001078 dirent = realloc(dirent, tmp_size);
1079 if (!dirent) {
Heinrich Schuchardtaa27e5d2021-04-11 06:53:04 +02001080 dirent = old_dirent;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001081 ret = EFI_OUT_OF_RESOURCES;
1082 goto err;
1083 }
1084 dirent_size = tmp_size;
1085 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1086 }
1087 if (ret != EFI_SUCCESS)
1088 goto err;
1089 if (!tmp_size)
1090 break;
1091
Heinrich Schuchardt76b708a2021-02-09 17:45:33 +01001092 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001093 count++;
1094 }
1095
1096 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
1097 if (ret != EFI_SUCCESS)
1098 goto err;
1099
1100 /* make a list */
AKASHI Takahiroc8fc12f2021-01-22 10:43:27 +09001101 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001102 if (!tmp_files) {
1103 ret = EFI_OUT_OF_RESOURCES;
1104 goto err;
1105 }
1106
1107 count = 0;
1108 while (1) {
1109 tmp_size = dirent_size;
1110 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
1111 if (ret != EFI_SUCCESS)
1112 goto err;
1113 if (!tmp_size)
1114 break;
1115
1116 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
Simon Glass90975372022-01-23 12:55:12 -07001117 u16_strcmp(dirent->file_name, u".") &&
1118 u16_strcmp(dirent->file_name, u".."))
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001119 tmp_files[count++] = u16_strdup(dirent->file_name);
1120 }
1121 /* ignore an error */
1122 EFI_CALL((*dirh->close)(dirh));
1123
Heinrich Schuchardtba0a2ad2022-12-29 14:44:05 +01001124 /*
1125 * Capsule files are applied in case insensitive alphabetic order
1126 *
1127 * TODO: special handling of rightmost period
1128 */
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001129 qsort(tmp_files, count, sizeof(*tmp_files),
Heinrich Schuchardtba0a2ad2022-12-29 14:44:05 +01001130 (int (*)(const void *, const void *))u16_strcasecmp);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001131 *files = tmp_files;
1132 *num = count;
1133 ret = EFI_SUCCESS;
1134err:
1135 free(dirent);
1136
1137 return ret;
1138}
1139
1140/**
1141 * efi_capsule_read_file - read in a capsule file
1142 * @filename: File name
1143 * @capsule: Pointer to buffer for capsule
1144 *
1145 * Read a capsule file and put its content in @capsule.
1146 *
1147 * Return: status code
1148 */
1149static efi_status_t efi_capsule_read_file(const u16 *filename,
1150 struct efi_capsule_header **capsule)
1151{
1152 struct efi_file_handle *dirh, *fh;
1153 struct efi_file_info *file_info = NULL;
1154 struct efi_capsule_header *buf = NULL;
1155 efi_uintn_t size;
1156 efi_status_t ret;
1157
1158 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1159 EFI_CAPSULE_DIR,
1160 EFI_FILE_MODE_READ, 0));
1161 if (ret != EFI_SUCCESS)
1162 return ret;
1163 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1164 EFI_FILE_MODE_READ, 0));
1165 /* ignore an error */
1166 EFI_CALL((*dirh->close)(dirh));
1167 if (ret != EFI_SUCCESS)
1168 return ret;
1169
1170 /* file size */
1171 size = 0;
1172 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1173 &size, file_info));
1174 if (ret == EFI_BUFFER_TOO_SMALL) {
1175 file_info = malloc(size);
1176 if (!file_info) {
1177 ret = EFI_OUT_OF_RESOURCES;
1178 goto err;
1179 }
1180 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
1181 &size, file_info));
1182 }
1183 if (ret != EFI_SUCCESS)
1184 goto err;
1185 size = file_info->file_size;
1186 free(file_info);
1187 buf = malloc(size);
1188 if (!buf) {
1189 ret = EFI_OUT_OF_RESOURCES;
1190 goto err;
1191 }
1192
1193 /* fetch data */
1194 ret = EFI_CALL((*fh->read)(fh, &size, buf));
1195 if (ret == EFI_SUCCESS) {
1196 if (size >= buf->capsule_image_size) {
1197 *capsule = buf;
1198 } else {
1199 free(buf);
1200 ret = EFI_INVALID_PARAMETER;
1201 }
1202 } else {
1203 free(buf);
1204 }
1205err:
1206 EFI_CALL((*fh->close)(fh));
1207
1208 return ret;
1209}
1210
1211/**
1212 * efi_capsule_delete_file - delete a capsule file
1213 * @filename: File name
1214 *
1215 * Delete a capsule file from capsule directory.
1216 *
1217 * Return: status code
1218 */
1219static efi_status_t efi_capsule_delete_file(const u16 *filename)
1220{
1221 struct efi_file_handle *dirh, *fh;
1222 efi_status_t ret;
1223
1224 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
1225 EFI_CAPSULE_DIR,
1226 EFI_FILE_MODE_READ, 0));
1227 if (ret != EFI_SUCCESS)
1228 return ret;
1229 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
1230 EFI_FILE_MODE_READ, 0));
1231 /* ignore an error */
1232 EFI_CALL((*dirh->close)(dirh));
1233
Heinrich Schuchardte5c22812021-06-02 19:28:22 +02001234 if (ret == EFI_SUCCESS)
1235 ret = EFI_CALL((*fh->delete)(fh));
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001236
1237 return ret;
1238}
1239
1240/**
1241 * efi_capsule_scan_done - reset a scan help function
1242 *
1243 * Reset a scan help function
1244 */
1245static void efi_capsule_scan_done(void)
1246{
1247 EFI_CALL((*bootdev_root->close)(bootdev_root));
1248 bootdev_root = NULL;
1249}
1250
1251/**
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001252 * check_run_capsules() - check whether capsule update should run
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001253 *
1254 * The spec says OsIndications must be set in order to run the capsule update
1255 * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to
1256 * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001257 *
1258 * Return: EFI_SUCCESS if update to run, EFI_NOT_FOUND otherwise
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001259 */
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001260static efi_status_t check_run_capsules(void)
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001261{
Sughosh Ganu0f5ca5c2022-06-01 23:30:39 +05301262 u64 os_indications = 0x0;
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001263 efi_uintn_t size;
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001264 efi_status_t r;
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001265
1266 size = sizeof(os_indications);
Simon Glass90975372022-01-23 12:55:12 -07001267 r = efi_get_variable_int(u"OsIndications", &efi_global_variable_guid,
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001268 NULL, &size, &os_indications, NULL);
Sughosh Ganu0f5ca5c2022-06-01 23:30:39 +05301269 if (!IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS) &&
1270 (r != EFI_SUCCESS || size != sizeof(os_indications)))
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001271 return EFI_NOT_FOUND;
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001272
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001273 if (os_indications &
1274 EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED) {
1275 os_indications &=
1276 ~EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED;
Simon Glass90975372022-01-23 12:55:12 -07001277 r = efi_set_variable_int(u"OsIndications",
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001278 &efi_global_variable_guid,
1279 EFI_VARIABLE_NON_VOLATILE |
1280 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1281 EFI_VARIABLE_RUNTIME_ACCESS,
1282 sizeof(os_indications),
1283 &os_indications, false);
1284 if (r != EFI_SUCCESS)
1285 log_err("Setting %ls failed\n", L"OsIndications");
1286 return EFI_SUCCESS;
1287 } else if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS)) {
1288 return EFI_SUCCESS;
Sughosh Ganu0f5ca5c2022-06-01 23:30:39 +05301289 } else {
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001290 return EFI_NOT_FOUND;
1291 }
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001292}
1293
1294/**
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001295 * efi_launch_capsule - launch capsules
1296 *
1297 * Launch all the capsules in system at boot time.
1298 * Called by efi init code
1299 *
1300 * Return: status codde
1301 */
1302efi_status_t efi_launch_capsules(void)
1303{
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001304 struct efi_capsule_header *capsule = NULL;
1305 u16 **files;
Etienne Carriere6326e912023-02-16 18:21:41 +01001306 unsigned int nfiles, index, index_max, i;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001307 efi_status_t ret;
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301308 bool capsule_update = true;
1309 bool update_status = true;
1310 bool fw_accept_os = false;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001311
Heinrich Schuchardte9e84992021-11-20 11:53:12 +01001312 if (check_run_capsules() != EFI_SUCCESS)
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001313 return EFI_SUCCESS;
1314
Etienne Carriere6326e912023-02-16 18:21:41 +01001315 index_max = get_max_capsule();
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001316 index = get_last_capsule();
1317
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001318 /*
1319 * Find capsules on disk.
1320 * All the capsules are collected at the beginning because
1321 * capsule files will be removed instantly.
1322 */
1323 nfiles = 0;
1324 files = NULL;
1325 ret = efi_capsule_scan_dir(&files, &nfiles);
1326 if (ret != EFI_SUCCESS)
1327 return ret;
1328 if (!nfiles)
1329 return EFI_SUCCESS;
1330
1331 /* Launch capsules */
1332 for (i = 0, ++index; i < nfiles; i++, index++) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001333 log_debug("Applying %ls\n", files[i]);
Etienne Carriere6326e912023-02-16 18:21:41 +01001334 if (index > index_max)
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001335 index = 0;
1336 ret = efi_capsule_read_file(files[i], &capsule);
1337 if (ret == EFI_SUCCESS) {
Masami Hiramatsu3454a692022-02-16 15:15:42 +09001338 ret = efi_capsule_update_firmware(capsule);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301339 if (ret != EFI_SUCCESS) {
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001340 log_err("Applying capsule %ls failed.\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001341 files[i]);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301342 update_status = false;
1343 } else {
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001344 log_info("Applying capsule %ls succeeded.\n",
1345 files[i]);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301346 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1347 fwu_post_update_checks(capsule,
1348 &fw_accept_os,
1349 &capsule_update);
1350 }
1351 }
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001352
Masami Hiramatsu1001a102021-11-12 22:05:15 +09001353 /* create CapsuleXXXX */
1354 set_capsule_result(index, capsule, ret);
1355
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001356 free(capsule);
1357 } else {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001358 log_err("Reading capsule %ls failed\n", files[i]);
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301359 update_status = false;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001360 }
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001361 /* delete a capsule either in case of success or failure */
1362 ret = efi_capsule_delete_file(files[i]);
1363 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001364 log_err("Deleting capsule %ls failed\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001365 files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001366 }
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301367
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001368 efi_capsule_scan_done();
1369
Sughosh Ganu1cadae22022-10-21 18:16:03 +05301370 if (IS_ENABLED(CONFIG_FWU_MULTI_BANK_UPDATE)) {
1371 if (capsule_update == true && update_status == true) {
1372 ret = fwu_post_update_process(fw_accept_os);
1373 } else if (capsule_update == true && update_status == false) {
1374 log_err("All capsules were not updated. Not updating FWU metadata\n");
1375 }
1376 }
1377
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001378 for (i = 0; i < nfiles; i++)
1379 free(files[i]);
1380 free(files);
1381
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001382 /*
1383 * UEFI spec requires to reset system after complete processing capsule
1384 * update on the storage.
1385 */
Masami Hiramatsuff744862022-03-21 22:37:56 +09001386 log_info("Reboot after firmware update.\n");
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001387 /* Cold reset is required for loading the new firmware. */
Masami Hiramatsuff744862022-03-21 22:37:56 +09001388 sysreset_walk_halt(SYSRESET_COLD);
Masami Hiramatsu4b2f8c12022-02-16 15:16:12 +09001389 hang();
1390 /* not reach here */
1391
1392 return 0;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001393}
1394#endif /* CONFIG_EFI_CAPSULE_ON_DISK */