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 156 static io_type_t device_type_rcar(void) 157 { 158 return IO_TYPE_FIRMWARE_IMAGE_PACKAGE; 159 } 160 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 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 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 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 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 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 } 514 #endif 515 516 rc = io_seek(handle, IO_SEEK_SET, offset); 517 if (rc != IO_SUCCESS) { 518 WARN("Firmware Image Package header failed to seek\n"); 519 goto error; 520 } 521 522 rc = io_read(handle, (uintptr_t) &header, sizeof(header), &cnt); 523 if (rc != IO_SUCCESS) { 524 WARN("Firmware Image Package header failed to read\n"); 525 goto error; 526 } 527 528 #if RCAR_BL2_DCACHE == 1 529 inv_dcache_range((uint64_t) header, sizeof(header)); 530 #endif 531 532 rcar_image_number = header[0]; 533 if (rcar_image_number == 0 || rcar_image_number > RCAR_MAX_BL3X_IMAGE) { 534 WARN("Firmware Image Package header check failed.\n"); 535 rc = IO_FAIL; 536 goto error; 537 } 538 539 for (i = 0; i < rcar_image_number + 2; i++) { 540 rcar_image_header[i] = header[i * 2 + 1]; 541 rcar_image_header_prttn[i] = header[i * 2 + 2]; 542 } 543 544 rc = io_seek(handle, IO_SEEK_SET, offset + RCAR_SECTOR6_CERT_OFFSET); 545 if (rc != IO_SUCCESS) { 546 WARN("Firmware Image Package header failed to seek cert\n"); 547 goto error; 548 } 549 550 rc = io_read(handle, RCAR_SDRAM_certESS, 551 RCAR_CERT_SIZE * (2 + rcar_image_number), &cnt); 552 if (rc != IO_SUCCESS) { 553 WARN("cert file read error.\n"); 554 goto error; 555 } 556 557 #if RCAR_BL2_DCACHE == 1 558 inv_dcache_range(RCAR_SDRAM_certESS, 559 RCAR_CERT_SIZE * (2 + rcar_image_number)); 560 #endif 561 562 rcar_cert_load = RCAR_CERT_LOAD; 563 error: 564 565 if (rc != IO_SUCCESS) { 566 rc = IO_FAIL; 567 } 568 569 io_close(handle); 570 571 return rc; 572 573 } 574 575 static int32_t rcar_file_open(io_dev_info_t *info, const uintptr_t file_spec, 576 io_entity_t *entity) 577 { 578 const io_drv_spec_t *spec = (io_drv_spec_t *) file_spec; 579 uintptr_t partition, offset, dst; 580 uint32_t noload, cert, len; 581 int32_t rc; 582 583 /* 584 * Only one file open at a time. We need to track state (ie, file 585 * cursor position). Since the header lives at offset zero, this entry 586 * should never be zero in an active file. 587 * Once the system supports dynamic memory allocation we will allow more 588 * than one open file at a time. 589 */ 590 if (current_file.offset != 0U) { 591 WARN("%s: Only one open file at a time.\n", __func__); 592 return IO_RESOURCES_EXHAUSTED; 593 } 594 595 rc = file_to_offset(spec->offset, &offset, &cert, &noload, &partition); 596 if (rc != IO_SUCCESS) { 597 WARN("Failed to open file name %ld (%i)\n", spec->offset, rc); 598 return IO_FAIL; 599 } 600 601 if (noload != 0U) { 602 current_file.offset = 1; 603 current_file.dst = 0; 604 current_file.size = 1; 605 current_file.position = 0; 606 current_file.no_load = noload; 607 current_file.partition = 0; 608 entity->info = (uintptr_t) ¤t_file; 609 610 return IO_SUCCESS; 611 } 612 613 rcar_read_certificate((uint64_t) cert, &len, &dst); 614 615 current_file.partition = partition; 616 current_file.no_load = noload; 617 current_file.offset = offset; 618 current_file.position = 0; 619 current_file.size = len; 620 current_file.dst = dst; 621 entity->info = (uintptr_t) ¤t_file; 622 623 return IO_SUCCESS; 624 } 625 626 static int32_t rcar_file_len(io_entity_t *entity, size_t *length) 627 { 628 *length = ((file_state_t *) entity->info)->size; 629 630 NOTICE("%s: len: 0x%08lx\n", __func__, *length); 631 632 return IO_SUCCESS; 633 } 634 635 static int32_t rcar_file_read(io_entity_t *entity, uintptr_t buffer, 636 size_t length, size_t *cnt) 637 { 638 file_state_t *fp = (file_state_t *) entity->info; 639 ssize_t offset = fp->offset + fp->position; 640 uintptr_t handle; 641 int32_t rc; 642 643 #ifdef SPD_NONE 644 static uint32_t load_bl33x_counter = 1; 645 #else 646 static uint32_t load_bl33x_counter; 647 #endif 648 if (current_file.no_load != 0U) { 649 *cnt = length; 650 return IO_SUCCESS; 651 } 652 653 ((io_drv_spec_t *) rcar_spec)->partition = fp->partition; 654 655 rc = io_open(rcar_handle, rcar_spec, &handle); 656 if (rc != IO_SUCCESS) { 657 WARN("Failed to open FIP (%i)\n", rc); 658 return IO_FAIL; 659 } 660 661 rc = io_seek(handle, IO_SEEK_SET, offset); 662 if (rc != IO_SUCCESS) { 663 WARN("%s: failed to seek\n", __func__); 664 goto error; 665 } 666 667 if (load_bl33x_counter == RCAR_COUNT_LOAD_BL33) { 668 rc = check_load_area(buffer, length); 669 if (rc != IO_SUCCESS) { 670 WARN("%s: load area err\n", __func__); 671 goto error; 672 } 673 } 674 675 rc = io_read(handle, buffer, length, cnt); 676 if (rc != IO_SUCCESS) { 677 WARN("Failed to read payload (%i)\n", rc); 678 goto error; 679 } 680 681 fp->position += *cnt; 682 io_close(handle); 683 684 load_bl33x_counter += 1; 685 if (load_bl33x_counter == RCAR_COUNT_LOAD_BL33X) { 686 return load_bl33x(); 687 } 688 689 return IO_SUCCESS; 690 error: 691 io_close(handle); 692 return IO_FAIL; 693 } 694 695 static int32_t rcar_file_close(io_entity_t *entity) 696 { 697 if (current_file.offset != 0U) { 698 memset(¤t_file, 0, sizeof(current_file)); 699 } 700 701 entity->info = 0U; 702 703 return IO_SUCCESS; 704 } 705 706 static const io_dev_funcs_t rcar_dev_funcs = { 707 .type = &device_type_rcar, 708 .open = &rcar_file_open, 709 .seek = NULL, 710 .size = &rcar_file_len, 711 .read = &rcar_file_read, 712 .write = NULL, 713 .close = &rcar_file_close, 714 .dev_init = &rcar_dev_init, 715 .dev_close = &rcar_dev_close, 716 }; 717 718 static const io_dev_info_t rcar_dev_info = { 719 .funcs = &rcar_dev_funcs, 720 .info = (uintptr_t) 0 721 }; 722 723 static const io_dev_connector_t rcar_dev_connector = { 724 .dev_open = &rcar_dev_open 725 }; 726 727 static int32_t rcar_dev_open(const uintptr_t dev_spec __attribute__ ((unused)), 728 io_dev_info_t **dev_info) 729 { 730 *dev_info = (io_dev_info_t *) &rcar_dev_info; 731 732 return IO_SUCCESS; 733 } 734 735 static int32_t rcar_dev_close(io_dev_info_t *dev_info) 736 { 737 rcar_handle = 0; 738 rcar_spec = 0; 739 740 return IO_SUCCESS; 741 } 742 743 int32_t rcar_register_io_dev(const io_dev_connector_t **dev_con) 744 { 745 int32_t result; 746 747 result = io_register_device(&rcar_dev_info); 748 if (result == IO_SUCCESS) { 749 *dev_con = &rcar_dev_connector; 750 } 751 752 return result; 753 } 754