blob: 6c6dc568ca34d1ad6e5e51621ae966b16caeb66e [file] [log] [blame]
Soby Mathew7c6df5b2018-01-15 14:43:42 +00001/*
2 * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3 *
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>
9
10#include <platform_def.h>
11
12#include <common/debug.h>
13#include <common/desc_image_load.h>
14#include <common/tbbr/tbbr_img_def.h>
John Tsichritzisc34341a2018-07-30 13:41:52 +010015#if TRUSTED_BOARD_BOOT
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <drivers/auth/mbedtls/mbedtls_config.h>
John Tsichritzisc34341a2018-07-30 13:41:52 +010017#endif
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000018#include <plat/common/platform.h>
Soby Mathew7c6df5b2018-01-15 14:43:42 +000019
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000020#include <arm_dyn_cfg_helpers.h>
21#include <plat_arm.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 Tsichritzis03459c22018-09-07 10:52:12 +010025static size_t tb_fw_cfg_dtb_size;
Soby Mathew96a1c6b2018-01-15 14:45:33 +000026
John Tsichritzisc34341a2018-07-30 13:41:52 +010027
28#if TRUSTED_BOARD_BOOT
29
30static void *mbedtls_heap_addr;
31static size_t mbedtls_heap_size;
32
33/*
34 * This function is the implementation of the shared Mbed TLS heap between
35 * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1
36 * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file
37 * which is a DTB.
38 *
39 * This function is placed inside an #if directive for the below reasons:
40 * - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot
41 * is enabled.
42 * - This implementation requires the DTB to be present so that BL1 has a
Antonio Nino Diaz05f49572018-09-25 11:37:23 +010043 * mechanism to pass the pointer to BL2.
John Tsichritzisc34341a2018-07-30 13:41:52 +010044 */
45int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
46{
47 assert(heap_addr != NULL);
48 assert(heap_size != NULL);
49
50#if defined(IMAGE_BL1) || BL2_AT_EL3
51
52 /* If in BL1 or BL2_AT_EL3 define a heap */
53 static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
54
55 *heap_addr = heap;
56 *heap_size = sizeof(heap);
57 mbedtls_heap_addr = heap;
58 mbedtls_heap_size = sizeof(heap);
59
60#elif defined(IMAGE_BL2)
61
62 int err;
63
64 /* If in BL2, retrieve the already allocated heap's info from DTB */
John Tsichritzis36507682018-09-07 10:42:37 +010065 if (tb_fw_cfg_dtb != NULL) {
66 err = arm_get_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, heap_addr,
67 heap_size);
68 if (err < 0) {
69 ERROR("BL2: unable to retrieve shared Mbed TLS heap information from DTB\n");
70 panic();
71 }
72 } else {
73 ERROR("BL2: DTB missing, cannot get Mbed TLS heap\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +010074 panic();
75 }
76#endif
77
78 return 0;
79}
80
81/*
82 * Puts the shared Mbed TLS heap information to the DTB.
83 * Executed only from BL1.
84 */
85void arm_bl1_set_mbedtls_heap(void)
86{
87 int err;
88
89 /*
90 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
91 * platform. As such, we don't attempt to write to the DTB at all.
92 *
93 * If mbedtls_heap_addr==NULL, then it means we are using the default
94 * heap implementation. As such, BL2 will have its own heap for sure
95 * and hence there is no need to pass any information to the DTB.
96 *
97 * In the latter case, if we still wanted to write in the DTB the heap
98 * information, we would need to call plat_get_mbedtls_heap to retrieve
99 * the default heap's address and size.
100 */
101 if ((tb_fw_cfg_dtb != NULL) && (mbedtls_heap_addr != NULL)) {
102 err = arm_set_dtb_mbedtls_heap_info(tb_fw_cfg_dtb,
103 mbedtls_heap_addr, mbedtls_heap_size);
104 if (err < 0) {
John Tsichritzis36507682018-09-07 10:42:37 +0100105 ERROR("BL1: unable to write shared Mbed TLS heap information to DTB\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +0100106 panic();
107 }
John Tsichritzis03459c22018-09-07 10:52:12 +0100108 /*
109 * Ensure that the info written to the DTB is visible to other
110 * images. It's critical because BL2 won't be able to proceed
111 * without the heap info.
112 */
113 flush_dcache_range((uintptr_t)tb_fw_cfg_dtb,
114 tb_fw_cfg_dtb_size);
John Tsichritzisc34341a2018-07-30 13:41:52 +0100115 }
116}
117
118#endif /* TRUSTED_BOARD_BOOT */
119
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000120/*
121 * Helper function to load TB_FW_CONFIG and populate the load information to
122 * arg0 of BL2 entrypoint info.
123 */
124void arm_load_tb_fw_config(void)
125{
126 int err;
Antonio Nino Diazb5acb3f2018-10-30 16:32:48 +0000127 uintptr_t config_base = 0UL;
128 image_desc_t *desc;
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000129
130 image_desc_t arm_tb_fw_info = {
131 .image_id = TB_FW_CONFIG_ID,
132 SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY,
133 VERSION_2, image_info_t, 0),
134 .image_info.image_base = ARM_TB_FW_CONFIG_BASE,
John Tsichritzis03459c22018-09-07 10:52:12 +0100135 .image_info.image_max_size =
136 ARM_TB_FW_CONFIG_LIMIT - ARM_TB_FW_CONFIG_BASE
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000137 };
138
139 VERBOSE("BL1: Loading TB_FW_CONFIG\n");
140 err = load_auth_image(TB_FW_CONFIG_ID, &arm_tb_fw_info.image_info);
Soby Mathewcc364842018-02-21 01:16:39 +0000141 if (err != 0) {
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000142 /* Return if TB_FW_CONFIG is not loaded */
143 VERBOSE("Failed to load TB_FW_CONFIG\n");
144 return;
145 }
146
John Tsichritzisc34341a2018-07-30 13:41:52 +0100147 /* At this point we know that a DTB is indeed available */
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000148 config_base = arm_tb_fw_info.image_info.image_base;
John Tsichritzisc34341a2018-07-30 13:41:52 +0100149 tb_fw_cfg_dtb = (void *)config_base;
John Tsichritzis03459c22018-09-07 10:52:12 +0100150 tb_fw_cfg_dtb_size = (size_t)arm_tb_fw_info.image_info.image_max_size;
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000151
152 /* The BL2 ep_info arg0 is modified to point to TB_FW_CONFIG */
Antonio Nino Diazb5acb3f2018-10-30 16:32:48 +0000153 desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
154 assert(desc != NULL);
155 desc->ep_info.args.arg0 = config_base;
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000156
Antonio Nino Diazb5acb3f2018-10-30 16:32:48 +0000157 INFO("BL1: TB_FW_CONFIG loaded at address = 0x%lx\n", config_base);
Soby Mathew45e39e22018-03-26 15:16:46 +0100158
159#if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
160 int tb_fw_node;
161 uint32_t disable_auth = 0;
162
163 err = arm_dyn_tb_fw_cfg_init((void *)config_base, &tb_fw_node);
164 if (err < 0) {
John Tsichritzis116d78a2018-06-15 11:43:02 +0100165 ERROR("Invalid TB_FW_CONFIG loaded\n");
166 panic();
Soby Mathew45e39e22018-03-26 15:16:46 +0100167 }
168
169 err = arm_dyn_get_disable_auth((void *)config_base, tb_fw_node, &disable_auth);
170 if (err < 0)
171 return;
172
173 if (disable_auth == 1)
174 dyn_disable_auth();
175#endif
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000176}
177
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000178/*
179 * BL2 utility function to set the address of TB_FW_CONFIG passed from BL1.
180 */
181void arm_bl2_set_tb_cfg_addr(void *dtb)
182{
Soby Mathewcc364842018-02-21 01:16:39 +0000183 assert(dtb != NULL);
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000184 tb_fw_cfg_dtb = dtb;
185}
186
187/*
188 * BL2 utility function to initialize dynamic configuration specified by
Soby Mathewb6814842018-04-04 09:40:32 +0100189 * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
190 * specified in TB_FW_CONFIG.
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000191 */
192void arm_bl2_dyn_cfg_init(void)
193{
Soby Mathewb6814842018-04-04 09:40:32 +0100194 int err = 0, tb_fw_node;
195 unsigned int i;
196 bl_mem_params_node_t *cfg_mem_params = NULL;
197 uint64_t image_base;
198 uint32_t image_size;
199 const unsigned int config_ids[] = {
200 HW_CONFIG_ID,
201 SOC_FW_CONFIG_ID,
202 NT_FW_CONFIG_ID,
203#ifdef SPD_tspd
204 /* Currently tos_fw_config is only present for TSP */
205 TOS_FW_CONFIG_ID
206#endif
207 };
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000208
209 if (tb_fw_cfg_dtb == NULL) {
210 VERBOSE("No TB_FW_CONFIG specified\n");
211 return;
212 }
213
John Tsichritzis5b8183c2018-08-23 09:57:54 +0100214 err = arm_dyn_tb_fw_cfg_init(tb_fw_cfg_dtb, &tb_fw_node);
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000215 if (err < 0) {
216 ERROR("Invalid TB_FW_CONFIG passed from BL1\n");
217 panic();
218 }
219
Soby Mathewb6814842018-04-04 09:40:32 +0100220 /* Iterate through all the fw config IDs */
221 for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
222 /* Get the config load address and size from TB_FW_CONFIG */
223 cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
224 if (cfg_mem_params == NULL) {
225 VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
226 continue;
227 }
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000228
John Tsichritzis5b8183c2018-08-23 09:57:54 +0100229 err = arm_dyn_get_config_load_info(tb_fw_cfg_dtb, tb_fw_node,
Soby Mathewb6814842018-04-04 09:40:32 +0100230 config_ids[i], &image_base, &image_size);
231 if (err < 0) {
232 VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n",
233 config_ids[i]);
234 continue;
235 }
236
237 /*
238 * Do some runtime checks on the load addresses of soc_fw_config,
239 * tos_fw_config, nt_fw_config. This is not a comprehensive check
240 * of all invalid addresses but to prevent trivial porting errors.
241 */
242 if (config_ids[i] != HW_CONFIG_ID) {
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000243
Antonio Nino Diazb5acb3f2018-10-30 16:32:48 +0000244 if (check_uptr_overflow(image_base, image_size))
Soby Mathewb6814842018-04-04 09:40:32 +0100245 continue;
246
Soby Mathewaf14b462018-06-01 16:53:38 +0100247 /* Ensure the configs don't overlap with BL31 */
248 if ((image_base > BL31_BASE) || ((image_base + image_size) > BL31_BASE))
Soby Mathewb6814842018-04-04 09:40:32 +0100249 continue;
250
251 /* Ensure the configs are loaded in a valid address */
252 if (image_base < ARM_BL_RAM_BASE)
253 continue;
254#ifdef BL32_BASE
255 /*
256 * If BL32 is present, ensure that the configs don't
257 * overlap with it.
258 */
259 if (image_base >= BL32_BASE && image_base <= BL32_LIMIT)
260 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
268 /* Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from HW_CONFIG node */
269 cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
270 }
Soby Mathew45e39e22018-03-26 15:16:46 +0100271
272#if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
273 uint32_t disable_auth = 0;
274
John Tsichritzis5b8183c2018-08-23 09:57:54 +0100275 err = arm_dyn_get_disable_auth(tb_fw_cfg_dtb, tb_fw_node,
Soby Mathew45e39e22018-03-26 15:16:46 +0100276 &disable_auth);
277 if (err < 0)
278 return;
279
280 if (disable_auth == 1)
281 dyn_disable_auth();
282#endif
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000283}