1c228956aSSoby Mathew /*
23b48ca17SChris Kay * Copyright (c) 2018-2024, 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
11a8eadc51SGovindraj Raja #if CRYPTO_SUPPORT
12a8eadc51SGovindraj Raja #include <mbedtls/version.h>
13a8eadc51SGovindraj Raja #endif /* CRYPTO_SUPPORT */
1409d40e0eSAntonio Nino Diaz
1509d40e0eSAntonio Nino Diaz #include <common/debug.h>
1609d40e0eSAntonio Nino Diaz #include <common/desc_image_load.h>
1709d40e0eSAntonio Nino Diaz #include <common/tbbr/tbbr_img_def.h>
1825ac8794SLouis Mayencourt #include <lib/fconf/fconf.h>
1925ac8794SLouis Mayencourt #include <lib/fconf/fconf_dyn_cfg_getter.h>
206c972317SLouis Mayencourt #include <lib/fconf/fconf_tbbr_getter.h>
217b4e1fbbSAlexei Fedorov
22bd9344f6SAntonio Nino Diaz #include <plat/arm/common/arm_dyn_cfg_helpers.h>
23bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h>
24a8eadc51SGovindraj Raja #include <platform_def.h>
2509d40e0eSAntonio Nino Diaz
26*ada4e59dSHarrison Mutai #if CRYPTO_SUPPORT && !TRANSFER_LIST
27ba597da7SJohn Tsichritzis
28ba597da7SJohn Tsichritzis static void *mbedtls_heap_addr;
29ba597da7SJohn Tsichritzis static size_t mbedtls_heap_size;
30ba597da7SJohn Tsichritzis
31ba597da7SJohn Tsichritzis /*
32ba597da7SJohn Tsichritzis * This function is the implementation of the shared Mbed TLS heap between
33ba597da7SJohn Tsichritzis * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1
34ba597da7SJohn Tsichritzis * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file
35ba597da7SJohn Tsichritzis * which is a DTB.
36ba597da7SJohn Tsichritzis *
37ba597da7SJohn Tsichritzis * This function is placed inside an #if directive for the below reasons:
38ba597da7SJohn Tsichritzis * - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot
39ba597da7SJohn Tsichritzis * is enabled.
40ba597da7SJohn Tsichritzis * - This implementation requires the DTB to be present so that BL1 has a
4160e19f57SAntonio Nino Diaz * mechanism to pass the pointer to BL2.
42ba597da7SJohn Tsichritzis */
arm_get_mbedtls_heap(void ** heap_addr,size_t * heap_size)43ba597da7SJohn Tsichritzis int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
44ba597da7SJohn Tsichritzis {
45ba597da7SJohn Tsichritzis assert(heap_addr != NULL);
46ba597da7SJohn Tsichritzis assert(heap_size != NULL);
47ba597da7SJohn Tsichritzis
4842d4d3baSArvind Ram Prakash #if defined(IMAGE_BL1) || RESET_TO_BL2 || defined(IMAGE_BL31)
49ba597da7SJohn Tsichritzis
5042d4d3baSArvind Ram Prakash /* If in BL1 or RESET_TO_BL2 define a heap */
51ba597da7SJohn Tsichritzis static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
52ba597da7SJohn Tsichritzis
53ba597da7SJohn Tsichritzis *heap_addr = heap;
54ba597da7SJohn Tsichritzis *heap_size = sizeof(heap);
55ba597da7SJohn Tsichritzis mbedtls_heap_addr = heap;
56ba597da7SJohn Tsichritzis mbedtls_heap_size = sizeof(heap);
57ba597da7SJohn Tsichritzis
58ba597da7SJohn Tsichritzis #elif defined(IMAGE_BL2)
59ba597da7SJohn Tsichritzis
60ba597da7SJohn Tsichritzis /* If in BL2, retrieve the already allocated heap's info from DTB */
616c972317SLouis Mayencourt *heap_addr = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_addr);
626c972317SLouis Mayencourt *heap_size = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_size);
636c972317SLouis Mayencourt
64ba597da7SJohn Tsichritzis #endif
65ba597da7SJohn Tsichritzis
66ba597da7SJohn Tsichritzis return 0;
67ba597da7SJohn Tsichritzis }
68ba597da7SJohn Tsichritzis
69ba597da7SJohn Tsichritzis /*
70ba597da7SJohn Tsichritzis * Puts the shared Mbed TLS heap information to the DTB.
71ba597da7SJohn Tsichritzis * Executed only from BL1.
72ba597da7SJohn Tsichritzis */
arm_bl1_set_mbedtls_heap(void)73ba597da7SJohn Tsichritzis void arm_bl1_set_mbedtls_heap(void)
74ba597da7SJohn Tsichritzis {
75ba597da7SJohn Tsichritzis int err;
7625ac8794SLouis Mayencourt uintptr_t tb_fw_cfg_dtb;
77fe6fd3e4SManish V Badarkhe const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
78ba597da7SJohn Tsichritzis
79ba597da7SJohn Tsichritzis /*
80ba597da7SJohn Tsichritzis * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
81ba597da7SJohn Tsichritzis * platform. As such, we don't attempt to write to the DTB at all.
82ba597da7SJohn Tsichritzis *
83ba597da7SJohn Tsichritzis * If mbedtls_heap_addr==NULL, then it means we are using the default
84ba597da7SJohn Tsichritzis * heap implementation. As such, BL2 will have its own heap for sure
85ba597da7SJohn Tsichritzis * and hence there is no need to pass any information to the DTB.
86ba597da7SJohn Tsichritzis *
87ba597da7SJohn Tsichritzis * In the latter case, if we still wanted to write in the DTB the heap
88ba597da7SJohn Tsichritzis * information, we would need to call plat_get_mbedtls_heap to retrieve
89ba597da7SJohn Tsichritzis * the default heap's address and size.
90ba597da7SJohn Tsichritzis */
9125ac8794SLouis Mayencourt
92fe6fd3e4SManish V Badarkhe tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
931d60052eSManish V Badarkhe assert(tb_fw_config_info != NULL);
941d60052eSManish V Badarkhe
95fe6fd3e4SManish V Badarkhe tb_fw_cfg_dtb = tb_fw_config_info->config_addr;
9625ac8794SLouis Mayencourt
9725ac8794SLouis Mayencourt if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) {
987b4e1fbbSAlexei Fedorov /* As libfdt uses 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) {
1047b4e1fbbSAlexei Fedorov ERROR("%swrite shared Mbed TLS heap information%s",
1057b4e1fbbSAlexei Fedorov "BL1: unable to ", " 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 *
114eab78e9bSManish V Badarkhe * In MEASURED_BOOT case flushing is done in a function which
115eab78e9bSManish V Badarkhe * is called after heap 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 }
121*ada4e59dSHarrison Mutai #endif /* CRYPTO_SUPPORT && !TRANSFER_LIST */
122ba597da7SJohn Tsichritzis
1233b48ca17SChris Kay #if IMAGE_BL2
124c228956aSSoby Mathew /*
125cab0b5b0SSoby Mathew * BL2 utility function to initialize dynamic configuration specified by
12604e06973SManish V Badarkhe * FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
12704e06973SManish V Badarkhe * specified in FW_CONFIG.
128cab0b5b0SSoby Mathew */
arm_bl2_dyn_cfg_init(void)129cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void)
130cab0b5b0SSoby Mathew {
1311d71ba14SSoby Mathew unsigned int i;
1321d71ba14SSoby Mathew bl_mem_params_node_t *cfg_mem_params = NULL;
13325ac8794SLouis Mayencourt uintptr_t image_base;
134a4ff9d7eSManish V Badarkhe uint32_t image_size;
1356f60e94eSManish V Badarkhe unsigned int error_config_id = MAX_IMAGE_IDS;
1361d71ba14SSoby Mathew const unsigned int config_ids[] = {
1371d71ba14SSoby Mathew HW_CONFIG_ID,
1381d71ba14SSoby Mathew SOC_FW_CONFIG_ID,
1391d71ba14SSoby Mathew NT_FW_CONFIG_ID,
1401d71ba14SSoby Mathew TOS_FW_CONFIG_ID
1411d71ba14SSoby Mathew };
142cab0b5b0SSoby Mathew
14325ac8794SLouis Mayencourt const struct dyn_cfg_dtb_info_t *dtb_info;
144cab0b5b0SSoby Mathew
1451d71ba14SSoby Mathew /* Iterate through all the fw config IDs */
1461d71ba14SSoby Mathew for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
1476f60e94eSManish V Badarkhe /* Get the config load address and size */
1481d71ba14SSoby Mathew cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
1491d71ba14SSoby Mathew if (cfg_mem_params == NULL) {
1506f60e94eSManish V Badarkhe VERBOSE("%sconfig_id = %d in bl_mem_params_node\n",
1516f60e94eSManish V Badarkhe "Couldn't find ", config_ids[i]);
1521d71ba14SSoby Mathew continue;
153cab0b5b0SSoby Mathew }
154cab0b5b0SSoby Mathew
15525ac8794SLouis Mayencourt dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]);
15625ac8794SLouis Mayencourt if (dtb_info == NULL) {
1576f60e94eSManish V Badarkhe VERBOSE("%sconfig_id %d load info in FW_CONFIG\n",
1587b4e1fbbSAlexei Fedorov "Couldn't find ", config_ids[i]);
1591d71ba14SSoby Mathew continue;
160cab0b5b0SSoby Mathew }
161cab0b5b0SSoby Mathew
16225ac8794SLouis Mayencourt image_base = dtb_info->config_addr;
16325ac8794SLouis Mayencourt image_size = dtb_info->config_max_size;
16425ac8794SLouis Mayencourt
1651d71ba14SSoby Mathew /*
1661d71ba14SSoby Mathew * Do some runtime checks on the load addresses of soc_fw_config,
1671d71ba14SSoby Mathew * tos_fw_config, nt_fw_config. This is not a comprehensive check
1681d71ba14SSoby Mathew * of all invalid addresses but to prevent trivial porting errors.
1691d71ba14SSoby Mathew */
1701d71ba14SSoby Mathew if (config_ids[i] != HW_CONFIG_ID) {
1711d71ba14SSoby Mathew
1727b4e1fbbSAlexei Fedorov if (check_uptr_overflow(image_base, image_size)) {
1736f60e94eSManish V Badarkhe VERBOSE("%s=%d as its %s is overflowing uptr\n",
1746f60e94eSManish V Badarkhe "skip loading of firmware config",
1756f60e94eSManish V Badarkhe config_ids[i],
1766f60e94eSManish V Badarkhe "load-address");
1776f60e94eSManish V Badarkhe error_config_id = config_ids[i];
1781d71ba14SSoby Mathew continue;
1797b4e1fbbSAlexei Fedorov }
1806393c787SUsama Arif #ifdef BL31_BASE
181c099cd39SSoby Mathew /* Ensure the configs don't overlap with BL31 */
1825ddcbdd8SAlexei Fedorov if ((image_base >= BL31_BASE) &&
1837b4e1fbbSAlexei Fedorov (image_base <= BL31_LIMIT)) {
1846f60e94eSManish V Badarkhe VERBOSE("%s=%d as its %s is overlapping BL31\n",
1856f60e94eSManish V Badarkhe "skip loading of firmware config",
1866f60e94eSManish V Badarkhe config_ids[i],
1876f60e94eSManish V Badarkhe "load-address");
1886f60e94eSManish V Badarkhe error_config_id = config_ids[i];
1891d71ba14SSoby Mathew continue;
1907b4e1fbbSAlexei Fedorov }
1916393c787SUsama Arif #endif
1921d71ba14SSoby Mathew /* Ensure the configs are loaded in a valid address */
1937b4e1fbbSAlexei Fedorov if (image_base < ARM_BL_RAM_BASE) {
1946f60e94eSManish V Badarkhe VERBOSE("%s=%d as its %s is invalid\n",
1956f60e94eSManish V Badarkhe "skip loading of firmware config",
1966f60e94eSManish V Badarkhe config_ids[i],
1976f60e94eSManish V Badarkhe "load-address");
1986f60e94eSManish V Badarkhe error_config_id = config_ids[i];
1991d71ba14SSoby Mathew continue;
2007b4e1fbbSAlexei Fedorov }
2011d71ba14SSoby Mathew #ifdef BL32_BASE
2021d71ba14SSoby Mathew /*
2031d71ba14SSoby Mathew * If BL32 is present, ensure that the configs don't
2041d71ba14SSoby Mathew * overlap with it.
2051d71ba14SSoby Mathew */
2065ddcbdd8SAlexei Fedorov if ((image_base >= BL32_BASE) &&
2077b4e1fbbSAlexei Fedorov (image_base <= BL32_LIMIT)) {
2086f60e94eSManish V Badarkhe VERBOSE("%s=%d as its %s is overlapping BL32\n",
2096f60e94eSManish V Badarkhe "skip loading of firmware config",
2106f60e94eSManish V Badarkhe config_ids[i],
2116f60e94eSManish V Badarkhe "load-address");
2126f60e94eSManish V Badarkhe error_config_id = config_ids[i];
2131d71ba14SSoby Mathew continue;
2147b4e1fbbSAlexei Fedorov }
2151d71ba14SSoby Mathew #endif
2161d71ba14SSoby Mathew }
2171d71ba14SSoby Mathew
21825ac8794SLouis Mayencourt cfg_mem_params->image_info.image_base = image_base;
21925ac8794SLouis Mayencourt cfg_mem_params->image_info.image_max_size = (uint32_t)image_size;
2201d71ba14SSoby Mathew
2215ddcbdd8SAlexei Fedorov /*
2225ddcbdd8SAlexei Fedorov * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from
2235ddcbdd8SAlexei Fedorov * HW_CONFIG or FW_CONFIG nodes
2245ddcbdd8SAlexei Fedorov */
2251d71ba14SSoby Mathew cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
2261d71ba14SSoby Mathew }
2276f60e94eSManish V Badarkhe
2286f60e94eSManish V Badarkhe if (error_config_id != MAX_IMAGE_IDS) {
2296f60e94eSManish V Badarkhe ERROR("Invalid config file %u\n", error_config_id);
2306f60e94eSManish V Badarkhe panic();
2316f60e94eSManish V Badarkhe }
232cab0b5b0SSoby Mathew }
2333b48ca17SChris Kay #endif /* IMAGE_BL2 */
234