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