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