xref: /rk3399_ARM-atf/bl2/bl2_main.c (revision 48bfb88eb6087bb3a293a13a0f702a0e40466b14)
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>
35*48bfb88eSYatharth Kochar #include <bl1.h>
364f6ad66aSAchin Gupta #include <bl_common.h>
3735e98e55SDan Handley #include <debug.h>
3878460a05SJuan Castillo #include <errno.h>
3997043ac9SDan Handley #include <platform.h>
405f0cdb05SDan Handley #include <platform_def.h>
4116948ae1SJuan Castillo #include <stdint.h>
425b827a8fSDan Handley #include "bl2_private.h"
434f6ad66aSAchin Gupta 
4493d81d64SSandrine Bailleux /*******************************************************************************
4593d81d64SSandrine Bailleux  * Load the BL3-0 image if there's one.
4693d81d64SSandrine Bailleux  * If a platform does not want to attempt to load BL3-0 image it must leave
4793d81d64SSandrine Bailleux  * BL30_BASE undefined.
4893d81d64SSandrine Bailleux  * Return 0 on success or if there's no BL3-0 image to load, a negative error
4993d81d64SSandrine Bailleux  * code otherwise.
5093d81d64SSandrine Bailleux  ******************************************************************************/
5193d81d64SSandrine Bailleux static int load_bl30(void)
5293d81d64SSandrine Bailleux {
5393d81d64SSandrine Bailleux 	int e = 0;
5493d81d64SSandrine Bailleux #ifdef BL30_BASE
5593d81d64SSandrine Bailleux 	meminfo_t bl30_mem_info;
5693d81d64SSandrine Bailleux 	image_info_t bl30_image_info;
5793d81d64SSandrine Bailleux 
5893d81d64SSandrine Bailleux 	/*
5993d81d64SSandrine Bailleux 	 * It is up to the platform to specify where BL3-0 should be loaded if
6093d81d64SSandrine Bailleux 	 * it exists. It could create space in the secure sram or point to a
6193d81d64SSandrine Bailleux 	 * completely different memory.
6293d81d64SSandrine Bailleux 	 *
6393d81d64SSandrine Bailleux 	 * The entry point information is not relevant in this case as the AP
6493d81d64SSandrine Bailleux 	 * won't execute the BL3-0 image.
6593d81d64SSandrine Bailleux 	 */
666ad2e461SDan Handley 	INFO("BL2: Loading BL3-0\n");
6793d81d64SSandrine Bailleux 	bl2_plat_get_bl30_meminfo(&bl30_mem_info);
6892de3565SJuan Castillo 	bl30_image_info.h.version = VERSION_1;
691779ba6bSJuan Castillo 	e = load_auth_image(&bl30_mem_info,
7016948ae1SJuan Castillo 			    BL30_IMAGE_ID,
7193d81d64SSandrine Bailleux 			    BL30_BASE,
7293d81d64SSandrine Bailleux 			    &bl30_image_info,
7393d81d64SSandrine Bailleux 			    NULL);
7493d81d64SSandrine Bailleux 
751779ba6bSJuan Castillo 	if (e == 0) {
7693d81d64SSandrine Bailleux 		/* The subsequent handling of BL3-0 is platform specific */
77bcb79b90SSandrine Bailleux 		e = bl2_plat_handle_bl30(&bl30_image_info);
78bcb79b90SSandrine Bailleux 		if (e) {
79bcb79b90SSandrine Bailleux 			ERROR("Failure in platform-specific handling of BL3-0 image.\n");
801779ba6bSJuan Castillo 		}
8193d81d64SSandrine Bailleux 	}
8293d81d64SSandrine Bailleux #endif /* BL30_BASE */
8393d81d64SSandrine Bailleux 
8493d81d64SSandrine Bailleux 	return e;
8593d81d64SSandrine Bailleux }
8629fb905dSVikram Kanigiri 
874c117f6cSSandrine Bailleux #ifndef EL3_PAYLOAD_BASE
8829fb905dSVikram Kanigiri /*******************************************************************************
8993d81d64SSandrine Bailleux  * Load the BL3-1 image.
9093d81d64SSandrine Bailleux  * The bl2_to_bl31_params and bl31_ep_info params will be updated with the
9193d81d64SSandrine Bailleux  * relevant BL3-1 information.
9293d81d64SSandrine Bailleux  * Return 0 on success, a negative error code otherwise.
934f6ad66aSAchin Gupta  ******************************************************************************/
9493d81d64SSandrine Bailleux static int load_bl31(bl31_params_t *bl2_to_bl31_params,
9593d81d64SSandrine Bailleux 		     entry_point_info_t *bl31_ep_info)
964f6ad66aSAchin Gupta {
97fb037bfbSDan Handley 	meminfo_t *bl2_tzram_layout;
984112bfa0SVikram Kanigiri 	int e;
994f6ad66aSAchin Gupta 
1006ad2e461SDan Handley 	INFO("BL2: Loading BL3-1\n");
10193d81d64SSandrine Bailleux 	assert(bl2_to_bl31_params != NULL);
10293d81d64SSandrine Bailleux 	assert(bl31_ep_info != NULL);
1034f6ad66aSAchin Gupta 
1044f6ad66aSAchin Gupta 	/* Find out how much free trusted ram remains after BL2 load */
105ee12f6f7SSandrine Bailleux 	bl2_tzram_layout = bl2_plat_sec_mem_layout();
1064f6ad66aSAchin Gupta 
10793d81d64SSandrine Bailleux 	/* Set the X0 parameter to BL3-1 */
10803462671SAndrew Thoelke 	bl31_ep_info->args.arg0 = (unsigned long)bl2_to_bl31_params;
10903462671SAndrew Thoelke 
1108f55dfb4SSandrine Bailleux 	/* Load the BL3-1 image */
1111779ba6bSJuan Castillo 	e = load_auth_image(bl2_tzram_layout,
11216948ae1SJuan Castillo 			    BL31_IMAGE_ID,
1134112bfa0SVikram Kanigiri 			    BL31_BASE,
1144112bfa0SVikram Kanigiri 			    bl2_to_bl31_params->bl31_image_info,
1154112bfa0SVikram Kanigiri 			    bl31_ep_info);
1164f6ad66aSAchin Gupta 
1171779ba6bSJuan Castillo 	if (e == 0) {
1184112bfa0SVikram Kanigiri 		bl2_plat_set_bl31_ep_info(bl2_to_bl31_params->bl31_image_info,
1194112bfa0SVikram Kanigiri 					  bl31_ep_info);
1201779ba6bSJuan Castillo 	}
121a3050ed5SAchin Gupta 
12293d81d64SSandrine Bailleux 	return e;
123561cd33eSHarry Liebel }
124e4d084eaSAchin Gupta 
12593d81d64SSandrine Bailleux /*******************************************************************************
12693d81d64SSandrine Bailleux  * Load the BL3-2 image if there's one.
12793d81d64SSandrine Bailleux  * The bl2_to_bl31_params param will be updated with the relevant BL3-2
12893d81d64SSandrine Bailleux  * information.
12993d81d64SSandrine Bailleux  * If a platform does not want to attempt to load BL3-2 image it must leave
13093d81d64SSandrine Bailleux  * BL32_BASE undefined.
13193d81d64SSandrine Bailleux  * Return 0 on success or if there's no BL3-2 image to load, a negative error
13293d81d64SSandrine Bailleux  * code otherwise.
13393d81d64SSandrine Bailleux  ******************************************************************************/
13493d81d64SSandrine Bailleux static int load_bl32(bl31_params_t *bl2_to_bl31_params)
13593d81d64SSandrine Bailleux {
13693d81d64SSandrine Bailleux 	int e = 0;
1371151c821SDan Handley #ifdef BL32_BASE
13893d81d64SSandrine Bailleux 	meminfo_t bl32_mem_info;
13993d81d64SSandrine Bailleux 
1406ad2e461SDan Handley 	INFO("BL2: Loading BL3-2\n");
14193d81d64SSandrine Bailleux 	assert(bl2_to_bl31_params != NULL);
14293d81d64SSandrine Bailleux 
14329fb905dSVikram Kanigiri 	/*
14493d81d64SSandrine Bailleux 	 * It is up to the platform to specify where BL3-2 should be loaded if
14593d81d64SSandrine Bailleux 	 * it exists. It could create space in the secure sram or point to a
1461151c821SDan Handley 	 * completely different memory.
14729fb905dSVikram Kanigiri 	 */
1486871c5d3SVikram Kanigiri 	bl2_plat_get_bl32_meminfo(&bl32_mem_info);
1491779ba6bSJuan Castillo 	e = load_auth_image(&bl32_mem_info,
15016948ae1SJuan Castillo 			    BL32_IMAGE_ID,
1514112bfa0SVikram Kanigiri 			    BL32_BASE,
1524112bfa0SVikram Kanigiri 			    bl2_to_bl31_params->bl32_image_info,
1534112bfa0SVikram Kanigiri 			    bl2_to_bl31_params->bl32_ep_info);
15429fb905dSVikram Kanigiri 
1551779ba6bSJuan Castillo 	if (e == 0) {
1564112bfa0SVikram Kanigiri 		bl2_plat_set_bl32_ep_info(
1574112bfa0SVikram Kanigiri 			bl2_to_bl31_params->bl32_image_info,
1584112bfa0SVikram Kanigiri 			bl2_to_bl31_params->bl32_ep_info);
1591779ba6bSJuan Castillo 	}
1601151c821SDan Handley #endif /* BL32_BASE */
1614112bfa0SVikram Kanigiri 
16293d81d64SSandrine Bailleux 	return e;
16393d81d64SSandrine Bailleux }
16493d81d64SSandrine Bailleux 
16593d81d64SSandrine Bailleux /*******************************************************************************
16693d81d64SSandrine Bailleux  * Load the BL3-3 image.
16793d81d64SSandrine Bailleux  * The bl2_to_bl31_params param will be updated with the relevant BL3-3
16893d81d64SSandrine Bailleux  * information.
16993d81d64SSandrine Bailleux  * Return 0 on success, a negative error code otherwise.
17093d81d64SSandrine Bailleux  ******************************************************************************/
17193d81d64SSandrine Bailleux static int load_bl33(bl31_params_t *bl2_to_bl31_params)
17293d81d64SSandrine Bailleux {
17393d81d64SSandrine Bailleux 	meminfo_t bl33_mem_info;
17493d81d64SSandrine Bailleux 	int e;
17593d81d64SSandrine Bailleux 
1766ad2e461SDan Handley 	INFO("BL2: Loading BL3-3\n");
17793d81d64SSandrine Bailleux 	assert(bl2_to_bl31_params != NULL);
17893d81d64SSandrine Bailleux 
17993d81d64SSandrine Bailleux 	bl2_plat_get_bl33_meminfo(&bl33_mem_info);
18093d81d64SSandrine Bailleux 
18193d81d64SSandrine Bailleux 	/* Load the BL3-3 image in non-secure memory provided by the platform */
1821779ba6bSJuan Castillo 	e = load_auth_image(&bl33_mem_info,
18316948ae1SJuan Castillo 			    BL33_IMAGE_ID,
18493d81d64SSandrine Bailleux 			    plat_get_ns_image_entrypoint(),
18593d81d64SSandrine Bailleux 			    bl2_to_bl31_params->bl33_image_info,
18693d81d64SSandrine Bailleux 			    bl2_to_bl31_params->bl33_ep_info);
18793d81d64SSandrine Bailleux 
1881779ba6bSJuan Castillo 	if (e == 0) {
18993d81d64SSandrine Bailleux 		bl2_plat_set_bl33_ep_info(bl2_to_bl31_params->bl33_image_info,
19093d81d64SSandrine Bailleux 					  bl2_to_bl31_params->bl33_ep_info);
1911779ba6bSJuan Castillo 	}
19293d81d64SSandrine Bailleux 
19393d81d64SSandrine Bailleux 	return e;
19493d81d64SSandrine Bailleux }
1954c117f6cSSandrine Bailleux #endif /* EL3_PAYLOAD_BASE */
19693d81d64SSandrine Bailleux 
19793d81d64SSandrine Bailleux /*******************************************************************************
19893d81d64SSandrine Bailleux  * The only thing to do in BL2 is to load further images and pass control to
19993d81d64SSandrine Bailleux  * BL3-1. The memory occupied by BL2 will be reclaimed by BL3-x stages. BL2 runs
20093d81d64SSandrine Bailleux  * entirely in S-EL1.
20193d81d64SSandrine Bailleux  ******************************************************************************/
20293d81d64SSandrine Bailleux void bl2_main(void)
20393d81d64SSandrine Bailleux {
20493d81d64SSandrine Bailleux 	bl31_params_t *bl2_to_bl31_params;
20593d81d64SSandrine Bailleux 	entry_point_info_t *bl31_ep_info;
20693d81d64SSandrine Bailleux 	int e;
20793d81d64SSandrine Bailleux 
2086ad2e461SDan Handley 	NOTICE("BL2: %s\n", version_string);
2096ad2e461SDan Handley 	NOTICE("BL2: %s\n", build_message);
2106ad2e461SDan Handley 
21193d81d64SSandrine Bailleux 	/* Perform remaining generic architectural setup in S-EL1 */
21293d81d64SSandrine Bailleux 	bl2_arch_setup();
21393d81d64SSandrine Bailleux 
214dec840afSJuan Castillo #if TRUSTED_BOARD_BOOT
215dec840afSJuan Castillo 	/* Initialize authentication module */
2161779ba6bSJuan Castillo 	auth_mod_init();
217dec840afSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */
218dec840afSJuan Castillo 
21993d81d64SSandrine Bailleux 	/*
22093d81d64SSandrine Bailleux 	 * Load the subsequent bootloader images
22193d81d64SSandrine Bailleux 	 */
22293d81d64SSandrine Bailleux 	e = load_bl30();
22393d81d64SSandrine Bailleux 	if (e) {
22493d81d64SSandrine Bailleux 		ERROR("Failed to load BL3-0 (%i)\n", e);
22540fc6cd1SJuan Castillo 		plat_error_handler(e);
22693d81d64SSandrine Bailleux 	}
22793d81d64SSandrine Bailleux 
228ef538c6fSJuan Castillo 	/* Perform platform setup in BL2 after loading BL3-0 */
229ef538c6fSJuan Castillo 	bl2_platform_setup();
230ef538c6fSJuan Castillo 
23193d81d64SSandrine Bailleux 	/*
23293d81d64SSandrine Bailleux 	 * Get a pointer to the memory the platform has set aside to pass
23393d81d64SSandrine Bailleux 	 * information to BL3-1.
23493d81d64SSandrine Bailleux 	 */
23593d81d64SSandrine Bailleux 	bl2_to_bl31_params = bl2_plat_get_bl31_params();
23693d81d64SSandrine Bailleux 	bl31_ep_info = bl2_plat_get_bl31_ep_info();
23793d81d64SSandrine Bailleux 
2384c117f6cSSandrine Bailleux #ifdef EL3_PAYLOAD_BASE
2394c117f6cSSandrine Bailleux 	/*
2404c117f6cSSandrine Bailleux 	 * In the case of an EL3 payload, we don't need to load any further
2414c117f6cSSandrine Bailleux 	 * images. Just update the BL31 entrypoint info structure to make BL1
2424c117f6cSSandrine Bailleux 	 * jump to the EL3 payload.
2434c117f6cSSandrine Bailleux 	 * The pointer to the memory the platform has set aside to pass
2444c117f6cSSandrine Bailleux 	 * information to BL3-1 in the normal boot flow is reused here, even
2454c117f6cSSandrine Bailleux 	 * though only a fraction of the information contained in the
2464c117f6cSSandrine Bailleux 	 * bl31_params_t structure makes sense in the context of EL3 payloads.
2474c117f6cSSandrine Bailleux 	 * This will be refined in the future.
2484c117f6cSSandrine Bailleux 	 */
2494c117f6cSSandrine Bailleux 	VERBOSE("BL2: Populating the entrypoint info for the EL3 payload\n");
2504c117f6cSSandrine Bailleux 	bl31_ep_info->pc = EL3_PAYLOAD_BASE;
2514c117f6cSSandrine Bailleux 	bl31_ep_info->args.arg0 = (unsigned long) bl2_to_bl31_params;
2524c117f6cSSandrine Bailleux 	bl2_plat_set_bl31_ep_info(NULL, bl31_ep_info);
2534c117f6cSSandrine Bailleux #else
25493d81d64SSandrine Bailleux 	e = load_bl31(bl2_to_bl31_params, bl31_ep_info);
25593d81d64SSandrine Bailleux 	if (e) {
25693d81d64SSandrine Bailleux 		ERROR("Failed to load BL3-1 (%i)\n", e);
25740fc6cd1SJuan Castillo 		plat_error_handler(e);
25893d81d64SSandrine Bailleux 	}
25993d81d64SSandrine Bailleux 
26093d81d64SSandrine Bailleux 	e = load_bl32(bl2_to_bl31_params);
261fedbc049SJuan Castillo 	if (e) {
26278460a05SJuan Castillo 		if (e == -EAUTH) {
263fedbc049SJuan Castillo 			ERROR("Failed to authenticate BL3-2\n");
26440fc6cd1SJuan Castillo 			plat_error_handler(e);
265fedbc049SJuan Castillo 		} else {
26693d81d64SSandrine Bailleux 			WARN("Failed to load BL3-2 (%i)\n", e);
267fedbc049SJuan Castillo 		}
268fedbc049SJuan Castillo 	}
26993d81d64SSandrine Bailleux 
27093d81d64SSandrine Bailleux 	e = load_bl33(bl2_to_bl31_params);
27193d81d64SSandrine Bailleux 	if (e) {
27293d81d64SSandrine Bailleux 		ERROR("Failed to load BL3-3 (%i)\n", e);
27340fc6cd1SJuan Castillo 		plat_error_handler(e);
27493d81d64SSandrine Bailleux 	}
2754c117f6cSSandrine Bailleux #endif /* EL3_PAYLOAD_BASE */
27693d81d64SSandrine Bailleux 
27703462671SAndrew Thoelke 	/* Flush the params to be passed to memory */
27803462671SAndrew Thoelke 	bl2_plat_flush_bl31_params();
27903462671SAndrew Thoelke 
2804f6ad66aSAchin Gupta 	/*
28193d81d64SSandrine Bailleux 	 * Run BL3-1 via an SMC to BL1. Information on how to pass control to
28293d81d64SSandrine Bailleux 	 * the BL3-2 (if present) and BL3-3 software images will be passed to
28393d81d64SSandrine Bailleux 	 * BL3-1 as an argument.
2844f6ad66aSAchin Gupta 	 */
285*48bfb88eSYatharth Kochar 	smc(BL1_SMC_RUN_IMAGE, (unsigned long)bl31_ep_info, 0, 0, 0, 0, 0, 0);
2864f6ad66aSAchin Gupta }
287