148bfb88eSYatharth Kochar /* 248bfb88eSYatharth Kochar * Copyright (c) 2015, 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); 6148bfb88eSYatharth Kochar static register_t bl1_fwu_image_resume(unsigned int image_id, 6248bfb88eSYatharth Kochar 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); 6748bfb88eSYatharth Kochar __dead2 static void bl1_fwu_done(void *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: 9848bfb88eSYatharth Kochar SMC_RET1(handle, bl1_fwu_image_resume(x1, x2, &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: 10448bfb88eSYatharth Kochar bl1_fwu_done(cookie, 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 { 12448bfb88eSYatharth Kochar uintptr_t base_addr; 12548bfb88eSYatharth Kochar meminfo_t *mem_layout; 12648bfb88eSYatharth Kochar 12748bfb88eSYatharth Kochar /* Get the image descriptor. */ 12848bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 12948bfb88eSYatharth Kochar 13048bfb88eSYatharth Kochar /* Check if we are in correct state. */ 13148bfb88eSYatharth Kochar if ((!image_desc) || 13248bfb88eSYatharth Kochar ((image_desc->state != IMAGE_STATE_RESET) && 13348bfb88eSYatharth Kochar (image_desc->state != IMAGE_STATE_COPYING))) { 13448bfb88eSYatharth Kochar WARN("BL1-FWU: Copy not allowed due to invalid state\n"); 13548bfb88eSYatharth Kochar return -EPERM; 13648bfb88eSYatharth Kochar } 13748bfb88eSYatharth Kochar 13848bfb88eSYatharth Kochar /* Only Normal world is allowed to copy a Secure image. */ 13948bfb88eSYatharth Kochar if ((GET_SEC_STATE(flags) == SECURE) || 14048bfb88eSYatharth Kochar (GET_SEC_STATE(image_desc->ep_info.h.attr) == NON_SECURE)) { 14148bfb88eSYatharth Kochar WARN("BL1-FWU: Copy not allowed for Non-Secure " 14248bfb88eSYatharth Kochar "image from Secure-world\n"); 14348bfb88eSYatharth Kochar return -EPERM; 14448bfb88eSYatharth Kochar } 14548bfb88eSYatharth Kochar 14648bfb88eSYatharth Kochar if ((!image_src) || (!block_size)) { 14748bfb88eSYatharth Kochar WARN("BL1-FWU: Copy not allowed due to invalid image source" 14848bfb88eSYatharth Kochar " or block size\n"); 14948bfb88eSYatharth Kochar return -ENOMEM; 15048bfb88eSYatharth Kochar } 15148bfb88eSYatharth Kochar 15248bfb88eSYatharth Kochar /* Get the image base address. */ 15348bfb88eSYatharth Kochar base_addr = image_desc->image_info.image_base; 15448bfb88eSYatharth Kochar 15548bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPYING) { 15648bfb88eSYatharth Kochar /* 15748bfb88eSYatharth Kochar * If last block is more than expected then 15848bfb88eSYatharth Kochar * clip the block to the required image size. 15948bfb88eSYatharth Kochar */ 16048bfb88eSYatharth Kochar if (image_desc->image_info.copied_size + block_size > 16148bfb88eSYatharth Kochar image_desc->image_info.image_size) { 16248bfb88eSYatharth Kochar block_size = image_desc->image_info.image_size - 16348bfb88eSYatharth Kochar image_desc->image_info.copied_size; 16448bfb88eSYatharth Kochar WARN("BL1-FWU: Copy argument block_size > remaining image size." 16548bfb88eSYatharth Kochar " Clipping block_size\n"); 16648bfb88eSYatharth Kochar } 16748bfb88eSYatharth Kochar 16848bfb88eSYatharth Kochar /* Make sure the image src/size is mapped. */ 16948bfb88eSYatharth Kochar if (bl1_plat_mem_check(image_src, block_size, flags)) { 17048bfb88eSYatharth Kochar WARN("BL1-FWU: Copy arguments source/size not mapped\n"); 17148bfb88eSYatharth Kochar return -ENOMEM; 17248bfb88eSYatharth Kochar } 17348bfb88eSYatharth Kochar 17448bfb88eSYatharth Kochar INFO("BL1-FWU: Continuing image copy in blocks\n"); 17548bfb88eSYatharth Kochar 17648bfb88eSYatharth Kochar /* Copy image for given block size. */ 17748bfb88eSYatharth Kochar base_addr += image_desc->image_info.copied_size; 17848bfb88eSYatharth Kochar image_desc->image_info.copied_size += block_size; 17948bfb88eSYatharth Kochar memcpy((void *)base_addr, (const void *)image_src, block_size); 18048bfb88eSYatharth Kochar flush_dcache_range(base_addr, block_size); 18148bfb88eSYatharth Kochar 18248bfb88eSYatharth Kochar /* Update the state if last block. */ 18348bfb88eSYatharth Kochar if (image_desc->image_info.copied_size == 18448bfb88eSYatharth Kochar image_desc->image_info.image_size) { 18548bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_COPIED; 18648bfb88eSYatharth Kochar INFO("BL1-FWU: Image copy in blocks completed\n"); 18748bfb88eSYatharth Kochar } 18848bfb88eSYatharth Kochar } else { 18948bfb88eSYatharth Kochar /* This means image is in RESET state and ready to be copied. */ 19048bfb88eSYatharth Kochar INFO("BL1-FWU: Fresh call to copy an image\n"); 19148bfb88eSYatharth Kochar 19248bfb88eSYatharth Kochar if (!image_size) { 19348bfb88eSYatharth Kochar WARN("BL1-FWU: Copy not allowed due to invalid image size\n"); 19448bfb88eSYatharth Kochar return -ENOMEM; 19548bfb88eSYatharth Kochar } 19648bfb88eSYatharth Kochar 19748bfb88eSYatharth Kochar /* 19848bfb88eSYatharth Kochar * If block size is more than total size then 19948bfb88eSYatharth Kochar * assume block size as the total image size. 20048bfb88eSYatharth Kochar */ 20148bfb88eSYatharth Kochar if (block_size > image_size) { 20248bfb88eSYatharth Kochar block_size = image_size; 20348bfb88eSYatharth Kochar WARN("BL1-FWU: Copy argument block_size > image size." 20448bfb88eSYatharth Kochar " Clipping block_size\n"); 20548bfb88eSYatharth Kochar } 20648bfb88eSYatharth Kochar 20748bfb88eSYatharth Kochar /* Make sure the image src/size is mapped. */ 20848bfb88eSYatharth Kochar if (bl1_plat_mem_check(image_src, block_size, flags)) { 20948bfb88eSYatharth Kochar WARN("BL1-FWU: Copy arguments source/size not mapped\n"); 21048bfb88eSYatharth Kochar return -ENOMEM; 21148bfb88eSYatharth Kochar } 21248bfb88eSYatharth Kochar 21348bfb88eSYatharth Kochar /* Find out how much free trusted ram remains after BL1 load */ 21448bfb88eSYatharth Kochar mem_layout = bl1_plat_sec_mem_layout(); 21548bfb88eSYatharth Kochar if ((image_desc->image_info.image_base < mem_layout->free_base) || 21648bfb88eSYatharth Kochar (image_desc->image_info.image_base + image_size > 21748bfb88eSYatharth Kochar mem_layout->free_base + mem_layout->free_size)) { 21848bfb88eSYatharth Kochar WARN("BL1-FWU: Memory not available to copy\n"); 21948bfb88eSYatharth Kochar return -ENOMEM; 22048bfb88eSYatharth Kochar } 22148bfb88eSYatharth Kochar 22248bfb88eSYatharth Kochar /* Update the image size. */ 22348bfb88eSYatharth Kochar image_desc->image_info.image_size = image_size; 22448bfb88eSYatharth Kochar 22548bfb88eSYatharth Kochar /* Copy image for given size. */ 22648bfb88eSYatharth Kochar memcpy((void *)base_addr, (const void *)image_src, block_size); 22748bfb88eSYatharth Kochar flush_dcache_range(base_addr, block_size); 22848bfb88eSYatharth Kochar 22948bfb88eSYatharth Kochar /* Update the state. */ 23048bfb88eSYatharth Kochar if (block_size == image_size) { 23148bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_COPIED; 23248bfb88eSYatharth Kochar INFO("BL1-FWU: Image is copied successfully\n"); 23348bfb88eSYatharth Kochar } else { 23448bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_COPYING; 23548bfb88eSYatharth Kochar INFO("BL1-FWU: Started image copy in blocks\n"); 23648bfb88eSYatharth Kochar } 23748bfb88eSYatharth Kochar 23848bfb88eSYatharth Kochar image_desc->image_info.copied_size = block_size; 23948bfb88eSYatharth Kochar } 24048bfb88eSYatharth Kochar 24148bfb88eSYatharth Kochar return 0; 24248bfb88eSYatharth Kochar } 24348bfb88eSYatharth Kochar 24448bfb88eSYatharth Kochar /******************************************************************************* 24548bfb88eSYatharth Kochar * This function is responsible for authenticating Normal/Secure images. 24648bfb88eSYatharth Kochar ******************************************************************************/ 24748bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id, 24848bfb88eSYatharth Kochar uintptr_t image_src, 24948bfb88eSYatharth Kochar unsigned int image_size, 25048bfb88eSYatharth Kochar unsigned int flags) 25148bfb88eSYatharth Kochar { 25248bfb88eSYatharth Kochar int result; 25348bfb88eSYatharth Kochar uintptr_t base_addr; 25448bfb88eSYatharth Kochar unsigned int total_size; 25548bfb88eSYatharth Kochar 25648bfb88eSYatharth Kochar /* Get the image descriptor. */ 25748bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 25848bfb88eSYatharth Kochar if (!image_desc) 25948bfb88eSYatharth Kochar return -EPERM; 26048bfb88eSYatharth Kochar 26148bfb88eSYatharth Kochar if (GET_SEC_STATE(flags) == SECURE) { 26248bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_RESET) { 26348bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication from secure world " 26448bfb88eSYatharth Kochar "while in invalid state\n"); 26548bfb88eSYatharth Kochar return -EPERM; 26648bfb88eSYatharth Kochar } 26748bfb88eSYatharth Kochar } else { 26848bfb88eSYatharth Kochar if (GET_SEC_STATE(image_desc->ep_info.h.attr) == SECURE) { 26948bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_COPIED) { 27048bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication of secure image " 27148bfb88eSYatharth Kochar "from non-secure world while not in copied state\n"); 27248bfb88eSYatharth Kochar return -EPERM; 27348bfb88eSYatharth Kochar } 27448bfb88eSYatharth Kochar } else { 27548bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_RESET) { 27648bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication of non-secure image " 27748bfb88eSYatharth Kochar "from non-secure world while in invalid state\n"); 27848bfb88eSYatharth Kochar return -EPERM; 27948bfb88eSYatharth Kochar } 28048bfb88eSYatharth Kochar } 28148bfb88eSYatharth Kochar } 28248bfb88eSYatharth Kochar 28348bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPIED) { 28448bfb88eSYatharth Kochar /* 28548bfb88eSYatharth Kochar * Image is in COPIED state. 28648bfb88eSYatharth Kochar * Use the stored address and size. 28748bfb88eSYatharth Kochar */ 28848bfb88eSYatharth Kochar base_addr = image_desc->image_info.image_base; 28948bfb88eSYatharth Kochar total_size = image_desc->image_info.image_size; 29048bfb88eSYatharth Kochar } else { 29148bfb88eSYatharth Kochar if ((!image_src) || (!image_size)) { 29248bfb88eSYatharth Kochar WARN("BL1-FWU: Auth not allowed due to invalid" 29348bfb88eSYatharth Kochar " image source/size\n"); 29448bfb88eSYatharth Kochar return -ENOMEM; 29548bfb88eSYatharth Kochar } 29648bfb88eSYatharth Kochar 29748bfb88eSYatharth Kochar /* 29848bfb88eSYatharth Kochar * Image is in RESET state. 29948bfb88eSYatharth Kochar * Check the parameters and authenticate the source image in place. 30048bfb88eSYatharth Kochar */ 301*03131c85SDan Handley if (bl1_plat_mem_check(image_src, image_size, \ 302*03131c85SDan Handley image_desc->ep_info.h.attr)) { 30348bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication arguments source/size not mapped\n"); 30448bfb88eSYatharth Kochar return -ENOMEM; 30548bfb88eSYatharth Kochar } 30648bfb88eSYatharth Kochar 30748bfb88eSYatharth Kochar base_addr = image_src; 30848bfb88eSYatharth Kochar total_size = image_size; 30948bfb88eSYatharth Kochar 31048bfb88eSYatharth Kochar /* Update the image size in the descriptor. */ 31148bfb88eSYatharth Kochar image_desc->image_info.image_size = total_size; 31248bfb88eSYatharth Kochar } 31348bfb88eSYatharth Kochar 31448bfb88eSYatharth Kochar /* 31548bfb88eSYatharth Kochar * Authenticate the image. 31648bfb88eSYatharth Kochar */ 31748bfb88eSYatharth Kochar INFO("BL1-FWU: Authenticating image_id:%d\n", image_id); 31848bfb88eSYatharth Kochar result = auth_mod_verify_img(image_id, (void *)base_addr, total_size); 31948bfb88eSYatharth Kochar if (result != 0) { 32048bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication Failed err=%d\n", result); 32148bfb88eSYatharth Kochar 32248bfb88eSYatharth Kochar /* 32348bfb88eSYatharth Kochar * Authentication has failed. 32448bfb88eSYatharth Kochar * Clear the memory if the image was copied. 32548bfb88eSYatharth Kochar * This is to prevent an attack where this contains 32648bfb88eSYatharth Kochar * some malicious code that can somehow be executed later. 32748bfb88eSYatharth Kochar */ 32848bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPIED) { 32948bfb88eSYatharth Kochar /* Clear the memory.*/ 33048bfb88eSYatharth Kochar memset((void *)base_addr, 0, total_size); 33148bfb88eSYatharth Kochar flush_dcache_range(base_addr, total_size); 33248bfb88eSYatharth Kochar 33348bfb88eSYatharth Kochar /* Indicate that image can be copied again*/ 33448bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_RESET; 33548bfb88eSYatharth Kochar } 33648bfb88eSYatharth Kochar return -EAUTH; 33748bfb88eSYatharth Kochar } 33848bfb88eSYatharth Kochar 33948bfb88eSYatharth Kochar /* Indicate that image is in authenticated state. */ 34048bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_AUTHENTICATED; 34148bfb88eSYatharth Kochar 34248bfb88eSYatharth Kochar /* 34348bfb88eSYatharth Kochar * Flush image_info to memory so that other 34448bfb88eSYatharth Kochar * secure world images can see changes. 34548bfb88eSYatharth Kochar */ 34648bfb88eSYatharth Kochar flush_dcache_range((unsigned long)&image_desc->image_info, 34748bfb88eSYatharth Kochar sizeof(image_info_t)); 34848bfb88eSYatharth Kochar 34948bfb88eSYatharth Kochar INFO("BL1-FWU: Authentication was successful\n"); 35048bfb88eSYatharth Kochar 35148bfb88eSYatharth Kochar return 0; 35248bfb88eSYatharth Kochar } 35348bfb88eSYatharth Kochar 35448bfb88eSYatharth Kochar /******************************************************************************* 35548bfb88eSYatharth Kochar * This function is responsible for executing Secure images. 35648bfb88eSYatharth Kochar ******************************************************************************/ 35748bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id, 35848bfb88eSYatharth Kochar void **handle, 35948bfb88eSYatharth Kochar unsigned int flags) 36048bfb88eSYatharth Kochar { 36148bfb88eSYatharth Kochar /* Get the image descriptor. */ 36248bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 36348bfb88eSYatharth Kochar 36448bfb88eSYatharth Kochar /* 36548bfb88eSYatharth Kochar * Execution is NOT allowed if: 36648bfb88eSYatharth Kochar * Caller is from Secure world OR 36748bfb88eSYatharth Kochar * Image is Non-Secure OR 36848bfb88eSYatharth Kochar * Image is Non-Executable OR 36948bfb88eSYatharth Kochar * Image is NOT in AUTHENTICATED state. 37048bfb88eSYatharth Kochar */ 37148bfb88eSYatharth Kochar if ((!image_desc) || 37248bfb88eSYatharth Kochar (GET_SEC_STATE(flags) == SECURE) || 37348bfb88eSYatharth Kochar (GET_SEC_STATE(image_desc->ep_info.h.attr) == NON_SECURE) || 37448bfb88eSYatharth Kochar (GET_EXEC_STATE(image_desc->image_info.h.attr) == NON_EXECUTABLE) || 37548bfb88eSYatharth Kochar (image_desc->state != IMAGE_STATE_AUTHENTICATED)) { 37648bfb88eSYatharth Kochar WARN("BL1-FWU: Execution not allowed due to invalid state/args\n"); 37748bfb88eSYatharth Kochar return -EPERM; 37848bfb88eSYatharth Kochar } 37948bfb88eSYatharth Kochar 38048bfb88eSYatharth Kochar INFO("BL1-FWU: Executing Secure image\n"); 38148bfb88eSYatharth Kochar 38248bfb88eSYatharth Kochar /* Save NS-EL1 system registers. */ 38348bfb88eSYatharth Kochar cm_el1_sysregs_context_save(NON_SECURE); 38448bfb88eSYatharth Kochar 38548bfb88eSYatharth Kochar /* Prepare the image for execution. */ 38648bfb88eSYatharth Kochar bl1_prepare_next_image(image_id); 38748bfb88eSYatharth Kochar 38848bfb88eSYatharth Kochar /* Update the secure image id. */ 38948bfb88eSYatharth Kochar sec_exec_image_id = image_id; 39048bfb88eSYatharth Kochar 39148bfb88eSYatharth Kochar *handle = cm_get_context(SECURE); 39248bfb88eSYatharth Kochar return 0; 39348bfb88eSYatharth Kochar } 39448bfb88eSYatharth Kochar 39548bfb88eSYatharth Kochar /******************************************************************************* 39648bfb88eSYatharth Kochar * This function is responsible for resuming Secure/Non-Secure images. 39748bfb88eSYatharth Kochar ******************************************************************************/ 39848bfb88eSYatharth Kochar static register_t bl1_fwu_image_resume(unsigned int image_id, 39948bfb88eSYatharth Kochar register_t image_param, 40048bfb88eSYatharth Kochar void **handle, 40148bfb88eSYatharth Kochar unsigned int flags) 40248bfb88eSYatharth Kochar { 40348bfb88eSYatharth Kochar image_desc_t *image_desc; 40448bfb88eSYatharth Kochar unsigned int resume_sec_state; 40548bfb88eSYatharth Kochar 40648bfb88eSYatharth Kochar if (GET_SEC_STATE(flags) == SECURE) { 40748bfb88eSYatharth Kochar /* Get the image descriptor for last executed secure image id. */ 40848bfb88eSYatharth Kochar image_desc = bl1_plat_get_image_desc(sec_exec_image_id); 40948bfb88eSYatharth Kochar 41048bfb88eSYatharth Kochar if ((!image_desc) || (image_desc->state != IMAGE_STATE_EXECUTED)) { 41148bfb88eSYatharth Kochar WARN("BL1-FWU: Resume not allowed for secure image " 41248bfb88eSYatharth Kochar "due to invalid state\n"); 41348bfb88eSYatharth Kochar return -EPERM; 41448bfb88eSYatharth Kochar } 41548bfb88eSYatharth Kochar 41648bfb88eSYatharth Kochar /* Update the flags. */ 41748bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_INTERRUPTED; 41848bfb88eSYatharth Kochar resume_sec_state = NON_SECURE; 41948bfb88eSYatharth Kochar } else { 42048bfb88eSYatharth Kochar /* Get the image descriptor for image id to be resumed. */ 42148bfb88eSYatharth Kochar image_desc = bl1_plat_get_image_desc(image_id); 42248bfb88eSYatharth Kochar 42348bfb88eSYatharth Kochar /* Make sure image is secure and was interrupted. */ 42448bfb88eSYatharth Kochar if ((!image_desc) || 42548bfb88eSYatharth Kochar (GET_SEC_STATE(image_desc->ep_info.h.attr) == NON_SECURE) || 42648bfb88eSYatharth Kochar (image_desc->state != IMAGE_STATE_INTERRUPTED)) { 42748bfb88eSYatharth Kochar WARN("BL1-FWU: Resume not allowed for NS image/ invalid state\n"); 42848bfb88eSYatharth Kochar return -EPERM; 42948bfb88eSYatharth Kochar } 43048bfb88eSYatharth Kochar 43148bfb88eSYatharth Kochar /* Update the flags. */ 43248bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_EXECUTED; 43348bfb88eSYatharth Kochar resume_sec_state = SECURE; 43448bfb88eSYatharth Kochar } 43548bfb88eSYatharth Kochar 43648bfb88eSYatharth Kochar /* Save the EL1 system registers of calling world. */ 43748bfb88eSYatharth Kochar cm_el1_sysregs_context_save(GET_SEC_STATE(flags)); 43848bfb88eSYatharth Kochar 43948bfb88eSYatharth Kochar /* Restore the EL1 system registers of resuming world. */ 44048bfb88eSYatharth Kochar cm_el1_sysregs_context_restore(resume_sec_state); 44148bfb88eSYatharth Kochar 44248bfb88eSYatharth Kochar /* Update the next context. */ 44348bfb88eSYatharth Kochar cm_set_next_eret_context(resume_sec_state); 44448bfb88eSYatharth Kochar 44548bfb88eSYatharth Kochar INFO("BL1-FWU: Resuming %s world context\n", 44648bfb88eSYatharth Kochar (resume_sec_state == SECURE) ? "Secure" : "Normal"); 44748bfb88eSYatharth Kochar 44848bfb88eSYatharth Kochar *handle = cm_get_context(resume_sec_state); 44948bfb88eSYatharth Kochar return image_param; 45048bfb88eSYatharth Kochar } 45148bfb88eSYatharth Kochar 45248bfb88eSYatharth Kochar /******************************************************************************* 45348bfb88eSYatharth Kochar * This function is responsible for resuming normal world context. 45448bfb88eSYatharth Kochar ******************************************************************************/ 45548bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle, unsigned int flags) 45648bfb88eSYatharth Kochar { 45748bfb88eSYatharth Kochar 45848bfb88eSYatharth Kochar /* Get the image descriptor for last executed secure image id. */ 45948bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(sec_exec_image_id); 46048bfb88eSYatharth Kochar 46148bfb88eSYatharth Kochar /* 46248bfb88eSYatharth Kochar * Make sure caller is from secure world 46348bfb88eSYatharth Kochar * and the image is in EXECUTED state. 46448bfb88eSYatharth Kochar */ 46548bfb88eSYatharth Kochar if ((!image_desc) || 46648bfb88eSYatharth Kochar (GET_SEC_STATE(flags) == NON_SECURE) || 46748bfb88eSYatharth Kochar (image_desc->state != IMAGE_STATE_EXECUTED)) { 46848bfb88eSYatharth Kochar WARN("BL1-FWU: Done not allowed for NS caller/ invalid state\n"); 46948bfb88eSYatharth Kochar return -EPERM; 47048bfb88eSYatharth Kochar } 47148bfb88eSYatharth Kochar 47248bfb88eSYatharth Kochar /* Update the flags. */ 47348bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_RESET; 47448bfb88eSYatharth Kochar sec_exec_image_id = INVALID_IMAGE_ID; 47548bfb88eSYatharth Kochar 47648bfb88eSYatharth Kochar /* 47748bfb88eSYatharth Kochar * Secure world is done so no need to save the context. 47848bfb88eSYatharth Kochar * Just restore the Non-Secure context. 47948bfb88eSYatharth Kochar */ 48048bfb88eSYatharth Kochar cm_el1_sysregs_context_restore(NON_SECURE); 48148bfb88eSYatharth Kochar 48248bfb88eSYatharth Kochar /* Update the next context. */ 48348bfb88eSYatharth Kochar cm_set_next_eret_context(NON_SECURE); 48448bfb88eSYatharth Kochar 48548bfb88eSYatharth Kochar INFO("BL1-FWU: Resuming Normal world context\n"); 48648bfb88eSYatharth Kochar 48748bfb88eSYatharth Kochar *handle = cm_get_context(NON_SECURE); 48848bfb88eSYatharth Kochar return 0; 48948bfb88eSYatharth Kochar } 49048bfb88eSYatharth Kochar 49148bfb88eSYatharth Kochar /******************************************************************************* 49248bfb88eSYatharth Kochar * This function provides the opportunity for users to perform any 49348bfb88eSYatharth Kochar * platform specific handling after the Firmware update is done. 49448bfb88eSYatharth Kochar ******************************************************************************/ 49548bfb88eSYatharth Kochar __dead2 static void bl1_fwu_done(void *cookie, void *reserved) 49648bfb88eSYatharth Kochar { 49748bfb88eSYatharth Kochar NOTICE("BL1-FWU: *******FWU Process Completed*******\n"); 49848bfb88eSYatharth Kochar 49948bfb88eSYatharth Kochar /* 50048bfb88eSYatharth Kochar * Call platform done function. 50148bfb88eSYatharth Kochar */ 50248bfb88eSYatharth Kochar bl1_plat_fwu_done(cookie, reserved); 50348bfb88eSYatharth Kochar assert(0); 50448bfb88eSYatharth Kochar } 505