xref: /rk3399_ARM-atf/bl1/bl1_fwu.c (revision 402b3cf8766fe2cb4ae462f7ee7761d08a1ba56c)
148bfb88eSYatharth Kochar /*
2279faa6dSAmbroise Vincent  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
348bfb88eSYatharth Kochar  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
548bfb88eSYatharth Kochar  */
648bfb88eSYatharth Kochar 
72a4b4b71SIsla Mitchell #include <assert.h>
848bfb88eSYatharth Kochar #include <errno.h>
948bfb88eSYatharth Kochar #include <string.h>
1009d40e0eSAntonio Nino Diaz 
1109d40e0eSAntonio Nino Diaz #include <platform_def.h>
1209d40e0eSAntonio Nino Diaz 
1309d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1409d40e0eSAntonio Nino Diaz #include <bl1/bl1.h>
1509d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1609d40e0eSAntonio Nino Diaz #include <common/debug.h>
1709d40e0eSAntonio Nino Diaz #include <context.h>
1809d40e0eSAntonio Nino Diaz #include <drivers/auth/auth_mod.h>
1909d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
2009d40e0eSAntonio Nino Diaz #include <lib/utils.h>
2109d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
2209d40e0eSAntonio Nino Diaz #include <smccc_helpers.h>
2309d40e0eSAntonio Nino Diaz 
2448bfb88eSYatharth Kochar #include "bl1_private.h"
2548bfb88eSYatharth Kochar 
2648bfb88eSYatharth Kochar /*
2748bfb88eSYatharth Kochar  * Function declarations.
2848bfb88eSYatharth Kochar  */
2948bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id,
30735181b6SRoberto Vargas 			uintptr_t image_src,
3148bfb88eSYatharth Kochar 			unsigned int block_size,
3248bfb88eSYatharth Kochar 			unsigned int image_size,
3348bfb88eSYatharth Kochar 			unsigned int flags);
3448bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id,
35735181b6SRoberto Vargas 			uintptr_t image_src,
3648bfb88eSYatharth Kochar 			unsigned int image_size,
3748bfb88eSYatharth Kochar 			unsigned int flags);
3848bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id,
3948bfb88eSYatharth Kochar 			void **handle,
4048bfb88eSYatharth Kochar 			unsigned int flags);
4128955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param,
4248bfb88eSYatharth Kochar 			void **handle,
4348bfb88eSYatharth Kochar 			unsigned int flags);
4448bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle,
4548bfb88eSYatharth Kochar 			unsigned int flags);
469d6fc3c3SAntonio Nino Diaz static int bl1_fwu_image_reset(unsigned int image_id,
479d6fc3c3SAntonio Nino Diaz 			unsigned int flags);
481f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved);
4948bfb88eSYatharth Kochar 
5048bfb88eSYatharth Kochar /*
5148bfb88eSYatharth Kochar  * This keeps track of last executed secure image id.
5248bfb88eSYatharth Kochar  */
5348bfb88eSYatharth Kochar static unsigned int sec_exec_image_id = INVALID_IMAGE_ID;
5448bfb88eSYatharth Kochar 
5548bfb88eSYatharth Kochar /*******************************************************************************
5648bfb88eSYatharth Kochar  * Top level handler for servicing FWU SMCs.
5748bfb88eSYatharth Kochar  ******************************************************************************/
5848bfb88eSYatharth Kochar register_t bl1_fwu_smc_handler(unsigned int smc_fid,
5948bfb88eSYatharth Kochar 			register_t x1,
6048bfb88eSYatharth Kochar 			register_t x2,
6148bfb88eSYatharth Kochar 			register_t x3,
6248bfb88eSYatharth Kochar 			register_t x4,
6348bfb88eSYatharth Kochar 			void *cookie,
6448bfb88eSYatharth Kochar 			void *handle,
6548bfb88eSYatharth Kochar 			unsigned int flags)
6648bfb88eSYatharth Kochar {
6748bfb88eSYatharth Kochar 
6848bfb88eSYatharth Kochar 	switch (smc_fid) {
6948bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_COPY:
7048bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_copy(x1, x2, x3, x4, flags));
7148bfb88eSYatharth Kochar 
7248bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_AUTH:
7348bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_auth(x1, x2, x3, flags));
7448bfb88eSYatharth Kochar 
7548bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_EXECUTE:
7648bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_execute(x1, &handle, flags));
7748bfb88eSYatharth Kochar 
7848bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_RESUME:
7928955d57SDan Handley 		SMC_RET1(handle, bl1_fwu_image_resume(x1, &handle, flags));
8048bfb88eSYatharth Kochar 
8148bfb88eSYatharth Kochar 	case FWU_SMC_SEC_IMAGE_DONE:
8248bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
8348bfb88eSYatharth Kochar 
849d6fc3c3SAntonio Nino Diaz 	case FWU_SMC_IMAGE_RESET:
859d6fc3c3SAntonio Nino Diaz 		SMC_RET1(handle, bl1_fwu_image_reset(x1, flags));
869d6fc3c3SAntonio Nino Diaz 
8748bfb88eSYatharth Kochar 	case FWU_SMC_UPDATE_DONE:
881f37b944SDan Handley 		bl1_fwu_done((void *)x1, NULL);
8948bfb88eSYatharth Kochar 
9048bfb88eSYatharth Kochar 	default:
91a08a2014SDaniel Boulby 		assert(0); /* Unreachable */
9248bfb88eSYatharth Kochar 		break;
9348bfb88eSYatharth Kochar 	}
9448bfb88eSYatharth Kochar 
957a317a70SAntonio Nino Diaz 	SMC_RET1(handle, SMC_UNK);
9648bfb88eSYatharth Kochar }
9748bfb88eSYatharth Kochar 
9848bfb88eSYatharth Kochar /*******************************************************************************
99128daee2SAntonio Nino Diaz  * Utility functions to keep track of the images that are loaded at any time.
100128daee2SAntonio Nino Diaz  ******************************************************************************/
101128daee2SAntonio Nino Diaz 
102128daee2SAntonio Nino Diaz #ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
103128daee2SAntonio Nino Diaz #define FWU_MAX_SIMULTANEOUS_IMAGES	PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
104128daee2SAntonio Nino Diaz #else
105128daee2SAntonio Nino Diaz #define FWU_MAX_SIMULTANEOUS_IMAGES	10
106128daee2SAntonio Nino Diaz #endif
107128daee2SAntonio Nino Diaz 
108279faa6dSAmbroise Vincent static unsigned int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = {
109128daee2SAntonio Nino Diaz 	[0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID
110128daee2SAntonio Nino Diaz };
111128daee2SAntonio Nino Diaz 
112128daee2SAntonio Nino Diaz /*
113128daee2SAntonio Nino Diaz  * Adds an image_id to the bl1_fwu_loaded_ids array.
114128daee2SAntonio Nino Diaz  * Returns 0 on success, 1 on error.
115128daee2SAntonio Nino Diaz  */
116279faa6dSAmbroise Vincent static int bl1_fwu_add_loaded_id(unsigned int image_id)
117128daee2SAntonio Nino Diaz {
118128daee2SAntonio Nino Diaz 	int i;
119128daee2SAntonio Nino Diaz 
120128daee2SAntonio Nino Diaz 	/* Check if the ID is already in the list */
121128daee2SAntonio Nino Diaz 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
122128daee2SAntonio Nino Diaz 		if (bl1_fwu_loaded_ids[i] == image_id)
123128daee2SAntonio Nino Diaz 			return 0;
124128daee2SAntonio Nino Diaz 	}
125128daee2SAntonio Nino Diaz 
126128daee2SAntonio Nino Diaz 	/* Find an empty slot */
127128daee2SAntonio Nino Diaz 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
128128daee2SAntonio Nino Diaz 		if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) {
129128daee2SAntonio Nino Diaz 			bl1_fwu_loaded_ids[i] = image_id;
130128daee2SAntonio Nino Diaz 			return 0;
131128daee2SAntonio Nino Diaz 		}
132128daee2SAntonio Nino Diaz 	}
133128daee2SAntonio Nino Diaz 
134128daee2SAntonio Nino Diaz 	return 1;
135128daee2SAntonio Nino Diaz }
136128daee2SAntonio Nino Diaz 
137128daee2SAntonio Nino Diaz /*
138128daee2SAntonio Nino Diaz  * Removes an image_id from the bl1_fwu_loaded_ids array.
139128daee2SAntonio Nino Diaz  * Returns 0 on success, 1 on error.
140128daee2SAntonio Nino Diaz  */
141279faa6dSAmbroise Vincent static int bl1_fwu_remove_loaded_id(unsigned int image_id)
142128daee2SAntonio Nino Diaz {
143128daee2SAntonio Nino Diaz 	int i;
144128daee2SAntonio Nino Diaz 
145128daee2SAntonio Nino Diaz 	/* Find the ID */
146128daee2SAntonio Nino Diaz 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
147128daee2SAntonio Nino Diaz 		if (bl1_fwu_loaded_ids[i] == image_id) {
148128daee2SAntonio Nino Diaz 			bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID;
149128daee2SAntonio Nino Diaz 			return 0;
150128daee2SAntonio Nino Diaz 		}
151128daee2SAntonio Nino Diaz 	}
152128daee2SAntonio Nino Diaz 
153128daee2SAntonio Nino Diaz 	return 1;
154128daee2SAntonio Nino Diaz }
155128daee2SAntonio Nino Diaz 
156128daee2SAntonio Nino Diaz /*******************************************************************************
157128daee2SAntonio Nino Diaz  * This function checks if the specified image overlaps another image already
158128daee2SAntonio Nino Diaz  * loaded. It returns 0 if there is no overlap, a negative error code otherwise.
159128daee2SAntonio Nino Diaz  ******************************************************************************/
160279faa6dSAmbroise Vincent static int bl1_fwu_image_check_overlaps(unsigned int image_id)
161128daee2SAntonio Nino Diaz {
162128daee2SAntonio Nino Diaz 	const image_desc_t *image_desc, *checked_image_desc;
163128daee2SAntonio Nino Diaz 	const image_info_t *info, *checked_info;
164128daee2SAntonio Nino Diaz 
165128daee2SAntonio Nino Diaz 	uintptr_t image_base, image_end;
166128daee2SAntonio Nino Diaz 	uintptr_t checked_image_base, checked_image_end;
167128daee2SAntonio Nino Diaz 
168128daee2SAntonio Nino Diaz 	checked_image_desc = bl1_plat_get_image_desc(image_id);
169128daee2SAntonio Nino Diaz 	checked_info = &checked_image_desc->image_info;
170128daee2SAntonio Nino Diaz 
171128daee2SAntonio Nino Diaz 	/* Image being checked mustn't be empty. */
172128daee2SAntonio Nino Diaz 	assert(checked_info->image_size != 0);
173128daee2SAntonio Nino Diaz 
174128daee2SAntonio Nino Diaz 	checked_image_base = checked_info->image_base;
175128daee2SAntonio Nino Diaz 	checked_image_end = checked_image_base + checked_info->image_size - 1;
176ee05ae16SSoby Mathew 	/* No need to check for overflows, it's done in bl1_fwu_image_copy(). */
177128daee2SAntonio Nino Diaz 
178128daee2SAntonio Nino Diaz 	for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
179128daee2SAntonio Nino Diaz 
180ee05ae16SSoby Mathew 		/* Skip INVALID_IMAGE_IDs and don't check image against itself */
181ee05ae16SSoby Mathew 		if ((bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) ||
182ee05ae16SSoby Mathew 				(bl1_fwu_loaded_ids[i] == image_id))
183128daee2SAntonio Nino Diaz 			continue;
184128daee2SAntonio Nino Diaz 
185128daee2SAntonio Nino Diaz 		image_desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]);
186128daee2SAntonio Nino Diaz 
187128daee2SAntonio Nino Diaz 		/* Only check images that are loaded or being loaded. */
188ee05ae16SSoby Mathew 		assert (image_desc && image_desc->state != IMAGE_STATE_RESET);
189128daee2SAntonio Nino Diaz 
190128daee2SAntonio Nino Diaz 		info = &image_desc->image_info;
191128daee2SAntonio Nino Diaz 
192128daee2SAntonio Nino Diaz 		/* There cannot be overlaps with an empty image. */
193128daee2SAntonio Nino Diaz 		if (info->image_size == 0)
194128daee2SAntonio Nino Diaz 			continue;
195128daee2SAntonio Nino Diaz 
196128daee2SAntonio Nino Diaz 		image_base = info->image_base;
197128daee2SAntonio Nino Diaz 		image_end = image_base + info->image_size - 1;
198128daee2SAntonio Nino Diaz 		/*
199128daee2SAntonio Nino Diaz 		 * Overflows cannot happen. It is checked in
200128daee2SAntonio Nino Diaz 		 * bl1_fwu_image_copy() when the image goes from RESET to
201128daee2SAntonio Nino Diaz 		 * COPYING or COPIED.
202128daee2SAntonio Nino Diaz 		 */
203128daee2SAntonio Nino Diaz 		assert (image_end > image_base);
204128daee2SAntonio Nino Diaz 
205128daee2SAntonio Nino Diaz 		/* Check if there are overlaps. */
206128daee2SAntonio Nino Diaz 		if (!(image_end < checked_image_base ||
207128daee2SAntonio Nino Diaz 		    checked_image_end < image_base)) {
208128daee2SAntonio Nino Diaz 			VERBOSE("Image with ID %d overlaps existing image with ID %d",
209128daee2SAntonio Nino Diaz 				checked_image_desc->image_id, image_desc->image_id);
210128daee2SAntonio Nino Diaz 			return -EPERM;
211128daee2SAntonio Nino Diaz 		}
212128daee2SAntonio Nino Diaz 	}
213128daee2SAntonio Nino Diaz 
214128daee2SAntonio Nino Diaz 	return 0;
215128daee2SAntonio Nino Diaz }
216128daee2SAntonio Nino Diaz 
217128daee2SAntonio Nino Diaz /*******************************************************************************
21848bfb88eSYatharth Kochar  * This function is responsible for copying secure images in AP Secure RAM.
21948bfb88eSYatharth Kochar  ******************************************************************************/
22048bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id,
22148bfb88eSYatharth Kochar 			uintptr_t image_src,
22248bfb88eSYatharth Kochar 			unsigned int block_size,
22348bfb88eSYatharth Kochar 			unsigned int image_size,
22448bfb88eSYatharth Kochar 			unsigned int flags)
22548bfb88eSYatharth Kochar {
2269f1489e4SSandrine Bailleux 	uintptr_t dest_addr;
227b38a9e5cSSandrine Bailleux 	unsigned int remaining;
22848bfb88eSYatharth Kochar 
22948bfb88eSYatharth Kochar 	/* Get the image descriptor. */
23048bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
2319f1489e4SSandrine Bailleux 	if (!image_desc) {
2329f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Invalid image ID %u\n", image_id);
23348bfb88eSYatharth Kochar 		return -EPERM;
23448bfb88eSYatharth Kochar 	}
23548bfb88eSYatharth Kochar 
2369f1489e4SSandrine Bailleux 	/*
2379f1489e4SSandrine Bailleux 	 * The request must originate from a non-secure caller and target a
2389f1489e4SSandrine Bailleux 	 * secure image. Any other scenario is invalid.
2399f1489e4SSandrine Bailleux 	 */
2409f1489e4SSandrine Bailleux 	if (GET_SECURITY_STATE(flags) == SECURE) {
2419f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed from secure world.\n");
2429f1489e4SSandrine Bailleux 		return -EPERM;
2439f1489e4SSandrine Bailleux 	}
2449f1489e4SSandrine Bailleux 	if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) {
2459f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
2469f1489e4SSandrine Bailleux 		return -EPERM;
2479f1489e4SSandrine Bailleux 	}
2489f1489e4SSandrine Bailleux 
2499f1489e4SSandrine Bailleux 	/* Check whether the FWU state machine is in the correct state. */
2509f1489e4SSandrine Bailleux 	if ((image_desc->state != IMAGE_STATE_RESET) &&
2519f1489e4SSandrine Bailleux 	    (image_desc->state != IMAGE_STATE_COPYING)) {
2529f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed at this point of the FWU"
2539f1489e4SSandrine Bailleux 			" process.\n");
25448bfb88eSYatharth Kochar 		return -EPERM;
25548bfb88eSYatharth Kochar 	}
25648bfb88eSYatharth Kochar 
257949a52d2SSandrine Bailleux 	if ((!image_src) || (!block_size) ||
258949a52d2SSandrine Bailleux 	    check_uptr_overflow(image_src, block_size - 1)) {
25948bfb88eSYatharth Kochar 		WARN("BL1-FWU: Copy not allowed due to invalid image source"
26048bfb88eSYatharth Kochar 			" or block size\n");
26148bfb88eSYatharth Kochar 		return -ENOMEM;
26248bfb88eSYatharth Kochar 	}
26348bfb88eSYatharth Kochar 
26448bfb88eSYatharth Kochar 	if (image_desc->state == IMAGE_STATE_COPYING) {
2651bfb7068SSandrine Bailleux 		/*
2661bfb7068SSandrine Bailleux 		 * There must have been at least 1 copy operation for this image
2671bfb7068SSandrine Bailleux 		 * previously.
2681bfb7068SSandrine Bailleux 		 */
2691bfb7068SSandrine Bailleux 		assert(image_desc->copied_size != 0);
2701bfb7068SSandrine Bailleux 		/*
2711bfb7068SSandrine Bailleux 		 * The image size must have been recorded in the 1st copy
2721bfb7068SSandrine Bailleux 		 * operation.
2731bfb7068SSandrine Bailleux 		 */
2749f1489e4SSandrine Bailleux 		image_size = image_desc->image_info.image_size;
2751bfb7068SSandrine Bailleux 		assert(image_size != 0);
2761bfb7068SSandrine Bailleux 		assert(image_desc->copied_size < image_size);
2771bfb7068SSandrine Bailleux 
27848bfb88eSYatharth Kochar 		INFO("BL1-FWU: Continuing image copy in blocks\n");
2799f1489e4SSandrine Bailleux 	} else { /* image_desc->state == IMAGE_STATE_RESET */
2809f1489e4SSandrine Bailleux 		INFO("BL1-FWU: Initial call to copy an image\n");
28148bfb88eSYatharth Kochar 
2829f1489e4SSandrine Bailleux 		/*
2839f1489e4SSandrine Bailleux 		 * image_size is relevant only for the 1st copy request, it is
2849f1489e4SSandrine Bailleux 		 * then ignored for subsequent calls for this image.
2859f1489e4SSandrine Bailleux 		 */
28648bfb88eSYatharth Kochar 		if (!image_size) {
2879f1489e4SSandrine Bailleux 			WARN("BL1-FWU: Copy not allowed due to invalid image"
2889f1489e4SSandrine Bailleux 				" size\n");
28948bfb88eSYatharth Kochar 			return -ENOMEM;
29048bfb88eSYatharth Kochar 		}
29148bfb88eSYatharth Kochar 
29253d703a5SYatharth Kochar 		/* Check that the image size to load is within limit */
29353d703a5SYatharth Kochar 		if (image_size > image_desc->image_info.image_max_size) {
29453d703a5SYatharth Kochar 			WARN("BL1-FWU: Image size out of bounds\n");
29553d703a5SYatharth Kochar 			return -ENOMEM;
29653d703a5SYatharth Kochar 		}
29748bfb88eSYatharth Kochar 
2989f1489e4SSandrine Bailleux 		/* Save the given image size. */
29948bfb88eSYatharth Kochar 		image_desc->image_info.image_size = image_size;
30048bfb88eSYatharth Kochar 
301128daee2SAntonio Nino Diaz 		/* Make sure the image doesn't overlap other images. */
302128daee2SAntonio Nino Diaz 		if (bl1_fwu_image_check_overlaps(image_id)) {
303128daee2SAntonio Nino Diaz 			image_desc->image_info.image_size = 0;
304128daee2SAntonio Nino Diaz 			WARN("BL1-FWU: This image overlaps another one\n");
305128daee2SAntonio Nino Diaz 			return -EPERM;
306128daee2SAntonio Nino Diaz 		}
307128daee2SAntonio Nino Diaz 
308b38a9e5cSSandrine Bailleux 		/*
309b38a9e5cSSandrine Bailleux 		 * copied_size must be explicitly initialized here because the
310b38a9e5cSSandrine Bailleux 		 * FWU code doesn't necessarily do it when it resets the state
311b38a9e5cSSandrine Bailleux 		 * machine.
312b38a9e5cSSandrine Bailleux 		 */
313b38a9e5cSSandrine Bailleux 		image_desc->copied_size = 0;
314b38a9e5cSSandrine Bailleux 	}
315b38a9e5cSSandrine Bailleux 
316b38a9e5cSSandrine Bailleux 	/*
317b38a9e5cSSandrine Bailleux 	 * If the given block size is more than the total image size
318b38a9e5cSSandrine Bailleux 	 * then clip the former to the latter.
319b38a9e5cSSandrine Bailleux 	 */
320b38a9e5cSSandrine Bailleux 	remaining = image_size - image_desc->copied_size;
321b38a9e5cSSandrine Bailleux 	if (block_size > remaining) {
322b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Block size is too big, clipping it.\n");
323b38a9e5cSSandrine Bailleux 		block_size = remaining;
324b38a9e5cSSandrine Bailleux 	}
325b38a9e5cSSandrine Bailleux 
326b38a9e5cSSandrine Bailleux 	/* Make sure the source image is mapped in memory. */
327b38a9e5cSSandrine Bailleux 	if (bl1_plat_mem_check(image_src, block_size, flags)) {
328b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Source image is not mapped.\n");
329b38a9e5cSSandrine Bailleux 		return -ENOMEM;
330b38a9e5cSSandrine Bailleux 	}
331b38a9e5cSSandrine Bailleux 
332128daee2SAntonio Nino Diaz 	if (bl1_fwu_add_loaded_id(image_id)) {
333128daee2SAntonio Nino Diaz 		WARN("BL1-FWU: Too many images loaded at the same time.\n");
334128daee2SAntonio Nino Diaz 		return -ENOMEM;
335128daee2SAntonio Nino Diaz 	}
336128daee2SAntonio Nino Diaz 
337566034fcSSoby Mathew 	/* Allow the platform to handle pre-image load before copying */
338566034fcSSoby Mathew 	if (image_desc->state == IMAGE_STATE_RESET) {
339566034fcSSoby Mathew 		if (bl1_plat_handle_pre_image_load(image_id) != 0) {
340566034fcSSoby Mathew 			ERROR("BL1-FWU: Failure in pre-image load of image id %d\n",
341566034fcSSoby Mathew 					image_id);
342566034fcSSoby Mathew 			return -EPERM;
343566034fcSSoby Mathew 		}
344566034fcSSoby Mathew 	}
345566034fcSSoby Mathew 
346b38a9e5cSSandrine Bailleux 	/* Everything looks sane. Go ahead and copy the block of data. */
347b38a9e5cSSandrine Bailleux 	dest_addr = image_desc->image_info.image_base + image_desc->copied_size;
3489f1489e4SSandrine Bailleux 	memcpy((void *) dest_addr, (const void *) image_src, block_size);
3499f1489e4SSandrine Bailleux 	flush_dcache_range(dest_addr, block_size);
35048bfb88eSYatharth Kochar 
351b38a9e5cSSandrine Bailleux 	image_desc->copied_size += block_size;
352b38a9e5cSSandrine Bailleux 	image_desc->state = (block_size == remaining) ?
353b38a9e5cSSandrine Bailleux 		IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
35448bfb88eSYatharth Kochar 
355b38a9e5cSSandrine Bailleux 	INFO("BL1-FWU: Copy operation successful.\n");
35648bfb88eSYatharth Kochar 	return 0;
35748bfb88eSYatharth Kochar }
35848bfb88eSYatharth Kochar 
35948bfb88eSYatharth Kochar /*******************************************************************************
36048bfb88eSYatharth Kochar  * This function is responsible for authenticating Normal/Secure images.
36148bfb88eSYatharth Kochar  ******************************************************************************/
36248bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id,
36348bfb88eSYatharth Kochar 			uintptr_t image_src,
36448bfb88eSYatharth Kochar 			unsigned int image_size,
36548bfb88eSYatharth Kochar 			unsigned int flags)
36648bfb88eSYatharth Kochar {
36748bfb88eSYatharth Kochar 	int result;
36848bfb88eSYatharth Kochar 	uintptr_t base_addr;
36948bfb88eSYatharth Kochar 	unsigned int total_size;
37048bfb88eSYatharth Kochar 
37148bfb88eSYatharth Kochar 	/* Get the image descriptor. */
37248bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
37348bfb88eSYatharth Kochar 	if (!image_desc)
37448bfb88eSYatharth Kochar 		return -EPERM;
37548bfb88eSYatharth Kochar 
376843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == SECURE) {
37748bfb88eSYatharth Kochar 		if (image_desc->state != IMAGE_STATE_RESET) {
37848bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication from secure world "
37948bfb88eSYatharth Kochar 				"while in invalid state\n");
38048bfb88eSYatharth Kochar 			return -EPERM;
38148bfb88eSYatharth Kochar 		}
38248bfb88eSYatharth Kochar 	} else {
383843ddee4SYatharth Kochar 		if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) {
38448bfb88eSYatharth Kochar 			if (image_desc->state != IMAGE_STATE_COPIED) {
38548bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of secure image "
38648bfb88eSYatharth Kochar 					"from non-secure world while not in copied state\n");
38748bfb88eSYatharth Kochar 				return -EPERM;
38848bfb88eSYatharth Kochar 			}
38948bfb88eSYatharth Kochar 		} else {
39048bfb88eSYatharth Kochar 			if (image_desc->state != IMAGE_STATE_RESET) {
39148bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of non-secure image "
39248bfb88eSYatharth Kochar 					"from non-secure world while in invalid state\n");
39348bfb88eSYatharth Kochar 				return -EPERM;
39448bfb88eSYatharth Kochar 			}
39548bfb88eSYatharth Kochar 		}
39648bfb88eSYatharth Kochar 	}
39748bfb88eSYatharth Kochar 
39848bfb88eSYatharth Kochar 	if (image_desc->state == IMAGE_STATE_COPIED) {
39948bfb88eSYatharth Kochar 		/*
40048bfb88eSYatharth Kochar 		 * Image is in COPIED state.
40148bfb88eSYatharth Kochar 		 * Use the stored address and size.
40248bfb88eSYatharth Kochar 		 */
40348bfb88eSYatharth Kochar 		base_addr = image_desc->image_info.image_base;
40448bfb88eSYatharth Kochar 		total_size = image_desc->image_info.image_size;
40548bfb88eSYatharth Kochar 	} else {
406949a52d2SSandrine Bailleux 		if ((!image_src) || (!image_size) ||
407949a52d2SSandrine Bailleux 		    check_uptr_overflow(image_src, image_size - 1)) {
40848bfb88eSYatharth Kochar 			WARN("BL1-FWU: Auth not allowed due to invalid"
40948bfb88eSYatharth Kochar 				" image source/size\n");
41048bfb88eSYatharth Kochar 			return -ENOMEM;
41148bfb88eSYatharth Kochar 		}
41248bfb88eSYatharth Kochar 
41348bfb88eSYatharth Kochar 		/*
41448bfb88eSYatharth Kochar 		 * Image is in RESET state.
41548bfb88eSYatharth Kochar 		 * Check the parameters and authenticate the source image in place.
41648bfb88eSYatharth Kochar 		 */
41703131c85SDan Handley 		if (bl1_plat_mem_check(image_src, image_size,	\
41803131c85SDan Handley 					image_desc->ep_info.h.attr)) {
41948bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
42048bfb88eSYatharth Kochar 			return -ENOMEM;
42148bfb88eSYatharth Kochar 		}
42248bfb88eSYatharth Kochar 
423128daee2SAntonio Nino Diaz 		if (bl1_fwu_add_loaded_id(image_id)) {
424128daee2SAntonio Nino Diaz 			WARN("BL1-FWU: Too many images loaded at the same time.\n");
425128daee2SAntonio Nino Diaz 			return -ENOMEM;
426128daee2SAntonio Nino Diaz 		}
427128daee2SAntonio Nino Diaz 
42848bfb88eSYatharth Kochar 		base_addr = image_src;
42948bfb88eSYatharth Kochar 		total_size = image_size;
43048bfb88eSYatharth Kochar 
43148bfb88eSYatharth Kochar 		/* Update the image size in the descriptor. */
43248bfb88eSYatharth Kochar 		image_desc->image_info.image_size = total_size;
43348bfb88eSYatharth Kochar 	}
43448bfb88eSYatharth Kochar 
43548bfb88eSYatharth Kochar 	/*
43648bfb88eSYatharth Kochar 	 * Authenticate the image.
43748bfb88eSYatharth Kochar 	 */
43848bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
43948bfb88eSYatharth Kochar 	result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
44048bfb88eSYatharth Kochar 	if (result != 0) {
44148bfb88eSYatharth Kochar 		WARN("BL1-FWU: Authentication Failed err=%d\n", result);
44248bfb88eSYatharth Kochar 
44348bfb88eSYatharth Kochar 		/*
44448bfb88eSYatharth Kochar 		 * Authentication has failed.
44548bfb88eSYatharth Kochar 		 * Clear the memory if the image was copied.
44648bfb88eSYatharth Kochar 		 * This is to prevent an attack where this contains
44748bfb88eSYatharth Kochar 		 * some malicious code that can somehow be executed later.
44848bfb88eSYatharth Kochar 		 */
44948bfb88eSYatharth Kochar 		if (image_desc->state == IMAGE_STATE_COPIED) {
45048bfb88eSYatharth Kochar 			/* Clear the memory.*/
451308d359bSDouglas Raillard 			zero_normalmem((void *)base_addr, total_size);
45248bfb88eSYatharth Kochar 			flush_dcache_range(base_addr, total_size);
45348bfb88eSYatharth Kochar 
45448bfb88eSYatharth Kochar 			/* Indicate that image can be copied again*/
45548bfb88eSYatharth Kochar 			image_desc->state = IMAGE_STATE_RESET;
45648bfb88eSYatharth Kochar 		}
457128daee2SAntonio Nino Diaz 
458128daee2SAntonio Nino Diaz 		/*
459128daee2SAntonio Nino Diaz 		 * Even if this fails it's ok because the ID isn't in the array.
460128daee2SAntonio Nino Diaz 		 * The image cannot be in RESET state here, it is checked at the
461128daee2SAntonio Nino Diaz 		 * beginning of the function.
462128daee2SAntonio Nino Diaz 		 */
463128daee2SAntonio Nino Diaz 		bl1_fwu_remove_loaded_id(image_id);
46448bfb88eSYatharth Kochar 		return -EAUTH;
46548bfb88eSYatharth Kochar 	}
46648bfb88eSYatharth Kochar 
46748bfb88eSYatharth Kochar 	/* Indicate that image is in authenticated state. */
46848bfb88eSYatharth Kochar 	image_desc->state = IMAGE_STATE_AUTHENTICATED;
46948bfb88eSYatharth Kochar 
470566034fcSSoby Mathew 	/* Allow the platform to handle post-image load */
471566034fcSSoby Mathew 	result = bl1_plat_handle_post_image_load(image_id);
472566034fcSSoby Mathew 	if (result != 0) {
473566034fcSSoby Mathew 		ERROR("BL1-FWU: Failure %d in post-image load of image id %d\n",
474566034fcSSoby Mathew 				result, image_id);
475566034fcSSoby Mathew 		/*
476566034fcSSoby Mathew 		 * Panic here as the platform handling of post-image load is
477566034fcSSoby Mathew 		 * not correct.
478566034fcSSoby Mathew 		 */
479566034fcSSoby Mathew 		plat_error_handler(result);
480566034fcSSoby Mathew 	}
481566034fcSSoby Mathew 
48248bfb88eSYatharth Kochar 	/*
48348bfb88eSYatharth Kochar 	 * Flush image_info to memory so that other
48448bfb88eSYatharth Kochar 	 * secure world images can see changes.
48548bfb88eSYatharth Kochar 	 */
48648bfb88eSYatharth Kochar 	flush_dcache_range((unsigned long)&image_desc->image_info,
48748bfb88eSYatharth Kochar 		sizeof(image_info_t));
48848bfb88eSYatharth Kochar 
48948bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authentication was successful\n");
49048bfb88eSYatharth Kochar 
49148bfb88eSYatharth Kochar 	return 0;
49248bfb88eSYatharth Kochar }
49348bfb88eSYatharth Kochar 
49448bfb88eSYatharth Kochar /*******************************************************************************
49548bfb88eSYatharth Kochar  * This function is responsible for executing Secure images.
49648bfb88eSYatharth Kochar  ******************************************************************************/
49748bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id,
49848bfb88eSYatharth Kochar 			void **handle,
49948bfb88eSYatharth Kochar 			unsigned int flags)
50048bfb88eSYatharth Kochar {
50148bfb88eSYatharth Kochar 	/* Get the image descriptor. */
50248bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
50348bfb88eSYatharth Kochar 
50448bfb88eSYatharth Kochar 	/*
50548bfb88eSYatharth Kochar 	 * Execution is NOT allowed if:
50628955d57SDan Handley 	 * image_id is invalid OR
50748bfb88eSYatharth Kochar 	 * Caller is from Secure world OR
50848bfb88eSYatharth Kochar 	 * Image is Non-Secure OR
50948bfb88eSYatharth Kochar 	 * Image is Non-Executable OR
51048bfb88eSYatharth Kochar 	 * Image is NOT in AUTHENTICATED state.
51148bfb88eSYatharth Kochar 	 */
51248bfb88eSYatharth Kochar 	if ((!image_desc) ||
513843ddee4SYatharth Kochar 	    (GET_SECURITY_STATE(flags) == SECURE) ||
514843ddee4SYatharth Kochar 	    (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) ||
515843ddee4SYatharth Kochar 	    (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) ||
51648bfb88eSYatharth Kochar 	    (image_desc->state != IMAGE_STATE_AUTHENTICATED)) {
51748bfb88eSYatharth Kochar 		WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
51848bfb88eSYatharth Kochar 		return -EPERM;
51948bfb88eSYatharth Kochar 	}
52048bfb88eSYatharth Kochar 
52148bfb88eSYatharth Kochar 	INFO("BL1-FWU: Executing Secure image\n");
52248bfb88eSYatharth Kochar 
523*402b3cf8SJulius Werner #ifdef __aarch64__
52448bfb88eSYatharth Kochar 	/* Save NS-EL1 system registers. */
52548bfb88eSYatharth Kochar 	cm_el1_sysregs_context_save(NON_SECURE);
526a4409008Sdp-arm #endif
52748bfb88eSYatharth Kochar 
52848bfb88eSYatharth Kochar 	/* Prepare the image for execution. */
52948bfb88eSYatharth Kochar 	bl1_prepare_next_image(image_id);
53048bfb88eSYatharth Kochar 
53148bfb88eSYatharth Kochar 	/* Update the secure image id. */
53248bfb88eSYatharth Kochar 	sec_exec_image_id = image_id;
53348bfb88eSYatharth Kochar 
534*402b3cf8SJulius Werner #ifdef __aarch64__
53548bfb88eSYatharth Kochar 	*handle = cm_get_context(SECURE);
536a4409008Sdp-arm #else
537a4409008Sdp-arm 	*handle = smc_get_ctx(SECURE);
538a4409008Sdp-arm #endif
53948bfb88eSYatharth Kochar 	return 0;
54048bfb88eSYatharth Kochar }
54148bfb88eSYatharth Kochar 
54248bfb88eSYatharth Kochar /*******************************************************************************
54328955d57SDan Handley  * This function is responsible for resuming execution in the other security
54428955d57SDan Handley  * world
54548bfb88eSYatharth Kochar  ******************************************************************************/
54628955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param,
54748bfb88eSYatharth Kochar 			void **handle,
54848bfb88eSYatharth Kochar 			unsigned int flags)
54948bfb88eSYatharth Kochar {
55048bfb88eSYatharth Kochar 	image_desc_t *image_desc;
55148bfb88eSYatharth Kochar 	unsigned int resume_sec_state;
552843ddee4SYatharth Kochar 	unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
55348bfb88eSYatharth Kochar 
55448bfb88eSYatharth Kochar 	/* Get the image descriptor for last executed secure image id. */
55548bfb88eSYatharth Kochar 	image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
55628955d57SDan Handley 	if (caller_sec_state == NON_SECURE) {
55728955d57SDan Handley 		if (!image_desc) {
55828955d57SDan Handley 			WARN("BL1-FWU: Resume not allowed due to no available"
55928955d57SDan Handley 				"secure image\n");
56048bfb88eSYatharth Kochar 			return -EPERM;
56148bfb88eSYatharth Kochar 		}
56228955d57SDan Handley 	} else {
56328955d57SDan Handley 		/* image_desc must be valid for secure world callers */
56428955d57SDan Handley 		assert(image_desc);
56528955d57SDan Handley 	}
56628955d57SDan Handley 
567843ddee4SYatharth Kochar 	assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
568843ddee4SYatharth Kochar 	assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
56928955d57SDan Handley 
57028955d57SDan Handley 	if (caller_sec_state == SECURE) {
57128955d57SDan Handley 		assert(image_desc->state == IMAGE_STATE_EXECUTED);
57248bfb88eSYatharth Kochar 
57348bfb88eSYatharth Kochar 		/* Update the flags. */
57448bfb88eSYatharth Kochar 		image_desc->state = IMAGE_STATE_INTERRUPTED;
57548bfb88eSYatharth Kochar 		resume_sec_state = NON_SECURE;
57648bfb88eSYatharth Kochar 	} else {
57728955d57SDan Handley 		assert(image_desc->state == IMAGE_STATE_INTERRUPTED);
57848bfb88eSYatharth Kochar 
57948bfb88eSYatharth Kochar 		/* Update the flags. */
58048bfb88eSYatharth Kochar 		image_desc->state = IMAGE_STATE_EXECUTED;
58148bfb88eSYatharth Kochar 		resume_sec_state = SECURE;
58248bfb88eSYatharth Kochar 	}
58348bfb88eSYatharth Kochar 
584a4409008Sdp-arm 	INFO("BL1-FWU: Resuming %s world context\n",
585a4409008Sdp-arm 		(resume_sec_state == SECURE) ? "secure" : "normal");
586a4409008Sdp-arm 
587*402b3cf8SJulius Werner #ifdef __aarch64__
58848bfb88eSYatharth Kochar 	/* Save the EL1 system registers of calling world. */
58928955d57SDan Handley 	cm_el1_sysregs_context_save(caller_sec_state);
59048bfb88eSYatharth Kochar 
59148bfb88eSYatharth Kochar 	/* Restore the EL1 system registers of resuming world. */
59248bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(resume_sec_state);
59348bfb88eSYatharth Kochar 
59448bfb88eSYatharth Kochar 	/* Update the next context. */
59548bfb88eSYatharth Kochar 	cm_set_next_eret_context(resume_sec_state);
59648bfb88eSYatharth Kochar 
59748bfb88eSYatharth Kochar 	*handle = cm_get_context(resume_sec_state);
598a4409008Sdp-arm #else
599a4409008Sdp-arm 	/* Update the next context. */
600a4409008Sdp-arm 	cm_set_next_context(cm_get_context(resume_sec_state));
601a4409008Sdp-arm 
602a4409008Sdp-arm 	/* Prepare the smc context for the next BL image. */
603a4409008Sdp-arm 	smc_set_next_ctx(resume_sec_state);
604a4409008Sdp-arm 
605a4409008Sdp-arm 	*handle = smc_get_ctx(resume_sec_state);
606a4409008Sdp-arm #endif
60748bfb88eSYatharth Kochar 	return image_param;
60848bfb88eSYatharth Kochar }
60948bfb88eSYatharth Kochar 
61048bfb88eSYatharth Kochar /*******************************************************************************
61148bfb88eSYatharth Kochar  * This function is responsible for resuming normal world context.
61248bfb88eSYatharth Kochar  ******************************************************************************/
61348bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
61448bfb88eSYatharth Kochar {
61528955d57SDan Handley 	image_desc_t *image_desc;
61648bfb88eSYatharth Kochar 
61728955d57SDan Handley 	/* Make sure caller is from the secure world */
618843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == NON_SECURE) {
61928955d57SDan Handley 		WARN("BL1-FWU: Image done not allowed from normal world\n");
62048bfb88eSYatharth Kochar 		return -EPERM;
62148bfb88eSYatharth Kochar 	}
62248bfb88eSYatharth Kochar 
62328955d57SDan Handley 	/* Get the image descriptor for last executed secure image id */
62428955d57SDan Handley 	image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
62528955d57SDan Handley 
62628955d57SDan Handley 	/* image_desc must correspond to a valid secure executing image */
62728955d57SDan Handley 	assert(image_desc);
628843ddee4SYatharth Kochar 	assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
629843ddee4SYatharth Kochar 	assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
63028955d57SDan Handley 	assert(image_desc->state == IMAGE_STATE_EXECUTED);
63128955d57SDan Handley 
632128daee2SAntonio Nino Diaz #if ENABLE_ASSERTIONS
633128daee2SAntonio Nino Diaz 	int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id);
634128daee2SAntonio Nino Diaz 	assert(rc == 0);
635128daee2SAntonio Nino Diaz #else
636128daee2SAntonio Nino Diaz 	bl1_fwu_remove_loaded_id(sec_exec_image_id);
637128daee2SAntonio Nino Diaz #endif
638128daee2SAntonio Nino Diaz 
63948bfb88eSYatharth Kochar 	/* Update the flags. */
64048bfb88eSYatharth Kochar 	image_desc->state = IMAGE_STATE_RESET;
64148bfb88eSYatharth Kochar 	sec_exec_image_id = INVALID_IMAGE_ID;
64248bfb88eSYatharth Kochar 
643a4409008Sdp-arm 	INFO("BL1-FWU: Resuming Normal world context\n");
644*402b3cf8SJulius Werner #ifdef __aarch64__
64548bfb88eSYatharth Kochar 	/*
64648bfb88eSYatharth Kochar 	 * Secure world is done so no need to save the context.
64748bfb88eSYatharth Kochar 	 * Just restore the Non-Secure context.
64848bfb88eSYatharth Kochar 	 */
64948bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(NON_SECURE);
65048bfb88eSYatharth Kochar 
65148bfb88eSYatharth Kochar 	/* Update the next context. */
65248bfb88eSYatharth Kochar 	cm_set_next_eret_context(NON_SECURE);
65348bfb88eSYatharth Kochar 
65448bfb88eSYatharth Kochar 	*handle = cm_get_context(NON_SECURE);
655a4409008Sdp-arm #else
656a4409008Sdp-arm 	/* Update the next context. */
657a4409008Sdp-arm 	cm_set_next_context(cm_get_context(NON_SECURE));
658a4409008Sdp-arm 
659a4409008Sdp-arm 	/* Prepare the smc context for the next BL image. */
660a4409008Sdp-arm 	smc_set_next_ctx(NON_SECURE);
661a4409008Sdp-arm 
662a4409008Sdp-arm 	*handle = smc_get_ctx(NON_SECURE);
663a4409008Sdp-arm #endif
66448bfb88eSYatharth Kochar 	return 0;
66548bfb88eSYatharth Kochar }
66648bfb88eSYatharth Kochar 
66748bfb88eSYatharth Kochar /*******************************************************************************
66848bfb88eSYatharth Kochar  * This function provides the opportunity for users to perform any
66948bfb88eSYatharth Kochar  * platform specific handling after the Firmware update is done.
67048bfb88eSYatharth Kochar  ******************************************************************************/
6711f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
67248bfb88eSYatharth Kochar {
67348bfb88eSYatharth Kochar 	NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
67448bfb88eSYatharth Kochar 
67548bfb88eSYatharth Kochar 	/*
67648bfb88eSYatharth Kochar 	 * Call platform done function.
67748bfb88eSYatharth Kochar 	 */
6781f37b944SDan Handley 	bl1_plat_fwu_done(client_cookie, reserved);
67948bfb88eSYatharth Kochar 	assert(0);
68048bfb88eSYatharth Kochar }
6819d6fc3c3SAntonio Nino Diaz 
6829d6fc3c3SAntonio Nino Diaz /*******************************************************************************
6839d6fc3c3SAntonio Nino Diaz  * This function resets an image to IMAGE_STATE_RESET. It fails if the image is
6849d6fc3c3SAntonio Nino Diaz  * being executed.
6859d6fc3c3SAntonio Nino Diaz  ******************************************************************************/
6869d6fc3c3SAntonio Nino Diaz static int bl1_fwu_image_reset(unsigned int image_id, unsigned int flags)
6879d6fc3c3SAntonio Nino Diaz {
6889d6fc3c3SAntonio Nino Diaz 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
6899d6fc3c3SAntonio Nino Diaz 
6909d6fc3c3SAntonio Nino Diaz 	if ((!image_desc) || (GET_SECURITY_STATE(flags) == SECURE)) {
6919d6fc3c3SAntonio Nino Diaz 		WARN("BL1-FWU: Reset not allowed due to invalid args\n");
6929d6fc3c3SAntonio Nino Diaz 		return -EPERM;
6939d6fc3c3SAntonio Nino Diaz 	}
6949d6fc3c3SAntonio Nino Diaz 
6959d6fc3c3SAntonio Nino Diaz 	switch (image_desc->state) {
6969d6fc3c3SAntonio Nino Diaz 
6979d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_RESET:
6989d6fc3c3SAntonio Nino Diaz 		/* Nothing to do. */
6999d6fc3c3SAntonio Nino Diaz 		break;
7009d6fc3c3SAntonio Nino Diaz 
7019d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_INTERRUPTED:
7029d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_AUTHENTICATED:
7039d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_COPIED:
7049d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_COPYING:
7059d6fc3c3SAntonio Nino Diaz 
7069d6fc3c3SAntonio Nino Diaz 		if (bl1_fwu_remove_loaded_id(image_id)) {
7079d6fc3c3SAntonio Nino Diaz 			WARN("BL1-FWU: Image reset couldn't find the image ID\n");
7089d6fc3c3SAntonio Nino Diaz 			return -EPERM;
7099d6fc3c3SAntonio Nino Diaz 		}
7109d6fc3c3SAntonio Nino Diaz 
711ee05ae16SSoby Mathew 		if (image_desc->copied_size) {
712ee05ae16SSoby Mathew 			/* Clear the memory if the image is copied */
713ee05ae16SSoby Mathew 			assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
714ee05ae16SSoby Mathew 
7159d6fc3c3SAntonio Nino Diaz 			zero_normalmem((void *)image_desc->image_info.image_base,
7169d6fc3c3SAntonio Nino Diaz 					image_desc->copied_size);
7179d6fc3c3SAntonio Nino Diaz 			flush_dcache_range(image_desc->image_info.image_base,
7189d6fc3c3SAntonio Nino Diaz 					image_desc->copied_size);
719ee05ae16SSoby Mathew 		}
7209d6fc3c3SAntonio Nino Diaz 
7219d6fc3c3SAntonio Nino Diaz 		/* Reset status variables */
7229d6fc3c3SAntonio Nino Diaz 		image_desc->copied_size = 0;
7239d6fc3c3SAntonio Nino Diaz 		image_desc->image_info.image_size = 0;
7249d6fc3c3SAntonio Nino Diaz 		image_desc->state = IMAGE_STATE_RESET;
7259d6fc3c3SAntonio Nino Diaz 
7269d6fc3c3SAntonio Nino Diaz 		/* Clear authentication state */
7279d6fc3c3SAntonio Nino Diaz 		auth_img_flags[image_id] = 0;
7289d6fc3c3SAntonio Nino Diaz 
7299d6fc3c3SAntonio Nino Diaz 		break;
7309d6fc3c3SAntonio Nino Diaz 
7319d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_EXECUTED:
7329d6fc3c3SAntonio Nino Diaz 	default:
733a08a2014SDaniel Boulby 		assert(0); /* Unreachable */
734c9662db9SJonathan Wright 		break;
7359d6fc3c3SAntonio Nino Diaz 	}
7369d6fc3c3SAntonio Nino Diaz 
7379d6fc3c3SAntonio Nino Diaz 	return 0;
7389d6fc3c3SAntonio Nino Diaz }
739