2018-01-15 14:43:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2018-12-14 00:18:21 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <platform_def.h>
|
|
|
|
|
|
|
|
#include <common/debug.h>
|
|
|
|
#include <common/desc_image_load.h>
|
|
|
|
#include <common/tbbr/tbbr_img_def.h>
|
2018-07-30 13:41:52 +01:00
|
|
|
#if TRUSTED_BOARD_BOOT
|
2018-12-14 00:18:21 +00:00
|
|
|
#include <drivers/auth/mbedtls/mbedtls_config.h>
|
2018-07-30 13:41:52 +01:00
|
|
|
#endif
|
2019-01-25 14:30:04 +00:00
|
|
|
#include <plat/arm/common/arm_dyn_cfg_helpers.h>
|
|
|
|
#include <plat/arm/common/plat_arm.h>
|
2018-12-14 00:18:21 +00:00
|
|
|
#include <plat/common/platform.h>
|
2018-01-15 14:43:42 +00:00
|
|
|
|
2018-09-25 11:37:23 +01:00
|
|
|
/* Variable to store the address to TB_FW_CONFIG passed from BL1 */
|
2018-01-15 14:45:33 +00:00
|
|
|
static void *tb_fw_cfg_dtb;
|
2018-09-07 10:52:12 +01:00
|
|
|
static size_t tb_fw_cfg_dtb_size;
|
2018-01-15 14:45:33 +00:00
|
|
|
|
2018-07-30 13:41:52 +01:00
|
|
|
|
|
|
|
#if TRUSTED_BOARD_BOOT
|
|
|
|
|
|
|
|
static void *mbedtls_heap_addr;
|
|
|
|
static size_t mbedtls_heap_size;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is the implementation of the shared Mbed TLS heap between
|
|
|
|
* BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1
|
|
|
|
* to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file
|
|
|
|
* which is a DTB.
|
|
|
|
*
|
|
|
|
* This function is placed inside an #if directive for the below reasons:
|
|
|
|
* - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot
|
|
|
|
* is enabled.
|
|
|
|
* - This implementation requires the DTB to be present so that BL1 has a
|
2018-09-25 11:37:23 +01:00
|
|
|
* mechanism to pass the pointer to BL2.
|
2018-07-30 13:41:52 +01:00
|
|
|
*/
|
|
|
|
int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
|
|
|
|
{
|
|
|
|
assert(heap_addr != NULL);
|
|
|
|
assert(heap_size != NULL);
|
|
|
|
|
|
|
|
#if defined(IMAGE_BL1) || BL2_AT_EL3
|
|
|
|
|
|
|
|
/* If in BL1 or BL2_AT_EL3 define a heap */
|
|
|
|
static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
|
|
|
|
|
|
|
|
*heap_addr = heap;
|
|
|
|
*heap_size = sizeof(heap);
|
|
|
|
mbedtls_heap_addr = heap;
|
|
|
|
mbedtls_heap_size = sizeof(heap);
|
|
|
|
|
|
|
|
#elif defined(IMAGE_BL2)
|
|
|
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* If in BL2, retrieve the already allocated heap's info from DTB */
|
2018-09-07 10:42:37 +01:00
|
|
|
if (tb_fw_cfg_dtb != NULL) {
|
|
|
|
err = arm_get_dtb_mbedtls_heap_info(tb_fw_cfg_dtb, heap_addr,
|
|
|
|
heap_size);
|
|
|
|
if (err < 0) {
|
|
|
|
ERROR("BL2: unable to retrieve shared Mbed TLS heap information from DTB\n");
|
|
|
|
panic();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ERROR("BL2: DTB missing, cannot get Mbed TLS heap\n");
|
2018-07-30 13:41:52 +01:00
|
|
|
panic();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Puts the shared Mbed TLS heap information to the DTB.
|
|
|
|
* Executed only from BL1.
|
|
|
|
*/
|
|
|
|
void arm_bl1_set_mbedtls_heap(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If tb_fw_cfg_dtb==NULL then DTB is not present for the current
|
|
|
|
* platform. As such, we don't attempt to write to the DTB at all.
|
|
|
|
*
|
|
|
|
* If mbedtls_heap_addr==NULL, then it means we are using the default
|
|
|
|
* heap implementation. As such, BL2 will have its own heap for sure
|
|
|
|
* and hence there is no need to pass any information to the DTB.
|
|
|
|
*
|
|
|
|
* In the latter case, if we still wanted to write in the DTB the heap
|
|
|
|
* information, we would need to call plat_get_mbedtls_heap to retrieve
|
|
|
|
* the default heap's address and size.
|
|
|
|
*/
|
|
|
|
if ((tb_fw_cfg_dtb != NULL) && (mbedtls_heap_addr != NULL)) {
|
|
|
|
err = arm_set_dtb_mbedtls_heap_info(tb_fw_cfg_dtb,
|
|
|
|
mbedtls_heap_addr, mbedtls_heap_size);
|
|
|
|
if (err < 0) {
|
2018-09-07 10:42:37 +01:00
|
|
|
ERROR("BL1: unable to write shared Mbed TLS heap information to DTB\n");
|
2018-07-30 13:41:52 +01:00
|
|
|
panic();
|
|
|
|
}
|
2018-09-07 10:52:12 +01:00
|
|
|
/*
|
|
|
|
* Ensure that the info written to the DTB is visible to other
|
|
|
|
* images. It's critical because BL2 won't be able to proceed
|
|
|
|
* without the heap info.
|
|
|
|
*/
|
|
|
|
flush_dcache_range((uintptr_t)tb_fw_cfg_dtb,
|
|
|
|
tb_fw_cfg_dtb_size);
|
2018-07-30 13:41:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* TRUSTED_BOARD_BOOT */
|
|
|
|
|
2018-01-15 14:43:42 +00:00
|
|
|
/*
|
|
|
|
* Helper function to load TB_FW_CONFIG and populate the load information to
|
|
|
|
* arg0 of BL2 entrypoint info.
|
|
|
|
*/
|
|
|
|
void arm_load_tb_fw_config(void)
|
|
|
|
{
|
|
|
|
int err;
|
2018-10-30 16:32:48 +00:00
|
|
|
uintptr_t config_base = 0UL;
|
|
|
|
image_desc_t *desc;
|
2018-01-15 14:43:42 +00:00
|
|
|
|
|
|
|
image_desc_t arm_tb_fw_info = {
|
|
|
|
.image_id = TB_FW_CONFIG_ID,
|
|
|
|
SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY,
|
|
|
|
VERSION_2, image_info_t, 0),
|
|
|
|
.image_info.image_base = ARM_TB_FW_CONFIG_BASE,
|
2018-09-07 10:52:12 +01:00
|
|
|
.image_info.image_max_size =
|
|
|
|
ARM_TB_FW_CONFIG_LIMIT - ARM_TB_FW_CONFIG_BASE
|
2018-01-15 14:43:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
VERBOSE("BL1: Loading TB_FW_CONFIG\n");
|
|
|
|
err = load_auth_image(TB_FW_CONFIG_ID, &arm_tb_fw_info.image_info);
|
2018-02-21 01:16:39 +00:00
|
|
|
if (err != 0) {
|
2018-01-15 14:43:42 +00:00
|
|
|
/* Return if TB_FW_CONFIG is not loaded */
|
|
|
|
VERBOSE("Failed to load TB_FW_CONFIG\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-30 13:41:52 +01:00
|
|
|
/* At this point we know that a DTB is indeed available */
|
2018-01-15 14:43:42 +00:00
|
|
|
config_base = arm_tb_fw_info.image_info.image_base;
|
2018-07-30 13:41:52 +01:00
|
|
|
tb_fw_cfg_dtb = (void *)config_base;
|
2018-09-07 10:52:12 +01:00
|
|
|
tb_fw_cfg_dtb_size = (size_t)arm_tb_fw_info.image_info.image_max_size;
|
2018-01-15 14:43:42 +00:00
|
|
|
|
|
|
|
/* The BL2 ep_info arg0 is modified to point to TB_FW_CONFIG */
|
2018-10-30 16:32:48 +00:00
|
|
|
desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
|
|
|
|
assert(desc != NULL);
|
|
|
|
desc->ep_info.args.arg0 = config_base;
|
2018-01-15 14:43:42 +00:00
|
|
|
|
2018-10-30 16:32:48 +00:00
|
|
|
INFO("BL1: TB_FW_CONFIG loaded at address = 0x%lx\n", config_base);
|
2018-03-26 15:16:46 +01:00
|
|
|
|
|
|
|
#if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
|
|
|
|
int tb_fw_node;
|
|
|
|
uint32_t disable_auth = 0;
|
|
|
|
|
|
|
|
err = arm_dyn_tb_fw_cfg_init((void *)config_base, &tb_fw_node);
|
|
|
|
if (err < 0) {
|
Panic in BL1 when TB_FW_CONFIG is invalid
In Arm platforms, when using dynamic configuration, the necessary
parameters are made available as a DTB. The DTB is loaded by BL1 and,
later on, is parsed by BL1, BL2 or even both, depending on when
information from the DTB is needed.
When the DTB is going to be parsed, it must be validated first, to
ensure that it is properly structured. If an invalid DTB is detected
then:
- BL1 prints a diagnostic but allows execution to continue,
- BL2 prints a diagnostic and panics.
Now the behaviour of BL1 is changed so for it also to panic. Thus, the
behaviour of BL1 and BL2 is now similar.
Keep in mind that if BL1 only loads the DTB but it doesn't need to
read/write it, then it doesn't validate it. The validation is done only
when the DTB is actually going to be accessed.
Change-Id: Idcae6092e6dbeab7248dd5e041d6cbb7784fe410
Signed-off-by: John Tsichritzis <john.tsichritzis@arm.com>
2018-06-15 11:43:02 +01:00
|
|
|
ERROR("Invalid TB_FW_CONFIG loaded\n");
|
|
|
|
panic();
|
2018-03-26 15:16:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
err = arm_dyn_get_disable_auth((void *)config_base, tb_fw_node, &disable_auth);
|
|
|
|
if (err < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (disable_auth == 1)
|
|
|
|
dyn_disable_auth();
|
|
|
|
#endif
|
2018-01-15 14:43:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-15 14:45:33 +00:00
|
|
|
/*
|
|
|
|
* BL2 utility function to set the address of TB_FW_CONFIG passed from BL1.
|
|
|
|
*/
|
|
|
|
void arm_bl2_set_tb_cfg_addr(void *dtb)
|
|
|
|
{
|
2018-02-21 01:16:39 +00:00
|
|
|
assert(dtb != NULL);
|
2018-01-15 14:45:33 +00:00
|
|
|
tb_fw_cfg_dtb = dtb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BL2 utility function to initialize dynamic configuration specified by
|
2018-04-04 09:40:32 +01:00
|
|
|
* TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
|
|
|
|
* specified in TB_FW_CONFIG.
|
2018-01-15 14:45:33 +00:00
|
|
|
*/
|
|
|
|
void arm_bl2_dyn_cfg_init(void)
|
|
|
|
{
|
2018-04-04 09:40:32 +01:00
|
|
|
int err = 0, tb_fw_node;
|
|
|
|
unsigned int i;
|
|
|
|
bl_mem_params_node_t *cfg_mem_params = NULL;
|
|
|
|
uint64_t image_base;
|
|
|
|
uint32_t image_size;
|
|
|
|
const unsigned int config_ids[] = {
|
|
|
|
HW_CONFIG_ID,
|
|
|
|
SOC_FW_CONFIG_ID,
|
|
|
|
NT_FW_CONFIG_ID,
|
|
|
|
#ifdef SPD_tspd
|
|
|
|
/* Currently tos_fw_config is only present for TSP */
|
|
|
|
TOS_FW_CONFIG_ID
|
|
|
|
#endif
|
|
|
|
};
|
2018-01-15 14:45:33 +00:00
|
|
|
|
|
|
|
if (tb_fw_cfg_dtb == NULL) {
|
|
|
|
VERBOSE("No TB_FW_CONFIG specified\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-23 09:57:54 +01:00
|
|
|
err = arm_dyn_tb_fw_cfg_init(tb_fw_cfg_dtb, &tb_fw_node);
|
2018-01-15 14:45:33 +00:00
|
|
|
if (err < 0) {
|
|
|
|
ERROR("Invalid TB_FW_CONFIG passed from BL1\n");
|
|
|
|
panic();
|
|
|
|
}
|
|
|
|
|
2018-04-04 09:40:32 +01:00
|
|
|
/* Iterate through all the fw config IDs */
|
|
|
|
for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
|
|
|
|
/* Get the config load address and size from TB_FW_CONFIG */
|
|
|
|
cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
|
|
|
|
if (cfg_mem_params == NULL) {
|
|
|
|
VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-23 09:57:54 +01:00
|
|
|
err = arm_dyn_get_config_load_info(tb_fw_cfg_dtb, tb_fw_node,
|
2018-04-04 09:40:32 +01:00
|
|
|
config_ids[i], &image_base, &image_size);
|
|
|
|
if (err < 0) {
|
|
|
|
VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n",
|
|
|
|
config_ids[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do some runtime checks on the load addresses of soc_fw_config,
|
|
|
|
* tos_fw_config, nt_fw_config. This is not a comprehensive check
|
|
|
|
* of all invalid addresses but to prevent trivial porting errors.
|
|
|
|
*/
|
|
|
|
if (config_ids[i] != HW_CONFIG_ID) {
|
|
|
|
|
2018-10-30 16:32:48 +00:00
|
|
|
if (check_uptr_overflow(image_base, image_size))
|
2018-04-04 09:40:32 +01:00
|
|
|
continue;
|
|
|
|
|
2018-11-30 15:43:56 +00:00
|
|
|
#ifdef BL31_BASE
|
2018-06-01 16:53:38 +01:00
|
|
|
/* Ensure the configs don't overlap with BL31 */
|
|
|
|
if ((image_base > BL31_BASE) || ((image_base + image_size) > BL31_BASE))
|
2018-04-04 09:40:32 +01:00
|
|
|
continue;
|
2018-11-30 15:43:56 +00:00
|
|
|
#endif
|
2018-04-04 09:40:32 +01:00
|
|
|
/* Ensure the configs are loaded in a valid address */
|
|
|
|
if (image_base < ARM_BL_RAM_BASE)
|
|
|
|
continue;
|
|
|
|
#ifdef BL32_BASE
|
|
|
|
/*
|
|
|
|
* If BL32 is present, ensure that the configs don't
|
|
|
|
* overlap with it.
|
|
|
|
*/
|
|
|
|
if (image_base >= BL32_BASE && image_base <= BL32_LIMIT)
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
}
|
2018-01-15 14:45:33 +00:00
|
|
|
|
|
|
|
|
2018-04-04 09:40:32 +01:00
|
|
|
cfg_mem_params->image_info.image_base = (uintptr_t)image_base;
|
|
|
|
cfg_mem_params->image_info.image_max_size = image_size;
|
|
|
|
|
|
|
|
/* Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from HW_CONFIG node */
|
|
|
|
cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
|
|
|
|
}
|
2018-03-26 15:16:46 +01:00
|
|
|
|
|
|
|
#if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
|
|
|
|
uint32_t disable_auth = 0;
|
|
|
|
|
2018-08-23 09:57:54 +01:00
|
|
|
err = arm_dyn_get_disable_auth(tb_fw_cfg_dtb, tb_fw_node,
|
2018-03-26 15:16:46 +01:00
|
|
|
&disable_auth);
|
|
|
|
if (err < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (disable_auth == 1)
|
|
|
|
dyn_disable_auth();
|
|
|
|
#endif
|
2018-01-15 14:45:33 +00:00
|
|
|
}
|