blob: 29b66197d361774e2d70e26223c385accc99f069 [file] [log] [blame]
Alexei Fedorov61369a22020-07-13 14:59:02 +01001/*
Manish V Badarkheeba13bd2022-01-08 23:08:02 +00002 * Copyright (c) 2021-2022, 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 Banb0f83252022-02-11 09:49:36 +010011#include <drivers/measured_boot/rss/rss_measured_boot.h>
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010012#include <tools_share/tbbr_oid.h>
13#include <fvp_critical_data.h>
14
Alexei Fedorov61369a22020-07-13 14:59:02 +010015#include <plat/arm/common/plat_arm.h>
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010016#include <plat/common/common_def.h>
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010017
18/* Event Log data */
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010019static uint64_t event_log_base;
Alexei Fedorov61369a22020-07-13 14:59:02 +010020
21/* FVP table with platform specific image IDs, names and PCRs */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010022const event_log_metadata_t fvp_event_log_metadata[] = {
Manish V Badarkhe72e03692021-09-08 20:04:24 +010023 { 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 },
Tamas Ban6101c2a2022-01-10 15:13:00 +010033 { RMM_IMAGE_ID, EVLOG_RMM_STRING, PCR_0},
Manish V Badarkhe67009c32021-10-31 14:47:49 +000034
Imre Kisa48afe52022-04-11 16:33:13 +020035#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
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010046 { CRITICAL_DATA_ID, EVLOG_CRITICAL_DATA_STRING, PCR_1 },
47
Manish V Badarkhe67009c32021-10-31 14:47:49 +000048 { EVLOG_INVALID_ID, NULL, (unsigned int)(-1) } /* Terminator */
Alexei Fedorov61369a22020-07-13 14:59:02 +010049};
50
Tamas Banb0f83252022-02-11 09:49:36 +010051/* 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 */
54struct 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
Manish V Badarkhe5797b802021-08-06 09:26:20 +010083void bl2_plat_mboot_init(void)
84{
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010085 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);
Tamas Banb0f83252022-02-11 09:49:36 +0100112
113 rss_measured_boot_init();
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100114}
115
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100116int 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,
Manish V Badarkhedd1ee502022-11-18 20:27:21 +0000128 critical_data_id,
129 fvp_event_log_metadata);
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100130 if (err != 0) {
131 ERROR("%s%s critical data (%i)\n",
132 "Failed to ", "record", err);
133 return err;
134 }
135
136 return 0;
137}
138
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000139#if TRUSTED_BOARD_BOOT
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100140static int fvp_populate_critical_data(struct fvp_critical_data *critical_data)
141{
142 char *nv_ctr_oids[MAX_NV_CTR_IDS] = {
143 [TRUSTED_NV_CTR_ID] = TRUSTED_FW_NVCOUNTER_OID,
144 [NON_TRUSTED_NV_CTR_ID] = NON_TRUSTED_FW_NVCOUNTER_OID,
145 };
146
147 for (int i = 0; i < MAX_NV_CTR_IDS; i++) {
148 int rc = plat_get_nv_ctr(nv_ctr_oids[i],
149 &critical_data->nv_ctr[i]);
150 if (rc != 0) {
151 return rc;
152 }
153 }
154
155 return 0;
156}
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000157#endif /* TRUSTED_BOARD_BOOT */
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100158
159static int fvp_populate_and_measure_critical_data(void)
160{
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000161 int rc = 0;
162
163/*
164 * FVP platform only measures 'platform NV-counter' and hence its
165 * measurement makes sense during Trusted-Boot flow only.
166 */
167#if TRUSTED_BOARD_BOOT
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100168 struct fvp_critical_data populate_critical_data;
169
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000170 rc = fvp_populate_critical_data(&populate_critical_data);
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100171 if (rc == 0) {
172 rc = plat_mboot_measure_critical_data(CRITICAL_DATA_ID,
173 &populate_critical_data,
174 sizeof(populate_critical_data));
175 }
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000176#endif /* TRUSTED_BOARD_BOOT */
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100177
178 return rc;
179}
180
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100181void bl2_plat_mboot_finish(void)
182{
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100183 int rc;
184
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100185 /* Event Log address in Non-Secure memory */
186 uintptr_t ns_log_addr;
187
188 /* Event Log filled size */
189 size_t event_log_cur_size;
190
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100191 rc = fvp_populate_and_measure_critical_data();
192 if (rc != 0) {
193 panic();
194 }
195
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100196 event_log_cur_size = event_log_get_cur_size((uint8_t *)event_log_base);
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100197
198 rc = arm_set_nt_fw_info(
199#ifdef SPD_opteed
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100200 (uintptr_t)event_log_base,
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100201#endif
202 event_log_cur_size, &ns_log_addr);
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100203 if (rc != 0) {
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100204 ERROR("%s(): Unable to update %s_FW_CONFIG\n",
205 __func__, "NT");
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100206 /*
207 * It is a fatal error because on FVP secure world software
208 * assumes that a valid event log exists and will use it to
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100209 * record the measurements into the fTPM.
210 * Note: In FVP platform, OP-TEE uses nt_fw_config to get the
211 * secure Event Log buffer address.
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100212 */
213 panic();
214 }
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100215
216 /* Copy Event Log to Non-secure memory */
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100217 (void)memcpy((void *)ns_log_addr, (const void *)event_log_base,
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100218 event_log_cur_size);
219
220 /* Ensure that the Event Log is visible in Non-secure memory */
221 flush_dcache_range(ns_log_addr, event_log_cur_size);
222
223#if defined(SPD_tspd) || defined(SPD_spmd)
224 /* Set Event Log data in TOS_FW_CONFIG */
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100225 rc = arm_set_tos_fw_info((uintptr_t)event_log_base,
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100226 event_log_cur_size);
227 if (rc != 0) {
228 ERROR("%s(): Unable to update %s_FW_CONFIG\n",
229 __func__, "TOS");
230 panic();
231 }
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100232#endif /* defined(SPD_tspd) || defined(SPD_spmd) */
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100233
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100234 dump_event_log((uint8_t *)event_log_base, event_log_cur_size);
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100235}