blob: abe469b74bddbe88f931da7fb9fb6d4057900e08 [file] [log] [blame]
Alexei Fedorov71d81dc2020-07-13 13:58:06 +01001/*
Manish V Badarkhe6e836762022-06-09 22:39:32 +01002 * Copyright (c) 2020-2022, 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
17#include <plat/common/platform.h>
18
Manish V Badarkhee112a5a2021-10-06 23:41:50 +010019#if TPM_ALG_ID == TPM_ALG_SHA512
20#define CRYPTO_MD_ID CRYPTO_MD_SHA512
21#elif TPM_ALG_ID == TPM_ALG_SHA384
22#define CRYPTO_MD_ID CRYPTO_MD_SHA384
23#elif TPM_ALG_ID == TPM_ALG_SHA256
24#define CRYPTO_MD_ID CRYPTO_MD_SHA256
25#else
26# error Invalid TPM algorithm.
27#endif /* TPM_ALG_ID */
28
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010029/* Running Event Log Pointer */
30static uint8_t *log_ptr;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010031
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010032/* Pointer to the first byte past end of the Event Log buffer */
33static uintptr_t log_end;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010034
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010035/* Pointer to event_log_metadata_t */
36static const event_log_metadata_t *plat_metadata_ptr;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010037
38/* TCG_EfiSpecIdEvent */
39static const id_event_headers_t id_event_header = {
40 .header = {
41 .pcr_index = PCR_0,
42 .event_type = EV_NO_ACTION,
43 .digest = {0},
44 .event_size = (uint32_t)(sizeof(id_event_struct_t) +
45 (sizeof(id_event_algorithm_size_t) *
46 HASH_ALG_COUNT))
47 },
48
49 .struct_header = {
50 .signature = TCG_ID_EVENT_SIGNATURE_03,
51 .platform_class = PLATFORM_CLASS_CLIENT,
52 .spec_version_minor = TCG_SPEC_VERSION_MINOR_TPM2,
53 .spec_version_major = TCG_SPEC_VERSION_MAJOR_TPM2,
54 .spec_errata = TCG_SPEC_ERRATA_TPM2,
55 .uintn_size = (uint8_t)(sizeof(unsigned int) /
56 sizeof(uint32_t)),
57 .number_of_algorithms = HASH_ALG_COUNT
58 }
59};
60
61static const event2_header_t locality_event_header = {
Sandrine Bailleuxbe761432021-06-23 10:40:08 +020062 /*
63 * All EV_NO_ACTION events SHALL set
64 * TCG_PCR_EVENT2.pcrIndex = 0, unless otherwise specified
65 */
66 .pcr_index = PCR_0,
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010067
Sandrine Bailleuxbe761432021-06-23 10:40:08 +020068 /*
69 * All EV_NO_ACTION events SHALL set
70 * TCG_PCR_EVENT2.eventType = 03h
71 */
72 .event_type = EV_NO_ACTION,
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010073
Sandrine Bailleuxbe761432021-06-23 10:40:08 +020074 /*
75 * All EV_NO_ACTION events SHALL set TCG_PCR_EVENT2.digests to all
76 * 0x00's for each allocated Hash algorithm
77 */
78 .digests = {
79 .count = HASH_ALG_COUNT
80 }
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010081};
82
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010083/*
Sandrine Bailleux36af1c82021-06-17 15:44:40 +020084 * Record a measurement as a TCG_PCR_EVENT2 event
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010085 *
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010086 * @param[in] hash Pointer to hash data of TCG_DIGEST_SIZE bytes
87 * @param[in] metadata_ptr Pointer to event_log_metadata_t structure
Sandrine Bailleux9ebe81e2021-06-23 15:43:02 +020088 *
89 * There must be room for storing this new event into the event log buffer.
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010090 */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010091static void event_log_record(const uint8_t *hash,
92 const event_log_metadata_t *metadata_ptr)
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010093{
94 void *ptr = log_ptr;
95 uint32_t name_len;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010096
Sandrine Bailleux898c0992021-06-17 16:10:40 +020097 assert(hash != NULL);
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010098 assert(metadata_ptr != NULL);
99 assert(metadata_ptr->name != NULL);
100 /* event_log_init() must have been called prior to this. */
101 assert(log_ptr != NULL);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100102
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100103 name_len = (uint32_t)strlen(metadata_ptr->name) + 1U;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100104
105 /* Check for space in Event Log buffer */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100106 assert(((uintptr_t)ptr + (uint32_t)EVENT2_HDR_SIZE + name_len) <
107 log_end);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100108
109 /*
110 * As per TCG specifications, firmware components that are measured
111 * into PCR[0] must be logged in the event log using the event type
112 * EV_POST_CODE.
113 */
114 /* TCG_PCR_EVENT2.PCRIndex */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100115 ((event2_header_t *)ptr)->pcr_index = metadata_ptr->pcr;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100116
117 /* TCG_PCR_EVENT2.EventType */
118 ((event2_header_t *)ptr)->event_type = EV_POST_CODE;
119
120 /* TCG_PCR_EVENT2.Digests.Count */
121 ptr = (uint8_t *)ptr + offsetof(event2_header_t, digests);
122 ((tpml_digest_values *)ptr)->count = HASH_ALG_COUNT;
123
124 /* TCG_PCR_EVENT2.Digests[] */
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100125 ptr = (uint8_t *)((uintptr_t)ptr +
126 offsetof(tpml_digest_values, digests));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100127
128 /* TCG_PCR_EVENT2.Digests[].AlgorithmId */
129 ((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID;
130
131 /* TCG_PCR_EVENT2.Digests[].Digest[] */
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100132 ptr = (uint8_t *)((uintptr_t)ptr + offsetof(tpmt_ha, digest));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100133
Sandrine Bailleux898c0992021-06-17 16:10:40 +0200134 /* Copy digest */
135 (void)memcpy(ptr, (const void *)hash, TCG_DIGEST_SIZE);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100136
137 /* TCG_PCR_EVENT2.EventSize */
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100138 ptr = (uint8_t *)((uintptr_t)ptr + TCG_DIGEST_SIZE);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100139 ((event2_data_t *)ptr)->event_size = name_len;
140
141 /* Copy event data to TCG_PCR_EVENT2.Event */
142 (void)memcpy((void *)(((event2_data_t *)ptr)->event),
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100143 (const void *)metadata_ptr->name, name_len);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100144
145 /* End of event data */
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100146 log_ptr = (uint8_t *)((uintptr_t)ptr +
147 offsetof(event2_data_t, event) + name_len);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100148}
149
150/*
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100151 * Initialise Event Log global variables, used during the recording
152 * of various payload measurements into the Event Log buffer
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100153 *
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100154 * @param[in] event_log_start Base address of Event Log buffer
155 * @param[in] event_log_finish End address of Event Log buffer,
156 * it is a first byte past end of the
157 * buffer
158 */
159void event_log_init(uint8_t *event_log_start, uint8_t *event_log_finish)
160{
161 assert(event_log_start != NULL);
162 assert(event_log_finish > event_log_start);
163
164 log_ptr = event_log_start;
165 log_end = (uintptr_t)event_log_finish;
166
167 /* Get pointer to platform's event_log_metadata_t structure */
168 plat_metadata_ptr = plat_event_log_get_metadata();
169 assert(plat_metadata_ptr != NULL);
170}
171
172/*
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100173 * Initialises Event Log by writing Specification ID and
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100174 * Startup Locality events
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100175 */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100176void event_log_write_header(void)
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100177{
178 const char locality_signature[] = TCG_STARTUP_LOCALITY_SIGNATURE;
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100179 void *ptr = log_ptr;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100180
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100181 /* event_log_init() must have been called prior to this. */
182 assert(log_ptr != NULL);
Manish V Badarkhe6e836762022-06-09 22:39:32 +0100183 assert(((uintptr_t)log_ptr + ID_EVENT_SIZE + LOC_EVENT_SIZE) <
184 log_end);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100185
186 /*
187 * Add Specification ID Event first
188 *
189 * Copy TCG_EfiSpecIDEventStruct structure header
190 */
191 (void)memcpy(ptr, (const void *)&id_event_header,
192 sizeof(id_event_header));
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100193 ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_header));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100194
195 /* TCG_EfiSpecIdEventAlgorithmSize structure */
196 ((id_event_algorithm_size_t *)ptr)->algorithm_id = TPM_ALG_ID;
197 ((id_event_algorithm_size_t *)ptr)->digest_size = TCG_DIGEST_SIZE;
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100198 ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_algorithm_size_t));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100199
200 /*
201 * TCG_EfiSpecIDEventStruct.vendorInfoSize
202 * No vendor data
203 */
204 ((id_event_struct_data_t *)ptr)->vendor_info_size = 0;
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100205 ptr = (uint8_t *)((uintptr_t)ptr +
206 offsetof(id_event_struct_data_t, vendor_info));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100207
208 /*
209 * The Startup Locality event should be placed in the log before
210 * any event which extends PCR[0].
211 *
212 * Ref. TCG PC Client Platform Firmware Profile 9.4.5.3
213 */
214
215 /* Copy Startup Locality Event Header */
216 (void)memcpy(ptr, (const void *)&locality_event_header,
217 sizeof(locality_event_header));
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100218 ptr = (uint8_t *)((uintptr_t)ptr + sizeof(locality_event_header));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100219
220 /* TCG_PCR_EVENT2.Digests[].AlgorithmId */
221 ((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID;
222
223 /* TCG_PCR_EVENT2.Digests[].Digest[] */
Manish V Badarkhe6e836762022-06-09 22:39:32 +0100224 (void)memset(&((tpmt_ha *)ptr)->digest, 0, TCG_DIGEST_SIZE);
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100225 ptr = (uint8_t *)((uintptr_t)ptr +
226 offsetof(tpmt_ha, digest) + TCG_DIGEST_SIZE);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100227
228 /* TCG_PCR_EVENT2.EventSize */
229 ((event2_data_t *)ptr)->event_size =
230 (uint32_t)sizeof(startup_locality_event_t);
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100231 ptr = (uint8_t *)((uintptr_t)ptr + offsetof(event2_data_t, event));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100232
233 /* TCG_EfiStartupLocalityEvent.Signature */
234 (void)memcpy(ptr, (const void *)locality_signature,
235 sizeof(TCG_STARTUP_LOCALITY_SIGNATURE));
236
237 /*
238 * TCG_EfiStartupLocalityEvent.StartupLocality = 0:
239 * the platform's boot firmware
240 */
241 ((startup_locality_event_t *)ptr)->startup_locality = 0U;
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100242 log_ptr = (uint8_t *)((uintptr_t)ptr + sizeof(startup_locality_event_t));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100243}
244
245/*
246 * Calculate and write hash of image, configuration data, etc.
247 * to Event Log.
248 *
249 * @param[in] data_base Address of data
250 * @param[in] data_size Size of data
251 * @param[in] data_id Data ID
252 * @return:
253 * 0 = success
254 * < 0 = error
255 */
Sandrine Bailleux4e9af172021-07-01 14:13:09 +0200256int event_log_measure_and_record(uintptr_t data_base, uint32_t data_size,
257 uint32_t data_id)
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100258{
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100259 unsigned char hash_data[CRYPTO_MD_MAX_SIZE];
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100260 int rc;
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100261 const event_log_metadata_t *metadata_ptr = plat_metadata_ptr;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100262
Sandrine Bailleux74b8e172021-06-23 15:44:18 +0200263 /* Get the metadata associated with this image. */
Manish V Badarkhe67009c32021-10-31 14:47:49 +0000264 while ((metadata_ptr->id != EVLOG_INVALID_ID) &&
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100265 (metadata_ptr->id != data_id)) {
266 metadata_ptr++;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100267 }
Manish V Badarkhe67009c32021-10-31 14:47:49 +0000268 assert(metadata_ptr->id != EVLOG_INVALID_ID);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100269
270 /* Calculate hash */
Manish V Badarkhee112a5a2021-10-06 23:41:50 +0100271 rc = crypto_mod_calc_hash(CRYPTO_MD_ID,
272 (void *)data_base, data_size, hash_data);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100273 if (rc != 0) {
274 return rc;
275 }
276
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100277 event_log_record(hash_data, metadata_ptr);
Sandrine Bailleux36af1c82021-06-17 15:44:40 +0200278
Sandrine Bailleux9ebe81e2021-06-23 15:43:02 +0200279 return 0;
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100280}
281
282/*
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100283 * Get current Event Log buffer size i.e. used space of Event Log buffer
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100284 *
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100285 * @param[in] event_log_start Base Pointer to Event Log buffer
286 *
287 * @return: current Size of Event Log buffer
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100288 */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100289size_t event_log_get_cur_size(uint8_t *event_log_start)
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100290{
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100291 assert(event_log_start != NULL);
292 assert(log_ptr >= event_log_start);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100293
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100294 return (size_t)((uintptr_t)log_ptr - (uintptr_t)event_log_start);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100295}