1 /*
2  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <arch.h>
8 #include <arch_helpers.h>
9 #include <assert.h>
10 #include <auth_mod.h>
11 #include <bl_common.h>
12 #include <debug.h>
13 #include <errno.h>
14 #include <platform.h>
15 #include <platform_def.h>
16 #include <stdint.h>
17 
18 /*
19  * Check for platforms that use obsolete image terminology
20  */
21 #ifdef BL30_BASE
22 # error "BL30_BASE platform define no longer used - please use SCP_BL2_BASE"
23 #endif
24 
25 /*******************************************************************************
26  * Load the SCP_BL2 image if there's one.
27  * If a platform does not want to attempt to load SCP_BL2 image it must leave
28  * SCP_BL2_BASE undefined.
29  * Return 0 on success or if there's no SCP_BL2 image to load, a negative error
30  * code otherwise.
31  ******************************************************************************/
load_scp_bl2(void)32 static int load_scp_bl2(void)
33 {
34 	int e = 0;
35 #ifdef SCP_BL2_BASE
36 	meminfo_t scp_bl2_mem_info;
37 	image_info_t scp_bl2_image_info;
38 
39 	/*
40 	 * It is up to the platform to specify where SCP_BL2 should be loaded if
41 	 * it exists. It could create space in the secure sram or point to a
42 	 * completely different memory.
43 	 *
44 	 * The entry point information is not relevant in this case as the AP
45 	 * won't execute the SCP_BL2 image.
46 	 */
47 	INFO("BL2: Loading SCP_BL2\n");
48 	bl2_plat_get_scp_bl2_meminfo(&scp_bl2_mem_info);
49 	scp_bl2_image_info.h.version = VERSION_1;
50 	e = load_auth_image(&scp_bl2_mem_info,
51 			    SCP_BL2_IMAGE_ID,
52 			    SCP_BL2_BASE,
53 			    &scp_bl2_image_info,
54 			    NULL);
55 
56 	if (e == 0) {
57 		/* The subsequent handling of SCP_BL2 is platform specific */
58 		e = bl2_plat_handle_scp_bl2(&scp_bl2_image_info);
59 		if (e) {
60 			ERROR("Failure in platform-specific handling of SCP_BL2 image.\n");
61 		}
62 	}
63 #endif /* SCP_BL2_BASE */
64 
65 	return e;
66 }
67 
68 #ifndef EL3_PAYLOAD_BASE
69 /*******************************************************************************
70  * Load the BL31 image.
71  * The bl2_to_bl31_params and bl31_ep_info params will be updated with the
72  * relevant BL31 information.
73  * Return 0 on success, a negative error code otherwise.
74  ******************************************************************************/
load_bl31(bl31_params_t * bl2_to_bl31_params,entry_point_info_t * bl31_ep_info)75 static int load_bl31(bl31_params_t *bl2_to_bl31_params,
76 		     entry_point_info_t *bl31_ep_info)
77 {
78 	meminfo_t *bl2_tzram_layout;
79 	int e;
80 
81 	INFO("BL2: Loading BL31\n");
82 	assert(bl2_to_bl31_params != NULL);
83 	assert(bl31_ep_info != NULL);
84 
85 	/* Find out how much free trusted ram remains after BL2 load */
86 	bl2_tzram_layout = bl2_plat_sec_mem_layout();
87 
88 	/* Set the X0 parameter to BL31 */
89 	bl31_ep_info->args.arg0 = (unsigned long)bl2_to_bl31_params;
90 
91 	/* Load the BL31 image */
92 	e = load_auth_image(bl2_tzram_layout,
93 			    BL31_IMAGE_ID,
94 			    BL31_BASE,
95 			    bl2_to_bl31_params->bl31_image_info,
96 			    bl31_ep_info);
97 
98 	if (e == 0) {
99 		bl2_plat_set_bl31_ep_info(bl2_to_bl31_params->bl31_image_info,
100 					  bl31_ep_info);
101 	}
102 
103 	return e;
104 }
105 
106 /*******************************************************************************
107  * Load the BL32 image if there's one.
108  * The bl2_to_bl31_params param will be updated with the relevant BL32
109  * information.
110  * If a platform does not want to attempt to load BL32 image it must leave
111  * BL32_BASE undefined.
112  * Return 0 on success or if there's no BL32 image to load, a negative error
113  * code otherwise.
114  ******************************************************************************/
load_bl32(bl31_params_t * bl2_to_bl31_params)115 static int load_bl32(bl31_params_t *bl2_to_bl31_params)
116 {
117 	int e = 0;
118 #ifdef BL32_BASE
119 	meminfo_t bl32_mem_info;
120 
121 	INFO("BL2: Loading BL32\n");
122 	assert(bl2_to_bl31_params != NULL);
123 
124 	/*
125 	 * It is up to the platform to specify where BL32 should be loaded if
126 	 * it exists. It could create space in the secure sram or point to a
127 	 * completely different memory.
128 	 */
129 	bl2_plat_get_bl32_meminfo(&bl32_mem_info);
130 	e = load_auth_image(&bl32_mem_info,
131 			    BL32_IMAGE_ID,
132 			    BL32_BASE,
133 			    bl2_to_bl31_params->bl32_image_info,
134 			    bl2_to_bl31_params->bl32_ep_info);
135 
136 	if (e == 0) {
137 		bl2_plat_set_bl32_ep_info(
138 			bl2_to_bl31_params->bl32_image_info,
139 			bl2_to_bl31_params->bl32_ep_info);
140 	}
141 #endif /* BL32_BASE */
142 
143 	return e;
144 }
145 
146 #ifndef PRELOADED_BL33_BASE
147 /*******************************************************************************
148  * Load the BL33 image.
149  * The bl2_to_bl31_params param will be updated with the relevant BL33
150  * information.
151  * Return 0 on success, a negative error code otherwise.
152  ******************************************************************************/
load_bl33(bl31_params_t * bl2_to_bl31_params)153 static int load_bl33(bl31_params_t *bl2_to_bl31_params)
154 {
155 	meminfo_t bl33_mem_info;
156 	int e;
157 
158 	INFO("BL2: Loading BL33\n");
159 	assert(bl2_to_bl31_params != NULL);
160 
161 	bl2_plat_get_bl33_meminfo(&bl33_mem_info);
162 
163 	/* Load the BL33 image in non-secure memory provided by the platform */
164 	e = load_auth_image(&bl33_mem_info,
165 			    BL33_IMAGE_ID,
166 			    plat_get_ns_image_entrypoint(),
167 			    bl2_to_bl31_params->bl33_image_info,
168 			    bl2_to_bl31_params->bl33_ep_info);
169 
170 	if (e == 0) {
171 		bl2_plat_set_bl33_ep_info(bl2_to_bl31_params->bl33_image_info,
172 					  bl2_to_bl31_params->bl33_ep_info);
173 	}
174 
175 	return e;
176 }
177 #endif /* PRELOADED_BL33_BASE */
178 
179 #endif /* EL3_PAYLOAD_BASE */
180 
181 /*******************************************************************************
182  * This function loads SCP_BL2/BL3x images and returns the ep_info for
183  * the next executable image.
184  ******************************************************************************/
bl2_load_images(void)185 entry_point_info_t *bl2_load_images(void)
186 {
187 	bl31_params_t *bl2_to_bl31_params;
188 	entry_point_info_t *bl31_ep_info;
189 	int e;
190 
191 	e = load_scp_bl2();
192 	if (e) {
193 		ERROR("Failed to load SCP_BL2 (%i)\n", e);
194 		plat_error_handler(e);
195 	}
196 
197 	/* Perform platform setup in BL2 after loading SCP_BL2 */
198 	bl2_platform_setup();
199 
200 	/*
201 	 * Get a pointer to the memory the platform has set aside to pass
202 	 * information to BL31.
203 	 */
204 	bl2_to_bl31_params = bl2_plat_get_bl31_params();
205 	bl31_ep_info = bl2_plat_get_bl31_ep_info();
206 
207 #ifdef EL3_PAYLOAD_BASE
208 	/*
209 	 * In the case of an EL3 payload, we don't need to load any further
210 	 * images. Just update the BL31 entrypoint info structure to make BL1
211 	 * jump to the EL3 payload.
212 	 * The pointer to the memory the platform has set aside to pass
213 	 * information to BL31 in the normal boot flow is reused here, even
214 	 * though only a fraction of the information contained in the
215 	 * bl31_params_t structure makes sense in the context of EL3 payloads.
216 	 * This will be refined in the future.
217 	 */
218 	INFO("BL2: Populating the entrypoint info for the EL3 payload\n");
219 	bl31_ep_info->pc = EL3_PAYLOAD_BASE;
220 	bl31_ep_info->args.arg0 = (unsigned long) bl2_to_bl31_params;
221 	bl2_plat_set_bl31_ep_info(NULL, bl31_ep_info);
222 #else
223 	e = load_bl31(bl2_to_bl31_params, bl31_ep_info);
224 	if (e) {
225 		ERROR("Failed to load BL31 (%i)\n", e);
226 		plat_error_handler(e);
227 	}
228 
229 	e = load_bl32(bl2_to_bl31_params);
230 	if (e) {
231 		if (e == -EAUTH) {
232 			ERROR("Failed to authenticate BL32\n");
233 			plat_error_handler(e);
234 		} else {
235 			WARN("Failed to load BL32 (%i)\n", e);
236 		}
237 	}
238 
239 #ifdef PRELOADED_BL33_BASE
240 	/*
241 	 * In this case, don't load the BL33 image as it's already loaded in
242 	 * memory. Update BL33 entrypoint information.
243 	 */
244 	INFO("BL2: Populating the entrypoint info for the preloaded BL33\n");
245 	bl2_to_bl31_params->bl33_ep_info->pc = PRELOADED_BL33_BASE;
246 	bl2_plat_set_bl33_ep_info(NULL, bl2_to_bl31_params->bl33_ep_info);
247 #else
248 	e = load_bl33(bl2_to_bl31_params);
249 	if (e) {
250 		ERROR("Failed to load BL33 (%i)\n", e);
251 		plat_error_handler(e);
252 	}
253 #endif /* PRELOADED_BL33_BASE */
254 
255 #endif /* EL3_PAYLOAD_BASE */
256 
257 	/* Flush the params to be passed to memory */
258 	bl2_plat_flush_bl31_params();
259 
260 	return bl31_ep_info;
261 }
262