blob: b75e4bcba1a9821b579bc606ed64ef3dda887f9e [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 <common.h>
12#include <efi_loader.h>
13#include <efi_variable.h>
14#include <fs.h>
15#include <malloc.h>
AKASHI Takahiro45b819542020-11-17 09:27:56 +090016#include <mapmem.h>
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090017#include <sort.h>
18
Sughosh Ganu586bb982020-12-30 19:27:09 +053019#include <crypto/pkcs7.h>
20#include <crypto/pkcs7_parser.h>
21#include <linux/err.h>
22
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090023const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
AKASHI Takahiro0d963782020-11-30 18:12:11 +090024static const efi_guid_t efi_guid_firmware_management_capsule_id =
25 EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
26const efi_guid_t efi_guid_firmware_management_protocol =
27 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090028
AKASHI Takahiro45b819542020-11-17 09:27:56 +090029#ifdef CONFIG_EFI_CAPSULE_ON_DISK
30/* for file system access */
31static struct efi_file_handle *bootdev_root;
32#endif
33
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090034/**
35 * get_last_capsule - get the last capsule index
36 *
37 * Retrieve the index of the capsule invoked last time from "CapsuleLast"
38 * variable.
39 *
40 * Return:
41 * * > 0 - the last capsule index invoked
42 * * 0xffff - on error, or no capsule invoked yet
43 */
44static __maybe_unused unsigned int get_last_capsule(void)
45{
46 u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010047 char value[5];
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090048 efi_uintn_t size;
49 unsigned long index = 0xffff;
50 efi_status_t ret;
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010051 int i;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090052
53 size = sizeof(value16);
54 ret = efi_get_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
55 NULL, &size, value16, NULL);
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010056 if (ret != EFI_SUCCESS || size != 22 ||
57 u16_strncmp(value16, L"Capsule", 7))
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090058 goto err;
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010059 for (i = 0; i < 4; ++i) {
60 u16 c = value16[i + 7];
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090061
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010062 if (!c || c > 0x7f)
63 goto err;
64 value[i] = c;
65 }
66 value[4] = 0;
67 if (strict_strtoul(value, 16, &index))
68 index = 0xffff;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090069err:
70 return index;
71}
72
73/**
74 * set_capsule_result - set a result variable
75 * @capsule: Capsule
76 * @return_status: Return status
77 *
78 * Create and set a result variable, "CapsuleXXXX", for the capsule,
79 * @capsule.
80 */
81static __maybe_unused
82void set_capsule_result(int index, struct efi_capsule_header *capsule,
83 efi_status_t return_status)
84{
85 u16 variable_name16[12];
86 struct efi_capsule_result_variable_header result;
87 struct efi_time time;
88 efi_status_t ret;
89
Ilias Apalodimas21575292020-12-31 12:26:46 +020090 efi_create_indexed_name(variable_name16, sizeof(variable_name16),
91 "Capsule", index);
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090092 result.variable_total_size = sizeof(result);
93 result.capsule_guid = capsule->capsule_guid;
94 ret = EFI_CALL((*efi_runtime_services.get_time)(&time, NULL));
95 if (ret == EFI_SUCCESS)
96 memcpy(&result.capsule_processed, &time, sizeof(time));
97 else
98 memset(&result.capsule_processed, 0, sizeof(time));
99 result.capsule_status = return_status;
Heinrich Schuchardt24adaa72021-07-10 11:10:26 +0200100 ret = efi_set_variable_int(variable_name16, &efi_guid_capsule_report,
101 EFI_VARIABLE_NON_VOLATILE |
102 EFI_VARIABLE_BOOTSERVICE_ACCESS |
103 EFI_VARIABLE_RUNTIME_ACCESS,
104 sizeof(result), &result, false);
Heinrich Schuchardtb1fae8c2021-07-10 11:14:13 +0200105 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200106 log_err("Setting %ls failed\n", variable_name16);
Heinrich Schuchardtb1fae8c2021-07-10 11:14:13 +0200107 return;
108 }
109
110 /* Variable CapsuleLast must not include terminating 0x0000 */
111 ret = efi_set_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
112 EFI_VARIABLE_READ_ONLY |
113 EFI_VARIABLE_NON_VOLATILE |
114 EFI_VARIABLE_BOOTSERVICE_ACCESS |
115 EFI_VARIABLE_RUNTIME_ACCESS,
116 22, variable_name16, false);
117 if (ret != EFI_SUCCESS)
118 log_err("Setting %ls failed\n", L"CapsuleLast");
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900119}
120
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900121#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT
122/**
123 * efi_fmp_find - search for Firmware Management Protocol drivers
124 * @image_type: Image type guid
125 * @instance: Instance number
126 * @handles: Handles of FMP drivers
127 * @no_handles: Number of handles
128 *
129 * Search for Firmware Management Protocol drivers, matching the image
130 * type, @image_type and the machine instance, @instance, from the list,
131 * @handles.
132 *
133 * Return:
134 * * Protocol instance - on success
135 * * NULL - on failure
136 */
137static struct efi_firmware_management_protocol *
138efi_fmp_find(efi_guid_t *image_type, u64 instance, efi_handle_t *handles,
139 efi_uintn_t no_handles)
140{
141 efi_handle_t *handle;
142 struct efi_firmware_management_protocol *fmp;
143 struct efi_firmware_image_descriptor *image_info, *desc;
144 efi_uintn_t info_size, descriptor_size;
145 u32 descriptor_version;
146 u8 descriptor_count;
147 u32 package_version;
148 u16 *package_version_name;
149 bool found = false;
150 int i, j;
151 efi_status_t ret;
152
153 for (i = 0, handle = handles; i < no_handles; i++, handle++) {
154 ret = EFI_CALL(efi_handle_protocol(
155 *handle,
156 &efi_guid_firmware_management_protocol,
157 (void **)&fmp));
158 if (ret != EFI_SUCCESS)
159 continue;
160
161 /* get device's image info */
162 info_size = 0;
163 image_info = NULL;
164 descriptor_version = 0;
165 descriptor_count = 0;
166 descriptor_size = 0;
167 package_version = 0;
168 package_version_name = NULL;
169 ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
170 image_info,
171 &descriptor_version,
172 &descriptor_count,
173 &descriptor_size,
174 &package_version,
175 &package_version_name));
176 if (ret != EFI_BUFFER_TOO_SMALL)
177 goto skip;
178
179 image_info = malloc(info_size);
180 if (!image_info)
181 goto skip;
182
183 ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
184 image_info,
185 &descriptor_version,
186 &descriptor_count,
187 &descriptor_size,
188 &package_version,
189 &package_version_name));
190 if (ret != EFI_SUCCESS ||
191 descriptor_version != EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION)
192 goto skip;
193
194 /* matching */
195 for (j = 0, desc = image_info; j < descriptor_count;
196 j++, desc = (void *)desc + descriptor_size) {
197 log_debug("+++ desc[%d] index: %d, name: %ls\n",
198 j, desc->image_index, desc->image_id_name);
199 if (!guidcmp(&desc->image_type_id, image_type) &&
200 (!instance ||
201 !desc->hardware_instance ||
202 desc->hardware_instance == instance))
203 found = true;
204 }
205
206skip:
207 efi_free_pool(package_version_name);
208 free(image_info);
209 EFI_CALL(efi_close_protocol(
210 (efi_handle_t)fmp,
211 &efi_guid_firmware_management_protocol,
212 NULL, NULL));
213 if (found)
214 return fmp;
215 }
216
217 return NULL;
218}
219
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900220/**
221 * efi_remove_auth_hdr - remove authentication data from image
222 * @image: Pointer to pointer to Image
223 * @image_size: Pointer to Image size
224 *
225 * Remove the authentication data from image if possible.
226 * Update @image and @image_size.
227 *
228 * Return: status code
229 */
230static efi_status_t efi_remove_auth_hdr(void **image, efi_uintn_t *image_size)
231{
232 struct efi_firmware_image_authentication *auth_hdr;
233 efi_status_t ret = EFI_INVALID_PARAMETER;
234
235 auth_hdr = (struct efi_firmware_image_authentication *)*image;
236 if (*image_size < sizeof(*auth_hdr))
237 goto out;
238
239 if (auth_hdr->auth_info.hdr.dwLength <=
240 offsetof(struct win_certificate_uefi_guid, cert_data))
241 goto out;
242
243 *image = (uint8_t *)*image + sizeof(auth_hdr->monotonic_count) +
244 auth_hdr->auth_info.hdr.dwLength;
245 *image_size = *image_size - auth_hdr->auth_info.hdr.dwLength -
246 sizeof(auth_hdr->monotonic_count);
247
248 ret = EFI_SUCCESS;
249out:
250 return ret;
251}
252
Sughosh Ganu586bb982020-12-30 19:27:09 +0530253#if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
254
Sughosh Ganu586bb982020-12-30 19:27:09 +0530255efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
256 void **image, efi_uintn_t *image_size)
257{
258 u8 *buf;
259 int ret;
Simon Glass1f78c122021-08-02 08:44:31 -0600260 void *fdt_pkey, *pkey;
Sughosh Ganu586bb982020-12-30 19:27:09 +0530261 efi_uintn_t pkey_len;
262 uint64_t monotonic_count;
263 struct efi_signature_store *truststore;
264 struct pkcs7_message *capsule_sig;
265 struct efi_image_regions *regs;
266 struct efi_firmware_image_authentication *auth_hdr;
267 efi_status_t status;
268
269 status = EFI_SECURITY_VIOLATION;
270 capsule_sig = NULL;
271 truststore = NULL;
272 regs = NULL;
273
274 /* Sanity checks */
275 if (capsule == NULL || capsule_size == 0)
276 goto out;
277
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900278 *image = (uint8_t *)capsule;
279 *image_size = capsule_size;
280 if (efi_remove_auth_hdr(image, image_size) != EFI_SUCCESS)
Sughosh Ganu586bb982020-12-30 19:27:09 +0530281 goto out;
282
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900283 auth_hdr = (struct efi_firmware_image_authentication *)capsule;
Sughosh Ganu586bb982020-12-30 19:27:09 +0530284 if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
285 goto out;
286
Sughosh Ganu586bb982020-12-30 19:27:09 +0530287 memcpy(&monotonic_count, &auth_hdr->monotonic_count,
288 sizeof(monotonic_count));
289
290 /* data to be digested */
291 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 2, 1);
292 if (!regs)
293 goto out;
294
295 regs->max = 2;
296 efi_image_region_add(regs, (uint8_t *)*image,
297 (uint8_t *)*image + *image_size, 1);
298
299 efi_image_region_add(regs, (uint8_t *)&monotonic_count,
300 (uint8_t *)&monotonic_count + sizeof(monotonic_count),
301 1);
302
303 capsule_sig = efi_parse_pkcs7_header(auth_hdr->auth_info.cert_data,
304 auth_hdr->auth_info.hdr.dwLength
305 - sizeof(auth_hdr->auth_info),
306 &buf);
307 if (IS_ERR(capsule_sig)) {
308 debug("Parsing variable's pkcs7 header failed\n");
309 capsule_sig = NULL;
310 goto out;
311 }
312
Simon Glass1f78c122021-08-02 08:44:31 -0600313 ret = efi_get_public_key_data(&fdt_pkey, &pkey_len);
Sughosh Ganu586bb982020-12-30 19:27:09 +0530314 if (ret < 0)
315 goto out;
316
317 pkey = malloc(pkey_len);
318 if (!pkey)
319 goto out;
320
Simon Glass1f78c122021-08-02 08:44:31 -0600321 memcpy(pkey, fdt_pkey, pkey_len);
Sughosh Ganu586bb982020-12-30 19:27:09 +0530322 truststore = efi_build_signature_store(pkey, pkey_len);
323 if (!truststore)
324 goto out;
325
326 /* verify signature */
327 if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) {
328 debug("Verified\n");
329 } else {
330 debug("Verifying variable's signature failed\n");
331 goto out;
332 }
333
334 status = EFI_SUCCESS;
335
336out:
337 efi_sigstore_free(truststore);
338 pkcs7_free_message(capsule_sig);
339 free(regs);
340
341 return status;
342}
343#else
344efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
345 void **image, efi_uintn_t *image_size)
346{
347 return EFI_UNSUPPORTED;
348}
349#endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */
350
351
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900352/**
353 * efi_capsule_update_firmware - update firmware from capsule
354 * @capsule_data: Capsule
355 *
356 * Update firmware, using a capsule, @capsule_data. Loading any FMP
357 * drivers embedded in a capsule is not supported.
358 *
359 * Return: status code
360 */
361static efi_status_t efi_capsule_update_firmware(
362 struct efi_capsule_header *capsule_data)
363{
364 struct efi_firmware_management_capsule_header *capsule;
365 struct efi_firmware_management_capsule_image_header *image;
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900366 size_t capsule_size, image_binary_size;
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900367 void *image_binary, *vendor_code;
368 efi_handle_t *handles;
369 efi_uintn_t no_handles;
370 int item;
371 struct efi_firmware_management_protocol *fmp;
372 u16 *abort_reason;
373 efi_status_t ret = EFI_SUCCESS;
374
375 /* sanity check */
376 if (capsule_data->header_size < sizeof(*capsule) ||
377 capsule_data->header_size >= capsule_data->capsule_image_size)
378 return EFI_INVALID_PARAMETER;
379
380 capsule = (void *)capsule_data + capsule_data->header_size;
381 capsule_size = capsule_data->capsule_image_size
382 - capsule_data->header_size;
383
384 if (capsule->version != 0x00000001)
385 return EFI_UNSUPPORTED;
386
387 handles = NULL;
388 ret = EFI_CALL(efi_locate_handle_buffer(
389 BY_PROTOCOL,
390 &efi_guid_firmware_management_protocol,
391 NULL, &no_handles, (efi_handle_t **)&handles));
392 if (ret != EFI_SUCCESS)
393 return EFI_UNSUPPORTED;
394
395 /* Payload */
396 for (item = capsule->embedded_driver_count;
397 item < capsule->embedded_driver_count
398 + capsule->payload_item_count; item++) {
399 /* sanity check */
400 if ((capsule->item_offset_list[item] + sizeof(*image)
401 >= capsule_size)) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200402 log_err("Capsule does not have enough data\n");
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900403 ret = EFI_INVALID_PARAMETER;
404 goto out;
405 }
406
407 image = (void *)capsule + capsule->item_offset_list[item];
408
409 if (image->version != 0x00000003) {
410 ret = EFI_UNSUPPORTED;
411 goto out;
412 }
413
414 /* find a device for update firmware */
415 /* TODO: should we pass index as well, or nothing but type? */
416 fmp = efi_fmp_find(&image->update_image_type_id,
417 image->update_hardware_instance,
418 handles, no_handles);
419 if (!fmp) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200420 log_err("FMP driver not found for firmware type %pUl, hardware instance %lld\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900421 &image->update_image_type_id,
422 image->update_hardware_instance);
423 ret = EFI_UNSUPPORTED;
424 goto out;
425 }
426
427 /* do update */
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900428 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
429 !(image->image_capsule_support &
430 CAPSULE_SUPPORT_AUTHENTICATION)) {
431 /* no signature */
432 ret = EFI_SECURITY_VIOLATION;
433 goto out;
434 }
435
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900436 image_binary = (void *)image + sizeof(*image);
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900437 image_binary_size = image->update_image_size;
438 vendor_code = image_binary + image_binary_size;
439 if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
440 (image->image_capsule_support &
441 CAPSULE_SUPPORT_AUTHENTICATION)) {
442 ret = efi_remove_auth_hdr(&image_binary,
443 &image_binary_size);
444 if (ret != EFI_SUCCESS)
445 goto out;
446 }
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900447
448 abort_reason = NULL;
449 ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
450 image_binary,
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900451 image_binary_size,
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900452 vendor_code, NULL,
453 &abort_reason));
454 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200455 log_err("Firmware update failed: %ls\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900456 abort_reason);
457 efi_free_pool(abort_reason);
458 goto out;
459 }
460 }
461
462out:
463 efi_free_pool(handles);
464
465 return ret;
466}
467#else
468static efi_status_t efi_capsule_update_firmware(
469 struct efi_capsule_header *capsule_data)
470{
471 return EFI_UNSUPPORTED;
472}
473#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
474
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900475/**
476 * efi_update_capsule() - process information from operating system
477 * @capsule_header_array: Array of virtual address pointers
478 * @capsule_count: Number of pointers in capsule_header_array
479 * @scatter_gather_list: Array of physical address pointers
480 *
481 * This function implements the UpdateCapsule() runtime service.
482 *
483 * See the Unified Extensible Firmware Interface (UEFI) specification for
484 * details.
485 *
486 * Return: status code
487 */
488efi_status_t EFIAPI efi_update_capsule(
489 struct efi_capsule_header **capsule_header_array,
490 efi_uintn_t capsule_count,
491 u64 scatter_gather_list)
492{
493 struct efi_capsule_header *capsule;
494 unsigned int i;
495 efi_status_t ret;
496
Simon Glass83698b22021-02-07 14:27:02 -0700497 EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900498 scatter_gather_list);
499
500 if (!capsule_count) {
501 ret = EFI_INVALID_PARAMETER;
502 goto out;
503 }
504
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900505 ret = EFI_SUCCESS;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900506 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
507 i++, capsule = *(++capsule_header_array)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900508 /* sanity check */
509 if (capsule->header_size < sizeof(*capsule) ||
510 capsule->capsule_image_size < sizeof(*capsule)) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200511 log_err("Capsule does not have enough data\n");
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900512 continue;
513 }
514
515 log_debug("Capsule[%d] (guid:%pUl)\n",
516 i, &capsule->capsule_guid);
517 if (!guidcmp(&capsule->capsule_guid,
518 &efi_guid_firmware_management_capsule_id)) {
519 ret = efi_capsule_update_firmware(capsule);
520 } else {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200521 log_err("Unsupported capsule type: %pUl\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900522 &capsule->capsule_guid);
523 ret = EFI_UNSUPPORTED;
524 }
525
526 if (ret != EFI_SUCCESS)
527 goto out;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900528 }
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000529
530 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
531 /* Rebuild the ESRT to reflect any updated FW images. */
532 ret = efi_esrt_populate();
533 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200534 log_warning("ESRT update failed\n");
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000535 }
Jose Marinhoaf886ce2021-04-19 14:54:33 +0100536out:
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000537
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900538 return EFI_EXIT(ret);
539}
540
541/**
542 * efi_query_capsule_caps() - check if capsule is supported
543 * @capsule_header_array: Array of virtual pointers
544 * @capsule_count: Number of pointers in capsule_header_array
545 * @maximum_capsule_size: Maximum capsule size
546 * @reset_type: Type of reset needed for capsule update
547 *
548 * This function implements the QueryCapsuleCapabilities() runtime service.
549 *
550 * See the Unified Extensible Firmware Interface (UEFI) specification for
551 * details.
552 *
553 * Return: status code
554 */
555efi_status_t EFIAPI efi_query_capsule_caps(
556 struct efi_capsule_header **capsule_header_array,
557 efi_uintn_t capsule_count,
558 u64 *maximum_capsule_size,
559 u32 *reset_type)
560{
561 struct efi_capsule_header *capsule __attribute__((unused));
562 unsigned int i;
563 efi_status_t ret;
564
Simon Glass83698b22021-02-07 14:27:02 -0700565 EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900566 maximum_capsule_size, reset_type);
567
568 if (!maximum_capsule_size) {
569 ret = EFI_INVALID_PARAMETER;
570 goto out;
571 }
572
573 *maximum_capsule_size = U64_MAX;
574 *reset_type = EFI_RESET_COLD;
575
576 ret = EFI_SUCCESS;
577 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
578 i++, capsule = *(++capsule_header_array)) {
579 /* TODO */
580 }
581out:
582 return EFI_EXIT(ret);
583}
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900584
585#ifdef CONFIG_EFI_CAPSULE_ON_DISK
586/**
587 * get_dp_device - retrieve a device path from boot variable
588 * @boot_var: Boot variable name
589 * @device_dp Device path
590 *
591 * Retrieve a device patch from boot variable, @boot_var.
592 *
593 * Return: status code
594 */
595static efi_status_t get_dp_device(u16 *boot_var,
596 struct efi_device_path **device_dp)
597{
598 void *buf = NULL;
599 efi_uintn_t size;
600 struct efi_load_option lo;
601 struct efi_device_path *file_dp;
602 efi_status_t ret;
603
604 size = 0;
605 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
606 NULL, &size, NULL, NULL);
607 if (ret == EFI_BUFFER_TOO_SMALL) {
608 buf = malloc(size);
609 if (!buf)
610 return EFI_OUT_OF_RESOURCES;
611 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
612 NULL, &size, buf, NULL);
613 }
614 if (ret != EFI_SUCCESS)
615 return ret;
616
617 efi_deserialize_load_option(&lo, buf, &size);
618
619 if (lo.attributes & LOAD_OPTION_ACTIVE) {
620 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
621 efi_free_pool(file_dp);
622
623 ret = EFI_SUCCESS;
624 } else {
625 ret = EFI_NOT_FOUND;
626 }
627
628 free(buf);
629
630 return ret;
631}
632
633/**
634 * device_is_present_and_system_part - check if a device exists
635 * @dp Device path
636 *
637 * Check if a device pointed to by the device path, @dp, exists and is
638 * located in UEFI system partition.
639 *
640 * Return: true - yes, false - no
641 */
642static bool device_is_present_and_system_part(struct efi_device_path *dp)
643{
644 efi_handle_t handle;
645
646 handle = efi_dp_find_obj(dp, NULL);
647 if (!handle)
648 return false;
649
650 return efi_disk_is_system_part(handle);
651}
652
653/**
654 * find_boot_device - identify the boot device
655 *
656 * Identify the boot device from boot-related variables as UEFI
657 * specification describes and put its handle into bootdev_root.
658 *
659 * Return: status code
660 */
661static efi_status_t find_boot_device(void)
662{
663 char boot_var[9];
664 u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
665 efi_uintn_t size;
666 int i, num;
667 struct efi_simple_file_system_protocol *volume;
668 struct efi_device_path *boot_dev = NULL;
669 efi_status_t ret;
670
671 /* find active boot device in BootNext */
672 bootnext = 0;
673 size = sizeof(bootnext);
674 ret = efi_get_variable_int(L"BootNext",
675 (efi_guid_t *)&efi_global_variable_guid,
676 NULL, &size, &bootnext, NULL);
677 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
678 /* BootNext does exist here */
679 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900680 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900681 goto skip;
682 }
683 sprintf((char *)boot_var, "Boot%04X", bootnext);
684 p = boot_var16;
685 utf8_utf16_strcpy(&p, boot_var);
686
687 ret = get_dp_device(boot_var16, &boot_dev);
688 if (ret == EFI_SUCCESS) {
689 if (device_is_present_and_system_part(boot_dev)) {
Masami Hiramatsu10165752021-07-12 18:05:17 +0900690 goto found;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900691 } else {
692 efi_free_pool(boot_dev);
693 boot_dev = NULL;
694 }
695 }
696 }
697
698skip:
699 /* find active boot device in BootOrder */
700 size = 0;
701 ret = efi_get_variable_int(L"BootOrder", &efi_global_variable_guid,
702 NULL, &size, NULL, NULL);
703 if (ret == EFI_BUFFER_TOO_SMALL) {
704 boot_order = malloc(size);
705 if (!boot_order) {
706 ret = EFI_OUT_OF_RESOURCES;
707 goto out;
708 }
709
710 ret = efi_get_variable_int(L"BootOrder",
711 &efi_global_variable_guid,
712 NULL, &size, boot_order, NULL);
713 }
714 if (ret != EFI_SUCCESS)
715 goto out;
716
717 /* check in higher order */
718 num = size / sizeof(u16);
719 for (i = 0; i < num; i++) {
720 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
721 p = boot_var16;
722 utf8_utf16_strcpy(&p, boot_var);
723 ret = get_dp_device(boot_var16, &boot_dev);
724 if (ret != EFI_SUCCESS)
725 continue;
726
727 if (device_is_present_and_system_part(boot_dev))
728 break;
729
730 efi_free_pool(boot_dev);
731 boot_dev = NULL;
732 }
Masami Hiramatsu10165752021-07-12 18:05:17 +0900733found:
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900734 if (boot_dev) {
Masami Hiramatsud9763bb2021-07-14 14:19:13 +0900735 log_debug("Boot device %pD\n", boot_dev);
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900736
737 volume = efi_fs_from_path(boot_dev);
738 if (!volume)
739 ret = EFI_DEVICE_ERROR;
740 else
741 ret = EFI_CALL(volume->open_volume(volume,
742 &bootdev_root));
743 efi_free_pool(boot_dev);
744 } else {
745 ret = EFI_NOT_FOUND;
746 }
AKASHI Takahirofa390e62021-04-20 10:03:16 +0900747out:
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900748 free(boot_order);
749
750 return ret;
751}
752
753/**
754 * efi_capsule_scan_dir - traverse a capsule directory in boot device
755 * @files: Array of file names
756 * @num: Number of elements in @files
757 *
758 * Traverse a capsule directory in boot device.
759 * Called by initialization code, and returns an array of capsule file
760 * names in @files.
761 *
762 * Return: status code
763 */
764static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
765{
766 struct efi_file_handle *dirh;
767 struct efi_file_info *dirent;
768 efi_uintn_t dirent_size, tmp_size;
769 unsigned int count;
770 u16 **tmp_files;
771 efi_status_t ret;
772
773 ret = find_boot_device();
774 if (ret == EFI_NOT_FOUND) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200775 log_debug("Boot device is not set\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900776 *num = 0;
777 return EFI_SUCCESS;
778 } else if (ret != EFI_SUCCESS) {
779 return EFI_DEVICE_ERROR;
780 }
781
782 /* count capsule files */
783 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
784 EFI_CAPSULE_DIR,
785 EFI_FILE_MODE_READ, 0));
786 if (ret != EFI_SUCCESS) {
787 *num = 0;
788 return EFI_SUCCESS;
789 }
790
791 dirent_size = 256;
792 dirent = malloc(dirent_size);
793 if (!dirent)
794 return EFI_OUT_OF_RESOURCES;
795
796 count = 0;
797 while (1) {
798 tmp_size = dirent_size;
799 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
800 if (ret == EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardtaa27e5d2021-04-11 06:53:04 +0200801 struct efi_file_info *old_dirent = dirent;
802
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900803 dirent = realloc(dirent, tmp_size);
804 if (!dirent) {
Heinrich Schuchardtaa27e5d2021-04-11 06:53:04 +0200805 dirent = old_dirent;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900806 ret = EFI_OUT_OF_RESOURCES;
807 goto err;
808 }
809 dirent_size = tmp_size;
810 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
811 }
812 if (ret != EFI_SUCCESS)
813 goto err;
814 if (!tmp_size)
815 break;
816
Heinrich Schuchardt76b708a2021-02-09 17:45:33 +0100817 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900818 count++;
819 }
820
821 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
822 if (ret != EFI_SUCCESS)
823 goto err;
824
825 /* make a list */
AKASHI Takahiroc8fc12f2021-01-22 10:43:27 +0900826 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900827 if (!tmp_files) {
828 ret = EFI_OUT_OF_RESOURCES;
829 goto err;
830 }
831
832 count = 0;
833 while (1) {
834 tmp_size = dirent_size;
835 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
836 if (ret != EFI_SUCCESS)
837 goto err;
838 if (!tmp_size)
839 break;
840
841 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
842 u16_strcmp(dirent->file_name, L".") &&
843 u16_strcmp(dirent->file_name, L".."))
844 tmp_files[count++] = u16_strdup(dirent->file_name);
845 }
846 /* ignore an error */
847 EFI_CALL((*dirh->close)(dirh));
848
849 /* in ascii order */
850 /* FIXME: u16 version of strcasecmp */
851 qsort(tmp_files, count, sizeof(*tmp_files),
852 (int (*)(const void *, const void *))strcasecmp);
853 *files = tmp_files;
854 *num = count;
855 ret = EFI_SUCCESS;
856err:
857 free(dirent);
858
859 return ret;
860}
861
862/**
863 * efi_capsule_read_file - read in a capsule file
864 * @filename: File name
865 * @capsule: Pointer to buffer for capsule
866 *
867 * Read a capsule file and put its content in @capsule.
868 *
869 * Return: status code
870 */
871static efi_status_t efi_capsule_read_file(const u16 *filename,
872 struct efi_capsule_header **capsule)
873{
874 struct efi_file_handle *dirh, *fh;
875 struct efi_file_info *file_info = NULL;
876 struct efi_capsule_header *buf = NULL;
877 efi_uintn_t size;
878 efi_status_t ret;
879
880 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
881 EFI_CAPSULE_DIR,
882 EFI_FILE_MODE_READ, 0));
883 if (ret != EFI_SUCCESS)
884 return ret;
885 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
886 EFI_FILE_MODE_READ, 0));
887 /* ignore an error */
888 EFI_CALL((*dirh->close)(dirh));
889 if (ret != EFI_SUCCESS)
890 return ret;
891
892 /* file size */
893 size = 0;
894 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
895 &size, file_info));
896 if (ret == EFI_BUFFER_TOO_SMALL) {
897 file_info = malloc(size);
898 if (!file_info) {
899 ret = EFI_OUT_OF_RESOURCES;
900 goto err;
901 }
902 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
903 &size, file_info));
904 }
905 if (ret != EFI_SUCCESS)
906 goto err;
907 size = file_info->file_size;
908 free(file_info);
909 buf = malloc(size);
910 if (!buf) {
911 ret = EFI_OUT_OF_RESOURCES;
912 goto err;
913 }
914
915 /* fetch data */
916 ret = EFI_CALL((*fh->read)(fh, &size, buf));
917 if (ret == EFI_SUCCESS) {
918 if (size >= buf->capsule_image_size) {
919 *capsule = buf;
920 } else {
921 free(buf);
922 ret = EFI_INVALID_PARAMETER;
923 }
924 } else {
925 free(buf);
926 }
927err:
928 EFI_CALL((*fh->close)(fh));
929
930 return ret;
931}
932
933/**
934 * efi_capsule_delete_file - delete a capsule file
935 * @filename: File name
936 *
937 * Delete a capsule file from capsule directory.
938 *
939 * Return: status code
940 */
941static efi_status_t efi_capsule_delete_file(const u16 *filename)
942{
943 struct efi_file_handle *dirh, *fh;
944 efi_status_t ret;
945
946 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
947 EFI_CAPSULE_DIR,
948 EFI_FILE_MODE_READ, 0));
949 if (ret != EFI_SUCCESS)
950 return ret;
951 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
952 EFI_FILE_MODE_READ, 0));
953 /* ignore an error */
954 EFI_CALL((*dirh->close)(dirh));
955
Heinrich Schuchardte5c22812021-06-02 19:28:22 +0200956 if (ret == EFI_SUCCESS)
957 ret = EFI_CALL((*fh->delete)(fh));
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900958
959 return ret;
960}
961
962/**
963 * efi_capsule_scan_done - reset a scan help function
964 *
965 * Reset a scan help function
966 */
967static void efi_capsule_scan_done(void)
968{
969 EFI_CALL((*bootdev_root->close)(bootdev_root));
970 bootdev_root = NULL;
971}
972
973/**
Ilias Apalodimasfac773e2021-06-22 17:38:53 +0300974 * efi_load_capsule_drivers - initialize capsule drivers
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900975 *
Ilias Apalodimasfac773e2021-06-22 17:38:53 +0300976 * Generic FMP drivers backed by DFU
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900977 *
978 * Return: status code
979 */
Ilias Apalodimasfac773e2021-06-22 17:38:53 +0300980efi_status_t __weak efi_load_capsule_drivers(void)
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900981{
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900982 __maybe_unused efi_handle_t handle;
983 efi_status_t ret = EFI_SUCCESS;
984
985 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
986 handle = NULL;
987 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
988 &handle, &efi_guid_firmware_management_protocol,
989 &efi_fmp_fit, NULL));
990 }
991
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900992 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
993 handle = NULL;
994 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
Masami Hiramatsuf5388522021-06-22 17:38:51 +0300995 &handle,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900996 &efi_guid_firmware_management_protocol,
997 &efi_fmp_raw, NULL));
998 }
999
AKASHI Takahirof4818e62020-11-30 18:12:12 +09001000 return ret;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001001}
1002
1003/**
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001004 * check_run_capsules - Check whether capsule update should run
1005 *
1006 * The spec says OsIndications must be set in order to run the capsule update
1007 * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to
1008 * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected
1009 */
1010static bool check_run_capsules(void)
1011{
1012 u64 os_indications;
1013 efi_uintn_t size;
1014 efi_status_t ret;
1015
1016 if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS))
1017 return true;
1018
1019 size = sizeof(os_indications);
1020 ret = efi_get_variable_int(L"OsIndications", &efi_global_variable_guid,
1021 NULL, &size, &os_indications, NULL);
1022 if (ret == EFI_SUCCESS &&
1023 (os_indications
1024 & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED))
1025 return true;
1026
1027 return false;
1028}
1029
1030/**
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001031 * efi_launch_capsule - launch capsules
1032 *
1033 * Launch all the capsules in system at boot time.
1034 * Called by efi init code
1035 *
1036 * Return: status codde
1037 */
1038efi_status_t efi_launch_capsules(void)
1039{
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001040 struct efi_capsule_header *capsule = NULL;
1041 u16 **files;
1042 unsigned int nfiles, index, i;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001043 efi_status_t ret;
1044
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001045 if (!check_run_capsules())
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001046 return EFI_SUCCESS;
1047
1048 index = get_last_capsule();
1049
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001050 /*
1051 * Find capsules on disk.
1052 * All the capsules are collected at the beginning because
1053 * capsule files will be removed instantly.
1054 */
1055 nfiles = 0;
1056 files = NULL;
1057 ret = efi_capsule_scan_dir(&files, &nfiles);
1058 if (ret != EFI_SUCCESS)
1059 return ret;
1060 if (!nfiles)
1061 return EFI_SUCCESS;
1062
1063 /* Launch capsules */
1064 for (i = 0, ++index; i < nfiles; i++, index++) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001065 log_debug("Applying %ls\n", files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001066 if (index > 0xffff)
1067 index = 0;
1068 ret = efi_capsule_read_file(files[i], &capsule);
1069 if (ret == EFI_SUCCESS) {
1070 ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0));
1071 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001072 log_err("Applying capsule %ls failed\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001073 files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001074
1075 free(capsule);
1076 } else {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001077 log_err("Reading capsule %ls failed\n", files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001078 }
1079 /* create CapsuleXXXX */
1080 set_capsule_result(index, capsule, ret);
1081
1082 /* delete a capsule either in case of success or failure */
1083 ret = efi_capsule_delete_file(files[i]);
1084 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001085 log_err("Deleting capsule %ls failed\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001086 files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001087 }
1088 efi_capsule_scan_done();
1089
1090 for (i = 0; i < nfiles; i++)
1091 free(files[i]);
1092 free(files);
1093
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001094 return ret;
1095}
1096#endif /* CONFIG_EFI_CAPSULE_ON_DISK */