Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. |
| 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> |
| 9 | |
| 10 | #include <platform_def.h> |
| 11 | |
| 12 | #include <common/debug.h> |
| 13 | #include <common/desc_image_load.h> |
| 14 | #include <common/tbbr/tbbr_img_def.h> |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 15 | #if TRUSTED_BOARD_BOOT |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 16 | #include <drivers/auth/mbedtls/mbedtls_config.h> |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 17 | #endif |
Antonio Nino Diaz | bd7b740 | 2019-01-25 14:30:04 +0000 | [diff] [blame] | 18 | #include <plat/arm/common/arm_dyn_cfg_helpers.h> |
| 19 | #include <plat/arm/common/plat_arm.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 20 | #include <plat/common/platform.h> |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 21 | |
Antonio Nino Diaz | 05f4957 | 2018-09-25 11:37:23 +0100 | [diff] [blame] | 22 | /* Variable to store the address to TB_FW_CONFIG passed from BL1 */ |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 23 | static void *tb_fw_cfg_dtb; |
John Tsichritzis | 03459c2 | 2018-09-07 10:52:12 +0100 | [diff] [blame] | 24 | static size_t tb_fw_cfg_dtb_size; |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 25 | |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 26 | |
| 27 | #if TRUSTED_BOARD_BOOT |
| 28 | |
| 29 | static void *mbedtls_heap_addr; |
| 30 | static size_t mbedtls_heap_size; |
| 31 | |
| 32 | /* |
| 33 | * This function is the implementation of the shared Mbed TLS heap between |
| 34 | * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1 |
| 35 | * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file |
| 36 | * which is a DTB. |
| 37 | * |
| 38 | * This function is placed inside an #if directive for the below reasons: |
| 39 | * - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot |
| 40 | * is enabled. |
| 41 | * - 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] | 42 | * mechanism to pass the pointer to BL2. |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 43 | */ |
| 44 | int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size) |
| 45 | { |
| 46 | assert(heap_addr != NULL); |
| 47 | assert(heap_size != NULL); |
| 48 | |
| 49 | #if defined(IMAGE_BL1) || BL2_AT_EL3 |
| 50 | |
| 51 | /* If in BL1 or BL2_AT_EL3 define a heap */ |
| 52 | static unsigned char heap[TF_MBEDTLS_HEAP_SIZE]; |
| 53 | |
| 54 | *heap_addr = heap; |
| 55 | *heap_size = sizeof(heap); |
| 56 | mbedtls_heap_addr = heap; |
| 57 | mbedtls_heap_size = sizeof(heap); |
| 58 | |
| 59 | #elif defined(IMAGE_BL2) |
| 60 | |
| 61 | int err; |
| 62 | |
| 63 | /* If in BL2, retrieve the already allocated heap's info from DTB */ |
John Tsichritzis | 3650768 | 2018-09-07 10:42:37 +0100 | [diff] [blame] | 64 | if (tb_fw_cfg_dtb != NULL) { |
| 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 information from DTB\n"); |
| 69 | panic(); |
| 70 | } |
| 71 | } else { |
| 72 | ERROR("BL2: DTB missing, cannot get Mbed TLS heap\n"); |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 73 | panic(); |
| 74 | } |
| 75 | #endif |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Puts the shared Mbed TLS heap information to the DTB. |
| 82 | * Executed only from BL1. |
| 83 | */ |
| 84 | void arm_bl1_set_mbedtls_heap(void) |
| 85 | { |
| 86 | int err; |
| 87 | |
| 88 | /* |
| 89 | * If tb_fw_cfg_dtb==NULL then DTB is not present for the current |
| 90 | * platform. As such, we don't attempt to write to the DTB at all. |
| 91 | * |
| 92 | * If mbedtls_heap_addr==NULL, then it means we are using the default |
| 93 | * heap implementation. As such, BL2 will have its own heap for sure |
| 94 | * and hence there is no need to pass any information to the DTB. |
| 95 | * |
| 96 | * In the latter case, if we still wanted to write in the DTB the heap |
| 97 | * information, we would need to call plat_get_mbedtls_heap to retrieve |
| 98 | * the default heap's address and size. |
| 99 | */ |
| 100 | if ((tb_fw_cfg_dtb != NULL) && (mbedtls_heap_addr != NULL)) { |
| 101 | err = arm_set_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, |
| 102 | mbedtls_heap_addr, mbedtls_heap_size); |
| 103 | if (err < 0) { |
John Tsichritzis | 3650768 | 2018-09-07 10:42:37 +0100 | [diff] [blame] | 104 | ERROR("BL1: unable to write shared Mbed TLS heap information to DTB\n"); |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 105 | panic(); |
| 106 | } |
John Tsichritzis | 03459c2 | 2018-09-07 10:52:12 +0100 | [diff] [blame] | 107 | /* |
| 108 | * Ensure that the info written to the DTB is visible to other |
| 109 | * images. It's critical because BL2 won't be able to proceed |
| 110 | * without the heap info. |
| 111 | */ |
| 112 | flush_dcache_range((uintptr_t)tb_fw_cfg_dtb, |
| 113 | tb_fw_cfg_dtb_size); |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | #endif /* TRUSTED_BOARD_BOOT */ |
| 118 | |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 119 | /* |
| 120 | * Helper function to load TB_FW_CONFIG and populate the load information to |
| 121 | * arg0 of BL2 entrypoint info. |
| 122 | */ |
| 123 | void arm_load_tb_fw_config(void) |
| 124 | { |
| 125 | int err; |
Antonio Nino Diaz | b5acb3f | 2018-10-30 16:32:48 +0000 | [diff] [blame] | 126 | uintptr_t config_base = 0UL; |
| 127 | image_desc_t *desc; |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 128 | |
| 129 | image_desc_t arm_tb_fw_info = { |
| 130 | .image_id = TB_FW_CONFIG_ID, |
| 131 | SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY, |
| 132 | VERSION_2, image_info_t, 0), |
| 133 | .image_info.image_base = ARM_TB_FW_CONFIG_BASE, |
John Tsichritzis | 03459c2 | 2018-09-07 10:52:12 +0100 | [diff] [blame] | 134 | .image_info.image_max_size = |
| 135 | ARM_TB_FW_CONFIG_LIMIT - ARM_TB_FW_CONFIG_BASE |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | VERBOSE("BL1: Loading TB_FW_CONFIG\n"); |
| 139 | err = load_auth_image(TB_FW_CONFIG_ID, &arm_tb_fw_info.image_info); |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 140 | if (err != 0) { |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 141 | /* Return if TB_FW_CONFIG is not loaded */ |
| 142 | VERBOSE("Failed to load TB_FW_CONFIG\n"); |
| 143 | return; |
| 144 | } |
| 145 | |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 146 | /* At this point we know that a DTB is indeed available */ |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 147 | config_base = arm_tb_fw_info.image_info.image_base; |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 148 | tb_fw_cfg_dtb = (void *)config_base; |
John Tsichritzis | 03459c2 | 2018-09-07 10:52:12 +0100 | [diff] [blame] | 149 | tb_fw_cfg_dtb_size = (size_t)arm_tb_fw_info.image_info.image_max_size; |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 150 | |
| 151 | /* The BL2 ep_info arg0 is modified to point to TB_FW_CONFIG */ |
Antonio Nino Diaz | b5acb3f | 2018-10-30 16:32:48 +0000 | [diff] [blame] | 152 | desc = bl1_plat_get_image_desc(BL2_IMAGE_ID); |
| 153 | assert(desc != NULL); |
| 154 | desc->ep_info.args.arg0 = config_base; |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 155 | |
Antonio Nino Diaz | b5acb3f | 2018-10-30 16:32:48 +0000 | [diff] [blame] | 156 | INFO("BL1: TB_FW_CONFIG loaded at address = 0x%lx\n", config_base); |
Soby Mathew | 45e39e2 | 2018-03-26 15:16:46 +0100 | [diff] [blame] | 157 | |
| 158 | #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH) |
| 159 | int tb_fw_node; |
| 160 | uint32_t disable_auth = 0; |
| 161 | |
| 162 | err = arm_dyn_tb_fw_cfg_init((void *)config_base, &tb_fw_node); |
| 163 | if (err < 0) { |
John Tsichritzis | 116d78a | 2018-06-15 11:43:02 +0100 | [diff] [blame] | 164 | ERROR("Invalid TB_FW_CONFIG loaded\n"); |
| 165 | panic(); |
Soby Mathew | 45e39e2 | 2018-03-26 15:16:46 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | err = arm_dyn_get_disable_auth((void *)config_base, tb_fw_node, &disable_auth); |
| 169 | if (err < 0) |
| 170 | return; |
| 171 | |
| 172 | if (disable_auth == 1) |
| 173 | dyn_disable_auth(); |
| 174 | #endif |
Soby Mathew | 7c6df5b | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 177 | /* |
| 178 | * BL2 utility function to set the address of TB_FW_CONFIG passed from BL1. |
| 179 | */ |
| 180 | void arm_bl2_set_tb_cfg_addr(void *dtb) |
| 181 | { |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 182 | assert(dtb != NULL); |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 183 | tb_fw_cfg_dtb = dtb; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * BL2 utility function to initialize dynamic configuration specified by |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 188 | * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if |
| 189 | * specified in TB_FW_CONFIG. |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 190 | */ |
| 191 | void arm_bl2_dyn_cfg_init(void) |
| 192 | { |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 193 | int err = 0, tb_fw_node; |
| 194 | unsigned int i; |
| 195 | bl_mem_params_node_t *cfg_mem_params = NULL; |
| 196 | uint64_t image_base; |
| 197 | uint32_t image_size; |
| 198 | const unsigned int config_ids[] = { |
| 199 | HW_CONFIG_ID, |
| 200 | SOC_FW_CONFIG_ID, |
| 201 | NT_FW_CONFIG_ID, |
| 202 | #ifdef SPD_tspd |
| 203 | /* Currently tos_fw_config is only present for TSP */ |
| 204 | TOS_FW_CONFIG_ID |
| 205 | #endif |
| 206 | }; |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 207 | |
| 208 | if (tb_fw_cfg_dtb == NULL) { |
| 209 | VERBOSE("No TB_FW_CONFIG specified\n"); |
| 210 | return; |
| 211 | } |
| 212 | |
John Tsichritzis | 5b8183c | 2018-08-23 09:57:54 +0100 | [diff] [blame] | 213 | err = arm_dyn_tb_fw_cfg_init(tb_fw_cfg_dtb, &tb_fw_node); |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 214 | if (err < 0) { |
| 215 | ERROR("Invalid TB_FW_CONFIG passed from BL1\n"); |
| 216 | panic(); |
| 217 | } |
| 218 | |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 219 | /* Iterate through all the fw config IDs */ |
| 220 | for (i = 0; i < ARRAY_SIZE(config_ids); i++) { |
| 221 | /* Get the config load address and size from TB_FW_CONFIG */ |
| 222 | cfg_mem_params = get_bl_mem_params_node(config_ids[i]); |
| 223 | if (cfg_mem_params == NULL) { |
| 224 | VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n"); |
| 225 | continue; |
| 226 | } |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 227 | |
John Tsichritzis | 5b8183c | 2018-08-23 09:57:54 +0100 | [diff] [blame] | 228 | err = arm_dyn_get_config_load_info(tb_fw_cfg_dtb, tb_fw_node, |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 229 | config_ids[i], &image_base, &image_size); |
| 230 | if (err < 0) { |
| 231 | VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n", |
| 232 | config_ids[i]); |
| 233 | continue; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Do some runtime checks on the load addresses of soc_fw_config, |
| 238 | * tos_fw_config, nt_fw_config. This is not a comprehensive check |
| 239 | * of all invalid addresses but to prevent trivial porting errors. |
| 240 | */ |
| 241 | if (config_ids[i] != HW_CONFIG_ID) { |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 242 | |
Antonio Nino Diaz | b5acb3f | 2018-10-30 16:32:48 +0000 | [diff] [blame] | 243 | if (check_uptr_overflow(image_base, image_size)) |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 244 | continue; |
| 245 | |
Usama Arif | e97998f | 2018-11-30 15:43:56 +0000 | [diff] [blame] | 246 | #ifdef BL31_BASE |
Soby Mathew | af14b46 | 2018-06-01 16:53:38 +0100 | [diff] [blame] | 247 | /* Ensure the configs don't overlap with BL31 */ |
| 248 | if ((image_base > BL31_BASE) || ((image_base + image_size) > BL31_BASE)) |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 249 | continue; |
Usama Arif | e97998f | 2018-11-30 15:43:56 +0000 | [diff] [blame] | 250 | #endif |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 251 | /* Ensure the configs are loaded in a valid address */ |
| 252 | if (image_base < ARM_BL_RAM_BASE) |
| 253 | continue; |
| 254 | #ifdef BL32_BASE |
| 255 | /* |
| 256 | * If BL32 is present, ensure that the configs don't |
| 257 | * overlap with it. |
| 258 | */ |
| 259 | if (image_base >= BL32_BASE && image_base <= BL32_LIMIT) |
| 260 | continue; |
| 261 | #endif |
| 262 | } |
| 263 | |
| 264 | |
| 265 | cfg_mem_params->image_info.image_base = (uintptr_t)image_base; |
| 266 | cfg_mem_params->image_info.image_max_size = image_size; |
| 267 | |
| 268 | /* Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from HW_CONFIG node */ |
| 269 | cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING; |
| 270 | } |
Soby Mathew | 45e39e2 | 2018-03-26 15:16:46 +0100 | [diff] [blame] | 271 | |
| 272 | #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH) |
| 273 | uint32_t disable_auth = 0; |
| 274 | |
John Tsichritzis | 5b8183c | 2018-08-23 09:57:54 +0100 | [diff] [blame] | 275 | err = arm_dyn_get_disable_auth(tb_fw_cfg_dtb, tb_fw_node, |
Soby Mathew | 45e39e2 | 2018-03-26 15:16:46 +0100 | [diff] [blame] | 276 | &disable_auth); |
| 277 | if (err < 0) |
| 278 | return; |
| 279 | |
| 280 | if (disable_auth == 1) |
| 281 | dyn_disable_auth(); |
| 282 | #endif |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 283 | } |