blob: 37818fcc85150eaab783dc86bca50bc45c085120 [file] [log] [blame]
Ilias Apalodimas95537a62024-06-23 14:48:15 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2023 Linaro Limited
4 */
5
6#include <dm.h>
7#include <dm/of_access.h>
8#include <tpm_api.h>
9#include <tpm-common.h>
10#include <tpm-v2.h>
11#include <tpm_tcg2.h>
12#include <u-boot/sha1.h>
13#include <u-boot/sha256.h>
14#include <u-boot/sha512.h>
15#include <version_string.h>
16#include <asm/io.h>
17#include <linux/bitops.h>
18#include <linux/unaligned/be_byteshift.h>
19#include <linux/unaligned/generic.h>
20#include <linux/unaligned/le_byteshift.h>
21#include "tpm-utils.h"
22
Raymond Mao6434c4a2024-12-24 08:01:06 -080023int tcg2_get_pcr_info(struct udevice *dev, u32 *supported_bank, u32 *active_bank,
24 u32 *bank_num)
Ilias Apalodimascb356612024-06-23 14:48:17 +030025{
Ilias Apalodimascb356612024-06-23 14:48:17 +030026 struct tpml_pcr_selection pcrs;
27 size_t i;
28 u32 ret;
29
Raymond Mao6434c4a2024-12-24 08:01:06 -080030 *supported_bank = 0;
31 *active_bank = 0;
32 *bank_num = 0;
Ilias Apalodimascb356612024-06-23 14:48:17 +030033
34 ret = tpm2_get_pcr_info(dev, &pcrs);
35 if (ret)
36 return ret;
37
38 for (i = 0; i < pcrs.count; i++) {
Raymond Mao43158122024-12-24 08:01:07 -080039 struct tpms_pcr_selection *sel = &pcrs.selection[i];
40 u32 hash_mask = tcg2_algorithm_to_mask(sel->hash);
Ilias Apalodimascb356612024-06-23 14:48:17 +030041
Raymond Mao43158122024-12-24 08:01:07 -080042 if (tpm2_algorithm_supported(sel->hash))
Raymond Mao6434c4a2024-12-24 08:01:06 -080043 *supported_bank |= hash_mask;
Raymond Mao43158122024-12-24 08:01:07 -080044 else
45 log_warning("%s: unknown algorithm %x\n", __func__,
46 sel->hash);
47
48 if (tpm2_is_active_bank(sel))
49 *active_bank |= hash_mask;
Ilias Apalodimascb356612024-06-23 14:48:17 +030050 }
51
Raymond Mao6434c4a2024-12-24 08:01:06 -080052 *bank_num = pcrs.count;
Ilias Apalodimascb356612024-06-23 14:48:17 +030053
54 return 0;
55}
56
Ilias Apalodimas95537a62024-06-23 14:48:15 +030057int tcg2_get_active_pcr_banks(struct udevice *dev, u32 *active_pcr_banks)
58{
59 u32 supported = 0;
60 u32 pcr_banks = 0;
61 u32 active = 0;
62 int rc;
63
Ilias Apalodimascb356612024-06-23 14:48:17 +030064 rc = tcg2_get_pcr_info(dev, &supported, &active, &pcr_banks);
Ilias Apalodimas95537a62024-06-23 14:48:15 +030065 if (rc)
66 return rc;
67
68 *active_pcr_banks = active;
69
70 return 0;
71}
72
73u32 tcg2_event_get_size(struct tpml_digest_values *digest_list)
74{
75 u32 len;
76 size_t i;
77
78 len = offsetof(struct tcg_pcr_event2, digests);
79 len += offsetof(struct tpml_digest_values, digests);
80 for (i = 0; i < digest_list->count; ++i) {
81 u16 l = tpm2_algorithm_to_len(digest_list->digests[i].hash_alg);
82
83 if (!l)
84 continue;
85
86 len += l + offsetof(struct tpmt_ha, digest);
87 }
88 len += sizeof(u32);
89
90 return len;
91}
92
93int tcg2_create_digest(struct udevice *dev, const u8 *input, u32 length,
94 struct tpml_digest_values *digest_list)
95{
Ilias Apalodimase5ade052024-12-24 08:01:10 -080096 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
Ilias Apalodimas95537a62024-06-23 14:48:15 +030097 u8 final[sizeof(union tpmu_ha)];
98 sha256_context ctx_256;
99 sha512_context ctx_512;
100 sha1_context ctx;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300101 size_t i;
102 u32 len;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300103
104 digest_list->count = 0;
Ilias Apalodimase5ade052024-12-24 08:01:10 -0800105 for (i = 0; i < priv->active_bank_count; i++) {
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300106
Ilias Apalodimase5ade052024-12-24 08:01:10 -0800107 switch (priv->active_banks[i]) {
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300108 case TPM2_ALG_SHA1:
109 sha1_starts(&ctx);
110 sha1_update(&ctx, input, length);
111 sha1_finish(&ctx, final);
112 len = TPM2_SHA1_DIGEST_SIZE;
113 break;
114 case TPM2_ALG_SHA256:
115 sha256_starts(&ctx_256);
116 sha256_update(&ctx_256, input, length);
117 sha256_finish(&ctx_256, final);
118 len = TPM2_SHA256_DIGEST_SIZE;
119 break;
120 case TPM2_ALG_SHA384:
121 sha384_starts(&ctx_512);
122 sha384_update(&ctx_512, input, length);
123 sha384_finish(&ctx_512, final);
124 len = TPM2_SHA384_DIGEST_SIZE;
125 break;
126 case TPM2_ALG_SHA512:
127 sha512_starts(&ctx_512);
128 sha512_update(&ctx_512, input, length);
129 sha512_finish(&ctx_512, final);
130 len = TPM2_SHA512_DIGEST_SIZE;
131 break;
132 default:
133 printf("%s: unsupported algorithm %x\n", __func__,
Ilias Apalodimase5ade052024-12-24 08:01:10 -0800134 priv->active_banks[i]);
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300135 continue;
136 }
137
138 digest_list->digests[digest_list->count].hash_alg =
Ilias Apalodimase5ade052024-12-24 08:01:10 -0800139 priv->active_banks[i];
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300140 memcpy(&digest_list->digests[digest_list->count].digest, final,
141 len);
142 digest_list->count++;
143 }
144
145 return 0;
146}
147
148void tcg2_log_append(u32 pcr_index, u32 event_type,
149 struct tpml_digest_values *digest_list, u32 size,
150 const u8 *event, u8 *log)
151{
152 size_t len;
153 size_t pos;
154 u32 i;
155
156 pos = offsetof(struct tcg_pcr_event2, pcr_index);
157 put_unaligned_le32(pcr_index, log);
158 pos = offsetof(struct tcg_pcr_event2, event_type);
159 put_unaligned_le32(event_type, log + pos);
160 pos = offsetof(struct tcg_pcr_event2, digests) +
161 offsetof(struct tpml_digest_values, count);
162 put_unaligned_le32(digest_list->count, log + pos);
163
164 pos = offsetof(struct tcg_pcr_event2, digests) +
165 offsetof(struct tpml_digest_values, digests);
166 for (i = 0; i < digest_list->count; ++i) {
167 u16 hash_alg = digest_list->digests[i].hash_alg;
168
169 len = tpm2_algorithm_to_len(hash_alg);
170 if (!len)
171 continue;
172
173 pos += offsetof(struct tpmt_ha, hash_alg);
174 put_unaligned_le16(hash_alg, log + pos);
175 pos += offsetof(struct tpmt_ha, digest);
176 memcpy(log + pos, (u8 *)&digest_list->digests[i].digest, len);
177 pos += len;
178 }
179
180 put_unaligned_le32(size, log + pos);
181 pos += sizeof(u32);
182 memcpy(log + pos, event, size);
183}
184
185static int tcg2_log_append_check(struct tcg2_event_log *elog, u32 pcr_index,
186 u32 event_type,
187 struct tpml_digest_values *digest_list,
188 u32 size, const u8 *event)
189{
190 u32 event_size;
191 u8 *log;
192
193 event_size = size + tcg2_event_get_size(digest_list);
194 if (elog->log_position + event_size > elog->log_size) {
195 printf("%s: log too large: %u + %u > %u\n", __func__,
196 elog->log_position, event_size, elog->log_size);
197 return -ENOBUFS;
198 }
199
200 log = elog->log + elog->log_position;
201 elog->log_position += event_size;
202
203 tcg2_log_append(pcr_index, event_type, digest_list, size, event, log);
204
205 return 0;
206}
207
208static int tcg2_log_init(struct udevice *dev, struct tcg2_event_log *elog)
209{
Ilias Apalodimasc22a0642024-12-24 08:01:11 -0800210 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300211 struct tcg_efi_spec_id_event *ev;
212 struct tcg_pcr_event *log;
213 u32 event_size;
214 u32 count = 0;
215 u32 log_size;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300216 size_t i;
217 u16 len;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300218
Ilias Apalodimasc22a0642024-12-24 08:01:11 -0800219 count = priv->active_bank_count;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300220 event_size = offsetof(struct tcg_efi_spec_id_event, digest_sizes);
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300221 event_size += 1 +
222 (sizeof(struct tcg_efi_spec_id_event_algorithm_size) * count);
223 log_size = offsetof(struct tcg_pcr_event, event) + event_size;
224
225 if (log_size > elog->log_size) {
226 printf("%s: log too large: %u > %u\n", __func__, log_size,
227 elog->log_size);
228 return -ENOBUFS;
229 }
230
231 log = (struct tcg_pcr_event *)elog->log;
232 put_unaligned_le32(0, &log->pcr_index);
233 put_unaligned_le32(EV_NO_ACTION, &log->event_type);
234 memset(&log->digest, 0, sizeof(log->digest));
235 put_unaligned_le32(event_size, &log->event_size);
236
237 ev = (struct tcg_efi_spec_id_event *)log->event;
238 strlcpy((char *)ev->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
239 sizeof(ev->signature));
240 put_unaligned_le32(0, &ev->platform_class);
241 ev->spec_version_minor = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2;
242 ev->spec_version_major = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2;
243 ev->spec_errata = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2;
244 ev->uintn_size = sizeof(size_t) / sizeof(u32);
245 put_unaligned_le32(count, &ev->number_of_algorithms);
246
Ilias Apalodimasc22a0642024-12-24 08:01:11 -0800247 for (i = 0; i < count; ++i) {
248 len = tpm2_algorithm_to_len(priv->active_banks[i]);
249 put_unaligned_le16(priv->active_banks[i],
250 &ev->digest_sizes[i].algorithm_id);
251 put_unaligned_le16(len, &ev->digest_sizes[i].digest_size);
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300252 }
253
254 *((u8 *)ev + (event_size - 1)) = 0;
255 elog->log_position = log_size;
256
257 return 0;
258}
259
260static int tcg2_replay_eventlog(struct tcg2_event_log *elog,
261 struct udevice *dev,
262 struct tpml_digest_values *digest_list,
263 u32 log_position)
264{
265 const u32 offset = offsetof(struct tcg_pcr_event2, digests) +
266 offsetof(struct tpml_digest_values, digests);
267 u32 event_size;
268 u32 count;
269 u16 algo;
270 u32 pcr;
271 u32 pos;
272 u16 len;
273 u8 *log;
274 int rc;
275 u32 i;
276
277 while (log_position + offset < elog->log_size) {
278 log = elog->log + log_position;
279
280 pos = offsetof(struct tcg_pcr_event2, pcr_index);
281 pcr = get_unaligned_le32(log + pos);
282 pos = offsetof(struct tcg_pcr_event2, event_type);
283 if (!get_unaligned_le32(log + pos))
284 return 0;
285
286 pos = offsetof(struct tcg_pcr_event2, digests) +
287 offsetof(struct tpml_digest_values, count);
288 count = get_unaligned_le32(log + pos);
289 if (count > ARRAY_SIZE(hash_algo_list) ||
290 (digest_list->count && digest_list->count != count))
291 return 0;
292
293 pos = offsetof(struct tcg_pcr_event2, digests) +
294 offsetof(struct tpml_digest_values, digests);
295 for (i = 0; i < count; ++i) {
296 pos += offsetof(struct tpmt_ha, hash_alg);
297 if (log_position + pos + sizeof(u16) >= elog->log_size)
298 return 0;
299
300 algo = get_unaligned_le16(log + pos);
301 pos += offsetof(struct tpmt_ha, digest);
302 switch (algo) {
303 case TPM2_ALG_SHA1:
304 case TPM2_ALG_SHA256:
305 case TPM2_ALG_SHA384:
306 case TPM2_ALG_SHA512:
307 len = tpm2_algorithm_to_len(algo);
308 break;
309 default:
310 return 0;
311 }
312
313 if (digest_list->count) {
314 if (algo != digest_list->digests[i].hash_alg ||
315 log_position + pos + len >= elog->log_size)
316 return 0;
317
318 memcpy(digest_list->digests[i].digest.sha512,
319 log + pos, len);
320 }
321
322 pos += len;
323 }
324
325 if (log_position + pos + sizeof(u32) >= elog->log_size)
326 return 0;
327
328 event_size = get_unaligned_le32(log + pos);
329 pos += event_size + sizeof(u32);
330 if (log_position + pos > elog->log_size)
331 return 0;
332
333 if (digest_list->count) {
334 rc = tcg2_pcr_extend(dev, pcr, digest_list);
335 if (rc)
336 return rc;
337 }
338
339 log_position += pos;
340 }
341
342 elog->log_position = log_position;
343 elog->found = true;
344 return 0;
345}
346
347static int tcg2_log_parse(struct udevice *dev, struct tcg2_event_log *elog)
348{
349 struct tpml_digest_values digest_list;
350 struct tcg_efi_spec_id_event *event;
351 struct tcg_pcr_event *log;
352 u32 log_active;
353 u32 calc_size;
354 u32 active;
355 u32 count;
356 u32 evsz;
357 u32 mask;
358 u16 algo;
359 u16 len;
360 int rc;
361 u32 i;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300362
363 if (elog->log_size <= offsetof(struct tcg_pcr_event, event))
364 return 0;
365
366 log = (struct tcg_pcr_event *)elog->log;
367 if (get_unaligned_le32(&log->pcr_index) != 0 ||
368 get_unaligned_le32(&log->event_type) != EV_NO_ACTION)
369 return 0;
370
371 for (i = 0; i < sizeof(log->digest); i++) {
372 if (log->digest[i])
373 return 0;
374 }
375
376 evsz = get_unaligned_le32(&log->event_size);
377 if (evsz < offsetof(struct tcg_efi_spec_id_event, digest_sizes) ||
378 evsz + offsetof(struct tcg_pcr_event, event) > elog->log_size)
379 return 0;
380
381 event = (struct tcg_efi_spec_id_event *)log->event;
382 if (memcmp(event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
383 sizeof(TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03)))
384 return 0;
385
386 if (event->spec_version_minor != TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 ||
387 event->spec_version_major != TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2)
388 return 0;
389
390 count = get_unaligned_le32(&event->number_of_algorithms);
391 if (count > ARRAY_SIZE(hash_algo_list))
392 return 0;
393
394 calc_size = offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
395 (sizeof(struct tcg_efi_spec_id_event_algorithm_size) * count) +
396 1;
397 if (evsz != calc_size)
398 return 0;
399
Ilias Apalodimas8b0cba82024-12-24 08:01:12 -0800400 /*
401 * Go through the algorithms the EventLog contains. If the EventLog
402 * algorithms don't match the active TPM ones exit and report the
403 * erroneous banks.
404 * We've already checked that U-Boot supports all the enabled TPM
405 * algorithms, so just check the EvenLog against the TPM active ones.
406 */
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300407 digest_list.count = 0;
408 log_active = 0;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300409 for (i = 0; i < count; ++i) {
410 algo = get_unaligned_le16(&event->digest_sizes[i].algorithm_id);
411 mask = tcg2_algorithm_to_mask(algo);
412
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300413 switch (algo) {
414 case TPM2_ALG_SHA1:
415 case TPM2_ALG_SHA256:
416 case TPM2_ALG_SHA384:
417 case TPM2_ALG_SHA512:
418 len = get_unaligned_le16(&event->digest_sizes[i].digest_size);
Ilias Apalodimas8b0cba82024-12-24 08:01:12 -0800419 if (tpm2_algorithm_to_len(algo) != len) {
420 log_err("EventLog invalid algorithm length\n");
421 return -1;
422 }
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300423 digest_list.digests[digest_list.count++].hash_alg = algo;
424 break;
425 default:
Ilias Apalodimas8b0cba82024-12-24 08:01:12 -0800426 /*
427 * We can ignore this if the TPM PCRs is not extended
428 * by the previous bootloader. But for now just exit
429 */
430 log_err("EventLog has unsupported algorithm 0x%x\n",
431 algo);
432 return -1;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300433 }
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300434 log_active |= mask;
435 }
436
Ilias Apalodimas8b0cba82024-12-24 08:01:12 -0800437 rc = tcg2_get_active_pcr_banks(dev, &active);
438 if (rc)
439 return rc;
440 /* If the EventLog and active algorithms don't match exit */
441 if (log_active != active) {
442 log_err("EventLog doesn't contain all active PCR banks\n");
443 return -1;
444 }
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300445
446 /* Read PCR0 to check if previous firmware extended the PCRs or not. */
447 rc = tcg2_pcr_read(dev, 0, &digest_list);
448 if (rc)
449 return rc;
450
451 for (i = 0; i < digest_list.count; ++i) {
Ilias Apalodimas8b0cba82024-12-24 08:01:12 -0800452 u8 hash_buf[TPM2_SHA512_DIGEST_SIZE] = { 0 };
453 u16 hash_alg = digest_list.digests[i].hash_alg;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300454
Ilias Apalodimas8b0cba82024-12-24 08:01:12 -0800455 if (memcmp((u8 *)&digest_list.digests[i].digest, hash_buf,
456 tpm2_algorithm_to_len(hash_alg)))
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300457 digest_list.count = 0;
Ilias Apalodimas8b0cba82024-12-24 08:01:12 -0800458
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300459 }
460
461 return tcg2_replay_eventlog(elog, dev, &digest_list,
462 offsetof(struct tcg_pcr_event, event) +
463 evsz);
464}
465
466int tcg2_pcr_extend(struct udevice *dev, u32 pcr_index,
467 struct tpml_digest_values *digest_list)
468{
469 u32 rc;
470 u32 i;
471
472 for (i = 0; i < digest_list->count; i++) {
473 u32 alg = digest_list->digests[i].hash_alg;
474
475 rc = tpm2_pcr_extend(dev, pcr_index, alg,
476 (u8 *)&digest_list->digests[i].digest,
477 tpm2_algorithm_to_len(alg));
478 if (rc) {
479 printf("%s: error pcr:%u alg:%08x\n", __func__,
480 pcr_index, alg);
481 return rc;
482 }
483 }
484
485 return 0;
486}
487
488int tcg2_pcr_read(struct udevice *dev, u32 pcr_index,
489 struct tpml_digest_values *digest_list)
490{
491 struct tpm_chip_priv *priv;
492 u32 rc;
493 u32 i;
494
495 priv = dev_get_uclass_priv(dev);
496 if (!priv)
497 return -ENODEV;
498
499 for (i = 0; i < digest_list->count; i++) {
500 u32 alg = digest_list->digests[i].hash_alg;
501 u8 *digest = (u8 *)&digest_list->digests[i].digest;
502
503 rc = tpm2_pcr_read(dev, pcr_index, priv->pcr_select_min, alg,
504 digest, tpm2_algorithm_to_len(alg), NULL);
505 if (rc) {
506 printf("%s: error pcr:%u alg:%08x\n", __func__,
507 pcr_index, alg);
508 return rc;
509 }
510 }
511
512 return 0;
513}
514
515int tcg2_measure_data(struct udevice *dev, struct tcg2_event_log *elog,
516 u32 pcr_index, u32 size, const u8 *data, u32 event_type,
517 u32 event_size, const u8 *event)
518{
519 struct tpml_digest_values digest_list;
520 int rc;
521
522 if (data)
523 rc = tcg2_create_digest(dev, data, size, &digest_list);
524 else
525 rc = tcg2_create_digest(dev, event, event_size, &digest_list);
526 if (rc)
527 return rc;
528
529 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
530 if (rc)
531 return rc;
532
533 return tcg2_log_append_check(elog, pcr_index, event_type, &digest_list,
534 event_size, event);
535}
536
537int tcg2_log_prepare_buffer(struct udevice *dev, struct tcg2_event_log *elog,
538 bool ignore_existing_log)
539{
540 struct tcg2_event_log log;
Ilias Apalodimas7b1e5222024-12-24 08:01:08 -0800541 int rc, i;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300542
543 elog->log_position = 0;
544 elog->found = false;
545
Ilias Apalodimas7b1e5222024-12-24 08:01:08 -0800546 /*
547 * Make sure U-Boot is compiled with all the active PCRs
548 * since we are about to create an EventLog and we won't
549 * measure anything if the PCR banks don't match
550 */
551 if (!tpm2_check_active_banks(dev)) {
552 log_err("Cannot create EventLog\n");
553 log_err("Mismatch between U-Boot and TPM hash algos\n");
554 log_info("TPM:\n");
555 tpm2_print_active_banks(dev);
556 log_info("U-Boot:\n");
557 for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
558 const struct digest_info *algo = &hash_algo_list[i];
559 const char *str;
560
561 if (!algo->supported)
562 continue;
563
564 str = tpm2_algorithm_name(algo->hash_alg);
565 if (str)
566 log_info("%s\n", str);
567 }
568 return -EINVAL;
569 }
570
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300571 rc = tcg2_platform_get_log(dev, (void **)&log.log, &log.log_size);
572 if (!rc) {
573 log.log_position = 0;
574 log.found = false;
575
576 if (!ignore_existing_log) {
577 rc = tcg2_log_parse(dev, &log);
578 if (rc)
579 return rc;
580 }
581
582 if (elog->log_size) {
583 if (log.found) {
584 if (elog->log_size < log.log_position)
585 return -ENOBUFS;
586
587 /*
588 * Copy the discovered log into the user buffer
589 * if there's enough space.
590 */
591 memcpy(elog->log, log.log, log.log_position);
592 }
593
594 unmap_physmem(log.log, MAP_NOCACHE);
595 } else {
596 elog->log = log.log;
597 elog->log_size = log.log_size;
598 }
599
600 elog->log_position = log.log_position;
601 elog->found = log.found;
602 }
603
604 /*
605 * Initialize the log buffer if no log was discovered and the buffer is
606 * valid. User's can pass in their own buffer as a fallback if no
607 * memory region is found.
608 */
609 if (!elog->found && elog->log_size)
610 rc = tcg2_log_init(dev, elog);
611
612 return rc;
613}
614
615int tcg2_measurement_init(struct udevice **dev, struct tcg2_event_log *elog,
616 bool ignore_existing_log)
617{
618 int rc;
619
620 rc = tcg2_platform_get_tpm2(dev);
621 if (rc)
622 return rc;
623
624 rc = tpm_auto_start(*dev);
625 if (rc)
626 return rc;
627
628 rc = tcg2_log_prepare_buffer(*dev, elog, ignore_existing_log);
629 if (rc) {
630 tcg2_measurement_term(*dev, elog, true);
631 return rc;
632 }
633
634 rc = tcg2_measure_event(*dev, elog, 0, EV_S_CRTM_VERSION,
635 strlen(version_string) + 1,
636 (u8 *)version_string);
637 if (rc) {
638 tcg2_measurement_term(*dev, elog, true);
639 return rc;
640 }
641
642 return 0;
643}
644
645void tcg2_measurement_term(struct udevice *dev, struct tcg2_event_log *elog,
646 bool error)
647{
648 u32 event = error ? 0x1 : 0xffffffff;
649 int i;
650
651 for (i = 0; i < 8; ++i)
652 tcg2_measure_event(dev, elog, i, EV_SEPARATOR, sizeof(event),
653 (const u8 *)&event);
654
655 if (elog->log)
656 unmap_physmem(elog->log, MAP_NOCACHE);
657}
658
659__weak int tcg2_platform_get_log(struct udevice *dev, void **addr, u32 *size)
660{
661 const __be32 *addr_prop;
662 const __be32 *size_prop;
663 int asize;
664 int ssize;
665
666 *addr = NULL;
667 *size = 0;
668
669 addr_prop = dev_read_prop(dev, "tpm_event_log_addr", &asize);
670 if (!addr_prop)
671 addr_prop = dev_read_prop(dev, "linux,sml-base", &asize);
672
673 size_prop = dev_read_prop(dev, "tpm_event_log_size", &ssize);
674 if (!size_prop)
675 size_prop = dev_read_prop(dev, "linux,sml-size", &ssize);
676
677 if (addr_prop && size_prop) {
678 u64 a = of_read_number(addr_prop, asize / sizeof(__be32));
679 u64 s = of_read_number(size_prop, ssize / sizeof(__be32));
680
681 *addr = map_physmem(a, s, MAP_NOCACHE);
682 *size = (u32)s;
683 } else {
684 struct ofnode_phandle_args args;
685 phys_addr_t a;
686 fdt_size_t s;
687
688 if (dev_read_phandle_with_args(dev, "memory-region", NULL, 0,
689 0, &args))
690 return -ENODEV;
691
692 a = ofnode_get_addr_size(args.node, "reg", &s);
693 if (a == FDT_ADDR_T_NONE)
694 return -ENOMEM;
695
696 *addr = map_physmem(a, s, MAP_NOCACHE);
697 *size = (u32)s;
698 }
699
700 return 0;
701}
702
703__weak int tcg2_platform_get_tpm2(struct udevice **dev)
704{
705 for_each_tpm_device(*dev) {
706 if (tpm_get_version(*dev) == TPM_V2)
707 return 0;
708 }
709
710 return -ENODEV;
711}
712
713u32 tcg2_algorithm_to_mask(enum tpm2_algorithms algo)
714{
715 size_t i;
716
717 for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
718 if (hash_algo_list[i].hash_alg == algo)
719 return hash_algo_list[i].hash_mask;
720 }
721
722 return 0;
723}
724
725__weak void tcg2_platform_startup_error(struct udevice *dev, int rc) {}