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 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 */
get_fsbl_cpu(const struct xfsbl_partition * partition)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 */
get_fsbl_el(const struct xfsbl_partition * partition)92 static int 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 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 *
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 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 */
get_fsbl_estate(const struct xfsbl_partition * partition)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
150 * atf_handoff_addr: ATF handoff address
151 *
152 * Process the handoff paramters from the FSBL and populate the BL32 and BL33
153 * image info structures accordingly.
154 *
155 * Return: Return the status of the handoff. The value will be from the
156 * fsbl_handoff enum.
157 */
fsbl_atf_handover(entry_point_info_t * bl32,entry_point_info_t * bl33,uint64_t atf_handoff_addr)158 enum fsbl_handoff fsbl_atf_handover(entry_point_info_t *bl32,
159 entry_point_info_t *bl33,
160 uint64_t atf_handoff_addr)
161 {
162 const struct xfsbl_atf_handoff_params *ATFHandoffParams;
163 assert((atf_handoff_addr < BL31_BASE) ||
164 (atf_handoff_addr > (uint64_t)&__BL31_END__));
165 if (!atf_handoff_addr) {
166 WARN("BL31: No ATF handoff structure passed\n");
167 return FSBL_HANDOFF_NO_STRUCT;
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')) {
175 ERROR("BL31: invalid ATF handoff structure at %" PRIx64 "\n",
176 atf_handoff_addr);
177 return FSBL_HANDOFF_INVAL_STRUCT;
178 }
179
180 VERBOSE("BL31: ATF handoff params at:0x%" PRIx64 ", entries:%u\n",
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);
185 return FSBL_HANDOFF_TOO_MANY_PARTS;
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;
196 int target_cpu, target_endianness, target_el;
197
198 VERBOSE("BL31: %zd: entry:0x%" PRIx64 ", flags:0x%" PRIx64 "\n", i,
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]);
224 target_endianness = get_fsbl_endian(&ATFHandoffParams->partition[i]);
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,
231 target_endianness,
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,
246 target_endianness,
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
259 VERBOSE("Setting up %s entry point to:%" PRIx64 ", el:%x\n",
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
265 if (target_endianness == SPSR_E_BIG)
266 EP_SET_EE(image->h.attr, EP_EE_BIG);
267 else
268 EP_SET_EE(image->h.attr, EP_EE_LITTLE);
269 }
270
271 return FSBL_HANDOFF_SUCCESS;
272 }
273