xref: /rk3399_ARM-atf/plat/arm/common/arm_io_storage.c (revision c948f77136c42a92d0bb660543a3600c36dcf7f1)
1 /*
2  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <string.h>
9 
10 #include <platform_def.h>
11 
12 #include <common/debug.h>
13 #include <drivers/io/io_driver.h>
14 #include <drivers/io/io_fip.h>
15 #include <drivers/io/io_memmap.h>
16 #include <drivers/io/io_storage.h>
17 #include <lib/utils.h>
18 #include <plat/common/platform.h>
19 #include <tools_share/firmware_image_package.h>
20 
21 #include <plat_arm.h>
22 
23 /* IO devices */
24 static const io_dev_connector_t *fip_dev_con;
25 static uintptr_t fip_dev_handle;
26 static const io_dev_connector_t *memmap_dev_con;
27 static uintptr_t memmap_dev_handle;
28 
29 static const io_block_spec_t fip_block_spec = {
30 	.offset = PLAT_ARM_FIP_BASE,
31 	.length = PLAT_ARM_FIP_MAX_SIZE
32 };
33 
34 static const io_uuid_spec_t bl2_uuid_spec = {
35 	.uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
36 };
37 
38 static const io_uuid_spec_t scp_bl2_uuid_spec = {
39 	.uuid = UUID_SCP_FIRMWARE_SCP_BL2,
40 };
41 
42 static const io_uuid_spec_t bl31_uuid_spec = {
43 	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
44 };
45 
46 static const io_uuid_spec_t bl32_uuid_spec = {
47 	.uuid = UUID_SECURE_PAYLOAD_BL32,
48 };
49 
50 static const io_uuid_spec_t bl32_extra1_uuid_spec = {
51 	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
52 };
53 
54 static const io_uuid_spec_t bl32_extra2_uuid_spec = {
55 	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
56 };
57 
58 static const io_uuid_spec_t bl33_uuid_spec = {
59 	.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
60 };
61 
62 static const io_uuid_spec_t tb_fw_config_uuid_spec = {
63 	.uuid = UUID_TB_FW_CONFIG,
64 };
65 
66 static const io_uuid_spec_t hw_config_uuid_spec = {
67 	.uuid = UUID_HW_CONFIG,
68 };
69 
70 static const io_uuid_spec_t soc_fw_config_uuid_spec = {
71 	.uuid = UUID_SOC_FW_CONFIG,
72 };
73 
74 static const io_uuid_spec_t tos_fw_config_uuid_spec = {
75 	.uuid = UUID_TOS_FW_CONFIG,
76 };
77 
78 static const io_uuid_spec_t nt_fw_config_uuid_spec = {
79 	.uuid = UUID_NT_FW_CONFIG,
80 };
81 
82 #if TRUSTED_BOARD_BOOT
83 static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
84 	.uuid = UUID_TRUSTED_BOOT_FW_CERT,
85 };
86 
87 static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
88 	.uuid = UUID_TRUSTED_KEY_CERT,
89 };
90 
91 static const io_uuid_spec_t scp_fw_key_cert_uuid_spec = {
92 	.uuid = UUID_SCP_FW_KEY_CERT,
93 };
94 
95 static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
96 	.uuid = UUID_SOC_FW_KEY_CERT,
97 };
98 
99 static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
100 	.uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
101 };
102 
103 static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
104 	.uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
105 };
106 
107 static const io_uuid_spec_t scp_fw_cert_uuid_spec = {
108 	.uuid = UUID_SCP_FW_CONTENT_CERT,
109 };
110 
111 static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
112 	.uuid = UUID_SOC_FW_CONTENT_CERT,
113 };
114 
115 static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
116 	.uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
117 };
118 
119 static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
120 	.uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
121 };
122 #endif /* TRUSTED_BOARD_BOOT */
123 
124 
125 static int open_fip(const uintptr_t spec);
126 static int open_memmap(const uintptr_t spec);
127 
128 struct plat_io_policy {
129 	uintptr_t *dev_handle;
130 	uintptr_t image_spec;
131 	int (*check)(const uintptr_t spec);
132 };
133 
134 /* By default, ARM platforms load images from the FIP */
135 static const struct plat_io_policy policies[] = {
136 	[FIP_IMAGE_ID] = {
137 		&memmap_dev_handle,
138 		(uintptr_t)&fip_block_spec,
139 		open_memmap
140 	},
141 	[BL2_IMAGE_ID] = {
142 		&fip_dev_handle,
143 		(uintptr_t)&bl2_uuid_spec,
144 		open_fip
145 	},
146 	[SCP_BL2_IMAGE_ID] = {
147 		&fip_dev_handle,
148 		(uintptr_t)&scp_bl2_uuid_spec,
149 		open_fip
150 	},
151 	[BL31_IMAGE_ID] = {
152 		&fip_dev_handle,
153 		(uintptr_t)&bl31_uuid_spec,
154 		open_fip
155 	},
156 	[BL32_IMAGE_ID] = {
157 		&fip_dev_handle,
158 		(uintptr_t)&bl32_uuid_spec,
159 		open_fip
160 	},
161 	[BL32_EXTRA1_IMAGE_ID] = {
162 		&fip_dev_handle,
163 		(uintptr_t)&bl32_extra1_uuid_spec,
164 		open_fip
165 	},
166 	[BL32_EXTRA2_IMAGE_ID] = {
167 		&fip_dev_handle,
168 		(uintptr_t)&bl32_extra2_uuid_spec,
169 		open_fip
170 	},
171 	[BL33_IMAGE_ID] = {
172 		&fip_dev_handle,
173 		(uintptr_t)&bl33_uuid_spec,
174 		open_fip
175 	},
176 	[TB_FW_CONFIG_ID] = {
177 		&fip_dev_handle,
178 		(uintptr_t)&tb_fw_config_uuid_spec,
179 		open_fip
180 	},
181 	[HW_CONFIG_ID] = {
182 		&fip_dev_handle,
183 		(uintptr_t)&hw_config_uuid_spec,
184 		open_fip
185 	},
186 	[SOC_FW_CONFIG_ID] = {
187 			&fip_dev_handle,
188 			(uintptr_t)&soc_fw_config_uuid_spec,
189 			open_fip
190 	},
191 	[TOS_FW_CONFIG_ID] = {
192 		&fip_dev_handle,
193 		(uintptr_t)&tos_fw_config_uuid_spec,
194 		open_fip
195 	},
196 	[NT_FW_CONFIG_ID] = {
197 		&fip_dev_handle,
198 		(uintptr_t)&nt_fw_config_uuid_spec,
199 		open_fip
200 	},
201 #if TRUSTED_BOARD_BOOT
202 	[TRUSTED_BOOT_FW_CERT_ID] = {
203 		&fip_dev_handle,
204 		(uintptr_t)&tb_fw_cert_uuid_spec,
205 		open_fip
206 	},
207 	[TRUSTED_KEY_CERT_ID] = {
208 		&fip_dev_handle,
209 		(uintptr_t)&trusted_key_cert_uuid_spec,
210 		open_fip
211 	},
212 	[SCP_FW_KEY_CERT_ID] = {
213 		&fip_dev_handle,
214 		(uintptr_t)&scp_fw_key_cert_uuid_spec,
215 		open_fip
216 	},
217 	[SOC_FW_KEY_CERT_ID] = {
218 		&fip_dev_handle,
219 		(uintptr_t)&soc_fw_key_cert_uuid_spec,
220 		open_fip
221 	},
222 	[TRUSTED_OS_FW_KEY_CERT_ID] = {
223 		&fip_dev_handle,
224 		(uintptr_t)&tos_fw_key_cert_uuid_spec,
225 		open_fip
226 	},
227 	[NON_TRUSTED_FW_KEY_CERT_ID] = {
228 		&fip_dev_handle,
229 		(uintptr_t)&nt_fw_key_cert_uuid_spec,
230 		open_fip
231 	},
232 	[SCP_FW_CONTENT_CERT_ID] = {
233 		&fip_dev_handle,
234 		(uintptr_t)&scp_fw_cert_uuid_spec,
235 		open_fip
236 	},
237 	[SOC_FW_CONTENT_CERT_ID] = {
238 		&fip_dev_handle,
239 		(uintptr_t)&soc_fw_cert_uuid_spec,
240 		open_fip
241 	},
242 	[TRUSTED_OS_FW_CONTENT_CERT_ID] = {
243 		&fip_dev_handle,
244 		(uintptr_t)&tos_fw_cert_uuid_spec,
245 		open_fip
246 	},
247 	[NON_TRUSTED_FW_CONTENT_CERT_ID] = {
248 		&fip_dev_handle,
249 		(uintptr_t)&nt_fw_cert_uuid_spec,
250 		open_fip
251 	},
252 #endif /* TRUSTED_BOARD_BOOT */
253 };
254 
255 
256 /* Weak definitions may be overridden in specific ARM standard platform */
257 #pragma weak plat_arm_io_setup
258 #pragma weak plat_arm_get_alt_image_source
259 
260 
261 static int open_fip(const uintptr_t spec)
262 {
263 	int result;
264 	uintptr_t local_image_handle;
265 
266 	/* See if a Firmware Image Package is available */
267 	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
268 	if (result == 0) {
269 		result = io_open(fip_dev_handle, spec, &local_image_handle);
270 		if (result == 0) {
271 			VERBOSE("Using FIP\n");
272 			io_close(local_image_handle);
273 		}
274 	}
275 	return result;
276 }
277 
278 
279 static int open_memmap(const uintptr_t spec)
280 {
281 	int result;
282 	uintptr_t local_image_handle;
283 
284 	result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
285 	if (result == 0) {
286 		result = io_open(memmap_dev_handle, spec, &local_image_handle);
287 		if (result == 0) {
288 			VERBOSE("Using Memmap\n");
289 			io_close(local_image_handle);
290 		}
291 	}
292 	return result;
293 }
294 
295 
296 void arm_io_setup(void)
297 {
298 	int io_result;
299 
300 	io_result = register_io_dev_fip(&fip_dev_con);
301 	assert(io_result == 0);
302 
303 	io_result = register_io_dev_memmap(&memmap_dev_con);
304 	assert(io_result == 0);
305 
306 	/* Open connections to devices and cache the handles */
307 	io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
308 				&fip_dev_handle);
309 	assert(io_result == 0);
310 
311 	io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
312 				&memmap_dev_handle);
313 	assert(io_result == 0);
314 
315 	/* Ignore improbable errors in release builds */
316 	(void)io_result;
317 }
318 
319 void plat_arm_io_setup(void)
320 {
321 	arm_io_setup();
322 }
323 
324 int plat_arm_get_alt_image_source(
325 	unsigned int image_id __unused,
326 	uintptr_t *dev_handle __unused,
327 	uintptr_t *image_spec __unused)
328 {
329 	/* By default do not try an alternative */
330 	return -ENOENT;
331 }
332 
333 /* Return an IO device handle and specification which can be used to access
334  * an image. Use this to enforce platform load policy */
335 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
336 			  uintptr_t *image_spec)
337 {
338 	int result;
339 	const struct plat_io_policy *policy;
340 
341 	assert(image_id < ARRAY_SIZE(policies));
342 
343 	policy = &policies[image_id];
344 	result = policy->check(policy->image_spec);
345 	if (result == 0) {
346 		*image_spec = policy->image_spec;
347 		*dev_handle = *(policy->dev_handle);
348 	} else {
349 		VERBOSE("Trying alternative IO\n");
350 		result = plat_arm_get_alt_image_source(image_id, dev_handle,
351 						       image_spec);
352 	}
353 
354 	return result;
355 }
356 
357 /*
358  * See if a Firmware Image Package is available,
359  * by checking if TOC is valid or not.
360  */
361 int arm_io_is_toc_valid(void)
362 {
363 	int result;
364 
365 	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
366 
367 	return (result == 0);
368 }
369 
370