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