xref: /rk3399_ARM-atf/plat/arm/common/arm_dyn_cfg_helpers.c (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
1cab0b5b0SSoby Mathew /*
2cab0b5b0SSoby Mathew  * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3cab0b5b0SSoby Mathew  *
4cab0b5b0SSoby Mathew  * SPDX-License-Identifier: BSD-3-Clause
5cab0b5b0SSoby Mathew  */
6cab0b5b0SSoby Mathew 
7cab0b5b0SSoby Mathew #include <assert.h>
8*09d40e0eSAntonio Nino Diaz 
9cab0b5b0SSoby Mathew #include <libfdt.h>
10*09d40e0eSAntonio Nino Diaz 
11*09d40e0eSAntonio Nino Diaz #include <common/desc_image_load.h>
12*09d40e0eSAntonio Nino Diaz #include <common/fdt_wrappers.h>
13*09d40e0eSAntonio Nino Diaz 
14*09d40e0eSAntonio Nino Diaz #include <arm_dyn_cfg_helpers.h>
15da5f2745SSoby Mathew #include <plat_arm.h>
16cab0b5b0SSoby Mathew 
17ba597da7SJohn Tsichritzis #define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr"
18ba597da7SJohn Tsichritzis #define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size"
191d71ba14SSoby Mathew 
201d71ba14SSoby Mathew typedef struct config_load_info_prop {
211d71ba14SSoby Mathew 	unsigned int config_id;
221d71ba14SSoby Mathew 	const char *config_addr;
231d71ba14SSoby Mathew 	const char *config_max_size;
241d71ba14SSoby Mathew } config_load_info_prop_t;
251d71ba14SSoby Mathew 
261d71ba14SSoby Mathew static const config_load_info_prop_t prop_names[] = {
271d71ba14SSoby Mathew 	{HW_CONFIG_ID, "hw_config_addr", "hw_config_max_size"},
281d71ba14SSoby Mathew 	{SOC_FW_CONFIG_ID, "soc_fw_config_addr", "soc_fw_config_max_size"},
291d71ba14SSoby Mathew 	{TOS_FW_CONFIG_ID, "tos_fw_config_addr", "tos_fw_config_max_size"},
301d71ba14SSoby Mathew 	{NT_FW_CONFIG_ID, "nt_fw_config_addr", "nt_fw_config_max_size"}
311d71ba14SSoby Mathew };
321d71ba14SSoby Mathew 
33cab0b5b0SSoby Mathew /*******************************************************************************
341d71ba14SSoby Mathew  * Helper to read the load information corresponding to the `config_id` in
351d71ba14SSoby Mathew  * TB_FW_CONFIG. This function expects the following properties to be defined :
361d71ba14SSoby Mathew  *	<config>_addr		size : 2 cells
371d71ba14SSoby Mathew  *	<config>_max_size	size : 1 cell
38cab0b5b0SSoby Mathew  *
39cab0b5b0SSoby Mathew  * Arguments:
40cab0b5b0SSoby Mathew  *	void *dtb		 - pointer to the TB_FW_CONFIG in memory
41cab0b5b0SSoby Mathew  *	int node		 - The node offset to appropriate node in the
42cab0b5b0SSoby Mathew  *					 DTB.
431d71ba14SSoby Mathew  *	unsigned int config_id	 - The configuration id
441d71ba14SSoby Mathew  *	uint64_t *config_addr	 - Returns the `config` load address if read
45cab0b5b0SSoby Mathew  *					 is successful.
461d71ba14SSoby Mathew  *	uint32_t *config_size	 - Returns the `config` size if read is
47cab0b5b0SSoby Mathew  *					 successful.
48cab0b5b0SSoby Mathew  *
49cab0b5b0SSoby Mathew  * Returns 0 on success and -1 on error.
50cab0b5b0SSoby Mathew  ******************************************************************************/
511d71ba14SSoby Mathew int arm_dyn_get_config_load_info(void *dtb, int node, unsigned int config_id,
521d71ba14SSoby Mathew 		uint64_t *config_addr, uint32_t *config_size)
53cab0b5b0SSoby Mathew {
54cab0b5b0SSoby Mathew 	int err;
551d71ba14SSoby Mathew 	unsigned int i;
56cab0b5b0SSoby Mathew 
57da5f2745SSoby Mathew 	assert(dtb != NULL);
581d71ba14SSoby Mathew 	assert(config_addr != NULL);
591d71ba14SSoby Mathew 	assert(config_size != NULL);
601d71ba14SSoby Mathew 
611d71ba14SSoby Mathew 	for (i = 0; i < ARRAY_SIZE(prop_names); i++) {
621d71ba14SSoby Mathew 		if (prop_names[i].config_id == config_id)
631d71ba14SSoby Mathew 			break;
641d71ba14SSoby Mathew 	}
651d71ba14SSoby Mathew 
661d71ba14SSoby Mathew 	if (i == ARRAY_SIZE(prop_names)) {
671d71ba14SSoby Mathew 		WARN("Invalid config id %d\n", config_id);
681d71ba14SSoby Mathew 		return -1;
691d71ba14SSoby Mathew 	}
70cab0b5b0SSoby Mathew 
71cab0b5b0SSoby Mathew 	/* Check if the pointer to DT is correct */
72cab0b5b0SSoby Mathew 	assert(fdt_check_header(dtb) == 0);
73cab0b5b0SSoby Mathew 
74cab0b5b0SSoby Mathew 	/* Assert the node offset point to "arm,tb_fw" compatible property */
75cab0b5b0SSoby Mathew 	assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw"));
76cab0b5b0SSoby Mathew 
771d71ba14SSoby Mathew 	err = fdtw_read_cells(dtb, node, prop_names[i].config_addr, 2,
781d71ba14SSoby Mathew 				(void *) config_addr);
79cab0b5b0SSoby Mathew 	if (err < 0) {
801d71ba14SSoby Mathew 		WARN("Read cell failed for %s\n", prop_names[i].config_addr);
81cab0b5b0SSoby Mathew 		return -1;
82cab0b5b0SSoby Mathew 	}
83cab0b5b0SSoby Mathew 
841d71ba14SSoby Mathew 	err = fdtw_read_cells(dtb, node, prop_names[i].config_max_size, 1,
851d71ba14SSoby Mathew 				(void *) config_size);
86cab0b5b0SSoby Mathew 	if (err < 0) {
871d71ba14SSoby Mathew 		WARN("Read cell failed for %s\n", prop_names[i].config_max_size);
88cab0b5b0SSoby Mathew 		return -1;
89cab0b5b0SSoby Mathew 	}
90cab0b5b0SSoby Mathew 
911d71ba14SSoby Mathew 	VERBOSE("Dyn cfg: Read config_id %d load info from TB_FW_CONFIG 0x%llx 0x%x\n",
921d71ba14SSoby Mathew 				config_id, (unsigned long long)*config_addr, *config_size);
93cab0b5b0SSoby Mathew 
94cab0b5b0SSoby Mathew 	return 0;
95cab0b5b0SSoby Mathew }
96cab0b5b0SSoby Mathew 
97cab0b5b0SSoby Mathew /*******************************************************************************
986e79f9fdSSoby Mathew  * Helper to read the `disable_auth` property in config DTB. This function
996e79f9fdSSoby Mathew  * expects the following properties to be present in the config DTB.
1006e79f9fdSSoby Mathew  *	name : disable_auth		size : 1 cell
1016e79f9fdSSoby Mathew  *
1026e79f9fdSSoby Mathew  * Arguments:
1036e79f9fdSSoby Mathew  *	void *dtb		 - pointer to the TB_FW_CONFIG in memory
1046e79f9fdSSoby Mathew  *	int node		 - The node offset to appropriate node in the
1056e79f9fdSSoby Mathew  *				   DTB.
1066e79f9fdSSoby Mathew  *	uint64_t *disable_auth	 - The value of `disable_auth` property on
1076e79f9fdSSoby Mathew  *				   successful read. Must be 0 or 1.
1086e79f9fdSSoby Mathew  *
1096e79f9fdSSoby Mathew  * Returns 0 on success and -1 on error.
1106e79f9fdSSoby Mathew  ******************************************************************************/
1116e79f9fdSSoby Mathew int arm_dyn_get_disable_auth(void *dtb, int node, uint32_t *disable_auth)
1126e79f9fdSSoby Mathew {
1136e79f9fdSSoby Mathew 	int err;
1146e79f9fdSSoby Mathew 
1156e79f9fdSSoby Mathew 	assert(dtb != NULL);
1166e79f9fdSSoby Mathew 	assert(disable_auth != NULL);
1176e79f9fdSSoby Mathew 
1186e79f9fdSSoby Mathew 	/* Check if the pointer to DT is correct */
1196e79f9fdSSoby Mathew 	assert(fdt_check_header(dtb) == 0);
1206e79f9fdSSoby Mathew 
1216e79f9fdSSoby Mathew 	/* Assert the node offset point to "arm,tb_fw" compatible property */
1226e79f9fdSSoby Mathew 	assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw"));
1236e79f9fdSSoby Mathew 
1246e79f9fdSSoby Mathew 	/* Locate the disable_auth cell and read the value */
1256e79f9fdSSoby Mathew 	err = fdtw_read_cells(dtb, node, "disable_auth", 1, disable_auth);
1266e79f9fdSSoby Mathew 	if (err < 0) {
1276e79f9fdSSoby Mathew 		WARN("Read cell failed for `disable_auth`\n");
1286e79f9fdSSoby Mathew 		return -1;
1296e79f9fdSSoby Mathew 	}
1306e79f9fdSSoby Mathew 
1316e79f9fdSSoby Mathew 	/* Check if the value is boolean */
1321d71ba14SSoby Mathew 	if ((*disable_auth != 0U) && (*disable_auth != 1U)) {
1336e79f9fdSSoby Mathew 		WARN("Invalid value for `disable_auth` cell %d\n", *disable_auth);
1346e79f9fdSSoby Mathew 		return -1;
1356e79f9fdSSoby Mathew 	}
1366e79f9fdSSoby Mathew 
1376e79f9fdSSoby Mathew 	VERBOSE("Dyn cfg: `disable_auth` cell found with value = %d\n",
1386e79f9fdSSoby Mathew 					*disable_auth);
1396e79f9fdSSoby Mathew 	return 0;
1406e79f9fdSSoby Mathew }
1416e79f9fdSSoby Mathew 
1426e79f9fdSSoby Mathew /*******************************************************************************
143cab0b5b0SSoby Mathew  * Validate the tb_fw_config is a valid DTB file and returns the node offset
144cab0b5b0SSoby Mathew  * to "arm,tb_fw" property.
145cab0b5b0SSoby Mathew  * Arguments:
146cab0b5b0SSoby Mathew  *	void *dtb - pointer to the TB_FW_CONFIG in memory
147cab0b5b0SSoby Mathew  *	int *node - Returns the node offset to "arm,tb_fw" property if found.
148cab0b5b0SSoby Mathew  *
149cab0b5b0SSoby Mathew  * Returns 0 on success and -1 on error.
150cab0b5b0SSoby Mathew  ******************************************************************************/
151cab0b5b0SSoby Mathew int arm_dyn_tb_fw_cfg_init(void *dtb, int *node)
152cab0b5b0SSoby Mathew {
153da5f2745SSoby Mathew 	assert(dtb != NULL);
154da5f2745SSoby Mathew 	assert(node != NULL);
155cab0b5b0SSoby Mathew 
156cab0b5b0SSoby Mathew 	/* Check if the pointer to DT is correct */
157cab0b5b0SSoby Mathew 	if (fdt_check_header(dtb) != 0) {
158cab0b5b0SSoby Mathew 		WARN("Invalid DTB file passed as TB_FW_CONFIG\n");
159cab0b5b0SSoby Mathew 		return -1;
160cab0b5b0SSoby Mathew 	}
161cab0b5b0SSoby Mathew 
162cab0b5b0SSoby Mathew 	/* Assert the node offset point to "arm,tb_fw" compatible property */
163cab0b5b0SSoby Mathew 	*node = fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw");
164cab0b5b0SSoby Mathew 	if (*node < 0) {
165cab0b5b0SSoby Mathew 		WARN("The compatible property `arm,tb_fw` not found in the config\n");
166cab0b5b0SSoby Mathew 		return -1;
167cab0b5b0SSoby Mathew 	}
168cab0b5b0SSoby Mathew 
169cab0b5b0SSoby Mathew 	VERBOSE("Dyn cfg: Found \"arm,tb_fw\" in the config\n");
170cab0b5b0SSoby Mathew 	return 0;
171cab0b5b0SSoby Mathew }
172ba597da7SJohn Tsichritzis 
173ba597da7SJohn Tsichritzis /*
174ba597da7SJohn Tsichritzis  * Reads and returns the Mbed TLS shared heap information from the DTB.
175ba597da7SJohn Tsichritzis  * This function is supposed to be called *only* when a DTB is present.
176ba597da7SJohn Tsichritzis  * This function is supposed to be called only by BL2.
177ba597da7SJohn Tsichritzis  *
178ba597da7SJohn Tsichritzis  * Returns:
179ba597da7SJohn Tsichritzis  *	0 = success
180ba597da7SJohn Tsichritzis  *	-1 = error. In this case the values of heap_addr, heap_size should be
181ba597da7SJohn Tsichritzis  *	    considered as garbage by the caller.
182ba597da7SJohn Tsichritzis  */
183ba597da7SJohn Tsichritzis int arm_get_dtb_mbedtls_heap_info(void *dtb, void **heap_addr,
184ba597da7SJohn Tsichritzis 	size_t *heap_size)
185ba597da7SJohn Tsichritzis {
186ba597da7SJohn Tsichritzis 	int err, dtb_root;
187ba597da7SJohn Tsichritzis 
188ba597da7SJohn Tsichritzis 	/* Verify the DTB is valid and get the root node */
189ba597da7SJohn Tsichritzis 	err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
190ba597da7SJohn Tsichritzis 	if (err < 0) {
1917af2dd2eSJohn Tsichritzis 		ERROR("Invalid TB_FW_CONFIG. Cannot retrieve Mbed TLS heap information from DTB\n");
192ba597da7SJohn Tsichritzis 		return -1;
193ba597da7SJohn Tsichritzis 	}
194ba597da7SJohn Tsichritzis 
195ba597da7SJohn Tsichritzis 	/* Retrieve the Mbed TLS heap details from the DTB */
196ba597da7SJohn Tsichritzis 	err = fdtw_read_cells(dtb, dtb_root,
197ba597da7SJohn Tsichritzis 		DTB_PROP_MBEDTLS_HEAP_ADDR, 2, heap_addr);
198ba597da7SJohn Tsichritzis 	if (err < 0) {
1997af2dd2eSJohn Tsichritzis 		ERROR("Error while reading %s from DTB\n",
200ba597da7SJohn Tsichritzis 			DTB_PROP_MBEDTLS_HEAP_ADDR);
201ba597da7SJohn Tsichritzis 		return -1;
202ba597da7SJohn Tsichritzis 	}
203ba597da7SJohn Tsichritzis 	err = fdtw_read_cells(dtb, dtb_root,
204ba597da7SJohn Tsichritzis 		DTB_PROP_MBEDTLS_HEAP_SIZE, 1, heap_size);
205ba597da7SJohn Tsichritzis 	if (err < 0) {
2067af2dd2eSJohn Tsichritzis 		ERROR("Error while reading %s from DTB\n",
207ba597da7SJohn Tsichritzis 			DTB_PROP_MBEDTLS_HEAP_SIZE);
208ba597da7SJohn Tsichritzis 		return -1;
209ba597da7SJohn Tsichritzis 	}
210ba597da7SJohn Tsichritzis 	return 0;
211ba597da7SJohn Tsichritzis }
212ba597da7SJohn Tsichritzis 
213ba597da7SJohn Tsichritzis 
214ba597da7SJohn Tsichritzis /*
215ba597da7SJohn Tsichritzis  * This function writes the Mbed TLS heap address and size in the DTB. When it
216ba597da7SJohn Tsichritzis  * is called, it is guaranteed that a DTB is available. However it is not
217ba597da7SJohn Tsichritzis  * guaranteed that the shared Mbed TLS heap implementation is used. Thus we
218ba597da7SJohn Tsichritzis  * return error code from here and it's the responsibility of the caller to
219ba597da7SJohn Tsichritzis  * determine the action upon error.
220ba597da7SJohn Tsichritzis  *
221ba597da7SJohn Tsichritzis  * This function is supposed to be called only by BL1.
222ba597da7SJohn Tsichritzis  *
223ba597da7SJohn Tsichritzis  * Returns:
224ba597da7SJohn Tsichritzis  *	0 = success
225ba597da7SJohn Tsichritzis  *	1 = error
226ba597da7SJohn Tsichritzis  */
227ba597da7SJohn Tsichritzis int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size)
228ba597da7SJohn Tsichritzis {
229ba597da7SJohn Tsichritzis 	int err, dtb_root;
230ba597da7SJohn Tsichritzis 
231ba597da7SJohn Tsichritzis 	/*
232ba597da7SJohn Tsichritzis 	 * Verify that the DTB is valid, before attempting to write to it,
233ba597da7SJohn Tsichritzis 	 * and get the DTB root node.
234ba597da7SJohn Tsichritzis 	 */
235ba597da7SJohn Tsichritzis 	err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
236ba597da7SJohn Tsichritzis 	if (err < 0) {
2377af2dd2eSJohn Tsichritzis 		ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n");
238ba597da7SJohn Tsichritzis 		return -1;
239ba597da7SJohn Tsichritzis 	}
240ba597da7SJohn Tsichritzis 
241ba597da7SJohn Tsichritzis 	/*
242ba597da7SJohn Tsichritzis 	 * Write the heap address and size in the DTB.
243ba597da7SJohn Tsichritzis 	 *
244ba597da7SJohn Tsichritzis 	 * NOTE: The variables heap_addr and heap_size are corrupted
245ba597da7SJohn Tsichritzis 	 * by the "fdtw_write_inplace_cells" function. After the
246ba597da7SJohn Tsichritzis 	 * function calls they must NOT be reused.
247ba597da7SJohn Tsichritzis 	 */
248ba597da7SJohn Tsichritzis 	err = fdtw_write_inplace_cells(dtb, dtb_root,
249ba597da7SJohn Tsichritzis 		DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr);
250ba597da7SJohn Tsichritzis 	if (err < 0) {
2517af2dd2eSJohn Tsichritzis 		ERROR("Unable to write DTB property %s\n",
2527af2dd2eSJohn Tsichritzis 			DTB_PROP_MBEDTLS_HEAP_ADDR);
253ba597da7SJohn Tsichritzis 		return -1;
254ba597da7SJohn Tsichritzis 	}
255ba597da7SJohn Tsichritzis 
256ba597da7SJohn Tsichritzis 	err = fdtw_write_inplace_cells(dtb, dtb_root,
257ba597da7SJohn Tsichritzis 		DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size);
258ba597da7SJohn Tsichritzis 	if (err < 0) {
2597af2dd2eSJohn Tsichritzis 		ERROR("Unable to write DTB property %s\n",
2607af2dd2eSJohn Tsichritzis 			DTB_PROP_MBEDTLS_HEAP_SIZE);
261ba597da7SJohn Tsichritzis 		return -1;
262ba597da7SJohn Tsichritzis 	}
263ba597da7SJohn Tsichritzis 
264ba597da7SJohn Tsichritzis 	return 0;
265ba597da7SJohn Tsichritzis }
266