xref: /rk3399_ARM-atf/plat/arm/common/arm_dyn_cfg.c (revision 3b5ea741fd92088c9e005d3508b73e50f1d5daf7)
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 #include <string.h>
9 #include <libfdt.h>
10 
11 #include <platform_def.h>
12 
13 #include <common/debug.h>
14 #include <common/desc_image_load.h>
15 #include <common/tbbr/tbbr_img_def.h>
16 #if TRUSTED_BOARD_BOOT
17 #include <drivers/auth/mbedtls/mbedtls_config.h>
18 #endif
19 #include <plat/arm/common/arm_dyn_cfg_helpers.h>
20 #include <plat/arm/common/plat_arm.h>
21 #include <plat/common/platform.h>
22 
23 /* Variable to store the address to TB_FW_CONFIG passed from BL1 */
24 static void *tb_fw_cfg_dtb;
25 
26 #if TRUSTED_BOARD_BOOT
27 
28 static void *mbedtls_heap_addr;
29 static size_t mbedtls_heap_size;
30 
31 /*
32  * This function is the implementation of the shared Mbed TLS heap between
33  * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1
34  * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file
35  * which is a DTB.
36  *
37  * This function is placed inside an #if directive for the below reasons:
38  *   - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot
39  *     is enabled.
40  *   - This implementation requires the DTB to be present so that BL1 has a
41  *     mechanism to pass the pointer to BL2.
42  */
43 int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
44 {
45 	assert(heap_addr != NULL);
46 	assert(heap_size != NULL);
47 
48 #if defined(IMAGE_BL1) || BL2_AT_EL3
49 
50 	/* If in BL1 or BL2_AT_EL3 define a heap */
51 	static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
52 
53 	*heap_addr = heap;
54 	*heap_size = sizeof(heap);
55 	mbedtls_heap_addr = heap;
56 	mbedtls_heap_size = sizeof(heap);
57 
58 #elif defined(IMAGE_BL2)
59 
60 	int err;
61 
62 	/* If in BL2, retrieve the already allocated heap's info from DTB */
63 	if (tb_fw_cfg_dtb != NULL) {
64 		err = arm_get_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, heap_addr,
65 			heap_size);
66 		if (err < 0) {
67 			ERROR("BL2: unable to retrieve shared Mbed TLS heap information from DTB\n");
68 			panic();
69 		}
70 	} else {
71 		ERROR("BL2: DTB missing, cannot get Mbed TLS heap\n");
72 		panic();
73 	}
74 #endif
75 
76 	return 0;
77 }
78 
79 /*
80  * Puts the shared Mbed TLS heap information to the DTB.
81  * Executed only from BL1.
82  */
83 void arm_bl1_set_mbedtls_heap(void)
84 {
85 	int err;
86 
87 	/*
88 	 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
89 	 * platform. As such, we don't attempt to write to the DTB at all.
90 	 *
91 	 * If mbedtls_heap_addr==NULL, then it means we are using the default
92 	 * heap implementation. As such, BL2 will have its own heap for sure
93 	 * and hence there is no need to pass any information to the DTB.
94 	 *
95 	 * In the latter case, if we still wanted to write in the DTB the heap
96 	 * information, we would need to call plat_get_mbedtls_heap to retrieve
97 	 * the default heap's address and size.
98 	 */
99 	if ((tb_fw_cfg_dtb != NULL) && (mbedtls_heap_addr != NULL)) {
100 		err = arm_set_dtb_mbedtls_heap_info(tb_fw_cfg_dtb,
101 			mbedtls_heap_addr, mbedtls_heap_size);
102 		if (err < 0) {
103 			ERROR("BL1: unable to write shared Mbed TLS heap information to DTB\n");
104 			panic();
105 		}
106 		/*
107 		 * Ensure that the info written to the DTB is visible to other
108 		 * images. It's critical because BL2 won't be able to proceed
109 		 * without the heap info.
110 		 */
111 		flush_dcache_range((uintptr_t)tb_fw_cfg_dtb,
112 			fdt_totalsize(tb_fw_cfg_dtb));
113 	}
114 }
115 
116 #endif /* TRUSTED_BOARD_BOOT */
117 
118 /*
119  * BL2 utility function to set the address of TB_FW_CONFIG passed from BL1.
120  */
121 void arm_bl2_set_tb_cfg_addr(void *dtb)
122 {
123 	assert(dtb != NULL);
124 	tb_fw_cfg_dtb = dtb;
125 }
126 
127 /*
128  * BL2 utility function to initialize dynamic configuration specified by
129  * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
130  * specified in TB_FW_CONFIG.
131  */
132 void arm_bl2_dyn_cfg_init(void)
133 {
134 	int err = 0, tb_fw_node;
135 	unsigned int i;
136 	bl_mem_params_node_t *cfg_mem_params = NULL;
137 	uint64_t image_base;
138 	uint32_t image_size;
139 	const unsigned int config_ids[] = {
140 			HW_CONFIG_ID,
141 			SOC_FW_CONFIG_ID,
142 			NT_FW_CONFIG_ID,
143 #ifdef SPD_tspd
144 			/* Currently tos_fw_config is only present for TSP */
145 			TOS_FW_CONFIG_ID
146 #endif
147 	};
148 
149 	if (tb_fw_cfg_dtb == NULL) {
150 		VERBOSE("No TB_FW_CONFIG specified\n");
151 		return;
152 	}
153 
154 	err = arm_dyn_tb_fw_cfg_init(tb_fw_cfg_dtb, &tb_fw_node);
155 	if (err < 0) {
156 		ERROR("Invalid TB_FW_CONFIG passed from BL1\n");
157 		panic();
158 	}
159 
160 	/* Iterate through all the fw config IDs */
161 	for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
162 		/* Get the config load address and size from TB_FW_CONFIG */
163 		cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
164 		if (cfg_mem_params == NULL) {
165 			VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
166 			continue;
167 		}
168 
169 		err = arm_dyn_get_config_load_info(tb_fw_cfg_dtb, tb_fw_node,
170 				config_ids[i], &image_base, &image_size);
171 		if (err < 0) {
172 			VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n",
173 					config_ids[i]);
174 			continue;
175 		}
176 
177 		/*
178 		 * Do some runtime checks on the load addresses of soc_fw_config,
179 		 * tos_fw_config, nt_fw_config. This is not a comprehensive check
180 		 * of all invalid addresses but to prevent trivial porting errors.
181 		 */
182 		if (config_ids[i] != HW_CONFIG_ID) {
183 
184 			if (check_uptr_overflow(image_base, image_size))
185 				continue;
186 
187 #ifdef	BL31_BASE
188 			/* Ensure the configs don't overlap with BL31 */
189 			if ((image_base >= BL31_BASE) &&
190 			    (image_base <= BL31_LIMIT))
191 				continue;
192 #endif
193 			/* Ensure the configs are loaded in a valid address */
194 			if (image_base < ARM_BL_RAM_BASE)
195 				continue;
196 #ifdef BL32_BASE
197 			/*
198 			 * If BL32 is present, ensure that the configs don't
199 			 * overlap with it.
200 			 */
201 			if ((image_base >= BL32_BASE) &&
202 			    (image_base <= BL32_LIMIT))
203 				continue;
204 #endif
205 		}
206 
207 
208 		cfg_mem_params->image_info.image_base = (uintptr_t)image_base;
209 		cfg_mem_params->image_info.image_max_size = image_size;
210 
211 		/*
212 		 * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from
213 		 * HW_CONFIG or FW_CONFIG nodes
214 		 */
215 		cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
216 	}
217 
218 #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
219 	uint32_t disable_auth = 0;
220 
221 	err = arm_dyn_get_disable_auth(tb_fw_cfg_dtb, tb_fw_node,
222 					&disable_auth);
223 	if (err < 0)
224 		return;
225 
226 	if (disable_auth == 1)
227 		dyn_disable_auth();
228 #endif
229 }
230