xref: /arm-trusted-firmware/bl1/bl1_fwu.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu #include <errno.h>
9*91f16700Schasinglulu #include <string.h>
10*91f16700Schasinglulu 
11*91f16700Schasinglulu #include <platform_def.h>
12*91f16700Schasinglulu 
13*91f16700Schasinglulu #include <arch_helpers.h>
14*91f16700Schasinglulu #include <bl1/bl1.h>
15*91f16700Schasinglulu #include <common/bl_common.h>
16*91f16700Schasinglulu #include <common/debug.h>
17*91f16700Schasinglulu #include <context.h>
18*91f16700Schasinglulu #include <drivers/auth/auth_mod.h>
19*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h>
20*91f16700Schasinglulu #include <lib/utils.h>
21*91f16700Schasinglulu #include <plat/common/platform.h>
22*91f16700Schasinglulu #include <smccc_helpers.h>
23*91f16700Schasinglulu 
24*91f16700Schasinglulu #include "bl1_private.h"
25*91f16700Schasinglulu 
26*91f16700Schasinglulu /*
27*91f16700Schasinglulu  * Function declarations.
28*91f16700Schasinglulu  */
29*91f16700Schasinglulu static int bl1_fwu_image_copy(unsigned int image_id,
30*91f16700Schasinglulu 			uintptr_t image_src,
31*91f16700Schasinglulu 			unsigned int block_size,
32*91f16700Schasinglulu 			unsigned int image_size,
33*91f16700Schasinglulu 			unsigned int flags);
34*91f16700Schasinglulu static int bl1_fwu_image_auth(unsigned int image_id,
35*91f16700Schasinglulu 			uintptr_t image_src,
36*91f16700Schasinglulu 			unsigned int image_size,
37*91f16700Schasinglulu 			unsigned int flags);
38*91f16700Schasinglulu static int bl1_fwu_image_execute(unsigned int image_id,
39*91f16700Schasinglulu 			void **handle,
40*91f16700Schasinglulu 			unsigned int flags);
41*91f16700Schasinglulu static register_t bl1_fwu_image_resume(register_t image_param,
42*91f16700Schasinglulu 			void **handle,
43*91f16700Schasinglulu 			unsigned int flags);
44*91f16700Schasinglulu static int bl1_fwu_sec_image_done(void **handle,
45*91f16700Schasinglulu 			unsigned int flags);
46*91f16700Schasinglulu static int bl1_fwu_image_reset(unsigned int image_id,
47*91f16700Schasinglulu 			unsigned int flags);
48*91f16700Schasinglulu __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved);
49*91f16700Schasinglulu 
50*91f16700Schasinglulu /*
51*91f16700Schasinglulu  * This keeps track of last executed secure image id.
52*91f16700Schasinglulu  */
53*91f16700Schasinglulu static unsigned int sec_exec_image_id = INVALID_IMAGE_ID;
54*91f16700Schasinglulu 
55*91f16700Schasinglulu /*******************************************************************************
56*91f16700Schasinglulu  * Top level handler for servicing FWU SMCs.
57*91f16700Schasinglulu  ******************************************************************************/
58*91f16700Schasinglulu u_register_t bl1_fwu_smc_handler(unsigned int smc_fid,
59*91f16700Schasinglulu 			u_register_t x1,
60*91f16700Schasinglulu 			u_register_t x2,
61*91f16700Schasinglulu 			u_register_t x3,
62*91f16700Schasinglulu 			u_register_t x4,
63*91f16700Schasinglulu 			void *cookie,
64*91f16700Schasinglulu 			void *handle,
65*91f16700Schasinglulu 			unsigned int flags)
66*91f16700Schasinglulu {
67*91f16700Schasinglulu 
68*91f16700Schasinglulu 	switch (smc_fid) {
69*91f16700Schasinglulu 	case FWU_SMC_IMAGE_COPY:
70*91f16700Schasinglulu 		SMC_RET1(handle, bl1_fwu_image_copy((uint32_t)x1, x2,
71*91f16700Schasinglulu 			(uint32_t)x3, (uint32_t)x4, flags));
72*91f16700Schasinglulu 
73*91f16700Schasinglulu 	case FWU_SMC_IMAGE_AUTH:
74*91f16700Schasinglulu 		SMC_RET1(handle, bl1_fwu_image_auth((uint32_t)x1, x2,
75*91f16700Schasinglulu 			(uint32_t)x3, flags));
76*91f16700Schasinglulu 
77*91f16700Schasinglulu 	case FWU_SMC_IMAGE_EXECUTE:
78*91f16700Schasinglulu 		SMC_RET1(handle, bl1_fwu_image_execute((uint32_t)x1, &handle,
79*91f16700Schasinglulu 			flags));
80*91f16700Schasinglulu 
81*91f16700Schasinglulu 	case FWU_SMC_IMAGE_RESUME:
82*91f16700Schasinglulu 		SMC_RET1(handle, bl1_fwu_image_resume((register_t)x1, &handle,
83*91f16700Schasinglulu 			flags));
84*91f16700Schasinglulu 
85*91f16700Schasinglulu 	case FWU_SMC_SEC_IMAGE_DONE:
86*91f16700Schasinglulu 		SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
87*91f16700Schasinglulu 
88*91f16700Schasinglulu 	case FWU_SMC_IMAGE_RESET:
89*91f16700Schasinglulu 		SMC_RET1(handle, bl1_fwu_image_reset((uint32_t)x1, flags));
90*91f16700Schasinglulu 
91*91f16700Schasinglulu 	case FWU_SMC_UPDATE_DONE:
92*91f16700Schasinglulu 		bl1_fwu_done((void *)x1, NULL);
93*91f16700Schasinglulu 
94*91f16700Schasinglulu 	default:
95*91f16700Schasinglulu 		assert(false); /* Unreachable */
96*91f16700Schasinglulu 		break;
97*91f16700Schasinglulu 	}
98*91f16700Schasinglulu 
99*91f16700Schasinglulu 	SMC_RET1(handle, SMC_UNK);
100*91f16700Schasinglulu }
101*91f16700Schasinglulu 
102*91f16700Schasinglulu /*******************************************************************************
103*91f16700Schasinglulu  * Utility functions to keep track of the images that are loaded at any time.
104*91f16700Schasinglulu  ******************************************************************************/
105*91f16700Schasinglulu 
106*91f16700Schasinglulu #ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
107*91f16700Schasinglulu #define FWU_MAX_SIMULTANEOUS_IMAGES	PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
108*91f16700Schasinglulu #else
109*91f16700Schasinglulu #define FWU_MAX_SIMULTANEOUS_IMAGES	10
110*91f16700Schasinglulu #endif
111*91f16700Schasinglulu 
112*91f16700Schasinglulu static unsigned int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = {
113*91f16700Schasinglulu 	[0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID
114*91f16700Schasinglulu };
115*91f16700Schasinglulu 
116*91f16700Schasinglulu /*
117*91f16700Schasinglulu  * Adds an image_id to the bl1_fwu_loaded_ids array.
118*91f16700Schasinglulu  * Returns 0 on success, 1 on error.
119*91f16700Schasinglulu  */
120*91f16700Schasinglulu static int bl1_fwu_add_loaded_id(unsigned int image_id)
121*91f16700Schasinglulu {
122*91f16700Schasinglulu 	int i;
123*91f16700Schasinglulu 
124*91f16700Schasinglulu 	/* Check if the ID is already in the list */
125*91f16700Schasinglulu 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
126*91f16700Schasinglulu 		if (bl1_fwu_loaded_ids[i] == image_id)
127*91f16700Schasinglulu 			return 0;
128*91f16700Schasinglulu 	}
129*91f16700Schasinglulu 
130*91f16700Schasinglulu 	/* Find an empty slot */
131*91f16700Schasinglulu 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
132*91f16700Schasinglulu 		if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) {
133*91f16700Schasinglulu 			bl1_fwu_loaded_ids[i] = image_id;
134*91f16700Schasinglulu 			return 0;
135*91f16700Schasinglulu 		}
136*91f16700Schasinglulu 	}
137*91f16700Schasinglulu 
138*91f16700Schasinglulu 	return 1;
139*91f16700Schasinglulu }
140*91f16700Schasinglulu 
141*91f16700Schasinglulu /*
142*91f16700Schasinglulu  * Removes an image_id from the bl1_fwu_loaded_ids array.
143*91f16700Schasinglulu  * Returns 0 on success, 1 on error.
144*91f16700Schasinglulu  */
145*91f16700Schasinglulu static int bl1_fwu_remove_loaded_id(unsigned int image_id)
146*91f16700Schasinglulu {
147*91f16700Schasinglulu 	int i;
148*91f16700Schasinglulu 
149*91f16700Schasinglulu 	/* Find the ID */
150*91f16700Schasinglulu 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
151*91f16700Schasinglulu 		if (bl1_fwu_loaded_ids[i] == image_id) {
152*91f16700Schasinglulu 			bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID;
153*91f16700Schasinglulu 			return 0;
154*91f16700Schasinglulu 		}
155*91f16700Schasinglulu 	}
156*91f16700Schasinglulu 
157*91f16700Schasinglulu 	return 1;
158*91f16700Schasinglulu }
159*91f16700Schasinglulu 
160*91f16700Schasinglulu /*******************************************************************************
161*91f16700Schasinglulu  * This function checks if the specified image overlaps another image already
162*91f16700Schasinglulu  * loaded. It returns 0 if there is no overlap, a negative error code otherwise.
163*91f16700Schasinglulu  ******************************************************************************/
164*91f16700Schasinglulu static int bl1_fwu_image_check_overlaps(unsigned int image_id)
165*91f16700Schasinglulu {
166*91f16700Schasinglulu 	const image_desc_t *desc, *checked_desc;
167*91f16700Schasinglulu 	const image_info_t *info, *checked_info;
168*91f16700Schasinglulu 
169*91f16700Schasinglulu 	uintptr_t image_base, image_end;
170*91f16700Schasinglulu 	uintptr_t checked_image_base, checked_image_end;
171*91f16700Schasinglulu 
172*91f16700Schasinglulu 	checked_desc = bl1_plat_get_image_desc(image_id);
173*91f16700Schasinglulu 	checked_info = &checked_desc->image_info;
174*91f16700Schasinglulu 
175*91f16700Schasinglulu 	/* Image being checked mustn't be empty. */
176*91f16700Schasinglulu 	assert(checked_info->image_size != 0);
177*91f16700Schasinglulu 
178*91f16700Schasinglulu 	checked_image_base = checked_info->image_base;
179*91f16700Schasinglulu 	checked_image_end = checked_image_base + checked_info->image_size - 1;
180*91f16700Schasinglulu 	/* No need to check for overflows, it's done in bl1_fwu_image_copy(). */
181*91f16700Schasinglulu 
182*91f16700Schasinglulu 	for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
183*91f16700Schasinglulu 
184*91f16700Schasinglulu 		/* Skip INVALID_IMAGE_IDs and don't check image against itself */
185*91f16700Schasinglulu 		if ((bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) ||
186*91f16700Schasinglulu 				(bl1_fwu_loaded_ids[i] == image_id))
187*91f16700Schasinglulu 			continue;
188*91f16700Schasinglulu 
189*91f16700Schasinglulu 		desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]);
190*91f16700Schasinglulu 
191*91f16700Schasinglulu 		/* Only check images that are loaded or being loaded. */
192*91f16700Schasinglulu 		assert ((desc != NULL) && (desc->state != IMAGE_STATE_RESET));
193*91f16700Schasinglulu 
194*91f16700Schasinglulu 		info = &desc->image_info;
195*91f16700Schasinglulu 
196*91f16700Schasinglulu 		/* There cannot be overlaps with an empty image. */
197*91f16700Schasinglulu 		if (info->image_size == 0)
198*91f16700Schasinglulu 			continue;
199*91f16700Schasinglulu 
200*91f16700Schasinglulu 		image_base = info->image_base;
201*91f16700Schasinglulu 		image_end = image_base + info->image_size - 1;
202*91f16700Schasinglulu 		/*
203*91f16700Schasinglulu 		 * Overflows cannot happen. It is checked in
204*91f16700Schasinglulu 		 * bl1_fwu_image_copy() when the image goes from RESET to
205*91f16700Schasinglulu 		 * COPYING or COPIED.
206*91f16700Schasinglulu 		 */
207*91f16700Schasinglulu 		assert (image_end > image_base);
208*91f16700Schasinglulu 
209*91f16700Schasinglulu 		/* Check if there are overlaps. */
210*91f16700Schasinglulu 		if (!((image_end < checked_image_base) ||
211*91f16700Schasinglulu 		    (checked_image_end < image_base))) {
212*91f16700Schasinglulu 			VERBOSE("Image with ID %d overlaps existing image with ID %d",
213*91f16700Schasinglulu 				checked_desc->image_id, desc->image_id);
214*91f16700Schasinglulu 			return -EPERM;
215*91f16700Schasinglulu 		}
216*91f16700Schasinglulu 	}
217*91f16700Schasinglulu 
218*91f16700Schasinglulu 	return 0;
219*91f16700Schasinglulu }
220*91f16700Schasinglulu 
221*91f16700Schasinglulu /*******************************************************************************
222*91f16700Schasinglulu  * This function is responsible for copying secure images in AP Secure RAM.
223*91f16700Schasinglulu  ******************************************************************************/
224*91f16700Schasinglulu static int bl1_fwu_image_copy(unsigned int image_id,
225*91f16700Schasinglulu 			uintptr_t image_src,
226*91f16700Schasinglulu 			unsigned int block_size,
227*91f16700Schasinglulu 			unsigned int image_size,
228*91f16700Schasinglulu 			unsigned int flags)
229*91f16700Schasinglulu {
230*91f16700Schasinglulu 	uintptr_t dest_addr;
231*91f16700Schasinglulu 	unsigned int remaining;
232*91f16700Schasinglulu 	image_desc_t *desc;
233*91f16700Schasinglulu 
234*91f16700Schasinglulu 	/* Get the image descriptor. */
235*91f16700Schasinglulu 	desc = bl1_plat_get_image_desc(image_id);
236*91f16700Schasinglulu 	if (desc == NULL) {
237*91f16700Schasinglulu 		WARN("BL1-FWU: Invalid image ID %u\n", image_id);
238*91f16700Schasinglulu 		return -EPERM;
239*91f16700Schasinglulu 	}
240*91f16700Schasinglulu 
241*91f16700Schasinglulu 	/*
242*91f16700Schasinglulu 	 * The request must originate from a non-secure caller and target a
243*91f16700Schasinglulu 	 * secure image. Any other scenario is invalid.
244*91f16700Schasinglulu 	 */
245*91f16700Schasinglulu 	if (GET_SECURITY_STATE(flags) == SECURE) {
246*91f16700Schasinglulu 		WARN("BL1-FWU: Copy not allowed from secure world.\n");
247*91f16700Schasinglulu 		return -EPERM;
248*91f16700Schasinglulu 	}
249*91f16700Schasinglulu 	if (GET_SECURITY_STATE(desc->ep_info.h.attr) == NON_SECURE) {
250*91f16700Schasinglulu 		WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
251*91f16700Schasinglulu 		return -EPERM;
252*91f16700Schasinglulu 	}
253*91f16700Schasinglulu 
254*91f16700Schasinglulu 	/* Check whether the FWU state machine is in the correct state. */
255*91f16700Schasinglulu 	if ((desc->state != IMAGE_STATE_RESET) &&
256*91f16700Schasinglulu 	    (desc->state != IMAGE_STATE_COPYING)) {
257*91f16700Schasinglulu 		WARN("BL1-FWU: Copy not allowed at this point of the FWU"
258*91f16700Schasinglulu 			" process.\n");
259*91f16700Schasinglulu 		return -EPERM;
260*91f16700Schasinglulu 	}
261*91f16700Schasinglulu 
262*91f16700Schasinglulu 	if ((image_src == 0U) || (block_size == 0U) ||
263*91f16700Schasinglulu 	    check_uptr_overflow(image_src, block_size - 1)) {
264*91f16700Schasinglulu 		WARN("BL1-FWU: Copy not allowed due to invalid image source"
265*91f16700Schasinglulu 			" or block size\n");
266*91f16700Schasinglulu 		return -ENOMEM;
267*91f16700Schasinglulu 	}
268*91f16700Schasinglulu 
269*91f16700Schasinglulu 	if (desc->state == IMAGE_STATE_COPYING) {
270*91f16700Schasinglulu 		/*
271*91f16700Schasinglulu 		 * There must have been at least 1 copy operation for this image
272*91f16700Schasinglulu 		 * previously.
273*91f16700Schasinglulu 		 */
274*91f16700Schasinglulu 		assert(desc->copied_size != 0U);
275*91f16700Schasinglulu 		/*
276*91f16700Schasinglulu 		 * The image size must have been recorded in the 1st copy
277*91f16700Schasinglulu 		 * operation.
278*91f16700Schasinglulu 		 */
279*91f16700Schasinglulu 		image_size = desc->image_info.image_size;
280*91f16700Schasinglulu 		assert(image_size != 0);
281*91f16700Schasinglulu 		assert(desc->copied_size < image_size);
282*91f16700Schasinglulu 
283*91f16700Schasinglulu 		INFO("BL1-FWU: Continuing image copy in blocks\n");
284*91f16700Schasinglulu 	} else { /* desc->state == IMAGE_STATE_RESET */
285*91f16700Schasinglulu 		INFO("BL1-FWU: Initial call to copy an image\n");
286*91f16700Schasinglulu 
287*91f16700Schasinglulu 		/*
288*91f16700Schasinglulu 		 * image_size is relevant only for the 1st copy request, it is
289*91f16700Schasinglulu 		 * then ignored for subsequent calls for this image.
290*91f16700Schasinglulu 		 */
291*91f16700Schasinglulu 		if (image_size == 0) {
292*91f16700Schasinglulu 			WARN("BL1-FWU: Copy not allowed due to invalid image"
293*91f16700Schasinglulu 				" size\n");
294*91f16700Schasinglulu 			return -ENOMEM;
295*91f16700Schasinglulu 		}
296*91f16700Schasinglulu 
297*91f16700Schasinglulu 		/* Check that the image size to load is within limit */
298*91f16700Schasinglulu 		if (image_size > desc->image_info.image_max_size) {
299*91f16700Schasinglulu 			WARN("BL1-FWU: Image size out of bounds\n");
300*91f16700Schasinglulu 			return -ENOMEM;
301*91f16700Schasinglulu 		}
302*91f16700Schasinglulu 
303*91f16700Schasinglulu 		/* Save the given image size. */
304*91f16700Schasinglulu 		desc->image_info.image_size = image_size;
305*91f16700Schasinglulu 
306*91f16700Schasinglulu 		/* Make sure the image doesn't overlap other images. */
307*91f16700Schasinglulu 		if (bl1_fwu_image_check_overlaps(image_id) != 0) {
308*91f16700Schasinglulu 			desc->image_info.image_size = 0;
309*91f16700Schasinglulu 			WARN("BL1-FWU: This image overlaps another one\n");
310*91f16700Schasinglulu 			return -EPERM;
311*91f16700Schasinglulu 		}
312*91f16700Schasinglulu 
313*91f16700Schasinglulu 		/*
314*91f16700Schasinglulu 		 * copied_size must be explicitly initialized here because the
315*91f16700Schasinglulu 		 * FWU code doesn't necessarily do it when it resets the state
316*91f16700Schasinglulu 		 * machine.
317*91f16700Schasinglulu 		 */
318*91f16700Schasinglulu 		desc->copied_size = 0;
319*91f16700Schasinglulu 	}
320*91f16700Schasinglulu 
321*91f16700Schasinglulu 	/*
322*91f16700Schasinglulu 	 * If the given block size is more than the total image size
323*91f16700Schasinglulu 	 * then clip the former to the latter.
324*91f16700Schasinglulu 	 */
325*91f16700Schasinglulu 	remaining = image_size - desc->copied_size;
326*91f16700Schasinglulu 	if (block_size > remaining) {
327*91f16700Schasinglulu 		WARN("BL1-FWU: Block size is too big, clipping it.\n");
328*91f16700Schasinglulu 		block_size = remaining;
329*91f16700Schasinglulu 	}
330*91f16700Schasinglulu 
331*91f16700Schasinglulu 	/* Make sure the source image is mapped in memory. */
332*91f16700Schasinglulu 	if (bl1_plat_mem_check(image_src, block_size, flags) != 0) {
333*91f16700Schasinglulu 		WARN("BL1-FWU: Source image is not mapped.\n");
334*91f16700Schasinglulu 		return -ENOMEM;
335*91f16700Schasinglulu 	}
336*91f16700Schasinglulu 
337*91f16700Schasinglulu 	if (bl1_fwu_add_loaded_id(image_id) != 0) {
338*91f16700Schasinglulu 		WARN("BL1-FWU: Too many images loaded at the same time.\n");
339*91f16700Schasinglulu 		return -ENOMEM;
340*91f16700Schasinglulu 	}
341*91f16700Schasinglulu 
342*91f16700Schasinglulu 	/* Allow the platform to handle pre-image load before copying */
343*91f16700Schasinglulu 	if (desc->state == IMAGE_STATE_RESET) {
344*91f16700Schasinglulu 		if (bl1_plat_handle_pre_image_load(image_id) != 0) {
345*91f16700Schasinglulu 			ERROR("BL1-FWU: Failure in pre-image load of image id %d\n",
346*91f16700Schasinglulu 					image_id);
347*91f16700Schasinglulu 			return -EPERM;
348*91f16700Schasinglulu 		}
349*91f16700Schasinglulu 	}
350*91f16700Schasinglulu 
351*91f16700Schasinglulu 	/* Everything looks sane. Go ahead and copy the block of data. */
352*91f16700Schasinglulu 	dest_addr = desc->image_info.image_base + desc->copied_size;
353*91f16700Schasinglulu 	(void)memcpy((void *) dest_addr, (const void *) image_src, block_size);
354*91f16700Schasinglulu 	flush_dcache_range(dest_addr, block_size);
355*91f16700Schasinglulu 
356*91f16700Schasinglulu 	desc->copied_size += block_size;
357*91f16700Schasinglulu 	desc->state = (block_size == remaining) ?
358*91f16700Schasinglulu 		IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
359*91f16700Schasinglulu 
360*91f16700Schasinglulu 	INFO("BL1-FWU: Copy operation successful.\n");
361*91f16700Schasinglulu 	return 0;
362*91f16700Schasinglulu }
363*91f16700Schasinglulu 
364*91f16700Schasinglulu /*******************************************************************************
365*91f16700Schasinglulu  * This function is responsible for authenticating Normal/Secure images.
366*91f16700Schasinglulu  ******************************************************************************/
367*91f16700Schasinglulu static int bl1_fwu_image_auth(unsigned int image_id,
368*91f16700Schasinglulu 			uintptr_t image_src,
369*91f16700Schasinglulu 			unsigned int image_size,
370*91f16700Schasinglulu 			unsigned int flags)
371*91f16700Schasinglulu {
372*91f16700Schasinglulu 	int result;
373*91f16700Schasinglulu 	uintptr_t base_addr;
374*91f16700Schasinglulu 	unsigned int total_size;
375*91f16700Schasinglulu 	image_desc_t *desc;
376*91f16700Schasinglulu 
377*91f16700Schasinglulu 	/* Get the image descriptor. */
378*91f16700Schasinglulu 	desc = bl1_plat_get_image_desc(image_id);
379*91f16700Schasinglulu 	if (desc ==  NULL)
380*91f16700Schasinglulu 		return -EPERM;
381*91f16700Schasinglulu 
382*91f16700Schasinglulu 	if (GET_SECURITY_STATE(flags) == SECURE) {
383*91f16700Schasinglulu 		if (desc->state != IMAGE_STATE_RESET) {
384*91f16700Schasinglulu 			WARN("BL1-FWU: Authentication from secure world "
385*91f16700Schasinglulu 				"while in invalid state\n");
386*91f16700Schasinglulu 			return -EPERM;
387*91f16700Schasinglulu 		}
388*91f16700Schasinglulu 	} else {
389*91f16700Schasinglulu 		if (GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE) {
390*91f16700Schasinglulu 			if (desc->state != IMAGE_STATE_COPIED) {
391*91f16700Schasinglulu 				WARN("BL1-FWU: Authentication of secure image "
392*91f16700Schasinglulu 					"from non-secure world while not in copied state\n");
393*91f16700Schasinglulu 				return -EPERM;
394*91f16700Schasinglulu 			}
395*91f16700Schasinglulu 		} else {
396*91f16700Schasinglulu 			if (desc->state != IMAGE_STATE_RESET) {
397*91f16700Schasinglulu 				WARN("BL1-FWU: Authentication of non-secure image "
398*91f16700Schasinglulu 					"from non-secure world while in invalid state\n");
399*91f16700Schasinglulu 				return -EPERM;
400*91f16700Schasinglulu 			}
401*91f16700Schasinglulu 		}
402*91f16700Schasinglulu 	}
403*91f16700Schasinglulu 
404*91f16700Schasinglulu 	if (desc->state == IMAGE_STATE_COPIED) {
405*91f16700Schasinglulu 		/*
406*91f16700Schasinglulu 		 * Image is in COPIED state.
407*91f16700Schasinglulu 		 * Use the stored address and size.
408*91f16700Schasinglulu 		 */
409*91f16700Schasinglulu 		base_addr = desc->image_info.image_base;
410*91f16700Schasinglulu 		total_size = desc->image_info.image_size;
411*91f16700Schasinglulu 	} else {
412*91f16700Schasinglulu 		if ((image_src == 0U) || (image_size == 0U) ||
413*91f16700Schasinglulu 		    check_uptr_overflow(image_src, image_size - 1)) {
414*91f16700Schasinglulu 			WARN("BL1-FWU: Auth not allowed due to invalid"
415*91f16700Schasinglulu 				" image source/size\n");
416*91f16700Schasinglulu 			return -ENOMEM;
417*91f16700Schasinglulu 		}
418*91f16700Schasinglulu 
419*91f16700Schasinglulu 		/*
420*91f16700Schasinglulu 		 * Image is in RESET state.
421*91f16700Schasinglulu 		 * Check the parameters and authenticate the source image in place.
422*91f16700Schasinglulu 		 */
423*91f16700Schasinglulu 		if (bl1_plat_mem_check(image_src, image_size,
424*91f16700Schasinglulu 					desc->ep_info.h.attr) != 0) {
425*91f16700Schasinglulu 			WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
426*91f16700Schasinglulu 			return -ENOMEM;
427*91f16700Schasinglulu 		}
428*91f16700Schasinglulu 
429*91f16700Schasinglulu 		if (bl1_fwu_add_loaded_id(image_id) != 0) {
430*91f16700Schasinglulu 			WARN("BL1-FWU: Too many images loaded at the same time.\n");
431*91f16700Schasinglulu 			return -ENOMEM;
432*91f16700Schasinglulu 		}
433*91f16700Schasinglulu 
434*91f16700Schasinglulu 		base_addr = image_src;
435*91f16700Schasinglulu 		total_size = image_size;
436*91f16700Schasinglulu 
437*91f16700Schasinglulu 		/* Update the image size in the descriptor. */
438*91f16700Schasinglulu 		desc->image_info.image_size = total_size;
439*91f16700Schasinglulu 	}
440*91f16700Schasinglulu 
441*91f16700Schasinglulu 	/*
442*91f16700Schasinglulu 	 * Authenticate the image.
443*91f16700Schasinglulu 	 */
444*91f16700Schasinglulu 	INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
445*91f16700Schasinglulu 	result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
446*91f16700Schasinglulu 	if (result != 0) {
447*91f16700Schasinglulu 		WARN("BL1-FWU: Authentication Failed err=%d\n", result);
448*91f16700Schasinglulu 
449*91f16700Schasinglulu 		/*
450*91f16700Schasinglulu 		 * Authentication has failed.
451*91f16700Schasinglulu 		 * Clear the memory if the image was copied.
452*91f16700Schasinglulu 		 * This is to prevent an attack where this contains
453*91f16700Schasinglulu 		 * some malicious code that can somehow be executed later.
454*91f16700Schasinglulu 		 */
455*91f16700Schasinglulu 		if (desc->state == IMAGE_STATE_COPIED) {
456*91f16700Schasinglulu 			/* Clear the memory.*/
457*91f16700Schasinglulu 			zero_normalmem((void *)base_addr, total_size);
458*91f16700Schasinglulu 			flush_dcache_range(base_addr, total_size);
459*91f16700Schasinglulu 
460*91f16700Schasinglulu 			/* Indicate that image can be copied again*/
461*91f16700Schasinglulu 			desc->state = IMAGE_STATE_RESET;
462*91f16700Schasinglulu 		}
463*91f16700Schasinglulu 
464*91f16700Schasinglulu 		/*
465*91f16700Schasinglulu 		 * Even if this fails it's ok because the ID isn't in the array.
466*91f16700Schasinglulu 		 * The image cannot be in RESET state here, it is checked at the
467*91f16700Schasinglulu 		 * beginning of the function.
468*91f16700Schasinglulu 		 */
469*91f16700Schasinglulu 		(void)bl1_fwu_remove_loaded_id(image_id);
470*91f16700Schasinglulu 		return -EAUTH;
471*91f16700Schasinglulu 	}
472*91f16700Schasinglulu 
473*91f16700Schasinglulu 	/* Indicate that image is in authenticated state. */
474*91f16700Schasinglulu 	desc->state = IMAGE_STATE_AUTHENTICATED;
475*91f16700Schasinglulu 
476*91f16700Schasinglulu 	/* Allow the platform to handle post-image load */
477*91f16700Schasinglulu 	result = bl1_plat_handle_post_image_load(image_id);
478*91f16700Schasinglulu 	if (result != 0) {
479*91f16700Schasinglulu 		ERROR("BL1-FWU: Failure %d in post-image load of image id %d\n",
480*91f16700Schasinglulu 				result, image_id);
481*91f16700Schasinglulu 		/*
482*91f16700Schasinglulu 		 * Panic here as the platform handling of post-image load is
483*91f16700Schasinglulu 		 * not correct.
484*91f16700Schasinglulu 		 */
485*91f16700Schasinglulu 		plat_error_handler(result);
486*91f16700Schasinglulu 	}
487*91f16700Schasinglulu 
488*91f16700Schasinglulu 	/*
489*91f16700Schasinglulu 	 * Flush image_info to memory so that other
490*91f16700Schasinglulu 	 * secure world images can see changes.
491*91f16700Schasinglulu 	 */
492*91f16700Schasinglulu 	flush_dcache_range((uintptr_t)&desc->image_info,
493*91f16700Schasinglulu 		sizeof(image_info_t));
494*91f16700Schasinglulu 
495*91f16700Schasinglulu 	INFO("BL1-FWU: Authentication was successful\n");
496*91f16700Schasinglulu 
497*91f16700Schasinglulu 	return 0;
498*91f16700Schasinglulu }
499*91f16700Schasinglulu 
500*91f16700Schasinglulu /*******************************************************************************
501*91f16700Schasinglulu  * This function is responsible for executing Secure images.
502*91f16700Schasinglulu  ******************************************************************************/
503*91f16700Schasinglulu static int bl1_fwu_image_execute(unsigned int image_id,
504*91f16700Schasinglulu 			void **handle,
505*91f16700Schasinglulu 			unsigned int flags)
506*91f16700Schasinglulu {
507*91f16700Schasinglulu 	/* Get the image descriptor. */
508*91f16700Schasinglulu 	image_desc_t *desc = bl1_plat_get_image_desc(image_id);
509*91f16700Schasinglulu 
510*91f16700Schasinglulu 	/*
511*91f16700Schasinglulu 	 * Execution is NOT allowed if:
512*91f16700Schasinglulu 	 * image_id is invalid OR
513*91f16700Schasinglulu 	 * Caller is from Secure world OR
514*91f16700Schasinglulu 	 * Image is Non-Secure OR
515*91f16700Schasinglulu 	 * Image is Non-Executable OR
516*91f16700Schasinglulu 	 * Image is NOT in AUTHENTICATED state.
517*91f16700Schasinglulu 	 */
518*91f16700Schasinglulu 	if ((desc == NULL) ||
519*91f16700Schasinglulu 	    (GET_SECURITY_STATE(flags) == SECURE) ||
520*91f16700Schasinglulu 	    (GET_SECURITY_STATE(desc->ep_info.h.attr) == NON_SECURE) ||
521*91f16700Schasinglulu 	    (EP_GET_EXE(desc->ep_info.h.attr) == NON_EXECUTABLE) ||
522*91f16700Schasinglulu 	    (desc->state != IMAGE_STATE_AUTHENTICATED)) {
523*91f16700Schasinglulu 		WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
524*91f16700Schasinglulu 		return -EPERM;
525*91f16700Schasinglulu 	}
526*91f16700Schasinglulu 
527*91f16700Schasinglulu 	INFO("BL1-FWU: Executing Secure image\n");
528*91f16700Schasinglulu 
529*91f16700Schasinglulu #ifdef __aarch64__
530*91f16700Schasinglulu 	/* Save NS-EL1 system registers. */
531*91f16700Schasinglulu 	cm_el1_sysregs_context_save(NON_SECURE);
532*91f16700Schasinglulu #endif
533*91f16700Schasinglulu 
534*91f16700Schasinglulu 	/* Prepare the image for execution. */
535*91f16700Schasinglulu 	bl1_prepare_next_image(image_id);
536*91f16700Schasinglulu 
537*91f16700Schasinglulu 	/* Update the secure image id. */
538*91f16700Schasinglulu 	sec_exec_image_id = image_id;
539*91f16700Schasinglulu 
540*91f16700Schasinglulu #ifdef __aarch64__
541*91f16700Schasinglulu 	*handle = cm_get_context(SECURE);
542*91f16700Schasinglulu #else
543*91f16700Schasinglulu 	*handle = smc_get_ctx(SECURE);
544*91f16700Schasinglulu #endif
545*91f16700Schasinglulu 	return 0;
546*91f16700Schasinglulu }
547*91f16700Schasinglulu 
548*91f16700Schasinglulu /*******************************************************************************
549*91f16700Schasinglulu  * This function is responsible for resuming execution in the other security
550*91f16700Schasinglulu  * world
551*91f16700Schasinglulu  ******************************************************************************/
552*91f16700Schasinglulu static register_t bl1_fwu_image_resume(register_t image_param,
553*91f16700Schasinglulu 			void **handle,
554*91f16700Schasinglulu 			unsigned int flags)
555*91f16700Schasinglulu {
556*91f16700Schasinglulu 	image_desc_t *desc;
557*91f16700Schasinglulu 	unsigned int resume_sec_state;
558*91f16700Schasinglulu 	unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
559*91f16700Schasinglulu 
560*91f16700Schasinglulu 	/* Get the image descriptor for last executed secure image id. */
561*91f16700Schasinglulu 	desc = bl1_plat_get_image_desc(sec_exec_image_id);
562*91f16700Schasinglulu 	if (caller_sec_state == NON_SECURE) {
563*91f16700Schasinglulu 		if (desc == NULL) {
564*91f16700Schasinglulu 			WARN("BL1-FWU: Resume not allowed due to no available"
565*91f16700Schasinglulu 				"secure image\n");
566*91f16700Schasinglulu 			return -EPERM;
567*91f16700Schasinglulu 		}
568*91f16700Schasinglulu 	} else {
569*91f16700Schasinglulu 		/* desc must be valid for secure world callers */
570*91f16700Schasinglulu 		assert(desc != NULL);
571*91f16700Schasinglulu 	}
572*91f16700Schasinglulu 
573*91f16700Schasinglulu 	assert(GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE);
574*91f16700Schasinglulu 	assert(EP_GET_EXE(desc->ep_info.h.attr) == EXECUTABLE);
575*91f16700Schasinglulu 
576*91f16700Schasinglulu 	if (caller_sec_state == SECURE) {
577*91f16700Schasinglulu 		assert(desc->state == IMAGE_STATE_EXECUTED);
578*91f16700Schasinglulu 
579*91f16700Schasinglulu 		/* Update the flags. */
580*91f16700Schasinglulu 		desc->state = IMAGE_STATE_INTERRUPTED;
581*91f16700Schasinglulu 		resume_sec_state = NON_SECURE;
582*91f16700Schasinglulu 	} else {
583*91f16700Schasinglulu 		assert(desc->state == IMAGE_STATE_INTERRUPTED);
584*91f16700Schasinglulu 
585*91f16700Schasinglulu 		/* Update the flags. */
586*91f16700Schasinglulu 		desc->state = IMAGE_STATE_EXECUTED;
587*91f16700Schasinglulu 		resume_sec_state = SECURE;
588*91f16700Schasinglulu 	}
589*91f16700Schasinglulu 
590*91f16700Schasinglulu 	INFO("BL1-FWU: Resuming %s world context\n",
591*91f16700Schasinglulu 		(resume_sec_state == SECURE) ? "secure" : "normal");
592*91f16700Schasinglulu 
593*91f16700Schasinglulu #ifdef __aarch64__
594*91f16700Schasinglulu 	/* Save the EL1 system registers of calling world. */
595*91f16700Schasinglulu 	cm_el1_sysregs_context_save(caller_sec_state);
596*91f16700Schasinglulu 
597*91f16700Schasinglulu 	/* Restore the EL1 system registers of resuming world. */
598*91f16700Schasinglulu 	cm_el1_sysregs_context_restore(resume_sec_state);
599*91f16700Schasinglulu 
600*91f16700Schasinglulu 	/* Update the next context. */
601*91f16700Schasinglulu 	cm_set_next_eret_context(resume_sec_state);
602*91f16700Schasinglulu 
603*91f16700Schasinglulu 	*handle = cm_get_context(resume_sec_state);
604*91f16700Schasinglulu #else
605*91f16700Schasinglulu 	/* Update the next context. */
606*91f16700Schasinglulu 	cm_set_next_context(cm_get_context(resume_sec_state));
607*91f16700Schasinglulu 
608*91f16700Schasinglulu 	/* Prepare the smc context for the next BL image. */
609*91f16700Schasinglulu 	smc_set_next_ctx(resume_sec_state);
610*91f16700Schasinglulu 
611*91f16700Schasinglulu 	*handle = smc_get_ctx(resume_sec_state);
612*91f16700Schasinglulu #endif
613*91f16700Schasinglulu 	return image_param;
614*91f16700Schasinglulu }
615*91f16700Schasinglulu 
616*91f16700Schasinglulu /*******************************************************************************
617*91f16700Schasinglulu  * This function is responsible for resuming normal world context.
618*91f16700Schasinglulu  ******************************************************************************/
619*91f16700Schasinglulu static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
620*91f16700Schasinglulu {
621*91f16700Schasinglulu 	image_desc_t *desc;
622*91f16700Schasinglulu 
623*91f16700Schasinglulu 	/* Make sure caller is from the secure world */
624*91f16700Schasinglulu 	if (GET_SECURITY_STATE(flags) == NON_SECURE) {
625*91f16700Schasinglulu 		WARN("BL1-FWU: Image done not allowed from normal world\n");
626*91f16700Schasinglulu 		return -EPERM;
627*91f16700Schasinglulu 	}
628*91f16700Schasinglulu 
629*91f16700Schasinglulu 	/* Get the image descriptor for last executed secure image id */
630*91f16700Schasinglulu 	desc = bl1_plat_get_image_desc(sec_exec_image_id);
631*91f16700Schasinglulu 
632*91f16700Schasinglulu 	/* desc must correspond to a valid secure executing image */
633*91f16700Schasinglulu 	assert(desc != NULL);
634*91f16700Schasinglulu 	assert(GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE);
635*91f16700Schasinglulu 	assert(EP_GET_EXE(desc->ep_info.h.attr) == EXECUTABLE);
636*91f16700Schasinglulu 	assert(desc->state == IMAGE_STATE_EXECUTED);
637*91f16700Schasinglulu 
638*91f16700Schasinglulu #if ENABLE_ASSERTIONS
639*91f16700Schasinglulu 	int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id);
640*91f16700Schasinglulu 	assert(rc == 0);
641*91f16700Schasinglulu #else
642*91f16700Schasinglulu 	bl1_fwu_remove_loaded_id(sec_exec_image_id);
643*91f16700Schasinglulu #endif
644*91f16700Schasinglulu 
645*91f16700Schasinglulu 	/* Update the flags. */
646*91f16700Schasinglulu 	desc->state = IMAGE_STATE_RESET;
647*91f16700Schasinglulu 	sec_exec_image_id = INVALID_IMAGE_ID;
648*91f16700Schasinglulu 
649*91f16700Schasinglulu 	INFO("BL1-FWU: Resuming Normal world context\n");
650*91f16700Schasinglulu #ifdef __aarch64__
651*91f16700Schasinglulu 	/*
652*91f16700Schasinglulu 	 * Secure world is done so no need to save the context.
653*91f16700Schasinglulu 	 * Just restore the Non-Secure context.
654*91f16700Schasinglulu 	 */
655*91f16700Schasinglulu 	cm_el1_sysregs_context_restore(NON_SECURE);
656*91f16700Schasinglulu 
657*91f16700Schasinglulu 	/* Update the next context. */
658*91f16700Schasinglulu 	cm_set_next_eret_context(NON_SECURE);
659*91f16700Schasinglulu 
660*91f16700Schasinglulu 	*handle = cm_get_context(NON_SECURE);
661*91f16700Schasinglulu #else
662*91f16700Schasinglulu 	/* Update the next context. */
663*91f16700Schasinglulu 	cm_set_next_context(cm_get_context(NON_SECURE));
664*91f16700Schasinglulu 
665*91f16700Schasinglulu 	/* Prepare the smc context for the next BL image. */
666*91f16700Schasinglulu 	smc_set_next_ctx(NON_SECURE);
667*91f16700Schasinglulu 
668*91f16700Schasinglulu 	*handle = smc_get_ctx(NON_SECURE);
669*91f16700Schasinglulu #endif
670*91f16700Schasinglulu 	return 0;
671*91f16700Schasinglulu }
672*91f16700Schasinglulu 
673*91f16700Schasinglulu /*******************************************************************************
674*91f16700Schasinglulu  * This function provides the opportunity for users to perform any
675*91f16700Schasinglulu  * platform specific handling after the Firmware update is done.
676*91f16700Schasinglulu  ******************************************************************************/
677*91f16700Schasinglulu __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
678*91f16700Schasinglulu {
679*91f16700Schasinglulu 	NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
680*91f16700Schasinglulu 
681*91f16700Schasinglulu 	/*
682*91f16700Schasinglulu 	 * Call platform done function.
683*91f16700Schasinglulu 	 */
684*91f16700Schasinglulu 	bl1_plat_fwu_done(client_cookie, reserved);
685*91f16700Schasinglulu 	assert(false);
686*91f16700Schasinglulu }
687*91f16700Schasinglulu 
688*91f16700Schasinglulu /*******************************************************************************
689*91f16700Schasinglulu  * This function resets an image to IMAGE_STATE_RESET. It fails if the image is
690*91f16700Schasinglulu  * being executed.
691*91f16700Schasinglulu  ******************************************************************************/
692*91f16700Schasinglulu static int bl1_fwu_image_reset(unsigned int image_id, unsigned int flags)
693*91f16700Schasinglulu {
694*91f16700Schasinglulu 	image_desc_t *desc = bl1_plat_get_image_desc(image_id);
695*91f16700Schasinglulu 
696*91f16700Schasinglulu 	if ((desc == NULL) || (GET_SECURITY_STATE(flags) == SECURE)) {
697*91f16700Schasinglulu 		WARN("BL1-FWU: Reset not allowed due to invalid args\n");
698*91f16700Schasinglulu 		return -EPERM;
699*91f16700Schasinglulu 	}
700*91f16700Schasinglulu 
701*91f16700Schasinglulu 	switch (desc->state) {
702*91f16700Schasinglulu 
703*91f16700Schasinglulu 	case IMAGE_STATE_RESET:
704*91f16700Schasinglulu 		/* Nothing to do. */
705*91f16700Schasinglulu 		break;
706*91f16700Schasinglulu 
707*91f16700Schasinglulu 	case IMAGE_STATE_INTERRUPTED:
708*91f16700Schasinglulu 	case IMAGE_STATE_AUTHENTICATED:
709*91f16700Schasinglulu 	case IMAGE_STATE_COPIED:
710*91f16700Schasinglulu 	case IMAGE_STATE_COPYING:
711*91f16700Schasinglulu 
712*91f16700Schasinglulu 		if (bl1_fwu_remove_loaded_id(image_id) != 0) {
713*91f16700Schasinglulu 			WARN("BL1-FWU: Image reset couldn't find the image ID\n");
714*91f16700Schasinglulu 			return -EPERM;
715*91f16700Schasinglulu 		}
716*91f16700Schasinglulu 
717*91f16700Schasinglulu 		if (desc->copied_size != 0U) {
718*91f16700Schasinglulu 			/* Clear the memory if the image is copied */
719*91f16700Schasinglulu 			assert(GET_SECURITY_STATE(desc->ep_info.h.attr)
720*91f16700Schasinglulu 				== SECURE);
721*91f16700Schasinglulu 
722*91f16700Schasinglulu 			zero_normalmem((void *)desc->image_info.image_base,
723*91f16700Schasinglulu 					desc->copied_size);
724*91f16700Schasinglulu 			flush_dcache_range(desc->image_info.image_base,
725*91f16700Schasinglulu 					desc->copied_size);
726*91f16700Schasinglulu 		}
727*91f16700Schasinglulu 
728*91f16700Schasinglulu 		/* Reset status variables */
729*91f16700Schasinglulu 		desc->copied_size = 0;
730*91f16700Schasinglulu 		desc->image_info.image_size = 0;
731*91f16700Schasinglulu 		desc->state = IMAGE_STATE_RESET;
732*91f16700Schasinglulu 
733*91f16700Schasinglulu 		/* Clear authentication state */
734*91f16700Schasinglulu 		auth_img_flags[image_id] = 0;
735*91f16700Schasinglulu 
736*91f16700Schasinglulu 		break;
737*91f16700Schasinglulu 
738*91f16700Schasinglulu 	case IMAGE_STATE_EXECUTED:
739*91f16700Schasinglulu 	default:
740*91f16700Schasinglulu 		assert(false); /* Unreachable */
741*91f16700Schasinglulu 		break;
742*91f16700Schasinglulu 	}
743*91f16700Schasinglulu 
744*91f16700Schasinglulu 	return 0;
745*91f16700Schasinglulu }
746