blob: ed86a220fbd60984184892d5b7f4ad4cfc9bddb6 [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>
14#include <efi_tcg2.h>
15#include <log.h>
Ilias Apalodimasf69a2012021-03-24 16:50:46 +020016#include <version.h>
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020017#include <tpm-v2.h>
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020018#include <u-boot/sha1.h>
19#include <u-boot/sha256.h>
20#include <u-boot/sha512.h>
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020021#include <linux/unaligned/access_ok.h>
22#include <linux/unaligned/generic.h>
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020023#include <hexdump.h>
Ilias Apalodimasc1c02102020-11-11 11:18:11 +020024
Ilias Apalodimasc8d0fd52020-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 Apalodimasc1c02102020-11-11 11:18:11 +020034
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020035static struct event_log_buffer event_log;
Ilias Apalodimasc1c02102020-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 Apalodimasc8d0fd52020-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 Apalodimas9aeb3802020-11-16 08:52:41 +020051 u16 hash_alg;
52 u32 hash_mask;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020053 u16 hash_len;
54};
55
56const static struct digest_info hash_algo_list[] = {
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +020057 {
58 TPM2_ALG_SHA1,
59 EFI_TCG2_BOOT_HASH_ALG_SHA1,
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020060 TPM2_SHA1_DIGEST_SIZE,
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +020061 },
62 {
63 TPM2_ALG_SHA256,
64 EFI_TCG2_BOOT_HASH_ALG_SHA256,
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020065 TPM2_SHA256_DIGEST_SIZE,
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +020066 },
67 {
68 TPM2_ALG_SHA384,
69 EFI_TCG2_BOOT_HASH_ALG_SHA384,
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020070 TPM2_SHA384_DIGEST_SIZE,
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +020071 },
72 {
73 TPM2_ALG_SHA512,
74 EFI_TCG2_BOOT_HASH_ALG_SHA512,
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020075 TPM2_SHA512_DIGEST_SIZE,
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +020076 },
77};
78
79#define MAX_HASH_COUNT ARRAY_SIZE(hash_algo_list)
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +020080
Ilias Apalodimas9aeb3802020-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{
90 int i;
91
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 Apalodimasc8d0fd52020-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{
109 int i;
110
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;
122 int i;
123
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;
148 int i;
149
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 Apalodimasf8cd72d2021-03-30 00:42:36 +0300179 void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200180 size_t pos;
181 int i;
182 u32 event_size;
183
184 if (event_log.get_event_called)
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +0300185 log = (void *)((uintptr_t)event_log.final_buffer +
186 event_log.final_pos);
Ilias Apalodimasc8d0fd52020-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 Apalodimasf8cd72d2021-03-30 00:42:36 +0300201 put_unaligned_le32(event_type, (void *)((uintptr_t)log + pos));
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200202 pos = offsetof(struct tcg_pcr_event2, digests); /* count */
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +0300203 put_unaligned_le32(digest_list->count, (void *)((uintptr_t)log + pos));
Ilias Apalodimasc8d0fd52020-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 Apalodimasf8cd72d2021-03-30 00:42:36 +0300210 put_unaligned_le16(hash_alg, (void *)((uintptr_t)log + pos));
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200211 pos += offsetof(struct tpmt_ha, digest);
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +0300212 memcpy((void *)((uintptr_t)log + pos), digest, alg_to_len(hash_alg));
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200213 pos += alg_to_len(hash_alg);
214 }
215
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +0300216 put_unaligned_le32(size, (void *)((uintptr_t)log + pos));
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200217 pos += sizeof(u32); /* tcg_pcr_event2 event_size*/
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +0300218 memcpy((void *)((uintptr_t)log + pos), event, size);
Ilias Apalodimasc8d0fd52020-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 Apalodimasc1c02102020-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 Apalodimas9aeb3802020-11-16 08:52:41 +0200254 for_each_tpm_device(*dev) {
255 /* Only support TPMv2 devices */
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200256 if (tpm_get_version(*dev) == TPM_V2)
257 return EFI_SUCCESS;
258 }
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200259
Ilias Apalodimasc1c02102020-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 Apalodimasc8d0fd52020-11-30 11:47:40 +0200372static bool is_active_pcr(struct tpms_pcr_selection *selection)
Ilias Apalodimasc1c02102020-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;
403 int i, tpm_ret;
404
405 memset(response, 0, sizeof(response));
406 ret = tpm2_get_capability(dev, TPM2_CAP_PCRS, 0, response, 1);
407 if (ret)
408 goto out;
409
410 pcrs.count = get_unaligned_be32(response);
411 /*
412 * We only support 5 algorithms for now so check against that
413 * instead of TPM2_NUM_PCR_BANKS
414 */
415 if (pcrs.count > MAX_HASH_COUNT || pcrs.count < 1)
416 goto out;
417
418 tpm_ret = tpm2_get_num_pcr(dev, &num_pcr);
419 if (tpm_ret)
420 goto out;
421
422 for (i = 0; i < pcrs.count; i++) {
423 /*
424 * Definition of TPMS_PCR_SELECTION Structure
425 * hash: u16
426 * size_of_select: u8
427 * pcr_select: u8 array
428 *
429 * The offsets depend on the number of the device PCRs
430 * so we have to calculate them based on that
431 */
432 u32 hash_offset = offsetof(struct tpml_pcr_selection, selection) +
433 i * offsetof(struct tpms_pcr_selection, pcr_select) +
434 i * ((num_pcr + 7) / 8);
435 u32 size_select_offset =
436 hash_offset + offsetof(struct tpms_pcr_selection,
437 size_of_select);
438 u32 pcr_select_offset =
439 hash_offset + offsetof(struct tpms_pcr_selection,
440 pcr_select);
441
442 pcrs.selection[i].hash =
443 get_unaligned_be16(response + hash_offset);
444 pcrs.selection[i].size_of_select =
445 __get_unaligned_be(response + size_select_offset);
446 if (pcrs.selection[i].size_of_select > TPM2_PCR_SELECT_MAX)
447 goto out;
448 /* copy the array of pcr_select */
449 memcpy(pcrs.selection[i].pcr_select, response + pcr_select_offset,
450 pcrs.selection[i].size_of_select);
451 }
452
453 for (i = 0; i < pcrs.count; i++) {
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200454 u32 hash_mask = alg_to_mask(pcrs.selection[i].hash);
455
456 if (hash_mask) {
457 *supported_pcr |= hash_mask;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200458 if (is_active_pcr(&pcrs.selection[i]))
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200459 *active_pcr |= hash_mask;
460 } else {
461 EFI_PRINT("Unknown algorithm %x\n", pcrs.selection[i].hash);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200462 }
463 }
464
465 *pcr_banks = pcrs.count;
466
467 return 0;
468out:
469 return -1;
470}
471
472/**
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200473 * __get_active_pcr_banks() - returns the currently active PCR banks
474 *
475 * @active_pcr_banks: pointer for receiving the bitmap of currently
476 * active PCR banks
477 *
478 * Return: status code
479 */
480static efi_status_t __get_active_pcr_banks(u32 *active_pcr_banks)
481{
482 struct udevice *dev;
483 u32 active, supported, pcr_banks;
484 efi_status_t ret;
485 int err;
486
487 ret = platform_get_tpm2_device(&dev);
488 if (ret != EFI_SUCCESS)
489 goto out;
490
491 err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_banks);
492 if (err) {
493 ret = EFI_DEVICE_ERROR;
494 goto out;
495 }
496
497 *active_pcr_banks = active;
498
499out:
500 return ret;
501}
502
503/* tcg2_create_digest - create a list of digests of the supported PCR banks
504 * for a given memory range
505 *
506 * @input: input memory
507 * @length: length of buffer to calculate the digest
508 * @digest_list: list of digests to fill in
509 *
510 * Return: status code
511 */
512static efi_status_t tcg2_create_digest(const u8 *input, u32 length,
513 struct tpml_digest_values *digest_list)
514{
515 sha1_context ctx;
516 sha256_context ctx_256;
517 sha512_context ctx_512;
518 u8 final[TPM2_ALG_SHA512];
519 efi_status_t ret;
520 u32 active;
521 int i;
522
523 ret = __get_active_pcr_banks(&active);
524 if (ret != EFI_SUCCESS)
525 return ret;
526
527 digest_list->count = 0;
528 for (i = 0; i < MAX_HASH_COUNT; i++) {
529 u16 hash_alg = hash_algo_list[i].hash_alg;
530
531 if (!(active & alg_to_mask(hash_alg)))
532 continue;
533 switch (hash_alg) {
534 case TPM2_ALG_SHA1:
535 sha1_starts(&ctx);
536 sha1_update(&ctx, input, length);
537 sha1_finish(&ctx, final);
538 digest_list->count++;
539 break;
540 case TPM2_ALG_SHA256:
541 sha256_starts(&ctx_256);
542 sha256_update(&ctx_256, input, length);
543 sha256_finish(&ctx_256, final);
544 digest_list->count++;
545 break;
546 case TPM2_ALG_SHA384:
547 sha384_starts(&ctx_512);
548 sha384_update(&ctx_512, input, length);
549 sha384_finish(&ctx_512, final);
550 digest_list->count++;
551 break;
552 case TPM2_ALG_SHA512:
553 sha512_starts(&ctx_512);
554 sha512_update(&ctx_512, input, length);
555 sha512_finish(&ctx_512, final);
556 digest_list->count++;
557 break;
558 default:
559 EFI_PRINT("Unsupported algorithm %x\n", hash_alg);
560 return EFI_INVALID_PARAMETER;
561 }
562 digest_list->digests[i].hash_alg = hash_alg;
563 memcpy(&digest_list->digests[i].digest, final, (u32)alg_to_len(hash_alg));
564 }
565
566 return EFI_SUCCESS;
567}
568
569/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200570 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200571 *
572 * @this: TCG2 protocol instance
573 * @capability: caller allocated memory with size field to the size of
574 * the structure allocated
575
576 * Return: status code
577 */
578static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200579efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
580 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200581{
582 struct udevice *dev;
583 efi_status_t efi_ret;
584 int ret;
585
586 EFI_ENTRY("%p, %p", this, capability);
587
588 if (!this || !capability) {
589 efi_ret = EFI_INVALID_PARAMETER;
590 goto out;
591 }
592
593 if (capability->size < boot_service_capability_min) {
594 capability->size = boot_service_capability_min;
595 efi_ret = EFI_BUFFER_TOO_SMALL;
596 goto out;
597 }
598
599 if (capability->size < sizeof(*capability)) {
600 capability->size = sizeof(*capability);
601 efi_ret = EFI_BUFFER_TOO_SMALL;
602 goto out;
603 }
604
605 capability->structure_version.major = 1;
606 capability->structure_version.minor = 1;
607 capability->protocol_version.major = 1;
608 capability->protocol_version.minor = 1;
609
610 efi_ret = platform_get_tpm2_device(&dev);
611 if (efi_ret != EFI_SUCCESS) {
612 capability->supported_event_logs = 0;
613 capability->hash_algorithm_bitmap = 0;
614 capability->tpm_present_flag = false;
615 capability->max_command_size = 0;
616 capability->max_response_size = 0;
617 capability->manufacturer_id = 0;
618 capability->number_of_pcr_banks = 0;
619 capability->active_pcr_banks = 0;
620
621 efi_ret = EFI_SUCCESS;
622 goto out;
623 }
624
625 /* We only allow a TPMv2 device to register the EFI protocol */
626 capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
627
628 capability->tpm_present_flag = true;
629
630 /* Supported and active PCRs */
631 capability->hash_algorithm_bitmap = 0;
632 capability->active_pcr_banks = 0;
633 ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
634 &capability->active_pcr_banks,
635 &capability->number_of_pcr_banks);
636 if (ret) {
637 efi_ret = EFI_DEVICE_ERROR;
638 goto out;
639 }
640
641 /* Max command size */
642 ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
643 if (ret) {
644 efi_ret = EFI_DEVICE_ERROR;
645 goto out;
646 }
647
648 /* Max response size */
649 ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
650 if (ret) {
651 efi_ret = EFI_DEVICE_ERROR;
652 goto out;
653 }
654
655 /* Manufacturer ID */
656 ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
657 if (ret) {
658 efi_ret = EFI_DEVICE_ERROR;
659 goto out;
660 }
661
662 return EFI_EXIT(EFI_SUCCESS);
663out:
664 return EFI_EXIT(efi_ret);
665}
666
667/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200668 * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
669 * last entry
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200670 *
671 * @this: TCG2 protocol instance
672 * @log_format: type of event log format
673 * @event_log_location: pointer to the memory address of the event log
674 * @event_log_last_entry: pointer to the address of the start of the last
675 * entry in the event log in memory, if log contains
676 * more than 1 entry
677 * @event_log_truncated: set to true, if the Event Log is missing at i
678 * least one entry
679 *
680 * Return: status code
681 */
682static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200683efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
684 efi_tcg_event_log_format log_format,
685 u64 *event_log_location, u64 *event_log_last_entry,
686 bool *event_log_truncated)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200687{
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200688 efi_status_t ret = EFI_SUCCESS;
689 struct udevice *dev;
690
691 EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
692 event_log_last_entry, event_log_truncated);
693
694 ret = platform_get_tpm2_device(&dev);
695 if (ret != EFI_SUCCESS) {
696 event_log_location = NULL;
697 event_log_last_entry = NULL;
698 *event_log_truncated = false;
699 ret = EFI_SUCCESS;
700 goto out;
701 }
702 *event_log_location = (uintptr_t)event_log.buffer;
703 *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
704 event_log.last_event_size);
705 *event_log_truncated = event_log.truncated;
706 event_log.get_event_called = true;
707
708out:
709 return EFI_EXIT(ret);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200710}
711
712/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200713 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200714 *
715 * @this: TCG2 protocol instance
716 * @flags: bitmap providing additional information on the
717 * operation
718 * @data_to_hash: physical address of the start of the data buffer
719 * to be hashed
720 * @data_to_hash_len: the length in bytes of the buffer referenced by
721 * data_to_hash
722 * @efi_tcg_event: pointer to data buffer containing information
723 * about the event
724 *
725 * Return: status code
726 */
727static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200728efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
729 u64 data_to_hash, u64 data_to_hash_len,
730 struct efi_tcg2_event *efi_tcg_event)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200731{
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200732 struct udevice *dev;
733 efi_status_t ret;
734 u32 event_type, pcr_index, event_size;
735 struct tpml_digest_values digest_list;
736
737 EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
738 data_to_hash_len, efi_tcg_event);
739
740 if (!this || !data_to_hash || !efi_tcg_event) {
741 ret = EFI_INVALID_PARAMETER;
742 goto out;
743 }
744
745 ret = platform_get_tpm2_device(&dev);
746 if (ret != EFI_SUCCESS)
747 goto out;
748
749 if (efi_tcg_event->size < efi_tcg_event->header.header_size +
750 sizeof(u32)) {
751 ret = EFI_INVALID_PARAMETER;
752 goto out;
753 }
754
755 if (efi_tcg_event->header.pcr_index < 0 ||
756 efi_tcg_event->header.pcr_index > TPM2_MAX_PCRS) {
757 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
777 ret = tcg2_create_digest((u8 *)data_to_hash, data_to_hash_len,
778 &digest_list);
779 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 Apalodimasc1c02102020-11-11 11:18:11 +0200802}
803
804/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200805 * efi_tcg2_submit_command() - Send command to the TPM
Ilias Apalodimasc1c02102020-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 Apalodimasc8d0fd52020-11-30 11:47:40 +0200815static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200816efi_tcg2_submit_command(struct efi_tcg2_protocol *this,
817 u32 input_param_block_size, u8 *input_param_block,
818 u32 output_param_block_size, u8 *output_param_block)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200819{
820 return EFI_UNSUPPORTED;
821}
822
823/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200824 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200825 *
826 * @this: TCG2 protocol instance
827 * @active_pcr_banks: pointer for receiving the bitmap of currently
828 * active PCR banks
829 *
830 * Return: status code
831 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200832static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200833efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
834 u32 *active_pcr_banks)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200835{
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200836 efi_status_t ret;
837
838 EFI_ENTRY("%p, %p", this, active_pcr_banks);
839 ret = __get_active_pcr_banks(active_pcr_banks);
840
841 return EFI_EXIT(ret);
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200842}
843
844/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200845 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200846 *
847 * @this: TCG2 protocol instance
848 * @active_pcr_banks: bitmap of the requested active PCR banks
849 *
850 * Return: status code
851 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200852static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200853efi_tcg2_set_active_pcr_banks(struct efi_tcg2_protocol *this,
854 u32 active_pcr_banks)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200855{
856 return EFI_UNSUPPORTED;
857}
858
859/**
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200860 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
861 * set_active_pcr_banks()
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200862 *
863 * @this: TCG2 protocol instance
864 * @operation_present: non-zero value to indicate a
865 * set_active_pcr_banks operation was
866 * invoked during last boot
867 * @response: result value could be returned
868 *
869 * Return: status code
870 */
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200871static efi_status_t EFIAPI
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200872efi_tcg2_get_result_of_set_active_pcr_banks(struct efi_tcg2_protocol *this,
873 u32 *operation_present, u32 *response)
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200874{
875 return EFI_UNSUPPORTED;
876}
877
878static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +0200879 .get_capability = efi_tcg2_get_capability,
880 .get_eventlog = efi_tcg2_get_eventlog,
881 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
882 .submit_command = efi_tcg2_submit_command,
883 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
884 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
885 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimasc1c02102020-11-11 11:18:11 +0200886};
887
888/**
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200889 * create_specid_event() - Create the first event in the eventlog
890 *
891 * @dev: tpm device
892 * @event_header: Pointer to the final event header
893 * @event_size: final spec event size
894 *
895 * Return: status code
896 */
897static efi_status_t create_specid_event(struct udevice *dev, void *buffer,
898 size_t *event_size)
899{
900 struct tcg_efi_spec_id_event *spec_event;
901 size_t spec_event_size;
902 efi_status_t ret = EFI_DEVICE_ERROR;
903 u32 active, supported;
904 int err, i;
905
906 /*
907 * Create Spec event. This needs to be the first event in the log
908 * according to the TCG EFI protocol spec
909 */
910
911 /* Setup specID event data */
912 spec_event = (struct tcg_efi_spec_id_event *)buffer;
913 memcpy(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
914 sizeof(spec_event->signature));
915 put_unaligned_le32(0, &spec_event->platform_class); /* type client */
916 spec_event->spec_version_minor =
917 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2;
918 spec_event->spec_version_major =
919 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2;
920 spec_event->spec_errata =
921 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2;
922 spec_event->uintn_size = sizeof(efi_uintn_t) / sizeof(u32);
923
924 err = tpm2_get_pcr_info(dev, &supported, &active,
925 &spec_event->number_of_algorithms);
926 if (err)
927 goto out;
928 if (spec_event->number_of_algorithms > MAX_HASH_COUNT ||
929 spec_event->number_of_algorithms < 1)
930 goto out;
931
932 for (i = 0; i < spec_event->number_of_algorithms; i++) {
933 u16 hash_alg = hash_algo_list[i].hash_alg;
934 u16 hash_len = hash_algo_list[i].hash_len;
935
936 if (active && alg_to_mask(hash_alg)) {
937 put_unaligned_le16(hash_alg,
938 &spec_event->digest_sizes[i].algorithm_id);
939 put_unaligned_le16(hash_len,
940 &spec_event->digest_sizes[i].digest_size);
941 }
942 }
943 /*
944 * the size of the spec event and placement of vendor_info_size
945 * depends on supported algoriths
946 */
947 spec_event_size =
948 offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
949 spec_event->number_of_algorithms * sizeof(spec_event->digest_sizes[0]);
950 /* no vendor info for us */
951 memset(buffer + spec_event_size, 0,
952 sizeof(spec_event->vendor_info_size));
953 spec_event_size += sizeof(spec_event->vendor_info_size);
954 *event_size = spec_event_size;
955
956 return EFI_SUCCESS;
957
958out:
959 return ret;
960}
961
962/**
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +0200963 * tcg2_uninit - remove the final event table and free efi memory on failures
964 */
965void tcg2_uninit(void)
966{
967 efi_status_t ret;
968
969 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
970 if (ret != EFI_SUCCESS)
971 log_err("Failed to delete final events config table\n");
972
973 efi_free_pool(event_log.buffer);
974 event_log.buffer = NULL;
975 efi_free_pool(event_log.final_buffer);
976 event_log.final_buffer = NULL;
977}
978
979/**
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +0200980 * create_final_event() - Create the final event and install the config
981 * defined by the TCG EFI spec
982 */
983static efi_status_t create_final_event(void)
984{
985 struct efi_tcg2_final_events_table *final_event;
986 efi_status_t ret;
987
988 /*
989 * All events generated after the invocation of
990 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
991 * EFI_CONFIGURATION_TABLE
992 */
993 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
994 &event_log.final_buffer);
995 if (ret != EFI_SUCCESS)
996 goto out;
997
998 memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
999 final_event = event_log.final_buffer;
1000 final_event->number_of_events = 0;
1001 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
1002 event_log.final_pos = sizeof(*final_event);
1003 ret = efi_install_configuration_table(&efi_guid_final_events,
1004 final_event);
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001005out:
1006 return ret;
1007}
1008
1009/**
1010 * efi_init_event_log() - initialize an eventlog
1011 */
1012static efi_status_t efi_init_event_log(void)
1013{
1014 /*
1015 * vendor_info_size is currently set to 0, we need to change the length
1016 * and allocate the flexible array member if this changes
1017 */
1018 struct tcg_pcr_event *event_header = NULL;
1019 struct udevice *dev;
1020 size_t spec_event_size;
1021 efi_status_t ret;
1022
1023 ret = platform_get_tpm2_device(&dev);
1024 if (ret != EFI_SUCCESS)
1025 goto out;
1026
1027 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
1028 (void **)&event_log.buffer);
1029 if (ret != EFI_SUCCESS)
1030 goto out;
1031
1032 /*
1033 * initialize log area as 0xff so the OS can easily figure out the
1034 * last log entry
1035 */
1036 memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
1037 event_log.pos = 0;
1038 event_log.last_event_size = 0;
1039 event_log.get_event_called = false;
1040 event_log.truncated = false;
1041
1042 /*
1043 * The log header is defined to be in SHA1 event log entry format.
1044 * Setup event header
1045 */
1046 event_header = (struct tcg_pcr_event *)event_log.buffer;
1047 put_unaligned_le32(0, &event_header->pcr_index);
1048 put_unaligned_le32(EV_NO_ACTION, &event_header->event_type);
1049 memset(&event_header->digest, 0, sizeof(event_header->digest));
Ilias Apalodimasf8cd72d2021-03-30 00:42:36 +03001050 ret = create_specid_event(dev, (void *)((uintptr_t)event_log.buffer + sizeof(*event_header)),
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001051 &spec_event_size);
1052 if (ret != EFI_SUCCESS)
1053 goto out;
1054 put_unaligned_le32(spec_event_size, &event_header->event_size);
1055 event_log.pos = spec_event_size + sizeof(*event_header);
1056 event_log.last_event_size = event_log.pos;
1057
1058 ret = create_final_event();
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001059 if (ret != EFI_SUCCESS)
1060 goto out;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001061
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001062 return EFI_SUCCESS;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001063out:
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001064 tcg2_uninit();
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001065 return ret;
1066}
1067
1068/**
Ilias Apalodimasf69a2012021-03-24 16:50:46 +02001069 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
1070 * eventlog and extend the PCRs
1071 *
1072 * @dev: TPM device
1073 *
1074 * @Return: status code
1075 */
1076static efi_status_t efi_append_scrtm_version(struct udevice *dev)
1077{
1078 struct tpml_digest_values digest_list;
1079 u8 ver[] = U_BOOT_VERSION_STRING;
1080 const int pcr_index = 0;
1081 efi_status_t ret;
1082
1083 ret = tcg2_create_digest(ver, sizeof(ver), &digest_list);
1084 if (ret != EFI_SUCCESS)
1085 goto out;
1086
1087 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
1088 if (ret != EFI_SUCCESS)
1089 goto out;
1090
1091 ret = tcg2_agile_log_append(pcr_index, EV_S_CRTM_VERSION, &digest_list,
1092 sizeof(ver), ver);
1093
1094out:
1095 return ret;
1096}
1097
1098/**
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001099 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1100 *
1101 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1102 *
1103 * Return: An error status is only returned if adding the protocol fails.
1104 */
1105efi_status_t efi_tcg2_register(void)
1106{
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001107 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001108 struct udevice *dev;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001109
1110 ret = platform_get_tpm2_device(&dev);
Ilias Apalodimas9aeb3802020-11-16 08:52:41 +02001111 if (ret != EFI_SUCCESS) {
1112 log_warning("Unable to find TPMv2 device\n");
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001113 ret = EFI_SUCCESS;
1114 goto out;
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001115 }
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001116
1117 ret = efi_init_event_log();
1118 if (ret != EFI_SUCCESS)
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001119 goto fail;
Ilias Apalodimasc8d0fd52020-11-30 11:47:40 +02001120
Ilias Apalodimasf69a2012021-03-24 16:50:46 +02001121 ret = efi_append_scrtm_version(dev);
1122 if (ret != EFI_SUCCESS)
1123 goto out;
1124
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001125 ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol,
1126 (void *)&efi_tcg2_protocol);
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001127 if (ret != EFI_SUCCESS) {
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001128 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001129 goto fail;
1130 }
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001131
Ilias Apalodimasd8cf1132021-03-25 13:31:45 +02001132out:
1133 return ret;
1134fail:
1135 tcg2_uninit();
Ilias Apalodimasc1c02102020-11-11 11:18:11 +02001136 return ret;
1137}