arm_dyn_cfg.c 7.65 KB
Newer Older
1
/*
2
 * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
8
#include <string.h>
9
#include <libfdt.h>
10
11
12
13
14
15

#include <platform_def.h>

#include <common/debug.h>
#include <common/desc_image_load.h>
#include <common/tbbr/tbbr_img_def.h>
16
#if TRUSTED_BOARD_BOOT
17
#include <drivers/auth/mbedtls/mbedtls_config.h>
18
19
20
21
#if MEASURED_BOOT
#include <drivers/auth/crypto_mod.h>
#include <mbedtls/md.h>
#endif
22
#endif
23
24
#include <lib/fconf/fconf.h>
#include <lib/fconf/fconf_dyn_cfg_getter.h>
25
#include <lib/fconf/fconf_tbbr_getter.h>
26

27
28
#include <plat/arm/common/arm_dyn_cfg_helpers.h>
#include <plat/arm/common/plat_arm.h>
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#if TRUSTED_BOARD_BOOT

static void *mbedtls_heap_addr;
static size_t mbedtls_heap_size;

/*
 * This function is the implementation of the shared Mbed TLS heap between
 * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1
 * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file
 * which is a DTB.
 *
 * This function is placed inside an #if directive for the below reasons:
 *   - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot
 *     is enabled.
 *   - This implementation requires the DTB to be present so that BL1 has a
45
 *     mechanism to pass the pointer to BL2.
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 */
int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
{
	assert(heap_addr != NULL);
	assert(heap_size != NULL);

#if defined(IMAGE_BL1) || BL2_AT_EL3

	/* If in BL1 or BL2_AT_EL3 define a heap */
	static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];

	*heap_addr = heap;
	*heap_size = sizeof(heap);
	mbedtls_heap_addr = heap;
	mbedtls_heap_size = sizeof(heap);

#elif defined(IMAGE_BL2)

	/* If in BL2, retrieve the already allocated heap's info from DTB */
65
66
67
	*heap_addr = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_addr);
	*heap_size = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_size);

68
69
70
71
72
73
74
75
76
77
78
79
#endif

	return 0;
}

/*
 * Puts the shared Mbed TLS heap information to the DTB.
 * Executed only from BL1.
 */
void arm_bl1_set_mbedtls_heap(void)
{
	int err;
80
	uintptr_t tb_fw_cfg_dtb;
81
	const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
82
83
84
85
86
87
88
89
90
91
92
93
94

	/*
	 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
	 * platform. As such, we don't attempt to write to the DTB at all.
	 *
	 * If mbedtls_heap_addr==NULL, then it means we are using the default
	 * heap implementation. As such, BL2 will have its own heap for sure
	 * and hence there is no need to pass any information to the DTB.
	 *
	 * In the latter case, if we still wanted to write in the DTB the heap
	 * information, we would need to call plat_get_mbedtls_heap to retrieve
	 * the default heap's address and size.
	 */
95

96
	tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
97
98
	assert(tb_fw_config_info != NULL);

99
	tb_fw_cfg_dtb = tb_fw_config_info->config_addr;
100
101

	if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) {
102
		/* As libfdt uses void *, we can't avoid this cast */
103
104
105
		void *dtb = (void *)tb_fw_cfg_dtb;

		err = arm_set_dtb_mbedtls_heap_info(dtb,
106
107
			mbedtls_heap_addr, mbedtls_heap_size);
		if (err < 0) {
108
109
			ERROR("%swrite shared Mbed TLS heap information%s",
				"BL1: unable to ", " to DTB\n");
110
111
			panic();
		}
112
#if !MEASURED_BOOT
113
114
115
116
		/*
		 * Ensure that the info written to the DTB is visible to other
		 * images. It's critical because BL2 won't be able to proceed
		 * without the heap info.
117
118
119
120
		 *
		 * In MEASURED_BOOT case flushing is done in
		 * arm_bl1_set_bl2_hash() function which is called after heap
		 * information is written in the DTB.
121
		 */
122
		flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize(dtb));
123
#endif /* !MEASURED_BOOT */
124
125
126
	}
}

127
128
#if MEASURED_BOOT
/*
129
 * Calculates and writes BL2 hash data to TB_FW_CONFIG DTB.
130
131
 * Executed only from BL1.
 */
132
void arm_bl1_set_bl2_hash(const image_desc_t *image_desc)
133
134
{
	unsigned char hash_data[MBEDTLS_MD_MAX_SIZE];
135
	const image_info_t image_info = image_desc->image_info;
136
137
	uintptr_t tb_fw_cfg_dtb;
	int err;
138
	const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
139

140
	tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
141
142
	assert(tb_fw_config_info != NULL);

143
	tb_fw_cfg_dtb = tb_fw_config_info->config_addr;
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

	/*
	 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
	 * platform. As such, we cannot write to the DTB at all and pass
	 * measured data.
	 */
	if (tb_fw_cfg_dtb == 0UL) {
		panic();
	}

	/* Calculate hash */
	err = crypto_mod_calc_hash(MBEDTLS_MD_ID,
					(void *)image_info.image_base,
					image_info.image_size, hash_data);
	if (err != 0) {
159
160
		ERROR("%scalculate%s\n", "BL1: unable to ",
						" BL2 hash");
161
162
163
164
165
		panic();
	}

	err = arm_set_bl2_hash_info((void *)tb_fw_cfg_dtb, hash_data);
	if (err < 0) {
166
167
		ERROR("%swrite%sdata%s\n", "BL1: unable to ",
					" BL2 hash ", "to DTB\n");
168
169
170
171
172
173
174
175
176
177
		panic();
	}

	/*
	 * Ensure that the info written to the DTB is visible to other
	 * images. It's critical because BL2 won't be able to proceed
	 * without the heap info and its hash data.
	 */
	flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize((void *)tb_fw_cfg_dtb));
}
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

/*
 * Reads TCG_DIGEST_SIZE bytes of BL2 hash data from the DTB.
 * Executed only from BL2.
 */
void arm_bl2_get_hash(void *data)
{
	const void *bl2_hash;

	assert(data != NULL);

	/* Retrieve TCG_DIGEST_SIZE bytes of BL2 hash data from the DTB */
	bl2_hash = FCONF_GET_PROPERTY(tbbr, dyn_config, bl2_hash_data);
	(void)memcpy(data, bl2_hash, TCG_DIGEST_SIZE);
}
193
#endif /* MEASURED_BOOT */
194
195
#endif /* TRUSTED_BOARD_BOOT */

196
197
/*
 * BL2 utility function to initialize dynamic configuration specified by
198
199
 * FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
 * specified in FW_CONFIG.
200
201
202
 */
void arm_bl2_dyn_cfg_init(void)
{
203
204
	unsigned int i;
	bl_mem_params_node_t *cfg_mem_params = NULL;
205
	uintptr_t image_base;
206
	uint32_t image_size;
207
208
209
210
	const unsigned int config_ids[] = {
			HW_CONFIG_ID,
			SOC_FW_CONFIG_ID,
			NT_FW_CONFIG_ID,
211
212
#if defined(SPD_tspd) || defined(SPD_spmd)
			/* tos_fw_config is only present for TSPD/SPMD */
213
214
215
			TOS_FW_CONFIG_ID
#endif
	};
216

217
	const struct dyn_cfg_dtb_info_t *dtb_info;
218

219
220
221
222
223
	/* Iterate through all the fw config IDs */
	for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
		/* Get the config load address and size from TB_FW_CONFIG */
		cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
		if (cfg_mem_params == NULL) {
224
225
			VERBOSE("%sHW_CONFIG in bl_mem_params_node\n",
				"Couldn't find ");
226
227
228
			continue;
		}

229
230
		dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]);
		if (dtb_info == NULL) {
231
232
			VERBOSE("%sconfig_id %d load info in TB_FW_CONFIG\n",
				"Couldn't find ", config_ids[i]);
233
234
235
			continue;
		}

236
237
238
		image_base = dtb_info->config_addr;
		image_size = dtb_info->config_max_size;

239
240
241
242
243
244
245
		/*
		 * Do some runtime checks on the load addresses of soc_fw_config,
		 * tos_fw_config, nt_fw_config. This is not a comprehensive check
		 * of all invalid addresses but to prevent trivial porting errors.
		 */
		if (config_ids[i] != HW_CONFIG_ID) {

246
			if (check_uptr_overflow(image_base, image_size)) {
247
				continue;
248
			}
249
#ifdef	BL31_BASE
250
			/* Ensure the configs don't overlap with BL31 */
251
			if ((image_base >= BL31_BASE) &&
252
			    (image_base <= BL31_LIMIT)) {
253
				continue;
254
			}
255
#endif
256
			/* Ensure the configs are loaded in a valid address */
257
			if (image_base < ARM_BL_RAM_BASE) {
258
				continue;
259
			}
260
261
262
263
264
#ifdef BL32_BASE
			/*
			 * If BL32 is present, ensure that the configs don't
			 * overlap with it.
			 */
265
			if ((image_base >= BL32_BASE) &&
266
			    (image_base <= BL32_LIMIT)) {
267
				continue;
268
			}
269
270
#endif
		}
271

272
273
		cfg_mem_params->image_info.image_base = image_base;
		cfg_mem_params->image_info.image_max_size = (uint32_t)image_size;
274

275
276
277
278
		/*
		 * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from
		 * HW_CONFIG or FW_CONFIG nodes
		 */
279
280
		cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
	}
281
}