1 /* 2 * Copyright (c) 2025, Arm Limited. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <stdarg.h> 9 #include <stdint.h> 10 11 #include <plat/arm/common/plat_arm.h> 12 #include <plat/common/platform.h> 13 #include <platform_def.h> 14 15 #include <tpm2.h> 16 #include <tpm2_chip.h> 17 18 #include <common/desc_image_load.h> 19 #include <common/ep_info.h> 20 #include <drivers/auth/crypto_mod.h> 21 #include <drivers/delay_timer.h> 22 #include <drivers/gpio_spi.h> 23 #include <drivers/measured_boot/metadata.h> 24 #include <drivers/tpm/tpm2_slb9670/slb9670_gpio.h> 25 #include <event_measure.h> 26 #include <event_print.h> 27 #include <rpi_shared.h> 28 29 /* Event Log data */ 30 uint8_t event_log[PLAT_ARM_EVENT_LOG_MAX_SIZE]; 31 static const struct event_log_hash_info crypto_hash_info = { 32 .func = crypto_mod_calc_hash, 33 .ids = (const uint32_t[]){ CRYPTO_MD_ID }, 34 .count = 1U, 35 }; 36 37 /* RPI3 table with platform specific image IDs, names and PCRs */ 38 const event_log_metadata_t rpi3_event_log_metadata[] = { 39 { FW_CONFIG_ID, MBOOT_FW_CONFIG_STRING, PCR_0 }, 40 { TB_FW_CONFIG_ID, MBOOT_TB_FW_CONFIG_STRING, PCR_0 }, 41 { BL2_IMAGE_ID, MBOOT_BL2_IMAGE_STRING, PCR_0 }, 42 43 { EVLOG_INVALID_ID, NULL, (unsigned int)(-1) } /* Terminator */ 44 }; 45 46 #if DISCRETE_TPM 47 extern struct tpm_chip_data tpm_chip_data; 48 #if (TPM_INTERFACE == FIFO_SPI) 49 50 #endif 51 52 static void rpi3_bl1_tpm_early_interface_setup(void) 53 { 54 #if TPM_INTERFACE_FIFO_SPI 55 int rc; 56 struct spi_plat *spidev; 57 const struct tpm_timeout_ops timeout_ops = { 58 .timeout_init_us = timeout_init_us, 59 .timeout_elapsed = timeout_elapsed 60 }; 61 const struct gpio_spi_config *tpm_rpi3_gpio_data = 62 tpm2_slb9670_get_config(); 63 64 tpm2_slb9670_gpio_init(tpm_rpi3_gpio_data); 65 66 tpm2_slb9670_reset_chip(tpm_rpi3_gpio_data); 67 68 spidev = gpio_spi_init(tpm_rpi3_gpio_data); 69 70 rc = tpm_interface_init(spidev, &timeout_ops, &tpm_chip_data, 0); 71 if (rc != 0) { 72 ERROR("BL1: TPM interface init failed\n"); 73 panic(); 74 } 75 76 #endif 77 } 78 #endif 79 80 void bl1_plat_mboot_init(void) 81 { 82 #if DISCRETE_TPM 83 int rc; 84 85 rpi3_bl1_tpm_early_interface_setup(); 86 rc = tpm_startup(&tpm_chip_data, TPM_SU_CLEAR); 87 if (rc != 0) { 88 ERROR("BL1: TPM Startup failed\n"); 89 panic(); 90 } 91 #endif 92 93 rc = event_log_init_and_reg(event_log, event_log + sizeof(event_log), 94 &crypto_hash_info); 95 if (rc < 0) { 96 ERROR("Failed to initialize event log (%d).\n", rc); 97 panic(); 98 } 99 100 rc = event_log_write_header(); 101 if (rc < 0) { 102 ERROR("Failed to write event log header (%d).\n", rc); 103 panic(); 104 } 105 } 106 107 void bl1_plat_mboot_finish(void) 108 { 109 size_t event_log_cur_size; 110 image_desc_t *image_desc; 111 entry_point_info_t *ep_info; 112 113 event_log_cur_size = event_log_get_cur_size(event_log); 114 image_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID); 115 assert(image_desc != NULL); 116 117 /* Get the entry point info */ 118 ep_info = &image_desc->ep_info; 119 ep_info->args.arg2 = (uint64_t) event_log; 120 ep_info->args.arg3 = (uint32_t) event_log_cur_size; 121 122 #if DISCRETE_TPM 123 int rc; 124 125 /* relinquish control of TPM locality 0 and close interface */ 126 rc = tpm_interface_close(&tpm_chip_data, 0); 127 if (rc != 0) { 128 ERROR("BL1: TPM interface close failed\n"); 129 panic(); 130 } 131 #endif 132 } 133 134 int plat_mboot_measure_image(unsigned int image_id, image_info_t *image_data) 135 { 136 int rc = 0; 137 unsigned char hash_data[CRYPTO_MD_MAX_SIZE]; 138 const event_log_metadata_t *metadata_ptr = rpi3_event_log_metadata; 139 140 rc = event_log_measure(image_data->image_base, image_data->image_size, hash_data); 141 if (rc != 0) { 142 return rc; 143 } 144 145 #if DISCRETE_TPM 146 rc = tpm_pcr_extend(&tpm_chip_data, 0, TPM_ALG_ID, hash_data, TCG_DIGEST_SIZE); 147 if (rc != 0) { 148 ERROR("BL1: TPM PCR-0 extend failed\n"); 149 panic(); 150 } 151 #endif 152 153 while ((metadata_ptr->id != EVLOG_INVALID_ID) && 154 (metadata_ptr->id != image_id)) { 155 metadata_ptr++; 156 } 157 assert(metadata_ptr->id != EVLOG_INVALID_ID); 158 159 event_log_record(hash_data, EV_POST_CODE, metadata_ptr); 160 161 /* Dump Event Log for user view */ 162 event_log_dump((uint8_t *)event_log, event_log_get_cur_size(event_log)); 163 164 return rc; 165 } 166