blob: fd15b70d3f6fa55713317903e72f86346b5c550e [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
Sandrine Bailleux3c2db6f2021-07-07 14:47:08 +02009#include <drivers/measured_boot/event_log/event_log.h>
Tamas Banb0f83252022-02-11 09:49:36 +010010#include <drivers/measured_boot/rss/rss_measured_boot.h>
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010011#include <tools_share/tbbr_oid.h>
12#include <fvp_critical_data.h>
13
Alexei Fedorov61369a22020-07-13 14:59:02 +010014#include <plat/arm/common/plat_arm.h>
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010015#include <plat/common/common_def.h>
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010016
17/* Event Log data */
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010018static uint64_t event_log_base;
Alexei Fedorov61369a22020-07-13 14:59:02 +010019
20/* FVP table with platform specific image IDs, names and PCRs */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010021const event_log_metadata_t fvp_event_log_metadata[] = {
Manish V Badarkhe72e03692021-09-08 20:04:24 +010022 { BL31_IMAGE_ID, EVLOG_BL31_STRING, PCR_0 },
23 { BL32_IMAGE_ID, EVLOG_BL32_STRING, PCR_0 },
24 { BL32_EXTRA1_IMAGE_ID, EVLOG_BL32_EXTRA1_STRING, PCR_0 },
25 { BL32_EXTRA2_IMAGE_ID, EVLOG_BL32_EXTRA2_STRING, PCR_0 },
26 { BL33_IMAGE_ID, EVLOG_BL33_STRING, PCR_0 },
27 { HW_CONFIG_ID, EVLOG_HW_CONFIG_STRING, PCR_0 },
28 { NT_FW_CONFIG_ID, EVLOG_NT_FW_CONFIG_STRING, PCR_0 },
29 { SCP_BL2_IMAGE_ID, EVLOG_SCP_BL2_STRING, PCR_0 },
30 { SOC_FW_CONFIG_ID, EVLOG_SOC_FW_CONFIG_STRING, PCR_0 },
31 { TOS_FW_CONFIG_ID, EVLOG_TOS_FW_CONFIG_STRING, PCR_0 },
Tamas Ban6101c2a2022-01-10 15:13:00 +010032 { RMM_IMAGE_ID, EVLOG_RMM_STRING, PCR_0},
Manish V Badarkhe67009c32021-10-31 14:47:49 +000033
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010034 { CRITICAL_DATA_ID, EVLOG_CRITICAL_DATA_STRING, PCR_1 },
35
Manish V Badarkhe67009c32021-10-31 14:47:49 +000036 { EVLOG_INVALID_ID, NULL, (unsigned int)(-1) } /* Terminator */
Alexei Fedorov61369a22020-07-13 14:59:02 +010037};
38
Tamas Banb0f83252022-02-11 09:49:36 +010039/* FVP table with platform specific image IDs and metadata. Intentionally not a
40 * const struct, some members might set by bootloaders during trusted boot.
41 */
42struct rss_mboot_metadata fvp_rss_mboot_metadata[] = {
43 {
44 .id = BL31_IMAGE_ID,
45 .slot = U(9),
46 .signer_id_size = SIGNER_ID_MIN_SIZE,
47 .sw_type = RSS_MBOOT_BL31_STRING,
48 .lock_measurement = true },
49 {
50 .id = HW_CONFIG_ID,
51 .slot = U(10),
52 .signer_id_size = SIGNER_ID_MIN_SIZE,
53 .sw_type = RSS_MBOOT_HW_CONFIG_STRING,
54 .lock_measurement = true },
55 {
56 .id = SOC_FW_CONFIG_ID,
57 .slot = U(11),
58 .signer_id_size = SIGNER_ID_MIN_SIZE,
59 .sw_type = RSS_MBOOT_SOC_FW_CONFIG_STRING,
60 .lock_measurement = true },
61 {
62 .id = RMM_IMAGE_ID,
63 .slot = U(12),
64 .signer_id_size = SIGNER_ID_MIN_SIZE,
65 .sw_type = RSS_MBOOT_RMM_STRING,
66 .lock_measurement = true },
67 {
68 .id = RSS_MBOOT_INVALID_ID }
69};
70
Manish V Badarkhe5797b802021-08-06 09:26:20 +010071void bl2_plat_mboot_init(void)
72{
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010073 uint8_t *event_log_start;
74 uint8_t *event_log_finish;
75 size_t bl1_event_log_size;
76 int rc;
77
78 rc = arm_get_tb_fw_info(&event_log_base, &bl1_event_log_size);
79 if (rc != 0) {
80 ERROR("%s(): Unable to get Event Log info from TB_FW_CONFIG\n",
81 __func__);
82 /*
83 * It is a fatal error because on FVP platform, BL2 software
84 * assumes that a valid Event Log buffer exist and it will use
85 * same Event Log buffer to append image measurements.
86 */
87 panic();
88 }
89
90 /*
91 * BL1 and BL2 share the same Event Log buffer and that BL2 will
92 * append its measurements after BL1's
93 */
94 event_log_start = (uint8_t *)((uintptr_t)event_log_base +
95 bl1_event_log_size);
96 event_log_finish = (uint8_t *)((uintptr_t)event_log_base +
97 PLAT_ARM_EVENT_LOG_MAX_SIZE);
98
99 event_log_init((uint8_t *)event_log_start, event_log_finish);
Tamas Banb0f83252022-02-11 09:49:36 +0100100
101 rss_measured_boot_init();
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100102}
103
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100104int plat_mboot_measure_critical_data(unsigned int critical_data_id,
105 const void *base, size_t size)
106{
107 /*
108 * It is very unlikely that the critical data size would be
109 * bigger than 2^32 bytes
110 */
111 assert(size < UINT32_MAX);
112 assert(base != NULL);
113
114 /* Calculate image hash and record data in Event Log */
115 int err = event_log_measure_and_record((uintptr_t)base, (uint32_t)size,
116 critical_data_id);
117 if (err != 0) {
118 ERROR("%s%s critical data (%i)\n",
119 "Failed to ", "record", err);
120 return err;
121 }
122
123 return 0;
124}
125
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000126#if TRUSTED_BOARD_BOOT
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100127static int fvp_populate_critical_data(struct fvp_critical_data *critical_data)
128{
129 char *nv_ctr_oids[MAX_NV_CTR_IDS] = {
130 [TRUSTED_NV_CTR_ID] = TRUSTED_FW_NVCOUNTER_OID,
131 [NON_TRUSTED_NV_CTR_ID] = NON_TRUSTED_FW_NVCOUNTER_OID,
132 };
133
134 for (int i = 0; i < MAX_NV_CTR_IDS; i++) {
135 int rc = plat_get_nv_ctr(nv_ctr_oids[i],
136 &critical_data->nv_ctr[i]);
137 if (rc != 0) {
138 return rc;
139 }
140 }
141
142 return 0;
143}
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000144#endif /* TRUSTED_BOARD_BOOT */
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100145
146static int fvp_populate_and_measure_critical_data(void)
147{
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000148 int rc = 0;
149
150/*
151 * FVP platform only measures 'platform NV-counter' and hence its
152 * measurement makes sense during Trusted-Boot flow only.
153 */
154#if TRUSTED_BOARD_BOOT
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100155 struct fvp_critical_data populate_critical_data;
156
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000157 rc = fvp_populate_critical_data(&populate_critical_data);
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100158 if (rc == 0) {
159 rc = plat_mboot_measure_critical_data(CRITICAL_DATA_ID,
160 &populate_critical_data,
161 sizeof(populate_critical_data));
162 }
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000163#endif /* TRUSTED_BOARD_BOOT */
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100164
165 return rc;
166}
167
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100168void bl2_plat_mboot_finish(void)
169{
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100170 int rc;
171
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100172 /* Event Log address in Non-Secure memory */
173 uintptr_t ns_log_addr;
174
175 /* Event Log filled size */
176 size_t event_log_cur_size;
177
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100178 rc = fvp_populate_and_measure_critical_data();
179 if (rc != 0) {
180 panic();
181 }
182
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100183 event_log_cur_size = event_log_get_cur_size((uint8_t *)event_log_base);
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100184
185 rc = arm_set_nt_fw_info(
186#ifdef SPD_opteed
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100187 (uintptr_t)event_log_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 Badarkhe4edf4bd2021-08-11 10:45:03 +0100212 rc = arm_set_tos_fw_info((uintptr_t)event_log_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}