1 /*
2 * Copyright (c) 2014-2020, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8 #include <inttypes.h>
9 #include <stdint.h>
10
11 #include <arch_helpers.h>
12 #include <common/debug.h>
13 #include <plat_startup.h>
14
15
16 /*
17 * ATFHandoffParams
18 * Parameter bitfield encoding
19 * -----------------------------------------------------------------------------
20 * Exec State 0 0 -> Aarch64, 1-> Aarch32
21 * endianness 1 0 -> LE, 1 -> BE
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 0U
28 #define FSBL_FLAGS_ESTATE_MASK (1U << FSBL_FLAGS_ESTATE_SHIFT)
29 #define FSBL_FLAGS_ESTATE_A64 0U
30 #define FSBL_FLAGS_ESTATE_A32 1U
31
32 #define FSBL_FLAGS_ENDIAN_SHIFT 1U
33 #define FSBL_FLAGS_ENDIAN_MASK (1U << FSBL_FLAGS_ENDIAN_SHIFT)
34 #define FSBL_FLAGS_ENDIAN_LE 0U
35 #define FSBL_FLAGS_ENDIAN_BE 1U
36
37 #define FSBL_FLAGS_TZ_SHIFT 2U
38 #define FSBL_FLAGS_TZ_MASK (1U << FSBL_FLAGS_TZ_SHIFT)
39 #define FSBL_FLAGS_NON_SECURE 0U
40 #define FSBL_FLAGS_SECURE 1U
41
42 #define FSBL_FLAGS_EL_SHIFT 3U
43 #define FSBL_FLAGS_EL_MASK (3U << FSBL_FLAGS_EL_SHIFT)
44 #define FSBL_FLAGS_EL0 0U
45 #define FSBL_FLAGS_EL1 1U
46 #define FSBL_FLAGS_EL2 2U
47 #define FSBL_FLAGS_EL3 3U
48
49 #define FSBL_FLAGS_CPU_SHIFT 5U
50 #define FSBL_FLAGS_CPU_MASK (3U << FSBL_FLAGS_CPU_SHIFT)
51 #define FSBL_FLAGS_A53_0 0U
52 #define FSBL_FLAGS_A53_1 1U
53 #define FSBL_FLAGS_A53_2 2U
54 #define FSBL_FLAGS_A53_3 3U
55
56 #define FSBL_MAX_PARTITIONS 8U
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 */
get_fsbl_cpu(const struct xfsbl_partition * partition)78 static int32_t 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 */
get_fsbl_el(const struct xfsbl_partition * partition)92 static int32_t get_fsbl_el(const struct xfsbl_partition *partition)
93 {
94 uint64_t flags = partition->flags & FSBL_FLAGS_EL_MASK;
95
96 return flags >> FSBL_FLAGS_EL_SHIFT;
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 */
get_fsbl_ss(const struct xfsbl_partition * partition)106 static int32_t 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 *
116 * Get the target endianness for @partition.
117 *
118 * Return: SPSR_E_LITTLE or SPSR_E_BIG
119 */
get_fsbl_endian(const struct xfsbl_partition * partition)120 static int32_t 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 /**
134 * @partition: Pointer to partition struct
135 *
136 * Get the target execution state for @partition.
137 *
138 * Return: FSBL_FLAGS_ESTATE_A32 or FSBL_FLAGS_ESTATE_A64
139 */
get_fsbl_estate(const struct xfsbl_partition * partition)140 static int32_t get_fsbl_estate(const struct xfsbl_partition *partition)
141 {
142 uint64_t flags = partition->flags & FSBL_FLAGS_ESTATE_MASK;
143
144 return flags >> FSBL_FLAGS_ESTATE_SHIFT;
145 }
146
147 /**
148 * Populates the bl32 and bl33 image info structures
149 * @bl32: BL32 image info structure
150 * @bl33: BL33 image info structure
151 * atf_handoff_addr: ATF handoff address
152 *
153 * Process the handoff paramters from the FSBL and populate the BL32 and BL33
154 * image info structures accordingly.
155 *
156 * Return: Return the status of the handoff. The value will be from the
157 * fsbl_handoff enum.
158 */
fsbl_atf_handover(entry_point_info_t * bl32,entry_point_info_t * bl33,uint64_t atf_handoff_addr)159 enum fsbl_handoff fsbl_atf_handover(entry_point_info_t *bl32,
160 entry_point_info_t *bl33,
161 uint64_t atf_handoff_addr)
162 {
163 const struct xfsbl_atf_handoff_params *ATFHandoffParams;
164 assert((atf_handoff_addr < BL31_BASE) ||
165 (atf_handoff_addr > (uint64_t)&__BL31_END__));
166 if (!atf_handoff_addr) {
167 WARN("BL31: No ATF handoff structure passed\n");
168 return FSBL_HANDOFF_NO_STRUCT;
169 }
170
171 ATFHandoffParams = (struct xfsbl_atf_handoff_params *)atf_handoff_addr;
172 if ((ATFHandoffParams->magic[0] != 'X') ||
173 (ATFHandoffParams->magic[1] != 'L') ||
174 (ATFHandoffParams->magic[2] != 'N') ||
175 (ATFHandoffParams->magic[3] != 'X')) {
176 ERROR("BL31: invalid ATF handoff structure at %" PRIx64 "\n",
177 atf_handoff_addr);
178 return FSBL_HANDOFF_INVAL_STRUCT;
179 }
180
181 VERBOSE("BL31: ATF handoff params at:0x%" PRIx64 ", entries:%u\n",
182 atf_handoff_addr, ATFHandoffParams->num_entries);
183 if (ATFHandoffParams->num_entries > FSBL_MAX_PARTITIONS) {
184 ERROR("BL31: ATF handoff params: too many partitions (%u/%u)\n",
185 ATFHandoffParams->num_entries, FSBL_MAX_PARTITIONS);
186 return FSBL_HANDOFF_TOO_MANY_PARTS;
187 }
188
189 /*
190 * we loop over all passed entries but only populate two image structs
191 * (bl32, bl33). I.e. the last applicable images in the handoff
192 * structure will be used for the hand off
193 */
194 for (size_t i = 0; i < ATFHandoffParams->num_entries; i++) {
195 entry_point_info_t *image;
196 int32_t target_estate, target_secure, target_cpu;
197 uint32_t target_endianness, target_el;
198
199 VERBOSE("BL31: %zd: entry:0x%" PRIx64 ", flags:0x%" PRIx64 "\n", i,
200 ATFHandoffParams->partition[i].entry_point,
201 ATFHandoffParams->partition[i].flags);
202
203 target_cpu = get_fsbl_cpu(&ATFHandoffParams->partition[i]);
204 if (target_cpu != FSBL_FLAGS_A53_0) {
205 WARN("BL31: invalid target CPU (%i)\n", target_cpu);
206 continue;
207 }
208
209 target_el = get_fsbl_el(&ATFHandoffParams->partition[i]);
210 if ((target_el == FSBL_FLAGS_EL3) ||
211 (target_el == FSBL_FLAGS_EL0)) {
212 WARN("BL31: invalid exception level (%i)\n", target_el);
213 continue;
214 }
215
216 target_secure = get_fsbl_ss(&ATFHandoffParams->partition[i]);
217 if (target_secure == FSBL_FLAGS_SECURE &&
218 target_el == FSBL_FLAGS_EL2) {
219 WARN("BL31: invalid security state (%i) for exception level (%i)\n",
220 target_secure, target_el);
221 continue;
222 }
223
224 target_estate = get_fsbl_estate(&ATFHandoffParams->partition[i]);
225 target_endianness = get_fsbl_endian(&ATFHandoffParams->partition[i]);
226
227 if (target_secure == FSBL_FLAGS_SECURE) {
228 image = bl32;
229
230 if (target_estate == FSBL_FLAGS_ESTATE_A32) {
231 bl32->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
232 target_endianness,
233 DISABLE_ALL_EXCEPTIONS);
234 } else {
235 bl32->spsr = SPSR_64(MODE_EL1, MODE_SP_ELX,
236 DISABLE_ALL_EXCEPTIONS);
237 }
238 } else {
239 image = bl33;
240
241 if (target_estate == FSBL_FLAGS_ESTATE_A32) {
242 if (target_el == FSBL_FLAGS_EL2) {
243 target_el = MODE32_hyp;
244 } else {
245 target_el = MODE32_sys;
246 }
247
248 bl33->spsr = SPSR_MODE32(target_el, SPSR_T_ARM,
249 target_endianness,
250 DISABLE_ALL_EXCEPTIONS);
251 } else {
252 if (target_el == FSBL_FLAGS_EL2) {
253 target_el = MODE_EL2;
254 } else {
255 target_el = MODE_EL1;
256 }
257
258 bl33->spsr = SPSR_64(target_el, MODE_SP_ELX,
259 DISABLE_ALL_EXCEPTIONS);
260 }
261 }
262
263 VERBOSE("Setting up %s entry point to:%" PRIx64 ", el:%x\n",
264 target_secure == FSBL_FLAGS_SECURE ? "BL32" : "BL33",
265 ATFHandoffParams->partition[i].entry_point,
266 target_el);
267 image->pc = ATFHandoffParams->partition[i].entry_point;
268
269 if (target_endianness == SPSR_E_BIG) {
270 EP_SET_EE(image->h.attr, EP_EE_BIG);
271 } else {
272 EP_SET_EE(image->h.attr, EP_EE_LITTLE);
273 }
274 }
275
276 return FSBL_HANDOFF_SUCCESS;
277 }
278