blob: 8bf7dad69b0141c76082199064ae29ee9b7466f0 [file] [log] [blame]
Alexei Fedorov61369a22020-07-13 14:59:02 +01001/*
Tamas Banb1cc0692023-06-05 11:11:28 +02002 * Copyright (c) 2021-2024, Arm Limited. All rights reserved.
Alexei Fedorov61369a22020-07-13 14:59:02 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Manish V Badarkhe5797b802021-08-06 09:26:20 +01007#include <stdint.h>
8
Imre Kisa48afe52022-04-11 16:33:13 +02009#include <common/tbbr/tbbr_img_def.h>
Sandrine Bailleux3c2db6f2021-07-07 14:47:08 +020010#include <drivers/measured_boot/event_log/event_log.h>
Tamas Banb1cc0692023-06-05 11:11:28 +020011#include <drivers/measured_boot/metadata.h>
Manish V Badarkhe95af6a02023-04-11 14:46:10 +010012#if defined(ARM_COT_cca)
13#include <tools_share/cca_oid.h>
14#else
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010015#include <tools_share/tbbr_oid.h>
Manish V Badarkhe95af6a02023-04-11 14:46:10 +010016#endif /* ARM_COT_cca */
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010017#include <fvp_critical_data.h>
18
Alexei Fedorov61369a22020-07-13 14:59:02 +010019#include <plat/arm/common/plat_arm.h>
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010020#include <plat/common/common_def.h>
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010021
Manish V Badarkhe550cb592023-02-18 20:04:43 +000022#if defined(SPD_tspd) || defined(SPD_opteed) || defined(SPD_spmd)
23CASSERT(ARM_EVENT_LOG_DRAM1_SIZE >= PLAT_ARM_EVENT_LOG_MAX_SIZE, \
24 assert_res_eventlog_mem_insufficient);
25#endif /* defined(SPD_tspd) || defined(SPD_opteed) || defined(SPD_spmd) */
26
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010027/* Event Log data */
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010028static uint64_t event_log_base;
Alexei Fedorov61369a22020-07-13 14:59:02 +010029
30/* FVP table with platform specific image IDs, names and PCRs */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010031const event_log_metadata_t fvp_event_log_metadata[] = {
Tamas Banb1cc0692023-06-05 11:11:28 +020032 { BL31_IMAGE_ID, MBOOT_BL31_IMAGE_STRING, PCR_0 },
33 { BL32_IMAGE_ID, MBOOT_BL32_IMAGE_STRING, PCR_0 },
34 { BL32_EXTRA1_IMAGE_ID, MBOOT_BL32_EXTRA1_IMAGE_STRING, PCR_0 },
35 { BL32_EXTRA2_IMAGE_ID, MBOOT_BL32_EXTRA2_IMAGE_STRING, PCR_0 },
36 { BL33_IMAGE_ID, MBOOT_BL33_IMAGE_STRING, PCR_0 },
37 { HW_CONFIG_ID, MBOOT_HW_CONFIG_STRING, PCR_0 },
38 { NT_FW_CONFIG_ID, MBOOT_NT_FW_CONFIG_STRING, PCR_0 },
39 { SCP_BL2_IMAGE_ID, MBOOT_SCP_BL2_IMAGE_STRING, PCR_0 },
40 { SOC_FW_CONFIG_ID, MBOOT_SOC_FW_CONFIG_STRING, PCR_0 },
41 { TOS_FW_CONFIG_ID, MBOOT_TOS_FW_CONFIG_STRING, PCR_0 },
42 { RMM_IMAGE_ID, MBOOT_RMM_IMAGE_STRING, PCR_0},
Manish V Badarkhe67009c32021-10-31 14:47:49 +000043
Imre Kisa48afe52022-04-11 16:33:13 +020044#if defined(SPD_spmd)
Tamas Banb1cc0692023-06-05 11:11:28 +020045 { SP_PKG1_ID, MBOOT_SP1_STRING, PCR_0 },
46 { SP_PKG2_ID, MBOOT_SP2_STRING, PCR_0 },
47 { SP_PKG3_ID, MBOOT_SP3_STRING, PCR_0 },
48 { SP_PKG4_ID, MBOOT_SP4_STRING, PCR_0 },
49 { SP_PKG5_ID, MBOOT_SP5_STRING, PCR_0 },
50 { SP_PKG6_ID, MBOOT_SP6_STRING, PCR_0 },
51 { SP_PKG7_ID, MBOOT_SP7_STRING, PCR_0 },
52 { SP_PKG8_ID, MBOOT_SP8_STRING, PCR_0 },
Imre Kisa48afe52022-04-11 16:33:13 +020053#endif
54
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010055 { CRITICAL_DATA_ID, EVLOG_CRITICAL_DATA_STRING, PCR_1 },
56
Manish V Badarkhe67009c32021-10-31 14:47:49 +000057 { EVLOG_INVALID_ID, NULL, (unsigned int)(-1) } /* Terminator */
Alexei Fedorov61369a22020-07-13 14:59:02 +010058};
59
Manish V Badarkhe5797b802021-08-06 09:26:20 +010060void bl2_plat_mboot_init(void)
61{
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010062 uint8_t *event_log_start;
63 uint8_t *event_log_finish;
64 size_t bl1_event_log_size;
Manish V Badarkhe6e6df442023-03-20 14:58:06 +000065 size_t event_log_max_size;
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010066 int rc;
67
Manish V Badarkhe6e6df442023-03-20 14:58:06 +000068 rc = arm_get_tb_fw_info(&event_log_base, &bl1_event_log_size,
69 &event_log_max_size);
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010070 if (rc != 0) {
71 ERROR("%s(): Unable to get Event Log info from TB_FW_CONFIG\n",
72 __func__);
73 /*
74 * It is a fatal error because on FVP platform, BL2 software
75 * assumes that a valid Event Log buffer exist and it will use
76 * same Event Log buffer to append image measurements.
77 */
78 panic();
79 }
80
81 /*
82 * BL1 and BL2 share the same Event Log buffer and that BL2 will
83 * append its measurements after BL1's
84 */
85 event_log_start = (uint8_t *)((uintptr_t)event_log_base +
86 bl1_event_log_size);
87 event_log_finish = (uint8_t *)((uintptr_t)event_log_base +
Manish V Badarkhe6e6df442023-03-20 14:58:06 +000088 event_log_max_size);
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010089
90 event_log_init((uint8_t *)event_log_start, event_log_finish);
Manish V Badarkhe5797b802021-08-06 09:26:20 +010091}
92
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010093int plat_mboot_measure_critical_data(unsigned int critical_data_id,
94 const void *base, size_t size)
95{
96 /*
97 * It is very unlikely that the critical data size would be
98 * bigger than 2^32 bytes
99 */
100 assert(size < UINT32_MAX);
101 assert(base != NULL);
102
103 /* Calculate image hash and record data in Event Log */
104 int err = event_log_measure_and_record((uintptr_t)base, (uint32_t)size,
Manish V Badarkhedd1ee502022-11-18 20:27:21 +0000105 critical_data_id,
106 fvp_event_log_metadata);
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100107 if (err != 0) {
108 ERROR("%s%s critical data (%i)\n",
109 "Failed to ", "record", err);
110 return err;
111 }
112
113 return 0;
114}
115
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000116#if TRUSTED_BOARD_BOOT
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100117static int fvp_populate_critical_data(struct fvp_critical_data *critical_data)
118{
119 char *nv_ctr_oids[MAX_NV_CTR_IDS] = {
120 [TRUSTED_NV_CTR_ID] = TRUSTED_FW_NVCOUNTER_OID,
121 [NON_TRUSTED_NV_CTR_ID] = NON_TRUSTED_FW_NVCOUNTER_OID,
122 };
123
124 for (int i = 0; i < MAX_NV_CTR_IDS; i++) {
125 int rc = plat_get_nv_ctr(nv_ctr_oids[i],
126 &critical_data->nv_ctr[i]);
127 if (rc != 0) {
128 return rc;
129 }
130 }
131
132 return 0;
133}
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000134#endif /* TRUSTED_BOARD_BOOT */
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100135
136static int fvp_populate_and_measure_critical_data(void)
137{
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000138 int rc = 0;
139
140/*
141 * FVP platform only measures 'platform NV-counter' and hence its
142 * measurement makes sense during Trusted-Boot flow only.
143 */
144#if TRUSTED_BOARD_BOOT
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100145 struct fvp_critical_data populate_critical_data;
146
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000147 rc = fvp_populate_critical_data(&populate_critical_data);
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100148 if (rc == 0) {
149 rc = plat_mboot_measure_critical_data(CRITICAL_DATA_ID,
150 &populate_critical_data,
151 sizeof(populate_critical_data));
152 }
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000153#endif /* TRUSTED_BOARD_BOOT */
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100154
155 return rc;
156}
157
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100158void bl2_plat_mboot_finish(void)
159{
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100160 int rc;
161
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100162 /* Event Log address in Non-Secure memory */
163 uintptr_t ns_log_addr;
164
165 /* Event Log filled size */
166 size_t event_log_cur_size;
167
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100168 rc = fvp_populate_and_measure_critical_data();
169 if (rc != 0) {
170 panic();
171 }
172
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100173 event_log_cur_size = event_log_get_cur_size((uint8_t *)event_log_base);
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100174
Manish V Badarkhe550cb592023-02-18 20:04:43 +0000175#if defined(SPD_tspd) || defined(SPD_opteed) || defined(SPD_spmd)
176 /* Copy Event Log to TZC secured DRAM memory */
177 (void)memcpy((void *)ARM_EVENT_LOG_DRAM1_BASE,
178 (const void *)event_log_base,
179 event_log_cur_size);
180
181 /* Ensure that the Event Log is visible in TZC secured DRAM memory */
182 flush_dcache_range(ARM_EVENT_LOG_DRAM1_BASE, event_log_cur_size);
183#endif /* defined(SPD_tspd) || defined(SPD_opteed) || defined(SPD_spmd) */
184
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100185 rc = arm_set_nt_fw_info(
186#ifdef SPD_opteed
Manish V Badarkhe550cb592023-02-18 20:04:43 +0000187 (uintptr_t)ARM_EVENT_LOG_DRAM1_BASE,
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100188#endif
189 event_log_cur_size, &ns_log_addr);
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100190 if (rc != 0) {
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100191 ERROR("%s(): Unable to update %s_FW_CONFIG\n",
192 __func__, "NT");
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100193 /*
194 * It is a fatal error because on FVP secure world software
195 * assumes that a valid event log exists and will use it to
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100196 * record the measurements into the fTPM.
197 * Note: In FVP platform, OP-TEE uses nt_fw_config to get the
198 * secure Event Log buffer address.
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100199 */
200 panic();
201 }
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100202
203 /* Copy Event Log to Non-secure memory */
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100204 (void)memcpy((void *)ns_log_addr, (const void *)event_log_base,
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100205 event_log_cur_size);
206
207 /* Ensure that the Event Log is visible in Non-secure memory */
208 flush_dcache_range(ns_log_addr, event_log_cur_size);
209
210#if defined(SPD_tspd) || defined(SPD_spmd)
211 /* Set Event Log data in TOS_FW_CONFIG */
Manish V Badarkhe550cb592023-02-18 20:04:43 +0000212 rc = arm_set_tos_fw_info((uintptr_t)ARM_EVENT_LOG_DRAM1_BASE,
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100213 event_log_cur_size);
214 if (rc != 0) {
215 ERROR("%s(): Unable to update %s_FW_CONFIG\n",
216 __func__, "TOS");
217 panic();
218 }
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100219#endif /* defined(SPD_tspd) || defined(SPD_spmd) */
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100220
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100221 dump_event_log((uint8_t *)event_log_base, event_log_cur_size);
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100222}