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