blob: b07fba3340b26fb7323648db4a6da33f5cb78e43 [file] [log] [blame]
Yatharth Kochar3345a8d2016-09-12 16:08:41 +01001/*
Roberto Vargas484f6dd2018-02-12 12:36:17 +00002 * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
Yatharth Kochar3345a8d2016-09-12 16:08:41 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Yatharth Kochar3345a8d2016-09-12 16:08:41 +01005 */
6
7#include <arch_helpers.h>
8#include <assert.h>
9#include <bl_common.h>
10#include <desc_image_load.h>
11
12
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010013
14static bl_load_info_t bl_load_info;
15static bl_params_t next_bl_params;
16
17
18/*******************************************************************************
19 * This function flushes the data structures so that they are visible
20 * in memory for the next BL image.
21 ******************************************************************************/
22void flush_bl_params_desc(void)
23{
Dan Handley09c97c92017-04-18 14:46:23 +010024 flush_dcache_range((uintptr_t)bl_mem_params_desc_ptr,
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010025 sizeof(*bl_mem_params_desc_ptr) * bl_mem_params_desc_num);
Dan Handley09c97c92017-04-18 14:46:23 +010026
27 flush_dcache_range((uintptr_t)&next_bl_params,
28 sizeof(next_bl_params));
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010029}
30
31/*******************************************************************************
32 * This function returns the index for given image_id, within the
33 * image descriptor array provided by bl_image_info_descs_ptr, if the
34 * image is found else it returns -1.
35 ******************************************************************************/
36int get_bl_params_node_index(unsigned int image_id)
37{
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010038 unsigned int index;
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010039 assert(image_id != INVALID_IMAGE_ID);
40
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010041 for (index = 0U; index < bl_mem_params_desc_num; index++) {
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010042 if (bl_mem_params_desc_ptr[index].image_id == image_id)
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010043 return (int)index;
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010044 }
45
46 return -1;
47}
48
49/*******************************************************************************
50 * This function returns the pointer to `bl_mem_params_node_t` object for
51 * given image_id, within the image descriptor array provided by
52 * bl_mem_params_desc_ptr, if the image is found else it returns NULL.
53 ******************************************************************************/
54bl_mem_params_node_t *get_bl_mem_params_node(unsigned int image_id)
55{
56 int index;
57 assert(image_id != INVALID_IMAGE_ID);
58
59 index = get_bl_params_node_index(image_id);
60 if (index >= 0)
61 return &bl_mem_params_desc_ptr[index];
62 else
63 return NULL;
64}
65
66/*******************************************************************************
67 * This function creates the list of loadable images, by populating and
68 * linking each `bl_load_info_node_t` type node, using the internal array
69 * of image descriptor provided by bl_mem_params_desc_ptr. It also populates
70 * and returns `bl_load_info_t` type structure that contains head of the list
71 * of loadable images.
72 ******************************************************************************/
73bl_load_info_t *get_bl_load_info_from_mem_params_desc(void)
74{
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010075 unsigned int index = 0;
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010076
77 /* If there is no image to start with, return NULL */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010078 if (bl_mem_params_desc_num == 0U)
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010079 return NULL;
80
81 /* Assign initial data structures */
82 bl_load_info_node_t *bl_node_info =
83 &bl_mem_params_desc_ptr[index].load_node_mem;
84 bl_load_info.head = bl_node_info;
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010085 SET_PARAM_HEAD(&bl_load_info, PARAM_BL_LOAD_INFO, VERSION_2, 0U);
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010086
87 /* Go through the image descriptor array and create the list */
88 for (; index < bl_mem_params_desc_num; index++) {
89
90 /* Populate the image information */
91 bl_node_info->image_id = bl_mem_params_desc_ptr[index].image_id;
92 bl_node_info->image_info = &bl_mem_params_desc_ptr[index].image_info;
93
94 /* Link next image if present */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010095 if ((index + 1U) < bl_mem_params_desc_num) {
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010096 /* Get the memory and link the next node */
97 bl_node_info->next_load_info =
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010098 &bl_mem_params_desc_ptr[index + 1U].load_node_mem;
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010099 bl_node_info = bl_node_info->next_load_info;
100 }
101 }
102
103 return &bl_load_info;
104}
105
106/*******************************************************************************
107 * This function creates the list of executable images, by populating and
108 * linking each `bl_params_node_t` type node, using the internal array of
109 * image descriptor provided by bl_mem_params_desc_ptr. It also populates
110 * and returns `bl_params_t` type structure that contains head of the list
111 * of executable images.
112 ******************************************************************************/
113bl_params_t *get_next_bl_params_from_mem_params_desc(void)
114{
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100115 unsigned int count;
116 unsigned int img_id = 0U;
117 unsigned int link_index = 0U;
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100118 bl_params_node_t *bl_current_exec_node = NULL;
119 bl_params_node_t *bl_last_exec_node = NULL;
120 bl_mem_params_node_t *desc_ptr;
121
122 /* If there is no image to start with, return NULL */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100123 if (bl_mem_params_desc_num == 0U)
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100124 return NULL;
125
126 /* Get the list HEAD */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100127 for (count = 0U; count < bl_mem_params_desc_num; count++) {
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100128
129 desc_ptr = &bl_mem_params_desc_ptr[count];
130
131 if ((EP_GET_EXE(desc_ptr->ep_info.h.attr) == EXECUTABLE) &&
132 (EP_GET_FIRST_EXE(desc_ptr->ep_info.h.attr) == EP_FIRST_EXE)) {
133 next_bl_params.head = &desc_ptr->params_node_mem;
134 link_index = count;
135 break;
136 }
137 }
138
139 /* Make sure we have a HEAD node */
140 assert(next_bl_params.head != NULL);
141
142 /* Populate the HEAD information */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100143 SET_PARAM_HEAD(&next_bl_params, PARAM_BL_PARAMS, VERSION_2, 0U);
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100144
145 /*
146 * Go through the image descriptor array and create the list.
147 * This bounded loop is to make sure that we are not looping forever.
148 */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100149 for (count = 0U; count < bl_mem_params_desc_num; count++) {
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100150
151 desc_ptr = &bl_mem_params_desc_ptr[link_index];
152
153 /* Make sure the image is executable */
154 assert(EP_GET_EXE(desc_ptr->ep_info.h.attr) == EXECUTABLE);
155
156 /* Get the memory for current node */
157 bl_current_exec_node = &desc_ptr->params_node_mem;
158
159 /* Populate the image information */
160 bl_current_exec_node->image_id = desc_ptr->image_id;
161 bl_current_exec_node->image_info = &desc_ptr->image_info;
162 bl_current_exec_node->ep_info = &desc_ptr->ep_info;
163
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100164 if (bl_last_exec_node != NULL) {
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100165 /* Assert if loop detected */
166 assert(bl_last_exec_node->next_params_info == NULL);
167
168 /* Link the previous node to the current one */
169 bl_last_exec_node->next_params_info = bl_current_exec_node;
170 }
171
172 /* Update the last node */
173 bl_last_exec_node = bl_current_exec_node;
174
175 /* If no next hand-off image then break out */
176 img_id = desc_ptr->next_handoff_image_id;
177 if (img_id == INVALID_IMAGE_ID)
178 break;
179
180 /* Get the index for the next hand-off image */
181 link_index = get_bl_params_node_index(img_id);
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100182 assert((link_index > 0U) &&
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100183 (link_index < bl_mem_params_desc_num));
184 }
185
186 /* Invalid image is expected to terminate the loop */
187 assert(img_id == INVALID_IMAGE_ID);
188
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100189 return &next_bl_params;
190}
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000191
192/*******************************************************************************
193 * This function populates the entry point information with the corresponding
194 * config file for all executable BL images described in bl_params.
195 ******************************************************************************/
196void populate_next_bl_params_config(bl_params_t *bl2_to_next_bl_params)
197{
198 bl_params_node_t *params_node;
199 unsigned int fw_config_id;
200 uintptr_t hw_config_base = 0, fw_config_base;
201 bl_mem_params_node_t *mem_params;
202
Soby Mathewcc364842018-02-21 01:16:39 +0000203 assert(bl2_to_next_bl_params != NULL);
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000204
205 /*
206 * Get the `bl_mem_params_node_t` corresponding to HW_CONFIG
207 * if available.
208 */
209 mem_params = get_bl_mem_params_node(HW_CONFIG_ID);
210 if (mem_params != NULL)
211 hw_config_base = mem_params->image_info.image_base;
212
213 for (params_node = bl2_to_next_bl_params->head; params_node != NULL;
214 params_node = params_node->next_params_info) {
215
216 fw_config_base = 0;
217
218 switch (params_node->image_id) {
219 case BL31_IMAGE_ID:
220 fw_config_id = SOC_FW_CONFIG_ID;
221 break;
222 case BL32_IMAGE_ID:
223 fw_config_id = TOS_FW_CONFIG_ID;
224 break;
225 case BL33_IMAGE_ID:
226 fw_config_id = NT_FW_CONFIG_ID;
227 break;
228 default:
229 fw_config_id = INVALID_IMAGE_ID;
230 break;
231 }
232
233 if (fw_config_id != INVALID_IMAGE_ID) {
234 mem_params = get_bl_mem_params_node(fw_config_id);
235 if (mem_params != NULL)
236 fw_config_base = mem_params->image_info.image_base;
237 }
238
239 /*
240 * Pass hw and tb_fw config addresses to next images. NOTE - for
241 * EL3 runtime images (BL31 for AArch64 and BL32 for AArch32),
Amit Daniel Kachhap4a3bc082018-03-02 18:47:55 +0530242 * arg0 is already used by generic code. Take care of not
243 * overwriting the previous initialisations.
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000244 */
245 if (params_node == bl2_to_next_bl_params->head) {
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100246 if (params_node->ep_info->args.arg1 == 0U)
Amit Daniel Kachhap4a3bc082018-03-02 18:47:55 +0530247 params_node->ep_info->args.arg1 =
248 fw_config_base;
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100249 if (params_node->ep_info->args.arg2 == 0U)
Amit Daniel Kachhap4a3bc082018-03-02 18:47:55 +0530250 params_node->ep_info->args.arg2 =
251 hw_config_base;
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000252 } else {
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100253 if (params_node->ep_info->args.arg0 == 0U)
Amit Daniel Kachhap4a3bc082018-03-02 18:47:55 +0530254 params_node->ep_info->args.arg0 =
255 fw_config_base;
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100256 if (params_node->ep_info->args.arg1 == 0U)
Amit Daniel Kachhap4a3bc082018-03-02 18:47:55 +0530257 params_node->ep_info->args.arg1 =
258 hw_config_base;
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000259 }
260 }
261}