1 /*
2  * Copyright (c) 2015-2020, Renesas Electronics Corporation. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <string.h>
8 
9 #include <common/debug.h>
10 #include <drivers/io/io_driver.h>
11 #include <drivers/io/io_storage.h>
12 #include <drivers/io/io_semihosting.h>
13 
14 #include "io_common.h"
15 #include "io_memdrv.h"
16 #include "io_emmcdrv.h"
17 #include "io_private.h"
18 #include "io_rcar.h"
19 #include <platform_def.h>
20 
21 static uintptr_t emmcdrv_dev_handle;
22 static uintptr_t memdrv_dev_handle;
23 static uintptr_t rcar_dev_handle;
24 
25 static uintptr_t boot_io_drv_id;
26 
27 static const io_block_spec_t rcar_block_spec = {
28 	.offset = FLASH0_BASE,
29 	.length = FLASH0_SIZE
30 };
31 
32 static const io_block_spec_t bl2_file_spec = {
33 	.offset = BL2_IMAGE_ID,
34 };
35 
36 static const io_block_spec_t bl31_file_spec = {
37 	.offset = BL31_IMAGE_ID,
38 };
39 
40 static const io_block_spec_t bl32_file_spec = {
41 	.offset = BL32_IMAGE_ID,
42 };
43 
44 static const io_block_spec_t bl33_file_spec = {
45 	.offset = BL33_IMAGE_ID,
46 };
47 
48 static const io_block_spec_t bl332_file_spec = {
49 	.offset = BL332_IMAGE_ID,
50 };
51 
52 static const io_block_spec_t bl333_file_spec = {
53 	.offset = BL333_IMAGE_ID,
54 };
55 
56 static const io_block_spec_t bl334_file_spec = {
57 	.offset = BL334_IMAGE_ID,
58 };
59 
60 static const io_block_spec_t bl335_file_spec = {
61 	.offset = BL335_IMAGE_ID,
62 };
63 
64 static const io_block_spec_t bl336_file_spec = {
65 	.offset = BL336_IMAGE_ID,
66 };
67 
68 static const io_block_spec_t bl337_file_spec = {
69 	.offset = BL337_IMAGE_ID,
70 };
71 
72 static const io_block_spec_t bl338_file_spec = {
73 	.offset = BL338_IMAGE_ID,
74 };
75 
76 #if TRUSTED_BOARD_BOOT
77 static const io_block_spec_t trusted_key_cert_file_spec = {
78 	.offset = TRUSTED_KEY_CERT_ID,
79 };
80 
81 static const io_block_spec_t bl31_key_cert_file_spec = {
82 	.offset = SOC_FW_KEY_CERT_ID,
83 };
84 
85 static const io_block_spec_t bl32_key_cert_file_spec = {
86 	.offset = TRUSTED_OS_FW_KEY_CERT_ID,
87 };
88 
89 static const io_block_spec_t bl33_key_cert_file_spec = {
90 	.offset = NON_TRUSTED_FW_KEY_CERT_ID,
91 };
92 
93 static const io_block_spec_t bl332_key_cert_file_spec = {
94 	.offset = BL332_KEY_CERT_ID,
95 };
96 
97 static const io_block_spec_t bl333_key_cert_file_spec = {
98 	.offset = BL333_KEY_CERT_ID,
99 };
100 
101 static const io_block_spec_t bl334_key_cert_file_spec = {
102 	.offset = BL334_KEY_CERT_ID,
103 };
104 
105 static const io_block_spec_t bl335_key_cert_file_spec = {
106 	.offset = BL335_KEY_CERT_ID,
107 };
108 
109 static const io_block_spec_t bl336_key_cert_file_spec = {
110 	.offset = BL336_KEY_CERT_ID,
111 };
112 
113 static const io_block_spec_t bl337_key_cert_file_spec = {
114 	.offset = BL337_KEY_CERT_ID,
115 };
116 
117 static const io_block_spec_t bl338_key_cert_file_spec = {
118 	.offset = BL338_KEY_CERT_ID,
119 };
120 
121 static const io_block_spec_t bl31_cert_file_spec = {
122 	.offset = SOC_FW_CONTENT_CERT_ID,
123 };
124 
125 static const io_block_spec_t bl32_cert_file_spec = {
126 	.offset = TRUSTED_OS_FW_CONTENT_CERT_ID,
127 };
128 
129 static const io_block_spec_t bl33_cert_file_spec = {
130 	.offset = NON_TRUSTED_FW_CONTENT_CERT_ID,
131 };
132 
133 static const io_block_spec_t bl332_cert_file_spec = {
134 	.offset = BL332_CERT_ID,
135 };
136 
137 static const io_block_spec_t bl333_cert_file_spec = {
138 	.offset = BL333_CERT_ID,
139 };
140 
141 static const io_block_spec_t bl334_cert_file_spec = {
142 	.offset = BL334_CERT_ID,
143 };
144 
145 static const io_block_spec_t bl335_cert_file_spec = {
146 	.offset = BL335_CERT_ID,
147 };
148 
149 static const io_block_spec_t bl336_cert_file_spec = {
150 	.offset = BL336_CERT_ID,
151 };
152 
153 static const io_block_spec_t bl337_cert_file_spec = {
154 	.offset = BL337_CERT_ID,
155 };
156 
157 static const io_block_spec_t bl338_cert_file_spec = {
158 	.offset = BL338_CERT_ID,
159 };
160 #endif
161 
162 static int32_t open_emmcdrv(const uintptr_t spec);
163 static int32_t open_memmap(const uintptr_t spec);
164 static int32_t open_rcar(const uintptr_t spec);
165 
166 struct plat_io_policy {
167 	uintptr_t *dev_handle;
168 	uintptr_t image_spec;
169 	int32_t (*check)(const uintptr_t spec);
170 };
171 
172 static const struct plat_io_policy policies[] = {
173 	[FIP_IMAGE_ID] = {
174 			  &memdrv_dev_handle,
175 			  (uintptr_t) &rcar_block_spec,
176 			  &open_memmap},
177 	[BL2_IMAGE_ID] = {
178 			  &rcar_dev_handle,
179 			  (uintptr_t) &bl2_file_spec,
180 			  &open_rcar},
181 	[BL31_IMAGE_ID] = {
182 			   &rcar_dev_handle,
183 			   (uintptr_t) &bl31_file_spec,
184 			   &open_rcar},
185 	[BL32_IMAGE_ID] = {
186 			   &rcar_dev_handle,
187 			   (uintptr_t) &bl32_file_spec,
188 			   &open_rcar},
189 	[BL33_IMAGE_ID] = {
190 			   &rcar_dev_handle,
191 			   (uintptr_t) &bl33_file_spec,
192 			   &open_rcar},
193 	[BL332_IMAGE_ID] = {
194 			    &rcar_dev_handle,
195 			    (uintptr_t) &bl332_file_spec,
196 			    &open_rcar},
197 	[BL333_IMAGE_ID] = {
198 			    &rcar_dev_handle,
199 			    (uintptr_t) &bl333_file_spec,
200 			    &open_rcar},
201 	[BL334_IMAGE_ID] = {
202 			    &rcar_dev_handle,
203 			    (uintptr_t) &bl334_file_spec,
204 			    &open_rcar},
205 	[BL335_IMAGE_ID] = {
206 			    &rcar_dev_handle,
207 			    (uintptr_t) &bl335_file_spec,
208 			    &open_rcar},
209 	[BL336_IMAGE_ID] = {
210 			    &rcar_dev_handle,
211 			    (uintptr_t) &bl336_file_spec,
212 			    &open_rcar},
213 	[BL337_IMAGE_ID] = {
214 			    &rcar_dev_handle,
215 			    (uintptr_t) &bl337_file_spec,
216 			    &open_rcar},
217 	[BL338_IMAGE_ID] = {
218 			    &rcar_dev_handle,
219 			    (uintptr_t) &bl338_file_spec,
220 			    &open_rcar},
221 #if TRUSTED_BOARD_BOOT
222 	[TRUSTED_KEY_CERT_ID] = {
223 				 &rcar_dev_handle,
224 				 (uintptr_t) &trusted_key_cert_file_spec,
225 				 &open_rcar},
226 	[SOC_FW_KEY_CERT_ID] = {
227 				&rcar_dev_handle,
228 				(uintptr_t) &bl31_key_cert_file_spec,
229 				&open_rcar},
230 	[TRUSTED_OS_FW_KEY_CERT_ID] = {
231 				       &rcar_dev_handle,
232 				       (uintptr_t) &bl32_key_cert_file_spec,
233 				       &open_rcar},
234 	[NON_TRUSTED_FW_KEY_CERT_ID] = {
235 					&rcar_dev_handle,
236 					(uintptr_t) &bl33_key_cert_file_spec,
237 					&open_rcar},
238 	[BL332_KEY_CERT_ID] = {
239 			       &rcar_dev_handle,
240 			       (uintptr_t) &bl332_key_cert_file_spec,
241 			       &open_rcar},
242 	[BL333_KEY_CERT_ID] = {
243 			       &rcar_dev_handle,
244 			       (uintptr_t) &bl333_key_cert_file_spec,
245 			       &open_rcar},
246 	[BL334_KEY_CERT_ID] = {
247 			       &rcar_dev_handle,
248 			       (uintptr_t) &bl334_key_cert_file_spec,
249 			       &open_rcar},
250 	[BL335_KEY_CERT_ID] = {
251 			       &rcar_dev_handle,
252 			       (uintptr_t) &bl335_key_cert_file_spec,
253 			       &open_rcar},
254 	[BL336_KEY_CERT_ID] = {
255 			       &rcar_dev_handle,
256 			       (uintptr_t) &bl336_key_cert_file_spec,
257 			       &open_rcar},
258 	[BL337_KEY_CERT_ID] = {
259 			       &rcar_dev_handle,
260 			       (uintptr_t) &bl337_key_cert_file_spec,
261 			       &open_rcar},
262 	[BL338_KEY_CERT_ID] = {
263 			       &rcar_dev_handle,
264 			       (uintptr_t) &bl338_key_cert_file_spec,
265 			       &open_rcar},
266 	[SOC_FW_CONTENT_CERT_ID] = {
267 				    &rcar_dev_handle,
268 				    (uintptr_t) &bl31_cert_file_spec,
269 				    &open_rcar},
270 	[TRUSTED_OS_FW_CONTENT_CERT_ID] = {
271 					   &rcar_dev_handle,
272 					   (uintptr_t) &bl32_cert_file_spec,
273 					   &open_rcar},
274 	[NON_TRUSTED_FW_CONTENT_CERT_ID] = {
275 					    &rcar_dev_handle,
276 					    (uintptr_t) &bl33_cert_file_spec,
277 					    &open_rcar},
278 	[BL332_CERT_ID] = {
279 			   &rcar_dev_handle,
280 			   (uintptr_t) &bl332_cert_file_spec,
281 			   &open_rcar},
282 	[BL333_CERT_ID] = {
283 			   &rcar_dev_handle,
284 			   (uintptr_t) &bl333_cert_file_spec,
285 			   &open_rcar},
286 	[BL334_CERT_ID] = {
287 			   &rcar_dev_handle,
288 			   (uintptr_t) &bl334_cert_file_spec,
289 			   &open_rcar},
290 	[BL335_CERT_ID] = {
291 			   &rcar_dev_handle,
292 			   (uintptr_t) &bl335_cert_file_spec,
293 			   &open_rcar},
294 	[BL336_CERT_ID] = {
295 			   &rcar_dev_handle,
296 			   (uintptr_t) &bl336_cert_file_spec,
297 			   &open_rcar},
298 	[BL337_CERT_ID] = {
299 			   &rcar_dev_handle,
300 			   (uintptr_t) &bl337_cert_file_spec,
301 			   &open_rcar},
302 	[BL338_CERT_ID] = {
303 			   &rcar_dev_handle,
304 			   (uintptr_t) &bl338_cert_file_spec,
305 			   &open_rcar}, {
306 #else
307 					{
308 #endif
309 					 0, 0, 0}
310 };
311 
312 static io_drv_spec_t io_drv_spec_memdrv = {
313 	FLASH0_BASE,
314 	FLASH0_SIZE,
315 	0,
316 };
317 
318 static io_drv_spec_t io_drv_spec_emmcdrv = {
319 	0,
320 	0,
321 	0,
322 };
323 
324 static struct plat_io_policy drv_policies[] __attribute__ ((section(".data"))) = {
325 	/* FLASH_DEV_ID */
326 	{ &memdrv_dev_handle, (uintptr_t) &io_drv_spec_memdrv, &open_memmap, },
327 	/* EMMC_DEV_ID */
328 	{ &emmcdrv_dev_handle, (uintptr_t) &io_drv_spec_emmcdrv, &open_emmcdrv, }
329 };
330 
open_rcar(const uintptr_t spec)331 static int32_t open_rcar(const uintptr_t spec)
332 {
333 	return io_dev_init(rcar_dev_handle, boot_io_drv_id);
334 }
335 
open_memmap(const uintptr_t spec)336 static int32_t open_memmap(const uintptr_t spec)
337 {
338 	uintptr_t handle;
339 	int32_t result;
340 
341 	result = io_dev_init(memdrv_dev_handle, 0);
342 	if (result != IO_SUCCESS)
343 		return result;
344 
345 	result = io_open(memdrv_dev_handle, spec, &handle);
346 	if (result == IO_SUCCESS)
347 		io_close(handle);
348 
349 	return result;
350 }
351 
open_emmcdrv(const uintptr_t spec)352 static int32_t open_emmcdrv(const uintptr_t spec)
353 {
354 	return io_dev_init(emmcdrv_dev_handle, 0);
355 }
356 
rcar_io_setup(void)357 void rcar_io_setup(void)
358 {
359 	const io_dev_connector_t *memmap;
360 	const io_dev_connector_t *rcar;
361 
362 	boot_io_drv_id = FLASH_DEV_ID;
363 
364 	rcar_register_io_dev(&rcar);
365 	rcar_register_io_dev_memdrv(&memmap);
366 	io_dev_open(rcar, 0, &rcar_dev_handle);
367 	io_dev_open(memmap, 0, &memdrv_dev_handle);
368 }
369 
rcar_io_emmc_setup(void)370 void rcar_io_emmc_setup(void)
371 {
372 	const io_dev_connector_t *rcar;
373 	const io_dev_connector_t *emmc;
374 
375 	boot_io_drv_id = EMMC_DEV_ID;
376 
377 	rcar_register_io_dev(&rcar);
378 	rcar_register_io_dev_emmcdrv(&emmc);
379 	io_dev_open(rcar, 0, &rcar_dev_handle);
380 	io_dev_open(emmc, 0, &emmcdrv_dev_handle);
381 }
382 
plat_get_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)383 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
384 			  uintptr_t *image_spec)
385 {
386 	const struct plat_io_policy *policy;
387 	int result;
388 
389 	policy = &policies[image_id];
390 
391 	result = policy->check(policy->image_spec);
392 	if (result != IO_SUCCESS)
393 		return result;
394 
395 	*image_spec = policy->image_spec;
396 	*dev_handle = *(policy->dev_handle);
397 
398 	return IO_SUCCESS;
399 }
400 
plat_get_drv_source(uint32_t io_drv_id,uintptr_t * dev_handle,uintptr_t * image_spec)401 int32_t plat_get_drv_source(uint32_t io_drv_id, uintptr_t *dev_handle,
402 			    uintptr_t *image_spec)
403 {
404 	const struct plat_io_policy *policy;
405 	int32_t result;
406 
407 	policy = &drv_policies[io_drv_id];
408 
409 	result = policy->check(policy->image_spec);
410 	if (result != IO_SUCCESS)
411 		return result;
412 
413 	*image_spec = policy->image_spec;
414 	*dev_handle = *(policy->dev_handle);
415 
416 	return IO_SUCCESS;
417 }
418