blob: c3e8319de4b27e68ff4781ba675c833cd66d5ab0 [file] [log] [blame]
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +02001/*
Biju Das50bfdc42020-12-13 20:28:45 +00002 * Copyright (c) 2015-2021, Renesas Electronics Corporation. All rights reserved.
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +02007#include <errno.h>
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +02008#include <stdint.h>
9#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020011#include <arch_helpers.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#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
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020022#include "io_rcar.h"
23#include "io_common.h"
24#include "io_private.h"
Biju Das50bfdc42020-12-13 20:28:45 +000025#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000026
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020027extern int32_t plat_get_drv_source(uint32_t id, uintptr_t *dev,
28 uintptr_t *image_spec);
29
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020030static int32_t rcar_dev_open(const uintptr_t dev_spec __attribute__ ((unused)),
31 io_dev_info_t **dev_info);
32static int32_t rcar_dev_close(io_dev_info_t *dev_info);
33
34typedef struct {
35 const int32_t name;
36 const uint32_t offset;
37 const uint32_t attr;
38} plat_rcar_name_offset_t;
39
40typedef struct {
Biju Das50bfdc42020-12-13 20:28:45 +000041 /*
42 * Put position above the struct to allow {0} on static init.
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020043 * 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) |\
Biju Das50bfdc42020-12-13 20:28:45 +000062 RCAR_ATTR_SET_ISNOLOAD(b) |\
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +020063 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
87static const plat_rcar_name_offset_t name_offset[] = {
88 {BL31_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(0, 0, 0)},
89
90 /* BL3-2 is optional in the platform */
91 {BL32_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(1, 0, 1)},
92 {BL33_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(2, 0, 2)},
93 {BL332_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(3, 0, 3)},
94 {BL333_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(4, 0, 4)},
95 {BL334_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(5, 0, 5)},
96 {BL335_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(6, 0, 6)},
97 {BL336_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(7, 0, 7)},
98 {BL337_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(8, 0, 8)},
99 {BL338_IMAGE_ID, 0U, RCAR_ATTR_SET_ALL(9, 0, 9)},
100};
101
102#if TRUSTED_BOARD_BOOT
103static const plat_rcar_name_offset_t cert_offset[] = {
104 /* Certificates */
105 {TRUSTED_KEY_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 0)},
106 {SOC_FW_KEY_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 0)},
107 {TRUSTED_OS_FW_KEY_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 0)},
108 {NON_TRUSTED_FW_KEY_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 0)},
109 {SOC_FW_CONTENT_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 0)},
110 {TRUSTED_OS_FW_CONTENT_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 1)},
111 {NON_TRUSTED_FW_CONTENT_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 2)},
112 {BL332_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 3)},
113 {BL333_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 4)},
114 {BL334_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 5)},
115 {BL335_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 6)},
116 {BL336_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 7)},
117 {BL337_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 8)},
118 {BL338_CERT_ID, 0U, RCAR_ATTR_SET_ALL(0, 1, 9)},
119};
120#endif /* TRUSTED_BOARD_BOOT */
121
122static file_state_t current_file = { 0 };
123
124static uintptr_t rcar_handle, rcar_spec;
125static uint64_t rcar_image_header[RCAR_MAX_BL3X_IMAGE + 2U] = { 0U };
126static uint64_t rcar_image_header_prttn[RCAR_MAX_BL3X_IMAGE + 2U] = { 0U };
127static uint64_t rcar_image_number = { 0U };
128static uint32_t rcar_cert_load = { 0U };
129
130static io_type_t device_type_rcar(void)
131{
132 return IO_TYPE_FIRMWARE_IMAGE_PACKAGE;
133}
134
135int32_t rcar_get_certificate(const int32_t name, uint32_t *cert)
136{
137#if TRUSTED_BOARD_BOOT
138 int32_t i;
Biju Das50bfdc42020-12-13 20:28:45 +0000139
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200140 for (i = 0; i < ARRAY_SIZE(cert_offset); i++) {
Biju Das50bfdc42020-12-13 20:28:45 +0000141 if (name != cert_offset[i].name) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200142 continue;
Biju Das50bfdc42020-12-13 20:28:45 +0000143 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200144
145 *cert = RCAR_CERT_SIZE;
146 *cert *= RCAR_ATTR_GET_CERTOFF(cert_offset[i].attr);
147 *cert += RCAR_SDRAM_certESS;
148 return 0;
149 }
150#endif
151 return -EINVAL;
152}
153
154static int32_t file_to_offset(const int32_t name, uintptr_t *offset,
155 uint32_t *cert, uint32_t *no_load,
156 uintptr_t *partition)
157{
158 uint32_t addr;
159 int32_t i;
160
161 for (i = 0; i < ARRAY_SIZE(name_offset); i++) {
Biju Das50bfdc42020-12-13 20:28:45 +0000162 if (name != name_offset[i].name) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200163 continue;
Biju Das50bfdc42020-12-13 20:28:45 +0000164 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200165
166 addr = RCAR_ATTR_GET_CALCADDR(name_offset[i].attr);
Biju Das50bfdc42020-12-13 20:28:45 +0000167 if (rcar_image_number + 2U < addr) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200168 continue;
Biju Das50bfdc42020-12-13 20:28:45 +0000169 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200170
171 *offset = rcar_image_header[addr];
172 *cert = RCAR_CERT_SIZE;
173 *cert *= RCAR_ATTR_GET_CERTOFF(name_offset[i].attr);
174 *cert += RCAR_SDRAM_certESS;
175 *no_load = RCAR_ATTR_GET_ISNOLOAD(name_offset[i].attr);
176 *partition = rcar_image_header_prttn[addr];
177 return IO_SUCCESS;
178 }
179
180#if TRUSTED_BOARD_BOOT
181 for (i = 0; i < ARRAY_SIZE(cert_offset); i++) {
Biju Das50bfdc42020-12-13 20:28:45 +0000182 if (name != cert_offset[i].name) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200183 continue;
Biju Das50bfdc42020-12-13 20:28:45 +0000184 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200185
186 *no_load = RCAR_ATTR_GET_ISNOLOAD(cert_offset[i].attr);
187 *partition = 0U;
188 *offset = 0U;
189 *cert = 0U;
190 return IO_SUCCESS;
191 }
192#endif
193 return -EINVAL;
194}
195
196#define RCAR_BOOT_KEY_CERT_NEW (0xE6300F00U)
197#define RCAR_CERT_MAGIC_NUM (0xE291F358U)
198
199void rcar_read_certificate(uint64_t cert, uint32_t *len, uintptr_t *dst)
200{
201 uint32_t seed, val, info_1, info_2;
202 uintptr_t size, dsth, dstl;
203
204 cert &= 0xFFFFFFFFU;
205
206 seed = mmio_read_32(RCAR_BOOT_KEY_CERT_NEW);
207 val = mmio_read_32(RCAR_BOOT_KEY_CERT_NEW + 0xC);
208 info_1 = (val >> 18) & 0x3U;
209 val = mmio_read_32(cert + 0xC);
210 info_2 = (val >> 21) & 0x3;
211
212 if (seed == RCAR_CERT_MAGIC_NUM) {
213 if (info_1 != 1) {
214 ERROR("BL2: Cert is invalid.\n");
215 *dst = 0;
216 *len = 0;
217 return;
218 }
219
220 if (info_2 > 2) {
221 ERROR("BL2: Cert is invalid.\n");
222 *dst = 0;
223 *len = 0;
224 return;
225 }
226
227 switch (info_2) {
228 case 2:
229 size = cert + RCAR_CERT_INFO_SIZE_OFFSET2;
230 dstl = cert + RCAR_CERT_INFO_DST_OFFSET2;
231 break;
232 case 1:
233 size = cert + RCAR_CERT_INFO_SIZE_OFFSET1;
234 dstl = cert + RCAR_CERT_INFO_DST_OFFSET1;
235 break;
236 case 0:
237 size = cert + RCAR_CERT_INFO_SIZE_OFFSET;
238 dstl = cert + RCAR_CERT_INFO_DST_OFFSET;
239 break;
240 }
241
242 *len = mmio_read_32(size) * 4U;
243 dsth = dstl + 4U;
244 *dst = ((uintptr_t) mmio_read_32(dsth) << 32) +
245 ((uintptr_t) mmio_read_32(dstl));
246 return;
247 }
248
249 size = cert + RCAR_CERT_INFO_SIZE_OFFSET;
250 *len = mmio_read_32(size) * 4U;
251 dstl = cert + RCAR_CERT_INFO_DST_OFFSET;
252 dsth = dstl + 4U;
253 *dst = ((uintptr_t) mmio_read_32(dsth) << 32) +
254 ((uintptr_t) mmio_read_32(dstl));
255}
256
257static int32_t check_load_area(uintptr_t dst, uintptr_t len)
258{
259 uint32_t legacy = dst + len <= UINT32_MAX - 1 ? 1 : 0;
260 uintptr_t dram_start, dram_end;
261 uintptr_t prot_start, prot_end;
262 int32_t result = IO_SUCCESS;
263
264 dram_start = legacy ? DRAM1_BASE : DRAM_40BIT_BASE;
265
266 dram_end = legacy ? DRAM1_BASE + DRAM1_SIZE :
267 DRAM_40BIT_BASE + DRAM_40BIT_SIZE;
268
269 prot_start = legacy ? DRAM_PROTECTED_BASE : DRAM_40BIT_PROTECTED_BASE;
270
271 prot_end = prot_start + DRAM_PROTECTED_SIZE;
272
273 if (dst < dram_start || dst > dram_end - len) {
274 ERROR("BL2: dst address is on the protected area.\n");
275 result = IO_FAIL;
276 goto done;
277 }
278
279 /* load image is within SDRAM protected area */
280 if (dst >= prot_start && dst < prot_end) {
281 ERROR("BL2: dst address is on the protected area.\n");
282 result = IO_FAIL;
283 }
284
285 if (dst < prot_start && dst > prot_start - len) {
286 ERROR("BL2: loaded data is on the protected area.\n");
287 result = IO_FAIL;
288 }
289done:
Biju Das50bfdc42020-12-13 20:28:45 +0000290 if (result == IO_FAIL) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200291 ERROR("BL2: Out of range : dst=0x%lx len=0x%lx\n", dst, len);
Biju Das50bfdc42020-12-13 20:28:45 +0000292 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200293
294 return result;
295}
296
297static int32_t load_bl33x(void)
298{
299 static int32_t loaded = IO_NOT_SUPPORTED;
300 uintptr_t dst, partition, handle;
301 uint32_t noload, cert, len, i;
302 uintptr_t offset;
303 int32_t rc;
304 size_t cnt;
305 const int32_t img[] = {
306 BL33_IMAGE_ID,
307 BL332_IMAGE_ID,
308 BL333_IMAGE_ID,
309 BL334_IMAGE_ID,
310 BL335_IMAGE_ID,
311 BL336_IMAGE_ID,
312 BL337_IMAGE_ID,
313 BL338_IMAGE_ID
314 };
315
Biju Das50bfdc42020-12-13 20:28:45 +0000316 if (loaded != IO_NOT_SUPPORTED) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200317 return loaded;
Biju Das50bfdc42020-12-13 20:28:45 +0000318 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200319
320 for (i = 1; i < rcar_image_number; i++) {
321 rc = file_to_offset(img[i], &offset, &cert, &noload,
322 &partition);
323 if (rc != IO_SUCCESS) {
Biju Das50bfdc42020-12-13 20:28:45 +0000324 WARN("%s: failed to get offset\n", __func__);
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200325 loaded = IO_FAIL;
326 return loaded;
327 }
328
329 rcar_read_certificate((uint64_t) cert, &len, &dst);
330 ((io_drv_spec_t *) rcar_spec)->partition = partition;
331
332 rc = io_open(rcar_handle, rcar_spec, &handle);
333 if (rc != IO_SUCCESS) {
Biju Das50bfdc42020-12-13 20:28:45 +0000334 WARN("%s: Failed to open FIP (%i)\n", __func__, rc);
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200335 loaded = IO_FAIL;
336 return loaded;
337 }
338
339 rc = io_seek(handle, IO_SEEK_SET, offset);
340 if (rc != IO_SUCCESS) {
Biju Das50bfdc42020-12-13 20:28:45 +0000341 WARN("%s: failed to seek\n", __func__);
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200342 loaded = IO_FAIL;
343 return loaded;
344 }
345
346 rc = check_load_area(dst, len);
347 if (rc != IO_SUCCESS) {
Biju Das50bfdc42020-12-13 20:28:45 +0000348 WARN("%s: check load area\n", __func__);
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200349 loaded = IO_FAIL;
350 return loaded;
351 }
352
353 rc = io_read(handle, dst, len, &cnt);
354 if (rc != IO_SUCCESS) {
Biju Das50bfdc42020-12-13 20:28:45 +0000355 WARN("%s: failed to read\n", __func__);
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200356 loaded = IO_FAIL;
357 return loaded;
358 }
359#if TRUSTED_BOARD_BOOT
360 rc = auth_mod_verify_img(img[i], (void *)dst, len);
Biju Das50bfdc42020-12-13 20:28:45 +0000361 if (rc != 0) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200362 memset((void *)dst, 0x00, len);
363 loaded = IO_FAIL;
364 return loaded;
365 }
366#endif
367 io_close(handle);
368 }
369
370 loaded = IO_SUCCESS;
371
372 return loaded;
373}
374
375static int32_t rcar_dev_init(io_dev_info_t *dev_info, const uintptr_t name)
376{
Biju Das50bfdc42020-12-13 20:28:45 +0000377 uint64_t header[64] __aligned(FLASH_TRANS_SIZE_UNIT) = {0UL};
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200378 uintptr_t handle;
379 ssize_t offset;
380 uint32_t i;
381 int32_t rc;
382 size_t cnt;
383
384 /* Obtain a reference to the image by querying the platform layer */
385 rc = plat_get_drv_source(name, &rcar_handle, &rcar_spec);
386 if (rc != IO_SUCCESS) {
387 WARN("Failed to obtain reference to img %ld (%i)\n", name, rc);
388 return IO_FAIL;
389 }
390
Biju Das50bfdc42020-12-13 20:28:45 +0000391 if (rcar_cert_load == RCAR_CERT_LOAD) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200392 return IO_SUCCESS;
Biju Das50bfdc42020-12-13 20:28:45 +0000393 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200394
395 rc = io_open(rcar_handle, rcar_spec, &handle);
396 if (rc != IO_SUCCESS) {
397 WARN("Failed to access img %ld (%i)\n", name, rc);
398 return IO_FAIL;
399 }
400
Biju Das50bfdc42020-12-13 20:28:45 +0000401 /*
402 * get start address list
403 * [0] address num
404 * [1] BL33-1 image address
405 * [2] BL33-2 image address
406 * [3] BL33-3 image address
407 * [4] BL33-4 image address
408 * [5] BL33-5 image address
409 * [6] BL33-6 image address
410 * [7] BL33-7 image address
411 * [8] BL33-8 image address
412 */
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200413 offset = name == EMMC_DEV_ID ? RCAR_EMMC_CERT_HEADER :
414 RCAR_FLASH_CERT_HEADER;
415 rc = io_seek(handle, IO_SEEK_SET, offset);
416 if (rc != IO_SUCCESS) {
417 WARN("Firmware Image Package header failed to seek\n");
418 goto error;
419 }
420#if RCAR_BL2_DCACHE == 1
421 inv_dcache_range((uint64_t) header, sizeof(header));
422#endif
423 rc = io_read(handle, (uintptr_t) &header, sizeof(header), &cnt);
424 if (rc != IO_SUCCESS) {
425 WARN("Firmware Image Package header failed to read\n");
426 goto error;
427 }
428
429 rcar_image_number = header[0];
430 for (i = 0; i < rcar_image_number + 2; i++) {
431 rcar_image_header[i] = header[i * 2 + 1];
432 rcar_image_header_prttn[i] = header[i * 2 + 2];
433 }
434
435 if (rcar_image_number == 0 || rcar_image_number > RCAR_MAX_BL3X_IMAGE) {
436 WARN("Firmware Image Package header check failed.\n");
437 goto error;
438 }
439
440 rc = io_seek(handle, IO_SEEK_SET, offset + RCAR_SECTOR6_CERT_OFFSET);
441 if (rc != IO_SUCCESS) {
442 WARN("Firmware Image Package header failed to seek cert\n");
443 goto error;
444 }
445#if RCAR_BL2_DCACHE == 1
446 inv_dcache_range(RCAR_SDRAM_certESS,
447 RCAR_CERT_SIZE * (2 + rcar_image_number));
448#endif
449 rc = io_read(handle, RCAR_SDRAM_certESS,
450 RCAR_CERT_SIZE * (2 + rcar_image_number), &cnt);
451 if (rc != IO_SUCCESS) {
452 WARN("cert file read error.\n");
453 goto error;
454 }
455
456 rcar_cert_load = RCAR_CERT_LOAD;
457error:
458
Biju Das50bfdc42020-12-13 20:28:45 +0000459 if (rc != IO_SUCCESS) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200460 rc = IO_FAIL;
Biju Das50bfdc42020-12-13 20:28:45 +0000461 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200462
463 io_close(handle);
464
465 return rc;
466
467}
468
469static int32_t rcar_file_open(io_dev_info_t *info, const uintptr_t file_spec,
470 io_entity_t *entity)
471{
472 const io_drv_spec_t *spec = (io_drv_spec_t *) file_spec;
473 uintptr_t partition, offset, dst;
474 uint32_t noload, cert, len;
475 int32_t rc;
476
Biju Das50bfdc42020-12-13 20:28:45 +0000477 /*
478 * Only one file open at a time. We need to track state (ie, file
479 * cursor position). Since the header lives at offset zero, this entry
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200480 * should never be zero in an active file.
481 * Once the system supports dynamic memory allocation we will allow more
Biju Das50bfdc42020-12-13 20:28:45 +0000482 * than one open file at a time.
483 */
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200484 if (current_file.offset != 0U) {
Biju Das50bfdc42020-12-13 20:28:45 +0000485 WARN("%s: Only one open file at a time.\n", __func__);
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200486 return IO_RESOURCES_EXHAUSTED;
487 }
488
489 rc = file_to_offset(spec->offset, &offset, &cert, &noload, &partition);
490 if (rc != IO_SUCCESS) {
491 WARN("Failed to open file name %ld (%i)\n", spec->offset, rc);
492 return IO_FAIL;
493 }
494
Biju Das50bfdc42020-12-13 20:28:45 +0000495 if (noload != 0U) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200496 current_file.offset = 1;
497 current_file.dst = 0;
498 current_file.size = 1;
499 current_file.position = 0;
500 current_file.no_load = noload;
501 current_file.partition = 0;
502 entity->info = (uintptr_t) &current_file;
503
504 return IO_SUCCESS;
505 }
506
507 rcar_read_certificate((uint64_t) cert, &len, &dst);
508
Biju Das50bfdc42020-12-13 20:28:45 +0000509 /* Baylibre: HACK */
510 if (spec->offset == BL31_IMAGE_ID && len < RCAR_TRUSTED_SRAM_SIZE) {
511 WARN("%s,%s\n", "r-car ignoring the BL31 size from certificate",
512 "using RCAR_TRUSTED_SRAM_SIZE instead");
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200513 len = RCAR_TRUSTED_SRAM_SIZE;
514 }
515
516 current_file.partition = partition;
517 current_file.no_load = noload;
518 current_file.offset = offset;
519 current_file.position = 0;
520 current_file.size = len;
521 current_file.dst = dst;
522 entity->info = (uintptr_t) &current_file;
523
524 return IO_SUCCESS;
525}
526
527static int32_t rcar_file_len(io_entity_t *entity, size_t *length)
528{
529 *length = ((file_state_t *) entity->info)->size;
530
531 NOTICE("%s: len: 0x%08lx\n", __func__, *length);
532
533 return IO_SUCCESS;
534}
535
536static int32_t rcar_file_read(io_entity_t *entity, uintptr_t buffer,
537 size_t length, size_t *cnt)
538{
539 file_state_t *fp = (file_state_t *) entity->info;
540 ssize_t offset = fp->offset + fp->position;
541 uintptr_t handle;
542 int32_t rc;
543
544#ifdef SPD_NONE
545 static uint32_t load_bl33x_counter = 1;
546#else
547 static uint32_t load_bl33x_counter;
548#endif
Biju Das50bfdc42020-12-13 20:28:45 +0000549 if (current_file.no_load != 0U) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200550 *cnt = length;
551 return IO_SUCCESS;
552 }
553
554 ((io_drv_spec_t *) rcar_spec)->partition = fp->partition;
555
556 rc = io_open(rcar_handle, rcar_spec, &handle);
557 if (rc != IO_SUCCESS) {
558 WARN("Failed to open FIP (%i)\n", rc);
559 return IO_FAIL;
560 }
561
562 rc = io_seek(handle, IO_SEEK_SET, offset);
563 if (rc != IO_SUCCESS) {
Biju Das50bfdc42020-12-13 20:28:45 +0000564 WARN("%s: failed to seek\n", __func__);
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200565 goto error;
566 }
567
568 if (load_bl33x_counter == RCAR_COUNT_LOAD_BL33) {
569 rc = check_load_area(buffer, length);
570 if (rc != IO_SUCCESS) {
Biju Das50bfdc42020-12-13 20:28:45 +0000571 WARN("%s: load area err\n", __func__);
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200572 goto error;
573 }
574 }
575
576 rc = io_read(handle, buffer, length, cnt);
577 if (rc != IO_SUCCESS) {
578 WARN("Failed to read payload (%i)\n", rc);
579 goto error;
580 }
581
582 fp->position += *cnt;
583 io_close(handle);
584
585 load_bl33x_counter += 1;
Biju Das50bfdc42020-12-13 20:28:45 +0000586 if (load_bl33x_counter == RCAR_COUNT_LOAD_BL33X) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200587 return load_bl33x();
Biju Das50bfdc42020-12-13 20:28:45 +0000588 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200589
590 return IO_SUCCESS;
591error:
592 io_close(handle);
593 return IO_FAIL;
594}
595
596static int32_t rcar_file_close(io_entity_t *entity)
597{
Biju Das50bfdc42020-12-13 20:28:45 +0000598 if (current_file.offset != 0U) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200599 memset(&current_file, 0, sizeof(current_file));
Biju Das50bfdc42020-12-13 20:28:45 +0000600 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200601
602 entity->info = 0U;
603
604 return IO_SUCCESS;
605}
606
607static const io_dev_funcs_t rcar_dev_funcs = {
608 .type = &device_type_rcar,
609 .open = &rcar_file_open,
610 .seek = NULL,
611 .size = &rcar_file_len,
612 .read = &rcar_file_read,
613 .write = NULL,
614 .close = &rcar_file_close,
615 .dev_init = &rcar_dev_init,
616 .dev_close = &rcar_dev_close,
617};
618
619static const io_dev_info_t rcar_dev_info = {
620 .funcs = &rcar_dev_funcs,
621 .info = (uintptr_t) 0
622};
623
624static const io_dev_connector_t rcar_dev_connector = {
625 .dev_open = &rcar_dev_open
626};
627
628static int32_t rcar_dev_open(const uintptr_t dev_spec __attribute__ ((unused)),
629 io_dev_info_t **dev_info)
630{
631 *dev_info = (io_dev_info_t *) &rcar_dev_info;
632
633 return IO_SUCCESS;
634}
635
636static int32_t rcar_dev_close(io_dev_info_t *dev_info)
637{
638 rcar_handle = 0;
639 rcar_spec = 0;
640
641 return IO_SUCCESS;
642}
643
644int32_t rcar_register_io_dev(const io_dev_connector_t **dev_con)
645{
646 int32_t result;
647
648 result = io_register_device(&rcar_dev_info);
Biju Das50bfdc42020-12-13 20:28:45 +0000649 if (result == IO_SUCCESS) {
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200650 *dev_con = &rcar_dev_connector;
Biju Das50bfdc42020-12-13 20:28:45 +0000651 }
Jorge Ramirez-Ortizeaa63b42018-09-23 09:40:45 +0200652
653 return result;
654}