xref: /rk3399_ARM-atf/plat/arm/common/fconf/arm_fconf_io.c (revision ec767c1b99675fbb50ef1b2fdb2d38e881e4789d)
1 /*
2  * Copyright (c) 2019-2021, ARM Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 
9 #include <common/debug.h>
10 #include <common/fdt_wrappers.h>
11 #include <drivers/io/io_storage.h>
12 #include <drivers/partition/partition.h>
13 #include <lib/object_pool.h>
14 #include <libfdt.h>
15 #include <tools_share/firmware_image_package.h>
16 
17 #include <plat/arm/common/arm_fconf_getter.h>
18 #include <plat/arm/common/arm_fconf_io_storage.h>
19 #include <platform_def.h>
20 
21 io_block_spec_t fip_block_spec = {
22 /*
23  * This is fixed FIP address used by BL1, BL2 loads partition table
24  * to get FIP address.
25  */
26 #if ARM_GPT_SUPPORT
27 	.offset = PLAT_ARM_FLASH_IMAGE_BASE + PLAT_ARM_FIP_OFFSET_IN_GPT,
28 #else
29 	.offset = PLAT_ARM_FLASH_IMAGE_BASE,
30 #endif /* ARM_GPT_SUPPORT */
31 	.length = PLAT_ARM_FLASH_IMAGE_MAX_SIZE
32 };
33 
34 #if ARM_GPT_SUPPORT
35 static const io_block_spec_t gpt_spec = {
36 	.offset         = PLAT_ARM_FLASH_IMAGE_BASE,
37 	/*
38 	 * PLAT_PARTITION_BLOCK_SIZE = 512
39 	 * PLAT_PARTITION_MAX_ENTRIES = 128
40 	 * each sector has 4 partition entries, and there are
41 	 * 2 reserved sectors i.e. protective MBR and primary
42 	 * GPT header hence length gets calculated as,
43 	 * length = 512 * (128/4 + 2)
44 	 */
45 	.length         = PLAT_PARTITION_BLOCK_SIZE *
46 			  (PLAT_PARTITION_MAX_ENTRIES / 4 + 2),
47 };
48 #endif /* ARM_GPT_SUPPORT */
49 
50 const io_uuid_spec_t arm_uuid_spec[MAX_NUMBER_IDS] = {
51 	[BL2_IMAGE_ID] = {UUID_TRUSTED_BOOT_FIRMWARE_BL2},
52 	[TB_FW_CONFIG_ID] = {UUID_TB_FW_CONFIG},
53 	[FW_CONFIG_ID] = {UUID_FW_CONFIG},
54 #if !ARM_IO_IN_DTB
55 	[SCP_BL2_IMAGE_ID] = {UUID_SCP_FIRMWARE_SCP_BL2},
56 	[BL31_IMAGE_ID] = {UUID_EL3_RUNTIME_FIRMWARE_BL31},
57 	[BL32_IMAGE_ID] = {UUID_SECURE_PAYLOAD_BL32},
58 	[BL32_EXTRA1_IMAGE_ID] = {UUID_SECURE_PAYLOAD_BL32_EXTRA1},
59 	[BL32_EXTRA2_IMAGE_ID] = {UUID_SECURE_PAYLOAD_BL32_EXTRA2},
60 	[BL33_IMAGE_ID] = {UUID_NON_TRUSTED_FIRMWARE_BL33},
61 	[HW_CONFIG_ID] = {UUID_HW_CONFIG},
62 	[SOC_FW_CONFIG_ID] = {UUID_SOC_FW_CONFIG},
63 	[TOS_FW_CONFIG_ID] = {UUID_TOS_FW_CONFIG},
64 	[NT_FW_CONFIG_ID] = {UUID_NT_FW_CONFIG},
65 #endif /* ARM_IO_IN_DTB */
66 #if TRUSTED_BOARD_BOOT
67 	[TRUSTED_BOOT_FW_CERT_ID] = {UUID_TRUSTED_BOOT_FW_CERT},
68 #if !ARM_IO_IN_DTB
69 	[TRUSTED_KEY_CERT_ID] = {UUID_TRUSTED_KEY_CERT},
70 	[SCP_FW_KEY_CERT_ID] = {UUID_SCP_FW_KEY_CERT},
71 	[SOC_FW_KEY_CERT_ID] = {UUID_SOC_FW_KEY_CERT},
72 	[TRUSTED_OS_FW_KEY_CERT_ID] = {UUID_TRUSTED_OS_FW_KEY_CERT},
73 	[NON_TRUSTED_FW_KEY_CERT_ID] = {UUID_NON_TRUSTED_FW_KEY_CERT},
74 	[SCP_FW_CONTENT_CERT_ID] = {UUID_SCP_FW_CONTENT_CERT},
75 	[SOC_FW_CONTENT_CERT_ID] = {UUID_SOC_FW_CONTENT_CERT},
76 	[TRUSTED_OS_FW_CONTENT_CERT_ID] = {UUID_TRUSTED_OS_FW_CONTENT_CERT},
77 	[NON_TRUSTED_FW_CONTENT_CERT_ID] = {UUID_NON_TRUSTED_FW_CONTENT_CERT},
78 #if defined(SPD_spmd)
79 	[SIP_SP_CONTENT_CERT_ID] = {UUID_SIP_SECURE_PARTITION_CONTENT_CERT},
80 	[PLAT_SP_CONTENT_CERT_ID] = {UUID_PLAT_SECURE_PARTITION_CONTENT_CERT},
81 #endif
82 #endif /* ARM_IO_IN_DTB */
83 #endif /* TRUSTED_BOARD_BOOT */
84 };
85 
86 /* By default, ARM platforms load images from the FIP */
87 struct plat_io_policy policies[MAX_NUMBER_IDS] = {
88 #if ARM_GPT_SUPPORT
89 	[GPT_IMAGE_ID] = {
90 		&memmap_dev_handle,
91 		(uintptr_t)&gpt_spec,
92 		open_memmap
93 	},
94 #endif /* ARM_GPT_SUPPORT */
95 	[FIP_IMAGE_ID] = {
96 		&memmap_dev_handle,
97 		(uintptr_t)&fip_block_spec,
98 		open_memmap
99 	},
100 	[BL2_IMAGE_ID] = {
101 		&fip_dev_handle,
102 		(uintptr_t)&arm_uuid_spec[BL2_IMAGE_ID],
103 		open_fip
104 	},
105 	[TB_FW_CONFIG_ID] = {
106 		&fip_dev_handle,
107 		(uintptr_t)&arm_uuid_spec[TB_FW_CONFIG_ID],
108 		open_fip
109 	},
110 	[FW_CONFIG_ID] = {
111 		&fip_dev_handle,
112 		(uintptr_t)&arm_uuid_spec[FW_CONFIG_ID],
113 		open_fip
114 	},
115 #if !ARM_IO_IN_DTB
116 	[SCP_BL2_IMAGE_ID] = {
117 		&fip_dev_handle,
118 		(uintptr_t)&arm_uuid_spec[SCP_BL2_IMAGE_ID],
119 		open_fip
120 	},
121 	[BL31_IMAGE_ID] = {
122 		&fip_dev_handle,
123 		(uintptr_t)&arm_uuid_spec[BL31_IMAGE_ID],
124 		open_fip
125 	},
126 	[BL32_IMAGE_ID] = {
127 		&fip_dev_handle,
128 		(uintptr_t)&arm_uuid_spec[BL32_IMAGE_ID],
129 		open_fip
130 	},
131 	[BL32_EXTRA1_IMAGE_ID] = {
132 		&fip_dev_handle,
133 		(uintptr_t)&arm_uuid_spec[BL32_EXTRA1_IMAGE_ID],
134 		open_fip
135 	},
136 	[BL32_EXTRA2_IMAGE_ID] = {
137 		&fip_dev_handle,
138 		(uintptr_t)&arm_uuid_spec[BL32_EXTRA2_IMAGE_ID],
139 		open_fip
140 	},
141 	[BL33_IMAGE_ID] = {
142 		&fip_dev_handle,
143 		(uintptr_t)&arm_uuid_spec[BL33_IMAGE_ID],
144 		open_fip
145 	},
146 	[HW_CONFIG_ID] = {
147 		&fip_dev_handle,
148 		(uintptr_t)&arm_uuid_spec[HW_CONFIG_ID],
149 		open_fip
150 	},
151 	[SOC_FW_CONFIG_ID] = {
152 		&fip_dev_handle,
153 		(uintptr_t)&arm_uuid_spec[SOC_FW_CONFIG_ID],
154 		open_fip
155 	},
156 	[TOS_FW_CONFIG_ID] = {
157 		&fip_dev_handle,
158 		(uintptr_t)&arm_uuid_spec[TOS_FW_CONFIG_ID],
159 		open_fip
160 	},
161 	[NT_FW_CONFIG_ID] = {
162 		&fip_dev_handle,
163 		(uintptr_t)&arm_uuid_spec[NT_FW_CONFIG_ID],
164 		open_fip
165 	},
166 #endif /* ARM_IO_IN_DTB */
167 #if TRUSTED_BOARD_BOOT
168 	[TRUSTED_BOOT_FW_CERT_ID] = {
169 		&fip_dev_handle,
170 		(uintptr_t)&arm_uuid_spec[TRUSTED_BOOT_FW_CERT_ID],
171 		open_fip
172 	},
173 #if !ARM_IO_IN_DTB
174 	[TRUSTED_KEY_CERT_ID] = {
175 		&fip_dev_handle,
176 		(uintptr_t)&arm_uuid_spec[TRUSTED_KEY_CERT_ID],
177 		open_fip
178 	},
179 	[SCP_FW_KEY_CERT_ID] = {
180 		&fip_dev_handle,
181 		(uintptr_t)&arm_uuid_spec[SCP_FW_KEY_CERT_ID],
182 		open_fip
183 	},
184 	[SOC_FW_KEY_CERT_ID] = {
185 		&fip_dev_handle,
186 		(uintptr_t)&arm_uuid_spec[SOC_FW_KEY_CERT_ID],
187 		open_fip
188 	},
189 	[TRUSTED_OS_FW_KEY_CERT_ID] = {
190 		&fip_dev_handle,
191 		(uintptr_t)&arm_uuid_spec[TRUSTED_OS_FW_KEY_CERT_ID],
192 		open_fip
193 	},
194 	[NON_TRUSTED_FW_KEY_CERT_ID] = {
195 		&fip_dev_handle,
196 		(uintptr_t)&arm_uuid_spec[NON_TRUSTED_FW_KEY_CERT_ID],
197 		open_fip
198 	},
199 	[SCP_FW_CONTENT_CERT_ID] = {
200 		&fip_dev_handle,
201 		(uintptr_t)&arm_uuid_spec[SCP_FW_CONTENT_CERT_ID],
202 		open_fip
203 	},
204 	[SOC_FW_CONTENT_CERT_ID] = {
205 		&fip_dev_handle,
206 		(uintptr_t)&arm_uuid_spec[SOC_FW_CONTENT_CERT_ID],
207 		open_fip
208 	},
209 	[TRUSTED_OS_FW_CONTENT_CERT_ID] = {
210 		&fip_dev_handle,
211 		(uintptr_t)&arm_uuid_spec[TRUSTED_OS_FW_CONTENT_CERT_ID],
212 		open_fip
213 	},
214 	[NON_TRUSTED_FW_CONTENT_CERT_ID] = {
215 		&fip_dev_handle,
216 		(uintptr_t)&arm_uuid_spec[NON_TRUSTED_FW_CONTENT_CERT_ID],
217 		open_fip
218 	},
219 #if defined(SPD_spmd)
220 	[SIP_SP_CONTENT_CERT_ID] = {
221 		&fip_dev_handle,
222 		(uintptr_t)&arm_uuid_spec[SIP_SP_CONTENT_CERT_ID],
223 		open_fip
224 	},
225 	[PLAT_SP_CONTENT_CERT_ID] = {
226 		&fip_dev_handle,
227 		(uintptr_t)&arm_uuid_spec[PLAT_SP_CONTENT_CERT_ID],
228 		open_fip
229 	},
230 #endif
231 #endif /* ARM_IO_IN_DTB */
232 #endif /* TRUSTED_BOARD_BOOT */
233 };
234 
235 #ifdef IMAGE_BL2
236 
237 #if TRUSTED_BOARD_BOOT
238 #define FCONF_ARM_IO_UUID_NUMBER	U(21)
239 #else
240 #define FCONF_ARM_IO_UUID_NUMBER	U(10)
241 #endif
242 
243 static io_uuid_spec_t fconf_arm_uuids[FCONF_ARM_IO_UUID_NUMBER];
244 static OBJECT_POOL_ARRAY(fconf_arm_uuids_pool, fconf_arm_uuids);
245 
246 struct policies_load_info {
247 	unsigned int image_id;
248 	const char *name;
249 };
250 
251 /* image id to property name table */
252 static const struct policies_load_info load_info[FCONF_ARM_IO_UUID_NUMBER] = {
253 	{SCP_BL2_IMAGE_ID, "scp_bl2_uuid"},
254 	{BL31_IMAGE_ID, "bl31_uuid"},
255 	{BL32_IMAGE_ID, "bl32_uuid"},
256 	{BL32_EXTRA1_IMAGE_ID, "bl32_extra1_uuid"},
257 	{BL32_EXTRA2_IMAGE_ID, "bl32_extra2_uuid"},
258 	{BL33_IMAGE_ID, "bl33_uuid"},
259 	{HW_CONFIG_ID, "hw_cfg_uuid"},
260 	{SOC_FW_CONFIG_ID, "soc_fw_cfg_uuid"},
261 	{TOS_FW_CONFIG_ID, "tos_fw_cfg_uuid"},
262 	{NT_FW_CONFIG_ID, "nt_fw_cfg_uuid"},
263 #if TRUSTED_BOARD_BOOT
264 	{TRUSTED_KEY_CERT_ID, "t_key_cert_uuid"},
265 	{SCP_FW_KEY_CERT_ID, "scp_fw_key_uuid"},
266 	{SOC_FW_KEY_CERT_ID, "soc_fw_key_uuid"},
267 	{TRUSTED_OS_FW_KEY_CERT_ID, "tos_fw_key_cert_uuid"},
268 	{NON_TRUSTED_FW_KEY_CERT_ID, "nt_fw_key_cert_uuid"},
269 	{SCP_FW_CONTENT_CERT_ID, "scp_fw_content_cert_uuid"},
270 	{SOC_FW_CONTENT_CERT_ID, "soc_fw_content_cert_uuid"},
271 	{TRUSTED_OS_FW_CONTENT_CERT_ID, "tos_fw_content_cert_uuid"},
272 	{NON_TRUSTED_FW_CONTENT_CERT_ID, "nt_fw_content_cert_uuid"},
273 #if defined(SPD_spmd)
274 	{SIP_SP_CONTENT_CERT_ID, "sip_sp_content_cert_uuid"},
275 	{PLAT_SP_CONTENT_CERT_ID, "plat_sp_content_cert_uuid"},
276 #endif
277 #endif /* TRUSTED_BOARD_BOOT */
278 };
279 
280 int fconf_populate_arm_io_policies(uintptr_t config)
281 {
282 	int err, node;
283 	unsigned int i;
284 
285 	union uuid_helper_t uuid_helper;
286 	io_uuid_spec_t *uuid_ptr;
287 
288 	/* As libfdt uses void *, we can't avoid this cast */
289 	const void *dtb = (void *)config;
290 
291 	/* Assert the node offset point to "arm,io-fip-handle" compatible property */
292 	const char *compatible_str = "arm,io-fip-handle";
293 	node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
294 	if (node < 0) {
295 		ERROR("FCONF: Can't find %s compatible in dtb\n", compatible_str);
296 		return node;
297 	}
298 
299 	/* Locate the uuid cells and read the value for all the load info uuid */
300 	for (i = 0; i < FCONF_ARM_IO_UUID_NUMBER; i++) {
301 		uuid_ptr = pool_alloc(&fconf_arm_uuids_pool);
302 		err = fdtw_read_uuid(dtb, node, load_info[i].name, 16,
303 				     (uint8_t *)&uuid_helper);
304 		if (err < 0) {
305 			WARN("FCONF: Read cell failed for %s\n", load_info[i].name);
306 			return err;
307 		}
308 
309 		VERBOSE("FCONF: arm-io_policies.%s cell found with value = "
310 			"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
311 			load_info[i].name,
312 			uuid_helper.uuid_struct.time_low[0], uuid_helper.uuid_struct.time_low[1],
313 			uuid_helper.uuid_struct.time_low[2], uuid_helper.uuid_struct.time_low[3],
314 			uuid_helper.uuid_struct.time_mid[0], uuid_helper.uuid_struct.time_mid[1],
315 			uuid_helper.uuid_struct.time_hi_and_version[0],
316 			uuid_helper.uuid_struct.time_hi_and_version[1],
317 			uuid_helper.uuid_struct.clock_seq_hi_and_reserved,
318 			uuid_helper.uuid_struct.clock_seq_low,
319 			uuid_helper.uuid_struct.node[0], uuid_helper.uuid_struct.node[1],
320 			uuid_helper.uuid_struct.node[2], uuid_helper.uuid_struct.node[3],
321 			uuid_helper.uuid_struct.node[4], uuid_helper.uuid_struct.node[5]);
322 
323 		uuid_ptr->uuid = uuid_helper.uuid_struct;
324 		policies[load_info[i].image_id].image_spec = (uintptr_t)uuid_ptr;
325 		policies[load_info[i].image_id].dev_handle = &fip_dev_handle;
326 		policies[load_info[i].image_id].check = open_fip;
327 	}
328 	return 0;
329 }
330 
331 #if ARM_IO_IN_DTB
332 FCONF_REGISTER_POPULATOR(TB_FW, arm_io, fconf_populate_arm_io_policies);
333 #endif /* ARM_IO_IN_DTB */
334 
335 #endif /* IMAGE_BL2 */
336