xref: /rk3399_ARM-atf/bl1/bl1_fwu.c (revision 9d6fc3c32162cf4c6146a8f918c0790848686086)
148bfb88eSYatharth Kochar /*
2308d359bSDouglas Raillard  * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
348bfb88eSYatharth Kochar  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
548bfb88eSYatharth Kochar  */
648bfb88eSYatharth Kochar 
748bfb88eSYatharth Kochar #include <assert.h>
848bfb88eSYatharth Kochar #include <arch_helpers.h>
948bfb88eSYatharth Kochar #include <auth_mod.h>
1048bfb88eSYatharth Kochar #include <bl1.h>
1148bfb88eSYatharth Kochar #include <bl_common.h>
1248bfb88eSYatharth Kochar #include <context.h>
1348bfb88eSYatharth Kochar #include <context_mgmt.h>
1448bfb88eSYatharth Kochar #include <debug.h>
1548bfb88eSYatharth Kochar #include <errno.h>
1648bfb88eSYatharth Kochar #include <platform.h>
1748bfb88eSYatharth Kochar #include <platform_def.h>
1848bfb88eSYatharth Kochar #include <smcc_helpers.h>
1948bfb88eSYatharth Kochar #include <string.h>
20949a52d2SSandrine Bailleux #include <utils.h>
2148bfb88eSYatharth Kochar #include "bl1_private.h"
2248bfb88eSYatharth Kochar 
2348bfb88eSYatharth Kochar /*
2448bfb88eSYatharth Kochar  * Function declarations.
2548bfb88eSYatharth Kochar  */
2648bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id,
2748bfb88eSYatharth Kochar 			uintptr_t image_addr,
2848bfb88eSYatharth Kochar 			unsigned int block_size,
2948bfb88eSYatharth Kochar 			unsigned int image_size,
3048bfb88eSYatharth Kochar 			unsigned int flags);
3148bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id,
3248bfb88eSYatharth Kochar 			uintptr_t image_addr,
3348bfb88eSYatharth Kochar 			unsigned int image_size,
3448bfb88eSYatharth Kochar 			unsigned int flags);
3548bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id,
3648bfb88eSYatharth Kochar 			void **handle,
3748bfb88eSYatharth Kochar 			unsigned int flags);
3828955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param,
3948bfb88eSYatharth Kochar 			void **handle,
4048bfb88eSYatharth Kochar 			unsigned int flags);
4148bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle,
4248bfb88eSYatharth Kochar 			unsigned int flags);
43*9d6fc3c3SAntonio Nino Diaz static int bl1_fwu_image_reset(unsigned int image_id,
44*9d6fc3c3SAntonio Nino Diaz 			unsigned int flags);
451f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved);
4648bfb88eSYatharth Kochar 
4748bfb88eSYatharth Kochar /*
4848bfb88eSYatharth Kochar  * This keeps track of last executed secure image id.
4948bfb88eSYatharth Kochar  */
5048bfb88eSYatharth Kochar static unsigned int sec_exec_image_id = INVALID_IMAGE_ID;
5148bfb88eSYatharth Kochar 
52*9d6fc3c3SAntonio Nino Diaz /* Authentication status of each image. */
53*9d6fc3c3SAntonio Nino Diaz extern unsigned int auth_img_flags[];
54*9d6fc3c3SAntonio Nino Diaz 
55a4409008Sdp-arm void cm_set_next_context(void *cpu_context);
56a4409008Sdp-arm 
5748bfb88eSYatharth Kochar /*******************************************************************************
5848bfb88eSYatharth Kochar  * Top level handler for servicing FWU SMCs.
5948bfb88eSYatharth Kochar  ******************************************************************************/
6048bfb88eSYatharth Kochar register_t bl1_fwu_smc_handler(unsigned int smc_fid,
6148bfb88eSYatharth Kochar 			register_t x1,
6248bfb88eSYatharth Kochar 			register_t x2,
6348bfb88eSYatharth Kochar 			register_t x3,
6448bfb88eSYatharth Kochar 			register_t x4,
6548bfb88eSYatharth Kochar 			void *cookie,
6648bfb88eSYatharth Kochar 			void *handle,
6748bfb88eSYatharth Kochar 			unsigned int flags)
6848bfb88eSYatharth Kochar {
6948bfb88eSYatharth Kochar 
7048bfb88eSYatharth Kochar 	switch (smc_fid) {
7148bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_COPY:
7248bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_copy(x1, x2, x3, x4, flags));
7348bfb88eSYatharth Kochar 
7448bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_AUTH:
7548bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_auth(x1, x2, x3, flags));
7648bfb88eSYatharth Kochar 
7748bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_EXECUTE:
7848bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_execute(x1, &handle, flags));
7948bfb88eSYatharth Kochar 
8048bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_RESUME:
8128955d57SDan Handley 		SMC_RET1(handle, bl1_fwu_image_resume(x1, &handle, flags));
8248bfb88eSYatharth Kochar 
8348bfb88eSYatharth Kochar 	case FWU_SMC_SEC_IMAGE_DONE:
8448bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
8548bfb88eSYatharth Kochar 
86*9d6fc3c3SAntonio Nino Diaz 	case FWU_SMC_IMAGE_RESET:
87*9d6fc3c3SAntonio Nino Diaz 		SMC_RET1(handle, bl1_fwu_image_reset(x1, flags));
88*9d6fc3c3SAntonio Nino Diaz 
8948bfb88eSYatharth Kochar 	case FWU_SMC_UPDATE_DONE:
901f37b944SDan Handley 		bl1_fwu_done((void *)x1, NULL);
9148bfb88eSYatharth Kochar 		/* We should never return from bl1_fwu_done() */
9248bfb88eSYatharth Kochar 
9348bfb88eSYatharth Kochar 	default:
9448bfb88eSYatharth Kochar 		assert(0);
9548bfb88eSYatharth Kochar 		break;
9648bfb88eSYatharth Kochar 	}
9748bfb88eSYatharth Kochar 
987a317a70SAntonio Nino Diaz 	SMC_RET1(handle, SMC_UNK);
9948bfb88eSYatharth Kochar }
10048bfb88eSYatharth Kochar 
10148bfb88eSYatharth Kochar /*******************************************************************************
102128daee2SAntonio Nino Diaz  * Utility functions to keep track of the images that are loaded at any time.
103128daee2SAntonio Nino Diaz  ******************************************************************************/
104128daee2SAntonio Nino Diaz 
105128daee2SAntonio Nino Diaz #ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
106128daee2SAntonio Nino Diaz #define FWU_MAX_SIMULTANEOUS_IMAGES	PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
107128daee2SAntonio Nino Diaz #else
108128daee2SAntonio Nino Diaz #define FWU_MAX_SIMULTANEOUS_IMAGES	10
109128daee2SAntonio Nino Diaz #endif
110128daee2SAntonio Nino Diaz 
111128daee2SAntonio Nino Diaz static int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = {
112128daee2SAntonio Nino Diaz 	[0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID
113128daee2SAntonio Nino Diaz };
114128daee2SAntonio Nino Diaz 
115128daee2SAntonio Nino Diaz /*
116128daee2SAntonio Nino Diaz  * Adds an image_id to the bl1_fwu_loaded_ids array.
117128daee2SAntonio Nino Diaz  * Returns 0 on success, 1 on error.
118128daee2SAntonio Nino Diaz  */
119128daee2SAntonio Nino Diaz static int bl1_fwu_add_loaded_id(int image_id)
120128daee2SAntonio Nino Diaz {
121128daee2SAntonio Nino Diaz 	int i;
122128daee2SAntonio Nino Diaz 
123128daee2SAntonio Nino Diaz 	/* Check if the ID is already in the list */
124128daee2SAntonio Nino Diaz 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
125128daee2SAntonio Nino Diaz 		if (bl1_fwu_loaded_ids[i] == image_id)
126128daee2SAntonio Nino Diaz 			return 0;
127128daee2SAntonio Nino Diaz 	}
128128daee2SAntonio Nino Diaz 
129128daee2SAntonio Nino Diaz 	/* Find an empty slot */
130128daee2SAntonio Nino Diaz 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
131128daee2SAntonio Nino Diaz 		if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) {
132128daee2SAntonio Nino Diaz 			bl1_fwu_loaded_ids[i] = image_id;
133128daee2SAntonio Nino Diaz 			return 0;
134128daee2SAntonio Nino Diaz 		}
135128daee2SAntonio Nino Diaz 	}
136128daee2SAntonio Nino Diaz 
137128daee2SAntonio Nino Diaz 	return 1;
138128daee2SAntonio Nino Diaz }
139128daee2SAntonio Nino Diaz 
140128daee2SAntonio Nino Diaz /*
141128daee2SAntonio Nino Diaz  * Removes an image_id from the bl1_fwu_loaded_ids array.
142128daee2SAntonio Nino Diaz  * Returns 0 on success, 1 on error.
143128daee2SAntonio Nino Diaz  */
144128daee2SAntonio Nino Diaz static int bl1_fwu_remove_loaded_id(int image_id)
145128daee2SAntonio Nino Diaz {
146128daee2SAntonio Nino Diaz 	int i;
147128daee2SAntonio Nino Diaz 
148128daee2SAntonio Nino Diaz 	/* Find the ID */
149128daee2SAntonio Nino Diaz 	for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
150128daee2SAntonio Nino Diaz 		if (bl1_fwu_loaded_ids[i] == image_id) {
151128daee2SAntonio Nino Diaz 			bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID;
152128daee2SAntonio Nino Diaz 			return 0;
153128daee2SAntonio Nino Diaz 		}
154128daee2SAntonio Nino Diaz 	}
155128daee2SAntonio Nino Diaz 
156128daee2SAntonio Nino Diaz 	return 1;
157128daee2SAntonio Nino Diaz }
158128daee2SAntonio Nino Diaz 
159128daee2SAntonio Nino Diaz /*******************************************************************************
160128daee2SAntonio Nino Diaz  * This function checks if the specified image overlaps another image already
161128daee2SAntonio Nino Diaz  * loaded. It returns 0 if there is no overlap, a negative error code otherwise.
162128daee2SAntonio Nino Diaz  ******************************************************************************/
163128daee2SAntonio Nino Diaz static int bl1_fwu_image_check_overlaps(int image_id)
164128daee2SAntonio Nino Diaz {
165128daee2SAntonio Nino Diaz 	const image_desc_t *image_desc, *checked_image_desc;
166128daee2SAntonio Nino Diaz 	const image_info_t *info, *checked_info;
167128daee2SAntonio Nino Diaz 
168128daee2SAntonio Nino Diaz 	uintptr_t image_base, image_end;
169128daee2SAntonio Nino Diaz 	uintptr_t checked_image_base, checked_image_end;
170128daee2SAntonio Nino Diaz 
171128daee2SAntonio Nino Diaz 	checked_image_desc = bl1_plat_get_image_desc(image_id);
172128daee2SAntonio Nino Diaz 	checked_info = &checked_image_desc->image_info;
173128daee2SAntonio Nino Diaz 
174128daee2SAntonio Nino Diaz 	/* Image being checked mustn't be empty. */
175128daee2SAntonio Nino Diaz 	assert(checked_info->image_size != 0);
176128daee2SAntonio Nino Diaz 
177128daee2SAntonio Nino Diaz 	checked_image_base = checked_info->image_base;
178128daee2SAntonio Nino Diaz 	checked_image_end = checked_image_base + checked_info->image_size - 1;
179128daee2SAntonio Nino Diaz 	/* No need to check for overlaps, it's done in bl1_fwu_image_copy(). */
180128daee2SAntonio Nino Diaz 
181128daee2SAntonio Nino Diaz 	for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
182128daee2SAntonio Nino Diaz 
183128daee2SAntonio Nino Diaz 		/* Don't check image against itself. */
184128daee2SAntonio Nino Diaz 		if (bl1_fwu_loaded_ids[i] == image_id)
185128daee2SAntonio Nino Diaz 			continue;
186128daee2SAntonio Nino Diaz 
187128daee2SAntonio Nino Diaz 		image_desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]);
188128daee2SAntonio Nino Diaz 
189128daee2SAntonio Nino Diaz 		/* Only check images that are loaded or being loaded. */
190128daee2SAntonio Nino Diaz 		assert (image_desc->state != IMAGE_STATE_RESET);
191128daee2SAntonio Nino Diaz 
192128daee2SAntonio Nino Diaz 		info = &image_desc->image_info;
193128daee2SAntonio Nino Diaz 
194128daee2SAntonio Nino Diaz 		/* There cannot be overlaps with an empty image. */
195128daee2SAntonio Nino Diaz 		if (info->image_size == 0)
196128daee2SAntonio Nino Diaz 			continue;
197128daee2SAntonio Nino Diaz 
198128daee2SAntonio Nino Diaz 		image_base = info->image_base;
199128daee2SAntonio Nino Diaz 		image_end = image_base + info->image_size - 1;
200128daee2SAntonio Nino Diaz 		/*
201128daee2SAntonio Nino Diaz 		 * Overflows cannot happen. It is checked in
202128daee2SAntonio Nino Diaz 		 * bl1_fwu_image_copy() when the image goes from RESET to
203128daee2SAntonio Nino Diaz 		 * COPYING or COPIED.
204128daee2SAntonio Nino Diaz 		 */
205128daee2SAntonio Nino Diaz 		assert (image_end > image_base);
206128daee2SAntonio Nino Diaz 
207128daee2SAntonio Nino Diaz 		/* Check if there are overlaps. */
208128daee2SAntonio Nino Diaz 		if (!(image_end < checked_image_base ||
209128daee2SAntonio Nino Diaz 		    checked_image_end < image_base)) {
210128daee2SAntonio Nino Diaz 			VERBOSE("Image with ID %d overlaps existing image with ID %d",
211128daee2SAntonio Nino Diaz 				checked_image_desc->image_id, image_desc->image_id);
212128daee2SAntonio Nino Diaz 			return -EPERM;
213128daee2SAntonio Nino Diaz 		}
214128daee2SAntonio Nino Diaz 	}
215128daee2SAntonio Nino Diaz 
216128daee2SAntonio Nino Diaz 	return 0;
217128daee2SAntonio Nino Diaz }
218128daee2SAntonio Nino Diaz 
219128daee2SAntonio Nino Diaz /*******************************************************************************
22048bfb88eSYatharth Kochar  * This function is responsible for copying secure images in AP Secure RAM.
22148bfb88eSYatharth Kochar  ******************************************************************************/
22248bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id,
22348bfb88eSYatharth Kochar 			uintptr_t image_src,
22448bfb88eSYatharth Kochar 			unsigned int block_size,
22548bfb88eSYatharth Kochar 			unsigned int image_size,
22648bfb88eSYatharth Kochar 			unsigned int flags)
22748bfb88eSYatharth Kochar {
2289f1489e4SSandrine Bailleux 	uintptr_t dest_addr;
229b38a9e5cSSandrine Bailleux 	unsigned int remaining;
23048bfb88eSYatharth Kochar 
23148bfb88eSYatharth Kochar 	/* Get the image descriptor. */
23248bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
2339f1489e4SSandrine Bailleux 	if (!image_desc) {
2349f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Invalid image ID %u\n", image_id);
23548bfb88eSYatharth Kochar 		return -EPERM;
23648bfb88eSYatharth Kochar 	}
23748bfb88eSYatharth Kochar 
2389f1489e4SSandrine Bailleux 	/*
2399f1489e4SSandrine Bailleux 	 * The request must originate from a non-secure caller and target a
2409f1489e4SSandrine Bailleux 	 * secure image. Any other scenario is invalid.
2419f1489e4SSandrine Bailleux 	 */
2429f1489e4SSandrine Bailleux 	if (GET_SECURITY_STATE(flags) == SECURE) {
2439f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed from secure world.\n");
2449f1489e4SSandrine Bailleux 		return -EPERM;
2459f1489e4SSandrine Bailleux 	}
2469f1489e4SSandrine Bailleux 	if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) {
2479f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
2489f1489e4SSandrine Bailleux 		return -EPERM;
2499f1489e4SSandrine Bailleux 	}
2509f1489e4SSandrine Bailleux 
2519f1489e4SSandrine Bailleux 	/* Check whether the FWU state machine is in the correct state. */
2529f1489e4SSandrine Bailleux 	if ((image_desc->state != IMAGE_STATE_RESET) &&
2539f1489e4SSandrine Bailleux 	    (image_desc->state != IMAGE_STATE_COPYING)) {
2549f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed at this point of the FWU"
2559f1489e4SSandrine Bailleux 			" process.\n");
25648bfb88eSYatharth Kochar 		return -EPERM;
25748bfb88eSYatharth Kochar 	}
25848bfb88eSYatharth Kochar 
259949a52d2SSandrine Bailleux 	if ((!image_src) || (!block_size) ||
260949a52d2SSandrine Bailleux 	    check_uptr_overflow(image_src, block_size - 1)) {
26148bfb88eSYatharth Kochar 		WARN("BL1-FWU: Copy not allowed due to invalid image source"
26248bfb88eSYatharth Kochar 			" or block size\n");
26348bfb88eSYatharth Kochar 		return -ENOMEM;
26448bfb88eSYatharth Kochar 	}
26548bfb88eSYatharth Kochar 
26648bfb88eSYatharth Kochar 	if (image_desc->state == IMAGE_STATE_COPYING) {
2671bfb7068SSandrine Bailleux 		/*
2681bfb7068SSandrine Bailleux 		 * There must have been at least 1 copy operation for this image
2691bfb7068SSandrine Bailleux 		 * previously.
2701bfb7068SSandrine Bailleux 		 */
2711bfb7068SSandrine Bailleux 		assert(image_desc->copied_size != 0);
2721bfb7068SSandrine Bailleux 		/*
2731bfb7068SSandrine Bailleux 		 * The image size must have been recorded in the 1st copy
2741bfb7068SSandrine Bailleux 		 * operation.
2751bfb7068SSandrine Bailleux 		 */
2769f1489e4SSandrine Bailleux 		image_size = image_desc->image_info.image_size;
2771bfb7068SSandrine Bailleux 		assert(image_size != 0);
2781bfb7068SSandrine Bailleux 		assert(image_desc->copied_size < image_size);
2791bfb7068SSandrine Bailleux 
28048bfb88eSYatharth Kochar 		INFO("BL1-FWU: Continuing image copy in blocks\n");
2819f1489e4SSandrine Bailleux 	} else { /* image_desc->state == IMAGE_STATE_RESET */
2829f1489e4SSandrine Bailleux 		INFO("BL1-FWU: Initial call to copy an image\n");
28348bfb88eSYatharth Kochar 
2849f1489e4SSandrine Bailleux 		/*
2859f1489e4SSandrine Bailleux 		 * image_size is relevant only for the 1st copy request, it is
2869f1489e4SSandrine Bailleux 		 * then ignored for subsequent calls for this image.
2879f1489e4SSandrine Bailleux 		 */
28848bfb88eSYatharth Kochar 		if (!image_size) {
2899f1489e4SSandrine Bailleux 			WARN("BL1-FWU: Copy not allowed due to invalid image"
2909f1489e4SSandrine Bailleux 				" size\n");
29148bfb88eSYatharth Kochar 			return -ENOMEM;
29248bfb88eSYatharth Kochar 		}
29348bfb88eSYatharth Kochar 
29453d703a5SYatharth Kochar #if LOAD_IMAGE_V2
29553d703a5SYatharth Kochar 		/* Check that the image size to load is within limit */
29653d703a5SYatharth Kochar 		if (image_size > image_desc->image_info.image_max_size) {
29753d703a5SYatharth Kochar 			WARN("BL1-FWU: Image size out of bounds\n");
29853d703a5SYatharth Kochar 			return -ENOMEM;
29953d703a5SYatharth Kochar 		}
30053d703a5SYatharth Kochar #else
301949a52d2SSandrine Bailleux 		/*
302949a52d2SSandrine Bailleux 		 * Check the image will fit into the free trusted RAM after BL1
303949a52d2SSandrine Bailleux 		 * load.
304949a52d2SSandrine Bailleux 		 */
3059f1489e4SSandrine Bailleux 		const meminfo_t *mem_layout = bl1_plat_sec_mem_layout();
306949a52d2SSandrine Bailleux 		if (!is_mem_free(mem_layout->free_base, mem_layout->free_size,
307949a52d2SSandrine Bailleux 					image_desc->image_info.image_base,
308949a52d2SSandrine Bailleux 					image_size)) {
3099f1489e4SSandrine Bailleux 			WARN("BL1-FWU: Copy not allowed due to insufficient"
3109f1489e4SSandrine Bailleux 			     " resources.\n");
31148bfb88eSYatharth Kochar 			return -ENOMEM;
31248bfb88eSYatharth Kochar 		}
31353d703a5SYatharth Kochar #endif
31448bfb88eSYatharth Kochar 
3159f1489e4SSandrine Bailleux 		/* Save the given image size. */
31648bfb88eSYatharth Kochar 		image_desc->image_info.image_size = image_size;
31748bfb88eSYatharth Kochar 
318128daee2SAntonio Nino Diaz 		/* Make sure the image doesn't overlap other images. */
319128daee2SAntonio Nino Diaz 		if (bl1_fwu_image_check_overlaps(image_id)) {
320128daee2SAntonio Nino Diaz 			image_desc->image_info.image_size = 0;
321128daee2SAntonio Nino Diaz 			WARN("BL1-FWU: This image overlaps another one\n");
322128daee2SAntonio Nino Diaz 			return -EPERM;
323128daee2SAntonio Nino Diaz 		}
324128daee2SAntonio Nino Diaz 
325b38a9e5cSSandrine Bailleux 		/*
326b38a9e5cSSandrine Bailleux 		 * copied_size must be explicitly initialized here because the
327b38a9e5cSSandrine Bailleux 		 * FWU code doesn't necessarily do it when it resets the state
328b38a9e5cSSandrine Bailleux 		 * machine.
329b38a9e5cSSandrine Bailleux 		 */
330b38a9e5cSSandrine Bailleux 		image_desc->copied_size = 0;
331b38a9e5cSSandrine Bailleux 	}
332b38a9e5cSSandrine Bailleux 
333b38a9e5cSSandrine Bailleux 	/*
334b38a9e5cSSandrine Bailleux 	 * If the given block size is more than the total image size
335b38a9e5cSSandrine Bailleux 	 * then clip the former to the latter.
336b38a9e5cSSandrine Bailleux 	 */
337b38a9e5cSSandrine Bailleux 	remaining = image_size - image_desc->copied_size;
338b38a9e5cSSandrine Bailleux 	if (block_size > remaining) {
339b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Block size is too big, clipping it.\n");
340b38a9e5cSSandrine Bailleux 		block_size = remaining;
341b38a9e5cSSandrine Bailleux 	}
342b38a9e5cSSandrine Bailleux 
343b38a9e5cSSandrine Bailleux 	/* Make sure the source image is mapped in memory. */
344b38a9e5cSSandrine Bailleux 	if (bl1_plat_mem_check(image_src, block_size, flags)) {
345b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Source image is not mapped.\n");
346b38a9e5cSSandrine Bailleux 		return -ENOMEM;
347b38a9e5cSSandrine Bailleux 	}
348b38a9e5cSSandrine Bailleux 
349128daee2SAntonio Nino Diaz 	if (bl1_fwu_add_loaded_id(image_id)) {
350128daee2SAntonio Nino Diaz 		WARN("BL1-FWU: Too many images loaded at the same time.\n");
351128daee2SAntonio Nino Diaz 		return -ENOMEM;
352128daee2SAntonio Nino Diaz 	}
353128daee2SAntonio Nino Diaz 
354b38a9e5cSSandrine Bailleux 	/* Everything looks sane. Go ahead and copy the block of data. */
355b38a9e5cSSandrine Bailleux 	dest_addr = image_desc->image_info.image_base + image_desc->copied_size;
3569f1489e4SSandrine Bailleux 	memcpy((void *) dest_addr, (const void *) image_src, block_size);
3579f1489e4SSandrine Bailleux 	flush_dcache_range(dest_addr, block_size);
35848bfb88eSYatharth Kochar 
359b38a9e5cSSandrine Bailleux 	image_desc->copied_size += block_size;
360b38a9e5cSSandrine Bailleux 	image_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  ******************************************************************************/
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;
37848bfb88eSYatharth Kochar 
37948bfb88eSYatharth Kochar 	/* Get the image descriptor. */
38048bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
38148bfb88eSYatharth Kochar 	if (!image_desc)
38248bfb88eSYatharth Kochar 		return -EPERM;
38348bfb88eSYatharth Kochar 
384843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == SECURE) {
38548bfb88eSYatharth Kochar 		if (image_desc->state != IMAGE_STATE_RESET) {
38648bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication from secure world "
38748bfb88eSYatharth Kochar 				"while in invalid state\n");
38848bfb88eSYatharth Kochar 			return -EPERM;
38948bfb88eSYatharth Kochar 		}
39048bfb88eSYatharth Kochar 	} else {
391843ddee4SYatharth Kochar 		if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) {
39248bfb88eSYatharth Kochar 			if (image_desc->state != IMAGE_STATE_COPIED) {
39348bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of secure image "
39448bfb88eSYatharth Kochar 					"from non-secure world while not in copied state\n");
39548bfb88eSYatharth Kochar 				return -EPERM;
39648bfb88eSYatharth Kochar 			}
39748bfb88eSYatharth Kochar 		} else {
39848bfb88eSYatharth Kochar 			if (image_desc->state != IMAGE_STATE_RESET) {
39948bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of non-secure image "
40048bfb88eSYatharth Kochar 					"from non-secure world while in invalid state\n");
40148bfb88eSYatharth Kochar 				return -EPERM;
40248bfb88eSYatharth Kochar 			}
40348bfb88eSYatharth Kochar 		}
40448bfb88eSYatharth Kochar 	}
40548bfb88eSYatharth Kochar 
40648bfb88eSYatharth Kochar 	if (image_desc->state == IMAGE_STATE_COPIED) {
40748bfb88eSYatharth Kochar 		/*
40848bfb88eSYatharth Kochar 		 * Image is in COPIED state.
40948bfb88eSYatharth Kochar 		 * Use the stored address and size.
41048bfb88eSYatharth Kochar 		 */
41148bfb88eSYatharth Kochar 		base_addr = image_desc->image_info.image_base;
41248bfb88eSYatharth Kochar 		total_size = image_desc->image_info.image_size;
41348bfb88eSYatharth Kochar 	} else {
414949a52d2SSandrine Bailleux 		if ((!image_src) || (!image_size) ||
415949a52d2SSandrine Bailleux 		    check_uptr_overflow(image_src, image_size - 1)) {
41648bfb88eSYatharth Kochar 			WARN("BL1-FWU: Auth not allowed due to invalid"
41748bfb88eSYatharth Kochar 				" image source/size\n");
41848bfb88eSYatharth Kochar 			return -ENOMEM;
41948bfb88eSYatharth Kochar 		}
42048bfb88eSYatharth Kochar 
42148bfb88eSYatharth Kochar 		/*
42248bfb88eSYatharth Kochar 		 * Image is in RESET state.
42348bfb88eSYatharth Kochar 		 * Check the parameters and authenticate the source image in place.
42448bfb88eSYatharth Kochar 		 */
42503131c85SDan Handley 		if (bl1_plat_mem_check(image_src, image_size,	\
42603131c85SDan Handley 					image_desc->ep_info.h.attr)) {
42748bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
42848bfb88eSYatharth Kochar 			return -ENOMEM;
42948bfb88eSYatharth Kochar 		}
43048bfb88eSYatharth Kochar 
431128daee2SAntonio Nino Diaz 		if (bl1_fwu_add_loaded_id(image_id)) {
432128daee2SAntonio Nino Diaz 			WARN("BL1-FWU: Too many images loaded at the same time.\n");
433128daee2SAntonio Nino Diaz 			return -ENOMEM;
434128daee2SAntonio Nino Diaz 		}
435128daee2SAntonio Nino Diaz 
43648bfb88eSYatharth Kochar 		base_addr = image_src;
43748bfb88eSYatharth Kochar 		total_size = image_size;
43848bfb88eSYatharth Kochar 
43948bfb88eSYatharth Kochar 		/* Update the image size in the descriptor. */
44048bfb88eSYatharth Kochar 		image_desc->image_info.image_size = total_size;
44148bfb88eSYatharth Kochar 	}
44248bfb88eSYatharth Kochar 
44348bfb88eSYatharth Kochar 	/*
44448bfb88eSYatharth Kochar 	 * Authenticate the image.
44548bfb88eSYatharth Kochar 	 */
44648bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
44748bfb88eSYatharth Kochar 	result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
44848bfb88eSYatharth Kochar 	if (result != 0) {
44948bfb88eSYatharth Kochar 		WARN("BL1-FWU: Authentication Failed err=%d\n", result);
45048bfb88eSYatharth Kochar 
45148bfb88eSYatharth Kochar 		/*
45248bfb88eSYatharth Kochar 		 * Authentication has failed.
45348bfb88eSYatharth Kochar 		 * Clear the memory if the image was copied.
45448bfb88eSYatharth Kochar 		 * This is to prevent an attack where this contains
45548bfb88eSYatharth Kochar 		 * some malicious code that can somehow be executed later.
45648bfb88eSYatharth Kochar 		 */
45748bfb88eSYatharth Kochar 		if (image_desc->state == IMAGE_STATE_COPIED) {
45848bfb88eSYatharth Kochar 			/* Clear the memory.*/
459308d359bSDouglas Raillard 			zero_normalmem((void *)base_addr, total_size);
46048bfb88eSYatharth Kochar 			flush_dcache_range(base_addr, total_size);
46148bfb88eSYatharth Kochar 
46248bfb88eSYatharth Kochar 			/* Indicate that image can be copied again*/
46348bfb88eSYatharth Kochar 			image_desc->state = IMAGE_STATE_RESET;
46448bfb88eSYatharth Kochar 		}
465128daee2SAntonio Nino Diaz 
466128daee2SAntonio Nino Diaz 		/*
467128daee2SAntonio Nino Diaz 		 * Even if this fails it's ok because the ID isn't in the array.
468128daee2SAntonio Nino Diaz 		 * The image cannot be in RESET state here, it is checked at the
469128daee2SAntonio Nino Diaz 		 * beginning of the function.
470128daee2SAntonio Nino Diaz 		 */
471128daee2SAntonio Nino Diaz 		bl1_fwu_remove_loaded_id(image_id);
47248bfb88eSYatharth Kochar 		return -EAUTH;
47348bfb88eSYatharth Kochar 	}
47448bfb88eSYatharth Kochar 
47548bfb88eSYatharth Kochar 	/* Indicate that image is in authenticated state. */
47648bfb88eSYatharth Kochar 	image_desc->state = IMAGE_STATE_AUTHENTICATED;
47748bfb88eSYatharth Kochar 
47848bfb88eSYatharth Kochar 	/*
47948bfb88eSYatharth Kochar 	 * Flush image_info to memory so that other
48048bfb88eSYatharth Kochar 	 * secure world images can see changes.
48148bfb88eSYatharth Kochar 	 */
48248bfb88eSYatharth Kochar 	flush_dcache_range((unsigned long)&image_desc->image_info,
48348bfb88eSYatharth Kochar 		sizeof(image_info_t));
48448bfb88eSYatharth Kochar 
48548bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authentication was successful\n");
48648bfb88eSYatharth Kochar 
48748bfb88eSYatharth Kochar 	return 0;
48848bfb88eSYatharth Kochar }
48948bfb88eSYatharth Kochar 
49048bfb88eSYatharth Kochar /*******************************************************************************
49148bfb88eSYatharth Kochar  * This function is responsible for executing Secure images.
49248bfb88eSYatharth Kochar  ******************************************************************************/
49348bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id,
49448bfb88eSYatharth Kochar 			void **handle,
49548bfb88eSYatharth Kochar 			unsigned int flags)
49648bfb88eSYatharth Kochar {
49748bfb88eSYatharth Kochar 	/* Get the image descriptor. */
49848bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
49948bfb88eSYatharth Kochar 
50048bfb88eSYatharth Kochar 	/*
50148bfb88eSYatharth Kochar 	 * Execution is NOT allowed if:
50228955d57SDan Handley 	 * image_id is invalid OR
50348bfb88eSYatharth Kochar 	 * Caller is from Secure world OR
50448bfb88eSYatharth Kochar 	 * Image is Non-Secure OR
50548bfb88eSYatharth Kochar 	 * Image is Non-Executable OR
50648bfb88eSYatharth Kochar 	 * Image is NOT in AUTHENTICATED state.
50748bfb88eSYatharth Kochar 	 */
50848bfb88eSYatharth Kochar 	if ((!image_desc) ||
509843ddee4SYatharth Kochar 	    (GET_SECURITY_STATE(flags) == SECURE) ||
510843ddee4SYatharth Kochar 	    (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) ||
511843ddee4SYatharth Kochar 	    (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) ||
51248bfb88eSYatharth Kochar 	    (image_desc->state != IMAGE_STATE_AUTHENTICATED)) {
51348bfb88eSYatharth Kochar 		WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
51448bfb88eSYatharth Kochar 		return -EPERM;
51548bfb88eSYatharth Kochar 	}
51648bfb88eSYatharth Kochar 
51748bfb88eSYatharth Kochar 	INFO("BL1-FWU: Executing Secure image\n");
51848bfb88eSYatharth Kochar 
519a4409008Sdp-arm #ifdef AARCH64
52048bfb88eSYatharth Kochar 	/* Save NS-EL1 system registers. */
52148bfb88eSYatharth Kochar 	cm_el1_sysregs_context_save(NON_SECURE);
522a4409008Sdp-arm #endif
52348bfb88eSYatharth Kochar 
52448bfb88eSYatharth Kochar 	/* Prepare the image for execution. */
52548bfb88eSYatharth Kochar 	bl1_prepare_next_image(image_id);
52648bfb88eSYatharth Kochar 
52748bfb88eSYatharth Kochar 	/* Update the secure image id. */
52848bfb88eSYatharth Kochar 	sec_exec_image_id = image_id;
52948bfb88eSYatharth Kochar 
530a4409008Sdp-arm #ifdef AARCH64
53148bfb88eSYatharth Kochar 	*handle = cm_get_context(SECURE);
532a4409008Sdp-arm #else
533a4409008Sdp-arm 	*handle = smc_get_ctx(SECURE);
534a4409008Sdp-arm #endif
53548bfb88eSYatharth Kochar 	return 0;
53648bfb88eSYatharth Kochar }
53748bfb88eSYatharth Kochar 
53848bfb88eSYatharth Kochar /*******************************************************************************
53928955d57SDan Handley  * This function is responsible for resuming execution in the other security
54028955d57SDan Handley  * world
54148bfb88eSYatharth Kochar  ******************************************************************************/
54228955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param,
54348bfb88eSYatharth Kochar 			void **handle,
54448bfb88eSYatharth Kochar 			unsigned int flags)
54548bfb88eSYatharth Kochar {
54648bfb88eSYatharth Kochar 	image_desc_t *image_desc;
54748bfb88eSYatharth Kochar 	unsigned int resume_sec_state;
548843ddee4SYatharth Kochar 	unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
54948bfb88eSYatharth Kochar 
55048bfb88eSYatharth Kochar 	/* Get the image descriptor for last executed secure image id. */
55148bfb88eSYatharth Kochar 	image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
55228955d57SDan Handley 	if (caller_sec_state == NON_SECURE) {
55328955d57SDan Handley 		if (!image_desc) {
55428955d57SDan Handley 			WARN("BL1-FWU: Resume not allowed due to no available"
55528955d57SDan Handley 				"secure image\n");
55648bfb88eSYatharth Kochar 			return -EPERM;
55748bfb88eSYatharth Kochar 		}
55828955d57SDan Handley 	} else {
55928955d57SDan Handley 		/* image_desc must be valid for secure world callers */
56028955d57SDan Handley 		assert(image_desc);
56128955d57SDan Handley 	}
56228955d57SDan Handley 
563843ddee4SYatharth Kochar 	assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
564843ddee4SYatharth Kochar 	assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
56528955d57SDan Handley 
56628955d57SDan Handley 	if (caller_sec_state == SECURE) {
56728955d57SDan Handley 		assert(image_desc->state == IMAGE_STATE_EXECUTED);
56848bfb88eSYatharth Kochar 
56948bfb88eSYatharth Kochar 		/* Update the flags. */
57048bfb88eSYatharth Kochar 		image_desc->state = IMAGE_STATE_INTERRUPTED;
57148bfb88eSYatharth Kochar 		resume_sec_state = NON_SECURE;
57248bfb88eSYatharth Kochar 	} else {
57328955d57SDan Handley 		assert(image_desc->state == IMAGE_STATE_INTERRUPTED);
57448bfb88eSYatharth Kochar 
57548bfb88eSYatharth Kochar 		/* Update the flags. */
57648bfb88eSYatharth Kochar 		image_desc->state = IMAGE_STATE_EXECUTED;
57748bfb88eSYatharth Kochar 		resume_sec_state = SECURE;
57848bfb88eSYatharth Kochar 	}
57948bfb88eSYatharth Kochar 
580a4409008Sdp-arm 	INFO("BL1-FWU: Resuming %s world context\n",
581a4409008Sdp-arm 		(resume_sec_state == SECURE) ? "secure" : "normal");
582a4409008Sdp-arm 
583a4409008Sdp-arm #ifdef AARCH64
58448bfb88eSYatharth Kochar 	/* Save the EL1 system registers of calling world. */
58528955d57SDan Handley 	cm_el1_sysregs_context_save(caller_sec_state);
58648bfb88eSYatharth Kochar 
58748bfb88eSYatharth Kochar 	/* Restore the EL1 system registers of resuming world. */
58848bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(resume_sec_state);
58948bfb88eSYatharth Kochar 
59048bfb88eSYatharth Kochar 	/* Update the next context. */
59148bfb88eSYatharth Kochar 	cm_set_next_eret_context(resume_sec_state);
59248bfb88eSYatharth Kochar 
59348bfb88eSYatharth Kochar 	*handle = cm_get_context(resume_sec_state);
594a4409008Sdp-arm #else
595a4409008Sdp-arm 	/* Update the next context. */
596a4409008Sdp-arm 	cm_set_next_context(cm_get_context(resume_sec_state));
597a4409008Sdp-arm 
598a4409008Sdp-arm 	/* Prepare the smc context for the next BL image. */
599a4409008Sdp-arm 	smc_set_next_ctx(resume_sec_state);
600a4409008Sdp-arm 
601a4409008Sdp-arm 	*handle = smc_get_ctx(resume_sec_state);
602a4409008Sdp-arm #endif
60348bfb88eSYatharth Kochar 	return image_param;
60448bfb88eSYatharth Kochar }
60548bfb88eSYatharth Kochar 
60648bfb88eSYatharth Kochar /*******************************************************************************
60748bfb88eSYatharth Kochar  * This function is responsible for resuming normal world context.
60848bfb88eSYatharth Kochar  ******************************************************************************/
60948bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
61048bfb88eSYatharth Kochar {
61128955d57SDan Handley 	image_desc_t *image_desc;
61248bfb88eSYatharth Kochar 
61328955d57SDan Handley 	/* Make sure caller is from the secure world */
614843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == NON_SECURE) {
61528955d57SDan Handley 		WARN("BL1-FWU: Image done not allowed from normal world\n");
61648bfb88eSYatharth Kochar 		return -EPERM;
61748bfb88eSYatharth Kochar 	}
61848bfb88eSYatharth Kochar 
61928955d57SDan Handley 	/* Get the image descriptor for last executed secure image id */
62028955d57SDan Handley 	image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
62128955d57SDan Handley 
62228955d57SDan Handley 	/* image_desc must correspond to a valid secure executing image */
62328955d57SDan Handley 	assert(image_desc);
624843ddee4SYatharth Kochar 	assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
625843ddee4SYatharth Kochar 	assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
62628955d57SDan Handley 	assert(image_desc->state == IMAGE_STATE_EXECUTED);
62728955d57SDan Handley 
628128daee2SAntonio Nino Diaz #if ENABLE_ASSERTIONS
629128daee2SAntonio Nino Diaz 	int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id);
630128daee2SAntonio Nino Diaz 	assert(rc == 0);
631128daee2SAntonio Nino Diaz #else
632128daee2SAntonio Nino Diaz 	bl1_fwu_remove_loaded_id(sec_exec_image_id);
633128daee2SAntonio Nino Diaz #endif
634128daee2SAntonio Nino Diaz 
63548bfb88eSYatharth Kochar 	/* Update the flags. */
63648bfb88eSYatharth Kochar 	image_desc->state = IMAGE_STATE_RESET;
63748bfb88eSYatharth Kochar 	sec_exec_image_id = INVALID_IMAGE_ID;
63848bfb88eSYatharth Kochar 
639a4409008Sdp-arm 	INFO("BL1-FWU: Resuming Normal world context\n");
640a4409008Sdp-arm #ifdef AARCH64
64148bfb88eSYatharth Kochar 	/*
64248bfb88eSYatharth Kochar 	 * Secure world is done so no need to save the context.
64348bfb88eSYatharth Kochar 	 * Just restore the Non-Secure context.
64448bfb88eSYatharth Kochar 	 */
64548bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(NON_SECURE);
64648bfb88eSYatharth Kochar 
64748bfb88eSYatharth Kochar 	/* Update the next context. */
64848bfb88eSYatharth Kochar 	cm_set_next_eret_context(NON_SECURE);
64948bfb88eSYatharth Kochar 
65048bfb88eSYatharth Kochar 	*handle = cm_get_context(NON_SECURE);
651a4409008Sdp-arm #else
652a4409008Sdp-arm 	/* Update the next context. */
653a4409008Sdp-arm 	cm_set_next_context(cm_get_context(NON_SECURE));
654a4409008Sdp-arm 
655a4409008Sdp-arm 	/* Prepare the smc context for the next BL image. */
656a4409008Sdp-arm 	smc_set_next_ctx(NON_SECURE);
657a4409008Sdp-arm 
658a4409008Sdp-arm 	*handle = smc_get_ctx(NON_SECURE);
659a4409008Sdp-arm #endif
66048bfb88eSYatharth Kochar 	return 0;
66148bfb88eSYatharth Kochar }
66248bfb88eSYatharth Kochar 
66348bfb88eSYatharth Kochar /*******************************************************************************
66448bfb88eSYatharth Kochar  * This function provides the opportunity for users to perform any
66548bfb88eSYatharth Kochar  * platform specific handling after the Firmware update is done.
66648bfb88eSYatharth Kochar  ******************************************************************************/
6671f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
66848bfb88eSYatharth Kochar {
66948bfb88eSYatharth Kochar 	NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
67048bfb88eSYatharth Kochar 
67148bfb88eSYatharth Kochar 	/*
67248bfb88eSYatharth Kochar 	 * Call platform done function.
67348bfb88eSYatharth Kochar 	 */
6741f37b944SDan Handley 	bl1_plat_fwu_done(client_cookie, reserved);
67548bfb88eSYatharth Kochar 	assert(0);
67648bfb88eSYatharth Kochar }
677*9d6fc3c3SAntonio Nino Diaz 
678*9d6fc3c3SAntonio Nino Diaz /*******************************************************************************
679*9d6fc3c3SAntonio Nino Diaz  * This function resets an image to IMAGE_STATE_RESET. It fails if the image is
680*9d6fc3c3SAntonio Nino Diaz  * being executed.
681*9d6fc3c3SAntonio Nino Diaz  ******************************************************************************/
682*9d6fc3c3SAntonio Nino Diaz static int bl1_fwu_image_reset(unsigned int image_id, unsigned int flags)
683*9d6fc3c3SAntonio Nino Diaz {
684*9d6fc3c3SAntonio Nino Diaz 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
685*9d6fc3c3SAntonio Nino Diaz 
686*9d6fc3c3SAntonio Nino Diaz 	if ((!image_desc) || (GET_SECURITY_STATE(flags) == SECURE)) {
687*9d6fc3c3SAntonio Nino Diaz 		WARN("BL1-FWU: Reset not allowed due to invalid args\n");
688*9d6fc3c3SAntonio Nino Diaz 		return -EPERM;
689*9d6fc3c3SAntonio Nino Diaz 	}
690*9d6fc3c3SAntonio Nino Diaz 
691*9d6fc3c3SAntonio Nino Diaz 	switch (image_desc->state) {
692*9d6fc3c3SAntonio Nino Diaz 
693*9d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_RESET:
694*9d6fc3c3SAntonio Nino Diaz 		/* Nothing to do. */
695*9d6fc3c3SAntonio Nino Diaz 		break;
696*9d6fc3c3SAntonio Nino Diaz 
697*9d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_INTERRUPTED:
698*9d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_AUTHENTICATED:
699*9d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_COPIED:
700*9d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_COPYING:
701*9d6fc3c3SAntonio Nino Diaz 
702*9d6fc3c3SAntonio Nino Diaz 		if (bl1_fwu_remove_loaded_id(image_id)) {
703*9d6fc3c3SAntonio Nino Diaz 			WARN("BL1-FWU: Image reset couldn't find the image ID\n");
704*9d6fc3c3SAntonio Nino Diaz 			return -EPERM;
705*9d6fc3c3SAntonio Nino Diaz 		}
706*9d6fc3c3SAntonio Nino Diaz 
707*9d6fc3c3SAntonio Nino Diaz 		/* Clear the memory.*/
708*9d6fc3c3SAntonio Nino Diaz 		zero_normalmem((void *)image_desc->image_info.image_base,
709*9d6fc3c3SAntonio Nino Diaz 				image_desc->copied_size);
710*9d6fc3c3SAntonio Nino Diaz 		flush_dcache_range(image_desc->image_info.image_base,
711*9d6fc3c3SAntonio Nino Diaz 				image_desc->copied_size);
712*9d6fc3c3SAntonio Nino Diaz 
713*9d6fc3c3SAntonio Nino Diaz 		/* Reset status variables */
714*9d6fc3c3SAntonio Nino Diaz 		image_desc->copied_size = 0;
715*9d6fc3c3SAntonio Nino Diaz 		image_desc->image_info.image_size = 0;
716*9d6fc3c3SAntonio Nino Diaz 		image_desc->state = IMAGE_STATE_RESET;
717*9d6fc3c3SAntonio Nino Diaz 
718*9d6fc3c3SAntonio Nino Diaz 		/* Clear authentication state */
719*9d6fc3c3SAntonio Nino Diaz 		auth_img_flags[image_id] = 0;
720*9d6fc3c3SAntonio Nino Diaz 
721*9d6fc3c3SAntonio Nino Diaz 		break;
722*9d6fc3c3SAntonio Nino Diaz 
723*9d6fc3c3SAntonio Nino Diaz 	case IMAGE_STATE_EXECUTED:
724*9d6fc3c3SAntonio Nino Diaz 	default:
725*9d6fc3c3SAntonio Nino Diaz 		assert(0);
726*9d6fc3c3SAntonio Nino Diaz 	}
727*9d6fc3c3SAntonio Nino Diaz 
728*9d6fc3c3SAntonio Nino Diaz 	return 0;
729*9d6fc3c3SAntonio Nino Diaz }
730