14f6ad66aSAchin Gupta /* 20aa0b3afSManish V Badarkhe * Copyright (c) 2013-2022, 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> 180aa0b3afSManish V Badarkhe #include <drivers/auth/crypto_mod.h> 1909d40e0eSAntonio Nino Diaz #include <drivers/console.h> 20*6bb96fa6SBoyan Karatotev #include <lib/cpus/errata.h> 2109d40e0eSAntonio Nino Diaz #include <lib/utils.h> 2209d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 23085e80ecSAntonio Nino Diaz #include <smccc_helpers.h> 2409d40e0eSAntonio Nino Diaz #include <tools_share/uuid.h> 2509d40e0eSAntonio Nino Diaz 262a4b4b71SIsla Mitchell #include "bl1_private.h" 2748bfb88eSYatharth Kochar 287baff11fSYatharth Kochar static void bl1_load_bl2(void); 2929fb905dSVikram Kanigiri 30530ceda5SAlexei Fedorov #if ENABLE_PAUTH 31530ceda5SAlexei Fedorov uint64_t bl1_apiakey[2]; 32530ceda5SAlexei Fedorov #endif 33530ceda5SAlexei Fedorov 348f55dfb4SSandrine Bailleux /******************************************************************************* 35101d01e2SSoby Mathew * Helper utility to calculate the BL2 memory layout taking into consideration 36101d01e2SSoby Mathew * the BL1 RW data assuming that it is at the top of the memory layout. 378f55dfb4SSandrine Bailleux ******************************************************************************/ 38101d01e2SSoby Mathew void bl1_calc_bl2_mem_layout(const meminfo_t *bl1_mem_layout, 398f55dfb4SSandrine Bailleux meminfo_t *bl2_mem_layout) 408f55dfb4SSandrine Bailleux { 418f55dfb4SSandrine Bailleux assert(bl1_mem_layout != NULL); 428f55dfb4SSandrine Bailleux assert(bl2_mem_layout != NULL); 438f55dfb4SSandrine Bailleux 4442019bf4SYatharth Kochar /* 4542019bf4SYatharth Kochar * Remove BL1 RW data from the scope of memory visible to BL2. 4642019bf4SYatharth Kochar * This is assuming BL1 RW data is at the top of bl1_mem_layout. 4742019bf4SYatharth Kochar */ 4842019bf4SYatharth Kochar assert(BL1_RW_BASE > bl1_mem_layout->total_base); 4942019bf4SYatharth Kochar bl2_mem_layout->total_base = bl1_mem_layout->total_base; 5042019bf4SYatharth Kochar bl2_mem_layout->total_size = BL1_RW_BASE - bl1_mem_layout->total_base; 518f55dfb4SSandrine Bailleux 52ee006a79SDeepika Bhavnani flush_dcache_range((uintptr_t)bl2_mem_layout, sizeof(meminfo_t)); 538f55dfb4SSandrine Bailleux } 5429fb905dSVikram Kanigiri 5529fb905dSVikram Kanigiri /******************************************************************************* 56cd7d6b0eSAntonio Nino Diaz * Setup function for BL1. 57cd7d6b0eSAntonio Nino Diaz ******************************************************************************/ 58cd7d6b0eSAntonio Nino Diaz void bl1_setup(void) 59cd7d6b0eSAntonio Nino Diaz { 60cd7d6b0eSAntonio Nino Diaz /* Perform early platform-specific setup */ 61cd7d6b0eSAntonio Nino Diaz bl1_early_platform_setup(); 62cd7d6b0eSAntonio Nino Diaz 63cd7d6b0eSAntonio Nino Diaz /* Perform late platform-specific setup */ 64cd7d6b0eSAntonio Nino Diaz bl1_plat_arch_setup(); 65ed108b56SAlexei Fedorov 66ed108b56SAlexei Fedorov #if CTX_INCLUDE_PAUTH_REGS 67ed108b56SAlexei Fedorov /* 68ed108b56SAlexei Fedorov * Assert that the ARMv8.3-PAuth registers are present or an access 69ed108b56SAlexei Fedorov * fault will be triggered when they are being saved or restored. 70ed108b56SAlexei Fedorov */ 71ed108b56SAlexei Fedorov assert(is_armv8_3_pauth_present()); 72ed108b56SAlexei Fedorov #endif /* CTX_INCLUDE_PAUTH_REGS */ 73cd7d6b0eSAntonio Nino Diaz } 74cd7d6b0eSAntonio Nino Diaz 75cd7d6b0eSAntonio Nino Diaz /******************************************************************************* 764f6ad66aSAchin Gupta * Function to perform late architectural and platform specific initialization. 777baff11fSYatharth Kochar * It also queries the platform to load and run next BL image. Only called 787baff11fSYatharth Kochar * by the primary cpu after a cold boot. 794f6ad66aSAchin Gupta ******************************************************************************/ 804f6ad66aSAchin Gupta void bl1_main(void) 814f6ad66aSAchin Gupta { 827baff11fSYatharth Kochar unsigned int image_id; 837baff11fSYatharth Kochar 846ad2e461SDan Handley /* Announce our arrival */ 856ad2e461SDan Handley NOTICE(FIRMWARE_WELCOME_STR); 866ad2e461SDan Handley NOTICE("BL1: %s\n", version_string); 876ad2e461SDan Handley NOTICE("BL1: %s\n", build_message); 886ad2e461SDan Handley 893443a702SJohn Powell INFO("BL1: RAM %p - %p\n", (void *)BL1_RAM_BASE, (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 */ 98402b3cf8SJulius Werner #ifdef __aarch64__ 99ce4c820dSDan Handley val = read_sctlr_el3(); 100402b3cf8SJulius Werner #else 101402b3cf8SJulius Werner val = read_sctlr(); 102f3b4914bSYatharth Kochar #endif 1033443a702SJohn Powell assert((val & SCTLR_M_BIT) != 0); 1043443a702SJohn Powell assert((val & SCTLR_C_BIT) != 0); 1053443a702SJohn Powell assert((val & SCTLR_I_BIT) != 0); 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 1250aa0b3afSManish V Badarkhe crypto_mod_init(); 1260aa0b3afSManish V Badarkhe 1277baff11fSYatharth Kochar /* Initialize authentication module */ 1287baff11fSYatharth Kochar auth_mod_init(); 1297baff11fSYatharth Kochar 13048ba0345SManish V Badarkhe /* Initialize the measured boot */ 13148ba0345SManish V Badarkhe bl1_plat_mboot_init(); 13248ba0345SManish V Badarkhe 1334f6ad66aSAchin Gupta /* Perform platform setup in BL1. */ 1344f6ad66aSAchin Gupta bl1_platform_setup(); 1354f6ad66aSAchin Gupta 136530ceda5SAlexei Fedorov #if ENABLE_PAUTH 137530ceda5SAlexei Fedorov /* Store APIAKey_EL1 key */ 138530ceda5SAlexei Fedorov bl1_apiakey[0] = read_apiakeylo_el1(); 139530ceda5SAlexei Fedorov bl1_apiakey[1] = read_apiakeyhi_el1(); 140530ceda5SAlexei Fedorov #endif /* ENABLE_PAUTH */ 141530ceda5SAlexei Fedorov 1427baff11fSYatharth Kochar /* Get the image id of next image to load and run. */ 1437baff11fSYatharth Kochar image_id = bl1_plat_get_next_image_id(); 1447baff11fSYatharth Kochar 14548bfb88eSYatharth Kochar /* 14648bfb88eSYatharth Kochar * We currently interpret any image id other than 14748bfb88eSYatharth Kochar * BL2_IMAGE_ID as the start of firmware update. 14848bfb88eSYatharth Kochar */ 1497baff11fSYatharth Kochar if (image_id == BL2_IMAGE_ID) 1507baff11fSYatharth Kochar bl1_load_bl2(); 15148bfb88eSYatharth Kochar else 15248bfb88eSYatharth Kochar NOTICE("BL1-FWU: *******FWU Process Started*******\n"); 1537baff11fSYatharth Kochar 15448ba0345SManish V Badarkhe /* Teardown the measured boot driver */ 15548ba0345SManish V Badarkhe bl1_plat_mboot_finish(); 15648ba0345SManish V Badarkhe 1577baff11fSYatharth Kochar bl1_prepare_next_image(image_id); 1580b32628eSAntonio Nino Diaz 1590b32628eSAntonio Nino Diaz console_flush(); 1607baff11fSYatharth Kochar } 1617baff11fSYatharth Kochar 1627baff11fSYatharth Kochar /******************************************************************************* 1637baff11fSYatharth Kochar * This function locates and loads the BL2 raw binary image in the trusted SRAM. 1647baff11fSYatharth Kochar * Called by the primary cpu after a cold boot. 1657baff11fSYatharth Kochar * TODO: Add support for alternative image load mechanism e.g using virtio/elf 1667baff11fSYatharth Kochar * loader etc. 1677baff11fSYatharth Kochar ******************************************************************************/ 168ce3f9a6dSRoberto Vargas static void bl1_load_bl2(void) 1697baff11fSYatharth Kochar { 1703443a702SJohn Powell image_desc_t *desc; 1713443a702SJohn Powell image_info_t *info; 1727baff11fSYatharth Kochar int err; 1737baff11fSYatharth Kochar 1747baff11fSYatharth Kochar /* Get the image descriptor */ 1753443a702SJohn Powell desc = bl1_plat_get_image_desc(BL2_IMAGE_ID); 1763443a702SJohn Powell assert(desc != NULL); 1777baff11fSYatharth Kochar 1787baff11fSYatharth Kochar /* Get the image info */ 1793443a702SJohn Powell info = &desc->image_info; 18016948ae1SJuan Castillo INFO("BL1: Loading BL2\n"); 18116948ae1SJuan Castillo 182566034fcSSoby Mathew err = bl1_plat_handle_pre_image_load(BL2_IMAGE_ID); 1833443a702SJohn Powell if (err != 0) { 18411f001cbSMasahiro Yamada ERROR("Failure in pre image load handling of BL2 (%d)\n", err); 18511f001cbSMasahiro Yamada plat_error_handler(err); 18611f001cbSMasahiro Yamada } 18711f001cbSMasahiro Yamada 1883443a702SJohn Powell err = load_auth_image(BL2_IMAGE_ID, info); 1893443a702SJohn Powell if (err != 0) { 1906ad2e461SDan Handley ERROR("Failed to load BL2 firmware.\n"); 19140fc6cd1SJuan Castillo plat_error_handler(err); 1924112bfa0SVikram Kanigiri } 19301df3c14SJuan Castillo 19411f001cbSMasahiro Yamada /* Allow platform to handle image information. */ 195566034fcSSoby Mathew err = bl1_plat_handle_post_image_load(BL2_IMAGE_ID); 1963443a702SJohn Powell if (err != 0) { 19711f001cbSMasahiro Yamada ERROR("Failure in post image load handling of BL2 (%d)\n", err); 19811f001cbSMasahiro Yamada plat_error_handler(err); 19911f001cbSMasahiro Yamada } 20011f001cbSMasahiro Yamada 2017baff11fSYatharth Kochar NOTICE("BL1: Booting BL2\n"); 2024f6ad66aSAchin Gupta } 2034f6ad66aSAchin Gupta 2044f6ad66aSAchin Gupta /******************************************************************************* 205f3b4914bSYatharth Kochar * Function called just before handing over to the next BL to inform the user 206f3b4914bSYatharth Kochar * about the boot progress. In debug mode, also print details about the BL 207f3b4914bSYatharth Kochar * image's execution context. 2084f6ad66aSAchin Gupta ******************************************************************************/ 209f3b4914bSYatharth Kochar void bl1_print_next_bl_ep_info(const entry_point_info_t *bl_ep_info) 2104f6ad66aSAchin Gupta { 211402b3cf8SJulius Werner #ifdef __aarch64__ 212d178637dSJuan Castillo NOTICE("BL1: Booting BL31\n"); 213402b3cf8SJulius Werner #else 214402b3cf8SJulius Werner NOTICE("BL1: Booting BL32\n"); 215402b3cf8SJulius Werner #endif /* __aarch64__ */ 216f3b4914bSYatharth Kochar print_entry_point_info(bl_ep_info); 2174f6ad66aSAchin Gupta } 21835e8c766SSandrine Bailleux 21935e8c766SSandrine Bailleux #if SPIN_ON_BL1_EXIT 22035e8c766SSandrine Bailleux void print_debug_loop_message(void) 22135e8c766SSandrine Bailleux { 22235e8c766SSandrine Bailleux NOTICE("BL1: Debug loop, spinning forever\n"); 22335e8c766SSandrine Bailleux NOTICE("BL1: Please connect the debugger to continue\n"); 22435e8c766SSandrine Bailleux } 22535e8c766SSandrine Bailleux #endif 22648bfb88eSYatharth Kochar 22748bfb88eSYatharth Kochar /******************************************************************************* 22848bfb88eSYatharth Kochar * Top level handler for servicing BL1 SMCs. 22948bfb88eSYatharth Kochar ******************************************************************************/ 2302fe75a2dSZelalem u_register_t bl1_smc_handler(unsigned int smc_fid, 2312fe75a2dSZelalem u_register_t x1, 2322fe75a2dSZelalem u_register_t x2, 2332fe75a2dSZelalem u_register_t x3, 2342fe75a2dSZelalem u_register_t x4, 23548bfb88eSYatharth Kochar void *cookie, 23648bfb88eSYatharth Kochar void *handle, 23748bfb88eSYatharth Kochar unsigned int flags) 23848bfb88eSYatharth Kochar { 239a14988c6SJimmy Brisson /* BL1 Service UUID */ 240a14988c6SJimmy Brisson DEFINE_SVC_UUID2(bl1_svc_uid, 241a14988c6SJimmy Brisson U(0xd46739fd), 0xcb72, 0x9a4d, 0xb5, 0x75, 242a14988c6SJimmy Brisson 0x67, 0x15, 0xd6, 0xf4, 0xbb, 0x4a); 243a14988c6SJimmy Brisson 24448bfb88eSYatharth Kochar 24548bfb88eSYatharth Kochar #if TRUSTED_BOARD_BOOT 24648bfb88eSYatharth Kochar /* 24748bfb88eSYatharth Kochar * Dispatch FWU calls to FWU SMC handler and return its return 24848bfb88eSYatharth Kochar * value 24948bfb88eSYatharth Kochar */ 25048bfb88eSYatharth Kochar if (is_fwu_fid(smc_fid)) { 25148bfb88eSYatharth Kochar return bl1_fwu_smc_handler(smc_fid, x1, x2, x3, x4, cookie, 25248bfb88eSYatharth Kochar handle, flags); 25348bfb88eSYatharth Kochar } 25448bfb88eSYatharth Kochar #endif 25548bfb88eSYatharth Kochar 25648bfb88eSYatharth Kochar switch (smc_fid) { 25748bfb88eSYatharth Kochar case BL1_SMC_CALL_COUNT: 25848bfb88eSYatharth Kochar SMC_RET1(handle, BL1_NUM_SMC_CALLS); 25948bfb88eSYatharth Kochar 26048bfb88eSYatharth Kochar case BL1_SMC_UID: 26148bfb88eSYatharth Kochar SMC_UUID_RET(handle, bl1_svc_uid); 26248bfb88eSYatharth Kochar 26348bfb88eSYatharth Kochar case BL1_SMC_VERSION: 26448bfb88eSYatharth Kochar SMC_RET1(handle, BL1_SMC_MAJOR_VER | BL1_SMC_MINOR_VER); 26548bfb88eSYatharth Kochar 26648bfb88eSYatharth Kochar default: 26748bfb88eSYatharth Kochar WARN("Unimplemented BL1 SMC Call: 0x%x\n", smc_fid); 26848bfb88eSYatharth Kochar SMC_RET1(handle, SMC_UNK); 26948bfb88eSYatharth Kochar } 2703443a702SJohn Powell } 271a4409008Sdp-arm 272a4409008Sdp-arm /******************************************************************************* 273a4409008Sdp-arm * BL1 SMC wrapper. This function is only used in AArch32 mode to ensure ABI 274a4409008Sdp-arm * compliance when invoking bl1_smc_handler. 275a4409008Sdp-arm ******************************************************************************/ 2762fe75a2dSZelalem u_register_t bl1_smc_wrapper(uint32_t smc_fid, 277a4409008Sdp-arm void *cookie, 278a4409008Sdp-arm void *handle, 279a4409008Sdp-arm unsigned int flags) 280a4409008Sdp-arm { 2812fe75a2dSZelalem u_register_t x1, x2, x3, x4; 282a4409008Sdp-arm 283466bb285SZelalem assert(handle != NULL); 284a4409008Sdp-arm 285a4409008Sdp-arm get_smc_params_from_ctx(handle, x1, x2, x3, x4); 286a4409008Sdp-arm return bl1_smc_handler(smc_fid, x1, x2, x3, x4, cookie, handle, flags); 287a4409008Sdp-arm } 288