| 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 |
|
| 153 |
static io_type_t device_type_rcar(void) |
| 154 |
{ |
| 155 |
return IO_TYPE_FIRMWARE_IMAGE_PACKAGE; |
| 156 |
} |
| 157 |
|
| 158 |
int32_t rcar_get_certificate(const int32_t name, uint32_t *cert) |
| 159 |
{ |
| 160 |
#if TRUSTED_BOARD_BOOT |
| 161 |
int32_t i; |
| 162 |
|
| 163 |
for (i = 0; i < ARRAY_SIZE(cert_offset); i++) { |
| 164 |
if (name != cert_offset[i].name) { |
| 165 |
continue; |
| 166 |
} |
| 167 |
|
| 168 |
*cert = RCAR_CERT_SIZE; |
| 169 |
*cert *= RCAR_ATTR_GET_CERTOFF(cert_offset[i].attr); |
| 170 |
*cert += RCAR_SDRAM_certESS; |
| 171 |
return 0; |
| 172 |
} |
| 173 |
#endif |
| 174 |
return -EINVAL; |
| 175 |
} |
| 176 |
|
| 177 |
#define MFISBTSTSR (0xE6260604U) |
| 178 |
#define MFISBTSTSR_BOOT_PARTITION (0x00000010U) |
| 179 |
|
| 180 |
static int32_t file_to_offset(const int32_t name, uintptr_t *offset, |
| 181 |
uint32_t *cert, uint32_t *no_load, |
| 182 |
uintptr_t *partition) |
| 183 |
{ |
| 184 |
uint32_t addr; |
| 185 |
int32_t i; |
| 186 |
|
| 187 |
for (i = 0; i < ARRAY_SIZE(name_offset); i++) { |
| 188 |
if (name != name_offset[i].name) { |
| 189 |
continue; |
| 190 |
} |
| 191 |
|
| 192 |
addr = RCAR_ATTR_GET_CALCADDR(name_offset[i].attr); |
| 193 |
if (rcar_image_number + 2U < addr) { |
| 194 |
continue; |
| 195 |
} |
| 196 |
|
| 197 |
*offset = rcar_image_header[addr]; |
| 198 |
|
| 199 |
if (mmio_read_32(MFISBTSTSR) & MFISBTSTSR_BOOT_PARTITION) |
| 200 |
*offset += 0x800000; |
| 201 |
*cert = RCAR_CERT_SIZE; |
| 202 |
*cert *= RCAR_ATTR_GET_CERTOFF(name_offset[i].attr); |
| 203 |
*cert += RCAR_SDRAM_certESS; |
| 204 |
*no_load = RCAR_ATTR_GET_ISNOLOAD(name_offset[i].attr); |
| 205 |
*partition = rcar_image_header_prttn[addr]; |
| 206 |
return IO_SUCCESS; |
| 207 |
} |
| 208 |
|
| 209 |
#if TRUSTED_BOARD_BOOT |
| 210 |
for (i = 0; i < ARRAY_SIZE(cert_offset); i++) { |
| 211 |
if (name != cert_offset[i].name) { |
| 212 |
continue; |
| 213 |
} |
| 214 |
|
| 215 |
*no_load = RCAR_ATTR_GET_ISNOLOAD(cert_offset[i].attr); |
| 216 |
*partition = 0U; |
| 217 |
*offset = 0U; |
| 218 |
*cert = 0U; |
| 219 |
return IO_SUCCESS; |
| 220 |
} |
| 221 |
#endif |
| 222 |
return -EINVAL; |
| 223 |
} |
| 224 |
|
| 225 |
#define RCAR_BOOT_KEY_CERT_NEW (0xE6300F00U) |
| 226 |
#define RCAR_CERT_MAGIC_NUM (0xE291F358U) |
| 227 |
|
| 228 |
void rcar_read_certificate(uint64_t cert, uint32_t *len, uintptr_t *dst) |
| 229 |
{ |
| 230 |
uint32_t seed, val, info_1, info_2; |
| 231 |
uintptr_t size, dsth, dstl; |
| 232 |
|
| 233 |
cert &= 0xFFFFFFFFU; |
| 234 |
|
| 235 |
seed = mmio_read_32(RCAR_BOOT_KEY_CERT_NEW); |
| 236 |
val = mmio_read_32(RCAR_BOOT_KEY_CERT_NEW + 0xC); |
| 237 |
info_1 = (val >> 18) & 0x3U; |
| 238 |
val = mmio_read_32(cert + 0xC); |
| 239 |
info_2 = (val >> 21) & 0x3; |
| 240 |
|
| 241 |
if (seed == RCAR_CERT_MAGIC_NUM) { |
| 242 |
if (info_1 != 1) { |
| 243 |
ERROR("BL2: Cert is invalid.\n"); |
| 244 |
*dst = 0; |
| 245 |
*len = 0; |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
if (info_2 > 2) { |
| 250 |
ERROR("BL2: Cert is invalid.\n"); |
| 251 |
*dst = 0; |
| 252 |
*len = 0; |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
switch (info_2) { |
| 257 |
case 2: |
| 258 |
size = cert + RCAR_CERT_INFO_SIZE_OFFSET2; |
| 259 |
dstl = cert + RCAR_CERT_INFO_DST_OFFSET2; |
| 260 |
break; |
| 261 |
case 1: |
| 262 |
size = cert + RCAR_CERT_INFO_SIZE_OFFSET1; |
| 263 |
dstl = cert + RCAR_CERT_INFO_DST_OFFSET1; |
| 264 |
break; |
| 265 |
case 0: |
| 266 |
size = cert + RCAR_CERT_INFO_SIZE_OFFSET; |
| 267 |
dstl = cert + RCAR_CERT_INFO_DST_OFFSET; |
| 268 |
break; |
| 269 |
} |
| 270 |
val = mmio_read_32(size); |
| 271 |
if (val > (UINT32_MAX / 4)) { |
| 272 |
ERROR("BL2: %s[%d] uint32 overflow!\n", |
| 273 |
__func__, __LINE__); |
| 274 |
*dst = 0; |
| 275 |
*len = 0; |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
*len = val * 4U; |
| 280 |
dsth = dstl + 4U; |
| 281 |
*dst = ((uintptr_t) mmio_read_32(dsth) << 32) + |
| 282 |
((uintptr_t) mmio_read_32(dstl)); |
| 283 |
return; |
| 284 |
} |
| 285 |
|
| 286 |
size = cert + RCAR_CERT_INFO_SIZE_OFFSET; |
| 287 |
val = mmio_read_32(size); |
| 288 |
if (val > (UINT32_MAX / 4)) { |
| 289 |
ERROR("BL2: %s[%d] uint32 overflow!\n", __func__, __LINE__); |
| 290 |
*dst = 0; |
| 291 |
*len = 0; |
| 292 |
return; |
| 293 |
} |
| 294 |
*len = val * 4U; |
| 295 |
dstl = cert + RCAR_CERT_INFO_DST_OFFSET; |
| 296 |
dsth = dstl + 4U; |
| 297 |
*dst = ((uintptr_t) mmio_read_32(dsth) << 32) + |
| 298 |
((uintptr_t) mmio_read_32(dstl)); |
| 299 |
} |
| 300 |
|
| 301 |
static int32_t check_load_area(uintptr_t dst, uintptr_t len) |
| 302 |
{ |
| 303 |
uint32_t legacy = dst + len <= UINT32_MAX - 1 ? 1 : 0; |
| 304 |
uintptr_t dram_start, dram_end; |
| 305 |
uintptr_t prot_start, prot_end; |
| 306 |
int32_t result = IO_SUCCESS; |
| 307 |
int n; |
| 308 |
|
| 309 |
dram_start = legacy ? DRAM1_NS_BASE : DRAM_40BIT_BASE; |
| 310 |
|
| 311 |
dram_end = legacy ? DRAM1_NS_BASE + DRAM1_NS_SIZE : |
| 312 |
DRAM_40BIT_BASE + DRAM_40BIT_SIZE; |
| 313 |
|
| 314 |
prot_start = legacy ? DRAM_PROTECTED_BASE : DRAM_40BIT_PROTECTED_BASE; |
| 315 |
|
| 316 |
prot_end = prot_start + DRAM_PROTECTED_SIZE; |
| 317 |
|
| 318 |
if (dst < dram_start || len > dram_end || dst > dram_end - len) { |
| 319 |
ERROR("BL2: dst address is on the protected area.\n"); |
| 320 |
result = IO_FAIL; |
| 321 |
goto done; |
| 322 |
} |
| 323 |
|
| 324 |
/* load image is within SDRAM protected area */ |
| 325 |
if (dst >= prot_start && dst < prot_end) { |
| 326 |
ERROR("BL2: dst address is on the protected area.\n"); |
| 327 |
result = IO_FAIL; |
| 328 |
goto done; |
| 329 |
} |
| 330 |
|
| 331 |
if (len > prot_start || (dst < prot_start && dst > prot_start - len)) { |
| 332 |
ERROR("BL2: %s[%d] loaded data is on the protected area.\n", |
| 333 |
__func__, __LINE__); |
| 334 |
result = IO_FAIL; |
| 335 |
goto done; |
| 336 |
} |
| 337 |
|
| 338 |
if (addr_loaded_cnt >= CHECK_IMAGE_AREA_CNT) { |
| 339 |
ERROR("BL2: max loadable non secure images reached\n"); |
| 340 |
result = IO_FAIL; |
| 341 |
goto done; |
| 342 |
} |
| 343 |
|
| 344 |
addr_loaded[addr_loaded_cnt].dest = dst; |
| 345 |
addr_loaded[addr_loaded_cnt].length = len; |
| 346 |
for (n = 0; n < addr_loaded_cnt; n++) { |
| 347 |
/* |
| 348 |
* Check if next image invades a previous loaded image |
| 349 |
* |
| 350 |
* IMAGE n: area from previous image: dest| IMAGE n |length |
| 351 |
* IMAGE n+1: area from next image: dst | IMAGE n |len |
| 352 |
* |
| 353 |
* 1. check: |
| 354 |
* | IMAGE n | |
| 355 |
* | IMAGE n+1 | |
| 356 |
* 2. check: |
| 357 |
* | IMAGE n | |
| 358 |
* | IMAGE n+1 | |
| 359 |
* 3. check: |
| 360 |
* | IMAGE n | |
| 361 |
* | IMAGE n+1 | |
| 362 |
*/ |
| 363 |
if (((dst >= addr_loaded[n].dest) && |
| 364 |
(dst <= addr_loaded[n].dest + addr_loaded[n].length)) || |
| 365 |
((dst + len >= addr_loaded[n].dest) && |
| 366 |
(dst + len <= addr_loaded[n].dest + addr_loaded[n].length)) || |
| 367 |
((dst <= addr_loaded[n].dest) && |
| 368 |
(dst + len >= addr_loaded[n].dest + addr_loaded[n].length))) { |
| 369 |
ERROR("BL2: next image overlap a previous image area.\n"); |
| 370 |
result = IO_FAIL; |
| 371 |
goto done; |
| 372 |
} |
| 373 |
} |
| 374 |
addr_loaded_cnt++; |
| 375 |
|
| 376 |
done: |
| 377 |
if (result == IO_FAIL) { |
| 378 |
ERROR("BL2: Out of range : dst=0x%lx len=0x%lx\n", dst, len); |
| 379 |
} |
| 380 |
|
| 381 |
return result; |
| 382 |
} |
| 383 |
|
| 384 |
static int32_t load_bl33x(void) |
| 385 |
{ |
| 386 |
static int32_t loaded = IO_NOT_SUPPORTED; |
| 387 |
uintptr_t dst, partition, handle; |
| 388 |
uint32_t noload, cert, len, i; |
| 389 |
uintptr_t offset; |
| 390 |
int32_t rc; |
| 391 |
size_t cnt; |
| 392 |
const int32_t img[] = { |
| 393 |
BL33_IMAGE_ID, |
| 394 |
BL332_IMAGE_ID, |
| 395 |
BL333_IMAGE_ID, |
| 396 |
BL334_IMAGE_ID, |
| 397 |
BL335_IMAGE_ID, |
| 398 |
BL336_IMAGE_ID, |
| 399 |
BL337_IMAGE_ID, |
| 400 |
BL338_IMAGE_ID |
| 401 |
}; |
| 402 |
|
| 403 |
if (loaded != IO_NOT_SUPPORTED) { |
| 404 |
return loaded; |
| 405 |
} |
| 406 |
|
| 407 |
for (i = 1; i < rcar_image_number; i++) { |
| 408 |
rc = file_to_offset(img[i], &offset, &cert, &noload, |
| 409 |
&partition); |
| 410 |
if (rc != IO_SUCCESS) { |
| 411 |
WARN("%s: failed to get offset\n", __func__); |
| 412 |
loaded = IO_FAIL; |
| 413 |
return loaded; |
| 414 |
} |
| 415 |
|
| 416 |
rcar_read_certificate((uint64_t) cert, &len, &dst); |
| 417 |
((io_drv_spec_t *) rcar_spec)->partition = partition; |
| 418 |
|
| 419 |
rc = io_open(rcar_handle, rcar_spec, &handle); |
| 420 |
if (rc != IO_SUCCESS) { |
| 421 |
WARN("%s: Failed to open FIP (%i)\n", __func__, rc); |
| 422 |
loaded = IO_FAIL; |
| 423 |
return loaded; |
| 424 |
} |
| 425 |
|
| 426 |
rc = io_seek(handle, IO_SEEK_SET, offset); |
| 427 |
if (rc != IO_SUCCESS) { |
| 428 |
WARN("%s: failed to seek\n", __func__); |
| 429 |
loaded = IO_FAIL; |
| 430 |
return loaded; |
| 431 |
} |
| 432 |
|
| 433 |
rc = check_load_area(dst, len); |
| 434 |
if (rc != IO_SUCCESS) { |
| 435 |
WARN("%s: check load area\n", __func__); |
| 436 |
loaded = IO_FAIL; |
| 437 |
return loaded; |
| 438 |
} |
| 439 |
|
| 440 |
rc = io_read(handle, dst, len, &cnt); |
| 441 |
if (rc != IO_SUCCESS) { |
| 442 |
WARN("%s: failed to read\n", __func__); |
| 443 |
loaded = IO_FAIL; |
| 444 |
return loaded; |
| 445 |
} |
| 446 |
#if TRUSTED_BOARD_BOOT |
| 447 |
rc = auth_mod_verify_img(img[i], (void *)dst, len); |
| 448 |
if (rc != 0) { |
| 449 |
memset((void *)dst, 0x00, len); |
| 450 |
loaded = IO_FAIL; |
| 451 |
return loaded; |
| 452 |
} |
| 453 |
#endif |
| 454 |
io_close(handle); |
| 455 |
} |
| 456 |
|
| 457 |
loaded = IO_SUCCESS; |
| 458 |
|
| 459 |
return loaded; |
| 460 |
} |
| 461 |
|
| 462 |
static int32_t rcar_dev_init(io_dev_info_t *dev_info, const uintptr_t name) |
| 463 |
{ |
| 464 |
static uint64_t header[64] __aligned(FLASH_TRANS_SIZE_UNIT) = {0UL}; |
| 465 |
uintptr_t handle; |
| 466 |
ssize_t offset; |
| 467 |
uint32_t i; |
| 468 |
int32_t rc; |
| 469 |
size_t cnt; |
| 470 |
|
| 471 |
/* Obtain a reference to the image by querying the platform layer */ |
| 472 |
rc = plat_get_drv_source(name, &rcar_handle, &rcar_spec); |
| 473 |
if (rc != IO_SUCCESS) { |
| 474 |
WARN("Failed to obtain reference to img %ld (%i)\n", name, rc); |
| 475 |
return IO_FAIL; |
| 476 |
} |
| 477 |
|
| 478 |
if (rcar_cert_load == RCAR_CERT_LOAD) { |
| 479 |
return IO_SUCCESS; |
| 480 |
} |
| 481 |
|
| 482 |
rc = io_open(rcar_handle, rcar_spec, &handle); |
| 483 |
if (rc != IO_SUCCESS) { |
| 484 |
WARN("Failed to access img %ld (%i)\n", name, rc); |
| 485 |
return IO_FAIL; |
| 486 |
} |
| 487 |
|
| 488 |
/* |
| 489 |
* get start address list |
| 490 |
* [0] address num |
| 491 |
* [1] BL33-1 image address |
| 492 |
* [2] BL33-2 image address |
| 493 |
* [3] BL33-3 image address |
| 494 |
* [4] BL33-4 image address |
| 495 |
* [5] BL33-5 image address |
| 496 |
* [6] BL33-6 image address |
| 497 |
* [7] BL33-7 image address |
| 498 |
* [8] BL33-8 image address |
| 499 |
*/ |
| 500 |
offset = name == EMMC_DEV_ID ? RCAR_EMMC_CERT_HEADER : |
| 501 |
RCAR_FLASH_CERT_HEADER; |
| 502 |
rc = io_seek(handle, IO_SEEK_SET, offset); |
| 503 |
if (rc != IO_SUCCESS) { |
| 504 |
WARN("Firmware Image Package header failed to seek\n"); |
| 505 |
goto error; |
| 506 |
} |
| 507 |
|
| 508 |
rc = io_read(handle, (uintptr_t) &header, sizeof(header), &cnt); |
| 509 |
if (rc != IO_SUCCESS) { |
| 510 |
WARN("Firmware Image Package header failed to read\n"); |
| 511 |
goto error; |
| 512 |
} |
| 513 |
|
| 514 |
#if RCAR_BL2_DCACHE == 1 |
| 515 |
inv_dcache_range((uint64_t) header, sizeof(header)); |
| 516 |
#endif |
| 517 |
|
| 518 |
rcar_image_number = header[0]; |
| 519 |
if (rcar_image_number == 0 || rcar_image_number > RCAR_MAX_BL3X_IMAGE) { |
| 520 |
WARN("Firmware Image Package header check failed.\n"); |
| 521 |
rc = IO_FAIL; |
| 522 |
goto error; |
| 523 |
} |
| 524 |
|
| 525 |
for (i = 0; i < rcar_image_number + 2; i++) { |
| 526 |
rcar_image_header[i] = header[i * 2 + 1]; |
| 527 |
rcar_image_header_prttn[i] = header[i * 2 + 2]; |
| 528 |
} |
| 529 |
|
| 530 |
rc = io_seek(handle, IO_SEEK_SET, offset + RCAR_SECTOR6_CERT_OFFSET); |
| 531 |
if (rc != IO_SUCCESS) { |
| 532 |
WARN("Firmware Image Package header failed to seek cert\n"); |
| 533 |
goto error; |
| 534 |
} |
| 535 |
|
| 536 |
rc = io_read(handle, RCAR_SDRAM_certESS, |
| 537 |
RCAR_CERT_SIZE * (2 + rcar_image_number), &cnt); |
| 538 |
if (rc != IO_SUCCESS) { |
| 539 |
WARN("cert file read error.\n"); |
| 540 |
goto error; |
| 541 |
} |
| 542 |
|
| 543 |
#if RCAR_BL2_DCACHE == 1 |
| 544 |
inv_dcache_range(RCAR_SDRAM_certESS, |
| 545 |
RCAR_CERT_SIZE * (2 + rcar_image_number)); |
| 546 |
#endif |
| 547 |
|
| 548 |
rcar_cert_load = RCAR_CERT_LOAD; |
| 549 |
error: |
| 550 |
|
| 551 |
if (rc != IO_SUCCESS) { |
| 552 |
rc = IO_FAIL; |
| 553 |
} |
| 554 |
|
| 555 |
io_close(handle); |
| 556 |
|
| 557 |
return rc; |
| 558 |
|
| 559 |
} |
| 560 |
|
| 561 |
static int32_t rcar_file_open(io_dev_info_t *info, const uintptr_t file_spec, |
| 562 |
io_entity_t *entity) |
| 563 |
{ |
| 564 |
const io_drv_spec_t *spec = (io_drv_spec_t *) file_spec; |
| 565 |
uintptr_t partition, offset, dst; |
| 566 |
uint32_t noload, cert, len; |
| 567 |
int32_t rc; |
| 568 |
|
| 569 |
/* |
| 570 |
* Only one file open at a time. We need to track state (ie, file |
| 571 |
* cursor position). Since the header lives at offset zero, this entry |
| 572 |
* should never be zero in an active file. |
| 573 |
* Once the system supports dynamic memory allocation we will allow more |
| 574 |
* than one open file at a time. |
| 575 |
*/ |
| 576 |
if (current_file.offset != 0U) { |
| 577 |
WARN("%s: Only one open file at a time.\n", __func__); |
| 578 |
return IO_RESOURCES_EXHAUSTED; |
| 579 |
} |
| 580 |
|
| 581 |
rc = file_to_offset(spec->offset, &offset, &cert, &noload, &partition); |
| 582 |
if (rc != IO_SUCCESS) { |
| 583 |
WARN("Failed to open file name %ld (%i)\n", spec->offset, rc); |
| 584 |
return IO_FAIL; |
| 585 |
} |
| 586 |
|
| 587 |
if (noload != 0U) { |
| 588 |
current_file.offset = 1; |
| 589 |
current_file.dst = 0; |
| 590 |
current_file.size = 1; |
| 591 |
current_file.position = 0; |
| 592 |
current_file.no_load = noload; |
| 593 |
current_file.partition = 0; |
| 594 |
entity->info = (uintptr_t) ¤t_file; |
| 595 |
|
| 596 |
return IO_SUCCESS; |
| 597 |
} |
| 598 |
|
| 599 |
rcar_read_certificate((uint64_t) cert, &len, &dst); |
| 600 |
|
| 601 |
current_file.partition = partition; |
| 602 |
current_file.no_load = noload; |
| 603 |
current_file.offset = offset; |
| 604 |
current_file.position = 0; |
| 605 |
current_file.size = len; |
| 606 |
current_file.dst = dst; |
| 607 |
entity->info = (uintptr_t) ¤t_file; |
| 608 |
|
| 609 |
return IO_SUCCESS; |
| 610 |
} |
| 611 |
|
| 612 |
static int32_t rcar_file_len(io_entity_t *entity, size_t *length) |
| 613 |
{ |
| 614 |
*length = ((file_state_t *) entity->info)->size; |
| 615 |
|
| 616 |
NOTICE("%s: len: 0x%08lx\n", __func__, *length); |
| 617 |
|
| 618 |
return IO_SUCCESS; |
| 619 |
} |
| 620 |
|
| 621 |
static int32_t rcar_file_read(io_entity_t *entity, uintptr_t buffer, |
| 622 |
size_t length, size_t *cnt) |
| 623 |
{ |
| 624 |
file_state_t *fp = (file_state_t *) entity->info; |
| 625 |
ssize_t offset = fp->offset + fp->position; |
| 626 |
uintptr_t handle; |
| 627 |
int32_t rc; |
| 628 |
|
| 629 |
#ifdef SPD_NONE |
| 630 |
static uint32_t load_bl33x_counter = 1; |
| 631 |
#else |
| 632 |
static uint32_t load_bl33x_counter; |
| 633 |
#endif |
| 634 |
if (current_file.no_load != 0U) { |
| 635 |
*cnt = length; |
| 636 |
return IO_SUCCESS; |
| 637 |
} |
| 638 |
|
| 639 |
((io_drv_spec_t *) rcar_spec)->partition = fp->partition; |
| 640 |
|
| 641 |
rc = io_open(rcar_handle, rcar_spec, &handle); |
| 642 |
if (rc != IO_SUCCESS) { |
| 643 |
WARN("Failed to open FIP (%i)\n", rc); |
| 644 |
return IO_FAIL; |
| 645 |
} |
| 646 |
|
| 647 |
rc = io_seek(handle, IO_SEEK_SET, offset); |
| 648 |
if (rc != IO_SUCCESS) { |
| 649 |
WARN("%s: failed to seek\n", __func__); |
| 650 |
goto error; |
| 651 |
} |
| 652 |
|
| 653 |
if (load_bl33x_counter == RCAR_COUNT_LOAD_BL33) { |
| 654 |
rc = check_load_area(buffer, length); |
| 655 |
if (rc != IO_SUCCESS) { |
| 656 |
WARN("%s: load area err\n", __func__); |
| 657 |
goto error; |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
rc = io_read(handle, buffer, length, cnt); |
| 662 |
if (rc != IO_SUCCESS) { |
| 663 |
WARN("Failed to read payload (%i)\n", rc); |
| 664 |
goto error; |
| 665 |
} |
| 666 |
|
| 667 |
fp->position += *cnt; |
| 668 |
io_close(handle); |
| 669 |
|
| 670 |
load_bl33x_counter += 1; |
| 671 |
if (load_bl33x_counter == RCAR_COUNT_LOAD_BL33X) { |
| 672 |
return load_bl33x(); |
| 673 |
} |
| 674 |
|
| 675 |
return IO_SUCCESS; |
| 676 |
error: |
| 677 |
io_close(handle); |
| 678 |
return IO_FAIL; |
| 679 |
} |
| 680 |
|
| 681 |
static int32_t rcar_file_close(io_entity_t *entity) |
| 682 |
{ |
| 683 |
if (current_file.offset != 0U) { |
| 684 |
memset(¤t_file, 0, sizeof(current_file)); |
| 685 |
} |
| 686 |
|
| 687 |
entity->info = 0U; |
| 688 |
|
| 689 |
return IO_SUCCESS; |
| 690 |
} |
| 691 |
|
| 692 |
static const io_dev_funcs_t rcar_dev_funcs = { |
| 693 |
.type = &device_type_rcar, |
| 694 |
.open = &rcar_file_open, |
| 695 |
.seek = NULL, |
| 696 |
.size = &rcar_file_len, |
| 697 |
.read = &rcar_file_read, |
| 698 |
.write = NULL, |
| 699 |
.close = &rcar_file_close, |
| 700 |
.dev_init = &rcar_dev_init, |
| 701 |
.dev_close = &rcar_dev_close, |
| 702 |
}; |
| 703 |
|
| 704 |
static const io_dev_info_t rcar_dev_info = { |
| 705 |
.funcs = &rcar_dev_funcs, |
| 706 |
.info = (uintptr_t) 0 |
| 707 |
}; |
| 708 |
|
| 709 |
static const io_dev_connector_t rcar_dev_connector = { |
| 710 |
.dev_open = &rcar_dev_open |
| 711 |
}; |
| 712 |
|
| 713 |
static int32_t rcar_dev_open(const uintptr_t dev_spec __attribute__ ((unused)), |
| 714 |
io_dev_info_t **dev_info) |
| 715 |
{ |
| 716 |
*dev_info = (io_dev_info_t *) &rcar_dev_info; |
| 717 |
|
| 718 |
return IO_SUCCESS; |
| 719 |
} |
| 720 |
|
| 721 |
static int32_t rcar_dev_close(io_dev_info_t *dev_info) |
| 722 |
{ |
| 723 |
rcar_handle = 0; |
| 724 |
rcar_spec = 0; |
| 725 |
|
| 726 |
return IO_SUCCESS; |
| 727 |
} |
| 728 |
|
| 729 |
int32_t rcar_register_io_dev(const io_dev_connector_t **dev_con) |
| 730 |
{ |
| 731 |
int32_t result; |
| 732 |
|
| 733 |
result = io_register_device(&rcar_dev_info); |
| 734 |
if (result == IO_SUCCESS) { |
| 735 |
*dev_con = &rcar_dev_connector; |
| 736 |
} |
| 737 |
|
| 738 |
return result; |
| 739 |
} |