Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 1 | /* |
| 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 */ |
| 21 | static 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 | |
| 26 | CASSERT(sizeof(event_log) >= LOG_MIN_SIZE, assert_event_log_size); |
| 27 | |
| 28 | /* Pointer in event_log[] */ |
| 29 | static uint8_t *log_ptr = event_log; |
| 30 | |
| 31 | /* Pointer to measured_boot_data_t */ |
| 32 | const static measured_boot_data_t *plat_data_ptr; |
| 33 | |
| 34 | static uintptr_t tos_fw_config_base; |
| 35 | static uintptr_t nt_fw_config_base; |
| 36 | |
| 37 | /* TCG_EfiSpecIdEvent */ |
| 38 | static 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 | |
| 60 | static const event2_header_t locality_event_header = { |
Sandrine Bailleux | be76143 | 2021-06-23 10:40:08 +0200 | [diff] [blame] | 61 | /* |
| 62 | * All EV_NO_ACTION events SHALL set |
| 63 | * TCG_PCR_EVENT2.pcrIndex = 0, unless otherwise specified |
| 64 | */ |
| 65 | .pcr_index = PCR_0, |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 66 | |
Sandrine Bailleux | be76143 | 2021-06-23 10:40:08 +0200 | [diff] [blame] | 67 | /* |
| 68 | * All EV_NO_ACTION events SHALL set |
| 69 | * TCG_PCR_EVENT2.eventType = 03h |
| 70 | */ |
| 71 | .event_type = EV_NO_ACTION, |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 72 | |
Sandrine Bailleux | be76143 | 2021-06-23 10:40:08 +0200 | [diff] [blame] | 73 | /* |
| 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 Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 80 | }; |
| 81 | |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 82 | /* |
| 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 |
Sandrine Bailleux | 9ebe81e | 2021-06-23 15:43:02 +0200 | [diff] [blame] | 87 | * |
| 88 | * There must be room for storing this new event into the event log buffer. |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 89 | */ |
Sandrine Bailleux | 9ebe81e | 2021-06-23 15:43:02 +0200 | [diff] [blame] | 90 | static void add_event2(const uint8_t *hash, const image_data_t *image_ptr) |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 91 | { |
| 92 | void *ptr = log_ptr; |
| 93 | uint32_t name_len; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 94 | |
| 95 | assert(image_ptr != NULL); |
| 96 | assert(image_ptr->name != NULL); |
| 97 | |
| 98 | name_len = (uint32_t)strlen(image_ptr->name) + 1U; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 99 | |
| 100 | /* Check for space in Event Log buffer */ |
Sandrine Bailleux | 9ebe81e | 2021-06-23 15:43:02 +0200 | [diff] [blame] | 101 | assert(((uintptr_t)ptr + (uint32_t)EVENT2_HDR_SIZE + name_len) <= |
| 102 | EVENT_LOG_END); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * As per TCG specifications, firmware components that are measured |
| 106 | * into PCR[0] must be logged in the event log using the event type |
| 107 | * EV_POST_CODE. |
| 108 | */ |
| 109 | /* TCG_PCR_EVENT2.PCRIndex */ |
| 110 | ((event2_header_t *)ptr)->pcr_index = image_ptr->pcr; |
| 111 | |
| 112 | /* TCG_PCR_EVENT2.EventType */ |
| 113 | ((event2_header_t *)ptr)->event_type = EV_POST_CODE; |
| 114 | |
| 115 | /* TCG_PCR_EVENT2.Digests.Count */ |
| 116 | ptr = (uint8_t *)ptr + offsetof(event2_header_t, digests); |
| 117 | ((tpml_digest_values *)ptr)->count = HASH_ALG_COUNT; |
| 118 | |
| 119 | /* TCG_PCR_EVENT2.Digests[] */ |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 120 | ptr = (uint8_t *)((uintptr_t)ptr + |
| 121 | offsetof(tpml_digest_values, digests)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 122 | |
| 123 | /* TCG_PCR_EVENT2.Digests[].AlgorithmId */ |
| 124 | ((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID; |
| 125 | |
| 126 | /* TCG_PCR_EVENT2.Digests[].Digest[] */ |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 127 | ptr = (uint8_t *)((uintptr_t)ptr + offsetof(tpmt_ha, digest)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 128 | |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 129 | if (hash == NULL) { |
| 130 | /* Get BL2 hash from DTB */ |
| 131 | bl2_plat_get_hash(ptr); |
| 132 | } else { |
| 133 | /* Copy digest */ |
| 134 | (void)memcpy(ptr, (const void *)hash, TCG_DIGEST_SIZE); |
| 135 | } |
| 136 | |
| 137 | /* TCG_PCR_EVENT2.EventSize */ |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 138 | ptr = (uint8_t *)((uintptr_t)ptr + TCG_DIGEST_SIZE); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 139 | ((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), |
| 143 | (const void *)image_ptr->name, name_len); |
| 144 | |
| 145 | /* End of event data */ |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 146 | log_ptr = (uint8_t *)((uintptr_t)ptr + |
| 147 | offsetof(event2_data_t, event) + name_len); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Init Event Log |
| 152 | * |
| 153 | * Initialises Event Log by writing Specification ID and |
| 154 | * Startup Locality events. |
| 155 | */ |
| 156 | void event_log_init(void) |
| 157 | { |
| 158 | const char locality_signature[] = TCG_STARTUP_LOCALITY_SIGNATURE; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 159 | void *ptr = event_log; |
| 160 | |
| 161 | /* Get pointer to platform's measured_boot_data_t structure */ |
| 162 | plat_data_ptr = plat_get_measured_boot_data(); |
| 163 | |
| 164 | /* |
| 165 | * Add Specification ID Event first |
| 166 | * |
| 167 | * Copy TCG_EfiSpecIDEventStruct structure header |
| 168 | */ |
| 169 | (void)memcpy(ptr, (const void *)&id_event_header, |
| 170 | sizeof(id_event_header)); |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 171 | ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_header)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 172 | |
| 173 | /* TCG_EfiSpecIdEventAlgorithmSize structure */ |
| 174 | ((id_event_algorithm_size_t *)ptr)->algorithm_id = TPM_ALG_ID; |
| 175 | ((id_event_algorithm_size_t *)ptr)->digest_size = TCG_DIGEST_SIZE; |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 176 | ptr = (uint8_t *)((uintptr_t)ptr + sizeof(id_event_algorithm_size_t)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 177 | |
| 178 | /* |
| 179 | * TCG_EfiSpecIDEventStruct.vendorInfoSize |
| 180 | * No vendor data |
| 181 | */ |
| 182 | ((id_event_struct_data_t *)ptr)->vendor_info_size = 0; |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 183 | ptr = (uint8_t *)((uintptr_t)ptr + |
| 184 | offsetof(id_event_struct_data_t, vendor_info)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * The Startup Locality event should be placed in the log before |
| 188 | * any event which extends PCR[0]. |
| 189 | * |
| 190 | * Ref. TCG PC Client Platform Firmware Profile 9.4.5.3 |
| 191 | */ |
| 192 | |
| 193 | /* Copy Startup Locality Event Header */ |
| 194 | (void)memcpy(ptr, (const void *)&locality_event_header, |
| 195 | sizeof(locality_event_header)); |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 196 | ptr = (uint8_t *)((uintptr_t)ptr + sizeof(locality_event_header)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 197 | |
| 198 | /* TCG_PCR_EVENT2.Digests[].AlgorithmId */ |
| 199 | ((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID; |
| 200 | |
| 201 | /* TCG_PCR_EVENT2.Digests[].Digest[] */ |
| 202 | (void)memset(&((tpmt_ha *)ptr)->digest, 0, TPM_ALG_ID); |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 203 | ptr = (uint8_t *)((uintptr_t)ptr + |
| 204 | offsetof(tpmt_ha, digest) + TCG_DIGEST_SIZE); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 205 | |
| 206 | /* TCG_PCR_EVENT2.EventSize */ |
| 207 | ((event2_data_t *)ptr)->event_size = |
| 208 | (uint32_t)sizeof(startup_locality_event_t); |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 209 | ptr = (uint8_t *)((uintptr_t)ptr + offsetof(event2_data_t, event)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 210 | |
| 211 | /* TCG_EfiStartupLocalityEvent.Signature */ |
| 212 | (void)memcpy(ptr, (const void *)locality_signature, |
| 213 | sizeof(TCG_STARTUP_LOCALITY_SIGNATURE)); |
| 214 | |
| 215 | /* |
| 216 | * TCG_EfiStartupLocalityEvent.StartupLocality = 0: |
| 217 | * the platform's boot firmware |
| 218 | */ |
| 219 | ((startup_locality_event_t *)ptr)->startup_locality = 0U; |
Alexei Fedorov | f52e6a1 | 2020-09-28 14:47:54 +0100 | [diff] [blame] | 220 | ptr = (uint8_t *)((uintptr_t)ptr + sizeof(startup_locality_event_t)); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 221 | |
| 222 | log_ptr = (uint8_t *)ptr; |
| 223 | |
| 224 | /* Add BL2 event */ |
Sandrine Bailleux | 9ebe81e | 2021-06-23 15:43:02 +0200 | [diff] [blame] | 225 | add_event2(NULL, plat_data_ptr->images_data); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Calculate and write hash of image, configuration data, etc. |
| 230 | * to Event Log. |
| 231 | * |
| 232 | * @param[in] data_base Address of data |
| 233 | * @param[in] data_size Size of data |
| 234 | * @param[in] data_id Data ID |
| 235 | * @return: |
| 236 | * 0 = success |
| 237 | * < 0 = error |
| 238 | */ |
| 239 | int tpm_record_measurement(uintptr_t data_base, uint32_t data_size, |
| 240 | uint32_t data_id) |
| 241 | { |
| 242 | const image_data_t *data_ptr = plat_data_ptr->images_data; |
| 243 | unsigned char hash_data[MBEDTLS_MD_MAX_SIZE]; |
| 244 | int rc; |
| 245 | |
Sandrine Bailleux | 74b8e17 | 2021-06-23 15:44:18 +0200 | [diff] [blame] | 246 | /* Get the metadata associated with this image. */ |
| 247 | while ((data_ptr->id != INVALID_ID) && (data_ptr->id != data_id)) { |
| 248 | data_ptr++; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 249 | } |
Sandrine Bailleux | 74b8e17 | 2021-06-23 15:44:18 +0200 | [diff] [blame] | 250 | assert(data_ptr->id != INVALID_ID); |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 251 | |
| 252 | if (data_id == TOS_FW_CONFIG_ID) { |
| 253 | tos_fw_config_base = data_base; |
| 254 | } else if (data_id == NT_FW_CONFIG_ID) { |
| 255 | nt_fw_config_base = data_base; |
| 256 | } else { |
| 257 | /* No action */ |
| 258 | } |
| 259 | |
| 260 | /* Calculate hash */ |
| 261 | rc = crypto_mod_calc_hash((unsigned int)MBEDTLS_MD_ID, |
| 262 | (void *)data_base, data_size, hash_data); |
| 263 | if (rc != 0) { |
| 264 | return rc; |
| 265 | } |
| 266 | |
Sandrine Bailleux | 9ebe81e | 2021-06-23 15:43:02 +0200 | [diff] [blame] | 267 | add_event2(hash_data, data_ptr); |
| 268 | return 0; |
Alexei Fedorov | 71d81dc | 2020-07-13 13:58:06 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Finalise Event Log |
| 273 | * |
| 274 | * @param[out] log_addr Pointer to return Event Log address |
| 275 | * @param[out] log_size Pointer to return Event Log size |
| 276 | * @return: |
| 277 | * 0 = success |
| 278 | * < 0 = error code |
| 279 | */ |
| 280 | int event_log_finalise(uint8_t **log_addr, size_t *log_size) |
| 281 | { |
| 282 | /* Event Log size */ |
| 283 | size_t num_bytes = (uintptr_t)log_ptr - (uintptr_t)event_log; |
| 284 | int rc; |
| 285 | |
| 286 | assert(log_addr != NULL); |
| 287 | assert(log_size != NULL); |
| 288 | |
| 289 | if (nt_fw_config_base == 0UL) { |
| 290 | ERROR("%s(): %s_FW_CONFIG not loaded\n", __func__, "NT"); |
| 291 | return -ENOENT; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Set Event Log data in NT_FW_CONFIG and |
| 296 | * get Event Log address in Non-Secure memory |
| 297 | */ |
| 298 | if (plat_data_ptr->set_nt_fw_info != NULL) { |
| 299 | |
| 300 | /* Event Log address in Non-Secure memory */ |
| 301 | uintptr_t ns_log_addr; |
| 302 | |
| 303 | rc = plat_data_ptr->set_nt_fw_info( |
| 304 | nt_fw_config_base, |
| 305 | #ifdef SPD_opteed |
| 306 | (uintptr_t)event_log, |
| 307 | #endif |
| 308 | num_bytes, &ns_log_addr); |
| 309 | if (rc != 0) { |
| 310 | ERROR("%s(): Unable to update %s_FW_CONFIG\n", |
| 311 | __func__, "NT"); |
| 312 | return rc; |
| 313 | } |
| 314 | |
| 315 | /* Copy Event Log to Non-secure memory */ |
| 316 | (void)memcpy((void *)ns_log_addr, (const void *)event_log, |
| 317 | num_bytes); |
| 318 | |
| 319 | /* Ensure that the Event Log is visible in Non-secure memory */ |
| 320 | flush_dcache_range(ns_log_addr, num_bytes); |
| 321 | |
| 322 | /* Return Event Log address in Non-Secure memory */ |
| 323 | *log_addr = (uint8_t *)ns_log_addr; |
| 324 | |
| 325 | } else { |
| 326 | INFO("%s(): set_%s_fw_info not set\n", __func__, "nt"); |
| 327 | |
| 328 | /* Return Event Log address in Secure memory */ |
| 329 | *log_addr = event_log; |
| 330 | } |
| 331 | |
| 332 | if (tos_fw_config_base != 0UL) { |
| 333 | if (plat_data_ptr->set_tos_fw_info != NULL) { |
| 334 | |
| 335 | /* Set Event Log data in TOS_FW_CONFIG */ |
| 336 | rc = plat_data_ptr->set_tos_fw_info( |
| 337 | tos_fw_config_base, |
| 338 | (uintptr_t)event_log, |
| 339 | num_bytes); |
| 340 | if (rc != 0) { |
| 341 | ERROR("%s(): Unable to update %s_FW_CONFIG\n", |
| 342 | __func__, "TOS"); |
| 343 | return rc; |
| 344 | } |
| 345 | } else { |
| 346 | INFO("%s(): set_%s_fw_info not set\n", __func__, "tos"); |
| 347 | } |
| 348 | } else { |
| 349 | INFO("%s(): %s_FW_CONFIG not loaded\n", __func__, "TOS"); |
| 350 | } |
| 351 | |
| 352 | /* Ensure that the Event Log is visible in Secure memory */ |
| 353 | flush_dcache_range((uintptr_t)event_log, num_bytes); |
| 354 | |
| 355 | /* Return Event Log size */ |
| 356 | *log_size = num_bytes; |
| 357 | |
| 358 | return 0; |
| 359 | } |