blob: 99e28098f84ab1900f6c3a94bf80342b11a77a23 [file] [log] [blame]
Soby Mathew7c6df5b2018-01-15 14:43:42 +00001/*
Govindraj Raja9c7dfb02023-01-11 18:34:58 +00002 * Copyright (c) 2018-2023, 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
Govindraj Raja9c7dfb02023-01-11 18:34:58 +000011#if CRYPTO_SUPPORT
12#include <mbedtls/version.h>
13#endif /* CRYPTO_SUPPORT */
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014
15#include <common/debug.h>
16#include <common/desc_image_load.h>
17#include <common/tbbr/tbbr_img_def.h>
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000018#include <lib/fconf/fconf.h>
19#include <lib/fconf/fconf_dyn_cfg_getter.h>
Louis Mayencourt5b9055f2019-10-01 10:45:14 +010020#include <lib/fconf/fconf_tbbr_getter.h>
Alexei Fedorovc7176172020-07-13 12:11:05 +010021
Antonio Nino Diazbd7b7402019-01-25 14:30:04 +000022#include <plat/arm/common/arm_dyn_cfg_helpers.h>
23#include <plat/arm/common/plat_arm.h>
Govindraj Raja9c7dfb02023-01-11 18:34:58 +000024#include <platform_def.h>
Soby Mathew7c6df5b2018-01-15 14:43:42 +000025
Manish V Badarkheeba13bd2022-01-08 23:08:02 +000026#if CRYPTO_SUPPORT
John Tsichritzisc34341a2018-07-30 13:41:52 +010027
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
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -060048#if defined(IMAGE_BL1) || RESET_TO_BL2 || defined(IMAGE_BL31)
John Tsichritzisc34341a2018-07-30 13:41:52 +010049
Arvind Ram Prakash11b9b492022-11-22 14:41:00 -060050 /* If in BL1 or RESET_TO_BL2 define a heap */
John Tsichritzisc34341a2018-07-30 13:41:52 +010051 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
John Tsichritzisc34341a2018-07-30 13:41:52 +010060 /* If in BL2, retrieve the already allocated heap's info from DTB */
Louis Mayencourt5b9055f2019-10-01 10:45:14 +010061 *heap_addr = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_addr);
62 *heap_size = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_size);
63
John Tsichritzisc34341a2018-07-30 13:41:52 +010064#endif
65
66 return 0;
67}
68
69/*
70 * Puts the shared Mbed TLS heap information to the DTB.
71 * Executed only from BL1.
72 */
73void arm_bl1_set_mbedtls_heap(void)
74{
75 int err;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000076 uintptr_t tb_fw_cfg_dtb;
Manish V Badarkhe8c66f7a2020-06-11 22:09:10 +010077 const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
John Tsichritzisc34341a2018-07-30 13:41:52 +010078
79 /*
80 * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
81 * platform. As such, we don't attempt to write to the DTB at all.
82 *
83 * If mbedtls_heap_addr==NULL, then it means we are using the default
84 * heap implementation. As such, BL2 will have its own heap for sure
85 * and hence there is no need to pass any information to the DTB.
86 *
87 * In the latter case, if we still wanted to write in the DTB the heap
88 * information, we would need to call plat_get_mbedtls_heap to retrieve
89 * the default heap's address and size.
90 */
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000091
Manish V Badarkhe8c66f7a2020-06-11 22:09:10 +010092 tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
Manish V Badarkhe9cb29f02020-06-29 07:17:24 +010093 assert(tb_fw_config_info != NULL);
94
Manish V Badarkhe8c66f7a2020-06-11 22:09:10 +010095 tb_fw_cfg_dtb = tb_fw_config_info->config_addr;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000096
97 if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) {
Alexei Fedorovc7176172020-07-13 12:11:05 +010098 /* As libfdt uses void *, we can't avoid this cast */
Louis Mayencourt6d2b5732019-12-17 13:17:25 +000099 void *dtb = (void *)tb_fw_cfg_dtb;
100
101 err = arm_set_dtb_mbedtls_heap_info(dtb,
John Tsichritzisc34341a2018-07-30 13:41:52 +0100102 mbedtls_heap_addr, mbedtls_heap_size);
103 if (err < 0) {
Alexei Fedorovc7176172020-07-13 12:11:05 +0100104 ERROR("%swrite shared Mbed TLS heap information%s",
105 "BL1: unable to ", " 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 *
Manish V Badarkhe3ff0f792021-08-10 20:51:55 +0100114 * In MEASURED_BOOT case flushing is done in a function which
115 * is called after heap information is written in the DTB.
John Tsichritzis03459c22018-09-07 10:52:12 +0100116 */
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000117 flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize(dtb));
Alexei Fedorov25d7c882020-03-20 18:38:55 +0000118#endif /* !MEASURED_BOOT */
John Tsichritzisc34341a2018-07-30 13:41:52 +0100119 }
Alexei Fedorovc7176172020-07-13 12:11:05 +0100120}
Manish V Badarkheeba13bd2022-01-08 23:08:02 +0000121#endif /* CRYPTO_SUPPORT */
John Tsichritzisc34341a2018-07-30 13:41:52 +0100122
Soby Mathew7c6df5b2018-01-15 14:43:42 +0000123/*
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000124 * BL2 utility function to initialize dynamic configuration specified by
Manish V Badarkhe1da211a2020-05-31 10:17:59 +0100125 * FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
126 * specified in FW_CONFIG.
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000127 */
128void arm_bl2_dyn_cfg_init(void)
129{
Soby Mathewb6814842018-04-04 09:40:32 +0100130 unsigned int i;
131 bl_mem_params_node_t *cfg_mem_params = NULL;
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000132 uintptr_t image_base;
Manish V Badarkhea8be3bb2020-07-15 04:27:57 +0100133 uint32_t image_size;
Manish V Badarkhe055cc662022-06-20 17:40:40 +0100134 unsigned int error_config_id = MAX_IMAGE_IDS;
Soby Mathewb6814842018-04-04 09:40:32 +0100135 const unsigned int config_ids[] = {
136 HW_CONFIG_ID,
137 SOC_FW_CONFIG_ID,
138 NT_FW_CONFIG_ID,
Soby Mathewb6814842018-04-04 09:40:32 +0100139 TOS_FW_CONFIG_ID
Soby Mathewb6814842018-04-04 09:40:32 +0100140 };
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000141
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000142 const struct dyn_cfg_dtb_info_t *dtb_info;
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000143
Soby Mathewb6814842018-04-04 09:40:32 +0100144 /* Iterate through all the fw config IDs */
145 for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
Manish V Badarkhe055cc662022-06-20 17:40:40 +0100146 /* Get the config load address and size */
Soby Mathewb6814842018-04-04 09:40:32 +0100147 cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
148 if (cfg_mem_params == NULL) {
Manish V Badarkhe055cc662022-06-20 17:40:40 +0100149 VERBOSE("%sconfig_id = %d in bl_mem_params_node\n",
150 "Couldn't find ", config_ids[i]);
Soby Mathewb6814842018-04-04 09:40:32 +0100151 continue;
152 }
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000153
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000154 dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]);
155 if (dtb_info == NULL) {
Manish V Badarkhe055cc662022-06-20 17:40:40 +0100156 VERBOSE("%sconfig_id %d load info in FW_CONFIG\n",
Alexei Fedorovc7176172020-07-13 12:11:05 +0100157 "Couldn't find ", config_ids[i]);
Soby Mathewb6814842018-04-04 09:40:32 +0100158 continue;
159 }
160
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000161 image_base = dtb_info->config_addr;
162 image_size = dtb_info->config_max_size;
163
Soby Mathewb6814842018-04-04 09:40:32 +0100164 /*
165 * Do some runtime checks on the load addresses of soc_fw_config,
166 * tos_fw_config, nt_fw_config. This is not a comprehensive check
167 * of all invalid addresses but to prevent trivial porting errors.
168 */
169 if (config_ids[i] != HW_CONFIG_ID) {
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000170
Alexei Fedorovc7176172020-07-13 12:11:05 +0100171 if (check_uptr_overflow(image_base, image_size)) {
Manish V Badarkhe055cc662022-06-20 17:40:40 +0100172 VERBOSE("%s=%d as its %s is overflowing uptr\n",
173 "skip loading of firmware config",
174 config_ids[i],
175 "load-address");
176 error_config_id = config_ids[i];
Soby Mathewb6814842018-04-04 09:40:32 +0100177 continue;
Alexei Fedorovc7176172020-07-13 12:11:05 +0100178 }
Usama Arife97998f2018-11-30 15:43:56 +0000179#ifdef BL31_BASE
Soby Mathewaf14b462018-06-01 16:53:38 +0100180 /* Ensure the configs don't overlap with BL31 */
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000181 if ((image_base >= BL31_BASE) &&
Alexei Fedorovc7176172020-07-13 12:11:05 +0100182 (image_base <= BL31_LIMIT)) {
Manish V Badarkhe055cc662022-06-20 17:40:40 +0100183 VERBOSE("%s=%d as its %s is overlapping BL31\n",
184 "skip loading of firmware config",
185 config_ids[i],
186 "load-address");
187 error_config_id = config_ids[i];
Soby Mathewb6814842018-04-04 09:40:32 +0100188 continue;
Alexei Fedorovc7176172020-07-13 12:11:05 +0100189 }
Usama Arife97998f2018-11-30 15:43:56 +0000190#endif
Soby Mathewb6814842018-04-04 09:40:32 +0100191 /* Ensure the configs are loaded in a valid address */
Alexei Fedorovc7176172020-07-13 12:11:05 +0100192 if (image_base < ARM_BL_RAM_BASE) {
Manish V Badarkhe055cc662022-06-20 17:40:40 +0100193 VERBOSE("%s=%d as its %s is invalid\n",
194 "skip loading of firmware config",
195 config_ids[i],
196 "load-address");
197 error_config_id = config_ids[i];
Soby Mathewb6814842018-04-04 09:40:32 +0100198 continue;
Alexei Fedorovc7176172020-07-13 12:11:05 +0100199 }
Soby Mathewb6814842018-04-04 09:40:32 +0100200#ifdef BL32_BASE
201 /*
202 * If BL32 is present, ensure that the configs don't
203 * overlap with it.
204 */
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000205 if ((image_base >= BL32_BASE) &&
Alexei Fedorovc7176172020-07-13 12:11:05 +0100206 (image_base <= BL32_LIMIT)) {
Manish V Badarkhe055cc662022-06-20 17:40:40 +0100207 VERBOSE("%s=%d as its %s is overlapping BL32\n",
208 "skip loading of firmware config",
209 config_ids[i],
210 "load-address");
211 error_config_id = config_ids[i];
Soby Mathewb6814842018-04-04 09:40:32 +0100212 continue;
Alexei Fedorovc7176172020-07-13 12:11:05 +0100213 }
Soby Mathewb6814842018-04-04 09:40:32 +0100214#endif
215 }
216
Louis Mayencourt6d2b5732019-12-17 13:17:25 +0000217 cfg_mem_params->image_info.image_base = image_base;
218 cfg_mem_params->image_info.image_max_size = (uint32_t)image_size;
Soby Mathewb6814842018-04-04 09:40:32 +0100219
Alexei Fedorov91e20c82019-12-19 11:59:31 +0000220 /*
221 * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from
222 * HW_CONFIG or FW_CONFIG nodes
223 */
Soby Mathewb6814842018-04-04 09:40:32 +0100224 cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
225 }
Manish V Badarkhe055cc662022-06-20 17:40:40 +0100226
227 if (error_config_id != MAX_IMAGE_IDS) {
228 ERROR("Invalid config file %u\n", error_config_id);
229 panic();
230 }
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000231}