Philipp Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 1 | /* |
| 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 Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 16 | #include <console/console.h> |
| 17 | #include <security/tpm/tspi.h> |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 18 | #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 Kitching | 2c469ad | 2019-08-06 17:42:45 +0800 | [diff] [blame] | 25 | #include <vb2_sha.h> |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 26 | |
| 27 | static 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 Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 47 | |
Philipp Deppenwiese | bce49c2 | 2018-08-01 06:26:00 +0200 | [diff] [blame] | 48 | static struct tcpa_table *tcpa_log_init(void) |
Philipp Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 49 | { |
Philipp Deppenwiese | bce49c2 | 2018-08-01 06:26:00 +0200 | [diff] [blame] | 50 | MAYBE_STATIC struct tcpa_table *tclt = NULL; |
Philipp Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 51 | |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 52 | /* We are dealing here with pre CBMEM environment. |
| 53 | * If cbmem isn't available use CAR or SRAM */ |
| 54 | if (!cbmem_possibly_online() && |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 55 | !CONFIG(VBOOT_RETURN_FROM_VERSTAGE)) |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 56 | return (struct tcpa_table *)_vboot2_tpm_log; |
| 57 | else if (ENV_ROMSTAGE && |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 58 | !CONFIG(VBOOT_RETURN_FROM_VERSTAGE)) { |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 59 | tclt = tcpa_cbmem_init(); |
| 60 | if (!tclt) |
| 61 | return (struct tcpa_table *)_vboot2_tpm_log; |
| 62 | } else { |
| 63 | tclt = tcpa_cbmem_init(); |
Philipp Deppenwiese | bce49c2 | 2018-08-01 06:26:00 +0200 | [diff] [blame] | 64 | } |
Philipp Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 65 | |
Philipp Deppenwiese | bce49c2 | 2018-08-01 06:26:00 +0200 | [diff] [blame] | 66 | return tclt; |
Philipp Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 69 | void tcpa_log_dump(void *unused) |
Philipp Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 70 | { |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 71 | int i, j; |
Philipp Deppenwiese | bce49c2 | 2018-08-01 06:26:00 +0200 | [diff] [blame] | 72 | struct tcpa_table *tclt; |
Philipp Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 73 | |
Philipp Deppenwiese | bce49c2 | 2018-08-01 06:26:00 +0200 | [diff] [blame] | 74 | tclt = tcpa_log_init(); |
Furquan Shaikh | c49ab45 | 2018-08-07 09:57:40 -0700 | [diff] [blame] | 75 | if (!tclt) |
| 76 | return; |
| 77 | |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 78 | 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 | |
| 94 | void 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 Shaikh | 38f3ffa | 2018-07-31 14:26:39 -0700 | [diff] [blame] | 102 | return; |
Philipp Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 105 | if (tclt->num_entries >= tclt->max_entries) { |
| 106 | printk(BIOS_WARNING, "TCPA: TCPA log table is full\n"); |
| 107 | return; |
| 108 | } |
Philipp Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 109 | |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 110 | 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 Deppenwiese | bce49c2 | 2018-08-01 06:26:00 +0200 | [diff] [blame] | 116 | strncpy(tce->name, name, TCPA_PCR_HASH_NAME - 1); |
Philipp Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 117 | tce->pcr = pcr; |
Philipp Deppenwiese | bce49c2 | 2018-08-01 06:26:00 +0200 | [diff] [blame] | 118 | |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 119 | if (digest_len > TCPA_DIGEST_MAX_LENGTH) { |
| 120 | printk(BIOS_WARNING, "TCPA: PCR digest too long for TCPA log entry\n"); |
Philipp Deppenwiese | bce49c2 | 2018-08-01 06:26:00 +0200 | [diff] [blame] | 121 | return; |
| 122 | } |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 123 | |
| 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 Deppenwiese | f18dc5c | 2017-12-14 15:49:32 +0100 | [diff] [blame] | 129 | } |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 130 | |
| 131 | void 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 Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 139 | #if !CONFIG(VBOOT_RETURN_FROM_VERSTAGE) |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 140 | static 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 | } |
| 172 | ROMSTAGE_CBMEM_INIT_HOOK(recover_tcpa_log); |
| 173 | #endif |
| 174 | |
| 175 | BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_BOOT, BS_ON_ENTRY, tcpa_log_dump, NULL); |