140814266SManish V Badarkhe /* 2*2ec44880SManish V Badarkhe * Copyright (c) 2022-2025 Arm Limited. All rights reserved. 340814266SManish V Badarkhe * 440814266SManish V Badarkhe * SPDX-License-Identifier: BSD-3-Clause 540814266SManish V Badarkhe * 640814266SManish V Badarkhe * DRTM measurements into TPM PCRs. 740814266SManish V Badarkhe * 840814266SManish V Badarkhe * Authors: 940814266SManish V Badarkhe * Lucian Paul-Trifu <lucian.paultrifu@gmail.com> 1040814266SManish V Badarkhe * 1140814266SManish V Badarkhe */ 1240814266SManish V Badarkhe #include <assert.h> 1340814266SManish V Badarkhe 1440814266SManish V Badarkhe #include <common/debug.h> 1540814266SManish V Badarkhe #include <drivers/auth/crypto_mod.h> 1640814266SManish V Badarkhe #include <drivers/measured_boot/event_log/event_log.h> 1740814266SManish V Badarkhe #include "drtm_main.h" 1840814266SManish V Badarkhe #include "drtm_measurements.h" 1940814266SManish V Badarkhe #include <lib/xlat_tables/xlat_tables_v2.h> 2040814266SManish V Badarkhe 2140814266SManish V Badarkhe /* Event Log buffer */ 2240814266SManish V Badarkhe static uint8_t drtm_event_log[PLAT_DRTM_EVENT_LOG_MAX_SIZE]; 2340814266SManish V Badarkhe 2440814266SManish V Badarkhe /* 2540814266SManish V Badarkhe * Calculate and write hash of various payloads as per DRTM specification 2640814266SManish V Badarkhe * to Event Log. 2740814266SManish V Badarkhe * 2840814266SManish V Badarkhe * @param[in] data_base Address of data 2940814266SManish V Badarkhe * @param[in] data_size Size of data 3040814266SManish V Badarkhe * @param[in] event_type Type of Event 3140814266SManish V Badarkhe * @param[in] event_name Name of the Event 3240814266SManish V Badarkhe * @return: 3340814266SManish V Badarkhe * 0 = success 3440814266SManish V Badarkhe * < 0 = error 3540814266SManish V Badarkhe */ 3640814266SManish V Badarkhe static int drtm_event_log_measure_and_record(uintptr_t data_base, 3740814266SManish V Badarkhe uint32_t data_size, 3840814266SManish V Badarkhe uint32_t event_type, 3940814266SManish V Badarkhe const char *event_name, 4040814266SManish V Badarkhe unsigned int pcr) 4140814266SManish V Badarkhe { 4240814266SManish V Badarkhe int rc; 4340814266SManish V Badarkhe unsigned char hash_data[CRYPTO_MD_MAX_SIZE]; 4440814266SManish V Badarkhe event_log_metadata_t metadata = {0}; 4540814266SManish V Badarkhe 4640814266SManish V Badarkhe metadata.name = event_name; 4740814266SManish V Badarkhe metadata.pcr = pcr; 4840814266SManish V Badarkhe 4940814266SManish V Badarkhe /* 501b491eeaSElyes Haouas * Measure the payloads requested by D-CRTM and DCE components 5140814266SManish V Badarkhe * Hash algorithm decided by the Event Log driver at build-time 5240814266SManish V Badarkhe */ 5340814266SManish V Badarkhe rc = event_log_measure(data_base, data_size, hash_data); 5440814266SManish V Badarkhe if (rc != 0) { 5540814266SManish V Badarkhe return rc; 5640814266SManish V Badarkhe } 5740814266SManish V Badarkhe 5840814266SManish V Badarkhe /* Record the mesasurement in the EventLog buffer */ 5940814266SManish V Badarkhe event_log_record(hash_data, event_type, &metadata); 6040814266SManish V Badarkhe 6140814266SManish V Badarkhe return 0; 6240814266SManish V Badarkhe } 6340814266SManish V Badarkhe 6440814266SManish V Badarkhe /* 6540814266SManish V Badarkhe * Initialise Event Log global variables, used during the recording 6640814266SManish V Badarkhe * of various payload measurements into the Event Log buffer 6740814266SManish V Badarkhe * 6840814266SManish V Badarkhe * @param[in] event_log_start Base address of Event Log buffer 6940814266SManish V Badarkhe * @param[in] event_log_finish End address of Event Log buffer, 7040814266SManish V Badarkhe * it is a first byte past end of the 7140814266SManish V Badarkhe * buffer 7240814266SManish V Badarkhe */ 7340814266SManish V Badarkhe static void drtm_event_log_init(uint8_t *event_log_start, 7440814266SManish V Badarkhe uint8_t *event_log_finish) 7540814266SManish V Badarkhe { 7640814266SManish V Badarkhe event_log_buf_init(event_log_start, event_log_finish); 7740814266SManish V Badarkhe event_log_write_specid_event(); 7840814266SManish V Badarkhe } 7940814266SManish V Badarkhe 8040814266SManish V Badarkhe enum drtm_retc drtm_take_measurements(const struct_drtm_dl_args *a) 8140814266SManish V Badarkhe { 8240814266SManish V Badarkhe int rc; 8340814266SManish V Badarkhe uintptr_t dlme_img_mapping; 8440814266SManish V Badarkhe uint64_t dlme_img_ep; 8540814266SManish V Badarkhe size_t dlme_img_mapping_bytes; 8640814266SManish V Badarkhe uint8_t drtm_null_data = 0U; 8740814266SManish V Badarkhe uint8_t pcr_schema = DL_ARGS_GET_PCR_SCHEMA(a); 8840814266SManish V Badarkhe const char *drtm_event_arm_sep_data = "ARM_DRTM"; 8940814266SManish V Badarkhe 9040814266SManish V Badarkhe /* Initialise the EventLog driver */ 9140814266SManish V Badarkhe drtm_event_log_init(drtm_event_log, drtm_event_log + 9240814266SManish V Badarkhe sizeof(drtm_event_log)); 9340814266SManish V Badarkhe 9440814266SManish V Badarkhe /** 9540814266SManish V Badarkhe * Measurements extended into PCR-17. 9640814266SManish V Badarkhe * 9740814266SManish V Badarkhe * PCR-17: Measure the DCE image. Extend digest of (char)0 into PCR-17 9840814266SManish V Badarkhe * since the D-CRTM and the DCE are not separate. 9940814266SManish V Badarkhe */ 10040814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)&drtm_null_data, 10140814266SManish V Badarkhe sizeof(drtm_null_data), 10240814266SManish V Badarkhe DRTM_EVENT_ARM_DCE, NULL, 10340814266SManish V Badarkhe PCR_17); 10440814266SManish V Badarkhe CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DCE)); 10540814266SManish V Badarkhe 10640814266SManish V Badarkhe /* PCR-17: Measure the PCR schema DRTM launch argument. */ 10740814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)&pcr_schema, 10840814266SManish V Badarkhe sizeof(pcr_schema), 10940814266SManish V Badarkhe DRTM_EVENT_ARM_PCR_SCHEMA, 11040814266SManish V Badarkhe NULL, PCR_17); 11140814266SManish V Badarkhe CHECK_RC(rc, 11240814266SManish V Badarkhe drtm_event_log_measure_and_record(DRTM_EVENT_ARM_PCR_SCHEMA)); 11340814266SManish V Badarkhe 11440814266SManish V Badarkhe /* PCR-17: Measure the enable state of external-debug, and trace. */ 11540814266SManish V Badarkhe /* 11640814266SManish V Badarkhe * TODO: Measure the enable state of external-debug and trace. This should 11740814266SManish V Badarkhe * be returned through a platform-specific hook. 11840814266SManish V Badarkhe */ 11940814266SManish V Badarkhe 12040814266SManish V Badarkhe /* PCR-17: Measure the security lifecycle state. */ 12140814266SManish V Badarkhe /* 12240814266SManish V Badarkhe * TODO: Measure the security lifecycle state. This is an implementation- 12340814266SManish V Badarkhe * defined value, retrieved through an implementation-defined mechanisms. 12440814266SManish V Badarkhe */ 12540814266SManish V Badarkhe 12640814266SManish V Badarkhe /* 12740814266SManish V Badarkhe * PCR-17: Optionally measure the NWd DCE. 12840814266SManish V Badarkhe * It is expected that such subsequent DCE stages are signed and verified. 12940814266SManish V Badarkhe * Whether they are measured in addition to signing is implementation 13040814266SManish V Badarkhe * -defined. 13140814266SManish V Badarkhe * Here the choice is to not measure any NWd DCE, in favour of PCR value 13240814266SManish V Badarkhe * resilience to any NWd DCE updates. 13340814266SManish V Badarkhe */ 13440814266SManish V Badarkhe 13540814266SManish V Badarkhe /* PCR-17: End of DCE measurements. */ 13640814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)drtm_event_arm_sep_data, 13740814266SManish V Badarkhe strlen(drtm_event_arm_sep_data), 13840814266SManish V Badarkhe DRTM_EVENT_ARM_SEPARATOR, NULL, 13940814266SManish V Badarkhe PCR_17); 14040814266SManish V Badarkhe CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_SEPARATOR)); 14140814266SManish V Badarkhe 14240814266SManish V Badarkhe /** 14340814266SManish V Badarkhe * Measurements extended into PCR-18. 14440814266SManish V Badarkhe * 14540814266SManish V Badarkhe * PCR-18: Measure the PCR schema DRTM launch argument. 14640814266SManish V Badarkhe */ 14740814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)&pcr_schema, 14840814266SManish V Badarkhe sizeof(pcr_schema), 14940814266SManish V Badarkhe DRTM_EVENT_ARM_PCR_SCHEMA, 15040814266SManish V Badarkhe NULL, PCR_18); 15140814266SManish V Badarkhe CHECK_RC(rc, 15240814266SManish V Badarkhe drtm_event_log_measure_and_record(DRTM_EVENT_ARM_PCR_SCHEMA)); 15340814266SManish V Badarkhe 15440814266SManish V Badarkhe /* 15540814266SManish V Badarkhe * PCR-18: Measure the public key used to verify DCE image(s) signatures. 15640814266SManish V Badarkhe * Extend digest of (char)0, since we do not expect the NWd DCE to be 15740814266SManish V Badarkhe * present. 15840814266SManish V Badarkhe */ 15940814266SManish V Badarkhe assert(a->dce_nwd_size == 0); 16040814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)&drtm_null_data, 16140814266SManish V Badarkhe sizeof(drtm_null_data), 16240814266SManish V Badarkhe DRTM_EVENT_ARM_DCE_PUBKEY, 16340814266SManish V Badarkhe NULL, PCR_18); 16440814266SManish V Badarkhe CHECK_RC(rc, 16540814266SManish V Badarkhe drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DCE_PUBKEY)); 16640814266SManish V Badarkhe 16740814266SManish V Badarkhe /* PCR-18: Measure the DLME image. */ 16840814266SManish V Badarkhe dlme_img_mapping_bytes = page_align(a->dlme_img_size, UP); 16940814266SManish V Badarkhe rc = mmap_add_dynamic_region_alloc_va(a->dlme_paddr + a->dlme_img_off, 17040814266SManish V Badarkhe &dlme_img_mapping, 17140814266SManish V Badarkhe dlme_img_mapping_bytes, MT_RO_DATA | MT_NS); 17240814266SManish V Badarkhe if (rc) { 17340814266SManish V Badarkhe WARN("DRTM: %s: mmap_add_dynamic_region() failed rc=%d\n", 17440814266SManish V Badarkhe __func__, rc); 17540814266SManish V Badarkhe return INTERNAL_ERROR; 17640814266SManish V Badarkhe } 17740814266SManish V Badarkhe 17840814266SManish V Badarkhe rc = drtm_event_log_measure_and_record(dlme_img_mapping, a->dlme_img_size, 17940814266SManish V Badarkhe DRTM_EVENT_ARM_DLME, NULL, 18040814266SManish V Badarkhe PCR_18); 18140814266SManish V Badarkhe CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DLME)); 18240814266SManish V Badarkhe 18340814266SManish V Badarkhe rc = mmap_remove_dynamic_region(dlme_img_mapping, dlme_img_mapping_bytes); 18440814266SManish V Badarkhe CHECK_RC(rc, mmap_remove_dynamic_region); 18540814266SManish V Badarkhe 18640814266SManish V Badarkhe /* PCR-18: Measure the DLME image entry point. */ 18740814266SManish V Badarkhe dlme_img_ep = DL_ARGS_GET_DLME_ENTRY_POINT(a); 18840814266SManish V Badarkhe drtm_event_log_measure_and_record((uintptr_t)&dlme_img_ep, 18940814266SManish V Badarkhe sizeof(dlme_img_ep), 19040814266SManish V Badarkhe DRTM_EVENT_ARM_DLME_EP, NULL, 19140814266SManish V Badarkhe PCR_18); 19240814266SManish V Badarkhe CHECK_RC(rc, drtm_event_log_measure_and_record(DRTM_EVENT_ARM_DLME_EP)); 19340814266SManish V Badarkhe 19440814266SManish V Badarkhe /* PCR-18: End of DCE measurements. */ 19540814266SManish V Badarkhe rc = drtm_event_log_measure_and_record((uintptr_t)drtm_event_arm_sep_data, 19640814266SManish V Badarkhe strlen(drtm_event_arm_sep_data), 19740814266SManish V Badarkhe DRTM_EVENT_ARM_SEPARATOR, NULL, 19840814266SManish V Badarkhe PCR_18); 19940814266SManish V Badarkhe CHECK_RC(rc, 20040814266SManish V Badarkhe drtm_event_log_measure_and_record(DRTM_EVENT_ARM_SEPARATOR)); 201*2ec44880SManish V Badarkhe 202*2ec44880SManish V Badarkhe /* Measure no Action event but not extend it in PCR */ 203*2ec44880SManish V Badarkhe CHECK_RC(rc, 204*2ec44880SManish V Badarkhe drtm_event_log_measure_and_record(DRTM_EVENT_ARM_NO_ACTION)); 20540814266SManish V Badarkhe /* 20640814266SManish V Badarkhe * If the DCE is unable to log a measurement because there is no available 20740814266SManish V Badarkhe * space in the event log region, the DCE must extend a hash of the value 20840814266SManish V Badarkhe * 0xFF (1 byte in size) into PCR[17] and PCR[18] and enter remediation. 20940814266SManish V Badarkhe */ 21040814266SManish V Badarkhe 21140814266SManish V Badarkhe return SUCCESS; 21240814266SManish V Badarkhe } 21340814266SManish V Badarkhe 21440814266SManish V Badarkhe void drtm_serialise_event_log(uint8_t *dst, size_t *event_log_size_out) 21540814266SManish V Badarkhe { 21640814266SManish V Badarkhe *event_log_size_out = event_log_get_cur_size(drtm_event_log); 21740814266SManish V Badarkhe memcpy(dst, drtm_event_log, *event_log_size_out); 21840814266SManish V Badarkhe } 219