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; 80ba597da7SJohn Tsichritzis 81ba597da7SJohn Tsichritzis /* 82ba597da7SJohn Tsichritzis * If tb_fw_cfg_dtb==NULL then DTB is not present for the current 83ba597da7SJohn Tsichritzis * platform. As such, we don't attempt to write to the DTB at all. 84ba597da7SJohn Tsichritzis * 85ba597da7SJohn Tsichritzis * If mbedtls_heap_addr==NULL, then it means we are using the default 86ba597da7SJohn Tsichritzis * heap implementation. As such, BL2 will have its own heap for sure 87ba597da7SJohn Tsichritzis * and hence there is no need to pass any information to the DTB. 88ba597da7SJohn Tsichritzis * 89ba597da7SJohn Tsichritzis * In the latter case, if we still wanted to write in the DTB the heap 90ba597da7SJohn Tsichritzis * information, we would need to call plat_get_mbedtls_heap to retrieve 91ba597da7SJohn Tsichritzis * the default heap's address and size. 92ba597da7SJohn Tsichritzis */ 9325ac8794SLouis Mayencourt 9425ac8794SLouis Mayencourt /* fconf FW_CONFIG and TB_FW_CONFIG are currently the same DTB */ 9525ac8794SLouis Mayencourt tb_fw_cfg_dtb = FCONF_GET_PROPERTY(fconf, dtb, base_addr); 9625ac8794SLouis Mayencourt 9725ac8794SLouis Mayencourt if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) { 9825ac8794SLouis Mayencourt /* As libfdt use void *, we can't avoid this cast */ 9925ac8794SLouis Mayencourt void *dtb = (void *)tb_fw_cfg_dtb; 10025ac8794SLouis Mayencourt 10125ac8794SLouis Mayencourt err = arm_set_dtb_mbedtls_heap_info(dtb, 102ba597da7SJohn Tsichritzis mbedtls_heap_addr, mbedtls_heap_size); 103ba597da7SJohn Tsichritzis if (err < 0) { 104a606031eSJohn Tsichritzis ERROR("BL1: unable to write shared Mbed TLS heap information to DTB\n"); 105ba597da7SJohn Tsichritzis panic(); 106ba597da7SJohn Tsichritzis } 1070ab49645SAlexei Fedorov #if !MEASURED_BOOT 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. 1120ab49645SAlexei Fedorov * 1130ab49645SAlexei Fedorov * In MEASURED_BOOT case flushing is done in 1140ab49645SAlexei Fedorov * arm_bl1_set_bl2_hash() function which is called after heap 1150ab49645SAlexei Fedorov * information is written in the DTB. 11663cc2658SJohn Tsichritzis */ 11725ac8794SLouis Mayencourt flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize(dtb)); 1180ab49645SAlexei Fedorov #endif /* !MEASURED_BOOT */ 119ba597da7SJohn Tsichritzis } 120ba597da7SJohn Tsichritzis } 121ba597da7SJohn Tsichritzis 1220ab49645SAlexei Fedorov #if MEASURED_BOOT 1230ab49645SAlexei Fedorov /* 1240ab49645SAlexei Fedorov * Puts the BL2 hash data to TB_FW_CONFIG DTB. 1250ab49645SAlexei Fedorov * Executed only from BL1. 1260ab49645SAlexei Fedorov */ 1270ab49645SAlexei Fedorov void arm_bl1_set_bl2_hash(image_desc_t *image_desc) 1280ab49645SAlexei Fedorov { 1290ab49645SAlexei Fedorov unsigned char hash_data[MBEDTLS_MD_MAX_SIZE]; 1300ab49645SAlexei Fedorov image_info_t image_info = image_desc->image_info; 1310ab49645SAlexei Fedorov uintptr_t tb_fw_cfg_dtb; 1320ab49645SAlexei Fedorov int err; 1330ab49645SAlexei Fedorov 1340ab49645SAlexei Fedorov /* fconf FW_CONFIG and TB_FW_CONFIG are currently the same DTB */ 1350ab49645SAlexei Fedorov tb_fw_cfg_dtb = FCONF_GET_PROPERTY(fconf, dtb, base_addr); 1360ab49645SAlexei Fedorov 1370ab49645SAlexei Fedorov /* 1380ab49645SAlexei Fedorov * If tb_fw_cfg_dtb==NULL then DTB is not present for the current 1390ab49645SAlexei Fedorov * platform. As such, we cannot write to the DTB at all and pass 1400ab49645SAlexei Fedorov * measured data. 1410ab49645SAlexei Fedorov */ 1420ab49645SAlexei Fedorov if (tb_fw_cfg_dtb == 0UL) { 1430ab49645SAlexei Fedorov panic(); 1440ab49645SAlexei Fedorov } 1450ab49645SAlexei Fedorov 1460ab49645SAlexei Fedorov /* Calculate hash */ 1470ab49645SAlexei Fedorov err = crypto_mod_calc_hash(MBEDTLS_MD_ID, 1480ab49645SAlexei Fedorov (void *)image_info.image_base, 1490ab49645SAlexei Fedorov image_info.image_size, hash_data); 1500ab49645SAlexei Fedorov if (err != 0) { 1510ab49645SAlexei Fedorov ERROR("BL1: unable to calculate BL2 hash\n"); 1520ab49645SAlexei Fedorov panic(); 1530ab49645SAlexei Fedorov } 1540ab49645SAlexei Fedorov 1550ab49645SAlexei Fedorov err = arm_set_bl2_hash_info((void *)tb_fw_cfg_dtb, hash_data); 1560ab49645SAlexei Fedorov if (err < 0) { 1570ab49645SAlexei Fedorov ERROR("BL1: unable to write BL2 hash data to DTB\n"); 1580ab49645SAlexei Fedorov panic(); 1590ab49645SAlexei Fedorov } 1600ab49645SAlexei Fedorov 1610ab49645SAlexei Fedorov /* 1620ab49645SAlexei Fedorov * Ensure that the info written to the DTB is visible to other 1630ab49645SAlexei Fedorov * images. It's critical because BL2 won't be able to proceed 1640ab49645SAlexei Fedorov * without the heap info and its hash data. 1650ab49645SAlexei Fedorov */ 1660ab49645SAlexei Fedorov flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize((void *)tb_fw_cfg_dtb)); 1670ab49645SAlexei Fedorov } 1680ab49645SAlexei Fedorov #endif /* MEASURED_BOOT */ 169ba597da7SJohn Tsichritzis #endif /* TRUSTED_BOARD_BOOT */ 170ba597da7SJohn Tsichritzis 171c228956aSSoby Mathew /* 172cab0b5b0SSoby Mathew * BL2 utility function to initialize dynamic configuration specified by 173*04e06973SManish V Badarkhe * FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if 174*04e06973SManish V Badarkhe * specified in FW_CONFIG. 175cab0b5b0SSoby Mathew */ 176cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void) 177cab0b5b0SSoby Mathew { 1781d71ba14SSoby Mathew unsigned int i; 1791d71ba14SSoby Mathew bl_mem_params_node_t *cfg_mem_params = NULL; 18025ac8794SLouis Mayencourt uintptr_t image_base; 18125ac8794SLouis Mayencourt size_t image_size; 1821d71ba14SSoby Mathew const unsigned int config_ids[] = { 1831d71ba14SSoby Mathew HW_CONFIG_ID, 1841d71ba14SSoby Mathew SOC_FW_CONFIG_ID, 1851d71ba14SSoby Mathew NT_FW_CONFIG_ID, 1860cb64d01SAchin Gupta #if defined(SPD_tspd) || defined(SPD_spmd) 1870cb64d01SAchin Gupta /* tos_fw_config is only present for TSPD/SPMD */ 1881d71ba14SSoby Mathew TOS_FW_CONFIG_ID 1891d71ba14SSoby Mathew #endif 1901d71ba14SSoby Mathew }; 191cab0b5b0SSoby Mathew 19225ac8794SLouis Mayencourt const struct dyn_cfg_dtb_info_t *dtb_info; 193cab0b5b0SSoby Mathew 1941d71ba14SSoby Mathew /* Iterate through all the fw config IDs */ 1951d71ba14SSoby Mathew for (i = 0; i < ARRAY_SIZE(config_ids); i++) { 1961d71ba14SSoby Mathew /* Get the config load address and size from TB_FW_CONFIG */ 1971d71ba14SSoby Mathew cfg_mem_params = get_bl_mem_params_node(config_ids[i]); 1981d71ba14SSoby Mathew if (cfg_mem_params == NULL) { 199cab0b5b0SSoby Mathew VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n"); 2001d71ba14SSoby Mathew continue; 201cab0b5b0SSoby Mathew } 202cab0b5b0SSoby Mathew 20325ac8794SLouis Mayencourt dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]); 20425ac8794SLouis Mayencourt if (dtb_info == NULL) { 2051d71ba14SSoby Mathew VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n", 2061d71ba14SSoby Mathew config_ids[i]); 2071d71ba14SSoby Mathew continue; 208cab0b5b0SSoby Mathew } 209cab0b5b0SSoby Mathew 21025ac8794SLouis Mayencourt image_base = dtb_info->config_addr; 21125ac8794SLouis Mayencourt image_size = dtb_info->config_max_size; 21225ac8794SLouis Mayencourt 2131d71ba14SSoby Mathew /* 2141d71ba14SSoby Mathew * Do some runtime checks on the load addresses of soc_fw_config, 2151d71ba14SSoby Mathew * tos_fw_config, nt_fw_config. This is not a comprehensive check 2161d71ba14SSoby Mathew * of all invalid addresses but to prevent trivial porting errors. 2171d71ba14SSoby Mathew */ 2181d71ba14SSoby Mathew if (config_ids[i] != HW_CONFIG_ID) { 2191d71ba14SSoby Mathew 220b8a02d53SAntonio Nino Diaz if (check_uptr_overflow(image_base, image_size)) 2211d71ba14SSoby Mathew continue; 2221d71ba14SSoby Mathew 2236393c787SUsama Arif #ifdef BL31_BASE 224c099cd39SSoby Mathew /* Ensure the configs don't overlap with BL31 */ 2255ddcbdd8SAlexei Fedorov if ((image_base >= BL31_BASE) && 2265ddcbdd8SAlexei Fedorov (image_base <= BL31_LIMIT)) 2271d71ba14SSoby Mathew continue; 2286393c787SUsama Arif #endif 2291d71ba14SSoby Mathew /* Ensure the configs are loaded in a valid address */ 2301d71ba14SSoby Mathew if (image_base < ARM_BL_RAM_BASE) 2311d71ba14SSoby Mathew continue; 2321d71ba14SSoby Mathew #ifdef BL32_BASE 2331d71ba14SSoby Mathew /* 2341d71ba14SSoby Mathew * If BL32 is present, ensure that the configs don't 2351d71ba14SSoby Mathew * overlap with it. 2361d71ba14SSoby Mathew */ 2375ddcbdd8SAlexei Fedorov if ((image_base >= BL32_BASE) && 2385ddcbdd8SAlexei Fedorov (image_base <= BL32_LIMIT)) 2391d71ba14SSoby Mathew continue; 2401d71ba14SSoby Mathew #endif 2411d71ba14SSoby Mathew } 2421d71ba14SSoby Mathew 2431d71ba14SSoby Mathew 24425ac8794SLouis Mayencourt cfg_mem_params->image_info.image_base = image_base; 24525ac8794SLouis Mayencourt cfg_mem_params->image_info.image_max_size = (uint32_t)image_size; 2461d71ba14SSoby Mathew 2475ddcbdd8SAlexei Fedorov /* 2485ddcbdd8SAlexei Fedorov * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from 2495ddcbdd8SAlexei Fedorov * HW_CONFIG or FW_CONFIG nodes 2505ddcbdd8SAlexei Fedorov */ 2511d71ba14SSoby Mathew cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING; 2521d71ba14SSoby Mathew } 253cab0b5b0SSoby Mathew } 254