Soby Mathew | c228956 | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 1 | /* |
Louis Mayencourt | 3b5ea74 | 2019-10-17 14:46:51 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved. |
Soby Mathew | c228956 | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Soby Mathew | c228956 | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 8 | #include <string.h> |
Louis Mayencourt | 6c77dfc | 2019-12-09 11:29:38 +0000 | [diff] [blame] | 9 | #include <libfdt.h> |
Soby Mathew | c228956 | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 10 | |
Antonio Nino Diaz | 09d40e0 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 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> |
| 16 | #if TRUSTED_BOARD_BOOT |
| 17 | #include <drivers/auth/mbedtls/mbedtls_config.h> |
| 18 | #endif |
Louis Mayencourt | 25ac879 | 2019-12-17 13:17:25 +0000 | [diff] [blame^] | 19 | #include <lib/fconf/fconf.h> |
| 20 | #include <lib/fconf/fconf_dyn_cfg_getter.h> |
Antonio Nino Diaz | bd9344f | 2019-01-25 14:30:04 +0000 | [diff] [blame] | 21 | #include <plat/arm/common/arm_dyn_cfg_helpers.h> |
| 22 | #include <plat/arm/common/plat_arm.h> |
Antonio Nino Diaz | 09d40e0 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 23 | #include <plat/common/platform.h> |
| 24 | |
John Tsichritzis | ba597da | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 25 | #if TRUSTED_BOARD_BOOT |
| 26 | |
| 27 | static void *mbedtls_heap_addr; |
| 28 | static size_t mbedtls_heap_size; |
| 29 | |
| 30 | /* |
| 31 | * This function is the implementation of the shared Mbed TLS heap between |
| 32 | * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1 |
| 33 | * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file |
| 34 | * which is a DTB. |
| 35 | * |
| 36 | * This function is placed inside an #if directive for the below reasons: |
| 37 | * - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot |
| 38 | * is enabled. |
| 39 | * - This implementation requires the DTB to be present so that BL1 has a |
Antonio Nino Diaz | 60e19f5 | 2018-09-25 11:37:23 +0100 | [diff] [blame] | 40 | * mechanism to pass the pointer to BL2. |
John Tsichritzis | ba597da | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 41 | */ |
| 42 | int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size) |
| 43 | { |
| 44 | assert(heap_addr != NULL); |
| 45 | assert(heap_size != NULL); |
| 46 | |
| 47 | #if defined(IMAGE_BL1) || BL2_AT_EL3 |
| 48 | |
| 49 | /* If in BL1 or BL2_AT_EL3 define a heap */ |
| 50 | static unsigned char heap[TF_MBEDTLS_HEAP_SIZE]; |
| 51 | |
| 52 | *heap_addr = heap; |
| 53 | *heap_size = sizeof(heap); |
| 54 | mbedtls_heap_addr = heap; |
| 55 | mbedtls_heap_size = sizeof(heap); |
| 56 | |
| 57 | #elif defined(IMAGE_BL2) |
| 58 | |
| 59 | int err; |
Louis Mayencourt | 25ac879 | 2019-12-17 13:17:25 +0000 | [diff] [blame^] | 60 | void *tb_fw_cfg_dtb; |
| 61 | |
| 62 | /* fconf FW_CONFIG and TB_FW_CONFIG are currently the same DTB*/ |
| 63 | tb_fw_cfg_dtb = (void *)FCONF_GET_PROPERTY(fconf, dtb, base_addr); |
John Tsichritzis | ba597da | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 64 | |
| 65 | /* If in BL2, retrieve the already allocated heap's info from DTB */ |
John Tsichritzis | a606031 | 2018-09-07 10:42:37 +0100 | [diff] [blame] | 66 | if (tb_fw_cfg_dtb != NULL) { |
| 67 | err = arm_get_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, heap_addr, |
| 68 | heap_size); |
| 69 | if (err < 0) { |
| 70 | ERROR("BL2: unable to retrieve shared Mbed TLS heap information from DTB\n"); |
| 71 | panic(); |
| 72 | } |
| 73 | } else { |
| 74 | ERROR("BL2: DTB missing, cannot get Mbed TLS heap\n"); |
John Tsichritzis | ba597da | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 75 | panic(); |
| 76 | } |
| 77 | #endif |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Puts the shared Mbed TLS heap information to the DTB. |
| 84 | * Executed only from BL1. |
| 85 | */ |
| 86 | void arm_bl1_set_mbedtls_heap(void) |
| 87 | { |
| 88 | int err; |
Louis Mayencourt | 25ac879 | 2019-12-17 13:17:25 +0000 | [diff] [blame^] | 89 | uintptr_t tb_fw_cfg_dtb; |
John Tsichritzis | ba597da | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * If tb_fw_cfg_dtb==NULL then DTB is not present for the current |
| 93 | * platform. As such, we don't attempt to write to the DTB at all. |
| 94 | * |
| 95 | * If mbedtls_heap_addr==NULL, then it means we are using the default |
| 96 | * heap implementation. As such, BL2 will have its own heap for sure |
| 97 | * and hence there is no need to pass any information to the DTB. |
| 98 | * |
| 99 | * In the latter case, if we still wanted to write in the DTB the heap |
| 100 | * information, we would need to call plat_get_mbedtls_heap to retrieve |
| 101 | * the default heap's address and size. |
| 102 | */ |
Louis Mayencourt | 25ac879 | 2019-12-17 13:17:25 +0000 | [diff] [blame^] | 103 | |
| 104 | /* fconf FW_CONFIG and TB_FW_CONFIG are currently the same DTB*/ |
| 105 | tb_fw_cfg_dtb = FCONF_GET_PROPERTY(fconf, dtb, base_addr); |
| 106 | |
| 107 | if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) { |
| 108 | /* As libfdt use void *, we can't avoid this cast */ |
| 109 | void *dtb = (void *)tb_fw_cfg_dtb; |
| 110 | |
| 111 | err = arm_set_dtb_mbedtls_heap_info(dtb, |
John Tsichritzis | ba597da | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 112 | mbedtls_heap_addr, mbedtls_heap_size); |
| 113 | if (err < 0) { |
John Tsichritzis | a606031 | 2018-09-07 10:42:37 +0100 | [diff] [blame] | 114 | ERROR("BL1: unable to write shared Mbed TLS heap information to DTB\n"); |
John Tsichritzis | ba597da | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 115 | panic(); |
| 116 | } |
John Tsichritzis | 63cc265 | 2018-09-07 10:52:12 +0100 | [diff] [blame] | 117 | /* |
| 118 | * Ensure that the info written to the DTB is visible to other |
| 119 | * images. It's critical because BL2 won't be able to proceed |
| 120 | * without the heap info. |
| 121 | */ |
Louis Mayencourt | 25ac879 | 2019-12-17 13:17:25 +0000 | [diff] [blame^] | 122 | flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize(dtb)); |
John Tsichritzis | ba597da | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | #endif /* TRUSTED_BOARD_BOOT */ |
| 127 | |
Soby Mathew | c228956 | 2018-01-15 14:43:42 +0000 | [diff] [blame] | 128 | /* |
Soby Mathew | cab0b5b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 129 | * BL2 utility function to initialize dynamic configuration specified by |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 130 | * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if |
| 131 | * specified in TB_FW_CONFIG. |
Soby Mathew | cab0b5b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 132 | */ |
| 133 | void arm_bl2_dyn_cfg_init(void) |
| 134 | { |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 135 | unsigned int i; |
| 136 | bl_mem_params_node_t *cfg_mem_params = NULL; |
Louis Mayencourt | 25ac879 | 2019-12-17 13:17:25 +0000 | [diff] [blame^] | 137 | uintptr_t image_base; |
| 138 | size_t image_size; |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 139 | const unsigned int config_ids[] = { |
| 140 | HW_CONFIG_ID, |
| 141 | SOC_FW_CONFIG_ID, |
| 142 | NT_FW_CONFIG_ID, |
| 143 | #ifdef SPD_tspd |
| 144 | /* Currently tos_fw_config is only present for TSP */ |
| 145 | TOS_FW_CONFIG_ID |
| 146 | #endif |
| 147 | }; |
Soby Mathew | cab0b5b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 148 | |
Louis Mayencourt | 25ac879 | 2019-12-17 13:17:25 +0000 | [diff] [blame^] | 149 | const struct dyn_cfg_dtb_info_t *dtb_info; |
Soby Mathew | cab0b5b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 150 | |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 151 | /* Iterate through all the fw config IDs */ |
| 152 | for (i = 0; i < ARRAY_SIZE(config_ids); i++) { |
| 153 | /* Get the config load address and size from TB_FW_CONFIG */ |
| 154 | cfg_mem_params = get_bl_mem_params_node(config_ids[i]); |
| 155 | if (cfg_mem_params == NULL) { |
| 156 | VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n"); |
| 157 | continue; |
| 158 | } |
Soby Mathew | cab0b5b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 159 | |
Louis Mayencourt | 25ac879 | 2019-12-17 13:17:25 +0000 | [diff] [blame^] | 160 | dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]); |
| 161 | if (dtb_info == NULL) { |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 162 | VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n", |
| 163 | config_ids[i]); |
| 164 | continue; |
| 165 | } |
Soby Mathew | cab0b5b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 166 | |
Louis Mayencourt | 25ac879 | 2019-12-17 13:17:25 +0000 | [diff] [blame^] | 167 | image_base = dtb_info->config_addr; |
| 168 | image_size = dtb_info->config_max_size; |
| 169 | |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 170 | /* |
| 171 | * Do some runtime checks on the load addresses of soc_fw_config, |
| 172 | * tos_fw_config, nt_fw_config. This is not a comprehensive check |
| 173 | * of all invalid addresses but to prevent trivial porting errors. |
| 174 | */ |
| 175 | if (config_ids[i] != HW_CONFIG_ID) { |
| 176 | |
Antonio Nino Diaz | b8a02d5 | 2018-10-30 16:32:48 +0000 | [diff] [blame] | 177 | if (check_uptr_overflow(image_base, image_size)) |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 178 | continue; |
| 179 | |
Usama Arif | 6393c78 | 2018-11-30 15:43:56 +0000 | [diff] [blame] | 180 | #ifdef BL31_BASE |
Soby Mathew | c099cd3 | 2018-06-01 16:53:38 +0100 | [diff] [blame] | 181 | /* Ensure the configs don't overlap with BL31 */ |
Alexei Fedorov | 5ddcbdd | 2019-12-19 11:59:31 +0000 | [diff] [blame] | 182 | if ((image_base >= BL31_BASE) && |
| 183 | (image_base <= BL31_LIMIT)) |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 184 | continue; |
Usama Arif | 6393c78 | 2018-11-30 15:43:56 +0000 | [diff] [blame] | 185 | #endif |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 186 | /* Ensure the configs are loaded in a valid address */ |
| 187 | if (image_base < ARM_BL_RAM_BASE) |
| 188 | continue; |
| 189 | #ifdef BL32_BASE |
| 190 | /* |
| 191 | * If BL32 is present, ensure that the configs don't |
| 192 | * overlap with it. |
| 193 | */ |
Alexei Fedorov | 5ddcbdd | 2019-12-19 11:59:31 +0000 | [diff] [blame] | 194 | if ((image_base >= BL32_BASE) && |
| 195 | (image_base <= BL32_LIMIT)) |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 196 | continue; |
| 197 | #endif |
| 198 | } |
| 199 | |
| 200 | |
Louis Mayencourt | 25ac879 | 2019-12-17 13:17:25 +0000 | [diff] [blame^] | 201 | cfg_mem_params->image_info.image_base = image_base; |
| 202 | cfg_mem_params->image_info.image_max_size = (uint32_t)image_size; |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 203 | |
Alexei Fedorov | 5ddcbdd | 2019-12-19 11:59:31 +0000 | [diff] [blame] | 204 | /* |
| 205 | * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from |
| 206 | * HW_CONFIG or FW_CONFIG nodes |
| 207 | */ |
Soby Mathew | 1d71ba1 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 208 | cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING; |
| 209 | } |
Soby Mathew | 6e79f9f | 2018-03-26 15:16:46 +0100 | [diff] [blame] | 210 | |
| 211 | #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH) |
| 212 | uint32_t disable_auth = 0; |
Louis Mayencourt | 25ac879 | 2019-12-17 13:17:25 +0000 | [diff] [blame^] | 213 | void *tb_fw_cfg_dtb; |
| 214 | int err, tb_fw_node; |
| 215 | |
| 216 | dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID); |
| 217 | tb_fw_cfg_dtb = (void *)dtb_info->config_addr; |
| 218 | |
| 219 | err = arm_dyn_tb_fw_cfg_init(tb_fw_cfg_dtb, &tb_fw_node); |
| 220 | if (err < 0) { |
| 221 | ERROR("Invalid TB_FW_CONFIG passed from BL1\n"); |
| 222 | panic(); |
| 223 | } |
Soby Mathew | 6e79f9f | 2018-03-26 15:16:46 +0100 | [diff] [blame] | 224 | |
John Tsichritzis | 432f0ad | 2018-08-23 09:57:54 +0100 | [diff] [blame] | 225 | err = arm_dyn_get_disable_auth(tb_fw_cfg_dtb, tb_fw_node, |
Soby Mathew | 6e79f9f | 2018-03-26 15:16:46 +0100 | [diff] [blame] | 226 | &disable_auth); |
| 227 | if (err < 0) |
| 228 | return; |
| 229 | |
| 230 | if (disable_auth == 1) |
| 231 | dyn_disable_auth(); |
| 232 | #endif |
Soby Mathew | cab0b5b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 233 | } |