blob: e6c5a736159ab27f0b4434cfff5515959fb8ba2e [file] [log] [blame]
Soby Mathew7c6df5b2018-01-15 14:43:42 +00001/*
Louis Mayencourt73d42d72019-12-09 11:29:38 +00002 * Copyright (c) 2018-2019, 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
Antonio Nino Diazbd7b7402019-01-25 14:30:04 +000019#include <plat/arm/common/arm_dyn_cfg_helpers.h>
20#include <plat/arm/common/plat_arm.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000021#include <plat/common/platform.h>
Soby Mathew7c6df5b2018-01-15 14:43:42 +000022
Antonio Nino Diaz05f49572018-09-25 11:37:23 +010023/* Variable to store the address to TB_FW_CONFIG passed from BL1 */
Soby Mathew96a1c6b2018-01-15 14:45:33 +000024static void *tb_fw_cfg_dtb;
John Tsichritzisc34341a2018-07-30 13:41:52 +010025
26#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
60 int err;
61
62 /* If in BL2, retrieve the already allocated heap's info from DTB */
John Tsichritzis36507682018-09-07 10:42:37 +010063 if (tb_fw_cfg_dtb != NULL) {
64 err = arm_get_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, heap_addr,
65 heap_size);
66 if (err < 0) {
67 ERROR("BL2: unable to retrieve shared Mbed TLS heap information from DTB\n");
68 panic();
69 }
70 } else {
71 ERROR("BL2: DTB missing, cannot get Mbed TLS heap\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +010072 panic();
73 }
74#endif
75
76 return 0;
77}
78
79/*
80 * Puts the shared Mbed TLS heap information to the DTB.
81 * Executed only from BL1.
82 */
83void arm_bl1_set_mbedtls_heap(void)
84{
85 int err;
86
87 /*
88 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
89 * platform. As such, we don't attempt to write to the DTB at all.
90 *
91 * If mbedtls_heap_addr==NULL, then it means we are using the default
92 * heap implementation. As such, BL2 will have its own heap for sure
93 * and hence there is no need to pass any information to the DTB.
94 *
95 * In the latter case, if we still wanted to write in the DTB the heap
96 * information, we would need to call plat_get_mbedtls_heap to retrieve
97 * the default heap's address and size.
98 */
99 if ((tb_fw_cfg_dtb != NULL) && (mbedtls_heap_addr != NULL)) {
100 err = arm_set_dtb_mbedtls_heap_info(tb_fw_cfg_dtb,
101 mbedtls_heap_addr, mbedtls_heap_size);
102 if (err < 0) {
John Tsichritzis36507682018-09-07 10:42:37 +0100103 ERROR("BL1: unable to write shared Mbed TLS heap information to DTB\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +0100104 panic();
105 }
John Tsichritzis03459c22018-09-07 10:52:12 +0100106 /*
107 * Ensure that the info written to the DTB is visible to other
108 * images. It's critical because BL2 won't be able to proceed
109 * without the heap info.
110 */
111 flush_dcache_range((uintptr_t)tb_fw_cfg_dtb,
Louis Mayencourt73d42d72019-12-09 11:29:38 +0000112 fdt_totalsize(tb_fw_cfg_dtb));
John Tsichritzisc34341a2018-07-30 13:41:52 +0100113 }
114}
115
116#endif /* TRUSTED_BOARD_BOOT */
117
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000118/*
119 * Helper function to load TB_FW_CONFIG and populate the load information to
120 * arg0 of BL2 entrypoint info.
121 */
122void arm_load_tb_fw_config(void)
123{
124 int err;
Antonio Nino Diazb5acb3f2018-10-30 16:32:48 +0000125 uintptr_t config_base = 0UL;
126 image_desc_t *desc;
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000127
128 image_desc_t arm_tb_fw_info = {
129 .image_id = TB_FW_CONFIG_ID,
130 SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY,
131 VERSION_2, image_info_t, 0),
132 .image_info.image_base = ARM_TB_FW_CONFIG_BASE,
John Tsichritzis03459c22018-09-07 10:52:12 +0100133 .image_info.image_max_size =
134 ARM_TB_FW_CONFIG_LIMIT - ARM_TB_FW_CONFIG_BASE
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000135 };
136
137 VERBOSE("BL1: Loading TB_FW_CONFIG\n");
138 err = load_auth_image(TB_FW_CONFIG_ID, &arm_tb_fw_info.image_info);
Soby Mathewcc364842018-02-21 01:16:39 +0000139 if (err != 0) {
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000140 /* Return if TB_FW_CONFIG is not loaded */
141 VERBOSE("Failed to load TB_FW_CONFIG\n");
142 return;
143 }
144
John Tsichritzisc34341a2018-07-30 13:41:52 +0100145 /* At this point we know that a DTB is indeed available */
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000146 config_base = arm_tb_fw_info.image_info.image_base;
John Tsichritzisc34341a2018-07-30 13:41:52 +0100147 tb_fw_cfg_dtb = (void *)config_base;
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000148
149 /* The BL2 ep_info arg0 is modified to point to TB_FW_CONFIG */
Antonio Nino Diazb5acb3f2018-10-30 16:32:48 +0000150 desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
151 assert(desc != NULL);
152 desc->ep_info.args.arg0 = config_base;
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000153
Antonio Nino Diazb5acb3f2018-10-30 16:32:48 +0000154 INFO("BL1: TB_FW_CONFIG loaded at address = 0x%lx\n", config_base);
Soby Mathew45e39e22018-03-26 15:16:46 +0100155
156#if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
157 int tb_fw_node;
158 uint32_t disable_auth = 0;
159
160 err = arm_dyn_tb_fw_cfg_init((void *)config_base, &tb_fw_node);
161 if (err < 0) {
John Tsichritzis116d78a2018-06-15 11:43:02 +0100162 ERROR("Invalid TB_FW_CONFIG loaded\n");
163 panic();
Soby Mathew45e39e22018-03-26 15:16:46 +0100164 }
165
166 err = arm_dyn_get_disable_auth((void *)config_base, tb_fw_node, &disable_auth);
167 if (err < 0)
168 return;
169
170 if (disable_auth == 1)
171 dyn_disable_auth();
172#endif
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000173}
174
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000175/*
176 * BL2 utility function to set the address of TB_FW_CONFIG passed from BL1.
177 */
178void arm_bl2_set_tb_cfg_addr(void *dtb)
179{
Soby Mathewcc364842018-02-21 01:16:39 +0000180 assert(dtb != NULL);
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000181 tb_fw_cfg_dtb = dtb;
182}
183
184/*
185 * BL2 utility function to initialize dynamic configuration specified by
Soby Mathewb6814842018-04-04 09:40:32 +0100186 * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
187 * specified in TB_FW_CONFIG.
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000188 */
189void arm_bl2_dyn_cfg_init(void)
190{
Soby Mathewb6814842018-04-04 09:40:32 +0100191 int err = 0, tb_fw_node;
192 unsigned int i;
193 bl_mem_params_node_t *cfg_mem_params = NULL;
194 uint64_t image_base;
195 uint32_t image_size;
196 const unsigned int config_ids[] = {
197 HW_CONFIG_ID,
198 SOC_FW_CONFIG_ID,
199 NT_FW_CONFIG_ID,
200#ifdef SPD_tspd
201 /* Currently tos_fw_config is only present for TSP */
202 TOS_FW_CONFIG_ID
203#endif
204 };
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000205
206 if (tb_fw_cfg_dtb == NULL) {
207 VERBOSE("No TB_FW_CONFIG specified\n");
208 return;
209 }
210
John Tsichritzis5b8183c2018-08-23 09:57:54 +0100211 err = arm_dyn_tb_fw_cfg_init(tb_fw_cfg_dtb, &tb_fw_node);
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000212 if (err < 0) {
213 ERROR("Invalid TB_FW_CONFIG passed from BL1\n");
214 panic();
215 }
216
Soby Mathewb6814842018-04-04 09:40:32 +0100217 /* Iterate through all the fw config IDs */
218 for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
219 /* Get the config load address and size from TB_FW_CONFIG */
220 cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
221 if (cfg_mem_params == NULL) {
222 VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
223 continue;
224 }
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000225
John Tsichritzis5b8183c2018-08-23 09:57:54 +0100226 err = arm_dyn_get_config_load_info(tb_fw_cfg_dtb, tb_fw_node,
Soby Mathewb6814842018-04-04 09:40:32 +0100227 config_ids[i], &image_base, &image_size);
228 if (err < 0) {
229 VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n",
230 config_ids[i]);
231 continue;
232 }
233
234 /*
235 * Do some runtime checks on the load addresses of soc_fw_config,
236 * tos_fw_config, nt_fw_config. This is not a comprehensive check
237 * of all invalid addresses but to prevent trivial porting errors.
238 */
239 if (config_ids[i] != HW_CONFIG_ID) {
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000240
Antonio Nino Diazb5acb3f2018-10-30 16:32:48 +0000241 if (check_uptr_overflow(image_base, image_size))
Soby Mathewb6814842018-04-04 09:40:32 +0100242 continue;
243
Usama Arife97998f2018-11-30 15:43:56 +0000244#ifdef BL31_BASE
Soby Mathewaf14b462018-06-01 16:53:38 +0100245 /* Ensure the configs don't overlap with BL31 */
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000246 if ((image_base >= BL31_BASE) &&
247 (image_base <= BL31_LIMIT))
Soby Mathewb6814842018-04-04 09:40:32 +0100248 continue;
Usama Arife97998f2018-11-30 15:43:56 +0000249#endif
Soby Mathewb6814842018-04-04 09:40:32 +0100250 /* Ensure the configs are loaded in a valid address */
251 if (image_base < ARM_BL_RAM_BASE)
252 continue;
253#ifdef BL32_BASE
254 /*
255 * If BL32 is present, ensure that the configs don't
256 * overlap with it.
257 */
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000258 if ((image_base >= BL32_BASE) &&
259 (image_base <= BL32_LIMIT))
Soby Mathewb6814842018-04-04 09:40:32 +0100260 continue;
261#endif
262 }
263
264
265 cfg_mem_params->image_info.image_base = (uintptr_t)image_base;
266 cfg_mem_params->image_info.image_max_size = image_size;
267
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000268 /*
269 * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from
270 * HW_CONFIG or FW_CONFIG nodes
271 */
Soby Mathewb6814842018-04-04 09:40:32 +0100272 cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
273 }
Soby Mathew45e39e22018-03-26 15:16:46 +0100274
275#if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
276 uint32_t disable_auth = 0;
277
John Tsichritzis5b8183c2018-08-23 09:57:54 +0100278 err = arm_dyn_get_disable_auth(tb_fw_cfg_dtb, tb_fw_node,
Soby Mathew45e39e22018-03-26 15:16:46 +0100279 &disable_auth);
280 if (err < 0)
281 return;
282
283 if (disable_auth == 1)
284 dyn_disable_auth();
285#endif
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000286}