blob: 1755dd95e56d44b914b6176acfdfd88d3b66de4c [file] [log] [blame]
Alexei Fedorov71d81dc2020-07-13 13:58:06 +01001/*
Sandrine Bailleux4e9af172021-07-01 14:13:09 +02002 * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
Alexei Fedorov71d81dc2020-07-13 13:58:06 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
8#include <errno.h>
9#include <string.h>
10#include <arch_helpers.h>
11
12#include <common/bl_common.h>
13#include <common/debug.h>
14#include <drivers/auth/crypto_mod.h>
Sandrine Bailleux3c2db6f2021-07-07 14:47:08 +020015#include <drivers/measured_boot/event_log/event_log.h>
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010016#include <mbedtls/md.h>
17
18#include <plat/common/platform.h>
19
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010020/* Running Event Log Pointer */
21static uint8_t *log_ptr;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010022
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010023/* Pointer to the first byte past end of the Event Log buffer */
24static uintptr_t log_end;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010025
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010026/* Pointer to event_log_metadata_t */
27static const event_log_metadata_t *plat_metadata_ptr;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010028
29/* TCG_EfiSpecIdEvent */
30static const id_event_headers_t id_event_header = {
31 .header = {
32 .pcr_index = PCR_0,
33 .event_type = EV_NO_ACTION,
34 .digest = {0},
35 .event_size = (uint32_t)(sizeof(id_event_struct_t) +
36 (sizeof(id_event_algorithm_size_t) *
37 HASH_ALG_COUNT))
38 },
39
40 .struct_header = {
41 .signature = TCG_ID_EVENT_SIGNATURE_03,
42 .platform_class = PLATFORM_CLASS_CLIENT,
43 .spec_version_minor = TCG_SPEC_VERSION_MINOR_TPM2,
44 .spec_version_major = TCG_SPEC_VERSION_MAJOR_TPM2,
45 .spec_errata = TCG_SPEC_ERRATA_TPM2,
46 .uintn_size = (uint8_t)(sizeof(unsigned int) /
47 sizeof(uint32_t)),
48 .number_of_algorithms = HASH_ALG_COUNT
49 }
50};
51
52static const event2_header_t locality_event_header = {
Sandrine Bailleuxbe761432021-06-23 10:40:08 +020053 /*
54 * All EV_NO_ACTION events SHALL set
55 * TCG_PCR_EVENT2.pcrIndex = 0, unless otherwise specified
56 */
57 .pcr_index = PCR_0,
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010058
Sandrine Bailleuxbe761432021-06-23 10:40:08 +020059 /*
60 * All EV_NO_ACTION events SHALL set
61 * TCG_PCR_EVENT2.eventType = 03h
62 */
63 .event_type = EV_NO_ACTION,
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010064
Sandrine Bailleuxbe761432021-06-23 10:40:08 +020065 /*
66 * All EV_NO_ACTION events SHALL set TCG_PCR_EVENT2.digests to all
67 * 0x00's for each allocated Hash algorithm
68 */
69 .digests = {
70 .count = HASH_ALG_COUNT
71 }
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010072};
73
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010074/*
Sandrine Bailleux36af1c82021-06-17 15:44:40 +020075 * Record a measurement as a TCG_PCR_EVENT2 event
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010076 *
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010077 * @param[in] hash Pointer to hash data of TCG_DIGEST_SIZE bytes
78 * @param[in] metadata_ptr Pointer to event_log_metadata_t structure
Sandrine Bailleux9ebe81e2021-06-23 15:43:02 +020079 *
80 * There must be room for storing this new event into the event log buffer.
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010081 */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010082static void event_log_record(const uint8_t *hash,
83 const event_log_metadata_t *metadata_ptr)
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010084{
85 void *ptr = log_ptr;
86 uint32_t name_len;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010087
Sandrine Bailleux898c0992021-06-17 16:10:40 +020088 assert(hash != NULL);
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010089 assert(metadata_ptr != NULL);
90 assert(metadata_ptr->name != NULL);
91 /* event_log_init() must have been called prior to this. */
92 assert(log_ptr != NULL);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010093
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010094 name_len = (uint32_t)strlen(metadata_ptr->name) + 1U;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010095
96 /* Check for space in Event Log buffer */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010097 assert(((uintptr_t)ptr + (uint32_t)EVENT2_HDR_SIZE + name_len) <
98 log_end);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010099
100 /*
101 * As per TCG specifications, firmware components that are measured
102 * into PCR[0] must be logged in the event log using the event type
103 * EV_POST_CODE.
104 */
105 /* TCG_PCR_EVENT2.PCRIndex */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100106 ((event2_header_t *)ptr)->pcr_index = metadata_ptr->pcr;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100107
108 /* TCG_PCR_EVENT2.EventType */
109 ((event2_header_t *)ptr)->event_type = EV_POST_CODE;
110
111 /* TCG_PCR_EVENT2.Digests.Count */
112 ptr = (uint8_t *)ptr + offsetof(event2_header_t, digests);
113 ((tpml_digest_values *)ptr)->count = HASH_ALG_COUNT;
114
115 /* TCG_PCR_EVENT2.Digests[] */
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100116 ptr = (uint8_t *)((uintptr_t)ptr +
117 offsetof(tpml_digest_values, digests));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100118
119 /* TCG_PCR_EVENT2.Digests[].AlgorithmId */
120 ((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID;
121
122 /* TCG_PCR_EVENT2.Digests[].Digest[] */
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100123 ptr = (uint8_t *)((uintptr_t)ptr + offsetof(tpmt_ha, digest));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100124
Sandrine Bailleux898c0992021-06-17 16:10:40 +0200125 /* Copy digest */
126 (void)memcpy(ptr, (const void *)hash, TCG_DIGEST_SIZE);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100127
128 /* TCG_PCR_EVENT2.EventSize */
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100129 ptr = (uint8_t *)((uintptr_t)ptr + TCG_DIGEST_SIZE);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100130 ((event2_data_t *)ptr)->event_size = name_len;
131
132 /* Copy event data to TCG_PCR_EVENT2.Event */
133 (void)memcpy((void *)(((event2_data_t *)ptr)->event),
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100134 (const void *)metadata_ptr->name, name_len);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100135
136 /* End of event data */
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100137 log_ptr = (uint8_t *)((uintptr_t)ptr +
138 offsetof(event2_data_t, event) + name_len);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100139}
140
141/*
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100142 * Initialise Event Log global variables, used during the recording
143 * of various payload measurements into the Event Log buffer
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100144 *
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100145 * @param[in] event_log_start Base address of Event Log buffer
146 * @param[in] event_log_finish End address of Event Log buffer,
147 * it is a first byte past end of the
148 * buffer
149 */
150void event_log_init(uint8_t *event_log_start, uint8_t *event_log_finish)
151{
152 assert(event_log_start != NULL);
153 assert(event_log_finish > event_log_start);
154
155 log_ptr = event_log_start;
156 log_end = (uintptr_t)event_log_finish;
157
158 /* Get pointer to platform's event_log_metadata_t structure */
159 plat_metadata_ptr = plat_event_log_get_metadata();
160 assert(plat_metadata_ptr != NULL);
161}
162
163/*
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100164 * Initialises Event Log by writing Specification ID and
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100165 * Startup Locality events
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100166 */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100167void event_log_write_header(void)
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100168{
169 const char locality_signature[] = TCG_STARTUP_LOCALITY_SIGNATURE;
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100170 void *ptr = log_ptr;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100171
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100172 /* event_log_init() must have been called prior to this. */
173 assert(log_ptr != NULL);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100174
175 /*
176 * Add Specification ID Event first
177 *
178 * Copy TCG_EfiSpecIDEventStruct structure header
179 */
180 (void)memcpy(ptr, (const void *)&id_event_header,
181 sizeof(id_event_header));
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100182 ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_header));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100183
184 /* TCG_EfiSpecIdEventAlgorithmSize structure */
185 ((id_event_algorithm_size_t *)ptr)->algorithm_id = TPM_ALG_ID;
186 ((id_event_algorithm_size_t *)ptr)->digest_size = TCG_DIGEST_SIZE;
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100187 ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_algorithm_size_t));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100188
189 /*
190 * TCG_EfiSpecIDEventStruct.vendorInfoSize
191 * No vendor data
192 */
193 ((id_event_struct_data_t *)ptr)->vendor_info_size = 0;
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100194 ptr = (uint8_t *)((uintptr_t)ptr +
195 offsetof(id_event_struct_data_t, vendor_info));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100196
197 /*
198 * The Startup Locality event should be placed in the log before
199 * any event which extends PCR[0].
200 *
201 * Ref. TCG PC Client Platform Firmware Profile 9.4.5.3
202 */
203
204 /* Copy Startup Locality Event Header */
205 (void)memcpy(ptr, (const void *)&locality_event_header,
206 sizeof(locality_event_header));
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100207 ptr = (uint8_t *)((uintptr_t)ptr + sizeof(locality_event_header));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100208
209 /* TCG_PCR_EVENT2.Digests[].AlgorithmId */
210 ((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID;
211
212 /* TCG_PCR_EVENT2.Digests[].Digest[] */
213 (void)memset(&((tpmt_ha *)ptr)->digest, 0, TPM_ALG_ID);
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100214 ptr = (uint8_t *)((uintptr_t)ptr +
215 offsetof(tpmt_ha, digest) + TCG_DIGEST_SIZE);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100216
217 /* TCG_PCR_EVENT2.EventSize */
218 ((event2_data_t *)ptr)->event_size =
219 (uint32_t)sizeof(startup_locality_event_t);
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100220 ptr = (uint8_t *)((uintptr_t)ptr + offsetof(event2_data_t, event));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100221
222 /* TCG_EfiStartupLocalityEvent.Signature */
223 (void)memcpy(ptr, (const void *)locality_signature,
224 sizeof(TCG_STARTUP_LOCALITY_SIGNATURE));
225
226 /*
227 * TCG_EfiStartupLocalityEvent.StartupLocality = 0:
228 * the platform's boot firmware
229 */
230 ((startup_locality_event_t *)ptr)->startup_locality = 0U;
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100231 log_ptr = (uint8_t *)((uintptr_t)ptr + sizeof(startup_locality_event_t));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100232}
233
234/*
235 * Calculate and write hash of image, configuration data, etc.
236 * to Event Log.
237 *
238 * @param[in] data_base Address of data
239 * @param[in] data_size Size of data
240 * @param[in] data_id Data ID
241 * @return:
242 * 0 = success
243 * < 0 = error
244 */
Sandrine Bailleux4e9af172021-07-01 14:13:09 +0200245int event_log_measure_and_record(uintptr_t data_base, uint32_t data_size,
246 uint32_t data_id)
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100247{
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100248 unsigned char hash_data[MBEDTLS_MD_MAX_SIZE];
249 int rc;
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100250 const event_log_metadata_t *metadata_ptr = plat_metadata_ptr;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100251
Sandrine Bailleux74b8e172021-06-23 15:44:18 +0200252 /* Get the metadata associated with this image. */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100253 while ((metadata_ptr->id != INVALID_ID) &&
254 (metadata_ptr->id != data_id)) {
255 metadata_ptr++;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100256 }
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100257 assert(metadata_ptr->id != INVALID_ID);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100258
259 /* Calculate hash */
260 rc = crypto_mod_calc_hash((unsigned int)MBEDTLS_MD_ID,
261 (void *)data_base, data_size, hash_data);
262 if (rc != 0) {
263 return rc;
264 }
265
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100266 event_log_record(hash_data, metadata_ptr);
Sandrine Bailleux36af1c82021-06-17 15:44:40 +0200267
Sandrine Bailleux9ebe81e2021-06-23 15:43:02 +0200268 return 0;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100269}
270
271/*
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100272 * Get current Event Log buffer size i.e. used space of Event Log buffer
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100273 *
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100274 * @param[in] event_log_start Base Pointer to Event Log buffer
275 *
276 * @return: current Size of Event Log buffer
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100277 */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100278size_t event_log_get_cur_size(uint8_t *event_log_start)
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100279{
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100280 assert(event_log_start != NULL);
281 assert(log_ptr >= event_log_start);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100282
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100283 return (size_t)((uintptr_t)log_ptr - (uintptr_t)event_log_start);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100284}