xref: /rk3399_ARM-atf/bl1/bl1_fwu.c (revision e83738c07417739315b528db93f2dedea8e51961)
148bfb88eSYatharth Kochar /*
22fe75a2dSZelalem  * Copyright (c) 2015-2020, 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  ******************************************************************************/
bl1_fwu_smc_handler(unsigned int smc_fid,u_register_t x1,u_register_t x2,u_register_t x3,u_register_t x4,void * cookie,void * handle,unsigned int flags)582fe75a2dSZelalem u_register_t bl1_fwu_smc_handler(unsigned int smc_fid,
592fe75a2dSZelalem 			u_register_t x1,
602fe75a2dSZelalem 			u_register_t x2,
612fe75a2dSZelalem 			u_register_t x3,
622fe75a2dSZelalem 			u_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:
703443a702SJohn Powell 		SMC_RET1(handle, bl1_fwu_image_copy((uint32_t)x1, x2,
713443a702SJohn Powell 			(uint32_t)x3, (uint32_t)x4, flags));
7248bfb88eSYatharth Kochar 
7348bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_AUTH:
743443a702SJohn Powell 		SMC_RET1(handle, bl1_fwu_image_auth((uint32_t)x1, x2,
753443a702SJohn Powell 			(uint32_t)x3, flags));
7648bfb88eSYatharth Kochar 
7748bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_EXECUTE:
783443a702SJohn Powell 		SMC_RET1(handle, bl1_fwu_image_execute((uint32_t)x1, &handle,
793443a702SJohn Powell 			flags));
8048bfb88eSYatharth Kochar 
8148bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_RESUME:
823443a702SJohn Powell 		SMC_RET1(handle, bl1_fwu_image_resume((register_t)x1, &handle,
833443a702SJohn Powell 			flags));
8448bfb88eSYatharth Kochar 
8548bfb88eSYatharth Kochar 	case FWU_SMC_SEC_IMAGE_DONE:
8648bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
8748bfb88eSYatharth Kochar 
889d6fc3c3SAntonio Nino Diaz 	case FWU_SMC_IMAGE_RESET:
893443a702SJohn Powell 		SMC_RET1(handle, bl1_fwu_image_reset((uint32_t)x1, flags));
909d6fc3c3SAntonio Nino Diaz 
9148bfb88eSYatharth Kochar 	case FWU_SMC_UPDATE_DONE:
921f37b944SDan Handley 		bl1_fwu_done((void *)x1, NULL);
9348bfb88eSYatharth Kochar 
9448bfb88eSYatharth Kochar 	default:
953443a702SJohn Powell 		assert(false); /* Unreachable */
9648bfb88eSYatharth Kochar 		break;
9748bfb88eSYatharth Kochar 	}
9848bfb88eSYatharth Kochar 
997a317a70SAntonio Nino Diaz 	SMC_RET1(handle, SMC_UNK);
10048bfb88eSYatharth Kochar }
10148bfb88eSYatharth Kochar 
10248bfb88eSYatharth Kochar /*******************************************************************************
103128daee2SAntonio Nino Diaz  * Utility functions to keep track of the images that are loaded at any time.
104128daee2SAntonio Nino Diaz  ******************************************************************************/
105128daee2SAntonio Nino Diaz 
106128daee2SAntonio Nino Diaz #ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
107128daee2SAntonio Nino Diaz #define FWU_MAX_SIMULTANEOUS_IMAGES	PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
108128daee2SAntonio Nino Diaz #else
109128daee2SAntonio Nino Diaz #define FWU_MAX_SIMULTANEOUS_IMAGES	10
110128daee2SAntonio Nino Diaz #endif
111128daee2SAntonio Nino Diaz 
112279faa6dSAmbroise Vincent static unsigned int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = {
113128daee2SAntonio Nino Diaz 	[0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID
114128daee2SAntonio Nino Diaz };
115128daee2SAntonio Nino Diaz 
116128daee2SAntonio Nino Diaz /*
117128daee2SAntonio Nino Diaz  * Adds an image_id to the bl1_fwu_loaded_ids array.
118128daee2SAntonio Nino Diaz  * Returns 0 on success, 1 on error.
119128daee2SAntonio Nino Diaz  */
bl1_fwu_add_loaded_id(unsigned int image_id)120279faa6dSAmbroise Vincent static int bl1_fwu_add_loaded_id(unsigned int image_id)
121128daee2SAntonio Nino Diaz {
122128daee2SAntonio Nino Diaz 	int i;
123128daee2SAntonio Nino Diaz 
124128daee2SAntonio Nino Diaz 	/* Check if the ID is already in the list */
125128daee2SAntonio Nino Diaz 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
126128daee2SAntonio Nino Diaz 		if (bl1_fwu_loaded_ids[i] == image_id)
127128daee2SAntonio Nino Diaz 			return 0;
128128daee2SAntonio Nino Diaz 	}
129128daee2SAntonio Nino Diaz 
130128daee2SAntonio Nino Diaz 	/* Find an empty slot */
131128daee2SAntonio Nino Diaz 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
132128daee2SAntonio Nino Diaz 		if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) {
133128daee2SAntonio Nino Diaz 			bl1_fwu_loaded_ids[i] = image_id;
134128daee2SAntonio Nino Diaz 			return 0;
135128daee2SAntonio Nino Diaz 		}
136128daee2SAntonio Nino Diaz 	}
137128daee2SAntonio Nino Diaz 
138128daee2SAntonio Nino Diaz 	return 1;
139128daee2SAntonio Nino Diaz }
140128daee2SAntonio Nino Diaz 
141128daee2SAntonio Nino Diaz /*
142128daee2SAntonio Nino Diaz  * Removes an image_id from the bl1_fwu_loaded_ids array.
143128daee2SAntonio Nino Diaz  * Returns 0 on success, 1 on error.
144128daee2SAntonio Nino Diaz  */
bl1_fwu_remove_loaded_id(unsigned int image_id)145279faa6dSAmbroise Vincent static int bl1_fwu_remove_loaded_id(unsigned int image_id)
146128daee2SAntonio Nino Diaz {
147128daee2SAntonio Nino Diaz 	int i;
148128daee2SAntonio Nino Diaz 
149128daee2SAntonio Nino Diaz 	/* Find the ID */
150128daee2SAntonio Nino Diaz 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
151128daee2SAntonio Nino Diaz 		if (bl1_fwu_loaded_ids[i] == image_id) {
152128daee2SAntonio Nino Diaz 			bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID;
153128daee2SAntonio Nino Diaz 			return 0;
154128daee2SAntonio Nino Diaz 		}
155128daee2SAntonio Nino Diaz 	}
156128daee2SAntonio Nino Diaz 
157128daee2SAntonio Nino Diaz 	return 1;
158128daee2SAntonio Nino Diaz }
159128daee2SAntonio Nino Diaz 
160128daee2SAntonio Nino Diaz /*******************************************************************************
161128daee2SAntonio Nino Diaz  * This function checks if the specified image overlaps another image already
162128daee2SAntonio Nino Diaz  * loaded. It returns 0 if there is no overlap, a negative error code otherwise.
163128daee2SAntonio Nino Diaz  ******************************************************************************/
bl1_fwu_image_check_overlaps(unsigned int image_id)164279faa6dSAmbroise Vincent static int bl1_fwu_image_check_overlaps(unsigned int image_id)
165128daee2SAntonio Nino Diaz {
1663443a702SJohn Powell 	const image_desc_t *desc, *checked_desc;
167128daee2SAntonio Nino Diaz 	const image_info_t *info, *checked_info;
168128daee2SAntonio Nino Diaz 
169128daee2SAntonio Nino Diaz 	uintptr_t image_base, image_end;
170128daee2SAntonio Nino Diaz 	uintptr_t checked_image_base, checked_image_end;
171128daee2SAntonio Nino Diaz 
1723443a702SJohn Powell 	checked_desc = bl1_plat_get_image_desc(image_id);
173*2d3b44e3SManish V Badarkhe 
174*2d3b44e3SManish V Badarkhe 	assert(checked_desc != NULL);
175*2d3b44e3SManish V Badarkhe 
1763443a702SJohn Powell 	checked_info = &checked_desc->image_info;
177128daee2SAntonio Nino Diaz 
178128daee2SAntonio Nino Diaz 	/* Image being checked mustn't be empty. */
179128daee2SAntonio Nino Diaz 	assert(checked_info->image_size != 0);
180128daee2SAntonio Nino Diaz 
181128daee2SAntonio Nino Diaz 	checked_image_base = checked_info->image_base;
182128daee2SAntonio Nino Diaz 	checked_image_end = checked_image_base + checked_info->image_size - 1;
183ee05ae16SSoby Mathew 	/* No need to check for overflows, it's done in bl1_fwu_image_copy(). */
184128daee2SAntonio Nino Diaz 
185128daee2SAntonio Nino Diaz 	for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
186128daee2SAntonio Nino Diaz 
187ee05ae16SSoby Mathew 		/* Skip INVALID_IMAGE_IDs and don't check image against itself */
188ee05ae16SSoby Mathew 		if ((bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) ||
189ee05ae16SSoby Mathew 				(bl1_fwu_loaded_ids[i] == image_id))
190128daee2SAntonio Nino Diaz 			continue;
191128daee2SAntonio Nino Diaz 
1923443a702SJohn Powell 		desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]);
193128daee2SAntonio Nino Diaz 
194128daee2SAntonio Nino Diaz 		/* Only check images that are loaded or being loaded. */
1953443a702SJohn Powell 		assert ((desc != NULL) && (desc->state != IMAGE_STATE_RESET));
196128daee2SAntonio Nino Diaz 
1973443a702SJohn Powell 		info = &desc->image_info;
198128daee2SAntonio Nino Diaz 
199128daee2SAntonio Nino Diaz 		/* There cannot be overlaps with an empty image. */
200128daee2SAntonio Nino Diaz 		if (info->image_size == 0)
201128daee2SAntonio Nino Diaz 			continue;
202128daee2SAntonio Nino Diaz 
203128daee2SAntonio Nino Diaz 		image_base = info->image_base;
204128daee2SAntonio Nino Diaz 		image_end = image_base + info->image_size - 1;
205128daee2SAntonio Nino Diaz 		/*
206128daee2SAntonio Nino Diaz 		 * Overflows cannot happen. It is checked in
207128daee2SAntonio Nino Diaz 		 * bl1_fwu_image_copy() when the image goes from RESET to
208128daee2SAntonio Nino Diaz 		 * COPYING or COPIED.
209128daee2SAntonio Nino Diaz 		 */
210128daee2SAntonio Nino Diaz 		assert (image_end > image_base);
211128daee2SAntonio Nino Diaz 
212128daee2SAntonio Nino Diaz 		/* Check if there are overlaps. */
2133443a702SJohn Powell 		if (!((image_end < checked_image_base) ||
2143443a702SJohn Powell 		    (checked_image_end < image_base))) {
215128daee2SAntonio Nino Diaz 			VERBOSE("Image with ID %d overlaps existing image with ID %d",
2163443a702SJohn Powell 				checked_desc->image_id, desc->image_id);
217128daee2SAntonio Nino Diaz 			return -EPERM;
218128daee2SAntonio Nino Diaz 		}
219128daee2SAntonio Nino Diaz 	}
220128daee2SAntonio Nino Diaz 
221128daee2SAntonio Nino Diaz 	return 0;
222128daee2SAntonio Nino Diaz }
223128daee2SAntonio Nino Diaz 
224128daee2SAntonio Nino Diaz /*******************************************************************************
22548bfb88eSYatharth Kochar  * This function is responsible for copying secure images in AP Secure RAM.
22648bfb88eSYatharth Kochar  ******************************************************************************/
bl1_fwu_image_copy(unsigned int image_id,uintptr_t image_src,unsigned int block_size,unsigned int image_size,unsigned int flags)22748bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id,
22848bfb88eSYatharth Kochar 			uintptr_t image_src,
22948bfb88eSYatharth Kochar 			unsigned int block_size,
23048bfb88eSYatharth Kochar 			unsigned int image_size,
23148bfb88eSYatharth Kochar 			unsigned int flags)
23248bfb88eSYatharth Kochar {
2339f1489e4SSandrine Bailleux 	uintptr_t dest_addr;
234b38a9e5cSSandrine Bailleux 	unsigned int remaining;
2353443a702SJohn Powell 	image_desc_t *desc;
23648bfb88eSYatharth Kochar 
23748bfb88eSYatharth Kochar 	/* Get the image descriptor. */
2383443a702SJohn Powell 	desc = bl1_plat_get_image_desc(image_id);
2393443a702SJohn Powell 	if (desc == NULL) {
2409f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Invalid image ID %u\n", image_id);
24148bfb88eSYatharth Kochar 		return -EPERM;
24248bfb88eSYatharth Kochar 	}
24348bfb88eSYatharth Kochar 
2449f1489e4SSandrine Bailleux 	/*
2459f1489e4SSandrine Bailleux 	 * The request must originate from a non-secure caller and target a
2469f1489e4SSandrine Bailleux 	 * secure image. Any other scenario is invalid.
2479f1489e4SSandrine Bailleux 	 */
2489f1489e4SSandrine Bailleux 	if (GET_SECURITY_STATE(flags) == SECURE) {
2499f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed from secure world.\n");
2509f1489e4SSandrine Bailleux 		return -EPERM;
2519f1489e4SSandrine Bailleux 	}
2523443a702SJohn Powell 	if (GET_SECURITY_STATE(desc->ep_info.h.attr) == NON_SECURE) {
2539f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
2549f1489e4SSandrine Bailleux 		return -EPERM;
2559f1489e4SSandrine Bailleux 	}
2569f1489e4SSandrine Bailleux 
2579f1489e4SSandrine Bailleux 	/* Check whether the FWU state machine is in the correct state. */
2583443a702SJohn Powell 	if ((desc->state != IMAGE_STATE_RESET) &&
2593443a702SJohn Powell 	    (desc->state != IMAGE_STATE_COPYING)) {
2609f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed at this point of the FWU"
2619f1489e4SSandrine Bailleux 			" process.\n");
26248bfb88eSYatharth Kochar 		return -EPERM;
26348bfb88eSYatharth Kochar 	}
26448bfb88eSYatharth Kochar 
2653443a702SJohn Powell 	if ((image_src == 0U) || (block_size == 0U) ||
266949a52d2SSandrine Bailleux 	    check_uptr_overflow(image_src, block_size - 1)) {
26748bfb88eSYatharth Kochar 		WARN("BL1-FWU: Copy not allowed due to invalid image source"
26848bfb88eSYatharth Kochar 			" or block size\n");
26948bfb88eSYatharth Kochar 		return -ENOMEM;
27048bfb88eSYatharth Kochar 	}
27148bfb88eSYatharth Kochar 
2723443a702SJohn Powell 	if (desc->state == IMAGE_STATE_COPYING) {
2731bfb7068SSandrine Bailleux 		/*
2741bfb7068SSandrine Bailleux 		 * There must have been at least 1 copy operation for this image
2751bfb7068SSandrine Bailleux 		 * previously.
2761bfb7068SSandrine Bailleux 		 */
2773443a702SJohn Powell 		assert(desc->copied_size != 0U);
2781bfb7068SSandrine Bailleux 		/*
2791bfb7068SSandrine Bailleux 		 * The image size must have been recorded in the 1st copy
2801bfb7068SSandrine Bailleux 		 * operation.
2811bfb7068SSandrine Bailleux 		 */
2823443a702SJohn Powell 		image_size = desc->image_info.image_size;
2831bfb7068SSandrine Bailleux 		assert(image_size != 0);
2843443a702SJohn Powell 		assert(desc->copied_size < image_size);
2851bfb7068SSandrine Bailleux 
28648bfb88eSYatharth Kochar 		INFO("BL1-FWU: Continuing image copy in blocks\n");
2873443a702SJohn Powell 	} else { /* desc->state == IMAGE_STATE_RESET */
2889f1489e4SSandrine Bailleux 		INFO("BL1-FWU: Initial call to copy an image\n");
28948bfb88eSYatharth Kochar 
2909f1489e4SSandrine Bailleux 		/*
2919f1489e4SSandrine Bailleux 		 * image_size is relevant only for the 1st copy request, it is
2929f1489e4SSandrine Bailleux 		 * then ignored for subsequent calls for this image.
2939f1489e4SSandrine Bailleux 		 */
2943443a702SJohn Powell 		if (image_size == 0) {
2959f1489e4SSandrine Bailleux 			WARN("BL1-FWU: Copy not allowed due to invalid image"
2969f1489e4SSandrine Bailleux 				" size\n");
29748bfb88eSYatharth Kochar 			return -ENOMEM;
29848bfb88eSYatharth Kochar 		}
29948bfb88eSYatharth Kochar 
30053d703a5SYatharth Kochar 		/* Check that the image size to load is within limit */
3013443a702SJohn Powell 		if (image_size > desc->image_info.image_max_size) {
30253d703a5SYatharth Kochar 			WARN("BL1-FWU: Image size out of bounds\n");
30353d703a5SYatharth Kochar 			return -ENOMEM;
30453d703a5SYatharth Kochar 		}
30548bfb88eSYatharth Kochar 
3069f1489e4SSandrine Bailleux 		/* Save the given image size. */
3073443a702SJohn Powell 		desc->image_info.image_size = image_size;
30848bfb88eSYatharth Kochar 
309128daee2SAntonio Nino Diaz 		/* Make sure the image doesn't overlap other images. */
3103443a702SJohn Powell 		if (bl1_fwu_image_check_overlaps(image_id) != 0) {
3113443a702SJohn Powell 			desc->image_info.image_size = 0;
312128daee2SAntonio Nino Diaz 			WARN("BL1-FWU: This image overlaps another one\n");
313128daee2SAntonio Nino Diaz 			return -EPERM;
314128daee2SAntonio Nino Diaz 		}
315128daee2SAntonio Nino Diaz 
316b38a9e5cSSandrine Bailleux 		/*
317b38a9e5cSSandrine Bailleux 		 * copied_size must be explicitly initialized here because the
318b38a9e5cSSandrine Bailleux 		 * FWU code doesn't necessarily do it when it resets the state
319b38a9e5cSSandrine Bailleux 		 * machine.
320b38a9e5cSSandrine Bailleux 		 */
3213443a702SJohn Powell 		desc->copied_size = 0;
322b38a9e5cSSandrine Bailleux 	}
323b38a9e5cSSandrine Bailleux 
324b38a9e5cSSandrine Bailleux 	/*
325b38a9e5cSSandrine Bailleux 	 * If the given block size is more than the total image size
326b38a9e5cSSandrine Bailleux 	 * then clip the former to the latter.
327b38a9e5cSSandrine Bailleux 	 */
3283443a702SJohn Powell 	remaining = image_size - desc->copied_size;
329b38a9e5cSSandrine Bailleux 	if (block_size > remaining) {
330b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Block size is too big, clipping it.\n");
331b38a9e5cSSandrine Bailleux 		block_size = remaining;
332b38a9e5cSSandrine Bailleux 	}
333b38a9e5cSSandrine Bailleux 
334b38a9e5cSSandrine Bailleux 	/* Make sure the source image is mapped in memory. */
3353443a702SJohn Powell 	if (bl1_plat_mem_check(image_src, block_size, flags) != 0) {
336b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Source image is not mapped.\n");
337b38a9e5cSSandrine Bailleux 		return -ENOMEM;
338b38a9e5cSSandrine Bailleux 	}
339b38a9e5cSSandrine Bailleux 
3403443a702SJohn Powell 	if (bl1_fwu_add_loaded_id(image_id) != 0) {
341128daee2SAntonio Nino Diaz 		WARN("BL1-FWU: Too many images loaded at the same time.\n");
342128daee2SAntonio Nino Diaz 		return -ENOMEM;
343128daee2SAntonio Nino Diaz 	}
344128daee2SAntonio Nino Diaz 
345566034fcSSoby Mathew 	/* Allow the platform to handle pre-image load before copying */
3463443a702SJohn Powell 	if (desc->state == IMAGE_STATE_RESET) {
347566034fcSSoby Mathew 		if (bl1_plat_handle_pre_image_load(image_id) != 0) {
348566034fcSSoby Mathew 			ERROR("BL1-FWU: Failure in pre-image load of image id %d\n",
349566034fcSSoby Mathew 					image_id);
350566034fcSSoby Mathew 			return -EPERM;
351566034fcSSoby Mathew 		}
352566034fcSSoby Mathew 	}
353566034fcSSoby Mathew 
354b38a9e5cSSandrine Bailleux 	/* Everything looks sane. Go ahead and copy the block of data. */
3553443a702SJohn Powell 	dest_addr = desc->image_info.image_base + desc->copied_size;
3563443a702SJohn Powell 	(void)memcpy((void *) dest_addr, (const void *) image_src, block_size);
3579f1489e4SSandrine Bailleux 	flush_dcache_range(dest_addr, block_size);
35848bfb88eSYatharth Kochar 
3593443a702SJohn Powell 	desc->copied_size += block_size;
3603443a702SJohn Powell 	desc->state = (block_size == remaining) ?
361b38a9e5cSSandrine Bailleux 		IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
36248bfb88eSYatharth Kochar 
363b38a9e5cSSandrine Bailleux 	INFO("BL1-FWU: Copy operation successful.\n");
36448bfb88eSYatharth Kochar 	return 0;
36548bfb88eSYatharth Kochar }
36648bfb88eSYatharth Kochar 
36748bfb88eSYatharth Kochar /*******************************************************************************
36848bfb88eSYatharth Kochar  * This function is responsible for authenticating Normal/Secure images.
36948bfb88eSYatharth Kochar  ******************************************************************************/
bl1_fwu_image_auth(unsigned int image_id,uintptr_t image_src,unsigned int image_size,unsigned int flags)37048bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id,
37148bfb88eSYatharth Kochar 			uintptr_t image_src,
37248bfb88eSYatharth Kochar 			unsigned int image_size,
37348bfb88eSYatharth Kochar 			unsigned int flags)
37448bfb88eSYatharth Kochar {
37548bfb88eSYatharth Kochar 	int result;
37648bfb88eSYatharth Kochar 	uintptr_t base_addr;
37748bfb88eSYatharth Kochar 	unsigned int total_size;
3783443a702SJohn Powell 	image_desc_t *desc;
37948bfb88eSYatharth Kochar 
38048bfb88eSYatharth Kochar 	/* Get the image descriptor. */
3813443a702SJohn Powell 	desc = bl1_plat_get_image_desc(image_id);
3823443a702SJohn Powell 	if (desc ==  NULL)
38348bfb88eSYatharth Kochar 		return -EPERM;
38448bfb88eSYatharth Kochar 
385843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == SECURE) {
3863443a702SJohn Powell 		if (desc->state != IMAGE_STATE_RESET) {
38748bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication from secure world "
38848bfb88eSYatharth Kochar 				"while in invalid state\n");
38948bfb88eSYatharth Kochar 			return -EPERM;
39048bfb88eSYatharth Kochar 		}
39148bfb88eSYatharth Kochar 	} else {
3923443a702SJohn Powell 		if (GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE) {
3933443a702SJohn Powell 			if (desc->state != IMAGE_STATE_COPIED) {
39448bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of secure image "
39548bfb88eSYatharth Kochar 					"from non-secure world while not in copied state\n");
39648bfb88eSYatharth Kochar 				return -EPERM;
39748bfb88eSYatharth Kochar 			}
39848bfb88eSYatharth Kochar 		} else {
3993443a702SJohn Powell 			if (desc->state != IMAGE_STATE_RESET) {
40048bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of non-secure image "
40148bfb88eSYatharth Kochar 					"from non-secure world while in invalid state\n");
40248bfb88eSYatharth Kochar 				return -EPERM;
40348bfb88eSYatharth Kochar 			}
40448bfb88eSYatharth Kochar 		}
40548bfb88eSYatharth Kochar 	}
40648bfb88eSYatharth Kochar 
4073443a702SJohn Powell 	if (desc->state == IMAGE_STATE_COPIED) {
40848bfb88eSYatharth Kochar 		/*
40948bfb88eSYatharth Kochar 		 * Image is in COPIED state.
41048bfb88eSYatharth Kochar 		 * Use the stored address and size.
41148bfb88eSYatharth Kochar 		 */
4123443a702SJohn Powell 		base_addr = desc->image_info.image_base;
4133443a702SJohn Powell 		total_size = desc->image_info.image_size;
41448bfb88eSYatharth Kochar 	} else {
4153443a702SJohn Powell 		if ((image_src == 0U) || (image_size == 0U) ||
416949a52d2SSandrine Bailleux 		    check_uptr_overflow(image_src, image_size - 1)) {
41748bfb88eSYatharth Kochar 			WARN("BL1-FWU: Auth not allowed due to invalid"
41848bfb88eSYatharth Kochar 				" image source/size\n");
41948bfb88eSYatharth Kochar 			return -ENOMEM;
42048bfb88eSYatharth Kochar 		}
42148bfb88eSYatharth Kochar 
42248bfb88eSYatharth Kochar 		/*
42348bfb88eSYatharth Kochar 		 * Image is in RESET state.
42448bfb88eSYatharth Kochar 		 * Check the parameters and authenticate the source image in place.
42548bfb88eSYatharth Kochar 		 */
4269a90d720SElyes Haouas 		if (bl1_plat_mem_check(image_src, image_size,
4273443a702SJohn Powell 					desc->ep_info.h.attr) != 0) {
42848bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
42948bfb88eSYatharth Kochar 			return -ENOMEM;
43048bfb88eSYatharth Kochar 		}
43148bfb88eSYatharth Kochar 
4323443a702SJohn Powell 		if (bl1_fwu_add_loaded_id(image_id) != 0) {
433128daee2SAntonio Nino Diaz 			WARN("BL1-FWU: Too many images loaded at the same time.\n");
434128daee2SAntonio Nino Diaz 			return -ENOMEM;
435128daee2SAntonio Nino Diaz 		}
436128daee2SAntonio Nino Diaz 
43748bfb88eSYatharth Kochar 		base_addr = image_src;
43848bfb88eSYatharth Kochar 		total_size = image_size;
43948bfb88eSYatharth Kochar 
44048bfb88eSYatharth Kochar 		/* Update the image size in the descriptor. */
4413443a702SJohn Powell 		desc->image_info.image_size = total_size;
44248bfb88eSYatharth Kochar 	}
44348bfb88eSYatharth Kochar 
44448bfb88eSYatharth Kochar 	/*
44548bfb88eSYatharth Kochar 	 * Authenticate the image.
44648bfb88eSYatharth Kochar 	 */
44748bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
44848bfb88eSYatharth Kochar 	result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
44948bfb88eSYatharth Kochar 	if (result != 0) {
45048bfb88eSYatharth Kochar 		WARN("BL1-FWU: Authentication Failed err=%d\n", result);
45148bfb88eSYatharth Kochar 
45248bfb88eSYatharth Kochar 		/*
45348bfb88eSYatharth Kochar 		 * Authentication has failed.
45448bfb88eSYatharth Kochar 		 * Clear the memory if the image was copied.
45548bfb88eSYatharth Kochar 		 * This is to prevent an attack where this contains
45648bfb88eSYatharth Kochar 		 * some malicious code that can somehow be executed later.
45748bfb88eSYatharth Kochar 		 */
4583443a702SJohn Powell 		if (desc->state == IMAGE_STATE_COPIED) {
45948bfb88eSYatharth Kochar 			/* Clear the memory.*/
460308d359bSDouglas Raillard 			zero_normalmem((void *)base_addr, total_size);
46148bfb88eSYatharth Kochar 			flush_dcache_range(base_addr, total_size);
46248bfb88eSYatharth Kochar 
46348bfb88eSYatharth Kochar 			/* Indicate that image can be copied again*/
4643443a702SJohn Powell 			desc->state = IMAGE_STATE_RESET;
46548bfb88eSYatharth Kochar 		}
466128daee2SAntonio Nino Diaz 
467128daee2SAntonio Nino Diaz 		/*
468128daee2SAntonio Nino Diaz 		 * Even if this fails it's ok because the ID isn't in the array.
469128daee2SAntonio Nino Diaz 		 * The image cannot be in RESET state here, it is checked at the
470128daee2SAntonio Nino Diaz 		 * beginning of the function.
471128daee2SAntonio Nino Diaz 		 */
4723443a702SJohn Powell 		(void)bl1_fwu_remove_loaded_id(image_id);
47348bfb88eSYatharth Kochar 		return -EAUTH;
47448bfb88eSYatharth Kochar 	}
47548bfb88eSYatharth Kochar 
47648bfb88eSYatharth Kochar 	/* Indicate that image is in authenticated state. */
4773443a702SJohn Powell 	desc->state = IMAGE_STATE_AUTHENTICATED;
47848bfb88eSYatharth Kochar 
479566034fcSSoby Mathew 	/* Allow the platform to handle post-image load */
480566034fcSSoby Mathew 	result = bl1_plat_handle_post_image_load(image_id);
481566034fcSSoby Mathew 	if (result != 0) {
482566034fcSSoby Mathew 		ERROR("BL1-FWU: Failure %d in post-image load of image id %d\n",
483566034fcSSoby Mathew 				result, image_id);
484566034fcSSoby Mathew 		/*
485566034fcSSoby Mathew 		 * Panic here as the platform handling of post-image load is
486566034fcSSoby Mathew 		 * not correct.
487566034fcSSoby Mathew 		 */
488566034fcSSoby Mathew 		plat_error_handler(result);
489566034fcSSoby Mathew 	}
490566034fcSSoby Mathew 
49148bfb88eSYatharth Kochar 	/*
49248bfb88eSYatharth Kochar 	 * Flush image_info to memory so that other
49348bfb88eSYatharth Kochar 	 * secure world images can see changes.
49448bfb88eSYatharth Kochar 	 */
4953443a702SJohn Powell 	flush_dcache_range((uintptr_t)&desc->image_info,
49648bfb88eSYatharth Kochar 		sizeof(image_info_t));
49748bfb88eSYatharth Kochar 
49848bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authentication was successful\n");
49948bfb88eSYatharth Kochar 
50048bfb88eSYatharth Kochar 	return 0;
50148bfb88eSYatharth Kochar }
50248bfb88eSYatharth Kochar 
50348bfb88eSYatharth Kochar /*******************************************************************************
50448bfb88eSYatharth Kochar  * This function is responsible for executing Secure images.
50548bfb88eSYatharth Kochar  ******************************************************************************/
bl1_fwu_image_execute(unsigned int image_id,void ** handle,unsigned int flags)50648bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id,
50748bfb88eSYatharth Kochar 			void **handle,
50848bfb88eSYatharth Kochar 			unsigned int flags)
50948bfb88eSYatharth Kochar {
51048bfb88eSYatharth Kochar 	/* Get the image descriptor. */
5113443a702SJohn Powell 	image_desc_t *desc = bl1_plat_get_image_desc(image_id);
51248bfb88eSYatharth Kochar 
51348bfb88eSYatharth Kochar 	/*
51448bfb88eSYatharth Kochar 	 * Execution is NOT allowed if:
51528955d57SDan Handley 	 * image_id is invalid OR
51648bfb88eSYatharth Kochar 	 * Caller is from Secure world OR
51748bfb88eSYatharth Kochar 	 * Image is Non-Secure OR
51848bfb88eSYatharth Kochar 	 * Image is Non-Executable OR
51948bfb88eSYatharth Kochar 	 * Image is NOT in AUTHENTICATED state.
52048bfb88eSYatharth Kochar 	 */
5213443a702SJohn Powell 	if ((desc == NULL) ||
522843ddee4SYatharth Kochar 	    (GET_SECURITY_STATE(flags) == SECURE) ||
5233443a702SJohn Powell 	    (GET_SECURITY_STATE(desc->ep_info.h.attr) == NON_SECURE) ||
5243443a702SJohn Powell 	    (EP_GET_EXE(desc->ep_info.h.attr) == NON_EXECUTABLE) ||
5253443a702SJohn Powell 	    (desc->state != IMAGE_STATE_AUTHENTICATED)) {
52648bfb88eSYatharth Kochar 		WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
52748bfb88eSYatharth Kochar 		return -EPERM;
52848bfb88eSYatharth Kochar 	}
52948bfb88eSYatharth Kochar 
53048bfb88eSYatharth Kochar 	INFO("BL1-FWU: Executing Secure image\n");
53148bfb88eSYatharth Kochar 
532402b3cf8SJulius Werner #ifdef __aarch64__
53348bfb88eSYatharth Kochar 	/* Save NS-EL1 system registers. */
53448bfb88eSYatharth Kochar 	cm_el1_sysregs_context_save(NON_SECURE);
535a4409008Sdp-arm #endif
53648bfb88eSYatharth Kochar 
53748bfb88eSYatharth Kochar 	/* Prepare the image for execution. */
53848bfb88eSYatharth Kochar 	bl1_prepare_next_image(image_id);
53948bfb88eSYatharth Kochar 
54048bfb88eSYatharth Kochar 	/* Update the secure image id. */
54148bfb88eSYatharth Kochar 	sec_exec_image_id = image_id;
54248bfb88eSYatharth Kochar 
543402b3cf8SJulius Werner #ifdef __aarch64__
54448bfb88eSYatharth Kochar 	*handle = cm_get_context(SECURE);
545a4409008Sdp-arm #else
546a4409008Sdp-arm 	*handle = smc_get_ctx(SECURE);
547a4409008Sdp-arm #endif
54848bfb88eSYatharth Kochar 	return 0;
54948bfb88eSYatharth Kochar }
55048bfb88eSYatharth Kochar 
55148bfb88eSYatharth Kochar /*******************************************************************************
55228955d57SDan Handley  * This function is responsible for resuming execution in the other security
55328955d57SDan Handley  * world
55448bfb88eSYatharth Kochar  ******************************************************************************/
bl1_fwu_image_resume(register_t image_param,void ** handle,unsigned int flags)55528955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param,
55648bfb88eSYatharth Kochar 			void **handle,
55748bfb88eSYatharth Kochar 			unsigned int flags)
55848bfb88eSYatharth Kochar {
5593443a702SJohn Powell 	image_desc_t *desc;
56048bfb88eSYatharth Kochar 	unsigned int resume_sec_state;
561843ddee4SYatharth Kochar 	unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
56248bfb88eSYatharth Kochar 
56348bfb88eSYatharth Kochar 	/* Get the image descriptor for last executed secure image id. */
5643443a702SJohn Powell 	desc = bl1_plat_get_image_desc(sec_exec_image_id);
56528955d57SDan Handley 	if (caller_sec_state == NON_SECURE) {
5663443a702SJohn Powell 		if (desc == NULL) {
56728955d57SDan Handley 			WARN("BL1-FWU: Resume not allowed due to no available"
56828955d57SDan Handley 				"secure image\n");
56948bfb88eSYatharth Kochar 			return -EPERM;
57048bfb88eSYatharth Kochar 		}
57128955d57SDan Handley 	} else {
5723443a702SJohn Powell 		/* desc must be valid for secure world callers */
5733443a702SJohn Powell 		assert(desc != NULL);
57428955d57SDan Handley 	}
57528955d57SDan Handley 
5763443a702SJohn Powell 	assert(GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE);
5773443a702SJohn Powell 	assert(EP_GET_EXE(desc->ep_info.h.attr) == EXECUTABLE);
57828955d57SDan Handley 
57928955d57SDan Handley 	if (caller_sec_state == SECURE) {
5803443a702SJohn Powell 		assert(desc->state == IMAGE_STATE_EXECUTED);
58148bfb88eSYatharth Kochar 
58248bfb88eSYatharth Kochar 		/* Update the flags. */
5833443a702SJohn Powell 		desc->state = IMAGE_STATE_INTERRUPTED;
58448bfb88eSYatharth Kochar 		resume_sec_state = NON_SECURE;
58548bfb88eSYatharth Kochar 	} else {
5863443a702SJohn Powell 		assert(desc->state == IMAGE_STATE_INTERRUPTED);
58748bfb88eSYatharth Kochar 
58848bfb88eSYatharth Kochar 		/* Update the flags. */
5893443a702SJohn Powell 		desc->state = IMAGE_STATE_EXECUTED;
59048bfb88eSYatharth Kochar 		resume_sec_state = SECURE;
59148bfb88eSYatharth Kochar 	}
59248bfb88eSYatharth Kochar 
593a4409008Sdp-arm 	INFO("BL1-FWU: Resuming %s world context\n",
594a4409008Sdp-arm 		(resume_sec_state == SECURE) ? "secure" : "normal");
595a4409008Sdp-arm 
596402b3cf8SJulius Werner #ifdef __aarch64__
59748bfb88eSYatharth Kochar 	/* Save the EL1 system registers of calling world. */
59828955d57SDan Handley 	cm_el1_sysregs_context_save(caller_sec_state);
59948bfb88eSYatharth Kochar 
60048bfb88eSYatharth Kochar 	/* Restore the EL1 system registers of resuming world. */
60148bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(resume_sec_state);
60248bfb88eSYatharth Kochar 
60348bfb88eSYatharth Kochar 	/* Update the next context. */
60448bfb88eSYatharth Kochar 	cm_set_next_eret_context(resume_sec_state);
60548bfb88eSYatharth Kochar 
60648bfb88eSYatharth Kochar 	*handle = cm_get_context(resume_sec_state);
607a4409008Sdp-arm #else
608a4409008Sdp-arm 	/* Update the next context. */
609a4409008Sdp-arm 	cm_set_next_context(cm_get_context(resume_sec_state));
610a4409008Sdp-arm 
611a4409008Sdp-arm 	/* Prepare the smc context for the next BL image. */
612a4409008Sdp-arm 	smc_set_next_ctx(resume_sec_state);
613a4409008Sdp-arm 
614a4409008Sdp-arm 	*handle = smc_get_ctx(resume_sec_state);
615a4409008Sdp-arm #endif
61648bfb88eSYatharth Kochar 	return image_param;
61748bfb88eSYatharth Kochar }
61848bfb88eSYatharth Kochar 
61948bfb88eSYatharth Kochar /*******************************************************************************
62048bfb88eSYatharth Kochar  * This function is responsible for resuming normal world context.
62148bfb88eSYatharth Kochar  ******************************************************************************/
bl1_fwu_sec_image_done(void ** handle,unsigned int flags)62248bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
62348bfb88eSYatharth Kochar {
6243443a702SJohn Powell 	image_desc_t *desc;
62548bfb88eSYatharth Kochar 
62628955d57SDan Handley 	/* Make sure caller is from the secure world */
627843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == NON_SECURE) {
62828955d57SDan Handley 		WARN("BL1-FWU: Image done not allowed from normal world\n");
62948bfb88eSYatharth Kochar 		return -EPERM;
63048bfb88eSYatharth Kochar 	}
63148bfb88eSYatharth Kochar 
63228955d57SDan Handley 	/* Get the image descriptor for last executed secure image id */
6333443a702SJohn Powell 	desc = bl1_plat_get_image_desc(sec_exec_image_id);
63428955d57SDan Handley 
6353443a702SJohn Powell 	/* desc must correspond to a valid secure executing image */
6363443a702SJohn Powell 	assert(desc != NULL);
6373443a702SJohn Powell 	assert(GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE);
6383443a702SJohn Powell 	assert(EP_GET_EXE(desc->ep_info.h.attr) == EXECUTABLE);
6393443a702SJohn Powell 	assert(desc->state == IMAGE_STATE_EXECUTED);
64028955d57SDan Handley 
641128daee2SAntonio Nino Diaz #if ENABLE_ASSERTIONS
642128daee2SAntonio Nino Diaz 	int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id);
643128daee2SAntonio Nino Diaz 	assert(rc == 0);
644128daee2SAntonio Nino Diaz #else
645128daee2SAntonio Nino Diaz 	bl1_fwu_remove_loaded_id(sec_exec_image_id);
646128daee2SAntonio Nino Diaz #endif
647128daee2SAntonio Nino Diaz 
64848bfb88eSYatharth Kochar 	/* Update the flags. */
6493443a702SJohn Powell 	desc->state = IMAGE_STATE_RESET;
65048bfb88eSYatharth Kochar 	sec_exec_image_id = INVALID_IMAGE_ID;
65148bfb88eSYatharth Kochar 
652a4409008Sdp-arm 	INFO("BL1-FWU: Resuming Normal world context\n");
653402b3cf8SJulius Werner #ifdef __aarch64__
65448bfb88eSYatharth Kochar 	/*
65548bfb88eSYatharth Kochar 	 * Secure world is done so no need to save the context.
65648bfb88eSYatharth Kochar 	 * Just restore the Non-Secure context.
65748bfb88eSYatharth Kochar 	 */
65848bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(NON_SECURE);
65948bfb88eSYatharth Kochar 
66048bfb88eSYatharth Kochar 	/* Update the next context. */
66148bfb88eSYatharth Kochar 	cm_set_next_eret_context(NON_SECURE);
66248bfb88eSYatharth Kochar 
66348bfb88eSYatharth Kochar 	*handle = cm_get_context(NON_SECURE);
664a4409008Sdp-arm #else
665a4409008Sdp-arm 	/* Update the next context. */
666a4409008Sdp-arm 	cm_set_next_context(cm_get_context(NON_SECURE));
667a4409008Sdp-arm 
668a4409008Sdp-arm 	/* Prepare the smc context for the next BL image. */
669a4409008Sdp-arm 	smc_set_next_ctx(NON_SECURE);
670a4409008Sdp-arm 
671a4409008Sdp-arm 	*handle = smc_get_ctx(NON_SECURE);
672a4409008Sdp-arm #endif
67348bfb88eSYatharth Kochar 	return 0;
67448bfb88eSYatharth Kochar }
67548bfb88eSYatharth Kochar 
67648bfb88eSYatharth Kochar /*******************************************************************************
67748bfb88eSYatharth Kochar  * This function provides the opportunity for users to perform any
67848bfb88eSYatharth Kochar  * platform specific handling after the Firmware update is done.
67948bfb88eSYatharth Kochar  ******************************************************************************/
bl1_fwu_done(void * client_cookie,void * reserved)6801f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
68148bfb88eSYatharth Kochar {
68248bfb88eSYatharth Kochar 	NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
68348bfb88eSYatharth Kochar 
68448bfb88eSYatharth Kochar 	/*
68548bfb88eSYatharth Kochar 	 * Call platform done function.
68648bfb88eSYatharth Kochar 	 */
6871f37b944SDan Handley 	bl1_plat_fwu_done(client_cookie, reserved);
6883443a702SJohn Powell 	assert(false);
68948bfb88eSYatharth Kochar }
6909d6fc3c3SAntonio Nino Diaz 
6919d6fc3c3SAntonio Nino Diaz /*******************************************************************************
6929d6fc3c3SAntonio Nino Diaz  * This function resets an image to IMAGE_STATE_RESET. It fails if the image is
6939d6fc3c3SAntonio Nino Diaz  * being executed.
6949d6fc3c3SAntonio Nino Diaz  ******************************************************************************/
bl1_fwu_image_reset(unsigned int image_id,unsigned int flags)6959d6fc3c3SAntonio Nino Diaz static int bl1_fwu_image_reset(unsigned int image_id, unsigned int flags)
6969d6fc3c3SAntonio Nino Diaz {
6973443a702SJohn Powell 	image_desc_t *desc = bl1_plat_get_image_desc(image_id);
6989d6fc3c3SAntonio Nino Diaz 
6993443a702SJohn Powell 	if ((desc == NULL) || (GET_SECURITY_STATE(flags) == SECURE)) {
7009d6fc3c3SAntonio Nino Diaz 		WARN("BL1-FWU: Reset not allowed due to invalid args\n");
7019d6fc3c3SAntonio Nino Diaz 		return -EPERM;
7029d6fc3c3SAntonio Nino Diaz 	}
7039d6fc3c3SAntonio Nino Diaz 
7043443a702SJohn Powell 	switch (desc->state) {
7059d6fc3c3SAntonio Nino Diaz 
7069d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_RESET:
7079d6fc3c3SAntonio Nino Diaz 		/* Nothing to do. */
7089d6fc3c3SAntonio Nino Diaz 		break;
7099d6fc3c3SAntonio Nino Diaz 
7109d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_INTERRUPTED:
7119d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_AUTHENTICATED:
7129d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_COPIED:
7139d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_COPYING:
7149d6fc3c3SAntonio Nino Diaz 
7153443a702SJohn Powell 		if (bl1_fwu_remove_loaded_id(image_id) != 0) {
7169d6fc3c3SAntonio Nino Diaz 			WARN("BL1-FWU: Image reset couldn't find the image ID\n");
7179d6fc3c3SAntonio Nino Diaz 			return -EPERM;
7189d6fc3c3SAntonio Nino Diaz 		}
7199d6fc3c3SAntonio Nino Diaz 
7203443a702SJohn Powell 		if (desc->copied_size != 0U) {
721ee05ae16SSoby Mathew 			/* Clear the memory if the image is copied */
7223443a702SJohn Powell 			assert(GET_SECURITY_STATE(desc->ep_info.h.attr)
7233443a702SJohn Powell 				== SECURE);
724ee05ae16SSoby Mathew 
7253443a702SJohn Powell 			zero_normalmem((void *)desc->image_info.image_base,
7263443a702SJohn Powell 					desc->copied_size);
7273443a702SJohn Powell 			flush_dcache_range(desc->image_info.image_base,
7283443a702SJohn Powell 					desc->copied_size);
729ee05ae16SSoby Mathew 		}
7309d6fc3c3SAntonio Nino Diaz 
7319d6fc3c3SAntonio Nino Diaz 		/* Reset status variables */
7323443a702SJohn Powell 		desc->copied_size = 0;
7333443a702SJohn Powell 		desc->image_info.image_size = 0;
7343443a702SJohn Powell 		desc->state = IMAGE_STATE_RESET;
7359d6fc3c3SAntonio Nino Diaz 
7369d6fc3c3SAntonio Nino Diaz 		/* Clear authentication state */
7379d6fc3c3SAntonio Nino Diaz 		auth_img_flags[image_id] = 0;
7389d6fc3c3SAntonio Nino Diaz 
7399d6fc3c3SAntonio Nino Diaz 		break;
7409d6fc3c3SAntonio Nino Diaz 
7419d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_EXECUTED:
7429d6fc3c3SAntonio Nino Diaz 	default:
7433443a702SJohn Powell 		assert(false); /* Unreachable */
744c9662db9SJonathan Wright 		break;
7459d6fc3c3SAntonio Nino Diaz 	}
7469d6fc3c3SAntonio Nino Diaz 
7479d6fc3c3SAntonio Nino Diaz 	return 0;
7489d6fc3c3SAntonio Nino Diaz }
749