blob: 6c72688b8067c1fd8036a61910e6c46756ff59dd [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;
362 u16 j;
363
364 if (elog->log_size <= offsetof(struct tcg_pcr_event, event))
365 return 0;
366
367 log = (struct tcg_pcr_event *)elog->log;
368 if (get_unaligned_le32(&log->pcr_index) != 0 ||
369 get_unaligned_le32(&log->event_type) != EV_NO_ACTION)
370 return 0;
371
372 for (i = 0; i < sizeof(log->digest); i++) {
373 if (log->digest[i])
374 return 0;
375 }
376
377 evsz = get_unaligned_le32(&log->event_size);
378 if (evsz < offsetof(struct tcg_efi_spec_id_event, digest_sizes) ||
379 evsz + offsetof(struct tcg_pcr_event, event) > elog->log_size)
380 return 0;
381
382 event = (struct tcg_efi_spec_id_event *)log->event;
383 if (memcmp(event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03,
384 sizeof(TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03)))
385 return 0;
386
387 if (event->spec_version_minor != TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 ||
388 event->spec_version_major != TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2)
389 return 0;
390
391 count = get_unaligned_le32(&event->number_of_algorithms);
392 if (count > ARRAY_SIZE(hash_algo_list))
393 return 0;
394
395 calc_size = offsetof(struct tcg_efi_spec_id_event, digest_sizes) +
396 (sizeof(struct tcg_efi_spec_id_event_algorithm_size) * count) +
397 1;
398 if (evsz != calc_size)
399 return 0;
400
401 rc = tcg2_get_active_pcr_banks(dev, &active);
402 if (rc)
403 return rc;
404
405 digest_list.count = 0;
406 log_active = 0;
407
408 for (i = 0; i < count; ++i) {
409 algo = get_unaligned_le16(&event->digest_sizes[i].algorithm_id);
410 mask = tcg2_algorithm_to_mask(algo);
411
412 if (!(active & mask))
413 return 0;
414
415 switch (algo) {
416 case TPM2_ALG_SHA1:
417 case TPM2_ALG_SHA256:
418 case TPM2_ALG_SHA384:
419 case TPM2_ALG_SHA512:
420 len = get_unaligned_le16(&event->digest_sizes[i].digest_size);
421 if (tpm2_algorithm_to_len(algo) != len)
422 return 0;
423 digest_list.digests[digest_list.count++].hash_alg = algo;
424 break;
425 default:
426 return 0;
427 }
428
429 log_active |= mask;
430 }
431
432 /* Ensure the previous firmware extended all the PCRs. */
433 if (log_active != active)
434 return 0;
435
436 /* Read PCR0 to check if previous firmware extended the PCRs or not. */
437 rc = tcg2_pcr_read(dev, 0, &digest_list);
438 if (rc)
439 return rc;
440
441 for (i = 0; i < digest_list.count; ++i) {
442 len = tpm2_algorithm_to_len(digest_list.digests[i].hash_alg);
443 for (j = 0; j < len; ++j) {
444 if (digest_list.digests[i].digest.sha512[j])
445 break;
446 }
447
448 /* PCR is non-zero; it has been extended, so skip extending. */
449 if (j != len) {
450 digest_list.count = 0;
451 break;
452 }
453 }
454
455 return tcg2_replay_eventlog(elog, dev, &digest_list,
456 offsetof(struct tcg_pcr_event, event) +
457 evsz);
458}
459
460int tcg2_pcr_extend(struct udevice *dev, u32 pcr_index,
461 struct tpml_digest_values *digest_list)
462{
463 u32 rc;
464 u32 i;
465
466 for (i = 0; i < digest_list->count; i++) {
467 u32 alg = digest_list->digests[i].hash_alg;
468
469 rc = tpm2_pcr_extend(dev, pcr_index, alg,
470 (u8 *)&digest_list->digests[i].digest,
471 tpm2_algorithm_to_len(alg));
472 if (rc) {
473 printf("%s: error pcr:%u alg:%08x\n", __func__,
474 pcr_index, alg);
475 return rc;
476 }
477 }
478
479 return 0;
480}
481
482int tcg2_pcr_read(struct udevice *dev, u32 pcr_index,
483 struct tpml_digest_values *digest_list)
484{
485 struct tpm_chip_priv *priv;
486 u32 rc;
487 u32 i;
488
489 priv = dev_get_uclass_priv(dev);
490 if (!priv)
491 return -ENODEV;
492
493 for (i = 0; i < digest_list->count; i++) {
494 u32 alg = digest_list->digests[i].hash_alg;
495 u8 *digest = (u8 *)&digest_list->digests[i].digest;
496
497 rc = tpm2_pcr_read(dev, pcr_index, priv->pcr_select_min, alg,
498 digest, tpm2_algorithm_to_len(alg), NULL);
499 if (rc) {
500 printf("%s: error pcr:%u alg:%08x\n", __func__,
501 pcr_index, alg);
502 return rc;
503 }
504 }
505
506 return 0;
507}
508
509int tcg2_measure_data(struct udevice *dev, struct tcg2_event_log *elog,
510 u32 pcr_index, u32 size, const u8 *data, u32 event_type,
511 u32 event_size, const u8 *event)
512{
513 struct tpml_digest_values digest_list;
514 int rc;
515
516 if (data)
517 rc = tcg2_create_digest(dev, data, size, &digest_list);
518 else
519 rc = tcg2_create_digest(dev, event, event_size, &digest_list);
520 if (rc)
521 return rc;
522
523 rc = tcg2_pcr_extend(dev, pcr_index, &digest_list);
524 if (rc)
525 return rc;
526
527 return tcg2_log_append_check(elog, pcr_index, event_type, &digest_list,
528 event_size, event);
529}
530
531int tcg2_log_prepare_buffer(struct udevice *dev, struct tcg2_event_log *elog,
532 bool ignore_existing_log)
533{
534 struct tcg2_event_log log;
Ilias Apalodimas7b1e5222024-12-24 08:01:08 -0800535 int rc, i;
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300536
537 elog->log_position = 0;
538 elog->found = false;
539
Ilias Apalodimas7b1e5222024-12-24 08:01:08 -0800540 /*
541 * Make sure U-Boot is compiled with all the active PCRs
542 * since we are about to create an EventLog and we won't
543 * measure anything if the PCR banks don't match
544 */
545 if (!tpm2_check_active_banks(dev)) {
546 log_err("Cannot create EventLog\n");
547 log_err("Mismatch between U-Boot and TPM hash algos\n");
548 log_info("TPM:\n");
549 tpm2_print_active_banks(dev);
550 log_info("U-Boot:\n");
551 for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
552 const struct digest_info *algo = &hash_algo_list[i];
553 const char *str;
554
555 if (!algo->supported)
556 continue;
557
558 str = tpm2_algorithm_name(algo->hash_alg);
559 if (str)
560 log_info("%s\n", str);
561 }
562 return -EINVAL;
563 }
564
Ilias Apalodimas95537a62024-06-23 14:48:15 +0300565 rc = tcg2_platform_get_log(dev, (void **)&log.log, &log.log_size);
566 if (!rc) {
567 log.log_position = 0;
568 log.found = false;
569
570 if (!ignore_existing_log) {
571 rc = tcg2_log_parse(dev, &log);
572 if (rc)
573 return rc;
574 }
575
576 if (elog->log_size) {
577 if (log.found) {
578 if (elog->log_size < log.log_position)
579 return -ENOBUFS;
580
581 /*
582 * Copy the discovered log into the user buffer
583 * if there's enough space.
584 */
585 memcpy(elog->log, log.log, log.log_position);
586 }
587
588 unmap_physmem(log.log, MAP_NOCACHE);
589 } else {
590 elog->log = log.log;
591 elog->log_size = log.log_size;
592 }
593
594 elog->log_position = log.log_position;
595 elog->found = log.found;
596 }
597
598 /*
599 * Initialize the log buffer if no log was discovered and the buffer is
600 * valid. User's can pass in their own buffer as a fallback if no
601 * memory region is found.
602 */
603 if (!elog->found && elog->log_size)
604 rc = tcg2_log_init(dev, elog);
605
606 return rc;
607}
608
609int tcg2_measurement_init(struct udevice **dev, struct tcg2_event_log *elog,
610 bool ignore_existing_log)
611{
612 int rc;
613
614 rc = tcg2_platform_get_tpm2(dev);
615 if (rc)
616 return rc;
617
618 rc = tpm_auto_start(*dev);
619 if (rc)
620 return rc;
621
622 rc = tcg2_log_prepare_buffer(*dev, elog, ignore_existing_log);
623 if (rc) {
624 tcg2_measurement_term(*dev, elog, true);
625 return rc;
626 }
627
628 rc = tcg2_measure_event(*dev, elog, 0, EV_S_CRTM_VERSION,
629 strlen(version_string) + 1,
630 (u8 *)version_string);
631 if (rc) {
632 tcg2_measurement_term(*dev, elog, true);
633 return rc;
634 }
635
636 return 0;
637}
638
639void tcg2_measurement_term(struct udevice *dev, struct tcg2_event_log *elog,
640 bool error)
641{
642 u32 event = error ? 0x1 : 0xffffffff;
643 int i;
644
645 for (i = 0; i < 8; ++i)
646 tcg2_measure_event(dev, elog, i, EV_SEPARATOR, sizeof(event),
647 (const u8 *)&event);
648
649 if (elog->log)
650 unmap_physmem(elog->log, MAP_NOCACHE);
651}
652
653__weak int tcg2_platform_get_log(struct udevice *dev, void **addr, u32 *size)
654{
655 const __be32 *addr_prop;
656 const __be32 *size_prop;
657 int asize;
658 int ssize;
659
660 *addr = NULL;
661 *size = 0;
662
663 addr_prop = dev_read_prop(dev, "tpm_event_log_addr", &asize);
664 if (!addr_prop)
665 addr_prop = dev_read_prop(dev, "linux,sml-base", &asize);
666
667 size_prop = dev_read_prop(dev, "tpm_event_log_size", &ssize);
668 if (!size_prop)
669 size_prop = dev_read_prop(dev, "linux,sml-size", &ssize);
670
671 if (addr_prop && size_prop) {
672 u64 a = of_read_number(addr_prop, asize / sizeof(__be32));
673 u64 s = of_read_number(size_prop, ssize / sizeof(__be32));
674
675 *addr = map_physmem(a, s, MAP_NOCACHE);
676 *size = (u32)s;
677 } else {
678 struct ofnode_phandle_args args;
679 phys_addr_t a;
680 fdt_size_t s;
681
682 if (dev_read_phandle_with_args(dev, "memory-region", NULL, 0,
683 0, &args))
684 return -ENODEV;
685
686 a = ofnode_get_addr_size(args.node, "reg", &s);
687 if (a == FDT_ADDR_T_NONE)
688 return -ENOMEM;
689
690 *addr = map_physmem(a, s, MAP_NOCACHE);
691 *size = (u32)s;
692 }
693
694 return 0;
695}
696
697__weak int tcg2_platform_get_tpm2(struct udevice **dev)
698{
699 for_each_tpm_device(*dev) {
700 if (tpm_get_version(*dev) == TPM_V2)
701 return 0;
702 }
703
704 return -ENODEV;
705}
706
707u32 tcg2_algorithm_to_mask(enum tpm2_algorithms algo)
708{
709 size_t i;
710
711 for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) {
712 if (hash_algo_list[i].hash_alg == algo)
713 return hash_algo_list[i].hash_mask;
714 }
715
716 return 0;
717}
718
719__weak void tcg2_platform_startup_error(struct udevice *dev, int rc) {}