xref: /rk3399_ARM-atf/plat/arm/common/arm_dyn_cfg.c (revision 432f0ad0bba8b3147e1f35cc3b20034612016f5d)
1c228956aSSoby Mathew /*
2c228956aSSoby Mathew  * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3c228956aSSoby Mathew  *
4c228956aSSoby Mathew  * SPDX-License-Identifier: BSD-3-Clause
5c228956aSSoby Mathew  */
6c228956aSSoby Mathew 
7cab0b5b0SSoby Mathew #include <arm_dyn_cfg_helpers.h>
8c228956aSSoby Mathew #include <assert.h>
9c228956aSSoby Mathew #include <debug.h>
10c228956aSSoby Mathew #include <desc_image_load.h>
11da5f2745SSoby Mathew #include <plat_arm.h>
12c228956aSSoby Mathew #include <platform.h>
13c228956aSSoby Mathew #include <platform_def.h>
14c228956aSSoby Mathew #include <string.h>
15c228956aSSoby Mathew #include <tbbr_img_def.h>
16c228956aSSoby Mathew 
17c228956aSSoby Mathew #if LOAD_IMAGE_V2
18c228956aSSoby Mathew 
19cab0b5b0SSoby Mathew /* Variable to store the address to TB_FW_CONFIG passed from BL1 */
20cab0b5b0SSoby Mathew static void *tb_fw_cfg_dtb;
21cab0b5b0SSoby Mathew 
22c228956aSSoby Mathew /*
23c228956aSSoby Mathew  * Helper function to load TB_FW_CONFIG and populate the load information to
24c228956aSSoby Mathew  * arg0 of BL2 entrypoint info.
25c228956aSSoby Mathew  */
26c228956aSSoby Mathew void arm_load_tb_fw_config(void)
27c228956aSSoby Mathew {
28c228956aSSoby Mathew 	int err;
29c228956aSSoby Mathew 	uintptr_t config_base = 0;
30c228956aSSoby Mathew 	image_desc_t *image_desc;
31c228956aSSoby Mathew 
32c228956aSSoby Mathew 	image_desc_t arm_tb_fw_info = {
33c228956aSSoby Mathew 		.image_id = TB_FW_CONFIG_ID,
34c228956aSSoby Mathew 		SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY,
35c228956aSSoby Mathew 				VERSION_2, image_info_t, 0),
36c228956aSSoby Mathew 		.image_info.image_base = ARM_TB_FW_CONFIG_BASE,
37c228956aSSoby Mathew 		.image_info.image_max_size = ARM_TB_FW_CONFIG_LIMIT - ARM_TB_FW_CONFIG_BASE,
38c228956aSSoby Mathew 	};
39c228956aSSoby Mathew 
40c228956aSSoby Mathew 	VERBOSE("BL1: Loading TB_FW_CONFIG\n");
41c228956aSSoby Mathew 	err = load_auth_image(TB_FW_CONFIG_ID, &arm_tb_fw_info.image_info);
42da5f2745SSoby Mathew 	if (err != 0) {
43c228956aSSoby Mathew 		/* Return if TB_FW_CONFIG is not loaded */
44c228956aSSoby Mathew 		VERBOSE("Failed to load TB_FW_CONFIG\n");
45c228956aSSoby Mathew 		return;
46c228956aSSoby Mathew 	}
47c228956aSSoby Mathew 
48c228956aSSoby Mathew 	config_base = arm_tb_fw_info.image_info.image_base;
49c228956aSSoby Mathew 
50c228956aSSoby Mathew 	/* The BL2 ep_info arg0 is modified to point to TB_FW_CONFIG */
51c228956aSSoby Mathew 	image_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
52da5f2745SSoby Mathew 	assert(image_desc != NULL);
53c228956aSSoby Mathew 	image_desc->ep_info.args.arg0 = config_base;
54c228956aSSoby Mathew 
55c228956aSSoby Mathew 	INFO("BL1: TB_FW_CONFIG loaded at address = %p\n",
56c228956aSSoby Mathew 			(void *) config_base);
576e79f9fdSSoby Mathew 
586e79f9fdSSoby Mathew #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
596e79f9fdSSoby Mathew 	int tb_fw_node;
606e79f9fdSSoby Mathew 	uint32_t disable_auth = 0;
616e79f9fdSSoby Mathew 
626e79f9fdSSoby Mathew 	err = arm_dyn_tb_fw_cfg_init((void *)config_base, &tb_fw_node);
636e79f9fdSSoby Mathew 	if (err < 0) {
64355e0967SJohn Tsichritzis 		ERROR("Invalid TB_FW_CONFIG loaded\n");
65355e0967SJohn Tsichritzis 		panic();
666e79f9fdSSoby Mathew 	}
676e79f9fdSSoby Mathew 
686e79f9fdSSoby Mathew 	err = arm_dyn_get_disable_auth((void *)config_base, tb_fw_node, &disable_auth);
696e79f9fdSSoby Mathew 	if (err < 0)
706e79f9fdSSoby Mathew 		return;
716e79f9fdSSoby Mathew 
726e79f9fdSSoby Mathew 	if (disable_auth == 1)
736e79f9fdSSoby Mathew 		dyn_disable_auth();
746e79f9fdSSoby Mathew #endif
75c228956aSSoby Mathew }
76c228956aSSoby Mathew 
77cab0b5b0SSoby Mathew /*
78cab0b5b0SSoby Mathew  * BL2 utility function to set the address of TB_FW_CONFIG passed from BL1.
79cab0b5b0SSoby Mathew  */
80cab0b5b0SSoby Mathew void arm_bl2_set_tb_cfg_addr(void *dtb)
81cab0b5b0SSoby Mathew {
82da5f2745SSoby Mathew 	assert(dtb != NULL);
83cab0b5b0SSoby Mathew 	tb_fw_cfg_dtb = dtb;
84cab0b5b0SSoby Mathew }
85cab0b5b0SSoby Mathew 
86cab0b5b0SSoby Mathew /*
87cab0b5b0SSoby Mathew  * BL2 utility function to initialize dynamic configuration specified by
881d71ba14SSoby Mathew  * TB_FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
891d71ba14SSoby Mathew  * specified in TB_FW_CONFIG.
90cab0b5b0SSoby Mathew  */
91cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void)
92cab0b5b0SSoby Mathew {
931d71ba14SSoby Mathew 	int err = 0, tb_fw_node;
941d71ba14SSoby Mathew 	unsigned int i;
951d71ba14SSoby Mathew 	bl_mem_params_node_t *cfg_mem_params = NULL;
961d71ba14SSoby Mathew 	uint64_t image_base;
971d71ba14SSoby Mathew 	uint32_t image_size;
981d71ba14SSoby Mathew 	const unsigned int config_ids[] = {
991d71ba14SSoby Mathew 			HW_CONFIG_ID,
1001d71ba14SSoby Mathew 			SOC_FW_CONFIG_ID,
1011d71ba14SSoby Mathew 			NT_FW_CONFIG_ID,
1021d71ba14SSoby Mathew #ifdef SPD_tspd
1031d71ba14SSoby Mathew 			/* Currently tos_fw_config is only present for TSP */
1041d71ba14SSoby Mathew 			TOS_FW_CONFIG_ID
1051d71ba14SSoby Mathew #endif
1061d71ba14SSoby Mathew 	};
107cab0b5b0SSoby Mathew 
108cab0b5b0SSoby Mathew 	if (tb_fw_cfg_dtb == NULL) {
109cab0b5b0SSoby Mathew 		VERBOSE("No TB_FW_CONFIG specified\n");
110cab0b5b0SSoby Mathew 		return;
111cab0b5b0SSoby Mathew 	}
112cab0b5b0SSoby Mathew 
113*432f0ad0SJohn Tsichritzis 	err = arm_dyn_tb_fw_cfg_init(tb_fw_cfg_dtb, &tb_fw_node);
114cab0b5b0SSoby Mathew 	if (err < 0) {
115cab0b5b0SSoby Mathew 		ERROR("Invalid TB_FW_CONFIG passed from BL1\n");
116cab0b5b0SSoby Mathew 		panic();
117cab0b5b0SSoby Mathew 	}
118cab0b5b0SSoby Mathew 
1191d71ba14SSoby Mathew 	/* Iterate through all the fw config IDs */
1201d71ba14SSoby Mathew 	for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
1211d71ba14SSoby Mathew 		/* Get the config load address and size from TB_FW_CONFIG */
1221d71ba14SSoby Mathew 		cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
1231d71ba14SSoby Mathew 		if (cfg_mem_params == NULL) {
124cab0b5b0SSoby Mathew 			VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
1251d71ba14SSoby Mathew 			continue;
126cab0b5b0SSoby Mathew 		}
127cab0b5b0SSoby Mathew 
128*432f0ad0SJohn Tsichritzis 		err = arm_dyn_get_config_load_info(tb_fw_cfg_dtb, tb_fw_node,
1291d71ba14SSoby Mathew 				config_ids[i], &image_base, &image_size);
130cab0b5b0SSoby Mathew 		if (err < 0) {
1311d71ba14SSoby Mathew 			VERBOSE("Couldn't find config_id %d load info in TB_FW_CONFIG\n",
1321d71ba14SSoby Mathew 					config_ids[i]);
1331d71ba14SSoby Mathew 			continue;
134cab0b5b0SSoby Mathew 		}
135cab0b5b0SSoby Mathew 
1361d71ba14SSoby Mathew 		/*
1371d71ba14SSoby Mathew 		 * Do some runtime checks on the load addresses of soc_fw_config,
1381d71ba14SSoby Mathew 		 * tos_fw_config, nt_fw_config. This is not a comprehensive check
1391d71ba14SSoby Mathew 		 * of all invalid addresses but to prevent trivial porting errors.
1401d71ba14SSoby Mathew 		 */
1411d71ba14SSoby Mathew 		if (config_ids[i] != HW_CONFIG_ID) {
1421d71ba14SSoby Mathew 
1431d71ba14SSoby Mathew 			if (check_uptr_overflow(image_base, image_size) != 0)
1441d71ba14SSoby Mathew 				continue;
1451d71ba14SSoby Mathew 
146c099cd39SSoby Mathew 			/* Ensure the configs don't overlap with BL31 */
147c099cd39SSoby Mathew 			if ((image_base > BL31_BASE) || ((image_base + image_size) > BL31_BASE))
1481d71ba14SSoby Mathew 				continue;
1491d71ba14SSoby Mathew 
1501d71ba14SSoby Mathew 			/* Ensure the configs are loaded in a valid address */
1511d71ba14SSoby Mathew 			if (image_base < ARM_BL_RAM_BASE)
1521d71ba14SSoby Mathew 				continue;
1531d71ba14SSoby Mathew #ifdef BL32_BASE
1541d71ba14SSoby Mathew 			/*
1551d71ba14SSoby Mathew 			 * If BL32 is present, ensure that the configs don't
1561d71ba14SSoby Mathew 			 * overlap with it.
1571d71ba14SSoby Mathew 			 */
1581d71ba14SSoby Mathew 			if (image_base >= BL32_BASE && image_base <= BL32_LIMIT)
1591d71ba14SSoby Mathew 				continue;
1601d71ba14SSoby Mathew #endif
1611d71ba14SSoby Mathew 		}
1621d71ba14SSoby Mathew 
1631d71ba14SSoby Mathew 
1641d71ba14SSoby Mathew 		cfg_mem_params->image_info.image_base = (uintptr_t)image_base;
1651d71ba14SSoby Mathew 		cfg_mem_params->image_info.image_max_size = image_size;
1661d71ba14SSoby Mathew 
167cab0b5b0SSoby Mathew 		/* Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from HW_CONFIG node */
1681d71ba14SSoby Mathew 		cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
1691d71ba14SSoby Mathew 	}
1706e79f9fdSSoby Mathew 
1716e79f9fdSSoby Mathew #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
1726e79f9fdSSoby Mathew 	uint32_t disable_auth = 0;
1736e79f9fdSSoby Mathew 
174*432f0ad0SJohn Tsichritzis 	err = arm_dyn_get_disable_auth(tb_fw_cfg_dtb, tb_fw_node,
1756e79f9fdSSoby Mathew 					&disable_auth);
1766e79f9fdSSoby Mathew 	if (err < 0)
1776e79f9fdSSoby Mathew 		return;
1786e79f9fdSSoby Mathew 
1796e79f9fdSSoby Mathew 	if (disable_auth == 1)
1806e79f9fdSSoby Mathew 		dyn_disable_auth();
1816e79f9fdSSoby Mathew #endif
182cab0b5b0SSoby Mathew }
183cab0b5b0SSoby Mathew 
184c228956aSSoby Mathew #endif /* LOAD_IMAGE_V2 */
185