xref: /rk3399_ARM-atf/bl1/bl1_fwu.c (revision 308d359b260d888f024a2d26c76cd4a50789e432)
148bfb88eSYatharth Kochar /*
2*308d359bSDouglas Raillard  * Copyright (c) 2015-2017, 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>
44949a52d2SSandrine Bailleux #include <utils.h>
4548bfb88eSYatharth Kochar #include "bl1_private.h"
4648bfb88eSYatharth Kochar 
4748bfb88eSYatharth Kochar /*
4848bfb88eSYatharth Kochar  * Function declarations.
4948bfb88eSYatharth Kochar  */
5048bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id,
5148bfb88eSYatharth Kochar 			uintptr_t image_addr,
5248bfb88eSYatharth Kochar 			unsigned int block_size,
5348bfb88eSYatharth Kochar 			unsigned int image_size,
5448bfb88eSYatharth Kochar 			unsigned int flags);
5548bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id,
5648bfb88eSYatharth Kochar 			uintptr_t image_addr,
5748bfb88eSYatharth Kochar 			unsigned int image_size,
5848bfb88eSYatharth Kochar 			unsigned int flags);
5948bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id,
6048bfb88eSYatharth Kochar 			void **handle,
6148bfb88eSYatharth Kochar 			unsigned int flags);
6228955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param,
6348bfb88eSYatharth Kochar 			void **handle,
6448bfb88eSYatharth Kochar 			unsigned int flags);
6548bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle,
6648bfb88eSYatharth Kochar 			unsigned int flags);
671f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved);
6848bfb88eSYatharth Kochar 
6948bfb88eSYatharth Kochar /*
7048bfb88eSYatharth Kochar  * This keeps track of last executed secure image id.
7148bfb88eSYatharth Kochar  */
7248bfb88eSYatharth Kochar static unsigned int sec_exec_image_id = INVALID_IMAGE_ID;
7348bfb88eSYatharth Kochar 
7448bfb88eSYatharth Kochar /*******************************************************************************
7548bfb88eSYatharth Kochar  * Top level handler for servicing FWU SMCs.
7648bfb88eSYatharth Kochar  ******************************************************************************/
7748bfb88eSYatharth Kochar register_t bl1_fwu_smc_handler(unsigned int smc_fid,
7848bfb88eSYatharth Kochar 			register_t x1,
7948bfb88eSYatharth Kochar 			register_t x2,
8048bfb88eSYatharth Kochar 			register_t x3,
8148bfb88eSYatharth Kochar 			register_t x4,
8248bfb88eSYatharth Kochar 			void *cookie,
8348bfb88eSYatharth Kochar 			void *handle,
8448bfb88eSYatharth Kochar 			unsigned int flags)
8548bfb88eSYatharth Kochar {
8648bfb88eSYatharth Kochar 
8748bfb88eSYatharth Kochar 	switch (smc_fid) {
8848bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_COPY:
8948bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_copy(x1, x2, x3, x4, flags));
9048bfb88eSYatharth Kochar 
9148bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_AUTH:
9248bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_auth(x1, x2, x3, flags));
9348bfb88eSYatharth Kochar 
9448bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_EXECUTE:
9548bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_image_execute(x1, &handle, flags));
9648bfb88eSYatharth Kochar 
9748bfb88eSYatharth Kochar 	case FWU_SMC_IMAGE_RESUME:
9828955d57SDan Handley 		SMC_RET1(handle, bl1_fwu_image_resume(x1, &handle, flags));
9948bfb88eSYatharth Kochar 
10048bfb88eSYatharth Kochar 	case FWU_SMC_SEC_IMAGE_DONE:
10148bfb88eSYatharth Kochar 		SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
10248bfb88eSYatharth Kochar 
10348bfb88eSYatharth Kochar 	case FWU_SMC_UPDATE_DONE:
1041f37b944SDan Handley 		bl1_fwu_done((void *)x1, NULL);
10548bfb88eSYatharth Kochar 		/* We should never return from bl1_fwu_done() */
10648bfb88eSYatharth Kochar 
10748bfb88eSYatharth Kochar 	default:
10848bfb88eSYatharth Kochar 		assert(0);
10948bfb88eSYatharth Kochar 		break;
11048bfb88eSYatharth Kochar 	}
11148bfb88eSYatharth Kochar 
11248bfb88eSYatharth Kochar 	SMC_RET0(handle);
11348bfb88eSYatharth Kochar }
11448bfb88eSYatharth Kochar 
11548bfb88eSYatharth Kochar /*******************************************************************************
11648bfb88eSYatharth Kochar  * This function is responsible for copying secure images in AP Secure RAM.
11748bfb88eSYatharth Kochar  ******************************************************************************/
11848bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id,
11948bfb88eSYatharth Kochar 			uintptr_t image_src,
12048bfb88eSYatharth Kochar 			unsigned int block_size,
12148bfb88eSYatharth Kochar 			unsigned int image_size,
12248bfb88eSYatharth Kochar 			unsigned int flags)
12348bfb88eSYatharth Kochar {
1249f1489e4SSandrine Bailleux 	uintptr_t dest_addr;
125b38a9e5cSSandrine Bailleux 	unsigned int remaining;
12648bfb88eSYatharth Kochar 
12748bfb88eSYatharth Kochar 	/* Get the image descriptor. */
12848bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
1299f1489e4SSandrine Bailleux 	if (!image_desc) {
1309f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Invalid image ID %u\n", image_id);
13148bfb88eSYatharth Kochar 		return -EPERM;
13248bfb88eSYatharth Kochar 	}
13348bfb88eSYatharth Kochar 
1349f1489e4SSandrine Bailleux 	/*
1359f1489e4SSandrine Bailleux 	 * The request must originate from a non-secure caller and target a
1369f1489e4SSandrine Bailleux 	 * secure image. Any other scenario is invalid.
1379f1489e4SSandrine Bailleux 	 */
1389f1489e4SSandrine Bailleux 	if (GET_SECURITY_STATE(flags) == SECURE) {
1399f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed from secure world.\n");
1409f1489e4SSandrine Bailleux 		return -EPERM;
1419f1489e4SSandrine Bailleux 	}
1429f1489e4SSandrine Bailleux 	if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) {
1439f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
1449f1489e4SSandrine Bailleux 		return -EPERM;
1459f1489e4SSandrine Bailleux 	}
1469f1489e4SSandrine Bailleux 
1479f1489e4SSandrine Bailleux 	/* Check whether the FWU state machine is in the correct state. */
1489f1489e4SSandrine Bailleux 	if ((image_desc->state != IMAGE_STATE_RESET) &&
1499f1489e4SSandrine Bailleux 	    (image_desc->state != IMAGE_STATE_COPYING)) {
1509f1489e4SSandrine Bailleux 		WARN("BL1-FWU: Copy not allowed at this point of the FWU"
1519f1489e4SSandrine Bailleux 			" process.\n");
15248bfb88eSYatharth Kochar 		return -EPERM;
15348bfb88eSYatharth Kochar 	}
15448bfb88eSYatharth Kochar 
155949a52d2SSandrine Bailleux 	if ((!image_src) || (!block_size) ||
156949a52d2SSandrine Bailleux 	    check_uptr_overflow(image_src, block_size - 1)) {
15748bfb88eSYatharth Kochar 		WARN("BL1-FWU: Copy not allowed due to invalid image source"
15848bfb88eSYatharth Kochar 			" or block size\n");
15948bfb88eSYatharth Kochar 		return -ENOMEM;
16048bfb88eSYatharth Kochar 	}
16148bfb88eSYatharth Kochar 
16248bfb88eSYatharth Kochar 	if (image_desc->state == IMAGE_STATE_COPYING) {
1631bfb7068SSandrine Bailleux 		/*
1641bfb7068SSandrine Bailleux 		 * There must have been at least 1 copy operation for this image
1651bfb7068SSandrine Bailleux 		 * previously.
1661bfb7068SSandrine Bailleux 		 */
1671bfb7068SSandrine Bailleux 		assert(image_desc->copied_size != 0);
1681bfb7068SSandrine Bailleux 		/*
1691bfb7068SSandrine Bailleux 		 * The image size must have been recorded in the 1st copy
1701bfb7068SSandrine Bailleux 		 * operation.
1711bfb7068SSandrine Bailleux 		 */
1729f1489e4SSandrine Bailleux 		image_size = image_desc->image_info.image_size;
1731bfb7068SSandrine Bailleux 		assert(image_size != 0);
1741bfb7068SSandrine Bailleux 		assert(image_desc->copied_size < image_size);
1751bfb7068SSandrine Bailleux 
17648bfb88eSYatharth Kochar 		INFO("BL1-FWU: Continuing image copy in blocks\n");
1779f1489e4SSandrine Bailleux 	} else { /* image_desc->state == IMAGE_STATE_RESET */
1789f1489e4SSandrine Bailleux 		INFO("BL1-FWU: Initial call to copy an image\n");
17948bfb88eSYatharth Kochar 
1809f1489e4SSandrine Bailleux 		/*
1819f1489e4SSandrine Bailleux 		 * image_size is relevant only for the 1st copy request, it is
1829f1489e4SSandrine Bailleux 		 * then ignored for subsequent calls for this image.
1839f1489e4SSandrine Bailleux 		 */
18448bfb88eSYatharth Kochar 		if (!image_size) {
1859f1489e4SSandrine Bailleux 			WARN("BL1-FWU: Copy not allowed due to invalid image"
1869f1489e4SSandrine Bailleux 				" size\n");
18748bfb88eSYatharth Kochar 			return -ENOMEM;
18848bfb88eSYatharth Kochar 		}
18948bfb88eSYatharth Kochar 
19053d703a5SYatharth Kochar #if LOAD_IMAGE_V2
19153d703a5SYatharth Kochar 		/* Check that the image size to load is within limit */
19253d703a5SYatharth Kochar 		if (image_size > image_desc->image_info.image_max_size) {
19353d703a5SYatharth Kochar 			WARN("BL1-FWU: Image size out of bounds\n");
19453d703a5SYatharth Kochar 			return -ENOMEM;
19553d703a5SYatharth Kochar 		}
19653d703a5SYatharth Kochar #else
197949a52d2SSandrine Bailleux 		/*
198949a52d2SSandrine Bailleux 		 * Check the image will fit into the free trusted RAM after BL1
199949a52d2SSandrine Bailleux 		 * load.
200949a52d2SSandrine Bailleux 		 */
2019f1489e4SSandrine Bailleux 		const meminfo_t *mem_layout = bl1_plat_sec_mem_layout();
202949a52d2SSandrine Bailleux 		if (!is_mem_free(mem_layout->free_base, mem_layout->free_size,
203949a52d2SSandrine Bailleux 					image_desc->image_info.image_base,
204949a52d2SSandrine Bailleux 					image_size)) {
2059f1489e4SSandrine Bailleux 			WARN("BL1-FWU: Copy not allowed due to insufficient"
2069f1489e4SSandrine Bailleux 			     " resources.\n");
20748bfb88eSYatharth Kochar 			return -ENOMEM;
20848bfb88eSYatharth Kochar 		}
20953d703a5SYatharth Kochar #endif
21048bfb88eSYatharth Kochar 
2119f1489e4SSandrine Bailleux 		/* Save the given image size. */
21248bfb88eSYatharth Kochar 		image_desc->image_info.image_size = image_size;
21348bfb88eSYatharth Kochar 
214b38a9e5cSSandrine Bailleux 		/*
215b38a9e5cSSandrine Bailleux 		 * copied_size must be explicitly initialized here because the
216b38a9e5cSSandrine Bailleux 		 * FWU code doesn't necessarily do it when it resets the state
217b38a9e5cSSandrine Bailleux 		 * machine.
218b38a9e5cSSandrine Bailleux 		 */
219b38a9e5cSSandrine Bailleux 		image_desc->copied_size = 0;
220b38a9e5cSSandrine Bailleux 	}
221b38a9e5cSSandrine Bailleux 
222b38a9e5cSSandrine Bailleux 	/*
223b38a9e5cSSandrine Bailleux 	 * If the given block size is more than the total image size
224b38a9e5cSSandrine Bailleux 	 * then clip the former to the latter.
225b38a9e5cSSandrine Bailleux 	 */
226b38a9e5cSSandrine Bailleux 	remaining = image_size - image_desc->copied_size;
227b38a9e5cSSandrine Bailleux 	if (block_size > remaining) {
228b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Block size is too big, clipping it.\n");
229b38a9e5cSSandrine Bailleux 		block_size = remaining;
230b38a9e5cSSandrine Bailleux 	}
231b38a9e5cSSandrine Bailleux 
232b38a9e5cSSandrine Bailleux 	/* Make sure the source image is mapped in memory. */
233b38a9e5cSSandrine Bailleux 	if (bl1_plat_mem_check(image_src, block_size, flags)) {
234b38a9e5cSSandrine Bailleux 		WARN("BL1-FWU: Source image is not mapped.\n");
235b38a9e5cSSandrine Bailleux 		return -ENOMEM;
236b38a9e5cSSandrine Bailleux 	}
237b38a9e5cSSandrine Bailleux 
238b38a9e5cSSandrine Bailleux 	/* Everything looks sane. Go ahead and copy the block of data. */
239b38a9e5cSSandrine Bailleux 	dest_addr = image_desc->image_info.image_base + image_desc->copied_size;
2409f1489e4SSandrine Bailleux 	memcpy((void *) dest_addr, (const void *) image_src, block_size);
2419f1489e4SSandrine Bailleux 	flush_dcache_range(dest_addr, block_size);
24248bfb88eSYatharth Kochar 
243b38a9e5cSSandrine Bailleux 	image_desc->copied_size += block_size;
244b38a9e5cSSandrine Bailleux 	image_desc->state = (block_size == remaining) ?
245b38a9e5cSSandrine Bailleux 		IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
24648bfb88eSYatharth Kochar 
247b38a9e5cSSandrine Bailleux 	INFO("BL1-FWU: Copy operation successful.\n");
24848bfb88eSYatharth Kochar 	return 0;
24948bfb88eSYatharth Kochar }
25048bfb88eSYatharth Kochar 
25148bfb88eSYatharth Kochar /*******************************************************************************
25248bfb88eSYatharth Kochar  * This function is responsible for authenticating Normal/Secure images.
25348bfb88eSYatharth Kochar  ******************************************************************************/
25448bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id,
25548bfb88eSYatharth Kochar 			uintptr_t image_src,
25648bfb88eSYatharth Kochar 			unsigned int image_size,
25748bfb88eSYatharth Kochar 			unsigned int flags)
25848bfb88eSYatharth Kochar {
25948bfb88eSYatharth Kochar 	int result;
26048bfb88eSYatharth Kochar 	uintptr_t base_addr;
26148bfb88eSYatharth Kochar 	unsigned int total_size;
26248bfb88eSYatharth Kochar 
26348bfb88eSYatharth Kochar 	/* Get the image descriptor. */
26448bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
26548bfb88eSYatharth Kochar 	if (!image_desc)
26648bfb88eSYatharth Kochar 		return -EPERM;
26748bfb88eSYatharth Kochar 
268843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == SECURE) {
26948bfb88eSYatharth Kochar 		if (image_desc->state != IMAGE_STATE_RESET) {
27048bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication from secure world "
27148bfb88eSYatharth Kochar 				"while in invalid state\n");
27248bfb88eSYatharth Kochar 			return -EPERM;
27348bfb88eSYatharth Kochar 		}
27448bfb88eSYatharth Kochar 	} else {
275843ddee4SYatharth Kochar 		if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) {
27648bfb88eSYatharth Kochar 			if (image_desc->state != IMAGE_STATE_COPIED) {
27748bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of secure image "
27848bfb88eSYatharth Kochar 					"from non-secure world while not in copied state\n");
27948bfb88eSYatharth Kochar 				return -EPERM;
28048bfb88eSYatharth Kochar 			}
28148bfb88eSYatharth Kochar 		} else {
28248bfb88eSYatharth Kochar 			if (image_desc->state != IMAGE_STATE_RESET) {
28348bfb88eSYatharth Kochar 				WARN("BL1-FWU: Authentication of non-secure image "
28448bfb88eSYatharth Kochar 					"from non-secure world while in invalid state\n");
28548bfb88eSYatharth Kochar 				return -EPERM;
28648bfb88eSYatharth Kochar 			}
28748bfb88eSYatharth Kochar 		}
28848bfb88eSYatharth Kochar 	}
28948bfb88eSYatharth Kochar 
29048bfb88eSYatharth Kochar 	if (image_desc->state == IMAGE_STATE_COPIED) {
29148bfb88eSYatharth Kochar 		/*
29248bfb88eSYatharth Kochar 		 * Image is in COPIED state.
29348bfb88eSYatharth Kochar 		 * Use the stored address and size.
29448bfb88eSYatharth Kochar 		 */
29548bfb88eSYatharth Kochar 		base_addr = image_desc->image_info.image_base;
29648bfb88eSYatharth Kochar 		total_size = image_desc->image_info.image_size;
29748bfb88eSYatharth Kochar 	} else {
298949a52d2SSandrine Bailleux 		if ((!image_src) || (!image_size) ||
299949a52d2SSandrine Bailleux 		    check_uptr_overflow(image_src, image_size - 1)) {
30048bfb88eSYatharth Kochar 			WARN("BL1-FWU: Auth not allowed due to invalid"
30148bfb88eSYatharth Kochar 				" image source/size\n");
30248bfb88eSYatharth Kochar 			return -ENOMEM;
30348bfb88eSYatharth Kochar 		}
30448bfb88eSYatharth Kochar 
30548bfb88eSYatharth Kochar 		/*
30648bfb88eSYatharth Kochar 		 * Image is in RESET state.
30748bfb88eSYatharth Kochar 		 * Check the parameters and authenticate the source image in place.
30848bfb88eSYatharth Kochar 		 */
30903131c85SDan Handley 		if (bl1_plat_mem_check(image_src, image_size,	\
31003131c85SDan Handley 					image_desc->ep_info.h.attr)) {
31148bfb88eSYatharth Kochar 			WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
31248bfb88eSYatharth Kochar 			return -ENOMEM;
31348bfb88eSYatharth Kochar 		}
31448bfb88eSYatharth Kochar 
31548bfb88eSYatharth Kochar 		base_addr = image_src;
31648bfb88eSYatharth Kochar 		total_size = image_size;
31748bfb88eSYatharth Kochar 
31848bfb88eSYatharth Kochar 		/* Update the image size in the descriptor. */
31948bfb88eSYatharth Kochar 		image_desc->image_info.image_size = total_size;
32048bfb88eSYatharth Kochar 	}
32148bfb88eSYatharth Kochar 
32248bfb88eSYatharth Kochar 	/*
32348bfb88eSYatharth Kochar 	 * Authenticate the image.
32448bfb88eSYatharth Kochar 	 */
32548bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
32648bfb88eSYatharth Kochar 	result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
32748bfb88eSYatharth Kochar 	if (result != 0) {
32848bfb88eSYatharth Kochar 		WARN("BL1-FWU: Authentication Failed err=%d\n", result);
32948bfb88eSYatharth Kochar 
33048bfb88eSYatharth Kochar 		/*
33148bfb88eSYatharth Kochar 		 * Authentication has failed.
33248bfb88eSYatharth Kochar 		 * Clear the memory if the image was copied.
33348bfb88eSYatharth Kochar 		 * This is to prevent an attack where this contains
33448bfb88eSYatharth Kochar 		 * some malicious code that can somehow be executed later.
33548bfb88eSYatharth Kochar 		 */
33648bfb88eSYatharth Kochar 		if (image_desc->state == IMAGE_STATE_COPIED) {
33748bfb88eSYatharth Kochar 			/* Clear the memory.*/
338*308d359bSDouglas Raillard 			zero_normalmem((void *)base_addr, total_size);
33948bfb88eSYatharth Kochar 			flush_dcache_range(base_addr, total_size);
34048bfb88eSYatharth Kochar 
34148bfb88eSYatharth Kochar 			/* Indicate that image can be copied again*/
34248bfb88eSYatharth Kochar 			image_desc->state = IMAGE_STATE_RESET;
34348bfb88eSYatharth Kochar 		}
34448bfb88eSYatharth Kochar 		return -EAUTH;
34548bfb88eSYatharth Kochar 	}
34648bfb88eSYatharth Kochar 
34748bfb88eSYatharth Kochar 	/* Indicate that image is in authenticated state. */
34848bfb88eSYatharth Kochar 	image_desc->state = IMAGE_STATE_AUTHENTICATED;
34948bfb88eSYatharth Kochar 
35048bfb88eSYatharth Kochar 	/*
35148bfb88eSYatharth Kochar 	 * Flush image_info to memory so that other
35248bfb88eSYatharth Kochar 	 * secure world images can see changes.
35348bfb88eSYatharth Kochar 	 */
35448bfb88eSYatharth Kochar 	flush_dcache_range((unsigned long)&image_desc->image_info,
35548bfb88eSYatharth Kochar 		sizeof(image_info_t));
35648bfb88eSYatharth Kochar 
35748bfb88eSYatharth Kochar 	INFO("BL1-FWU: Authentication was successful\n");
35848bfb88eSYatharth Kochar 
35948bfb88eSYatharth Kochar 	return 0;
36048bfb88eSYatharth Kochar }
36148bfb88eSYatharth Kochar 
36248bfb88eSYatharth Kochar /*******************************************************************************
36348bfb88eSYatharth Kochar  * This function is responsible for executing Secure images.
36448bfb88eSYatharth Kochar  ******************************************************************************/
36548bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id,
36648bfb88eSYatharth Kochar 			void **handle,
36748bfb88eSYatharth Kochar 			unsigned int flags)
36848bfb88eSYatharth Kochar {
36948bfb88eSYatharth Kochar 	/* Get the image descriptor. */
37048bfb88eSYatharth Kochar 	image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
37148bfb88eSYatharth Kochar 
37248bfb88eSYatharth Kochar 	/*
37348bfb88eSYatharth Kochar 	 * Execution is NOT allowed if:
37428955d57SDan Handley 	 * image_id is invalid OR
37548bfb88eSYatharth Kochar 	 * Caller is from Secure world OR
37648bfb88eSYatharth Kochar 	 * Image is Non-Secure OR
37748bfb88eSYatharth Kochar 	 * Image is Non-Executable OR
37848bfb88eSYatharth Kochar 	 * Image is NOT in AUTHENTICATED state.
37948bfb88eSYatharth Kochar 	 */
38048bfb88eSYatharth Kochar 	if ((!image_desc) ||
381843ddee4SYatharth Kochar 	    (GET_SECURITY_STATE(flags) == SECURE) ||
382843ddee4SYatharth Kochar 	    (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) ||
383843ddee4SYatharth Kochar 	    (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) ||
38448bfb88eSYatharth Kochar 	    (image_desc->state != IMAGE_STATE_AUTHENTICATED)) {
38548bfb88eSYatharth Kochar 		WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
38648bfb88eSYatharth Kochar 		return -EPERM;
38748bfb88eSYatharth Kochar 	}
38848bfb88eSYatharth Kochar 
38948bfb88eSYatharth Kochar 	INFO("BL1-FWU: Executing Secure image\n");
39048bfb88eSYatharth Kochar 
39148bfb88eSYatharth Kochar 	/* Save NS-EL1 system registers. */
39248bfb88eSYatharth Kochar 	cm_el1_sysregs_context_save(NON_SECURE);
39348bfb88eSYatharth Kochar 
39448bfb88eSYatharth Kochar 	/* Prepare the image for execution. */
39548bfb88eSYatharth Kochar 	bl1_prepare_next_image(image_id);
39648bfb88eSYatharth Kochar 
39748bfb88eSYatharth Kochar 	/* Update the secure image id. */
39848bfb88eSYatharth Kochar 	sec_exec_image_id = image_id;
39948bfb88eSYatharth Kochar 
40048bfb88eSYatharth Kochar 	*handle = cm_get_context(SECURE);
40148bfb88eSYatharth Kochar 	return 0;
40248bfb88eSYatharth Kochar }
40348bfb88eSYatharth Kochar 
40448bfb88eSYatharth Kochar /*******************************************************************************
40528955d57SDan Handley  * This function is responsible for resuming execution in the other security
40628955d57SDan Handley  * world
40748bfb88eSYatharth Kochar  ******************************************************************************/
40828955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param,
40948bfb88eSYatharth Kochar 			void **handle,
41048bfb88eSYatharth Kochar 			unsigned int flags)
41148bfb88eSYatharth Kochar {
41248bfb88eSYatharth Kochar 	image_desc_t *image_desc;
41348bfb88eSYatharth Kochar 	unsigned int resume_sec_state;
414843ddee4SYatharth Kochar 	unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
41548bfb88eSYatharth Kochar 
41648bfb88eSYatharth Kochar 	/* Get the image descriptor for last executed secure image id. */
41748bfb88eSYatharth Kochar 	image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
41828955d57SDan Handley 	if (caller_sec_state == NON_SECURE) {
41928955d57SDan Handley 		if (!image_desc) {
42028955d57SDan Handley 			WARN("BL1-FWU: Resume not allowed due to no available"
42128955d57SDan Handley 				"secure image\n");
42248bfb88eSYatharth Kochar 			return -EPERM;
42348bfb88eSYatharth Kochar 		}
42428955d57SDan Handley 	} else {
42528955d57SDan Handley 		/* image_desc must be valid for secure world callers */
42628955d57SDan Handley 		assert(image_desc);
42728955d57SDan Handley 	}
42828955d57SDan Handley 
429843ddee4SYatharth Kochar 	assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
430843ddee4SYatharth Kochar 	assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
43128955d57SDan Handley 
43228955d57SDan Handley 	if (caller_sec_state == SECURE) {
43328955d57SDan Handley 		assert(image_desc->state == IMAGE_STATE_EXECUTED);
43448bfb88eSYatharth Kochar 
43548bfb88eSYatharth Kochar 		/* Update the flags. */
43648bfb88eSYatharth Kochar 		image_desc->state = IMAGE_STATE_INTERRUPTED;
43748bfb88eSYatharth Kochar 		resume_sec_state = NON_SECURE;
43848bfb88eSYatharth Kochar 	} else {
43928955d57SDan Handley 		assert(image_desc->state == IMAGE_STATE_INTERRUPTED);
44048bfb88eSYatharth Kochar 
44148bfb88eSYatharth Kochar 		/* Update the flags. */
44248bfb88eSYatharth Kochar 		image_desc->state = IMAGE_STATE_EXECUTED;
44348bfb88eSYatharth Kochar 		resume_sec_state = SECURE;
44448bfb88eSYatharth Kochar 	}
44548bfb88eSYatharth Kochar 
44648bfb88eSYatharth Kochar 	/* Save the EL1 system registers of calling world. */
44728955d57SDan Handley 	cm_el1_sysregs_context_save(caller_sec_state);
44848bfb88eSYatharth Kochar 
44948bfb88eSYatharth Kochar 	/* Restore the EL1 system registers of resuming world. */
45048bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(resume_sec_state);
45148bfb88eSYatharth Kochar 
45248bfb88eSYatharth Kochar 	/* Update the next context. */
45348bfb88eSYatharth Kochar 	cm_set_next_eret_context(resume_sec_state);
45448bfb88eSYatharth Kochar 
45548bfb88eSYatharth Kochar 	INFO("BL1-FWU: Resuming %s world context\n",
45628955d57SDan Handley 		(resume_sec_state == SECURE) ? "secure" : "normal");
45748bfb88eSYatharth Kochar 
45848bfb88eSYatharth Kochar 	*handle = cm_get_context(resume_sec_state);
45948bfb88eSYatharth Kochar 	return image_param;
46048bfb88eSYatharth Kochar }
46148bfb88eSYatharth Kochar 
46248bfb88eSYatharth Kochar /*******************************************************************************
46348bfb88eSYatharth Kochar  * This function is responsible for resuming normal world context.
46448bfb88eSYatharth Kochar  ******************************************************************************/
46548bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
46648bfb88eSYatharth Kochar {
46728955d57SDan Handley 	image_desc_t *image_desc;
46848bfb88eSYatharth Kochar 
46928955d57SDan Handley 	/* Make sure caller is from the secure world */
470843ddee4SYatharth Kochar 	if (GET_SECURITY_STATE(flags) == NON_SECURE) {
47128955d57SDan Handley 		WARN("BL1-FWU: Image done not allowed from normal world\n");
47248bfb88eSYatharth Kochar 		return -EPERM;
47348bfb88eSYatharth Kochar 	}
47448bfb88eSYatharth Kochar 
47528955d57SDan Handley 	/* Get the image descriptor for last executed secure image id */
47628955d57SDan Handley 	image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
47728955d57SDan Handley 
47828955d57SDan Handley 	/* image_desc must correspond to a valid secure executing image */
47928955d57SDan Handley 	assert(image_desc);
480843ddee4SYatharth Kochar 	assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
481843ddee4SYatharth Kochar 	assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
48228955d57SDan Handley 	assert(image_desc->state == IMAGE_STATE_EXECUTED);
48328955d57SDan Handley 
48448bfb88eSYatharth Kochar 	/* Update the flags. */
48548bfb88eSYatharth Kochar 	image_desc->state = IMAGE_STATE_RESET;
48648bfb88eSYatharth Kochar 	sec_exec_image_id = INVALID_IMAGE_ID;
48748bfb88eSYatharth Kochar 
48848bfb88eSYatharth Kochar 	/*
48948bfb88eSYatharth Kochar 	 * Secure world is done so no need to save the context.
49048bfb88eSYatharth Kochar 	 * Just restore the Non-Secure context.
49148bfb88eSYatharth Kochar 	 */
49248bfb88eSYatharth Kochar 	cm_el1_sysregs_context_restore(NON_SECURE);
49348bfb88eSYatharth Kochar 
49448bfb88eSYatharth Kochar 	/* Update the next context. */
49548bfb88eSYatharth Kochar 	cm_set_next_eret_context(NON_SECURE);
49648bfb88eSYatharth Kochar 
49748bfb88eSYatharth Kochar 	INFO("BL1-FWU: Resuming Normal world context\n");
49848bfb88eSYatharth Kochar 
49948bfb88eSYatharth Kochar 	*handle = cm_get_context(NON_SECURE);
50048bfb88eSYatharth Kochar 	return 0;
50148bfb88eSYatharth Kochar }
50248bfb88eSYatharth Kochar 
50348bfb88eSYatharth Kochar /*******************************************************************************
50448bfb88eSYatharth Kochar  * This function provides the opportunity for users to perform any
50548bfb88eSYatharth Kochar  * platform specific handling after the Firmware update is done.
50648bfb88eSYatharth Kochar  ******************************************************************************/
5071f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
50848bfb88eSYatharth Kochar {
50948bfb88eSYatharth Kochar 	NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
51048bfb88eSYatharth Kochar 
51148bfb88eSYatharth Kochar 	/*
51248bfb88eSYatharth Kochar 	 * Call platform done function.
51348bfb88eSYatharth Kochar 	 */
5141f37b944SDan Handley 	bl1_plat_fwu_done(client_cookie, reserved);
51548bfb88eSYatharth Kochar 	assert(0);
51648bfb88eSYatharth Kochar }
517