blob: 73781dd09f022181c6fb73fb92cba3487e0b7ccd [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Dan Handleye83b0ca2014-01-14 18:17:09 +00002 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
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
Dan Handley2bd4ef22014-04-09 13:14:54 +010031#include <arch.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010032#include <arch_helpers.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010033#include <assert.h>
Juan Castilloa08a5e72015-05-19 11:54:12 +010034#include <auth_mod.h>
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +010035#include <bl1.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010036#include <bl_common.h>
Dan Handley714a0d22014-04-09 13:13:04 +010037#include <debug.h>
Juan Castilloec813f52015-10-01 18:37:40 +010038#include <errno.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010039#include <platform.h>
Dan Handleyed6ff952014-05-14 17:44:19 +010040#include <platform_def.h>
Juan Castillo3a66aca2015-04-13 17:36:19 +010041#include <stdint.h>
Dan Handleybcd60ba2014-04-17 18:53:42 +010042#include "bl2_private.h"
Achin Gupta4f6ad662013-10-25 09:08:21 +010043
Juan Castilloa72b6472015-12-10 15:49:17 +000044/*
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
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010051/*******************************************************************************
Juan Castilloa72b6472015-12-10 15:49:17 +000052 * 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
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010056 * code otherwise.
57 ******************************************************************************/
Juan Castilloa72b6472015-12-10 15:49:17 +000058static int load_scp_bl2(void)
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010059{
60 int e = 0;
Juan Castilloa72b6472015-12-10 15:49:17 +000061#ifdef SCP_BL2_BASE
62 meminfo_t scp_bl2_mem_info;
63 image_info_t scp_bl2_image_info;
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010064
65 /*
Juan Castilloa72b6472015-12-10 15:49:17 +000066 * It is up to the platform to specify where SCP_BL2 should be loaded if
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010067 * 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
Juan Castilloa72b6472015-12-10 15:49:17 +000071 * won't execute the SCP_BL2 image.
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010072 */
Juan Castilloa72b6472015-12-10 15:49:17 +000073 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,
Juan Castilloa08a5e72015-05-19 11:54:12 +010080 NULL);
Juan Castillo9246ab82015-01-28 16:46:57 +000081
Juan Castilloa08a5e72015-05-19 11:54:12 +010082 if (e == 0) {
Juan Castilloa72b6472015-12-10 15:49:17 +000083 /* The subsequent handling of SCP_BL2 is platform specific */
84 e = bl2_plat_handle_scp_bl2(&scp_bl2_image_info);
Juan Castilloa08a5e72015-05-19 11:54:12 +010085 if (e) {
Juan Castilloa72b6472015-12-10 15:49:17 +000086 ERROR("Failure in platform-specific handling of SCP_BL2 image.\n");
Juan Castilloa08a5e72015-05-19 11:54:12 +010087 }
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010088 }
Juan Castilloa72b6472015-12-10 15:49:17 +000089#endif /* SCP_BL2_BASE */
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010090
91 return e;
92}
Vikram Kanigiria3a5e4a2014-05-15 18:27:15 +010093
Sandrine Bailleux03897bb2015-11-26 16:31:34 +000094#ifndef EL3_PAYLOAD_BASE
Vikram Kanigiria3a5e4a2014-05-15 18:27:15 +010095/*******************************************************************************
Juan Castillo7d199412015-12-14 09:35:25 +000096 * Load the BL31 image.
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010097 * The bl2_to_bl31_params and bl31_ep_info params will be updated with the
Juan Castillo7d199412015-12-14 09:35:25 +000098 * relevant BL31 information.
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +010099 * Return 0 on success, a negative error code otherwise.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100100 ******************************************************************************/
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100101static int load_bl31(bl31_params_t *bl2_to_bl31_params,
102 entry_point_info_t *bl31_ep_info)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100103{
Dan Handleye2712bc2014-04-10 15:37:22 +0100104 meminfo_t *bl2_tzram_layout;
Vikram Kanigirida567432014-04-15 18:08:08 +0100105 int e;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100106
Juan Castillo7d199412015-12-14 09:35:25 +0000107 INFO("BL2: Loading BL31\n");
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100108 assert(bl2_to_bl31_params != NULL);
109 assert(bl31_ep_info != NULL);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100110
111 /* Find out how much free trusted ram remains after BL2 load */
Sandrine Bailleuxee12f6f2013-11-28 14:55:58 +0000112 bl2_tzram_layout = bl2_plat_sec_mem_layout();
Achin Gupta4f6ad662013-10-25 09:08:21 +0100113
Juan Castillo7d199412015-12-14 09:35:25 +0000114 /* Set the X0 parameter to BL31 */
Andrew Thoelkea55566d2014-05-28 22:22:55 +0100115 bl31_ep_info->args.arg0 = (unsigned long)bl2_to_bl31_params;
116
Juan Castillo7d199412015-12-14 09:35:25 +0000117 /* Load the BL31 image */
Juan Castilloa08a5e72015-05-19 11:54:12 +0100118 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);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100123
Juan Castilloa08a5e72015-05-19 11:54:12 +0100124 if (e == 0) {
125 bl2_plat_set_bl31_ep_info(bl2_to_bl31_params->bl31_image_info,
126 bl31_ep_info);
Sandrine Bailleuxaada44c2015-03-26 11:07:09 +0000127 }
Juan Castillo9246ab82015-01-28 16:46:57 +0000128
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100129 return e;
130}
Vikram Kanigirida567432014-04-15 18:08:08 +0100131
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100132/*******************************************************************************
Juan Castillo7d199412015-12-14 09:35:25 +0000133 * Load the BL32 image if there's one.
134 * The bl2_to_bl31_params param will be updated with the relevant BL32
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100135 * information.
Juan Castillo7d199412015-12-14 09:35:25 +0000136 * If a platform does not want to attempt to load BL32 image it must leave
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100137 * BL32_BASE undefined.
Juan Castillo7d199412015-12-14 09:35:25 +0000138 * Return 0 on success or if there's no BL32 image to load, a negative error
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100139 * code otherwise.
140 ******************************************************************************/
141static 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;
Harry Liebel561cd332014-02-14 14:42:48 +0000146
Juan Castillo7d199412015-12-14 09:35:25 +0000147 INFO("BL2: Loading BL32\n");
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100148 assert(bl2_to_bl31_params != NULL);
Dan Handley21a30ab2014-04-15 11:38:38 +0100149
Achin Gupta4f6ad662013-10-25 09:08:21 +0100150 /*
Juan Castillo7d199412015-12-14 09:35:25 +0000151 * It is up to the platform to specify where BL32 should be loaded if
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100152 * it exists. It could create space in the secure sram or point to a
Dan Handley21a30ab2014-04-15 11:38:38 +0100153 * completely different memory.
Vikram Kanigiria3a5e4a2014-05-15 18:27:15 +0100154 */
Vikram Kanigirid8c9d262014-05-16 18:48:12 +0100155 bl2_plat_get_bl32_meminfo(&bl32_mem_info);
Juan Castilloa08a5e72015-05-19 11:54:12 +0100156 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);
Juan Castillo9246ab82015-01-28 16:46:57 +0000161
Juan Castilloa08a5e72015-05-19 11:54:12 +0100162 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);
Sandrine Bailleuxaada44c2015-03-26 11:07:09 +0000166 }
Dan Handley21a30ab2014-04-15 11:38:38 +0100167#endif /* BL32_BASE */
Achin Guptaa3050ed2014-02-19 17:52:35 +0000168
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100169 return e;
170}
171
Antonio Nino Diazfaf573a2016-02-15 14:53:10 +0000172#ifndef BL33_BASE
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100173/*******************************************************************************
Juan Castillo7d199412015-12-14 09:35:25 +0000174 * Load the BL33 image.
175 * The bl2_to_bl31_params param will be updated with the relevant BL33
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100176 * information.
177 * Return 0 on success, a negative error code otherwise.
178 ******************************************************************************/
179static int load_bl33(bl31_params_t *bl2_to_bl31_params)
180{
181 meminfo_t bl33_mem_info;
182 int e;
183
Juan Castillo7d199412015-12-14 09:35:25 +0000184 INFO("BL2: Loading BL33\n");
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100185 assert(bl2_to_bl31_params != NULL);
186
187 bl2_plat_get_bl33_meminfo(&bl33_mem_info);
188
Juan Castillo7d199412015-12-14 09:35:25 +0000189 /* Load the BL33 image in non-secure memory provided by the platform */
Juan Castilloa08a5e72015-05-19 11:54:12 +0100190 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);
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100195
Juan Castilloa08a5e72015-05-19 11:54:12 +0100196 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);
Sandrine Bailleuxaada44c2015-03-26 11:07:09 +0000199 }
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100200
201 return e;
202}
Antonio Nino Diazfaf573a2016-02-15 14:53:10 +0000203#endif /* BL33_BASE */
204
Sandrine Bailleux03897bb2015-11-26 16:31:34 +0000205#endif /* EL3_PAYLOAD_BASE */
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100206
207/*******************************************************************************
208 * The only thing to do in BL2 is to load further images and pass control to
Juan Castillo7d199412015-12-14 09:35:25 +0000209 * BL31. The memory occupied by BL2 will be reclaimed by BL3x stages. BL2 runs
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100210 * entirely in S-EL1.
211 ******************************************************************************/
212void bl2_main(void)
213{
214 bl31_params_t *bl2_to_bl31_params;
215 entry_point_info_t *bl31_ep_info;
216 int e;
217
Dan Handley91b624e2014-07-29 17:14:00 +0100218 NOTICE("BL2: %s\n", version_string);
219 NOTICE("BL2: %s\n", build_message);
220
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100221 /* Perform remaining generic architectural setup in S-EL1 */
222 bl2_arch_setup();
223
Juan Castillo9246ab82015-01-28 16:46:57 +0000224#if TRUSTED_BOARD_BOOT
225 /* Initialize authentication module */
Juan Castilloa08a5e72015-05-19 11:54:12 +0100226 auth_mod_init();
Juan Castillo9246ab82015-01-28 16:46:57 +0000227#endif /* TRUSTED_BOARD_BOOT */
228
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100229 /*
230 * Load the subsequent bootloader images
231 */
Juan Castilloa72b6472015-12-10 15:49:17 +0000232 e = load_scp_bl2();
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100233 if (e) {
Juan Castilloa72b6472015-12-10 15:49:17 +0000234 ERROR("Failed to load SCP_BL2 (%i)\n", e);
Juan Castillo26ae5832015-09-25 15:41:14 +0100235 plat_error_handler(e);
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100236 }
237
Juan Castilloa72b6472015-12-10 15:49:17 +0000238 /* Perform platform setup in BL2 after loading SCP_BL2 */
Juan Castillo6b672f52014-09-04 14:43:09 +0100239 bl2_platform_setup();
240
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100241 /*
242 * Get a pointer to the memory the platform has set aside to pass
Juan Castillo7d199412015-12-14 09:35:25 +0000243 * information to BL31.
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100244 */
245 bl2_to_bl31_params = bl2_plat_get_bl31_params();
246 bl31_ep_info = bl2_plat_get_bl31_ep_info();
247
Sandrine Bailleux03897bb2015-11-26 16:31:34 +0000248#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
Juan Castillo7d199412015-12-14 09:35:25 +0000254 * information to BL31 in the normal boot flow is reused here, even
Sandrine Bailleux03897bb2015-11-26 16:31:34 +0000255 * 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 */
Antonio Nino Diazfaf573a2016-02-15 14:53:10 +0000259 INFO("BL2: Populating the entrypoint info for the EL3 payload\n");
Sandrine Bailleux03897bb2015-11-26 16:31:34 +0000260 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
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100264 e = load_bl31(bl2_to_bl31_params, bl31_ep_info);
265 if (e) {
Juan Castillo7d199412015-12-14 09:35:25 +0000266 ERROR("Failed to load BL31 (%i)\n", e);
Juan Castillo26ae5832015-09-25 15:41:14 +0100267 plat_error_handler(e);
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100268 }
269
270 e = load_bl32(bl2_to_bl31_params);
Juan Castillo97dbcf12015-08-17 10:43:27 +0100271 if (e) {
Juan Castilloec813f52015-10-01 18:37:40 +0100272 if (e == -EAUTH) {
Juan Castillo7d199412015-12-14 09:35:25 +0000273 ERROR("Failed to authenticate BL32\n");
Juan Castillo26ae5832015-09-25 15:41:14 +0100274 plat_error_handler(e);
Juan Castillo97dbcf12015-08-17 10:43:27 +0100275 } else {
Juan Castillo7d199412015-12-14 09:35:25 +0000276 WARN("Failed to load BL32 (%i)\n", e);
Juan Castillo97dbcf12015-08-17 10:43:27 +0100277 }
278 }
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100279
Antonio Nino Diazfaf573a2016-02-15 14:53:10 +0000280#ifdef 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 = BL33_BASE;
287 bl2_plat_set_bl33_ep_info(NULL, bl2_to_bl31_params->bl33_ep_info);
288#else
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100289 e = load_bl33(bl2_to_bl31_params);
290 if (e) {
Juan Castillo7d199412015-12-14 09:35:25 +0000291 ERROR("Failed to load BL33 (%i)\n", e);
Juan Castillo26ae5832015-09-25 15:41:14 +0100292 plat_error_handler(e);
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100293 }
Antonio Nino Diazfaf573a2016-02-15 14:53:10 +0000294#endif /* BL33_BASE */
295
Sandrine Bailleux03897bb2015-11-26 16:31:34 +0000296#endif /* EL3_PAYLOAD_BASE */
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100297
Andrew Thoelkea55566d2014-05-28 22:22:55 +0100298 /* Flush the params to be passed to memory */
299 bl2_plat_flush_bl31_params();
300
Achin Gupta4f6ad662013-10-25 09:08:21 +0100301 /*
Juan Castillo7d199412015-12-14 09:35:25 +0000302 * 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.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100305 */
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100306 smc(BL1_SMC_RUN_IMAGE, (unsigned long)bl31_ep_info, 0, 0, 0, 0, 0, 0);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100307}