1c228956aSSoby Mathew /* 2c228956aSSoby Mathew * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. 3c228956aSSoby Mathew * 4c228956aSSoby Mathew * SPDX-License-Identifier: BSD-3-Clause 5c228956aSSoby Mathew */ 6c228956aSSoby Mathew 7cab0b5b0SSoby Mathew #include <arm_dyn_cfg_helpers.h> 8c228956aSSoby Mathew #include <assert.h> 9c228956aSSoby Mathew #include <debug.h> 10c228956aSSoby Mathew #include <desc_image_load.h> 11*ba597da7SJohn Tsichritzis #if TRUSTED_BOARD_BOOT 12*ba597da7SJohn Tsichritzis #include <mbedtls_config.h> 13*ba597da7SJohn Tsichritzis #endif 14da5f2745SSoby Mathew #include <plat_arm.h> 15c228956aSSoby Mathew #include <platform.h> 16c228956aSSoby Mathew #include <platform_def.h> 17c228956aSSoby Mathew #include <string.h> 18c228956aSSoby Mathew #include <tbbr_img_def.h> 19c228956aSSoby Mathew 20c228956aSSoby Mathew #if LOAD_IMAGE_V2 21c228956aSSoby Mathew 22*ba597da7SJohn Tsichritzis /* Variable to store the address of TB_FW_CONFIG file */ 23cab0b5b0SSoby Mathew static void *tb_fw_cfg_dtb; 24cab0b5b0SSoby Mathew 25*ba597da7SJohn Tsichritzis 26*ba597da7SJohn Tsichritzis #if TRUSTED_BOARD_BOOT 27*ba597da7SJohn Tsichritzis 28*ba597da7SJohn Tsichritzis static void *mbedtls_heap_addr; 29*ba597da7SJohn Tsichritzis static size_t mbedtls_heap_size; 30*ba597da7SJohn Tsichritzis 31*ba597da7SJohn Tsichritzis /* 32*ba597da7SJohn Tsichritzis * This function is the implementation of the shared Mbed TLS heap between 33*ba597da7SJohn Tsichritzis * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1 34*ba597da7SJohn Tsichritzis * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file 35*ba597da7SJohn Tsichritzis * which is a DTB. 36*ba597da7SJohn Tsichritzis * 37*ba597da7SJohn Tsichritzis * This function is placed inside an #if directive for the below reasons: 38*ba597da7SJohn Tsichritzis * - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot 39*ba597da7SJohn Tsichritzis * is enabled. 40*ba597da7SJohn Tsichritzis * - This implementation requires the DTB to be present so that BL1 has a 41*ba597da7SJohn Tsichritzis * mechanism to pass the pointer to BL2. If LOAD_IMAGE_V2=0 then 42*ba597da7SJohn Tsichritzis * TB_FW_CONFIG is not present, which means that this implementation 43*ba597da7SJohn Tsichritzis * cannot be applied. 44*ba597da7SJohn Tsichritzis */ 45*ba597da7SJohn Tsichritzis int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size) 46*ba597da7SJohn Tsichritzis { 47*ba597da7SJohn Tsichritzis assert(heap_addr != NULL); 48*ba597da7SJohn Tsichritzis assert(heap_size != NULL); 49*ba597da7SJohn Tsichritzis 50*ba597da7SJohn Tsichritzis #if defined(IMAGE_BL1) || BL2_AT_EL3 51*ba597da7SJohn Tsichritzis 52*ba597da7SJohn Tsichritzis /* If in BL1 or BL2_AT_EL3 define a heap */ 53*ba597da7SJohn Tsichritzis static unsigned char heap[TF_MBEDTLS_HEAP_SIZE]; 54*ba597da7SJohn Tsichritzis 55*ba597da7SJohn Tsichritzis *heap_addr = heap; 56*ba597da7SJohn Tsichritzis *heap_size = sizeof(heap); 57*ba597da7SJohn Tsichritzis mbedtls_heap_addr = heap; 58*ba597da7SJohn Tsichritzis mbedtls_heap_size = sizeof(heap); 59*ba597da7SJohn Tsichritzis 60*ba597da7SJohn Tsichritzis #elif defined(IMAGE_BL2) 61*ba597da7SJohn Tsichritzis 62*ba597da7SJohn Tsichritzis int err; 63*ba597da7SJohn Tsichritzis 64*ba597da7SJohn Tsichritzis /* If in BL2, retrieve the already allocated heap's info from DTB */ 65*ba597da7SJohn Tsichritzis err = arm_get_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, heap_addr, 66*ba597da7SJohn Tsichritzis heap_size); 67*ba597da7SJohn Tsichritzis if (err < 0) { 68*ba597da7SJohn Tsichritzis ERROR("BL2: unable to retrieve shared Mbed TLS heap " 69*ba597da7SJohn Tsichritzis "information from DTB\n"); 70*ba597da7SJohn Tsichritzis panic(); 71*ba597da7SJohn Tsichritzis } 72*ba597da7SJohn Tsichritzis #endif 73*ba597da7SJohn Tsichritzis 74*ba597da7SJohn Tsichritzis return 0; 75*ba597da7SJohn Tsichritzis } 76*ba597da7SJohn Tsichritzis 77*ba597da7SJohn Tsichritzis /* 78*ba597da7SJohn Tsichritzis * Puts the shared Mbed TLS heap information to the DTB. 79*ba597da7SJohn Tsichritzis * Executed only from BL1. 80*ba597da7SJohn Tsichritzis */ 81*ba597da7SJohn Tsichritzis void arm_bl1_set_mbedtls_heap(void) 82*ba597da7SJohn Tsichritzis { 83*ba597da7SJohn Tsichritzis int err; 84*ba597da7SJohn Tsichritzis 85*ba597da7SJohn Tsichritzis /* 86*ba597da7SJohn Tsichritzis * If tb_fw_cfg_dtb==NULL then DTB is not present for the current 87*ba597da7SJohn Tsichritzis * platform. As such, we don't attempt to write to the DTB at all. 88*ba597da7SJohn Tsichritzis * 89*ba597da7SJohn Tsichritzis * If mbedtls_heap_addr==NULL, then it means we are using the default 90*ba597da7SJohn Tsichritzis * heap implementation. As such, BL2 will have its own heap for sure 91*ba597da7SJohn Tsichritzis * and hence there is no need to pass any information to the DTB. 92*ba597da7SJohn Tsichritzis * 93*ba597da7SJohn Tsichritzis * In the latter case, if we still wanted to write in the DTB the heap 94*ba597da7SJohn Tsichritzis * information, we would need to call plat_get_mbedtls_heap to retrieve 95*ba597da7SJohn Tsichritzis * the default heap's address and size. 96*ba597da7SJohn Tsichritzis */ 97*ba597da7SJohn Tsichritzis if ((tb_fw_cfg_dtb != NULL) && (mbedtls_heap_addr != NULL)) { 98*ba597da7SJohn Tsichritzis err = arm_set_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, 99*ba597da7SJohn Tsichritzis mbedtls_heap_addr, mbedtls_heap_size); 100*ba597da7SJohn Tsichritzis if (err < 0) { 101*ba597da7SJohn Tsichritzis ERROR("BL1: unable to write shared Mbed TLS heap " 102*ba597da7SJohn Tsichritzis "information to DTB\n"); 103*ba597da7SJohn Tsichritzis panic(); 104*ba597da7SJohn Tsichritzis } 105*ba597da7SJohn Tsichritzis } 106*ba597da7SJohn Tsichritzis } 107*ba597da7SJohn Tsichritzis 108*ba597da7SJohn Tsichritzis #endif /* TRUSTED_BOARD_BOOT */ 109*ba597da7SJohn Tsichritzis 110c228956aSSoby Mathew /* 111c228956aSSoby Mathew * Helper function to load TB_FW_CONFIG and populate the load information to 112c228956aSSoby Mathew * arg0 of BL2 entrypoint info. 113c228956aSSoby Mathew */ 114c228956aSSoby Mathew void arm_load_tb_fw_config(void) 115c228956aSSoby Mathew { 116c228956aSSoby Mathew int err; 117c228956aSSoby Mathew uintptr_t config_base = 0; 118c228956aSSoby Mathew image_desc_t *image_desc; 119c228956aSSoby Mathew 120c228956aSSoby Mathew image_desc_t arm_tb_fw_info = { 121c228956aSSoby Mathew .image_id = TB_FW_CONFIG_ID, 122c228956aSSoby Mathew SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY, 123c228956aSSoby Mathew VERSION_2, image_info_t, 0), 124c228956aSSoby Mathew .image_info.image_base = ARM_TB_FW_CONFIG_BASE, 125c228956aSSoby Mathew .image_info.image_max_size = ARM_TB_FW_CONFIG_LIMIT - ARM_TB_FW_CONFIG_BASE, 126c228956aSSoby Mathew }; 127c228956aSSoby Mathew 128c228956aSSoby Mathew VERBOSE("BL1: Loading TB_FW_CONFIG\n"); 129c228956aSSoby Mathew err = load_auth_image(TB_FW_CONFIG_ID, &arm_tb_fw_info.image_info); 130da5f2745SSoby Mathew if (err != 0) { 131c228956aSSoby Mathew /* Return if TB_FW_CONFIG is not loaded */ 132c228956aSSoby Mathew VERBOSE("Failed to load TB_FW_CONFIG\n"); 133c228956aSSoby Mathew return; 134c228956aSSoby Mathew } 135c228956aSSoby Mathew 136*ba597da7SJohn Tsichritzis /* At this point we know that a DTB is indeed available */ 137c228956aSSoby Mathew config_base = arm_tb_fw_info.image_info.image_base; 138*ba597da7SJohn Tsichritzis tb_fw_cfg_dtb = (void *)config_base; 139c228956aSSoby Mathew 140c228956aSSoby Mathew /* The BL2 ep_info arg0 is modified to point to TB_FW_CONFIG */ 141c228956aSSoby Mathew image_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID); 142da5f2745SSoby Mathew assert(image_desc != NULL); 143c228956aSSoby Mathew image_desc->ep_info.args.arg0 = config_base; 144c228956aSSoby Mathew 145c228956aSSoby Mathew INFO("BL1: TB_FW_CONFIG loaded at address = %p\n", 146c228956aSSoby Mathew (void *) config_base); 1476e79f9fdSSoby Mathew 1486e79f9fdSSoby Mathew #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH) 1496e79f9fdSSoby Mathew int tb_fw_node; 1506e79f9fdSSoby Mathew uint32_t disable_auth = 0; 1516e79f9fdSSoby Mathew 1526e79f9fdSSoby Mathew err = arm_dyn_tb_fw_cfg_init((void *)config_base, &tb_fw_node); 1536e79f9fdSSoby Mathew if (err < 0) { 154355e0967SJohn Tsichritzis ERROR("Invalid TB_FW_CONFIG loaded\n"); 155355e0967SJohn Tsichritzis panic(); 1566e79f9fdSSoby Mathew } 1576e79f9fdSSoby Mathew 1586e79f9fdSSoby Mathew err = arm_dyn_get_disable_auth((void *)config_base, tb_fw_node, &disable_auth); 1596e79f9fdSSoby Mathew if (err < 0) 1606e79f9fdSSoby Mathew return; 1616e79f9fdSSoby Mathew 1626e79f9fdSSoby Mathew if (disable_auth == 1) 1636e79f9fdSSoby Mathew dyn_disable_auth(); 1646e79f9fdSSoby Mathew #endif 165c228956aSSoby Mathew } 166c228956aSSoby Mathew 167cab0b5b0SSoby Mathew /* 168cab0b5b0SSoby Mathew * BL2 utility function to set the address of TB_FW_CONFIG passed from BL1. 169cab0b5b0SSoby Mathew */ 170cab0b5b0SSoby Mathew void arm_bl2_set_tb_cfg_addr(void *dtb) 171cab0b5b0SSoby Mathew { 172da5f2745SSoby Mathew assert(dtb != NULL); 173cab0b5b0SSoby Mathew tb_fw_cfg_dtb = dtb; 174cab0b5b0SSoby Mathew } 175cab0b5b0SSoby Mathew 176cab0b5b0SSoby Mathew /* 177cab0b5b0SSoby Mathew * BL2 utility function to initialize dynamic configuration specified by 1781d71ba14SSoby Mathew * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if 1791d71ba14SSoby Mathew * specified in TB_FW_CONFIG. 180cab0b5b0SSoby Mathew */ 181cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void) 182cab0b5b0SSoby Mathew { 1831d71ba14SSoby Mathew int err = 0, tb_fw_node; 1841d71ba14SSoby Mathew unsigned int i; 1851d71ba14SSoby Mathew bl_mem_params_node_t *cfg_mem_params = NULL; 1861d71ba14SSoby Mathew uint64_t image_base; 1871d71ba14SSoby Mathew uint32_t image_size; 1881d71ba14SSoby Mathew const unsigned int config_ids[] = { 1891d71ba14SSoby Mathew HW_CONFIG_ID, 1901d71ba14SSoby Mathew SOC_FW_CONFIG_ID, 1911d71ba14SSoby Mathew NT_FW_CONFIG_ID, 1921d71ba14SSoby Mathew #ifdef SPD_tspd 1931d71ba14SSoby Mathew /* Currently tos_fw_config is only present for TSP */ 1941d71ba14SSoby Mathew TOS_FW_CONFIG_ID 1951d71ba14SSoby Mathew #endif 1961d71ba14SSoby Mathew }; 197cab0b5b0SSoby Mathew 198cab0b5b0SSoby Mathew if (tb_fw_cfg_dtb == NULL) { 199cab0b5b0SSoby Mathew VERBOSE("No TB_FW_CONFIG specified\n"); 200cab0b5b0SSoby Mathew return; 201cab0b5b0SSoby Mathew } 202cab0b5b0SSoby Mathew 203432f0ad0SJohn Tsichritzis err = arm_dyn_tb_fw_cfg_init(tb_fw_cfg_dtb, &tb_fw_node); 204cab0b5b0SSoby Mathew if (err < 0) { 205cab0b5b0SSoby Mathew ERROR("Invalid TB_FW_CONFIG passed from BL1\n"); 206cab0b5b0SSoby Mathew panic(); 207cab0b5b0SSoby Mathew } 208cab0b5b0SSoby Mathew 2091d71ba14SSoby Mathew /* Iterate through all the fw config IDs */ 2101d71ba14SSoby Mathew for (i = 0; i < ARRAY_SIZE(config_ids); i++) { 2111d71ba14SSoby Mathew /* Get the config load address and size from TB_FW_CONFIG */ 2121d71ba14SSoby Mathew cfg_mem_params = get_bl_mem_params_node(config_ids[i]); 2131d71ba14SSoby Mathew if (cfg_mem_params == NULL) { 214cab0b5b0SSoby Mathew VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n"); 2151d71ba14SSoby Mathew continue; 216cab0b5b0SSoby Mathew } 217cab0b5b0SSoby Mathew 218432f0ad0SJohn Tsichritzis err = arm_dyn_get_config_load_info(tb_fw_cfg_dtb, tb_fw_node, 2191d71ba14SSoby Mathew config_ids[i], &image_base, &image_size); 220cab0b5b0SSoby Mathew if (err < 0) { 2211d71ba14SSoby Mathew VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n", 2221d71ba14SSoby Mathew config_ids[i]); 2231d71ba14SSoby Mathew continue; 224cab0b5b0SSoby Mathew } 225cab0b5b0SSoby Mathew 2261d71ba14SSoby Mathew /* 2271d71ba14SSoby Mathew * Do some runtime checks on the load addresses of soc_fw_config, 2281d71ba14SSoby Mathew * tos_fw_config, nt_fw_config. This is not a comprehensive check 2291d71ba14SSoby Mathew * of all invalid addresses but to prevent trivial porting errors. 2301d71ba14SSoby Mathew */ 2311d71ba14SSoby Mathew if (config_ids[i] != HW_CONFIG_ID) { 2321d71ba14SSoby Mathew 2331d71ba14SSoby Mathew if (check_uptr_overflow(image_base, image_size) != 0) 2341d71ba14SSoby Mathew continue; 2351d71ba14SSoby Mathew 236c099cd39SSoby Mathew /* Ensure the configs don't overlap with BL31 */ 237c099cd39SSoby Mathew if ((image_base > BL31_BASE) || ((image_base + image_size) > BL31_BASE)) 2381d71ba14SSoby Mathew continue; 2391d71ba14SSoby Mathew 2401d71ba14SSoby Mathew /* Ensure the configs are loaded in a valid address */ 2411d71ba14SSoby Mathew if (image_base < ARM_BL_RAM_BASE) 2421d71ba14SSoby Mathew continue; 2431d71ba14SSoby Mathew #ifdef BL32_BASE 2441d71ba14SSoby Mathew /* 2451d71ba14SSoby Mathew * If BL32 is present, ensure that the configs don't 2461d71ba14SSoby Mathew * overlap with it. 2471d71ba14SSoby Mathew */ 2481d71ba14SSoby Mathew if (image_base >= BL32_BASE && image_base <= BL32_LIMIT) 2491d71ba14SSoby Mathew continue; 2501d71ba14SSoby Mathew #endif 2511d71ba14SSoby Mathew } 2521d71ba14SSoby Mathew 2531d71ba14SSoby Mathew 2541d71ba14SSoby Mathew cfg_mem_params->image_info.image_base = (uintptr_t)image_base; 2551d71ba14SSoby Mathew cfg_mem_params->image_info.image_max_size = image_size; 2561d71ba14SSoby Mathew 257cab0b5b0SSoby Mathew /* Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from HW_CONFIG node */ 2581d71ba14SSoby Mathew cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING; 2591d71ba14SSoby Mathew } 2606e79f9fdSSoby Mathew 2616e79f9fdSSoby Mathew #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH) 2626e79f9fdSSoby Mathew uint32_t disable_auth = 0; 2636e79f9fdSSoby Mathew 264432f0ad0SJohn Tsichritzis err = arm_dyn_get_disable_auth(tb_fw_cfg_dtb, tb_fw_node, 2656e79f9fdSSoby Mathew &disable_auth); 2666e79f9fdSSoby Mathew if (err < 0) 2676e79f9fdSSoby Mathew return; 2686e79f9fdSSoby Mathew 2696e79f9fdSSoby Mathew if (disable_auth == 1) 2706e79f9fdSSoby Mathew dyn_disable_auth(); 2716e79f9fdSSoby Mathew #endif 272cab0b5b0SSoby Mathew } 273cab0b5b0SSoby Mathew 274c228956aSSoby Mathew #endif /* LOAD_IMAGE_V2 */ 275