blob: a28e0ccff250e1fff07c460b50254973778dd9f0 [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>
Alexei Fedorov25d7c882020-03-20 18:38:55 +000018#if MEASURED_BOOT
19#include <drivers/auth/crypto_mod.h>
20#include <mbedtls/md.h>
21#endif
John Tsichritzisc34341a2018-07-30 13:41:52 +010022#endif
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000023#include <lib/fconf/fconf.h>
24#include <lib/fconf/fconf_dyn_cfg_getter.h>
Louis Mayencourt5b9055f2019-10-01 10:45:14 +010025#include <lib/fconf/fconf_tbbr_getter.h>
Antonio Nino Diazbd7b7402019-01-25 14:30:04 +000026#include <plat/arm/common/arm_dyn_cfg_helpers.h>
27#include <plat/arm/common/plat_arm.h>
Soby Mathew7c6df5b2018-01-15 14:43:42 +000028
John Tsichritzisc34341a2018-07-30 13:41:52 +010029#if TRUSTED_BOARD_BOOT
30
31static void *mbedtls_heap_addr;
32static size_t mbedtls_heap_size;
33
34/*
35 * This function is the implementation of the shared Mbed TLS heap between
36 * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1
37 * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file
38 * which is a DTB.
39 *
40 * This function is placed inside an #if directive for the below reasons:
41 * - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot
42 * is enabled.
43 * - This implementation requires the DTB to be present so that BL1 has a
Antonio Nino Diaz05f49572018-09-25 11:37:23 +010044 * mechanism to pass the pointer to BL2.
John Tsichritzisc34341a2018-07-30 13:41:52 +010045 */
46int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
47{
48 assert(heap_addr != NULL);
49 assert(heap_size != NULL);
50
51#if defined(IMAGE_BL1) || BL2_AT_EL3
52
53 /* If in BL1 or BL2_AT_EL3 define a heap */
54 static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
55
56 *heap_addr = heap;
57 *heap_size = sizeof(heap);
58 mbedtls_heap_addr = heap;
59 mbedtls_heap_size = sizeof(heap);
60
61#elif defined(IMAGE_BL2)
62
John Tsichritzisc34341a2018-07-30 13:41:52 +010063 /* If in BL2, retrieve the already allocated heap's info from DTB */
Louis Mayencourt5b9055f2019-10-01 10:45:14 +010064 *heap_addr = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_addr);
65 *heap_size = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_size);
66
John Tsichritzisc34341a2018-07-30 13:41:52 +010067#endif
68
69 return 0;
70}
71
72/*
73 * Puts the shared Mbed TLS heap information to the DTB.
74 * Executed only from BL1.
75 */
76void arm_bl1_set_mbedtls_heap(void)
77{
78 int err;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000079 uintptr_t tb_fw_cfg_dtb;
Manish V Badarkhe8c66f7a2020-06-11 22:09:10 +010080 const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
John Tsichritzisc34341a2018-07-30 13:41:52 +010081
82 /*
83 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
84 * platform. As such, we don't attempt to write to the DTB at all.
85 *
86 * If mbedtls_heap_addr==NULL, then it means we are using the default
87 * heap implementation. As such, BL2 will have its own heap for sure
88 * and hence there is no need to pass any information to the DTB.
89 *
90 * In the latter case, if we still wanted to write in the DTB the heap
91 * information, we would need to call plat_get_mbedtls_heap to retrieve
92 * the default heap's address and size.
93 */
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000094
Manish V Badarkhe8c66f7a2020-06-11 22:09:10 +010095 tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
96 tb_fw_cfg_dtb = tb_fw_config_info->config_addr;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000097
98 if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) {
99 /* As libfdt use void *, we can't avoid this cast */
100 void *dtb = (void *)tb_fw_cfg_dtb;
101
102 err = arm_set_dtb_mbedtls_heap_info(dtb,
John Tsichritzisc34341a2018-07-30 13:41:52 +0100103 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 }
Alexei Fedorov25d7c882020-03-20 18:38:55 +0000108#if !MEASURED_BOOT
John Tsichritzis03459c22018-09-07 10:52:12 +0100109 /*
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.
Alexei Fedorov25d7c882020-03-20 18:38:55 +0000113 *
114 * In MEASURED_BOOT case flushing is done in
115 * arm_bl1_set_bl2_hash() function which is called after heap
116 * information is written in the DTB.
John Tsichritzis03459c22018-09-07 10:52:12 +0100117 */
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000118 flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize(dtb));
Alexei Fedorov25d7c882020-03-20 18:38:55 +0000119#endif /* !MEASURED_BOOT */
John Tsichritzisc34341a2018-07-30 13:41:52 +0100120 }
121}
122
Alexei Fedorov25d7c882020-03-20 18:38:55 +0000123#if MEASURED_BOOT
124/*
125 * Puts the BL2 hash data to TB_FW_CONFIG DTB.
126 * Executed only from BL1.
127 */
128void arm_bl1_set_bl2_hash(image_desc_t *image_desc)
129{
130 unsigned char hash_data[MBEDTLS_MD_MAX_SIZE];
131 image_info_t image_info = image_desc->image_info;
132 uintptr_t tb_fw_cfg_dtb;
133 int err;
Manish V Badarkhe8c66f7a2020-06-11 22:09:10 +0100134 const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
Alexei Fedorov25d7c882020-03-20 18:38:55 +0000135
Manish V Badarkhe8c66f7a2020-06-11 22:09:10 +0100136 tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
137 tb_fw_cfg_dtb = tb_fw_config_info->config_addr;
Alexei Fedorov25d7c882020-03-20 18:38:55 +0000138
139 /*
140 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
141 * platform. As such, we cannot write to the DTB at all and pass
142 * measured data.
143 */
144 if (tb_fw_cfg_dtb == 0UL) {
145 panic();
146 }
147
148 /* Calculate hash */
149 err = crypto_mod_calc_hash(MBEDTLS_MD_ID,
150 (void *)image_info.image_base,
151 image_info.image_size, hash_data);
152 if (err != 0) {
153 ERROR("BL1: unable to calculate BL2 hash\n");
154 panic();
155 }
156
157 err = arm_set_bl2_hash_info((void *)tb_fw_cfg_dtb, hash_data);
158 if (err < 0) {
159 ERROR("BL1: unable to write BL2 hash data to DTB\n");
160 panic();
161 }
162
163 /*
164 * Ensure that the info written to the DTB is visible to other
165 * images. It's critical because BL2 won't be able to proceed
166 * without the heap info and its hash data.
167 */
168 flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize((void *)tb_fw_cfg_dtb));
169}
170#endif /* MEASURED_BOOT */
John Tsichritzisc34341a2018-07-30 13:41:52 +0100171#endif /* TRUSTED_BOARD_BOOT */
172
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000173/*
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000174 * BL2 utility function to initialize dynamic configuration specified by
Manish V Badarkhe1da211a2020-05-31 10:17:59 +0100175 * FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
176 * specified in FW_CONFIG.
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000177 */
178void arm_bl2_dyn_cfg_init(void)
179{
Soby Mathewb6814842018-04-04 09:40:32 +0100180 unsigned int i;
181 bl_mem_params_node_t *cfg_mem_params = NULL;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000182 uintptr_t image_base;
183 size_t image_size;
Soby Mathewb6814842018-04-04 09:40:32 +0100184 const unsigned int config_ids[] = {
185 HW_CONFIG_ID,
186 SOC_FW_CONFIG_ID,
187 NT_FW_CONFIG_ID,
Achin Guptada6ef0e2019-10-11 14:54:48 +0100188#if defined(SPD_tspd) || defined(SPD_spmd)
189 /* tos_fw_config is only present for TSPD/SPMD */
Soby Mathewb6814842018-04-04 09:40:32 +0100190 TOS_FW_CONFIG_ID
191#endif
192 };
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000193
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000194 const struct dyn_cfg_dtb_info_t *dtb_info;
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000195
Soby Mathewb6814842018-04-04 09:40:32 +0100196 /* Iterate through all the fw config IDs */
197 for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
198 /* Get the config load address and size from TB_FW_CONFIG */
199 cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
200 if (cfg_mem_params == NULL) {
201 VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
202 continue;
203 }
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000204
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000205 dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]);
206 if (dtb_info == NULL) {
Soby Mathewb6814842018-04-04 09:40:32 +0100207 VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n",
208 config_ids[i]);
209 continue;
210 }
211
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000212 image_base = dtb_info->config_addr;
213 image_size = dtb_info->config_max_size;
214
Soby Mathewb6814842018-04-04 09:40:32 +0100215 /*
216 * Do some runtime checks on the load addresses of soc_fw_config,
217 * tos_fw_config, nt_fw_config. This is not a comprehensive check
218 * of all invalid addresses but to prevent trivial porting errors.
219 */
220 if (config_ids[i] != HW_CONFIG_ID) {
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000221
Antonio Nino Diazb5acb3f2018-10-30 16:32:48 +0000222 if (check_uptr_overflow(image_base, image_size))
Soby Mathewb6814842018-04-04 09:40:32 +0100223 continue;
224
Usama Arife97998f2018-11-30 15:43:56 +0000225#ifdef BL31_BASE
Soby Mathewaf14b462018-06-01 16:53:38 +0100226 /* Ensure the configs don't overlap with BL31 */
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000227 if ((image_base >= BL31_BASE) &&
228 (image_base <= BL31_LIMIT))
Soby Mathewb6814842018-04-04 09:40:32 +0100229 continue;
Usama Arife97998f2018-11-30 15:43:56 +0000230#endif
Soby Mathewb6814842018-04-04 09:40:32 +0100231 /* Ensure the configs are loaded in a valid address */
232 if (image_base < ARM_BL_RAM_BASE)
233 continue;
234#ifdef BL32_BASE
235 /*
236 * If BL32 is present, ensure that the configs don't
237 * overlap with it.
238 */
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000239 if ((image_base >= BL32_BASE) &&
240 (image_base <= BL32_LIMIT))
Soby Mathewb6814842018-04-04 09:40:32 +0100241 continue;
242#endif
243 }
244
245
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000246 cfg_mem_params->image_info.image_base = image_base;
247 cfg_mem_params->image_info.image_max_size = (uint32_t)image_size;
Soby Mathewb6814842018-04-04 09:40:32 +0100248
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000249 /*
250 * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from
251 * HW_CONFIG or FW_CONFIG nodes
252 */
Soby Mathewb6814842018-04-04 09:40:32 +0100253 cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
254 }
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000255}