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 = { |
| 61 | /* |
| 62 | * All EV_NO_ACTION events SHALL set |
| 63 | * TCG_PCR_EVENT2.pcrIndex = 0, unless otherwise specified |
| 64 | */ |
| 65 | .pcr_index = PCR_0, |
| 66 | |
| 67 | /* |
| 68 | * All EV_NO_ACTION events SHALL set |
| 69 | * TCG_PCR_EVENT2.eventType = 03h |
| 70 | */ |
| 71 | .event_type = EV_NO_ACTION, |
| 72 | |
| 73 | /* |
| 74 | * All EV_NO_ACTION events SHALL set |
| 75 | * TCG_PCR_EVENT2.digests to all |
| 76 | * 0x00's for each allocated Hash algorithm |
| 77 | */ |
| 78 | .digests = { |
| 79 | .count = HASH_ALG_COUNT |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | /* Platform's table with platform specific image IDs, names and PCRs */ |
| 84 | static const image_data_t plat_images_data[] = { |
| 85 | { BL2_IMAGE_ID, BL2_STRING, PCR_0 }, /* Reserved for BL2 */ |
| 86 | { INVALID_ID, NULL, (unsigned int)(-1) } /* Terminator */ |
| 87 | }; |
| 88 | |
| 89 | static const measured_boot_data_t plat_measured_boot_data = { |
| 90 | plat_images_data, |
| 91 | NULL, /* platform_set_nt_fw_info */ |
| 92 | NULL /* platform_set_tos_fw_info */ |
| 93 | }; |
| 94 | |
| 95 | /* |
| 96 | * Function retuns pointer to platform's measured_boot_data_t structure |
| 97 | * |
| 98 | * Must be overridden in the platform code |
| 99 | */ |
| 100 | #pragma weak plat_get_measured_boot_data |
| 101 | |
| 102 | const measured_boot_data_t *plat_get_measured_boot_data(void) |
| 103 | { |
| 104 | return &plat_measured_boot_data; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Add TCG_PCR_EVENT2 event |
| 109 | * |
| 110 | * @param[in] hash Pointer to hash data of TCG_DIGEST_SIZE bytes |
| 111 | * @param[in] image_ptr Pointer to image_data_t structure |
| 112 | * @return: |
| 113 | * 0 = success |
| 114 | * < 0 = error code |
| 115 | */ |
| 116 | static int add_event2(const uint8_t *hash, const image_data_t *image_ptr) |
| 117 | { |
| 118 | void *ptr = log_ptr; |
| 119 | uint32_t name_len; |
| 120 | uint32_t size_of_event; |
| 121 | |
| 122 | assert(image_ptr != NULL); |
| 123 | assert(image_ptr->name != NULL); |
| 124 | |
| 125 | name_len = (uint32_t)strlen(image_ptr->name) + 1U; |
| 126 | size_of_event = name_len + (uint32_t)EVENT2_HDR_SIZE; |
| 127 | |
| 128 | /* Check for space in Event Log buffer */ |
| 129 | if (((uintptr_t)ptr + size_of_event) > EVENT_LOG_END) { |
| 130 | ERROR("%s(): Event Log is short of memory", __func__); |
| 131 | return -ENOMEM; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * As per TCG specifications, firmware components that are measured |
| 136 | * into PCR[0] must be logged in the event log using the event type |
| 137 | * EV_POST_CODE. |
| 138 | */ |
| 139 | /* TCG_PCR_EVENT2.PCRIndex */ |
| 140 | ((event2_header_t *)ptr)->pcr_index = image_ptr->pcr; |
| 141 | |
| 142 | /* TCG_PCR_EVENT2.EventType */ |
| 143 | ((event2_header_t *)ptr)->event_type = EV_POST_CODE; |
| 144 | |
| 145 | /* TCG_PCR_EVENT2.Digests.Count */ |
| 146 | ptr = (uint8_t *)ptr + offsetof(event2_header_t, digests); |
| 147 | ((tpml_digest_values *)ptr)->count = HASH_ALG_COUNT; |
| 148 | |
| 149 | /* TCG_PCR_EVENT2.Digests[] */ |
| 150 | ptr = (uint8_t *)ptr + offsetof(tpml_digest_values, digests); |
| 151 | |
| 152 | /* TCG_PCR_EVENT2.Digests[].AlgorithmId */ |
| 153 | ((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID; |
| 154 | |
| 155 | /* TCG_PCR_EVENT2.Digests[].Digest[] */ |
| 156 | ptr = (uint8_t *)ptr + offsetof(tpmt_ha, digest); |
| 157 | |
| 158 | /* Check for space in Event Log buffer */ |
| 159 | if (((uintptr_t)ptr + TCG_DIGEST_SIZE) > EVENT_LOG_END) { |
| 160 | ERROR("%s(): Event Log is short of memory", __func__); |
| 161 | return -ENOMEM; |
| 162 | } |
| 163 | |
| 164 | if (hash == NULL) { |
| 165 | /* Get BL2 hash from DTB */ |
| 166 | bl2_plat_get_hash(ptr); |
| 167 | } else { |
| 168 | /* Copy digest */ |
| 169 | (void)memcpy(ptr, (const void *)hash, TCG_DIGEST_SIZE); |
| 170 | } |
| 171 | |
| 172 | /* TCG_PCR_EVENT2.EventSize */ |
| 173 | ptr = (uint8_t *)ptr + TCG_DIGEST_SIZE; |
| 174 | ((event2_data_t *)ptr)->event_size = name_len; |
| 175 | |
| 176 | /* Copy event data to TCG_PCR_EVENT2.Event */ |
| 177 | (void)memcpy((void *)(((event2_data_t *)ptr)->event), |
| 178 | (const void *)image_ptr->name, name_len); |
| 179 | |
| 180 | /* End of event data */ |
| 181 | log_ptr = (uint8_t *)ptr + offsetof(event2_data_t, event) + name_len; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Init Event Log |
| 188 | * |
| 189 | * Initialises Event Log by writing Specification ID and |
| 190 | * Startup Locality events. |
| 191 | */ |
| 192 | void event_log_init(void) |
| 193 | { |
| 194 | const char locality_signature[] = TCG_STARTUP_LOCALITY_SIGNATURE; |
| 195 | const uint8_t *start_ptr; |
| 196 | void *ptr = event_log; |
| 197 | |
| 198 | /* Get pointer to platform's measured_boot_data_t structure */ |
| 199 | plat_data_ptr = plat_get_measured_boot_data(); |
| 200 | |
| 201 | /* |
| 202 | * Add Specification ID Event first |
| 203 | * |
| 204 | * Copy TCG_EfiSpecIDEventStruct structure header |
| 205 | */ |
| 206 | (void)memcpy(ptr, (const void *)&id_event_header, |
| 207 | sizeof(id_event_header)); |
| 208 | ptr = (uint8_t *)ptr + sizeof(id_event_header); |
| 209 | |
| 210 | /* TCG_EfiSpecIdEventAlgorithmSize structure */ |
| 211 | ((id_event_algorithm_size_t *)ptr)->algorithm_id = TPM_ALG_ID; |
| 212 | ((id_event_algorithm_size_t *)ptr)->digest_size = TCG_DIGEST_SIZE; |
| 213 | ptr = (uint8_t *)ptr + sizeof(id_event_algorithm_size_t); |
| 214 | |
| 215 | /* |
| 216 | * TCG_EfiSpecIDEventStruct.vendorInfoSize |
| 217 | * No vendor data |
| 218 | */ |
| 219 | ((id_event_struct_data_t *)ptr)->vendor_info_size = 0; |
| 220 | ptr = (uint8_t *)ptr + offsetof(id_event_struct_data_t, vendor_info); |
| 221 | if ((uintptr_t)ptr != ((uintptr_t)event_log + ID_EVENT_SIZE)) { |
| 222 | panic(); |
| 223 | } |
| 224 | |
| 225 | start_ptr = (uint8_t *)ptr; |
| 226 | |
| 227 | /* |
| 228 | * The Startup Locality event should be placed in the log before |
| 229 | * any event which extends PCR[0]. |
| 230 | * |
| 231 | * Ref. TCG PC Client Platform Firmware Profile 9.4.5.3 |
| 232 | */ |
| 233 | |
| 234 | /* Copy Startup Locality Event Header */ |
| 235 | (void)memcpy(ptr, (const void *)&locality_event_header, |
| 236 | sizeof(locality_event_header)); |
| 237 | ptr = (uint8_t *)ptr + sizeof(locality_event_header); |
| 238 | |
| 239 | /* TCG_PCR_EVENT2.Digests[].AlgorithmId */ |
| 240 | ((tpmt_ha *)ptr)->algorithm_id = TPM_ALG_ID; |
| 241 | |
| 242 | /* TCG_PCR_EVENT2.Digests[].Digest[] */ |
| 243 | (void)memset(&((tpmt_ha *)ptr)->digest, 0, TPM_ALG_ID); |
| 244 | ptr = (uint8_t *)ptr + offsetof(tpmt_ha, digest) + TCG_DIGEST_SIZE; |
| 245 | |
| 246 | /* TCG_PCR_EVENT2.EventSize */ |
| 247 | ((event2_data_t *)ptr)->event_size = |
| 248 | (uint32_t)sizeof(startup_locality_event_t); |
| 249 | ptr = (uint8_t *)ptr + offsetof(event2_data_t, event); |
| 250 | |
| 251 | /* TCG_EfiStartupLocalityEvent.Signature */ |
| 252 | (void)memcpy(ptr, (const void *)locality_signature, |
| 253 | sizeof(TCG_STARTUP_LOCALITY_SIGNATURE)); |
| 254 | |
| 255 | /* |
| 256 | * TCG_EfiStartupLocalityEvent.StartupLocality = 0: |
| 257 | * the platform's boot firmware |
| 258 | */ |
| 259 | ((startup_locality_event_t *)ptr)->startup_locality = 0U; |
| 260 | ptr = (uint8_t *)ptr + sizeof(startup_locality_event_t); |
| 261 | if ((uintptr_t)ptr != ((uintptr_t)start_ptr + LOC_EVENT_SIZE)) { |
| 262 | panic(); |
| 263 | } |
| 264 | |
| 265 | log_ptr = (uint8_t *)ptr; |
| 266 | |
| 267 | /* Add BL2 event */ |
| 268 | if (add_event2(NULL, plat_data_ptr->images_data) != 0) { |
| 269 | panic(); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Calculate and write hash of image, configuration data, etc. |
| 275 | * to Event Log. |
| 276 | * |
| 277 | * @param[in] data_base Address of data |
| 278 | * @param[in] data_size Size of data |
| 279 | * @param[in] data_id Data ID |
| 280 | * @return: |
| 281 | * 0 = success |
| 282 | * < 0 = error |
| 283 | */ |
| 284 | int tpm_record_measurement(uintptr_t data_base, uint32_t data_size, |
| 285 | uint32_t data_id) |
| 286 | { |
| 287 | const image_data_t *data_ptr = plat_data_ptr->images_data; |
| 288 | unsigned char hash_data[MBEDTLS_MD_MAX_SIZE]; |
| 289 | int rc; |
| 290 | |
| 291 | /* Check if image_id is supported */ |
| 292 | while (data_ptr->id != data_id) { |
| 293 | if ((data_ptr++)->id == INVALID_ID) { |
| 294 | ERROR("%s(): image_id %u not supported\n", |
| 295 | __func__, data_id); |
| 296 | return -EINVAL; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if (data_id == TOS_FW_CONFIG_ID) { |
| 301 | tos_fw_config_base = data_base; |
| 302 | } else if (data_id == NT_FW_CONFIG_ID) { |
| 303 | nt_fw_config_base = data_base; |
| 304 | } else { |
| 305 | /* No action */ |
| 306 | } |
| 307 | |
| 308 | /* Calculate hash */ |
| 309 | rc = crypto_mod_calc_hash((unsigned int)MBEDTLS_MD_ID, |
| 310 | (void *)data_base, data_size, hash_data); |
| 311 | if (rc != 0) { |
| 312 | return rc; |
| 313 | } |
| 314 | |
| 315 | return add_event2(hash_data, data_ptr); |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * Finalise Event Log |
| 320 | * |
| 321 | * @param[out] log_addr Pointer to return Event Log address |
| 322 | * @param[out] log_size Pointer to return Event Log size |
| 323 | * @return: |
| 324 | * 0 = success |
| 325 | * < 0 = error code |
| 326 | */ |
| 327 | int event_log_finalise(uint8_t **log_addr, size_t *log_size) |
| 328 | { |
| 329 | /* Event Log size */ |
| 330 | size_t num_bytes = (uintptr_t)log_ptr - (uintptr_t)event_log; |
| 331 | int rc; |
| 332 | |
| 333 | assert(log_addr != NULL); |
| 334 | assert(log_size != NULL); |
| 335 | |
| 336 | if (nt_fw_config_base == 0UL) { |
| 337 | ERROR("%s(): %s_FW_CONFIG not loaded\n", __func__, "NT"); |
| 338 | return -ENOENT; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Set Event Log data in NT_FW_CONFIG and |
| 343 | * get Event Log address in Non-Secure memory |
| 344 | */ |
| 345 | if (plat_data_ptr->set_nt_fw_info != NULL) { |
| 346 | |
| 347 | /* Event Log address in Non-Secure memory */ |
| 348 | uintptr_t ns_log_addr; |
| 349 | |
| 350 | rc = plat_data_ptr->set_nt_fw_info( |
| 351 | nt_fw_config_base, |
| 352 | #ifdef SPD_opteed |
| 353 | (uintptr_t)event_log, |
| 354 | #endif |
| 355 | num_bytes, &ns_log_addr); |
| 356 | if (rc != 0) { |
| 357 | ERROR("%s(): Unable to update %s_FW_CONFIG\n", |
| 358 | __func__, "NT"); |
| 359 | return rc; |
| 360 | } |
| 361 | |
| 362 | /* Copy Event Log to Non-secure memory */ |
| 363 | (void)memcpy((void *)ns_log_addr, (const void *)event_log, |
| 364 | num_bytes); |
| 365 | |
| 366 | /* Ensure that the Event Log is visible in Non-secure memory */ |
| 367 | flush_dcache_range(ns_log_addr, num_bytes); |
| 368 | |
| 369 | /* Return Event Log address in Non-Secure memory */ |
| 370 | *log_addr = (uint8_t *)ns_log_addr; |
| 371 | |
| 372 | } else { |
| 373 | INFO("%s(): set_%s_fw_info not set\n", __func__, "nt"); |
| 374 | |
| 375 | /* Return Event Log address in Secure memory */ |
| 376 | *log_addr = event_log; |
| 377 | } |
| 378 | |
| 379 | if (tos_fw_config_base != 0UL) { |
| 380 | if (plat_data_ptr->set_tos_fw_info != NULL) { |
| 381 | |
| 382 | /* Set Event Log data in TOS_FW_CONFIG */ |
| 383 | rc = plat_data_ptr->set_tos_fw_info( |
| 384 | tos_fw_config_base, |
| 385 | (uintptr_t)event_log, |
| 386 | num_bytes); |
| 387 | if (rc != 0) { |
| 388 | ERROR("%s(): Unable to update %s_FW_CONFIG\n", |
| 389 | __func__, "TOS"); |
| 390 | return rc; |
| 391 | } |
| 392 | } else { |
| 393 | INFO("%s(): set_%s_fw_info not set\n", __func__, "tos"); |
| 394 | } |
| 395 | } else { |
| 396 | INFO("%s(): %s_FW_CONFIG not loaded\n", __func__, "TOS"); |
| 397 | } |
| 398 | |
| 399 | /* Ensure that the Event Log is visible in Secure memory */ |
| 400 | flush_dcache_range((uintptr_t)event_log, num_bytes); |
| 401 | |
| 402 | /* Return Event Log size */ |
| 403 | *log_size = num_bytes; |
| 404 | |
| 405 | return 0; |
| 406 | } |