xref: /rk3399_ARM-atf/plat/arm/common/arm_dyn_cfg_helpers.c (revision eab78e9ba4e36da27941bac99d2de0d5228eca6c)
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 <libfdt.h>
15 
16 #include <plat/arm/common/arm_dyn_cfg_helpers.h>
17 #include <plat/arm/common/plat_arm.h>
18 
19 #define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr"
20 #define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size"
21 
22 #if MEASURED_BOOT
23 #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"
29 #endif /* SPD_opteed */
30 #define DTB_PROP_HW_LOG_ADDR	"tpm_event_log_addr"
31 #define DTB_PROP_HW_LOG_SIZE    "tpm_event_log_size"
32 #endif /* MEASURED_BOOT */
33 
34 /*******************************************************************************
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  ******************************************************************************/
43 int arm_dyn_tb_fw_cfg_init(void *dtb, int *node)
44 {
45 	assert(dtb != NULL);
46 	assert(node != NULL);
47 
48 	/* Check if the pointer to DT is correct */
49 	if (fdt_check_header(dtb) != 0) {
50 		WARN("Invalid DTB file passed as%s\n", " TB_FW_CONFIG");
51 		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) {
57 		WARN("The compatible property '%s' not%s", "arm,tb_fw",
58 			" found in the config\n");
59 		return -1;
60 	}
61 
62 	VERBOSE("Dyn cfg: '%s'%s", "arm,tb_fw", " found in the config\n");
63 	return 0;
64 }
65 
66 /*
67  * 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
77  *     -1 = error
78  */
79 int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size)
80 {
81 	int dtb_root;
82 
83 	/*
84 	 * Verify that the DTB is valid, before attempting to write to it,
85 	 * and get the DTB root node.
86 	 */
87 	int err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
88 	if (err < 0) {
89 		ERROR("Invalid%s loaded. Unable to get root node\n",
90 			" TB_FW_CONFIG");
91 		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) {
104 		ERROR("%sDTB property '%s'\n",
105 			"Unable to write ", DTB_PROP_MBEDTLS_HEAP_ADDR);
106 		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) {
112 		ERROR("%sDTB property '%s'\n",
113 			"Unable to write ", DTB_PROP_MBEDTLS_HEAP_SIZE);
114 		return -1;
115 	}
116 
117 	return 0;
118 }
119 
120 #if MEASURED_BOOT
121 /*
122  * 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  */
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  */
208 int 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  */
240 int 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 }
277 #endif /* MEASURED_BOOT */
278