14f6ad66aSAchin Gupta /* 2*48ba0345SManish V Badarkhe * Copyright (c) 2013-2021, ARM Limited and Contributors. All rights reserved. 34f6ad66aSAchin Gupta * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 54f6ad66aSAchin Gupta */ 64f6ad66aSAchin Gupta 709d40e0eSAntonio Nino Diaz #include <assert.h> 809d40e0eSAntonio Nino Diaz 909d40e0eSAntonio Nino Diaz #include <platform_def.h> 1009d40e0eSAntonio Nino Diaz 1197043ac9SDan Handley #include <arch.h> 12ed108b56SAlexei Fedorov #include <arch_features.h> 134f6ad66aSAchin Gupta #include <arch_helpers.h> 1409d40e0eSAntonio Nino Diaz #include <bl1/bl1.h> 1509d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 1609d40e0eSAntonio Nino Diaz #include <common/debug.h> 1709d40e0eSAntonio Nino Diaz #include <drivers/auth/auth_mod.h> 1809d40e0eSAntonio Nino Diaz #include <drivers/console.h> 1909d40e0eSAntonio Nino Diaz #include <lib/cpus/errata_report.h> 2009d40e0eSAntonio Nino Diaz #include <lib/utils.h> 2109d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 22085e80ecSAntonio Nino Diaz #include <smccc_helpers.h> 2309d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h> 2409d40e0eSAntonio Nino Diaz 252a4b4b71SIsla Mitchell #include "bl1_private.h" 2648bfb88eSYatharth Kochar 277baff11fSYatharth Kochar static void bl1_load_bl2(void); 2829fb905dSVikram Kanigiri 29530ceda5SAlexei Fedorov #if ENABLE_PAUTH 30530ceda5SAlexei Fedorov uint64_t bl1_apiakey[2]; 31530ceda5SAlexei Fedorov #endif 32530ceda5SAlexei Fedorov 338f55dfb4SSandrine Bailleux /******************************************************************************* 34101d01e2SSoby Mathew * Helper utility to calculate the BL2 memory layout taking into consideration 35101d01e2SSoby Mathew * the BL1 RW data assuming that it is at the top of the memory layout. 368f55dfb4SSandrine Bailleux ******************************************************************************/ 37101d01e2SSoby Mathew void bl1_calc_bl2_mem_layout(const meminfo_t *bl1_mem_layout, 388f55dfb4SSandrine Bailleux meminfo_t *bl2_mem_layout) 398f55dfb4SSandrine Bailleux { 408f55dfb4SSandrine Bailleux assert(bl1_mem_layout != NULL); 418f55dfb4SSandrine Bailleux assert(bl2_mem_layout != NULL); 428f55dfb4SSandrine Bailleux 4342019bf4SYatharth Kochar /* 4442019bf4SYatharth Kochar * Remove BL1 RW data from the scope of memory visible to BL2. 4542019bf4SYatharth Kochar * This is assuming BL1 RW data is at the top of bl1_mem_layout. 4642019bf4SYatharth Kochar */ 4742019bf4SYatharth Kochar assert(BL1_RW_BASE > bl1_mem_layout->total_base); 4842019bf4SYatharth Kochar bl2_mem_layout->total_base = bl1_mem_layout->total_base; 4942019bf4SYatharth Kochar bl2_mem_layout->total_size = BL1_RW_BASE - bl1_mem_layout->total_base; 508f55dfb4SSandrine Bailleux 51ee006a79SDeepika Bhavnani flush_dcache_range((uintptr_t)bl2_mem_layout, sizeof(meminfo_t)); 528f55dfb4SSandrine Bailleux } 5329fb905dSVikram Kanigiri 5429fb905dSVikram Kanigiri /******************************************************************************* 55cd7d6b0eSAntonio Nino Diaz * Setup function for BL1. 56cd7d6b0eSAntonio Nino Diaz ******************************************************************************/ 57cd7d6b0eSAntonio Nino Diaz void bl1_setup(void) 58cd7d6b0eSAntonio Nino Diaz { 59cd7d6b0eSAntonio Nino Diaz /* Perform early platform-specific setup */ 60cd7d6b0eSAntonio Nino Diaz bl1_early_platform_setup(); 61cd7d6b0eSAntonio Nino Diaz 62cd7d6b0eSAntonio Nino Diaz /* Perform late platform-specific setup */ 63cd7d6b0eSAntonio Nino Diaz bl1_plat_arch_setup(); 64ed108b56SAlexei Fedorov 65ed108b56SAlexei Fedorov #if CTX_INCLUDE_PAUTH_REGS 66ed108b56SAlexei Fedorov /* 67ed108b56SAlexei Fedorov * Assert that the ARMv8.3-PAuth registers are present or an access 68ed108b56SAlexei Fedorov * fault will be triggered when they are being saved or restored. 69ed108b56SAlexei Fedorov */ 70ed108b56SAlexei Fedorov assert(is_armv8_3_pauth_present()); 71ed108b56SAlexei Fedorov #endif /* CTX_INCLUDE_PAUTH_REGS */ 72cd7d6b0eSAntonio Nino Diaz } 73cd7d6b0eSAntonio Nino Diaz 74cd7d6b0eSAntonio Nino Diaz /******************************************************************************* 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 883443a702SJohn Powell INFO("BL1: RAM %p - %p\n", (void *)BL1_RAM_BASE, (void *)BL1_RAM_LIMIT); 896ad2e461SDan Handley 9010bcd761SJeenu Viswambharan print_errata_status(); 914f6ad66aSAchin Gupta 92aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS 93f3b4914bSYatharth Kochar u_register_t val; 944f6ad66aSAchin Gupta /* 954f6ad66aSAchin Gupta * Ensure that MMU/Caches and coherency are turned on 964f6ad66aSAchin Gupta */ 97402b3cf8SJulius Werner #ifdef __aarch64__ 98ce4c820dSDan Handley val = read_sctlr_el3(); 99402b3cf8SJulius Werner #else 100402b3cf8SJulius Werner val = read_sctlr(); 101f3b4914bSYatharth Kochar #endif 1023443a702SJohn Powell assert((val & SCTLR_M_BIT) != 0); 1033443a702SJohn Powell assert((val & SCTLR_C_BIT) != 0); 1043443a702SJohn Powell assert((val & SCTLR_I_BIT) != 0); 105ce4c820dSDan Handley /* 106ce4c820dSDan Handley * Check that Cache Writeback Granule (CWG) in CTR_EL0 matches the 107ce4c820dSDan Handley * provided platform value 108ce4c820dSDan Handley */ 109ce4c820dSDan Handley val = (read_ctr_el0() >> CTR_CWG_SHIFT) & CTR_CWG_MASK; 110ce4c820dSDan Handley /* 111ce4c820dSDan Handley * If CWG is zero, then no CWG information is available but we can 112ce4c820dSDan Handley * at least check the platform value is less than the architectural 113ce4c820dSDan Handley * maximum. 114ce4c820dSDan Handley */ 115ce4c820dSDan Handley if (val != 0) 116ce4c820dSDan Handley assert(CACHE_WRITEBACK_GRANULE == SIZE_FROM_LOG2_WORDS(val)); 117ce4c820dSDan Handley else 118ce4c820dSDan Handley assert(CACHE_WRITEBACK_GRANULE <= MAX_CACHE_LINE_SIZE); 119aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */ 1204f6ad66aSAchin Gupta 1214f6ad66aSAchin Gupta /* Perform remaining generic architectural setup from EL3 */ 1224f6ad66aSAchin Gupta bl1_arch_setup(); 1234f6ad66aSAchin Gupta 1247baff11fSYatharth Kochar #if TRUSTED_BOARD_BOOT 1257baff11fSYatharth Kochar /* Initialize authentication module */ 1267baff11fSYatharth Kochar auth_mod_init(); 1277baff11fSYatharth Kochar #endif /* TRUSTED_BOARD_BOOT */ 1287baff11fSYatharth Kochar 129*48ba0345SManish V Badarkhe /* Initialize the measured boot */ 130*48ba0345SManish V Badarkhe bl1_plat_mboot_init(); 131*48ba0345SManish V Badarkhe 1324f6ad66aSAchin Gupta /* Perform platform setup in BL1. */ 1334f6ad66aSAchin Gupta bl1_platform_setup(); 1344f6ad66aSAchin Gupta 135530ceda5SAlexei Fedorov #if ENABLE_PAUTH 136530ceda5SAlexei Fedorov /* Store APIAKey_EL1 key */ 137530ceda5SAlexei Fedorov bl1_apiakey[0] = read_apiakeylo_el1(); 138530ceda5SAlexei Fedorov bl1_apiakey[1] = read_apiakeyhi_el1(); 139530ceda5SAlexei Fedorov #endif /* ENABLE_PAUTH */ 140530ceda5SAlexei Fedorov 1417baff11fSYatharth Kochar /* Get the image id of next image to load and run. */ 1427baff11fSYatharth Kochar image_id = bl1_plat_get_next_image_id(); 1437baff11fSYatharth Kochar 14448bfb88eSYatharth Kochar /* 14548bfb88eSYatharth Kochar * We currently interpret any image id other than 14648bfb88eSYatharth Kochar * BL2_IMAGE_ID as the start of firmware update. 14748bfb88eSYatharth Kochar */ 1487baff11fSYatharth Kochar if (image_id == BL2_IMAGE_ID) 1497baff11fSYatharth Kochar bl1_load_bl2(); 15048bfb88eSYatharth Kochar else 15148bfb88eSYatharth Kochar NOTICE("BL1-FWU: *******FWU Process Started*******\n"); 1527baff11fSYatharth Kochar 153*48ba0345SManish V Badarkhe /* Teardown the measured boot driver */ 154*48ba0345SManish V Badarkhe bl1_plat_mboot_finish(); 155*48ba0345SManish V Badarkhe 1567baff11fSYatharth Kochar bl1_prepare_next_image(image_id); 1570b32628eSAntonio Nino Diaz 1580b32628eSAntonio Nino Diaz console_flush(); 1597baff11fSYatharth Kochar } 1607baff11fSYatharth Kochar 1617baff11fSYatharth Kochar /******************************************************************************* 1627baff11fSYatharth Kochar * This function locates and loads the BL2 raw binary image in the trusted SRAM. 1637baff11fSYatharth Kochar * Called by the primary cpu after a cold boot. 1647baff11fSYatharth Kochar * TODO: Add support for alternative image load mechanism e.g using virtio/elf 1657baff11fSYatharth Kochar * loader etc. 1667baff11fSYatharth Kochar ******************************************************************************/ 167ce3f9a6dSRoberto Vargas static void bl1_load_bl2(void) 1687baff11fSYatharth Kochar { 1693443a702SJohn Powell image_desc_t *desc; 1703443a702SJohn Powell image_info_t *info; 1717baff11fSYatharth Kochar int err; 1727baff11fSYatharth Kochar 1737baff11fSYatharth Kochar /* Get the image descriptor */ 1743443a702SJohn Powell desc = bl1_plat_get_image_desc(BL2_IMAGE_ID); 1753443a702SJohn Powell assert(desc != NULL); 1767baff11fSYatharth Kochar 1777baff11fSYatharth Kochar /* Get the image info */ 1783443a702SJohn Powell info = &desc->image_info; 17916948ae1SJuan Castillo INFO("BL1: Loading BL2\n"); 18016948ae1SJuan Castillo 181566034fcSSoby Mathew err = bl1_plat_handle_pre_image_load(BL2_IMAGE_ID); 1823443a702SJohn Powell if (err != 0) { 18311f001cbSMasahiro Yamada ERROR("Failure in pre image load handling of BL2 (%d)\n", err); 18411f001cbSMasahiro Yamada plat_error_handler(err); 18511f001cbSMasahiro Yamada } 18611f001cbSMasahiro Yamada 1873443a702SJohn Powell err = load_auth_image(BL2_IMAGE_ID, info); 1883443a702SJohn Powell if (err != 0) { 1896ad2e461SDan Handley ERROR("Failed to load BL2 firmware.\n"); 19040fc6cd1SJuan Castillo plat_error_handler(err); 1914112bfa0SVikram Kanigiri } 19201df3c14SJuan Castillo 19311f001cbSMasahiro Yamada /* Allow platform to handle image information. */ 194566034fcSSoby Mathew err = bl1_plat_handle_post_image_load(BL2_IMAGE_ID); 1953443a702SJohn Powell if (err != 0) { 19611f001cbSMasahiro Yamada ERROR("Failure in post image load handling of BL2 (%d)\n", err); 19711f001cbSMasahiro Yamada plat_error_handler(err); 19811f001cbSMasahiro Yamada } 19911f001cbSMasahiro Yamada 2007baff11fSYatharth Kochar NOTICE("BL1: Booting BL2\n"); 2014f6ad66aSAchin Gupta } 2024f6ad66aSAchin Gupta 2034f6ad66aSAchin Gupta /******************************************************************************* 204f3b4914bSYatharth Kochar * Function called just before handing over to the next BL to inform the user 205f3b4914bSYatharth Kochar * about the boot progress. In debug mode, also print details about the BL 206f3b4914bSYatharth Kochar * image's execution context. 2074f6ad66aSAchin Gupta ******************************************************************************/ 208f3b4914bSYatharth Kochar void bl1_print_next_bl_ep_info(const entry_point_info_t *bl_ep_info) 2094f6ad66aSAchin Gupta { 210402b3cf8SJulius Werner #ifdef __aarch64__ 211d178637dSJuan Castillo NOTICE("BL1: Booting BL31\n"); 212402b3cf8SJulius Werner #else 213402b3cf8SJulius Werner NOTICE("BL1: Booting BL32\n"); 214402b3cf8SJulius Werner #endif /* __aarch64__ */ 215f3b4914bSYatharth Kochar print_entry_point_info(bl_ep_info); 2164f6ad66aSAchin Gupta } 21735e8c766SSandrine Bailleux 21835e8c766SSandrine Bailleux #if SPIN_ON_BL1_EXIT 21935e8c766SSandrine Bailleux void print_debug_loop_message(void) 22035e8c766SSandrine Bailleux { 22135e8c766SSandrine Bailleux NOTICE("BL1: Debug loop, spinning forever\n"); 22235e8c766SSandrine Bailleux NOTICE("BL1: Please connect the debugger to continue\n"); 22335e8c766SSandrine Bailleux } 22435e8c766SSandrine Bailleux #endif 22548bfb88eSYatharth Kochar 22648bfb88eSYatharth Kochar /******************************************************************************* 22748bfb88eSYatharth Kochar * Top level handler for servicing BL1 SMCs. 22848bfb88eSYatharth Kochar ******************************************************************************/ 2292fe75a2dSZelalem u_register_t bl1_smc_handler(unsigned int smc_fid, 2302fe75a2dSZelalem u_register_t x1, 2312fe75a2dSZelalem u_register_t x2, 2322fe75a2dSZelalem u_register_t x3, 2332fe75a2dSZelalem u_register_t x4, 23448bfb88eSYatharth Kochar void *cookie, 23548bfb88eSYatharth Kochar void *handle, 23648bfb88eSYatharth Kochar unsigned int flags) 23748bfb88eSYatharth Kochar { 238a14988c6SJimmy Brisson /* BL1 Service UUID */ 239a14988c6SJimmy Brisson DEFINE_SVC_UUID2(bl1_svc_uid, 240a14988c6SJimmy Brisson U(0xd46739fd), 0xcb72, 0x9a4d, 0xb5, 0x75, 241a14988c6SJimmy Brisson 0x67, 0x15, 0xd6, 0xf4, 0xbb, 0x4a); 242a14988c6SJimmy Brisson 24348bfb88eSYatharth Kochar 24448bfb88eSYatharth Kochar #if TRUSTED_BOARD_BOOT 24548bfb88eSYatharth Kochar /* 24648bfb88eSYatharth Kochar * Dispatch FWU calls to FWU SMC handler and return its return 24748bfb88eSYatharth Kochar * value 24848bfb88eSYatharth Kochar */ 24948bfb88eSYatharth Kochar if (is_fwu_fid(smc_fid)) { 25048bfb88eSYatharth Kochar return bl1_fwu_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 25148bfb88eSYatharth Kochar handle, flags); 25248bfb88eSYatharth Kochar } 25348bfb88eSYatharth Kochar #endif 25448bfb88eSYatharth Kochar 25548bfb88eSYatharth Kochar switch (smc_fid) { 25648bfb88eSYatharth Kochar case BL1_SMC_CALL_COUNT: 25748bfb88eSYatharth Kochar SMC_RET1(handle, BL1_NUM_SMC_CALLS); 25848bfb88eSYatharth Kochar 25948bfb88eSYatharth Kochar case BL1_SMC_UID: 26048bfb88eSYatharth Kochar SMC_UUID_RET(handle, bl1_svc_uid); 26148bfb88eSYatharth Kochar 26248bfb88eSYatharth Kochar case BL1_SMC_VERSION: 26348bfb88eSYatharth Kochar SMC_RET1(handle, BL1_SMC_MAJOR_VER | BL1_SMC_MINOR_VER); 26448bfb88eSYatharth Kochar 26548bfb88eSYatharth Kochar default: 26648bfb88eSYatharth Kochar WARN("Unimplemented BL1 SMC Call: 0x%x\n", smc_fid); 26748bfb88eSYatharth Kochar SMC_RET1(handle, SMC_UNK); 26848bfb88eSYatharth Kochar } 2693443a702SJohn Powell } 270a4409008Sdp-arm 271a4409008Sdp-arm /******************************************************************************* 272a4409008Sdp-arm * BL1 SMC wrapper. This function is only used in AArch32 mode to ensure ABI 273a4409008Sdp-arm * compliance when invoking bl1_smc_handler. 274a4409008Sdp-arm ******************************************************************************/ 2752fe75a2dSZelalem u_register_t bl1_smc_wrapper(uint32_t smc_fid, 276a4409008Sdp-arm void *cookie, 277a4409008Sdp-arm void *handle, 278a4409008Sdp-arm unsigned int flags) 279a4409008Sdp-arm { 2802fe75a2dSZelalem u_register_t x1, x2, x3, x4; 281a4409008Sdp-arm 282466bb285SZelalem assert(handle != NULL); 283a4409008Sdp-arm 284a4409008Sdp-arm get_smc_params_from_ctx(handle, x1, x2, x3, x4); 285a4409008Sdp-arm return bl1_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, flags); 286a4409008Sdp-arm } 287