xref: /rk3399_ARM-atf/plat/arm/common/arm_dyn_cfg.c (revision 6e79f9fd4b65f473374391595e31c155e9e0ad85)
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);
57*6e79f9fdSSoby Mathew 
58*6e79f9fdSSoby Mathew #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
59*6e79f9fdSSoby Mathew 	int tb_fw_node;
60*6e79f9fdSSoby Mathew 	uint32_t disable_auth = 0;
61*6e79f9fdSSoby Mathew 
62*6e79f9fdSSoby Mathew 	err = arm_dyn_tb_fw_cfg_init((void *)config_base, &tb_fw_node);
63*6e79f9fdSSoby Mathew 	if (err < 0) {
64*6e79f9fdSSoby Mathew 		WARN("Invalid TB_FW_CONFIG loaded\n");
65*6e79f9fdSSoby Mathew 		return;
66*6e79f9fdSSoby Mathew 	}
67*6e79f9fdSSoby Mathew 
68*6e79f9fdSSoby Mathew 	err = arm_dyn_get_disable_auth((void *)config_base, tb_fw_node, &disable_auth);
69*6e79f9fdSSoby Mathew 	if (err < 0)
70*6e79f9fdSSoby Mathew 		return;
71*6e79f9fdSSoby Mathew 
72*6e79f9fdSSoby Mathew 	if (disable_auth == 1)
73*6e79f9fdSSoby Mathew 		dyn_disable_auth();
74*6e79f9fdSSoby 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
88cab0b5b0SSoby Mathew  * TB_FW_CONFIG. Return early if TB_FW_CONFIG is not found or HW_CONFIG is
89cab0b5b0SSoby Mathew  * not specified in TB_FW_CONFIG.
90cab0b5b0SSoby Mathew  */
91cab0b5b0SSoby Mathew void arm_bl2_dyn_cfg_init(void)
92cab0b5b0SSoby Mathew {
93cab0b5b0SSoby Mathew 	int err = 0;
94cab0b5b0SSoby Mathew 	int tb_fw_node;
95cab0b5b0SSoby Mathew 	bl_mem_params_node_t *hw_cfg_mem_params = NULL;
96cab0b5b0SSoby Mathew 
97cab0b5b0SSoby Mathew 	if (tb_fw_cfg_dtb == NULL) {
98cab0b5b0SSoby Mathew 		VERBOSE("No TB_FW_CONFIG specified\n");
99cab0b5b0SSoby Mathew 		return;
100cab0b5b0SSoby Mathew 	}
101cab0b5b0SSoby Mathew 
102cab0b5b0SSoby Mathew 	err = arm_dyn_tb_fw_cfg_init((void *)tb_fw_cfg_dtb, &tb_fw_node);
103cab0b5b0SSoby Mathew 	if (err < 0) {
104cab0b5b0SSoby Mathew 		ERROR("Invalid TB_FW_CONFIG passed from BL1\n");
105cab0b5b0SSoby Mathew 		panic();
106cab0b5b0SSoby Mathew 	}
107cab0b5b0SSoby Mathew 
108cab0b5b0SSoby Mathew 	/* Get the hw_config load address and size from TB_FW_CONFIG */
109cab0b5b0SSoby Mathew 	hw_cfg_mem_params = get_bl_mem_params_node(HW_CONFIG_ID);
110cab0b5b0SSoby Mathew 	if (hw_cfg_mem_params == NULL) {
111cab0b5b0SSoby Mathew 		VERBOSE("Couldn't find HW_CONFIG in bl_mem_params_node\n");
112cab0b5b0SSoby Mathew 		return;
113cab0b5b0SSoby Mathew 	}
114cab0b5b0SSoby Mathew 
115cab0b5b0SSoby Mathew 	err = arm_dyn_get_hwconfig_info((void *)tb_fw_cfg_dtb, tb_fw_node,
116cab0b5b0SSoby Mathew 		(uint64_t *) &hw_cfg_mem_params->image_info.image_base,
117cab0b5b0SSoby Mathew 		&hw_cfg_mem_params->image_info.image_max_size);
118cab0b5b0SSoby Mathew 	if (err < 0) {
119cab0b5b0SSoby Mathew 		VERBOSE("Couldn't find HW_CONFIG load info in TB_FW_CONFIG\n");
120cab0b5b0SSoby Mathew 		return;
121cab0b5b0SSoby Mathew 	}
122cab0b5b0SSoby Mathew 
123cab0b5b0SSoby Mathew 	/* Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from HW_CONFIG node */
124cab0b5b0SSoby Mathew 	hw_cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
125*6e79f9fdSSoby Mathew 
126*6e79f9fdSSoby Mathew #if TRUSTED_BOARD_BOOT && defined(DYN_DISABLE_AUTH)
127*6e79f9fdSSoby Mathew 	uint32_t disable_auth = 0;
128*6e79f9fdSSoby Mathew 
129*6e79f9fdSSoby Mathew 	err = arm_dyn_get_disable_auth((void *)tb_fw_cfg_dtb, tb_fw_node,
130*6e79f9fdSSoby Mathew 					&disable_auth);
131*6e79f9fdSSoby Mathew 	if (err < 0)
132*6e79f9fdSSoby Mathew 		return;
133*6e79f9fdSSoby Mathew 
134*6e79f9fdSSoby Mathew 	if (disable_auth == 1)
135*6e79f9fdSSoby Mathew 		dyn_disable_auth();
136*6e79f9fdSSoby Mathew #endif
137cab0b5b0SSoby Mathew }
138cab0b5b0SSoby Mathew 
139c228956aSSoby Mathew #endif /* LOAD_IMAGE_V2 */
140