xref: /rk3399_ARM-atf/plat/arm/common/arm_dyn_cfg.c (revision 7b4e1fbb8f5c2d0e18326223aa0a945d6e7c3a55)
1c228956aSSoby Mathew /*
20cb64d01SAchin Gupta  * 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>
180ab49645SAlexei Fedorov #if MEASURED_BOOT
190ab49645SAlexei Fedorov #include <drivers/auth/crypto_mod.h>
200ab49645SAlexei Fedorov #include <mbedtls/md.h>
210ab49645SAlexei Fedorov #endif
2209d40e0eSAntonio Nino Diaz #endif
2325ac8794SLouis Mayencourt #include <lib/fconf/fconf.h>
2425ac8794SLouis Mayencourt #include <lib/fconf/fconf_dyn_cfg_getter.h>
256c972317SLouis Mayencourt #include <lib/fconf/fconf_tbbr_getter.h>
26*7b4e1fbbSAlexei Fedorov 
27bd9344f6SAntonio Nino Diaz #include <plat/arm/common/arm_dyn_cfg_helpers.h>
28bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
2909d40e0eSAntonio Nino Diaz 
30ba597da7SJohn Tsichritzis #if TRUSTED_BOARD_BOOT
31ba597da7SJohn Tsichritzis 
32ba597da7SJohn Tsichritzis static void *mbedtls_heap_addr;
33ba597da7SJohn Tsichritzis static size_t mbedtls_heap_size;
34ba597da7SJohn Tsichritzis 
35ba597da7SJohn Tsichritzis /*
36ba597da7SJohn Tsichritzis  * This function is the implementation of the shared Mbed TLS heap between
37ba597da7SJohn Tsichritzis  * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1
38ba597da7SJohn Tsichritzis  * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file
39ba597da7SJohn Tsichritzis  * which is a DTB.
40ba597da7SJohn Tsichritzis  *
41ba597da7SJohn Tsichritzis  * This function is placed inside an #if directive for the below reasons:
42ba597da7SJohn Tsichritzis  *   - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot
43ba597da7SJohn Tsichritzis  *     is enabled.
44ba597da7SJohn Tsichritzis  *   - This implementation requires the DTB to be present so that BL1 has a
4560e19f57SAntonio Nino Diaz  *     mechanism to pass the pointer to BL2.
46ba597da7SJohn Tsichritzis  */
47ba597da7SJohn Tsichritzis int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
48ba597da7SJohn Tsichritzis {
49ba597da7SJohn Tsichritzis 	assert(heap_addr != NULL);
50ba597da7SJohn Tsichritzis 	assert(heap_size != NULL);
51ba597da7SJohn Tsichritzis 
52ba597da7SJohn Tsichritzis #if defined(IMAGE_BL1) || BL2_AT_EL3
53ba597da7SJohn Tsichritzis 
54ba597da7SJohn Tsichritzis 	/* If in BL1 or BL2_AT_EL3 define a heap */
55ba597da7SJohn Tsichritzis 	static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
56ba597da7SJohn Tsichritzis 
57ba597da7SJohn Tsichritzis 	*heap_addr = heap;
58ba597da7SJohn Tsichritzis 	*heap_size = sizeof(heap);
59ba597da7SJohn Tsichritzis 	mbedtls_heap_addr = heap;
60ba597da7SJohn Tsichritzis 	mbedtls_heap_size = sizeof(heap);
61ba597da7SJohn Tsichritzis 
62ba597da7SJohn Tsichritzis #elif defined(IMAGE_BL2)
63ba597da7SJohn Tsichritzis 
64ba597da7SJohn Tsichritzis 	/* If in BL2, retrieve the already allocated heap's info from DTB */
656c972317SLouis Mayencourt 	*heap_addr = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_addr);
666c972317SLouis Mayencourt 	*heap_size = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_size);
676c972317SLouis Mayencourt 
68ba597da7SJohn Tsichritzis #endif
69ba597da7SJohn Tsichritzis 
70ba597da7SJohn Tsichritzis 	return 0;
71ba597da7SJohn Tsichritzis }
72ba597da7SJohn Tsichritzis 
73ba597da7SJohn Tsichritzis /*
74ba597da7SJohn Tsichritzis  * Puts the shared Mbed TLS heap information to the DTB.
75ba597da7SJohn Tsichritzis  * Executed only from BL1.
76ba597da7SJohn Tsichritzis  */
77ba597da7SJohn Tsichritzis void arm_bl1_set_mbedtls_heap(void)
78ba597da7SJohn Tsichritzis {
79ba597da7SJohn Tsichritzis 	int err;
8025ac8794SLouis Mayencourt 	uintptr_t tb_fw_cfg_dtb;
81fe6fd3e4SManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
82ba597da7SJohn Tsichritzis 
83ba597da7SJohn Tsichritzis 	/*
84ba597da7SJohn Tsichritzis 	 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
85ba597da7SJohn Tsichritzis 	 * platform. As such, we don't attempt to write to the DTB at all.
86ba597da7SJohn Tsichritzis 	 *
87ba597da7SJohn Tsichritzis 	 * If mbedtls_heap_addr==NULL, then it means we are using the default
88ba597da7SJohn Tsichritzis 	 * heap implementation. As such, BL2 will have its own heap for sure
89ba597da7SJohn Tsichritzis 	 * and hence there is no need to pass any information to the DTB.
90ba597da7SJohn Tsichritzis 	 *
91ba597da7SJohn Tsichritzis 	 * In the latter case, if we still wanted to write in the DTB the heap
92ba597da7SJohn Tsichritzis 	 * information, we would need to call plat_get_mbedtls_heap to retrieve
93ba597da7SJohn Tsichritzis 	 * the default heap's address and size.
94ba597da7SJohn Tsichritzis 	 */
9525ac8794SLouis Mayencourt 
96fe6fd3e4SManish V Badarkhe 	tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
971d60052eSManish V Badarkhe 	assert(tb_fw_config_info != NULL);
981d60052eSManish V Badarkhe 
99fe6fd3e4SManish V Badarkhe 	tb_fw_cfg_dtb = tb_fw_config_info->config_addr;
10025ac8794SLouis Mayencourt 
10125ac8794SLouis Mayencourt 	if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) {
102*7b4e1fbbSAlexei Fedorov 		/* As libfdt uses void *, we can't avoid this cast */
10325ac8794SLouis Mayencourt 		void *dtb = (void *)tb_fw_cfg_dtb;
10425ac8794SLouis Mayencourt 
10525ac8794SLouis Mayencourt 		err = arm_set_dtb_mbedtls_heap_info(dtb,
106ba597da7SJohn Tsichritzis 			mbedtls_heap_addr, mbedtls_heap_size);
107ba597da7SJohn Tsichritzis 		if (err < 0) {
108*7b4e1fbbSAlexei Fedorov 			ERROR("%swrite shared Mbed TLS heap information%s",
109*7b4e1fbbSAlexei Fedorov 				"BL1: unable to ", " to DTB\n");
110ba597da7SJohn Tsichritzis 			panic();
111ba597da7SJohn Tsichritzis 		}
1120ab49645SAlexei Fedorov #if !MEASURED_BOOT
11363cc2658SJohn Tsichritzis 		/*
11463cc2658SJohn Tsichritzis 		 * Ensure that the info written to the DTB is visible to other
11563cc2658SJohn Tsichritzis 		 * images. It's critical because BL2 won't be able to proceed
11663cc2658SJohn Tsichritzis 		 * without the heap info.
1170ab49645SAlexei Fedorov 		 *
1180ab49645SAlexei Fedorov 		 * In MEASURED_BOOT case flushing is done in
1190ab49645SAlexei Fedorov 		 * arm_bl1_set_bl2_hash() function which is called after heap
1200ab49645SAlexei Fedorov 		 * information is written in the DTB.
12163cc2658SJohn Tsichritzis 		 */
12225ac8794SLouis Mayencourt 		flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize(dtb));
1230ab49645SAlexei Fedorov #endif /* !MEASURED_BOOT */
124ba597da7SJohn Tsichritzis 	}
125ba597da7SJohn Tsichritzis }
126ba597da7SJohn Tsichritzis 
1270ab49645SAlexei Fedorov #if MEASURED_BOOT
1280ab49645SAlexei Fedorov /*
129*7b4e1fbbSAlexei Fedorov  * Calculates and writes BL2 hash data to TB_FW_CONFIG DTB.
1300ab49645SAlexei Fedorov  * Executed only from BL1.
1310ab49645SAlexei Fedorov  */
132*7b4e1fbbSAlexei Fedorov void arm_bl1_set_bl2_hash(const image_desc_t *image_desc)
1330ab49645SAlexei Fedorov {
1340ab49645SAlexei Fedorov 	unsigned char hash_data[MBEDTLS_MD_MAX_SIZE];
135*7b4e1fbbSAlexei Fedorov 	const image_info_t image_info = image_desc->image_info;
1360ab49645SAlexei Fedorov 	uintptr_t tb_fw_cfg_dtb;
1370ab49645SAlexei Fedorov 	int err;
138fe6fd3e4SManish V Badarkhe 	const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
1390ab49645SAlexei Fedorov 
140fe6fd3e4SManish V Badarkhe 	tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
1411d60052eSManish V Badarkhe 	assert(tb_fw_config_info != NULL);
1421d60052eSManish V Badarkhe 
143fe6fd3e4SManish V Badarkhe 	tb_fw_cfg_dtb = tb_fw_config_info->config_addr;
1440ab49645SAlexei Fedorov 
1450ab49645SAlexei Fedorov 	/*
1460ab49645SAlexei Fedorov 	 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
1470ab49645SAlexei Fedorov 	 * platform. As such, we cannot write to the DTB at all and pass
1480ab49645SAlexei Fedorov 	 * measured data.
1490ab49645SAlexei Fedorov 	 */
1500ab49645SAlexei Fedorov 	if (tb_fw_cfg_dtb == 0UL) {
1510ab49645SAlexei Fedorov 		panic();
1520ab49645SAlexei Fedorov 	}
1530ab49645SAlexei Fedorov 
1540ab49645SAlexei Fedorov 	/* Calculate hash */
1550ab49645SAlexei Fedorov 	err = crypto_mod_calc_hash(MBEDTLS_MD_ID,
1560ab49645SAlexei Fedorov 					(void *)image_info.image_base,
1570ab49645SAlexei Fedorov 					image_info.image_size, hash_data);
1580ab49645SAlexei Fedorov 	if (err != 0) {
159*7b4e1fbbSAlexei Fedorov 		ERROR("%scalculate%s\n", "BL1: unable to ",
160*7b4e1fbbSAlexei Fedorov 						" BL2 hash");
1610ab49645SAlexei Fedorov 		panic();
1620ab49645SAlexei Fedorov 	}
1630ab49645SAlexei Fedorov 
1640ab49645SAlexei Fedorov 	err = arm_set_bl2_hash_info((void *)tb_fw_cfg_dtb, hash_data);
1650ab49645SAlexei Fedorov 	if (err < 0) {
166*7b4e1fbbSAlexei Fedorov 		ERROR("%swrite%sdata%s\n", "BL1: unable to ",
167*7b4e1fbbSAlexei Fedorov 					" BL2 hash ", "to DTB\n");
1680ab49645SAlexei Fedorov 		panic();
1690ab49645SAlexei Fedorov 	}
1700ab49645SAlexei Fedorov 
1710ab49645SAlexei Fedorov 	/*
1720ab49645SAlexei Fedorov 	 * Ensure that the info written to the DTB is visible to other
1730ab49645SAlexei Fedorov 	 * images. It's critical because BL2 won't be able to proceed
1740ab49645SAlexei Fedorov 	 * without the heap info and its hash data.
1750ab49645SAlexei Fedorov 	 */
1760ab49645SAlexei Fedorov 	flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize((void *)tb_fw_cfg_dtb));
1770ab49645SAlexei Fedorov }
178*7b4e1fbbSAlexei Fedorov 
179*7b4e1fbbSAlexei Fedorov /*
180*7b4e1fbbSAlexei Fedorov  * Reads TCG_DIGEST_SIZE bytes of BL2 hash data from the DTB.
181*7b4e1fbbSAlexei Fedorov  * Executed only from BL2.
182*7b4e1fbbSAlexei Fedorov  */
183*7b4e1fbbSAlexei Fedorov void arm_bl2_get_hash(void *data)
184*7b4e1fbbSAlexei Fedorov {
185*7b4e1fbbSAlexei Fedorov 	const void *bl2_hash;
186*7b4e1fbbSAlexei Fedorov 
187*7b4e1fbbSAlexei Fedorov 	assert(data != NULL);
188*7b4e1fbbSAlexei Fedorov 
189*7b4e1fbbSAlexei Fedorov 	/* Retrieve TCG_DIGEST_SIZE bytes of BL2 hash data from the DTB */
190*7b4e1fbbSAlexei Fedorov 	bl2_hash = FCONF_GET_PROPERTY(tbbr, dyn_config, bl2_hash_data);
191*7b4e1fbbSAlexei Fedorov 	(void)memcpy(data, bl2_hash, TCG_DIGEST_SIZE);
192*7b4e1fbbSAlexei Fedorov }
1930ab49645SAlexei Fedorov #endif /* MEASURED_BOOT */
194ba597da7SJohn Tsichritzis #endif /* TRUSTED_BOARD_BOOT */
195ba597da7SJohn Tsichritzis 
196c228956aSSoby Mathew /*
197cab0b5b0SSoby Mathew  * BL2 utility function to initialize dynamic configuration specified by
19804e06973SManish V Badarkhe  * FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
19904e06973SManish V Badarkhe  * specified in FW_CONFIG.
200cab0b5b0SSoby Mathew  */
201cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void)
202cab0b5b0SSoby Mathew {
2031d71ba14SSoby Mathew 	unsigned int i;
2041d71ba14SSoby Mathew 	bl_mem_params_node_t *cfg_mem_params = NULL;
20525ac8794SLouis Mayencourt 	uintptr_t image_base;
20625ac8794SLouis Mayencourt 	size_t image_size;
2071d71ba14SSoby Mathew 	const unsigned int config_ids[] = {
2081d71ba14SSoby Mathew 			HW_CONFIG_ID,
2091d71ba14SSoby Mathew 			SOC_FW_CONFIG_ID,
2101d71ba14SSoby Mathew 			NT_FW_CONFIG_ID,
2110cb64d01SAchin Gupta #if defined(SPD_tspd) || defined(SPD_spmd)
2120cb64d01SAchin Gupta 			/* tos_fw_config is only present for TSPD/SPMD */
2131d71ba14SSoby Mathew 			TOS_FW_CONFIG_ID
2141d71ba14SSoby Mathew #endif
2151d71ba14SSoby Mathew 	};
216cab0b5b0SSoby Mathew 
21725ac8794SLouis Mayencourt 	const struct dyn_cfg_dtb_info_t *dtb_info;
218cab0b5b0SSoby Mathew 
2191d71ba14SSoby Mathew 	/* Iterate through all the fw config IDs */
2201d71ba14SSoby Mathew 	for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
2211d71ba14SSoby Mathew 		/* Get the config load address and size from TB_FW_CONFIG */
2221d71ba14SSoby Mathew 		cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
2231d71ba14SSoby Mathew 		if (cfg_mem_params == NULL) {
224*7b4e1fbbSAlexei Fedorov 			VERBOSE("%sHW_CONFIG in bl_mem_params_node\n",
225*7b4e1fbbSAlexei Fedorov 				"Couldn't find ");
2261d71ba14SSoby Mathew 			continue;
227cab0b5b0SSoby Mathew 		}
228cab0b5b0SSoby Mathew 
22925ac8794SLouis Mayencourt 		dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]);
23025ac8794SLouis Mayencourt 		if (dtb_info == NULL) {
231*7b4e1fbbSAlexei Fedorov 			VERBOSE("%sconfig_id %d load info in TB_FW_CONFIG\n",
232*7b4e1fbbSAlexei Fedorov 				"Couldn't find ", config_ids[i]);
2331d71ba14SSoby Mathew 			continue;
234cab0b5b0SSoby Mathew 		}
235cab0b5b0SSoby Mathew 
23625ac8794SLouis Mayencourt 		image_base = dtb_info->config_addr;
23725ac8794SLouis Mayencourt 		image_size = dtb_info->config_max_size;
23825ac8794SLouis Mayencourt 
2391d71ba14SSoby Mathew 		/*
2401d71ba14SSoby Mathew 		 * Do some runtime checks on the load addresses of soc_fw_config,
2411d71ba14SSoby Mathew 		 * tos_fw_config, nt_fw_config. This is not a comprehensive check
2421d71ba14SSoby Mathew 		 * of all invalid addresses but to prevent trivial porting errors.
2431d71ba14SSoby Mathew 		 */
2441d71ba14SSoby Mathew 		if (config_ids[i] != HW_CONFIG_ID) {
2451d71ba14SSoby Mathew 
246*7b4e1fbbSAlexei Fedorov 			if (check_uptr_overflow(image_base, image_size)) {
2471d71ba14SSoby Mathew 				continue;
248*7b4e1fbbSAlexei Fedorov 			}
2496393c787SUsama Arif #ifdef	BL31_BASE
250c099cd39SSoby Mathew 			/* Ensure the configs don't overlap with BL31 */
2515ddcbdd8SAlexei Fedorov 			if ((image_base >= BL31_BASE) &&
252*7b4e1fbbSAlexei Fedorov 			    (image_base <= BL31_LIMIT)) {
2531d71ba14SSoby Mathew 				continue;
254*7b4e1fbbSAlexei Fedorov 			}
2556393c787SUsama Arif #endif
2561d71ba14SSoby Mathew 			/* Ensure the configs are loaded in a valid address */
257*7b4e1fbbSAlexei Fedorov 			if (image_base < ARM_BL_RAM_BASE) {
2581d71ba14SSoby Mathew 				continue;
259*7b4e1fbbSAlexei Fedorov 			}
2601d71ba14SSoby Mathew #ifdef BL32_BASE
2611d71ba14SSoby Mathew 			/*
2621d71ba14SSoby Mathew 			 * If BL32 is present, ensure that the configs don't
2631d71ba14SSoby Mathew 			 * overlap with it.
2641d71ba14SSoby Mathew 			 */
2655ddcbdd8SAlexei Fedorov 			if ((image_base >= BL32_BASE) &&
266*7b4e1fbbSAlexei Fedorov 			    (image_base <= BL32_LIMIT)) {
2671d71ba14SSoby Mathew 				continue;
268*7b4e1fbbSAlexei Fedorov 			}
2691d71ba14SSoby Mathew #endif
2701d71ba14SSoby Mathew 		}
2711d71ba14SSoby Mathew 
27225ac8794SLouis Mayencourt 		cfg_mem_params->image_info.image_base = image_base;
27325ac8794SLouis Mayencourt 		cfg_mem_params->image_info.image_max_size = (uint32_t)image_size;
2741d71ba14SSoby Mathew 
2755ddcbdd8SAlexei Fedorov 		/*
2765ddcbdd8SAlexei Fedorov 		 * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from
2775ddcbdd8SAlexei Fedorov 		 * HW_CONFIG or FW_CONFIG nodes
2785ddcbdd8SAlexei Fedorov 		 */
2791d71ba14SSoby Mathew 		cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
2801d71ba14SSoby Mathew 	}
281cab0b5b0SSoby Mathew }
282