blob: 2eaa12b83b1661bbda498b886f62a11f5c4b23e0 [file] [log] [blame]
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Defines APIs that allow an OS to interact with UEFI firmware to query
4 * information about the device.
5 * https://trustedcomputinggroup.org/resource/tcg-efi-protocol-specification/
6 *
7 * Copyright (c) 2020, Linaro Limited
8 */
9
10#define LOG_CATEGORY LOGC_EFI
11#include <common.h>
12#include <dm.h>
13#include <efi_loader.h>
Heinrich Schuchardta45dac12021-09-09 08:50:01 +020014#include <efi_variable.h>
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020015#include <efi_tcg2.h>
16#include <log.h>
Masahisa Kojima163a0d72021-05-26 12:09:58 +090017#include <malloc.h>
Masahisa Kojima3d49ee82021-10-26 17:27:24 +090018#include <smbios.h>
Pali Rohárbdfb6d72021-08-02 15:18:31 +020019#include <version_string.h>
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020020#include <tpm-v2.h>
Ilias Apalodimasd6b55a42021-11-18 10:13:42 +020021#include <tpm_api.h>
Masahisa Kojima163a0d72021-05-26 12:09:58 +090022#include <u-boot/hash-checksum.h>
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020023#include <u-boot/sha1.h>
24#include <u-boot/sha256.h>
25#include <u-boot/sha512.h>
Masahisa Kojima14cbb332021-11-03 11:04:09 +090026#include <linux/unaligned/be_byteshift.h>
27#include <linux/unaligned/le_byteshift.h>
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020028#include <linux/unaligned/generic.h>
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020029#include <hexdump.h>
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020030
Ilias Apalodimas5ba03972021-11-18 09:03:39 +020031/**
32 * struct event_log_buffer - internal eventlog management structure
33 *
34 * @buffer: eventlog buffer
35 * @final_buffer: finalevent config table buffer
36 * @pos: current position of 'buffer'
37 * @final_pos: current position of 'final_buffer'
38 * @get_event_called: true if GetEventLog has been invoked at least once
39 * @ebs_called: true if ExitBootServices has been invoked
40 * @truncated: true if the 'buffer' is truncated
41 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020042struct event_log_buffer {
43 void *buffer;
44 void *final_buffer;
45 size_t pos; /* eventlog position */
46 size_t final_pos; /* final events config table position */
47 size_t last_event_size;
48 bool get_event_called;
Ilias Apalodimas5ba03972021-11-18 09:03:39 +020049 bool ebs_called;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020050 bool truncated;
51};
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020052
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020053static struct event_log_buffer event_log;
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +090054static bool tcg2_efi_app_invoked;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020055/*
56 * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
57 * Since the current tpm2_get_capability() response buffers starts at
58 * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate
59 * the response size and offset once for all consumers
60 */
61#define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \
62 offsetof(struct tpms_capability_data, data))
63#define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \
64 offsetof(struct tpms_tagged_property, value))
65
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020066static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID;
67static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID;
68
Masahisa Kojima96485d22021-10-26 17:27:26 +090069struct variable_info {
70 const u16 *name;
71 bool accept_empty;
Masahisa Kojima65aa2592021-10-26 17:27:27 +090072 u32 pcr_index;
Masahisa Kojima96485d22021-10-26 17:27:26 +090073};
74
75static struct variable_info secure_variables[] = {
Masahisa Kojima65aa2592021-10-26 17:27:27 +090076 {u"SecureBoot", true, 7},
77 {u"PK", true, 7},
78 {u"KEK", true, 7},
79 {u"db", true, 7},
80 {u"dbx", true, 7},
81 {u"dbt", false, 7},
82 {u"dbr", false, 7},
83 {u"DeployedMode", false, 1},
84 {u"AuditMode", false, 1},
Masahisa Kojimacfbcf052021-08-13 16:12:39 +090085};
86
Masahisa Kojima54bec172021-12-07 14:15:31 +090087static bool is_tcg2_protocol_installed(void)
88{
89 struct efi_handler *handler;
90 efi_status_t ret;
91
92 ret = efi_search_protocol(efi_root, &efi_guid_tcg2_protocol, &handler);
93 return ret == EFI_SUCCESS;
94}
95
Ilias Apalodimas5ba03972021-11-18 09:03:39 +020096/* tcg2_agile_log_append - Append an agile event to an eventlog
97 *
98 * @pcr_index: PCR index
99 * @event_type: type of event added
100 * @digest_list: list of digest algorithms to add
101 * @size: size of event
102 * @event: event to add
103 * @log: log buffer to append the event
104 *
105 * @Return: status code
106 */
107static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type,
108 struct tpml_digest_values *digest_list,
109 u32 size, u8 event[])
110{
111 void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
Eddie James97707f12023-10-24 10:43:49 -0500112 u32 event_size = size + tcg2_event_get_size(digest_list);
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200113 struct efi_tcg2_final_events_table *final_event;
114 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200115
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200116 /* if ExitBootServices hasn't been called update the normal log */
117 if (!event_log.ebs_called) {
118 if (event_log.truncated ||
119 event_log.pos + event_size > TPM2_EVENT_LOG_SIZE) {
120 event_log.truncated = true;
121 return EFI_VOLUME_FULL;
122 }
Eddie James97707f12023-10-24 10:43:49 -0500123 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200124 event_log.pos += event_size;
125 event_log.last_event_size = event_size;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200126 }
127
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200128 if (!event_log.get_event_called)
129 return ret;
130
131 /* if GetEventLog has been called update FinalEventLog as well */
132 if (event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE)
133 return EFI_VOLUME_FULL;
134
135 log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos);
Eddie James97707f12023-10-24 10:43:49 -0500136 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
Ilias Apalodimas5ba03972021-11-18 09:03:39 +0200137
138 final_event = event_log.final_buffer;
139 final_event->number_of_events++;
140 event_log.final_pos += event_size;
141
142 return ret;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200143}
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200144
145/**
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200146 * tpm2_get_max_command_size() - get the supported max command size
147 *
148 * @dev: TPM device
149 * @max_command_size: output buffer for the size
150 *
151 * Return: 0 on success, -1 on error
152 */
153static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
154{
155 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
156 u32 ret;
157
158 memset(response, 0, sizeof(response));
159 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
160 TPM2_PT_MAX_COMMAND_SIZE, response, 1);
161 if (ret)
162 return -1;
163
164 *max_command_size = (uint16_t)get_unaligned_be32(response +
165 properties_offset);
166
167 return 0;
168}
169
170/**
171 * tpm2_get_max_response_size() - get the supported max response size
172 *
173 * @dev: TPM device
174 * @max_response_size: output buffer for the size
175 *
176 * Return: 0 on success, -1 on error
177 */
178static int tpm2_get_max_response_size(struct udevice *dev,
179 u16 *max_response_size)
180{
181 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
182 u32 ret;
183
184 memset(response, 0, sizeof(response));
185 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
186 TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
187 if (ret)
188 return -1;
189
190 *max_response_size = (uint16_t)get_unaligned_be32(response +
191 properties_offset);
192
193 return 0;
194}
195
196/**
197 * tpm2_get_manufacturer_id() - get the manufacturer ID
198 *
199 * @dev: TPM device
200 * @manufacturer_id: output buffer for the id
201 *
202 * Return: 0 on success, -1 on error
203 */
204static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
205{
206 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
207 u32 ret;
208
209 memset(response, 0, sizeof(response));
210 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
211 TPM2_PT_MANUFACTURER, response, 1);
212 if (ret)
213 return -1;
214
215 *manufacturer_id = get_unaligned_be32(response + properties_offset);
216
217 return 0;
218}
219
220/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200221 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200222 *
223 * @this: TCG2 protocol instance
224 * @capability: caller allocated memory with size field to the size of
225 * the structure allocated
226
227 * Return: status code
228 */
229static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200230efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
231 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200232{
233 struct udevice *dev;
234 efi_status_t efi_ret;
235 int ret;
236
237 EFI_ENTRY("%p, %p", this, capability);
238
239 if (!this || !capability) {
240 efi_ret = EFI_INVALID_PARAMETER;
241 goto out;
242 }
243
Masahisa Kojimabad49da2021-09-06 12:04:12 +0900244 if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) {
245 capability->size = BOOT_SERVICE_CAPABILITY_MIN;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200246 efi_ret = EFI_BUFFER_TOO_SMALL;
247 goto out;
248 }
249
250 if (capability->size < sizeof(*capability)) {
251 capability->size = sizeof(*capability);
252 efi_ret = EFI_BUFFER_TOO_SMALL;
253 goto out;
254 }
255
256 capability->structure_version.major = 1;
257 capability->structure_version.minor = 1;
258 capability->protocol_version.major = 1;
259 capability->protocol_version.minor = 1;
260
Eddie James97707f12023-10-24 10:43:49 -0500261 efi_ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200262 if (efi_ret != EFI_SUCCESS) {
263 capability->supported_event_logs = 0;
264 capability->hash_algorithm_bitmap = 0;
265 capability->tpm_present_flag = false;
266 capability->max_command_size = 0;
267 capability->max_response_size = 0;
268 capability->manufacturer_id = 0;
269 capability->number_of_pcr_banks = 0;
270 capability->active_pcr_banks = 0;
271
272 efi_ret = EFI_SUCCESS;
273 goto out;
274 }
275
276 /* We only allow a TPMv2 device to register the EFI protocol */
277 capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
278
279 capability->tpm_present_flag = true;
280
281 /* Supported and active PCRs */
282 capability->hash_algorithm_bitmap = 0;
283 capability->active_pcr_banks = 0;
284 ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
285 &capability->active_pcr_banks,
286 &capability->number_of_pcr_banks);
287 if (ret) {
288 efi_ret = EFI_DEVICE_ERROR;
289 goto out;
290 }
291
292 /* Max command size */
293 ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
294 if (ret) {
295 efi_ret = EFI_DEVICE_ERROR;
296 goto out;
297 }
298
299 /* Max response size */
300 ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
301 if (ret) {
302 efi_ret = EFI_DEVICE_ERROR;
303 goto out;
304 }
305
306 /* Manufacturer ID */
307 ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
308 if (ret) {
309 efi_ret = EFI_DEVICE_ERROR;
310 goto out;
311 }
312
313 return EFI_EXIT(EFI_SUCCESS);
314out:
315 return EFI_EXIT(efi_ret);
316}
317
318/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200319 * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
320 * last entry
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200321 *
322 * @this: TCG2 protocol instance
323 * @log_format: type of event log format
324 * @event_log_location: pointer to the memory address of the event log
325 * @event_log_last_entry: pointer to the address of the start of the last
326 * entry in the event log in memory, if log contains
327 * more than 1 entry
328 * @event_log_truncated: set to true, if the Event Log is missing at i
329 * least one entry
330 *
331 * Return: status code
332 */
333static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200334efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
335 efi_tcg_event_log_format log_format,
336 u64 *event_log_location, u64 *event_log_last_entry,
337 bool *event_log_truncated)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200338{
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200339 efi_status_t ret = EFI_SUCCESS;
340 struct udevice *dev;
341
342 EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
343 event_log_last_entry, event_log_truncated);
344
Masahisa Kojima580d7242021-09-03 10:55:50 +0900345 if (!this || !event_log_location || !event_log_last_entry ||
346 !event_log_truncated) {
347 ret = EFI_INVALID_PARAMETER;
348 goto out;
349 }
350
351 /* Only support TPMV2 */
352 if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) {
353 ret = EFI_INVALID_PARAMETER;
354 goto out;
355 }
356
Eddie James97707f12023-10-24 10:43:49 -0500357 ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200358 if (ret != EFI_SUCCESS) {
359 event_log_location = NULL;
360 event_log_last_entry = NULL;
361 *event_log_truncated = false;
362 ret = EFI_SUCCESS;
363 goto out;
364 }
365 *event_log_location = (uintptr_t)event_log.buffer;
366 *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
367 event_log.last_event_size);
368 *event_log_truncated = event_log.truncated;
369 event_log.get_event_called = true;
370
371out:
372 return EFI_EXIT(ret);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200373}
374
375/**
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900376 * tcg2_hash_pe_image() - calculate PE/COFF image hash
377 *
378 * @efi: pointer to the EFI binary
379 * @efi_size: size of @efi binary
380 * @digest_list: list of digest algorithms to extend
381 *
382 * Return: status code
383 */
384static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size,
385 struct tpml_digest_values *digest_list)
386{
387 WIN_CERTIFICATE *wincerts = NULL;
388 size_t wincerts_len;
389 struct efi_image_regions *regs = NULL;
390 void *new_efi = NULL;
391 u8 hash[TPM2_SHA512_DIGEST_SIZE];
Eddie James97707f12023-10-24 10:43:49 -0500392 struct udevice *dev;
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900393 efi_status_t ret;
394 u32 active;
395 int i;
396
397 new_efi = efi_prepare_aligned_image(efi, &efi_size);
398 if (!new_efi)
399 return EFI_OUT_OF_RESOURCES;
400
401 if (!efi_image_parse(new_efi, efi_size, &regs, &wincerts,
402 &wincerts_len)) {
403 log_err("Parsing PE executable image failed\n");
404 ret = EFI_UNSUPPORTED;
405 goto out;
406 }
407
Eddie James97707f12023-10-24 10:43:49 -0500408 ret = tcg2_platform_get_tpm2(&dev);
409 if (ret != EFI_SUCCESS)
410 goto out;
411
412 ret = tcg2_get_active_pcr_banks(dev, &active);
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900413 if (ret != EFI_SUCCESS) {
414 goto out;
415 }
416
417 digest_list->count = 0;
Eddie James97707f12023-10-24 10:43:49 -0500418 for (i = 0; i < ARRAY_SIZE(tpm2_supported_algorithms); i++) {
419 u16 hash_alg = tpm2_supported_algorithms[i];
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900420
Eddie James97707f12023-10-24 10:43:49 -0500421 if (!(active & tpm2_algorithm_to_mask(hash_alg)))
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900422 continue;
423 switch (hash_alg) {
424 case TPM2_ALG_SHA1:
425 hash_calculate("sha1", regs->reg, regs->num, hash);
426 break;
427 case TPM2_ALG_SHA256:
428 hash_calculate("sha256", regs->reg, regs->num, hash);
429 break;
430 case TPM2_ALG_SHA384:
431 hash_calculate("sha384", regs->reg, regs->num, hash);
432 break;
433 case TPM2_ALG_SHA512:
434 hash_calculate("sha512", regs->reg, regs->num, hash);
435 break;
436 default:
Heinrich Schuchardtd12c3ef2023-07-31 14:11:34 +0200437 continue;
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900438 }
Ruchika Gupta346cee32021-09-14 12:14:31 +0530439 digest_list->digests[digest_list->count].hash_alg = hash_alg;
440 memcpy(&digest_list->digests[digest_list->count].digest, hash,
Eddie James97707f12023-10-24 10:43:49 -0500441 (u32)tpm2_algorithm_to_len(hash_alg));
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900442 digest_list->count++;
443 }
444
445out:
446 if (new_efi != efi)
447 free(new_efi);
448 free(regs);
449
450 return ret;
451}
452
453/**
454 * tcg2_measure_pe_image() - measure PE/COFF image
455 *
456 * @efi: pointer to the EFI binary
457 * @efi_size: size of @efi binary
458 * @handle: loaded image handle
459 * @loaded_image: loaded image protocol
460 *
461 * Return: status code
462 */
463efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size,
464 struct efi_loaded_image_obj *handle,
465 struct efi_loaded_image *loaded_image)
466{
467 struct tpml_digest_values digest_list;
468 efi_status_t ret;
469 struct udevice *dev;
470 u32 pcr_index, event_type, event_size;
471 struct uefi_image_load_event *image_load_event;
472 struct efi_device_path *device_path;
473 u32 device_path_length;
474 IMAGE_DOS_HEADER *dos;
475 IMAGE_NT_HEADERS32 *nt;
476 struct efi_handler *handler;
477
Masahisa Kojima9e32bf92021-12-07 14:15:32 +0900478 if (!is_tcg2_protocol_installed())
479 return EFI_SUCCESS;
480
Eddie James97707f12023-10-24 10:43:49 -0500481 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900482 if (ret != EFI_SUCCESS)
Masahisa Kojimaf9b51dc2021-12-07 14:15:33 +0900483 return EFI_SECURITY_VIOLATION;
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900484
485 switch (handle->image_type) {
486 case IMAGE_SUBSYSTEM_EFI_APPLICATION:
487 pcr_index = 4;
488 event_type = EV_EFI_BOOT_SERVICES_APPLICATION;
489 break;
490 case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
491 pcr_index = 2;
492 event_type = EV_EFI_BOOT_SERVICES_DRIVER;
493 break;
494 case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
495 pcr_index = 2;
496 event_type = EV_EFI_RUNTIME_SERVICES_DRIVER;
497 break;
498 default:
499 return EFI_UNSUPPORTED;
500 }
501
502 ret = tcg2_hash_pe_image(efi, efi_size, &digest_list);
503 if (ret != EFI_SUCCESS)
504 return ret;
505
506 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
507 if (ret != EFI_SUCCESS)
508 return ret;
509
Ilias Apalodimas0bf538c2021-09-09 00:30:49 +0300510 ret = efi_search_protocol(&handle->header,
511 &efi_guid_loaded_image_device_path, &handler);
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900512 if (ret != EFI_SUCCESS)
513 return ret;
514
Ilias Apalodimas0bf538c2021-09-09 00:30:49 +0300515 device_path = handler->protocol_interface;
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900516 device_path_length = efi_dp_size(device_path);
517 if (device_path_length > 0) {
518 /* add end node size */
519 device_path_length += sizeof(struct efi_device_path);
520 }
521 event_size = sizeof(struct uefi_image_load_event) + device_path_length;
Ilias Apalodimas0bf538c2021-09-09 00:30:49 +0300522 image_load_event = calloc(1, event_size);
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900523 if (!image_load_event)
524 return EFI_OUT_OF_RESOURCES;
525
526 image_load_event->image_location_in_memory = (uintptr_t)efi;
527 image_load_event->image_length_in_memory = efi_size;
528 image_load_event->length_of_device_path = device_path_length;
529
530 dos = (IMAGE_DOS_HEADER *)efi;
531 nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew);
532 if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
533 IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt;
534
535 image_load_event->image_link_time_address =
536 nt64->OptionalHeader.ImageBase;
537 } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
538 image_load_event->image_link_time_address =
539 nt->OptionalHeader.ImageBase;
540 } else {
541 ret = EFI_INVALID_PARAMETER;
542 goto out;
543 }
544
Ilias Apalodimas0bf538c2021-09-09 00:30:49 +0300545 /* device_path_length might be zero */
546 memcpy(image_load_event->device_path, device_path, device_path_length);
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900547
548 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
549 event_size, (u8 *)image_load_event);
550
551out:
552 free(image_load_event);
553
554 return ret;
555}
556
557/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200558 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200559 *
560 * @this: TCG2 protocol instance
561 * @flags: bitmap providing additional information on the
562 * operation
563 * @data_to_hash: physical address of the start of the data buffer
564 * to be hashed
565 * @data_to_hash_len: the length in bytes of the buffer referenced by
566 * data_to_hash
567 * @efi_tcg_event: pointer to data buffer containing information
568 * about the event
569 *
570 * Return: status code
571 */
572static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200573efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
574 u64 data_to_hash, u64 data_to_hash_len,
575 struct efi_tcg2_event *efi_tcg_event)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200576{
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200577 struct udevice *dev;
578 efi_status_t ret;
579 u32 event_type, pcr_index, event_size;
580 struct tpml_digest_values digest_list;
581
582 EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
583 data_to_hash_len, efi_tcg_event);
584
585 if (!this || !data_to_hash || !efi_tcg_event) {
586 ret = EFI_INVALID_PARAMETER;
587 goto out;
588 }
589
Eddie James97707f12023-10-24 10:43:49 -0500590 ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200591 if (ret != EFI_SUCCESS)
592 goto out;
593
594 if (efi_tcg_event->size < efi_tcg_event->header.header_size +
595 sizeof(u32)) {
596 ret = EFI_INVALID_PARAMETER;
597 goto out;
598 }
599
Masahisa Kojima538c0f22021-09-03 10:55:52 +0900600 if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) {
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200601 ret = EFI_INVALID_PARAMETER;
602 goto out;
603 }
604
605 /*
606 * if PE_COFF_IMAGE is set we need to make sure the image is not
607 * corrupted, verify it and hash the PE/COFF image in accordance with
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900608 * the procedure specified in "Calculating the PE Image Hash"
609 * section of the "Windows Authenticode Portable Executable Signature
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200610 * Format"
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200611 */
612 if (flags & PE_COFF_IMAGE) {
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900613 IMAGE_NT_HEADERS32 *nt;
614
615 ret = efi_check_pe((void *)(uintptr_t)data_to_hash,
616 data_to_hash_len, (void **)&nt);
617 if (ret != EFI_SUCCESS) {
618 log_err("Not a valid PE-COFF file\n");
Masahisa Kojima580d7242021-09-03 10:55:50 +0900619 ret = EFI_UNSUPPORTED;
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900620 goto out;
621 }
622 ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash,
623 data_to_hash_len, &digest_list);
624 } else {
Eddie James97707f12023-10-24 10:43:49 -0500625 ret = tcg2_create_digest(dev, (u8 *)(uintptr_t)data_to_hash,
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900626 data_to_hash_len, &digest_list);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200627 }
628
Masahisa Kojima163a0d72021-05-26 12:09:58 +0900629 if (ret != EFI_SUCCESS)
630 goto out;
631
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200632 pcr_index = efi_tcg_event->header.pcr_index;
633 event_type = efi_tcg_event->header.event_type;
634
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200635 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
636 if (ret != EFI_SUCCESS)
637 goto out;
638
639 if (flags & EFI_TCG2_EXTEND_ONLY) {
640 if (event_log.truncated)
641 ret = EFI_VOLUME_FULL;
642 goto out;
643 }
644
645 /*
646 * The efi_tcg_event size includes the size component and the
647 * headersize
648 */
649 event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
650 efi_tcg_event->header.header_size;
651 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
652 event_size, efi_tcg_event->event);
653out:
654 return EFI_EXIT(ret);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200655}
656
657/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200658 * efi_tcg2_submit_command() - Send command to the TPM
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200659 *
660 * @this: TCG2 protocol instance
661 * @input_param_block_size: size of the TPM input parameter block
662 * @input_param_block: pointer to the TPM input parameter block
663 * @output_param_block_size: size of the TPM output parameter block
664 * @output_param_block: pointer to the TPM output parameter block
665 *
666 * Return: status code
667 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200668static efi_status_t EFIAPI
Masahisa Kojima7fc93ca2021-11-04 22:59:16 +0900669efi_tcg2_submit_command(struct efi_tcg2_protocol *this,
670 u32 input_param_block_size,
671 u8 *input_param_block,
672 u32 output_param_block_size,
673 u8 *output_param_block)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200674{
Masahisa Kojima7fc93ca2021-11-04 22:59:16 +0900675 struct udevice *dev;
676 efi_status_t ret;
677 u32 rc;
678 size_t resp_buf_size = output_param_block_size;
679
680 EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size,
681 input_param_block, output_param_block_size, output_param_block);
682
683 if (!this || !input_param_block || !input_param_block_size) {
684 ret = EFI_INVALID_PARAMETER;
685 goto out;
686 }
687
Eddie James97707f12023-10-24 10:43:49 -0500688 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima7fc93ca2021-11-04 22:59:16 +0900689 if (ret != EFI_SUCCESS)
690 goto out;
691
692 rc = tpm2_submit_command(dev, input_param_block,
693 output_param_block, &resp_buf_size);
694 if (rc) {
695 ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR;
696
697 goto out;
698 }
699
700out:
701 return EFI_EXIT(ret);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200702}
703
704/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200705 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200706 *
707 * @this: TCG2 protocol instance
708 * @active_pcr_banks: pointer for receiving the bitmap of currently
709 * active PCR banks
710 *
711 * Return: status code
712 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200713static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200714efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
715 u32 *active_pcr_banks)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200716{
Eddie James97707f12023-10-24 10:43:49 -0500717 struct udevice *dev;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200718 efi_status_t ret;
719
Ilias Apalodimase9fc0182023-10-24 10:43:53 -0500720 EFI_ENTRY("%p, %p", this, active_pcr_banks);
721
Masahisa Kojima580d7242021-09-03 10:55:50 +0900722 if (!this || !active_pcr_banks) {
723 ret = EFI_INVALID_PARAMETER;
724 goto out;
725 }
Eddie James97707f12023-10-24 10:43:49 -0500726 ret = tcg2_platform_get_tpm2(&dev);
727 if (ret != EFI_SUCCESS)
728 goto out;
729
Eddie James97707f12023-10-24 10:43:49 -0500730 ret = tcg2_get_active_pcr_banks(dev, active_pcr_banks);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200731
Masahisa Kojima580d7242021-09-03 10:55:50 +0900732out:
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200733 return EFI_EXIT(ret);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200734}
735
736/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200737 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200738 *
739 * @this: TCG2 protocol instance
740 * @active_pcr_banks: bitmap of the requested active PCR banks
741 *
742 * Return: status code
743 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200744static efi_status_t EFIAPI
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +0300745efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
746 u32 __maybe_unused active_pcr_banks)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200747{
748 return EFI_UNSUPPORTED;
749}
750
751/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200752 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
753 * set_active_pcr_banks()
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200754 *
755 * @this: TCG2 protocol instance
756 * @operation_present: non-zero value to indicate a
757 * set_active_pcr_banks operation was
758 * invoked during last boot
759 * @response: result value could be returned
760 *
761 * Return: status code
762 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200763static efi_status_t EFIAPI
Ilias Apalodimas1f6871d2021-05-25 14:35:31 +0300764efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
765 u32 __maybe_unused *operation_present,
766 u32 __maybe_unused *response)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200767{
768 return EFI_UNSUPPORTED;
769}
770
771static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200772 .get_capability = efi_tcg2_get_capability,
773 .get_eventlog = efi_tcg2_get_eventlog,
774 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
775 .submit_command = efi_tcg2_submit_command,
776 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
777 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
778 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200779};
780
781/**
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +0200782 * tcg2_uninit - remove the final event table and free efi memory on failures
783 */
784void tcg2_uninit(void)
785{
786 efi_status_t ret;
787
788 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
789 if (ret != EFI_SUCCESS)
790 log_err("Failed to delete final events config table\n");
791
792 efi_free_pool(event_log.buffer);
793 event_log.buffer = NULL;
794 efi_free_pool(event_log.final_buffer);
795 event_log.final_buffer = NULL;
Masahisa Kojima54bec172021-12-07 14:15:31 +0900796
797 if (!is_tcg2_protocol_installed())
798 return;
799
Ilias Apalodimas4a3baf92023-06-19 14:14:02 +0300800 ret = efi_uninstall_multiple_protocol_interfaces(efi_root, &efi_guid_tcg2_protocol,
801 &efi_tcg2_protocol, NULL);
Masahisa Kojima54bec172021-12-07 14:15:31 +0900802 if (ret != EFI_SUCCESS)
803 log_err("Failed to remove EFI TCG2 protocol\n");
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +0200804}
805
806/**
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200807 * create_final_event() - Create the final event and install the config
808 * defined by the TCG EFI spec
809 */
810static efi_status_t create_final_event(void)
811{
812 struct efi_tcg2_final_events_table *final_event;
813 efi_status_t ret;
814
815 /*
816 * All events generated after the invocation of
817 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
818 * EFI_CONFIGURATION_TABLE
819 */
820 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
821 &event_log.final_buffer);
822 if (ret != EFI_SUCCESS)
823 goto out;
824
825 memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
826 final_event = event_log.final_buffer;
827 final_event->number_of_events = 0;
828 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
829 event_log.final_pos = sizeof(*final_event);
830 ret = efi_install_configuration_table(&efi_guid_final_events,
831 final_event);
Ilias Apalodimas20527592021-05-12 00:03:41 +0300832 if (ret != EFI_SUCCESS) {
833 efi_free_pool(event_log.final_buffer);
834 event_log.final_buffer = NULL;
835 }
836
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200837out:
838 return ret;
839}
840
841/**
Eddie James97707f12023-10-24 10:43:49 -0500842 * measure_event() - common function to add event log and extend PCR
Masahisa Kojimacfbcf052021-08-13 16:12:39 +0900843 *
844 * @dev: TPM device
845 * @pcr_index: PCR index
846 * @event_type: type of event added
847 * @size: event size
848 * @event: event data
849 *
850 * Return: status code
851 */
Eddie James97707f12023-10-24 10:43:49 -0500852static efi_status_t measure_event(struct udevice *dev, u32 pcr_index,
853 u32 event_type, u32 size, u8 event[])
Masahisa Kojimacfbcf052021-08-13 16:12:39 +0900854{
855 struct tpml_digest_values digest_list;
856 efi_status_t ret;
857
Eddie James97707f12023-10-24 10:43:49 -0500858 ret = tcg2_create_digest(dev, event, size, &digest_list);
Masahisa Kojimacfbcf052021-08-13 16:12:39 +0900859 if (ret != EFI_SUCCESS)
860 goto out;
861
862 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
863 if (ret != EFI_SUCCESS)
864 goto out;
865
866 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
867 size, event);
868
869out:
870 return ret;
871}
872
873/**
Ilias Apalodimasf69a2012021-03-24 16:50:46 +0200874 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
875 * eventlog and extend the PCRs
876 *
877 * @dev: TPM device
878 *
879 * @Return: status code
880 */
881static efi_status_t efi_append_scrtm_version(struct udevice *dev)
882{
Ilias Apalodimasf69a2012021-03-24 16:50:46 +0200883 efi_status_t ret;
884
Eddie James97707f12023-10-24 10:43:49 -0500885 ret = measure_event(dev, 0, EV_S_CRTM_VERSION,
886 strlen(version_string) + 1, (u8 *)version_string);
Ilias Apalodimasf69a2012021-03-24 16:50:46 +0200887
Ilias Apalodimasf69a2012021-03-24 16:50:46 +0200888 return ret;
889}
890
891/**
Ruchika Gupta34287ef2021-11-29 13:09:44 +0530892 * efi_init_event_log() - initialize an eventlog
893 *
894 * Return: status code
895 */
896static efi_status_t efi_init_event_log(void)
897{
898 /*
899 * vendor_info_size is currently set to 0, we need to change the length
900 * and allocate the flexible array member if this changes
901 */
Eddie James97707f12023-10-24 10:43:49 -0500902 struct tcg2_event_log elog;
Ruchika Gupta34287ef2021-11-29 13:09:44 +0530903 struct udevice *dev;
Ruchika Gupta34287ef2021-11-29 13:09:44 +0530904 efi_status_t ret;
905
Eddie James97707f12023-10-24 10:43:49 -0500906 ret = tcg2_platform_get_tpm2(&dev);
Ruchika Gupta34287ef2021-11-29 13:09:44 +0530907 if (ret != EFI_SUCCESS)
908 return ret;
909
910 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
911 (void **)&event_log.buffer);
912 if (ret != EFI_SUCCESS)
913 return ret;
914
915 /*
916 * initialize log area as 0xff so the OS can easily figure out the
917 * last log entry
918 */
919 memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
920
921 /*
922 * The log header is defined to be in SHA1 event log entry format.
923 * Setup event header
924 */
Ruchika Gupta34287ef2021-11-29 13:09:44 +0530925 event_log.pos = 0;
926 event_log.last_event_size = 0;
927 event_log.get_event_called = false;
928 event_log.ebs_called = false;
929 event_log.truncated = false;
930
931 /*
932 * Check if earlier firmware have passed any eventlog. Different
933 * platforms can use different ways to do so.
934 */
Eddie James97707f12023-10-24 10:43:49 -0500935 elog.log = event_log.buffer;
936 elog.log_size = TPM2_EVENT_LOG_SIZE;
937 ret = tcg2_log_prepare_buffer(dev, &elog, false);
Ruchika Gupta34287ef2021-11-29 13:09:44 +0530938 if (ret != EFI_SUCCESS)
939 goto free_pool;
940
Eddie James97707f12023-10-24 10:43:49 -0500941 event_log.pos = elog.log_position;
942
943 /*
944 * Add SCRTM version to the log if previous firmmware
945 * doesn't pass an eventlog.
946 */
947 if (!elog.found)
948 ret = efi_append_scrtm_version(dev);
949
Ruchika Gupta34287ef2021-11-29 13:09:44 +0530950 ret = create_final_event();
951 if (ret != EFI_SUCCESS)
952 goto free_pool;
953
954 return ret;
955
956free_pool:
957 efi_free_pool(event_log.buffer);
958 event_log.buffer = NULL;
959 return ret;
960}
961
962/**
Masahisa Kojimacfbcf052021-08-13 16:12:39 +0900963 * tcg2_measure_variable() - add variable event log and extend PCR
964 *
965 * @dev: TPM device
966 * @pcr_index: PCR index
967 * @event_type: type of event added
968 * @var_name: variable name
969 * @guid: guid
970 * @data_size: variable data size
971 * @data: variable data
972 *
973 * Return: status code
974 */
975static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index,
Heinrich Schuchardtd47671c2021-09-09 07:12:14 +0200976 u32 event_type, const u16 *var_name,
Masahisa Kojimacfbcf052021-08-13 16:12:39 +0900977 const efi_guid_t *guid,
978 efi_uintn_t data_size, u8 *data)
979{
980 u32 event_size;
981 efi_status_t ret;
982 struct efi_tcg2_uefi_variable_data *event;
983
984 event_size = sizeof(event->variable_name) +
985 sizeof(event->unicode_name_length) +
986 sizeof(event->variable_data_length) +
987 (u16_strlen(var_name) * sizeof(u16)) + data_size;
988 event = malloc(event_size);
989 if (!event)
990 return EFI_OUT_OF_RESOURCES;
991
992 guidcpy(&event->variable_name, guid);
993 event->unicode_name_length = u16_strlen(var_name);
994 event->variable_data_length = data_size;
995 memcpy(event->unicode_name, var_name,
996 (event->unicode_name_length * sizeof(u16)));
997 if (data) {
998 memcpy((u16 *)event->unicode_name + event->unicode_name_length,
999 data, data_size);
1000 }
Eddie James97707f12023-10-24 10:43:49 -05001001 ret = measure_event(dev, pcr_index, event_type, event_size,
1002 (u8 *)event);
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09001003 free(event);
1004 return ret;
1005}
1006
1007/**
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001008 * tcg2_measure_boot_variable() - measure boot variables
1009 *
1010 * @dev: TPM device
1011 *
1012 * Return: status code
1013 */
1014static efi_status_t tcg2_measure_boot_variable(struct udevice *dev)
1015{
1016 u16 *boot_order;
1017 u16 *boot_index;
Simon Glass156ccbc2022-01-23 12:55:12 -07001018 u16 var_name[] = u"BootOrder";
1019 u16 boot_name[] = u"Boot####";
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001020 u8 *bootvar;
1021 efi_uintn_t var_data_size;
1022 u32 count, i;
1023 efi_status_t ret;
1024
1025 boot_order = efi_get_var(var_name, &efi_global_variable_guid,
1026 &var_data_size);
1027 if (!boot_order) {
Masahisa Kojimac9c1cdb2021-11-09 18:44:54 +09001028 /* If "BootOrder" is not defined, skip the boot variable measurement */
1029 return EFI_SUCCESS;
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001030 }
1031
1032 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name,
1033 &efi_global_variable_guid, var_data_size,
1034 (u8 *)boot_order);
1035 if (ret != EFI_SUCCESS)
1036 goto error;
1037
1038 count = var_data_size / sizeof(*boot_order);
1039 boot_index = boot_order;
1040 for (i = 0; i < count; i++) {
1041 efi_create_indexed_name(boot_name, sizeof(boot_name),
1042 "Boot", *boot_index++);
1043
1044 bootvar = efi_get_var(boot_name, &efi_global_variable_guid,
1045 &var_data_size);
1046
1047 if (!bootvar) {
Masahisa Kojima3961bd92021-11-09 20:35:53 +09001048 log_debug("%ls not found\n", boot_name);
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001049 continue;
1050 }
1051
1052 ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2,
1053 boot_name,
1054 &efi_global_variable_guid,
1055 var_data_size, bootvar);
1056 free(bootvar);
1057 if (ret != EFI_SUCCESS)
1058 goto error;
1059 }
1060
1061error:
1062 free(boot_order);
1063 return ret;
1064}
1065
1066/**
Masahisa Kojima3d49ee82021-10-26 17:27:24 +09001067 * tcg2_measure_smbios() - measure smbios table
1068 *
1069 * @dev: TPM device
1070 * @entry: pointer to the smbios_entry structure
1071 *
1072 * Return: status code
1073 */
1074static efi_status_t
1075tcg2_measure_smbios(struct udevice *dev,
1076 const struct smbios_entry *entry)
1077{
1078 efi_status_t ret;
1079 struct smbios_header *smbios_copy;
1080 struct smbios_handoff_table_pointers2 *event = NULL;
1081 u32 event_size;
1082
1083 /*
1084 * TCG PC Client PFP Spec says
1085 * "SMBIOS structures that contain static configuration information
1086 * (e.g. Platform Manufacturer Enterprise Number assigned by IANA,
1087 * platform model number, Vendor and Device IDs for each SMBIOS table)
1088 * that is relevant to the security of the platform MUST be measured".
1089 * Device dependent parameters such as serial number are cleared to
1090 * zero or spaces for the measurement.
1091 */
1092 event_size = sizeof(struct smbios_handoff_table_pointers2) +
1093 FIELD_SIZEOF(struct efi_configuration_table, guid) +
1094 entry->struct_table_length;
1095 event = calloc(1, event_size);
1096 if (!event) {
1097 ret = EFI_OUT_OF_RESOURCES;
1098 goto out;
1099 }
1100
1101 event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC);
1102 memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC,
1103 sizeof(SMBIOS_HANDOFF_TABLE_DESC));
1104 put_unaligned_le64(1, &event->number_of_tables);
1105 guidcpy(&event->table_entry[0].guid, &smbios_guid);
1106 smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table);
1107 memcpy(&event->table_entry[0].table,
1108 (void *)((uintptr_t)entry->struct_table_address),
1109 entry->struct_table_length);
1110
1111 smbios_prepare_measurement(entry, smbios_copy);
1112
Eddie James97707f12023-10-24 10:43:49 -05001113 ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size,
1114 (u8 *)event);
Masahisa Kojima3d49ee82021-10-26 17:27:24 +09001115 if (ret != EFI_SUCCESS)
1116 goto out;
1117
1118out:
1119 free(event);
1120
1121 return ret;
1122}
1123
1124/**
1125 * find_smbios_table() - find smbios table
1126 *
1127 * Return: pointer to the smbios table
1128 */
1129static void *find_smbios_table(void)
1130{
1131 u32 i;
1132
1133 for (i = 0; i < systab.nr_tables; i++) {
1134 if (!guidcmp(&smbios_guid, &systab.tables[i].guid))
1135 return systab.tables[i].table;
1136 }
1137
1138 return NULL;
1139}
1140
1141/**
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09001142 * tcg2_measure_gpt_table() - measure gpt table
1143 *
1144 * @dev: TPM device
1145 * @loaded_image: handle to the loaded image
1146 *
1147 * Return: status code
1148 */
1149static efi_status_t
1150tcg2_measure_gpt_data(struct udevice *dev,
1151 struct efi_loaded_image_obj *loaded_image)
1152{
1153 efi_status_t ret;
1154 efi_handle_t handle;
Heinrich Schuchardtbb8bb302022-10-07 14:28:18 +02001155 struct efi_handler *dp_handler, *io_handler;
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09001156 struct efi_device_path *orig_device_path;
1157 struct efi_device_path *device_path;
1158 struct efi_device_path *dp;
1159 struct efi_block_io *block_io;
1160 struct efi_gpt_data *event = NULL;
1161 efi_guid_t null_guid = NULL_GUID;
1162 gpt_header *gpt_h;
1163 gpt_entry *entry = NULL;
1164 gpt_entry *gpt_e;
1165 u32 num_of_valid_entry = 0;
1166 u32 event_size;
1167 u32 i;
1168 u32 total_gpt_entry_size;
1169
1170 ret = efi_search_protocol(&loaded_image->header,
1171 &efi_guid_loaded_image_device_path,
1172 &dp_handler);
1173 if (ret != EFI_SUCCESS)
1174 return ret;
1175
1176 orig_device_path = dp_handler->protocol_interface;
1177 if (!orig_device_path) /* no device path, skip GPT measurement */
1178 return EFI_SUCCESS;
1179
1180 device_path = efi_dp_dup(orig_device_path);
1181 if (!device_path)
1182 return EFI_OUT_OF_RESOURCES;
1183
1184 dp = search_gpt_dp_node(device_path);
1185 if (!dp) {
1186 /* no GPT device path node found, skip GPT measurement */
1187 ret = EFI_SUCCESS;
1188 goto out1;
1189 }
1190
1191 /* read GPT header */
1192 dp->type = DEVICE_PATH_TYPE_END;
1193 dp->sub_type = DEVICE_PATH_SUB_TYPE_END;
1194 dp = device_path;
1195 ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid,
1196 &dp, &handle));
1197 if (ret != EFI_SUCCESS)
1198 goto out1;
1199
Heinrich Schuchardtbb8bb302022-10-07 14:28:18 +02001200 ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler);
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09001201 if (ret != EFI_SUCCESS)
1202 goto out1;
Heinrich Schuchardtbb8bb302022-10-07 14:28:18 +02001203 block_io = io_handler->protocol_interface;
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09001204
1205 gpt_h = memalign(block_io->media->io_align, block_io->media->block_size);
1206 if (!gpt_h) {
1207 ret = EFI_OUT_OF_RESOURCES;
1208 goto out2;
1209 }
1210
1211 ret = block_io->read_blocks(block_io, block_io->media->media_id, 1,
1212 block_io->media->block_size, gpt_h);
1213 if (ret != EFI_SUCCESS)
1214 goto out2;
1215
1216 /* read GPT entry */
1217 total_gpt_entry_size = gpt_h->num_partition_entries *
1218 gpt_h->sizeof_partition_entry;
1219 entry = memalign(block_io->media->io_align, total_gpt_entry_size);
1220 if (!entry) {
1221 ret = EFI_OUT_OF_RESOURCES;
1222 goto out2;
1223 }
1224
1225 ret = block_io->read_blocks(block_io, block_io->media->media_id,
1226 gpt_h->partition_entry_lba,
1227 total_gpt_entry_size, entry);
1228 if (ret != EFI_SUCCESS)
1229 goto out2;
1230
1231 /* count valid GPT entry */
1232 gpt_e = entry;
1233 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1234 if (guidcmp(&null_guid, &gpt_e->partition_type_guid))
1235 num_of_valid_entry++;
1236
1237 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1238 }
1239
1240 /* prepare event data for measurement */
1241 event_size = sizeof(struct efi_gpt_data) +
1242 (num_of_valid_entry * gpt_h->sizeof_partition_entry);
1243 event = calloc(1, event_size);
1244 if (!event) {
1245 ret = EFI_OUT_OF_RESOURCES;
1246 goto out2;
1247 }
1248 memcpy(event, gpt_h, sizeof(gpt_header));
1249 put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions);
1250
1251 /* copy valid GPT entry */
1252 gpt_e = entry;
1253 num_of_valid_entry = 0;
1254 for (i = 0; i < gpt_h->num_partition_entries; i++) {
1255 if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) {
1256 memcpy((u8 *)event->partitions +
1257 (num_of_valid_entry * gpt_h->sizeof_partition_entry),
1258 gpt_e, gpt_h->sizeof_partition_entry);
1259 num_of_valid_entry++;
1260 }
1261
1262 gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry);
1263 }
1264
Eddie James97707f12023-10-24 10:43:49 -05001265 ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event);
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09001266
1267out2:
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09001268 free(gpt_h);
1269 free(entry);
1270 free(event);
1271out1:
1272 efi_free_pool(device_path);
1273
1274 return ret;
1275}
1276
Etienne Carriereaa2d3942023-02-16 17:29:48 +01001277/* Return the byte size of reserved map area in DTB or -1 upon error */
1278static ssize_t size_of_rsvmap(void *dtb)
1279{
1280 struct fdt_reserve_entry e;
1281 ssize_t size_max;
1282 ssize_t size;
1283 u8 *rsvmap_base;
1284
1285 rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb);
1286 size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb);
1287 size = 0;
1288
1289 do {
1290 memcpy(&e, rsvmap_base + size, sizeof(e));
1291 size += sizeof(e);
1292 if (size > size_max)
1293 return -1;
1294 } while (e.size);
1295
1296 return size;
1297}
1298
1299/**
1300 * efi_tcg2_measure_dtb() - measure DTB passed to the OS
1301 *
1302 * @dtb: pointer to the device tree blob
1303 *
1304 * Return: status code
1305 */
1306efi_status_t efi_tcg2_measure_dtb(void *dtb)
1307{
1308 struct uefi_platform_firmware_blob2 *blob;
1309 struct fdt_header *header;
1310 sha256_context hash_ctx;
1311 struct udevice *dev;
1312 ssize_t rsvmap_size;
1313 efi_status_t ret;
1314 u32 event_size;
1315
1316 if (!is_tcg2_protocol_installed())
1317 return EFI_SUCCESS;
1318
Eddie James97707f12023-10-24 10:43:49 -05001319 ret = tcg2_platform_get_tpm2(&dev);
Etienne Carriereaa2d3942023-02-16 17:29:48 +01001320 if (ret != EFI_SUCCESS)
1321 return EFI_SECURITY_VIOLATION;
1322
1323 rsvmap_size = size_of_rsvmap(dtb);
1324 if (rsvmap_size < 0)
1325 return EFI_SECURITY_VIOLATION;
1326
1327 event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN;
1328 blob = calloc(1, event_size);
1329 if (!blob)
1330 return EFI_OUT_OF_RESOURCES;
1331
1332 blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING);
1333 memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size);
1334
1335 /* Measure populated areas of the DTB */
1336 header = dtb;
1337 sha256_starts(&hash_ctx);
1338 sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header));
1339 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb));
1340 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb));
1341 sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size);
1342 sha256_finish(&hash_ctx, blob->data + blob->blob_description_size);
1343
Eddie James97707f12023-10-24 10:43:49 -05001344 ret = measure_event(dev, 0, EV_POST_CODE, event_size, (u8 *)blob);
Etienne Carriereaa2d3942023-02-16 17:29:48 +01001345
1346 free(blob);
1347 return ret;
1348}
1349
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09001350/**
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001351 * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation
1352 *
1353 * Return: status code
1354 */
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09001355efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle)
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001356{
1357 efi_status_t ret;
1358 u32 pcr_index;
1359 struct udevice *dev;
1360 u32 event = 0;
Masahisa Kojima3d49ee82021-10-26 17:27:24 +09001361 struct smbios_entry *entry;
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001362
Masahisa Kojima9e32bf92021-12-07 14:15:32 +09001363 if (!is_tcg2_protocol_installed())
1364 return EFI_SUCCESS;
1365
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001366 if (tcg2_efi_app_invoked)
1367 return EFI_SUCCESS;
1368
Eddie James97707f12023-10-24 10:43:49 -05001369 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001370 if (ret != EFI_SUCCESS)
Masahisa Kojimaf9b51dc2021-12-07 14:15:33 +09001371 return EFI_SECURITY_VIOLATION;
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001372
1373 ret = tcg2_measure_boot_variable(dev);
1374 if (ret != EFI_SUCCESS)
1375 goto out;
1376
Eddie James97707f12023-10-24 10:43:49 -05001377 ret = measure_event(dev, 4, EV_EFI_ACTION,
1378 strlen(EFI_CALLING_EFI_APPLICATION),
1379 (u8 *)EFI_CALLING_EFI_APPLICATION);
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001380 if (ret != EFI_SUCCESS)
1381 goto out;
1382
Masahisa Kojima3d49ee82021-10-26 17:27:24 +09001383 entry = (struct smbios_entry *)find_smbios_table();
1384 if (entry) {
1385 ret = tcg2_measure_smbios(dev, entry);
1386 if (ret != EFI_SUCCESS)
1387 goto out;
1388 }
1389
Masahisa Kojimace3dbc52021-10-26 17:27:25 +09001390 ret = tcg2_measure_gpt_data(dev, handle);
1391 if (ret != EFI_SUCCESS)
1392 goto out;
1393
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001394 for (pcr_index = 0; pcr_index <= 7; pcr_index++) {
Eddie James97707f12023-10-24 10:43:49 -05001395 ret = measure_event(dev, pcr_index, EV_SEPARATOR,
1396 sizeof(event), (u8 *)&event);
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001397 if (ret != EFI_SUCCESS)
1398 goto out;
1399 }
1400
1401 tcg2_efi_app_invoked = true;
1402out:
1403 return ret;
1404}
1405
1406/**
1407 * efi_tcg2_measure_efi_app_exit() - measure efi app exit
1408 *
1409 * Return: status code
1410 */
1411efi_status_t efi_tcg2_measure_efi_app_exit(void)
1412{
1413 efi_status_t ret;
1414 struct udevice *dev;
1415
Masahisa Kojima9e32bf92021-12-07 14:15:32 +09001416 if (!is_tcg2_protocol_installed())
1417 return EFI_SUCCESS;
1418
Eddie James97707f12023-10-24 10:43:49 -05001419 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001420 if (ret != EFI_SUCCESS)
1421 return ret;
1422
Eddie James97707f12023-10-24 10:43:49 -05001423 ret = measure_event(dev, 4, EV_EFI_ACTION,
1424 strlen(EFI_RETURNING_FROM_EFI_APPLICATION),
1425 (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION);
Masahisa Kojima8fc4e0b2021-08-13 16:12:40 +09001426 return ret;
1427}
1428
1429/**
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09001430 * efi_tcg2_notify_exit_boot_services() - ExitBootService callback
1431 *
1432 * @event: callback event
1433 * @context: callback context
1434 */
1435static void EFIAPI
1436efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context)
1437{
1438 efi_status_t ret;
1439 struct udevice *dev;
1440
1441 EFI_ENTRY("%p, %p", event, context);
1442
Ilias Apalodimas5ba03972021-11-18 09:03:39 +02001443 event_log.ebs_called = true;
Masahisa Kojima9e32bf92021-12-07 14:15:32 +09001444
1445 if (!is_tcg2_protocol_installed()) {
1446 ret = EFI_SUCCESS;
1447 goto out;
1448 }
1449
Eddie James97707f12023-10-24 10:43:49 -05001450 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09001451 if (ret != EFI_SUCCESS)
1452 goto out;
1453
Eddie James97707f12023-10-24 10:43:49 -05001454 ret = measure_event(dev, 5, EV_EFI_ACTION,
1455 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1456 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09001457 if (ret != EFI_SUCCESS)
1458 goto out;
1459
Eddie James97707f12023-10-24 10:43:49 -05001460 ret = measure_event(dev, 5, EV_EFI_ACTION,
1461 strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED),
1462 (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED);
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09001463
1464out:
1465 EFI_EXIT(ret);
1466}
1467
1468/**
1469 * efi_tcg2_notify_exit_boot_services_failed()
1470 * - notify ExitBootServices() is failed
1471 *
1472 * Return: status code
1473 */
1474efi_status_t efi_tcg2_notify_exit_boot_services_failed(void)
1475{
1476 struct udevice *dev;
1477 efi_status_t ret;
1478
Masahisa Kojima9e32bf92021-12-07 14:15:32 +09001479 if (!is_tcg2_protocol_installed())
1480 return EFI_SUCCESS;
1481
Eddie James97707f12023-10-24 10:43:49 -05001482 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09001483 if (ret != EFI_SUCCESS)
1484 goto out;
1485
Eddie James97707f12023-10-24 10:43:49 -05001486 ret = measure_event(dev, 5, EV_EFI_ACTION,
1487 strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION),
1488 (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION);
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09001489 if (ret != EFI_SUCCESS)
1490 goto out;
1491
Eddie James97707f12023-10-24 10:43:49 -05001492 ret = measure_event(dev, 5, EV_EFI_ACTION,
1493 strlen(EFI_EXIT_BOOT_SERVICES_FAILED),
1494 (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED);
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09001495
1496out:
1497 return ret;
1498}
1499
1500/**
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09001501 * tcg2_measure_secure_boot_variable() - measure secure boot variables
1502 *
1503 * @dev: TPM device
1504 *
1505 * Return: status code
1506 */
1507static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev)
1508{
1509 u8 *data;
1510 efi_uintn_t data_size;
1511 u32 count, i;
1512 efi_status_t ret;
Masahisa Kojima65aa2592021-10-26 17:27:27 +09001513 u8 deployed_mode;
1514 efi_uintn_t size;
1515 u32 deployed_audit_pcr_index = 1;
1516
1517 size = sizeof(deployed_mode);
1518 ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid,
1519 NULL, &size, &deployed_mode, NULL);
1520 if (ret != EFI_SUCCESS || !deployed_mode)
1521 deployed_audit_pcr_index = 7;
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09001522
1523 count = ARRAY_SIZE(secure_variables);
1524 for (i = 0; i < count; i++) {
Heinrich Schuchardta45dac12021-09-09 08:50:01 +02001525 const efi_guid_t *guid;
1526
Masahisa Kojima96485d22021-10-26 17:27:26 +09001527 guid = efi_auth_var_get_guid(secure_variables[i].name);
Heinrich Schuchardta45dac12021-09-09 08:50:01 +02001528
Masahisa Kojima96485d22021-10-26 17:27:26 +09001529 data = efi_get_var(secure_variables[i].name, guid, &data_size);
1530 if (!data && !secure_variables[i].accept_empty)
1531 continue;
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09001532
Masahisa Kojima65aa2592021-10-26 17:27:27 +09001533 if (u16_strcmp(u"DeployedMode", secure_variables[i].name))
1534 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1535 if (u16_strcmp(u"AuditMode", secure_variables[i].name))
1536 secure_variables[i].pcr_index = deployed_audit_pcr_index;
1537
1538 ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index,
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09001539 EV_EFI_VARIABLE_DRIVER_CONFIG,
Masahisa Kojima96485d22021-10-26 17:27:26 +09001540 secure_variables[i].name, guid,
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09001541 data_size, data);
1542 free(data);
1543 if (ret != EFI_SUCCESS)
1544 goto error;
1545 }
1546
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09001547error:
1548 return ret;
1549}
1550
1551/**
Masahisa Kojima54bec172021-12-07 14:15:31 +09001552 * efi_tcg2_do_initial_measurement() - do initial measurement
1553 *
1554 * Return: status code
1555 */
1556efi_status_t efi_tcg2_do_initial_measurement(void)
1557{
1558 efi_status_t ret;
1559 struct udevice *dev;
1560
1561 if (!is_tcg2_protocol_installed())
1562 return EFI_SUCCESS;
1563
Eddie James97707f12023-10-24 10:43:49 -05001564 ret = tcg2_platform_get_tpm2(&dev);
Masahisa Kojima54bec172021-12-07 14:15:31 +09001565 if (ret != EFI_SUCCESS)
1566 return EFI_SECURITY_VIOLATION;
1567
1568 ret = tcg2_measure_secure_boot_variable(dev);
1569 if (ret != EFI_SUCCESS)
1570 goto out;
1571
1572out:
1573 return ret;
1574}
1575
1576/**
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001577 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1578 *
1579 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1580 *
Masahisa Kojima54bec172021-12-07 14:15:31 +09001581 * Return: status code
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001582 */
1583efi_status_t efi_tcg2_register(void)
1584{
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001585 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001586 struct udevice *dev;
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09001587 struct efi_event *event;
Ilias Apalodimasd6b55a42021-11-18 10:13:42 +02001588 u32 err;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001589
Eddie James97707f12023-10-24 10:43:49 -05001590 ret = tcg2_platform_get_tpm2(&dev);
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +02001591 if (ret != EFI_SUCCESS) {
Ilias Apalodimascd63e2d2023-01-19 16:29:15 +02001592 log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n");
Ilias Apalodimas97f446a2021-05-10 21:19:14 +03001593 return EFI_SUCCESS;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001594 }
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001595
Ilias Apalodimasd6b55a42021-11-18 10:13:42 +02001596 /* initialize the TPM as early as possible. */
Ilias Apalodimas78fd2f52023-01-25 13:06:03 +02001597 err = tpm_auto_start(dev);
Ilias Apalodimasd6b55a42021-11-18 10:13:42 +02001598 if (err) {
1599 log_err("TPM startup failed\n");
1600 goto fail;
1601 }
1602
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001603 ret = efi_init_event_log();
Masahisa Kojima54bec172021-12-07 14:15:31 +09001604 if (ret != EFI_SUCCESS) {
1605 tcg2_uninit();
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001606 goto fail;
Masahisa Kojima54bec172021-12-07 14:15:31 +09001607 }
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001608
Ilias Apalodimas4a3baf92023-06-19 14:14:02 +03001609 ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol,
1610 &efi_tcg2_protocol, NULL);
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001611 if (ret != EFI_SUCCESS) {
Ilias Apalodimas20527592021-05-12 00:03:41 +03001612 tcg2_uninit();
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001613 goto fail;
1614 }
Masahisa Kojimacfbcf052021-08-13 16:12:39 +09001615
Masahisa Kojimafdff03e2021-08-13 16:12:41 +09001616 ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK,
1617 efi_tcg2_notify_exit_boot_services, NULL,
1618 NULL, &event);
1619 if (ret != EFI_SUCCESS) {
1620 tcg2_uninit();
1621 goto fail;
1622 }
1623
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001624 return ret;
Ilias Apalodimas97f446a2021-05-10 21:19:14 +03001625
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001626fail:
Ilias Apalodimas20527592021-05-12 00:03:41 +03001627 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
Masahisa Kojima54bec172021-12-07 14:15:31 +09001628 return ret;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001629}