Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 1 | /* |
Haojian Zhuang | b755da3 | 2018-01-25 16:10:14 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 7 | #include <assert.h> |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 8 | #include <errno.h> |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 9 | #include <string.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
| 11 | #include <platform_def.h> |
| 12 | |
| 13 | #include <arch_helpers.h> |
| 14 | #include <common/debug.h> |
| 15 | #include <drivers/io/io_block.h> |
| 16 | #include <drivers/io/io_driver.h> |
| 17 | #include <drivers/io/io_fip.h> |
| 18 | #include <drivers/io/io_memmap.h> |
| 19 | #include <drivers/io/io_storage.h> |
| 20 | #include <drivers/mmc.h> |
Haojian Zhuang | 53e70df | 2019-09-14 19:18:01 +0800 | [diff] [blame] | 21 | #include <drivers/partition/partition.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 22 | #include <lib/mmio.h> |
| 23 | #include <lib/semihosting.h> |
| 24 | #include <tools_share/firmware_image_package.h> |
| 25 | |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 26 | #include "hikey_private.h" |
| 27 | |
| 28 | #define EMMC_BLOCK_SHIFT 9 |
| 29 | |
| 30 | /* Page 1024, since only a few pages before 2048 are used as partition table */ |
| 31 | #define SERIALNO_EMMC_OFFSET (1024 * 512) |
| 32 | |
| 33 | struct plat_io_policy { |
| 34 | uintptr_t *dev_handle; |
| 35 | uintptr_t image_spec; |
| 36 | int (*check)(const uintptr_t spec); |
| 37 | }; |
| 38 | |
| 39 | static const io_dev_connector_t *emmc_dev_con; |
| 40 | static uintptr_t emmc_dev_handle; |
| 41 | static const io_dev_connector_t *fip_dev_con; |
| 42 | static uintptr_t fip_dev_handle; |
| 43 | |
| 44 | static int check_emmc(const uintptr_t spec); |
| 45 | static int check_fip(const uintptr_t spec); |
| 46 | |
Haojian Zhuang | 53e70df | 2019-09-14 19:18:01 +0800 | [diff] [blame] | 47 | static io_block_spec_t emmc_fip_spec; |
| 48 | |
| 49 | static const io_block_spec_t emmc_gpt_spec = { |
| 50 | .offset = 0, |
| 51 | .length = PLAT_PARTITION_BLOCK_SIZE * |
| 52 | (PLAT_PARTITION_MAX_ENTRIES / 4 + 2), |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | static const io_block_dev_spec_t emmc_dev_spec = { |
| 56 | /* It's used as temp buffer in block driver. */ |
Roberto Vargas | 8247796 | 2017-10-23 08:22:17 +0100 | [diff] [blame] | 57 | #ifdef IMAGE_BL1 |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 58 | .buffer = { |
| 59 | .offset = HIKEY_BL1_MMC_DATA_BASE, |
| 60 | .length = HIKEY_BL1_MMC_DATA_SIZE, |
| 61 | }, |
| 62 | #else |
| 63 | .buffer = { |
| 64 | .offset = HIKEY_MMC_DATA_BASE, |
| 65 | .length = HIKEY_MMC_DATA_SIZE, |
| 66 | }, |
| 67 | #endif |
| 68 | .ops = { |
Haojian Zhuang | e971377 | 2018-08-04 18:07:10 +0800 | [diff] [blame] | 69 | .read = mmc_read_blocks, |
| 70 | .write = mmc_write_blocks, |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 71 | }, |
Haojian Zhuang | e971377 | 2018-08-04 18:07:10 +0800 | [diff] [blame] | 72 | .block_size = MMC_BLOCK_SIZE, |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 73 | }; |
| 74 | |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 75 | static const io_uuid_spec_t bl31_uuid_spec = { |
| 76 | .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31, |
| 77 | }; |
| 78 | |
Victor Chong | b9a8db2 | 2017-05-28 00:14:25 +0900 | [diff] [blame] | 79 | static const io_uuid_spec_t bl32_uuid_spec = { |
| 80 | .uuid = UUID_SECURE_PAYLOAD_BL32, |
| 81 | }; |
| 82 | |
Victor Chong | 7d787f5 | 2017-08-16 13:53:56 +0900 | [diff] [blame] | 83 | static const io_uuid_spec_t bl32_extra1_uuid_spec = { |
| 84 | .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1, |
| 85 | }; |
| 86 | |
| 87 | static const io_uuid_spec_t bl32_extra2_uuid_spec = { |
| 88 | .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2, |
| 89 | }; |
| 90 | |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 91 | static const io_uuid_spec_t bl33_uuid_spec = { |
| 92 | .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33, |
| 93 | }; |
| 94 | |
| 95 | static const io_uuid_spec_t scp_bl2_uuid_spec = { |
| 96 | .uuid = UUID_SCP_FIRMWARE_SCP_BL2, |
| 97 | }; |
| 98 | |
Teddy Reed | 349cf89 | 2018-06-22 22:23:36 -0400 | [diff] [blame] | 99 | #if TRUSTED_BOARD_BOOT |
| 100 | static const io_uuid_spec_t trusted_key_cert_uuid_spec = { |
| 101 | .uuid = UUID_TRUSTED_KEY_CERT, |
| 102 | }; |
| 103 | |
| 104 | static const io_uuid_spec_t scp_fw_key_cert_uuid_spec = { |
| 105 | .uuid = UUID_SCP_FW_KEY_CERT, |
| 106 | }; |
| 107 | |
| 108 | static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = { |
| 109 | .uuid = UUID_SOC_FW_KEY_CERT, |
| 110 | }; |
| 111 | |
| 112 | static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = { |
| 113 | .uuid = UUID_TRUSTED_OS_FW_KEY_CERT, |
| 114 | }; |
| 115 | |
| 116 | static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = { |
| 117 | .uuid = UUID_NON_TRUSTED_FW_KEY_CERT, |
| 118 | }; |
| 119 | |
| 120 | static const io_uuid_spec_t scp_fw_cert_uuid_spec = { |
| 121 | .uuid = UUID_SCP_FW_CONTENT_CERT, |
| 122 | }; |
| 123 | |
| 124 | static const io_uuid_spec_t soc_fw_cert_uuid_spec = { |
| 125 | .uuid = UUID_SOC_FW_CONTENT_CERT, |
| 126 | }; |
| 127 | |
| 128 | static const io_uuid_spec_t tos_fw_cert_uuid_spec = { |
| 129 | .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT, |
| 130 | }; |
| 131 | |
| 132 | static const io_uuid_spec_t nt_fw_cert_uuid_spec = { |
| 133 | .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT, |
| 134 | }; |
| 135 | #endif /* TRUSTED_BOARD_BOOT */ |
| 136 | |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 137 | static const struct plat_io_policy policies[] = { |
| 138 | [FIP_IMAGE_ID] = { |
| 139 | &emmc_dev_handle, |
| 140 | (uintptr_t)&emmc_fip_spec, |
| 141 | check_emmc |
| 142 | }, |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 143 | [SCP_BL2_IMAGE_ID] = { |
| 144 | &fip_dev_handle, |
| 145 | (uintptr_t)&scp_bl2_uuid_spec, |
| 146 | check_fip |
| 147 | }, |
| 148 | [BL31_IMAGE_ID] = { |
| 149 | &fip_dev_handle, |
| 150 | (uintptr_t)&bl31_uuid_spec, |
| 151 | check_fip |
| 152 | }, |
Victor Chong | b9a8db2 | 2017-05-28 00:14:25 +0900 | [diff] [blame] | 153 | [BL32_IMAGE_ID] = { |
| 154 | &fip_dev_handle, |
| 155 | (uintptr_t)&bl32_uuid_spec, |
| 156 | check_fip |
| 157 | }, |
Victor Chong | 7d787f5 | 2017-08-16 13:53:56 +0900 | [diff] [blame] | 158 | [BL32_EXTRA1_IMAGE_ID] = { |
| 159 | &fip_dev_handle, |
| 160 | (uintptr_t)&bl32_extra1_uuid_spec, |
| 161 | check_fip |
| 162 | }, |
| 163 | [BL32_EXTRA2_IMAGE_ID] = { |
| 164 | &fip_dev_handle, |
| 165 | (uintptr_t)&bl32_extra2_uuid_spec, |
| 166 | check_fip |
| 167 | }, |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 168 | [BL33_IMAGE_ID] = { |
| 169 | &fip_dev_handle, |
| 170 | (uintptr_t)&bl33_uuid_spec, |
| 171 | check_fip |
Teddy Reed | 349cf89 | 2018-06-22 22:23:36 -0400 | [diff] [blame] | 172 | }, |
| 173 | #if TRUSTED_BOARD_BOOT |
| 174 | [TRUSTED_KEY_CERT_ID] = { |
| 175 | &fip_dev_handle, |
| 176 | (uintptr_t)&trusted_key_cert_uuid_spec, |
| 177 | check_fip |
| 178 | }, |
| 179 | [SCP_FW_KEY_CERT_ID] = { |
| 180 | &fip_dev_handle, |
| 181 | (uintptr_t)&scp_fw_key_cert_uuid_spec, |
| 182 | check_fip |
| 183 | }, |
| 184 | [SOC_FW_KEY_CERT_ID] = { |
| 185 | &fip_dev_handle, |
| 186 | (uintptr_t)&soc_fw_key_cert_uuid_spec, |
| 187 | check_fip |
| 188 | }, |
| 189 | [TRUSTED_OS_FW_KEY_CERT_ID] = { |
| 190 | &fip_dev_handle, |
| 191 | (uintptr_t)&tos_fw_key_cert_uuid_spec, |
| 192 | check_fip |
| 193 | }, |
| 194 | [NON_TRUSTED_FW_KEY_CERT_ID] = { |
| 195 | &fip_dev_handle, |
| 196 | (uintptr_t)&nt_fw_key_cert_uuid_spec, |
| 197 | check_fip |
| 198 | }, |
| 199 | [SCP_FW_CONTENT_CERT_ID] = { |
| 200 | &fip_dev_handle, |
| 201 | (uintptr_t)&scp_fw_cert_uuid_spec, |
| 202 | check_fip |
| 203 | }, |
| 204 | [SOC_FW_CONTENT_CERT_ID] = { |
| 205 | &fip_dev_handle, |
| 206 | (uintptr_t)&soc_fw_cert_uuid_spec, |
| 207 | check_fip |
| 208 | }, |
| 209 | [TRUSTED_OS_FW_CONTENT_CERT_ID] = { |
| 210 | &fip_dev_handle, |
| 211 | (uintptr_t)&tos_fw_cert_uuid_spec, |
| 212 | check_fip |
| 213 | }, |
| 214 | [NON_TRUSTED_FW_CONTENT_CERT_ID] = { |
| 215 | &fip_dev_handle, |
| 216 | (uintptr_t)&nt_fw_cert_uuid_spec, |
| 217 | check_fip |
| 218 | }, |
| 219 | #endif /* TRUSTED_BOARD_BOOT */ |
Haojian Zhuang | 53e70df | 2019-09-14 19:18:01 +0800 | [diff] [blame] | 220 | [GPT_IMAGE_ID] = { |
| 221 | &emmc_dev_handle, |
| 222 | (uintptr_t)&emmc_gpt_spec, |
| 223 | check_emmc |
| 224 | }, |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | static int check_emmc(const uintptr_t spec) |
| 228 | { |
| 229 | int result; |
| 230 | uintptr_t local_handle; |
| 231 | |
| 232 | result = io_dev_init(emmc_dev_handle, (uintptr_t)NULL); |
| 233 | if (result == 0) { |
| 234 | result = io_open(emmc_dev_handle, spec, &local_handle); |
| 235 | if (result == 0) |
| 236 | io_close(local_handle); |
| 237 | } |
| 238 | return result; |
| 239 | } |
| 240 | |
| 241 | static int check_fip(const uintptr_t spec) |
| 242 | { |
| 243 | int result; |
| 244 | uintptr_t local_image_handle; |
| 245 | |
| 246 | /* See if a Firmware Image Package is available */ |
| 247 | result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID); |
| 248 | if (result == 0) { |
| 249 | result = io_open(fip_dev_handle, spec, &local_image_handle); |
| 250 | if (result == 0) { |
| 251 | VERBOSE("Using FIP\n"); |
| 252 | io_close(local_image_handle); |
| 253 | } |
| 254 | } |
| 255 | return result; |
| 256 | } |
| 257 | |
| 258 | void hikey_io_setup(void) |
| 259 | { |
| 260 | int result; |
| 261 | |
| 262 | result = register_io_dev_block(&emmc_dev_con); |
| 263 | assert(result == 0); |
| 264 | |
| 265 | result = register_io_dev_fip(&fip_dev_con); |
| 266 | assert(result == 0); |
| 267 | |
| 268 | result = io_dev_open(emmc_dev_con, (uintptr_t)&emmc_dev_spec, |
| 269 | &emmc_dev_handle); |
| 270 | assert(result == 0); |
| 271 | |
| 272 | result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle); |
| 273 | assert(result == 0); |
| 274 | |
| 275 | /* Ignore improbable errors in release builds */ |
| 276 | (void)result; |
| 277 | } |
| 278 | |
Haojian Zhuang | 53e70df | 2019-09-14 19:18:01 +0800 | [diff] [blame] | 279 | int hikey_set_fip_addr(unsigned int image_id, const char *name) |
| 280 | { |
| 281 | const partition_entry_t *entry; |
| 282 | |
| 283 | if (emmc_fip_spec.length == 0) { |
| 284 | partition_init(GPT_IMAGE_ID); |
| 285 | entry = get_partition_entry(name); |
| 286 | if (entry == NULL) { |
| 287 | ERROR("Could NOT find the %s partition!\n", name); |
| 288 | return -ENOENT; |
| 289 | } |
| 290 | emmc_fip_spec.offset = entry->start; |
| 291 | emmc_fip_spec.length = entry->length; |
| 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
Haojian Zhuang | 5f281b3 | 2017-05-24 08:45:05 +0800 | [diff] [blame] | 296 | /* Return an IO device handle and specification which can be used to access |
| 297 | * an image. Use this to enforce platform load policy |
| 298 | */ |
| 299 | int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, |
| 300 | uintptr_t *image_spec) |
| 301 | { |
| 302 | int result; |
| 303 | const struct plat_io_policy *policy; |
| 304 | |
| 305 | assert(image_id < ARRAY_SIZE(policies)); |
| 306 | |
| 307 | policy = &policies[image_id]; |
| 308 | result = policy->check(policy->image_spec); |
| 309 | assert(result == 0); |
| 310 | |
| 311 | *image_spec = policy->image_spec; |
| 312 | *dev_handle = *(policy->dev_handle); |
| 313 | |
| 314 | return result; |
| 315 | } |