xref: /rk3399_ARM-atf/bl2/bl2_main.c (revision 4c117f6c49330682cabdbf6d082e7c02dd32e84b)
14f6ad66aSAchin Gupta /*
2e83b0cadSDan Handley  * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta  *
44f6ad66aSAchin Gupta  * Redistribution and use in source and binary forms, with or without
54f6ad66aSAchin Gupta  * modification, are permitted provided that the following conditions are met:
64f6ad66aSAchin Gupta  *
74f6ad66aSAchin Gupta  * Redistributions of source code must retain the above copyright notice, this
84f6ad66aSAchin Gupta  * list of conditions and the following disclaimer.
94f6ad66aSAchin Gupta  *
104f6ad66aSAchin Gupta  * Redistributions in binary form must reproduce the above copyright notice,
114f6ad66aSAchin Gupta  * this list of conditions and the following disclaimer in the documentation
124f6ad66aSAchin Gupta  * and/or other materials provided with the distribution.
134f6ad66aSAchin Gupta  *
144f6ad66aSAchin Gupta  * Neither the name of ARM nor the names of its contributors may be used
154f6ad66aSAchin Gupta  * to endorse or promote products derived from this software without specific
164f6ad66aSAchin Gupta  * prior written permission.
174f6ad66aSAchin Gupta  *
184f6ad66aSAchin Gupta  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
194f6ad66aSAchin Gupta  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204f6ad66aSAchin Gupta  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
214f6ad66aSAchin Gupta  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
224f6ad66aSAchin Gupta  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
234f6ad66aSAchin Gupta  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
244f6ad66aSAchin Gupta  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
254f6ad66aSAchin Gupta  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
264f6ad66aSAchin Gupta  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
274f6ad66aSAchin Gupta  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
284f6ad66aSAchin Gupta  * POSSIBILITY OF SUCH DAMAGE.
294f6ad66aSAchin Gupta  */
304f6ad66aSAchin Gupta 
3197043ac9SDan Handley #include <arch.h>
324f6ad66aSAchin Gupta #include <arch_helpers.h>
3397043ac9SDan Handley #include <assert.h>
341779ba6bSJuan Castillo #include <auth_mod.h>
354f6ad66aSAchin Gupta #include <bl_common.h>
3635e98e55SDan Handley #include <debug.h>
3778460a05SJuan Castillo #include <errno.h>
3897043ac9SDan Handley #include <platform.h>
395f0cdb05SDan Handley #include <platform_def.h>
4016948ae1SJuan Castillo #include <stdint.h>
415b827a8fSDan Handley #include "bl2_private.h"
424f6ad66aSAchin Gupta 
4393d81d64SSandrine Bailleux /*******************************************************************************
4493d81d64SSandrine Bailleux  * Load the BL3-0 image if there's one.
4593d81d64SSandrine Bailleux  * If a platform does not want to attempt to load BL3-0 image it must leave
4693d81d64SSandrine Bailleux  * BL30_BASE undefined.
4793d81d64SSandrine Bailleux  * Return 0 on success or if there's no BL3-0 image to load, a negative error
4893d81d64SSandrine Bailleux  * code otherwise.
4993d81d64SSandrine Bailleux  ******************************************************************************/
5093d81d64SSandrine Bailleux static int load_bl30(void)
5193d81d64SSandrine Bailleux {
5293d81d64SSandrine Bailleux 	int e = 0;
5393d81d64SSandrine Bailleux #ifdef BL30_BASE
5493d81d64SSandrine Bailleux 	meminfo_t bl30_mem_info;
5593d81d64SSandrine Bailleux 	image_info_t bl30_image_info;
5693d81d64SSandrine Bailleux 
5793d81d64SSandrine Bailleux 	/*
5893d81d64SSandrine Bailleux 	 * It is up to the platform to specify where BL3-0 should be loaded if
5993d81d64SSandrine Bailleux 	 * it exists. It could create space in the secure sram or point to a
6093d81d64SSandrine Bailleux 	 * completely different memory.
6193d81d64SSandrine Bailleux 	 *
6293d81d64SSandrine Bailleux 	 * The entry point information is not relevant in this case as the AP
6393d81d64SSandrine Bailleux 	 * won't execute the BL3-0 image.
6493d81d64SSandrine Bailleux 	 */
656ad2e461SDan Handley 	INFO("BL2: Loading BL3-0\n");
6693d81d64SSandrine Bailleux 	bl2_plat_get_bl30_meminfo(&bl30_mem_info);
6792de3565SJuan Castillo 	bl30_image_info.h.version = VERSION_1;
681779ba6bSJuan Castillo 	e = load_auth_image(&bl30_mem_info,
6916948ae1SJuan Castillo 			    BL30_IMAGE_ID,
7093d81d64SSandrine Bailleux 			    BL30_BASE,
7193d81d64SSandrine Bailleux 			    &bl30_image_info,
7293d81d64SSandrine Bailleux 			    NULL);
7393d81d64SSandrine Bailleux 
741779ba6bSJuan Castillo 	if (e == 0) {
7593d81d64SSandrine Bailleux 		/* The subsequent handling of BL3-0 is platform specific */
76bcb79b90SSandrine Bailleux 		e = bl2_plat_handle_bl30(&bl30_image_info);
77bcb79b90SSandrine Bailleux 		if (e) {
78bcb79b90SSandrine Bailleux 			ERROR("Failure in platform-specific handling of BL3-0 image.\n");
791779ba6bSJuan Castillo 		}
8093d81d64SSandrine Bailleux 	}
8193d81d64SSandrine Bailleux #endif /* BL30_BASE */
8293d81d64SSandrine Bailleux 
8393d81d64SSandrine Bailleux 	return e;
8493d81d64SSandrine Bailleux }
8529fb905dSVikram Kanigiri 
86*4c117f6cSSandrine Bailleux #ifndef EL3_PAYLOAD_BASE
8729fb905dSVikram Kanigiri /*******************************************************************************
8893d81d64SSandrine Bailleux  * Load the BL3-1 image.
8993d81d64SSandrine Bailleux  * The bl2_to_bl31_params and bl31_ep_info params will be updated with the
9093d81d64SSandrine Bailleux  * relevant BL3-1 information.
9193d81d64SSandrine Bailleux  * Return 0 on success, a negative error code otherwise.
924f6ad66aSAchin Gupta  ******************************************************************************/
9393d81d64SSandrine Bailleux static int load_bl31(bl31_params_t *bl2_to_bl31_params,
9493d81d64SSandrine Bailleux 		     entry_point_info_t *bl31_ep_info)
954f6ad66aSAchin Gupta {
96fb037bfbSDan Handley 	meminfo_t *bl2_tzram_layout;
974112bfa0SVikram Kanigiri 	int e;
984f6ad66aSAchin Gupta 
996ad2e461SDan Handley 	INFO("BL2: Loading BL3-1\n");
10093d81d64SSandrine Bailleux 	assert(bl2_to_bl31_params != NULL);
10193d81d64SSandrine Bailleux 	assert(bl31_ep_info != NULL);
1024f6ad66aSAchin Gupta 
1034f6ad66aSAchin Gupta 	/* Find out how much free trusted ram remains after BL2 load */
104ee12f6f7SSandrine Bailleux 	bl2_tzram_layout = bl2_plat_sec_mem_layout();
1054f6ad66aSAchin Gupta 
10693d81d64SSandrine Bailleux 	/* Set the X0 parameter to BL3-1 */
10703462671SAndrew Thoelke 	bl31_ep_info->args.arg0 = (unsigned long)bl2_to_bl31_params;
10803462671SAndrew Thoelke 
1098f55dfb4SSandrine Bailleux 	/* Load the BL3-1 image */
1101779ba6bSJuan Castillo 	e = load_auth_image(bl2_tzram_layout,
11116948ae1SJuan Castillo 			    BL31_IMAGE_ID,
1124112bfa0SVikram Kanigiri 			    BL31_BASE,
1134112bfa0SVikram Kanigiri 			    bl2_to_bl31_params->bl31_image_info,
1144112bfa0SVikram Kanigiri 			    bl31_ep_info);
1154f6ad66aSAchin Gupta 
1161779ba6bSJuan Castillo 	if (e == 0) {
1174112bfa0SVikram Kanigiri 		bl2_plat_set_bl31_ep_info(bl2_to_bl31_params->bl31_image_info,
1184112bfa0SVikram Kanigiri 					  bl31_ep_info);
1191779ba6bSJuan Castillo 	}
120a3050ed5SAchin Gupta 
12193d81d64SSandrine Bailleux 	return e;
122561cd33eSHarry Liebel }
123e4d084eaSAchin Gupta 
12493d81d64SSandrine Bailleux /*******************************************************************************
12593d81d64SSandrine Bailleux  * Load the BL3-2 image if there's one.
12693d81d64SSandrine Bailleux  * The bl2_to_bl31_params param will be updated with the relevant BL3-2
12793d81d64SSandrine Bailleux  * information.
12893d81d64SSandrine Bailleux  * If a platform does not want to attempt to load BL3-2 image it must leave
12993d81d64SSandrine Bailleux  * BL32_BASE undefined.
13093d81d64SSandrine Bailleux  * Return 0 on success or if there's no BL3-2 image to load, a negative error
13193d81d64SSandrine Bailleux  * code otherwise.
13293d81d64SSandrine Bailleux  ******************************************************************************/
13393d81d64SSandrine Bailleux static int load_bl32(bl31_params_t *bl2_to_bl31_params)
13493d81d64SSandrine Bailleux {
13593d81d64SSandrine Bailleux 	int e = 0;
1361151c821SDan Handley #ifdef BL32_BASE
13793d81d64SSandrine Bailleux 	meminfo_t bl32_mem_info;
13893d81d64SSandrine Bailleux 
1396ad2e461SDan Handley 	INFO("BL2: Loading BL3-2\n");
14093d81d64SSandrine Bailleux 	assert(bl2_to_bl31_params != NULL);
14193d81d64SSandrine Bailleux 
14229fb905dSVikram Kanigiri 	/*
14393d81d64SSandrine Bailleux 	 * It is up to the platform to specify where BL3-2 should be loaded if
14493d81d64SSandrine Bailleux 	 * it exists. It could create space in the secure sram or point to a
1451151c821SDan Handley 	 * completely different memory.
14629fb905dSVikram Kanigiri 	 */
1476871c5d3SVikram Kanigiri 	bl2_plat_get_bl32_meminfo(&bl32_mem_info);
1481779ba6bSJuan Castillo 	e = load_auth_image(&bl32_mem_info,
14916948ae1SJuan Castillo 			    BL32_IMAGE_ID,
1504112bfa0SVikram Kanigiri 			    BL32_BASE,
1514112bfa0SVikram Kanigiri 			    bl2_to_bl31_params->bl32_image_info,
1524112bfa0SVikram Kanigiri 			    bl2_to_bl31_params->bl32_ep_info);
15329fb905dSVikram Kanigiri 
1541779ba6bSJuan Castillo 	if (e == 0) {
1554112bfa0SVikram Kanigiri 		bl2_plat_set_bl32_ep_info(
1564112bfa0SVikram Kanigiri 			bl2_to_bl31_params->bl32_image_info,
1574112bfa0SVikram Kanigiri 			bl2_to_bl31_params->bl32_ep_info);
1581779ba6bSJuan Castillo 	}
1591151c821SDan Handley #endif /* BL32_BASE */
1604112bfa0SVikram Kanigiri 
16193d81d64SSandrine Bailleux 	return e;
16293d81d64SSandrine Bailleux }
16393d81d64SSandrine Bailleux 
16493d81d64SSandrine Bailleux /*******************************************************************************
16593d81d64SSandrine Bailleux  * Load the BL3-3 image.
16693d81d64SSandrine Bailleux  * The bl2_to_bl31_params param will be updated with the relevant BL3-3
16793d81d64SSandrine Bailleux  * information.
16893d81d64SSandrine Bailleux  * Return 0 on success, a negative error code otherwise.
16993d81d64SSandrine Bailleux  ******************************************************************************/
17093d81d64SSandrine Bailleux static int load_bl33(bl31_params_t *bl2_to_bl31_params)
17193d81d64SSandrine Bailleux {
17293d81d64SSandrine Bailleux 	meminfo_t bl33_mem_info;
17393d81d64SSandrine Bailleux 	int e;
17493d81d64SSandrine Bailleux 
1756ad2e461SDan Handley 	INFO("BL2: Loading BL3-3\n");
17693d81d64SSandrine Bailleux 	assert(bl2_to_bl31_params != NULL);
17793d81d64SSandrine Bailleux 
17893d81d64SSandrine Bailleux 	bl2_plat_get_bl33_meminfo(&bl33_mem_info);
17993d81d64SSandrine Bailleux 
18093d81d64SSandrine Bailleux 	/* Load the BL3-3 image in non-secure memory provided by the platform */
1811779ba6bSJuan Castillo 	e = load_auth_image(&bl33_mem_info,
18216948ae1SJuan Castillo 			    BL33_IMAGE_ID,
18393d81d64SSandrine Bailleux 			    plat_get_ns_image_entrypoint(),
18493d81d64SSandrine Bailleux 			    bl2_to_bl31_params->bl33_image_info,
18593d81d64SSandrine Bailleux 			    bl2_to_bl31_params->bl33_ep_info);
18693d81d64SSandrine Bailleux 
1871779ba6bSJuan Castillo 	if (e == 0) {
18893d81d64SSandrine Bailleux 		bl2_plat_set_bl33_ep_info(bl2_to_bl31_params->bl33_image_info,
18993d81d64SSandrine Bailleux 					  bl2_to_bl31_params->bl33_ep_info);
1901779ba6bSJuan Castillo 	}
19193d81d64SSandrine Bailleux 
19293d81d64SSandrine Bailleux 	return e;
19393d81d64SSandrine Bailleux }
194*4c117f6cSSandrine Bailleux #endif /* EL3_PAYLOAD_BASE */
19593d81d64SSandrine Bailleux 
19693d81d64SSandrine Bailleux /*******************************************************************************
19793d81d64SSandrine Bailleux  * The only thing to do in BL2 is to load further images and pass control to
19893d81d64SSandrine Bailleux  * BL3-1. The memory occupied by BL2 will be reclaimed by BL3-x stages. BL2 runs
19993d81d64SSandrine Bailleux  * entirely in S-EL1.
20093d81d64SSandrine Bailleux  ******************************************************************************/
20193d81d64SSandrine Bailleux void bl2_main(void)
20293d81d64SSandrine Bailleux {
20393d81d64SSandrine Bailleux 	bl31_params_t *bl2_to_bl31_params;
20493d81d64SSandrine Bailleux 	entry_point_info_t *bl31_ep_info;
20593d81d64SSandrine Bailleux 	int e;
20693d81d64SSandrine Bailleux 
2076ad2e461SDan Handley 	NOTICE("BL2: %s\n", version_string);
2086ad2e461SDan Handley 	NOTICE("BL2: %s\n", build_message);
2096ad2e461SDan Handley 
21093d81d64SSandrine Bailleux 	/* Perform remaining generic architectural setup in S-EL1 */
21193d81d64SSandrine Bailleux 	bl2_arch_setup();
21293d81d64SSandrine Bailleux 
213dec840afSJuan Castillo #if TRUSTED_BOARD_BOOT
214dec840afSJuan Castillo 	/* Initialize authentication module */
2151779ba6bSJuan Castillo 	auth_mod_init();
216dec840afSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */
217dec840afSJuan Castillo 
21893d81d64SSandrine Bailleux 	/*
21993d81d64SSandrine Bailleux 	 * Load the subsequent bootloader images
22093d81d64SSandrine Bailleux 	 */
22193d81d64SSandrine Bailleux 	e = load_bl30();
22293d81d64SSandrine Bailleux 	if (e) {
22393d81d64SSandrine Bailleux 		ERROR("Failed to load BL3-0 (%i)\n", e);
22440fc6cd1SJuan Castillo 		plat_error_handler(e);
22593d81d64SSandrine Bailleux 	}
22693d81d64SSandrine Bailleux 
227ef538c6fSJuan Castillo 	/* Perform platform setup in BL2 after loading BL3-0 */
228ef538c6fSJuan Castillo 	bl2_platform_setup();
229ef538c6fSJuan Castillo 
23093d81d64SSandrine Bailleux 	/*
23193d81d64SSandrine Bailleux 	 * Get a pointer to the memory the platform has set aside to pass
23293d81d64SSandrine Bailleux 	 * information to BL3-1.
23393d81d64SSandrine Bailleux 	 */
23493d81d64SSandrine Bailleux 	bl2_to_bl31_params = bl2_plat_get_bl31_params();
23593d81d64SSandrine Bailleux 	bl31_ep_info = bl2_plat_get_bl31_ep_info();
23693d81d64SSandrine Bailleux 
237*4c117f6cSSandrine Bailleux #ifdef EL3_PAYLOAD_BASE
238*4c117f6cSSandrine Bailleux 	/*
239*4c117f6cSSandrine Bailleux 	 * In the case of an EL3 payload, we don't need to load any further
240*4c117f6cSSandrine Bailleux 	 * images. Just update the BL31 entrypoint info structure to make BL1
241*4c117f6cSSandrine Bailleux 	 * jump to the EL3 payload.
242*4c117f6cSSandrine Bailleux 	 * The pointer to the memory the platform has set aside to pass
243*4c117f6cSSandrine Bailleux 	 * information to BL3-1 in the normal boot flow is reused here, even
244*4c117f6cSSandrine Bailleux 	 * though only a fraction of the information contained in the
245*4c117f6cSSandrine Bailleux 	 * bl31_params_t structure makes sense in the context of EL3 payloads.
246*4c117f6cSSandrine Bailleux 	 * This will be refined in the future.
247*4c117f6cSSandrine Bailleux 	 */
248*4c117f6cSSandrine Bailleux 	VERBOSE("BL2: Populating the entrypoint info for the EL3 payload\n");
249*4c117f6cSSandrine Bailleux 	bl31_ep_info->pc = EL3_PAYLOAD_BASE;
250*4c117f6cSSandrine Bailleux 	bl31_ep_info->args.arg0 = (unsigned long) bl2_to_bl31_params;
251*4c117f6cSSandrine Bailleux 	bl2_plat_set_bl31_ep_info(NULL, bl31_ep_info);
252*4c117f6cSSandrine Bailleux #else
25393d81d64SSandrine Bailleux 	e = load_bl31(bl2_to_bl31_params, bl31_ep_info);
25493d81d64SSandrine Bailleux 	if (e) {
25593d81d64SSandrine Bailleux 		ERROR("Failed to load BL3-1 (%i)\n", e);
25640fc6cd1SJuan Castillo 		plat_error_handler(e);
25793d81d64SSandrine Bailleux 	}
25893d81d64SSandrine Bailleux 
25993d81d64SSandrine Bailleux 	e = load_bl32(bl2_to_bl31_params);
260fedbc049SJuan Castillo 	if (e) {
26178460a05SJuan Castillo 		if (e == -EAUTH) {
262fedbc049SJuan Castillo 			ERROR("Failed to authenticate BL3-2\n");
26340fc6cd1SJuan Castillo 			plat_error_handler(e);
264fedbc049SJuan Castillo 		} else {
26593d81d64SSandrine Bailleux 			WARN("Failed to load BL3-2 (%i)\n", e);
266fedbc049SJuan Castillo 		}
267fedbc049SJuan Castillo 	}
26893d81d64SSandrine Bailleux 
26993d81d64SSandrine Bailleux 	e = load_bl33(bl2_to_bl31_params);
27093d81d64SSandrine Bailleux 	if (e) {
27193d81d64SSandrine Bailleux 		ERROR("Failed to load BL3-3 (%i)\n", e);
27240fc6cd1SJuan Castillo 		plat_error_handler(e);
27393d81d64SSandrine Bailleux 	}
274*4c117f6cSSandrine Bailleux #endif /* EL3_PAYLOAD_BASE */
27593d81d64SSandrine Bailleux 
27603462671SAndrew Thoelke 	/* Flush the params to be passed to memory */
27703462671SAndrew Thoelke 	bl2_plat_flush_bl31_params();
27803462671SAndrew Thoelke 
2794f6ad66aSAchin Gupta 	/*
28093d81d64SSandrine Bailleux 	 * Run BL3-1 via an SMC to BL1. Information on how to pass control to
28193d81d64SSandrine Bailleux 	 * the BL3-2 (if present) and BL3-3 software images will be passed to
28293d81d64SSandrine Bailleux 	 * BL3-1 as an argument.
2834f6ad66aSAchin Gupta 	 */
28403462671SAndrew Thoelke 	smc(RUN_IMAGE, (unsigned long)bl31_ep_info, 0, 0, 0, 0, 0, 0);
2854f6ad66aSAchin Gupta }
286