blob: 6a2a6f89a6423b8a36aba236704939a719b029bb [file] [log] [blame]
Soby Mathew96a1c6b2018-01-15 14:45:33 +00001/*
Zelalem87675d42020-02-03 14:56:42 -06002 * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
Soby Mathew96a1c6b2018-01-15 14:45:33 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
Alexei Fedorovc7176172020-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
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +010014#include <lib/fconf/fconf.h>
15#include <lib/fconf/fconf_dyn_cfg_getter.h>
Soby Mathew96a1c6b2018-01-15 14:45:33 +000016#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017
Antonio Nino Diazbd7b7402019-01-25 14:30:04 +000018#include <plat/arm/common/arm_dyn_cfg_helpers.h>
19#include <plat/arm/common/plat_arm.h>
Soby Mathew96a1c6b2018-01-15 14:45:33 +000020
John Tsichritzisc34341a2018-07-30 13:41:52 +010021#define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr"
22#define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size"
Soby Mathewb6814842018-04-04 09:40:32 +010023
Alexei Fedorov25d7c882020-03-20 18:38:55 +000024#if MEASURED_BOOT
Alexei Fedorovc7176172020-07-13 12:11:05 +010025#ifdef SPD_opteed
26/*
27 * Currently OP-TEE does not support reading DTBs from Secure memory
28 * and this property should be removed when this feature is supported.
29 */
30#define DTB_PROP_HW_SM_LOG_ADDR "tpm_event_log_sm_addr"
Manish V Badarkhe3ff0f792021-08-10 20:51:55 +010031#endif /* SPD_opteed */
Alexei Fedorovc7176172020-07-13 12:11:05 +010032#define DTB_PROP_HW_LOG_ADDR "tpm_event_log_addr"
33#define DTB_PROP_HW_LOG_SIZE "tpm_event_log_size"
Alexei Fedorov25d7c882020-03-20 18:38:55 +000034#endif /* MEASURED_BOOT */
35
Soby Mathew96a1c6b2018-01-15 14:45:33 +000036/*******************************************************************************
37 * Validate the tb_fw_config is a valid DTB file and returns the node offset
38 * to "arm,tb_fw" property.
39 * Arguments:
40 * void *dtb - pointer to the TB_FW_CONFIG in memory
41 * int *node - Returns the node offset to "arm,tb_fw" property if found.
42 *
43 * Returns 0 on success and -1 on error.
44 ******************************************************************************/
45int arm_dyn_tb_fw_cfg_init(void *dtb, int *node)
46{
Soby Mathewcc364842018-02-21 01:16:39 +000047 assert(dtb != NULL);
48 assert(node != NULL);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000049
50 /* Check if the pointer to DT is correct */
51 if (fdt_check_header(dtb) != 0) {
Alexei Fedorovc7176172020-07-13 12:11:05 +010052 WARN("Invalid DTB file passed as%s\n", " TB_FW_CONFIG");
Soby Mathew96a1c6b2018-01-15 14:45:33 +000053 return -1;
54 }
55
56 /* Assert the node offset point to "arm,tb_fw" compatible property */
57 *node = fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw");
58 if (*node < 0) {
Alexei Fedorovc7176172020-07-13 12:11:05 +010059 WARN("The compatible property '%s' not%s", "arm,tb_fw",
60 " found in the config\n");
Soby Mathew96a1c6b2018-01-15 14:45:33 +000061 return -1;
62 }
63
Alexei Fedorovc7176172020-07-13 12:11:05 +010064 VERBOSE("Dyn cfg: '%s'%s", "arm,tb_fw", " found in the config\n");
Soby Mathew96a1c6b2018-01-15 14:45:33 +000065 return 0;
66}
John Tsichritzisc34341a2018-07-30 13:41:52 +010067
John Tsichritzisc34341a2018-07-30 13:41:52 +010068/*
John Tsichritzisc34341a2018-07-30 13:41:52 +010069 * This function writes the Mbed TLS heap address and size in the DTB. When it
70 * is called, it is guaranteed that a DTB is available. However it is not
71 * guaranteed that the shared Mbed TLS heap implementation is used. Thus we
72 * return error code from here and it's the responsibility of the caller to
73 * determine the action upon error.
74 *
75 * This function is supposed to be called only by BL1.
76 *
77 * Returns:
78 * 0 = success
Alexei Fedorov25d7c882020-03-20 18:38:55 +000079 * -1 = error
John Tsichritzisc34341a2018-07-30 13:41:52 +010080 */
81int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size)
82{
Alexei Fedorov25d7c882020-03-20 18:38:55 +000083 int dtb_root;
Manish V Badarkhe3ff0f792021-08-10 20:51:55 +010084
John Tsichritzisc34341a2018-07-30 13:41:52 +010085 /*
86 * Verify that the DTB is valid, before attempting to write to it,
87 * and get the DTB root node.
88 */
Alexei Fedorov25d7c882020-03-20 18:38:55 +000089 int err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
John Tsichritzisc34341a2018-07-30 13:41:52 +010090 if (err < 0) {
Alexei Fedorovc7176172020-07-13 12:11:05 +010091 ERROR("Invalid%s loaded. Unable to get root node\n",
92 " TB_FW_CONFIG");
John Tsichritzisc34341a2018-07-30 13:41:52 +010093 return -1;
94 }
95
96 /*
97 * Write the heap address and size in the DTB.
98 *
99 * NOTE: The variables heap_addr and heap_size are corrupted
100 * by the "fdtw_write_inplace_cells" function. After the
101 * function calls they must NOT be reused.
102 */
103 err = fdtw_write_inplace_cells(dtb, dtb_root,
104 DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr);
105 if (err < 0) {
Alexei Fedorovc7176172020-07-13 12:11:05 +0100106 ERROR("%sDTB property '%s'\n",
107 "Unable to write ", DTB_PROP_MBEDTLS_HEAP_ADDR);
John Tsichritzisc34341a2018-07-30 13:41:52 +0100108 return -1;
109 }
110
111 err = fdtw_write_inplace_cells(dtb, dtb_root,
112 DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size);
113 if (err < 0) {
Alexei Fedorovc7176172020-07-13 12:11:05 +0100114 ERROR("%sDTB property '%s'\n",
115 "Unable to write ", DTB_PROP_MBEDTLS_HEAP_SIZE);
John Tsichritzisc34341a2018-07-30 13:41:52 +0100116 return -1;
117 }
118
119 return 0;
120}
Alexei Fedorov25d7c882020-03-20 18:38:55 +0000121
122#if MEASURED_BOOT
123/*
Alexei Fedorovc7176172020-07-13 12:11:05 +0100124 * Write the Event Log address and its size in the DTB.
125 *
Alexei Fedorovc7176172020-07-13 12:11:05 +0100126 * 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 */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100208int arm_set_tos_fw_info(uintptr_t log_addr, size_t log_size)
Alexei Fedorovc7176172020-07-13 12:11:05 +0100209{
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100210 uintptr_t config_base;
211 const bl_mem_params_node_t *cfg_mem_params;
Alexei Fedorovc7176172020-07-13 12:11:05 +0100212 int err;
213
Alexei Fedorovc7176172020-07-13 12:11:05 +0100214 assert(log_addr != 0UL);
215
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100216 /* Get the config load address and size of TOS_FW_CONFIG */
217 cfg_mem_params = get_bl_mem_params_node(TOS_FW_CONFIG_ID);
218 assert(cfg_mem_params != NULL);
219
220 config_base = cfg_mem_params->image_info.image_base;
221
Alexei Fedorovc7176172020-07-13 12:11:05 +0100222 /* Write the Event Log address and its size in the DTB */
223 err = arm_set_event_log_info(config_base,
224#ifdef SPD_opteed
225 0UL,
226#endif
227 log_addr, log_size);
228 if (err < 0) {
229 ERROR("%sEvent Log data to TOS_FW_CONFIG\n",
230 "Unable to write ");
231 }
232
233 return err;
234}
235
236/*
237 * This function writes the Event Log address and its size
238 * in the NT_FW_CONFIG DTB.
239 *
240 * This function is supposed to be called only by BL2.
241 *
242 * Returns:
243 * 0 = success
244 * < 0 = error
245 */
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100246int arm_set_nt_fw_info(
Alexei Fedorovc7176172020-07-13 12:11:05 +0100247#ifdef SPD_opteed
248 uintptr_t log_addr,
249#endif
250 size_t log_size, uintptr_t *ns_log_addr)
251{
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100252 uintptr_t config_base;
Alexei Fedorovc7176172020-07-13 12:11:05 +0100253 uintptr_t ns_addr;
254 const bl_mem_params_node_t *cfg_mem_params;
255 int err;
256
Alexei Fedorovc7176172020-07-13 12:11:05 +0100257 assert(ns_log_addr != NULL);
258
259 /* Get the config load address and size from NT_FW_CONFIG */
260 cfg_mem_params = get_bl_mem_params_node(NT_FW_CONFIG_ID);
261 assert(cfg_mem_params != NULL);
262
Manish V Badarkhe7ca9d652021-09-14 22:41:46 +0100263 config_base = cfg_mem_params->image_info.image_base;
264
Alexei Fedorovc7176172020-07-13 12:11:05 +0100265 /* Calculate Event Log address in Non-secure memory */
266 ns_addr = cfg_mem_params->image_info.image_base +
267 cfg_mem_params->image_info.image_max_size;
268
269 /* Check for memory space */
270 if ((uint64_t)(ns_addr + log_size) > ARM_NS_DRAM1_END) {
271 return -1;
272 }
273
274 /* Write the Event Log address and its size in the DTB */
275 err = arm_set_event_log_info(config_base,
276#ifdef SPD_opteed
277 log_addr,
278#endif
279 ns_addr, log_size);
280
281 /* Return Event Log address in Non-secure memory */
282 *ns_log_addr = (err < 0) ? 0UL : ns_addr;
283 return err;
284}
Manish V Badarkhe4edf4bd2021-08-11 10:45:03 +0100285
286/*
287 * This function writes the Event Log address and its size
288 * in the TB_FW_CONFIG DTB.
289 *
290 * This function is supposed to be called only by BL1.
291 *
292 * Returns:
293 * 0 = success
294 * < 0 = error
295 */
296int arm_set_tb_fw_info(uintptr_t log_addr, size_t log_size)
297{
298 /*
299 * Read tb_fw_config device tree for Event Log properties
300 * and write the Event Log address and its size in the DTB
301 */
302 const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
303 uintptr_t tb_fw_cfg_dtb;
304 int err;
305
306 tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
307 assert(tb_fw_config_info != NULL);
308
309 tb_fw_cfg_dtb = tb_fw_config_info->config_addr;
310
311 err = arm_set_event_log_info(tb_fw_cfg_dtb,
312#ifdef SPD_opteed
313 0UL,
314#endif
315 log_addr, log_size);
316 return err;
317}
318
319/*
320 * This function reads the Event Log address and its size
321 * properties present in TB_FW_CONFIG DTB.
322 *
323 * This function is supposed to be called only by BL2.
324 *
325 * Returns:
326 * 0 = success
327 * < 0 = error
328 * Alongside returns Event Log address and its size.
329 */
330
331int arm_get_tb_fw_info(uint64_t *log_addr, size_t *log_size)
332{
333 /* As libfdt uses void *, we can't avoid this cast */
334 const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
335 int node, rc;
336
337 tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
338 assert(tb_fw_config_info != NULL);
339
340 void *dtb = (void *)tb_fw_config_info->config_addr;
341 const char *compatible = "arm,tpm_event_log";
342
343 /* Assert the node offset point to compatible property */
344 node = fdt_node_offset_by_compatible(dtb, -1, compatible);
345 if (node < 0) {
346 WARN("The compatible property '%s'%s", compatible,
347 " not specified in TB_FW config.\n");
348 return node;
349 }
350
351 VERBOSE("Dyn cfg: '%s'%s", compatible, " found in the config\n");
352
353 rc = fdt_read_uint64(dtb, node, DTB_PROP_HW_LOG_ADDR, log_addr);
354 if (rc != 0) {
355 ERROR("%s%s", DTB_PROP_HW_LOG_ADDR,
356 " not specified in TB_FW config.\n");
357 return rc;
358 }
359
360 rc = fdt_read_uint32(dtb, node, DTB_PROP_HW_LOG_SIZE, (uint32_t *)log_size);
361 if (rc != 0) {
362 ERROR("%s%s", DTB_PROP_HW_LOG_SIZE,
363 " not specified in TB_FW config.\n");
364 }
365
366 return rc;
367}
Alexei Fedorov25d7c882020-03-20 18:38:55 +0000368#endif /* MEASURED_BOOT */