Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 1 | /* |
Chris Kay | f3c0fe1 | 2024-02-06 16:03:24 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2018-2024, Arm Limited and Contributors. All rights reserved. |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #include <string.h> |
Louis Mayencourt | 73d42d7 | 2019-12-09 11:29:38 +0000 | [diff] [blame] | 9 | #include <libfdt.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
Govindraj Raja | 9c7dfb0 | 2023-01-11 18:34:58 +0000 | [diff] [blame] | 11 | #if CRYPTO_SUPPORT |
| 12 | #include <mbedtls/version.h> |
| 13 | #endif /* CRYPTO_SUPPORT */ |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | |
| 15 | #include <common/debug.h> |
| 16 | #include <common/desc_image_load.h> |
| 17 | #include <common/tbbr/tbbr_img_def.h> |
Louis Mayencourt | 6d2b573 | 2019-12-17 13:17:25 +0000 | [diff] [blame] | 18 | #include <lib/fconf/fconf.h> |
| 19 | #include <lib/fconf/fconf_dyn_cfg_getter.h> |
Louis Mayencourt | 5b9055f | 2019-10-01 10:45:14 +0100 | [diff] [blame] | 20 | #include <lib/fconf/fconf_tbbr_getter.h> |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 21 | |
Antonio Nino Diaz | bd7b740 | 2019-01-25 14:30:04 +0000 | [diff] [blame] | 22 | #include <plat/arm/common/arm_dyn_cfg_helpers.h> |
| 23 | #include <plat/arm/common/plat_arm.h> |
Govindraj Raja | 9c7dfb0 | 2023-01-11 18:34:58 +0000 | [diff] [blame] | 24 | #include <platform_def.h> |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 25 | |
Manish V Badarkhe | eba13bd | 2022-01-08 23:08:02 +0000 | [diff] [blame] | 26 | #if CRYPTO_SUPPORT |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 27 | |
| 28 | static void *mbedtls_heap_addr; |
| 29 | static 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 Diaz | 05f4957 | 2018-09-25 11:37:23 +0100 | [diff] [blame] | 41 | * mechanism to pass the pointer to BL2. |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 42 | */ |
| 43 | int 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 Prakash | 11b9b49 | 2022-11-22 14:41:00 -0600 | [diff] [blame] | 48 | #if defined(IMAGE_BL1) || RESET_TO_BL2 || defined(IMAGE_BL31) |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 49 | |
Arvind Ram Prakash | 11b9b49 | 2022-11-22 14:41:00 -0600 | [diff] [blame] | 50 | /* If in BL1 or RESET_TO_BL2 define a heap */ |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 51 | 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 Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 60 | /* If in BL2, retrieve the already allocated heap's info from DTB */ |
Louis Mayencourt | 5b9055f | 2019-10-01 10:45:14 +0100 | [diff] [blame] | 61 | *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 Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 64 | #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 | */ |
| 73 | void arm_bl1_set_mbedtls_heap(void) |
| 74 | { |
| 75 | int err; |
Louis Mayencourt | 6d2b573 | 2019-12-17 13:17:25 +0000 | [diff] [blame] | 76 | uintptr_t tb_fw_cfg_dtb; |
Manish V Badarkhe | 8c66f7a | 2020-06-11 22:09:10 +0100 | [diff] [blame] | 77 | const struct dyn_cfg_dtb_info_t *tb_fw_config_info; |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 78 | |
| 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 Mayencourt | 6d2b573 | 2019-12-17 13:17:25 +0000 | [diff] [blame] | 91 | |
Manish V Badarkhe | 8c66f7a | 2020-06-11 22:09:10 +0100 | [diff] [blame] | 92 | tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID); |
Manish V Badarkhe | 9cb29f0 | 2020-06-29 07:17:24 +0100 | [diff] [blame] | 93 | assert(tb_fw_config_info != NULL); |
| 94 | |
Manish V Badarkhe | 8c66f7a | 2020-06-11 22:09:10 +0100 | [diff] [blame] | 95 | tb_fw_cfg_dtb = tb_fw_config_info->config_addr; |
Louis Mayencourt | 6d2b573 | 2019-12-17 13:17:25 +0000 | [diff] [blame] | 96 | |
| 97 | if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) { |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 98 | /* As libfdt uses void *, we can't avoid this cast */ |
Louis Mayencourt | 6d2b573 | 2019-12-17 13:17:25 +0000 | [diff] [blame] | 99 | void *dtb = (void *)tb_fw_cfg_dtb; |
| 100 | |
| 101 | err = arm_set_dtb_mbedtls_heap_info(dtb, |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 102 | mbedtls_heap_addr, mbedtls_heap_size); |
| 103 | if (err < 0) { |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 104 | ERROR("%swrite shared Mbed TLS heap information%s", |
| 105 | "BL1: unable to ", " to DTB\n"); |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 106 | panic(); |
| 107 | } |
Alexei Fedorov | 25d7c88 | 2020-03-20 18:38:55 +0000 | [diff] [blame] | 108 | #if !MEASURED_BOOT |
John Tsichritzis | 03459c2 | 2018-09-07 10:52:12 +0100 | [diff] [blame] | 109 | /* |
| 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 Fedorov | 25d7c88 | 2020-03-20 18:38:55 +0000 | [diff] [blame] | 113 | * |
Manish V Badarkhe | 3ff0f79 | 2021-08-10 20:51:55 +0100 | [diff] [blame] | 114 | * In MEASURED_BOOT case flushing is done in a function which |
| 115 | * is called after heap information is written in the DTB. |
John Tsichritzis | 03459c2 | 2018-09-07 10:52:12 +0100 | [diff] [blame] | 116 | */ |
Louis Mayencourt | 6d2b573 | 2019-12-17 13:17:25 +0000 | [diff] [blame] | 117 | flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize(dtb)); |
Alexei Fedorov | 25d7c88 | 2020-03-20 18:38:55 +0000 | [diff] [blame] | 118 | #endif /* !MEASURED_BOOT */ |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 119 | } |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 120 | } |
Manish V Badarkhe | eba13bd | 2022-01-08 23:08:02 +0000 | [diff] [blame] | 121 | #endif /* CRYPTO_SUPPORT */ |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 122 | |
Chris Kay | f3c0fe1 | 2024-02-06 16:03:24 +0000 | [diff] [blame^] | 123 | #if IMAGE_BL2 |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 124 | /* |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 125 | * BL2 utility function to initialize dynamic configuration specified by |
Manish V Badarkhe | 1da211a | 2020-05-31 10:17:59 +0100 | [diff] [blame] | 126 | * FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if |
| 127 | * specified in FW_CONFIG. |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 128 | */ |
| 129 | void arm_bl2_dyn_cfg_init(void) |
| 130 | { |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 131 | unsigned int i; |
| 132 | bl_mem_params_node_t *cfg_mem_params = NULL; |
Louis Mayencourt | 6d2b573 | 2019-12-17 13:17:25 +0000 | [diff] [blame] | 133 | uintptr_t image_base; |
Manish V Badarkhe | a8be3bb | 2020-07-15 04:27:57 +0100 | [diff] [blame] | 134 | uint32_t image_size; |
Manish V Badarkhe | 055cc66 | 2022-06-20 17:40:40 +0100 | [diff] [blame] | 135 | unsigned int error_config_id = MAX_IMAGE_IDS; |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 136 | const unsigned int config_ids[] = { |
| 137 | HW_CONFIG_ID, |
| 138 | SOC_FW_CONFIG_ID, |
| 139 | NT_FW_CONFIG_ID, |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 140 | TOS_FW_CONFIG_ID |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 141 | }; |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 142 | |
Louis Mayencourt | 6d2b573 | 2019-12-17 13:17:25 +0000 | [diff] [blame] | 143 | const struct dyn_cfg_dtb_info_t *dtb_info; |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 144 | |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 145 | /* Iterate through all the fw config IDs */ |
| 146 | for (i = 0; i < ARRAY_SIZE(config_ids); i++) { |
Manish V Badarkhe | 055cc66 | 2022-06-20 17:40:40 +0100 | [diff] [blame] | 147 | /* Get the config load address and size */ |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 148 | cfg_mem_params = get_bl_mem_params_node(config_ids[i]); |
| 149 | if (cfg_mem_params == NULL) { |
Manish V Badarkhe | 055cc66 | 2022-06-20 17:40:40 +0100 | [diff] [blame] | 150 | VERBOSE("%sconfig_id = %d in bl_mem_params_node\n", |
| 151 | "Couldn't find ", config_ids[i]); |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 152 | continue; |
| 153 | } |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 154 | |
Louis Mayencourt | 6d2b573 | 2019-12-17 13:17:25 +0000 | [diff] [blame] | 155 | dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]); |
| 156 | if (dtb_info == NULL) { |
Manish V Badarkhe | 055cc66 | 2022-06-20 17:40:40 +0100 | [diff] [blame] | 157 | VERBOSE("%sconfig_id %d load info in FW_CONFIG\n", |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 158 | "Couldn't find ", config_ids[i]); |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 159 | continue; |
| 160 | } |
| 161 | |
Louis Mayencourt | 6d2b573 | 2019-12-17 13:17:25 +0000 | [diff] [blame] | 162 | image_base = dtb_info->config_addr; |
| 163 | image_size = dtb_info->config_max_size; |
| 164 | |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 165 | /* |
| 166 | * Do some runtime checks on the load addresses of soc_fw_config, |
| 167 | * tos_fw_config, nt_fw_config. This is not a comprehensive check |
| 168 | * of all invalid addresses but to prevent trivial porting errors. |
| 169 | */ |
| 170 | if (config_ids[i] != HW_CONFIG_ID) { |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 171 | |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 172 | if (check_uptr_overflow(image_base, image_size)) { |
Manish V Badarkhe | 055cc66 | 2022-06-20 17:40:40 +0100 | [diff] [blame] | 173 | VERBOSE("%s=%d as its %s is overflowing uptr\n", |
| 174 | "skip loading of firmware config", |
| 175 | config_ids[i], |
| 176 | "load-address"); |
| 177 | error_config_id = config_ids[i]; |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 178 | continue; |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 179 | } |
Usama Arif | e97998f | 2018-11-30 15:43:56 +0000 | [diff] [blame] | 180 | #ifdef BL31_BASE |
Soby Mathew | af14b46 | 2018-06-01 16:53:38 +0100 | [diff] [blame] | 181 | /* Ensure the configs don't overlap with BL31 */ |
Alexei Fedorov | 91e20c8 | 2019-12-19 11:59:31 +0000 | [diff] [blame] | 182 | if ((image_base >= BL31_BASE) && |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 183 | (image_base <= BL31_LIMIT)) { |
Manish V Badarkhe | 055cc66 | 2022-06-20 17:40:40 +0100 | [diff] [blame] | 184 | VERBOSE("%s=%d as its %s is overlapping BL31\n", |
| 185 | "skip loading of firmware config", |
| 186 | config_ids[i], |
| 187 | "load-address"); |
| 188 | error_config_id = config_ids[i]; |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 189 | continue; |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 190 | } |
Usama Arif | e97998f | 2018-11-30 15:43:56 +0000 | [diff] [blame] | 191 | #endif |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 192 | /* Ensure the configs are loaded in a valid address */ |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 193 | if (image_base < ARM_BL_RAM_BASE) { |
Manish V Badarkhe | 055cc66 | 2022-06-20 17:40:40 +0100 | [diff] [blame] | 194 | VERBOSE("%s=%d as its %s is invalid\n", |
| 195 | "skip loading of firmware config", |
| 196 | config_ids[i], |
| 197 | "load-address"); |
| 198 | error_config_id = config_ids[i]; |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 199 | continue; |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 200 | } |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 201 | #ifdef BL32_BASE |
| 202 | /* |
| 203 | * If BL32 is present, ensure that the configs don't |
| 204 | * overlap with it. |
| 205 | */ |
Alexei Fedorov | 91e20c8 | 2019-12-19 11:59:31 +0000 | [diff] [blame] | 206 | if ((image_base >= BL32_BASE) && |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 207 | (image_base <= BL32_LIMIT)) { |
Manish V Badarkhe | 055cc66 | 2022-06-20 17:40:40 +0100 | [diff] [blame] | 208 | VERBOSE("%s=%d as its %s is overlapping BL32\n", |
| 209 | "skip loading of firmware config", |
| 210 | config_ids[i], |
| 211 | "load-address"); |
| 212 | error_config_id = config_ids[i]; |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 213 | continue; |
Alexei Fedorov | c717617 | 2020-07-13 12:11:05 +0100 | [diff] [blame] | 214 | } |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 215 | #endif |
| 216 | } |
| 217 | |
Louis Mayencourt | 6d2b573 | 2019-12-17 13:17:25 +0000 | [diff] [blame] | 218 | cfg_mem_params->image_info.image_base = image_base; |
| 219 | cfg_mem_params->image_info.image_max_size = (uint32_t)image_size; |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 220 | |
Alexei Fedorov | 91e20c8 | 2019-12-19 11:59:31 +0000 | [diff] [blame] | 221 | /* |
| 222 | * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from |
| 223 | * HW_CONFIG or FW_CONFIG nodes |
| 224 | */ |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 225 | cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING; |
| 226 | } |
Manish V Badarkhe | 055cc66 | 2022-06-20 17:40:40 +0100 | [diff] [blame] | 227 | |
| 228 | if (error_config_id != MAX_IMAGE_IDS) { |
| 229 | ERROR("Invalid config file %u\n", error_config_id); |
| 230 | panic(); |
| 231 | } |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 232 | } |
Chris Kay | f3c0fe1 | 2024-02-06 16:03:24 +0000 | [diff] [blame^] | 233 | #endif /* IMAGE_BL2 */ |