Antonio Nino Diaz | ae6779e | 2017-11-06 14:49:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <bl_common.h> |
| 9 | #include <debug.h> |
| 10 | #include <firmware_image_package.h> |
| 11 | #include <io_driver.h> |
| 12 | #include <io_fip.h> |
| 13 | #include <io_memmap.h> |
| 14 | #include <platform_def.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | /* Semihosting filenames */ |
| 18 | #define BL2_IMAGE_NAME "bl2.bin" |
| 19 | #define BL31_IMAGE_NAME "bl31.bin" |
| 20 | #define BL32_IMAGE_NAME "bl32.bin" |
| 21 | #define BL33_IMAGE_NAME "bl33.bin" |
| 22 | |
| 23 | #if TRUSTED_BOARD_BOOT |
| 24 | #define BL2_CERT_NAME "bl2.crt" |
| 25 | #define TRUSTED_KEY_CERT_NAME "trusted_key.crt" |
| 26 | #define BL31_KEY_CERT_NAME "bl31_key.crt" |
| 27 | #define BL32_KEY_CERT_NAME "bl32_key.crt" |
| 28 | #define BL33_KEY_CERT_NAME "bl33_key.crt" |
| 29 | #define BL31_CERT_NAME "bl31.crt" |
| 30 | #define BL32_CERT_NAME "bl32.crt" |
| 31 | #define BL33_CERT_NAME "bl33.crt" |
| 32 | #endif /* TRUSTED_BOARD_BOOT */ |
| 33 | |
| 34 | /* IO devices */ |
| 35 | static const io_dev_connector_t *fip_dev_con; |
| 36 | static uintptr_t fip_dev_handle; |
| 37 | static const io_dev_connector_t *memmap_dev_con; |
| 38 | static uintptr_t memmap_dev_handle; |
| 39 | |
| 40 | static const io_block_spec_t fip_block_spec = { |
| 41 | .offset = PLAT_RPI3_FIP_BASE, |
| 42 | .length = PLAT_RPI3_FIP_MAX_SIZE |
| 43 | }; |
| 44 | |
| 45 | static const io_uuid_spec_t bl2_uuid_spec = { |
| 46 | .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2, |
| 47 | }; |
| 48 | |
| 49 | static const io_uuid_spec_t bl31_uuid_spec = { |
| 50 | .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31, |
| 51 | }; |
| 52 | |
| 53 | static const io_uuid_spec_t bl32_uuid_spec = { |
| 54 | .uuid = UUID_SECURE_PAYLOAD_BL32, |
| 55 | }; |
| 56 | |
| 57 | static const io_uuid_spec_t bl33_uuid_spec = { |
| 58 | .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33, |
| 59 | }; |
| 60 | |
| 61 | #if TRUSTED_BOARD_BOOT |
| 62 | static const io_uuid_spec_t bl2_cert_uuid_spec = { |
| 63 | .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2_CERT, |
| 64 | }; |
| 65 | |
| 66 | static const io_uuid_spec_t trusted_key_cert_uuid_spec = { |
| 67 | .uuid = UUID_TRUSTED_KEY_CERT, |
| 68 | }; |
| 69 | |
| 70 | static const io_uuid_spec_t bl31_key_cert_uuid_spec = { |
| 71 | .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31_KEY_CERT, |
| 72 | }; |
| 73 | |
| 74 | static const io_uuid_spec_t bl32_key_cert_uuid_spec = { |
| 75 | .uuid = UUID_SECURE_PAYLOAD_BL32_KEY_CERT, |
| 76 | }; |
| 77 | |
| 78 | static const io_uuid_spec_t bl33_key_cert_uuid_spec = { |
| 79 | .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33_KEY_CERT, |
| 80 | }; |
| 81 | |
| 82 | static const io_uuid_spec_t bl31_cert_uuid_spec = { |
| 83 | .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31_CERT, |
| 84 | }; |
| 85 | |
| 86 | static const io_uuid_spec_t bl32_cert_uuid_spec = { |
| 87 | .uuid = UUID_SECURE_PAYLOAD_BL32_CERT, |
| 88 | }; |
| 89 | |
| 90 | static const io_uuid_spec_t bl33_cert_uuid_spec = { |
| 91 | .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33_CERT, |
| 92 | }; |
| 93 | #endif /* TRUSTED_BOARD_BOOT */ |
| 94 | |
| 95 | static int open_fip(const uintptr_t spec); |
| 96 | static int open_memmap(const uintptr_t spec); |
| 97 | |
| 98 | struct plat_io_policy { |
| 99 | uintptr_t *dev_handle; |
| 100 | uintptr_t image_spec; |
| 101 | int (*check)(const uintptr_t spec); |
| 102 | }; |
| 103 | |
| 104 | /* By default, load images from the FIP */ |
| 105 | static const struct plat_io_policy policies[] = { |
| 106 | [FIP_IMAGE_ID] = { |
| 107 | &memmap_dev_handle, |
| 108 | (uintptr_t)&fip_block_spec, |
| 109 | open_memmap |
| 110 | }, |
| 111 | [BL2_IMAGE_ID] = { |
| 112 | &fip_dev_handle, |
| 113 | (uintptr_t)&bl2_uuid_spec, |
| 114 | open_fip |
| 115 | }, |
| 116 | [BL31_IMAGE_ID] = { |
| 117 | &fip_dev_handle, |
| 118 | (uintptr_t)&bl31_uuid_spec, |
| 119 | open_fip |
| 120 | }, |
| 121 | [BL32_IMAGE_ID] = { |
| 122 | &fip_dev_handle, |
| 123 | (uintptr_t)&bl32_uuid_spec, |
| 124 | open_fip |
| 125 | }, |
| 126 | [BL33_IMAGE_ID] = { |
| 127 | &fip_dev_handle, |
| 128 | (uintptr_t)&bl33_uuid_spec, |
| 129 | open_fip |
| 130 | }, |
| 131 | #if TRUSTED_BOARD_BOOT |
| 132 | [BL2_CERT_ID] = { |
| 133 | &fip_dev_handle, |
| 134 | (uintptr_t)&bl2_cert_uuid_spec, |
| 135 | open_fip |
| 136 | }, |
| 137 | [TRUSTED_KEY_CERT_ID] = { |
| 138 | &fip_dev_handle, |
| 139 | (uintptr_t)&trusted_key_cert_uuid_spec, |
| 140 | open_fip |
| 141 | }, |
| 142 | [BL31_KEY_CERT_ID] = { |
| 143 | &fip_dev_handle, |
| 144 | (uintptr_t)&bl31_key_cert_uuid_spec, |
| 145 | open_fip |
| 146 | }, |
| 147 | [BL32_KEY_CERT_ID] = { |
| 148 | &fip_dev_handle, |
| 149 | (uintptr_t)&bl32_key_cert_uuid_spec, |
| 150 | open_fip |
| 151 | }, |
| 152 | [BL33_KEY_CERT_ID] = { |
| 153 | &fip_dev_handle, |
| 154 | (uintptr_t)&bl33_key_cert_uuid_spec, |
| 155 | open_fip |
| 156 | }, |
| 157 | [BL31_CERT_ID] = { |
| 158 | &fip_dev_handle, |
| 159 | (uintptr_t)&bl31_cert_uuid_spec, |
| 160 | open_fip |
| 161 | }, |
| 162 | [BL32_CERT_ID] = { |
| 163 | &fip_dev_handle, |
| 164 | (uintptr_t)&bl32_cert_uuid_spec, |
| 165 | open_fip |
| 166 | }, |
| 167 | [BL33_CERT_ID] = { |
| 168 | &fip_dev_handle, |
| 169 | (uintptr_t)&bl33_cert_uuid_spec, |
| 170 | open_fip |
| 171 | }, |
| 172 | #endif /* TRUSTED_BOARD_BOOT */ |
| 173 | }; |
| 174 | |
| 175 | static int open_fip(const uintptr_t spec) |
| 176 | { |
| 177 | int result; |
| 178 | uintptr_t local_image_handle; |
| 179 | |
| 180 | /* See if a Firmware Image Package is available */ |
| 181 | result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID); |
| 182 | if (result == 0) { |
| 183 | result = io_open(fip_dev_handle, spec, &local_image_handle); |
| 184 | if (result == 0) { |
| 185 | VERBOSE("Using FIP\n"); |
| 186 | io_close(local_image_handle); |
| 187 | } |
| 188 | } |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | static int open_memmap(const uintptr_t spec) |
| 193 | { |
| 194 | int result; |
| 195 | uintptr_t local_image_handle; |
| 196 | |
| 197 | result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL); |
| 198 | if (result == 0) { |
| 199 | result = io_open(memmap_dev_handle, spec, &local_image_handle); |
| 200 | if (result == 0) { |
| 201 | VERBOSE("Using Memmap\n"); |
| 202 | io_close(local_image_handle); |
| 203 | } |
| 204 | } |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | void plat_rpi3_io_setup(void) |
| 209 | { |
| 210 | int io_result; |
| 211 | |
| 212 | io_result = register_io_dev_fip(&fip_dev_con); |
| 213 | assert(io_result == 0); |
| 214 | |
| 215 | io_result = register_io_dev_memmap(&memmap_dev_con); |
| 216 | assert(io_result == 0); |
| 217 | |
| 218 | /* Open connections to devices and cache the handles */ |
| 219 | io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL, |
| 220 | &fip_dev_handle); |
| 221 | assert(io_result == 0); |
| 222 | |
| 223 | io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL, |
| 224 | &memmap_dev_handle); |
| 225 | assert(io_result == 0); |
| 226 | |
| 227 | /* Ignore improbable errors in release builds */ |
| 228 | (void)io_result; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Return an IO device handle and specification which can be used to access |
| 233 | * an image. Use this to enforce platform load policy |
| 234 | */ |
| 235 | int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle, |
| 236 | uintptr_t *image_spec) |
| 237 | { |
| 238 | int result; |
| 239 | const struct plat_io_policy *policy; |
| 240 | |
| 241 | assert(image_id < ARRAY_SIZE(policies)); |
| 242 | |
| 243 | policy = &policies[image_id]; |
| 244 | result = policy->check(policy->image_spec); |
| 245 | if (result == 0) { |
| 246 | *image_spec = policy->image_spec; |
| 247 | *dev_handle = *(policy->dev_handle); |
| 248 | } |
| 249 | |
| 250 | return result; |
| 251 | } |