1 /*
2 * Copyright (c) 2018-2021, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8 #include <string.h>
9 #include <libfdt.h>
10
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>
16 #if TRUSTED_BOARD_BOOT
17 #include <drivers/auth/mbedtls/mbedtls_config.h>
18 #endif
19 #include <lib/fconf/fconf.h>
20 #include <lib/fconf/fconf_dyn_cfg_getter.h>
21 #include <lib/fconf/fconf_tbbr_getter.h>
22
23 #include <plat/arm/common/arm_dyn_cfg_helpers.h>
24 #include <plat/arm/common/plat_arm.h>
25
26 #if TRUSTED_BOARD_BOOT
27
28 static void *mbedtls_heap_addr;
29 static 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
41 * mechanism to pass the pointer to BL2.
42 */
arm_get_mbedtls_heap(void ** heap_addr,size_t * heap_size)43 int 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
60 /* If in BL2, retrieve the already allocated heap's info from DTB */
61 *heap_addr = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_addr);
62 *heap_size = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_size);
63
64 #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 */
arm_bl1_set_mbedtls_heap(void)73 void arm_bl1_set_mbedtls_heap(void)
74 {
75 int err;
76 uintptr_t tb_fw_cfg_dtb;
77 const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
78
79 /*
80 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
81 * platform. As such, we don't attempt to write to the DTB at all.
82 *
83 * If mbedtls_heap_addr==NULL, then it means we are using the default
84 * heap implementation. As such, BL2 will have its own heap for sure
85 * and hence there is no need to pass any information to the DTB.
86 *
87 * In the latter case, if we still wanted to write in the DTB the heap
88 * information, we would need to call plat_get_mbedtls_heap to retrieve
89 * the default heap's address and size.
90 */
91
92 tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
93 assert(tb_fw_config_info != NULL);
94
95 tb_fw_cfg_dtb = tb_fw_config_info->config_addr;
96
97 if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) {
98 /* As libfdt uses void *, we can't avoid this cast */
99 void *dtb = (void *)tb_fw_cfg_dtb;
100
101 err = arm_set_dtb_mbedtls_heap_info(dtb,
102 mbedtls_heap_addr, mbedtls_heap_size);
103 if (err < 0) {
104 ERROR("%swrite shared Mbed TLS heap information%s",
105 "BL1: unable to ", " to DTB\n");
106 panic();
107 }
108 #if !MEASURED_BOOT
109 /*
110 * Ensure that the info written to the DTB is visible to other
111 * images. It's critical because BL2 won't be able to proceed
112 * without the heap info.
113 *
114 * In MEASURED_BOOT case flushing is done in a function which
115 * is called after heap information is written in the DTB.
116 */
117 flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize(dtb));
118 #endif /* !MEASURED_BOOT */
119 }
120 }
121 #endif /* TRUSTED_BOARD_BOOT */
122
123 /*
124 * BL2 utility function to initialize dynamic configuration specified by
125 * FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
126 * specified in FW_CONFIG.
127 */
arm_bl2_dyn_cfg_init(void)128 void arm_bl2_dyn_cfg_init(void)
129 {
130 unsigned int i;
131 bl_mem_params_node_t *cfg_mem_params = NULL;
132 uintptr_t image_base;
133 uint32_t image_size;
134 const unsigned int config_ids[] = {
135 HW_CONFIG_ID,
136 SOC_FW_CONFIG_ID,
137 NT_FW_CONFIG_ID,
138 TOS_FW_CONFIG_ID
139 };
140
141 const struct dyn_cfg_dtb_info_t *dtb_info;
142
143 /* Iterate through all the fw config IDs */
144 for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
145 /* Get the config load address and size from TB_FW_CONFIG */
146 cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
147 if (cfg_mem_params == NULL) {
148 VERBOSE("%sHW_CONFIG in bl_mem_params_node\n",
149 "Couldn't find ");
150 continue;
151 }
152
153 dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]);
154 if (dtb_info == NULL) {
155 VERBOSE("%sconfig_id %d load info in TB_FW_CONFIG\n",
156 "Couldn't find ", config_ids[i]);
157 continue;
158 }
159
160 image_base = dtb_info->config_addr;
161 image_size = dtb_info->config_max_size;
162
163 /*
164 * Do some runtime checks on the load addresses of soc_fw_config,
165 * tos_fw_config, nt_fw_config. This is not a comprehensive check
166 * of all invalid addresses but to prevent trivial porting errors.
167 */
168 if (config_ids[i] != HW_CONFIG_ID) {
169
170 if (check_uptr_overflow(image_base, image_size)) {
171 continue;
172 }
173 #ifdef BL31_BASE
174 /* Ensure the configs don't overlap with BL31 */
175 if ((image_base >= BL31_BASE) &&
176 (image_base <= BL31_LIMIT)) {
177 continue;
178 }
179 #endif
180 /* Ensure the configs are loaded in a valid address */
181 if (image_base < ARM_BL_RAM_BASE) {
182 continue;
183 }
184 #ifdef BL32_BASE
185 /*
186 * If BL32 is present, ensure that the configs don't
187 * overlap with it.
188 */
189 if ((image_base >= BL32_BASE) &&
190 (image_base <= BL32_LIMIT)) {
191 continue;
192 }
193 #endif
194 }
195
196 cfg_mem_params->image_info.image_base = image_base;
197 cfg_mem_params->image_info.image_max_size = (uint32_t)image_size;
198
199 /*
200 * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from
201 * HW_CONFIG or FW_CONFIG nodes
202 */
203 cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
204 }
205 }
206