xref: /rk3399_ARM-atf/plat/arm/common/arm_dyn_cfg.c (revision 1d71ba141d32c9e8974d4e3e973a90fd0c6bf458)
1c228956aSSoby Mathew /*
2c228956aSSoby Mathew  * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3c228956aSSoby Mathew  *
4c228956aSSoby Mathew  * SPDX-License-Identifier: BSD-3-Clause
5c228956aSSoby Mathew  */
6c228956aSSoby Mathew 
7cab0b5b0SSoby Mathew #include <arm_dyn_cfg_helpers.h>
8c228956aSSoby Mathew #include <assert.h>
9c228956aSSoby Mathew #include <debug.h>
10c228956aSSoby Mathew #include <desc_image_load.h>
11da5f2745SSoby Mathew #include <plat_arm.h>
12c228956aSSoby Mathew #include <platform.h>
13c228956aSSoby Mathew #include <platform_def.h>
14c228956aSSoby Mathew #include <string.h>
15c228956aSSoby Mathew #include <tbbr_img_def.h>
16c228956aSSoby Mathew 
17c228956aSSoby Mathew #if LOAD_IMAGE_V2
18c228956aSSoby Mathew 
19cab0b5b0SSoby Mathew /* Variable to store the address to TB_FW_CONFIG passed from BL1 */
20cab0b5b0SSoby Mathew static void *tb_fw_cfg_dtb;
21cab0b5b0SSoby Mathew 
22c228956aSSoby Mathew /*
23c228956aSSoby Mathew  * Helper function to load TB_FW_CONFIG and populate the load information to
24c228956aSSoby Mathew  * arg0 of BL2 entrypoint info.
25c228956aSSoby Mathew  */
26c228956aSSoby Mathew void arm_load_tb_fw_config(void)
27c228956aSSoby Mathew {
28c228956aSSoby Mathew 	int err;
29c228956aSSoby Mathew 	uintptr_t config_base = 0;
30c228956aSSoby Mathew 	image_desc_t *image_desc;
31c228956aSSoby Mathew 
32c228956aSSoby Mathew 	image_desc_t arm_tb_fw_info = {
33c228956aSSoby Mathew 		.image_id = TB_FW_CONFIG_ID,
34c228956aSSoby Mathew 		SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY,
35c228956aSSoby Mathew 				VERSION_2, image_info_t, 0),
36c228956aSSoby Mathew 		.image_info.image_base = ARM_TB_FW_CONFIG_BASE,
37c228956aSSoby Mathew 		.image_info.image_max_size = ARM_TB_FW_CONFIG_LIMIT - ARM_TB_FW_CONFIG_BASE,
38c228956aSSoby Mathew 	};
39c228956aSSoby Mathew 
40c228956aSSoby Mathew 	VERBOSE("BL1: Loading TB_FW_CONFIG\n");
41c228956aSSoby Mathew 	err = load_auth_image(TB_FW_CONFIG_ID, &arm_tb_fw_info.image_info);
42da5f2745SSoby Mathew 	if (err != 0) {
43c228956aSSoby Mathew 		/* Return if TB_FW_CONFIG is not loaded */
44c228956aSSoby Mathew 		VERBOSE("Failed to load TB_FW_CONFIG\n");
45c228956aSSoby Mathew 		return;
46c228956aSSoby Mathew 	}
47c228956aSSoby Mathew 
48c228956aSSoby Mathew 	config_base = arm_tb_fw_info.image_info.image_base;
49c228956aSSoby Mathew 
50c228956aSSoby Mathew 	/* The BL2 ep_info arg0 is modified to point to TB_FW_CONFIG */
51c228956aSSoby Mathew 	image_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
52da5f2745SSoby Mathew 	assert(image_desc != NULL);
53c228956aSSoby Mathew 	image_desc->ep_info.args.arg0 = config_base;
54c228956aSSoby Mathew 
55c228956aSSoby Mathew 	INFO("BL1: TB_FW_CONFIG loaded at address = %p\n",
56c228956aSSoby Mathew 			(void *) config_base);
576e79f9fdSSoby Mathew 
586e79f9fdSSoby Mathew #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
596e79f9fdSSoby Mathew 	int tb_fw_node;
606e79f9fdSSoby Mathew 	uint32_t disable_auth = 0;
616e79f9fdSSoby Mathew 
626e79f9fdSSoby Mathew 	err = arm_dyn_tb_fw_cfg_init((void *)config_base, &tb_fw_node);
636e79f9fdSSoby Mathew 	if (err < 0) {
646e79f9fdSSoby Mathew 		WARN("Invalid TB_FW_CONFIG loaded\n");
656e79f9fdSSoby Mathew 		return;
666e79f9fdSSoby Mathew 	}
676e79f9fdSSoby Mathew 
686e79f9fdSSoby Mathew 	err = arm_dyn_get_disable_auth((void *)config_base, tb_fw_node, &disable_auth);
696e79f9fdSSoby Mathew 	if (err < 0)
706e79f9fdSSoby Mathew 		return;
716e79f9fdSSoby Mathew 
726e79f9fdSSoby Mathew 	if (disable_auth == 1)
736e79f9fdSSoby Mathew 		dyn_disable_auth();
746e79f9fdSSoby Mathew #endif
75c228956aSSoby Mathew }
76c228956aSSoby Mathew 
77cab0b5b0SSoby Mathew /*
78cab0b5b0SSoby Mathew  * BL2 utility function to set the address of TB_FW_CONFIG passed from BL1.
79cab0b5b0SSoby Mathew  */
80cab0b5b0SSoby Mathew void arm_bl2_set_tb_cfg_addr(void *dtb)
81cab0b5b0SSoby Mathew {
82da5f2745SSoby Mathew 	assert(dtb != NULL);
83cab0b5b0SSoby Mathew 	tb_fw_cfg_dtb = dtb;
84cab0b5b0SSoby Mathew }
85cab0b5b0SSoby Mathew 
86cab0b5b0SSoby Mathew /*
87cab0b5b0SSoby Mathew  * BL2 utility function to initialize dynamic configuration specified by
88*1d71ba14SSoby Mathew  * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
89*1d71ba14SSoby Mathew  * specified in TB_FW_CONFIG.
90cab0b5b0SSoby Mathew  */
91cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void)
92cab0b5b0SSoby Mathew {
93*1d71ba14SSoby Mathew 	int err = 0, tb_fw_node;
94*1d71ba14SSoby Mathew 	unsigned int i;
95*1d71ba14SSoby Mathew 	bl_mem_params_node_t *cfg_mem_params = NULL;
96*1d71ba14SSoby Mathew 	uint64_t image_base;
97*1d71ba14SSoby Mathew 	uint32_t image_size;
98*1d71ba14SSoby Mathew 	const unsigned int config_ids[] = {
99*1d71ba14SSoby Mathew 			HW_CONFIG_ID,
100*1d71ba14SSoby Mathew 			SOC_FW_CONFIG_ID,
101*1d71ba14SSoby Mathew 			NT_FW_CONFIG_ID,
102*1d71ba14SSoby Mathew #ifdef SPD_tspd
103*1d71ba14SSoby Mathew 			/* Currently tos_fw_config is only present for TSP */
104*1d71ba14SSoby Mathew 			TOS_FW_CONFIG_ID
105*1d71ba14SSoby Mathew #endif
106*1d71ba14SSoby Mathew 	};
107cab0b5b0SSoby Mathew 
108cab0b5b0SSoby Mathew 	if (tb_fw_cfg_dtb == NULL) {
109cab0b5b0SSoby Mathew 		VERBOSE("No TB_FW_CONFIG specified\n");
110cab0b5b0SSoby Mathew 		return;
111cab0b5b0SSoby Mathew 	}
112cab0b5b0SSoby Mathew 
113cab0b5b0SSoby Mathew 	err = arm_dyn_tb_fw_cfg_init((void *)tb_fw_cfg_dtb, &tb_fw_node);
114cab0b5b0SSoby Mathew 	if (err < 0) {
115cab0b5b0SSoby Mathew 		ERROR("Invalid TB_FW_CONFIG passed from BL1\n");
116cab0b5b0SSoby Mathew 		panic();
117cab0b5b0SSoby Mathew 	}
118cab0b5b0SSoby Mathew 
119*1d71ba14SSoby Mathew 	/* Iterate through all the fw config IDs */
120*1d71ba14SSoby Mathew 	for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
121*1d71ba14SSoby Mathew 		/* Get the config load address and size from TB_FW_CONFIG */
122*1d71ba14SSoby Mathew 		cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
123*1d71ba14SSoby Mathew 		if (cfg_mem_params == NULL) {
124cab0b5b0SSoby Mathew 			VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
125*1d71ba14SSoby Mathew 			continue;
126cab0b5b0SSoby Mathew 		}
127cab0b5b0SSoby Mathew 
128*1d71ba14SSoby Mathew 		err = arm_dyn_get_config_load_info((void *)tb_fw_cfg_dtb, tb_fw_node,
129*1d71ba14SSoby Mathew 				config_ids[i], &image_base, &image_size);
130cab0b5b0SSoby Mathew 		if (err < 0) {
131*1d71ba14SSoby Mathew 			VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n",
132*1d71ba14SSoby Mathew 					config_ids[i]);
133*1d71ba14SSoby Mathew 			continue;
134cab0b5b0SSoby Mathew 		}
135cab0b5b0SSoby Mathew 
136*1d71ba14SSoby Mathew 		/*
137*1d71ba14SSoby Mathew 		 * Do some runtime checks on the load addresses of soc_fw_config,
138*1d71ba14SSoby Mathew 		 * tos_fw_config, nt_fw_config. This is not a comprehensive check
139*1d71ba14SSoby Mathew 		 * of all invalid addresses but to prevent trivial porting errors.
140*1d71ba14SSoby Mathew 		 */
141*1d71ba14SSoby Mathew 		if (config_ids[i] != HW_CONFIG_ID) {
142*1d71ba14SSoby Mathew 
143*1d71ba14SSoby Mathew 			if (check_uptr_overflow(image_base, image_size) != 0)
144*1d71ba14SSoby Mathew 				continue;
145*1d71ba14SSoby Mathew 
146*1d71ba14SSoby Mathew 			/* Ensure the configs don't overlap with BL2 */
147*1d71ba14SSoby Mathew 			if ((image_base > BL2_BASE) || ((image_base + image_size) > BL2_BASE))
148*1d71ba14SSoby Mathew 				continue;
149*1d71ba14SSoby Mathew 
150*1d71ba14SSoby Mathew 			/* Ensure the configs are loaded in a valid address */
151*1d71ba14SSoby Mathew 			if (image_base < ARM_BL_RAM_BASE)
152*1d71ba14SSoby Mathew 				continue;
153*1d71ba14SSoby Mathew #ifdef BL32_BASE
154*1d71ba14SSoby Mathew 			/*
155*1d71ba14SSoby Mathew 			 * If BL32 is present, ensure that the configs don't
156*1d71ba14SSoby Mathew 			 * overlap with it.
157*1d71ba14SSoby Mathew 			 */
158*1d71ba14SSoby Mathew 			if (image_base >= BL32_BASE && image_base <= BL32_LIMIT)
159*1d71ba14SSoby Mathew 				continue;
160*1d71ba14SSoby Mathew #endif
161*1d71ba14SSoby Mathew 		}
162*1d71ba14SSoby Mathew 
163*1d71ba14SSoby Mathew 
164*1d71ba14SSoby Mathew 		cfg_mem_params->image_info.image_base = (uintptr_t)image_base;
165*1d71ba14SSoby Mathew 		cfg_mem_params->image_info.image_max_size = image_size;
166*1d71ba14SSoby Mathew 
167cab0b5b0SSoby Mathew 		/* Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from HW_CONFIG node */
168*1d71ba14SSoby Mathew 		cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
169*1d71ba14SSoby Mathew 	}
1706e79f9fdSSoby Mathew 
1716e79f9fdSSoby Mathew #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
1726e79f9fdSSoby Mathew 	uint32_t disable_auth = 0;
1736e79f9fdSSoby Mathew 
1746e79f9fdSSoby Mathew 	err = arm_dyn_get_disable_auth((void *)tb_fw_cfg_dtb, tb_fw_node,
1756e79f9fdSSoby Mathew 					&disable_auth);
1766e79f9fdSSoby Mathew 	if (err < 0)
1776e79f9fdSSoby Mathew 		return;
1786e79f9fdSSoby Mathew 
1796e79f9fdSSoby Mathew 	if (disable_auth == 1)
1806e79f9fdSSoby Mathew 		dyn_disable_auth();
1816e79f9fdSSoby Mathew #endif
182cab0b5b0SSoby Mathew }
183cab0b5b0SSoby Mathew 
184c228956aSSoby Mathew #endif /* LOAD_IMAGE_V2 */
185