xref: /rk3399_ARM-atf/drivers/renesas/common/io/io_rcar.c (revision 11665772b3ae0b1c2e64c69504116acc6bd641b3)
1 /*
2  * Copyright (c) 2015-2023, Renesas Electronics Corporation. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <errno.h>
8 #include <stdint.h>
9 #include <string.h>
10 
11 #include <arch_helpers.h>
12 #include <common/bl_common.h>
13 #include <common/debug.h>
14 #include <drivers/auth/auth_mod.h>
15 #include <drivers/io/io_driver.h>
16 #include <drivers/io/io_storage.h>
17 #include <lib/mmio.h>
18 #include <plat/common/platform.h>
19 #include <tools_share/firmware_image_package.h>
20 #include <tools_share/uuid.h>
21 
22 #include "io_rcar.h"
23 #include "io_common.h"
24 #include "io_private.h"
25 #include <platform_def.h>
26 
27 extern int32_t plat_get_drv_source(uint32_t id, uintptr_t *dev,
28 				   uintptr_t *image_spec);
29 
30 static int32_t rcar_dev_open(const uintptr_t dev_spec __attribute__ ((unused)),
31 			     io_dev_info_t **dev_info);
32 static int32_t rcar_dev_close(io_dev_info_t *dev_info);
33 
34 typedef struct {
35 	const int32_t name;
36 	const uint32_t offset;
37 	const uint32_t attr;
38 } plat_rcar_name_offset_t;
39 
40 typedef struct {
41 	/*
42 	 * Put position above the struct to allow {0} on static init.
43 	 * It is a workaround for a known bug in GCC
44 	 * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53119
45 	 */
46 	uint32_t position;
47 	uint32_t no_load;
48 	uintptr_t offset;
49 	uint32_t size;
50 	uintptr_t dst;
51 	uintptr_t partition;	/* for eMMC */
52 	/* RCAR_EMMC_PARTITION_BOOT_0 */
53 	/* RCAR_EMMC_PARTITION_BOOT_1 */
54 	/* RCAR_EMMC_PARTITION_USER   */
55 } file_state_t;
56 
57 #define RCAR_GET_FLASH_ADR(a, b)	((uint32_t)((0x40000U * (a)) + (b)))
58 #define RCAR_ATTR_SET_CALCADDR(a)	((a) & 0xF)
59 #define RCAR_ATTR_SET_ISNOLOAD(a)	(((a) & 0x1) << 16U)
60 #define RCAR_ATTR_SET_CERTOFF(a)	(((a) & 0xF) << 8U)
61 #define RCAR_ATTR_SET_ALL(a, b, c)	((uint32_t)(RCAR_ATTR_SET_CALCADDR(a) |\
62 					RCAR_ATTR_SET_ISNOLOAD(b) |\
63 					RCAR_ATTR_SET_CERTOFF(c)))
64 
65 #define RCAR_ATTR_GET_CALCADDR(a)	((a) & 0xFU)
66 #define RCAR_ATTR_GET_ISNOLOAD(a)	(((a) >> 16) & 0x1U)
67 #define RCAR_ATTR_GET_CERTOFF(a)	((uint32_t)(((a) >> 8) & 0xFU))
68 
69 #define RCAR_MAX_BL3X_IMAGE		(8U)
70 #define RCAR_SECTOR6_CERT_OFFSET	(0x400U)
71 #define RCAR_SDRAM_certESS		(0x43F00000U)
72 #define RCAR_CERT_SIZE			(0x800U)
73 #define RCAR_CERT_INFO_SIZE_OFFSET	(0x264U)
74 #define RCAR_CERT_INFO_DST_OFFSET	(0x154U)
75 #define RCAR_CERT_INFO_SIZE_OFFSET1	(0x364U)
76 #define RCAR_CERT_INFO_DST_OFFSET1	(0x1D4U)
77 #define RCAR_CERT_INFO_SIZE_OFFSET2	(0x464U)
78 #define RCAR_CERT_INFO_DST_OFFSET2	(0x254U)
79 #define RCAR_CERT_LOAD			(1U)
80 
81 #define RCAR_FLASH_CERT_HEADER		RCAR_GET_FLASH_ADR(6U, 0U)
82 #define RCAR_EMMC_CERT_HEADER		(0x00030000U)
83 
84 #define RCAR_COUNT_LOAD_BL33		(2U)
85 #define RCAR_COUNT_LOAD_BL33X		(3U)
86 
87 #define CHECK_IMAGE_AREA_CNT (7U)
88 #define BOOT_BL2_ADDR (0xE6304000U)
89 #define BOOT_BL2_LENGTH (0x19000U)
90 
91 typedef struct {
92 	uintptr_t dest;
93 	uintptr_t length;
94 } addr_loaded_t;
95 
96 static addr_loaded_t addr_loaded[CHECK_IMAGE_AREA_CNT] = {
97 	[0] = {BOOT_BL2_ADDR, BOOT_BL2_LENGTH},
98 	[1] = {BL31_BASE, RCAR_TRUSTED_SRAM_SIZE},
99 #ifndef SPD_NONE
100 	[2] = {BL32_BASE, BL32_SIZE}
101 #endif
102 };
103 
104 #ifndef SPD_NONE
105 static uint32_t addr_loaded_cnt = 3;
106 #else
107 static uint32_t addr_loaded_cnt = 2;
108 #endif
109 
110 static const plat_rcar_name_offset_t name_offset[] = {
111 	{BL31_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(0, 0, 0)},
112 
113 	/* BL3-2 is optional in the platform */
114 	{BL32_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(1, 0, 1)},
115 	{BL33_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(2, 0, 2)},
116 	{BL332_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(3, 0, 3)},
117 	{BL333_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(4, 0, 4)},
118 	{BL334_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(5, 0, 5)},
119 	{BL335_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(6, 0, 6)},
120 	{BL336_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(7, 0, 7)},
121 	{BL337_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(8, 0, 8)},
122 	{BL338_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(9, 0, 9)},
123 };
124 
125 #if TRUSTED_BOARD_BOOT
126 static const plat_rcar_name_offset_t cert_offset[] = {
127 	/* Certificates */
128 	{TRUSTED_KEY_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 0)},
129 	{SOC_FW_KEY_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 0)},
130 	{TRUSTED_OS_FW_KEY_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 0)},
131 	{NON_TRUSTED_FW_KEY_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 0)},
132 	{SOC_FW_CONTENT_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 0)},
133 	{TRUSTED_OS_FW_CONTENT_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 1)},
134 	{NON_TRUSTED_FW_CONTENT_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 2)},
135 	{BL332_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 3)},
136 	{BL333_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 4)},
137 	{BL334_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 5)},
138 	{BL335_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 6)},
139 	{BL336_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 7)},
140 	{BL337_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 8)},
141 	{BL338_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 9)},
142 };
143 #endif /* TRUSTED_BOARD_BOOT */
144 
145 static file_state_t current_file = { 0 };
146 
147 static uintptr_t rcar_handle, rcar_spec;
148 static uint64_t rcar_image_header[RCAR_MAX_BL3X_IMAGE + 2U] = { 0U };
149 static uint64_t rcar_image_header_prttn[RCAR_MAX_BL3X_IMAGE + 2U] = { 0U };
150 static uint64_t rcar_image_number = { 0U };
151 static uint32_t rcar_cert_load = { 0U };
152 #if (RCAR_RPC_HYPERFLASH_ABLOADER == 1)
153 static uint32_t rcar_image_offset = 0U;
154 #endif
155 
device_type_rcar(void)156 static io_type_t device_type_rcar(void)
157 {
158 	return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
159 }
160 
rcar_get_certificate(const int32_t name,uint32_t * cert)161 int32_t rcar_get_certificate(const int32_t name, uint32_t *cert)
162 {
163 #if TRUSTED_BOARD_BOOT
164 	int32_t i;
165 
166 	for (i = 0; i < ARRAY_SIZE(cert_offset); i++) {
167 		if (name != cert_offset[i].name) {
168 			continue;
169 		}
170 
171 		*cert = RCAR_CERT_SIZE;
172 		*cert *= RCAR_ATTR_GET_CERTOFF(cert_offset[i].attr);
173 		*cert += RCAR_SDRAM_certESS;
174 		return 0;
175 	}
176 #endif
177 	return -EINVAL;
178 }
179 
180 #define MFISBTSTSR			(0xE6260604U)
181 #define MFISBTSTSR_BOOT_PARTITION	(0x00000010U)
182 
file_to_offset(const int32_t name,uintptr_t * offset,uint32_t * cert,uint32_t * no_load,uintptr_t * partition)183 static int32_t file_to_offset(const int32_t name, uintptr_t *offset,
184 			      uint32_t *cert, uint32_t *no_load,
185 			      uintptr_t *partition)
186 {
187 	uint32_t addr;
188 	int32_t i;
189 
190 	for (i = 0; i < ARRAY_SIZE(name_offset); i++) {
191 		if (name != name_offset[i].name) {
192 			continue;
193 		}
194 
195 		addr = RCAR_ATTR_GET_CALCADDR(name_offset[i].attr);
196 		if (rcar_image_number + 2U < addr) {
197 			continue;
198 		}
199 
200 		*offset = rcar_image_header[addr];
201 
202 #if (RCAR_RPC_HYPERFLASH_ABLOADER == 1)
203 		*offset += rcar_image_offset;
204 #endif
205 
206 		*cert = RCAR_CERT_SIZE;
207 		*cert *= RCAR_ATTR_GET_CERTOFF(name_offset[i].attr);
208 		*cert += RCAR_SDRAM_certESS;
209 		*no_load = RCAR_ATTR_GET_ISNOLOAD(name_offset[i].attr);
210 		*partition = rcar_image_header_prttn[addr];
211 		return IO_SUCCESS;
212 	}
213 
214 #if TRUSTED_BOARD_BOOT
215 	for (i = 0; i < ARRAY_SIZE(cert_offset); i++) {
216 		if (name != cert_offset[i].name) {
217 			continue;
218 		}
219 
220 		*no_load = RCAR_ATTR_GET_ISNOLOAD(cert_offset[i].attr);
221 		*partition = 0U;
222 		*offset = 0U;
223 		*cert = 0U;
224 		return IO_SUCCESS;
225 	}
226 #endif
227 	return -EINVAL;
228 }
229 
230 #define RCAR_BOOT_KEY_CERT_NEW	(0xE6300F00U)
231 #define	RCAR_CERT_MAGIC_NUM	(0xE291F358U)
232 
rcar_read_certificate(uint64_t cert,uint32_t * len,uintptr_t * dst)233 void rcar_read_certificate(uint64_t cert, uint32_t *len, uintptr_t *dst)
234 {
235 	uint32_t seed, val, info_1, info_2;
236 	uintptr_t size, dsth, dstl;
237 
238 	cert &= 0xFFFFFFFFU;
239 
240 	seed = mmio_read_32(RCAR_BOOT_KEY_CERT_NEW);
241 	val = mmio_read_32(RCAR_BOOT_KEY_CERT_NEW + 0xC);
242 	info_1 = (val >> 18) & 0x3U;
243 	val = mmio_read_32(cert + 0xC);
244 	info_2 = (val >> 21) & 0x3;
245 
246 	if (seed == RCAR_CERT_MAGIC_NUM) {
247 		if (info_1 != 1) {
248 			ERROR("BL2: Cert is invalid.\n");
249 			*dst = 0;
250 			*len = 0;
251 			return;
252 		}
253 
254 		if (info_2 > 2) {
255 			ERROR("BL2: Cert is invalid.\n");
256 			*dst = 0;
257 			*len = 0;
258 			return;
259 		}
260 
261 		switch (info_2) {
262 		case 2:
263 			size = cert + RCAR_CERT_INFO_SIZE_OFFSET2;
264 			dstl = cert + RCAR_CERT_INFO_DST_OFFSET2;
265 			break;
266 		case 1:
267 			size = cert + RCAR_CERT_INFO_SIZE_OFFSET1;
268 			dstl = cert + RCAR_CERT_INFO_DST_OFFSET1;
269 			break;
270 		case 0:
271 			size = cert + RCAR_CERT_INFO_SIZE_OFFSET;
272 			dstl = cert + RCAR_CERT_INFO_DST_OFFSET;
273 			break;
274 		}
275 		val = mmio_read_32(size);
276 		if (val > (UINT32_MAX / 4)) {
277 			ERROR("BL2: %s[%d] uint32 overflow!\n",
278 				__func__, __LINE__);
279 			*dst = 0;
280 			*len = 0;
281 			return;
282 		}
283 
284 		*len = val * 4U;
285 		dsth = dstl + 4U;
286 		*dst = ((uintptr_t) mmio_read_32(dsth) << 32) +
287 		    ((uintptr_t) mmio_read_32(dstl));
288 		return;
289 	}
290 
291 	size = cert + RCAR_CERT_INFO_SIZE_OFFSET;
292 	val = mmio_read_32(size);
293 	if (val > (UINT32_MAX / 4)) {
294 		ERROR("BL2: %s[%d] uint32 overflow!\n", __func__, __LINE__);
295 		*dst = 0;
296 		*len = 0;
297 		return;
298 	}
299 	*len = val * 4U;
300 	dstl = cert + RCAR_CERT_INFO_DST_OFFSET;
301 	dsth = dstl + 4U;
302 	*dst = ((uintptr_t) mmio_read_32(dsth) << 32) +
303 	    ((uintptr_t) mmio_read_32(dstl));
304 }
305 
check_load_area(uintptr_t dst,uintptr_t len)306 static int32_t check_load_area(uintptr_t dst, uintptr_t len)
307 {
308 	uint32_t legacy = dst + len <= UINT32_MAX - 1 ? 1 : 0;
309 	uintptr_t dram_start, dram_end;
310 	uintptr_t prot_start, prot_end;
311 	int32_t result = IO_SUCCESS;
312 	int n;
313 
314 	dram_start = legacy ? DRAM1_NS_BASE : DRAM_40BIT_BASE;
315 
316 	dram_end = legacy ? DRAM1_NS_BASE + DRAM1_NS_SIZE :
317 	    DRAM_40BIT_BASE + DRAM_40BIT_SIZE;
318 
319 	prot_start = legacy ? DRAM_PROTECTED_BASE : DRAM_40BIT_PROTECTED_BASE;
320 
321 	prot_end = prot_start + DRAM_PROTECTED_SIZE;
322 
323 	if (dst < dram_start || len > dram_end || dst > dram_end - len) {
324 		ERROR("BL2: dst address is on the protected area.\n");
325 		result = IO_FAIL;
326 		goto done;
327 	}
328 
329 	/* load image is within SDRAM protected area */
330 	if (dst >= prot_start && dst < prot_end) {
331 		ERROR("BL2: dst address is on the protected area.\n");
332 		result = IO_FAIL;
333 		goto done;
334 	}
335 
336 	if (len > prot_start || (dst < prot_start && dst > prot_start - len)) {
337 		ERROR("BL2: %s[%d] loaded data is on the protected area.\n",
338 			__func__, __LINE__);
339 		result = IO_FAIL;
340 		goto done;
341 	}
342 
343 	if (addr_loaded_cnt >= CHECK_IMAGE_AREA_CNT) {
344 		ERROR("BL2: max loadable non secure images reached\n");
345 		result = IO_FAIL;
346 		goto done;
347 	}
348 
349 	addr_loaded[addr_loaded_cnt].dest = dst;
350 	addr_loaded[addr_loaded_cnt].length = len;
351 	for (n = 0; n < addr_loaded_cnt; n++) {
352 		/*
353 		 * Check if next image invades a previous loaded image
354 		 *
355 		 * IMAGE n: area from previous image:	dest| IMAGE n |length
356 		 * IMAGE n+1: area from next image:	dst | IMAGE n |len
357 		 *
358 		 * 1. check:
359 		 *      | IMAGE n |
360 		 *        | IMAGE n+1 |
361 		 * 2. check:
362 		 *      | IMAGE n |
363 		 *  | IMAGE n+1 |
364 		 * 3. check:
365 		 *      | IMAGE n |
366 		 *  |    IMAGE n+1    |
367 		 */
368 		if (((dst >= addr_loaded[n].dest) &&
369 		     (dst <= addr_loaded[n].dest + addr_loaded[n].length)) ||
370 		    ((dst + len >= addr_loaded[n].dest) &&
371 		     (dst + len <= addr_loaded[n].dest + addr_loaded[n].length)) ||
372 		    ((dst <= addr_loaded[n].dest) &&
373 		     (dst + len >= addr_loaded[n].dest + addr_loaded[n].length))) {
374 			ERROR("BL2: next image overlap a previous image area.\n");
375 			result = IO_FAIL;
376 			goto done;
377 		}
378 	}
379 	addr_loaded_cnt++;
380 
381 done:
382 	if (result == IO_FAIL) {
383 		ERROR("BL2: Out of range : dst=0x%lx len=0x%lx\n", dst, len);
384 	}
385 
386 	return result;
387 }
388 
load_bl33x(void)389 static int32_t load_bl33x(void)
390 {
391 	static int32_t loaded = IO_NOT_SUPPORTED;
392 	uintptr_t dst, partition, handle;
393 	uint32_t noload, cert, len, i;
394 	uintptr_t offset;
395 	int32_t rc;
396 	size_t cnt;
397 	const int32_t img[] = {
398 		BL33_IMAGE_ID,
399 		BL332_IMAGE_ID,
400 		BL333_IMAGE_ID,
401 		BL334_IMAGE_ID,
402 		BL335_IMAGE_ID,
403 		BL336_IMAGE_ID,
404 		BL337_IMAGE_ID,
405 		BL338_IMAGE_ID
406 	};
407 
408 	if (loaded != IO_NOT_SUPPORTED) {
409 		return loaded;
410 	}
411 
412 	for (i = 1; i < rcar_image_number; i++) {
413 		rc = file_to_offset(img[i], &offset, &cert, &noload,
414 				    &partition);
415 		if (rc != IO_SUCCESS) {
416 			WARN("%s: failed to get offset\n", __func__);
417 			loaded = IO_FAIL;
418 			return loaded;
419 		}
420 
421 		rcar_read_certificate((uint64_t) cert, &len, &dst);
422 		((io_drv_spec_t *) rcar_spec)->partition = partition;
423 
424 		rc = io_open(rcar_handle, rcar_spec, &handle);
425 		if (rc != IO_SUCCESS) {
426 			WARN("%s: Failed to open FIP (%i)\n", __func__, rc);
427 			loaded = IO_FAIL;
428 			return loaded;
429 		}
430 
431 		rc = io_seek(handle, IO_SEEK_SET, offset);
432 		if (rc != IO_SUCCESS) {
433 			WARN("%s: failed to seek\n", __func__);
434 			loaded = IO_FAIL;
435 			return loaded;
436 		}
437 
438 		rc = check_load_area(dst, len);
439 		if (rc != IO_SUCCESS) {
440 			WARN("%s: check load area\n", __func__);
441 			loaded = IO_FAIL;
442 			return loaded;
443 		}
444 
445 		rc = io_read(handle, dst, len, &cnt);
446 		if (rc != IO_SUCCESS) {
447 			WARN("%s: failed to read\n", __func__);
448 			loaded = IO_FAIL;
449 			return loaded;
450 		}
451 #if TRUSTED_BOARD_BOOT
452 		rc = auth_mod_verify_img(img[i], (void *)dst, len);
453 		if (rc != 0) {
454 			memset((void *)dst, 0x00, len);
455 			loaded = IO_FAIL;
456 			return loaded;
457 		}
458 #endif
459 		io_close(handle);
460 	}
461 
462 	loaded = IO_SUCCESS;
463 
464 	return loaded;
465 }
466 
rcar_dev_init(io_dev_info_t * dev_info,const uintptr_t name)467 static int32_t rcar_dev_init(io_dev_info_t *dev_info, const uintptr_t name)
468 {
469 	static uint64_t header[64] __aligned(FLASH_TRANS_SIZE_UNIT) = {0UL};
470 	uintptr_t handle;
471 	ssize_t offset;
472 	uint32_t i;
473 	int32_t rc;
474 	size_t cnt;
475 
476 	/* Obtain a reference to the image by querying the platform layer */
477 	rc = plat_get_drv_source(name, &rcar_handle, &rcar_spec);
478 	if (rc != IO_SUCCESS) {
479 		WARN("Failed to obtain reference to img %ld (%i)\n", name, rc);
480 		return IO_FAIL;
481 	}
482 
483 	if (rcar_cert_load == RCAR_CERT_LOAD) {
484 		return IO_SUCCESS;
485 	}
486 
487 	rc = io_open(rcar_handle, rcar_spec, &handle);
488 	if (rc != IO_SUCCESS) {
489 		WARN("Failed to access img %ld (%i)\n", name, rc);
490 		return IO_FAIL;
491 	}
492 
493 	/*
494 	 * get start address list
495 	 * [0] address num
496 	 * [1] BL33-1 image address
497 	 * [2] BL33-2 image address
498 	 * [3] BL33-3 image address
499 	 * [4] BL33-4 image address
500 	 * [5] BL33-5 image address
501 	 * [6] BL33-6 image address
502 	 * [7] BL33-7 image address
503 	 * [8] BL33-8 image address
504 	 */
505 	offset = name == EMMC_DEV_ID ? RCAR_EMMC_CERT_HEADER :
506 	    RCAR_FLASH_CERT_HEADER;
507 
508 #if (RCAR_RPC_HYPERFLASH_ABLOADER == 1)
509 	rcar_image_offset = 0;
510 	if ((name == FLASH_DEV_ID) &&
511 	    (mmio_read_32(MFISBTSTSR) & MFISBTSTSR_BOOT_PARTITION)) {
512 		rcar_image_offset = 0x800000;
513 		offset += rcar_image_offset;
514 	}
515 #endif
516 
517 	rc = io_seek(handle, IO_SEEK_SET, offset);
518 	if (rc != IO_SUCCESS) {
519 		WARN("Firmware Image Package header failed to seek\n");
520 		goto error;
521 	}
522 
523 	rc = io_read(handle, (uintptr_t) &header, sizeof(header), &cnt);
524 	if (rc != IO_SUCCESS) {
525 		WARN("Firmware Image Package header failed to read\n");
526 		goto error;
527 	}
528 
529 #if RCAR_BL2_DCACHE == 1
530 	inv_dcache_range((uint64_t) header, sizeof(header));
531 #endif
532 
533 	rcar_image_number = header[0];
534 	if (rcar_image_number == 0 || rcar_image_number > RCAR_MAX_BL3X_IMAGE) {
535 		WARN("Firmware Image Package header check failed.\n");
536 		rc = IO_FAIL;
537 		goto error;
538 	}
539 
540 	for (i = 0; i < rcar_image_number + 2; i++) {
541 		rcar_image_header[i] = header[i * 2 + 1];
542 		rcar_image_header_prttn[i] = header[i * 2 + 2];
543 	}
544 
545 	rc = io_seek(handle, IO_SEEK_SET, offset + RCAR_SECTOR6_CERT_OFFSET);
546 	if (rc != IO_SUCCESS) {
547 		WARN("Firmware Image Package header failed to seek cert\n");
548 		goto error;
549 	}
550 
551 	rc = io_read(handle, RCAR_SDRAM_certESS,
552 		     RCAR_CERT_SIZE * (2 + rcar_image_number), &cnt);
553 	if (rc != IO_SUCCESS) {
554 		WARN("cert file read error.\n");
555 		goto error;
556 	}
557 
558 #if RCAR_BL2_DCACHE == 1
559 	inv_dcache_range(RCAR_SDRAM_certESS,
560 			 RCAR_CERT_SIZE * (2 + rcar_image_number));
561 #endif
562 
563 	rcar_cert_load = RCAR_CERT_LOAD;
564 error:
565 
566 	if (rc != IO_SUCCESS) {
567 		rc = IO_FAIL;
568 	}
569 
570 	io_close(handle);
571 
572 	return rc;
573 
574 }
575 
rcar_file_open(io_dev_info_t * info,const uintptr_t file_spec,io_entity_t * entity)576 static int32_t rcar_file_open(io_dev_info_t *info, const uintptr_t file_spec,
577 			      io_entity_t *entity)
578 {
579 	const io_drv_spec_t *spec = (io_drv_spec_t *) file_spec;
580 	uintptr_t partition, offset, dst;
581 	uint32_t noload, cert, len;
582 	int32_t rc;
583 
584 	/*
585 	 * Only one file open at a time. We need to  track state (ie, file
586 	 * cursor position). Since the header lives at offset zero, this entry
587 	 * should never be zero in an active file.
588 	 * Once the system supports dynamic memory allocation we will allow more
589 	 * than one open file at a time.
590 	 */
591 	if (current_file.offset != 0U) {
592 		WARN("%s: Only one open file at a time.\n", __func__);
593 		return IO_RESOURCES_EXHAUSTED;
594 	}
595 
596 	rc = file_to_offset(spec->offset, &offset, &cert, &noload, &partition);
597 	if (rc != IO_SUCCESS) {
598 		WARN("Failed to open file name %ld (%i)\n", spec->offset, rc);
599 		return IO_FAIL;
600 	}
601 
602 	if (noload != 0U) {
603 		current_file.offset = 1;
604 		current_file.dst = 0;
605 		current_file.size = 1;
606 		current_file.position = 0;
607 		current_file.no_load = noload;
608 		current_file.partition = 0;
609 		entity->info = (uintptr_t) &current_file;
610 
611 		return IO_SUCCESS;
612 	}
613 
614 	rcar_read_certificate((uint64_t) cert, &len, &dst);
615 
616 	current_file.partition = partition;
617 	current_file.no_load = noload;
618 	current_file.offset = offset;
619 	current_file.position = 0;
620 	current_file.size = len;
621 	current_file.dst = dst;
622 	entity->info = (uintptr_t) &current_file;
623 
624 	return IO_SUCCESS;
625 }
626 
rcar_file_len(io_entity_t * entity,size_t * length)627 static int32_t rcar_file_len(io_entity_t *entity, size_t *length)
628 {
629 	*length = ((file_state_t *) entity->info)->size;
630 
631 	NOTICE("%s: len: 0x%08lx\n", __func__, *length);
632 
633 	return IO_SUCCESS;
634 }
635 
rcar_file_read(io_entity_t * entity,uintptr_t buffer,size_t length,size_t * cnt)636 static int32_t rcar_file_read(io_entity_t *entity, uintptr_t buffer,
637 			      size_t length, size_t *cnt)
638 {
639 	file_state_t *fp = (file_state_t *) entity->info;
640 	ssize_t offset = fp->offset + fp->position;
641 	uintptr_t handle;
642 	int32_t rc;
643 
644 #ifdef SPD_NONE
645 	static uint32_t load_bl33x_counter = 1;
646 #else
647 	static uint32_t load_bl33x_counter;
648 #endif
649 	if (current_file.no_load != 0U) {
650 		*cnt = length;
651 		return IO_SUCCESS;
652 	}
653 
654 	((io_drv_spec_t *) rcar_spec)->partition = fp->partition;
655 
656 	rc = io_open(rcar_handle, rcar_spec, &handle);
657 	if (rc != IO_SUCCESS) {
658 		WARN("Failed to open FIP (%i)\n", rc);
659 		return IO_FAIL;
660 	}
661 
662 	rc = io_seek(handle, IO_SEEK_SET, offset);
663 	if (rc != IO_SUCCESS) {
664 		WARN("%s: failed to seek\n", __func__);
665 		goto error;
666 	}
667 
668 	if (load_bl33x_counter == RCAR_COUNT_LOAD_BL33) {
669 		rc = check_load_area(buffer, length);
670 		if (rc != IO_SUCCESS) {
671 			WARN("%s: load area err\n", __func__);
672 			goto error;
673 		}
674 	}
675 
676 	rc = io_read(handle, buffer, length, cnt);
677 	if (rc != IO_SUCCESS) {
678 		WARN("Failed to read payload (%i)\n", rc);
679 		goto error;
680 	}
681 
682 	fp->position += *cnt;
683 	io_close(handle);
684 
685 	load_bl33x_counter += 1;
686 	if (load_bl33x_counter == RCAR_COUNT_LOAD_BL33X) {
687 		return load_bl33x();
688 	}
689 
690 	return IO_SUCCESS;
691 error:
692 	io_close(handle);
693 	return IO_FAIL;
694 }
695 
rcar_file_close(io_entity_t * entity)696 static int32_t rcar_file_close(io_entity_t *entity)
697 {
698 	if (current_file.offset != 0U) {
699 		memset(&current_file, 0, sizeof(current_file));
700 	}
701 
702 	entity->info = 0U;
703 
704 	return IO_SUCCESS;
705 }
706 
707 static const io_dev_funcs_t rcar_dev_funcs = {
708 	.type = &device_type_rcar,
709 	.open = &rcar_file_open,
710 	.seek = NULL,
711 	.size = &rcar_file_len,
712 	.read = &rcar_file_read,
713 	.write = NULL,
714 	.close = &rcar_file_close,
715 	.dev_init = &rcar_dev_init,
716 	.dev_close = &rcar_dev_close,
717 };
718 
719 static const io_dev_info_t rcar_dev_info = {
720 	.funcs = &rcar_dev_funcs,
721 	.info = (uintptr_t) 0
722 };
723 
724 static const io_dev_connector_t rcar_dev_connector = {
725 	.dev_open = &rcar_dev_open
726 };
727 
rcar_dev_open(const uintptr_t dev_spec,io_dev_info_t ** dev_info)728 static int32_t rcar_dev_open(const uintptr_t dev_spec __attribute__ ((unused)),
729 			     io_dev_info_t **dev_info)
730 {
731 	*dev_info = (io_dev_info_t *) &rcar_dev_info;
732 
733 	return IO_SUCCESS;
734 }
735 
rcar_dev_close(io_dev_info_t * dev_info)736 static int32_t rcar_dev_close(io_dev_info_t *dev_info)
737 {
738 	rcar_handle = 0;
739 	rcar_spec = 0;
740 
741 	return IO_SUCCESS;
742 }
743 
rcar_register_io_dev(const io_dev_connector_t ** dev_con)744 int32_t rcar_register_io_dev(const io_dev_connector_t **dev_con)
745 {
746 	int32_t result;
747 
748 	result = io_register_device(&rcar_dev_info);
749 	if (result == IO_SUCCESS) {
750 		*dev_con = &rcar_dev_connector;
751 	}
752 
753 	return result;
754 }
755