xref: /rk3399_ARM-atf/bl1/bl1_main.c (revision 566034fc27fc270b240efbef7a7dd9b0caf13c82)
14f6ad66aSAchin Gupta /*
211f001cbSMasahiro Yamada  * Copyright (c) 2013-2018, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
54f6ad66aSAchin Gupta  */
64f6ad66aSAchin Gupta 
797043ac9SDan Handley #include <arch.h>
84f6ad66aSAchin Gupta #include <arch_helpers.h>
997043ac9SDan Handley #include <assert.h>
101779ba6bSJuan Castillo #include <auth_mod.h>
1148bfb88eSYatharth Kochar #include <bl1.h>
1297043ac9SDan Handley #include <bl_common.h>
130b32628eSAntonio Nino Diaz #include <console.h>
144112bfa0SVikram Kanigiri #include <debug.h>
1510bcd761SJeenu Viswambharan #include <errata_report.h>
1697043ac9SDan Handley #include <platform.h>
175f0cdb05SDan Handley #include <platform_def.h>
1848bfb88eSYatharth Kochar #include <smcc_helpers.h>
19c45f627dSSoby Mathew #include <utils.h>
2048bfb88eSYatharth Kochar #include <uuid.h>
212a4b4b71SIsla Mitchell #include "bl1_private.h"
2248bfb88eSYatharth Kochar 
2348bfb88eSYatharth Kochar /* BL1 Service UUID */
2448bfb88eSYatharth Kochar DEFINE_SVC_UUID(bl1_svc_uid,
2548bfb88eSYatharth Kochar 	0xfd3967d4, 0x72cb, 0x4d9a, 0xb5, 0x75,
2648bfb88eSYatharth Kochar 	0x67, 0x15, 0xd6, 0xf4, 0xbb, 0x4a);
274f6ad66aSAchin Gupta 
28a2f8b166SVikram Kanigiri 
297baff11fSYatharth Kochar static void bl1_load_bl2(void);
3029fb905dSVikram Kanigiri 
318f55dfb4SSandrine Bailleux /*******************************************************************************
328f55dfb4SSandrine Bailleux  * The next function has a weak definition. Platform specific code can override
338f55dfb4SSandrine Bailleux  * it if it wishes to.
348f55dfb4SSandrine Bailleux  ******************************************************************************/
358f55dfb4SSandrine Bailleux #pragma weak bl1_init_bl2_mem_layout
368f55dfb4SSandrine Bailleux 
378f55dfb4SSandrine Bailleux /*******************************************************************************
388f55dfb4SSandrine Bailleux  * Function that takes a memory layout into which BL2 has been loaded and
398f55dfb4SSandrine Bailleux  * populates a new memory layout for BL2 that ensures that BL1's data sections
408f55dfb4SSandrine Bailleux  * resident in secure RAM are not visible to BL2.
418f55dfb4SSandrine Bailleux  ******************************************************************************/
428f55dfb4SSandrine Bailleux void bl1_init_bl2_mem_layout(const meminfo_t *bl1_mem_layout,
438f55dfb4SSandrine Bailleux 			     meminfo_t *bl2_mem_layout)
448f55dfb4SSandrine Bailleux {
458f55dfb4SSandrine Bailleux 
468f55dfb4SSandrine Bailleux 	assert(bl1_mem_layout != NULL);
478f55dfb4SSandrine Bailleux 	assert(bl2_mem_layout != NULL);
488f55dfb4SSandrine Bailleux 
4942019bf4SYatharth Kochar #if LOAD_IMAGE_V2
5042019bf4SYatharth Kochar 	/*
5142019bf4SYatharth Kochar 	 * Remove BL1 RW data from the scope of memory visible to BL2.
5242019bf4SYatharth Kochar 	 * This is assuming BL1 RW data is at the top of bl1_mem_layout.
5342019bf4SYatharth Kochar 	 */
5442019bf4SYatharth Kochar 	assert(BL1_RW_BASE > bl1_mem_layout->total_base);
5542019bf4SYatharth Kochar 	bl2_mem_layout->total_base = bl1_mem_layout->total_base;
5642019bf4SYatharth Kochar 	bl2_mem_layout->total_size = BL1_RW_BASE - bl1_mem_layout->total_base;
5742019bf4SYatharth Kochar #else
588f55dfb4SSandrine Bailleux 	/* Check that BL1's memory is lying outside of the free memory */
598f55dfb4SSandrine Bailleux 	assert((BL1_RAM_LIMIT <= bl1_mem_layout->free_base) ||
607baff11fSYatharth Kochar 	       (BL1_RAM_BASE >= bl1_mem_layout->free_base +
617baff11fSYatharth Kochar 				bl1_mem_layout->free_size));
628f55dfb4SSandrine Bailleux 
638f55dfb4SSandrine Bailleux 	/* Remove BL1 RW data from the scope of memory visible to BL2 */
648f55dfb4SSandrine Bailleux 	*bl2_mem_layout = *bl1_mem_layout;
658f55dfb4SSandrine Bailleux 	reserve_mem(&bl2_mem_layout->total_base,
668f55dfb4SSandrine Bailleux 		    &bl2_mem_layout->total_size,
678f55dfb4SSandrine Bailleux 		    BL1_RAM_BASE,
6842019bf4SYatharth Kochar 		    BL1_RAM_LIMIT - BL1_RAM_BASE);
6942019bf4SYatharth Kochar #endif /* LOAD_IMAGE_V2 */
708f55dfb4SSandrine Bailleux 
718f55dfb4SSandrine Bailleux 	flush_dcache_range((unsigned long)bl2_mem_layout, sizeof(meminfo_t));
728f55dfb4SSandrine Bailleux }
7329fb905dSVikram Kanigiri 
7429fb905dSVikram Kanigiri /*******************************************************************************
754f6ad66aSAchin Gupta  * Function to perform late architectural and platform specific initialization.
767baff11fSYatharth Kochar  * It also queries the platform to load and run next BL image. Only called
777baff11fSYatharth Kochar  * by the primary cpu after a cold boot.
784f6ad66aSAchin Gupta  ******************************************************************************/
794f6ad66aSAchin Gupta void bl1_main(void)
804f6ad66aSAchin Gupta {
817baff11fSYatharth Kochar 	unsigned int image_id;
827baff11fSYatharth Kochar 
836ad2e461SDan Handley 	/* Announce our arrival */
846ad2e461SDan Handley 	NOTICE(FIRMWARE_WELCOME_STR);
856ad2e461SDan Handley 	NOTICE("BL1: %s\n", version_string);
866ad2e461SDan Handley 	NOTICE("BL1: %s\n", build_message);
876ad2e461SDan Handley 
88f3b4914bSYatharth Kochar 	INFO("BL1: RAM %p - %p\n", (void *)BL1_RAM_BASE,
89f3b4914bSYatharth Kochar 					(void *)BL1_RAM_LIMIT);
906ad2e461SDan Handley 
9110bcd761SJeenu Viswambharan 	print_errata_status();
924f6ad66aSAchin Gupta 
93aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS
94f3b4914bSYatharth Kochar 	u_register_t val;
954f6ad66aSAchin Gupta 	/*
964f6ad66aSAchin Gupta 	 * Ensure that MMU/Caches and coherency are turned on
974f6ad66aSAchin Gupta 	 */
98f3b4914bSYatharth Kochar #ifdef AARCH32
99f3b4914bSYatharth Kochar 	val = read_sctlr();
100f3b4914bSYatharth Kochar #else
101ce4c820dSDan Handley 	val = read_sctlr_el3();
102f3b4914bSYatharth Kochar #endif
103354ab57dSAndrew Thoelke 	assert(val & SCTLR_M_BIT);
104354ab57dSAndrew Thoelke 	assert(val & SCTLR_C_BIT);
105354ab57dSAndrew Thoelke 	assert(val & SCTLR_I_BIT);
106ce4c820dSDan Handley 	/*
107ce4c820dSDan Handley 	 * Check that Cache Writeback Granule (CWG) in CTR_EL0 matches the
108ce4c820dSDan Handley 	 * provided platform value
109ce4c820dSDan Handley 	 */
110ce4c820dSDan Handley 	val = (read_ctr_el0() >> CTR_CWG_SHIFT) & CTR_CWG_MASK;
111ce4c820dSDan Handley 	/*
112ce4c820dSDan Handley 	 * If CWG is zero, then no CWG information is available but we can
113ce4c820dSDan Handley 	 * at least check the platform value is less than the architectural
114ce4c820dSDan Handley 	 * maximum.
115ce4c820dSDan Handley 	 */
116ce4c820dSDan Handley 	if (val != 0)
117ce4c820dSDan Handley 		assert(CACHE_WRITEBACK_GRANULE == SIZE_FROM_LOG2_WORDS(val));
118ce4c820dSDan Handley 	else
119ce4c820dSDan Handley 		assert(CACHE_WRITEBACK_GRANULE <= MAX_CACHE_LINE_SIZE);
120aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */
1214f6ad66aSAchin Gupta 
1224f6ad66aSAchin Gupta 	/* Perform remaining generic architectural setup from EL3 */
1234f6ad66aSAchin Gupta 	bl1_arch_setup();
1244f6ad66aSAchin Gupta 
1257baff11fSYatharth Kochar #if TRUSTED_BOARD_BOOT
1267baff11fSYatharth Kochar 	/* Initialize authentication module */
1277baff11fSYatharth Kochar 	auth_mod_init();
1287baff11fSYatharth Kochar #endif /* TRUSTED_BOARD_BOOT */
1297baff11fSYatharth Kochar 
1304f6ad66aSAchin Gupta 	/* Perform platform setup in BL1. */
1314f6ad66aSAchin Gupta 	bl1_platform_setup();
1324f6ad66aSAchin Gupta 
1337baff11fSYatharth Kochar 	/* Get the image id of next image to load and run. */
1347baff11fSYatharth Kochar 	image_id = bl1_plat_get_next_image_id();
1357baff11fSYatharth Kochar 
13648bfb88eSYatharth Kochar 	/*
13748bfb88eSYatharth Kochar 	 * We currently interpret any image id other than
13848bfb88eSYatharth Kochar 	 * BL2_IMAGE_ID as the start of firmware update.
13948bfb88eSYatharth Kochar 	 */
1407baff11fSYatharth Kochar 	if (image_id == BL2_IMAGE_ID)
1417baff11fSYatharth Kochar 		bl1_load_bl2();
14248bfb88eSYatharth Kochar 	else
14348bfb88eSYatharth Kochar 		NOTICE("BL1-FWU: *******FWU Process Started*******\n");
1447baff11fSYatharth Kochar 
1457baff11fSYatharth Kochar 	bl1_prepare_next_image(image_id);
1460b32628eSAntonio Nino Diaz 
1470b32628eSAntonio Nino Diaz 	console_flush();
1487baff11fSYatharth Kochar }
1497baff11fSYatharth Kochar 
1507baff11fSYatharth Kochar /*******************************************************************************
1517baff11fSYatharth Kochar  * This function locates and loads the BL2 raw binary image in the trusted SRAM.
1527baff11fSYatharth Kochar  * Called by the primary cpu after a cold boot.
1537baff11fSYatharth Kochar  * TODO: Add support for alternative image load mechanism e.g using virtio/elf
1547baff11fSYatharth Kochar  * loader etc.
1557baff11fSYatharth Kochar  ******************************************************************************/
1567baff11fSYatharth Kochar void bl1_load_bl2(void)
1577baff11fSYatharth Kochar {
1587baff11fSYatharth Kochar 	image_desc_t *image_desc;
1597baff11fSYatharth Kochar 	image_info_t *image_info;
1607baff11fSYatharth Kochar 	entry_point_info_t *ep_info;
1617baff11fSYatharth Kochar 	meminfo_t *bl1_tzram_layout;
1627baff11fSYatharth Kochar 	meminfo_t *bl2_tzram_layout;
1637baff11fSYatharth Kochar 	int err;
1647baff11fSYatharth Kochar 
1657baff11fSYatharth Kochar 	/* Get the image descriptor */
1667baff11fSYatharth Kochar 	image_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
1677baff11fSYatharth Kochar 	assert(image_desc);
1687baff11fSYatharth Kochar 
1697baff11fSYatharth Kochar 	/* Get the image info */
1707baff11fSYatharth Kochar 	image_info = &image_desc->image_info;
1717baff11fSYatharth Kochar 
1727baff11fSYatharth Kochar 	/* Get the entry point info */
1737baff11fSYatharth Kochar 	ep_info = &image_desc->ep_info;
1744112bfa0SVikram Kanigiri 
1758f55dfb4SSandrine Bailleux 	/* Find out how much free trusted ram remains after BL1 load */
176ee12f6f7SSandrine Bailleux 	bl1_tzram_layout = bl1_plat_sec_mem_layout();
1778f55dfb4SSandrine Bailleux 
17816948ae1SJuan Castillo 	INFO("BL1: Loading BL2\n");
17916948ae1SJuan Castillo 
180*566034fcSSoby Mathew 	err = bl1_plat_handle_pre_image_load(BL2_IMAGE_ID);
18111f001cbSMasahiro Yamada 	if (err) {
18211f001cbSMasahiro Yamada 		ERROR("Failure in pre image load handling of BL2 (%d)\n", err);
18311f001cbSMasahiro Yamada 		plat_error_handler(err);
18411f001cbSMasahiro Yamada 	}
18511f001cbSMasahiro Yamada 
186*566034fcSSoby Mathew #if LOAD_IMAGE_V2
18742019bf4SYatharth Kochar 	err = load_auth_image(BL2_IMAGE_ID, image_info);
18842019bf4SYatharth Kochar #else
1898f55dfb4SSandrine Bailleux 	/* Load the BL2 image */
1901779ba6bSJuan Castillo 	err = load_auth_image(bl1_tzram_layout,
19116948ae1SJuan Castillo 			 BL2_IMAGE_ID,
1927baff11fSYatharth Kochar 			 image_info->image_base,
1937baff11fSYatharth Kochar 			 image_info,
1947baff11fSYatharth Kochar 			 ep_info);
1951779ba6bSJuan Castillo 
19642019bf4SYatharth Kochar #endif /* LOAD_IMAGE_V2 */
19742019bf4SYatharth Kochar 
1984112bfa0SVikram Kanigiri 	if (err) {
1996ad2e461SDan Handley 		ERROR("Failed to load BL2 firmware.\n");
20040fc6cd1SJuan Castillo 		plat_error_handler(err);
2014112bfa0SVikram Kanigiri 	}
20201df3c14SJuan Castillo 
20311f001cbSMasahiro Yamada 	/* Allow platform to handle image information. */
204*566034fcSSoby Mathew 	err = bl1_plat_handle_post_image_load(BL2_IMAGE_ID);
20511f001cbSMasahiro Yamada 	if (err) {
20611f001cbSMasahiro Yamada 		ERROR("Failure in post image load handling of BL2 (%d)\n", err);
20711f001cbSMasahiro Yamada 		plat_error_handler(err);
20811f001cbSMasahiro Yamada 	}
20911f001cbSMasahiro Yamada 
210*566034fcSSoby Mathew #if LOAD_IMAGE_V2
2114f6ad66aSAchin Gupta 	/*
2124f6ad66aSAchin Gupta 	 * Create a new layout of memory for BL2 as seen by BL1 i.e.
2134f6ad66aSAchin Gupta 	 * tell it the amount of total and free memory available.
2144f6ad66aSAchin Gupta 	 * This layout is created at the first free address visible
2154f6ad66aSAchin Gupta 	 * to BL2. BL2 will read the memory layout before using its
2164f6ad66aSAchin Gupta 	 * memory for other purposes.
2174f6ad66aSAchin Gupta 	 */
21842019bf4SYatharth Kochar 	bl2_tzram_layout = (meminfo_t *) bl1_tzram_layout->total_base;
21942019bf4SYatharth Kochar #else
220fb037bfbSDan Handley 	bl2_tzram_layout = (meminfo_t *) bl1_tzram_layout->free_base;
22142019bf4SYatharth Kochar #endif /* LOAD_IMAGE_V2 */
22242019bf4SYatharth Kochar 
2238f55dfb4SSandrine Bailleux 	bl1_init_bl2_mem_layout(bl1_tzram_layout, bl2_tzram_layout);
2244f6ad66aSAchin Gupta 
225f3b4914bSYatharth Kochar 	ep_info->args.arg1 = (uintptr_t)bl2_tzram_layout;
2267baff11fSYatharth Kochar 	NOTICE("BL1: Booting BL2\n");
227f3b4914bSYatharth Kochar 	VERBOSE("BL1: BL2 memory layout address = %p\n",
228f3b4914bSYatharth Kochar 		(void *) bl2_tzram_layout);
2294f6ad66aSAchin Gupta }
2304f6ad66aSAchin Gupta 
2314f6ad66aSAchin Gupta /*******************************************************************************
232f3b4914bSYatharth Kochar  * Function called just before handing over to the next BL to inform the user
233f3b4914bSYatharth Kochar  * about the boot progress. In debug mode, also print details about the BL
234f3b4914bSYatharth Kochar  * image's execution context.
2354f6ad66aSAchin Gupta  ******************************************************************************/
236f3b4914bSYatharth Kochar void bl1_print_next_bl_ep_info(const entry_point_info_t *bl_ep_info)
2374f6ad66aSAchin Gupta {
238f3b4914bSYatharth Kochar #ifdef AARCH32
239f3b4914bSYatharth Kochar 	NOTICE("BL1: Booting BL32\n");
240f3b4914bSYatharth Kochar #else
241d178637dSJuan Castillo 	NOTICE("BL1: Booting BL31\n");
242f3b4914bSYatharth Kochar #endif /* AARCH32 */
243f3b4914bSYatharth Kochar 	print_entry_point_info(bl_ep_info);
2444f6ad66aSAchin Gupta }
24535e8c766SSandrine Bailleux 
24635e8c766SSandrine Bailleux #if SPIN_ON_BL1_EXIT
24735e8c766SSandrine Bailleux void print_debug_loop_message(void)
24835e8c766SSandrine Bailleux {
24935e8c766SSandrine Bailleux 	NOTICE("BL1: Debug loop, spinning forever\n");
25035e8c766SSandrine Bailleux 	NOTICE("BL1: Please connect the debugger to continue\n");
25135e8c766SSandrine Bailleux }
25235e8c766SSandrine Bailleux #endif
25348bfb88eSYatharth Kochar 
25448bfb88eSYatharth Kochar /*******************************************************************************
25548bfb88eSYatharth Kochar  * Top level handler for servicing BL1 SMCs.
25648bfb88eSYatharth Kochar  ******************************************************************************/
25748bfb88eSYatharth Kochar register_t bl1_smc_handler(unsigned int smc_fid,
25848bfb88eSYatharth Kochar 	register_t x1,
25948bfb88eSYatharth Kochar 	register_t x2,
26048bfb88eSYatharth Kochar 	register_t x3,
26148bfb88eSYatharth Kochar 	register_t x4,
26248bfb88eSYatharth Kochar 	void *cookie,
26348bfb88eSYatharth Kochar 	void *handle,
26448bfb88eSYatharth Kochar 	unsigned int flags)
26548bfb88eSYatharth Kochar {
26648bfb88eSYatharth Kochar 
26748bfb88eSYatharth Kochar #if TRUSTED_BOARD_BOOT
26848bfb88eSYatharth Kochar 	/*
26948bfb88eSYatharth Kochar 	 * Dispatch FWU calls to FWU SMC handler and return its return
27048bfb88eSYatharth Kochar 	 * value
27148bfb88eSYatharth Kochar 	 */
27248bfb88eSYatharth Kochar 	if (is_fwu_fid(smc_fid)) {
27348bfb88eSYatharth Kochar 		return bl1_fwu_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
27448bfb88eSYatharth Kochar 			handle, flags);
27548bfb88eSYatharth Kochar 	}
27648bfb88eSYatharth Kochar #endif
27748bfb88eSYatharth Kochar 
27848bfb88eSYatharth Kochar 	switch (smc_fid) {
27948bfb88eSYatharth Kochar 	case BL1_SMC_CALL_COUNT:
28048bfb88eSYatharth Kochar 		SMC_RET1(handle, BL1_NUM_SMC_CALLS);
28148bfb88eSYatharth Kochar 
28248bfb88eSYatharth Kochar 	case BL1_SMC_UID:
28348bfb88eSYatharth Kochar 		SMC_UUID_RET(handle, bl1_svc_uid);
28448bfb88eSYatharth Kochar 
28548bfb88eSYatharth Kochar 	case BL1_SMC_VERSION:
28648bfb88eSYatharth Kochar 		SMC_RET1(handle, BL1_SMC_MAJOR_VER | BL1_SMC_MINOR_VER);
28748bfb88eSYatharth Kochar 
28848bfb88eSYatharth Kochar 	default:
28948bfb88eSYatharth Kochar 		break;
29048bfb88eSYatharth Kochar 	}
29148bfb88eSYatharth Kochar 
29248bfb88eSYatharth Kochar 	WARN("Unimplemented BL1 SMC Call: 0x%x \n", smc_fid);
29348bfb88eSYatharth Kochar 	SMC_RET1(handle, SMC_UNK);
29448bfb88eSYatharth Kochar }
295a4409008Sdp-arm 
296a4409008Sdp-arm /*******************************************************************************
297a4409008Sdp-arm  * BL1 SMC wrapper.  This function is only used in AArch32 mode to ensure ABI
298a4409008Sdp-arm  * compliance when invoking bl1_smc_handler.
299a4409008Sdp-arm  ******************************************************************************/
300a4409008Sdp-arm register_t bl1_smc_wrapper(uint32_t smc_fid,
301a4409008Sdp-arm 	void *cookie,
302a4409008Sdp-arm 	void *handle,
303a4409008Sdp-arm 	unsigned int flags)
304a4409008Sdp-arm {
305a4409008Sdp-arm 	register_t x1, x2, x3, x4;
306a4409008Sdp-arm 
307a4409008Sdp-arm 	assert(handle);
308a4409008Sdp-arm 
309a4409008Sdp-arm 	get_smc_params_from_ctx(handle, x1, x2, x3, x4);
310a4409008Sdp-arm 	return bl1_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, flags);
311a4409008Sdp-arm }
312