blob: b57f0302c5998d2b4d5c1ef69bbd8b07dc7ad4d4 [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
452 EFI_ENTRY("%p, %lu, %llu\n", capsule_header_array, capsule_count,
453 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:
485 return EFI_EXIT(ret);
486}
487
488/**
489 * efi_query_capsule_caps() - check if capsule is supported
490 * @capsule_header_array: Array of virtual pointers
491 * @capsule_count: Number of pointers in capsule_header_array
492 * @maximum_capsule_size: Maximum capsule size
493 * @reset_type: Type of reset needed for capsule update
494 *
495 * This function implements the QueryCapsuleCapabilities() runtime service.
496 *
497 * See the Unified Extensible Firmware Interface (UEFI) specification for
498 * details.
499 *
500 * Return: status code
501 */
502efi_status_t EFIAPI efi_query_capsule_caps(
503 struct efi_capsule_header **capsule_header_array,
504 efi_uintn_t capsule_count,
505 u64 *maximum_capsule_size,
506 u32 *reset_type)
507{
508 struct efi_capsule_header *capsule __attribute__((unused));
509 unsigned int i;
510 efi_status_t ret;
511
512 EFI_ENTRY("%p, %lu, %p, %p\n", capsule_header_array, capsule_count,
513 maximum_capsule_size, reset_type);
514
515 if (!maximum_capsule_size) {
516 ret = EFI_INVALID_PARAMETER;
517 goto out;
518 }
519
520 *maximum_capsule_size = U64_MAX;
521 *reset_type = EFI_RESET_COLD;
522
523 ret = EFI_SUCCESS;
524 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
525 i++, capsule = *(++capsule_header_array)) {
526 /* TODO */
527 }
528out:
529 return EFI_EXIT(ret);
530}
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900531
532#ifdef CONFIG_EFI_CAPSULE_ON_DISK
533/**
534 * get_dp_device - retrieve a device path from boot variable
535 * @boot_var: Boot variable name
536 * @device_dp Device path
537 *
538 * Retrieve a device patch from boot variable, @boot_var.
539 *
540 * Return: status code
541 */
542static efi_status_t get_dp_device(u16 *boot_var,
543 struct efi_device_path **device_dp)
544{
545 void *buf = NULL;
546 efi_uintn_t size;
547 struct efi_load_option lo;
548 struct efi_device_path *file_dp;
549 efi_status_t ret;
550
551 size = 0;
552 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
553 NULL, &size, NULL, NULL);
554 if (ret == EFI_BUFFER_TOO_SMALL) {
555 buf = malloc(size);
556 if (!buf)
557 return EFI_OUT_OF_RESOURCES;
558 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
559 NULL, &size, buf, NULL);
560 }
561 if (ret != EFI_SUCCESS)
562 return ret;
563
564 efi_deserialize_load_option(&lo, buf, &size);
565
566 if (lo.attributes & LOAD_OPTION_ACTIVE) {
567 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
568 efi_free_pool(file_dp);
569
570 ret = EFI_SUCCESS;
571 } else {
572 ret = EFI_NOT_FOUND;
573 }
574
575 free(buf);
576
577 return ret;
578}
579
580/**
581 * device_is_present_and_system_part - check if a device exists
582 * @dp Device path
583 *
584 * Check if a device pointed to by the device path, @dp, exists and is
585 * located in UEFI system partition.
586 *
587 * Return: true - yes, false - no
588 */
589static bool device_is_present_and_system_part(struct efi_device_path *dp)
590{
591 efi_handle_t handle;
592
593 handle = efi_dp_find_obj(dp, NULL);
594 if (!handle)
595 return false;
596
597 return efi_disk_is_system_part(handle);
598}
599
600/**
601 * find_boot_device - identify the boot device
602 *
603 * Identify the boot device from boot-related variables as UEFI
604 * specification describes and put its handle into bootdev_root.
605 *
606 * Return: status code
607 */
608static efi_status_t find_boot_device(void)
609{
610 char boot_var[9];
611 u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
612 efi_uintn_t size;
613 int i, num;
614 struct efi_simple_file_system_protocol *volume;
615 struct efi_device_path *boot_dev = NULL;
616 efi_status_t ret;
617
618 /* find active boot device in BootNext */
619 bootnext = 0;
620 size = sizeof(bootnext);
621 ret = efi_get_variable_int(L"BootNext",
622 (efi_guid_t *)&efi_global_variable_guid,
623 NULL, &size, &bootnext, NULL);
624 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
625 /* BootNext does exist here */
626 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900627 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900628 goto skip;
629 }
630 sprintf((char *)boot_var, "Boot%04X", bootnext);
631 p = boot_var16;
632 utf8_utf16_strcpy(&p, boot_var);
633
634 ret = get_dp_device(boot_var16, &boot_dev);
635 if (ret == EFI_SUCCESS) {
636 if (device_is_present_and_system_part(boot_dev)) {
637 goto out;
638 } else {
639 efi_free_pool(boot_dev);
640 boot_dev = NULL;
641 }
642 }
643 }
644
645skip:
646 /* find active boot device in BootOrder */
647 size = 0;
648 ret = efi_get_variable_int(L"BootOrder", &efi_global_variable_guid,
649 NULL, &size, NULL, NULL);
650 if (ret == EFI_BUFFER_TOO_SMALL) {
651 boot_order = malloc(size);
652 if (!boot_order) {
653 ret = EFI_OUT_OF_RESOURCES;
654 goto out;
655 }
656
657 ret = efi_get_variable_int(L"BootOrder",
658 &efi_global_variable_guid,
659 NULL, &size, boot_order, NULL);
660 }
661 if (ret != EFI_SUCCESS)
662 goto out;
663
664 /* check in higher order */
665 num = size / sizeof(u16);
666 for (i = 0; i < num; i++) {
667 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
668 p = boot_var16;
669 utf8_utf16_strcpy(&p, boot_var);
670 ret = get_dp_device(boot_var16, &boot_dev);
671 if (ret != EFI_SUCCESS)
672 continue;
673
674 if (device_is_present_and_system_part(boot_dev))
675 break;
676
677 efi_free_pool(boot_dev);
678 boot_dev = NULL;
679 }
680out:
681 if (boot_dev) {
682 u16 *path_str;
683
684 path_str = efi_dp_str(boot_dev);
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900685 log_debug("EFI Capsule: bootdev is %ls\n", path_str);
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900686 efi_free_pool(path_str);
687
688 volume = efi_fs_from_path(boot_dev);
689 if (!volume)
690 ret = EFI_DEVICE_ERROR;
691 else
692 ret = EFI_CALL(volume->open_volume(volume,
693 &bootdev_root));
694 efi_free_pool(boot_dev);
695 } else {
696 ret = EFI_NOT_FOUND;
697 }
698 free(boot_order);
699
700 return ret;
701}
702
703/**
704 * efi_capsule_scan_dir - traverse a capsule directory in boot device
705 * @files: Array of file names
706 * @num: Number of elements in @files
707 *
708 * Traverse a capsule directory in boot device.
709 * Called by initialization code, and returns an array of capsule file
710 * names in @files.
711 *
712 * Return: status code
713 */
714static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
715{
716 struct efi_file_handle *dirh;
717 struct efi_file_info *dirent;
718 efi_uintn_t dirent_size, tmp_size;
719 unsigned int count;
720 u16 **tmp_files;
721 efi_status_t ret;
722
723 ret = find_boot_device();
724 if (ret == EFI_NOT_FOUND) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900725 log_debug("EFI Capsule: bootdev is not set\n");
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900726 *num = 0;
727 return EFI_SUCCESS;
728 } else if (ret != EFI_SUCCESS) {
729 return EFI_DEVICE_ERROR;
730 }
731
732 /* count capsule files */
733 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
734 EFI_CAPSULE_DIR,
735 EFI_FILE_MODE_READ, 0));
736 if (ret != EFI_SUCCESS) {
737 *num = 0;
738 return EFI_SUCCESS;
739 }
740
741 dirent_size = 256;
742 dirent = malloc(dirent_size);
743 if (!dirent)
744 return EFI_OUT_OF_RESOURCES;
745
746 count = 0;
747 while (1) {
748 tmp_size = dirent_size;
749 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
750 if (ret == EFI_BUFFER_TOO_SMALL) {
751 dirent = realloc(dirent, tmp_size);
752 if (!dirent) {
753 ret = EFI_OUT_OF_RESOURCES;
754 goto err;
755 }
756 dirent_size = tmp_size;
757 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
758 }
759 if (ret != EFI_SUCCESS)
760 goto err;
761 if (!tmp_size)
762 break;
763
Heinrich Schuchardt76b708a2021-02-09 17:45:33 +0100764 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900765 count++;
766 }
767
768 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
769 if (ret != EFI_SUCCESS)
770 goto err;
771
772 /* make a list */
AKASHI Takahiroc8fc12f2021-01-22 10:43:27 +0900773 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900774 if (!tmp_files) {
775 ret = EFI_OUT_OF_RESOURCES;
776 goto err;
777 }
778
779 count = 0;
780 while (1) {
781 tmp_size = dirent_size;
782 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
783 if (ret != EFI_SUCCESS)
784 goto err;
785 if (!tmp_size)
786 break;
787
788 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
789 u16_strcmp(dirent->file_name, L".") &&
790 u16_strcmp(dirent->file_name, L".."))
791 tmp_files[count++] = u16_strdup(dirent->file_name);
792 }
793 /* ignore an error */
794 EFI_CALL((*dirh->close)(dirh));
795
796 /* in ascii order */
797 /* FIXME: u16 version of strcasecmp */
798 qsort(tmp_files, count, sizeof(*tmp_files),
799 (int (*)(const void *, const void *))strcasecmp);
800 *files = tmp_files;
801 *num = count;
802 ret = EFI_SUCCESS;
803err:
804 free(dirent);
805
806 return ret;
807}
808
809/**
810 * efi_capsule_read_file - read in a capsule file
811 * @filename: File name
812 * @capsule: Pointer to buffer for capsule
813 *
814 * Read a capsule file and put its content in @capsule.
815 *
816 * Return: status code
817 */
818static efi_status_t efi_capsule_read_file(const u16 *filename,
819 struct efi_capsule_header **capsule)
820{
821 struct efi_file_handle *dirh, *fh;
822 struct efi_file_info *file_info = NULL;
823 struct efi_capsule_header *buf = NULL;
824 efi_uintn_t size;
825 efi_status_t ret;
826
827 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
828 EFI_CAPSULE_DIR,
829 EFI_FILE_MODE_READ, 0));
830 if (ret != EFI_SUCCESS)
831 return ret;
832 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
833 EFI_FILE_MODE_READ, 0));
834 /* ignore an error */
835 EFI_CALL((*dirh->close)(dirh));
836 if (ret != EFI_SUCCESS)
837 return ret;
838
839 /* file size */
840 size = 0;
841 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
842 &size, file_info));
843 if (ret == EFI_BUFFER_TOO_SMALL) {
844 file_info = malloc(size);
845 if (!file_info) {
846 ret = EFI_OUT_OF_RESOURCES;
847 goto err;
848 }
849 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
850 &size, file_info));
851 }
852 if (ret != EFI_SUCCESS)
853 goto err;
854 size = file_info->file_size;
855 free(file_info);
856 buf = malloc(size);
857 if (!buf) {
858 ret = EFI_OUT_OF_RESOURCES;
859 goto err;
860 }
861
862 /* fetch data */
863 ret = EFI_CALL((*fh->read)(fh, &size, buf));
864 if (ret == EFI_SUCCESS) {
865 if (size >= buf->capsule_image_size) {
866 *capsule = buf;
867 } else {
868 free(buf);
869 ret = EFI_INVALID_PARAMETER;
870 }
871 } else {
872 free(buf);
873 }
874err:
875 EFI_CALL((*fh->close)(fh));
876
877 return ret;
878}
879
880/**
881 * efi_capsule_delete_file - delete a capsule file
882 * @filename: File name
883 *
884 * Delete a capsule file from capsule directory.
885 *
886 * Return: status code
887 */
888static efi_status_t efi_capsule_delete_file(const u16 *filename)
889{
890 struct efi_file_handle *dirh, *fh;
891 efi_status_t ret;
892
893 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
894 EFI_CAPSULE_DIR,
895 EFI_FILE_MODE_READ, 0));
896 if (ret != EFI_SUCCESS)
897 return ret;
898 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
899 EFI_FILE_MODE_READ, 0));
900 /* ignore an error */
901 EFI_CALL((*dirh->close)(dirh));
902
903 ret = EFI_CALL((*fh->delete)(fh));
904
905 return ret;
906}
907
908/**
909 * efi_capsule_scan_done - reset a scan help function
910 *
911 * Reset a scan help function
912 */
913static void efi_capsule_scan_done(void)
914{
915 EFI_CALL((*bootdev_root->close)(bootdev_root));
916 bootdev_root = NULL;
917}
918
919/**
920 * arch_efi_load_capsule_drivers - initialize capsule drivers
921 *
922 * Architecture or board specific initialization routine
923 *
924 * Return: status code
925 */
926efi_status_t __weak arch_efi_load_capsule_drivers(void)
927{
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900928 __maybe_unused efi_handle_t handle;
929 efi_status_t ret = EFI_SUCCESS;
930
931 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
932 handle = NULL;
933 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
934 &handle, &efi_guid_firmware_management_protocol,
935 &efi_fmp_fit, NULL));
936 }
937
AKASHI Takahiro7ff3f3c2020-11-17 09:28:00 +0900938 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
939 handle = NULL;
940 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
941 &efi_root,
942 &efi_guid_firmware_management_protocol,
943 &efi_fmp_raw, NULL));
944 }
945
AKASHI Takahirof4818e62020-11-30 18:12:12 +0900946 return ret;
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900947}
948
949/**
950 * efi_launch_capsule - launch capsules
951 *
952 * Launch all the capsules in system at boot time.
953 * Called by efi init code
954 *
955 * Return: status codde
956 */
957efi_status_t efi_launch_capsules(void)
958{
959 u64 os_indications;
960 efi_uintn_t size;
961 struct efi_capsule_header *capsule = NULL;
962 u16 **files;
963 unsigned int nfiles, index, i;
964 u16 variable_name16[12];
965 efi_status_t ret;
966
967 size = sizeof(os_indications);
968 ret = efi_get_variable_int(L"OsIndications", &efi_global_variable_guid,
969 NULL, &size, &os_indications, NULL);
970 if (ret != EFI_SUCCESS ||
971 !(os_indications
972 & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED))
973 return EFI_SUCCESS;
974
975 index = get_last_capsule();
976
977 /* Load capsule drivers */
978 ret = arch_efi_load_capsule_drivers();
979 if (ret != EFI_SUCCESS)
980 return ret;
981
982 /*
983 * Find capsules on disk.
984 * All the capsules are collected at the beginning because
985 * capsule files will be removed instantly.
986 */
987 nfiles = 0;
988 files = NULL;
989 ret = efi_capsule_scan_dir(&files, &nfiles);
990 if (ret != EFI_SUCCESS)
991 return ret;
992 if (!nfiles)
993 return EFI_SUCCESS;
994
995 /* Launch capsules */
996 for (i = 0, ++index; i < nfiles; i++, index++) {
AKASHI Takahiro0d963782020-11-30 18:12:11 +0900997 log_debug("capsule from %ls ...\n", files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +0900998 if (index > 0xffff)
999 index = 0;
1000 ret = efi_capsule_read_file(files[i], &capsule);
1001 if (ret == EFI_SUCCESS) {
1002 ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0));
1003 if (ret != EFI_SUCCESS)
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001004 log_err("EFI Capsule update failed at %ls\n",
1005 files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001006
1007 free(capsule);
1008 } else {
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001009 log_err("EFI: reading capsule failed: %ls\n", files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001010 }
1011 /* create CapsuleXXXX */
1012 set_capsule_result(index, capsule, ret);
1013
1014 /* delete a capsule either in case of success or failure */
1015 ret = efi_capsule_delete_file(files[i]);
1016 if (ret != EFI_SUCCESS)
AKASHI Takahiro0d963782020-11-30 18:12:11 +09001017 log_err("EFI: deleting a capsule file failed: %ls\n",
1018 files[i]);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001019 }
1020 efi_capsule_scan_done();
1021
1022 for (i = 0; i < nfiles; i++)
1023 free(files[i]);
1024 free(files);
1025
1026 /* CapsuleLast */
Ilias Apalodimas21575292020-12-31 12:26:46 +02001027 efi_create_indexed_name(variable_name16, sizeof(variable_name16),
1028 "Capsule", index - 1);
AKASHI Takahiro45b819542020-11-17 09:27:56 +09001029 efi_set_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
1030 EFI_VARIABLE_READ_ONLY |
1031 EFI_VARIABLE_NON_VOLATILE |
1032 EFI_VARIABLE_BOOTSERVICE_ACCESS |
1033 EFI_VARIABLE_RUNTIME_ACCESS,
1034 22, variable_name16, false);
1035
1036 return ret;
1037}
1038#endif /* CONFIG_EFI_CAPSULE_ON_DISK */