blob: 94e8f22bbb6913fd15c3af26f0954288096e09f2 [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
56const static 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{
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 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{
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 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;
181 int i;
182 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;
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 Apalodimasc67fef62020-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 Apalodimas590fef62020-11-11 11:18:11 +0200458 if (is_active_pcr(&pcrs.selection[i]))
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200459 *active_pcr |= hash_mask;
460 } else {
461 EFI_PRINT("Unknown algorithm %x\n", pcrs.selection[i].hash);
Ilias Apalodimas590fef62020-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 Apalodimas967650d2020-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;
Masahisa Kojimaeb74a902021-04-14 11:55:49 +0900518 u8 final[TPM2_SHA512_DIGEST_SIZE];
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200519 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);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200538 break;
539 case TPM2_ALG_SHA256:
540 sha256_starts(&ctx_256);
541 sha256_update(&ctx_256, input, length);
542 sha256_finish(&ctx_256, final);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200543 break;
544 case TPM2_ALG_SHA384:
545 sha384_starts(&ctx_512);
546 sha384_update(&ctx_512, input, length);
547 sha384_finish(&ctx_512, final);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200548 break;
549 case TPM2_ALG_SHA512:
550 sha512_starts(&ctx_512);
551 sha512_update(&ctx_512, input, length);
552 sha512_finish(&ctx_512, final);
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200553 break;
554 default:
555 EFI_PRINT("Unsupported algorithm %x\n", hash_alg);
556 return EFI_INVALID_PARAMETER;
557 }
Ilias Apalodimas754b3a42021-04-22 14:32:14 +0300558 digest_list->count++;
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200559 digest_list->digests[i].hash_alg = hash_alg;
560 memcpy(&digest_list->digests[i].digest, final, (u32)alg_to_len(hash_alg));
561 }
562
563 return EFI_SUCCESS;
564}
565
566/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200567 * efi_tcg2_get_capability() - protocol capability information and state information
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200568 *
569 * @this: TCG2 protocol instance
570 * @capability: caller allocated memory with size field to the size of
571 * the structure allocated
572
573 * Return: status code
574 */
575static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200576efi_tcg2_get_capability(struct efi_tcg2_protocol *this,
577 struct efi_tcg2_boot_service_capability *capability)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200578{
579 struct udevice *dev;
580 efi_status_t efi_ret;
581 int ret;
582
583 EFI_ENTRY("%p, %p", this, capability);
584
585 if (!this || !capability) {
586 efi_ret = EFI_INVALID_PARAMETER;
587 goto out;
588 }
589
590 if (capability->size < boot_service_capability_min) {
591 capability->size = boot_service_capability_min;
592 efi_ret = EFI_BUFFER_TOO_SMALL;
593 goto out;
594 }
595
596 if (capability->size < sizeof(*capability)) {
597 capability->size = sizeof(*capability);
598 efi_ret = EFI_BUFFER_TOO_SMALL;
599 goto out;
600 }
601
602 capability->structure_version.major = 1;
603 capability->structure_version.minor = 1;
604 capability->protocol_version.major = 1;
605 capability->protocol_version.minor = 1;
606
607 efi_ret = platform_get_tpm2_device(&dev);
608 if (efi_ret != EFI_SUCCESS) {
609 capability->supported_event_logs = 0;
610 capability->hash_algorithm_bitmap = 0;
611 capability->tpm_present_flag = false;
612 capability->max_command_size = 0;
613 capability->max_response_size = 0;
614 capability->manufacturer_id = 0;
615 capability->number_of_pcr_banks = 0;
616 capability->active_pcr_banks = 0;
617
618 efi_ret = EFI_SUCCESS;
619 goto out;
620 }
621
622 /* We only allow a TPMv2 device to register the EFI protocol */
623 capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2;
624
625 capability->tpm_present_flag = true;
626
627 /* Supported and active PCRs */
628 capability->hash_algorithm_bitmap = 0;
629 capability->active_pcr_banks = 0;
630 ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap,
631 &capability->active_pcr_banks,
632 &capability->number_of_pcr_banks);
633 if (ret) {
634 efi_ret = EFI_DEVICE_ERROR;
635 goto out;
636 }
637
638 /* Max command size */
639 ret = tpm2_get_max_command_size(dev, &capability->max_command_size);
640 if (ret) {
641 efi_ret = EFI_DEVICE_ERROR;
642 goto out;
643 }
644
645 /* Max response size */
646 ret = tpm2_get_max_response_size(dev, &capability->max_response_size);
647 if (ret) {
648 efi_ret = EFI_DEVICE_ERROR;
649 goto out;
650 }
651
652 /* Manufacturer ID */
653 ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id);
654 if (ret) {
655 efi_ret = EFI_DEVICE_ERROR;
656 goto out;
657 }
658
659 return EFI_EXIT(EFI_SUCCESS);
660out:
661 return EFI_EXIT(efi_ret);
662}
663
664/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200665 * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its
666 * last entry
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200667 *
668 * @this: TCG2 protocol instance
669 * @log_format: type of event log format
670 * @event_log_location: pointer to the memory address of the event log
671 * @event_log_last_entry: pointer to the address of the start of the last
672 * entry in the event log in memory, if log contains
673 * more than 1 entry
674 * @event_log_truncated: set to true, if the Event Log is missing at i
675 * least one entry
676 *
677 * Return: status code
678 */
679static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200680efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this,
681 efi_tcg_event_log_format log_format,
682 u64 *event_log_location, u64 *event_log_last_entry,
683 bool *event_log_truncated)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200684{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200685 efi_status_t ret = EFI_SUCCESS;
686 struct udevice *dev;
687
688 EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location,
689 event_log_last_entry, event_log_truncated);
690
691 ret = platform_get_tpm2_device(&dev);
692 if (ret != EFI_SUCCESS) {
693 event_log_location = NULL;
694 event_log_last_entry = NULL;
695 *event_log_truncated = false;
696 ret = EFI_SUCCESS;
697 goto out;
698 }
699 *event_log_location = (uintptr_t)event_log.buffer;
700 *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos -
701 event_log.last_event_size);
702 *event_log_truncated = event_log.truncated;
703 event_log.get_event_called = true;
704
705out:
706 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200707}
708
709/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200710 * efi_tcg2_hash_log_extend_event() - extend and optionally log events
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200711 *
712 * @this: TCG2 protocol instance
713 * @flags: bitmap providing additional information on the
714 * operation
715 * @data_to_hash: physical address of the start of the data buffer
716 * to be hashed
717 * @data_to_hash_len: the length in bytes of the buffer referenced by
718 * data_to_hash
719 * @efi_tcg_event: pointer to data buffer containing information
720 * about the event
721 *
722 * Return: status code
723 */
724static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200725efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags,
726 u64 data_to_hash, u64 data_to_hash_len,
727 struct efi_tcg2_event *efi_tcg_event)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200728{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200729 struct udevice *dev;
730 efi_status_t ret;
731 u32 event_type, pcr_index, event_size;
732 struct tpml_digest_values digest_list;
733
734 EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash,
735 data_to_hash_len, efi_tcg_event);
736
737 if (!this || !data_to_hash || !efi_tcg_event) {
738 ret = EFI_INVALID_PARAMETER;
739 goto out;
740 }
741
742 ret = platform_get_tpm2_device(&dev);
743 if (ret != EFI_SUCCESS)
744 goto out;
745
746 if (efi_tcg_event->size < efi_tcg_event->header.header_size +
747 sizeof(u32)) {
748 ret = EFI_INVALID_PARAMETER;
749 goto out;
750 }
751
752 if (efi_tcg_event->header.pcr_index < 0 ||
753 efi_tcg_event->header.pcr_index > TPM2_MAX_PCRS) {
754 ret = EFI_INVALID_PARAMETER;
755 goto out;
756 }
757
758 /*
759 * if PE_COFF_IMAGE is set we need to make sure the image is not
760 * corrupted, verify it and hash the PE/COFF image in accordance with
761 * the procedure specified in "Calculating the PE Image Hash"
762 * section of the "Windows Authenticode Portable Executable Signature
763 * Format"
764 * Not supported for now
765 */
766 if (flags & PE_COFF_IMAGE) {
767 ret = EFI_UNSUPPORTED;
768 goto out;
769 }
770
771 pcr_index = efi_tcg_event->header.pcr_index;
772 event_type = efi_tcg_event->header.event_type;
773
774 ret = tcg2_create_digest((u8 *)data_to_hash, data_to_hash_len,
775 &digest_list);
776 if (ret != EFI_SUCCESS)
777 goto out;
778
779 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
780 if (ret != EFI_SUCCESS)
781 goto out;
782
783 if (flags & EFI_TCG2_EXTEND_ONLY) {
784 if (event_log.truncated)
785 ret = EFI_VOLUME_FULL;
786 goto out;
787 }
788
789 /*
790 * The efi_tcg_event size includes the size component and the
791 * headersize
792 */
793 event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) -
794 efi_tcg_event->header.header_size;
795 ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list,
796 event_size, efi_tcg_event->event);
797out:
798 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200799}
800
801/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200802 * efi_tcg2_submit_command() - Send command to the TPM
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200803 *
804 * @this: TCG2 protocol instance
805 * @input_param_block_size: size of the TPM input parameter block
806 * @input_param_block: pointer to the TPM input parameter block
807 * @output_param_block_size: size of the TPM output parameter block
808 * @output_param_block: pointer to the TPM output parameter block
809 *
810 * Return: status code
811 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200812static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200813efi_tcg2_submit_command(struct efi_tcg2_protocol *this,
814 u32 input_param_block_size, u8 *input_param_block,
815 u32 output_param_block_size, u8 *output_param_block)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200816{
817 return EFI_UNSUPPORTED;
818}
819
820/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200821 * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200822 *
823 * @this: TCG2 protocol instance
824 * @active_pcr_banks: pointer for receiving the bitmap of currently
825 * active PCR banks
826 *
827 * Return: status code
828 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200829static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200830efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this,
831 u32 *active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200832{
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200833 efi_status_t ret;
834
835 EFI_ENTRY("%p, %p", this, active_pcr_banks);
836 ret = __get_active_pcr_banks(active_pcr_banks);
837
838 return EFI_EXIT(ret);
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200839}
840
841/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200842 * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200843 *
844 * @this: TCG2 protocol instance
845 * @active_pcr_banks: bitmap of the requested active PCR banks
846 *
847 * Return: status code
848 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200849static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200850efi_tcg2_set_active_pcr_banks(struct efi_tcg2_protocol *this,
851 u32 active_pcr_banks)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200852{
853 return EFI_UNSUPPORTED;
854}
855
856/**
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200857 * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous
858 * set_active_pcr_banks()
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200859 *
860 * @this: TCG2 protocol instance
861 * @operation_present: non-zero value to indicate a
862 * set_active_pcr_banks operation was
863 * invoked during last boot
864 * @response: result value could be returned
865 *
866 * Return: status code
867 */
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200868static efi_status_t EFIAPI
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200869efi_tcg2_get_result_of_set_active_pcr_banks(struct efi_tcg2_protocol *this,
870 u32 *operation_present, u32 *response)
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200871{
872 return EFI_UNSUPPORTED;
873}
874
875static const struct efi_tcg2_protocol efi_tcg2_protocol = {
Ilias Apalodimasc67fef62020-11-16 08:52:41 +0200876 .get_capability = efi_tcg2_get_capability,
877 .get_eventlog = efi_tcg2_get_eventlog,
878 .hash_log_extend_event = efi_tcg2_hash_log_extend_event,
879 .submit_command = efi_tcg2_submit_command,
880 .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks,
881 .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks,
882 .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks,
Ilias Apalodimas590fef62020-11-11 11:18:11 +0200883};
884
885/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200886 * create_specid_event() - Create the first event in the eventlog
887 *
888 * @dev: tpm device
889 * @event_header: Pointer to the final event header
890 * @event_size: final spec event size
891 *
892 * Return: status code
893 */
894static efi_status_t create_specid_event(struct udevice *dev, void *buffer,
895 size_t *event_size)
896{
897 struct tcg_efi_spec_id_event *spec_event;
898 size_t spec_event_size;
899 efi_status_t ret = EFI_DEVICE_ERROR;
900 u32 active, supported;
901 int err, i;
902
903 /*
904 * Create Spec event. This needs to be the first event in the log
905 * according to the TCG EFI protocol spec
906 */
907
908 /* Setup specID event data */
909 spec_event = (struct tcg_efi_spec_id_event *)buffer;
910 memcpy(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
911 sizeof(spec_event->signature));
912 put_unaligned_le32(0, &spec_event->platform_class); /* type client */
913 spec_event->spec_version_minor =
914 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2;
915 spec_event->spec_version_major =
916 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2;
917 spec_event->spec_errata =
918 TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2;
919 spec_event->uintn_size = sizeof(efi_uintn_t) / sizeof(u32);
920
921 err = tpm2_get_pcr_info(dev, &supported, &active,
922 &spec_event->number_of_algorithms);
923 if (err)
924 goto out;
925 if (spec_event->number_of_algorithms > MAX_HASH_COUNT ||
926 spec_event->number_of_algorithms < 1)
927 goto out;
928
929 for (i = 0; i < spec_event->number_of_algorithms; i++) {
930 u16 hash_alg = hash_algo_list[i].hash_alg;
931 u16 hash_len = hash_algo_list[i].hash_len;
932
933 if (active && alg_to_mask(hash_alg)) {
934 put_unaligned_le16(hash_alg,
935 &spec_event->digest_sizes[i].algorithm_id);
936 put_unaligned_le16(hash_len,
937 &spec_event->digest_sizes[i].digest_size);
938 }
939 }
940 /*
941 * the size of the spec event and placement of vendor_info_size
942 * depends on supported algoriths
943 */
944 spec_event_size =
945 offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
946 spec_event->number_of_algorithms * sizeof(spec_event->digest_sizes[0]);
947 /* no vendor info for us */
948 memset(buffer + spec_event_size, 0,
949 sizeof(spec_event->vendor_info_size));
950 spec_event_size += sizeof(spec_event->vendor_info_size);
951 *event_size = spec_event_size;
952
953 return EFI_SUCCESS;
954
955out:
956 return ret;
957}
958
959/**
Ilias Apalodimas1b278e62021-03-25 13:31:45 +0200960 * tcg2_uninit - remove the final event table and free efi memory on failures
961 */
962void tcg2_uninit(void)
963{
964 efi_status_t ret;
965
966 ret = efi_install_configuration_table(&efi_guid_final_events, NULL);
967 if (ret != EFI_SUCCESS)
968 log_err("Failed to delete final events config table\n");
969
970 efi_free_pool(event_log.buffer);
971 event_log.buffer = NULL;
972 efi_free_pool(event_log.final_buffer);
973 event_log.final_buffer = NULL;
974}
975
976/**
Ilias Apalodimas967650d2020-11-30 11:47:40 +0200977 * create_final_event() - Create the final event and install the config
978 * defined by the TCG EFI spec
979 */
980static efi_status_t create_final_event(void)
981{
982 struct efi_tcg2_final_events_table *final_event;
983 efi_status_t ret;
984
985 /*
986 * All events generated after the invocation of
987 * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an
988 * EFI_CONFIGURATION_TABLE
989 */
990 ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE,
991 &event_log.final_buffer);
992 if (ret != EFI_SUCCESS)
993 goto out;
994
995 memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE);
996 final_event = event_log.final_buffer;
997 final_event->number_of_events = 0;
998 final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION;
999 event_log.final_pos = sizeof(*final_event);
1000 ret = efi_install_configuration_table(&efi_guid_final_events,
1001 final_event);
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001002out:
1003 return ret;
1004}
1005
1006/**
1007 * efi_init_event_log() - initialize an eventlog
1008 */
1009static efi_status_t efi_init_event_log(void)
1010{
1011 /*
1012 * vendor_info_size is currently set to 0, we need to change the length
1013 * and allocate the flexible array member if this changes
1014 */
1015 struct tcg_pcr_event *event_header = NULL;
1016 struct udevice *dev;
1017 size_t spec_event_size;
1018 efi_status_t ret;
1019
1020 ret = platform_get_tpm2_device(&dev);
1021 if (ret != EFI_SUCCESS)
1022 goto out;
1023
1024 ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE,
1025 (void **)&event_log.buffer);
1026 if (ret != EFI_SUCCESS)
1027 goto out;
1028
1029 /*
1030 * initialize log area as 0xff so the OS can easily figure out the
1031 * last log entry
1032 */
1033 memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE);
1034 event_log.pos = 0;
1035 event_log.last_event_size = 0;
1036 event_log.get_event_called = false;
1037 event_log.truncated = false;
1038
1039 /*
1040 * The log header is defined to be in SHA1 event log entry format.
1041 * Setup event header
1042 */
1043 event_header = (struct tcg_pcr_event *)event_log.buffer;
1044 put_unaligned_le32(0, &event_header->pcr_index);
1045 put_unaligned_le32(EV_NO_ACTION, &event_header->event_type);
1046 memset(&event_header->digest, 0, sizeof(event_header->digest));
Ilias Apalodimas4390e762021-03-30 00:42:36 +03001047 ret = create_specid_event(dev, (void *)((uintptr_t)event_log.buffer + sizeof(*event_header)),
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001048 &spec_event_size);
1049 if (ret != EFI_SUCCESS)
1050 goto out;
1051 put_unaligned_le32(spec_event_size, &event_header->event_size);
1052 event_log.pos = spec_event_size + sizeof(*event_header);
1053 event_log.last_event_size = event_log.pos;
1054
1055 ret = create_final_event();
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001056 if (ret != EFI_SUCCESS)
1057 goto out;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001058
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001059 return EFI_SUCCESS;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001060out:
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001061 tcg2_uninit();
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001062 return ret;
1063}
1064
1065/**
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +02001066 * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the
1067 * eventlog and extend the PCRs
1068 *
1069 * @dev: TPM device
1070 *
1071 * @Return: status code
1072 */
1073static efi_status_t efi_append_scrtm_version(struct udevice *dev)
1074{
1075 struct tpml_digest_values digest_list;
1076 u8 ver[] = U_BOOT_VERSION_STRING;
1077 const int pcr_index = 0;
1078 efi_status_t ret;
1079
1080 ret = tcg2_create_digest(ver, sizeof(ver), &digest_list);
1081 if (ret != EFI_SUCCESS)
1082 goto out;
1083
1084 ret = tcg2_pcr_extend(dev, pcr_index, &digest_list);
1085 if (ret != EFI_SUCCESS)
1086 goto out;
1087
1088 ret = tcg2_agile_log_append(pcr_index, EV_S_CRTM_VERSION, &digest_list,
1089 sizeof(ver), ver);
1090
1091out:
1092 return ret;
1093}
1094
1095/**
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001096 * efi_tcg2_register() - register EFI_TCG2_PROTOCOL
1097 *
1098 * If a TPM2 device is available, the TPM TCG2 Protocol is registered
1099 *
1100 * Return: An error status is only returned if adding the protocol fails.
1101 */
1102efi_status_t efi_tcg2_register(void)
1103{
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001104 efi_status_t ret = EFI_SUCCESS;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001105 struct udevice *dev;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001106
1107 ret = platform_get_tpm2_device(&dev);
Ilias Apalodimasc67fef62020-11-16 08:52:41 +02001108 if (ret != EFI_SUCCESS) {
1109 log_warning("Unable to find TPMv2 device\n");
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001110 ret = EFI_SUCCESS;
1111 goto out;
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001112 }
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001113
1114 ret = efi_init_event_log();
1115 if (ret != EFI_SUCCESS)
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001116 goto fail;
Ilias Apalodimas967650d2020-11-30 11:47:40 +02001117
Ilias Apalodimasf576f7d2021-03-24 16:50:46 +02001118 ret = efi_append_scrtm_version(dev);
1119 if (ret != EFI_SUCCESS)
1120 goto out;
1121
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001122 ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol,
1123 (void *)&efi_tcg2_protocol);
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001124 if (ret != EFI_SUCCESS) {
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001125 log_err("Cannot install EFI_TCG2_PROTOCOL\n");
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001126 goto fail;
1127 }
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001128
Ilias Apalodimas1b278e62021-03-25 13:31:45 +02001129out:
1130 return ret;
1131fail:
1132 tcg2_uninit();
Ilias Apalodimas590fef62020-11-11 11:18:11 +02001133 return ret;
1134}