xref: /rk3399_ARM-atf/plat/arm/common/arm_dyn_cfg_helpers.c (revision f893160690725fe79c8eb63fd90a945cc0374d90)
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 #include <libfdt.h>
10 
11 #include <common/fdt_wrappers.h>
12 #include <plat/arm/common/arm_dyn_cfg_helpers.h>
13 #include <plat/arm/common/plat_arm.h>
14 
15 #define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr"
16 #define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size"
17 
18 typedef struct config_load_info_prop {
19 	unsigned int config_id;
20 	const char *config_addr;
21 	const char *config_max_size;
22 } config_load_info_prop_t;
23 
24 static const config_load_info_prop_t prop_names[] = {
25 	{HW_CONFIG_ID, "hw_config_addr", "hw_config_max_size"},
26 	{SOC_FW_CONFIG_ID, "soc_fw_config_addr", "soc_fw_config_max_size"},
27 	{TOS_FW_CONFIG_ID, "tos_fw_config_addr", "tos_fw_config_max_size"},
28 	{NT_FW_CONFIG_ID, "nt_fw_config_addr", "nt_fw_config_max_size"}
29 };
30 
31 /*******************************************************************************
32  * Helper to read the load information corresponding to the `config_id` in
33  * TB_FW_CONFIG. This function expects the following properties to be defined :
34  *	<config>_addr		size : 2 cells
35  *	<config>_max_size	size : 1 cell
36  *
37  * Arguments:
38  *	void *dtb		 - pointer to the TB_FW_CONFIG in memory
39  *	int node		 - The node offset to appropriate node in the
40  *					 DTB.
41  *	unsigned int config_id	 - The configuration id
42  *	uint64_t *config_addr	 - Returns the `config` load address if read
43  *					 is successful.
44  *	uint32_t *config_size	 - Returns the `config` size if read is
45  *					 successful.
46  *
47  * Returns 0 on success and -1 on error.
48  ******************************************************************************/
49 int arm_dyn_get_config_load_info(void *dtb, int node, unsigned int config_id,
50 		uint64_t *config_addr, uint32_t *config_size)
51 {
52 	int err;
53 	unsigned int i;
54 
55 	assert(dtb != NULL);
56 	assert(config_addr != NULL);
57 	assert(config_size != NULL);
58 
59 	for (i = 0; i < ARRAY_SIZE(prop_names); i++) {
60 		if (prop_names[i].config_id == config_id)
61 			break;
62 	}
63 
64 	if (i == ARRAY_SIZE(prop_names)) {
65 		WARN("Invalid config id %d\n", config_id);
66 		return -1;
67 	}
68 
69 	/* Check if the pointer to DT is correct */
70 	assert(fdt_check_header(dtb) == 0);
71 
72 	/* Assert the node offset point to "arm,tb_fw" compatible property */
73 	assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw"));
74 
75 	err = fdtw_read_cells(dtb, node, prop_names[i].config_addr, 2,
76 				(void *) config_addr);
77 	if (err < 0) {
78 		WARN("Read cell failed for %s\n", prop_names[i].config_addr);
79 		return -1;
80 	}
81 
82 	err = fdtw_read_cells(dtb, node, prop_names[i].config_max_size, 1,
83 				(void *) config_size);
84 	if (err < 0) {
85 		WARN("Read cell failed for %s\n", prop_names[i].config_max_size);
86 		return -1;
87 	}
88 
89 	VERBOSE("Dyn cfg: Read config_id %d load info from TB_FW_CONFIG 0x%llx 0x%x\n",
90 				config_id, (unsigned long long)*config_addr, *config_size);
91 
92 	return 0;
93 }
94 
95 /*******************************************************************************
96  * Helper to read the `disable_auth` property in config DTB. This function
97  * expects the following properties to be present in the config DTB.
98  *	name : disable_auth		size : 1 cell
99  *
100  * Arguments:
101  *	void *dtb		 - pointer to the TB_FW_CONFIG in memory
102  *	int node		 - The node offset to appropriate node in the
103  *				   DTB.
104  *	uint64_t *disable_auth	 - The value of `disable_auth` property on
105  *				   successful read. Must be 0 or 1.
106  *
107  * Returns 0 on success and -1 on error.
108  ******************************************************************************/
109 int arm_dyn_get_disable_auth(void *dtb, int node, uint32_t *disable_auth)
110 {
111 	int err;
112 
113 	assert(dtb != NULL);
114 	assert(disable_auth != NULL);
115 
116 	/* Check if the pointer to DT is correct */
117 	assert(fdt_check_header(dtb) == 0);
118 
119 	/* Assert the node offset point to "arm,tb_fw" compatible property */
120 	assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw"));
121 
122 	/* Locate the disable_auth cell and read the value */
123 	err = fdtw_read_cells(dtb, node, "disable_auth", 1, disable_auth);
124 	if (err < 0) {
125 		WARN("Read cell failed for `disable_auth`\n");
126 		return -1;
127 	}
128 
129 	/* Check if the value is boolean */
130 	if ((*disable_auth != 0U) && (*disable_auth != 1U)) {
131 		WARN("Invalid value for `disable_auth` cell %d\n", *disable_auth);
132 		return -1;
133 	}
134 
135 	VERBOSE("Dyn cfg: `disable_auth` cell found with value = %d\n",
136 					*disable_auth);
137 	return 0;
138 }
139 
140 /*******************************************************************************
141  * Validate the tb_fw_config is a valid DTB file and returns the node offset
142  * to "arm,tb_fw" property.
143  * Arguments:
144  *	void *dtb - pointer to the TB_FW_CONFIG in memory
145  *	int *node - Returns the node offset to "arm,tb_fw" property if found.
146  *
147  * Returns 0 on success and -1 on error.
148  ******************************************************************************/
149 int arm_dyn_tb_fw_cfg_init(void *dtb, int *node)
150 {
151 	assert(dtb != NULL);
152 	assert(node != NULL);
153 
154 	/* Check if the pointer to DT is correct */
155 	if (fdt_check_header(dtb) != 0) {
156 		WARN("Invalid DTB file passed as TB_FW_CONFIG\n");
157 		return -1;
158 	}
159 
160 	/* Assert the node offset point to "arm,tb_fw" compatible property */
161 	*node = fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw");
162 	if (*node < 0) {
163 		WARN("The compatible property `arm,tb_fw` not found in the config\n");
164 		return -1;
165 	}
166 
167 	VERBOSE("Dyn cfg: Found \"arm,tb_fw\" in the config\n");
168 	return 0;
169 }
170 
171 /*
172  * Reads and returns the Mbed TLS shared heap information from the DTB.
173  * This function is supposed to be called *only* when a DTB is present.
174  * This function is supposed to be called only by BL2.
175  *
176  * Returns:
177  *	0 = success
178  *	-1 = error. In this case the values of heap_addr, heap_size should be
179  *	    considered as garbage by the caller.
180  */
181 int arm_get_dtb_mbedtls_heap_info(void *dtb, void **heap_addr,
182 	size_t *heap_size)
183 {
184 	int err, dtb_root;
185 
186 	/* Verify the DTB is valid and get the root node */
187 	err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
188 	if (err < 0) {
189 		ERROR("Invalid TB_FW_CONFIG. Cannot retrieve Mbed TLS heap information from DTB\n");
190 		return -1;
191 	}
192 
193 	/* Retrieve the Mbed TLS heap details from the DTB */
194 	err = fdtw_read_cells(dtb, dtb_root,
195 		DTB_PROP_MBEDTLS_HEAP_ADDR, 2, heap_addr);
196 	if (err < 0) {
197 		ERROR("Error while reading %s from DTB\n",
198 			DTB_PROP_MBEDTLS_HEAP_ADDR);
199 		return -1;
200 	}
201 	err = fdtw_read_cells(dtb, dtb_root,
202 		DTB_PROP_MBEDTLS_HEAP_SIZE, 1, heap_size);
203 	if (err < 0) {
204 		ERROR("Error while reading %s from DTB\n",
205 			DTB_PROP_MBEDTLS_HEAP_SIZE);
206 		return -1;
207 	}
208 	return 0;
209 }
210 
211 
212 /*
213  * This function writes the Mbed TLS heap address and size in the DTB. When it
214  * is called, it is guaranteed that a DTB is available. However it is not
215  * guaranteed that the shared Mbed TLS heap implementation is used. Thus we
216  * return error code from here and it's the responsibility of the caller to
217  * determine the action upon error.
218  *
219  * This function is supposed to be called only by BL1.
220  *
221  * Returns:
222  *	0 = success
223  *	1 = error
224  */
225 int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size)
226 {
227 	int err, dtb_root;
228 
229 	/*
230 	 * Verify that the DTB is valid, before attempting to write to it,
231 	 * and get the DTB root node.
232 	 */
233 	err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
234 	if (err < 0) {
235 		ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n");
236 		return -1;
237 	}
238 
239 	/*
240 	 * Write the heap address and size in the DTB.
241 	 *
242 	 * NOTE: The variables heap_addr and heap_size are corrupted
243 	 * by the "fdtw_write_inplace_cells" function. After the
244 	 * function calls they must NOT be reused.
245 	 */
246 	err = fdtw_write_inplace_cells(dtb, dtb_root,
247 		DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr);
248 	if (err < 0) {
249 		ERROR("Unable to write DTB property %s\n",
250 			DTB_PROP_MBEDTLS_HEAP_ADDR);
251 		return -1;
252 	}
253 
254 	err = fdtw_write_inplace_cells(dtb, dtb_root,
255 		DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size);
256 	if (err < 0) {
257 		ERROR("Unable to write DTB property %s\n",
258 			DTB_PROP_MBEDTLS_HEAP_SIZE);
259 		return -1;
260 	}
261 
262 	return 0;
263 }
264