blob: 9ff75d29a809731355c6574035b8fe3aa7045970 [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
172/*******************************************************************************
Juan Castillo7d199412015-12-14 09:35:25 +0000173 * Load the BL33 image.
174 * The bl2_to_bl31_params param will be updated with the relevant BL33
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100175 * information.
176 * Return 0 on success, a negative error code otherwise.
177 ******************************************************************************/
178static int load_bl33(bl31_params_t *bl2_to_bl31_params)
179{
180 meminfo_t bl33_mem_info;
181 int e;
182
Juan Castillo7d199412015-12-14 09:35:25 +0000183 INFO("BL2: Loading BL33\n");
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100184 assert(bl2_to_bl31_params != NULL);
185
186 bl2_plat_get_bl33_meminfo(&bl33_mem_info);
187
Juan Castillo7d199412015-12-14 09:35:25 +0000188 /* Load the BL33 image in non-secure memory provided by the platform */
Juan Castilloa08a5e72015-05-19 11:54:12 +0100189 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);
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100194
Juan Castilloa08a5e72015-05-19 11:54:12 +0100195 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);
Sandrine Bailleuxaada44c2015-03-26 11:07:09 +0000198 }
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100199
200 return e;
201}
Sandrine Bailleux03897bb2015-11-26 16:31:34 +0000202#endif /* EL3_PAYLOAD_BASE */
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100203
204/*******************************************************************************
205 * The only thing to do in BL2 is to load further images and pass control to
Juan Castillo7d199412015-12-14 09:35:25 +0000206 * BL31. The memory occupied by BL2 will be reclaimed by BL3x stages. BL2 runs
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100207 * entirely in S-EL1.
208 ******************************************************************************/
209void bl2_main(void)
210{
211 bl31_params_t *bl2_to_bl31_params;
212 entry_point_info_t *bl31_ep_info;
213 int e;
214
Dan Handley91b624e2014-07-29 17:14:00 +0100215 NOTICE("BL2: %s\n", version_string);
216 NOTICE("BL2: %s\n", build_message);
217
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100218 /* Perform remaining generic architectural setup in S-EL1 */
219 bl2_arch_setup();
220
Juan Castillo9246ab82015-01-28 16:46:57 +0000221#if TRUSTED_BOARD_BOOT
222 /* Initialize authentication module */
Juan Castilloa08a5e72015-05-19 11:54:12 +0100223 auth_mod_init();
Juan Castillo9246ab82015-01-28 16:46:57 +0000224#endif /* TRUSTED_BOARD_BOOT */
225
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100226 /*
227 * Load the subsequent bootloader images
228 */
Juan Castilloa72b6472015-12-10 15:49:17 +0000229 e = load_scp_bl2();
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100230 if (e) {
Juan Castilloa72b6472015-12-10 15:49:17 +0000231 ERROR("Failed to load SCP_BL2 (%i)\n", e);
Juan Castillo26ae5832015-09-25 15:41:14 +0100232 plat_error_handler(e);
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100233 }
234
Juan Castilloa72b6472015-12-10 15:49:17 +0000235 /* Perform platform setup in BL2 after loading SCP_BL2 */
Juan Castillo6b672f52014-09-04 14:43:09 +0100236 bl2_platform_setup();
237
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100238 /*
239 * Get a pointer to the memory the platform has set aside to pass
Juan Castillo7d199412015-12-14 09:35:25 +0000240 * information to BL31.
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100241 */
242 bl2_to_bl31_params = bl2_plat_get_bl31_params();
243 bl31_ep_info = bl2_plat_get_bl31_ep_info();
244
Sandrine Bailleux03897bb2015-11-26 16:31:34 +0000245#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
Juan Castillo7d199412015-12-14 09:35:25 +0000251 * information to BL31 in the normal boot flow is reused here, even
Sandrine Bailleux03897bb2015-11-26 16:31:34 +0000252 * 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
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100261 e = load_bl31(bl2_to_bl31_params, bl31_ep_info);
262 if (e) {
Juan Castillo7d199412015-12-14 09:35:25 +0000263 ERROR("Failed to load BL31 (%i)\n", e);
Juan Castillo26ae5832015-09-25 15:41:14 +0100264 plat_error_handler(e);
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100265 }
266
267 e = load_bl32(bl2_to_bl31_params);
Juan Castillo97dbcf12015-08-17 10:43:27 +0100268 if (e) {
Juan Castilloec813f52015-10-01 18:37:40 +0100269 if (e == -EAUTH) {
Juan Castillo7d199412015-12-14 09:35:25 +0000270 ERROR("Failed to authenticate BL32\n");
Juan Castillo26ae5832015-09-25 15:41:14 +0100271 plat_error_handler(e);
Juan Castillo97dbcf12015-08-17 10:43:27 +0100272 } else {
Juan Castillo7d199412015-12-14 09:35:25 +0000273 WARN("Failed to load BL32 (%i)\n", e);
Juan Castillo97dbcf12015-08-17 10:43:27 +0100274 }
275 }
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100276
277 e = load_bl33(bl2_to_bl31_params);
278 if (e) {
Juan Castillo7d199412015-12-14 09:35:25 +0000279 ERROR("Failed to load BL33 (%i)\n", e);
Juan Castillo26ae5832015-09-25 15:41:14 +0100280 plat_error_handler(e);
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100281 }
Sandrine Bailleux03897bb2015-11-26 16:31:34 +0000282#endif /* EL3_PAYLOAD_BASE */
Sandrine Bailleuxf841ef02014-06-24 14:19:36 +0100283
Andrew Thoelkea55566d2014-05-28 22:22:55 +0100284 /* Flush the params to be passed to memory */
285 bl2_plat_flush_bl31_params();
286
Achin Gupta4f6ad662013-10-25 09:08:21 +0100287 /*
Juan Castillo7d199412015-12-14 09:35:25 +0000288 * 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.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100291 */
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100292 smc(BL1_SMC_RUN_IMAGE, (unsigned long)bl31_ep_info, 0, 0, 0, 0, 0, 0);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100293}