blob: 26990bc2df4a21b1e4420731e212156f4494b012 [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
Ilias Apalodimas27e059c2021-07-17 17:26:44 +030019#include <asm/sections.h>
Sughosh Ganu586bb982020-12-30 19:27:09 +053020#include <crypto/pkcs7.h>
21#include <crypto/pkcs7_parser.h>
22#include <linux/err.h>
23
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090024const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
AKASHI Takahiro0d963782020-11-30 18:12:11 +090025static const efi_guid_t efi_guid_firmware_management_capsule_id =
26 EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
27const efi_guid_t efi_guid_firmware_management_protocol =
28 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090029
AKASHI Takahiro45b819542020-11-17 09:27:56 +090030#ifdef CONFIG_EFI_CAPSULE_ON_DISK
31/* for file system access */
32static struct efi_file_handle *bootdev_root;
33#endif
34
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090035/**
36 * get_last_capsule - get the last capsule index
37 *
38 * Retrieve the index of the capsule invoked last time from "CapsuleLast"
39 * variable.
40 *
41 * Return:
42 * * > 0 - the last capsule index invoked
43 * * 0xffff - on error, or no capsule invoked yet
44 */
45static __maybe_unused unsigned int get_last_capsule(void)
46{
47 u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010048 char value[5];
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090049 efi_uintn_t size;
50 unsigned long index = 0xffff;
51 efi_status_t ret;
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010052 int i;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090053
54 size = sizeof(value16);
55 ret = efi_get_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
56 NULL, &size, value16, NULL);
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010057 if (ret != EFI_SUCCESS || size != 22 ||
58 u16_strncmp(value16, L"Capsule", 7))
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090059 goto err;
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010060 for (i = 0; i < 4; ++i) {
61 u16 c = value16[i + 7];
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090062
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010063 if (!c || c > 0x7f)
64 goto err;
65 value[i] = c;
66 }
67 value[4] = 0;
68 if (strict_strtoul(value, 16, &index))
69 index = 0xffff;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090070err:
71 return index;
72}
73
74/**
75 * set_capsule_result - set a result variable
76 * @capsule: Capsule
77 * @return_status: Return status
78 *
79 * Create and set a result variable, "CapsuleXXXX", for the capsule,
80 * @capsule.
81 */
82static __maybe_unused
83void set_capsule_result(int index, struct efi_capsule_header *capsule,
84 efi_status_t return_status)
85{
86 u16 variable_name16[12];
87 struct efi_capsule_result_variable_header result;
88 struct efi_time time;
89 efi_status_t ret;
90
Ilias Apalodimas21575292020-12-31 12:26:46 +020091 efi_create_indexed_name(variable_name16, sizeof(variable_name16),
92 "Capsule", index);
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090093 result.variable_total_size = sizeof(result);
94 result.capsule_guid = capsule->capsule_guid;
95 ret = EFI_CALL((*efi_runtime_services.get_time)(&time, NULL));
96 if (ret == EFI_SUCCESS)
97 memcpy(&result.capsule_processed, &time, sizeof(time));
98 else
99 memset(&result.capsule_processed, 0, sizeof(time));
100 result.capsule_status = return_status;
Heinrich Schuchardt24adaa72021-07-10 11:10:26 +0200101 ret = efi_set_variable_int(variable_name16, &efi_guid_capsule_report,
102 EFI_VARIABLE_NON_VOLATILE |
103 EFI_VARIABLE_BOOTSERVICE_ACCESS |
104 EFI_VARIABLE_RUNTIME_ACCESS,
105 sizeof(result), &result, false);
Heinrich Schuchardtb1fae8c2021-07-10 11:14:13 +0200106 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200107 log_err("Setting %ls failed\n", variable_name16);
Heinrich Schuchardtb1fae8c2021-07-10 11:14:13 +0200108 return;
109 }
110
111 /* Variable CapsuleLast must not include terminating 0x0000 */
112 ret = efi_set_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
113 EFI_VARIABLE_READ_ONLY |
114 EFI_VARIABLE_NON_VOLATILE |
115 EFI_VARIABLE_BOOTSERVICE_ACCESS |
116 EFI_VARIABLE_RUNTIME_ACCESS,
117 22, variable_name16, false);
118 if (ret != EFI_SUCCESS)
119 log_err("Setting %ls failed\n", L"CapsuleLast");
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900120}
121
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900122#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT
123/**
124 * efi_fmp_find - search for Firmware Management Protocol drivers
125 * @image_type: Image type guid
126 * @instance: Instance number
127 * @handles: Handles of FMP drivers
128 * @no_handles: Number of handles
129 *
130 * Search for Firmware Management Protocol drivers, matching the image
131 * type, @image_type and the machine instance, @instance, from the list,
132 * @handles.
133 *
134 * Return:
135 * * Protocol instance - on success
136 * * NULL - on failure
137 */
138static struct efi_firmware_management_protocol *
139efi_fmp_find(efi_guid_t *image_type, u64 instance, efi_handle_t *handles,
140 efi_uintn_t no_handles)
141{
142 efi_handle_t *handle;
143 struct efi_firmware_management_protocol *fmp;
144 struct efi_firmware_image_descriptor *image_info, *desc;
145 efi_uintn_t info_size, descriptor_size;
146 u32 descriptor_version;
147 u8 descriptor_count;
148 u32 package_version;
149 u16 *package_version_name;
150 bool found = false;
151 int i, j;
152 efi_status_t ret;
153
154 for (i = 0, handle = handles; i < no_handles; i++, handle++) {
155 ret = EFI_CALL(efi_handle_protocol(
156 *handle,
157 &efi_guid_firmware_management_protocol,
158 (void **)&fmp));
159 if (ret != EFI_SUCCESS)
160 continue;
161
162 /* get device's image info */
163 info_size = 0;
164 image_info = NULL;
165 descriptor_version = 0;
166 descriptor_count = 0;
167 descriptor_size = 0;
168 package_version = 0;
169 package_version_name = NULL;
170 ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
171 image_info,
172 &descriptor_version,
173 &descriptor_count,
174 &descriptor_size,
175 &package_version,
176 &package_version_name));
177 if (ret != EFI_BUFFER_TOO_SMALL)
178 goto skip;
179
180 image_info = malloc(info_size);
181 if (!image_info)
182 goto skip;
183
184 ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
185 image_info,
186 &descriptor_version,
187 &descriptor_count,
188 &descriptor_size,
189 &package_version,
190 &package_version_name));
191 if (ret != EFI_SUCCESS ||
192 descriptor_version != EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION)
193 goto skip;
194
195 /* matching */
196 for (j = 0, desc = image_info; j < descriptor_count;
197 j++, desc = (void *)desc + descriptor_size) {
198 log_debug("+++ desc[%d] index: %d, name: %ls\n",
199 j, desc->image_index, desc->image_id_name);
200 if (!guidcmp(&desc->image_type_id, image_type) &&
201 (!instance ||
202 !desc->hardware_instance ||
203 desc->hardware_instance == instance))
204 found = true;
205 }
206
207skip:
208 efi_free_pool(package_version_name);
209 free(image_info);
210 EFI_CALL(efi_close_protocol(
211 (efi_handle_t)fmp,
212 &efi_guid_firmware_management_protocol,
213 NULL, NULL));
214 if (found)
215 return fmp;
216 }
217
218 return NULL;
219}
220
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900221/**
222 * efi_remove_auth_hdr - remove authentication data from image
223 * @image: Pointer to pointer to Image
224 * @image_size: Pointer to Image size
225 *
226 * Remove the authentication data from image if possible.
227 * Update @image and @image_size.
228 *
229 * Return: status code
230 */
231static efi_status_t efi_remove_auth_hdr(void **image, efi_uintn_t *image_size)
232{
233 struct efi_firmware_image_authentication *auth_hdr;
234 efi_status_t ret = EFI_INVALID_PARAMETER;
235
236 auth_hdr = (struct efi_firmware_image_authentication *)*image;
237 if (*image_size < sizeof(*auth_hdr))
238 goto out;
239
240 if (auth_hdr->auth_info.hdr.dwLength <=
241 offsetof(struct win_certificate_uefi_guid, cert_data))
242 goto out;
243
244 *image = (uint8_t *)*image + sizeof(auth_hdr->monotonic_count) +
245 auth_hdr->auth_info.hdr.dwLength;
246 *image_size = *image_size - auth_hdr->auth_info.hdr.dwLength -
247 sizeof(auth_hdr->monotonic_count);
248
249 ret = EFI_SUCCESS;
250out:
251 return ret;
252}
253
Sughosh Ganu586bb982020-12-30 19:27:09 +0530254#if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
255
Ilias Apalodimas27e059c2021-07-17 17:26:44 +0300256static int efi_get_public_key_data(void **pkey, efi_uintn_t *pkey_len)
257{
258 const void *blob = __efi_capsule_sig_begin;
259 const int len = __efi_capsule_sig_end - __efi_capsule_sig_begin;
260
261 *pkey = (void *)blob;
262 *pkey_len = len;
263
264 return 0;
265}
266
Sughosh Ganu586bb982020-12-30 19:27:09 +0530267efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
268 void **image, efi_uintn_t *image_size)
269{
270 u8 *buf;
271 int ret;
Ilias Apalodimas27e059c2021-07-17 17:26:44 +0300272 void *stored_pkey, *pkey;
Sughosh Ganu586bb982020-12-30 19:27:09 +0530273 efi_uintn_t pkey_len;
274 uint64_t monotonic_count;
275 struct efi_signature_store *truststore;
276 struct pkcs7_message *capsule_sig;
277 struct efi_image_regions *regs;
278 struct efi_firmware_image_authentication *auth_hdr;
279 efi_status_t status;
280
281 status = EFI_SECURITY_VIOLATION;
282 capsule_sig = NULL;
283 truststore = NULL;
284 regs = NULL;
285
286 /* Sanity checks */
287 if (capsule == NULL || capsule_size == 0)
288 goto out;
289
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900290 *image = (uint8_t *)capsule;
291 *image_size = capsule_size;
292 if (efi_remove_auth_hdr(image, image_size) != EFI_SUCCESS)
Sughosh Ganu586bb982020-12-30 19:27:09 +0530293 goto out;
294
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900295 auth_hdr = (struct efi_firmware_image_authentication *)capsule;
Sughosh Ganu586bb982020-12-30 19:27:09 +0530296 if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
297 goto out;
298
Sughosh Ganu586bb982020-12-30 19:27:09 +0530299 memcpy(&monotonic_count, &auth_hdr->monotonic_count,
300 sizeof(monotonic_count));
301
302 /* data to be digested */
303 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 2, 1);
304 if (!regs)
305 goto out;
306
307 regs->max = 2;
308 efi_image_region_add(regs, (uint8_t *)*image,
309 (uint8_t *)*image + *image_size, 1);
310
311 efi_image_region_add(regs, (uint8_t *)&monotonic_count,
312 (uint8_t *)&monotonic_count + sizeof(monotonic_count),
313 1);
314
315 capsule_sig = efi_parse_pkcs7_header(auth_hdr->auth_info.cert_data,
316 auth_hdr->auth_info.hdr.dwLength
317 - sizeof(auth_hdr->auth_info),
318 &buf);
319 if (IS_ERR(capsule_sig)) {
320 debug("Parsing variable's pkcs7 header failed\n");
321 capsule_sig = NULL;
322 goto out;
323 }
324
Ilias Apalodimas27e059c2021-07-17 17:26:44 +0300325 ret = efi_get_public_key_data(&stored_pkey, &pkey_len);
Sughosh Ganu586bb982020-12-30 19:27:09 +0530326 if (ret < 0)
327 goto out;
328
329 pkey = malloc(pkey_len);
330 if (!pkey)
331 goto out;
332
Ilias Apalodimas27e059c2021-07-17 17:26:44 +0300333 memcpy(pkey, stored_pkey, pkey_len);
Sughosh Ganu586bb982020-12-30 19:27:09 +0530334 truststore = efi_build_signature_store(pkey, pkey_len);
335 if (!truststore)
336 goto out;
337
338 /* verify signature */
339 if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) {
340 debug("Verified\n");
341 } else {
342 debug("Verifying variable's signature failed\n");
343 goto out;
344 }
345
346 status = EFI_SUCCESS;
347
348out:
349 efi_sigstore_free(truststore);
350 pkcs7_free_message(capsule_sig);
351 free(regs);
352
353 return status;
354}
355#else
356efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
357 void **image, efi_uintn_t *image_size)
358{
359 return EFI_UNSUPPORTED;
360}
361#endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */
362
363
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900364/**
365 * efi_capsule_update_firmware - update firmware from capsule
366 * @capsule_data: Capsule
367 *
368 * Update firmware, using a capsule, @capsule_data. Loading any FMP
369 * drivers embedded in a capsule is not supported.
370 *
371 * Return: status code
372 */
373static efi_status_t efi_capsule_update_firmware(
374 struct efi_capsule_header *capsule_data)
375{
376 struct efi_firmware_management_capsule_header *capsule;
377 struct efi_firmware_management_capsule_image_header *image;
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900378 size_t capsule_size, image_binary_size;
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900379 void *image_binary, *vendor_code;
380 efi_handle_t *handles;
381 efi_uintn_t no_handles;
382 int item;
383 struct efi_firmware_management_protocol *fmp;
384 u16 *abort_reason;
385 efi_status_t ret = EFI_SUCCESS;
386
387 /* sanity check */
388 if (capsule_data->header_size < sizeof(*capsule) ||
389 capsule_data->header_size >= capsule_data->capsule_image_size)
390 return EFI_INVALID_PARAMETER;
391
392 capsule = (void *)capsule_data + capsule_data->header_size;
393 capsule_size = capsule_data->capsule_image_size
394 - capsule_data->header_size;
395
396 if (capsule->version != 0x00000001)
397 return EFI_UNSUPPORTED;
398
399 handles = NULL;
400 ret = EFI_CALL(efi_locate_handle_buffer(
401 BY_PROTOCOL,
402 &efi_guid_firmware_management_protocol,
403 NULL, &no_handles, (efi_handle_t **)&handles));
404 if (ret != EFI_SUCCESS)
405 return EFI_UNSUPPORTED;
406
407 /* Payload */
408 for (item = capsule->embedded_driver_count;
409 item < capsule->embedded_driver_count
410 + capsule->payload_item_count; item++) {
411 /* sanity check */
412 if ((capsule->item_offset_list[item] + sizeof(*image)
413 >= capsule_size)) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200414 log_err("Capsule does not have enough data\n");
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900415 ret = EFI_INVALID_PARAMETER;
416 goto out;
417 }
418
419 image = (void *)capsule + capsule->item_offset_list[item];
420
421 if (image->version != 0x00000003) {
422 ret = EFI_UNSUPPORTED;
423 goto out;
424 }
425
426 /* find a device for update firmware */
427 /* TODO: should we pass index as well, or nothing but type? */
428 fmp = efi_fmp_find(&image->update_image_type_id,
429 image->update_hardware_instance,
430 handles, no_handles);
431 if (!fmp) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200432 log_err("FMP driver not found for firmware type %pUl, hardware instance %lld\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900433 &image->update_image_type_id,
434 image->update_hardware_instance);
435 ret = EFI_UNSUPPORTED;
436 goto out;
437 }
438
439 /* do update */
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900440 if (IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
441 !(image->image_capsule_support &
442 CAPSULE_SUPPORT_AUTHENTICATION)) {
443 /* no signature */
444 ret = EFI_SECURITY_VIOLATION;
445 goto out;
446 }
447
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900448 image_binary = (void *)image + sizeof(*image);
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900449 image_binary_size = image->update_image_size;
450 vendor_code = image_binary + image_binary_size;
451 if (!IS_ENABLED(CONFIG_EFI_CAPSULE_AUTHENTICATE) &&
452 (image->image_capsule_support &
453 CAPSULE_SUPPORT_AUTHENTICATION)) {
454 ret = efi_remove_auth_hdr(&image_binary,
455 &image_binary_size);
456 if (ret != EFI_SUCCESS)
457 goto out;
458 }
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900459
460 abort_reason = NULL;
461 ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
462 image_binary,
AKASHI Takahiro920671c2021-07-20 14:52:05 +0900463 image_binary_size,
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900464 vendor_code, NULL,
465 &abort_reason));
466 if (ret != EFI_SUCCESS) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200467 log_err("Firmware update failed: %ls\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900468 abort_reason);
469 efi_free_pool(abort_reason);
470 goto out;
471 }
472 }
473
474out:
475 efi_free_pool(handles);
476
477 return ret;
478}
479#else
480static efi_status_t efi_capsule_update_firmware(
481 struct efi_capsule_header *capsule_data)
482{
483 return EFI_UNSUPPORTED;
484}
485#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
486
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900487/**
488 * efi_update_capsule() - process information from operating system
489 * @capsule_header_array: Array of virtual address pointers
490 * @capsule_count: Number of pointers in capsule_header_array
491 * @scatter_gather_list: Array of physical address pointers
492 *
493 * This function implements the UpdateCapsule() runtime service.
494 *
495 * See the Unified Extensible Firmware Interface (UEFI) specification for
496 * details.
497 *
498 * Return: status code
499 */
500efi_status_t EFIAPI efi_update_capsule(
501 struct efi_capsule_header **capsule_header_array,
502 efi_uintn_t capsule_count,
503 u64 scatter_gather_list)
504{
505 struct efi_capsule_header *capsule;
506 unsigned int i;
507 efi_status_t ret;
508
Simon Glass83698b22021-02-07 14:27:02 -0700509 EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900510 scatter_gather_list);
511
512 if (!capsule_count) {
513 ret = EFI_INVALID_PARAMETER;
514 goto out;
515 }
516
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900517 ret = EFI_SUCCESS;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900518 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
519 i++, capsule = *(++capsule_header_array)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900520 /* sanity check */
521 if (capsule->header_size < sizeof(*capsule) ||
522 capsule->capsule_image_size < sizeof(*capsule)) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200523 log_err("Capsule does not have enough data\n");
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900524 continue;
525 }
526
527 log_debug("Capsule[%d] (guid:%pUl)\n",
528 i, &capsule->capsule_guid);
529 if (!guidcmp(&capsule->capsule_guid,
530 &efi_guid_firmware_management_capsule_id)) {
531 ret = efi_capsule_update_firmware(capsule);
532 } else {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200533 log_err("Unsupported capsule type: %pUl\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900534 &capsule->capsule_guid);
535 ret = EFI_UNSUPPORTED;
536 }
537
538 if (ret != EFI_SUCCESS)
539 goto out;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900540 }
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000541
542 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
543 /* Rebuild the ESRT to reflect any updated FW images. */
544 ret = efi_esrt_populate();
545 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200546 log_warning("ESRT update failed\n");
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000547 }
Jose Marinhoaf886ce2021-04-19 14:54:33 +0100548out:
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000549
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900550 return EFI_EXIT(ret);
551}
552
553/**
554 * efi_query_capsule_caps() - check if capsule is supported
555 * @capsule_header_array: Array of virtual pointers
556 * @capsule_count: Number of pointers in capsule_header_array
557 * @maximum_capsule_size: Maximum capsule size
558 * @reset_type: Type of reset needed for capsule update
559 *
560 * This function implements the QueryCapsuleCapabilities() runtime service.
561 *
562 * See the Unified Extensible Firmware Interface (UEFI) specification for
563 * details.
564 *
565 * Return: status code
566 */
567efi_status_t EFIAPI efi_query_capsule_caps(
568 struct efi_capsule_header **capsule_header_array,
569 efi_uintn_t capsule_count,
570 u64 *maximum_capsule_size,
571 u32 *reset_type)
572{
573 struct efi_capsule_header *capsule __attribute__((unused));
574 unsigned int i;
575 efi_status_t ret;
576
Simon Glass83698b22021-02-07 14:27:02 -0700577 EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900578 maximum_capsule_size, reset_type);
579
580 if (!maximum_capsule_size) {
581 ret = EFI_INVALID_PARAMETER;
582 goto out;
583 }
584
585 *maximum_capsule_size = U64_MAX;
586 *reset_type = EFI_RESET_COLD;
587
588 ret = EFI_SUCCESS;
589 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
590 i++, capsule = *(++capsule_header_array)) {
591 /* TODO */
592 }
593out:
594 return EFI_EXIT(ret);
595}
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900596
597#ifdef CONFIG_EFI_CAPSULE_ON_DISK
598/**
599 * get_dp_device - retrieve a device path from boot variable
600 * @boot_var: Boot variable name
601 * @device_dp Device path
602 *
603 * Retrieve a device patch from boot variable, @boot_var.
604 *
605 * Return: status code
606 */
607static efi_status_t get_dp_device(u16 *boot_var,
608 struct efi_device_path **device_dp)
609{
610 void *buf = NULL;
611 efi_uintn_t size;
612 struct efi_load_option lo;
613 struct efi_device_path *file_dp;
614 efi_status_t ret;
615
616 size = 0;
617 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
618 NULL, &size, NULL, NULL);
619 if (ret == EFI_BUFFER_TOO_SMALL) {
620 buf = malloc(size);
621 if (!buf)
622 return EFI_OUT_OF_RESOURCES;
623 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
624 NULL, &size, buf, NULL);
625 }
626 if (ret != EFI_SUCCESS)
627 return ret;
628
629 efi_deserialize_load_option(&lo, buf, &size);
630
631 if (lo.attributes & LOAD_OPTION_ACTIVE) {
632 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
633 efi_free_pool(file_dp);
634
635 ret = EFI_SUCCESS;
636 } else {
637 ret = EFI_NOT_FOUND;
638 }
639
640 free(buf);
641
642 return ret;
643}
644
645/**
646 * device_is_present_and_system_part - check if a device exists
647 * @dp Device path
648 *
649 * Check if a device pointed to by the device path, @dp, exists and is
650 * located in UEFI system partition.
651 *
652 * Return: true - yes, false - no
653 */
654static bool device_is_present_and_system_part(struct efi_device_path *dp)
655{
656 efi_handle_t handle;
657
658 handle = efi_dp_find_obj(dp, NULL);
659 if (!handle)
660 return false;
661
662 return efi_disk_is_system_part(handle);
663}
664
665/**
666 * find_boot_device - identify the boot device
667 *
668 * Identify the boot device from boot-related variables as UEFI
669 * specification describes and put its handle into bootdev_root.
670 *
671 * Return: status code
672 */
673static efi_status_t find_boot_device(void)
674{
675 char boot_var[9];
676 u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
677 efi_uintn_t size;
678 int i, num;
679 struct efi_simple_file_system_protocol *volume;
680 struct efi_device_path *boot_dev = NULL;
681 efi_status_t ret;
682
683 /* find active boot device in BootNext */
684 bootnext = 0;
685 size = sizeof(bootnext);
686 ret = efi_get_variable_int(L"BootNext",
687 (efi_guid_t *)&efi_global_variable_guid,
688 NULL, &size, &bootnext, NULL);
689 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
690 /* BootNext does exist here */
691 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900692 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900693 goto skip;
694 }
695 sprintf((char *)boot_var, "Boot%04X", bootnext);
696 p = boot_var16;
697 utf8_utf16_strcpy(&p, boot_var);
698
699 ret = get_dp_device(boot_var16, &boot_dev);
700 if (ret == EFI_SUCCESS) {
701 if (device_is_present_and_system_part(boot_dev)) {
Masami Hiramatsu10165752021-07-12 18:05:17 +0900702 goto found;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900703 } else {
704 efi_free_pool(boot_dev);
705 boot_dev = NULL;
706 }
707 }
708 }
709
710skip:
711 /* find active boot device in BootOrder */
712 size = 0;
713 ret = efi_get_variable_int(L"BootOrder", &efi_global_variable_guid,
714 NULL, &size, NULL, NULL);
715 if (ret == EFI_BUFFER_TOO_SMALL) {
716 boot_order = malloc(size);
717 if (!boot_order) {
718 ret = EFI_OUT_OF_RESOURCES;
719 goto out;
720 }
721
722 ret = efi_get_variable_int(L"BootOrder",
723 &efi_global_variable_guid,
724 NULL, &size, boot_order, NULL);
725 }
726 if (ret != EFI_SUCCESS)
727 goto out;
728
729 /* check in higher order */
730 num = size / sizeof(u16);
731 for (i = 0; i < num; i++) {
732 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
733 p = boot_var16;
734 utf8_utf16_strcpy(&p, boot_var);
735 ret = get_dp_device(boot_var16, &boot_dev);
736 if (ret != EFI_SUCCESS)
737 continue;
738
739 if (device_is_present_and_system_part(boot_dev))
740 break;
741
742 efi_free_pool(boot_dev);
743 boot_dev = NULL;
744 }
Masami Hiramatsu10165752021-07-12 18:05:17 +0900745found:
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900746 if (boot_dev) {
Masami Hiramatsud9763bb2021-07-14 14:19:13 +0900747 log_debug("Boot device %pD\n", boot_dev);
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900748
749 volume = efi_fs_from_path(boot_dev);
750 if (!volume)
751 ret = EFI_DEVICE_ERROR;
752 else
753 ret = EFI_CALL(volume->open_volume(volume,
754 &bootdev_root));
755 efi_free_pool(boot_dev);
756 } else {
757 ret = EFI_NOT_FOUND;
758 }
AKASHI Takahirofa390e62021-04-20 10:03:16 +0900759out:
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900760 free(boot_order);
761
762 return ret;
763}
764
765/**
766 * efi_capsule_scan_dir - traverse a capsule directory in boot device
767 * @files: Array of file names
768 * @num: Number of elements in @files
769 *
770 * Traverse a capsule directory in boot device.
771 * Called by initialization code, and returns an array of capsule file
772 * names in @files.
773 *
774 * Return: status code
775 */
776static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
777{
778 struct efi_file_handle *dirh;
779 struct efi_file_info *dirent;
780 efi_uintn_t dirent_size, tmp_size;
781 unsigned int count;
782 u16 **tmp_files;
783 efi_status_t ret;
784
785 ret = find_boot_device();
786 if (ret == EFI_NOT_FOUND) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +0200787 log_debug("Boot device is not set\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900788 *num = 0;
789 return EFI_SUCCESS;
790 } else if (ret != EFI_SUCCESS) {
791 return EFI_DEVICE_ERROR;
792 }
793
794 /* count capsule files */
795 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
796 EFI_CAPSULE_DIR,
797 EFI_FILE_MODE_READ, 0));
798 if (ret != EFI_SUCCESS) {
799 *num = 0;
800 return EFI_SUCCESS;
801 }
802
803 dirent_size = 256;
804 dirent = malloc(dirent_size);
805 if (!dirent)
806 return EFI_OUT_OF_RESOURCES;
807
808 count = 0;
809 while (1) {
810 tmp_size = dirent_size;
811 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
812 if (ret == EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardtaa27e5d2021-04-11 06:53:04 +0200813 struct efi_file_info *old_dirent = dirent;
814
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900815 dirent = realloc(dirent, tmp_size);
816 if (!dirent) {
Heinrich Schuchardtaa27e5d2021-04-11 06:53:04 +0200817 dirent = old_dirent;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900818 ret = EFI_OUT_OF_RESOURCES;
819 goto err;
820 }
821 dirent_size = tmp_size;
822 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
823 }
824 if (ret != EFI_SUCCESS)
825 goto err;
826 if (!tmp_size)
827 break;
828
Heinrich Schuchardt76b708a2021-02-09 17:45:33 +0100829 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900830 count++;
831 }
832
833 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
834 if (ret != EFI_SUCCESS)
835 goto err;
836
837 /* make a list */
AKASHI Takahiroc8fc12f2021-01-22 10:43:27 +0900838 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900839 if (!tmp_files) {
840 ret = EFI_OUT_OF_RESOURCES;
841 goto err;
842 }
843
844 count = 0;
845 while (1) {
846 tmp_size = dirent_size;
847 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
848 if (ret != EFI_SUCCESS)
849 goto err;
850 if (!tmp_size)
851 break;
852
853 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
854 u16_strcmp(dirent->file_name, L".") &&
855 u16_strcmp(dirent->file_name, L".."))
856 tmp_files[count++] = u16_strdup(dirent->file_name);
857 }
858 /* ignore an error */
859 EFI_CALL((*dirh->close)(dirh));
860
861 /* in ascii order */
862 /* FIXME: u16 version of strcasecmp */
863 qsort(tmp_files, count, sizeof(*tmp_files),
864 (int (*)(const void *, const void *))strcasecmp);
865 *files = tmp_files;
866 *num = count;
867 ret = EFI_SUCCESS;
868err:
869 free(dirent);
870
871 return ret;
872}
873
874/**
875 * efi_capsule_read_file - read in a capsule file
876 * @filename: File name
877 * @capsule: Pointer to buffer for capsule
878 *
879 * Read a capsule file and put its content in @capsule.
880 *
881 * Return: status code
882 */
883static efi_status_t efi_capsule_read_file(const u16 *filename,
884 struct efi_capsule_header **capsule)
885{
886 struct efi_file_handle *dirh, *fh;
887 struct efi_file_info *file_info = NULL;
888 struct efi_capsule_header *buf = NULL;
889 efi_uintn_t size;
890 efi_status_t ret;
891
892 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
893 EFI_CAPSULE_DIR,
894 EFI_FILE_MODE_READ, 0));
895 if (ret != EFI_SUCCESS)
896 return ret;
897 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
898 EFI_FILE_MODE_READ, 0));
899 /* ignore an error */
900 EFI_CALL((*dirh->close)(dirh));
901 if (ret != EFI_SUCCESS)
902 return ret;
903
904 /* file size */
905 size = 0;
906 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
907 &size, file_info));
908 if (ret == EFI_BUFFER_TOO_SMALL) {
909 file_info = malloc(size);
910 if (!file_info) {
911 ret = EFI_OUT_OF_RESOURCES;
912 goto err;
913 }
914 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
915 &size, file_info));
916 }
917 if (ret != EFI_SUCCESS)
918 goto err;
919 size = file_info->file_size;
920 free(file_info);
921 buf = malloc(size);
922 if (!buf) {
923 ret = EFI_OUT_OF_RESOURCES;
924 goto err;
925 }
926
927 /* fetch data */
928 ret = EFI_CALL((*fh->read)(fh, &size, buf));
929 if (ret == EFI_SUCCESS) {
930 if (size >= buf->capsule_image_size) {
931 *capsule = buf;
932 } else {
933 free(buf);
934 ret = EFI_INVALID_PARAMETER;
935 }
936 } else {
937 free(buf);
938 }
939err:
940 EFI_CALL((*fh->close)(fh));
941
942 return ret;
943}
944
945/**
946 * efi_capsule_delete_file - delete a capsule file
947 * @filename: File name
948 *
949 * Delete a capsule file from capsule directory.
950 *
951 * Return: status code
952 */
953static efi_status_t efi_capsule_delete_file(const u16 *filename)
954{
955 struct efi_file_handle *dirh, *fh;
956 efi_status_t ret;
957
958 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
959 EFI_CAPSULE_DIR,
960 EFI_FILE_MODE_READ, 0));
961 if (ret != EFI_SUCCESS)
962 return ret;
963 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
964 EFI_FILE_MODE_READ, 0));
965 /* ignore an error */
966 EFI_CALL((*dirh->close)(dirh));
967
Heinrich Schuchardte5c22812021-06-02 19:28:22 +0200968 if (ret == EFI_SUCCESS)
969 ret = EFI_CALL((*fh->delete)(fh));
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900970
971 return ret;
972}
973
974/**
975 * efi_capsule_scan_done - reset a scan help function
976 *
977 * Reset a scan help function
978 */
979static void efi_capsule_scan_done(void)
980{
981 EFI_CALL((*bootdev_root->close)(bootdev_root));
982 bootdev_root = NULL;
983}
984
985/**
Ilias Apalodimasfac773e2021-06-22 17:38:53 +0300986 * efi_load_capsule_drivers - initialize capsule drivers
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900987 *
Ilias Apalodimasfac773e2021-06-22 17:38:53 +0300988 * Generic FMP drivers backed by DFU
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900989 *
990 * Return: status code
991 */
Ilias Apalodimasfac773e2021-06-22 17:38:53 +0300992efi_status_t __weak efi_load_capsule_drivers(void)
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900993{
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900994 __maybe_unused efi_handle_t handle;
995 efi_status_t ret = EFI_SUCCESS;
996
997 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
998 handle = NULL;
999 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
1000 &handle, &efi_guid_firmware_management_protocol,
1001 &efi_fmp_fit, NULL));
1002 }
1003
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +09001004 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
1005 handle = NULL;
1006 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
Masami Hiramatsuf5388522021-06-22 17:38:51 +03001007 &handle,
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +09001008 &efi_guid_firmware_management_protocol,
1009 &efi_fmp_raw, NULL));
1010 }
1011
AKASHI Takahirof4818e62020-11-30 18:12:12 +09001012 return ret;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001013}
1014
1015/**
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001016 * check_run_capsules - Check whether capsule update should run
1017 *
1018 * The spec says OsIndications must be set in order to run the capsule update
1019 * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to
1020 * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected
1021 */
1022static bool check_run_capsules(void)
1023{
1024 u64 os_indications;
1025 efi_uintn_t size;
1026 efi_status_t ret;
1027
1028 if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS))
1029 return true;
1030
1031 size = sizeof(os_indications);
1032 ret = efi_get_variable_int(L"OsIndications", &efi_global_variable_guid,
1033 NULL, &size, &os_indications, NULL);
1034 if (ret == EFI_SUCCESS &&
1035 (os_indications
1036 & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED))
1037 return true;
1038
1039 return false;
1040}
1041
1042/**
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001043 * efi_launch_capsule - launch capsules
1044 *
1045 * Launch all the capsules in system at boot time.
1046 * Called by efi init code
1047 *
1048 * Return: status codde
1049 */
1050efi_status_t efi_launch_capsules(void)
1051{
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001052 struct efi_capsule_header *capsule = NULL;
1053 u16 **files;
1054 unsigned int nfiles, index, i;
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001055 efi_status_t ret;
1056
Ilias Apalodimasa38d0cb2021-06-29 07:55:51 +03001057 if (!check_run_capsules())
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001058 return EFI_SUCCESS;
1059
1060 index = get_last_capsule();
1061
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001062 /*
1063 * Find capsules on disk.
1064 * All the capsules are collected at the beginning because
1065 * capsule files will be removed instantly.
1066 */
1067 nfiles = 0;
1068 files = NULL;
1069 ret = efi_capsule_scan_dir(&files, &nfiles);
1070 if (ret != EFI_SUCCESS)
1071 return ret;
1072 if (!nfiles)
1073 return EFI_SUCCESS;
1074
1075 /* Launch capsules */
1076 for (i = 0, ++index; i < nfiles; i++, index++) {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001077 log_debug("Applying %ls\n", files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001078 if (index > 0xffff)
1079 index = 0;
1080 ret = efi_capsule_read_file(files[i], &capsule);
1081 if (ret == EFI_SUCCESS) {
1082 ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0));
1083 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001084 log_err("Applying capsule %ls failed\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001085 files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001086
1087 free(capsule);
1088 } else {
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001089 log_err("Reading capsule %ls failed\n", files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001090 }
1091 /* create CapsuleXXXX */
1092 set_capsule_result(index, capsule, ret);
1093
1094 /* delete a capsule either in case of success or failure */
1095 ret = efi_capsule_delete_file(files[i]);
1096 if (ret != EFI_SUCCESS)
Heinrich Schuchardte3087a12021-07-10 11:03:27 +02001097 log_err("Deleting capsule %ls failed\n",
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001098 files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001099 }
1100 efi_capsule_scan_done();
1101
1102 for (i = 0; i < nfiles; i++)
1103 free(files[i]);
1104 free(files);
1105
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001106 return ret;
1107}
1108#endif /* CONFIG_EFI_CAPSULE_ON_DISK */