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 7c228956aSSoby Mathew #include <assert.h> 8c228956aSSoby Mathew #include <string.h> 9c228956aSSoby Mathew 10*09d40e0eSAntonio Nino Diaz #include <platform_def.h> 11*09d40e0eSAntonio Nino Diaz 12*09d40e0eSAntonio Nino Diaz #include <common/debug.h> 13*09d40e0eSAntonio Nino Diaz #include <common/desc_image_load.h> 14*09d40e0eSAntonio Nino Diaz #include <common/tbbr/tbbr_img_def.h> 15*09d40e0eSAntonio Nino Diaz #if TRUSTED_BOARD_BOOT 16*09d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_config.h> 17*09d40e0eSAntonio Nino Diaz #endif 18*09d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 19*09d40e0eSAntonio Nino Diaz 20*09d40e0eSAntonio Nino Diaz #include <arm_dyn_cfg_helpers.h> 21*09d40e0eSAntonio Nino Diaz #include <plat_arm.h> 22c228956aSSoby Mathew 2360e19f57SAntonio Nino Diaz /* Variable to store the address to TB_FW_CONFIG passed from BL1 */ 24cab0b5b0SSoby Mathew static void *tb_fw_cfg_dtb; 2563cc2658SJohn Tsichritzis static size_t tb_fw_cfg_dtb_size; 26cab0b5b0SSoby Mathew 27ba597da7SJohn Tsichritzis 28ba597da7SJohn Tsichritzis #if TRUSTED_BOARD_BOOT 29ba597da7SJohn Tsichritzis 30ba597da7SJohn Tsichritzis static void *mbedtls_heap_addr; 31ba597da7SJohn Tsichritzis static size_t mbedtls_heap_size; 32ba597da7SJohn Tsichritzis 33ba597da7SJohn Tsichritzis /* 34ba597da7SJohn Tsichritzis * This function is the implementation of the shared Mbed TLS heap between 35ba597da7SJohn Tsichritzis * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1 36ba597da7SJohn Tsichritzis * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file 37ba597da7SJohn Tsichritzis * which is a DTB. 38ba597da7SJohn Tsichritzis * 39ba597da7SJohn Tsichritzis * This function is placed inside an #if directive for the below reasons: 40ba597da7SJohn Tsichritzis * - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot 41ba597da7SJohn Tsichritzis * is enabled. 42ba597da7SJohn Tsichritzis * - This implementation requires the DTB to be present so that BL1 has a 4360e19f57SAntonio Nino Diaz * mechanism to pass the pointer to BL2. 44ba597da7SJohn Tsichritzis */ 45ba597da7SJohn Tsichritzis int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size) 46ba597da7SJohn Tsichritzis { 47ba597da7SJohn Tsichritzis assert(heap_addr != NULL); 48ba597da7SJohn Tsichritzis assert(heap_size != NULL); 49ba597da7SJohn Tsichritzis 50ba597da7SJohn Tsichritzis #if defined(IMAGE_BL1) || BL2_AT_EL3 51ba597da7SJohn Tsichritzis 52ba597da7SJohn Tsichritzis /* If in BL1 or BL2_AT_EL3 define a heap */ 53ba597da7SJohn Tsichritzis static unsigned char heap[TF_MBEDTLS_HEAP_SIZE]; 54ba597da7SJohn Tsichritzis 55ba597da7SJohn Tsichritzis *heap_addr = heap; 56ba597da7SJohn Tsichritzis *heap_size = sizeof(heap); 57ba597da7SJohn Tsichritzis mbedtls_heap_addr = heap; 58ba597da7SJohn Tsichritzis mbedtls_heap_size = sizeof(heap); 59ba597da7SJohn Tsichritzis 60ba597da7SJohn Tsichritzis #elif defined(IMAGE_BL2) 61ba597da7SJohn Tsichritzis 62ba597da7SJohn Tsichritzis int err; 63ba597da7SJohn Tsichritzis 64ba597da7SJohn Tsichritzis /* If in BL2, retrieve the already allocated heap's info from DTB */ 65a606031eSJohn Tsichritzis if (tb_fw_cfg_dtb != NULL) { 66ba597da7SJohn Tsichritzis err = arm_get_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, heap_addr, 67ba597da7SJohn Tsichritzis heap_size); 68ba597da7SJohn Tsichritzis if (err < 0) { 69a606031eSJohn Tsichritzis ERROR("BL2: unable to retrieve shared Mbed TLS heap information from DTB\n"); 70a606031eSJohn Tsichritzis panic(); 71a606031eSJohn Tsichritzis } 72a606031eSJohn Tsichritzis } else { 73a606031eSJohn Tsichritzis ERROR("BL2: DTB missing, cannot get Mbed TLS heap\n"); 74ba597da7SJohn Tsichritzis panic(); 75ba597da7SJohn Tsichritzis } 76ba597da7SJohn Tsichritzis #endif 77ba597da7SJohn Tsichritzis 78ba597da7SJohn Tsichritzis return 0; 79ba597da7SJohn Tsichritzis } 80ba597da7SJohn Tsichritzis 81ba597da7SJohn Tsichritzis /* 82ba597da7SJohn Tsichritzis * Puts the shared Mbed TLS heap information to the DTB. 83ba597da7SJohn Tsichritzis * Executed only from BL1. 84ba597da7SJohn Tsichritzis */ 85ba597da7SJohn Tsichritzis void arm_bl1_set_mbedtls_heap(void) 86ba597da7SJohn Tsichritzis { 87ba597da7SJohn Tsichritzis int err; 88ba597da7SJohn Tsichritzis 89ba597da7SJohn Tsichritzis /* 90ba597da7SJohn Tsichritzis * If tb_fw_cfg_dtb==NULL then DTB is not present for the current 91ba597da7SJohn Tsichritzis * platform. As such, we don't attempt to write to the DTB at all. 92ba597da7SJohn Tsichritzis * 93ba597da7SJohn Tsichritzis * If mbedtls_heap_addr==NULL, then it means we are using the default 94ba597da7SJohn Tsichritzis * heap implementation. As such, BL2 will have its own heap for sure 95ba597da7SJohn Tsichritzis * and hence there is no need to pass any information to the DTB. 96ba597da7SJohn Tsichritzis * 97ba597da7SJohn Tsichritzis * In the latter case, if we still wanted to write in the DTB the heap 98ba597da7SJohn Tsichritzis * information, we would need to call plat_get_mbedtls_heap to retrieve 99ba597da7SJohn Tsichritzis * the default heap's address and size. 100ba597da7SJohn Tsichritzis */ 101ba597da7SJohn Tsichritzis if ((tb_fw_cfg_dtb != NULL) && (mbedtls_heap_addr != NULL)) { 102ba597da7SJohn Tsichritzis err = arm_set_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, 103ba597da7SJohn Tsichritzis mbedtls_heap_addr, mbedtls_heap_size); 104ba597da7SJohn Tsichritzis if (err < 0) { 105a606031eSJohn Tsichritzis ERROR("BL1: unable to write shared Mbed TLS heap information to DTB\n"); 106ba597da7SJohn Tsichritzis panic(); 107ba597da7SJohn Tsichritzis } 10863cc2658SJohn Tsichritzis /* 10963cc2658SJohn Tsichritzis * Ensure that the info written to the DTB is visible to other 11063cc2658SJohn Tsichritzis * images. It's critical because BL2 won't be able to proceed 11163cc2658SJohn Tsichritzis * without the heap info. 11263cc2658SJohn Tsichritzis */ 11363cc2658SJohn Tsichritzis flush_dcache_range((uintptr_t)tb_fw_cfg_dtb, 11463cc2658SJohn Tsichritzis tb_fw_cfg_dtb_size); 115ba597da7SJohn Tsichritzis } 116ba597da7SJohn Tsichritzis } 117ba597da7SJohn Tsichritzis 118ba597da7SJohn Tsichritzis #endif /* TRUSTED_BOARD_BOOT */ 119ba597da7SJohn Tsichritzis 120c228956aSSoby Mathew /* 121c228956aSSoby Mathew * Helper function to load TB_FW_CONFIG and populate the load information to 122c228956aSSoby Mathew * arg0 of BL2 entrypoint info. 123c228956aSSoby Mathew */ 124c228956aSSoby Mathew void arm_load_tb_fw_config(void) 125c228956aSSoby Mathew { 126c228956aSSoby Mathew int err; 127b8a02d53SAntonio Nino Diaz uintptr_t config_base = 0UL; 128b8a02d53SAntonio Nino Diaz image_desc_t *desc; 129c228956aSSoby Mathew 130c228956aSSoby Mathew image_desc_t arm_tb_fw_info = { 131c228956aSSoby Mathew .image_id = TB_FW_CONFIG_ID, 132c228956aSSoby Mathew SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY, 133c228956aSSoby Mathew VERSION_2, image_info_t, 0), 134c228956aSSoby Mathew .image_info.image_base = ARM_TB_FW_CONFIG_BASE, 13563cc2658SJohn Tsichritzis .image_info.image_max_size = 13663cc2658SJohn Tsichritzis ARM_TB_FW_CONFIG_LIMIT - ARM_TB_FW_CONFIG_BASE 137c228956aSSoby Mathew }; 138c228956aSSoby Mathew 139c228956aSSoby Mathew VERBOSE("BL1: Loading TB_FW_CONFIG\n"); 140c228956aSSoby Mathew err = load_auth_image(TB_FW_CONFIG_ID, &arm_tb_fw_info.image_info); 141da5f2745SSoby Mathew if (err != 0) { 142c228956aSSoby Mathew /* Return if TB_FW_CONFIG is not loaded */ 143c228956aSSoby Mathew VERBOSE("Failed to load TB_FW_CONFIG\n"); 144c228956aSSoby Mathew return; 145c228956aSSoby Mathew } 146c228956aSSoby Mathew 147ba597da7SJohn Tsichritzis /* At this point we know that a DTB is indeed available */ 148c228956aSSoby Mathew config_base = arm_tb_fw_info.image_info.image_base; 149ba597da7SJohn Tsichritzis tb_fw_cfg_dtb = (void *)config_base; 15063cc2658SJohn Tsichritzis tb_fw_cfg_dtb_size = (size_t)arm_tb_fw_info.image_info.image_max_size; 151c228956aSSoby Mathew 152c228956aSSoby Mathew /* The BL2 ep_info arg0 is modified to point to TB_FW_CONFIG */ 153b8a02d53SAntonio Nino Diaz desc = bl1_plat_get_image_desc(BL2_IMAGE_ID); 154b8a02d53SAntonio Nino Diaz assert(desc != NULL); 155b8a02d53SAntonio Nino Diaz desc->ep_info.args.arg0 = config_base; 156c228956aSSoby Mathew 157b8a02d53SAntonio Nino Diaz INFO("BL1: TB_FW_CONFIG loaded at address = 0x%lx\n", config_base); 1586e79f9fdSSoby Mathew 1596e79f9fdSSoby Mathew #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH) 1606e79f9fdSSoby Mathew int tb_fw_node; 1616e79f9fdSSoby Mathew uint32_t disable_auth = 0; 1626e79f9fdSSoby Mathew 1636e79f9fdSSoby Mathew err = arm_dyn_tb_fw_cfg_init((void *)config_base, &tb_fw_node); 1646e79f9fdSSoby Mathew if (err < 0) { 165355e0967SJohn Tsichritzis ERROR("Invalid TB_FW_CONFIG loaded\n"); 166355e0967SJohn Tsichritzis panic(); 1676e79f9fdSSoby Mathew } 1686e79f9fdSSoby Mathew 1696e79f9fdSSoby Mathew err = arm_dyn_get_disable_auth((void *)config_base, tb_fw_node, &disable_auth); 1706e79f9fdSSoby Mathew if (err < 0) 1716e79f9fdSSoby Mathew return; 1726e79f9fdSSoby Mathew 1736e79f9fdSSoby Mathew if (disable_auth == 1) 1746e79f9fdSSoby Mathew dyn_disable_auth(); 1756e79f9fdSSoby Mathew #endif 176c228956aSSoby Mathew } 177c228956aSSoby Mathew 178cab0b5b0SSoby Mathew /* 179cab0b5b0SSoby Mathew * BL2 utility function to set the address of TB_FW_CONFIG passed from BL1. 180cab0b5b0SSoby Mathew */ 181cab0b5b0SSoby Mathew void arm_bl2_set_tb_cfg_addr(void *dtb) 182cab0b5b0SSoby Mathew { 183da5f2745SSoby Mathew assert(dtb != NULL); 184cab0b5b0SSoby Mathew tb_fw_cfg_dtb = dtb; 185cab0b5b0SSoby Mathew } 186cab0b5b0SSoby Mathew 187cab0b5b0SSoby Mathew /* 188cab0b5b0SSoby Mathew * BL2 utility function to initialize dynamic configuration specified by 1891d71ba14SSoby Mathew * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if 1901d71ba14SSoby Mathew * specified in TB_FW_CONFIG. 191cab0b5b0SSoby Mathew */ 192cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void) 193cab0b5b0SSoby Mathew { 1941d71ba14SSoby Mathew int err = 0, tb_fw_node; 1951d71ba14SSoby Mathew unsigned int i; 1961d71ba14SSoby Mathew bl_mem_params_node_t *cfg_mem_params = NULL; 1971d71ba14SSoby Mathew uint64_t image_base; 1981d71ba14SSoby Mathew uint32_t image_size; 1991d71ba14SSoby Mathew const unsigned int config_ids[] = { 2001d71ba14SSoby Mathew HW_CONFIG_ID, 2011d71ba14SSoby Mathew SOC_FW_CONFIG_ID, 2021d71ba14SSoby Mathew NT_FW_CONFIG_ID, 2031d71ba14SSoby Mathew #ifdef SPD_tspd 2041d71ba14SSoby Mathew /* Currently tos_fw_config is only present for TSP */ 2051d71ba14SSoby Mathew TOS_FW_CONFIG_ID 2061d71ba14SSoby Mathew #endif 2071d71ba14SSoby Mathew }; 208cab0b5b0SSoby Mathew 209cab0b5b0SSoby Mathew if (tb_fw_cfg_dtb == NULL) { 210cab0b5b0SSoby Mathew VERBOSE("No TB_FW_CONFIG specified\n"); 211cab0b5b0SSoby Mathew return; 212cab0b5b0SSoby Mathew } 213cab0b5b0SSoby Mathew 214432f0ad0SJohn Tsichritzis err = arm_dyn_tb_fw_cfg_init(tb_fw_cfg_dtb, &tb_fw_node); 215cab0b5b0SSoby Mathew if (err < 0) { 216cab0b5b0SSoby Mathew ERROR("Invalid TB_FW_CONFIG passed from BL1\n"); 217cab0b5b0SSoby Mathew panic(); 218cab0b5b0SSoby Mathew } 219cab0b5b0SSoby Mathew 2201d71ba14SSoby Mathew /* Iterate through all the fw config IDs */ 2211d71ba14SSoby Mathew for (i = 0; i < ARRAY_SIZE(config_ids); i++) { 2221d71ba14SSoby Mathew /* Get the config load address and size from TB_FW_CONFIG */ 2231d71ba14SSoby Mathew cfg_mem_params = get_bl_mem_params_node(config_ids[i]); 2241d71ba14SSoby Mathew if (cfg_mem_params == NULL) { 225cab0b5b0SSoby Mathew VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n"); 2261d71ba14SSoby Mathew continue; 227cab0b5b0SSoby Mathew } 228cab0b5b0SSoby Mathew 229432f0ad0SJohn Tsichritzis err = arm_dyn_get_config_load_info(tb_fw_cfg_dtb, tb_fw_node, 2301d71ba14SSoby Mathew config_ids[i], &image_base, &image_size); 231cab0b5b0SSoby Mathew if (err < 0) { 2321d71ba14SSoby Mathew VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n", 2331d71ba14SSoby Mathew config_ids[i]); 2341d71ba14SSoby Mathew continue; 235cab0b5b0SSoby Mathew } 236cab0b5b0SSoby Mathew 2371d71ba14SSoby Mathew /* 2381d71ba14SSoby Mathew * Do some runtime checks on the load addresses of soc_fw_config, 2391d71ba14SSoby Mathew * tos_fw_config, nt_fw_config. This is not a comprehensive check 2401d71ba14SSoby Mathew * of all invalid addresses but to prevent trivial porting errors. 2411d71ba14SSoby Mathew */ 2421d71ba14SSoby Mathew if (config_ids[i] != HW_CONFIG_ID) { 2431d71ba14SSoby Mathew 244b8a02d53SAntonio Nino Diaz if (check_uptr_overflow(image_base, image_size)) 2451d71ba14SSoby Mathew continue; 2461d71ba14SSoby Mathew 247c099cd39SSoby Mathew /* Ensure the configs don't overlap with BL31 */ 248c099cd39SSoby Mathew if ((image_base > BL31_BASE) || ((image_base + image_size) > BL31_BASE)) 2491d71ba14SSoby Mathew continue; 2501d71ba14SSoby Mathew 2511d71ba14SSoby Mathew /* Ensure the configs are loaded in a valid address */ 2521d71ba14SSoby Mathew if (image_base < ARM_BL_RAM_BASE) 2531d71ba14SSoby Mathew continue; 2541d71ba14SSoby Mathew #ifdef BL32_BASE 2551d71ba14SSoby Mathew /* 2561d71ba14SSoby Mathew * If BL32 is present, ensure that the configs don't 2571d71ba14SSoby Mathew * overlap with it. 2581d71ba14SSoby Mathew */ 2591d71ba14SSoby Mathew if (image_base >= BL32_BASE && image_base <= BL32_LIMIT) 2601d71ba14SSoby Mathew continue; 2611d71ba14SSoby Mathew #endif 2621d71ba14SSoby Mathew } 2631d71ba14SSoby Mathew 2641d71ba14SSoby Mathew 2651d71ba14SSoby Mathew cfg_mem_params->image_info.image_base = (uintptr_t)image_base; 2661d71ba14SSoby Mathew cfg_mem_params->image_info.image_max_size = image_size; 2671d71ba14SSoby Mathew 268cab0b5b0SSoby Mathew /* Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from HW_CONFIG node */ 2691d71ba14SSoby Mathew cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING; 2701d71ba14SSoby Mathew } 2716e79f9fdSSoby Mathew 2726e79f9fdSSoby Mathew #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH) 2736e79f9fdSSoby Mathew uint32_t disable_auth = 0; 2746e79f9fdSSoby Mathew 275432f0ad0SJohn Tsichritzis err = arm_dyn_get_disable_auth(tb_fw_cfg_dtb, tb_fw_node, 2766e79f9fdSSoby Mathew &disable_auth); 2776e79f9fdSSoby Mathew if (err < 0) 2786e79f9fdSSoby Mathew return; 2796e79f9fdSSoby Mathew 2806e79f9fdSSoby Mathew if (disable_auth == 1) 2816e79f9fdSSoby Mathew dyn_disable_auth(); 2826e79f9fdSSoby Mathew #endif 283cab0b5b0SSoby Mathew } 284