Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 1 | /* |
Jens Wiklander | d4b84f0 | 2022-11-18 15:40:04 +0100 | [diff] [blame] | 2 | * Copyright (c) 2015-2022, ARM Limited and Contributors. All rights reserved. |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 8 | #include <string.h> |
| 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <platform_def.h> |
| 11 | |
| 12 | #include <common/bl_common.h> |
| 13 | #include <common/debug.h> |
Jens Wiklander | a43c128 | 2022-11-22 14:39:26 +0100 | [diff] [blame] | 14 | #include <common/desc_image_load.h> |
| 15 | #include <common/uuid.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 16 | #include <drivers/io/io_driver.h> |
Sumit Garg | 4ac3034 | 2019-11-14 17:34:56 +0530 | [diff] [blame] | 17 | #include <drivers/io/io_encrypted.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 18 | #include <drivers/io/io_fip.h> |
| 19 | #include <drivers/io/io_memmap.h> |
| 20 | #include <drivers/io/io_semihosting.h> |
| 21 | #include <drivers/io/io_storage.h> |
| 22 | #include <lib/semihosting.h> |
| 23 | #include <tools_share/firmware_image_package.h> |
| 24 | |
Jens Wiklander | a43c128 | 2022-11-22 14:39:26 +0100 | [diff] [blame] | 25 | #include "qemu_private.h" |
| 26 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 27 | /* Semihosting filenames */ |
| 28 | #define BL2_IMAGE_NAME "bl2.bin" |
| 29 | #define BL31_IMAGE_NAME "bl31.bin" |
| 30 | #define BL32_IMAGE_NAME "bl32.bin" |
Jens Wiklander | a43c128 | 2022-11-22 14:39:26 +0100 | [diff] [blame] | 31 | #define TB_FW_CONFIG_NAME "tb_fw_config.dtb" |
Jens Wiklander | d4b84f0 | 2022-11-18 15:40:04 +0100 | [diff] [blame] | 32 | #define TOS_FW_CONFIG_NAME "tos_fw_config.dtb" |
Jens Wiklander | 0acbaaa | 2017-08-24 13:16:26 +0200 | [diff] [blame] | 33 | #define BL32_EXTRA1_IMAGE_NAME "bl32_extra1.bin" |
| 34 | #define BL32_EXTRA2_IMAGE_NAME "bl32_extra2.bin" |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 35 | #define BL33_IMAGE_NAME "bl33.bin" |
| 36 | |
| 37 | #if TRUSTED_BOARD_BOOT |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 38 | #define TRUSTED_BOOT_FW_CERT_NAME "tb_fw.crt" |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 39 | #define TRUSTED_KEY_CERT_NAME "trusted_key.crt" |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 40 | #define SOC_FW_KEY_CERT_NAME "soc_fw_key.crt" |
| 41 | #define TOS_FW_KEY_CERT_NAME "tos_fw_key.crt" |
| 42 | #define NT_FW_KEY_CERT_NAME "nt_fw_key.crt" |
| 43 | #define SOC_FW_CONTENT_CERT_NAME "soc_fw_content.crt" |
| 44 | #define TOS_FW_CONTENT_CERT_NAME "tos_fw_content.crt" |
| 45 | #define NT_FW_CONTENT_CERT_NAME "nt_fw_content.crt" |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 46 | #endif /* TRUSTED_BOARD_BOOT */ |
| 47 | |
| 48 | |
| 49 | |
| 50 | /* IO devices */ |
| 51 | static const io_dev_connector_t *fip_dev_con; |
| 52 | static uintptr_t fip_dev_handle; |
| 53 | static const io_dev_connector_t *memmap_dev_con; |
| 54 | static uintptr_t memmap_dev_handle; |
| 55 | static const io_dev_connector_t *sh_dev_con; |
| 56 | static uintptr_t sh_dev_handle; |
Sumit Garg | 4ac3034 | 2019-11-14 17:34:56 +0530 | [diff] [blame] | 57 | #ifndef DECRYPTION_SUPPORT_none |
| 58 | static const io_dev_connector_t *enc_dev_con; |
| 59 | static uintptr_t enc_dev_handle; |
| 60 | #endif |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 61 | |
| 62 | static const io_block_spec_t fip_block_spec = { |
| 63 | .offset = PLAT_QEMU_FIP_BASE, |
| 64 | .length = PLAT_QEMU_FIP_MAX_SIZE |
| 65 | }; |
| 66 | |
| 67 | static const io_uuid_spec_t bl2_uuid_spec = { |
| 68 | .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2, |
| 69 | }; |
| 70 | |
| 71 | static const io_uuid_spec_t bl31_uuid_spec = { |
| 72 | .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31, |
| 73 | }; |
| 74 | |
| 75 | static const io_uuid_spec_t bl32_uuid_spec = { |
| 76 | .uuid = UUID_SECURE_PAYLOAD_BL32, |
| 77 | }; |
| 78 | |
Jens Wiklander | 0acbaaa | 2017-08-24 13:16:26 +0200 | [diff] [blame] | 79 | static const io_uuid_spec_t bl32_extra1_uuid_spec = { |
| 80 | .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1, |
| 81 | }; |
| 82 | |
| 83 | static const io_uuid_spec_t bl32_extra2_uuid_spec = { |
| 84 | .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2, |
| 85 | }; |
| 86 | |
Jens Wiklander | a43c128 | 2022-11-22 14:39:26 +0100 | [diff] [blame] | 87 | static const io_uuid_spec_t tb_fw_config_uuid_spec = { |
| 88 | .uuid = UUID_TB_FW_CONFIG, |
| 89 | }; |
| 90 | |
Jens Wiklander | d4b84f0 | 2022-11-18 15:40:04 +0100 | [diff] [blame] | 91 | static const io_uuid_spec_t tos_fw_config_uuid_spec = { |
| 92 | .uuid = UUID_TOS_FW_CONFIG, |
| 93 | }; |
| 94 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 95 | static const io_uuid_spec_t bl33_uuid_spec = { |
| 96 | .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33, |
| 97 | }; |
| 98 | |
| 99 | #if TRUSTED_BOARD_BOOT |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 100 | static const io_uuid_spec_t tb_fw_cert_uuid_spec = { |
| 101 | .uuid = UUID_TRUSTED_BOOT_FW_CERT, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | static const io_uuid_spec_t trusted_key_cert_uuid_spec = { |
| 105 | .uuid = UUID_TRUSTED_KEY_CERT, |
| 106 | }; |
| 107 | |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 108 | static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = { |
| 109 | .uuid = UUID_SOC_FW_KEY_CERT, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 110 | }; |
| 111 | |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 112 | static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = { |
| 113 | .uuid = UUID_TRUSTED_OS_FW_KEY_CERT, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 116 | static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = { |
| 117 | .uuid = UUID_NON_TRUSTED_FW_KEY_CERT, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 120 | static const io_uuid_spec_t soc_fw_cert_uuid_spec = { |
| 121 | .uuid = UUID_SOC_FW_CONTENT_CERT, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 122 | }; |
| 123 | |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 124 | static const io_uuid_spec_t tos_fw_cert_uuid_spec = { |
| 125 | .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 126 | }; |
| 127 | |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 128 | static const io_uuid_spec_t nt_fw_cert_uuid_spec = { |
| 129 | .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 130 | }; |
| 131 | #endif /* TRUSTED_BOARD_BOOT */ |
| 132 | |
| 133 | static const io_file_spec_t sh_file_spec[] = { |
| 134 | [BL2_IMAGE_ID] = { |
| 135 | .path = BL2_IMAGE_NAME, |
| 136 | .mode = FOPEN_MODE_RB |
| 137 | }, |
| 138 | [BL31_IMAGE_ID] = { |
| 139 | .path = BL31_IMAGE_NAME, |
| 140 | .mode = FOPEN_MODE_RB |
| 141 | }, |
| 142 | [BL32_IMAGE_ID] = { |
| 143 | .path = BL32_IMAGE_NAME, |
| 144 | .mode = FOPEN_MODE_RB |
| 145 | }, |
Jens Wiklander | 0acbaaa | 2017-08-24 13:16:26 +0200 | [diff] [blame] | 146 | [BL32_EXTRA1_IMAGE_ID] = { |
| 147 | .path = BL32_EXTRA1_IMAGE_NAME, |
| 148 | .mode = FOPEN_MODE_RB |
| 149 | }, |
| 150 | [BL32_EXTRA2_IMAGE_ID] = { |
| 151 | .path = BL32_EXTRA2_IMAGE_NAME, |
| 152 | .mode = FOPEN_MODE_RB |
| 153 | }, |
Jens Wiklander | a43c128 | 2022-11-22 14:39:26 +0100 | [diff] [blame] | 154 | [TB_FW_CONFIG_ID] = { |
| 155 | .path = TB_FW_CONFIG_NAME, |
| 156 | .mode = FOPEN_MODE_RB |
| 157 | }, |
Jens Wiklander | d4b84f0 | 2022-11-18 15:40:04 +0100 | [diff] [blame] | 158 | [TOS_FW_CONFIG_ID] = { |
| 159 | .path = TOS_FW_CONFIG_NAME, |
| 160 | .mode = FOPEN_MODE_RB |
| 161 | }, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 162 | [BL33_IMAGE_ID] = { |
| 163 | .path = BL33_IMAGE_NAME, |
| 164 | .mode = FOPEN_MODE_RB |
| 165 | }, |
| 166 | #if TRUSTED_BOARD_BOOT |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 167 | [TRUSTED_BOOT_FW_CERT_ID] = { |
| 168 | .path = TRUSTED_BOOT_FW_CERT_NAME, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 169 | .mode = FOPEN_MODE_RB |
| 170 | }, |
| 171 | [TRUSTED_KEY_CERT_ID] = { |
| 172 | .path = TRUSTED_KEY_CERT_NAME, |
| 173 | .mode = FOPEN_MODE_RB |
| 174 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 175 | [SOC_FW_KEY_CERT_ID] = { |
| 176 | .path = SOC_FW_KEY_CERT_NAME, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 177 | .mode = FOPEN_MODE_RB |
| 178 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 179 | [TRUSTED_OS_FW_KEY_CERT_ID] = { |
| 180 | .path = TOS_FW_KEY_CERT_NAME, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 181 | .mode = FOPEN_MODE_RB |
| 182 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 183 | [NON_TRUSTED_FW_KEY_CERT_ID] = { |
| 184 | .path = NT_FW_KEY_CERT_NAME, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 185 | .mode = FOPEN_MODE_RB |
| 186 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 187 | [SOC_FW_CONTENT_CERT_ID] = { |
| 188 | .path = SOC_FW_CONTENT_CERT_NAME, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 189 | .mode = FOPEN_MODE_RB |
| 190 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 191 | [TRUSTED_OS_FW_CONTENT_CERT_ID] = { |
| 192 | .path = TOS_FW_CONTENT_CERT_NAME, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 193 | .mode = FOPEN_MODE_RB |
| 194 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 195 | [NON_TRUSTED_FW_CONTENT_CERT_ID] = { |
| 196 | .path = NT_FW_CONTENT_CERT_NAME, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 197 | .mode = FOPEN_MODE_RB |
| 198 | }, |
| 199 | #endif /* TRUSTED_BOARD_BOOT */ |
| 200 | }; |
| 201 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 202 | static int open_fip(const uintptr_t spec); |
| 203 | static int open_memmap(const uintptr_t spec); |
Sumit Garg | 4ac3034 | 2019-11-14 17:34:56 +0530 | [diff] [blame] | 204 | #ifndef DECRYPTION_SUPPORT_none |
| 205 | static int open_enc_fip(const uintptr_t spec); |
| 206 | #endif |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 207 | |
| 208 | struct plat_io_policy { |
| 209 | uintptr_t *dev_handle; |
| 210 | uintptr_t image_spec; |
| 211 | int (*check)(const uintptr_t spec); |
| 212 | }; |
| 213 | |
| 214 | /* By default, ARM platforms load images from the FIP */ |
| 215 | static const struct plat_io_policy policies[] = { |
| 216 | [FIP_IMAGE_ID] = { |
| 217 | &memmap_dev_handle, |
| 218 | (uintptr_t)&fip_block_spec, |
| 219 | open_memmap |
| 220 | }, |
Sumit Garg | 4ac3034 | 2019-11-14 17:34:56 +0530 | [diff] [blame] | 221 | [ENC_IMAGE_ID] = { |
| 222 | &fip_dev_handle, |
| 223 | (uintptr_t)NULL, |
| 224 | open_fip |
| 225 | }, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 226 | [BL2_IMAGE_ID] = { |
| 227 | &fip_dev_handle, |
| 228 | (uintptr_t)&bl2_uuid_spec, |
| 229 | open_fip |
| 230 | }, |
Sumit Garg | 4ac3034 | 2019-11-14 17:34:56 +0530 | [diff] [blame] | 231 | #if ENCRYPT_BL31 && !defined(DECRYPTION_SUPPORT_none) |
| 232 | [BL31_IMAGE_ID] = { |
| 233 | &enc_dev_handle, |
| 234 | (uintptr_t)&bl31_uuid_spec, |
| 235 | open_enc_fip |
| 236 | }, |
| 237 | #else |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 238 | [BL31_IMAGE_ID] = { |
| 239 | &fip_dev_handle, |
| 240 | (uintptr_t)&bl31_uuid_spec, |
| 241 | open_fip |
| 242 | }, |
Sumit Garg | 4ac3034 | 2019-11-14 17:34:56 +0530 | [diff] [blame] | 243 | #endif |
| 244 | #if ENCRYPT_BL32 && !defined(DECRYPTION_SUPPORT_none) |
| 245 | [BL32_IMAGE_ID] = { |
| 246 | &enc_dev_handle, |
| 247 | (uintptr_t)&bl32_uuid_spec, |
| 248 | open_enc_fip |
| 249 | }, |
| 250 | [BL32_EXTRA1_IMAGE_ID] = { |
| 251 | &enc_dev_handle, |
| 252 | (uintptr_t)&bl32_extra1_uuid_spec, |
| 253 | open_enc_fip |
| 254 | }, |
| 255 | [BL32_EXTRA2_IMAGE_ID] = { |
| 256 | &enc_dev_handle, |
| 257 | (uintptr_t)&bl32_extra2_uuid_spec, |
| 258 | open_enc_fip |
| 259 | }, |
| 260 | #else |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 261 | [BL32_IMAGE_ID] = { |
| 262 | &fip_dev_handle, |
| 263 | (uintptr_t)&bl32_uuid_spec, |
| 264 | open_fip |
| 265 | }, |
Jens Wiklander | 0acbaaa | 2017-08-24 13:16:26 +0200 | [diff] [blame] | 266 | [BL32_EXTRA1_IMAGE_ID] = { |
| 267 | &fip_dev_handle, |
| 268 | (uintptr_t)&bl32_extra1_uuid_spec, |
| 269 | open_fip |
| 270 | }, |
| 271 | [BL32_EXTRA2_IMAGE_ID] = { |
| 272 | &fip_dev_handle, |
| 273 | (uintptr_t)&bl32_extra2_uuid_spec, |
| 274 | open_fip |
| 275 | }, |
Sumit Garg | 4ac3034 | 2019-11-14 17:34:56 +0530 | [diff] [blame] | 276 | #endif |
Jens Wiklander | a43c128 | 2022-11-22 14:39:26 +0100 | [diff] [blame] | 277 | [TB_FW_CONFIG_ID] = { |
| 278 | &fip_dev_handle, |
| 279 | (uintptr_t)&tb_fw_config_uuid_spec, |
| 280 | open_fip |
| 281 | }, |
Jens Wiklander | d4b84f0 | 2022-11-18 15:40:04 +0100 | [diff] [blame] | 282 | [TOS_FW_CONFIG_ID] = { |
| 283 | &fip_dev_handle, |
| 284 | (uintptr_t)&tos_fw_config_uuid_spec, |
| 285 | open_fip |
| 286 | }, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 287 | [BL33_IMAGE_ID] = { |
| 288 | &fip_dev_handle, |
| 289 | (uintptr_t)&bl33_uuid_spec, |
| 290 | open_fip |
| 291 | }, |
| 292 | #if TRUSTED_BOARD_BOOT |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 293 | [TRUSTED_BOOT_FW_CERT_ID] = { |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 294 | &fip_dev_handle, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 295 | (uintptr_t)&tb_fw_cert_uuid_spec, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 296 | open_fip |
| 297 | }, |
| 298 | [TRUSTED_KEY_CERT_ID] = { |
| 299 | &fip_dev_handle, |
| 300 | (uintptr_t)&trusted_key_cert_uuid_spec, |
| 301 | open_fip |
| 302 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 303 | [SOC_FW_KEY_CERT_ID] = { |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 304 | &fip_dev_handle, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 305 | (uintptr_t)&soc_fw_key_cert_uuid_spec, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 306 | open_fip |
| 307 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 308 | [TRUSTED_OS_FW_KEY_CERT_ID] = { |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 309 | &fip_dev_handle, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 310 | (uintptr_t)&tos_fw_key_cert_uuid_spec, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 311 | open_fip |
| 312 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 313 | [NON_TRUSTED_FW_KEY_CERT_ID] = { |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 314 | &fip_dev_handle, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 315 | (uintptr_t)&nt_fw_key_cert_uuid_spec, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 316 | open_fip |
| 317 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 318 | [SOC_FW_CONTENT_CERT_ID] = { |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 319 | &fip_dev_handle, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 320 | (uintptr_t)&soc_fw_cert_uuid_spec, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 321 | open_fip |
| 322 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 323 | [TRUSTED_OS_FW_CONTENT_CERT_ID] = { |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 324 | &fip_dev_handle, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 325 | (uintptr_t)&tos_fw_cert_uuid_spec, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 326 | open_fip |
| 327 | }, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 328 | [NON_TRUSTED_FW_CONTENT_CERT_ID] = { |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 329 | &fip_dev_handle, |
Michalis Pappas | 3469cd0 | 2017-10-18 09:43:37 +0800 | [diff] [blame] | 330 | (uintptr_t)&nt_fw_cert_uuid_spec, |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 331 | open_fip |
| 332 | }, |
| 333 | #endif /* TRUSTED_BOARD_BOOT */ |
| 334 | }; |
| 335 | |
Jens Wiklander | a43c128 | 2022-11-22 14:39:26 +0100 | [diff] [blame] | 336 | #if defined(SPD_spmd) |
| 337 | static struct sp_pkg { |
| 338 | struct plat_io_policy policy; |
| 339 | io_file_spec_t sh_file_spec; |
| 340 | uint8_t uuid[UUID_BYTES_LENGTH]; |
| 341 | char path[80]; |
| 342 | } sp_pkgs[MAX_SP_IDS]; |
| 343 | static unsigned int sp_pkg_count; |
| 344 | |
| 345 | int qemu_io_register_sp_pkg(const char *name, const char *uuid, |
| 346 | uintptr_t load_addr) |
| 347 | { |
| 348 | struct sp_pkg *pkg; |
| 349 | bl_mem_params_node_t *mem_params; |
| 350 | |
| 351 | if (sp_pkg_count == MAX_SP_IDS) { |
| 352 | INFO("Reached Max number of SPs\n"); |
| 353 | return -1; |
| 354 | } |
| 355 | mem_params = get_bl_mem_params_node(SP_PKG1_ID + sp_pkg_count); |
| 356 | if (mem_params == NULL) { |
| 357 | ERROR("Can't find SP_PKG ID %u (SP_PKG%u_ID)\n", |
| 358 | SP_PKG1_ID + sp_pkg_count, sp_pkg_count); |
| 359 | return -1; |
| 360 | } |
| 361 | pkg = sp_pkgs + sp_pkg_count; |
| 362 | |
| 363 | if (read_uuid(pkg->uuid, (char *)uuid)) { |
| 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | strlcpy(pkg->path, name, sizeof(pkg->path)); |
| 368 | strlcat(pkg->path, ".pkg", sizeof(pkg->path)); |
| 369 | |
| 370 | pkg->policy.dev_handle = &fip_dev_handle; |
| 371 | pkg->policy.image_spec = (uintptr_t)&pkg->uuid; |
| 372 | pkg->policy.check = open_fip; |
| 373 | pkg->sh_file_spec.path = pkg->path; |
| 374 | pkg->sh_file_spec.mode = FOPEN_MODE_RB; |
| 375 | |
| 376 | mem_params->image_info.image_base = load_addr; |
| 377 | mem_params->image_info.image_max_size = SZ_4M; |
| 378 | mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING; |
| 379 | |
| 380 | sp_pkg_count++; |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | #endif /*SPD_spmd*/ |
| 385 | |
| 386 | static const io_file_spec_t *get_io_file_spec(unsigned int image_id) |
| 387 | { |
| 388 | #if defined(SPD_spmd) |
| 389 | if (image_id >= SP_PKG1_ID && image_id <= SP_PKG8_ID) { |
| 390 | return &sp_pkgs[image_id - SP_PKG1_ID].sh_file_spec; |
| 391 | } |
| 392 | #endif |
| 393 | |
| 394 | assert(image_id < ARRAY_SIZE(sh_file_spec)); |
| 395 | return &sh_file_spec[image_id]; |
| 396 | } |
| 397 | |
| 398 | static const struct plat_io_policy *get_io_policy(unsigned int image_id) |
| 399 | { |
| 400 | #if defined(SPD_spmd) |
| 401 | if (image_id >= SP_PKG1_ID && image_id <= SP_PKG8_ID) { |
| 402 | return &sp_pkgs[image_id - SP_PKG1_ID].policy; |
| 403 | } |
| 404 | #endif |
| 405 | |
| 406 | assert(image_id < ARRAY_SIZE(policies)); |
| 407 | return &policies[image_id]; |
| 408 | } |
| 409 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 410 | static int open_fip(const uintptr_t spec) |
| 411 | { |
| 412 | int result; |
| 413 | uintptr_t local_image_handle; |
| 414 | |
| 415 | /* See if a Firmware Image Package is available */ |
| 416 | result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID); |
Sumit Garg | 4ac3034 | 2019-11-14 17:34:56 +0530 | [diff] [blame] | 417 | if (result == 0 && spec != (uintptr_t)NULL) { |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 418 | result = io_open(fip_dev_handle, spec, &local_image_handle); |
| 419 | if (result == 0) { |
| 420 | VERBOSE("Using FIP\n"); |
| 421 | io_close(local_image_handle); |
| 422 | } |
| 423 | } |
| 424 | return result; |
| 425 | } |
| 426 | |
Sumit Garg | 4ac3034 | 2019-11-14 17:34:56 +0530 | [diff] [blame] | 427 | #ifndef DECRYPTION_SUPPORT_none |
| 428 | static int open_enc_fip(const uintptr_t spec) |
| 429 | { |
| 430 | int result; |
| 431 | uintptr_t local_image_handle; |
| 432 | |
| 433 | /* See if an encrypted FIP is available */ |
| 434 | result = io_dev_init(enc_dev_handle, (uintptr_t)ENC_IMAGE_ID); |
| 435 | if (result == 0) { |
| 436 | result = io_open(enc_dev_handle, spec, &local_image_handle); |
| 437 | if (result == 0) { |
| 438 | VERBOSE("Using encrypted FIP\n"); |
| 439 | io_close(local_image_handle); |
| 440 | } |
| 441 | } |
| 442 | return result; |
| 443 | } |
| 444 | #endif |
| 445 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 446 | static int open_memmap(const uintptr_t spec) |
| 447 | { |
| 448 | int result; |
| 449 | uintptr_t local_image_handle; |
| 450 | |
| 451 | result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL); |
| 452 | if (result == 0) { |
| 453 | result = io_open(memmap_dev_handle, spec, &local_image_handle); |
| 454 | if (result == 0) { |
| 455 | VERBOSE("Using Memmap\n"); |
| 456 | io_close(local_image_handle); |
| 457 | } |
| 458 | } |
| 459 | return result; |
| 460 | } |
| 461 | |
| 462 | static int open_semihosting(const uintptr_t spec) |
| 463 | { |
| 464 | int result; |
| 465 | uintptr_t local_image_handle; |
| 466 | |
| 467 | /* See if the file exists on semi-hosting.*/ |
| 468 | result = io_dev_init(sh_dev_handle, (uintptr_t)NULL); |
| 469 | if (result == 0) { |
| 470 | result = io_open(sh_dev_handle, spec, &local_image_handle); |
| 471 | if (result == 0) { |
| 472 | VERBOSE("Using Semi-hosting IO\n"); |
| 473 | io_close(local_image_handle); |
| 474 | } |
| 475 | } |
| 476 | return result; |
| 477 | } |
| 478 | |
| 479 | void plat_qemu_io_setup(void) |
| 480 | { |
| 481 | int io_result; |
| 482 | |
| 483 | io_result = register_io_dev_fip(&fip_dev_con); |
| 484 | assert(io_result == 0); |
| 485 | |
| 486 | io_result = register_io_dev_memmap(&memmap_dev_con); |
| 487 | assert(io_result == 0); |
| 488 | |
| 489 | /* Open connections to devices and cache the handles */ |
| 490 | io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL, |
| 491 | &fip_dev_handle); |
| 492 | assert(io_result == 0); |
| 493 | |
| 494 | io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL, |
| 495 | &memmap_dev_handle); |
| 496 | assert(io_result == 0); |
| 497 | |
Sumit Garg | 4ac3034 | 2019-11-14 17:34:56 +0530 | [diff] [blame] | 498 | #ifndef DECRYPTION_SUPPORT_none |
| 499 | io_result = register_io_dev_enc(&enc_dev_con); |
| 500 | assert(io_result == 0); |
| 501 | |
| 502 | io_result = io_dev_open(enc_dev_con, (uintptr_t)NULL, |
| 503 | &enc_dev_handle); |
| 504 | assert(io_result == 0); |
| 505 | #endif |
| 506 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 507 | /* Register the additional IO devices on this platform */ |
| 508 | io_result = register_io_dev_sh(&sh_dev_con); |
| 509 | assert(io_result == 0); |
| 510 | |
| 511 | /* Open connections to devices and cache the handles */ |
| 512 | io_result = io_dev_open(sh_dev_con, (uintptr_t)NULL, &sh_dev_handle); |
| 513 | assert(io_result == 0); |
| 514 | |
| 515 | /* Ignore improbable errors in release builds */ |
| 516 | (void)io_result; |
| 517 | } |
| 518 | |
| 519 | static int get_alt_image_source(unsigned int image_id, uintptr_t *dev_handle, |
| 520 | uintptr_t *image_spec) |
| 521 | { |
Jens Wiklander | a43c128 | 2022-11-22 14:39:26 +0100 | [diff] [blame] | 522 | const io_file_spec_t *spec = get_io_file_spec(image_id); |
| 523 | int result; |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 524 | |
Jens Wiklander | a43c128 | 2022-11-22 14:39:26 +0100 | [diff] [blame] | 525 | result = open_semihosting((const uintptr_t)spec); |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 526 | if (result == 0) { |
| 527 | *dev_handle = sh_dev_handle; |
Jens Wiklander | a43c128 | 2022-11-22 14:39:26 +0100 | [diff] [blame] | 528 | *image_spec = (uintptr_t)spec; |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | return result; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Return an IO device handle and specification which can be used to access |
| 536 | * an image. Use this to enforce platform load policy |
| 537 | */ |
| 538 | int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, |
| 539 | uintptr_t *image_spec) |
| 540 | { |
Jens Wiklander | a43c128 | 2022-11-22 14:39:26 +0100 | [diff] [blame] | 541 | const struct plat_io_policy *policy = get_io_policy(image_id); |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 542 | int result; |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 543 | |
Jens Wiklander | 52c798e | 2015-12-07 14:37:10 +0100 | [diff] [blame] | 544 | result = policy->check(policy->image_spec); |
| 545 | if (result == 0) { |
| 546 | *image_spec = policy->image_spec; |
| 547 | *dev_handle = *(policy->dev_handle); |
| 548 | } else { |
| 549 | VERBOSE("Trying alternative IO\n"); |
| 550 | result = get_alt_image_source(image_id, dev_handle, image_spec); |
| 551 | } |
| 552 | |
| 553 | return result; |
| 554 | } |