xref: /rk3399_ARM-atf/bl2/bl2_main.c (revision 6331a31a66cdcf53421d3dccd3067f072c6da175)
1 /*
2  * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <arch.h>
32 #include <arch_helpers.h>
33 #include <assert.h>
34 #include <auth_mod.h>
35 #include <bl1.h>
36 #include <bl_common.h>
37 #include <debug.h>
38 #include <errno.h>
39 #include <platform.h>
40 #include <platform_def.h>
41 #include <stdint.h>
42 #include "bl2_private.h"
43 
44 /*
45  * Check for platforms that use obsolete image terminology
46  */
47 #ifdef BL30_BASE
48 # error "BL30_BASE platform define no longer used - please use SCP_BL2_BASE"
49 #endif
50 
51 /*******************************************************************************
52  * Load the SCP_BL2 image if there's one.
53  * If a platform does not want to attempt to load SCP_BL2 image it must leave
54  * SCP_BL2_BASE undefined.
55  * Return 0 on success or if there's no SCP_BL2 image to load, a negative error
56  * code otherwise.
57  ******************************************************************************/
58 static int load_scp_bl2(void)
59 {
60 	int e = 0;
61 #ifdef SCP_BL2_BASE
62 	meminfo_t scp_bl2_mem_info;
63 	image_info_t scp_bl2_image_info;
64 
65 	/*
66 	 * It is up to the platform to specify where SCP_BL2 should be loaded if
67 	 * it exists. It could create space in the secure sram or point to a
68 	 * completely different memory.
69 	 *
70 	 * The entry point information is not relevant in this case as the AP
71 	 * won't execute the SCP_BL2 image.
72 	 */
73 	INFO("BL2: Loading SCP_BL2\n");
74 	bl2_plat_get_scp_bl2_meminfo(&scp_bl2_mem_info);
75 	scp_bl2_image_info.h.version = VERSION_1;
76 	e = load_auth_image(&scp_bl2_mem_info,
77 			    SCP_BL2_IMAGE_ID,
78 			    SCP_BL2_BASE,
79 			    &scp_bl2_image_info,
80 			    NULL);
81 
82 	if (e == 0) {
83 		/* The subsequent handling of SCP_BL2 is platform specific */
84 		e = bl2_plat_handle_scp_bl2(&scp_bl2_image_info);
85 		if (e) {
86 			ERROR("Failure in platform-specific handling of SCP_BL2 image.\n");
87 		}
88 	}
89 #endif /* SCP_BL2_BASE */
90 
91 	return e;
92 }
93 
94 #ifndef EL3_PAYLOAD_BASE
95 /*******************************************************************************
96  * Load the BL31 image.
97  * The bl2_to_bl31_params and bl31_ep_info params will be updated with the
98  * relevant BL31 information.
99  * Return 0 on success, a negative error code otherwise.
100  ******************************************************************************/
101 static int load_bl31(bl31_params_t *bl2_to_bl31_params,
102 		     entry_point_info_t *bl31_ep_info)
103 {
104 	meminfo_t *bl2_tzram_layout;
105 	int e;
106 
107 	INFO("BL2: Loading BL31\n");
108 	assert(bl2_to_bl31_params != NULL);
109 	assert(bl31_ep_info != NULL);
110 
111 	/* Find out how much free trusted ram remains after BL2 load */
112 	bl2_tzram_layout = bl2_plat_sec_mem_layout();
113 
114 	/* Set the X0 parameter to BL31 */
115 	bl31_ep_info->args.arg0 = (unsigned long)bl2_to_bl31_params;
116 
117 	/* Load the BL31 image */
118 	e = load_auth_image(bl2_tzram_layout,
119 			    BL31_IMAGE_ID,
120 			    BL31_BASE,
121 			    bl2_to_bl31_params->bl31_image_info,
122 			    bl31_ep_info);
123 
124 	if (e == 0) {
125 		bl2_plat_set_bl31_ep_info(bl2_to_bl31_params->bl31_image_info,
126 					  bl31_ep_info);
127 	}
128 
129 	return e;
130 }
131 
132 /*******************************************************************************
133  * Load the BL32 image if there's one.
134  * The bl2_to_bl31_params param will be updated with the relevant BL32
135  * information.
136  * If a platform does not want to attempt to load BL32 image it must leave
137  * BL32_BASE undefined.
138  * Return 0 on success or if there's no BL32 image to load, a negative error
139  * code otherwise.
140  ******************************************************************************/
141 static int load_bl32(bl31_params_t *bl2_to_bl31_params)
142 {
143 	int e = 0;
144 #ifdef BL32_BASE
145 	meminfo_t bl32_mem_info;
146 
147 	INFO("BL2: Loading BL32\n");
148 	assert(bl2_to_bl31_params != NULL);
149 
150 	/*
151 	 * It is up to the platform to specify where BL32 should be loaded if
152 	 * it exists. It could create space in the secure sram or point to a
153 	 * completely different memory.
154 	 */
155 	bl2_plat_get_bl32_meminfo(&bl32_mem_info);
156 	e = load_auth_image(&bl32_mem_info,
157 			    BL32_IMAGE_ID,
158 			    BL32_BASE,
159 			    bl2_to_bl31_params->bl32_image_info,
160 			    bl2_to_bl31_params->bl32_ep_info);
161 
162 	if (e == 0) {
163 		bl2_plat_set_bl32_ep_info(
164 			bl2_to_bl31_params->bl32_image_info,
165 			bl2_to_bl31_params->bl32_ep_info);
166 	}
167 #endif /* BL32_BASE */
168 
169 	return e;
170 }
171 
172 #ifndef PRELOADED_BL33_BASE
173 /*******************************************************************************
174  * Load the BL33 image.
175  * The bl2_to_bl31_params param will be updated with the relevant BL33
176  * information.
177  * Return 0 on success, a negative error code otherwise.
178  ******************************************************************************/
179 static int load_bl33(bl31_params_t *bl2_to_bl31_params)
180 {
181 	meminfo_t bl33_mem_info;
182 	int e;
183 
184 	INFO("BL2: Loading BL33\n");
185 	assert(bl2_to_bl31_params != NULL);
186 
187 	bl2_plat_get_bl33_meminfo(&bl33_mem_info);
188 
189 	/* Load the BL33 image in non-secure memory provided by the platform */
190 	e = load_auth_image(&bl33_mem_info,
191 			    BL33_IMAGE_ID,
192 			    plat_get_ns_image_entrypoint(),
193 			    bl2_to_bl31_params->bl33_image_info,
194 			    bl2_to_bl31_params->bl33_ep_info);
195 
196 	if (e == 0) {
197 		bl2_plat_set_bl33_ep_info(bl2_to_bl31_params->bl33_image_info,
198 					  bl2_to_bl31_params->bl33_ep_info);
199 	}
200 
201 	return e;
202 }
203 #endif /* PRELOADED_BL33_BASE */
204 
205 #endif /* EL3_PAYLOAD_BASE */
206 
207 /*******************************************************************************
208  * The only thing to do in BL2 is to load further images and pass control to
209  * BL31. The memory occupied by BL2 will be reclaimed by BL3x stages. BL2 runs
210  * entirely in S-EL1.
211  ******************************************************************************/
212 void bl2_main(void)
213 {
214 	bl31_params_t *bl2_to_bl31_params;
215 	entry_point_info_t *bl31_ep_info;
216 	int e;
217 
218 	NOTICE("BL2: %s\n", version_string);
219 	NOTICE("BL2: %s\n", build_message);
220 
221 	/* Perform remaining generic architectural setup in S-EL1 */
222 	bl2_arch_setup();
223 
224 #if TRUSTED_BOARD_BOOT
225 	/* Initialize authentication module */
226 	auth_mod_init();
227 #endif /* TRUSTED_BOARD_BOOT */
228 
229 	/*
230 	 * Load the subsequent bootloader images
231 	 */
232 	e = load_scp_bl2();
233 	if (e) {
234 		ERROR("Failed to load SCP_BL2 (%i)\n", e);
235 		plat_error_handler(e);
236 	}
237 
238 	/* Perform platform setup in BL2 after loading SCP_BL2 */
239 	bl2_platform_setup();
240 
241 	/*
242 	 * Get a pointer to the memory the platform has set aside to pass
243 	 * information to BL31.
244 	 */
245 	bl2_to_bl31_params = bl2_plat_get_bl31_params();
246 	bl31_ep_info = bl2_plat_get_bl31_ep_info();
247 
248 #ifdef EL3_PAYLOAD_BASE
249 	/*
250 	 * In the case of an EL3 payload, we don't need to load any further
251 	 * images. Just update the BL31 entrypoint info structure to make BL1
252 	 * jump to the EL3 payload.
253 	 * The pointer to the memory the platform has set aside to pass
254 	 * information to BL31 in the normal boot flow is reused here, even
255 	 * though only a fraction of the information contained in the
256 	 * bl31_params_t structure makes sense in the context of EL3 payloads.
257 	 * This will be refined in the future.
258 	 */
259 	INFO("BL2: Populating the entrypoint info for the EL3 payload\n");
260 	bl31_ep_info->pc = EL3_PAYLOAD_BASE;
261 	bl31_ep_info->args.arg0 = (unsigned long) bl2_to_bl31_params;
262 	bl2_plat_set_bl31_ep_info(NULL, bl31_ep_info);
263 #else
264 	e = load_bl31(bl2_to_bl31_params, bl31_ep_info);
265 	if (e) {
266 		ERROR("Failed to load BL31 (%i)\n", e);
267 		plat_error_handler(e);
268 	}
269 
270 	e = load_bl32(bl2_to_bl31_params);
271 	if (e) {
272 		if (e == -EAUTH) {
273 			ERROR("Failed to authenticate BL32\n");
274 			plat_error_handler(e);
275 		} else {
276 			WARN("Failed to load BL32 (%i)\n", e);
277 		}
278 	}
279 
280 #ifdef PRELOADED_BL33_BASE
281 	/*
282 	 * In this case, don't load the BL33 image as it's already loaded in
283 	 * memory. Update BL33 entrypoint information.
284 	 */
285 	INFO("BL2: Populating the entrypoint info for the preloaded BL33\n");
286 	bl2_to_bl31_params->bl33_ep_info->pc = PRELOADED_BL33_BASE;
287 	bl2_plat_set_bl33_ep_info(NULL, bl2_to_bl31_params->bl33_ep_info);
288 #else
289 	e = load_bl33(bl2_to_bl31_params);
290 	if (e) {
291 		ERROR("Failed to load BL33 (%i)\n", e);
292 		plat_error_handler(e);
293 	}
294 #endif /* PRELOADED_BL33_BASE */
295 
296 #endif /* EL3_PAYLOAD_BASE */
297 
298 	/* Flush the params to be passed to memory */
299 	bl2_plat_flush_bl31_params();
300 
301 	/*
302 	 * Run BL31 via an SMC to BL1. Information on how to pass control to
303 	 * the BL32 (if present) and BL33 software images will be passed to
304 	 * BL31 as an argument.
305 	 */
306 	smc(BL1_SMC_RUN_IMAGE, (unsigned long)bl31_ep_info, 0, 0, 0, 0, 0, 0);
307 }
308