blob: af4306533645d65b809c4b4860c9f617a419604a [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>
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010010#include <tools_share/tbbr_oid.h>
11#include <fvp_critical_data.h>
12
Alexei Fedorov61369a22020-07-13 14:59:02 +010013#include <plat/arm/common/plat_arm.h>
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010014#include <plat/common/common_def.h>
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010015
16/* Event Log data */
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010017static uint64_t event_log_base;
Alexei Fedorov61369a22020-07-13 14:59:02 +010018
19/* FVP table with platform specific image IDs, names and PCRs */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +010020const event_log_metadata_t fvp_event_log_metadata[] = {
Manish V Badarkhe72e03692021-09-08 20:04:24 +010021 { BL31_IMAGE_ID, EVLOG_BL31_STRING, PCR_0 },
22 { BL32_IMAGE_ID, EVLOG_BL32_STRING, PCR_0 },
23 { BL32_EXTRA1_IMAGE_ID, EVLOG_BL32_EXTRA1_STRING, PCR_0 },
24 { BL32_EXTRA2_IMAGE_ID, EVLOG_BL32_EXTRA2_STRING, PCR_0 },
25 { BL33_IMAGE_ID, EVLOG_BL33_STRING, PCR_0 },
26 { HW_CONFIG_ID, EVLOG_HW_CONFIG_STRING, PCR_0 },
27 { NT_FW_CONFIG_ID, EVLOG_NT_FW_CONFIG_STRING, PCR_0 },
28 { SCP_BL2_IMAGE_ID, EVLOG_SCP_BL2_STRING, PCR_0 },
29 { SOC_FW_CONFIG_ID, EVLOG_SOC_FW_CONFIG_STRING, PCR_0 },
30 { TOS_FW_CONFIG_ID, EVLOG_TOS_FW_CONFIG_STRING, PCR_0 },
Manish V Badarkhe67009c32021-10-31 14:47:49 +000031
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010032 { CRITICAL_DATA_ID, EVLOG_CRITICAL_DATA_STRING, PCR_1 },
33
Manish V Badarkhe67009c32021-10-31 14:47:49 +000034 { EVLOG_INVALID_ID, NULL, (unsigned int)(-1) } /* Terminator */
Alexei Fedorov61369a22020-07-13 14:59:02 +010035};
36
Manish V Badarkhe5797b802021-08-06 09:26:20 +010037void bl2_plat_mboot_init(void)
38{
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010039 uint8_t *event_log_start;
40 uint8_t *event_log_finish;
41 size_t bl1_event_log_size;
42 int rc;
43
44 rc = arm_get_tb_fw_info(&event_log_base, &bl1_event_log_size);
45 if (rc != 0) {
46 ERROR("%s(): Unable to get Event Log info from TB_FW_CONFIG\n",
47 __func__);
48 /*
49 * It is a fatal error because on FVP platform, BL2 software
50 * assumes that a valid Event Log buffer exist and it will use
51 * same Event Log buffer to append image measurements.
52 */
53 panic();
54 }
55
56 /*
57 * BL1 and BL2 share the same Event Log buffer and that BL2 will
58 * append its measurements after BL1's
59 */
60 event_log_start = (uint8_t *)((uintptr_t)event_log_base +
61 bl1_event_log_size);
62 event_log_finish = (uint8_t *)((uintptr_t)event_log_base +
63 PLAT_ARM_EVENT_LOG_MAX_SIZE);
64
65 event_log_init((uint8_t *)event_log_start, event_log_finish);
Manish V Badarkhe5797b802021-08-06 09:26:20 +010066}
67
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010068int plat_mboot_measure_critical_data(unsigned int critical_data_id,
69 const void *base, size_t size)
70{
71 /*
72 * It is very unlikely that the critical data size would be
73 * bigger than 2^32 bytes
74 */
75 assert(size < UINT32_MAX);
76 assert(base != NULL);
77
78 /* Calculate image hash and record data in Event Log */
79 int err = event_log_measure_and_record((uintptr_t)base, (uint32_t)size,
80 critical_data_id);
81 if (err != 0) {
82 ERROR("%s%s critical data (%i)\n",
83 "Failed to ", "record", err);
84 return err;
85 }
86
87 return 0;
88}
89
Manish V Badarkheeba13bd2022-01-08 23:08:02 +000090#if TRUSTED_BOARD_BOOT
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +010091static int fvp_populate_critical_data(struct fvp_critical_data *critical_data)
92{
93 char *nv_ctr_oids[MAX_NV_CTR_IDS] = {
94 [TRUSTED_NV_CTR_ID] = TRUSTED_FW_NVCOUNTER_OID,
95 [NON_TRUSTED_NV_CTR_ID] = NON_TRUSTED_FW_NVCOUNTER_OID,
96 };
97
98 for (int i = 0; i < MAX_NV_CTR_IDS; i++) {
99 int rc = plat_get_nv_ctr(nv_ctr_oids[i],
100 &critical_data->nv_ctr[i]);
101 if (rc != 0) {
102 return rc;
103 }
104 }
105
106 return 0;
107}
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000108#endif /* TRUSTED_BOARD_BOOT */
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100109
110static int fvp_populate_and_measure_critical_data(void)
111{
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000112 int rc = 0;
113
114/*
115 * FVP platform only measures 'platform NV-counter' and hence its
116 * measurement makes sense during Trusted-Boot flow only.
117 */
118#if TRUSTED_BOARD_BOOT
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100119 struct fvp_critical_data populate_critical_data;
120
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000121 rc = fvp_populate_critical_data(&populate_critical_data);
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100122 if (rc == 0) {
123 rc = plat_mboot_measure_critical_data(CRITICAL_DATA_ID,
124 &populate_critical_data,
125 sizeof(populate_critical_data));
126 }
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000127#endif /* TRUSTED_BOARD_BOOT */
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100128
129 return rc;
130}
131
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100132void bl2_plat_mboot_finish(void)
133{
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100134 int rc;
135
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100136 /* Event Log address in Non-Secure memory */
137 uintptr_t ns_log_addr;
138
139 /* Event Log filled size */
140 size_t event_log_cur_size;
141
Manish V Badarkhe1ffa0092021-10-20 22:06:40 +0100142 rc = fvp_populate_and_measure_critical_data();
143 if (rc != 0) {
144 panic();
145 }
146
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100147 event_log_cur_size = event_log_get_cur_size((uint8_t *)event_log_base);
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100148
149 rc = arm_set_nt_fw_info(
150#ifdef SPD_opteed
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100151 (uintptr_t)event_log_base,
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100152#endif
153 event_log_cur_size, &ns_log_addr);
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100154 if (rc != 0) {
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100155 ERROR("%s(): Unable to update %s_FW_CONFIG\n",
156 __func__, "NT");
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100157 /*
158 * It is a fatal error because on FVP secure world software
159 * assumes that a valid event log exists and will use it to
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100160 * record the measurements into the fTPM.
161 * Note: In FVP platform, OP-TEE uses nt_fw_config to get the
162 * secure Event Log buffer address.
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100163 */
164 panic();
165 }
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100166
167 /* Copy Event Log to Non-secure memory */
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100168 (void)memcpy((void *)ns_log_addr, (const void *)event_log_base,
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100169 event_log_cur_size);
170
171 /* Ensure that the Event Log is visible in Non-secure memory */
172 flush_dcache_range(ns_log_addr, event_log_cur_size);
173
174#if defined(SPD_tspd) || defined(SPD_spmd)
175 /* Set Event Log data in TOS_FW_CONFIG */
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100176 rc = arm_set_tos_fw_info((uintptr_t)event_log_base,
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100177 event_log_cur_size);
178 if (rc != 0) {
179 ERROR("%s(): Unable to update %s_FW_CONFIG\n",
180 __func__, "TOS");
181 panic();
182 }
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100183#endif /* defined(SPD_tspd) || defined(SPD_spmd) */
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100184
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100185 dump_event_log((uint8_t *)event_log_base, event_log_cur_size);
Manish V Badarkhe5797b802021-08-06 09:26:20 +0100186}