blob: 9e63204f24c84996b5841e505e2a76f82e81021c [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{
210 struct tcg_efi_spec_id_event *ev;
211 struct tcg_pcr_event *log;
212 u32 event_size;
213 u32 count = 0;
214 u32 log_size;
215 u32 active;
216 size_t i;
217 u16 len;
218 int rc;
219
220 rc = tcg2_get_active_pcr_banks(dev, &active);
221 if (rc)
222 return rc;
223
224 event_size = offsetof(struct tcg_efi_spec_id_event, digest_sizes);
225 for (i = 0; i < ARRAY_SIZE(hash_algo_list); ++i) {
226 if (!(active & hash_algo_list[i].hash_mask))
227 continue;
228
229 switch (hash_algo_list[i].hash_alg) {
230 case TPM2_ALG_SHA1:
231 case TPM2_ALG_SHA256:
232 case TPM2_ALG_SHA384:
233 case TPM2_ALG_SHA512:
234 count++;
235 break;
236 default:
237 continue;
238 }
239 }
240
241 event_size += 1 +
242 (sizeof(struct tcg_efi_spec_id_event_algorithm_size) * count);
243 log_size = offsetof(struct tcg_pcr_event, event) + event_size;
244
245 if (log_size > elog->log_size) {
246 printf("%s: log too large: %u > %u\n", __func__, log_size,
247 elog->log_size);
248 return -ENOBUFS;
249 }
250
251 log = (struct tcg_pcr_event *)elog->log;
252 put_unaligned_le32(0, &log->pcr_index);
253 put_unaligned_le32(EV_NO_ACTION, &log->event_type);
254 memset(&log->digest, 0, sizeof(log->digest));
255 put_unaligned_le32(event_size, &log->event_size);
256
257 ev = (struct tcg_efi_spec_id_event *)log->event;
258 strlcpy((char *)ev->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
259 sizeof(ev->signature));
260 put_unaligned_le32(0, &ev->platform_class);
261 ev->spec_version_minor = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2;
262 ev->spec_version_major = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2;
263 ev->spec_errata = TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2;
264 ev->uintn_size = sizeof(size_t) / sizeof(u32);
265 put_unaligned_le32(count, &ev->number_of_algorithms);
266
267 count = 0;
268 for (i = 0; i < ARRAY_SIZE(hash_algo_list); ++i) {
269 if (!(active & hash_algo_list[i].hash_mask))
270 continue;
271
272 len = hash_algo_list[i].hash_len;
273 if (!len)
274 continue;
275
276 put_unaligned_le16(hash_algo_list[i].hash_alg,
277 &ev->digest_sizes[count].algorithm_id);
278 put_unaligned_le16(len, &ev->digest_sizes[count].digest_size);
279 count++;
280 }
281
282 *((u8 *)ev + (event_size - 1)) = 0;
283 elog->log_position = log_size;
284
285 return 0;
286}
287
288static int tcg2_replay_eventlog(struct tcg2_event_log *elog,
289 struct udevice *dev,
290 struct tpml_digest_values *digest_list,
291 u32 log_position)
292{
293 const u32 offset = offsetof(struct tcg_pcr_event2, digests) +
294 offsetof(struct tpml_digest_values, digests);
295 u32 event_size;
296 u32 count;
297 u16 algo;
298 u32 pcr;
299 u32 pos;
300 u16 len;
301 u8 *log;
302 int rc;
303 u32 i;
304
305 while (log_position + offset < elog->log_size) {
306 log = elog->log + log_position;
307
308 pos = offsetof(struct tcg_pcr_event2, pcr_index);
309 pcr = get_unaligned_le32(log + pos);
310 pos = offsetof(struct tcg_pcr_event2, event_type);
311 if (!get_unaligned_le32(log + pos))
312 return 0;
313
314 pos = offsetof(struct tcg_pcr_event2, digests) +
315 offsetof(struct tpml_digest_values, count);
316 count = get_unaligned_le32(log + pos);
317 if (count > ARRAY_SIZE(hash_algo_list) ||
318 (digest_list->count && digest_list->count != count))
319 return 0;
320
321 pos = offsetof(struct tcg_pcr_event2, digests) +
322 offsetof(struct tpml_digest_values, digests);
323 for (i = 0; i < count; ++i) {
324 pos += offsetof(struct tpmt_ha, hash_alg);
325 if (log_position + pos + sizeof(u16) >= elog->log_size)
326 return 0;
327
328 algo = get_unaligned_le16(log + pos);
329 pos += offsetof(struct tpmt_ha, digest);
330 switch (algo) {
331 case TPM2_ALG_SHA1:
332 case TPM2_ALG_SHA256:
333 case TPM2_ALG_SHA384:
334 case TPM2_ALG_SHA512:
335 len = tpm2_algorithm_to_len(algo);
336 break;
337 default:
338 return 0;
339 }
340
341 if (digest_list->count) {
342 if (algo != digest_list->digests[i].hash_alg ||
343 log_position + pos + len >= elog->log_size)
344 return 0;
345
346 memcpy(digest_list->digests[i].digest.sha512,
347 log + pos, len);
348 }
349
350 pos += len;
351 }
352
353 if (log_position + pos + sizeof(u32) >= elog->log_size)
354 return 0;
355
356 event_size = get_unaligned_le32(log + pos);
357 pos += event_size + sizeof(u32);
358 if (log_position + pos > elog->log_size)
359 return 0;
360
361 if (digest_list->count) {
362 rc = tcg2_pcr_extend(dev, pcr, digest_list);
363 if (rc)
364 return rc;
365 }
366
367 log_position += pos;
368 }
369
370 elog->log_position = log_position;
371 elog->found = true;
372 return 0;
373}
374
375static int tcg2_log_parse(struct udevice *dev, struct tcg2_event_log *elog)
376{
377 struct tpml_digest_values digest_list;
378 struct tcg_efi_spec_id_event *event;
379 struct tcg_pcr_event *log;
380 u32 log_active;
381 u32 calc_size;
382 u32 active;
383 u32 count;
384 u32 evsz;
385 u32 mask;
386 u16 algo;
387 u16 len;
388 int rc;
389 u32 i;
390 u16 j;
391
392 if (elog->log_size <= offsetof(struct tcg_pcr_event, event))
393 return 0;
394
395 log = (struct tcg_pcr_event *)elog->log;
396 if (get_unaligned_le32(&log->pcr_index) != 0 ||
397 get_unaligned_le32(&log->event_type) != EV_NO_ACTION)
398 return 0;
399
400 for (i = 0; i < sizeof(log->digest); i++) {
401 if (log->digest[i])
402 return 0;
403 }
404
405 evsz = get_unaligned_le32(&log->event_size);
406 if (evsz < offsetof(struct tcg_efi_spec_id_event, digest_sizes) ||
407 evsz + offsetof(struct tcg_pcr_event, event) > elog->log_size)
408 return 0;
409
410 event = (struct tcg_efi_spec_id_event *)log->event;
411 if (memcmp(event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
412 sizeof(TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03)))
413 return 0;
414
415 if (event->spec_version_minor != TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 ||
416 event->spec_version_major != TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2)
417 return 0;
418
419 count = get_unaligned_le32(&event->number_of_algorithms);
420 if (count > ARRAY_SIZE(hash_algo_list))
421 return 0;
422
423 calc_size = offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
424 (sizeof(struct tcg_efi_spec_id_event_algorithm_size) * count) +
425 1;
426 if (evsz != calc_size)
427 return 0;
428
429 rc = tcg2_get_active_pcr_banks(dev, &active);
430 if (rc)
431 return rc;
432
433 digest_list.count = 0;
434 log_active = 0;
435
436 for (i = 0; i < count; ++i) {
437 algo = get_unaligned_le16(&event->digest_sizes[i].algorithm_id);
438 mask = tcg2_algorithm_to_mask(algo);
439
440 if (!(active & mask))
441 return 0;
442
443 switch (algo) {
444 case TPM2_ALG_SHA1:
445 case TPM2_ALG_SHA256:
446 case TPM2_ALG_SHA384:
447 case TPM2_ALG_SHA512:
448 len = get_unaligned_le16(&event->digest_sizes[i].digest_size);
449 if (tpm2_algorithm_to_len(algo) != len)
450 return 0;
451 digest_list.digests[digest_list.count++].hash_alg = algo;
452 break;
453 default:
454 return 0;
455 }
456
457 log_active |= mask;
458 }
459
460 /* Ensure the previous firmware extended all the PCRs. */
461 if (log_active != active)
462 return 0;
463
464 /* Read PCR0 to check if previous firmware extended the PCRs or not. */
465 rc = tcg2_pcr_read(dev, 0, &digest_list);
466 if (rc)
467 return rc;
468
469 for (i = 0; i < digest_list.count; ++i) {
470 len = tpm2_algorithm_to_len(digest_list.digests[i].hash_alg);
471 for (j = 0; j < len; ++j) {
472 if (digest_list.digests[i].digest.sha512[j])
473 break;
474 }
475
476 /* PCR is non-zero; it has been extended, so skip extending. */
477 if (j != len) {
478 digest_list.count = 0;
479 break;
480 }
481 }
482
483 return tcg2_replay_eventlog(elog, dev, &digest_list,
484 offsetof(struct tcg_pcr_event, event) +
485 evsz);
486}
487
488int tcg2_pcr_extend(struct udevice *dev, u32 pcr_index,
489 struct tpml_digest_values *digest_list)
490{
491 u32 rc;
492 u32 i;
493
494 for (i = 0; i < digest_list->count; i++) {
495 u32 alg = digest_list->digests[i].hash_alg;
496
497 rc = tpm2_pcr_extend(dev, pcr_index, alg,
498 (u8 *)&digest_list->digests[i].digest,
499 tpm2_algorithm_to_len(alg));
500 if (rc) {
501 printf("%s: error pcr:%u alg:%08x\n", __func__,
502 pcr_index, alg);
503 return rc;
504 }
505 }
506
507 return 0;
508}
509
510int tcg2_pcr_read(struct udevice *dev, u32 pcr_index,
511 struct tpml_digest_values *digest_list)
512{
513 struct tpm_chip_priv *priv;
514 u32 rc;
515 u32 i;
516
517 priv = dev_get_uclass_priv(dev);
518 if (!priv)
519 return -ENODEV;
520
521 for (i = 0; i < digest_list->count; i++) {
522 u32 alg = digest_list->digests[i].hash_alg;
523 u8 *digest = (u8 *)&digest_list->digests[i].digest;
524
525 rc = tpm2_pcr_read(dev, pcr_index, priv->pcr_select_min, alg,
526 digest, tpm2_algorithm_to_len(alg), NULL);
527 if (rc) {
528 printf("%s: error pcr:%u alg:%08x\n", __func__,
529 pcr_index, alg);
530 return rc;
531 }
532 }
533
534 return 0;
535}
536
537int tcg2_measure_data(struct udevice *dev, struct tcg2_event_log *elog,
538 u32 pcr_index, u32 size, const u8 *data, u32 event_type,
539 u32 event_size, const u8 *event)
540{
541 struct tpml_digest_values digest_list;
542 int rc;
543
544 if (data)
545 rc = tcg2_create_digest(dev, data, size, &digest_list);
546 else
547 rc = tcg2_create_digest(dev, event, event_size, &digest_list);
548 if (rc)
549 return rc;
550
551 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
552 if (rc)
553 return rc;
554
555 return tcg2_log_append_check(elog, pcr_index, event_type, &digest_list,
556 event_size, event);
557}
558
559int tcg2_log_prepare_buffer(struct udevice *dev, struct tcg2_event_log *elog,
560 bool ignore_existing_log)
561{
562 struct tcg2_event_log log;
Ilias Apalodimas7b1e5222024-12-24 08:01:08 -0800563 int rc, i;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300564
565 elog->log_position = 0;
566 elog->found = false;
567
Ilias Apalodimas7b1e5222024-12-24 08:01:08 -0800568 /*
569 * Make sure U-Boot is compiled with all the active PCRs
570 * since we are about to create an EventLog and we won't
571 * measure anything if the PCR banks don't match
572 */
573 if (!tpm2_check_active_banks(dev)) {
574 log_err("Cannot create EventLog\n");
575 log_err("Mismatch between U-Boot and TPM hash algos\n");
576 log_info("TPM:\n");
577 tpm2_print_active_banks(dev);
578 log_info("U-Boot:\n");
579 for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
580 const struct digest_info *algo = &hash_algo_list[i];
581 const char *str;
582
583 if (!algo->supported)
584 continue;
585
586 str = tpm2_algorithm_name(algo->hash_alg);
587 if (str)
588 log_info("%s\n", str);
589 }
590 return -EINVAL;
591 }
592
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300593 rc = tcg2_platform_get_log(dev, (void **)&log.log, &log.log_size);
594 if (!rc) {
595 log.log_position = 0;
596 log.found = false;
597
598 if (!ignore_existing_log) {
599 rc = tcg2_log_parse(dev, &log);
600 if (rc)
601 return rc;
602 }
603
604 if (elog->log_size) {
605 if (log.found) {
606 if (elog->log_size < log.log_position)
607 return -ENOBUFS;
608
609 /*
610 * Copy the discovered log into the user buffer
611 * if there's enough space.
612 */
613 memcpy(elog->log, log.log, log.log_position);
614 }
615
616 unmap_physmem(log.log, MAP_NOCACHE);
617 } else {
618 elog->log = log.log;
619 elog->log_size = log.log_size;
620 }
621
622 elog->log_position = log.log_position;
623 elog->found = log.found;
624 }
625
626 /*
627 * Initialize the log buffer if no log was discovered and the buffer is
628 * valid. User's can pass in their own buffer as a fallback if no
629 * memory region is found.
630 */
631 if (!elog->found && elog->log_size)
632 rc = tcg2_log_init(dev, elog);
633
634 return rc;
635}
636
637int tcg2_measurement_init(struct udevice **dev, struct tcg2_event_log *elog,
638 bool ignore_existing_log)
639{
640 int rc;
641
642 rc = tcg2_platform_get_tpm2(dev);
643 if (rc)
644 return rc;
645
646 rc = tpm_auto_start(*dev);
647 if (rc)
648 return rc;
649
650 rc = tcg2_log_prepare_buffer(*dev, elog, ignore_existing_log);
651 if (rc) {
652 tcg2_measurement_term(*dev, elog, true);
653 return rc;
654 }
655
656 rc = tcg2_measure_event(*dev, elog, 0, EV_S_CRTM_VERSION,
657 strlen(version_string) + 1,
658 (u8 *)version_string);
659 if (rc) {
660 tcg2_measurement_term(*dev, elog, true);
661 return rc;
662 }
663
664 return 0;
665}
666
667void tcg2_measurement_term(struct udevice *dev, struct tcg2_event_log *elog,
668 bool error)
669{
670 u32 event = error ? 0x1 : 0xffffffff;
671 int i;
672
673 for (i = 0; i < 8; ++i)
674 tcg2_measure_event(dev, elog, i, EV_SEPARATOR, sizeof(event),
675 (const u8 *)&event);
676
677 if (elog->log)
678 unmap_physmem(elog->log, MAP_NOCACHE);
679}
680
681__weak int tcg2_platform_get_log(struct udevice *dev, void **addr, u32 *size)
682{
683 const __be32 *addr_prop;
684 const __be32 *size_prop;
685 int asize;
686 int ssize;
687
688 *addr = NULL;
689 *size = 0;
690
691 addr_prop = dev_read_prop(dev, "tpm_event_log_addr", &asize);
692 if (!addr_prop)
693 addr_prop = dev_read_prop(dev, "linux,sml-base", &asize);
694
695 size_prop = dev_read_prop(dev, "tpm_event_log_size", &ssize);
696 if (!size_prop)
697 size_prop = dev_read_prop(dev, "linux,sml-size", &ssize);
698
699 if (addr_prop && size_prop) {
700 u64 a = of_read_number(addr_prop, asize / sizeof(__be32));
701 u64 s = of_read_number(size_prop, ssize / sizeof(__be32));
702
703 *addr = map_physmem(a, s, MAP_NOCACHE);
704 *size = (u32)s;
705 } else {
706 struct ofnode_phandle_args args;
707 phys_addr_t a;
708 fdt_size_t s;
709
710 if (dev_read_phandle_with_args(dev, "memory-region", NULL, 0,
711 0, &args))
712 return -ENODEV;
713
714 a = ofnode_get_addr_size(args.node, "reg", &s);
715 if (a == FDT_ADDR_T_NONE)
716 return -ENOMEM;
717
718 *addr = map_physmem(a, s, MAP_NOCACHE);
719 *size = (u32)s;
720 }
721
722 return 0;
723}
724
725__weak int tcg2_platform_get_tpm2(struct udevice **dev)
726{
727 for_each_tpm_device(*dev) {
728 if (tpm_get_version(*dev) == TPM_V2)
729 return 0;
730 }
731
732 return -ENODEV;
733}
734
735u32 tcg2_algorithm_to_mask(enum tpm2_algorithms algo)
736{
737 size_t i;
738
739 for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
740 if (hash_algo_list[i].hash_alg == algo)
741 return hash_algo_list[i].hash_mask;
742 }
743
744 return 0;
745}
746
747__weak void tcg2_platform_startup_error(struct udevice *dev, int rc) {}