1c228956aSSoby Mathew /* 20cb64d01SAchin Gupta * Copyright (c) 2018-2020, 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> 96c77dfc5SLouis Mayencourt #include <libfdt.h> 10c228956aSSoby Mathew 1109d40e0eSAntonio Nino Diaz #include <platform_def.h> 1209d40e0eSAntonio Nino Diaz 1309d40e0eSAntonio Nino Diaz #include <common/debug.h> 1409d40e0eSAntonio Nino Diaz #include <common/desc_image_load.h> 1509d40e0eSAntonio Nino Diaz #include <common/tbbr/tbbr_img_def.h> 1609d40e0eSAntonio Nino Diaz #if TRUSTED_BOARD_BOOT 1709d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_config.h> 180ab49645SAlexei Fedorov #if MEASURED_BOOT 190ab49645SAlexei Fedorov #include <drivers/auth/crypto_mod.h> 200ab49645SAlexei Fedorov #include <mbedtls/md.h> 210ab49645SAlexei Fedorov #endif 2209d40e0eSAntonio Nino Diaz #endif 2325ac8794SLouis Mayencourt #include <lib/fconf/fconf.h> 2425ac8794SLouis Mayencourt #include <lib/fconf/fconf_dyn_cfg_getter.h> 256c972317SLouis Mayencourt #include <lib/fconf/fconf_tbbr_getter.h> 26bd9344f6SAntonio Nino Diaz #include <plat/arm/common/arm_dyn_cfg_helpers.h> 27bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h> 2809d40e0eSAntonio Nino Diaz 29ba597da7SJohn Tsichritzis #if TRUSTED_BOARD_BOOT 30ba597da7SJohn Tsichritzis 31ba597da7SJohn Tsichritzis static void *mbedtls_heap_addr; 32ba597da7SJohn Tsichritzis static size_t mbedtls_heap_size; 33ba597da7SJohn Tsichritzis 34ba597da7SJohn Tsichritzis /* 35ba597da7SJohn Tsichritzis * This function is the implementation of the shared Mbed TLS heap between 36ba597da7SJohn Tsichritzis * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1 37ba597da7SJohn Tsichritzis * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file 38ba597da7SJohn Tsichritzis * which is a DTB. 39ba597da7SJohn Tsichritzis * 40ba597da7SJohn Tsichritzis * This function is placed inside an #if directive for the below reasons: 41ba597da7SJohn Tsichritzis * - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot 42ba597da7SJohn Tsichritzis * is enabled. 43ba597da7SJohn Tsichritzis * - This implementation requires the DTB to be present so that BL1 has a 4460e19f57SAntonio Nino Diaz * mechanism to pass the pointer to BL2. 45ba597da7SJohn Tsichritzis */ 46ba597da7SJohn Tsichritzis int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size) 47ba597da7SJohn Tsichritzis { 48ba597da7SJohn Tsichritzis assert(heap_addr != NULL); 49ba597da7SJohn Tsichritzis assert(heap_size != NULL); 50ba597da7SJohn Tsichritzis 51ba597da7SJohn Tsichritzis #if defined(IMAGE_BL1) || BL2_AT_EL3 52ba597da7SJohn Tsichritzis 53ba597da7SJohn Tsichritzis /* If in BL1 or BL2_AT_EL3 define a heap */ 54ba597da7SJohn Tsichritzis static unsigned char heap[TF_MBEDTLS_HEAP_SIZE]; 55ba597da7SJohn Tsichritzis 56ba597da7SJohn Tsichritzis *heap_addr = heap; 57ba597da7SJohn Tsichritzis *heap_size = sizeof(heap); 58ba597da7SJohn Tsichritzis mbedtls_heap_addr = heap; 59ba597da7SJohn Tsichritzis mbedtls_heap_size = sizeof(heap); 60ba597da7SJohn Tsichritzis 61ba597da7SJohn Tsichritzis #elif defined(IMAGE_BL2) 62ba597da7SJohn Tsichritzis 63ba597da7SJohn Tsichritzis /* If in BL2, retrieve the already allocated heap's info from DTB */ 646c972317SLouis Mayencourt *heap_addr = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_addr); 656c972317SLouis Mayencourt *heap_size = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_size); 666c972317SLouis Mayencourt 67ba597da7SJohn Tsichritzis #endif 68ba597da7SJohn Tsichritzis 69ba597da7SJohn Tsichritzis return 0; 70ba597da7SJohn Tsichritzis } 71ba597da7SJohn Tsichritzis 72ba597da7SJohn Tsichritzis /* 73ba597da7SJohn Tsichritzis * Puts the shared Mbed TLS heap information to the DTB. 74ba597da7SJohn Tsichritzis * Executed only from BL1. 75ba597da7SJohn Tsichritzis */ 76ba597da7SJohn Tsichritzis void arm_bl1_set_mbedtls_heap(void) 77ba597da7SJohn Tsichritzis { 78ba597da7SJohn Tsichritzis int err; 7925ac8794SLouis Mayencourt uintptr_t tb_fw_cfg_dtb; 80*fe6fd3e4SManish V Badarkhe const struct dyn_cfg_dtb_info_t *tb_fw_config_info; 81ba597da7SJohn Tsichritzis 82ba597da7SJohn Tsichritzis /* 83ba597da7SJohn Tsichritzis * If tb_fw_cfg_dtb==NULL then DTB is not present for the current 84ba597da7SJohn Tsichritzis * platform. As such, we don't attempt to write to the DTB at all. 85ba597da7SJohn Tsichritzis * 86ba597da7SJohn Tsichritzis * If mbedtls_heap_addr==NULL, then it means we are using the default 87ba597da7SJohn Tsichritzis * heap implementation. As such, BL2 will have its own heap for sure 88ba597da7SJohn Tsichritzis * and hence there is no need to pass any information to the DTB. 89ba597da7SJohn Tsichritzis * 90ba597da7SJohn Tsichritzis * In the latter case, if we still wanted to write in the DTB the heap 91ba597da7SJohn Tsichritzis * information, we would need to call plat_get_mbedtls_heap to retrieve 92ba597da7SJohn Tsichritzis * the default heap's address and size. 93ba597da7SJohn Tsichritzis */ 9425ac8794SLouis Mayencourt 95*fe6fd3e4SManish V Badarkhe tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID); 96*fe6fd3e4SManish V Badarkhe tb_fw_cfg_dtb = tb_fw_config_info->config_addr; 9725ac8794SLouis Mayencourt 9825ac8794SLouis Mayencourt if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) { 9925ac8794SLouis Mayencourt /* As libfdt use void *, we can't avoid this cast */ 10025ac8794SLouis Mayencourt void *dtb = (void *)tb_fw_cfg_dtb; 10125ac8794SLouis Mayencourt 10225ac8794SLouis Mayencourt err = arm_set_dtb_mbedtls_heap_info(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 } 1080ab49645SAlexei Fedorov #if !MEASURED_BOOT 10963cc2658SJohn Tsichritzis /* 11063cc2658SJohn Tsichritzis * Ensure that the info written to the DTB is visible to other 11163cc2658SJohn Tsichritzis * images. It's critical because BL2 won't be able to proceed 11263cc2658SJohn Tsichritzis * without the heap info. 1130ab49645SAlexei Fedorov * 1140ab49645SAlexei Fedorov * In MEASURED_BOOT case flushing is done in 1150ab49645SAlexei Fedorov * arm_bl1_set_bl2_hash() function which is called after heap 1160ab49645SAlexei Fedorov * information is written in the DTB. 11763cc2658SJohn Tsichritzis */ 11825ac8794SLouis Mayencourt flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize(dtb)); 1190ab49645SAlexei Fedorov #endif /* !MEASURED_BOOT */ 120ba597da7SJohn Tsichritzis } 121ba597da7SJohn Tsichritzis } 122ba597da7SJohn Tsichritzis 1230ab49645SAlexei Fedorov #if MEASURED_BOOT 1240ab49645SAlexei Fedorov /* 1250ab49645SAlexei Fedorov * Puts the BL2 hash data to TB_FW_CONFIG DTB. 1260ab49645SAlexei Fedorov * Executed only from BL1. 1270ab49645SAlexei Fedorov */ 1280ab49645SAlexei Fedorov void arm_bl1_set_bl2_hash(image_desc_t *image_desc) 1290ab49645SAlexei Fedorov { 1300ab49645SAlexei Fedorov unsigned char hash_data[MBEDTLS_MD_MAX_SIZE]; 1310ab49645SAlexei Fedorov image_info_t image_info = image_desc->image_info; 1320ab49645SAlexei Fedorov uintptr_t tb_fw_cfg_dtb; 1330ab49645SAlexei Fedorov int err; 134*fe6fd3e4SManish V Badarkhe const struct dyn_cfg_dtb_info_t *tb_fw_config_info; 1350ab49645SAlexei Fedorov 136*fe6fd3e4SManish V Badarkhe tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID); 137*fe6fd3e4SManish V Badarkhe tb_fw_cfg_dtb = tb_fw_config_info->config_addr; 1380ab49645SAlexei Fedorov 1390ab49645SAlexei Fedorov /* 1400ab49645SAlexei Fedorov * If tb_fw_cfg_dtb==NULL then DTB is not present for the current 1410ab49645SAlexei Fedorov * platform. As such, we cannot write to the DTB at all and pass 1420ab49645SAlexei Fedorov * measured data. 1430ab49645SAlexei Fedorov */ 1440ab49645SAlexei Fedorov if (tb_fw_cfg_dtb == 0UL) { 1450ab49645SAlexei Fedorov panic(); 1460ab49645SAlexei Fedorov } 1470ab49645SAlexei Fedorov 1480ab49645SAlexei Fedorov /* Calculate hash */ 1490ab49645SAlexei Fedorov err = crypto_mod_calc_hash(MBEDTLS_MD_ID, 1500ab49645SAlexei Fedorov (void *)image_info.image_base, 1510ab49645SAlexei Fedorov image_info.image_size, hash_data); 1520ab49645SAlexei Fedorov if (err != 0) { 1530ab49645SAlexei Fedorov ERROR("BL1: unable to calculate BL2 hash\n"); 1540ab49645SAlexei Fedorov panic(); 1550ab49645SAlexei Fedorov } 1560ab49645SAlexei Fedorov 1570ab49645SAlexei Fedorov err = arm_set_bl2_hash_info((void *)tb_fw_cfg_dtb, hash_data); 1580ab49645SAlexei Fedorov if (err < 0) { 1590ab49645SAlexei Fedorov ERROR("BL1: unable to write BL2 hash data to DTB\n"); 1600ab49645SAlexei Fedorov panic(); 1610ab49645SAlexei Fedorov } 1620ab49645SAlexei Fedorov 1630ab49645SAlexei Fedorov /* 1640ab49645SAlexei Fedorov * Ensure that the info written to the DTB is visible to other 1650ab49645SAlexei Fedorov * images. It's critical because BL2 won't be able to proceed 1660ab49645SAlexei Fedorov * without the heap info and its hash data. 1670ab49645SAlexei Fedorov */ 1680ab49645SAlexei Fedorov flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize((void *)tb_fw_cfg_dtb)); 1690ab49645SAlexei Fedorov } 1700ab49645SAlexei Fedorov #endif /* MEASURED_BOOT */ 171ba597da7SJohn Tsichritzis #endif /* TRUSTED_BOARD_BOOT */ 172ba597da7SJohn Tsichritzis 173c228956aSSoby Mathew /* 174cab0b5b0SSoby Mathew * BL2 utility function to initialize dynamic configuration specified by 17504e06973SManish V Badarkhe * FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if 17604e06973SManish V Badarkhe * specified in FW_CONFIG. 177cab0b5b0SSoby Mathew */ 178cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void) 179cab0b5b0SSoby Mathew { 1801d71ba14SSoby Mathew unsigned int i; 1811d71ba14SSoby Mathew bl_mem_params_node_t *cfg_mem_params = NULL; 18225ac8794SLouis Mayencourt uintptr_t image_base; 18325ac8794SLouis Mayencourt size_t image_size; 1841d71ba14SSoby Mathew const unsigned int config_ids[] = { 1851d71ba14SSoby Mathew HW_CONFIG_ID, 1861d71ba14SSoby Mathew SOC_FW_CONFIG_ID, 1871d71ba14SSoby Mathew NT_FW_CONFIG_ID, 1880cb64d01SAchin Gupta #if defined(SPD_tspd) || defined(SPD_spmd) 1890cb64d01SAchin Gupta /* tos_fw_config is only present for TSPD/SPMD */ 1901d71ba14SSoby Mathew TOS_FW_CONFIG_ID 1911d71ba14SSoby Mathew #endif 1921d71ba14SSoby Mathew }; 193cab0b5b0SSoby Mathew 19425ac8794SLouis Mayencourt const struct dyn_cfg_dtb_info_t *dtb_info; 195cab0b5b0SSoby Mathew 1961d71ba14SSoby Mathew /* Iterate through all the fw config IDs */ 1971d71ba14SSoby Mathew for (i = 0; i < ARRAY_SIZE(config_ids); i++) { 1981d71ba14SSoby Mathew /* Get the config load address and size from TB_FW_CONFIG */ 1991d71ba14SSoby Mathew cfg_mem_params = get_bl_mem_params_node(config_ids[i]); 2001d71ba14SSoby Mathew if (cfg_mem_params == NULL) { 201cab0b5b0SSoby Mathew VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n"); 2021d71ba14SSoby Mathew continue; 203cab0b5b0SSoby Mathew } 204cab0b5b0SSoby Mathew 20525ac8794SLouis Mayencourt dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]); 20625ac8794SLouis Mayencourt if (dtb_info == NULL) { 2071d71ba14SSoby Mathew VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n", 2081d71ba14SSoby Mathew config_ids[i]); 2091d71ba14SSoby Mathew continue; 210cab0b5b0SSoby Mathew } 211cab0b5b0SSoby Mathew 21225ac8794SLouis Mayencourt image_base = dtb_info->config_addr; 21325ac8794SLouis Mayencourt image_size = dtb_info->config_max_size; 21425ac8794SLouis Mayencourt 2151d71ba14SSoby Mathew /* 2161d71ba14SSoby Mathew * Do some runtime checks on the load addresses of soc_fw_config, 2171d71ba14SSoby Mathew * tos_fw_config, nt_fw_config. This is not a comprehensive check 2181d71ba14SSoby Mathew * of all invalid addresses but to prevent trivial porting errors. 2191d71ba14SSoby Mathew */ 2201d71ba14SSoby Mathew if (config_ids[i] != HW_CONFIG_ID) { 2211d71ba14SSoby Mathew 222b8a02d53SAntonio Nino Diaz if (check_uptr_overflow(image_base, image_size)) 2231d71ba14SSoby Mathew continue; 2241d71ba14SSoby Mathew 2256393c787SUsama Arif #ifdef BL31_BASE 226c099cd39SSoby Mathew /* Ensure the configs don't overlap with BL31 */ 2275ddcbdd8SAlexei Fedorov if ((image_base >= BL31_BASE) && 2285ddcbdd8SAlexei Fedorov (image_base <= BL31_LIMIT)) 2291d71ba14SSoby Mathew continue; 2306393c787SUsama Arif #endif 2311d71ba14SSoby Mathew /* Ensure the configs are loaded in a valid address */ 2321d71ba14SSoby Mathew if (image_base < ARM_BL_RAM_BASE) 2331d71ba14SSoby Mathew continue; 2341d71ba14SSoby Mathew #ifdef BL32_BASE 2351d71ba14SSoby Mathew /* 2361d71ba14SSoby Mathew * If BL32 is present, ensure that the configs don't 2371d71ba14SSoby Mathew * overlap with it. 2381d71ba14SSoby Mathew */ 2395ddcbdd8SAlexei Fedorov if ((image_base >= BL32_BASE) && 2405ddcbdd8SAlexei Fedorov (image_base <= BL32_LIMIT)) 2411d71ba14SSoby Mathew continue; 2421d71ba14SSoby Mathew #endif 2431d71ba14SSoby Mathew } 2441d71ba14SSoby Mathew 2451d71ba14SSoby Mathew 24625ac8794SLouis Mayencourt cfg_mem_params->image_info.image_base = image_base; 24725ac8794SLouis Mayencourt cfg_mem_params->image_info.image_max_size = (uint32_t)image_size; 2481d71ba14SSoby Mathew 2495ddcbdd8SAlexei Fedorov /* 2505ddcbdd8SAlexei Fedorov * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from 2515ddcbdd8SAlexei Fedorov * HW_CONFIG or FW_CONFIG nodes 2525ddcbdd8SAlexei Fedorov */ 2531d71ba14SSoby Mathew cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING; 2541d71ba14SSoby Mathew } 255cab0b5b0SSoby Mathew } 256