blob: f2570a84ccb9a61aeae35f83450586a2e08c3539 [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
Soby Mathew96a1c6b2018-01-15 14:45:33 +00007#include <arm_dyn_cfg_helpers.h>
Soby Mathew7c6df5b2018-01-15 14:43:42 +00008#include <assert.h>
9#include <debug.h>
10#include <desc_image_load.h>
John Tsichritzisc34341a2018-07-30 13:41:52 +010011#if TRUSTED_BOARD_BOOT
12#include <mbedtls_config.h>
13#endif
Soby Mathewcc364842018-02-21 01:16:39 +000014#include <plat_arm.h>
Soby Mathew7c6df5b2018-01-15 14:43:42 +000015#include <platform.h>
16#include <platform_def.h>
17#include <string.h>
18#include <tbbr_img_def.h>
19
20#if LOAD_IMAGE_V2
21
John Tsichritzisc34341a2018-07-30 13:41:52 +010022/* Variable to store the address of TB_FW_CONFIG file */
Soby Mathew96a1c6b2018-01-15 14:45:33 +000023static void *tb_fw_cfg_dtb;
24
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
41 * mechanism to pass the pointer to BL2. If LOAD_IMAGE_V2=0 then
42 * TB_FW_CONFIG is not present, which means that this implementation
43 * cannot be applied.
44 */
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 */
65 err = arm_get_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, heap_addr,
66 heap_size);
67 if (err < 0) {
68 ERROR("BL2: unable to retrieve shared Mbed TLS heap "
69 "information from DTB\n");
70 panic();
71 }
72#endif
73
74 return 0;
75}
76
77/*
78 * Puts the shared Mbed TLS heap information to the DTB.
79 * Executed only from BL1.
80 */
81void arm_bl1_set_mbedtls_heap(void)
82{
83 int err;
84
85 /*
86 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
87 * platform. As such, we don't attempt to write to the DTB at all.
88 *
89 * If mbedtls_heap_addr==NULL, then it means we are using the default
90 * heap implementation. As such, BL2 will have its own heap for sure
91 * and hence there is no need to pass any information to the DTB.
92 *
93 * In the latter case, if we still wanted to write in the DTB the heap
94 * information, we would need to call plat_get_mbedtls_heap to retrieve
95 * the default heap's address and size.
96 */
97 if ((tb_fw_cfg_dtb != NULL) && (mbedtls_heap_addr != NULL)) {
98 err = arm_set_dtb_mbedtls_heap_info(tb_fw_cfg_dtb,
99 mbedtls_heap_addr, mbedtls_heap_size);
100 if (err < 0) {
101 ERROR("BL1: unable to write shared Mbed TLS heap "
102 "information to DTB\n");
103 panic();
104 }
105 }
106}
107
108#endif /* TRUSTED_BOARD_BOOT */
109
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000110/*
111 * Helper function to load TB_FW_CONFIG and populate the load information to
112 * arg0 of BL2 entrypoint info.
113 */
114void arm_load_tb_fw_config(void)
115{
116 int err;
117 uintptr_t config_base = 0;
118 image_desc_t *image_desc;
119
120 image_desc_t arm_tb_fw_info = {
121 .image_id = TB_FW_CONFIG_ID,
122 SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY,
123 VERSION_2, image_info_t, 0),
124 .image_info.image_base = ARM_TB_FW_CONFIG_BASE,
125 .image_info.image_max_size = ARM_TB_FW_CONFIG_LIMIT - ARM_TB_FW_CONFIG_BASE,
126 };
127
128 VERBOSE("BL1: Loading TB_FW_CONFIG\n");
129 err = load_auth_image(TB_FW_CONFIG_ID, &arm_tb_fw_info.image_info);
Soby Mathewcc364842018-02-21 01:16:39 +0000130 if (err != 0) {
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000131 /* Return if TB_FW_CONFIG is not loaded */
132 VERBOSE("Failed to load TB_FW_CONFIG\n");
133 return;
134 }
135
John Tsichritzisc34341a2018-07-30 13:41:52 +0100136 /* At this point we know that a DTB is indeed available */
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000137 config_base = arm_tb_fw_info.image_info.image_base;
John Tsichritzisc34341a2018-07-30 13:41:52 +0100138 tb_fw_cfg_dtb = (void *)config_base;
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000139
140 /* The BL2 ep_info arg0 is modified to point to TB_FW_CONFIG */
141 image_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
Soby Mathewcc364842018-02-21 01:16:39 +0000142 assert(image_desc != NULL);
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000143 image_desc->ep_info.args.arg0 = config_base;
144
145 INFO("BL1: TB_FW_CONFIG loaded at address = %p\n",
146 (void *) config_base);
Soby Mathew45e39e22018-03-26 15:16:46 +0100147
148#if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
149 int tb_fw_node;
150 uint32_t disable_auth = 0;
151
152 err = arm_dyn_tb_fw_cfg_init((void *)config_base, &tb_fw_node);
153 if (err < 0) {
John Tsichritzis116d78a2018-06-15 11:43:02 +0100154 ERROR("Invalid TB_FW_CONFIG loaded\n");
155 panic();
Soby Mathew45e39e22018-03-26 15:16:46 +0100156 }
157
158 err = arm_dyn_get_disable_auth((void *)config_base, tb_fw_node, &disable_auth);
159 if (err < 0)
160 return;
161
162 if (disable_auth == 1)
163 dyn_disable_auth();
164#endif
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000165}
166
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000167/*
168 * BL2 utility function to set the address of TB_FW_CONFIG passed from BL1.
169 */
170void arm_bl2_set_tb_cfg_addr(void *dtb)
171{
Soby Mathewcc364842018-02-21 01:16:39 +0000172 assert(dtb != NULL);
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000173 tb_fw_cfg_dtb = dtb;
174}
175
176/*
177 * BL2 utility function to initialize dynamic configuration specified by
Soby Mathewb6814842018-04-04 09:40:32 +0100178 * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
179 * specified in TB_FW_CONFIG.
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000180 */
181void arm_bl2_dyn_cfg_init(void)
182{
Soby Mathewb6814842018-04-04 09:40:32 +0100183 int err = 0, tb_fw_node;
184 unsigned int i;
185 bl_mem_params_node_t *cfg_mem_params = NULL;
186 uint64_t image_base;
187 uint32_t image_size;
188 const unsigned int config_ids[] = {
189 HW_CONFIG_ID,
190 SOC_FW_CONFIG_ID,
191 NT_FW_CONFIG_ID,
192#ifdef SPD_tspd
193 /* Currently tos_fw_config is only present for TSP */
194 TOS_FW_CONFIG_ID
195#endif
196 };
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000197
198 if (tb_fw_cfg_dtb == NULL) {
199 VERBOSE("No TB_FW_CONFIG specified\n");
200 return;
201 }
202
John Tsichritzis5b8183c2018-08-23 09:57:54 +0100203 err = arm_dyn_tb_fw_cfg_init(tb_fw_cfg_dtb, &tb_fw_node);
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000204 if (err < 0) {
205 ERROR("Invalid TB_FW_CONFIG passed from BL1\n");
206 panic();
207 }
208
Soby Mathewb6814842018-04-04 09:40:32 +0100209 /* Iterate through all the fw config IDs */
210 for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
211 /* Get the config load address and size from TB_FW_CONFIG */
212 cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
213 if (cfg_mem_params == NULL) {
214 VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
215 continue;
216 }
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000217
John Tsichritzis5b8183c2018-08-23 09:57:54 +0100218 err = arm_dyn_get_config_load_info(tb_fw_cfg_dtb, tb_fw_node,
Soby Mathewb6814842018-04-04 09:40:32 +0100219 config_ids[i], &image_base, &image_size);
220 if (err < 0) {
221 VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n",
222 config_ids[i]);
223 continue;
224 }
225
226 /*
227 * Do some runtime checks on the load addresses of soc_fw_config,
228 * tos_fw_config, nt_fw_config. This is not a comprehensive check
229 * of all invalid addresses but to prevent trivial porting errors.
230 */
231 if (config_ids[i] != HW_CONFIG_ID) {
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000232
Soby Mathewb6814842018-04-04 09:40:32 +0100233 if (check_uptr_overflow(image_base, image_size) != 0)
234 continue;
235
Soby Mathewaf14b462018-06-01 16:53:38 +0100236 /* Ensure the configs don't overlap with BL31 */
237 if ((image_base > BL31_BASE) || ((image_base + image_size) > BL31_BASE))
Soby Mathewb6814842018-04-04 09:40:32 +0100238 continue;
239
240 /* Ensure the configs are loaded in a valid address */
241 if (image_base < ARM_BL_RAM_BASE)
242 continue;
243#ifdef BL32_BASE
244 /*
245 * If BL32 is present, ensure that the configs don't
246 * overlap with it.
247 */
248 if (image_base >= BL32_BASE && image_base <= BL32_LIMIT)
249 continue;
250#endif
251 }
252
253
254 cfg_mem_params->image_info.image_base = (uintptr_t)image_base;
255 cfg_mem_params->image_info.image_max_size = image_size;
256
257 /* Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from HW_CONFIG node */
258 cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
259 }
Soby Mathew45e39e22018-03-26 15:16:46 +0100260
261#if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
262 uint32_t disable_auth = 0;
263
John Tsichritzis5b8183c2018-08-23 09:57:54 +0100264 err = arm_dyn_get_disable_auth(tb_fw_cfg_dtb, tb_fw_node,
Soby Mathew45e39e22018-03-26 15:16:46 +0100265 &disable_auth);
266 if (err < 0)
267 return;
268
269 if (disable_auth == 1)
270 dyn_disable_auth();
271#endif
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000272}
273
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000274#endif /* LOAD_IMAGE_V2 */