blob: a984c347e1efd15d86d48f9434e3bf1a904b283b [file] [log] [blame]
AKASHI Takahiro2bc27ca2020-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 Takahiroc74cd8b2020-11-17 09:27:56 +090014#include <mapmem.h>
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090015#include <sort.h>
16
Sughosh Ganu04be98b2020-12-30 19:27:09 +053017#include <crypto/pkcs7.h>
18#include <crypto/pkcs7_parser.h>
19#include <linux/err.h>
20
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090021const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
AKASHI Takahiro8d990262020-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 Takahiro2bc27ca2020-11-17 09:27:55 +090026
AKASHI Takahiroc74cd8b2020-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 Takahiro2bc27ca2020-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 Schuchardt15bbcaf2021-02-09 20:20:34 +010045 char value[5];
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090046 efi_uintn_t size;
47 unsigned long index = 0xffff;
48 efi_status_t ret;
Heinrich Schuchardt15bbcaf2021-02-09 20:20:34 +010049 int i;
AKASHI Takahiro2bc27ca2020-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 Schuchardt15bbcaf2021-02-09 20:20:34 +010054 if (ret != EFI_SUCCESS || size != 22 ||
55 u16_strncmp(value16, L"Capsule", 7))
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090056 goto err;
Heinrich Schuchardt15bbcaf2021-02-09 20:20:34 +010057 for (i = 0; i < 4; ++i) {
58 u16 c = value16[i + 7];
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090059
Heinrich Schuchardt15bbcaf2021-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 Takahiro2bc27ca2020-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 Apalodimasfe179d72020-12-31 12:26:46 +020088 efi_create_indexed_name(variable_name16, sizeof(variable_name16),
89 "Capsule", index);
AKASHI Takahiro2bc27ca2020-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 Takahiro8d990262020-11-30 18:12:11 +0900104 log_err("EFI: creating %ls failed\n", variable_name16);
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900105}
106
AKASHI Takahiro8d990262020-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 Ganu04be98b2020-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 Takahiro8d990262020-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 Takahiro2bc27ca2020-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 Glassdf7d89a2021-02-07 14:27:02 -0700452 EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
AKASHI Takahiro2bc27ca2020-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 Takahiro8d990262020-11-30 18:12:11 +0900460 ret = EFI_SUCCESS;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900461 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
462 i++, capsule = *(++capsule_header_array)) {
AKASHI Takahiro8d990262020-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 Takahiro2bc27ca2020-11-17 09:27:55 +0900483 }
Jose Marinho64a8aae2021-03-02 17:26:38 +0000484
485 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
486 /* Rebuild the ESRT to reflect any updated FW images. */
487 ret = efi_esrt_populate();
488 if (ret != EFI_SUCCESS)
489 log_warning("EFI Capsule: failed to update ESRT\n");
490 }
Jose Marinho3627cf42021-04-19 14:54:33 +0100491out:
Jose Marinho64a8aae2021-03-02 17:26:38 +0000492
AKASHI Takahiro2bc27ca2020-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 Glassdf7d89a2021-02-07 14:27:02 -0700520 EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
AKASHI Takahiro2bc27ca2020-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 Takahiroc74cd8b2020-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 Takahiro8d990262020-11-30 18:12:11 +0900635 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroc74cd8b2020-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 Takahiro8d990262020-11-30 18:12:11 +0900693 log_debug("EFI Capsule: bootdev is %ls\n", path_str);
AKASHI Takahiroc74cd8b2020-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 Takahiro8d990262020-11-30 18:12:11 +0900733 log_debug("EFI Capsule: bootdev is not set\n");
AKASHI Takahiroc74cd8b2020-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) {
Heinrich Schuchardte8287b02021-04-11 06:53:04 +0200759 struct efi_file_info *old_dirent = dirent;
760
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900761 dirent = realloc(dirent, tmp_size);
762 if (!dirent) {
Heinrich Schuchardte8287b02021-04-11 06:53:04 +0200763 dirent = old_dirent;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900764 ret = EFI_OUT_OF_RESOURCES;
765 goto err;
766 }
767 dirent_size = tmp_size;
768 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
769 }
770 if (ret != EFI_SUCCESS)
771 goto err;
772 if (!tmp_size)
773 break;
774
Heinrich Schuchardt841f7a42021-02-09 17:45:33 +0100775 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900776 count++;
777 }
778
779 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
780 if (ret != EFI_SUCCESS)
781 goto err;
782
783 /* make a list */
AKASHI Takahiro8f1844c2021-01-22 10:43:27 +0900784 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900785 if (!tmp_files) {
786 ret = EFI_OUT_OF_RESOURCES;
787 goto err;
788 }
789
790 count = 0;
791 while (1) {
792 tmp_size = dirent_size;
793 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
794 if (ret != EFI_SUCCESS)
795 goto err;
796 if (!tmp_size)
797 break;
798
799 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
800 u16_strcmp(dirent->file_name, L".") &&
801 u16_strcmp(dirent->file_name, L".."))
802 tmp_files[count++] = u16_strdup(dirent->file_name);
803 }
804 /* ignore an error */
805 EFI_CALL((*dirh->close)(dirh));
806
807 /* in ascii order */
808 /* FIXME: u16 version of strcasecmp */
809 qsort(tmp_files, count, sizeof(*tmp_files),
810 (int (*)(const void *, const void *))strcasecmp);
811 *files = tmp_files;
812 *num = count;
813 ret = EFI_SUCCESS;
814err:
815 free(dirent);
816
817 return ret;
818}
819
820/**
821 * efi_capsule_read_file - read in a capsule file
822 * @filename: File name
823 * @capsule: Pointer to buffer for capsule
824 *
825 * Read a capsule file and put its content in @capsule.
826 *
827 * Return: status code
828 */
829static efi_status_t efi_capsule_read_file(const u16 *filename,
830 struct efi_capsule_header **capsule)
831{
832 struct efi_file_handle *dirh, *fh;
833 struct efi_file_info *file_info = NULL;
834 struct efi_capsule_header *buf = NULL;
835 efi_uintn_t size;
836 efi_status_t ret;
837
838 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
839 EFI_CAPSULE_DIR,
840 EFI_FILE_MODE_READ, 0));
841 if (ret != EFI_SUCCESS)
842 return ret;
843 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
844 EFI_FILE_MODE_READ, 0));
845 /* ignore an error */
846 EFI_CALL((*dirh->close)(dirh));
847 if (ret != EFI_SUCCESS)
848 return ret;
849
850 /* file size */
851 size = 0;
852 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
853 &size, file_info));
854 if (ret == EFI_BUFFER_TOO_SMALL) {
855 file_info = malloc(size);
856 if (!file_info) {
857 ret = EFI_OUT_OF_RESOURCES;
858 goto err;
859 }
860 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
861 &size, file_info));
862 }
863 if (ret != EFI_SUCCESS)
864 goto err;
865 size = file_info->file_size;
866 free(file_info);
867 buf = malloc(size);
868 if (!buf) {
869 ret = EFI_OUT_OF_RESOURCES;
870 goto err;
871 }
872
873 /* fetch data */
874 ret = EFI_CALL((*fh->read)(fh, &size, buf));
875 if (ret == EFI_SUCCESS) {
876 if (size >= buf->capsule_image_size) {
877 *capsule = buf;
878 } else {
879 free(buf);
880 ret = EFI_INVALID_PARAMETER;
881 }
882 } else {
883 free(buf);
884 }
885err:
886 EFI_CALL((*fh->close)(fh));
887
888 return ret;
889}
890
891/**
892 * efi_capsule_delete_file - delete a capsule file
893 * @filename: File name
894 *
895 * Delete a capsule file from capsule directory.
896 *
897 * Return: status code
898 */
899static efi_status_t efi_capsule_delete_file(const u16 *filename)
900{
901 struct efi_file_handle *dirh, *fh;
902 efi_status_t ret;
903
904 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
905 EFI_CAPSULE_DIR,
906 EFI_FILE_MODE_READ, 0));
907 if (ret != EFI_SUCCESS)
908 return ret;
909 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
910 EFI_FILE_MODE_READ, 0));
911 /* ignore an error */
912 EFI_CALL((*dirh->close)(dirh));
913
914 ret = EFI_CALL((*fh->delete)(fh));
915
916 return ret;
917}
918
919/**
920 * efi_capsule_scan_done - reset a scan help function
921 *
922 * Reset a scan help function
923 */
924static void efi_capsule_scan_done(void)
925{
926 EFI_CALL((*bootdev_root->close)(bootdev_root));
927 bootdev_root = NULL;
928}
929
930/**
931 * arch_efi_load_capsule_drivers - initialize capsule drivers
932 *
933 * Architecture or board specific initialization routine
934 *
935 * Return: status code
936 */
937efi_status_t __weak arch_efi_load_capsule_drivers(void)
938{
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900939 __maybe_unused efi_handle_t handle;
940 efi_status_t ret = EFI_SUCCESS;
941
942 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
943 handle = NULL;
944 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
945 &handle, &efi_guid_firmware_management_protocol,
946 &efi_fmp_fit, NULL));
947 }
948
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900949 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
950 handle = NULL;
951 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
952 &efi_root,
953 &efi_guid_firmware_management_protocol,
954 &efi_fmp_raw, NULL));
955 }
956
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900957 return ret;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900958}
959
960/**
961 * efi_launch_capsule - launch capsules
962 *
963 * Launch all the capsules in system at boot time.
964 * Called by efi init code
965 *
966 * Return: status codde
967 */
968efi_status_t efi_launch_capsules(void)
969{
970 u64 os_indications;
971 efi_uintn_t size;
972 struct efi_capsule_header *capsule = NULL;
973 u16 **files;
974 unsigned int nfiles, index, i;
975 u16 variable_name16[12];
976 efi_status_t ret;
977
978 size = sizeof(os_indications);
979 ret = efi_get_variable_int(L"OsIndications", &efi_global_variable_guid,
980 NULL, &size, &os_indications, NULL);
981 if (ret != EFI_SUCCESS ||
982 !(os_indications
983 & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED))
984 return EFI_SUCCESS;
985
986 index = get_last_capsule();
987
988 /* Load capsule drivers */
989 ret = arch_efi_load_capsule_drivers();
990 if (ret != EFI_SUCCESS)
991 return ret;
992
993 /*
994 * Find capsules on disk.
995 * All the capsules are collected at the beginning because
996 * capsule files will be removed instantly.
997 */
998 nfiles = 0;
999 files = NULL;
1000 ret = efi_capsule_scan_dir(&files, &nfiles);
1001 if (ret != EFI_SUCCESS)
1002 return ret;
1003 if (!nfiles)
1004 return EFI_SUCCESS;
1005
1006 /* Launch capsules */
1007 for (i = 0, ++index; i < nfiles; i++, index++) {
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001008 log_debug("capsule from %ls ...\n", files[i]);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001009 if (index > 0xffff)
1010 index = 0;
1011 ret = efi_capsule_read_file(files[i], &capsule);
1012 if (ret == EFI_SUCCESS) {
1013 ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0));
1014 if (ret != EFI_SUCCESS)
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001015 log_err("EFI Capsule update failed at %ls\n",
1016 files[i]);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001017
1018 free(capsule);
1019 } else {
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001020 log_err("EFI: reading capsule failed: %ls\n", files[i]);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001021 }
1022 /* create CapsuleXXXX */
1023 set_capsule_result(index, capsule, ret);
1024
1025 /* delete a capsule either in case of success or failure */
1026 ret = efi_capsule_delete_file(files[i]);
1027 if (ret != EFI_SUCCESS)
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001028 log_err("EFI: deleting a capsule file failed: %ls\n",
1029 files[i]);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001030 }
1031 efi_capsule_scan_done();
1032
1033 for (i = 0; i < nfiles; i++)
1034 free(files[i]);
1035 free(files);
1036
1037 /* CapsuleLast */
Ilias Apalodimasfe179d72020-12-31 12:26:46 +02001038 efi_create_indexed_name(variable_name16, sizeof(variable_name16),
1039 "Capsule", index - 1);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001040 efi_set_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
1041 EFI_VARIABLE_READ_ONLY |
1042 EFI_VARIABLE_NON_VOLATILE |
1043 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1044 EFI_VARIABLE_RUNTIME_ACCESS,
1045 22, variable_name16, false);
1046
1047 return ret;
1048}
1049#endif /* CONFIG_EFI_CAPSULE_ON_DISK */