blob: ada02f8dd66945969a230251c4d4bd07a6f64931 [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
Yatharth Kochar3345a8d2016-09-12 16:08:41 +01007#include <assert.h>
Yatharth Kochar3345a8d2016-09-12 16:08:41 +01008
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009#include <arch_helpers.h>
10#include <common/bl_common.h>
11#include <common/desc_image_load.h>
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010012
13static bl_load_info_t bl_load_info;
14static bl_params_t next_bl_params;
15
16
17/*******************************************************************************
18 * This function flushes the data structures so that they are visible
19 * in memory for the next BL image.
20 ******************************************************************************/
21void flush_bl_params_desc(void)
22{
Dan Handley09c97c92017-04-18 14:46:23 +010023 flush_dcache_range((uintptr_t)bl_mem_params_desc_ptr,
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010024 sizeof(*bl_mem_params_desc_ptr) * bl_mem_params_desc_num);
Dan Handley09c97c92017-04-18 14:46:23 +010025
26 flush_dcache_range((uintptr_t)&next_bl_params,
27 sizeof(next_bl_params));
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010028}
29
30/*******************************************************************************
31 * This function returns the index for given image_id, within the
32 * image descriptor array provided by bl_image_info_descs_ptr, if the
33 * image is found else it returns -1.
34 ******************************************************************************/
35int get_bl_params_node_index(unsigned int image_id)
36{
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010037 unsigned int index;
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010038 assert(image_id != INVALID_IMAGE_ID);
39
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010040 for (index = 0U; index < bl_mem_params_desc_num; index++) {
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010041 if (bl_mem_params_desc_ptr[index].image_id == image_id)
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010042 return (int)index;
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010043 }
44
45 return -1;
46}
47
48/*******************************************************************************
49 * This function returns the pointer to `bl_mem_params_node_t` object for
50 * given image_id, within the image descriptor array provided by
51 * bl_mem_params_desc_ptr, if the image is found else it returns NULL.
52 ******************************************************************************/
53bl_mem_params_node_t *get_bl_mem_params_node(unsigned int image_id)
54{
55 int index;
56 assert(image_id != INVALID_IMAGE_ID);
57
58 index = get_bl_params_node_index(image_id);
59 if (index >= 0)
60 return &bl_mem_params_desc_ptr[index];
61 else
62 return NULL;
63}
64
65/*******************************************************************************
66 * This function creates the list of loadable images, by populating and
67 * linking each `bl_load_info_node_t` type node, using the internal array
68 * of image descriptor provided by bl_mem_params_desc_ptr. It also populates
69 * and returns `bl_load_info_t` type structure that contains head of the list
70 * of loadable images.
71 ******************************************************************************/
72bl_load_info_t *get_bl_load_info_from_mem_params_desc(void)
73{
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010074 unsigned int index = 0;
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010075
76 /* If there is no image to start with, return NULL */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010077 if (bl_mem_params_desc_num == 0U)
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010078 return NULL;
79
80 /* Assign initial data structures */
81 bl_load_info_node_t *bl_node_info =
82 &bl_mem_params_desc_ptr[index].load_node_mem;
83 bl_load_info.head = bl_node_info;
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010084 SET_PARAM_HEAD(&bl_load_info, PARAM_BL_LOAD_INFO, VERSION_2, 0U);
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010085
86 /* Go through the image descriptor array and create the list */
87 for (; index < bl_mem_params_desc_num; index++) {
88
89 /* Populate the image information */
90 bl_node_info->image_id = bl_mem_params_desc_ptr[index].image_id;
91 bl_node_info->image_info = &bl_mem_params_desc_ptr[index].image_info;
92
93 /* Link next image if present */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010094 if ((index + 1U) < bl_mem_params_desc_num) {
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010095 /* Get the memory and link the next node */
96 bl_node_info->next_load_info =
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +010097 &bl_mem_params_desc_ptr[index + 1U].load_node_mem;
Yatharth Kochar3345a8d2016-09-12 16:08:41 +010098 bl_node_info = bl_node_info->next_load_info;
99 }
100 }
101
102 return &bl_load_info;
103}
104
105/*******************************************************************************
106 * This function creates the list of executable images, by populating and
107 * linking each `bl_params_node_t` type node, using the internal array of
108 * image descriptor provided by bl_mem_params_desc_ptr. It also populates
109 * and returns `bl_params_t` type structure that contains head of the list
110 * of executable images.
111 ******************************************************************************/
112bl_params_t *get_next_bl_params_from_mem_params_desc(void)
113{
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100114 unsigned int count;
115 unsigned int img_id = 0U;
116 unsigned int link_index = 0U;
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100117 bl_params_node_t *bl_current_exec_node = NULL;
118 bl_params_node_t *bl_last_exec_node = NULL;
119 bl_mem_params_node_t *desc_ptr;
120
121 /* If there is no image to start with, return NULL */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100122 if (bl_mem_params_desc_num == 0U)
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100123 return NULL;
124
125 /* Get the list HEAD */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100126 for (count = 0U; count < bl_mem_params_desc_num; count++) {
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100127
128 desc_ptr = &bl_mem_params_desc_ptr[count];
129
130 if ((EP_GET_EXE(desc_ptr->ep_info.h.attr) == EXECUTABLE) &&
131 (EP_GET_FIRST_EXE(desc_ptr->ep_info.h.attr) == EP_FIRST_EXE)) {
132 next_bl_params.head = &desc_ptr->params_node_mem;
133 link_index = count;
134 break;
135 }
136 }
137
138 /* Make sure we have a HEAD node */
139 assert(next_bl_params.head != NULL);
140
141 /* Populate the HEAD information */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100142 SET_PARAM_HEAD(&next_bl_params, PARAM_BL_PARAMS, VERSION_2, 0U);
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100143
144 /*
145 * Go through the image descriptor array and create the list.
146 * This bounded loop is to make sure that we are not looping forever.
147 */
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100148 for (count = 0U; count < bl_mem_params_desc_num; count++) {
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100149
150 desc_ptr = &bl_mem_params_desc_ptr[link_index];
151
152 /* Make sure the image is executable */
153 assert(EP_GET_EXE(desc_ptr->ep_info.h.attr) == EXECUTABLE);
154
155 /* Get the memory for current node */
156 bl_current_exec_node = &desc_ptr->params_node_mem;
157
158 /* Populate the image information */
159 bl_current_exec_node->image_id = desc_ptr->image_id;
160 bl_current_exec_node->image_info = &desc_ptr->image_info;
161 bl_current_exec_node->ep_info = &desc_ptr->ep_info;
162
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100163 if (bl_last_exec_node != NULL) {
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100164 /* Assert if loop detected */
165 assert(bl_last_exec_node->next_params_info == NULL);
166
167 /* Link the previous node to the current one */
168 bl_last_exec_node->next_params_info = bl_current_exec_node;
169 }
170
171 /* Update the last node */
172 bl_last_exec_node = bl_current_exec_node;
173
174 /* If no next hand-off image then break out */
175 img_id = desc_ptr->next_handoff_image_id;
176 if (img_id == INVALID_IMAGE_ID)
177 break;
178
179 /* Get the index for the next hand-off image */
180 link_index = get_bl_params_node_index(img_id);
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100181 assert((link_index > 0U) &&
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100182 (link_index < bl_mem_params_desc_num));
183 }
184
185 /* Invalid image is expected to terminate the loop */
186 assert(img_id == INVALID_IMAGE_ID);
187
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100188 return &next_bl_params;
189}
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000190
191/*******************************************************************************
192 * This function populates the entry point information with the corresponding
193 * config file for all executable BL images described in bl_params.
194 ******************************************************************************/
195void populate_next_bl_params_config(bl_params_t *bl2_to_next_bl_params)
196{
197 bl_params_node_t *params_node;
198 unsigned int fw_config_id;
199 uintptr_t hw_config_base = 0, fw_config_base;
200 bl_mem_params_node_t *mem_params;
201
Soby Mathewcc364842018-02-21 01:16:39 +0000202 assert(bl2_to_next_bl_params != NULL);
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000203
204 /*
205 * Get the `bl_mem_params_node_t` corresponding to HW_CONFIG
206 * if available.
207 */
208 mem_params = get_bl_mem_params_node(HW_CONFIG_ID);
209 if (mem_params != NULL)
210 hw_config_base = mem_params->image_info.image_base;
211
212 for (params_node = bl2_to_next_bl_params->head; params_node != NULL;
213 params_node = params_node->next_params_info) {
214
215 fw_config_base = 0;
216
217 switch (params_node->image_id) {
218 case BL31_IMAGE_ID:
219 fw_config_id = SOC_FW_CONFIG_ID;
220 break;
221 case BL32_IMAGE_ID:
222 fw_config_id = TOS_FW_CONFIG_ID;
223 break;
224 case BL33_IMAGE_ID:
225 fw_config_id = NT_FW_CONFIG_ID;
226 break;
227 default:
228 fw_config_id = INVALID_IMAGE_ID;
229 break;
230 }
231
232 if (fw_config_id != INVALID_IMAGE_ID) {
233 mem_params = get_bl_mem_params_node(fw_config_id);
234 if (mem_params != NULL)
235 fw_config_base = mem_params->image_info.image_base;
236 }
237
238 /*
239 * Pass hw and tb_fw config addresses to next images. NOTE - for
240 * EL3 runtime images (BL31 for AArch64 and BL32 for AArch32),
Amit Daniel Kachhap4a3bc082018-03-02 18:47:55 +0530241 * arg0 is already used by generic code. Take care of not
242 * overwriting the previous initialisations.
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000243 */
244 if (params_node == bl2_to_next_bl_params->head) {
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100245 if (params_node->ep_info->args.arg1 == 0U)
Amit Daniel Kachhap4a3bc082018-03-02 18:47:55 +0530246 params_node->ep_info->args.arg1 =
247 fw_config_base;
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100248 if (params_node->ep_info->args.arg2 == 0U)
Amit Daniel Kachhap4a3bc082018-03-02 18:47:55 +0530249 params_node->ep_info->args.arg2 =
250 hw_config_base;
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000251 } else {
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100252 if (params_node->ep_info->args.arg0 == 0U)
Amit Daniel Kachhap4a3bc082018-03-02 18:47:55 +0530253 params_node->ep_info->args.arg0 =
254 fw_config_base;
Antonio Nino Diazf0b14cf2018-10-04 09:55:23 +0100255 if (params_node->ep_info->args.arg1 == 0U)
Amit Daniel Kachhap4a3bc082018-03-02 18:47:55 +0530256 params_node->ep_info->args.arg1 =
257 hw_config_base;
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000258 }
259 }
260}