blob: bef9d61f6758badd98200598f73cfa081e97edbf [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
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02009#define LOG_CATEGORY LOGC_EFI
10
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090011#include <common.h>
12#include <efi_loader.h>
13#include <efi_variable.h>
14#include <fs.h>
15#include <malloc.h>
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +090016#include <mapmem.h>
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090017#include <sort.h>
18
Sughosh Ganu04be98b2020-12-30 19:27:09 +053019#include <crypto/pkcs7.h>
20#include <crypto/pkcs7_parser.h>
21#include <linux/err.h>
22
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090023const efi_guid_t efi_guid_capsule_report = EFI_CAPSULE_REPORT_GUID;
AKASHI Takahiro8d990262020-11-30 18:12:11 +090024static const efi_guid_t efi_guid_firmware_management_capsule_id =
25 EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
26const efi_guid_t efi_guid_firmware_management_protocol =
27 EFI_FIRMWARE_MANAGEMENT_PROTOCOL_GUID;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090028
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +090029#ifdef CONFIG_EFI_CAPSULE_ON_DISK
30/* for file system access */
31static struct efi_file_handle *bootdev_root;
32#endif
33
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090034/**
35 * get_last_capsule - get the last capsule index
36 *
37 * Retrieve the index of the capsule invoked last time from "CapsuleLast"
38 * variable.
39 *
40 * Return:
41 * * > 0 - the last capsule index invoked
42 * * 0xffff - on error, or no capsule invoked yet
43 */
44static __maybe_unused unsigned int get_last_capsule(void)
45{
46 u16 value16[11]; /* "CapsuleXXXX": non-null-terminated */
Heinrich Schuchardt15bbcaf2021-02-09 20:20:34 +010047 char value[5];
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090048 efi_uintn_t size;
49 unsigned long index = 0xffff;
50 efi_status_t ret;
Heinrich Schuchardt15bbcaf2021-02-09 20:20:34 +010051 int i;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090052
53 size = sizeof(value16);
54 ret = efi_get_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
55 NULL, &size, value16, NULL);
Heinrich Schuchardt15bbcaf2021-02-09 20:20:34 +010056 if (ret != EFI_SUCCESS || size != 22 ||
57 u16_strncmp(value16, L"Capsule", 7))
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090058 goto err;
Heinrich Schuchardt15bbcaf2021-02-09 20:20:34 +010059 for (i = 0; i < 4; ++i) {
60 u16 c = value16[i + 7];
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090061
Heinrich Schuchardt15bbcaf2021-02-09 20:20:34 +010062 if (!c || c > 0x7f)
63 goto err;
64 value[i] = c;
65 }
66 value[4] = 0;
67 if (strict_strtoul(value, 16, &index))
68 index = 0xffff;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090069err:
70 return index;
71}
72
73/**
74 * set_capsule_result - set a result variable
75 * @capsule: Capsule
76 * @return_status: Return status
77 *
78 * Create and set a result variable, "CapsuleXXXX", for the capsule,
79 * @capsule.
80 */
81static __maybe_unused
82void set_capsule_result(int index, struct efi_capsule_header *capsule,
83 efi_status_t return_status)
84{
85 u16 variable_name16[12];
86 struct efi_capsule_result_variable_header result;
87 struct efi_time time;
88 efi_status_t ret;
89
Ilias Apalodimasfe179d72020-12-31 12:26:46 +020090 efi_create_indexed_name(variable_name16, sizeof(variable_name16),
91 "Capsule", index);
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +090092 result.variable_total_size = sizeof(result);
93 result.capsule_guid = capsule->capsule_guid;
94 ret = EFI_CALL((*efi_runtime_services.get_time)(&time, NULL));
95 if (ret == EFI_SUCCESS)
96 memcpy(&result.capsule_processed, &time, sizeof(time));
97 else
98 memset(&result.capsule_processed, 0, sizeof(time));
99 result.capsule_status = return_status;
Heinrich Schuchardtd7eedd92021-07-10 11:10:26 +0200100 ret = efi_set_variable_int(variable_name16, &efi_guid_capsule_report,
101 EFI_VARIABLE_NON_VOLATILE |
102 EFI_VARIABLE_BOOTSERVICE_ACCESS |
103 EFI_VARIABLE_RUNTIME_ACCESS,
104 sizeof(result), &result, false);
Heinrich Schuchardt70bad542021-07-10 11:14:13 +0200105 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200106 log_err("Setting %ls failed\n", variable_name16);
Heinrich Schuchardt70bad542021-07-10 11:14:13 +0200107 return;
108 }
109
110 /* Variable CapsuleLast must not include terminating 0x0000 */
111 ret = efi_set_variable_int(L"CapsuleLast", &efi_guid_capsule_report,
112 EFI_VARIABLE_READ_ONLY |
113 EFI_VARIABLE_NON_VOLATILE |
114 EFI_VARIABLE_BOOTSERVICE_ACCESS |
115 EFI_VARIABLE_RUNTIME_ACCESS,
116 22, variable_name16, false);
117 if (ret != EFI_SUCCESS)
118 log_err("Setting %ls failed\n", L"CapsuleLast");
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900119}
120
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900121#ifdef CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT
122/**
123 * efi_fmp_find - search for Firmware Management Protocol drivers
124 * @image_type: Image type guid
125 * @instance: Instance number
126 * @handles: Handles of FMP drivers
127 * @no_handles: Number of handles
128 *
129 * Search for Firmware Management Protocol drivers, matching the image
130 * type, @image_type and the machine instance, @instance, from the list,
131 * @handles.
132 *
133 * Return:
134 * * Protocol instance - on success
135 * * NULL - on failure
136 */
137static struct efi_firmware_management_protocol *
138efi_fmp_find(efi_guid_t *image_type, u64 instance, efi_handle_t *handles,
139 efi_uintn_t no_handles)
140{
141 efi_handle_t *handle;
142 struct efi_firmware_management_protocol *fmp;
143 struct efi_firmware_image_descriptor *image_info, *desc;
144 efi_uintn_t info_size, descriptor_size;
145 u32 descriptor_version;
146 u8 descriptor_count;
147 u32 package_version;
148 u16 *package_version_name;
149 bool found = false;
150 int i, j;
151 efi_status_t ret;
152
153 for (i = 0, handle = handles; i < no_handles; i++, handle++) {
154 ret = EFI_CALL(efi_handle_protocol(
155 *handle,
156 &efi_guid_firmware_management_protocol,
157 (void **)&fmp));
158 if (ret != EFI_SUCCESS)
159 continue;
160
161 /* get device's image info */
162 info_size = 0;
163 image_info = NULL;
164 descriptor_version = 0;
165 descriptor_count = 0;
166 descriptor_size = 0;
167 package_version = 0;
168 package_version_name = NULL;
169 ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
170 image_info,
171 &descriptor_version,
172 &descriptor_count,
173 &descriptor_size,
174 &package_version,
175 &package_version_name));
176 if (ret != EFI_BUFFER_TOO_SMALL)
177 goto skip;
178
179 image_info = malloc(info_size);
180 if (!image_info)
181 goto skip;
182
183 ret = EFI_CALL(fmp->get_image_info(fmp, &info_size,
184 image_info,
185 &descriptor_version,
186 &descriptor_count,
187 &descriptor_size,
188 &package_version,
189 &package_version_name));
190 if (ret != EFI_SUCCESS ||
191 descriptor_version != EFI_FIRMWARE_IMAGE_DESCRIPTOR_VERSION)
192 goto skip;
193
194 /* matching */
195 for (j = 0, desc = image_info; j < descriptor_count;
196 j++, desc = (void *)desc + descriptor_size) {
197 log_debug("+++ desc[%d] index: %d, name: %ls\n",
198 j, desc->image_index, desc->image_id_name);
199 if (!guidcmp(&desc->image_type_id, image_type) &&
200 (!instance ||
201 !desc->hardware_instance ||
202 desc->hardware_instance == instance))
203 found = true;
204 }
205
206skip:
207 efi_free_pool(package_version_name);
208 free(image_info);
209 EFI_CALL(efi_close_protocol(
210 (efi_handle_t)fmp,
211 &efi_guid_firmware_management_protocol,
212 NULL, NULL));
213 if (found)
214 return fmp;
215 }
216
217 return NULL;
218}
219
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530220#if defined(CONFIG_EFI_CAPSULE_AUTHENTICATE)
221
222const efi_guid_t efi_guid_capsule_root_cert_guid =
223 EFI_FIRMWARE_MANAGEMENT_CAPSULE_ID_GUID;
224
Sughosh Ganu04be98b2020-12-30 19:27:09 +0530225efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
226 void **image, efi_uintn_t *image_size)
227{
228 u8 *buf;
229 int ret;
230 void *fdt_pkey, *pkey;
231 efi_uintn_t pkey_len;
232 uint64_t monotonic_count;
233 struct efi_signature_store *truststore;
234 struct pkcs7_message *capsule_sig;
235 struct efi_image_regions *regs;
236 struct efi_firmware_image_authentication *auth_hdr;
237 efi_status_t status;
238
239 status = EFI_SECURITY_VIOLATION;
240 capsule_sig = NULL;
241 truststore = NULL;
242 regs = NULL;
243
244 /* Sanity checks */
245 if (capsule == NULL || capsule_size == 0)
246 goto out;
247
248 auth_hdr = (struct efi_firmware_image_authentication *)capsule;
249 if (capsule_size < sizeof(*auth_hdr))
250 goto out;
251
252 if (auth_hdr->auth_info.hdr.dwLength <=
253 offsetof(struct win_certificate_uefi_guid, cert_data))
254 goto out;
255
256 if (guidcmp(&auth_hdr->auth_info.cert_type, &efi_guid_cert_type_pkcs7))
257 goto out;
258
259 *image = (uint8_t *)capsule + sizeof(auth_hdr->monotonic_count) +
260 auth_hdr->auth_info.hdr.dwLength;
261 *image_size = capsule_size - auth_hdr->auth_info.hdr.dwLength -
262 sizeof(auth_hdr->monotonic_count);
263 memcpy(&monotonic_count, &auth_hdr->monotonic_count,
264 sizeof(monotonic_count));
265
266 /* data to be digested */
267 regs = calloc(sizeof(*regs) + sizeof(struct image_region) * 2, 1);
268 if (!regs)
269 goto out;
270
271 regs->max = 2;
272 efi_image_region_add(regs, (uint8_t *)*image,
273 (uint8_t *)*image + *image_size, 1);
274
275 efi_image_region_add(regs, (uint8_t *)&monotonic_count,
276 (uint8_t *)&monotonic_count + sizeof(monotonic_count),
277 1);
278
279 capsule_sig = efi_parse_pkcs7_header(auth_hdr->auth_info.cert_data,
280 auth_hdr->auth_info.hdr.dwLength
281 - sizeof(auth_hdr->auth_info),
282 &buf);
283 if (IS_ERR(capsule_sig)) {
284 debug("Parsing variable's pkcs7 header failed\n");
285 capsule_sig = NULL;
286 goto out;
287 }
288
289 ret = efi_get_public_key_data(&fdt_pkey, &pkey_len);
290 if (ret < 0)
291 goto out;
292
293 pkey = malloc(pkey_len);
294 if (!pkey)
295 goto out;
296
297 memcpy(pkey, fdt_pkey, pkey_len);
298 truststore = efi_build_signature_store(pkey, pkey_len);
299 if (!truststore)
300 goto out;
301
302 /* verify signature */
303 if (efi_signature_verify(regs, capsule_sig, truststore, NULL)) {
304 debug("Verified\n");
305 } else {
306 debug("Verifying variable's signature failed\n");
307 goto out;
308 }
309
310 status = EFI_SUCCESS;
311
312out:
313 efi_sigstore_free(truststore);
314 pkcs7_free_message(capsule_sig);
315 free(regs);
316
317 return status;
318}
319#else
320efi_status_t efi_capsule_authenticate(const void *capsule, efi_uintn_t capsule_size,
321 void **image, efi_uintn_t *image_size)
322{
323 return EFI_UNSUPPORTED;
324}
325#endif /* CONFIG_EFI_CAPSULE_AUTHENTICATE */
326
327
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900328/**
329 * efi_capsule_update_firmware - update firmware from capsule
330 * @capsule_data: Capsule
331 *
332 * Update firmware, using a capsule, @capsule_data. Loading any FMP
333 * drivers embedded in a capsule is not supported.
334 *
335 * Return: status code
336 */
337static efi_status_t efi_capsule_update_firmware(
338 struct efi_capsule_header *capsule_data)
339{
340 struct efi_firmware_management_capsule_header *capsule;
341 struct efi_firmware_management_capsule_image_header *image;
342 size_t capsule_size;
343 void *image_binary, *vendor_code;
344 efi_handle_t *handles;
345 efi_uintn_t no_handles;
346 int item;
347 struct efi_firmware_management_protocol *fmp;
348 u16 *abort_reason;
349 efi_status_t ret = EFI_SUCCESS;
350
351 /* sanity check */
352 if (capsule_data->header_size < sizeof(*capsule) ||
353 capsule_data->header_size >= capsule_data->capsule_image_size)
354 return EFI_INVALID_PARAMETER;
355
356 capsule = (void *)capsule_data + capsule_data->header_size;
357 capsule_size = capsule_data->capsule_image_size
358 - capsule_data->header_size;
359
360 if (capsule->version != 0x00000001)
361 return EFI_UNSUPPORTED;
362
363 handles = NULL;
364 ret = EFI_CALL(efi_locate_handle_buffer(
365 BY_PROTOCOL,
366 &efi_guid_firmware_management_protocol,
367 NULL, &no_handles, (efi_handle_t **)&handles));
368 if (ret != EFI_SUCCESS)
369 return EFI_UNSUPPORTED;
370
371 /* Payload */
372 for (item = capsule->embedded_driver_count;
373 item < capsule->embedded_driver_count
374 + capsule->payload_item_count; item++) {
375 /* sanity check */
376 if ((capsule->item_offset_list[item] + sizeof(*image)
377 >= capsule_size)) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200378 log_err("Capsule does not have enough data\n");
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900379 ret = EFI_INVALID_PARAMETER;
380 goto out;
381 }
382
383 image = (void *)capsule + capsule->item_offset_list[item];
384
385 if (image->version != 0x00000003) {
386 ret = EFI_UNSUPPORTED;
387 goto out;
388 }
389
390 /* find a device for update firmware */
391 /* TODO: should we pass index as well, or nothing but type? */
392 fmp = efi_fmp_find(&image->update_image_type_id,
393 image->update_hardware_instance,
394 handles, no_handles);
395 if (!fmp) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200396 log_err("FMP driver not found for firmware type %pUl, hardware instance %lld\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900397 &image->update_image_type_id,
398 image->update_hardware_instance);
399 ret = EFI_UNSUPPORTED;
400 goto out;
401 }
402
403 /* do update */
404 image_binary = (void *)image + sizeof(*image);
405 vendor_code = image_binary + image->update_image_size;
406
407 abort_reason = NULL;
408 ret = EFI_CALL(fmp->set_image(fmp, image->update_image_index,
409 image_binary,
410 image->update_image_size,
411 vendor_code, NULL,
412 &abort_reason));
413 if (ret != EFI_SUCCESS) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200414 log_err("Firmware update failed: %ls\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900415 abort_reason);
416 efi_free_pool(abort_reason);
417 goto out;
418 }
419 }
420
421out:
422 efi_free_pool(handles);
423
424 return ret;
425}
426#else
427static efi_status_t efi_capsule_update_firmware(
428 struct efi_capsule_header *capsule_data)
429{
430 return EFI_UNSUPPORTED;
431}
432#endif /* CONFIG_EFI_CAPSULE_FIRMWARE_MANAGEMENT */
433
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900434/**
435 * efi_update_capsule() - process information from operating system
436 * @capsule_header_array: Array of virtual address pointers
437 * @capsule_count: Number of pointers in capsule_header_array
438 * @scatter_gather_list: Array of physical address pointers
439 *
440 * This function implements the UpdateCapsule() runtime service.
441 *
442 * See the Unified Extensible Firmware Interface (UEFI) specification for
443 * details.
444 *
445 * Return: status code
446 */
447efi_status_t EFIAPI efi_update_capsule(
448 struct efi_capsule_header **capsule_header_array,
449 efi_uintn_t capsule_count,
450 u64 scatter_gather_list)
451{
452 struct efi_capsule_header *capsule;
453 unsigned int i;
454 efi_status_t ret;
455
Simon Glassdf7d89a2021-02-07 14:27:02 -0700456 EFI_ENTRY("%p, %zu, %llu\n", capsule_header_array, capsule_count,
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900457 scatter_gather_list);
458
459 if (!capsule_count) {
460 ret = EFI_INVALID_PARAMETER;
461 goto out;
462 }
463
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900464 ret = EFI_SUCCESS;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900465 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
466 i++, capsule = *(++capsule_header_array)) {
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900467 /* sanity check */
468 if (capsule->header_size < sizeof(*capsule) ||
469 capsule->capsule_image_size < sizeof(*capsule)) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200470 log_err("Capsule does not have enough data\n");
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900471 continue;
472 }
473
474 log_debug("Capsule[%d] (guid:%pUl)\n",
475 i, &capsule->capsule_guid);
476 if (!guidcmp(&capsule->capsule_guid,
477 &efi_guid_firmware_management_capsule_id)) {
478 ret = efi_capsule_update_firmware(capsule);
479 } else {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200480 log_err("Unsupported capsule type: %pUl\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900481 &capsule->capsule_guid);
482 ret = EFI_UNSUPPORTED;
483 }
484
485 if (ret != EFI_SUCCESS)
486 goto out;
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900487 }
Jose Marinho64a8aae2021-03-02 17:26:38 +0000488
489 if (IS_ENABLED(CONFIG_EFI_ESRT)) {
490 /* Rebuild the ESRT to reflect any updated FW images. */
491 ret = efi_esrt_populate();
492 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200493 log_warning("ESRT update failed\n");
Jose Marinho64a8aae2021-03-02 17:26:38 +0000494 }
Jose Marinho3627cf42021-04-19 14:54:33 +0100495out:
Jose Marinho64a8aae2021-03-02 17:26:38 +0000496
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900497 return EFI_EXIT(ret);
498}
499
500/**
501 * efi_query_capsule_caps() - check if capsule is supported
502 * @capsule_header_array: Array of virtual pointers
503 * @capsule_count: Number of pointers in capsule_header_array
504 * @maximum_capsule_size: Maximum capsule size
505 * @reset_type: Type of reset needed for capsule update
506 *
507 * This function implements the QueryCapsuleCapabilities() runtime service.
508 *
509 * See the Unified Extensible Firmware Interface (UEFI) specification for
510 * details.
511 *
512 * Return: status code
513 */
514efi_status_t EFIAPI efi_query_capsule_caps(
515 struct efi_capsule_header **capsule_header_array,
516 efi_uintn_t capsule_count,
517 u64 *maximum_capsule_size,
518 u32 *reset_type)
519{
520 struct efi_capsule_header *capsule __attribute__((unused));
521 unsigned int i;
522 efi_status_t ret;
523
Simon Glassdf7d89a2021-02-07 14:27:02 -0700524 EFI_ENTRY("%p, %zu, %p, %p\n", capsule_header_array, capsule_count,
AKASHI Takahiro2bc27ca2020-11-17 09:27:55 +0900525 maximum_capsule_size, reset_type);
526
527 if (!maximum_capsule_size) {
528 ret = EFI_INVALID_PARAMETER;
529 goto out;
530 }
531
532 *maximum_capsule_size = U64_MAX;
533 *reset_type = EFI_RESET_COLD;
534
535 ret = EFI_SUCCESS;
536 for (i = 0, capsule = *capsule_header_array; i < capsule_count;
537 i++, capsule = *(++capsule_header_array)) {
538 /* TODO */
539 }
540out:
541 return EFI_EXIT(ret);
542}
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900543
544#ifdef CONFIG_EFI_CAPSULE_ON_DISK
545/**
546 * get_dp_device - retrieve a device path from boot variable
547 * @boot_var: Boot variable name
548 * @device_dp Device path
549 *
550 * Retrieve a device patch from boot variable, @boot_var.
551 *
552 * Return: status code
553 */
554static efi_status_t get_dp_device(u16 *boot_var,
555 struct efi_device_path **device_dp)
556{
557 void *buf = NULL;
558 efi_uintn_t size;
559 struct efi_load_option lo;
560 struct efi_device_path *file_dp;
561 efi_status_t ret;
562
563 size = 0;
564 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
565 NULL, &size, NULL, NULL);
566 if (ret == EFI_BUFFER_TOO_SMALL) {
567 buf = malloc(size);
568 if (!buf)
569 return EFI_OUT_OF_RESOURCES;
570 ret = efi_get_variable_int(boot_var, &efi_global_variable_guid,
571 NULL, &size, buf, NULL);
572 }
573 if (ret != EFI_SUCCESS)
574 return ret;
575
576 efi_deserialize_load_option(&lo, buf, &size);
577
578 if (lo.attributes & LOAD_OPTION_ACTIVE) {
579 efi_dp_split_file_path(lo.file_path, device_dp, &file_dp);
580 efi_free_pool(file_dp);
581
582 ret = EFI_SUCCESS;
583 } else {
584 ret = EFI_NOT_FOUND;
585 }
586
587 free(buf);
588
589 return ret;
590}
591
592/**
593 * device_is_present_and_system_part - check if a device exists
594 * @dp Device path
595 *
596 * Check if a device pointed to by the device path, @dp, exists and is
597 * located in UEFI system partition.
598 *
599 * Return: true - yes, false - no
600 */
601static bool device_is_present_and_system_part(struct efi_device_path *dp)
602{
603 efi_handle_t handle;
604
605 handle = efi_dp_find_obj(dp, NULL);
606 if (!handle)
607 return false;
608
609 return efi_disk_is_system_part(handle);
610}
611
612/**
613 * find_boot_device - identify the boot device
614 *
615 * Identify the boot device from boot-related variables as UEFI
616 * specification describes and put its handle into bootdev_root.
617 *
618 * Return: status code
619 */
620static efi_status_t find_boot_device(void)
621{
622 char boot_var[9];
623 u16 boot_var16[9], *p, bootnext, *boot_order = NULL;
624 efi_uintn_t size;
625 int i, num;
626 struct efi_simple_file_system_protocol *volume;
627 struct efi_device_path *boot_dev = NULL;
628 efi_status_t ret;
629
630 /* find active boot device in BootNext */
631 bootnext = 0;
632 size = sizeof(bootnext);
633 ret = efi_get_variable_int(L"BootNext",
634 (efi_guid_t *)&efi_global_variable_guid,
635 NULL, &size, &bootnext, NULL);
636 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
637 /* BootNext does exist here */
638 if (ret == EFI_BUFFER_TOO_SMALL || size != sizeof(u16)) {
AKASHI Takahiro8d990262020-11-30 18:12:11 +0900639 log_err("BootNext must be 16-bit integer\n");
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900640 goto skip;
641 }
642 sprintf((char *)boot_var, "Boot%04X", bootnext);
643 p = boot_var16;
644 utf8_utf16_strcpy(&p, boot_var);
645
646 ret = get_dp_device(boot_var16, &boot_dev);
647 if (ret == EFI_SUCCESS) {
648 if (device_is_present_and_system_part(boot_dev)) {
649 goto out;
650 } else {
651 efi_free_pool(boot_dev);
652 boot_dev = NULL;
653 }
654 }
655 }
656
657skip:
658 /* find active boot device in BootOrder */
659 size = 0;
660 ret = efi_get_variable_int(L"BootOrder", &efi_global_variable_guid,
661 NULL, &size, NULL, NULL);
662 if (ret == EFI_BUFFER_TOO_SMALL) {
663 boot_order = malloc(size);
664 if (!boot_order) {
665 ret = EFI_OUT_OF_RESOURCES;
666 goto out;
667 }
668
669 ret = efi_get_variable_int(L"BootOrder",
670 &efi_global_variable_guid,
671 NULL, &size, boot_order, NULL);
672 }
673 if (ret != EFI_SUCCESS)
674 goto out;
675
676 /* check in higher order */
677 num = size / sizeof(u16);
678 for (i = 0; i < num; i++) {
679 sprintf((char *)boot_var, "Boot%04X", boot_order[i]);
680 p = boot_var16;
681 utf8_utf16_strcpy(&p, boot_var);
682 ret = get_dp_device(boot_var16, &boot_dev);
683 if (ret != EFI_SUCCESS)
684 continue;
685
686 if (device_is_present_and_system_part(boot_dev))
687 break;
688
689 efi_free_pool(boot_dev);
690 boot_dev = NULL;
691 }
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900692 if (boot_dev) {
693 u16 *path_str;
694
695 path_str = efi_dp_str(boot_dev);
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200696 log_debug("Boot device %ls\n", path_str);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900697 efi_free_pool(path_str);
698
699 volume = efi_fs_from_path(boot_dev);
700 if (!volume)
701 ret = EFI_DEVICE_ERROR;
702 else
703 ret = EFI_CALL(volume->open_volume(volume,
704 &bootdev_root));
705 efi_free_pool(boot_dev);
706 } else {
707 ret = EFI_NOT_FOUND;
708 }
AKASHI Takahiro2c1a6842021-04-20 10:03:16 +0900709out:
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900710 free(boot_order);
711
712 return ret;
713}
714
715/**
716 * efi_capsule_scan_dir - traverse a capsule directory in boot device
717 * @files: Array of file names
718 * @num: Number of elements in @files
719 *
720 * Traverse a capsule directory in boot device.
721 * Called by initialization code, and returns an array of capsule file
722 * names in @files.
723 *
724 * Return: status code
725 */
726static efi_status_t efi_capsule_scan_dir(u16 ***files, unsigned int *num)
727{
728 struct efi_file_handle *dirh;
729 struct efi_file_info *dirent;
730 efi_uintn_t dirent_size, tmp_size;
731 unsigned int count;
732 u16 **tmp_files;
733 efi_status_t ret;
734
735 ret = find_boot_device();
736 if (ret == EFI_NOT_FOUND) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +0200737 log_debug("Boot device is not set\n");
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900738 *num = 0;
739 return EFI_SUCCESS;
740 } else if (ret != EFI_SUCCESS) {
741 return EFI_DEVICE_ERROR;
742 }
743
744 /* count capsule files */
745 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
746 EFI_CAPSULE_DIR,
747 EFI_FILE_MODE_READ, 0));
748 if (ret != EFI_SUCCESS) {
749 *num = 0;
750 return EFI_SUCCESS;
751 }
752
753 dirent_size = 256;
754 dirent = malloc(dirent_size);
755 if (!dirent)
756 return EFI_OUT_OF_RESOURCES;
757
758 count = 0;
759 while (1) {
760 tmp_size = dirent_size;
761 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
762 if (ret == EFI_BUFFER_TOO_SMALL) {
Heinrich Schuchardte8287b02021-04-11 06:53:04 +0200763 struct efi_file_info *old_dirent = dirent;
764
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900765 dirent = realloc(dirent, tmp_size);
766 if (!dirent) {
Heinrich Schuchardte8287b02021-04-11 06:53:04 +0200767 dirent = old_dirent;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900768 ret = EFI_OUT_OF_RESOURCES;
769 goto err;
770 }
771 dirent_size = tmp_size;
772 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
773 }
774 if (ret != EFI_SUCCESS)
775 goto err;
776 if (!tmp_size)
777 break;
778
Heinrich Schuchardt841f7a42021-02-09 17:45:33 +0100779 if (!(dirent->attribute & EFI_FILE_DIRECTORY))
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900780 count++;
781 }
782
783 ret = EFI_CALL((*dirh->setpos)(dirh, 0));
784 if (ret != EFI_SUCCESS)
785 goto err;
786
787 /* make a list */
AKASHI Takahiro8f1844c2021-01-22 10:43:27 +0900788 tmp_files = malloc(count * sizeof(*tmp_files));
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900789 if (!tmp_files) {
790 ret = EFI_OUT_OF_RESOURCES;
791 goto err;
792 }
793
794 count = 0;
795 while (1) {
796 tmp_size = dirent_size;
797 ret = EFI_CALL((*dirh->read)(dirh, &tmp_size, dirent));
798 if (ret != EFI_SUCCESS)
799 goto err;
800 if (!tmp_size)
801 break;
802
803 if (!(dirent->attribute & EFI_FILE_DIRECTORY) &&
804 u16_strcmp(dirent->file_name, L".") &&
805 u16_strcmp(dirent->file_name, L".."))
806 tmp_files[count++] = u16_strdup(dirent->file_name);
807 }
808 /* ignore an error */
809 EFI_CALL((*dirh->close)(dirh));
810
811 /* in ascii order */
812 /* FIXME: u16 version of strcasecmp */
813 qsort(tmp_files, count, sizeof(*tmp_files),
814 (int (*)(const void *, const void *))strcasecmp);
815 *files = tmp_files;
816 *num = count;
817 ret = EFI_SUCCESS;
818err:
819 free(dirent);
820
821 return ret;
822}
823
824/**
825 * efi_capsule_read_file - read in a capsule file
826 * @filename: File name
827 * @capsule: Pointer to buffer for capsule
828 *
829 * Read a capsule file and put its content in @capsule.
830 *
831 * Return: status code
832 */
833static efi_status_t efi_capsule_read_file(const u16 *filename,
834 struct efi_capsule_header **capsule)
835{
836 struct efi_file_handle *dirh, *fh;
837 struct efi_file_info *file_info = NULL;
838 struct efi_capsule_header *buf = NULL;
839 efi_uintn_t size;
840 efi_status_t ret;
841
842 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
843 EFI_CAPSULE_DIR,
844 EFI_FILE_MODE_READ, 0));
845 if (ret != EFI_SUCCESS)
846 return ret;
847 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
848 EFI_FILE_MODE_READ, 0));
849 /* ignore an error */
850 EFI_CALL((*dirh->close)(dirh));
851 if (ret != EFI_SUCCESS)
852 return ret;
853
854 /* file size */
855 size = 0;
856 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
857 &size, file_info));
858 if (ret == EFI_BUFFER_TOO_SMALL) {
859 file_info = malloc(size);
860 if (!file_info) {
861 ret = EFI_OUT_OF_RESOURCES;
862 goto err;
863 }
864 ret = EFI_CALL((*fh->getinfo)(fh, &efi_file_info_guid,
865 &size, file_info));
866 }
867 if (ret != EFI_SUCCESS)
868 goto err;
869 size = file_info->file_size;
870 free(file_info);
871 buf = malloc(size);
872 if (!buf) {
873 ret = EFI_OUT_OF_RESOURCES;
874 goto err;
875 }
876
877 /* fetch data */
878 ret = EFI_CALL((*fh->read)(fh, &size, buf));
879 if (ret == EFI_SUCCESS) {
880 if (size >= buf->capsule_image_size) {
881 *capsule = buf;
882 } else {
883 free(buf);
884 ret = EFI_INVALID_PARAMETER;
885 }
886 } else {
887 free(buf);
888 }
889err:
890 EFI_CALL((*fh->close)(fh));
891
892 return ret;
893}
894
895/**
896 * efi_capsule_delete_file - delete a capsule file
897 * @filename: File name
898 *
899 * Delete a capsule file from capsule directory.
900 *
901 * Return: status code
902 */
903static efi_status_t efi_capsule_delete_file(const u16 *filename)
904{
905 struct efi_file_handle *dirh, *fh;
906 efi_status_t ret;
907
908 ret = EFI_CALL((*bootdev_root->open)(bootdev_root, &dirh,
909 EFI_CAPSULE_DIR,
910 EFI_FILE_MODE_READ, 0));
911 if (ret != EFI_SUCCESS)
912 return ret;
913 ret = EFI_CALL((*dirh->open)(dirh, &fh, (u16 *)filename,
914 EFI_FILE_MODE_READ, 0));
915 /* ignore an error */
916 EFI_CALL((*dirh->close)(dirh));
917
Heinrich Schuchardte27b0ff2021-06-02 19:28:22 +0200918 if (ret == EFI_SUCCESS)
919 ret = EFI_CALL((*fh->delete)(fh));
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900920
921 return ret;
922}
923
924/**
925 * efi_capsule_scan_done - reset a scan help function
926 *
927 * Reset a scan help function
928 */
929static void efi_capsule_scan_done(void)
930{
931 EFI_CALL((*bootdev_root->close)(bootdev_root));
932 bootdev_root = NULL;
933}
934
935/**
Ilias Apalodimas6e0184b2021-06-22 17:38:53 +0300936 * efi_load_capsule_drivers - initialize capsule drivers
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900937 *
Ilias Apalodimas6e0184b2021-06-22 17:38:53 +0300938 * Generic FMP drivers backed by DFU
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900939 *
940 * Return: status code
941 */
Ilias Apalodimas6e0184b2021-06-22 17:38:53 +0300942efi_status_t __weak efi_load_capsule_drivers(void)
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900943{
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900944 __maybe_unused efi_handle_t handle;
945 efi_status_t ret = EFI_SUCCESS;
946
947 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_FIT)) {
948 handle = NULL;
949 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
950 &handle, &efi_guid_firmware_management_protocol,
951 &efi_fmp_fit, NULL));
952 }
953
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900954 if (IS_ENABLED(CONFIG_EFI_CAPSULE_FIRMWARE_RAW)) {
955 handle = NULL;
956 ret = EFI_CALL(efi_install_multiple_protocol_interfaces(
Masami Hiramatsu3ef77222021-06-22 17:38:51 +0300957 &handle,
AKASHI Takahirobb7e71d2020-11-17 09:28:00 +0900958 &efi_guid_firmware_management_protocol,
959 &efi_fmp_raw, NULL));
960 }
961
AKASHI Takahirof27c2012020-11-30 18:12:12 +0900962 return ret;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900963}
964
965/**
Ilias Apalodimas0fa50202021-06-29 07:55:51 +0300966 * check_run_capsules - Check whether capsule update should run
967 *
968 * The spec says OsIndications must be set in order to run the capsule update
969 * on-disk. Since U-Boot doesn't support runtime SetVariable, allow capsules to
970 * run explicitly if CONFIG_EFI_IGNORE_OSINDICATIONS is selected
971 */
972static bool check_run_capsules(void)
973{
974 u64 os_indications;
975 efi_uintn_t size;
976 efi_status_t ret;
977
978 if (IS_ENABLED(CONFIG_EFI_IGNORE_OSINDICATIONS))
979 return true;
980
981 size = sizeof(os_indications);
982 ret = efi_get_variable_int(L"OsIndications", &efi_global_variable_guid,
983 NULL, &size, &os_indications, NULL);
984 if (ret == EFI_SUCCESS &&
985 (os_indications
986 & EFI_OS_INDICATIONS_FILE_CAPSULE_DELIVERY_SUPPORTED))
987 return true;
988
989 return false;
990}
991
992/**
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +0900993 * efi_launch_capsule - launch capsules
994 *
995 * Launch all the capsules in system at boot time.
996 * Called by efi init code
997 *
998 * Return: status codde
999 */
1000efi_status_t efi_launch_capsules(void)
1001{
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001002 struct efi_capsule_header *capsule = NULL;
1003 u16 **files;
1004 unsigned int nfiles, index, i;
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001005 efi_status_t ret;
1006
Ilias Apalodimas0fa50202021-06-29 07:55:51 +03001007 if (!check_run_capsules())
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001008 return EFI_SUCCESS;
1009
1010 index = get_last_capsule();
1011
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001012 /*
1013 * Find capsules on disk.
1014 * All the capsules are collected at the beginning because
1015 * capsule files will be removed instantly.
1016 */
1017 nfiles = 0;
1018 files = NULL;
1019 ret = efi_capsule_scan_dir(&files, &nfiles);
1020 if (ret != EFI_SUCCESS)
1021 return ret;
1022 if (!nfiles)
1023 return EFI_SUCCESS;
1024
1025 /* Launch capsules */
1026 for (i = 0, ++index; i < nfiles; i++, index++) {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001027 log_debug("Applying %ls\n", files[i]);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001028 if (index > 0xffff)
1029 index = 0;
1030 ret = efi_capsule_read_file(files[i], &capsule);
1031 if (ret == EFI_SUCCESS) {
1032 ret = EFI_CALL(efi_update_capsule(&capsule, 1, 0));
1033 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001034 log_err("Applying capsule %ls failed\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001035 files[i]);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001036
1037 free(capsule);
1038 } else {
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001039 log_err("Reading capsule %ls failed\n", files[i]);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001040 }
1041 /* create CapsuleXXXX */
1042 set_capsule_result(index, capsule, ret);
1043
1044 /* delete a capsule either in case of success or failure */
1045 ret = efi_capsule_delete_file(files[i]);
1046 if (ret != EFI_SUCCESS)
Heinrich Schuchardt3e491192021-07-10 11:03:27 +02001047 log_err("Deleting capsule %ls failed\n",
AKASHI Takahiro8d990262020-11-30 18:12:11 +09001048 files[i]);
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001049 }
1050 efi_capsule_scan_done();
1051
1052 for (i = 0; i < nfiles; i++)
1053 free(files[i]);
1054 free(files);
1055
AKASHI Takahiroc74cd8b2020-11-17 09:27:56 +09001056 return ret;
1057}
1058#endif /* CONFIG_EFI_CAPSULE_ON_DISK */