xref: /rk3399_ARM-atf/bl1/bl1_fwu.c (revision 82cb2c1ad9897473743f08437d0a3995bed561b9)
148bfb88eSYatharth Kochar /*
2308d359bSDouglas Raillard  * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
348bfb88eSYatharth Kochar  *
4*82cb2c1aSdp-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);
431f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved);
4448bfb88eSYatharth Kochar 
4548bfb88eSYatharth Kochar /*
4648bfb88eSYatharth Kochar  * This keeps track of last executed secure image id.
4748bfb88eSYatharth Kochar  */
4848bfb88eSYatharth Kochar static unsigned int sec_exec_image_id = INVALID_IMAGE_ID;
4948bfb88eSYatharth Kochar 
5048bfb88eSYatharth Kochar /*******************************************************************************
5148bfb88eSYatharth Kochar  * Top level handler for servicing FWU SMCs.
5248bfb88eSYatharth Kochar  ******************************************************************************/
5348bfb88eSYatharth Kochar register_t bl1_fwu_smc_handler(unsigned int smc_fid,
5448bfb88eSYatharth Kochar 			register_t x1,
5548bfb88eSYatharth Kochar 			register_t x2,
5648bfb88eSYatharth Kochar 			register_t x3,
5748bfb88eSYatharth Kochar 			register_t x4,
5848bfb88eSYatharth Kochar 			void *cookie,
5948bfb88eSYatharth Kochar 			void *handle,
6048bfb88eSYatharth Kochar 			unsigned int flags)
6148bfb88eSYatharth Kochar {
6248bfb88eSYatharth Kochar 
6348bfb88eSYatharth Kochar 	switch (smc_fid) {
6448bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_COPY:
6548bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_copy(x1, x2, x3, x4, flags));
6648bfb88eSYatharth Kochar 
6748bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_AUTH:
6848bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_auth(x1, x2, x3, flags));
6948bfb88eSYatharth Kochar 
7048bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_EXECUTE:
7148bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_execute(x1, &handle, flags));
7248bfb88eSYatharth Kochar 
7348bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_RESUME:
7428955d57SDan Handley 		SMC_RET1(handle, bl1_fwu_image_resume(x1, &handle, flags));
7548bfb88eSYatharth Kochar 
7648bfb88eSYatharth Kochar 	case FWU_SMC_SEC_IMAGE_DONE:
7748bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
7848bfb88eSYatharth Kochar 
7948bfb88eSYatharth Kochar 	case FWU_SMC_UPDATE_DONE:
801f37b944SDan Handley 		bl1_fwu_done((void *)x1, NULL);
8148bfb88eSYatharth Kochar 		/* We should never return from bl1_fwu_done() */
8248bfb88eSYatharth Kochar 
8348bfb88eSYatharth Kochar 	default:
8448bfb88eSYatharth Kochar 		assert(0);
8548bfb88eSYatharth Kochar 		break;
8648bfb88eSYatharth Kochar 	}
8748bfb88eSYatharth Kochar 
887a317a70SAntonio Nino Diaz 	SMC_RET1(handle, SMC_UNK);
8948bfb88eSYatharth Kochar }
9048bfb88eSYatharth Kochar 
9148bfb88eSYatharth Kochar /*******************************************************************************
9248bfb88eSYatharth Kochar  * This function is responsible for copying secure images in AP Secure RAM.
9348bfb88eSYatharth Kochar  ******************************************************************************/
9448bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id,
9548bfb88eSYatharth Kochar 			uintptr_t image_src,
9648bfb88eSYatharth Kochar 			unsigned int block_size,
9748bfb88eSYatharth Kochar 			unsigned int image_size,
9848bfb88eSYatharth Kochar 			unsigned int flags)
9948bfb88eSYatharth Kochar {
1009f1489e4SSandrine Bailleux 	uintptr_t dest_addr;
101b38a9e5cSSandrine Bailleux 	unsigned int remaining;
10248bfb88eSYatharth Kochar 
10348bfb88eSYatharth Kochar 	/* Get the image descriptor. */
10448bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
1059f1489e4SSandrine Bailleux 	if (!image_desc) {
1069f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Invalid image ID %u\n", image_id);
10748bfb88eSYatharth Kochar 		return -EPERM;
10848bfb88eSYatharth Kochar 	}
10948bfb88eSYatharth Kochar 
1109f1489e4SSandrine Bailleux 	/*
1119f1489e4SSandrine Bailleux 	 * The request must originate from a non-secure caller and target a
1129f1489e4SSandrine Bailleux 	 * secure image. Any other scenario is invalid.
1139f1489e4SSandrine Bailleux 	 */
1149f1489e4SSandrine Bailleux 	if (GET_SECURITY_STATE(flags) == SECURE) {
1159f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed from secure world.\n");
1169f1489e4SSandrine Bailleux 		return -EPERM;
1179f1489e4SSandrine Bailleux 	}
1189f1489e4SSandrine Bailleux 	if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) {
1199f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
1209f1489e4SSandrine Bailleux 		return -EPERM;
1219f1489e4SSandrine Bailleux 	}
1229f1489e4SSandrine Bailleux 
1239f1489e4SSandrine Bailleux 	/* Check whether the FWU state machine is in the correct state. */
1249f1489e4SSandrine Bailleux 	if ((image_desc->state != IMAGE_STATE_RESET) &&
1259f1489e4SSandrine Bailleux 	    (image_desc->state != IMAGE_STATE_COPYING)) {
1269f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed at this point of the FWU"
1279f1489e4SSandrine Bailleux 			" process.\n");
12848bfb88eSYatharth Kochar 		return -EPERM;
12948bfb88eSYatharth Kochar 	}
13048bfb88eSYatharth Kochar 
131949a52d2SSandrine Bailleux 	if ((!image_src) || (!block_size) ||
132949a52d2SSandrine Bailleux 	    check_uptr_overflow(image_src, block_size - 1)) {
13348bfb88eSYatharth Kochar 		WARN("BL1-FWU: Copy not allowed due to invalid image source"
13448bfb88eSYatharth Kochar 			" or block size\n");
13548bfb88eSYatharth Kochar 		return -ENOMEM;
13648bfb88eSYatharth Kochar 	}
13748bfb88eSYatharth Kochar 
13848bfb88eSYatharth Kochar 	if (image_desc->state == IMAGE_STATE_COPYING) {
1391bfb7068SSandrine Bailleux 		/*
1401bfb7068SSandrine Bailleux 		 * There must have been at least 1 copy operation for this image
1411bfb7068SSandrine Bailleux 		 * previously.
1421bfb7068SSandrine Bailleux 		 */
1431bfb7068SSandrine Bailleux 		assert(image_desc->copied_size != 0);
1441bfb7068SSandrine Bailleux 		/*
1451bfb7068SSandrine Bailleux 		 * The image size must have been recorded in the 1st copy
1461bfb7068SSandrine Bailleux 		 * operation.
1471bfb7068SSandrine Bailleux 		 */
1489f1489e4SSandrine Bailleux 		image_size = image_desc->image_info.image_size;
1491bfb7068SSandrine Bailleux 		assert(image_size != 0);
1501bfb7068SSandrine Bailleux 		assert(image_desc->copied_size < image_size);
1511bfb7068SSandrine Bailleux 
15248bfb88eSYatharth Kochar 		INFO("BL1-FWU: Continuing image copy in blocks\n");
1539f1489e4SSandrine Bailleux 	} else { /* image_desc->state == IMAGE_STATE_RESET */
1549f1489e4SSandrine Bailleux 		INFO("BL1-FWU: Initial call to copy an image\n");
15548bfb88eSYatharth Kochar 
1569f1489e4SSandrine Bailleux 		/*
1579f1489e4SSandrine Bailleux 		 * image_size is relevant only for the 1st copy request, it is
1589f1489e4SSandrine Bailleux 		 * then ignored for subsequent calls for this image.
1599f1489e4SSandrine Bailleux 		 */
16048bfb88eSYatharth Kochar 		if (!image_size) {
1619f1489e4SSandrine Bailleux 			WARN("BL1-FWU: Copy not allowed due to invalid image"
1629f1489e4SSandrine Bailleux 				" size\n");
16348bfb88eSYatharth Kochar 			return -ENOMEM;
16448bfb88eSYatharth Kochar 		}
16548bfb88eSYatharth Kochar 
16653d703a5SYatharth Kochar #if LOAD_IMAGE_V2
16753d703a5SYatharth Kochar 		/* Check that the image size to load is within limit */
16853d703a5SYatharth Kochar 		if (image_size > image_desc->image_info.image_max_size) {
16953d703a5SYatharth Kochar 			WARN("BL1-FWU: Image size out of bounds\n");
17053d703a5SYatharth Kochar 			return -ENOMEM;
17153d703a5SYatharth Kochar 		}
17253d703a5SYatharth Kochar #else
173949a52d2SSandrine Bailleux 		/*
174949a52d2SSandrine Bailleux 		 * Check the image will fit into the free trusted RAM after BL1
175949a52d2SSandrine Bailleux 		 * load.
176949a52d2SSandrine Bailleux 		 */
1779f1489e4SSandrine Bailleux 		const meminfo_t *mem_layout = bl1_plat_sec_mem_layout();
178949a52d2SSandrine Bailleux 		if (!is_mem_free(mem_layout->free_base, mem_layout->free_size,
179949a52d2SSandrine Bailleux 					image_desc->image_info.image_base,
180949a52d2SSandrine Bailleux 					image_size)) {
1819f1489e4SSandrine Bailleux 			WARN("BL1-FWU: Copy not allowed due to insufficient"
1829f1489e4SSandrine Bailleux 			     " resources.\n");
18348bfb88eSYatharth Kochar 			return -ENOMEM;
18448bfb88eSYatharth Kochar 		}
18553d703a5SYatharth Kochar #endif
18648bfb88eSYatharth Kochar 
1879f1489e4SSandrine Bailleux 		/* Save the given image size. */
18848bfb88eSYatharth Kochar 		image_desc->image_info.image_size = image_size;
18948bfb88eSYatharth Kochar 
190b38a9e5cSSandrine Bailleux 		/*
191b38a9e5cSSandrine Bailleux 		 * copied_size must be explicitly initialized here because the
192b38a9e5cSSandrine Bailleux 		 * FWU code doesn't necessarily do it when it resets the state
193b38a9e5cSSandrine Bailleux 		 * machine.
194b38a9e5cSSandrine Bailleux 		 */
195b38a9e5cSSandrine Bailleux 		image_desc->copied_size = 0;
196b38a9e5cSSandrine Bailleux 	}
197b38a9e5cSSandrine Bailleux 
198b38a9e5cSSandrine Bailleux 	/*
199b38a9e5cSSandrine Bailleux 	 * If the given block size is more than the total image size
200b38a9e5cSSandrine Bailleux 	 * then clip the former to the latter.
201b38a9e5cSSandrine Bailleux 	 */
202b38a9e5cSSandrine Bailleux 	remaining = image_size - image_desc->copied_size;
203b38a9e5cSSandrine Bailleux 	if (block_size > remaining) {
204b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Block size is too big, clipping it.\n");
205b38a9e5cSSandrine Bailleux 		block_size = remaining;
206b38a9e5cSSandrine Bailleux 	}
207b38a9e5cSSandrine Bailleux 
208b38a9e5cSSandrine Bailleux 	/* Make sure the source image is mapped in memory. */
209b38a9e5cSSandrine Bailleux 	if (bl1_plat_mem_check(image_src, block_size, flags)) {
210b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Source image is not mapped.\n");
211b38a9e5cSSandrine Bailleux 		return -ENOMEM;
212b38a9e5cSSandrine Bailleux 	}
213b38a9e5cSSandrine Bailleux 
214b38a9e5cSSandrine Bailleux 	/* Everything looks sane. Go ahead and copy the block of data. */
215b38a9e5cSSandrine Bailleux 	dest_addr = image_desc->image_info.image_base + image_desc->copied_size;
2169f1489e4SSandrine Bailleux 	memcpy((void *) dest_addr, (const void *) image_src, block_size);
2179f1489e4SSandrine Bailleux 	flush_dcache_range(dest_addr, block_size);
21848bfb88eSYatharth Kochar 
219b38a9e5cSSandrine Bailleux 	image_desc->copied_size += block_size;
220b38a9e5cSSandrine Bailleux 	image_desc->state = (block_size == remaining) ?
221b38a9e5cSSandrine Bailleux 		IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
22248bfb88eSYatharth Kochar 
223b38a9e5cSSandrine Bailleux 	INFO("BL1-FWU: Copy operation successful.\n");
22448bfb88eSYatharth Kochar 	return 0;
22548bfb88eSYatharth Kochar }
22648bfb88eSYatharth Kochar 
22748bfb88eSYatharth Kochar /*******************************************************************************
22848bfb88eSYatharth Kochar  * This function is responsible for authenticating Normal/Secure images.
22948bfb88eSYatharth Kochar  ******************************************************************************/
23048bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id,
23148bfb88eSYatharth Kochar 			uintptr_t image_src,
23248bfb88eSYatharth Kochar 			unsigned int image_size,
23348bfb88eSYatharth Kochar 			unsigned int flags)
23448bfb88eSYatharth Kochar {
23548bfb88eSYatharth Kochar 	int result;
23648bfb88eSYatharth Kochar 	uintptr_t base_addr;
23748bfb88eSYatharth Kochar 	unsigned int total_size;
23848bfb88eSYatharth Kochar 
23948bfb88eSYatharth Kochar 	/* Get the image descriptor. */
24048bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
24148bfb88eSYatharth Kochar 	if (!image_desc)
24248bfb88eSYatharth Kochar 		return -EPERM;
24348bfb88eSYatharth Kochar 
244843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == SECURE) {
24548bfb88eSYatharth Kochar 		if (image_desc->state != IMAGE_STATE_RESET) {
24648bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication from secure world "
24748bfb88eSYatharth Kochar 				"while in invalid state\n");
24848bfb88eSYatharth Kochar 			return -EPERM;
24948bfb88eSYatharth Kochar 		}
25048bfb88eSYatharth Kochar 	} else {
251843ddee4SYatharth Kochar 		if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) {
25248bfb88eSYatharth Kochar 			if (image_desc->state != IMAGE_STATE_COPIED) {
25348bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of secure image "
25448bfb88eSYatharth Kochar 					"from non-secure world while not in copied state\n");
25548bfb88eSYatharth Kochar 				return -EPERM;
25648bfb88eSYatharth Kochar 			}
25748bfb88eSYatharth Kochar 		} else {
25848bfb88eSYatharth Kochar 			if (image_desc->state != IMAGE_STATE_RESET) {
25948bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of non-secure image "
26048bfb88eSYatharth Kochar 					"from non-secure world while in invalid state\n");
26148bfb88eSYatharth Kochar 				return -EPERM;
26248bfb88eSYatharth Kochar 			}
26348bfb88eSYatharth Kochar 		}
26448bfb88eSYatharth Kochar 	}
26548bfb88eSYatharth Kochar 
26648bfb88eSYatharth Kochar 	if (image_desc->state == IMAGE_STATE_COPIED) {
26748bfb88eSYatharth Kochar 		/*
26848bfb88eSYatharth Kochar 		 * Image is in COPIED state.
26948bfb88eSYatharth Kochar 		 * Use the stored address and size.
27048bfb88eSYatharth Kochar 		 */
27148bfb88eSYatharth Kochar 		base_addr = image_desc->image_info.image_base;
27248bfb88eSYatharth Kochar 		total_size = image_desc->image_info.image_size;
27348bfb88eSYatharth Kochar 	} else {
274949a52d2SSandrine Bailleux 		if ((!image_src) || (!image_size) ||
275949a52d2SSandrine Bailleux 		    check_uptr_overflow(image_src, image_size - 1)) {
27648bfb88eSYatharth Kochar 			WARN("BL1-FWU: Auth not allowed due to invalid"
27748bfb88eSYatharth Kochar 				" image source/size\n");
27848bfb88eSYatharth Kochar 			return -ENOMEM;
27948bfb88eSYatharth Kochar 		}
28048bfb88eSYatharth Kochar 
28148bfb88eSYatharth Kochar 		/*
28248bfb88eSYatharth Kochar 		 * Image is in RESET state.
28348bfb88eSYatharth Kochar 		 * Check the parameters and authenticate the source image in place.
28448bfb88eSYatharth Kochar 		 */
28503131c85SDan Handley 		if (bl1_plat_mem_check(image_src, image_size,	\
28603131c85SDan Handley 					image_desc->ep_info.h.attr)) {
28748bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
28848bfb88eSYatharth Kochar 			return -ENOMEM;
28948bfb88eSYatharth Kochar 		}
29048bfb88eSYatharth Kochar 
29148bfb88eSYatharth Kochar 		base_addr = image_src;
29248bfb88eSYatharth Kochar 		total_size = image_size;
29348bfb88eSYatharth Kochar 
29448bfb88eSYatharth Kochar 		/* Update the image size in the descriptor. */
29548bfb88eSYatharth Kochar 		image_desc->image_info.image_size = total_size;
29648bfb88eSYatharth Kochar 	}
29748bfb88eSYatharth Kochar 
29848bfb88eSYatharth Kochar 	/*
29948bfb88eSYatharth Kochar 	 * Authenticate the image.
30048bfb88eSYatharth Kochar 	 */
30148bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
30248bfb88eSYatharth Kochar 	result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
30348bfb88eSYatharth Kochar 	if (result != 0) {
30448bfb88eSYatharth Kochar 		WARN("BL1-FWU: Authentication Failed err=%d\n", result);
30548bfb88eSYatharth Kochar 
30648bfb88eSYatharth Kochar 		/*
30748bfb88eSYatharth Kochar 		 * Authentication has failed.
30848bfb88eSYatharth Kochar 		 * Clear the memory if the image was copied.
30948bfb88eSYatharth Kochar 		 * This is to prevent an attack where this contains
31048bfb88eSYatharth Kochar 		 * some malicious code that can somehow be executed later.
31148bfb88eSYatharth Kochar 		 */
31248bfb88eSYatharth Kochar 		if (image_desc->state == IMAGE_STATE_COPIED) {
31348bfb88eSYatharth Kochar 			/* Clear the memory.*/
314308d359bSDouglas Raillard 			zero_normalmem((void *)base_addr, total_size);
31548bfb88eSYatharth Kochar 			flush_dcache_range(base_addr, total_size);
31648bfb88eSYatharth Kochar 
31748bfb88eSYatharth Kochar 			/* Indicate that image can be copied again*/
31848bfb88eSYatharth Kochar 			image_desc->state = IMAGE_STATE_RESET;
31948bfb88eSYatharth Kochar 		}
32048bfb88eSYatharth Kochar 		return -EAUTH;
32148bfb88eSYatharth Kochar 	}
32248bfb88eSYatharth Kochar 
32348bfb88eSYatharth Kochar 	/* Indicate that image is in authenticated state. */
32448bfb88eSYatharth Kochar 	image_desc->state = IMAGE_STATE_AUTHENTICATED;
32548bfb88eSYatharth Kochar 
32648bfb88eSYatharth Kochar 	/*
32748bfb88eSYatharth Kochar 	 * Flush image_info to memory so that other
32848bfb88eSYatharth Kochar 	 * secure world images can see changes.
32948bfb88eSYatharth Kochar 	 */
33048bfb88eSYatharth Kochar 	flush_dcache_range((unsigned long)&image_desc->image_info,
33148bfb88eSYatharth Kochar 		sizeof(image_info_t));
33248bfb88eSYatharth Kochar 
33348bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authentication was successful\n");
33448bfb88eSYatharth Kochar 
33548bfb88eSYatharth Kochar 	return 0;
33648bfb88eSYatharth Kochar }
33748bfb88eSYatharth Kochar 
33848bfb88eSYatharth Kochar /*******************************************************************************
33948bfb88eSYatharth Kochar  * This function is responsible for executing Secure images.
34048bfb88eSYatharth Kochar  ******************************************************************************/
34148bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id,
34248bfb88eSYatharth Kochar 			void **handle,
34348bfb88eSYatharth Kochar 			unsigned int flags)
34448bfb88eSYatharth Kochar {
34548bfb88eSYatharth Kochar 	/* Get the image descriptor. */
34648bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
34748bfb88eSYatharth Kochar 
34848bfb88eSYatharth Kochar 	/*
34948bfb88eSYatharth Kochar 	 * Execution is NOT allowed if:
35028955d57SDan Handley 	 * image_id is invalid OR
35148bfb88eSYatharth Kochar 	 * Caller is from Secure world OR
35248bfb88eSYatharth Kochar 	 * Image is Non-Secure OR
35348bfb88eSYatharth Kochar 	 * Image is Non-Executable OR
35448bfb88eSYatharth Kochar 	 * Image is NOT in AUTHENTICATED state.
35548bfb88eSYatharth Kochar 	 */
35648bfb88eSYatharth Kochar 	if ((!image_desc) ||
357843ddee4SYatharth Kochar 	    (GET_SECURITY_STATE(flags) == SECURE) ||
358843ddee4SYatharth Kochar 	    (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) ||
359843ddee4SYatharth Kochar 	    (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) ||
36048bfb88eSYatharth Kochar 	    (image_desc->state != IMAGE_STATE_AUTHENTICATED)) {
36148bfb88eSYatharth Kochar 		WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
36248bfb88eSYatharth Kochar 		return -EPERM;
36348bfb88eSYatharth Kochar 	}
36448bfb88eSYatharth Kochar 
36548bfb88eSYatharth Kochar 	INFO("BL1-FWU: Executing Secure image\n");
36648bfb88eSYatharth Kochar 
36748bfb88eSYatharth Kochar 	/* Save NS-EL1 system registers. */
36848bfb88eSYatharth Kochar 	cm_el1_sysregs_context_save(NON_SECURE);
36948bfb88eSYatharth Kochar 
37048bfb88eSYatharth Kochar 	/* Prepare the image for execution. */
37148bfb88eSYatharth Kochar 	bl1_prepare_next_image(image_id);
37248bfb88eSYatharth Kochar 
37348bfb88eSYatharth Kochar 	/* Update the secure image id. */
37448bfb88eSYatharth Kochar 	sec_exec_image_id = image_id;
37548bfb88eSYatharth Kochar 
37648bfb88eSYatharth Kochar 	*handle = cm_get_context(SECURE);
37748bfb88eSYatharth Kochar 	return 0;
37848bfb88eSYatharth Kochar }
37948bfb88eSYatharth Kochar 
38048bfb88eSYatharth Kochar /*******************************************************************************
38128955d57SDan Handley  * This function is responsible for resuming execution in the other security
38228955d57SDan Handley  * world
38348bfb88eSYatharth Kochar  ******************************************************************************/
38428955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param,
38548bfb88eSYatharth Kochar 			void **handle,
38648bfb88eSYatharth Kochar 			unsigned int flags)
38748bfb88eSYatharth Kochar {
38848bfb88eSYatharth Kochar 	image_desc_t *image_desc;
38948bfb88eSYatharth Kochar 	unsigned int resume_sec_state;
390843ddee4SYatharth Kochar 	unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
39148bfb88eSYatharth Kochar 
39248bfb88eSYatharth Kochar 	/* Get the image descriptor for last executed secure image id. */
39348bfb88eSYatharth Kochar 	image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
39428955d57SDan Handley 	if (caller_sec_state == NON_SECURE) {
39528955d57SDan Handley 		if (!image_desc) {
39628955d57SDan Handley 			WARN("BL1-FWU: Resume not allowed due to no available"
39728955d57SDan Handley 				"secure image\n");
39848bfb88eSYatharth Kochar 			return -EPERM;
39948bfb88eSYatharth Kochar 		}
40028955d57SDan Handley 	} else {
40128955d57SDan Handley 		/* image_desc must be valid for secure world callers */
40228955d57SDan Handley 		assert(image_desc);
40328955d57SDan Handley 	}
40428955d57SDan Handley 
405843ddee4SYatharth Kochar 	assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
406843ddee4SYatharth Kochar 	assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
40728955d57SDan Handley 
40828955d57SDan Handley 	if (caller_sec_state == SECURE) {
40928955d57SDan Handley 		assert(image_desc->state == IMAGE_STATE_EXECUTED);
41048bfb88eSYatharth Kochar 
41148bfb88eSYatharth Kochar 		/* Update the flags. */
41248bfb88eSYatharth Kochar 		image_desc->state = IMAGE_STATE_INTERRUPTED;
41348bfb88eSYatharth Kochar 		resume_sec_state = NON_SECURE;
41448bfb88eSYatharth Kochar 	} else {
41528955d57SDan Handley 		assert(image_desc->state == IMAGE_STATE_INTERRUPTED);
41648bfb88eSYatharth Kochar 
41748bfb88eSYatharth Kochar 		/* Update the flags. */
41848bfb88eSYatharth Kochar 		image_desc->state = IMAGE_STATE_EXECUTED;
41948bfb88eSYatharth Kochar 		resume_sec_state = SECURE;
42048bfb88eSYatharth Kochar 	}
42148bfb88eSYatharth Kochar 
42248bfb88eSYatharth Kochar 	/* Save the EL1 system registers of calling world. */
42328955d57SDan Handley 	cm_el1_sysregs_context_save(caller_sec_state);
42448bfb88eSYatharth Kochar 
42548bfb88eSYatharth Kochar 	/* Restore the EL1 system registers of resuming world. */
42648bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(resume_sec_state);
42748bfb88eSYatharth Kochar 
42848bfb88eSYatharth Kochar 	/* Update the next context. */
42948bfb88eSYatharth Kochar 	cm_set_next_eret_context(resume_sec_state);
43048bfb88eSYatharth Kochar 
43148bfb88eSYatharth Kochar 	INFO("BL1-FWU: Resuming %s world context\n",
43228955d57SDan Handley 		(resume_sec_state == SECURE) ? "secure" : "normal");
43348bfb88eSYatharth Kochar 
43448bfb88eSYatharth Kochar 	*handle = cm_get_context(resume_sec_state);
43548bfb88eSYatharth Kochar 	return image_param;
43648bfb88eSYatharth Kochar }
43748bfb88eSYatharth Kochar 
43848bfb88eSYatharth Kochar /*******************************************************************************
43948bfb88eSYatharth Kochar  * This function is responsible for resuming normal world context.
44048bfb88eSYatharth Kochar  ******************************************************************************/
44148bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
44248bfb88eSYatharth Kochar {
44328955d57SDan Handley 	image_desc_t *image_desc;
44448bfb88eSYatharth Kochar 
44528955d57SDan Handley 	/* Make sure caller is from the secure world */
446843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == NON_SECURE) {
44728955d57SDan Handley 		WARN("BL1-FWU: Image done not allowed from normal world\n");
44848bfb88eSYatharth Kochar 		return -EPERM;
44948bfb88eSYatharth Kochar 	}
45048bfb88eSYatharth Kochar 
45128955d57SDan Handley 	/* Get the image descriptor for last executed secure image id */
45228955d57SDan Handley 	image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
45328955d57SDan Handley 
45428955d57SDan Handley 	/* image_desc must correspond to a valid secure executing image */
45528955d57SDan Handley 	assert(image_desc);
456843ddee4SYatharth Kochar 	assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
457843ddee4SYatharth Kochar 	assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
45828955d57SDan Handley 	assert(image_desc->state == IMAGE_STATE_EXECUTED);
45928955d57SDan Handley 
46048bfb88eSYatharth Kochar 	/* Update the flags. */
46148bfb88eSYatharth Kochar 	image_desc->state = IMAGE_STATE_RESET;
46248bfb88eSYatharth Kochar 	sec_exec_image_id = INVALID_IMAGE_ID;
46348bfb88eSYatharth Kochar 
46448bfb88eSYatharth Kochar 	/*
46548bfb88eSYatharth Kochar 	 * Secure world is done so no need to save the context.
46648bfb88eSYatharth Kochar 	 * Just restore the Non-Secure context.
46748bfb88eSYatharth Kochar 	 */
46848bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(NON_SECURE);
46948bfb88eSYatharth Kochar 
47048bfb88eSYatharth Kochar 	/* Update the next context. */
47148bfb88eSYatharth Kochar 	cm_set_next_eret_context(NON_SECURE);
47248bfb88eSYatharth Kochar 
47348bfb88eSYatharth Kochar 	INFO("BL1-FWU: Resuming Normal world context\n");
47448bfb88eSYatharth Kochar 
47548bfb88eSYatharth Kochar 	*handle = cm_get_context(NON_SECURE);
47648bfb88eSYatharth Kochar 	return 0;
47748bfb88eSYatharth Kochar }
47848bfb88eSYatharth Kochar 
47948bfb88eSYatharth Kochar /*******************************************************************************
48048bfb88eSYatharth Kochar  * This function provides the opportunity for users to perform any
48148bfb88eSYatharth Kochar  * platform specific handling after the Firmware update is done.
48248bfb88eSYatharth Kochar  ******************************************************************************/
4831f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
48448bfb88eSYatharth Kochar {
48548bfb88eSYatharth Kochar 	NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
48648bfb88eSYatharth Kochar 
48748bfb88eSYatharth Kochar 	/*
48848bfb88eSYatharth Kochar 	 * Call platform done function.
48948bfb88eSYatharth Kochar 	 */
4901f37b944SDan Handley 	bl1_plat_fwu_done(client_cookie, reserved);
49148bfb88eSYatharth Kochar 	assert(0);
49248bfb88eSYatharth Kochar }
493