blob: 4019962a22ae7adccdb8eff5d2a132d5e39c630b [file] [log] [blame]
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +01001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2018 Facebook Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +010016#include <console/console.h>
17#include <security/tpm/tspi.h>
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +010018#include <arch/early_variables.h>
19#include <fmap.h>
20#include <region_file.h>
21#include <string.h>
22#include <security/vboot/symbols.h>
23#include <cbmem.h>
24#include <bootstate.h>
Joel Kitching2c469ad2019-08-06 17:42:45 +080025#include <vb2_sha.h>
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +010026
27static struct tcpa_table *tcpa_cbmem_init(void)
28{
29 MAYBE_STATIC struct tcpa_table *tclt = NULL;
30 if (tclt)
31 return tclt;
32
33 if (cbmem_possibly_online()) {
34 tclt = cbmem_find(CBMEM_ID_TCPA_LOG);
35 if (!tclt) {
36 size_t tcpa_log_len = sizeof(struct tcpa_table) +
37 MAX_TCPA_LOG_ENTRIES * sizeof(struct tcpa_entry);
38 tclt = cbmem_add(CBMEM_ID_TCPA_LOG, tcpa_log_len);
39 if (tclt) {
40 tclt->max_entries = MAX_TCPA_LOG_ENTRIES;
41 tclt->num_entries = 0;
42 }
43 }
44 }
45 return tclt;
46}
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +010047
Philipp Deppenwiesebce49c22018-08-01 06:26:00 +020048static struct tcpa_table *tcpa_log_init(void)
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +010049{
Philipp Deppenwiesebce49c22018-08-01 06:26:00 +020050 MAYBE_STATIC struct tcpa_table *tclt = NULL;
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +010051
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +010052 /* We are dealing here with pre CBMEM environment.
53 * If cbmem isn't available use CAR or SRAM */
54 if (!cbmem_possibly_online() &&
Julius Wernercd49cce2019-03-05 16:53:33 -080055 !CONFIG(VBOOT_RETURN_FROM_VERSTAGE))
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +010056 return (struct tcpa_table *)_vboot2_tpm_log;
57 else if (ENV_ROMSTAGE &&
Julius Wernercd49cce2019-03-05 16:53:33 -080058 !CONFIG(VBOOT_RETURN_FROM_VERSTAGE)) {
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +010059 tclt = tcpa_cbmem_init();
60 if (!tclt)
61 return (struct tcpa_table *)_vboot2_tpm_log;
62 } else {
63 tclt = tcpa_cbmem_init();
Philipp Deppenwiesebce49c22018-08-01 06:26:00 +020064 }
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +010065
Philipp Deppenwiesebce49c22018-08-01 06:26:00 +020066 return tclt;
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +010067}
68
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +010069void tcpa_log_dump(void *unused)
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +010070{
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +010071 int i, j;
Philipp Deppenwiesebce49c22018-08-01 06:26:00 +020072 struct tcpa_table *tclt;
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +010073
Philipp Deppenwiesebce49c22018-08-01 06:26:00 +020074 tclt = tcpa_log_init();
Furquan Shaikhc49ab452018-08-07 09:57:40 -070075 if (!tclt)
76 return;
77
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +010078 printk(BIOS_INFO, "coreboot TCPA measurements:\n\n");
79 for (i = 0; i < tclt->num_entries; i++) {
80 struct tcpa_entry *tce = &tclt->entries[i];
81 if (tce) {
82 printk(BIOS_INFO, " PCR-%u ", tce->pcr);
83
84 for (j = 0; j < tce->digest_length; j++)
85 printk(BIOS_INFO, "%02x", tce->digest[j]);
86
87 printk(BIOS_INFO, " %s [%s]\n",
88 tce->digest_type, tce->name);
89 }
90 }
91 printk(BIOS_INFO, "\n");
92}
93
94void tcpa_log_add_table_entry(const char *name, const uint32_t pcr,
95 enum vb2_hash_algorithm digest_algo,
96 const uint8_t *digest,
97 const size_t digest_len)
98{
99 struct tcpa_table *tclt = tcpa_log_init();
100 if (!tclt) {
101 printk(BIOS_WARNING, "TCPA: Log non-existent!\n");
Furquan Shaikh38f3ffa2018-07-31 14:26:39 -0700102 return;
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +0100103 }
104
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100105 if (tclt->num_entries >= tclt->max_entries) {
106 printk(BIOS_WARNING, "TCPA: TCPA log table is full\n");
107 return;
108 }
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +0100109
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100110 if (!name) {
111 printk(BIOS_WARNING, "TCPA: TCPA entry name not set\n");
112 return;
113 }
114
115 struct tcpa_entry *tce = &tclt->entries[tclt->num_entries++];
Philipp Deppenwiesebce49c22018-08-01 06:26:00 +0200116 strncpy(tce->name, name, TCPA_PCR_HASH_NAME - 1);
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +0100117 tce->pcr = pcr;
Philipp Deppenwiesebce49c22018-08-01 06:26:00 +0200118
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100119 if (digest_len > TCPA_DIGEST_MAX_LENGTH) {
120 printk(BIOS_WARNING, "TCPA: PCR digest too long for TCPA log entry\n");
Philipp Deppenwiesebce49c22018-08-01 06:26:00 +0200121 return;
122 }
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100123
124 strncpy(tce->digest_type,
125 vb2_get_hash_algorithm_name(digest_algo),
126 TCPA_PCR_HASH_LEN - 1);
127 tce->digest_length = digest_len;
128 memcpy(tce->digest, digest, tce->digest_length);
Philipp Deppenwiesef18dc5c2017-12-14 15:49:32 +0100129}
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100130
131void tcpa_preram_log_clear(void)
132{
133 printk(BIOS_INFO, "TCPA: Clearing coreboot TCPA log\n");
134 struct tcpa_table *tclt = (struct tcpa_table *)_vboot2_tpm_log;
135 tclt->max_entries = MAX_TCPA_LOG_ENTRIES;
136 tclt->num_entries = 0;
137}
138
Julius Wernercd49cce2019-03-05 16:53:33 -0800139#if !CONFIG(VBOOT_RETURN_FROM_VERSTAGE)
Philipp Deppenwiesec9b7d1f2018-11-10 00:35:02 +0100140static void recover_tcpa_log(int is_recovery)
141{
142 struct tcpa_table *preram_log = (struct tcpa_table *)_vboot2_tpm_log;
143 struct tcpa_table *ram_log = NULL;
144 int i;
145
146 if (preram_log->num_entries > MAX_PRERAM_TCPA_LOG_ENTRIES) {
147 printk(BIOS_WARNING, "TCPA: Pre-RAM TCPA log is too full, possible corruption\n");
148 return;
149 }
150
151 ram_log = tcpa_cbmem_init();
152 if (!ram_log) {
153 printk(BIOS_WARNING, "TCPA: CBMEM not available something went wrong\n");
154 return;
155 }
156
157 for (i = 0; i < preram_log->num_entries; i++) {
158 struct tcpa_entry *tce = &ram_log->entries[ram_log->num_entries++];
159 strncpy(tce->name, preram_log->entries[i].name, TCPA_PCR_HASH_NAME - 1);
160 tce->pcr = preram_log->entries[i].pcr;
161
162 if (preram_log->entries[i].digest_length > TCPA_DIGEST_MAX_LENGTH) {
163 printk(BIOS_WARNING, "TCPA: PCR digest too long for TCPA log entry\n");
164 return;
165 }
166
167 strncpy(tce->digest_type, preram_log->entries[i].digest_type, TCPA_PCR_HASH_LEN - 1);
168 tce->digest_length = MIN(preram_log->entries[i].digest_length, TCPA_DIGEST_MAX_LENGTH);
169 memcpy(tce->digest, preram_log->entries[i].digest, tce->digest_length);
170 }
171}
172ROMSTAGE_CBMEM_INIT_HOOK(recover_tcpa_log);
173#endif
174
175BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY, tcpa_log_dump, NULL);