xref: /rk3399_ARM-atf/bl1/bl1_main.c (revision 48bfb88eb6087bb3a293a13a0f702a0e40466b14)
14f6ad66aSAchin Gupta /*
2ce4c820dSDan Handley  * Copyright (c) 2013-2015, 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>
3697043ac9SDan Handley #include <bl_common.h>
374112bfa0SVikram Kanigiri #include <debug.h>
3897043ac9SDan Handley #include <platform.h>
395f0cdb05SDan Handley #include <platform_def.h>
40*48bfb88eSYatharth Kochar #include <smcc_helpers.h>
415b827a8fSDan Handley #include "bl1_private.h"
42*48bfb88eSYatharth Kochar #include <uuid.h>
43*48bfb88eSYatharth Kochar 
44*48bfb88eSYatharth Kochar /* BL1 Service UUID */
45*48bfb88eSYatharth Kochar DEFINE_SVC_UUID(bl1_svc_uid,
46*48bfb88eSYatharth Kochar 	0xfd3967d4, 0x72cb, 0x4d9a, 0xb5, 0x75,
47*48bfb88eSYatharth Kochar 	0x67, 0x15, 0xd6, 0xf4, 0xbb, 0x4a);
484f6ad66aSAchin Gupta 
49a2f8b166SVikram Kanigiri 
507baff11fSYatharth Kochar static void bl1_load_bl2(void);
5129fb905dSVikram Kanigiri 
528f55dfb4SSandrine Bailleux /*******************************************************************************
538f55dfb4SSandrine Bailleux  * The next function has a weak definition. Platform specific code can override
548f55dfb4SSandrine Bailleux  * it if it wishes to.
558f55dfb4SSandrine Bailleux  ******************************************************************************/
568f55dfb4SSandrine Bailleux #pragma weak bl1_init_bl2_mem_layout
578f55dfb4SSandrine Bailleux 
588f55dfb4SSandrine Bailleux /*******************************************************************************
598f55dfb4SSandrine Bailleux  * Function that takes a memory layout into which BL2 has been loaded and
608f55dfb4SSandrine Bailleux  * populates a new memory layout for BL2 that ensures that BL1's data sections
618f55dfb4SSandrine Bailleux  * resident in secure RAM are not visible to BL2.
628f55dfb4SSandrine Bailleux  ******************************************************************************/
638f55dfb4SSandrine Bailleux void bl1_init_bl2_mem_layout(const meminfo_t *bl1_mem_layout,
648f55dfb4SSandrine Bailleux 			     meminfo_t *bl2_mem_layout)
658f55dfb4SSandrine Bailleux {
668f55dfb4SSandrine Bailleux 	const size_t bl1_size = BL1_RAM_LIMIT - BL1_RAM_BASE;
678f55dfb4SSandrine Bailleux 
688f55dfb4SSandrine Bailleux 	assert(bl1_mem_layout != NULL);
698f55dfb4SSandrine Bailleux 	assert(bl2_mem_layout != NULL);
708f55dfb4SSandrine Bailleux 
718f55dfb4SSandrine Bailleux 	/* Check that BL1's memory is lying outside of the free memory */
728f55dfb4SSandrine Bailleux 	assert((BL1_RAM_LIMIT <= bl1_mem_layout->free_base) ||
737baff11fSYatharth Kochar 	       (BL1_RAM_BASE >= bl1_mem_layout->free_base +
747baff11fSYatharth Kochar 				bl1_mem_layout->free_size));
758f55dfb4SSandrine Bailleux 
768f55dfb4SSandrine Bailleux 	/* Remove BL1 RW data from the scope of memory visible to BL2 */
778f55dfb4SSandrine Bailleux 	*bl2_mem_layout = *bl1_mem_layout;
788f55dfb4SSandrine Bailleux 	reserve_mem(&bl2_mem_layout->total_base,
798f55dfb4SSandrine Bailleux 		    &bl2_mem_layout->total_size,
808f55dfb4SSandrine Bailleux 		    BL1_RAM_BASE,
818f55dfb4SSandrine Bailleux 		    bl1_size);
828f55dfb4SSandrine Bailleux 
838f55dfb4SSandrine Bailleux 	flush_dcache_range((unsigned long)bl2_mem_layout, sizeof(meminfo_t));
848f55dfb4SSandrine Bailleux }
8529fb905dSVikram Kanigiri 
8629fb905dSVikram Kanigiri /*******************************************************************************
874f6ad66aSAchin Gupta  * Function to perform late architectural and platform specific initialization.
887baff11fSYatharth Kochar  * It also queries the platform to load and run next BL image. Only called
897baff11fSYatharth Kochar  * by the primary cpu after a cold boot.
904f6ad66aSAchin Gupta  ******************************************************************************/
914f6ad66aSAchin Gupta void bl1_main(void)
924f6ad66aSAchin Gupta {
937baff11fSYatharth Kochar 	unsigned int image_id;
947baff11fSYatharth Kochar 
956ad2e461SDan Handley 	/* Announce our arrival */
966ad2e461SDan Handley 	NOTICE(FIRMWARE_WELCOME_STR);
976ad2e461SDan Handley 	NOTICE("BL1: %s\n", version_string);
986ad2e461SDan Handley 	NOTICE("BL1: %s\n", build_message);
996ad2e461SDan Handley 
1006ad2e461SDan Handley 	INFO("BL1: RAM 0x%lx - 0x%lx\n", BL1_RAM_BASE, BL1_RAM_LIMIT);
1016ad2e461SDan Handley 
1024f6ad66aSAchin Gupta 
103ce4c820dSDan Handley #if DEBUG
104ce4c820dSDan Handley 	unsigned long val;
1054f6ad66aSAchin Gupta 	/*
1064f6ad66aSAchin Gupta 	 * Ensure that MMU/Caches and coherency are turned on
1074f6ad66aSAchin Gupta 	 */
108ce4c820dSDan Handley 	val = read_sctlr_el3();
109354ab57dSAndrew Thoelke 	assert(val & SCTLR_M_BIT);
110354ab57dSAndrew Thoelke 	assert(val & SCTLR_C_BIT);
111354ab57dSAndrew Thoelke 	assert(val & SCTLR_I_BIT);
112ce4c820dSDan Handley 	/*
113ce4c820dSDan Handley 	 * Check that Cache Writeback Granule (CWG) in CTR_EL0 matches the
114ce4c820dSDan Handley 	 * provided platform value
115ce4c820dSDan Handley 	 */
116ce4c820dSDan Handley 	val = (read_ctr_el0() >> CTR_CWG_SHIFT) & CTR_CWG_MASK;
117ce4c820dSDan Handley 	/*
118ce4c820dSDan Handley 	 * If CWG is zero, then no CWG information is available but we can
119ce4c820dSDan Handley 	 * at least check the platform value is less than the architectural
120ce4c820dSDan Handley 	 * maximum.
121ce4c820dSDan Handley 	 */
122ce4c820dSDan Handley 	if (val != 0)
123ce4c820dSDan Handley 		assert(CACHE_WRITEBACK_GRANULE == SIZE_FROM_LOG2_WORDS(val));
124ce4c820dSDan Handley 	else
125ce4c820dSDan Handley 		assert(CACHE_WRITEBACK_GRANULE <= MAX_CACHE_LINE_SIZE);
126ce4c820dSDan Handley #endif
1274f6ad66aSAchin Gupta 
1284f6ad66aSAchin Gupta 	/* Perform remaining generic architectural setup from EL3 */
1294f6ad66aSAchin Gupta 	bl1_arch_setup();
1304f6ad66aSAchin Gupta 
1317baff11fSYatharth Kochar #if TRUSTED_BOARD_BOOT
1327baff11fSYatharth Kochar 	/* Initialize authentication module */
1337baff11fSYatharth Kochar 	auth_mod_init();
1347baff11fSYatharth Kochar #endif /* TRUSTED_BOARD_BOOT */
1357baff11fSYatharth Kochar 
1364f6ad66aSAchin Gupta 	/* Perform platform setup in BL1. */
1374f6ad66aSAchin Gupta 	bl1_platform_setup();
1384f6ad66aSAchin Gupta 
1397baff11fSYatharth Kochar 	/* Get the image id of next image to load and run. */
1407baff11fSYatharth Kochar 	image_id = bl1_plat_get_next_image_id();
1417baff11fSYatharth Kochar 
142*48bfb88eSYatharth Kochar 	/*
143*48bfb88eSYatharth Kochar 	 * We currently interpret any image id other than
144*48bfb88eSYatharth Kochar 	 * BL2_IMAGE_ID as the start of firmware update.
145*48bfb88eSYatharth Kochar 	 */
1467baff11fSYatharth Kochar 	if (image_id == BL2_IMAGE_ID)
1477baff11fSYatharth Kochar 		bl1_load_bl2();
148*48bfb88eSYatharth Kochar 	else
149*48bfb88eSYatharth Kochar 		NOTICE("BL1-FWU: *******FWU Process Started*******\n");
1507baff11fSYatharth Kochar 
1517baff11fSYatharth Kochar 	bl1_prepare_next_image(image_id);
1527baff11fSYatharth Kochar }
1537baff11fSYatharth Kochar 
1547baff11fSYatharth Kochar /*******************************************************************************
1557baff11fSYatharth Kochar  * This function locates and loads the BL2 raw binary image in the trusted SRAM.
1567baff11fSYatharth Kochar  * Called by the primary cpu after a cold boot.
1577baff11fSYatharth Kochar  * TODO: Add support for alternative image load mechanism e.g using virtio/elf
1587baff11fSYatharth Kochar  * loader etc.
1597baff11fSYatharth Kochar  ******************************************************************************/
1607baff11fSYatharth Kochar void bl1_load_bl2(void)
1617baff11fSYatharth Kochar {
1627baff11fSYatharth Kochar 	image_desc_t *image_desc;
1637baff11fSYatharth Kochar 	image_info_t *image_info;
1647baff11fSYatharth Kochar 	entry_point_info_t *ep_info;
1657baff11fSYatharth Kochar 	meminfo_t *bl1_tzram_layout;
1667baff11fSYatharth Kochar 	meminfo_t *bl2_tzram_layout;
1677baff11fSYatharth Kochar 	int err;
1687baff11fSYatharth Kochar 
1697baff11fSYatharth Kochar 	/* Get the image descriptor */
1707baff11fSYatharth Kochar 	image_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
1717baff11fSYatharth Kochar 	assert(image_desc);
1727baff11fSYatharth Kochar 
1737baff11fSYatharth Kochar 	/* Get the image info */
1747baff11fSYatharth Kochar 	image_info = &image_desc->image_info;
1757baff11fSYatharth Kochar 
1767baff11fSYatharth Kochar 	/* Get the entry point info */
1777baff11fSYatharth Kochar 	ep_info = &image_desc->ep_info;
1784112bfa0SVikram Kanigiri 
1798f55dfb4SSandrine Bailleux 	/* Find out how much free trusted ram remains after BL1 load */
180ee12f6f7SSandrine Bailleux 	bl1_tzram_layout = bl1_plat_sec_mem_layout();
1818f55dfb4SSandrine Bailleux 
18216948ae1SJuan Castillo 	INFO("BL1: Loading BL2\n");
18316948ae1SJuan Castillo 
1848f55dfb4SSandrine Bailleux 	/* Load the BL2 image */
1851779ba6bSJuan Castillo 	err = load_auth_image(bl1_tzram_layout,
18616948ae1SJuan Castillo 			 BL2_IMAGE_ID,
1877baff11fSYatharth Kochar 			 image_info->image_base,
1887baff11fSYatharth Kochar 			 image_info,
1897baff11fSYatharth Kochar 			 ep_info);
1901779ba6bSJuan Castillo 
1914112bfa0SVikram Kanigiri 	if (err) {
1926ad2e461SDan Handley 		ERROR("Failed to load BL2 firmware.\n");
19340fc6cd1SJuan Castillo 		plat_error_handler(err);
1944112bfa0SVikram Kanigiri 	}
19501df3c14SJuan Castillo 
1964f6ad66aSAchin Gupta 	/*
1974f6ad66aSAchin Gupta 	 * Create a new layout of memory for BL2 as seen by BL1 i.e.
1984f6ad66aSAchin Gupta 	 * tell it the amount of total and free memory available.
1994f6ad66aSAchin Gupta 	 * This layout is created at the first free address visible
2004f6ad66aSAchin Gupta 	 * to BL2. BL2 will read the memory layout before using its
2014f6ad66aSAchin Gupta 	 * memory for other purposes.
2024f6ad66aSAchin Gupta 	 */
203fb037bfbSDan Handley 	bl2_tzram_layout = (meminfo_t *) bl1_tzram_layout->free_base;
2048f55dfb4SSandrine Bailleux 	bl1_init_bl2_mem_layout(bl1_tzram_layout, bl2_tzram_layout);
2054f6ad66aSAchin Gupta 
2067baff11fSYatharth Kochar 	ep_info->args.arg1 = (unsigned long)bl2_tzram_layout;
2077baff11fSYatharth Kochar 	NOTICE("BL1: Booting BL2\n");
2087baff11fSYatharth Kochar 	VERBOSE("BL1: BL2 memory layout address = 0x%llx\n",
2097baff11fSYatharth Kochar 		(unsigned long long) bl2_tzram_layout);
2104f6ad66aSAchin Gupta }
2114f6ad66aSAchin Gupta 
2124f6ad66aSAchin Gupta /*******************************************************************************
213ee5c2b13SSandrine Bailleux  * Function called just before handing over to BL31 to inform the user about
214ee5c2b13SSandrine Bailleux  * the boot progress. In debug mode, also print details about the BL31 image's
215ee5c2b13SSandrine Bailleux  * execution context.
2164f6ad66aSAchin Gupta  ******************************************************************************/
217ee5c2b13SSandrine Bailleux void bl1_print_bl31_ep_info(const entry_point_info_t *bl31_ep_info)
2184f6ad66aSAchin Gupta {
2196ad2e461SDan Handley 	NOTICE("BL1: Booting BL3-1\n");
22068a68c92SSandrine Bailleux 	print_entry_point_info(bl31_ep_info);
2214f6ad66aSAchin Gupta }
22235e8c766SSandrine Bailleux 
22335e8c766SSandrine Bailleux #if SPIN_ON_BL1_EXIT
22435e8c766SSandrine Bailleux void print_debug_loop_message(void)
22535e8c766SSandrine Bailleux {
22635e8c766SSandrine Bailleux 	NOTICE("BL1: Debug loop, spinning forever\n");
22735e8c766SSandrine Bailleux 	NOTICE("BL1: Please connect the debugger to continue\n");
22835e8c766SSandrine Bailleux }
22935e8c766SSandrine Bailleux #endif
230*48bfb88eSYatharth Kochar 
231*48bfb88eSYatharth Kochar /*******************************************************************************
232*48bfb88eSYatharth Kochar  * Top level handler for servicing BL1 SMCs.
233*48bfb88eSYatharth Kochar  ******************************************************************************/
234*48bfb88eSYatharth Kochar register_t bl1_smc_handler(unsigned int smc_fid,
235*48bfb88eSYatharth Kochar 	register_t x1,
236*48bfb88eSYatharth Kochar 	register_t x2,
237*48bfb88eSYatharth Kochar 	register_t x3,
238*48bfb88eSYatharth Kochar 	register_t x4,
239*48bfb88eSYatharth Kochar 	void *cookie,
240*48bfb88eSYatharth Kochar 	void *handle,
241*48bfb88eSYatharth Kochar 	unsigned int flags)
242*48bfb88eSYatharth Kochar {
243*48bfb88eSYatharth Kochar 
244*48bfb88eSYatharth Kochar #if TRUSTED_BOARD_BOOT
245*48bfb88eSYatharth Kochar 	/*
246*48bfb88eSYatharth Kochar 	 * Dispatch FWU calls to FWU SMC handler and return its return
247*48bfb88eSYatharth Kochar 	 * value
248*48bfb88eSYatharth Kochar 	 */
249*48bfb88eSYatharth Kochar 	if (is_fwu_fid(smc_fid)) {
250*48bfb88eSYatharth Kochar 		return bl1_fwu_smc_handler(smc_fid, x1, x2, x3, x4, cookie,
251*48bfb88eSYatharth Kochar 			handle, flags);
252*48bfb88eSYatharth Kochar 	}
253*48bfb88eSYatharth Kochar #endif
254*48bfb88eSYatharth Kochar 
255*48bfb88eSYatharth Kochar 	switch (smc_fid) {
256*48bfb88eSYatharth Kochar 	case BL1_SMC_CALL_COUNT:
257*48bfb88eSYatharth Kochar 		SMC_RET1(handle, BL1_NUM_SMC_CALLS);
258*48bfb88eSYatharth Kochar 
259*48bfb88eSYatharth Kochar 	case BL1_SMC_UID:
260*48bfb88eSYatharth Kochar 		SMC_UUID_RET(handle, bl1_svc_uid);
261*48bfb88eSYatharth Kochar 
262*48bfb88eSYatharth Kochar 	case BL1_SMC_VERSION:
263*48bfb88eSYatharth Kochar 		SMC_RET1(handle, BL1_SMC_MAJOR_VER | BL1_SMC_MINOR_VER);
264*48bfb88eSYatharth Kochar 
265*48bfb88eSYatharth Kochar 	default:
266*48bfb88eSYatharth Kochar 		break;
267*48bfb88eSYatharth Kochar 	}
268*48bfb88eSYatharth Kochar 
269*48bfb88eSYatharth Kochar 	WARN("Unimplemented BL1 SMC Call: 0x%x \n", smc_fid);
270*48bfb88eSYatharth Kochar 	SMC_RET1(handle, SMC_UNK);
271*48bfb88eSYatharth Kochar }
272