xref: /rk3399_ARM-atf/common/desc_image_load.c (revision 52e486f6a6192bd18d36cdcbc35c59092eefc810)
1 /*
2  * Copyright (c) 2016-2025, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #include <plat/common/platform.h>
10 
11 #include <arch_helpers.h>
12 #include <common/bl_common.h>
13 #include <common/desc_image_load.h>
14 #include <common/tbbr/tbbr_img_def.h>
15 
16 static bl_load_info_t bl_load_info;
17 static bl_params_t next_bl_params;
18 
19 
20 /*******************************************************************************
21  * This function flushes the data structures so that they are visible
22  * in memory for the next BL image.
23  ******************************************************************************/
24 void flush_bl_params_desc(void)
25 {
26 	flush_bl_params_desc_args(bl_mem_params_desc_ptr,
27 		bl_mem_params_desc_num,
28 		&next_bl_params);
29 }
30 
31 /*******************************************************************************
32  * This function flushes the data structures specified as arguments so that they
33  * are visible in memory for the next BL image.
34  ******************************************************************************/
35 void flush_bl_params_desc_args(bl_mem_params_node_t *mem_params_desc_ptr,
36 	unsigned int mem_params_desc_num,
37 	bl_params_t *next_bl_params_ptr)
38 {
39 	assert(mem_params_desc_ptr != NULL);
40 	assert(mem_params_desc_num != 0U);
41 	assert(next_bl_params_ptr != NULL);
42 
43 	flush_dcache_range((uintptr_t)mem_params_desc_ptr,
44 		sizeof(*mem_params_desc_ptr) * mem_params_desc_num);
45 
46 	flush_dcache_range((uintptr_t)next_bl_params_ptr,
47 			sizeof(*next_bl_params_ptr));
48 }
49 
50 /*******************************************************************************
51  * This function returns the index for given image_id, within the
52  * image descriptor array provided by bl_image_info_descs_ptr, if the
53  * image is found else it returns -1.
54  ******************************************************************************/
55 int get_bl_params_node_index(unsigned int image_id)
56 {
57 	unsigned int index;
58 	assert(image_id != INVALID_IMAGE_ID);
59 
60 	for (index = 0U; index < bl_mem_params_desc_num; index++) {
61 		if (bl_mem_params_desc_ptr[index].image_id == image_id)
62 			return (int)index;
63 	}
64 
65 	return -1;
66 }
67 
68 /*******************************************************************************
69  * This function returns the pointer to `bl_mem_params_node_t` object for
70  * given image_id, within the image descriptor array provided by
71  * bl_mem_params_desc_ptr, if the image is found else it returns NULL.
72  ******************************************************************************/
73 bl_mem_params_node_t *get_bl_mem_params_node(unsigned int image_id)
74 {
75 	int index;
76 	assert(image_id != INVALID_IMAGE_ID);
77 
78 	index = get_bl_params_node_index(image_id);
79 	if (index >= 0)
80 		return &bl_mem_params_desc_ptr[index];
81 	else
82 		return NULL;
83 }
84 
85 /*******************************************************************************
86  * This function creates the list of loadable images, by populating and
87  * linking each `bl_load_info_node_t` type node, using the internal array
88  * of image descriptor provided by bl_mem_params_desc_ptr. It also populates
89  * and returns `bl_load_info_t` type structure that contains head of the list
90  * of loadable images.
91  ******************************************************************************/
92 bl_load_info_t *get_bl_load_info_from_mem_params_desc(void)
93 {
94 	unsigned int index = 0;
95 
96 	/* If there is no image to start with, return NULL */
97 	if (bl_mem_params_desc_num == 0U)
98 		return NULL;
99 
100 	/* Assign initial data structures */
101 	bl_load_info_node_t *bl_node_info =
102 		&bl_mem_params_desc_ptr[index].load_node_mem;
103 	bl_load_info.head = bl_node_info;
104 	SET_PARAM_HEAD(&bl_load_info, PARAM_BL_LOAD_INFO, VERSION_2, 0U);
105 
106 	/* Go through the image descriptor array and create the list */
107 	for (; index < bl_mem_params_desc_num; index++) {
108 
109 		/* Populate the image information */
110 		bl_node_info->image_id = bl_mem_params_desc_ptr[index].image_id;
111 		bl_node_info->image_info = &bl_mem_params_desc_ptr[index].image_info;
112 
113 		/* Link next image if present */
114 		if ((index + 1U) < bl_mem_params_desc_num) {
115 			/* Get the memory and link the next node */
116 			bl_node_info->next_load_info =
117 				&bl_mem_params_desc_ptr[index + 1U].load_node_mem;
118 			bl_node_info = bl_node_info->next_load_info;
119 		}
120 	}
121 
122 	return &bl_load_info;
123 }
124 
125 /*******************************************************************************
126  * This function creates the list of executable images, by populating and
127  * linking each `bl_params_node_t` type node, using the internal array of
128  * image descriptor provided by bl_mem_params_desc_ptr. It also populates
129  * and returns `bl_params_t` type structure that contains head of the list
130  * of executable images.
131  ******************************************************************************/
132 bl_params_t *get_next_bl_params_from_mem_params_desc(void)
133 {
134 	unsigned int count;
135 	unsigned int img_id = 0U;
136 	unsigned int link_index = 0U;
137 	bl_params_node_t *bl_current_exec_node = NULL;
138 	bl_params_node_t *bl_last_exec_node = NULL;
139 	bl_mem_params_node_t *desc_ptr;
140 
141 	/* If there is no image to start with, return NULL */
142 	if (bl_mem_params_desc_num == 0U)
143 		return NULL;
144 
145 	/* Get the list HEAD */
146 	for (count = 0U; count < bl_mem_params_desc_num; count++) {
147 
148 		desc_ptr = &bl_mem_params_desc_ptr[count];
149 
150 		if ((EP_GET_EXE(desc_ptr->ep_info.h.attr) == EXECUTABLE) &&
151 			(EP_GET_FIRST_EXE(desc_ptr->ep_info.h.attr) == EP_FIRST_EXE)) {
152 			next_bl_params.head = &desc_ptr->params_node_mem;
153 			link_index = count;
154 			break;
155 		}
156 	}
157 
158 	/* Make sure we have a HEAD node */
159 	assert(next_bl_params.head != NULL);
160 
161 	/* Populate the HEAD information */
162 	SET_PARAM_HEAD(&next_bl_params, PARAM_BL_PARAMS, VERSION_2, 0U);
163 
164 	/*
165 	 * Go through the image descriptor array and create the list.
166 	 * This bounded loop is to make sure that we are not looping forever.
167 	 */
168 	for (count = 0U; count < bl_mem_params_desc_num; count++) {
169 
170 		desc_ptr = &bl_mem_params_desc_ptr[link_index];
171 
172 		/* Make sure the image is executable */
173 		assert(EP_GET_EXE(desc_ptr->ep_info.h.attr) == EXECUTABLE);
174 
175 		/* Get the memory for current node */
176 		bl_current_exec_node = &desc_ptr->params_node_mem;
177 
178 		/* Populate the image information */
179 		bl_current_exec_node->image_id = desc_ptr->image_id;
180 		bl_current_exec_node->image_info = &desc_ptr->image_info;
181 		bl_current_exec_node->ep_info = &desc_ptr->ep_info;
182 
183 		if (bl_last_exec_node != NULL) {
184 			/* Assert if loop detected */
185 			assert(bl_last_exec_node->next_params_info == NULL);
186 
187 			/* Link the previous node to the current one */
188 			bl_last_exec_node->next_params_info = bl_current_exec_node;
189 		}
190 
191 		/* Update the last node */
192 		bl_last_exec_node = bl_current_exec_node;
193 
194 		/* If no next hand-off image then break out */
195 		img_id = desc_ptr->next_handoff_image_id;
196 		if (img_id == INVALID_IMAGE_ID)
197 			break;
198 
199 		/* Get the index for the next hand-off image */
200 		link_index = get_bl_params_node_index(img_id);
201 		assert((link_index > 0U) &&
202 			(link_index < bl_mem_params_desc_num));
203 	}
204 
205 	/* Invalid image is expected to terminate the loop */
206 	assert(img_id == INVALID_IMAGE_ID);
207 
208 	return &next_bl_params;
209 }
210 
211 /*******************************************************************************
212  * This function populates the entry point information with the corresponding
213  * config file for all executable BL images described in bl_params.
214  ******************************************************************************/
215 void populate_next_bl_params_config(bl_params_t *bl2_to_next_bl_params)
216 {
217 	/*
218 	 * With Firmware Handoff configuration data is shared dynamically, most of
219 	 * the *_CONFIG files will be deprecated. Avoid populating the entry point
220 	 * arguments with their information as they're discarded anyway.
221 	 */
222 #if !TRANSFER_LIST
223 	bl_params_node_t *params_node;
224 	unsigned int fw_config_id;
225 	uintptr_t fw_config_base;
226 	bl_mem_params_node_t *mem_params;
227 	uintptr_t hw_config_base = 0;
228 
229 	assert(bl2_to_next_bl_params != NULL);
230 
231 	/*
232 	 * Get the `bl_mem_params_node_t` corresponding to HW_CONFIG
233 	 * if available.
234 	 */
235 	mem_params = get_bl_mem_params_node(HW_CONFIG_ID);
236 
237 	if (mem_params != NULL)
238 		hw_config_base = mem_params->image_info.image_base;
239 
240 	for (params_node = bl2_to_next_bl_params->head; params_node != NULL;
241 			params_node = params_node->next_params_info) {
242 
243 		fw_config_base = 0;
244 
245 		switch (params_node->image_id) {
246 		case BL31_IMAGE_ID:
247 			fw_config_id = SOC_FW_CONFIG_ID;
248 			break;
249 		case BL32_IMAGE_ID:
250 		/*
251 		 * At the moment, OPTEE cannot accept a DTB in secure memory,
252 		 * so fall back and use NT_FW_CONFIG instead.
253 		 * This MUST be fixed as soon as OPTEE has support to
254 		 * receive DTBs in secure memory.
255 		 */
256 #ifndef SPD_opteed
257 			fw_config_id = TOS_FW_CONFIG_ID;
258 			break;
259 #endif
260 		case BL33_IMAGE_ID:
261 			fw_config_id = NT_FW_CONFIG_ID;
262 			break;
263 		default:
264 			fw_config_id = INVALID_IMAGE_ID;
265 			break;
266 		}
267 
268 		if (fw_config_id != INVALID_IMAGE_ID) {
269 			mem_params = get_bl_mem_params_node(fw_config_id);
270 			if (mem_params != NULL) {
271 				fw_config_base = mem_params->image_info.image_base;
272 			}
273 		}
274 
275 #ifdef SPD_opteed
276 		/*
277 		 * If SPD_opteed is enabled, arg[0,2] are populated by
278 		 * parse_optee_header(), which is called by
279 		 * arm_bl2_handle_post_image_load(). The meaning of the
280 		 * arguments are:
281 		 * arg0 <-- MODE_RW
282 		 * arg1 <-- Paged image base
283 		 * arg2 <-- Paged image size
284 		 */
285 		if (params_node->image_id == BL32_IMAGE_ID) {
286 			params_node->ep_info->args.arg3 = fw_config_base;
287 		} else
288 #endif
289 		{
290 			/*
291 			 * Pass hw and tb_fw config addresses to next images.
292 			 * NOTE - for EL3 runtime images (BL31 for AArch64
293 			 * and BL32 for AArch32), arg0 is already used by
294 			 * generic code. Take care of not overwriting the
295 			 * previous initialisations.
296 			 */
297 			if (params_node == bl2_to_next_bl_params->head) {
298 				if (params_node->ep_info->args.arg1 == 0U)
299 					params_node->ep_info->args.arg1 =
300 								fw_config_base;
301 				if (params_node->ep_info->args.arg2 == 0U)
302 					params_node->ep_info->args.arg2 =
303 								hw_config_base;
304 			}
305 #if USE_KERNEL_DT_CONVENTION
306 			else if (params_node->image_id == BL33_IMAGE_ID) {
307 				hw_config_base = plat_get_hw_dt_base();
308 
309 				if (params_node->ep_info->args.arg0 == 0U) {
310 					params_node->ep_info->args.arg0 =
311 						hw_config_base;
312 				}
313 			}
314 #endif /* USE_KERNEL_DT_CONVENTION */
315 			else {
316 				if (params_node->ep_info->args.arg0 == 0U) {
317 					params_node->ep_info->args.arg0 =
318 						fw_config_base;
319 				}
320 				if (params_node->ep_info->args.arg1 == 0U) {
321 					params_node->ep_info->args.arg1 =
322 						hw_config_base;
323 				}
324 			}
325 		}
326 	}
327 #endif /* !TRANSFER_LIST */
328 }
329 
330 /*******************************************************************************
331  * Helper to extract BL32/BL33 entry point info from arg0 passed to BL31, for
332  * platforms that are only interested in those. Platforms that need to extract
333  * more information can parse the structures themselves.
334  ******************************************************************************/
335 
336 void bl31_params_parse_helper(u_register_t param,
337 			      entry_point_info_t *bl32_ep_info_out,
338 			      entry_point_info_t *bl33_ep_info_out)
339 {
340 	bl_params_node_t *node;
341 	bl_params_t *v2 = (void *)(uintptr_t)param;
342 
343 #if !ERROR_DEPRECATED
344 	if (v2->h.version == PARAM_VERSION_1) {
345 		struct { /* Deprecated version 1 parameter structure. */
346 			param_header_t h;
347 			image_info_t *bl31_image_info;
348 			entry_point_info_t *bl32_ep_info;
349 			image_info_t *bl32_image_info;
350 			entry_point_info_t *bl33_ep_info;
351 			image_info_t *bl33_image_info;
352 		} *v1 = (void *)(uintptr_t)param;
353 		assert(v1->h.type == PARAM_BL31);
354 		if (bl32_ep_info_out != NULL)
355 			*bl32_ep_info_out = *v1->bl32_ep_info;
356 		if (bl33_ep_info_out != NULL)
357 			*bl33_ep_info_out = *v1->bl33_ep_info;
358 		return;
359 	}
360 #endif /* !ERROR_DEPRECATED */
361 
362 	assert(v2->h.version == PARAM_VERSION_2);
363 	assert(v2->h.type == PARAM_BL_PARAMS);
364 	for (node = v2->head; node != NULL; node = node->next_params_info) {
365 		if (node->image_id == BL32_IMAGE_ID)
366 			if (bl32_ep_info_out != NULL)
367 				*bl32_ep_info_out = *node->ep_info;
368 		if (node->image_id == BL33_IMAGE_ID)
369 			if (bl33_ep_info_out != NULL)
370 				*bl33_ep_info_out = *node->ep_info;
371 	}
372 }
373