1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * Unlike log.c this implements TPM log according to TPM2.0 specification
5 * rather then using coreboot-specific log format.
7 * First entry is in TPM1.2 format and serves as a header, the rest are in
8 * a newer (agile) format which supports SHA256 and multiple hashes, but we
11 * This is defined in "TCG EFI Protocol Specification".
15 #include <console/console.h>
16 #include <security/tpm/tspi.h>
17 #include <security/tpm/tspi/crtm.h>
18 #include <security/tpm/tspi/logs.h>
19 #include <region_file.h>
25 static uint16_t tpmalg_from_vb2_hash(enum vb2_hash_algorithm hash_type
)
31 return TPM2_ALG_SHA256
;
33 return TPM2_ALG_SHA384
;
35 return TPM2_ALG_SHA512
;
42 void *tpm2_log_cbmem_init(void)
44 static struct tpm_2_log_table
*tclt
;
50 struct tcg_efi_spec_id_event
*hdr
;
52 tclt
= cbmem_find(CBMEM_ID_TPM2_TCG_LOG
);
56 tpm_log_len
= sizeof(struct tpm_2_log_table
) +
57 MAX_TPM_LOG_ENTRIES
* sizeof(struct tpm_2_log_entry
);
58 tclt
= cbmem_add(CBMEM_ID_TPM2_TCG_LOG
, tpm_log_len
);
62 memset(tclt
, 0, tpm_log_len
);
65 hdr
->event_type
= htole32(EV_NO_ACTION
);
66 hdr
->event_size
= htole32(33 + sizeof(tclt
->vendor
));
67 strcpy((char *)hdr
->signature
, TPM_20_SPEC_ID_EVENT_SIGNATURE
);
68 hdr
->platform_class
= htole32(0x00); // client platform
69 hdr
->spec_version_minor
= 0x00;
70 hdr
->spec_version_major
= 0x02;
71 hdr
->spec_errata
= 0x00;
72 hdr
->uintn_size
= 0x02; // 64-bit UINT
73 hdr
->num_of_algorithms
= htole32(1);
74 hdr
->digest_sizes
[0].alg_id
= htole16(tpmalg_from_vb2_hash(TPM_MEASURE_ALGO
));
75 hdr
->digest_sizes
[0].digest_size
= htole16(vb2_digest_size(TPM_MEASURE_ALGO
));
77 tclt
->vendor_info_size
= sizeof(tclt
->vendor
);
78 tclt
->vendor
.reserved
= 0;
79 tclt
->vendor
.version_major
= TPM_20_LOG_VI_MAJOR
;
80 tclt
->vendor
.version_minor
= TPM_20_LOG_VI_MINOR
;
81 tclt
->vendor
.magic
= htole32(TPM_20_LOG_VI_MAGIC
);
82 tclt
->vendor
.max_entries
= htole16(MAX_TPM_LOG_ENTRIES
);
83 tclt
->vendor
.num_entries
= htole16(0);
84 tclt
->vendor
.entry_size
= htole32(sizeof(struct tpm_2_log_entry
));
90 void tpm2_log_dump(void)
93 struct tpm_2_log_table
*tclt
;
97 tclt
= tpm_log_init();
101 hash_size
= vb2_digest_size(TPM_MEASURE_ALGO
);
102 alg_name
= vb2_get_hash_algorithm_name(TPM_MEASURE_ALGO
);
104 printk(BIOS_INFO
, "coreboot TPM 2.0 measurements:\n\n");
105 for (i
= 0; i
< le16toh(tclt
->vendor
.num_entries
); i
++) {
106 struct tpm_2_log_entry
*tce
= &tclt
->entries
[i
];
108 printk(BIOS_INFO
, " PCR-%u ", le32toh(tce
->pcr
));
110 for (j
= 0; j
< hash_size
; j
++)
111 printk(BIOS_INFO
, "%02x", tce
->digest
[j
]);
113 printk(BIOS_INFO
, " %s [%s]\n", alg_name
, tce
->data
);
115 printk(BIOS_INFO
, "\n");
118 void tpm2_log_add_table_entry(const char *name
, const uint32_t pcr
,
119 enum vb2_hash_algorithm digest_algo
,
120 const uint8_t *digest
,
121 const size_t digest_len
)
123 struct tpm_2_log_table
*tclt
;
124 struct tpm_2_log_entry
*tce
;
126 tclt
= tpm_log_init();
128 printk(BIOS_WARNING
, "TPM LOG: non-existent!\n");
133 printk(BIOS_WARNING
, "TPM LOG: entry name not set\n");
137 if (digest_algo
!= TPM_MEASURE_ALGO
) {
138 printk(BIOS_WARNING
, "TPM LOG: digest is of unsupported type: %s\n",
139 vb2_get_hash_algorithm_name(digest_algo
));
143 if (digest_len
!= vb2_digest_size(TPM_MEASURE_ALGO
)) {
144 printk(BIOS_WARNING
, "TPM LOG: digest has invalid length: %d\n",
149 if (le16toh(tclt
->vendor
.num_entries
) >= le16toh(tclt
->vendor
.max_entries
)) {
150 printk(BIOS_WARNING
, "TPM LOG: log table is full\n");
154 tce
= &tclt
->entries
[le16toh(tclt
->vendor
.num_entries
)];
155 tclt
->vendor
.num_entries
= htole16(le16toh(tclt
->vendor
.num_entries
) + 1);
157 tce
->pcr
= htole32(pcr
);
158 tce
->event_type
= htole32(EV_ACTION
);
160 tce
->digest_count
= htole32(1);
161 tce
->digest_type
= htole16(tpmalg_from_vb2_hash(TPM_MEASURE_ALGO
));
162 memcpy(tce
->digest
, digest
, vb2_digest_size(TPM_MEASURE_ALGO
));
164 tce
->data_length
= htole32(sizeof(tce
->data
));
165 strncpy((char *)tce
->data
, name
, sizeof(tce
->data
) - 1);
166 tce
->data
[sizeof(tce
->data
) - 1] = '\0';
169 int tpm2_log_get(int entry_idx
, int *pcr
, const uint8_t **digest_data
,
170 enum vb2_hash_algorithm
*digest_algo
, const char **event_name
)
172 struct tpm_2_log_table
*tclt
;
173 struct tpm_2_log_entry
*tce
;
175 tclt
= tpm_log_init();
179 if (entry_idx
< 0 || entry_idx
>= le16toh(tclt
->vendor
.num_entries
))
182 tce
= &tclt
->entries
[entry_idx
];
184 *pcr
= le32toh(tce
->pcr
);
185 *digest_data
= tce
->digest
;
186 *digest_algo
= TPM_MEASURE_ALGO
; /* We validate algorithm on addition */
187 *event_name
= (char *)tce
->data
;
191 uint16_t tpm2_log_get_size(const void *log_table
)
193 const struct tpm_2_log_table
*tclt
= log_table
;
194 return le16toh(tclt
->vendor
.num_entries
);
197 void tpm2_preram_log_clear(void)
199 printk(BIOS_INFO
, "TPM LOG: clearing the log\n");
201 * Pre-RAM log is only for internal use and isn't exported anywhere, hence it's header
202 * is not initialized.
204 struct tpm_2_log_table
*tclt
= (struct tpm_2_log_table
*)_tpm_log
;
205 tclt
->vendor
.max_entries
= htole16(MAX_TPM_LOG_ENTRIES
);
206 tclt
->vendor
.num_entries
= htole16(0);
209 void tpm2_log_copy_entries(const void *from
, void *to
)
211 const struct tpm_2_log_table
*from_log
= from
;
212 struct tpm_2_log_table
*to_log
= to
;
215 for (i
= 0; i
< le16toh(from_log
->vendor
.num_entries
); i
++) {
216 if (le16toh(to_log
->vendor
.num_entries
) >= le16toh(to_log
->vendor
.max_entries
)) {
217 printk(BIOS_WARNING
, "TPM LOG: log table is full\n");
221 struct tpm_2_log_entry
*tce
=
222 &to_log
->entries
[le16toh(to_log
->vendor
.num_entries
)];
223 to_log
->vendor
.num_entries
= htole16(le16toh(to_log
->vendor
.num_entries
) + 1);
225 tce
->pcr
= from_log
->entries
[i
].pcr
;
226 tce
->event_type
= from_log
->entries
[i
].event_type
;
228 tce
->digest_count
= from_log
->entries
[i
].digest_count
;
229 tce
->digest_type
= from_log
->entries
[i
].digest_type
;
230 memcpy(tce
->digest
, from_log
->entries
[i
].digest
, sizeof(tce
->digest
));
232 tce
->data_length
= from_log
->entries
[i
].data_length
;
233 memcpy(tce
->data
, from_log
->entries
[i
].data
, sizeof(tce
->data
));