xref: /rk3399_ARM-atf/bl2/bl2_main.c (revision d178637d2bac46ef0d227395663c55cc46a25d73)
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 /*******************************************************************************
173  * Load the BL33 image.
174  * The bl2_to_bl31_params param will be updated with the relevant BL33
175  * information.
176  * Return 0 on success, a negative error code otherwise.
177  ******************************************************************************/
178 static int load_bl33(bl31_params_t *bl2_to_bl31_params)
179 {
180 	meminfo_t bl33_mem_info;
181 	int e;
182 
183 	INFO("BL2: Loading BL33\n");
184 	assert(bl2_to_bl31_params != NULL);
185 
186 	bl2_plat_get_bl33_meminfo(&bl33_mem_info);
187 
188 	/* Load the BL33 image in non-secure memory provided by the platform */
189 	e = load_auth_image(&bl33_mem_info,
190 			    BL33_IMAGE_ID,
191 			    plat_get_ns_image_entrypoint(),
192 			    bl2_to_bl31_params->bl33_image_info,
193 			    bl2_to_bl31_params->bl33_ep_info);
194 
195 	if (e == 0) {
196 		bl2_plat_set_bl33_ep_info(bl2_to_bl31_params->bl33_image_info,
197 					  bl2_to_bl31_params->bl33_ep_info);
198 	}
199 
200 	return e;
201 }
202 #endif /* EL3_PAYLOAD_BASE */
203 
204 /*******************************************************************************
205  * The only thing to do in BL2 is to load further images and pass control to
206  * BL31. The memory occupied by BL2 will be reclaimed by BL3x stages. BL2 runs
207  * entirely in S-EL1.
208  ******************************************************************************/
209 void bl2_main(void)
210 {
211 	bl31_params_t *bl2_to_bl31_params;
212 	entry_point_info_t *bl31_ep_info;
213 	int e;
214 
215 	NOTICE("BL2: %s\n", version_string);
216 	NOTICE("BL2: %s\n", build_message);
217 
218 	/* Perform remaining generic architectural setup in S-EL1 */
219 	bl2_arch_setup();
220 
221 #if TRUSTED_BOARD_BOOT
222 	/* Initialize authentication module */
223 	auth_mod_init();
224 #endif /* TRUSTED_BOARD_BOOT */
225 
226 	/*
227 	 * Load the subsequent bootloader images
228 	 */
229 	e = load_scp_bl2();
230 	if (e) {
231 		ERROR("Failed to load SCP_BL2 (%i)\n", e);
232 		plat_error_handler(e);
233 	}
234 
235 	/* Perform platform setup in BL2 after loading SCP_BL2 */
236 	bl2_platform_setup();
237 
238 	/*
239 	 * Get a pointer to the memory the platform has set aside to pass
240 	 * information to BL31.
241 	 */
242 	bl2_to_bl31_params = bl2_plat_get_bl31_params();
243 	bl31_ep_info = bl2_plat_get_bl31_ep_info();
244 
245 #ifdef EL3_PAYLOAD_BASE
246 	/*
247 	 * In the case of an EL3 payload, we don't need to load any further
248 	 * images. Just update the BL31 entrypoint info structure to make BL1
249 	 * jump to the EL3 payload.
250 	 * The pointer to the memory the platform has set aside to pass
251 	 * information to BL31 in the normal boot flow is reused here, even
252 	 * though only a fraction of the information contained in the
253 	 * bl31_params_t structure makes sense in the context of EL3 payloads.
254 	 * This will be refined in the future.
255 	 */
256 	VERBOSE("BL2: Populating the entrypoint info for the EL3 payload\n");
257 	bl31_ep_info->pc = EL3_PAYLOAD_BASE;
258 	bl31_ep_info->args.arg0 = (unsigned long) bl2_to_bl31_params;
259 	bl2_plat_set_bl31_ep_info(NULL, bl31_ep_info);
260 #else
261 	e = load_bl31(bl2_to_bl31_params, bl31_ep_info);
262 	if (e) {
263 		ERROR("Failed to load BL31 (%i)\n", e);
264 		plat_error_handler(e);
265 	}
266 
267 	e = load_bl32(bl2_to_bl31_params);
268 	if (e) {
269 		if (e == -EAUTH) {
270 			ERROR("Failed to authenticate BL32\n");
271 			plat_error_handler(e);
272 		} else {
273 			WARN("Failed to load BL32 (%i)\n", e);
274 		}
275 	}
276 
277 	e = load_bl33(bl2_to_bl31_params);
278 	if (e) {
279 		ERROR("Failed to load BL33 (%i)\n", e);
280 		plat_error_handler(e);
281 	}
282 #endif /* EL3_PAYLOAD_BASE */
283 
284 	/* Flush the params to be passed to memory */
285 	bl2_plat_flush_bl31_params();
286 
287 	/*
288 	 * Run BL31 via an SMC to BL1. Information on how to pass control to
289 	 * the BL32 (if present) and BL33 software images will be passed to
290 	 * BL31 as an argument.
291 	 */
292 	smc(BL1_SMC_RUN_IMAGE, (unsigned long)bl31_ep_info, 0, 0, 0, 0, 0, 0);
293 }
294