xref: /rk3399_ARM-atf/plat/arm/common/arm_dyn_cfg.c (revision 3b5ea741fd92088c9e005d3508b73e50f1d5daf7)
1c228956aSSoby Mathew /*
2*3b5ea741SLouis Mayencourt  * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
3c228956aSSoby Mathew  *
4c228956aSSoby Mathew  * SPDX-License-Identifier: BSD-3-Clause
5c228956aSSoby Mathew  */
6c228956aSSoby Mathew 
7c228956aSSoby Mathew #include <assert.h>
8c228956aSSoby Mathew #include <string.h>
96c77dfc5SLouis Mayencourt #include <libfdt.h>
10c228956aSSoby Mathew 
1109d40e0eSAntonio Nino Diaz #include <platform_def.h>
1209d40e0eSAntonio Nino Diaz 
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
1409d40e0eSAntonio Nino Diaz #include <common/desc_image_load.h>
1509d40e0eSAntonio Nino Diaz #include <common/tbbr/tbbr_img_def.h>
1609d40e0eSAntonio Nino Diaz #if TRUSTED_BOARD_BOOT
1709d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_config.h>
1809d40e0eSAntonio Nino Diaz #endif
19bd9344f6SAntonio Nino Diaz #include <plat/arm/common/arm_dyn_cfg_helpers.h>
20bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
2109d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2209d40e0eSAntonio Nino Diaz 
2360e19f57SAntonio Nino Diaz /* Variable to store the address to TB_FW_CONFIG passed from BL1 */
24cab0b5b0SSoby Mathew static void *tb_fw_cfg_dtb;
25ba597da7SJohn Tsichritzis 
26ba597da7SJohn Tsichritzis #if TRUSTED_BOARD_BOOT
27ba597da7SJohn Tsichritzis 
28ba597da7SJohn Tsichritzis static void *mbedtls_heap_addr;
29ba597da7SJohn Tsichritzis static size_t mbedtls_heap_size;
30ba597da7SJohn Tsichritzis 
31ba597da7SJohn Tsichritzis /*
32ba597da7SJohn Tsichritzis  * This function is the implementation of the shared Mbed TLS heap between
33ba597da7SJohn Tsichritzis  * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1
34ba597da7SJohn Tsichritzis  * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file
35ba597da7SJohn Tsichritzis  * which is a DTB.
36ba597da7SJohn Tsichritzis  *
37ba597da7SJohn Tsichritzis  * This function is placed inside an #if directive for the below reasons:
38ba597da7SJohn Tsichritzis  *   - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot
39ba597da7SJohn Tsichritzis  *     is enabled.
40ba597da7SJohn Tsichritzis  *   - This implementation requires the DTB to be present so that BL1 has a
4160e19f57SAntonio Nino Diaz  *     mechanism to pass the pointer to BL2.
42ba597da7SJohn Tsichritzis  */
43ba597da7SJohn Tsichritzis int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
44ba597da7SJohn Tsichritzis {
45ba597da7SJohn Tsichritzis 	assert(heap_addr != NULL);
46ba597da7SJohn Tsichritzis 	assert(heap_size != NULL);
47ba597da7SJohn Tsichritzis 
48ba597da7SJohn Tsichritzis #if defined(IMAGE_BL1) || BL2_AT_EL3
49ba597da7SJohn Tsichritzis 
50ba597da7SJohn Tsichritzis 	/* If in BL1 or BL2_AT_EL3 define a heap */
51ba597da7SJohn Tsichritzis 	static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
52ba597da7SJohn Tsichritzis 
53ba597da7SJohn Tsichritzis 	*heap_addr = heap;
54ba597da7SJohn Tsichritzis 	*heap_size = sizeof(heap);
55ba597da7SJohn Tsichritzis 	mbedtls_heap_addr = heap;
56ba597da7SJohn Tsichritzis 	mbedtls_heap_size = sizeof(heap);
57ba597da7SJohn Tsichritzis 
58ba597da7SJohn Tsichritzis #elif defined(IMAGE_BL2)
59ba597da7SJohn Tsichritzis 
60ba597da7SJohn Tsichritzis 	int err;
61ba597da7SJohn Tsichritzis 
62ba597da7SJohn Tsichritzis 	/* If in BL2, retrieve the already allocated heap's info from DTB */
63a606031eSJohn Tsichritzis 	if (tb_fw_cfg_dtb != NULL) {
64ba597da7SJohn Tsichritzis 		err = arm_get_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, heap_addr,
65ba597da7SJohn Tsichritzis 			heap_size);
66ba597da7SJohn Tsichritzis 		if (err < 0) {
67a606031eSJohn Tsichritzis 			ERROR("BL2: unable to retrieve shared Mbed TLS heap information from DTB\n");
68a606031eSJohn Tsichritzis 			panic();
69a606031eSJohn Tsichritzis 		}
70a606031eSJohn Tsichritzis 	} else {
71a606031eSJohn Tsichritzis 		ERROR("BL2: DTB missing, cannot get Mbed TLS heap\n");
72ba597da7SJohn Tsichritzis 		panic();
73ba597da7SJohn Tsichritzis 	}
74ba597da7SJohn Tsichritzis #endif
75ba597da7SJohn Tsichritzis 
76ba597da7SJohn Tsichritzis 	return 0;
77ba597da7SJohn Tsichritzis }
78ba597da7SJohn Tsichritzis 
79ba597da7SJohn Tsichritzis /*
80ba597da7SJohn Tsichritzis  * Puts the shared Mbed TLS heap information to the DTB.
81ba597da7SJohn Tsichritzis  * Executed only from BL1.
82ba597da7SJohn Tsichritzis  */
83ba597da7SJohn Tsichritzis void arm_bl1_set_mbedtls_heap(void)
84ba597da7SJohn Tsichritzis {
85ba597da7SJohn Tsichritzis 	int err;
86ba597da7SJohn Tsichritzis 
87ba597da7SJohn Tsichritzis 	/*
88ba597da7SJohn Tsichritzis 	 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
89ba597da7SJohn Tsichritzis 	 * platform. As such, we don't attempt to write to the DTB at all.
90ba597da7SJohn Tsichritzis 	 *
91ba597da7SJohn Tsichritzis 	 * If mbedtls_heap_addr==NULL, then it means we are using the default
92ba597da7SJohn Tsichritzis 	 * heap implementation. As such, BL2 will have its own heap for sure
93ba597da7SJohn Tsichritzis 	 * and hence there is no need to pass any information to the DTB.
94ba597da7SJohn Tsichritzis 	 *
95ba597da7SJohn Tsichritzis 	 * In the latter case, if we still wanted to write in the DTB the heap
96ba597da7SJohn Tsichritzis 	 * information, we would need to call plat_get_mbedtls_heap to retrieve
97ba597da7SJohn Tsichritzis 	 * the default heap's address and size.
98ba597da7SJohn Tsichritzis 	 */
99ba597da7SJohn Tsichritzis 	if ((tb_fw_cfg_dtb != NULL) && (mbedtls_heap_addr != NULL)) {
100ba597da7SJohn Tsichritzis 		err = arm_set_dtb_mbedtls_heap_info(tb_fw_cfg_dtb,
101ba597da7SJohn Tsichritzis 			mbedtls_heap_addr, mbedtls_heap_size);
102ba597da7SJohn Tsichritzis 		if (err < 0) {
103a606031eSJohn Tsichritzis 			ERROR("BL1: unable to write shared Mbed TLS heap information to DTB\n");
104ba597da7SJohn Tsichritzis 			panic();
105ba597da7SJohn Tsichritzis 		}
10663cc2658SJohn Tsichritzis 		/*
10763cc2658SJohn Tsichritzis 		 * Ensure that the info written to the DTB is visible to other
10863cc2658SJohn Tsichritzis 		 * images. It's critical because BL2 won't be able to proceed
10963cc2658SJohn Tsichritzis 		 * without the heap info.
11063cc2658SJohn Tsichritzis 		 */
11163cc2658SJohn Tsichritzis 		flush_dcache_range((uintptr_t)tb_fw_cfg_dtb,
1126c77dfc5SLouis Mayencourt 			fdt_totalsize(tb_fw_cfg_dtb));
113ba597da7SJohn Tsichritzis 	}
114ba597da7SJohn Tsichritzis }
115ba597da7SJohn Tsichritzis 
116ba597da7SJohn Tsichritzis #endif /* TRUSTED_BOARD_BOOT */
117ba597da7SJohn Tsichritzis 
118c228956aSSoby Mathew /*
119cab0b5b0SSoby Mathew  * BL2 utility function to set the address of TB_FW_CONFIG passed from BL1.
120cab0b5b0SSoby Mathew  */
121cab0b5b0SSoby Mathew void arm_bl2_set_tb_cfg_addr(void *dtb)
122cab0b5b0SSoby Mathew {
123da5f2745SSoby Mathew 	assert(dtb != NULL);
124cab0b5b0SSoby Mathew 	tb_fw_cfg_dtb = dtb;
125cab0b5b0SSoby Mathew }
126cab0b5b0SSoby Mathew 
127cab0b5b0SSoby Mathew /*
128cab0b5b0SSoby Mathew  * BL2 utility function to initialize dynamic configuration specified by
1291d71ba14SSoby Mathew  * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
1301d71ba14SSoby Mathew  * specified in TB_FW_CONFIG.
131cab0b5b0SSoby Mathew  */
132cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void)
133cab0b5b0SSoby Mathew {
1341d71ba14SSoby Mathew 	int err = 0, tb_fw_node;
1351d71ba14SSoby Mathew 	unsigned int i;
1361d71ba14SSoby Mathew 	bl_mem_params_node_t *cfg_mem_params = NULL;
1371d71ba14SSoby Mathew 	uint64_t image_base;
1381d71ba14SSoby Mathew 	uint32_t image_size;
1391d71ba14SSoby Mathew 	const unsigned int config_ids[] = {
1401d71ba14SSoby Mathew 			HW_CONFIG_ID,
1411d71ba14SSoby Mathew 			SOC_FW_CONFIG_ID,
1421d71ba14SSoby Mathew 			NT_FW_CONFIG_ID,
1431d71ba14SSoby Mathew #ifdef SPD_tspd
1441d71ba14SSoby Mathew 			/* Currently tos_fw_config is only present for TSP */
1451d71ba14SSoby Mathew 			TOS_FW_CONFIG_ID
1461d71ba14SSoby Mathew #endif
1471d71ba14SSoby Mathew 	};
148cab0b5b0SSoby Mathew 
149cab0b5b0SSoby Mathew 	if (tb_fw_cfg_dtb == NULL) {
150cab0b5b0SSoby Mathew 		VERBOSE("No TB_FW_CONFIG specified\n");
151cab0b5b0SSoby Mathew 		return;
152cab0b5b0SSoby Mathew 	}
153cab0b5b0SSoby Mathew 
154432f0ad0SJohn Tsichritzis 	err = arm_dyn_tb_fw_cfg_init(tb_fw_cfg_dtb, &tb_fw_node);
155cab0b5b0SSoby Mathew 	if (err < 0) {
156cab0b5b0SSoby Mathew 		ERROR("Invalid TB_FW_CONFIG passed from BL1\n");
157cab0b5b0SSoby Mathew 		panic();
158cab0b5b0SSoby Mathew 	}
159cab0b5b0SSoby Mathew 
1601d71ba14SSoby Mathew 	/* Iterate through all the fw config IDs */
1611d71ba14SSoby Mathew 	for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
1621d71ba14SSoby Mathew 		/* Get the config load address and size from TB_FW_CONFIG */
1631d71ba14SSoby Mathew 		cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
1641d71ba14SSoby Mathew 		if (cfg_mem_params == NULL) {
165cab0b5b0SSoby Mathew 			VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
1661d71ba14SSoby Mathew 			continue;
167cab0b5b0SSoby Mathew 		}
168cab0b5b0SSoby Mathew 
169432f0ad0SJohn Tsichritzis 		err = arm_dyn_get_config_load_info(tb_fw_cfg_dtb, tb_fw_node,
1701d71ba14SSoby Mathew 				config_ids[i], &image_base, &image_size);
171cab0b5b0SSoby Mathew 		if (err < 0) {
1721d71ba14SSoby Mathew 			VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n",
1731d71ba14SSoby Mathew 					config_ids[i]);
1741d71ba14SSoby Mathew 			continue;
175cab0b5b0SSoby Mathew 		}
176cab0b5b0SSoby Mathew 
1771d71ba14SSoby Mathew 		/*
1781d71ba14SSoby Mathew 		 * Do some runtime checks on the load addresses of soc_fw_config,
1791d71ba14SSoby Mathew 		 * tos_fw_config, nt_fw_config. This is not a comprehensive check
1801d71ba14SSoby Mathew 		 * of all invalid addresses but to prevent trivial porting errors.
1811d71ba14SSoby Mathew 		 */
1821d71ba14SSoby Mathew 		if (config_ids[i] != HW_CONFIG_ID) {
1831d71ba14SSoby Mathew 
184b8a02d53SAntonio Nino Diaz 			if (check_uptr_overflow(image_base, image_size))
1851d71ba14SSoby Mathew 				continue;
1861d71ba14SSoby Mathew 
1876393c787SUsama Arif #ifdef	BL31_BASE
188c099cd39SSoby Mathew 			/* Ensure the configs don't overlap with BL31 */
1895ddcbdd8SAlexei Fedorov 			if ((image_base >= BL31_BASE) &&
1905ddcbdd8SAlexei Fedorov 			    (image_base <= BL31_LIMIT))
1911d71ba14SSoby Mathew 				continue;
1926393c787SUsama Arif #endif
1931d71ba14SSoby Mathew 			/* Ensure the configs are loaded in a valid address */
1941d71ba14SSoby Mathew 			if (image_base < ARM_BL_RAM_BASE)
1951d71ba14SSoby Mathew 				continue;
1961d71ba14SSoby Mathew #ifdef BL32_BASE
1971d71ba14SSoby Mathew 			/*
1981d71ba14SSoby Mathew 			 * If BL32 is present, ensure that the configs don't
1991d71ba14SSoby Mathew 			 * overlap with it.
2001d71ba14SSoby Mathew 			 */
2015ddcbdd8SAlexei Fedorov 			if ((image_base >= BL32_BASE) &&
2025ddcbdd8SAlexei Fedorov 			    (image_base <= BL32_LIMIT))
2031d71ba14SSoby Mathew 				continue;
2041d71ba14SSoby Mathew #endif
2051d71ba14SSoby Mathew 		}
2061d71ba14SSoby Mathew 
2071d71ba14SSoby Mathew 
2081d71ba14SSoby Mathew 		cfg_mem_params->image_info.image_base = (uintptr_t)image_base;
2091d71ba14SSoby Mathew 		cfg_mem_params->image_info.image_max_size = image_size;
2101d71ba14SSoby Mathew 
2115ddcbdd8SAlexei Fedorov 		/*
2125ddcbdd8SAlexei Fedorov 		 * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from
2135ddcbdd8SAlexei Fedorov 		 * HW_CONFIG or FW_CONFIG nodes
2145ddcbdd8SAlexei Fedorov 		 */
2151d71ba14SSoby Mathew 		cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
2161d71ba14SSoby Mathew 	}
2176e79f9fdSSoby Mathew 
2186e79f9fdSSoby Mathew #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
2196e79f9fdSSoby Mathew 	uint32_t disable_auth = 0;
2206e79f9fdSSoby Mathew 
221432f0ad0SJohn Tsichritzis 	err = arm_dyn_get_disable_auth(tb_fw_cfg_dtb, tb_fw_node,
2226e79f9fdSSoby Mathew 					&disable_auth);
2236e79f9fdSSoby Mathew 	if (err < 0)
2246e79f9fdSSoby Mathew 		return;
2256e79f9fdSSoby Mathew 
2266e79f9fdSSoby Mathew 	if (disable_auth == 1)
2276e79f9fdSSoby Mathew 		dyn_disable_auth();
2286e79f9fdSSoby Mathew #endif
229cab0b5b0SSoby Mathew }
230