blob: e0fc26e39a2d518a0f9f26bdcd4d50d9a878789b [file] [log] [blame]
Alexei Fedorov71d81dc2020-07-13 13:58:06 +01001/*
2 * Copyright (c) 2020, Arm Limited. All rights reserved.
3 *
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>
15#include <drivers/measured_boot/event_log.h>
16#include <mbedtls/md.h>
17
18#include <plat/common/platform.h>
19
20/* Event Log data */
21static uint8_t event_log[EVENT_LOG_SIZE];
22
23/* End of Event Log */
24#define EVENT_LOG_END ((uintptr_t)event_log + sizeof(event_log) - 1U)
25
26CASSERT(sizeof(event_log) >= LOG_MIN_SIZE, assert_event_log_size);
27
28/* Pointer in event_log[] */
29static uint8_t *log_ptr = event_log;
30
31/* Pointer to measured_boot_data_t */
32const static measured_boot_data_t *plat_data_ptr;
33
34static uintptr_t tos_fw_config_base;
35static uintptr_t nt_fw_config_base;
36
37/* TCG_EfiSpecIdEvent */
38static const id_event_headers_t id_event_header = {
39 .header = {
40 .pcr_index = PCR_0,
41 .event_type = EV_NO_ACTION,
42 .digest = {0},
43 .event_size = (uint32_t)(sizeof(id_event_struct_t) +
44 (sizeof(id_event_algorithm_size_t) *
45 HASH_ALG_COUNT))
46 },
47
48 .struct_header = {
49 .signature = TCG_ID_EVENT_SIGNATURE_03,
50 .platform_class = PLATFORM_CLASS_CLIENT,
51 .spec_version_minor = TCG_SPEC_VERSION_MINOR_TPM2,
52 .spec_version_major = TCG_SPEC_VERSION_MAJOR_TPM2,
53 .spec_errata = TCG_SPEC_ERRATA_TPM2,
54 .uintn_size = (uint8_t)(sizeof(unsigned int) /
55 sizeof(uint32_t)),
56 .number_of_algorithms = HASH_ALG_COUNT
57 }
58};
59
60static const event2_header_t locality_event_header = {
Sandrine Bailleuxbe761432021-06-23 10:40:08 +020061 /*
62 * All EV_NO_ACTION events SHALL set
63 * TCG_PCR_EVENT2.pcrIndex = 0, unless otherwise specified
64 */
65 .pcr_index = PCR_0,
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010066
Sandrine Bailleuxbe761432021-06-23 10:40:08 +020067 /*
68 * All EV_NO_ACTION events SHALL set
69 * TCG_PCR_EVENT2.eventType = 03h
70 */
71 .event_type = EV_NO_ACTION,
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010072
Sandrine Bailleuxbe761432021-06-23 10:40:08 +020073 /*
74 * All EV_NO_ACTION events SHALL set TCG_PCR_EVENT2.digests to all
75 * 0x00's for each allocated Hash algorithm
76 */
77 .digests = {
78 .count = HASH_ALG_COUNT
79 }
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010080};
81
Alexei Fedorov71d81dc2020-07-13 13:58:06 +010082/*
83 * Add TCG_PCR_EVENT2 event
84 *
85 * @param[in] hash Pointer to hash data of TCG_DIGEST_SIZE bytes
86 * @param[in] image_ptr Pointer to image_data_t structure
87 * @return:
88 * 0 = success
89 * < 0 = error code
90 */
91static int add_event2(const uint8_t *hash, const image_data_t *image_ptr)
92{
93 void *ptr = log_ptr;
94 uint32_t name_len;
95 uint32_t size_of_event;
96
97 assert(image_ptr != NULL);
98 assert(image_ptr->name != NULL);
99
100 name_len = (uint32_t)strlen(image_ptr->name) + 1U;
101 size_of_event = name_len + (uint32_t)EVENT2_HDR_SIZE;
102
103 /* Check for space in Event Log buffer */
104 if (((uintptr_t)ptr + size_of_event) > EVENT_LOG_END) {
105 ERROR("%s(): Event Log is short of memory", __func__);
106 return -ENOMEM;
107 }
108
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 */
115 ((event2_header_t *)ptr)->pcr_index = image_ptr->pcr;
116
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
134 /* Check for space in Event Log buffer */
135 if (((uintptr_t)ptr + TCG_DIGEST_SIZE) > EVENT_LOG_END) {
136 ERROR("%s(): Event Log is short of memory", __func__);
137 return -ENOMEM;
138 }
139
140 if (hash == NULL) {
141 /* Get BL2 hash from DTB */
142 bl2_plat_get_hash(ptr);
143 } else {
144 /* Copy digest */
145 (void)memcpy(ptr, (const void *)hash, TCG_DIGEST_SIZE);
146 }
147
148 /* TCG_PCR_EVENT2.EventSize */
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100149 ptr = (uint8_t *)((uintptr_t)ptr + TCG_DIGEST_SIZE);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100150 ((event2_data_t *)ptr)->event_size = name_len;
151
152 /* Copy event data to TCG_PCR_EVENT2.Event */
153 (void)memcpy((void *)(((event2_data_t *)ptr)->event),
154 (const void *)image_ptr->name, name_len);
155
156 /* End of event data */
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100157 log_ptr = (uint8_t *)((uintptr_t)ptr +
158 offsetof(event2_data_t, event) + name_len);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100159
160 return 0;
161}
162
163/*
164 * Init Event Log
165 *
166 * Initialises Event Log by writing Specification ID and
167 * Startup Locality events.
168 */
169void event_log_init(void)
170{
171 const char locality_signature[] = TCG_STARTUP_LOCALITY_SIGNATURE;
172 const uint8_t *start_ptr;
173 void *ptr = event_log;
174
175 /* Get pointer to platform's measured_boot_data_t structure */
176 plat_data_ptr = plat_get_measured_boot_data();
177
178 /*
179 * Add Specification ID Event first
180 *
181 * Copy TCG_EfiSpecIDEventStruct structure header
182 */
183 (void)memcpy(ptr, (const void *)&id_event_header,
184 sizeof(id_event_header));
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100185 ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_header));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100186
187 /* TCG_EfiSpecIdEventAlgorithmSize structure */
188 ((id_event_algorithm_size_t *)ptr)->algorithm_id = TPM_ALG_ID;
189 ((id_event_algorithm_size_t *)ptr)->digest_size = TCG_DIGEST_SIZE;
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100190 ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_algorithm_size_t));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100191
192 /*
193 * TCG_EfiSpecIDEventStruct.vendorInfoSize
194 * No vendor data
195 */
196 ((id_event_struct_data_t *)ptr)->vendor_info_size = 0;
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100197 ptr = (uint8_t *)((uintptr_t)ptr +
198 offsetof(id_event_struct_data_t, vendor_info));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100199 if ((uintptr_t)ptr != ((uintptr_t)event_log + ID_EVENT_SIZE)) {
200 panic();
201 }
202
203 start_ptr = (uint8_t *)ptr;
204
205 /*
206 * The Startup Locality event should be placed in the log before
207 * any event which extends PCR[0].
208 *
209 * Ref. TCG PC Client Platform Firmware Profile 9.4.5.3
210 */
211
212 /* Copy Startup Locality Event Header */
213 (void)memcpy(ptr, (const void *)&locality_event_header,
214 sizeof(locality_event_header));
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100215 ptr = (uint8_t *)((uintptr_t)ptr + sizeof(locality_event_header));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100216
217 /* TCG_PCR_EVENT2.Digests[].AlgorithmId */
218 ((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID;
219
220 /* TCG_PCR_EVENT2.Digests[].Digest[] */
221 (void)memset(&((tpmt_ha *)ptr)->digest, 0, TPM_ALG_ID);
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100222 ptr = (uint8_t *)((uintptr_t)ptr +
223 offsetof(tpmt_ha, digest) + TCG_DIGEST_SIZE);
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100224
225 /* TCG_PCR_EVENT2.EventSize */
226 ((event2_data_t *)ptr)->event_size =
227 (uint32_t)sizeof(startup_locality_event_t);
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100228 ptr = (uint8_t *)((uintptr_t)ptr + offsetof(event2_data_t, event));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100229
230 /* TCG_EfiStartupLocalityEvent.Signature */
231 (void)memcpy(ptr, (const void *)locality_signature,
232 sizeof(TCG_STARTUP_LOCALITY_SIGNATURE));
233
234 /*
235 * TCG_EfiStartupLocalityEvent.StartupLocality = 0:
236 * the platform's boot firmware
237 */
238 ((startup_locality_event_t *)ptr)->startup_locality = 0U;
Alexei Fedorovf52e6a12020-09-28 14:47:54 +0100239 ptr = (uint8_t *)((uintptr_t)ptr + sizeof(startup_locality_event_t));
Alexei Fedorov71d81dc2020-07-13 13:58:06 +0100240 if ((uintptr_t)ptr != ((uintptr_t)start_ptr + LOC_EVENT_SIZE)) {
241 panic();
242 }
243
244 log_ptr = (uint8_t *)ptr;
245
246 /* Add BL2 event */
247 if (add_event2(NULL, plat_data_ptr->images_data) != 0) {
248 panic();
249 }
250}
251
252/*
253 * Calculate and write hash of image, configuration data, etc.
254 * to Event Log.
255 *
256 * @param[in] data_base Address of data
257 * @param[in] data_size Size of data
258 * @param[in] data_id Data ID
259 * @return:
260 * 0 = success
261 * < 0 = error
262 */
263int tpm_record_measurement(uintptr_t data_base, uint32_t data_size,
264 uint32_t data_id)
265{
266 const image_data_t *data_ptr = plat_data_ptr->images_data;
267 unsigned char hash_data[MBEDTLS_MD_MAX_SIZE];
268 int rc;
269
270 /* Check if image_id is supported */
271 while (data_ptr->id != data_id) {
272 if ((data_ptr++)->id == INVALID_ID) {
273 ERROR("%s(): image_id %u not supported\n",
274 __func__, data_id);
275 return -EINVAL;
276 }
277 }
278
279 if (data_id == TOS_FW_CONFIG_ID) {
280 tos_fw_config_base = data_base;
281 } else if (data_id == NT_FW_CONFIG_ID) {
282 nt_fw_config_base = data_base;
283 } else {
284 /* No action */
285 }
286
287 /* Calculate hash */
288 rc = crypto_mod_calc_hash((unsigned int)MBEDTLS_MD_ID,
289 (void *)data_base, data_size, hash_data);
290 if (rc != 0) {
291 return rc;
292 }
293
294 return add_event2(hash_data, data_ptr);
295}
296
297/*
298 * Finalise Event Log
299 *
300 * @param[out] log_addr Pointer to return Event Log address
301 * @param[out] log_size Pointer to return Event Log size
302 * @return:
303 * 0 = success
304 * < 0 = error code
305 */
306int event_log_finalise(uint8_t **log_addr, size_t *log_size)
307{
308 /* Event Log size */
309 size_t num_bytes = (uintptr_t)log_ptr - (uintptr_t)event_log;
310 int rc;
311
312 assert(log_addr != NULL);
313 assert(log_size != NULL);
314
315 if (nt_fw_config_base == 0UL) {
316 ERROR("%s(): %s_FW_CONFIG not loaded\n", __func__, "NT");
317 return -ENOENT;
318 }
319
320 /*
321 * Set Event Log data in NT_FW_CONFIG and
322 * get Event Log address in Non-Secure memory
323 */
324 if (plat_data_ptr->set_nt_fw_info != NULL) {
325
326 /* Event Log address in Non-Secure memory */
327 uintptr_t ns_log_addr;
328
329 rc = plat_data_ptr->set_nt_fw_info(
330 nt_fw_config_base,
331#ifdef SPD_opteed
332 (uintptr_t)event_log,
333#endif
334 num_bytes, &ns_log_addr);
335 if (rc != 0) {
336 ERROR("%s(): Unable to update %s_FW_CONFIG\n",
337 __func__, "NT");
338 return rc;
339 }
340
341 /* Copy Event Log to Non-secure memory */
342 (void)memcpy((void *)ns_log_addr, (const void *)event_log,
343 num_bytes);
344
345 /* Ensure that the Event Log is visible in Non-secure memory */
346 flush_dcache_range(ns_log_addr, num_bytes);
347
348 /* Return Event Log address in Non-Secure memory */
349 *log_addr = (uint8_t *)ns_log_addr;
350
351 } else {
352 INFO("%s(): set_%s_fw_info not set\n", __func__, "nt");
353
354 /* Return Event Log address in Secure memory */
355 *log_addr = event_log;
356 }
357
358 if (tos_fw_config_base != 0UL) {
359 if (plat_data_ptr->set_tos_fw_info != NULL) {
360
361 /* Set Event Log data in TOS_FW_CONFIG */
362 rc = plat_data_ptr->set_tos_fw_info(
363 tos_fw_config_base,
364 (uintptr_t)event_log,
365 num_bytes);
366 if (rc != 0) {
367 ERROR("%s(): Unable to update %s_FW_CONFIG\n",
368 __func__, "TOS");
369 return rc;
370 }
371 } else {
372 INFO("%s(): set_%s_fw_info not set\n", __func__, "tos");
373 }
374 } else {
375 INFO("%s(): %s_FW_CONFIG not loaded\n", __func__, "TOS");
376 }
377
378 /* Ensure that the Event Log is visible in Secure memory */
379 flush_dcache_range((uintptr_t)event_log, num_bytes);
380
381 /* Return Event Log size */
382 *log_size = num_bytes;
383
384 return 0;
385}