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