xref: /rk3399_ARM-atf/plat/arm/board/fvp/fvp_bl2_measured_boot.c (revision f4b8470feee4437fb3984baeee8c61ed91f63f51)
1 /*
2  * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stdint.h>
8 
9 #include <common/tbbr/tbbr_img_def.h>
10 #include <drivers/measured_boot/event_log/event_log.h>
11 #include <drivers/measured_boot/rss/rss_measured_boot.h>
12 #include <tools_share/tbbr_oid.h>
13 #include <fvp_critical_data.h>
14 
15 #include <plat/arm/common/plat_arm.h>
16 #include <plat/common/common_def.h>
17 
18 /* Event Log data */
19 static uint64_t event_log_base;
20 
21 /* FVP table with platform specific image IDs, names and PCRs */
22 const event_log_metadata_t fvp_event_log_metadata[] = {
23 	{ BL31_IMAGE_ID, EVLOG_BL31_STRING, PCR_0 },
24 	{ BL32_IMAGE_ID, EVLOG_BL32_STRING, PCR_0 },
25 	{ BL32_EXTRA1_IMAGE_ID, EVLOG_BL32_EXTRA1_STRING, PCR_0 },
26 	{ BL32_EXTRA2_IMAGE_ID, EVLOG_BL32_EXTRA2_STRING, PCR_0 },
27 	{ BL33_IMAGE_ID, EVLOG_BL33_STRING, PCR_0 },
28 	{ HW_CONFIG_ID, EVLOG_HW_CONFIG_STRING, PCR_0 },
29 	{ NT_FW_CONFIG_ID, EVLOG_NT_FW_CONFIG_STRING, PCR_0 },
30 	{ SCP_BL2_IMAGE_ID, EVLOG_SCP_BL2_STRING, PCR_0 },
31 	{ SOC_FW_CONFIG_ID, EVLOG_SOC_FW_CONFIG_STRING, PCR_0 },
32 	{ TOS_FW_CONFIG_ID, EVLOG_TOS_FW_CONFIG_STRING, PCR_0 },
33 	{ RMM_IMAGE_ID, EVLOG_RMM_STRING, PCR_0},
34 
35 #if defined(SPD_spmd)
36 	{ SP_PKG1_ID, EVLOG_SP1_STRING, PCR_0 },
37 	{ SP_PKG2_ID, EVLOG_SP2_STRING, PCR_0 },
38 	{ SP_PKG3_ID, EVLOG_SP3_STRING, PCR_0 },
39 	{ SP_PKG4_ID, EVLOG_SP4_STRING, PCR_0 },
40 	{ SP_PKG5_ID, EVLOG_SP5_STRING, PCR_0 },
41 	{ SP_PKG6_ID, EVLOG_SP6_STRING, PCR_0 },
42 	{ SP_PKG7_ID, EVLOG_SP7_STRING, PCR_0 },
43 	{ SP_PKG8_ID, EVLOG_SP8_STRING, PCR_0 },
44 #endif
45 
46 	{ CRITICAL_DATA_ID, EVLOG_CRITICAL_DATA_STRING, PCR_1 },
47 
48 	{ EVLOG_INVALID_ID, NULL, (unsigned int)(-1) }	/* Terminator */
49 };
50 
51 /* FVP table with platform specific image IDs and metadata. Intentionally not a
52  * const struct, some members might set by bootloaders during trusted boot.
53  */
54 struct rss_mboot_metadata fvp_rss_mboot_metadata[] = {
55 	{
56 		.id = BL31_IMAGE_ID,
57 		.slot = U(9),
58 		.signer_id_size = SIGNER_ID_MIN_SIZE,
59 		.sw_type = RSS_MBOOT_BL31_STRING,
60 		.lock_measurement = true },
61 	{
62 		.id = HW_CONFIG_ID,
63 		.slot = U(10),
64 		.signer_id_size = SIGNER_ID_MIN_SIZE,
65 		.sw_type = RSS_MBOOT_HW_CONFIG_STRING,
66 		.lock_measurement = true },
67 	{
68 		.id = SOC_FW_CONFIG_ID,
69 		.slot = U(11),
70 		.signer_id_size = SIGNER_ID_MIN_SIZE,
71 		.sw_type = RSS_MBOOT_SOC_FW_CONFIG_STRING,
72 		.lock_measurement = true },
73 	{
74 		.id = RMM_IMAGE_ID,
75 		.slot = U(12),
76 		.signer_id_size = SIGNER_ID_MIN_SIZE,
77 		.sw_type = RSS_MBOOT_RMM_STRING,
78 		.lock_measurement = true },
79 	{
80 		.id = RSS_MBOOT_INVALID_ID }
81 };
82 
83 void bl2_plat_mboot_init(void)
84 {
85 	uint8_t *event_log_start;
86 	uint8_t *event_log_finish;
87 	size_t bl1_event_log_size;
88 	int rc;
89 
90 	rc = arm_get_tb_fw_info(&event_log_base, &bl1_event_log_size);
91 	if (rc != 0) {
92 		ERROR("%s(): Unable to get Event Log info from TB_FW_CONFIG\n",
93 		      __func__);
94 		/*
95 		 * It is a fatal error because on FVP platform, BL2 software
96 		 * assumes that a valid Event Log buffer exist and it will use
97 		 * same Event Log buffer to append image measurements.
98 		 */
99 		panic();
100 	}
101 
102 	/*
103 	 * BL1 and BL2 share the same Event Log buffer and that BL2 will
104 	 * append its measurements after BL1's
105 	 */
106 	event_log_start = (uint8_t *)((uintptr_t)event_log_base +
107 				      bl1_event_log_size);
108 	event_log_finish = (uint8_t *)((uintptr_t)event_log_base +
109 				       PLAT_ARM_EVENT_LOG_MAX_SIZE);
110 
111 	event_log_init((uint8_t *)event_log_start, event_log_finish);
112 
113 	rss_measured_boot_init();
114 }
115 
116 int plat_mboot_measure_critical_data(unsigned int critical_data_id,
117 				     const void *base, size_t size)
118 {
119 	/*
120 	 * It is very unlikely that the critical data size would be
121 	 * bigger than 2^32 bytes
122 	 */
123 	assert(size < UINT32_MAX);
124 	assert(base != NULL);
125 
126 	/* Calculate image hash and record data in Event Log */
127 	int err = event_log_measure_and_record((uintptr_t)base, (uint32_t)size,
128 					       critical_data_id);
129 	if (err != 0) {
130 		ERROR("%s%s critical data (%i)\n",
131 		      "Failed to ", "record",  err);
132 		return err;
133 	}
134 
135 	return 0;
136 }
137 
138 #if TRUSTED_BOARD_BOOT
139 static int fvp_populate_critical_data(struct fvp_critical_data *critical_data)
140 {
141 	char *nv_ctr_oids[MAX_NV_CTR_IDS] = {
142 		[TRUSTED_NV_CTR_ID] = TRUSTED_FW_NVCOUNTER_OID,
143 		[NON_TRUSTED_NV_CTR_ID] = NON_TRUSTED_FW_NVCOUNTER_OID,
144 	};
145 
146 	for (int i = 0; i < MAX_NV_CTR_IDS; i++) {
147 		int rc = plat_get_nv_ctr(nv_ctr_oids[i],
148 					 &critical_data->nv_ctr[i]);
149 		if (rc != 0) {
150 			return rc;
151 		}
152 	}
153 
154 	return 0;
155 }
156 #endif /* TRUSTED_BOARD_BOOT */
157 
158 static int fvp_populate_and_measure_critical_data(void)
159 {
160 	int rc = 0;
161 
162 /*
163  * FVP platform only measures 'platform NV-counter' and hence its
164  * measurement makes sense during Trusted-Boot flow only.
165  */
166 #if TRUSTED_BOARD_BOOT
167 	struct fvp_critical_data populate_critical_data;
168 
169 	rc = fvp_populate_critical_data(&populate_critical_data);
170 	if (rc == 0) {
171 		rc = plat_mboot_measure_critical_data(CRITICAL_DATA_ID,
172 						&populate_critical_data,
173 						sizeof(populate_critical_data));
174 	}
175 #endif /* TRUSTED_BOARD_BOOT */
176 
177 	return rc;
178 }
179 
180 void bl2_plat_mboot_finish(void)
181 {
182 	int rc;
183 
184 	/* Event Log address in Non-Secure memory */
185 	uintptr_t ns_log_addr;
186 
187 	/* Event Log filled size */
188 	size_t event_log_cur_size;
189 
190 	rc = fvp_populate_and_measure_critical_data();
191 	if (rc != 0) {
192 		panic();
193 	}
194 
195 	event_log_cur_size = event_log_get_cur_size((uint8_t *)event_log_base);
196 
197 	rc = arm_set_nt_fw_info(
198 #ifdef SPD_opteed
199 			    (uintptr_t)event_log_base,
200 #endif
201 			    event_log_cur_size, &ns_log_addr);
202 	if (rc != 0) {
203 		ERROR("%s(): Unable to update %s_FW_CONFIG\n",
204 		      __func__, "NT");
205 		/*
206 		 * It is a fatal error because on FVP secure world software
207 		 * assumes that a valid event log exists and will use it to
208 		 * record the measurements into the fTPM.
209 		 * Note: In FVP platform, OP-TEE uses nt_fw_config to get the
210 		 * secure Event Log buffer address.
211 		 */
212 		panic();
213 	}
214 
215 	/* Copy Event Log to Non-secure memory */
216 	(void)memcpy((void *)ns_log_addr, (const void *)event_log_base,
217 		     event_log_cur_size);
218 
219 	/* Ensure that the Event Log is visible in Non-secure memory */
220 	flush_dcache_range(ns_log_addr, event_log_cur_size);
221 
222 #if defined(SPD_tspd) || defined(SPD_spmd)
223 	/* Set Event Log data in TOS_FW_CONFIG */
224 	rc = arm_set_tos_fw_info((uintptr_t)event_log_base,
225 				 event_log_cur_size);
226 	if (rc != 0) {
227 		ERROR("%s(): Unable to update %s_FW_CONFIG\n",
228 		      __func__, "TOS");
229 		panic();
230 	}
231 #endif /* defined(SPD_tspd) || defined(SPD_spmd) */
232 
233 	dump_event_log((uint8_t *)event_log_base, event_log_cur_size);
234 }
235