xref: /rk3399_ARM-atf/bl1/bl1_fwu.c (revision 1bfb706851c8606aede8f9f391afde1e5339fbf3)
148bfb88eSYatharth Kochar /*
2843ddee4SYatharth Kochar  * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved.
348bfb88eSYatharth Kochar  *
448bfb88eSYatharth Kochar  * Redistribution and use in source and binary forms, with or without
548bfb88eSYatharth Kochar  * modification, are permitted provided that the following conditions are met:
648bfb88eSYatharth Kochar  *
748bfb88eSYatharth Kochar  * Redistributions of source code must retain the above copyright notice, this
848bfb88eSYatharth Kochar  * list of conditions and the following disclaimer.
948bfb88eSYatharth Kochar  *
1048bfb88eSYatharth Kochar  * Redistributions in binary form must reproduce the above copyright notice,
1148bfb88eSYatharth Kochar  * this list of conditions and the following disclaimer in the documentation
1248bfb88eSYatharth Kochar  * and/or other materials provided with the distribution.
1348bfb88eSYatharth Kochar  *
1448bfb88eSYatharth Kochar  * Neither the name of ARM nor the names of its contributors may be used
1548bfb88eSYatharth Kochar  * to endorse or promote products derived from this software without specific
1648bfb88eSYatharth Kochar  * prior written permission.
1748bfb88eSYatharth Kochar  *
1848bfb88eSYatharth Kochar  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1948bfb88eSYatharth Kochar  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2048bfb88eSYatharth Kochar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2148bfb88eSYatharth Kochar  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2248bfb88eSYatharth Kochar  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2348bfb88eSYatharth Kochar  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2448bfb88eSYatharth Kochar  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2548bfb88eSYatharth Kochar  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2648bfb88eSYatharth Kochar  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2748bfb88eSYatharth Kochar  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2848bfb88eSYatharth Kochar  * POSSIBILITY OF SUCH DAMAGE.
2948bfb88eSYatharth Kochar  */
3048bfb88eSYatharth Kochar 
3148bfb88eSYatharth Kochar #include <assert.h>
3248bfb88eSYatharth Kochar #include <arch_helpers.h>
3348bfb88eSYatharth Kochar #include <auth_mod.h>
3448bfb88eSYatharth Kochar #include <bl1.h>
3548bfb88eSYatharth Kochar #include <bl_common.h>
3648bfb88eSYatharth Kochar #include <context.h>
3748bfb88eSYatharth Kochar #include <context_mgmt.h>
3848bfb88eSYatharth Kochar #include <debug.h>
3948bfb88eSYatharth Kochar #include <errno.h>
4048bfb88eSYatharth Kochar #include <platform.h>
4148bfb88eSYatharth Kochar #include <platform_def.h>
4248bfb88eSYatharth Kochar #include <smcc_helpers.h>
4348bfb88eSYatharth Kochar #include <string.h>
4448bfb88eSYatharth Kochar #include "bl1_private.h"
4548bfb88eSYatharth Kochar 
4648bfb88eSYatharth Kochar /*
4748bfb88eSYatharth Kochar  * Function declarations.
4848bfb88eSYatharth Kochar  */
4948bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id,
5048bfb88eSYatharth Kochar 			uintptr_t image_addr,
5148bfb88eSYatharth Kochar 			unsigned int block_size,
5248bfb88eSYatharth Kochar 			unsigned int image_size,
5348bfb88eSYatharth Kochar 			unsigned int flags);
5448bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id,
5548bfb88eSYatharth Kochar 			uintptr_t image_addr,
5648bfb88eSYatharth Kochar 			unsigned int image_size,
5748bfb88eSYatharth Kochar 			unsigned int flags);
5848bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id,
5948bfb88eSYatharth Kochar 			void **handle,
6048bfb88eSYatharth Kochar 			unsigned int flags);
6128955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param,
6248bfb88eSYatharth Kochar 			void **handle,
6348bfb88eSYatharth Kochar 			unsigned int flags);
6448bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle,
6548bfb88eSYatharth Kochar 			unsigned int flags);
661f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved);
6748bfb88eSYatharth Kochar 
6848bfb88eSYatharth Kochar /*
6948bfb88eSYatharth Kochar  * This keeps track of last executed secure image id.
7048bfb88eSYatharth Kochar  */
7148bfb88eSYatharth Kochar static unsigned int sec_exec_image_id = INVALID_IMAGE_ID;
7248bfb88eSYatharth Kochar 
7348bfb88eSYatharth Kochar /*******************************************************************************
7448bfb88eSYatharth Kochar  * Top level handler for servicing FWU SMCs.
7548bfb88eSYatharth Kochar  ******************************************************************************/
7648bfb88eSYatharth Kochar register_t bl1_fwu_smc_handler(unsigned int smc_fid,
7748bfb88eSYatharth Kochar 			register_t x1,
7848bfb88eSYatharth Kochar 			register_t x2,
7948bfb88eSYatharth Kochar 			register_t x3,
8048bfb88eSYatharth Kochar 			register_t x4,
8148bfb88eSYatharth Kochar 			void *cookie,
8248bfb88eSYatharth Kochar 			void *handle,
8348bfb88eSYatharth Kochar 			unsigned int flags)
8448bfb88eSYatharth Kochar {
8548bfb88eSYatharth Kochar 
8648bfb88eSYatharth Kochar 	switch (smc_fid) {
8748bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_COPY:
8848bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_copy(x1, x2, x3, x4, flags));
8948bfb88eSYatharth Kochar 
9048bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_AUTH:
9148bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_auth(x1, x2, x3, flags));
9248bfb88eSYatharth Kochar 
9348bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_EXECUTE:
9448bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_execute(x1, &handle, flags));
9548bfb88eSYatharth Kochar 
9648bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_RESUME:
9728955d57SDan Handley 		SMC_RET1(handle, bl1_fwu_image_resume(x1, &handle, flags));
9848bfb88eSYatharth Kochar 
9948bfb88eSYatharth Kochar 	case FWU_SMC_SEC_IMAGE_DONE:
10048bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
10148bfb88eSYatharth Kochar 
10248bfb88eSYatharth Kochar 	case FWU_SMC_UPDATE_DONE:
1031f37b944SDan Handley 		bl1_fwu_done((void *)x1, NULL);
10448bfb88eSYatharth Kochar 		/* We should never return from bl1_fwu_done() */
10548bfb88eSYatharth Kochar 
10648bfb88eSYatharth Kochar 	default:
10748bfb88eSYatharth Kochar 		assert(0);
10848bfb88eSYatharth Kochar 		break;
10948bfb88eSYatharth Kochar 	}
11048bfb88eSYatharth Kochar 
11148bfb88eSYatharth Kochar 	SMC_RET0(handle);
11248bfb88eSYatharth Kochar }
11348bfb88eSYatharth Kochar 
11448bfb88eSYatharth Kochar /*******************************************************************************
11548bfb88eSYatharth Kochar  * This function is responsible for copying secure images in AP Secure RAM.
11648bfb88eSYatharth Kochar  ******************************************************************************/
11748bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id,
11848bfb88eSYatharth Kochar 			uintptr_t image_src,
11948bfb88eSYatharth Kochar 			unsigned int block_size,
12048bfb88eSYatharth Kochar 			unsigned int image_size,
12148bfb88eSYatharth Kochar 			unsigned int flags)
12248bfb88eSYatharth Kochar {
1239f1489e4SSandrine Bailleux 	uintptr_t dest_addr;
124b38a9e5cSSandrine Bailleux 	unsigned int remaining;
12548bfb88eSYatharth Kochar 
12648bfb88eSYatharth Kochar 	/* Get the image descriptor. */
12748bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
1289f1489e4SSandrine Bailleux 	if (!image_desc) {
1299f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Invalid image ID %u\n", image_id);
13048bfb88eSYatharth Kochar 		return -EPERM;
13148bfb88eSYatharth Kochar 	}
13248bfb88eSYatharth Kochar 
1339f1489e4SSandrine Bailleux 	/*
1349f1489e4SSandrine Bailleux 	 * The request must originate from a non-secure caller and target a
1359f1489e4SSandrine Bailleux 	 * secure image. Any other scenario is invalid.
1369f1489e4SSandrine Bailleux 	 */
1379f1489e4SSandrine Bailleux 	if (GET_SECURITY_STATE(flags) == SECURE) {
1389f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed from secure world.\n");
1399f1489e4SSandrine Bailleux 		return -EPERM;
1409f1489e4SSandrine Bailleux 	}
1419f1489e4SSandrine Bailleux 	if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) {
1429f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
1439f1489e4SSandrine Bailleux 		return -EPERM;
1449f1489e4SSandrine Bailleux 	}
1459f1489e4SSandrine Bailleux 
1469f1489e4SSandrine Bailleux 	/* Check whether the FWU state machine is in the correct state. */
1479f1489e4SSandrine Bailleux 	if ((image_desc->state != IMAGE_STATE_RESET) &&
1489f1489e4SSandrine Bailleux 	    (image_desc->state != IMAGE_STATE_COPYING)) {
1499f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed at this point of the FWU"
1509f1489e4SSandrine Bailleux 			" process.\n");
15148bfb88eSYatharth Kochar 		return -EPERM;
15248bfb88eSYatharth Kochar 	}
15348bfb88eSYatharth Kochar 
15448bfb88eSYatharth Kochar 	if ((!image_src) || (!block_size)) {
15548bfb88eSYatharth Kochar 		WARN("BL1-FWU: Copy not allowed due to invalid image source"
15648bfb88eSYatharth Kochar 			" or block size\n");
15748bfb88eSYatharth Kochar 		return -ENOMEM;
15848bfb88eSYatharth Kochar 	}
15948bfb88eSYatharth Kochar 
16048bfb88eSYatharth Kochar 	if (image_desc->state == IMAGE_STATE_COPYING) {
161*1bfb7068SSandrine Bailleux 		/*
162*1bfb7068SSandrine Bailleux 		 * There must have been at least 1 copy operation for this image
163*1bfb7068SSandrine Bailleux 		 * previously.
164*1bfb7068SSandrine Bailleux 		 */
165*1bfb7068SSandrine Bailleux 		assert(image_desc->copied_size != 0);
166*1bfb7068SSandrine Bailleux 		/*
167*1bfb7068SSandrine Bailleux 		 * The image size must have been recorded in the 1st copy
168*1bfb7068SSandrine Bailleux 		 * operation.
169*1bfb7068SSandrine Bailleux 		 */
1709f1489e4SSandrine Bailleux 		image_size = image_desc->image_info.image_size;
171*1bfb7068SSandrine Bailleux 		assert(image_size != 0);
172*1bfb7068SSandrine Bailleux 		assert(image_desc->copied_size < image_size);
173*1bfb7068SSandrine Bailleux 
17448bfb88eSYatharth Kochar 		INFO("BL1-FWU: Continuing image copy in blocks\n");
1759f1489e4SSandrine Bailleux 	} else { /* image_desc->state == IMAGE_STATE_RESET */
1769f1489e4SSandrine Bailleux 		INFO("BL1-FWU: Initial call to copy an image\n");
17748bfb88eSYatharth Kochar 
1789f1489e4SSandrine Bailleux 		/*
1799f1489e4SSandrine Bailleux 		 * image_size is relevant only for the 1st copy request, it is
1809f1489e4SSandrine Bailleux 		 * then ignored for subsequent calls for this image.
1819f1489e4SSandrine Bailleux 		 */
18248bfb88eSYatharth Kochar 		if (!image_size) {
1839f1489e4SSandrine Bailleux 			WARN("BL1-FWU: Copy not allowed due to invalid image"
1849f1489e4SSandrine Bailleux 				" size\n");
18548bfb88eSYatharth Kochar 			return -ENOMEM;
18648bfb88eSYatharth Kochar 		}
18748bfb88eSYatharth Kochar 
18853d703a5SYatharth Kochar #if LOAD_IMAGE_V2
18953d703a5SYatharth Kochar 		/* Check that the image size to load is within limit */
19053d703a5SYatharth Kochar 		if (image_size > image_desc->image_info.image_max_size) {
19153d703a5SYatharth Kochar 			WARN("BL1-FWU: Image size out of bounds\n");
19253d703a5SYatharth Kochar 			return -ENOMEM;
19353d703a5SYatharth Kochar 		}
19453d703a5SYatharth Kochar #else
19548bfb88eSYatharth Kochar 		/* Find out how much free trusted ram remains after BL1 load */
1969f1489e4SSandrine Bailleux 		const meminfo_t *mem_layout = bl1_plat_sec_mem_layout();
19748bfb88eSYatharth Kochar 		if ((image_desc->image_info.image_base < mem_layout->free_base) ||
19848bfb88eSYatharth Kochar 			 (image_desc->image_info.image_base + image_size >
19948bfb88eSYatharth Kochar 			  mem_layout->free_base + mem_layout->free_size)) {
2009f1489e4SSandrine Bailleux 			WARN("BL1-FWU: Copy not allowed due to insufficient"
2019f1489e4SSandrine Bailleux 			     " resources.\n");
20248bfb88eSYatharth Kochar 			return -ENOMEM;
20348bfb88eSYatharth Kochar 		}
20453d703a5SYatharth Kochar #endif
20548bfb88eSYatharth Kochar 
2069f1489e4SSandrine Bailleux 		/* Save the given image size. */
20748bfb88eSYatharth Kochar 		image_desc->image_info.image_size = image_size;
20848bfb88eSYatharth Kochar 
209b38a9e5cSSandrine Bailleux 		/*
210b38a9e5cSSandrine Bailleux 		 * copied_size must be explicitly initialized here because the
211b38a9e5cSSandrine Bailleux 		 * FWU code doesn't necessarily do it when it resets the state
212b38a9e5cSSandrine Bailleux 		 * machine.
213b38a9e5cSSandrine Bailleux 		 */
214b38a9e5cSSandrine Bailleux 		image_desc->copied_size = 0;
215b38a9e5cSSandrine Bailleux 	}
216b38a9e5cSSandrine Bailleux 
217b38a9e5cSSandrine Bailleux 	/*
218b38a9e5cSSandrine Bailleux 	 * If the given block size is more than the total image size
219b38a9e5cSSandrine Bailleux 	 * then clip the former to the latter.
220b38a9e5cSSandrine Bailleux 	 */
221b38a9e5cSSandrine Bailleux 	remaining = image_size - image_desc->copied_size;
222b38a9e5cSSandrine Bailleux 	if (block_size > remaining) {
223b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Block size is too big, clipping it.\n");
224b38a9e5cSSandrine Bailleux 		block_size = remaining;
225b38a9e5cSSandrine Bailleux 	}
226b38a9e5cSSandrine Bailleux 
227b38a9e5cSSandrine Bailleux 	/* Make sure the source image is mapped in memory. */
228b38a9e5cSSandrine Bailleux 	if (bl1_plat_mem_check(image_src, block_size, flags)) {
229b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Source image is not mapped.\n");
230b38a9e5cSSandrine Bailleux 		return -ENOMEM;
231b38a9e5cSSandrine Bailleux 	}
232b38a9e5cSSandrine Bailleux 
233b38a9e5cSSandrine Bailleux 	/* Everything looks sane. Go ahead and copy the block of data. */
234b38a9e5cSSandrine Bailleux 	dest_addr = image_desc->image_info.image_base + image_desc->copied_size;
2359f1489e4SSandrine Bailleux 	memcpy((void *) dest_addr, (const void *) image_src, block_size);
2369f1489e4SSandrine Bailleux 	flush_dcache_range(dest_addr, block_size);
23748bfb88eSYatharth Kochar 
238b38a9e5cSSandrine Bailleux 	image_desc->copied_size += block_size;
239b38a9e5cSSandrine Bailleux 	image_desc->state = (block_size == remaining) ?
240b38a9e5cSSandrine Bailleux 		IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
24148bfb88eSYatharth Kochar 
242b38a9e5cSSandrine Bailleux 	INFO("BL1-FWU: Copy operation successful.\n");
24348bfb88eSYatharth Kochar 	return 0;
24448bfb88eSYatharth Kochar }
24548bfb88eSYatharth Kochar 
24648bfb88eSYatharth Kochar /*******************************************************************************
24748bfb88eSYatharth Kochar  * This function is responsible for authenticating Normal/Secure images.
24848bfb88eSYatharth Kochar  ******************************************************************************/
24948bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id,
25048bfb88eSYatharth Kochar 			uintptr_t image_src,
25148bfb88eSYatharth Kochar 			unsigned int image_size,
25248bfb88eSYatharth Kochar 			unsigned int flags)
25348bfb88eSYatharth Kochar {
25448bfb88eSYatharth Kochar 	int result;
25548bfb88eSYatharth Kochar 	uintptr_t base_addr;
25648bfb88eSYatharth Kochar 	unsigned int total_size;
25748bfb88eSYatharth Kochar 
25848bfb88eSYatharth Kochar 	/* Get the image descriptor. */
25948bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
26048bfb88eSYatharth Kochar 	if (!image_desc)
26148bfb88eSYatharth Kochar 		return -EPERM;
26248bfb88eSYatharth Kochar 
263843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == SECURE) {
26448bfb88eSYatharth Kochar 		if (image_desc->state != IMAGE_STATE_RESET) {
26548bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication from secure world "
26648bfb88eSYatharth Kochar 				"while in invalid state\n");
26748bfb88eSYatharth Kochar 			return -EPERM;
26848bfb88eSYatharth Kochar 		}
26948bfb88eSYatharth Kochar 	} else {
270843ddee4SYatharth Kochar 		if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) {
27148bfb88eSYatharth Kochar 			if (image_desc->state != IMAGE_STATE_COPIED) {
27248bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of secure image "
27348bfb88eSYatharth Kochar 					"from non-secure world while not in copied state\n");
27448bfb88eSYatharth Kochar 				return -EPERM;
27548bfb88eSYatharth Kochar 			}
27648bfb88eSYatharth Kochar 		} else {
27748bfb88eSYatharth Kochar 			if (image_desc->state != IMAGE_STATE_RESET) {
27848bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of non-secure image "
27948bfb88eSYatharth Kochar 					"from non-secure world while in invalid state\n");
28048bfb88eSYatharth Kochar 				return -EPERM;
28148bfb88eSYatharth Kochar 			}
28248bfb88eSYatharth Kochar 		}
28348bfb88eSYatharth Kochar 	}
28448bfb88eSYatharth Kochar 
28548bfb88eSYatharth Kochar 	if (image_desc->state == IMAGE_STATE_COPIED) {
28648bfb88eSYatharth Kochar 		/*
28748bfb88eSYatharth Kochar 		 * Image is in COPIED state.
28848bfb88eSYatharth Kochar 		 * Use the stored address and size.
28948bfb88eSYatharth Kochar 		 */
29048bfb88eSYatharth Kochar 		base_addr = image_desc->image_info.image_base;
29148bfb88eSYatharth Kochar 		total_size = image_desc->image_info.image_size;
29248bfb88eSYatharth Kochar 	} else {
29348bfb88eSYatharth Kochar 		if ((!image_src) || (!image_size)) {
29448bfb88eSYatharth Kochar 			WARN("BL1-FWU: Auth not allowed due to invalid"
29548bfb88eSYatharth Kochar 				" image source/size\n");
29648bfb88eSYatharth Kochar 			return -ENOMEM;
29748bfb88eSYatharth Kochar 		}
29848bfb88eSYatharth Kochar 
29948bfb88eSYatharth Kochar 		/*
30048bfb88eSYatharth Kochar 		 * Image is in RESET state.
30148bfb88eSYatharth Kochar 		 * Check the parameters and authenticate the source image in place.
30248bfb88eSYatharth Kochar 		 */
30303131c85SDan Handley 		if (bl1_plat_mem_check(image_src, image_size,	\
30403131c85SDan Handley 					image_desc->ep_info.h.attr)) {
30548bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
30648bfb88eSYatharth Kochar 			return -ENOMEM;
30748bfb88eSYatharth Kochar 		}
30848bfb88eSYatharth Kochar 
30948bfb88eSYatharth Kochar 		base_addr = image_src;
31048bfb88eSYatharth Kochar 		total_size = image_size;
31148bfb88eSYatharth Kochar 
31248bfb88eSYatharth Kochar 		/* Update the image size in the descriptor. */
31348bfb88eSYatharth Kochar 		image_desc->image_info.image_size = total_size;
31448bfb88eSYatharth Kochar 	}
31548bfb88eSYatharth Kochar 
31648bfb88eSYatharth Kochar 	/*
31748bfb88eSYatharth Kochar 	 * Authenticate the image.
31848bfb88eSYatharth Kochar 	 */
31948bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
32048bfb88eSYatharth Kochar 	result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
32148bfb88eSYatharth Kochar 	if (result != 0) {
32248bfb88eSYatharth Kochar 		WARN("BL1-FWU: Authentication Failed err=%d\n", result);
32348bfb88eSYatharth Kochar 
32448bfb88eSYatharth Kochar 		/*
32548bfb88eSYatharth Kochar 		 * Authentication has failed.
32648bfb88eSYatharth Kochar 		 * Clear the memory if the image was copied.
32748bfb88eSYatharth Kochar 		 * This is to prevent an attack where this contains
32848bfb88eSYatharth Kochar 		 * some malicious code that can somehow be executed later.
32948bfb88eSYatharth Kochar 		 */
33048bfb88eSYatharth Kochar 		if (image_desc->state == IMAGE_STATE_COPIED) {
33148bfb88eSYatharth Kochar 			/* Clear the memory.*/
33248bfb88eSYatharth Kochar 			memset((void *)base_addr, 0, total_size);
33348bfb88eSYatharth Kochar 			flush_dcache_range(base_addr, total_size);
33448bfb88eSYatharth Kochar 
33548bfb88eSYatharth Kochar 			/* Indicate that image can be copied again*/
33648bfb88eSYatharth Kochar 			image_desc->state = IMAGE_STATE_RESET;
33748bfb88eSYatharth Kochar 		}
33848bfb88eSYatharth Kochar 		return -EAUTH;
33948bfb88eSYatharth Kochar 	}
34048bfb88eSYatharth Kochar 
34148bfb88eSYatharth Kochar 	/* Indicate that image is in authenticated state. */
34248bfb88eSYatharth Kochar 	image_desc->state = IMAGE_STATE_AUTHENTICATED;
34348bfb88eSYatharth Kochar 
34448bfb88eSYatharth Kochar 	/*
34548bfb88eSYatharth Kochar 	 * Flush image_info to memory so that other
34648bfb88eSYatharth Kochar 	 * secure world images can see changes.
34748bfb88eSYatharth Kochar 	 */
34848bfb88eSYatharth Kochar 	flush_dcache_range((unsigned long)&image_desc->image_info,
34948bfb88eSYatharth Kochar 		sizeof(image_info_t));
35048bfb88eSYatharth Kochar 
35148bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authentication was successful\n");
35248bfb88eSYatharth Kochar 
35348bfb88eSYatharth Kochar 	return 0;
35448bfb88eSYatharth Kochar }
35548bfb88eSYatharth Kochar 
35648bfb88eSYatharth Kochar /*******************************************************************************
35748bfb88eSYatharth Kochar  * This function is responsible for executing Secure images.
35848bfb88eSYatharth Kochar  ******************************************************************************/
35948bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id,
36048bfb88eSYatharth Kochar 			void **handle,
36148bfb88eSYatharth Kochar 			unsigned int flags)
36248bfb88eSYatharth Kochar {
36348bfb88eSYatharth Kochar 	/* Get the image descriptor. */
36448bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
36548bfb88eSYatharth Kochar 
36648bfb88eSYatharth Kochar 	/*
36748bfb88eSYatharth Kochar 	 * Execution is NOT allowed if:
36828955d57SDan Handley 	 * image_id is invalid OR
36948bfb88eSYatharth Kochar 	 * Caller is from Secure world OR
37048bfb88eSYatharth Kochar 	 * Image is Non-Secure OR
37148bfb88eSYatharth Kochar 	 * Image is Non-Executable OR
37248bfb88eSYatharth Kochar 	 * Image is NOT in AUTHENTICATED state.
37348bfb88eSYatharth Kochar 	 */
37448bfb88eSYatharth Kochar 	if ((!image_desc) ||
375843ddee4SYatharth Kochar 	    (GET_SECURITY_STATE(flags) == SECURE) ||
376843ddee4SYatharth Kochar 	    (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) ||
377843ddee4SYatharth Kochar 	    (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) ||
37848bfb88eSYatharth Kochar 	    (image_desc->state != IMAGE_STATE_AUTHENTICATED)) {
37948bfb88eSYatharth Kochar 		WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
38048bfb88eSYatharth Kochar 		return -EPERM;
38148bfb88eSYatharth Kochar 	}
38248bfb88eSYatharth Kochar 
38348bfb88eSYatharth Kochar 	INFO("BL1-FWU: Executing Secure image\n");
38448bfb88eSYatharth Kochar 
38548bfb88eSYatharth Kochar 	/* Save NS-EL1 system registers. */
38648bfb88eSYatharth Kochar 	cm_el1_sysregs_context_save(NON_SECURE);
38748bfb88eSYatharth Kochar 
38848bfb88eSYatharth Kochar 	/* Prepare the image for execution. */
38948bfb88eSYatharth Kochar 	bl1_prepare_next_image(image_id);
39048bfb88eSYatharth Kochar 
39148bfb88eSYatharth Kochar 	/* Update the secure image id. */
39248bfb88eSYatharth Kochar 	sec_exec_image_id = image_id;
39348bfb88eSYatharth Kochar 
39448bfb88eSYatharth Kochar 	*handle = cm_get_context(SECURE);
39548bfb88eSYatharth Kochar 	return 0;
39648bfb88eSYatharth Kochar }
39748bfb88eSYatharth Kochar 
39848bfb88eSYatharth Kochar /*******************************************************************************
39928955d57SDan Handley  * This function is responsible for resuming execution in the other security
40028955d57SDan Handley  * world
40148bfb88eSYatharth Kochar  ******************************************************************************/
40228955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param,
40348bfb88eSYatharth Kochar 			void **handle,
40448bfb88eSYatharth Kochar 			unsigned int flags)
40548bfb88eSYatharth Kochar {
40648bfb88eSYatharth Kochar 	image_desc_t *image_desc;
40748bfb88eSYatharth Kochar 	unsigned int resume_sec_state;
408843ddee4SYatharth Kochar 	unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
40948bfb88eSYatharth Kochar 
41048bfb88eSYatharth Kochar 	/* Get the image descriptor for last executed secure image id. */
41148bfb88eSYatharth Kochar 	image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
41228955d57SDan Handley 	if (caller_sec_state == NON_SECURE) {
41328955d57SDan Handley 		if (!image_desc) {
41428955d57SDan Handley 			WARN("BL1-FWU: Resume not allowed due to no available"
41528955d57SDan Handley 				"secure image\n");
41648bfb88eSYatharth Kochar 			return -EPERM;
41748bfb88eSYatharth Kochar 		}
41828955d57SDan Handley 	} else {
41928955d57SDan Handley 		/* image_desc must be valid for secure world callers */
42028955d57SDan Handley 		assert(image_desc);
42128955d57SDan Handley 	}
42228955d57SDan Handley 
423843ddee4SYatharth Kochar 	assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
424843ddee4SYatharth Kochar 	assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
42528955d57SDan Handley 
42628955d57SDan Handley 	if (caller_sec_state == SECURE) {
42728955d57SDan Handley 		assert(image_desc->state == IMAGE_STATE_EXECUTED);
42848bfb88eSYatharth Kochar 
42948bfb88eSYatharth Kochar 		/* Update the flags. */
43048bfb88eSYatharth Kochar 		image_desc->state = IMAGE_STATE_INTERRUPTED;
43148bfb88eSYatharth Kochar 		resume_sec_state = NON_SECURE;
43248bfb88eSYatharth Kochar 	} else {
43328955d57SDan Handley 		assert(image_desc->state == IMAGE_STATE_INTERRUPTED);
43448bfb88eSYatharth Kochar 
43548bfb88eSYatharth Kochar 		/* Update the flags. */
43648bfb88eSYatharth Kochar 		image_desc->state = IMAGE_STATE_EXECUTED;
43748bfb88eSYatharth Kochar 		resume_sec_state = SECURE;
43848bfb88eSYatharth Kochar 	}
43948bfb88eSYatharth Kochar 
44048bfb88eSYatharth Kochar 	/* Save the EL1 system registers of calling world. */
44128955d57SDan Handley 	cm_el1_sysregs_context_save(caller_sec_state);
44248bfb88eSYatharth Kochar 
44348bfb88eSYatharth Kochar 	/* Restore the EL1 system registers of resuming world. */
44448bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(resume_sec_state);
44548bfb88eSYatharth Kochar 
44648bfb88eSYatharth Kochar 	/* Update the next context. */
44748bfb88eSYatharth Kochar 	cm_set_next_eret_context(resume_sec_state);
44848bfb88eSYatharth Kochar 
44948bfb88eSYatharth Kochar 	INFO("BL1-FWU: Resuming %s world context\n",
45028955d57SDan Handley 		(resume_sec_state == SECURE) ? "secure" : "normal");
45148bfb88eSYatharth Kochar 
45248bfb88eSYatharth Kochar 	*handle = cm_get_context(resume_sec_state);
45348bfb88eSYatharth Kochar 	return image_param;
45448bfb88eSYatharth Kochar }
45548bfb88eSYatharth Kochar 
45648bfb88eSYatharth Kochar /*******************************************************************************
45748bfb88eSYatharth Kochar  * This function is responsible for resuming normal world context.
45848bfb88eSYatharth Kochar  ******************************************************************************/
45948bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
46048bfb88eSYatharth Kochar {
46128955d57SDan Handley 	image_desc_t *image_desc;
46248bfb88eSYatharth Kochar 
46328955d57SDan Handley 	/* Make sure caller is from the secure world */
464843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == NON_SECURE) {
46528955d57SDan Handley 		WARN("BL1-FWU: Image done not allowed from normal world\n");
46648bfb88eSYatharth Kochar 		return -EPERM;
46748bfb88eSYatharth Kochar 	}
46848bfb88eSYatharth Kochar 
46928955d57SDan Handley 	/* Get the image descriptor for last executed secure image id */
47028955d57SDan Handley 	image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
47128955d57SDan Handley 
47228955d57SDan Handley 	/* image_desc must correspond to a valid secure executing image */
47328955d57SDan Handley 	assert(image_desc);
474843ddee4SYatharth Kochar 	assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
475843ddee4SYatharth Kochar 	assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
47628955d57SDan Handley 	assert(image_desc->state == IMAGE_STATE_EXECUTED);
47728955d57SDan Handley 
47848bfb88eSYatharth Kochar 	/* Update the flags. */
47948bfb88eSYatharth Kochar 	image_desc->state = IMAGE_STATE_RESET;
48048bfb88eSYatharth Kochar 	sec_exec_image_id = INVALID_IMAGE_ID;
48148bfb88eSYatharth Kochar 
48248bfb88eSYatharth Kochar 	/*
48348bfb88eSYatharth Kochar 	 * Secure world is done so no need to save the context.
48448bfb88eSYatharth Kochar 	 * Just restore the Non-Secure context.
48548bfb88eSYatharth Kochar 	 */
48648bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(NON_SECURE);
48748bfb88eSYatharth Kochar 
48848bfb88eSYatharth Kochar 	/* Update the next context. */
48948bfb88eSYatharth Kochar 	cm_set_next_eret_context(NON_SECURE);
49048bfb88eSYatharth Kochar 
49148bfb88eSYatharth Kochar 	INFO("BL1-FWU: Resuming Normal world context\n");
49248bfb88eSYatharth Kochar 
49348bfb88eSYatharth Kochar 	*handle = cm_get_context(NON_SECURE);
49448bfb88eSYatharth Kochar 	return 0;
49548bfb88eSYatharth Kochar }
49648bfb88eSYatharth Kochar 
49748bfb88eSYatharth Kochar /*******************************************************************************
49848bfb88eSYatharth Kochar  * This function provides the opportunity for users to perform any
49948bfb88eSYatharth Kochar  * platform specific handling after the Firmware update is done.
50048bfb88eSYatharth Kochar  ******************************************************************************/
5011f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
50248bfb88eSYatharth Kochar {
50348bfb88eSYatharth Kochar 	NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
50448bfb88eSYatharth Kochar 
50548bfb88eSYatharth Kochar 	/*
50648bfb88eSYatharth Kochar 	 * Call platform done function.
50748bfb88eSYatharth Kochar 	 */
5081f37b944SDan Handley 	bl1_plat_fwu_done(client_cookie, reserved);
50948bfb88eSYatharth Kochar 	assert(0);
51048bfb88eSYatharth Kochar }
511