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/bl_common.h>
13 #include <common/debug.h>
14 #include <drivers/io/io_driver.h>
15 #include <drivers/io/io_fip.h>
16 #include <drivers/io/io_memmap.h>
17 #include <tools_share/firmware_image_package.h>
18 
19 /* Semihosting filenames */
20 #define BL2_IMAGE_NAME			"bl2.bin"
21 #define BL31_IMAGE_NAME			"bl31.bin"
22 #define BL32_IMAGE_NAME			"bl32.bin"
23 #define BL33_IMAGE_NAME			"bl33.bin"
24 
25 #if TRUSTED_BOARD_BOOT
26 #define TRUSTED_BOOT_FW_CERT_NAME	"tb_fw.crt"
27 #define TRUSTED_KEY_CERT_NAME		"trusted_key.crt"
28 #define SOC_FW_KEY_CERT_NAME		"soc_fw_key.crt"
29 #define TOS_FW_KEY_CERT_NAME		"tos_fw_key.crt"
30 #define NT_FW_KEY_CERT_NAME		"nt_fw_key.crt"
31 #define SOC_FW_CONTENT_CERT_NAME	"soc_fw_content.crt"
32 #define TOS_FW_CONTENT_CERT_NAME	"tos_fw_content.crt"
33 #define NT_FW_CONTENT_CERT_NAME		"nt_fw_content.crt"
34 #endif /* TRUSTED_BOARD_BOOT */
35 
36 /* IO devices */
37 static const io_dev_connector_t *fip_dev_con;
38 static uintptr_t fip_dev_handle;
39 static const io_dev_connector_t *memmap_dev_con;
40 static uintptr_t memmap_dev_handle;
41 
42 static const io_block_spec_t fip_block_spec = {
43 	.offset = PLAT_RPI3_FIP_BASE,
44 	.length = PLAT_RPI3_FIP_MAX_SIZE
45 };
46 
47 static const io_uuid_spec_t bl2_uuid_spec = {
48 	.uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
49 };
50 
51 static const io_uuid_spec_t bl31_uuid_spec = {
52 	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
53 };
54 
55 static const io_uuid_spec_t bl32_uuid_spec = {
56 	.uuid = UUID_SECURE_PAYLOAD_BL32,
57 };
58 
59 static const io_uuid_spec_t bl32_extra1_uuid_spec = {
60 	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
61 };
62 
63 static const io_uuid_spec_t bl32_extra2_uuid_spec = {
64 	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
65 };
66 
67 static const io_uuid_spec_t bl33_uuid_spec = {
68 	.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
69 };
70 
71 #if TRUSTED_BOARD_BOOT
72 static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
73 	.uuid = UUID_TRUSTED_BOOT_FW_CERT,
74 };
75 
76 static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
77 	.uuid = UUID_TRUSTED_KEY_CERT,
78 };
79 
80 static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
81 	.uuid = UUID_SOC_FW_KEY_CERT,
82 };
83 
84 static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
85 	.uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
86 };
87 
88 static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
89 	.uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
90 };
91 
92 static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
93 	.uuid = UUID_SOC_FW_CONTENT_CERT,
94 };
95 
96 static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
97 	.uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
98 };
99 
100 static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
101 	.uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
102 };
103 #endif /* TRUSTED_BOARD_BOOT */
104 
105 static int open_fip(const uintptr_t spec);
106 static int open_memmap(const uintptr_t spec);
107 
108 struct plat_io_policy {
109 	uintptr_t *dev_handle;
110 	uintptr_t image_spec;
111 	int (*check)(const uintptr_t spec);
112 };
113 
114 /* By default, load images from the FIP */
115 static const struct plat_io_policy policies[] = {
116 	[FIP_IMAGE_ID] = {
117 		&memmap_dev_handle,
118 		(uintptr_t)&fip_block_spec,
119 		open_memmap
120 	},
121 	[BL2_IMAGE_ID] = {
122 		&fip_dev_handle,
123 		(uintptr_t)&bl2_uuid_spec,
124 		open_fip
125 	},
126 	[BL31_IMAGE_ID] = {
127 		&fip_dev_handle,
128 		(uintptr_t)&bl31_uuid_spec,
129 		open_fip
130 	},
131 	[BL32_IMAGE_ID] = {
132 		&fip_dev_handle,
133 		(uintptr_t)&bl32_uuid_spec,
134 		open_fip
135 	},
136 	[BL32_EXTRA1_IMAGE_ID] = {
137 		&fip_dev_handle,
138 		(uintptr_t)&bl32_extra1_uuid_spec,
139 		open_fip
140 	},
141 	[BL32_EXTRA2_IMAGE_ID] = {
142 		&fip_dev_handle,
143 		(uintptr_t)&bl32_extra2_uuid_spec,
144 		open_fip
145 	},
146 	[BL33_IMAGE_ID] = {
147 		&fip_dev_handle,
148 		(uintptr_t)&bl33_uuid_spec,
149 		open_fip
150 	},
151 #if TRUSTED_BOARD_BOOT
152 	[TRUSTED_BOOT_FW_CERT_ID] = {
153 		&fip_dev_handle,
154 		(uintptr_t)&tb_fw_cert_uuid_spec,
155 		open_fip
156 	},
157 	[TRUSTED_KEY_CERT_ID] = {
158 		&fip_dev_handle,
159 		(uintptr_t)&trusted_key_cert_uuid_spec,
160 		open_fip
161 	},
162 	[SOC_FW_KEY_CERT_ID] = {
163 		&fip_dev_handle,
164 		(uintptr_t)&soc_fw_key_cert_uuid_spec,
165 		open_fip
166 	},
167 	[TRUSTED_OS_FW_KEY_CERT_ID] = {
168 		&fip_dev_handle,
169 		(uintptr_t)&tos_fw_key_cert_uuid_spec,
170 		open_fip
171 	},
172 	[NON_TRUSTED_FW_KEY_CERT_ID] = {
173 		&fip_dev_handle,
174 		(uintptr_t)&nt_fw_key_cert_uuid_spec,
175 		open_fip
176 	},
177 	[SOC_FW_CONTENT_CERT_ID] = {
178 		&fip_dev_handle,
179 		(uintptr_t)&soc_fw_cert_uuid_spec,
180 		open_fip
181 	},
182 	[TRUSTED_OS_FW_CONTENT_CERT_ID] = {
183 		&fip_dev_handle,
184 		(uintptr_t)&tos_fw_cert_uuid_spec,
185 		open_fip
186 	},
187 	[NON_TRUSTED_FW_CONTENT_CERT_ID] = {
188 		&fip_dev_handle,
189 		(uintptr_t)&nt_fw_cert_uuid_spec,
190 		open_fip
191 	},
192 #endif /* TRUSTED_BOARD_BOOT */
193 };
194 
open_fip(const uintptr_t spec)195 static int open_fip(const uintptr_t spec)
196 {
197 	int result;
198 	uintptr_t local_image_handle;
199 
200 	/* See if a Firmware Image Package is available */
201 	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
202 	if (result == 0) {
203 		result = io_open(fip_dev_handle, spec, &local_image_handle);
204 		if (result == 0) {
205 			VERBOSE("Using FIP\n");
206 			io_close(local_image_handle);
207 		}
208 	}
209 	return result;
210 }
211 
open_memmap(const uintptr_t spec)212 static int open_memmap(const uintptr_t spec)
213 {
214 	int result;
215 	uintptr_t local_image_handle;
216 
217 	result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
218 	if (result == 0) {
219 		result = io_open(memmap_dev_handle, spec, &local_image_handle);
220 		if (result == 0) {
221 			VERBOSE("Using Memmap\n");
222 			io_close(local_image_handle);
223 		}
224 	}
225 	return result;
226 }
227 
plat_rpi3_io_setup(void)228 void plat_rpi3_io_setup(void)
229 {
230 	int io_result;
231 
232 	io_result = register_io_dev_fip(&fip_dev_con);
233 	assert(io_result == 0);
234 
235 	io_result = register_io_dev_memmap(&memmap_dev_con);
236 	assert(io_result == 0);
237 
238 	/* Open connections to devices and cache the handles */
239 	io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
240 				&fip_dev_handle);
241 	assert(io_result == 0);
242 
243 	io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
244 				&memmap_dev_handle);
245 	assert(io_result == 0);
246 
247 	/* Ignore improbable errors in release builds */
248 	(void)io_result;
249 }
250 
251 /*
252  * Return an IO device handle and specification which can be used to access
253  * an image. Use this to enforce platform load policy
254  */
plat_get_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)255 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
256 			  uintptr_t *image_spec)
257 {
258 	int result;
259 	const struct plat_io_policy *policy;
260 
261 	assert(image_id < ARRAY_SIZE(policies));
262 
263 	policy = &policies[image_id];
264 	result = policy->check(policy->image_spec);
265 	if (result == 0) {
266 		*image_spec = policy->image_spec;
267 		*dev_handle = *(policy->dev_handle);
268 	}
269 
270 	return result;
271 }
272