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; 124*b38a9e5cSSandrine 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) { 1619f1489e4SSandrine Bailleux image_size = image_desc->image_info.image_size; 16248bfb88eSYatharth Kochar INFO("BL1-FWU: Continuing image copy in blocks\n"); 1639f1489e4SSandrine Bailleux } else { /* image_desc->state == IMAGE_STATE_RESET */ 1649f1489e4SSandrine Bailleux INFO("BL1-FWU: Initial call to copy an image\n"); 16548bfb88eSYatharth Kochar 1669f1489e4SSandrine Bailleux /* 1679f1489e4SSandrine Bailleux * image_size is relevant only for the 1st copy request, it is 1689f1489e4SSandrine Bailleux * then ignored for subsequent calls for this image. 1699f1489e4SSandrine Bailleux */ 17048bfb88eSYatharth Kochar if (!image_size) { 1719f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed due to invalid image" 1729f1489e4SSandrine Bailleux " size\n"); 17348bfb88eSYatharth Kochar return -ENOMEM; 17448bfb88eSYatharth Kochar } 17548bfb88eSYatharth Kochar 17653d703a5SYatharth Kochar #if LOAD_IMAGE_V2 17753d703a5SYatharth Kochar /* Check that the image size to load is within limit */ 17853d703a5SYatharth Kochar if (image_size > image_desc->image_info.image_max_size) { 17953d703a5SYatharth Kochar WARN("BL1-FWU: Image size out of bounds\n"); 18053d703a5SYatharth Kochar return -ENOMEM; 18153d703a5SYatharth Kochar } 18253d703a5SYatharth Kochar #else 18348bfb88eSYatharth Kochar /* Find out how much free trusted ram remains after BL1 load */ 1849f1489e4SSandrine Bailleux const meminfo_t *mem_layout = bl1_plat_sec_mem_layout(); 18548bfb88eSYatharth Kochar if ((image_desc->image_info.image_base < mem_layout->free_base) || 18648bfb88eSYatharth Kochar (image_desc->image_info.image_base + image_size > 18748bfb88eSYatharth Kochar mem_layout->free_base + mem_layout->free_size)) { 1889f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed due to insufficient" 1899f1489e4SSandrine Bailleux " resources.\n"); 19048bfb88eSYatharth Kochar return -ENOMEM; 19148bfb88eSYatharth Kochar } 19253d703a5SYatharth Kochar #endif 19348bfb88eSYatharth Kochar 1949f1489e4SSandrine Bailleux /* Save the given image size. */ 19548bfb88eSYatharth Kochar image_desc->image_info.image_size = image_size; 19648bfb88eSYatharth Kochar 197*b38a9e5cSSandrine Bailleux /* 198*b38a9e5cSSandrine Bailleux * copied_size must be explicitly initialized here because the 199*b38a9e5cSSandrine Bailleux * FWU code doesn't necessarily do it when it resets the state 200*b38a9e5cSSandrine Bailleux * machine. 201*b38a9e5cSSandrine Bailleux */ 202*b38a9e5cSSandrine Bailleux image_desc->copied_size = 0; 203*b38a9e5cSSandrine Bailleux } 204*b38a9e5cSSandrine Bailleux 205*b38a9e5cSSandrine Bailleux /* 206*b38a9e5cSSandrine Bailleux * If the given block size is more than the total image size 207*b38a9e5cSSandrine Bailleux * then clip the former to the latter. 208*b38a9e5cSSandrine Bailleux */ 209*b38a9e5cSSandrine Bailleux remaining = image_size - image_desc->copied_size; 210*b38a9e5cSSandrine Bailleux if (block_size > remaining) { 211*b38a9e5cSSandrine Bailleux WARN("BL1-FWU: Block size is too big, clipping it.\n"); 212*b38a9e5cSSandrine Bailleux block_size = remaining; 213*b38a9e5cSSandrine Bailleux } 214*b38a9e5cSSandrine Bailleux 215*b38a9e5cSSandrine Bailleux /* Make sure the source image is mapped in memory. */ 216*b38a9e5cSSandrine Bailleux if (bl1_plat_mem_check(image_src, block_size, flags)) { 217*b38a9e5cSSandrine Bailleux WARN("BL1-FWU: Source image is not mapped.\n"); 218*b38a9e5cSSandrine Bailleux return -ENOMEM; 219*b38a9e5cSSandrine Bailleux } 220*b38a9e5cSSandrine Bailleux 221*b38a9e5cSSandrine Bailleux /* Everything looks sane. Go ahead and copy the block of data. */ 222*b38a9e5cSSandrine Bailleux dest_addr = image_desc->image_info.image_base + image_desc->copied_size; 2239f1489e4SSandrine Bailleux memcpy((void *) dest_addr, (const void *) image_src, block_size); 2249f1489e4SSandrine Bailleux flush_dcache_range(dest_addr, block_size); 22548bfb88eSYatharth Kochar 226*b38a9e5cSSandrine Bailleux image_desc->copied_size += block_size; 227*b38a9e5cSSandrine Bailleux image_desc->state = (block_size == remaining) ? 228*b38a9e5cSSandrine Bailleux IMAGE_STATE_COPIED : IMAGE_STATE_COPYING; 22948bfb88eSYatharth Kochar 230*b38a9e5cSSandrine Bailleux INFO("BL1-FWU: Copy operation successful.\n"); 23148bfb88eSYatharth Kochar return 0; 23248bfb88eSYatharth Kochar } 23348bfb88eSYatharth Kochar 23448bfb88eSYatharth Kochar /******************************************************************************* 23548bfb88eSYatharth Kochar * This function is responsible for authenticating Normal/Secure images. 23648bfb88eSYatharth Kochar ******************************************************************************/ 23748bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id, 23848bfb88eSYatharth Kochar uintptr_t image_src, 23948bfb88eSYatharth Kochar unsigned int image_size, 24048bfb88eSYatharth Kochar unsigned int flags) 24148bfb88eSYatharth Kochar { 24248bfb88eSYatharth Kochar int result; 24348bfb88eSYatharth Kochar uintptr_t base_addr; 24448bfb88eSYatharth Kochar unsigned int total_size; 24548bfb88eSYatharth Kochar 24648bfb88eSYatharth Kochar /* Get the image descriptor. */ 24748bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 24848bfb88eSYatharth Kochar if (!image_desc) 24948bfb88eSYatharth Kochar return -EPERM; 25048bfb88eSYatharth Kochar 251843ddee4SYatharth Kochar if (GET_SECURITY_STATE(flags) == SECURE) { 25248bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_RESET) { 25348bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication from secure world " 25448bfb88eSYatharth Kochar "while in invalid state\n"); 25548bfb88eSYatharth Kochar return -EPERM; 25648bfb88eSYatharth Kochar } 25748bfb88eSYatharth Kochar } else { 258843ddee4SYatharth Kochar if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) { 25948bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_COPIED) { 26048bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication of secure image " 26148bfb88eSYatharth Kochar "from non-secure world while not in copied state\n"); 26248bfb88eSYatharth Kochar return -EPERM; 26348bfb88eSYatharth Kochar } 26448bfb88eSYatharth Kochar } else { 26548bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_RESET) { 26648bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication of non-secure image " 26748bfb88eSYatharth Kochar "from non-secure world while in invalid state\n"); 26848bfb88eSYatharth Kochar return -EPERM; 26948bfb88eSYatharth Kochar } 27048bfb88eSYatharth Kochar } 27148bfb88eSYatharth Kochar } 27248bfb88eSYatharth Kochar 27348bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPIED) { 27448bfb88eSYatharth Kochar /* 27548bfb88eSYatharth Kochar * Image is in COPIED state. 27648bfb88eSYatharth Kochar * Use the stored address and size. 27748bfb88eSYatharth Kochar */ 27848bfb88eSYatharth Kochar base_addr = image_desc->image_info.image_base; 27948bfb88eSYatharth Kochar total_size = image_desc->image_info.image_size; 28048bfb88eSYatharth Kochar } else { 28148bfb88eSYatharth Kochar if ((!image_src) || (!image_size)) { 28248bfb88eSYatharth Kochar WARN("BL1-FWU: Auth not allowed due to invalid" 28348bfb88eSYatharth Kochar " image source/size\n"); 28448bfb88eSYatharth Kochar return -ENOMEM; 28548bfb88eSYatharth Kochar } 28648bfb88eSYatharth Kochar 28748bfb88eSYatharth Kochar /* 28848bfb88eSYatharth Kochar * Image is in RESET state. 28948bfb88eSYatharth Kochar * Check the parameters and authenticate the source image in place. 29048bfb88eSYatharth Kochar */ 29103131c85SDan Handley if (bl1_plat_mem_check(image_src, image_size, \ 29203131c85SDan Handley image_desc->ep_info.h.attr)) { 29348bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication arguments source/size not mapped\n"); 29448bfb88eSYatharth Kochar return -ENOMEM; 29548bfb88eSYatharth Kochar } 29648bfb88eSYatharth Kochar 29748bfb88eSYatharth Kochar base_addr = image_src; 29848bfb88eSYatharth Kochar total_size = image_size; 29948bfb88eSYatharth Kochar 30048bfb88eSYatharth Kochar /* Update the image size in the descriptor. */ 30148bfb88eSYatharth Kochar image_desc->image_info.image_size = total_size; 30248bfb88eSYatharth Kochar } 30348bfb88eSYatharth Kochar 30448bfb88eSYatharth Kochar /* 30548bfb88eSYatharth Kochar * Authenticate the image. 30648bfb88eSYatharth Kochar */ 30748bfb88eSYatharth Kochar INFO("BL1-FWU: Authenticating image_id:%d\n", image_id); 30848bfb88eSYatharth Kochar result = auth_mod_verify_img(image_id, (void *)base_addr, total_size); 30948bfb88eSYatharth Kochar if (result != 0) { 31048bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication Failed err=%d\n", result); 31148bfb88eSYatharth Kochar 31248bfb88eSYatharth Kochar /* 31348bfb88eSYatharth Kochar * Authentication has failed. 31448bfb88eSYatharth Kochar * Clear the memory if the image was copied. 31548bfb88eSYatharth Kochar * This is to prevent an attack where this contains 31648bfb88eSYatharth Kochar * some malicious code that can somehow be executed later. 31748bfb88eSYatharth Kochar */ 31848bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPIED) { 31948bfb88eSYatharth Kochar /* Clear the memory.*/ 32048bfb88eSYatharth Kochar memset((void *)base_addr, 0, total_size); 32148bfb88eSYatharth Kochar flush_dcache_range(base_addr, total_size); 32248bfb88eSYatharth Kochar 32348bfb88eSYatharth Kochar /* Indicate that image can be copied again*/ 32448bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_RESET; 32548bfb88eSYatharth Kochar } 32648bfb88eSYatharth Kochar return -EAUTH; 32748bfb88eSYatharth Kochar } 32848bfb88eSYatharth Kochar 32948bfb88eSYatharth Kochar /* Indicate that image is in authenticated state. */ 33048bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_AUTHENTICATED; 33148bfb88eSYatharth Kochar 33248bfb88eSYatharth Kochar /* 33348bfb88eSYatharth Kochar * Flush image_info to memory so that other 33448bfb88eSYatharth Kochar * secure world images can see changes. 33548bfb88eSYatharth Kochar */ 33648bfb88eSYatharth Kochar flush_dcache_range((unsigned long)&image_desc->image_info, 33748bfb88eSYatharth Kochar sizeof(image_info_t)); 33848bfb88eSYatharth Kochar 33948bfb88eSYatharth Kochar INFO("BL1-FWU: Authentication was successful\n"); 34048bfb88eSYatharth Kochar 34148bfb88eSYatharth Kochar return 0; 34248bfb88eSYatharth Kochar } 34348bfb88eSYatharth Kochar 34448bfb88eSYatharth Kochar /******************************************************************************* 34548bfb88eSYatharth Kochar * This function is responsible for executing Secure images. 34648bfb88eSYatharth Kochar ******************************************************************************/ 34748bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id, 34848bfb88eSYatharth Kochar void **handle, 34948bfb88eSYatharth Kochar unsigned int flags) 35048bfb88eSYatharth Kochar { 35148bfb88eSYatharth Kochar /* Get the image descriptor. */ 35248bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 35348bfb88eSYatharth Kochar 35448bfb88eSYatharth Kochar /* 35548bfb88eSYatharth Kochar * Execution is NOT allowed if: 35628955d57SDan Handley * image_id is invalid OR 35748bfb88eSYatharth Kochar * Caller is from Secure world OR 35848bfb88eSYatharth Kochar * Image is Non-Secure OR 35948bfb88eSYatharth Kochar * Image is Non-Executable OR 36048bfb88eSYatharth Kochar * Image is NOT in AUTHENTICATED state. 36148bfb88eSYatharth Kochar */ 36248bfb88eSYatharth Kochar if ((!image_desc) || 363843ddee4SYatharth Kochar (GET_SECURITY_STATE(flags) == SECURE) || 364843ddee4SYatharth Kochar (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) || 365843ddee4SYatharth Kochar (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) || 36648bfb88eSYatharth Kochar (image_desc->state != IMAGE_STATE_AUTHENTICATED)) { 36748bfb88eSYatharth Kochar WARN("BL1-FWU: Execution not allowed due to invalid state/args\n"); 36848bfb88eSYatharth Kochar return -EPERM; 36948bfb88eSYatharth Kochar } 37048bfb88eSYatharth Kochar 37148bfb88eSYatharth Kochar INFO("BL1-FWU: Executing Secure image\n"); 37248bfb88eSYatharth Kochar 37348bfb88eSYatharth Kochar /* Save NS-EL1 system registers. */ 37448bfb88eSYatharth Kochar cm_el1_sysregs_context_save(NON_SECURE); 37548bfb88eSYatharth Kochar 37648bfb88eSYatharth Kochar /* Prepare the image for execution. */ 37748bfb88eSYatharth Kochar bl1_prepare_next_image(image_id); 37848bfb88eSYatharth Kochar 37948bfb88eSYatharth Kochar /* Update the secure image id. */ 38048bfb88eSYatharth Kochar sec_exec_image_id = image_id; 38148bfb88eSYatharth Kochar 38248bfb88eSYatharth Kochar *handle = cm_get_context(SECURE); 38348bfb88eSYatharth Kochar return 0; 38448bfb88eSYatharth Kochar } 38548bfb88eSYatharth Kochar 38648bfb88eSYatharth Kochar /******************************************************************************* 38728955d57SDan Handley * This function is responsible for resuming execution in the other security 38828955d57SDan Handley * world 38948bfb88eSYatharth Kochar ******************************************************************************/ 39028955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param, 39148bfb88eSYatharth Kochar void **handle, 39248bfb88eSYatharth Kochar unsigned int flags) 39348bfb88eSYatharth Kochar { 39448bfb88eSYatharth Kochar image_desc_t *image_desc; 39548bfb88eSYatharth Kochar unsigned int resume_sec_state; 396843ddee4SYatharth Kochar unsigned int caller_sec_state = GET_SECURITY_STATE(flags); 39748bfb88eSYatharth Kochar 39848bfb88eSYatharth Kochar /* Get the image descriptor for last executed secure image id. */ 39948bfb88eSYatharth Kochar image_desc = bl1_plat_get_image_desc(sec_exec_image_id); 40028955d57SDan Handley if (caller_sec_state == NON_SECURE) { 40128955d57SDan Handley if (!image_desc) { 40228955d57SDan Handley WARN("BL1-FWU: Resume not allowed due to no available" 40328955d57SDan Handley "secure image\n"); 40448bfb88eSYatharth Kochar return -EPERM; 40548bfb88eSYatharth Kochar } 40628955d57SDan Handley } else { 40728955d57SDan Handley /* image_desc must be valid for secure world callers */ 40828955d57SDan Handley assert(image_desc); 40928955d57SDan Handley } 41028955d57SDan Handley 411843ddee4SYatharth Kochar assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE); 412843ddee4SYatharth Kochar assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE); 41328955d57SDan Handley 41428955d57SDan Handley if (caller_sec_state == SECURE) { 41528955d57SDan Handley assert(image_desc->state == IMAGE_STATE_EXECUTED); 41648bfb88eSYatharth Kochar 41748bfb88eSYatharth Kochar /* Update the flags. */ 41848bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_INTERRUPTED; 41948bfb88eSYatharth Kochar resume_sec_state = NON_SECURE; 42048bfb88eSYatharth Kochar } else { 42128955d57SDan Handley assert(image_desc->state == IMAGE_STATE_INTERRUPTED); 42248bfb88eSYatharth Kochar 42348bfb88eSYatharth Kochar /* Update the flags. */ 42448bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_EXECUTED; 42548bfb88eSYatharth Kochar resume_sec_state = SECURE; 42648bfb88eSYatharth Kochar } 42748bfb88eSYatharth Kochar 42848bfb88eSYatharth Kochar /* Save the EL1 system registers of calling world. */ 42928955d57SDan Handley cm_el1_sysregs_context_save(caller_sec_state); 43048bfb88eSYatharth Kochar 43148bfb88eSYatharth Kochar /* Restore the EL1 system registers of resuming world. */ 43248bfb88eSYatharth Kochar cm_el1_sysregs_context_restore(resume_sec_state); 43348bfb88eSYatharth Kochar 43448bfb88eSYatharth Kochar /* Update the next context. */ 43548bfb88eSYatharth Kochar cm_set_next_eret_context(resume_sec_state); 43648bfb88eSYatharth Kochar 43748bfb88eSYatharth Kochar INFO("BL1-FWU: Resuming %s world context\n", 43828955d57SDan Handley (resume_sec_state == SECURE) ? "secure" : "normal"); 43948bfb88eSYatharth Kochar 44048bfb88eSYatharth Kochar *handle = cm_get_context(resume_sec_state); 44148bfb88eSYatharth Kochar return image_param; 44248bfb88eSYatharth Kochar } 44348bfb88eSYatharth Kochar 44448bfb88eSYatharth Kochar /******************************************************************************* 44548bfb88eSYatharth Kochar * This function is responsible for resuming normal world context. 44648bfb88eSYatharth Kochar ******************************************************************************/ 44748bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle, unsigned int flags) 44848bfb88eSYatharth Kochar { 44928955d57SDan Handley image_desc_t *image_desc; 45048bfb88eSYatharth Kochar 45128955d57SDan Handley /* Make sure caller is from the secure world */ 452843ddee4SYatharth Kochar if (GET_SECURITY_STATE(flags) == NON_SECURE) { 45328955d57SDan Handley WARN("BL1-FWU: Image done not allowed from normal world\n"); 45448bfb88eSYatharth Kochar return -EPERM; 45548bfb88eSYatharth Kochar } 45648bfb88eSYatharth Kochar 45728955d57SDan Handley /* Get the image descriptor for last executed secure image id */ 45828955d57SDan Handley image_desc = bl1_plat_get_image_desc(sec_exec_image_id); 45928955d57SDan Handley 46028955d57SDan Handley /* image_desc must correspond to a valid secure executing image */ 46128955d57SDan Handley assert(image_desc); 462843ddee4SYatharth Kochar assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE); 463843ddee4SYatharth Kochar assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE); 46428955d57SDan Handley assert(image_desc->state == IMAGE_STATE_EXECUTED); 46528955d57SDan Handley 46648bfb88eSYatharth Kochar /* Update the flags. */ 46748bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_RESET; 46848bfb88eSYatharth Kochar sec_exec_image_id = INVALID_IMAGE_ID; 46948bfb88eSYatharth Kochar 47048bfb88eSYatharth Kochar /* 47148bfb88eSYatharth Kochar * Secure world is done so no need to save the context. 47248bfb88eSYatharth Kochar * Just restore the Non-Secure context. 47348bfb88eSYatharth Kochar */ 47448bfb88eSYatharth Kochar cm_el1_sysregs_context_restore(NON_SECURE); 47548bfb88eSYatharth Kochar 47648bfb88eSYatharth Kochar /* Update the next context. */ 47748bfb88eSYatharth Kochar cm_set_next_eret_context(NON_SECURE); 47848bfb88eSYatharth Kochar 47948bfb88eSYatharth Kochar INFO("BL1-FWU: Resuming Normal world context\n"); 48048bfb88eSYatharth Kochar 48148bfb88eSYatharth Kochar *handle = cm_get_context(NON_SECURE); 48248bfb88eSYatharth Kochar return 0; 48348bfb88eSYatharth Kochar } 48448bfb88eSYatharth Kochar 48548bfb88eSYatharth Kochar /******************************************************************************* 48648bfb88eSYatharth Kochar * This function provides the opportunity for users to perform any 48748bfb88eSYatharth Kochar * platform specific handling after the Firmware update is done. 48848bfb88eSYatharth Kochar ******************************************************************************/ 4891f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved) 49048bfb88eSYatharth Kochar { 49148bfb88eSYatharth Kochar NOTICE("BL1-FWU: *******FWU Process Completed*******\n"); 49248bfb88eSYatharth Kochar 49348bfb88eSYatharth Kochar /* 49448bfb88eSYatharth Kochar * Call platform done function. 49548bfb88eSYatharth Kochar */ 4961f37b944SDan Handley bl1_plat_fwu_done(client_cookie, reserved); 49748bfb88eSYatharth Kochar assert(0); 49848bfb88eSYatharth Kochar } 499