blob: 443d40fe8ce2dd18e4cf6e010dd7e11187cf3039 [file] [log] [blame]
Soby Mathew7c6df5b2018-01-15 14:43:42 +00001/*
Achin Guptada6ef0e2019-10-11 14:54:48 +01002 * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
Soby Mathew7c6df5b2018-01-15 14:43:42 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <string.h>
Louis Mayencourt73d42d72019-12-09 11:29:38 +00009#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
11#include <platform_def.h>
12
13#include <common/debug.h>
14#include <common/desc_image_load.h>
15#include <common/tbbr/tbbr_img_def.h>
John Tsichritzisc34341a2018-07-30 13:41:52 +010016#if TRUSTED_BOARD_BOOT
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <drivers/auth/mbedtls/mbedtls_config.h>
John Tsichritzisc34341a2018-07-30 13:41:52 +010018#endif
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000019#include <lib/fconf/fconf.h>
20#include <lib/fconf/fconf_dyn_cfg_getter.h>
Louis Mayencourt5b9055f2019-10-01 10:45:14 +010021#include <lib/fconf/fconf_tbbr_getter.h>
Antonio Nino Diazbd7b7402019-01-25 14:30:04 +000022#include <plat/arm/common/arm_dyn_cfg_helpers.h>
23#include <plat/arm/common/plat_arm.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000024#include <plat/common/platform.h>
Soby Mathew7c6df5b2018-01-15 14:43:42 +000025
John Tsichritzisc34341a2018-07-30 13:41:52 +010026#if TRUSTED_BOARD_BOOT
27
28static void *mbedtls_heap_addr;
29static size_t mbedtls_heap_size;
30
31/*
32 * This function is the implementation of the shared Mbed TLS heap between
33 * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1
34 * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file
35 * which is a DTB.
36 *
37 * This function is placed inside an #if directive for the below reasons:
38 * - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot
39 * is enabled.
40 * - This implementation requires the DTB to be present so that BL1 has a
Antonio Nino Diaz05f49572018-09-25 11:37:23 +010041 * mechanism to pass the pointer to BL2.
John Tsichritzisc34341a2018-07-30 13:41:52 +010042 */
43int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
44{
45 assert(heap_addr != NULL);
46 assert(heap_size != NULL);
47
48#if defined(IMAGE_BL1) || BL2_AT_EL3
49
50 /* If in BL1 or BL2_AT_EL3 define a heap */
51 static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
52
53 *heap_addr = heap;
54 *heap_size = sizeof(heap);
55 mbedtls_heap_addr = heap;
56 mbedtls_heap_size = sizeof(heap);
57
58#elif defined(IMAGE_BL2)
59
John Tsichritzisc34341a2018-07-30 13:41:52 +010060 /* If in BL2, retrieve the already allocated heap's info from DTB */
Louis Mayencourt5b9055f2019-10-01 10:45:14 +010061 *heap_addr = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_addr);
62 *heap_size = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_size);
63
John Tsichritzisc34341a2018-07-30 13:41:52 +010064#endif
65
66 return 0;
67}
68
69/*
70 * Puts the shared Mbed TLS heap information to the DTB.
71 * Executed only from BL1.
72 */
73void arm_bl1_set_mbedtls_heap(void)
74{
75 int err;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000076 uintptr_t tb_fw_cfg_dtb;
John Tsichritzisc34341a2018-07-30 13:41:52 +010077
78 /*
79 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
80 * platform. As such, we don't attempt to write to the DTB at all.
81 *
82 * If mbedtls_heap_addr==NULL, then it means we are using the default
83 * heap implementation. As such, BL2 will have its own heap for sure
84 * and hence there is no need to pass any information to the DTB.
85 *
86 * In the latter case, if we still wanted to write in the DTB the heap
87 * information, we would need to call plat_get_mbedtls_heap to retrieve
88 * the default heap's address and size.
89 */
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000090
91 /* fconf FW_CONFIG and TB_FW_CONFIG are currently the same DTB*/
92 tb_fw_cfg_dtb = FCONF_GET_PROPERTY(fconf, dtb, base_addr);
93
94 if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) {
95 /* As libfdt use void *, we can't avoid this cast */
96 void *dtb = (void *)tb_fw_cfg_dtb;
97
98 err = arm_set_dtb_mbedtls_heap_info(dtb,
John Tsichritzisc34341a2018-07-30 13:41:52 +010099 mbedtls_heap_addr, mbedtls_heap_size);
100 if (err < 0) {
John Tsichritzis36507682018-09-07 10:42:37 +0100101 ERROR("BL1: unable to write shared Mbed TLS heap information to DTB\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +0100102 panic();
103 }
John Tsichritzis03459c22018-09-07 10:52:12 +0100104 /*
105 * Ensure that the info written to the DTB is visible to other
106 * images. It's critical because BL2 won't be able to proceed
107 * without the heap info.
108 */
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000109 flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize(dtb));
John Tsichritzisc34341a2018-07-30 13:41:52 +0100110 }
111}
112
113#endif /* TRUSTED_BOARD_BOOT */
114
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000115/*
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000116 * BL2 utility function to initialize dynamic configuration specified by
Soby Mathewb6814842018-04-04 09:40:32 +0100117 * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
118 * specified in TB_FW_CONFIG.
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000119 */
120void arm_bl2_dyn_cfg_init(void)
121{
Soby Mathewb6814842018-04-04 09:40:32 +0100122 unsigned int i;
123 bl_mem_params_node_t *cfg_mem_params = NULL;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000124 uintptr_t image_base;
125 size_t image_size;
Soby Mathewb6814842018-04-04 09:40:32 +0100126 const unsigned int config_ids[] = {
127 HW_CONFIG_ID,
128 SOC_FW_CONFIG_ID,
129 NT_FW_CONFIG_ID,
Achin Guptada6ef0e2019-10-11 14:54:48 +0100130#if defined(SPD_tspd) || defined(SPD_spmd)
131 /* tos_fw_config is only present for TSPD/SPMD */
Soby Mathewb6814842018-04-04 09:40:32 +0100132 TOS_FW_CONFIG_ID
133#endif
134 };
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000135
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000136 const struct dyn_cfg_dtb_info_t *dtb_info;
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000137
Soby Mathewb6814842018-04-04 09:40:32 +0100138 /* Iterate through all the fw config IDs */
139 for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
140 /* Get the config load address and size from TB_FW_CONFIG */
141 cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
142 if (cfg_mem_params == NULL) {
143 VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
144 continue;
145 }
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000146
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000147 dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]);
148 if (dtb_info == NULL) {
Soby Mathewb6814842018-04-04 09:40:32 +0100149 VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n",
150 config_ids[i]);
151 continue;
152 }
153
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000154 image_base = dtb_info->config_addr;
155 image_size = dtb_info->config_max_size;
156
Soby Mathewb6814842018-04-04 09:40:32 +0100157 /*
158 * Do some runtime checks on the load addresses of soc_fw_config,
159 * tos_fw_config, nt_fw_config. This is not a comprehensive check
160 * of all invalid addresses but to prevent trivial porting errors.
161 */
162 if (config_ids[i] != HW_CONFIG_ID) {
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000163
Antonio Nino Diazb5acb3f2018-10-30 16:32:48 +0000164 if (check_uptr_overflow(image_base, image_size))
Soby Mathewb6814842018-04-04 09:40:32 +0100165 continue;
166
Usama Arife97998f2018-11-30 15:43:56 +0000167#ifdef BL31_BASE
Soby Mathewaf14b462018-06-01 16:53:38 +0100168 /* Ensure the configs don't overlap with BL31 */
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000169 if ((image_base >= BL31_BASE) &&
170 (image_base <= BL31_LIMIT))
Soby Mathewb6814842018-04-04 09:40:32 +0100171 continue;
Usama Arife97998f2018-11-30 15:43:56 +0000172#endif
Soby Mathewb6814842018-04-04 09:40:32 +0100173 /* Ensure the configs are loaded in a valid address */
174 if (image_base < ARM_BL_RAM_BASE)
175 continue;
176#ifdef BL32_BASE
177 /*
178 * If BL32 is present, ensure that the configs don't
179 * overlap with it.
180 */
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000181 if ((image_base >= BL32_BASE) &&
182 (image_base <= BL32_LIMIT))
Soby Mathewb6814842018-04-04 09:40:32 +0100183 continue;
184#endif
185 }
186
187
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000188 cfg_mem_params->image_info.image_base = image_base;
189 cfg_mem_params->image_info.image_max_size = (uint32_t)image_size;
Soby Mathewb6814842018-04-04 09:40:32 +0100190
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000191 /*
192 * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from
193 * HW_CONFIG or FW_CONFIG nodes
194 */
Soby Mathewb6814842018-04-04 09:40:32 +0100195 cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
196 }
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000197}