blob: ee743f5951fb9294610381a7bba544a14acb59f5 [file] [log] [blame]
Ilias Apalodimas590fef62020-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>
14#include <efi_tcg2.h>
15#include <log.h>
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +020016#include <version.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020017#include <tpm-v2.h>
Ilias Apalodimas967650d2020-11-30 11:47:40 +020018#include <u-boot/sha1.h>
19#include <u-boot/sha256.h>
20#include <u-boot/sha512.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020021#include <linux/unaligned/access_ok.h>
22#include <linux/unaligned/generic.h>
Ilias Apalodimas967650d2020-11-30 11:47:40 +020023#include <hexdump.h>
Ilias Apalodimas590fef62020-11-11 11:18:11 +020024
Ilias Apalodimas967650d2020-11-30 11:47:40 +020025struct event_log_buffer {
26 void *buffer;
27 void *final_buffer;
28 size_t pos; /* eventlog position */
29 size_t final_pos; /* final events config table position */
30 size_t last_event_size;
31 bool get_event_called;
32 bool truncated;
33};
Ilias Apalodimas590fef62020-11-11 11:18:11 +020034
Ilias Apalodimas967650d2020-11-30 11:47:40 +020035static struct event_log_buffer event_log;
Ilias Apalodimas590fef62020-11-11 11:18:11 +020036/*
37 * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset.
38 * Since the current tpm2_get_capability() response buffers starts at
39 * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate
40 * the response size and offset once for all consumers
41 */
42#define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \
43 offsetof(struct tpms_capability_data, data))
44#define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \
45 offsetof(struct tpms_tagged_property, value))
46
Ilias Apalodimas967650d2020-11-30 11:47:40 +020047static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID;
48static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID;
49
50struct digest_info {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020051 u16 hash_alg;
52 u32 hash_mask;
Ilias Apalodimas967650d2020-11-30 11:47:40 +020053 u16 hash_len;
54};
55
Ilias Apalodimas190b0a22021-05-25 14:35:31 +030056static const struct digest_info hash_algo_list[] = {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020057 {
58 TPM2_ALG_SHA1,
59 EFI_TCG2_BOOT_HASH_ALG_SHA1,
Ilias Apalodimas967650d2020-11-30 11:47:40 +020060 TPM2_SHA1_DIGEST_SIZE,
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020061 },
62 {
63 TPM2_ALG_SHA256,
64 EFI_TCG2_BOOT_HASH_ALG_SHA256,
Ilias Apalodimas967650d2020-11-30 11:47:40 +020065 TPM2_SHA256_DIGEST_SIZE,
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020066 },
67 {
68 TPM2_ALG_SHA384,
69 EFI_TCG2_BOOT_HASH_ALG_SHA384,
Ilias Apalodimas967650d2020-11-30 11:47:40 +020070 TPM2_SHA384_DIGEST_SIZE,
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020071 },
72 {
73 TPM2_ALG_SHA512,
74 EFI_TCG2_BOOT_HASH_ALG_SHA512,
Ilias Apalodimas967650d2020-11-30 11:47:40 +020075 TPM2_SHA512_DIGEST_SIZE,
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020076 },
77};
78
79#define MAX_HASH_COUNT ARRAY_SIZE(hash_algo_list)
Ilias Apalodimas967650d2020-11-30 11:47:40 +020080
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020081/**
82 * alg_to_mask - Get a TCG hash mask for algorithms
83 *
84 * @hash_alg: TCG defined algorithm
85 *
86 * @Return: TCG hashing algorithm bitmaps, 0 if the algorithm is not supported
87 */
88static u32 alg_to_mask(u16 hash_alg)
89{
Ilias Apalodimas190b0a22021-05-25 14:35:31 +030090 size_t i;
Ilias Apalodimasc67fef62020-11-16 08:52:41 +020091
92 for (i = 0; i < MAX_HASH_COUNT; i++) {
93 if (hash_algo_list[i].hash_alg == hash_alg)
94 return hash_algo_list[i].hash_mask;
95 }
96
97 return 0;
98}
99
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200100/**
101 * alg_to_len - Get a TCG hash len for algorithms
102 *
103 * @hash_alg: TCG defined algorithm
104 *
105 * @Return: len of chosen algorithm, 0 if the algorithm is not supported
106 */
107static u16 alg_to_len(u16 hash_alg)
108{
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300109 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200110
111 for (i = 0; i < MAX_HASH_COUNT; i++) {
112 if (hash_algo_list[i].hash_alg == hash_alg)
113 return hash_algo_list[i].hash_len;
114 }
115
116 return 0;
117}
118
119static u32 tcg_event_final_size(struct tpml_digest_values *digest_list)
120{
121 u32 len;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300122 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200123
124 len = offsetof(struct tcg_pcr_event2, digests);
125 len += offsetof(struct tpml_digest_values, digests);
126 for (i = 0; i < digest_list->count; i++) {
127 u16 hash_alg = digest_list->digests[i].hash_alg;
128
129 len += offsetof(struct tpmt_ha, digest);
130 len += alg_to_len(hash_alg);
131 }
132 len += sizeof(u32); /* tcg_pcr_event2 event_size*/
133
134 return len;
135}
136
137/* tcg2_pcr_extend - Extend PCRs for a TPM2 device for a given tpml_digest_values
138 *
139 * @dev: device
140 * @digest_list: list of digest algorithms to extend
141 *
142 * @Return: status code
143 */
144static efi_status_t tcg2_pcr_extend(struct udevice *dev, u32 pcr_index,
145 struct tpml_digest_values *digest_list)
146{
147 u32 rc;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300148 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200149
150 for (i = 0; i < digest_list->count; i++) {
151 u32 alg = digest_list->digests[i].hash_alg;
152
153 rc = tpm2_pcr_extend(dev, pcr_index, alg,
154 (u8 *)&digest_list->digests[i].digest,
155 alg_to_len(alg));
156 if (rc) {
157 EFI_PRINT("Failed to extend PCR\n");
158 return EFI_DEVICE_ERROR;
159 }
160 }
161
162 return EFI_SUCCESS;
163}
164
165/* tcg2_agile_log_append - Append an agile event to out eventlog
166 *
167 * @pcr_index: PCR index
168 * @event_type: type of event added
169 * @digest_list: list of digest algorithms to add
170 * @size: size of event
171 * @event: event to add
172 *
173 * @Return: status code
174 */
175static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type,
176 struct tpml_digest_values *digest_list,
177 u32 size, u8 event[])
178{
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300179 void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200180 size_t pos;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300181 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200182 u32 event_size;
183
184 if (event_log.get_event_called)
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300185 log = (void *)((uintptr_t)event_log.final_buffer +
186 event_log.final_pos);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200187
188 /*
189 * size refers to the length of event[] only, we need to check against
190 * the final tcg_pcr_event2 size
191 */
192 event_size = size + tcg_event_final_size(digest_list);
193 if (event_log.pos + event_size > TPM2_EVENT_LOG_SIZE ||
194 event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE) {
195 event_log.truncated = true;
196 return EFI_VOLUME_FULL;
197 }
198
199 put_unaligned_le32(pcr_index, log);
200 pos = offsetof(struct tcg_pcr_event2, event_type);
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300201 put_unaligned_le32(event_type, (void *)((uintptr_t)log + pos));
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200202 pos = offsetof(struct tcg_pcr_event2, digests); /* count */
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300203 put_unaligned_le32(digest_list->count, (void *)((uintptr_t)log + pos));
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200204
205 pos += offsetof(struct tpml_digest_values, digests);
206 for (i = 0; i < digest_list->count; i++) {
207 u16 hash_alg = digest_list->digests[i].hash_alg;
208 u8 *digest = (u8 *)&digest_list->digests[i].digest;
209
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300210 put_unaligned_le16(hash_alg, (void *)((uintptr_t)log + pos));
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200211 pos += offsetof(struct tpmt_ha, digest);
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300212 memcpy((void *)((uintptr_t)log + pos), digest, alg_to_len(hash_alg));
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200213 pos += alg_to_len(hash_alg);
214 }
215
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300216 put_unaligned_le32(size, (void *)((uintptr_t)log + pos));
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200217 pos += sizeof(u32); /* tcg_pcr_event2 event_size*/
Ilias Apalodimas4390e762021-03-30 00:42:36 +0300218 memcpy((void *)((uintptr_t)log + pos), event, size);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200219 pos += size;
220
221 /* make sure the calculated buffer is what we checked against */
222 if (pos != event_size)
223 return EFI_INVALID_PARAMETER;
224
225 /* if GetEventLog hasn't been called update the normal log */
226 if (!event_log.get_event_called) {
227 event_log.pos += pos;
228 event_log.last_event_size = pos;
229 } else {
230 /* if GetEventLog has been called update config table log */
231 struct efi_tcg2_final_events_table *final_event;
232
233 final_event =
234 (struct efi_tcg2_final_events_table *)(event_log.final_buffer);
235 final_event->number_of_events++;
236 event_log.final_pos += pos;
237 }
238
239 return EFI_SUCCESS;
240}
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200241
242/**
243 * platform_get_tpm_device() - retrieve TPM device
244 *
245 * This function retrieves the udevice implementing a TPM
246 *
247 * This function may be overridden if special initialization is needed.
248 *
249 * @dev: udevice
250 * Return: status code
251 */
252__weak efi_status_t platform_get_tpm2_device(struct udevice **dev)
253{
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200254 for_each_tpm_device(*dev) {
255 /* Only support TPMv2 devices */
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200256 if (tpm_get_version(*dev) == TPM_V2)
257 return EFI_SUCCESS;
258 }
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200259
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200260 return EFI_NOT_FOUND;
261}
262
263/**
264 * tpm2_get_max_command_size() - get the supported max command size
265 *
266 * @dev: TPM device
267 * @max_command_size: output buffer for the size
268 *
269 * Return: 0 on success, -1 on error
270 */
271static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size)
272{
273 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
274 u32 ret;
275
276 memset(response, 0, sizeof(response));
277 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
278 TPM2_PT_MAX_COMMAND_SIZE, response, 1);
279 if (ret)
280 return -1;
281
282 *max_command_size = (uint16_t)get_unaligned_be32(response +
283 properties_offset);
284
285 return 0;
286}
287
288/**
289 * tpm2_get_max_response_size() - get the supported max response size
290 *
291 * @dev: TPM device
292 * @max_response_size: output buffer for the size
293 *
294 * Return: 0 on success, -1 on error
295 */
296static int tpm2_get_max_response_size(struct udevice *dev,
297 u16 *max_response_size)
298{
299 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
300 u32 ret;
301
302 memset(response, 0, sizeof(response));
303 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
304 TPM2_PT_MAX_RESPONSE_SIZE, response, 1);
305 if (ret)
306 return -1;
307
308 *max_response_size = (uint16_t)get_unaligned_be32(response +
309 properties_offset);
310
311 return 0;
312}
313
314/**
315 * tpm2_get_manufacturer_id() - get the manufacturer ID
316 *
317 * @dev: TPM device
318 * @manufacturer_id: output buffer for the id
319 *
320 * Return: 0 on success, -1 on error
321 */
322static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id)
323{
324 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
325 u32 ret;
326
327 memset(response, 0, sizeof(response));
328 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
329 TPM2_PT_MANUFACTURER, response, 1);
330 if (ret)
331 return -1;
332
333 *manufacturer_id = get_unaligned_be32(response + properties_offset);
334
335 return 0;
336}
337
338/**
339 * tpm2_get_num_pcr() - get the number of PCRs
340 *
341 * @dev: TPM device
342 * @manufacturer_id: output buffer for the number
343 *
344 * Return: 0 on success, -1 on error
345 */
346static int tpm2_get_num_pcr(struct udevice *dev, u32 *num_pcr)
347{
348 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
349 u32 ret;
350
351 memset(response, 0, sizeof(response));
352 ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES,
353 TPM2_PT_PCR_COUNT, response, 1);
354 if (ret)
355 return -1;
356
357 *num_pcr = get_unaligned_be32(response + properties_offset);
358 if (*num_pcr > TPM2_MAX_PCRS)
359 return -1;
360
361 return 0;
362}
363
364/**
365 * is_active_pcr() - Check if a supported algorithm is active
366 *
367 * @dev: TPM device
368 * @selection: struct of PCR information
369 *
370 * Return: true if PCR is active
371 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200372static bool is_active_pcr(struct tpms_pcr_selection *selection)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200373{
374 int i;
375 /*
376 * check the pcr_select. If at least one of the PCRs supports the
377 * algorithm add it on the active ones
378 */
379 for (i = 0; i < selection->size_of_select; i++) {
380 if (selection->pcr_select[i])
381 return true;
382 }
383
384 return false;
385}
386
387/**
388 * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks
389 *
390 * @dev: TPM device
391 * @supported_pcr: bitmask with the algorithms supported
392 * @active_pcr: bitmask with the active algorithms
393 * @pcr_banks: number of PCR banks
394 *
395 * Return: 0 on success, -1 on error
396 */
397static int tpm2_get_pcr_info(struct udevice *dev, u32 *supported_pcr,
398 u32 *active_pcr, u32 *pcr_banks)
399{
400 u8 response[TPM2_RESPONSE_BUFFER_SIZE];
401 struct tpml_pcr_selection pcrs;
402 u32 ret, num_pcr;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300403 size_t i;
404 int tpm_ret;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200405
Ilias Apalodimas09402b12021-05-26 21:01:00 +0300406 *supported_pcr = 0;
407 *active_pcr = 0;
408 *pcr_banks = 0;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200409 memset(response, 0, sizeof(response));
410 ret = tpm2_get_capability(dev, TPM2_CAP_PCRS, 0, response, 1);
411 if (ret)
412 goto out;
413
414 pcrs.count = get_unaligned_be32(response);
415 /*
416 * We only support 5 algorithms for now so check against that
417 * instead of TPM2_NUM_PCR_BANKS
418 */
419 if (pcrs.count > MAX_HASH_COUNT || pcrs.count < 1)
420 goto out;
421
422 tpm_ret = tpm2_get_num_pcr(dev, &num_pcr);
423 if (tpm_ret)
424 goto out;
425
426 for (i = 0; i < pcrs.count; i++) {
427 /*
428 * Definition of TPMS_PCR_SELECTION Structure
429 * hash: u16
430 * size_of_select: u8
431 * pcr_select: u8 array
432 *
433 * The offsets depend on the number of the device PCRs
434 * so we have to calculate them based on that
435 */
436 u32 hash_offset = offsetof(struct tpml_pcr_selection, selection) +
437 i * offsetof(struct tpms_pcr_selection, pcr_select) +
438 i * ((num_pcr + 7) / 8);
439 u32 size_select_offset =
440 hash_offset + offsetof(struct tpms_pcr_selection,
441 size_of_select);
442 u32 pcr_select_offset =
443 hash_offset + offsetof(struct tpms_pcr_selection,
444 pcr_select);
445
446 pcrs.selection[i].hash =
447 get_unaligned_be16(response + hash_offset);
448 pcrs.selection[i].size_of_select =
449 __get_unaligned_be(response + size_select_offset);
450 if (pcrs.selection[i].size_of_select > TPM2_PCR_SELECT_MAX)
451 goto out;
452 /* copy the array of pcr_select */
453 memcpy(pcrs.selection[i].pcr_select, response + pcr_select_offset,
454 pcrs.selection[i].size_of_select);
455 }
456
457 for (i = 0; i < pcrs.count; i++) {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200458 u32 hash_mask = alg_to_mask(pcrs.selection[i].hash);
459
460 if (hash_mask) {
461 *supported_pcr |= hash_mask;
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200462 if (is_active_pcr(&pcrs.selection[i]))
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200463 *active_pcr |= hash_mask;
464 } else {
465 EFI_PRINT("Unknown algorithm %x\n", pcrs.selection[i].hash);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200466 }
467 }
468
469 *pcr_banks = pcrs.count;
470
471 return 0;
472out:
473 return -1;
474}
475
476/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200477 * __get_active_pcr_banks() - returns the currently active PCR banks
478 *
479 * @active_pcr_banks: pointer for receiving the bitmap of currently
480 * active PCR banks
481 *
482 * Return: status code
483 */
484static efi_status_t __get_active_pcr_banks(u32 *active_pcr_banks)
485{
486 struct udevice *dev;
Ilias Apalodimas09402b12021-05-26 21:01:00 +0300487 u32 active = 0, supported = 0, pcr_banks = 0;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200488 efi_status_t ret;
489 int err;
490
491 ret = platform_get_tpm2_device(&dev);
492 if (ret != EFI_SUCCESS)
493 goto out;
494
495 err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_banks);
496 if (err) {
497 ret = EFI_DEVICE_ERROR;
498 goto out;
499 }
500
501 *active_pcr_banks = active;
502
503out:
504 return ret;
505}
506
507/* tcg2_create_digest - create a list of digests of the supported PCR banks
508 * for a given memory range
509 *
510 * @input: input memory
511 * @length: length of buffer to calculate the digest
512 * @digest_list: list of digests to fill in
513 *
514 * Return: status code
515 */
516static efi_status_t tcg2_create_digest(const u8 *input, u32 length,
517 struct tpml_digest_values *digest_list)
518{
519 sha1_context ctx;
520 sha256_context ctx_256;
521 sha512_context ctx_512;
Masahisa Kojimaeb74a902021-04-14 11:55:49 +0900522 u8 final[TPM2_SHA512_DIGEST_SIZE];
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200523 efi_status_t ret;
524 u32 active;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300525 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200526
527 ret = __get_active_pcr_banks(&active);
528 if (ret != EFI_SUCCESS)
529 return ret;
530
531 digest_list->count = 0;
532 for (i = 0; i < MAX_HASH_COUNT; i++) {
533 u16 hash_alg = hash_algo_list[i].hash_alg;
534
535 if (!(active & alg_to_mask(hash_alg)))
536 continue;
537 switch (hash_alg) {
538 case TPM2_ALG_SHA1:
539 sha1_starts(&ctx);
540 sha1_update(&ctx, input, length);
541 sha1_finish(&ctx, final);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200542 break;
543 case TPM2_ALG_SHA256:
544 sha256_starts(&ctx_256);
545 sha256_update(&ctx_256, input, length);
546 sha256_finish(&ctx_256, final);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200547 break;
548 case TPM2_ALG_SHA384:
549 sha384_starts(&ctx_512);
550 sha384_update(&ctx_512, input, length);
551 sha384_finish(&ctx_512, final);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200552 break;
553 case TPM2_ALG_SHA512:
554 sha512_starts(&ctx_512);
555 sha512_update(&ctx_512, input, length);
556 sha512_finish(&ctx_512, final);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200557 break;
558 default:
559 EFI_PRINT("Unsupported algorithm %x\n", hash_alg);
560 return EFI_INVALID_PARAMETER;
561 }
Ilias Apalodimas754b3a42021-04-22 14:32:14 +0300562 digest_list->count++;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200563 digest_list->digests[i].hash_alg = hash_alg;
564 memcpy(&digest_list->digests[i].digest, final, (u32)alg_to_len(hash_alg));
565 }
566
567 return EFI_SUCCESS;
568}
569
570/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200571 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200572 *
573 * @this: TCG2 protocol instance
574 * @capability: caller allocated memory with size field to the size of
575 * the structure allocated
576
577 * Return: status code
578 */
579static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200580efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
581 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200582{
583 struct udevice *dev;
584 efi_status_t efi_ret;
585 int ret;
586
587 EFI_ENTRY("%p, %p", this, capability);
588
589 if (!this || !capability) {
590 efi_ret = EFI_INVALID_PARAMETER;
591 goto out;
592 }
593
594 if (capability->size < boot_service_capability_min) {
595 capability->size = boot_service_capability_min;
596 efi_ret = EFI_BUFFER_TOO_SMALL;
597 goto out;
598 }
599
600 if (capability->size < sizeof(*capability)) {
601 capability->size = sizeof(*capability);
602 efi_ret = EFI_BUFFER_TOO_SMALL;
603 goto out;
604 }
605
606 capability->structure_version.major = 1;
607 capability->structure_version.minor = 1;
608 capability->protocol_version.major = 1;
609 capability->protocol_version.minor = 1;
610
611 efi_ret = platform_get_tpm2_device(&dev);
612 if (efi_ret != EFI_SUCCESS) {
613 capability->supported_event_logs = 0;
614 capability->hash_algorithm_bitmap = 0;
615 capability->tpm_present_flag = false;
616 capability->max_command_size = 0;
617 capability->max_response_size = 0;
618 capability->manufacturer_id = 0;
619 capability->number_of_pcr_banks = 0;
620 capability->active_pcr_banks = 0;
621
622 efi_ret = EFI_SUCCESS;
623 goto out;
624 }
625
626 /* We only allow a TPMv2 device to register the EFI protocol */
627 capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
628
629 capability->tpm_present_flag = true;
630
631 /* Supported and active PCRs */
632 capability->hash_algorithm_bitmap = 0;
633 capability->active_pcr_banks = 0;
634 ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
635 &capability->active_pcr_banks,
636 &capability->number_of_pcr_banks);
637 if (ret) {
638 efi_ret = EFI_DEVICE_ERROR;
639 goto out;
640 }
641
642 /* Max command size */
643 ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
644 if (ret) {
645 efi_ret = EFI_DEVICE_ERROR;
646 goto out;
647 }
648
649 /* Max response size */
650 ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
651 if (ret) {
652 efi_ret = EFI_DEVICE_ERROR;
653 goto out;
654 }
655
656 /* Manufacturer ID */
657 ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
658 if (ret) {
659 efi_ret = EFI_DEVICE_ERROR;
660 goto out;
661 }
662
663 return EFI_EXIT(EFI_SUCCESS);
664out:
665 return EFI_EXIT(efi_ret);
666}
667
668/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200669 * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
670 * last entry
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200671 *
672 * @this: TCG2 protocol instance
673 * @log_format: type of event log format
674 * @event_log_location: pointer to the memory address of the event log
675 * @event_log_last_entry: pointer to the address of the start of the last
676 * entry in the event log in memory, if log contains
677 * more than 1 entry
678 * @event_log_truncated: set to true, if the Event Log is missing at i
679 * least one entry
680 *
681 * Return: status code
682 */
683static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200684efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
685 efi_tcg_event_log_format log_format,
686 u64 *event_log_location, u64 *event_log_last_entry,
687 bool *event_log_truncated)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200688{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200689 efi_status_t ret = EFI_SUCCESS;
690 struct udevice *dev;
691
692 EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
693 event_log_last_entry, event_log_truncated);
694
695 ret = platform_get_tpm2_device(&dev);
696 if (ret != EFI_SUCCESS) {
697 event_log_location = NULL;
698 event_log_last_entry = NULL;
699 *event_log_truncated = false;
700 ret = EFI_SUCCESS;
701 goto out;
702 }
703 *event_log_location = (uintptr_t)event_log.buffer;
704 *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
705 event_log.last_event_size);
706 *event_log_truncated = event_log.truncated;
707 event_log.get_event_called = true;
708
709out:
710 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200711}
712
713/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200714 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200715 *
716 * @this: TCG2 protocol instance
717 * @flags: bitmap providing additional information on the
718 * operation
719 * @data_to_hash: physical address of the start of the data buffer
720 * to be hashed
721 * @data_to_hash_len: the length in bytes of the buffer referenced by
722 * data_to_hash
723 * @efi_tcg_event: pointer to data buffer containing information
724 * about the event
725 *
726 * Return: status code
727 */
728static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200729efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
730 u64 data_to_hash, u64 data_to_hash_len,
731 struct efi_tcg2_event *efi_tcg_event)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200732{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200733 struct udevice *dev;
734 efi_status_t ret;
735 u32 event_type, pcr_index, event_size;
736 struct tpml_digest_values digest_list;
737
738 EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
739 data_to_hash_len, efi_tcg_event);
740
741 if (!this || !data_to_hash || !efi_tcg_event) {
742 ret = EFI_INVALID_PARAMETER;
743 goto out;
744 }
745
746 ret = platform_get_tpm2_device(&dev);
747 if (ret != EFI_SUCCESS)
748 goto out;
749
750 if (efi_tcg_event->size < efi_tcg_event->header.header_size +
751 sizeof(u32)) {
752 ret = EFI_INVALID_PARAMETER;
753 goto out;
754 }
755
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300756 if (efi_tcg_event->header.pcr_index > TPM2_MAX_PCRS) {
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200757 ret = EFI_INVALID_PARAMETER;
758 goto out;
759 }
760
761 /*
762 * if PE_COFF_IMAGE is set we need to make sure the image is not
763 * corrupted, verify it and hash the PE/COFF image in accordance with
764 * the procedure specified in "Calculating the PE Image Hash"
765 * section of the "Windows Authenticode Portable Executable Signature
766 * Format"
767 * Not supported for now
768 */
769 if (flags & PE_COFF_IMAGE) {
770 ret = EFI_UNSUPPORTED;
771 goto out;
772 }
773
774 pcr_index = efi_tcg_event->header.pcr_index;
775 event_type = efi_tcg_event->header.event_type;
776
Heinrich Schuchardtba1fc382021-05-12 17:37:20 +0200777 ret = tcg2_create_digest((u8 *)(uintptr_t)data_to_hash,
778 data_to_hash_len, &digest_list);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200779 if (ret != EFI_SUCCESS)
780 goto out;
781
782 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
783 if (ret != EFI_SUCCESS)
784 goto out;
785
786 if (flags & EFI_TCG2_EXTEND_ONLY) {
787 if (event_log.truncated)
788 ret = EFI_VOLUME_FULL;
789 goto out;
790 }
791
792 /*
793 * The efi_tcg_event size includes the size component and the
794 * headersize
795 */
796 event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
797 efi_tcg_event->header.header_size;
798 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
799 event_size, efi_tcg_event->event);
800out:
801 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200802}
803
804/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200805 * efi_tcg2_submit_command() - Send command to the TPM
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200806 *
807 * @this: TCG2 protocol instance
808 * @input_param_block_size: size of the TPM input parameter block
809 * @input_param_block: pointer to the TPM input parameter block
810 * @output_param_block_size: size of the TPM output parameter block
811 * @output_param_block: pointer to the TPM output parameter block
812 *
813 * Return: status code
814 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200815static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300816efi_tcg2_submit_command(__maybe_unused struct efi_tcg2_protocol *this,
817 u32 __maybe_unused input_param_block_size,
818 u8 __maybe_unused *input_param_block,
819 u32 __maybe_unused output_param_block_size,
820 u8 __maybe_unused *output_param_block)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200821{
822 return EFI_UNSUPPORTED;
823}
824
825/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200826 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200827 *
828 * @this: TCG2 protocol instance
829 * @active_pcr_banks: pointer for receiving the bitmap of currently
830 * active PCR banks
831 *
832 * Return: status code
833 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200834static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200835efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
836 u32 *active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200837{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200838 efi_status_t ret;
839
840 EFI_ENTRY("%p, %p", this, active_pcr_banks);
841 ret = __get_active_pcr_banks(active_pcr_banks);
842
843 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200844}
845
846/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200847 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200848 *
849 * @this: TCG2 protocol instance
850 * @active_pcr_banks: bitmap of the requested active PCR banks
851 *
852 * Return: status code
853 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200854static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300855efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
856 u32 __maybe_unused active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200857{
858 return EFI_UNSUPPORTED;
859}
860
861/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200862 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
863 * set_active_pcr_banks()
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200864 *
865 * @this: TCG2 protocol instance
866 * @operation_present: non-zero value to indicate a
867 * set_active_pcr_banks operation was
868 * invoked during last boot
869 * @response: result value could be returned
870 *
871 * Return: status code
872 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200873static efi_status_t EFIAPI
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300874efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this,
875 u32 __maybe_unused *operation_present,
876 u32 __maybe_unused *response)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200877{
878 return EFI_UNSUPPORTED;
879}
880
881static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200882 .get_capability = efi_tcg2_get_capability,
883 .get_eventlog = efi_tcg2_get_eventlog,
884 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
885 .submit_command = efi_tcg2_submit_command,
886 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
887 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
888 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200889};
890
891/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200892 * create_specid_event() - Create the first event in the eventlog
893 *
894 * @dev: tpm device
895 * @event_header: Pointer to the final event header
896 * @event_size: final spec event size
897 *
898 * Return: status code
899 */
900static efi_status_t create_specid_event(struct udevice *dev, void *buffer,
901 size_t *event_size)
902{
903 struct tcg_efi_spec_id_event *spec_event;
904 size_t spec_event_size;
905 efi_status_t ret = EFI_DEVICE_ERROR;
Ilias Apalodimas09402b12021-05-26 21:01:00 +0300906 u32 active = 0, supported = 0;
Ilias Apalodimas190b0a22021-05-25 14:35:31 +0300907 int err;
908 size_t i;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200909
910 /*
911 * Create Spec event. This needs to be the first event in the log
912 * according to the TCG EFI protocol spec
913 */
914
915 /* Setup specID event data */
916 spec_event = (struct tcg_efi_spec_id_event *)buffer;
917 memcpy(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
918 sizeof(spec_event->signature));
919 put_unaligned_le32(0, &spec_event->platform_class); /* type client */
920 spec_event->spec_version_minor =
921 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2;
922 spec_event->spec_version_major =
923 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2;
924 spec_event->spec_errata =
925 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2;
926 spec_event->uintn_size = sizeof(efi_uintn_t) / sizeof(u32);
927
928 err = tpm2_get_pcr_info(dev, &supported, &active,
929 &spec_event->number_of_algorithms);
930 if (err)
931 goto out;
932 if (spec_event->number_of_algorithms > MAX_HASH_COUNT ||
933 spec_event->number_of_algorithms < 1)
934 goto out;
935
936 for (i = 0; i < spec_event->number_of_algorithms; i++) {
937 u16 hash_alg = hash_algo_list[i].hash_alg;
938 u16 hash_len = hash_algo_list[i].hash_len;
939
940 if (active && alg_to_mask(hash_alg)) {
941 put_unaligned_le16(hash_alg,
942 &spec_event->digest_sizes[i].algorithm_id);
943 put_unaligned_le16(hash_len,
944 &spec_event->digest_sizes[i].digest_size);
945 }
946 }
947 /*
948 * the size of the spec event and placement of vendor_info_size
949 * depends on supported algoriths
950 */
951 spec_event_size =
952 offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
953 spec_event->number_of_algorithms * sizeof(spec_event->digest_sizes[0]);
954 /* no vendor info for us */
955 memset(buffer + spec_event_size, 0,
956 sizeof(spec_event->vendor_info_size));
957 spec_event_size += sizeof(spec_event->vendor_info_size);
958 *event_size = spec_event_size;
959
960 return EFI_SUCCESS;
961
962out:
963 return ret;
964}
965
966/**
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200967 * tcg2_uninit - remove the final event table and free efi memory on failures
968 */
969void tcg2_uninit(void)
970{
971 efi_status_t ret;
972
973 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
974 if (ret != EFI_SUCCESS)
975 log_err("Failed to delete final events config table\n");
976
977 efi_free_pool(event_log.buffer);
978 event_log.buffer = NULL;
979 efi_free_pool(event_log.final_buffer);
980 event_log.final_buffer = NULL;
981}
982
983/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200984 * create_final_event() - Create the final event and install the config
985 * defined by the TCG EFI spec
986 */
987static efi_status_t create_final_event(void)
988{
989 struct efi_tcg2_final_events_table *final_event;
990 efi_status_t ret;
991
992 /*
993 * All events generated after the invocation of
994 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
995 * EFI_CONFIGURATION_TABLE
996 */
997 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
998 &event_log.final_buffer);
999 if (ret != EFI_SUCCESS)
1000 goto out;
1001
1002 memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
1003 final_event = event_log.final_buffer;
1004 final_event->number_of_events = 0;
1005 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
1006 event_log.final_pos = sizeof(*final_event);
1007 ret = efi_install_configuration_table(&efi_guid_final_events,
1008 final_event);
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001009 if (ret != EFI_SUCCESS) {
1010 efi_free_pool(event_log.final_buffer);
1011 event_log.final_buffer = NULL;
1012 }
1013
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001014out:
1015 return ret;
1016}
1017
1018/**
1019 * efi_init_event_log() - initialize an eventlog
1020 */
1021static efi_status_t efi_init_event_log(void)
1022{
1023 /*
1024 * vendor_info_size is currently set to 0, we need to change the length
1025 * and allocate the flexible array member if this changes
1026 */
1027 struct tcg_pcr_event *event_header = NULL;
1028 struct udevice *dev;
1029 size_t spec_event_size;
1030 efi_status_t ret;
1031
1032 ret = platform_get_tpm2_device(&dev);
1033 if (ret != EFI_SUCCESS)
1034 goto out;
1035
1036 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
1037 (void **)&event_log.buffer);
1038 if (ret != EFI_SUCCESS)
1039 goto out;
1040
1041 /*
1042 * initialize log area as 0xff so the OS can easily figure out the
1043 * last log entry
1044 */
1045 memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
1046 event_log.pos = 0;
1047 event_log.last_event_size = 0;
1048 event_log.get_event_called = false;
1049 event_log.truncated = false;
1050
1051 /*
1052 * The log header is defined to be in SHA1 event log entry format.
1053 * Setup event header
1054 */
1055 event_header = (struct tcg_pcr_event *)event_log.buffer;
1056 put_unaligned_le32(0, &event_header->pcr_index);
1057 put_unaligned_le32(EV_NO_ACTION, &event_header->event_type);
1058 memset(&event_header->digest, 0, sizeof(event_header->digest));
Ilias Apalodimas4390e762021-03-30 00:42:36 +03001059 ret = create_specid_event(dev, (void *)((uintptr_t)event_log.buffer + sizeof(*event_header)),
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001060 &spec_event_size);
1061 if (ret != EFI_SUCCESS)
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001062 goto free_pool;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001063 put_unaligned_le32(spec_event_size, &event_header->event_size);
1064 event_log.pos = spec_event_size + sizeof(*event_header);
1065 event_log.last_event_size = event_log.pos;
1066
1067 ret = create_final_event();
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001068 if (ret != EFI_SUCCESS)
1069 goto free_pool;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001070
1071out:
1072 return ret;
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001073
1074free_pool:
1075 efi_free_pool(event_log.buffer);
1076 event_log.buffer = NULL;
1077 return ret;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001078}
1079
1080/**
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +02001081 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
1082 * eventlog and extend the PCRs
1083 *
1084 * @dev: TPM device
1085 *
1086 * @Return: status code
1087 */
1088static efi_status_t efi_append_scrtm_version(struct udevice *dev)
1089{
1090 struct tpml_digest_values digest_list;
1091 u8 ver[] = U_BOOT_VERSION_STRING;
1092 const int pcr_index = 0;
1093 efi_status_t ret;
1094
1095 ret = tcg2_create_digest(ver, sizeof(ver), &digest_list);
1096 if (ret != EFI_SUCCESS)
1097 goto out;
1098
1099 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
1100 if (ret != EFI_SUCCESS)
1101 goto out;
1102
1103 ret = tcg2_agile_log_append(pcr_index, EV_S_CRTM_VERSION, &digest_list,
1104 sizeof(ver), ver);
1105
1106out:
1107 return ret;
1108}
1109
1110/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001111 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1112 *
1113 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1114 *
1115 * Return: An error status is only returned if adding the protocol fails.
1116 */
1117efi_status_t efi_tcg2_register(void)
1118{
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001119 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001120 struct udevice *dev;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001121
1122 ret = platform_get_tpm2_device(&dev);
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001123 if (ret != EFI_SUCCESS) {
1124 log_warning("Unable to find TPMv2 device\n");
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001125 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001126 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001127
1128 ret = efi_init_event_log();
1129 if (ret != EFI_SUCCESS)
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001130 goto fail;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001131
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +02001132 ret = efi_append_scrtm_version(dev);
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001133 if (ret != EFI_SUCCESS) {
1134 tcg2_uninit();
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001135 goto fail;
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001136 }
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +02001137
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001138 ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol,
1139 (void *)&efi_tcg2_protocol);
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001140 if (ret != EFI_SUCCESS) {
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001141 tcg2_uninit();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001142 goto fail;
1143 }
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001144 return ret;
Ilias Apalodimasfa5217d2021-05-10 21:19:14 +03001145
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001146fail:
Ilias Apalodimas5a2baf92021-05-12 00:03:41 +03001147 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
1148 /*
1149 * Return EFI_SUCCESS and don't stop the EFI subsystem.
1150 * That's done for 2 reasons
1151 * - If the protocol is not installed the PCRs won't be extended. So
1152 * someone later in the boot flow will notice that and take the
1153 * necessary actions.
1154 * - The TPM sandbox is limited and we won't be able to run any efi
1155 * related tests with TCG2 enabled
1156 */
1157 return EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001158}