1 /*
2  * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #if MEASURED_BOOT
10 #include <common/desc_image_load.h>
11 #endif
12 #include <common/fdt_wrappers.h>
13 
14 #include <lib/fconf/fconf.h>
15 #include <lib/fconf/fconf_dyn_cfg_getter.h>
16 #include <libfdt.h>
17 
18 #include <plat/arm/common/arm_dyn_cfg_helpers.h>
19 #include <plat/arm/common/plat_arm.h>
20 
21 #define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr"
22 #define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size"
23 
24 #if MEASURED_BOOT
25 #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"
31 #endif /* SPD_opteed */
32 #define DTB_PROP_HW_LOG_ADDR	"tpm_event_log_addr"
33 #define DTB_PROP_HW_LOG_SIZE    "tpm_event_log_size"
34 #endif /* MEASURED_BOOT */
35 
36 /*******************************************************************************
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  ******************************************************************************/
arm_dyn_tb_fw_cfg_init(void * dtb,int * node)45 int arm_dyn_tb_fw_cfg_init(void *dtb, int *node)
46 {
47 	assert(dtb != NULL);
48 	assert(node != NULL);
49 
50 	/* Check if the pointer to DT is correct */
51 	if (fdt_check_header(dtb) != 0) {
52 		WARN("Invalid DTB file passed as%s\n", " TB_FW_CONFIG");
53 		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) {
59 		WARN("The compatible property '%s' not%s", "arm,tb_fw",
60 			" found in the config\n");
61 		return -1;
62 	}
63 
64 	VERBOSE("Dyn cfg: '%s'%s", "arm,tb_fw", " found in the config\n");
65 	return 0;
66 }
67 
68 /*
69  * 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
79  *     -1 = error
80  */
arm_set_dtb_mbedtls_heap_info(void * dtb,void * heap_addr,size_t heap_size)81 int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size)
82 {
83 	int dtb_root;
84 
85 	/*
86 	 * Verify that the DTB is valid, before attempting to write to it,
87 	 * and get the DTB root node.
88 	 */
89 	int err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
90 	if (err < 0) {
91 		ERROR("Invalid%s loaded. Unable to get root node\n",
92 			" TB_FW_CONFIG");
93 		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) {
106 		ERROR("%sDTB property '%s'\n",
107 			"Unable to write ", DTB_PROP_MBEDTLS_HEAP_ADDR);
108 		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) {
114 		ERROR("%sDTB property '%s'\n",
115 			"Unable to write ", DTB_PROP_MBEDTLS_HEAP_SIZE);
116 		return -1;
117 	}
118 
119 	return 0;
120 }
121 
122 #if MEASURED_BOOT
123 /*
124  * Write the Event Log address and its size in the DTB.
125  *
126  * Returns:
127  *	0 = success
128  *    < 0 = error
129  */
arm_set_event_log_info(uintptr_t config_base,uintptr_t sm_log_addr,uintptr_t log_addr,size_t log_size)130 static 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  */
arm_set_tos_fw_info(uintptr_t log_addr,size_t log_size)208 int arm_set_tos_fw_info(uintptr_t log_addr, size_t log_size)
209 {
210 	uintptr_t config_base;
211 	const bl_mem_params_node_t *cfg_mem_params;
212 	int err;
213 
214 	assert(log_addr != 0UL);
215 
216 	/* 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 
222 	/* 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  */
arm_set_nt_fw_info(uintptr_t log_addr,size_t log_size,uintptr_t * ns_log_addr)246 int arm_set_nt_fw_info(
247 #ifdef SPD_opteed
248 			uintptr_t log_addr,
249 #endif
250 			size_t log_size, uintptr_t *ns_log_addr)
251 {
252 	uintptr_t config_base;
253 	uintptr_t ns_addr;
254 	const bl_mem_params_node_t *cfg_mem_params;
255 	int err;
256 
257 	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 
263 	config_base = cfg_mem_params->image_info.image_base;
264 
265 	/* 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 }
285 
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  */
arm_set_tb_fw_info(uintptr_t log_addr,size_t log_size)296 int 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 
arm_get_tb_fw_info(uint64_t * log_addr,size_t * log_size)331 int 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 }
368 #endif /* MEASURED_BOOT */
369