blob: 9df9c35084c491f6960141497b6e4d86a592f2a8 [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
9#include <common.h>
10#include <efi_loader.h>
11#include <efi_variable.h>
12#include <fs.h>
13#include <malloc.h>
AKASHI Takahiro45b819542020-11-17 09:27:56 +090014#include <mapmem.h>
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090015#include <sort.h>
16
Sughosh Ganu586bb982020-12-30 19:27:09 +053017#include <crypto/pkcs7.h>
18#include <crypto/pkcs7_parser.h>
19#include <linux/err.h>
20
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090021const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
AKASHI Takahiro0d963782020-11-30 18:12:11 +090022static const efi_guid_t efi_guid_firmware_management_capsule_id =
23 EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
24const efi_guid_t efi_guid_firmware_management_protocol =
25 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090026
AKASHI Takahiro45b819542020-11-17 09:27:56 +090027#ifdef CONFIG_EFI_CAPSULE_ON_DISK
28/* for file system access */
29static struct efi_file_handle *bootdev_root;
30#endif
31
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090032/**
33 * get_last_capsule - get the last capsule index
34 *
35 * Retrieve the index of the capsule invoked last time from "CapsuleLast"
36 * variable.
37 *
38 * Return:
39 * * > 0 - the last capsule index invoked
40 * * 0xffff - on error, or no capsule invoked yet
41 */
42static __maybe_unused unsigned int get_last_capsule(void)
43{
44 u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010045 char value[5];
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090046 efi_uintn_t size;
47 unsigned long index = 0xffff;
48 efi_status_t ret;
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010049 int i;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090050
51 size = sizeof(value16);
52 ret = efi_get_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
53 NULL, &size, value16, NULL);
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010054 if (ret != EFI_SUCCESS || size != 22 ||
55 u16_strncmp(value16, L"Capsule", 7))
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090056 goto err;
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010057 for (i = 0; i < 4; ++i) {
58 u16 c = value16[i + 7];
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090059
Heinrich Schuchardt812f6e02021-02-09 20:20:34 +010060 if (!c || c > 0x7f)
61 goto err;
62 value[i] = c;
63 }
64 value[4] = 0;
65 if (strict_strtoul(value, 16, &index))
66 index = 0xffff;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090067err:
68 return index;
69}
70
71/**
72 * set_capsule_result - set a result variable
73 * @capsule: Capsule
74 * @return_status: Return status
75 *
76 * Create and set a result variable, "CapsuleXXXX", for the capsule,
77 * @capsule.
78 */
79static __maybe_unused
80void set_capsule_result(int index, struct efi_capsule_header *capsule,
81 efi_status_t return_status)
82{
83 u16 variable_name16[12];
84 struct efi_capsule_result_variable_header result;
85 struct efi_time time;
86 efi_status_t ret;
87
Ilias Apalodimas21575292020-12-31 12:26:46 +020088 efi_create_indexed_name(variable_name16, sizeof(variable_name16),
89 "Capsule", index);
AKASHI Takahiro473d9b32020-11-17 09:27:55 +090090 result.variable_total_size = sizeof(result);
91 result.capsule_guid = capsule->capsule_guid;
92 ret = EFI_CALL((*efi_runtime_services.get_time)(&time, NULL));
93 if (ret == EFI_SUCCESS)
94 memcpy(&result.capsule_processed, &time, sizeof(time));
95 else
96 memset(&result.capsule_processed, 0, sizeof(time));
97 result.capsule_status = return_status;
98 ret = efi_set_variable(variable_name16, &efi_guid_capsule_report,
99 EFI_VARIABLE_NON_VOLATILE |
100 EFI_VARIABLE_BOOTSERVICE_ACCESS |
101 EFI_VARIABLE_RUNTIME_ACCESS,
102 sizeof(result), &result);
103 if (ret)
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900104 log_err("EFI: creating %ls failed\n", variable_name16);
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900105}
106
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900107#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT
108/**
109 * efi_fmp_find - search for Firmware Management Protocol drivers
110 * @image_type: Image type guid
111 * @instance: Instance number
112 * @handles: Handles of FMP drivers
113 * @no_handles: Number of handles
114 *
115 * Search for Firmware Management Protocol drivers, matching the image
116 * type, @image_type and the machine instance, @instance, from the list,
117 * @handles.
118 *
119 * Return:
120 * * Protocol instance - on success
121 * * NULL - on failure
122 */
123static struct efi_firmware_management_protocol *
124efi_fmp_find(efi_guid_t *image_type, u64 instance, efi_handle_t *handles,
125 efi_uintn_t no_handles)
126{
127 efi_handle_t *handle;
128 struct efi_firmware_management_protocol *fmp;
129 struct efi_firmware_image_descriptor *image_info, *desc;
130 efi_uintn_t info_size, descriptor_size;
131 u32 descriptor_version;
132 u8 descriptor_count;
133 u32 package_version;
134 u16 *package_version_name;
135 bool found = false;
136 int i, j;
137 efi_status_t ret;
138
139 for (i = 0, handle = handles; i < no_handles; i++, handle++) {
140 ret = EFI_CALL(efi_handle_protocol(
141 *handle,
142 &efi_guid_firmware_management_protocol,
143 (void **)&fmp));
144 if (ret != EFI_SUCCESS)
145 continue;
146
147 /* get device's image info */
148 info_size = 0;
149 image_info = NULL;
150 descriptor_version = 0;
151 descriptor_count = 0;
152 descriptor_size = 0;
153 package_version = 0;
154 package_version_name = NULL;
155 ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
156 image_info,
157 &descriptor_version,
158 &descriptor_count,
159 &descriptor_size,
160 &package_version,
161 &package_version_name));
162 if (ret != EFI_BUFFER_TOO_SMALL)
163 goto skip;
164
165 image_info = malloc(info_size);
166 if (!image_info)
167 goto skip;
168
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_SUCCESS ||
177 descriptor_version != EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION)
178 goto skip;
179
180 /* matching */
181 for (j = 0, desc = image_info; j < descriptor_count;
182 j++, desc = (void *)desc + descriptor_size) {
183 log_debug("+++ desc[%d] index: %d, name: %ls\n",
184 j, desc->image_index, desc->image_id_name);
185 if (!guidcmp(&desc->image_type_id, image_type) &&
186 (!instance ||
187 !desc->hardware_instance ||
188 desc->hardware_instance == instance))
189 found = true;
190 }
191
192skip:
193 efi_free_pool(package_version_name);
194 free(image_info);
195 EFI_CALL(efi_close_protocol(
196 (efi_handle_t)fmp,
197 &efi_guid_firmware_management_protocol,
198 NULL, NULL));
199 if (found)
200 return fmp;
201 }
202
203 return NULL;
204}
205
Sughosh Ganu586bb982020-12-30 19:27:09 +0530206#if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
207
208const efi_guid_t efi_guid_capsule_root_cert_guid =
209 EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
210
211__weak int efi_get_public_key_data(void **pkey, efi_uintn_t *pkey_len)
212{
213 /* The platform is supposed to provide
214 * a method for getting the public key
215 * stored in the form of efi signature
216 * list
217 */
218 return 0;
219}
220
221efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
222 void **image, efi_uintn_t *image_size)
223{
224 u8 *buf;
225 int ret;
226 void *fdt_pkey, *pkey;
227 efi_uintn_t pkey_len;
228 uint64_t monotonic_count;
229 struct efi_signature_store *truststore;
230 struct pkcs7_message *capsule_sig;
231 struct efi_image_regions *regs;
232 struct efi_firmware_image_authentication *auth_hdr;
233 efi_status_t status;
234
235 status = EFI_SECURITY_VIOLATION;
236 capsule_sig = NULL;
237 truststore = NULL;
238 regs = NULL;
239
240 /* Sanity checks */
241 if (capsule == NULL || capsule_size == 0)
242 goto out;
243
244 auth_hdr = (struct efi_firmware_image_authentication *)capsule;
245 if (capsule_size < sizeof(*auth_hdr))
246 goto out;
247
248 if (auth_hdr->auth_info.hdr.dwLength <=
249 offsetof(struct win_certificate_uefi_guid, cert_data))
250 goto out;
251
252 if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
253 goto out;
254
255 *image = (uint8_t *)capsule + sizeof(auth_hdr->monotonic_count) +
256 auth_hdr->auth_info.hdr.dwLength;
257 *image_size = capsule_size - auth_hdr->auth_info.hdr.dwLength -
258 sizeof(auth_hdr->monotonic_count);
259 memcpy(&monotonic_count, &auth_hdr->monotonic_count,
260 sizeof(monotonic_count));
261
262 /* data to be digested */
263 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 2, 1);
264 if (!regs)
265 goto out;
266
267 regs->max = 2;
268 efi_image_region_add(regs, (uint8_t *)*image,
269 (uint8_t *)*image + *image_size, 1);
270
271 efi_image_region_add(regs, (uint8_t *)&monotonic_count,
272 (uint8_t *)&monotonic_count + sizeof(monotonic_count),
273 1);
274
275 capsule_sig = efi_parse_pkcs7_header(auth_hdr->auth_info.cert_data,
276 auth_hdr->auth_info.hdr.dwLength
277 - sizeof(auth_hdr->auth_info),
278 &buf);
279 if (IS_ERR(capsule_sig)) {
280 debug("Parsing variable's pkcs7 header failed\n");
281 capsule_sig = NULL;
282 goto out;
283 }
284
285 ret = efi_get_public_key_data(&fdt_pkey, &pkey_len);
286 if (ret < 0)
287 goto out;
288
289 pkey = malloc(pkey_len);
290 if (!pkey)
291 goto out;
292
293 memcpy(pkey, fdt_pkey, pkey_len);
294 truststore = efi_build_signature_store(pkey, pkey_len);
295 if (!truststore)
296 goto out;
297
298 /* verify signature */
299 if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) {
300 debug("Verified\n");
301 } else {
302 debug("Verifying variable's signature failed\n");
303 goto out;
304 }
305
306 status = EFI_SUCCESS;
307
308out:
309 efi_sigstore_free(truststore);
310 pkcs7_free_message(capsule_sig);
311 free(regs);
312
313 return status;
314}
315#else
316efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
317 void **image, efi_uintn_t *image_size)
318{
319 return EFI_UNSUPPORTED;
320}
321#endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */
322
323
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900324/**
325 * efi_capsule_update_firmware - update firmware from capsule
326 * @capsule_data: Capsule
327 *
328 * Update firmware, using a capsule, @capsule_data. Loading any FMP
329 * drivers embedded in a capsule is not supported.
330 *
331 * Return: status code
332 */
333static efi_status_t efi_capsule_update_firmware(
334 struct efi_capsule_header *capsule_data)
335{
336 struct efi_firmware_management_capsule_header *capsule;
337 struct efi_firmware_management_capsule_image_header *image;
338 size_t capsule_size;
339 void *image_binary, *vendor_code;
340 efi_handle_t *handles;
341 efi_uintn_t no_handles;
342 int item;
343 struct efi_firmware_management_protocol *fmp;
344 u16 *abort_reason;
345 efi_status_t ret = EFI_SUCCESS;
346
347 /* sanity check */
348 if (capsule_data->header_size < sizeof(*capsule) ||
349 capsule_data->header_size >= capsule_data->capsule_image_size)
350 return EFI_INVALID_PARAMETER;
351
352 capsule = (void *)capsule_data + capsule_data->header_size;
353 capsule_size = capsule_data->capsule_image_size
354 - capsule_data->header_size;
355
356 if (capsule->version != 0x00000001)
357 return EFI_UNSUPPORTED;
358
359 handles = NULL;
360 ret = EFI_CALL(efi_locate_handle_buffer(
361 BY_PROTOCOL,
362 &efi_guid_firmware_management_protocol,
363 NULL, &no_handles, (efi_handle_t **)&handles));
364 if (ret != EFI_SUCCESS)
365 return EFI_UNSUPPORTED;
366
367 /* Payload */
368 for (item = capsule->embedded_driver_count;
369 item < capsule->embedded_driver_count
370 + capsule->payload_item_count; item++) {
371 /* sanity check */
372 if ((capsule->item_offset_list[item] + sizeof(*image)
373 >= capsule_size)) {
374 log_err("EFI: A capsule has not enough data\n");
375 ret = EFI_INVALID_PARAMETER;
376 goto out;
377 }
378
379 image = (void *)capsule + capsule->item_offset_list[item];
380
381 if (image->version != 0x00000003) {
382 ret = EFI_UNSUPPORTED;
383 goto out;
384 }
385
386 /* find a device for update firmware */
387 /* TODO: should we pass index as well, or nothing but type? */
388 fmp = efi_fmp_find(&image->update_image_type_id,
389 image->update_hardware_instance,
390 handles, no_handles);
391 if (!fmp) {
392 log_err("EFI Capsule: driver not found for firmware type: %pUl, hardware instance: %lld\n",
393 &image->update_image_type_id,
394 image->update_hardware_instance);
395 ret = EFI_UNSUPPORTED;
396 goto out;
397 }
398
399 /* do update */
400 image_binary = (void *)image + sizeof(*image);
401 vendor_code = image_binary + image->update_image_size;
402
403 abort_reason = NULL;
404 ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
405 image_binary,
406 image->update_image_size,
407 vendor_code, NULL,
408 &abort_reason));
409 if (ret != EFI_SUCCESS) {
410 log_err("EFI Capsule: firmware update failed: %ls\n",
411 abort_reason);
412 efi_free_pool(abort_reason);
413 goto out;
414 }
415 }
416
417out:
418 efi_free_pool(handles);
419
420 return ret;
421}
422#else
423static efi_status_t efi_capsule_update_firmware(
424 struct efi_capsule_header *capsule_data)
425{
426 return EFI_UNSUPPORTED;
427}
428#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
429
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900430/**
431 * efi_update_capsule() - process information from operating system
432 * @capsule_header_array: Array of virtual address pointers
433 * @capsule_count: Number of pointers in capsule_header_array
434 * @scatter_gather_list: Array of physical address pointers
435 *
436 * This function implements the UpdateCapsule() runtime service.
437 *
438 * See the Unified Extensible Firmware Interface (UEFI) specification for
439 * details.
440 *
441 * Return: status code
442 */
443efi_status_t EFIAPI efi_update_capsule(
444 struct efi_capsule_header **capsule_header_array,
445 efi_uintn_t capsule_count,
446 u64 scatter_gather_list)
447{
448 struct efi_capsule_header *capsule;
449 unsigned int i;
450 efi_status_t ret;
451
Simon Glass83698b22021-02-07 14:27:02 -0700452 EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900453 scatter_gather_list);
454
455 if (!capsule_count) {
456 ret = EFI_INVALID_PARAMETER;
457 goto out;
458 }
459
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900460 ret = EFI_SUCCESS;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900461 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
462 i++, capsule = *(++capsule_header_array)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900463 /* sanity check */
464 if (capsule->header_size < sizeof(*capsule) ||
465 capsule->capsule_image_size < sizeof(*capsule)) {
466 log_err("EFI: A capsule has not enough data\n");
467 continue;
468 }
469
470 log_debug("Capsule[%d] (guid:%pUl)\n",
471 i, &capsule->capsule_guid);
472 if (!guidcmp(&capsule->capsule_guid,
473 &efi_guid_firmware_management_capsule_id)) {
474 ret = efi_capsule_update_firmware(capsule);
475 } else {
476 log_err("EFI: not support capsule type: %pUl\n",
477 &capsule->capsule_guid);
478 ret = EFI_UNSUPPORTED;
479 }
480
481 if (ret != EFI_SUCCESS)
482 goto out;
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900483 }
484out:
Jose Marinhoebb61ee2021-03-02 17:26:38 +0000485
486 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
487 /* Rebuild the ESRT to reflect any updated FW images. */
488 ret = efi_esrt_populate();
489 if (ret != EFI_SUCCESS)
490 log_warning("EFI Capsule: failed to update ESRT\n");
491 }
492
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900493 return EFI_EXIT(ret);
494}
495
496/**
497 * efi_query_capsule_caps() - check if capsule is supported
498 * @capsule_header_array: Array of virtual pointers
499 * @capsule_count: Number of pointers in capsule_header_array
500 * @maximum_capsule_size: Maximum capsule size
501 * @reset_type: Type of reset needed for capsule update
502 *
503 * This function implements the QueryCapsuleCapabilities() runtime service.
504 *
505 * See the Unified Extensible Firmware Interface (UEFI) specification for
506 * details.
507 *
508 * Return: status code
509 */
510efi_status_t EFIAPI efi_query_capsule_caps(
511 struct efi_capsule_header **capsule_header_array,
512 efi_uintn_t capsule_count,
513 u64 *maximum_capsule_size,
514 u32 *reset_type)
515{
516 struct efi_capsule_header *capsule __attribute__((unused));
517 unsigned int i;
518 efi_status_t ret;
519
Simon Glass83698b22021-02-07 14:27:02 -0700520 EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
AKASHI Takahiro473d9b32020-11-17 09:27:55 +0900521 maximum_capsule_size, reset_type);
522
523 if (!maximum_capsule_size) {
524 ret = EFI_INVALID_PARAMETER;
525 goto out;
526 }
527
528 *maximum_capsule_size = U64_MAX;
529 *reset_type = EFI_RESET_COLD;
530
531 ret = EFI_SUCCESS;
532 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
533 i++, capsule = *(++capsule_header_array)) {
534 /* TODO */
535 }
536out:
537 return EFI_EXIT(ret);
538}
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900539
540#ifdef CONFIG_EFI_CAPSULE_ON_DISK
541/**
542 * get_dp_device - retrieve a device path from boot variable
543 * @boot_var: Boot variable name
544 * @device_dp Device path
545 *
546 * Retrieve a device patch from boot variable, @boot_var.
547 *
548 * Return: status code
549 */
550static efi_status_t get_dp_device(u16 *boot_var,
551 struct efi_device_path **device_dp)
552{
553 void *buf = NULL;
554 efi_uintn_t size;
555 struct efi_load_option lo;
556 struct efi_device_path *file_dp;
557 efi_status_t ret;
558
559 size = 0;
560 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
561 NULL, &size, NULL, NULL);
562 if (ret == EFI_BUFFER_TOO_SMALL) {
563 buf = malloc(size);
564 if (!buf)
565 return EFI_OUT_OF_RESOURCES;
566 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
567 NULL, &size, buf, NULL);
568 }
569 if (ret != EFI_SUCCESS)
570 return ret;
571
572 efi_deserialize_load_option(&lo, buf, &size);
573
574 if (lo.attributes & LOAD_OPTION_ACTIVE) {
575 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
576 efi_free_pool(file_dp);
577
578 ret = EFI_SUCCESS;
579 } else {
580 ret = EFI_NOT_FOUND;
581 }
582
583 free(buf);
584
585 return ret;
586}
587
588/**
589 * device_is_present_and_system_part - check if a device exists
590 * @dp Device path
591 *
592 * Check if a device pointed to by the device path, @dp, exists and is
593 * located in UEFI system partition.
594 *
595 * Return: true - yes, false - no
596 */
597static bool device_is_present_and_system_part(struct efi_device_path *dp)
598{
599 efi_handle_t handle;
600
601 handle = efi_dp_find_obj(dp, NULL);
602 if (!handle)
603 return false;
604
605 return efi_disk_is_system_part(handle);
606}
607
608/**
609 * find_boot_device - identify the boot device
610 *
611 * Identify the boot device from boot-related variables as UEFI
612 * specification describes and put its handle into bootdev_root.
613 *
614 * Return: status code
615 */
616static efi_status_t find_boot_device(void)
617{
618 char boot_var[9];
619 u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
620 efi_uintn_t size;
621 int i, num;
622 struct efi_simple_file_system_protocol *volume;
623 struct efi_device_path *boot_dev = NULL;
624 efi_status_t ret;
625
626 /* find active boot device in BootNext */
627 bootnext = 0;
628 size = sizeof(bootnext);
629 ret = efi_get_variable_int(L"BootNext",
630 (efi_guid_t *)&efi_global_variable_guid,
631 NULL, &size, &bootnext, NULL);
632 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
633 /* BootNext does exist here */
634 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900635 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900636 goto skip;
637 }
638 sprintf((char *)boot_var, "Boot%04X", bootnext);
639 p = boot_var16;
640 utf8_utf16_strcpy(&p, boot_var);
641
642 ret = get_dp_device(boot_var16, &boot_dev);
643 if (ret == EFI_SUCCESS) {
644 if (device_is_present_and_system_part(boot_dev)) {
645 goto out;
646 } else {
647 efi_free_pool(boot_dev);
648 boot_dev = NULL;
649 }
650 }
651 }
652
653skip:
654 /* find active boot device in BootOrder */
655 size = 0;
656 ret = efi_get_variable_int(L"BootOrder", &efi_global_variable_guid,
657 NULL, &size, NULL, NULL);
658 if (ret == EFI_BUFFER_TOO_SMALL) {
659 boot_order = malloc(size);
660 if (!boot_order) {
661 ret = EFI_OUT_OF_RESOURCES;
662 goto out;
663 }
664
665 ret = efi_get_variable_int(L"BootOrder",
666 &efi_global_variable_guid,
667 NULL, &size, boot_order, NULL);
668 }
669 if (ret != EFI_SUCCESS)
670 goto out;
671
672 /* check in higher order */
673 num = size / sizeof(u16);
674 for (i = 0; i < num; i++) {
675 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
676 p = boot_var16;
677 utf8_utf16_strcpy(&p, boot_var);
678 ret = get_dp_device(boot_var16, &boot_dev);
679 if (ret != EFI_SUCCESS)
680 continue;
681
682 if (device_is_present_and_system_part(boot_dev))
683 break;
684
685 efi_free_pool(boot_dev);
686 boot_dev = NULL;
687 }
688out:
689 if (boot_dev) {
690 u16 *path_str;
691
692 path_str = efi_dp_str(boot_dev);
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900693 log_debug("EFI Capsule: bootdev is %ls\n", path_str);
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900694 efi_free_pool(path_str);
695
696 volume = efi_fs_from_path(boot_dev);
697 if (!volume)
698 ret = EFI_DEVICE_ERROR;
699 else
700 ret = EFI_CALL(volume->open_volume(volume,
701 &bootdev_root));
702 efi_free_pool(boot_dev);
703 } else {
704 ret = EFI_NOT_FOUND;
705 }
706 free(boot_order);
707
708 return ret;
709}
710
711/**
712 * efi_capsule_scan_dir - traverse a capsule directory in boot device
713 * @files: Array of file names
714 * @num: Number of elements in @files
715 *
716 * Traverse a capsule directory in boot device.
717 * Called by initialization code, and returns an array of capsule file
718 * names in @files.
719 *
720 * Return: status code
721 */
722static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
723{
724 struct efi_file_handle *dirh;
725 struct efi_file_info *dirent;
726 efi_uintn_t dirent_size, tmp_size;
727 unsigned int count;
728 u16 **tmp_files;
729 efi_status_t ret;
730
731 ret = find_boot_device();
732 if (ret == EFI_NOT_FOUND) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900733 log_debug("EFI Capsule: bootdev is not set\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900734 *num = 0;
735 return EFI_SUCCESS;
736 } else if (ret != EFI_SUCCESS) {
737 return EFI_DEVICE_ERROR;
738 }
739
740 /* count capsule files */
741 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
742 EFI_CAPSULE_DIR,
743 EFI_FILE_MODE_READ, 0));
744 if (ret != EFI_SUCCESS) {
745 *num = 0;
746 return EFI_SUCCESS;
747 }
748
749 dirent_size = 256;
750 dirent = malloc(dirent_size);
751 if (!dirent)
752 return EFI_OUT_OF_RESOURCES;
753
754 count = 0;
755 while (1) {
756 tmp_size = dirent_size;
757 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
758 if (ret == EFI_BUFFER_TOO_SMALL) {
759 dirent = realloc(dirent, tmp_size);
760 if (!dirent) {
761 ret = EFI_OUT_OF_RESOURCES;
762 goto err;
763 }
764 dirent_size = tmp_size;
765 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
766 }
767 if (ret != EFI_SUCCESS)
768 goto err;
769 if (!tmp_size)
770 break;
771
Heinrich Schuchardt76b708a2021-02-09 17:45:33 +0100772 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900773 count++;
774 }
775
776 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
777 if (ret != EFI_SUCCESS)
778 goto err;
779
780 /* make a list */
AKASHI Takahiroc8fc12f2021-01-22 10:43:27 +0900781 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900782 if (!tmp_files) {
783 ret = EFI_OUT_OF_RESOURCES;
784 goto err;
785 }
786
787 count = 0;
788 while (1) {
789 tmp_size = dirent_size;
790 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
791 if (ret != EFI_SUCCESS)
792 goto err;
793 if (!tmp_size)
794 break;
795
796 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
797 u16_strcmp(dirent->file_name, L".") &&
798 u16_strcmp(dirent->file_name, L".."))
799 tmp_files[count++] = u16_strdup(dirent->file_name);
800 }
801 /* ignore an error */
802 EFI_CALL((*dirh->close)(dirh));
803
804 /* in ascii order */
805 /* FIXME: u16 version of strcasecmp */
806 qsort(tmp_files, count, sizeof(*tmp_files),
807 (int (*)(const void *, const void *))strcasecmp);
808 *files = tmp_files;
809 *num = count;
810 ret = EFI_SUCCESS;
811err:
812 free(dirent);
813
814 return ret;
815}
816
817/**
818 * efi_capsule_read_file - read in a capsule file
819 * @filename: File name
820 * @capsule: Pointer to buffer for capsule
821 *
822 * Read a capsule file and put its content in @capsule.
823 *
824 * Return: status code
825 */
826static efi_status_t efi_capsule_read_file(const u16 *filename,
827 struct efi_capsule_header **capsule)
828{
829 struct efi_file_handle *dirh, *fh;
830 struct efi_file_info *file_info = NULL;
831 struct efi_capsule_header *buf = NULL;
832 efi_uintn_t size;
833 efi_status_t ret;
834
835 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
836 EFI_CAPSULE_DIR,
837 EFI_FILE_MODE_READ, 0));
838 if (ret != EFI_SUCCESS)
839 return ret;
840 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
841 EFI_FILE_MODE_READ, 0));
842 /* ignore an error */
843 EFI_CALL((*dirh->close)(dirh));
844 if (ret != EFI_SUCCESS)
845 return ret;
846
847 /* file size */
848 size = 0;
849 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
850 &size, file_info));
851 if (ret == EFI_BUFFER_TOO_SMALL) {
852 file_info = malloc(size);
853 if (!file_info) {
854 ret = EFI_OUT_OF_RESOURCES;
855 goto err;
856 }
857 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
858 &size, file_info));
859 }
860 if (ret != EFI_SUCCESS)
861 goto err;
862 size = file_info->file_size;
863 free(file_info);
864 buf = malloc(size);
865 if (!buf) {
866 ret = EFI_OUT_OF_RESOURCES;
867 goto err;
868 }
869
870 /* fetch data */
871 ret = EFI_CALL((*fh->read)(fh, &size, buf));
872 if (ret == EFI_SUCCESS) {
873 if (size >= buf->capsule_image_size) {
874 *capsule = buf;
875 } else {
876 free(buf);
877 ret = EFI_INVALID_PARAMETER;
878 }
879 } else {
880 free(buf);
881 }
882err:
883 EFI_CALL((*fh->close)(fh));
884
885 return ret;
886}
887
888/**
889 * efi_capsule_delete_file - delete a capsule file
890 * @filename: File name
891 *
892 * Delete a capsule file from capsule directory.
893 *
894 * Return: status code
895 */
896static efi_status_t efi_capsule_delete_file(const u16 *filename)
897{
898 struct efi_file_handle *dirh, *fh;
899 efi_status_t ret;
900
901 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
902 EFI_CAPSULE_DIR,
903 EFI_FILE_MODE_READ, 0));
904 if (ret != EFI_SUCCESS)
905 return ret;
906 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
907 EFI_FILE_MODE_READ, 0));
908 /* ignore an error */
909 EFI_CALL((*dirh->close)(dirh));
910
911 ret = EFI_CALL((*fh->delete)(fh));
912
913 return ret;
914}
915
916/**
917 * efi_capsule_scan_done - reset a scan help function
918 *
919 * Reset a scan help function
920 */
921static void efi_capsule_scan_done(void)
922{
923 EFI_CALL((*bootdev_root->close)(bootdev_root));
924 bootdev_root = NULL;
925}
926
927/**
928 * arch_efi_load_capsule_drivers - initialize capsule drivers
929 *
930 * Architecture or board specific initialization routine
931 *
932 * Return: status code
933 */
934efi_status_t __weak arch_efi_load_capsule_drivers(void)
935{
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900936 __maybe_unused efi_handle_t handle;
937 efi_status_t ret = EFI_SUCCESS;
938
939 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
940 handle = NULL;
941 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
942 &handle, &efi_guid_firmware_management_protocol,
943 &efi_fmp_fit, NULL));
944 }
945
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900946 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
947 handle = NULL;
948 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
949 &efi_root,
950 &efi_guid_firmware_management_protocol,
951 &efi_fmp_raw, NULL));
952 }
953
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900954 return ret;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900955}
956
957/**
958 * efi_launch_capsule - launch capsules
959 *
960 * Launch all the capsules in system at boot time.
961 * Called by efi init code
962 *
963 * Return: status codde
964 */
965efi_status_t efi_launch_capsules(void)
966{
967 u64 os_indications;
968 efi_uintn_t size;
969 struct efi_capsule_header *capsule = NULL;
970 u16 **files;
971 unsigned int nfiles, index, i;
972 u16 variable_name16[12];
973 efi_status_t ret;
974
975 size = sizeof(os_indications);
976 ret = efi_get_variable_int(L"OsIndications", &efi_global_variable_guid,
977 NULL, &size, &os_indications, NULL);
978 if (ret != EFI_SUCCESS ||
979 !(os_indications
980 & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED))
981 return EFI_SUCCESS;
982
983 index = get_last_capsule();
984
985 /* Load capsule drivers */
986 ret = arch_efi_load_capsule_drivers();
987 if (ret != EFI_SUCCESS)
988 return ret;
989
990 /*
991 * Find capsules on disk.
992 * All the capsules are collected at the beginning because
993 * capsule files will be removed instantly.
994 */
995 nfiles = 0;
996 files = NULL;
997 ret = efi_capsule_scan_dir(&files, &nfiles);
998 if (ret != EFI_SUCCESS)
999 return ret;
1000 if (!nfiles)
1001 return EFI_SUCCESS;
1002
1003 /* Launch capsules */
1004 for (i = 0, ++index; i < nfiles; i++, index++) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001005 log_debug("capsule from %ls ...\n", files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001006 if (index > 0xffff)
1007 index = 0;
1008 ret = efi_capsule_read_file(files[i], &capsule);
1009 if (ret == EFI_SUCCESS) {
1010 ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0));
1011 if (ret != EFI_SUCCESS)
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001012 log_err("EFI Capsule update failed at %ls\n",
1013 files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001014
1015 free(capsule);
1016 } else {
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001017 log_err("EFI: reading capsule failed: %ls\n", files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001018 }
1019 /* create CapsuleXXXX */
1020 set_capsule_result(index, capsule, ret);
1021
1022 /* delete a capsule either in case of success or failure */
1023 ret = efi_capsule_delete_file(files[i]);
1024 if (ret != EFI_SUCCESS)
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001025 log_err("EFI: deleting a capsule file failed: %ls\n",
1026 files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001027 }
1028 efi_capsule_scan_done();
1029
1030 for (i = 0; i < nfiles; i++)
1031 free(files[i]);
1032 free(files);
1033
1034 /* CapsuleLast */
Ilias Apalodimas21575292020-12-31 12:26:46 +02001035 efi_create_indexed_name(variable_name16, sizeof(variable_name16),
1036 "Capsule", index - 1);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001037 efi_set_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
1038 EFI_VARIABLE_READ_ONLY |
1039 EFI_VARIABLE_NON_VOLATILE |
1040 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1041 EFI_VARIABLE_RUNTIME_ACCESS,
1042 22, variable_name16, false);
1043
1044 return ret;
1045}
1046#endif /* CONFIG_EFI_CAPSULE_ON_DISK */