blob: 8ebb6d602d107760455cfdcd5524d63955d5b4c2 [file] [log] [blame]
Soby Mathewcab0b5b2018-01-15 14:45:33 +00001/*
Zelaleme6937282020-02-03 14:56:42 -06002 * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
Soby Mathewcab0b5b2018-01-15 14:45:33 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +00008
Alexei Fedorov7b4e1fb2020-07-13 12:11:05 +01009#if MEASURED_BOOT
10#include <common/desc_image_load.h>
11#endif
12#include <common/fdt_wrappers.h>
13
Soby Mathewcab0b5b2018-01-15 14:45:33 +000014#include <libfdt.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000015
Antonio Nino Diazbd9344f2019-01-25 14:30:04 +000016#include <plat/arm/common/arm_dyn_cfg_helpers.h>
17#include <plat/arm/common/plat_arm.h>
Soby Mathewcab0b5b2018-01-15 14:45:33 +000018
John Tsichritzisba597da2018-07-30 13:41:52 +010019#define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr"
20#define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size"
Soby Mathew1d71ba12018-04-04 09:40:32 +010021
Alexei Fedorov0ab49642020-03-20 18:38:55 +000022#if MEASURED_BOOT
Alexei Fedorov7b4e1fb2020-07-13 12:11:05 +010023#ifdef SPD_opteed
24/*
25 * Currently OP-TEE does not support reading DTBs from Secure memory
26 * and this property should be removed when this feature is supported.
27 */
28#define DTB_PROP_HW_SM_LOG_ADDR "tpm_event_log_sm_addr"
Manish V Badarkheeab78e92021-08-10 20:51:55 +010029#endif /* SPD_opteed */
Alexei Fedorov7b4e1fb2020-07-13 12:11:05 +010030#define DTB_PROP_HW_LOG_ADDR "tpm_event_log_addr"
31#define DTB_PROP_HW_LOG_SIZE "tpm_event_log_size"
Alexei Fedorov0ab49642020-03-20 18:38:55 +000032#endif /* MEASURED_BOOT */
33
Soby Mathewcab0b5b2018-01-15 14:45:33 +000034/*******************************************************************************
35 * Validate the tb_fw_config is a valid DTB file and returns the node offset
36 * to "arm,tb_fw" property.
37 * Arguments:
38 * void *dtb - pointer to the TB_FW_CONFIG in memory
39 * int *node - Returns the node offset to "arm,tb_fw" property if found.
40 *
41 * Returns 0 on success and -1 on error.
42 ******************************************************************************/
43int arm_dyn_tb_fw_cfg_init(void *dtb, int *node)
44{
Soby Mathewda5f2742018-02-21 01:16:39 +000045 assert(dtb != NULL);
46 assert(node != NULL);
Soby Mathewcab0b5b2018-01-15 14:45:33 +000047
48 /* Check if the pointer to DT is correct */
49 if (fdt_check_header(dtb) != 0) {
Alexei Fedorov7b4e1fb2020-07-13 12:11:05 +010050 WARN("Invalid DTB file passed as%s\n", " TB_FW_CONFIG");
Soby Mathewcab0b5b2018-01-15 14:45:33 +000051 return -1;
52 }
53
54 /* Assert the node offset point to "arm,tb_fw" compatible property */
55 *node = fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw");
56 if (*node < 0) {
Alexei Fedorov7b4e1fb2020-07-13 12:11:05 +010057 WARN("The compatible property '%s' not%s", "arm,tb_fw",
58 " found in the config\n");
Soby Mathewcab0b5b2018-01-15 14:45:33 +000059 return -1;
60 }
61
Alexei Fedorov7b4e1fb2020-07-13 12:11:05 +010062 VERBOSE("Dyn cfg: '%s'%s", "arm,tb_fw", " found in the config\n");
Soby Mathewcab0b5b2018-01-15 14:45:33 +000063 return 0;
64}
John Tsichritzisba597da2018-07-30 13:41:52 +010065
John Tsichritzisba597da2018-07-30 13:41:52 +010066/*
John Tsichritzisba597da2018-07-30 13:41:52 +010067 * This function writes the Mbed TLS heap address and size in the DTB. When it
68 * is called, it is guaranteed that a DTB is available. However it is not
69 * guaranteed that the shared Mbed TLS heap implementation is used. Thus we
70 * return error code from here and it's the responsibility of the caller to
71 * determine the action upon error.
72 *
73 * This function is supposed to be called only by BL1.
74 *
75 * Returns:
76 * 0 = success
Alexei Fedorov0ab49642020-03-20 18:38:55 +000077 * -1 = error
John Tsichritzisba597da2018-07-30 13:41:52 +010078 */
79int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size)
80{
Alexei Fedorov0ab49642020-03-20 18:38:55 +000081 int dtb_root;
Manish V Badarkheeab78e92021-08-10 20:51:55 +010082
John Tsichritzisba597da2018-07-30 13:41:52 +010083 /*
84 * Verify that the DTB is valid, before attempting to write to it,
85 * and get the DTB root node.
86 */
Alexei Fedorov0ab49642020-03-20 18:38:55 +000087 int err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
John Tsichritzisba597da2018-07-30 13:41:52 +010088 if (err < 0) {
Alexei Fedorov7b4e1fb2020-07-13 12:11:05 +010089 ERROR("Invalid%s loaded. Unable to get root node\n",
90 " TB_FW_CONFIG");
John Tsichritzisba597da2018-07-30 13:41:52 +010091 return -1;
92 }
93
94 /*
95 * Write the heap address and size in the DTB.
96 *
97 * NOTE: The variables heap_addr and heap_size are corrupted
98 * by the "fdtw_write_inplace_cells" function. After the
99 * function calls they must NOT be reused.
100 */
101 err = fdtw_write_inplace_cells(dtb, dtb_root,
102 DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr);
103 if (err < 0) {
Alexei Fedorov7b4e1fb2020-07-13 12:11:05 +0100104 ERROR("%sDTB property '%s'\n",
105 "Unable to write ", DTB_PROP_MBEDTLS_HEAP_ADDR);
John Tsichritzisba597da2018-07-30 13:41:52 +0100106 return -1;
107 }
108
109 err = fdtw_write_inplace_cells(dtb, dtb_root,
110 DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size);
111 if (err < 0) {
Alexei Fedorov7b4e1fb2020-07-13 12:11:05 +0100112 ERROR("%sDTB property '%s'\n",
113 "Unable to write ", DTB_PROP_MBEDTLS_HEAP_SIZE);
John Tsichritzisba597da2018-07-30 13:41:52 +0100114 return -1;
115 }
116
117 return 0;
118}
Alexei Fedorov0ab49642020-03-20 18:38:55 +0000119
120#if MEASURED_BOOT
121/*
Alexei Fedorov7b4e1fb2020-07-13 12:11:05 +0100122 * Write the Event Log address and its size in the DTB.
123 *
124 * This function is supposed to be called only by BL2.
125 *
126 * Returns:
127 * 0 = success
128 * < 0 = error
129 */
130static int arm_set_event_log_info(uintptr_t config_base,
131#ifdef SPD_opteed
132 uintptr_t sm_log_addr,
133#endif
134 uintptr_t log_addr, size_t log_size)
135{
136 /* As libfdt uses void *, we can't avoid this cast */
137 void *dtb = (void *)config_base;
138 const char *compatible = "arm,tpm_event_log";
139 int err, node;
140
141 /*
142 * Verify that the DTB is valid, before attempting to write to it,
143 * and get the DTB root node.
144 */
145
146 /* Check if the pointer to DT is correct */
147 err = fdt_check_header(dtb);
148 if (err < 0) {
149 WARN("Invalid DTB file passed\n");
150 return err;
151 }
152
153 /* Assert the node offset point to compatible property */
154 node = fdt_node_offset_by_compatible(dtb, -1, compatible);
155 if (node < 0) {
156 WARN("The compatible property '%s' not%s", compatible,
157 " found in the config\n");
158 return node;
159 }
160
161 VERBOSE("Dyn cfg: '%s'%s", compatible, " found in the config\n");
162
163#ifdef SPD_opteed
164 if (sm_log_addr != 0UL) {
165 err = fdtw_write_inplace_cells(dtb, node,
166 DTB_PROP_HW_SM_LOG_ADDR, 2, &sm_log_addr);
167 if (err < 0) {
168 ERROR("%sDTB property '%s'\n",
169 "Unable to write ", DTB_PROP_HW_SM_LOG_ADDR);
170 return err;
171 }
172 }
173#endif
174 err = fdtw_write_inplace_cells(dtb, node,
175 DTB_PROP_HW_LOG_ADDR, 2, &log_addr);
176 if (err < 0) {
177 ERROR("%sDTB property '%s'\n",
178 "Unable to write ", DTB_PROP_HW_LOG_ADDR);
179 return err;
180 }
181
182 err = fdtw_write_inplace_cells(dtb, node,
183 DTB_PROP_HW_LOG_SIZE, 1, &log_size);
184 if (err < 0) {
185 ERROR("%sDTB property '%s'\n",
186 "Unable to write ", DTB_PROP_HW_LOG_SIZE);
187 } else {
188 /*
189 * Ensure that the info written to the DTB is visible
190 * to other images.
191 */
192 flush_dcache_range(config_base, fdt_totalsize(dtb));
193 }
194
195 return err;
196}
197
198/*
199 * This function writes the Event Log address and its size
200 * in the TOS_FW_CONFIG DTB.
201 *
202 * This function is supposed to be called only by BL2.
203 *
204 * Returns:
205 * 0 = success
206 * < 0 = error
207 */
208int arm_set_tos_fw_info(uintptr_t config_base, uintptr_t log_addr,
209 size_t log_size)
210{
211 int err;
212
213 assert(config_base != 0UL);
214 assert(log_addr != 0UL);
215
216 /* Write the Event Log address and its size in the DTB */
217 err = arm_set_event_log_info(config_base,
218#ifdef SPD_opteed
219 0UL,
220#endif
221 log_addr, log_size);
222 if (err < 0) {
223 ERROR("%sEvent Log data to TOS_FW_CONFIG\n",
224 "Unable to write ");
225 }
226
227 return err;
228}
229
230/*
231 * This function writes the Event Log address and its size
232 * in the NT_FW_CONFIG DTB.
233 *
234 * This function is supposed to be called only by BL2.
235 *
236 * Returns:
237 * 0 = success
238 * < 0 = error
239 */
240int arm_set_nt_fw_info(uintptr_t config_base,
241#ifdef SPD_opteed
242 uintptr_t log_addr,
243#endif
244 size_t log_size, uintptr_t *ns_log_addr)
245{
246 uintptr_t ns_addr;
247 const bl_mem_params_node_t *cfg_mem_params;
248 int err;
249
250 assert(config_base != 0UL);
251 assert(ns_log_addr != NULL);
252
253 /* Get the config load address and size from NT_FW_CONFIG */
254 cfg_mem_params = get_bl_mem_params_node(NT_FW_CONFIG_ID);
255 assert(cfg_mem_params != NULL);
256
257 /* Calculate Event Log address in Non-secure memory */
258 ns_addr = cfg_mem_params->image_info.image_base +
259 cfg_mem_params->image_info.image_max_size;
260
261 /* Check for memory space */
262 if ((uint64_t)(ns_addr + log_size) > ARM_NS_DRAM1_END) {
263 return -1;
264 }
265
266 /* Write the Event Log address and its size in the DTB */
267 err = arm_set_event_log_info(config_base,
268#ifdef SPD_opteed
269 log_addr,
270#endif
271 ns_addr, log_size);
272
273 /* Return Event Log address in Non-secure memory */
274 *ns_log_addr = (err < 0) ? 0UL : ns_addr;
275 return err;
276}
Alexei Fedorov0ab49642020-03-20 18:38:55 +0000277#endif /* MEASURED_BOOT */