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