14f6ad66aSAchin Gupta /* 210bcd761SJeenu Viswambharan * Copyright (c) 2013-2017, 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> 205b827a8fSDan Handley #include "bl1_private.h" 2148bfb88eSYatharth Kochar #include <uuid.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 18042019bf4SYatharth Kochar #if LOAD_IMAGE_V2 18142019bf4SYatharth Kochar err = load_auth_image(BL2_IMAGE_ID, image_info); 18242019bf4SYatharth Kochar #else 1838f55dfb4SSandrine Bailleux /* Load the BL2 image */ 1841779ba6bSJuan Castillo err = load_auth_image(bl1_tzram_layout, 18516948ae1SJuan Castillo BL2_IMAGE_ID, 1867baff11fSYatharth Kochar image_info->image_base, 1877baff11fSYatharth Kochar image_info, 1887baff11fSYatharth Kochar ep_info); 1891779ba6bSJuan Castillo 19042019bf4SYatharth Kochar #endif /* LOAD_IMAGE_V2 */ 19142019bf4SYatharth Kochar 1924112bfa0SVikram Kanigiri if (err) { 1936ad2e461SDan Handley ERROR("Failed to load BL2 firmware.\n"); 19440fc6cd1SJuan Castillo plat_error_handler(err); 1954112bfa0SVikram Kanigiri } 19601df3c14SJuan Castillo 1974f6ad66aSAchin Gupta /* 1984f6ad66aSAchin Gupta * Create a new layout of memory for BL2 as seen by BL1 i.e. 1994f6ad66aSAchin Gupta * tell it the amount of total and free memory available. 2004f6ad66aSAchin Gupta * This layout is created at the first free address visible 2014f6ad66aSAchin Gupta * to BL2. BL2 will read the memory layout before using its 2024f6ad66aSAchin Gupta * memory for other purposes. 2034f6ad66aSAchin Gupta */ 20442019bf4SYatharth Kochar #if LOAD_IMAGE_V2 20542019bf4SYatharth Kochar bl2_tzram_layout = (meminfo_t *) bl1_tzram_layout->total_base; 20642019bf4SYatharth Kochar #else 207fb037bfbSDan Handley bl2_tzram_layout = (meminfo_t *) bl1_tzram_layout->free_base; 20842019bf4SYatharth Kochar #endif /* LOAD_IMAGE_V2 */ 20942019bf4SYatharth Kochar 2108f55dfb4SSandrine Bailleux bl1_init_bl2_mem_layout(bl1_tzram_layout, bl2_tzram_layout); 2114f6ad66aSAchin Gupta 212f3b4914bSYatharth Kochar ep_info->args.arg1 = (uintptr_t)bl2_tzram_layout; 2137baff11fSYatharth Kochar NOTICE("BL1: Booting BL2\n"); 214f3b4914bSYatharth Kochar VERBOSE("BL1: BL2 memory layout address = %p\n", 215f3b4914bSYatharth Kochar (void *) bl2_tzram_layout); 2164f6ad66aSAchin Gupta } 2174f6ad66aSAchin Gupta 2184f6ad66aSAchin Gupta /******************************************************************************* 219f3b4914bSYatharth Kochar * Function called just before handing over to the next BL to inform the user 220f3b4914bSYatharth Kochar * about the boot progress. In debug mode, also print details about the BL 221f3b4914bSYatharth Kochar * image's execution context. 2224f6ad66aSAchin Gupta ******************************************************************************/ 223f3b4914bSYatharth Kochar void bl1_print_next_bl_ep_info(const entry_point_info_t *bl_ep_info) 2244f6ad66aSAchin Gupta { 225f3b4914bSYatharth Kochar #ifdef AARCH32 226f3b4914bSYatharth Kochar NOTICE("BL1: Booting BL32\n"); 227f3b4914bSYatharth Kochar #else 228d178637dSJuan Castillo NOTICE("BL1: Booting BL31\n"); 229f3b4914bSYatharth Kochar #endif /* AARCH32 */ 230f3b4914bSYatharth Kochar print_entry_point_info(bl_ep_info); 2314f6ad66aSAchin Gupta } 23235e8c766SSandrine Bailleux 23335e8c766SSandrine Bailleux #if SPIN_ON_BL1_EXIT 23435e8c766SSandrine Bailleux void print_debug_loop_message(void) 23535e8c766SSandrine Bailleux { 23635e8c766SSandrine Bailleux NOTICE("BL1: Debug loop, spinning forever\n"); 23735e8c766SSandrine Bailleux NOTICE("BL1: Please connect the debugger to continue\n"); 23835e8c766SSandrine Bailleux } 23935e8c766SSandrine Bailleux #endif 24048bfb88eSYatharth Kochar 24148bfb88eSYatharth Kochar /******************************************************************************* 24248bfb88eSYatharth Kochar * Top level handler for servicing BL1 SMCs. 24348bfb88eSYatharth Kochar ******************************************************************************/ 24448bfb88eSYatharth Kochar register_t bl1_smc_handler(unsigned int smc_fid, 24548bfb88eSYatharth Kochar register_t x1, 24648bfb88eSYatharth Kochar register_t x2, 24748bfb88eSYatharth Kochar register_t x3, 24848bfb88eSYatharth Kochar register_t x4, 24948bfb88eSYatharth Kochar void *cookie, 25048bfb88eSYatharth Kochar void *handle, 25148bfb88eSYatharth Kochar unsigned int flags) 25248bfb88eSYatharth Kochar { 25348bfb88eSYatharth Kochar 25448bfb88eSYatharth Kochar #if TRUSTED_BOARD_BOOT 25548bfb88eSYatharth Kochar /* 25648bfb88eSYatharth Kochar * Dispatch FWU calls to FWU SMC handler and return its return 25748bfb88eSYatharth Kochar * value 25848bfb88eSYatharth Kochar */ 25948bfb88eSYatharth Kochar if (is_fwu_fid(smc_fid)) { 26048bfb88eSYatharth Kochar return bl1_fwu_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 26148bfb88eSYatharth Kochar handle, flags); 26248bfb88eSYatharth Kochar } 26348bfb88eSYatharth Kochar #endif 26448bfb88eSYatharth Kochar 26548bfb88eSYatharth Kochar switch (smc_fid) { 26648bfb88eSYatharth Kochar case BL1_SMC_CALL_COUNT: 26748bfb88eSYatharth Kochar SMC_RET1(handle, BL1_NUM_SMC_CALLS); 26848bfb88eSYatharth Kochar 26948bfb88eSYatharth Kochar case BL1_SMC_UID: 27048bfb88eSYatharth Kochar SMC_UUID_RET(handle, bl1_svc_uid); 27148bfb88eSYatharth Kochar 27248bfb88eSYatharth Kochar case BL1_SMC_VERSION: 27348bfb88eSYatharth Kochar SMC_RET1(handle, BL1_SMC_MAJOR_VER | BL1_SMC_MINOR_VER); 27448bfb88eSYatharth Kochar 27548bfb88eSYatharth Kochar default: 27648bfb88eSYatharth Kochar break; 27748bfb88eSYatharth Kochar } 27848bfb88eSYatharth Kochar 27948bfb88eSYatharth Kochar WARN("Unimplemented BL1 SMC Call: 0x%x \n", smc_fid); 28048bfb88eSYatharth Kochar SMC_RET1(handle, SMC_UNK); 28148bfb88eSYatharth Kochar } 282*a4409008Sdp-arm 283*a4409008Sdp-arm /******************************************************************************* 284*a4409008Sdp-arm * BL1 SMC wrapper. This function is only used in AArch32 mode to ensure ABI 285*a4409008Sdp-arm * compliance when invoking bl1_smc_handler. 286*a4409008Sdp-arm ******************************************************************************/ 287*a4409008Sdp-arm register_t bl1_smc_wrapper(uint32_t smc_fid, 288*a4409008Sdp-arm void *cookie, 289*a4409008Sdp-arm void *handle, 290*a4409008Sdp-arm unsigned int flags) 291*a4409008Sdp-arm { 292*a4409008Sdp-arm register_t x1, x2, x3, x4; 293*a4409008Sdp-arm 294*a4409008Sdp-arm assert(handle); 295*a4409008Sdp-arm 296*a4409008Sdp-arm get_smc_params_from_ctx(handle, x1, x2, x3, x4); 297*a4409008Sdp-arm return bl1_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, flags); 298*a4409008Sdp-arm } 299