Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 1 | /* |
Venkatesh Yadav Abbarapu | 1463dd5 | 2020-01-07 03:25:16 -0700 | [diff] [blame] | 2 | * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved. |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 7 | #include <assert.h> |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 8 | #include <inttypes.h> |
| 9 | #include <stdint.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
| 11 | #include <arch_helpers.h> |
| 12 | #include <common/debug.h> |
Venkatesh Yadav Abbarapu | 1463dd5 | 2020-01-07 03:25:16 -0700 | [diff] [blame] | 13 | #include <plat_startup.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 15 | |
| 16 | /* |
| 17 | * ATFHandoffParams |
| 18 | * Parameter bitfield encoding |
| 19 | * ----------------------------------------------------------------------------- |
| 20 | * Exec State 0 0 -> Aarch64, 1-> Aarch32 |
Soren Brinkmann | 8bcd305 | 2016-05-29 09:48:26 -0700 | [diff] [blame] | 21 | * endianness 1 0 -> LE, 1 -> BE |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 22 | * secure (TZ) 2 0 -> Non secure, 1 -> secure |
| 23 | * EL 3:4 00 -> EL0, 01 -> EL1, 10 -> EL2, 11 -> EL3 |
| 24 | * CPU# 5:6 00 -> A53_0, 01 -> A53_1, 10 -> A53_2, 11 -> A53_3 |
| 25 | */ |
| 26 | |
| 27 | #define FSBL_FLAGS_ESTATE_SHIFT 0 |
| 28 | #define FSBL_FLAGS_ESTATE_MASK (1 << FSBL_FLAGS_ESTATE_SHIFT) |
| 29 | #define FSBL_FLAGS_ESTATE_A64 0 |
| 30 | #define FSBL_FLAGS_ESTATE_A32 1 |
| 31 | |
| 32 | #define FSBL_FLAGS_ENDIAN_SHIFT 1 |
| 33 | #define FSBL_FLAGS_ENDIAN_MASK (1 << FSBL_FLAGS_ENDIAN_SHIFT) |
| 34 | #define FSBL_FLAGS_ENDIAN_LE 0 |
| 35 | #define FSBL_FLAGS_ENDIAN_BE 1 |
| 36 | |
| 37 | #define FSBL_FLAGS_TZ_SHIFT 2 |
| 38 | #define FSBL_FLAGS_TZ_MASK (1 << FSBL_FLAGS_TZ_SHIFT) |
| 39 | #define FSBL_FLAGS_NON_SECURE 0 |
| 40 | #define FSBL_FLAGS_SECURE 1 |
| 41 | |
| 42 | #define FSBL_FLAGS_EL_SHIFT 3 |
| 43 | #define FSBL_FLAGS_EL_MASK (3 << FSBL_FLAGS_EL_SHIFT) |
| 44 | #define FSBL_FLAGS_EL0 0 |
| 45 | #define FSBL_FLAGS_EL1 1 |
| 46 | #define FSBL_FLAGS_EL2 2 |
| 47 | #define FSBL_FLAGS_EL3 3 |
| 48 | |
| 49 | #define FSBL_FLAGS_CPU_SHIFT 5 |
| 50 | #define FSBL_FLAGS_CPU_MASK (3 << FSBL_FLAGS_CPU_SHIFT) |
| 51 | #define FSBL_FLAGS_A53_0 0 |
| 52 | #define FSBL_FLAGS_A53_1 1 |
| 53 | #define FSBL_FLAGS_A53_2 2 |
| 54 | #define FSBL_FLAGS_A53_3 3 |
| 55 | |
| 56 | #define FSBL_MAX_PARTITIONS 8 |
| 57 | |
| 58 | /* Structure corresponding to each partition entry */ |
| 59 | struct xfsbl_partition { |
| 60 | uint64_t entry_point; |
| 61 | uint64_t flags; |
| 62 | }; |
| 63 | |
| 64 | /* Structure for handoff parameters to ARM Trusted Firmware (ATF) */ |
| 65 | struct xfsbl_atf_handoff_params { |
| 66 | uint8_t magic[4]; |
| 67 | uint32_t num_entries; |
| 68 | struct xfsbl_partition partition[FSBL_MAX_PARTITIONS]; |
| 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * @partition: Pointer to partition struct |
| 73 | * |
| 74 | * Get the target CPU for @partition. |
| 75 | * |
| 76 | * Return: FSBL_FLAGS_A53_0, FSBL_FLAGS_A53_1, FSBL_FLAGS_A53_2 or FSBL_FLAGS_A53_3 |
| 77 | */ |
| 78 | static int get_fsbl_cpu(const struct xfsbl_partition *partition) |
| 79 | { |
| 80 | uint64_t flags = partition->flags & FSBL_FLAGS_CPU_MASK; |
| 81 | |
| 82 | return flags >> FSBL_FLAGS_CPU_SHIFT; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * @partition: Pointer to partition struct |
| 87 | * |
| 88 | * Get the target exception level for @partition. |
| 89 | * |
| 90 | * Return: FSBL_FLAGS_EL0, FSBL_FLAGS_EL1, FSBL_FLAGS_EL2 or FSBL_FLAGS_EL3 |
| 91 | */ |
| 92 | static int get_fsbl_el(const struct xfsbl_partition *partition) |
| 93 | { |
| 94 | uint64_t flags = partition->flags & FSBL_FLAGS_EL_MASK; |
| 95 | |
Soren Brinkmann | deba2af | 2016-05-29 09:48:44 -0700 | [diff] [blame] | 96 | return flags >> FSBL_FLAGS_EL_SHIFT; |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @partition: Pointer to partition struct |
| 101 | * |
| 102 | * Get the target security state for @partition. |
| 103 | * |
| 104 | * Return: FSBL_FLAGS_NON_SECURE or FSBL_FLAGS_SECURE |
| 105 | */ |
| 106 | static int get_fsbl_ss(const struct xfsbl_partition *partition) |
| 107 | { |
| 108 | uint64_t flags = partition->flags & FSBL_FLAGS_TZ_MASK; |
| 109 | |
| 110 | return flags >> FSBL_FLAGS_TZ_SHIFT; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @partition: Pointer to partition struct |
| 115 | * |
Soren Brinkmann | 8bcd305 | 2016-05-29 09:48:26 -0700 | [diff] [blame] | 116 | * Get the target endianness for @partition. |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 117 | * |
| 118 | * Return: SPSR_E_LITTLE or SPSR_E_BIG |
| 119 | */ |
| 120 | static int get_fsbl_endian(const struct xfsbl_partition *partition) |
| 121 | { |
| 122 | uint64_t flags = partition->flags & FSBL_FLAGS_ENDIAN_MASK; |
| 123 | |
| 124 | flags >>= FSBL_FLAGS_ENDIAN_SHIFT; |
| 125 | |
| 126 | if (flags == FSBL_FLAGS_ENDIAN_BE) |
| 127 | return SPSR_E_BIG; |
| 128 | else |
| 129 | return SPSR_E_LITTLE; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @partition: Pointer to partition struct |
| 134 | * |
| 135 | * Get the target execution state for @partition. |
| 136 | * |
| 137 | * Return: FSBL_FLAGS_ESTATE_A32 or FSBL_FLAGS_ESTATE_A64 |
| 138 | */ |
| 139 | static int get_fsbl_estate(const struct xfsbl_partition *partition) |
| 140 | { |
| 141 | uint64_t flags = partition->flags & FSBL_FLAGS_ESTATE_MASK; |
| 142 | |
| 143 | return flags >> FSBL_FLAGS_ESTATE_SHIFT; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Populates the bl32 and bl33 image info structures |
| 148 | * @bl32: BL32 image info structure |
| 149 | * @bl33: BL33 image info structure |
Venkatesh Yadav Abbarapu | 1463dd5 | 2020-01-07 03:25:16 -0700 | [diff] [blame] | 150 | * atf_handoff_addr: ATF handoff address |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 151 | * |
| 152 | * Process the handoff paramters from the FSBL and populate the BL32 and BL33 |
| 153 | * image info structures accordingly. |
Siva Durga Prasad Paladugu | 8f49972 | 2018-05-17 15:17:46 +0530 | [diff] [blame] | 154 | * |
| 155 | * Return: Return the status of the handoff. The value will be from the |
| 156 | * fsbl_handoff enum. |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 157 | */ |
Venkatesh Yadav Abbarapu | 1463dd5 | 2020-01-07 03:25:16 -0700 | [diff] [blame] | 158 | enum fsbl_handoff fsbl_atf_handover(entry_point_info_t *bl32, |
| 159 | entry_point_info_t *bl33, |
| 160 | uint64_t atf_handoff_addr) |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 161 | { |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 162 | const struct xfsbl_atf_handoff_params *ATFHandoffParams; |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 163 | assert((atf_handoff_addr < BL31_BASE) || |
| 164 | (atf_handoff_addr > (uint64_t)&__BL31_END__)); |
| 165 | if (!atf_handoff_addr) { |
Alistair Francis | b8d474f | 2017-11-30 16:21:21 -0800 | [diff] [blame] | 166 | WARN("BL31: No ATF handoff structure passed\n"); |
Siva Durga Prasad Paladugu | 8f49972 | 2018-05-17 15:17:46 +0530 | [diff] [blame] | 167 | return FSBL_HANDOFF_NO_STRUCT; |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | ATFHandoffParams = (struct xfsbl_atf_handoff_params *)atf_handoff_addr; |
| 171 | if ((ATFHandoffParams->magic[0] != 'X') || |
| 172 | (ATFHandoffParams->magic[1] != 'L') || |
| 173 | (ATFHandoffParams->magic[2] != 'N') || |
| 174 | (ATFHandoffParams->magic[3] != 'X')) { |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 175 | ERROR("BL31: invalid ATF handoff structure at %" PRIx64 "\n", |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 176 | atf_handoff_addr); |
Siva Durga Prasad Paladugu | 8f49972 | 2018-05-17 15:17:46 +0530 | [diff] [blame] | 177 | return FSBL_HANDOFF_INVAL_STRUCT; |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 180 | VERBOSE("BL31: ATF handoff params at:0x%" PRIx64 ", entries:%u\n", |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 181 | atf_handoff_addr, ATFHandoffParams->num_entries); |
| 182 | if (ATFHandoffParams->num_entries > FSBL_MAX_PARTITIONS) { |
| 183 | ERROR("BL31: ATF handoff params: too many partitions (%u/%u)\n", |
| 184 | ATFHandoffParams->num_entries, FSBL_MAX_PARTITIONS); |
Siva Durga Prasad Paladugu | 8f49972 | 2018-05-17 15:17:46 +0530 | [diff] [blame] | 185 | return FSBL_HANDOFF_TOO_MANY_PARTS; |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /* |
| 189 | * we loop over all passed entries but only populate two image structs |
| 190 | * (bl32, bl33). I.e. the last applicable images in the handoff |
| 191 | * structure will be used for the hand off |
| 192 | */ |
| 193 | for (size_t i = 0; i < ATFHandoffParams->num_entries; i++) { |
| 194 | entry_point_info_t *image; |
| 195 | int target_estate, target_secure; |
Soren Brinkmann | 8bcd305 | 2016-05-29 09:48:26 -0700 | [diff] [blame] | 196 | int target_cpu, target_endianness, target_el; |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 197 | |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 198 | VERBOSE("BL31: %zd: entry:0x%" PRIx64 ", flags:0x%" PRIx64 "\n", i, |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 199 | ATFHandoffParams->partition[i].entry_point, |
| 200 | ATFHandoffParams->partition[i].flags); |
| 201 | |
| 202 | target_cpu = get_fsbl_cpu(&ATFHandoffParams->partition[i]); |
| 203 | if (target_cpu != FSBL_FLAGS_A53_0) { |
| 204 | WARN("BL31: invalid target CPU (%i)\n", target_cpu); |
| 205 | continue; |
| 206 | } |
| 207 | |
| 208 | target_el = get_fsbl_el(&ATFHandoffParams->partition[i]); |
| 209 | if ((target_el == FSBL_FLAGS_EL3) || |
| 210 | (target_el == FSBL_FLAGS_EL0)) { |
| 211 | WARN("BL31: invalid exception level (%i)\n", target_el); |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | target_secure = get_fsbl_ss(&ATFHandoffParams->partition[i]); |
| 216 | if (target_secure == FSBL_FLAGS_SECURE && |
| 217 | target_el == FSBL_FLAGS_EL2) { |
| 218 | WARN("BL31: invalid security state (%i) for exception level (%i)\n", |
| 219 | target_secure, target_el); |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | target_estate = get_fsbl_estate(&ATFHandoffParams->partition[i]); |
Soren Brinkmann | 8bcd305 | 2016-05-29 09:48:26 -0700 | [diff] [blame] | 224 | target_endianness = get_fsbl_endian(&ATFHandoffParams->partition[i]); |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 225 | |
| 226 | if (target_secure == FSBL_FLAGS_SECURE) { |
| 227 | image = bl32; |
| 228 | |
| 229 | if (target_estate == FSBL_FLAGS_ESTATE_A32) |
| 230 | bl32->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM, |
Soren Brinkmann | 8bcd305 | 2016-05-29 09:48:26 -0700 | [diff] [blame] | 231 | target_endianness, |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 232 | DISABLE_ALL_EXCEPTIONS); |
| 233 | else |
| 234 | bl32->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX, |
| 235 | DISABLE_ALL_EXCEPTIONS); |
| 236 | } else { |
| 237 | image = bl33; |
| 238 | |
| 239 | if (target_estate == FSBL_FLAGS_ESTATE_A32) { |
| 240 | if (target_el == FSBL_FLAGS_EL2) |
| 241 | target_el = MODE32_hyp; |
| 242 | else |
| 243 | target_el = MODE32_sys; |
| 244 | |
| 245 | bl33->spsr = SPSR_MODE32(target_el, SPSR_T_ARM, |
Soren Brinkmann | 8bcd305 | 2016-05-29 09:48:26 -0700 | [diff] [blame] | 246 | target_endianness, |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 247 | DISABLE_ALL_EXCEPTIONS); |
| 248 | } else { |
| 249 | if (target_el == FSBL_FLAGS_EL2) |
| 250 | target_el = MODE_EL2; |
| 251 | else |
| 252 | target_el = MODE_EL1; |
| 253 | |
| 254 | bl33->spsr = SPSR_64(target_el, MODE_SP_ELX, |
| 255 | DISABLE_ALL_EXCEPTIONS); |
| 256 | } |
| 257 | } |
| 258 | |
Scott Branden | e5dcf98 | 2020-08-25 13:49:32 -0700 | [diff] [blame] | 259 | VERBOSE("Setting up %s entry point to:%" PRIx64 ", el:%x\n", |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 260 | target_secure == FSBL_FLAGS_SECURE ? "BL32" : "BL33", |
| 261 | ATFHandoffParams->partition[i].entry_point, |
| 262 | target_el); |
| 263 | image->pc = ATFHandoffParams->partition[i].entry_point; |
| 264 | |
Soren Brinkmann | 8bcd305 | 2016-05-29 09:48:26 -0700 | [diff] [blame] | 265 | if (target_endianness == SPSR_E_BIG) |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 266 | EP_SET_EE(image->h.attr, EP_EE_BIG); |
| 267 | else |
| 268 | EP_SET_EE(image->h.attr, EP_EE_LITTLE); |
| 269 | } |
Siva Durga Prasad Paladugu | 8f49972 | 2018-05-17 15:17:46 +0530 | [diff] [blame] | 270 | |
| 271 | return FSBL_HANDOFF_SUCCESS; |
Michal Simek | ef8f559 | 2015-06-15 14:22:50 +0200 | [diff] [blame] | 272 | } |