148bfb88eSYatharth Kochar /* 2308d359bSDouglas Raillard * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. 348bfb88eSYatharth Kochar * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 548bfb88eSYatharth Kochar */ 648bfb88eSYatharth Kochar 748bfb88eSYatharth Kochar #include <assert.h> 848bfb88eSYatharth Kochar #include <arch_helpers.h> 948bfb88eSYatharth Kochar #include <auth_mod.h> 1048bfb88eSYatharth Kochar #include <bl1.h> 1148bfb88eSYatharth Kochar #include <bl_common.h> 1248bfb88eSYatharth Kochar #include <context.h> 1348bfb88eSYatharth Kochar #include <context_mgmt.h> 1448bfb88eSYatharth Kochar #include <debug.h> 1548bfb88eSYatharth Kochar #include <errno.h> 1648bfb88eSYatharth Kochar #include <platform.h> 1748bfb88eSYatharth Kochar #include <platform_def.h> 1848bfb88eSYatharth Kochar #include <smcc_helpers.h> 1948bfb88eSYatharth Kochar #include <string.h> 20949a52d2SSandrine Bailleux #include <utils.h> 2148bfb88eSYatharth Kochar #include "bl1_private.h" 2248bfb88eSYatharth Kochar 2348bfb88eSYatharth Kochar /* 2448bfb88eSYatharth Kochar * Function declarations. 2548bfb88eSYatharth Kochar */ 2648bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id, 2748bfb88eSYatharth Kochar uintptr_t image_addr, 2848bfb88eSYatharth Kochar unsigned int block_size, 2948bfb88eSYatharth Kochar unsigned int image_size, 3048bfb88eSYatharth Kochar unsigned int flags); 3148bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id, 3248bfb88eSYatharth Kochar uintptr_t image_addr, 3348bfb88eSYatharth Kochar unsigned int image_size, 3448bfb88eSYatharth Kochar unsigned int flags); 3548bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id, 3648bfb88eSYatharth Kochar void **handle, 3748bfb88eSYatharth Kochar unsigned int flags); 3828955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param, 3948bfb88eSYatharth Kochar void **handle, 4048bfb88eSYatharth Kochar unsigned int flags); 4148bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle, 4248bfb88eSYatharth Kochar unsigned int flags); 431f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved); 4448bfb88eSYatharth Kochar 4548bfb88eSYatharth Kochar /* 4648bfb88eSYatharth Kochar * This keeps track of last executed secure image id. 4748bfb88eSYatharth Kochar */ 4848bfb88eSYatharth Kochar static unsigned int sec_exec_image_id = INVALID_IMAGE_ID; 4948bfb88eSYatharth Kochar 50*a4409008Sdp-arm void cm_set_next_context(void *cpu_context); 51*a4409008Sdp-arm 5248bfb88eSYatharth Kochar /******************************************************************************* 5348bfb88eSYatharth Kochar * Top level handler for servicing FWU SMCs. 5448bfb88eSYatharth Kochar ******************************************************************************/ 5548bfb88eSYatharth Kochar register_t bl1_fwu_smc_handler(unsigned int smc_fid, 5648bfb88eSYatharth Kochar register_t x1, 5748bfb88eSYatharth Kochar register_t x2, 5848bfb88eSYatharth Kochar register_t x3, 5948bfb88eSYatharth Kochar register_t x4, 6048bfb88eSYatharth Kochar void *cookie, 6148bfb88eSYatharth Kochar void *handle, 6248bfb88eSYatharth Kochar unsigned int flags) 6348bfb88eSYatharth Kochar { 6448bfb88eSYatharth Kochar 6548bfb88eSYatharth Kochar switch (smc_fid) { 6648bfb88eSYatharth Kochar case FWU_SMC_IMAGE_COPY: 6748bfb88eSYatharth Kochar SMC_RET1(handle, bl1_fwu_image_copy(x1, x2, x3, x4, flags)); 6848bfb88eSYatharth Kochar 6948bfb88eSYatharth Kochar case FWU_SMC_IMAGE_AUTH: 7048bfb88eSYatharth Kochar SMC_RET1(handle, bl1_fwu_image_auth(x1, x2, x3, flags)); 7148bfb88eSYatharth Kochar 7248bfb88eSYatharth Kochar case FWU_SMC_IMAGE_EXECUTE: 7348bfb88eSYatharth Kochar SMC_RET1(handle, bl1_fwu_image_execute(x1, &handle, flags)); 7448bfb88eSYatharth Kochar 7548bfb88eSYatharth Kochar case FWU_SMC_IMAGE_RESUME: 7628955d57SDan Handley SMC_RET1(handle, bl1_fwu_image_resume(x1, &handle, flags)); 7748bfb88eSYatharth Kochar 7848bfb88eSYatharth Kochar case FWU_SMC_SEC_IMAGE_DONE: 7948bfb88eSYatharth Kochar SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags)); 8048bfb88eSYatharth Kochar 8148bfb88eSYatharth Kochar case FWU_SMC_UPDATE_DONE: 821f37b944SDan Handley bl1_fwu_done((void *)x1, NULL); 8348bfb88eSYatharth Kochar /* We should never return from bl1_fwu_done() */ 8448bfb88eSYatharth Kochar 8548bfb88eSYatharth Kochar default: 8648bfb88eSYatharth Kochar assert(0); 8748bfb88eSYatharth Kochar break; 8848bfb88eSYatharth Kochar } 8948bfb88eSYatharth Kochar 907a317a70SAntonio Nino Diaz SMC_RET1(handle, SMC_UNK); 9148bfb88eSYatharth Kochar } 9248bfb88eSYatharth Kochar 9348bfb88eSYatharth Kochar /******************************************************************************* 9448bfb88eSYatharth Kochar * This function is responsible for copying secure images in AP Secure RAM. 9548bfb88eSYatharth Kochar ******************************************************************************/ 9648bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id, 9748bfb88eSYatharth Kochar uintptr_t image_src, 9848bfb88eSYatharth Kochar unsigned int block_size, 9948bfb88eSYatharth Kochar unsigned int image_size, 10048bfb88eSYatharth Kochar unsigned int flags) 10148bfb88eSYatharth Kochar { 1029f1489e4SSandrine Bailleux uintptr_t dest_addr; 103b38a9e5cSSandrine Bailleux unsigned int remaining; 10448bfb88eSYatharth Kochar 10548bfb88eSYatharth Kochar /* Get the image descriptor. */ 10648bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 1079f1489e4SSandrine Bailleux if (!image_desc) { 1089f1489e4SSandrine Bailleux WARN("BL1-FWU: Invalid image ID %u\n", image_id); 10948bfb88eSYatharth Kochar return -EPERM; 11048bfb88eSYatharth Kochar } 11148bfb88eSYatharth Kochar 1129f1489e4SSandrine Bailleux /* 1139f1489e4SSandrine Bailleux * The request must originate from a non-secure caller and target a 1149f1489e4SSandrine Bailleux * secure image. Any other scenario is invalid. 1159f1489e4SSandrine Bailleux */ 1169f1489e4SSandrine Bailleux if (GET_SECURITY_STATE(flags) == SECURE) { 1179f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed from secure world.\n"); 1189f1489e4SSandrine Bailleux return -EPERM; 1199f1489e4SSandrine Bailleux } 1209f1489e4SSandrine Bailleux if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) { 1219f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed for non-secure images.\n"); 1229f1489e4SSandrine Bailleux return -EPERM; 1239f1489e4SSandrine Bailleux } 1249f1489e4SSandrine Bailleux 1259f1489e4SSandrine Bailleux /* Check whether the FWU state machine is in the correct state. */ 1269f1489e4SSandrine Bailleux if ((image_desc->state != IMAGE_STATE_RESET) && 1279f1489e4SSandrine Bailleux (image_desc->state != IMAGE_STATE_COPYING)) { 1289f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed at this point of the FWU" 1299f1489e4SSandrine Bailleux " process.\n"); 13048bfb88eSYatharth Kochar return -EPERM; 13148bfb88eSYatharth Kochar } 13248bfb88eSYatharth Kochar 133949a52d2SSandrine Bailleux if ((!image_src) || (!block_size) || 134949a52d2SSandrine Bailleux check_uptr_overflow(image_src, block_size - 1)) { 13548bfb88eSYatharth Kochar WARN("BL1-FWU: Copy not allowed due to invalid image source" 13648bfb88eSYatharth Kochar " or block size\n"); 13748bfb88eSYatharth Kochar return -ENOMEM; 13848bfb88eSYatharth Kochar } 13948bfb88eSYatharth Kochar 14048bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPYING) { 1411bfb7068SSandrine Bailleux /* 1421bfb7068SSandrine Bailleux * There must have been at least 1 copy operation for this image 1431bfb7068SSandrine Bailleux * previously. 1441bfb7068SSandrine Bailleux */ 1451bfb7068SSandrine Bailleux assert(image_desc->copied_size != 0); 1461bfb7068SSandrine Bailleux /* 1471bfb7068SSandrine Bailleux * The image size must have been recorded in the 1st copy 1481bfb7068SSandrine Bailleux * operation. 1491bfb7068SSandrine Bailleux */ 1509f1489e4SSandrine Bailleux image_size = image_desc->image_info.image_size; 1511bfb7068SSandrine Bailleux assert(image_size != 0); 1521bfb7068SSandrine Bailleux assert(image_desc->copied_size < image_size); 1531bfb7068SSandrine Bailleux 15448bfb88eSYatharth Kochar INFO("BL1-FWU: Continuing image copy in blocks\n"); 1559f1489e4SSandrine Bailleux } else { /* image_desc->state == IMAGE_STATE_RESET */ 1569f1489e4SSandrine Bailleux INFO("BL1-FWU: Initial call to copy an image\n"); 15748bfb88eSYatharth Kochar 1589f1489e4SSandrine Bailleux /* 1599f1489e4SSandrine Bailleux * image_size is relevant only for the 1st copy request, it is 1609f1489e4SSandrine Bailleux * then ignored for subsequent calls for this image. 1619f1489e4SSandrine Bailleux */ 16248bfb88eSYatharth Kochar if (!image_size) { 1639f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed due to invalid image" 1649f1489e4SSandrine Bailleux " size\n"); 16548bfb88eSYatharth Kochar return -ENOMEM; 16648bfb88eSYatharth Kochar } 16748bfb88eSYatharth Kochar 16853d703a5SYatharth Kochar #if LOAD_IMAGE_V2 16953d703a5SYatharth Kochar /* Check that the image size to load is within limit */ 17053d703a5SYatharth Kochar if (image_size > image_desc->image_info.image_max_size) { 17153d703a5SYatharth Kochar WARN("BL1-FWU: Image size out of bounds\n"); 17253d703a5SYatharth Kochar return -ENOMEM; 17353d703a5SYatharth Kochar } 17453d703a5SYatharth Kochar #else 175949a52d2SSandrine Bailleux /* 176949a52d2SSandrine Bailleux * Check the image will fit into the free trusted RAM after BL1 177949a52d2SSandrine Bailleux * load. 178949a52d2SSandrine Bailleux */ 1799f1489e4SSandrine Bailleux const meminfo_t *mem_layout = bl1_plat_sec_mem_layout(); 180949a52d2SSandrine Bailleux if (!is_mem_free(mem_layout->free_base, mem_layout->free_size, 181949a52d2SSandrine Bailleux image_desc->image_info.image_base, 182949a52d2SSandrine Bailleux image_size)) { 1839f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed due to insufficient" 1849f1489e4SSandrine Bailleux " resources.\n"); 18548bfb88eSYatharth Kochar return -ENOMEM; 18648bfb88eSYatharth Kochar } 18753d703a5SYatharth Kochar #endif 18848bfb88eSYatharth Kochar 1899f1489e4SSandrine Bailleux /* Save the given image size. */ 19048bfb88eSYatharth Kochar image_desc->image_info.image_size = image_size; 19148bfb88eSYatharth Kochar 192b38a9e5cSSandrine Bailleux /* 193b38a9e5cSSandrine Bailleux * copied_size must be explicitly initialized here because the 194b38a9e5cSSandrine Bailleux * FWU code doesn't necessarily do it when it resets the state 195b38a9e5cSSandrine Bailleux * machine. 196b38a9e5cSSandrine Bailleux */ 197b38a9e5cSSandrine Bailleux image_desc->copied_size = 0; 198b38a9e5cSSandrine Bailleux } 199b38a9e5cSSandrine Bailleux 200b38a9e5cSSandrine Bailleux /* 201b38a9e5cSSandrine Bailleux * If the given block size is more than the total image size 202b38a9e5cSSandrine Bailleux * then clip the former to the latter. 203b38a9e5cSSandrine Bailleux */ 204b38a9e5cSSandrine Bailleux remaining = image_size - image_desc->copied_size; 205b38a9e5cSSandrine Bailleux if (block_size > remaining) { 206b38a9e5cSSandrine Bailleux WARN("BL1-FWU: Block size is too big, clipping it.\n"); 207b38a9e5cSSandrine Bailleux block_size = remaining; 208b38a9e5cSSandrine Bailleux } 209b38a9e5cSSandrine Bailleux 210b38a9e5cSSandrine Bailleux /* Make sure the source image is mapped in memory. */ 211b38a9e5cSSandrine Bailleux if (bl1_plat_mem_check(image_src, block_size, flags)) { 212b38a9e5cSSandrine Bailleux WARN("BL1-FWU: Source image is not mapped.\n"); 213b38a9e5cSSandrine Bailleux return -ENOMEM; 214b38a9e5cSSandrine Bailleux } 215b38a9e5cSSandrine Bailleux 216b38a9e5cSSandrine Bailleux /* Everything looks sane. Go ahead and copy the block of data. */ 217b38a9e5cSSandrine Bailleux dest_addr = image_desc->image_info.image_base + image_desc->copied_size; 2189f1489e4SSandrine Bailleux memcpy((void *) dest_addr, (const void *) image_src, block_size); 2199f1489e4SSandrine Bailleux flush_dcache_range(dest_addr, block_size); 22048bfb88eSYatharth Kochar 221b38a9e5cSSandrine Bailleux image_desc->copied_size += block_size; 222b38a9e5cSSandrine Bailleux image_desc->state = (block_size == remaining) ? 223b38a9e5cSSandrine Bailleux IMAGE_STATE_COPIED : IMAGE_STATE_COPYING; 22448bfb88eSYatharth Kochar 225b38a9e5cSSandrine Bailleux INFO("BL1-FWU: Copy operation successful.\n"); 22648bfb88eSYatharth Kochar return 0; 22748bfb88eSYatharth Kochar } 22848bfb88eSYatharth Kochar 22948bfb88eSYatharth Kochar /******************************************************************************* 23048bfb88eSYatharth Kochar * This function is responsible for authenticating Normal/Secure images. 23148bfb88eSYatharth Kochar ******************************************************************************/ 23248bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id, 23348bfb88eSYatharth Kochar uintptr_t image_src, 23448bfb88eSYatharth Kochar unsigned int image_size, 23548bfb88eSYatharth Kochar unsigned int flags) 23648bfb88eSYatharth Kochar { 23748bfb88eSYatharth Kochar int result; 23848bfb88eSYatharth Kochar uintptr_t base_addr; 23948bfb88eSYatharth Kochar unsigned int total_size; 24048bfb88eSYatharth Kochar 24148bfb88eSYatharth Kochar /* Get the image descriptor. */ 24248bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 24348bfb88eSYatharth Kochar if (!image_desc) 24448bfb88eSYatharth Kochar return -EPERM; 24548bfb88eSYatharth Kochar 246843ddee4SYatharth Kochar if (GET_SECURITY_STATE(flags) == SECURE) { 24748bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_RESET) { 24848bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication from secure world " 24948bfb88eSYatharth Kochar "while in invalid state\n"); 25048bfb88eSYatharth Kochar return -EPERM; 25148bfb88eSYatharth Kochar } 25248bfb88eSYatharth Kochar } else { 253843ddee4SYatharth Kochar if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) { 25448bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_COPIED) { 25548bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication of secure image " 25648bfb88eSYatharth Kochar "from non-secure world while not in copied state\n"); 25748bfb88eSYatharth Kochar return -EPERM; 25848bfb88eSYatharth Kochar } 25948bfb88eSYatharth Kochar } else { 26048bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_RESET) { 26148bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication of non-secure image " 26248bfb88eSYatharth Kochar "from non-secure world while in invalid state\n"); 26348bfb88eSYatharth Kochar return -EPERM; 26448bfb88eSYatharth Kochar } 26548bfb88eSYatharth Kochar } 26648bfb88eSYatharth Kochar } 26748bfb88eSYatharth Kochar 26848bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPIED) { 26948bfb88eSYatharth Kochar /* 27048bfb88eSYatharth Kochar * Image is in COPIED state. 27148bfb88eSYatharth Kochar * Use the stored address and size. 27248bfb88eSYatharth Kochar */ 27348bfb88eSYatharth Kochar base_addr = image_desc->image_info.image_base; 27448bfb88eSYatharth Kochar total_size = image_desc->image_info.image_size; 27548bfb88eSYatharth Kochar } else { 276949a52d2SSandrine Bailleux if ((!image_src) || (!image_size) || 277949a52d2SSandrine Bailleux check_uptr_overflow(image_src, image_size - 1)) { 27848bfb88eSYatharth Kochar WARN("BL1-FWU: Auth not allowed due to invalid" 27948bfb88eSYatharth Kochar " image source/size\n"); 28048bfb88eSYatharth Kochar return -ENOMEM; 28148bfb88eSYatharth Kochar } 28248bfb88eSYatharth Kochar 28348bfb88eSYatharth Kochar /* 28448bfb88eSYatharth Kochar * Image is in RESET state. 28548bfb88eSYatharth Kochar * Check the parameters and authenticate the source image in place. 28648bfb88eSYatharth Kochar */ 28703131c85SDan Handley if (bl1_plat_mem_check(image_src, image_size, \ 28803131c85SDan Handley image_desc->ep_info.h.attr)) { 28948bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication arguments source/size not mapped\n"); 29048bfb88eSYatharth Kochar return -ENOMEM; 29148bfb88eSYatharth Kochar } 29248bfb88eSYatharth Kochar 29348bfb88eSYatharth Kochar base_addr = image_src; 29448bfb88eSYatharth Kochar total_size = image_size; 29548bfb88eSYatharth Kochar 29648bfb88eSYatharth Kochar /* Update the image size in the descriptor. */ 29748bfb88eSYatharth Kochar image_desc->image_info.image_size = total_size; 29848bfb88eSYatharth Kochar } 29948bfb88eSYatharth Kochar 30048bfb88eSYatharth Kochar /* 30148bfb88eSYatharth Kochar * Authenticate the image. 30248bfb88eSYatharth Kochar */ 30348bfb88eSYatharth Kochar INFO("BL1-FWU: Authenticating image_id:%d\n", image_id); 30448bfb88eSYatharth Kochar result = auth_mod_verify_img(image_id, (void *)base_addr, total_size); 30548bfb88eSYatharth Kochar if (result != 0) { 30648bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication Failed err=%d\n", result); 30748bfb88eSYatharth Kochar 30848bfb88eSYatharth Kochar /* 30948bfb88eSYatharth Kochar * Authentication has failed. 31048bfb88eSYatharth Kochar * Clear the memory if the image was copied. 31148bfb88eSYatharth Kochar * This is to prevent an attack where this contains 31248bfb88eSYatharth Kochar * some malicious code that can somehow be executed later. 31348bfb88eSYatharth Kochar */ 31448bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPIED) { 31548bfb88eSYatharth Kochar /* Clear the memory.*/ 316308d359bSDouglas Raillard zero_normalmem((void *)base_addr, total_size); 31748bfb88eSYatharth Kochar flush_dcache_range(base_addr, total_size); 31848bfb88eSYatharth Kochar 31948bfb88eSYatharth Kochar /* Indicate that image can be copied again*/ 32048bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_RESET; 32148bfb88eSYatharth Kochar } 32248bfb88eSYatharth Kochar return -EAUTH; 32348bfb88eSYatharth Kochar } 32448bfb88eSYatharth Kochar 32548bfb88eSYatharth Kochar /* Indicate that image is in authenticated state. */ 32648bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_AUTHENTICATED; 32748bfb88eSYatharth Kochar 32848bfb88eSYatharth Kochar /* 32948bfb88eSYatharth Kochar * Flush image_info to memory so that other 33048bfb88eSYatharth Kochar * secure world images can see changes. 33148bfb88eSYatharth Kochar */ 33248bfb88eSYatharth Kochar flush_dcache_range((unsigned long)&image_desc->image_info, 33348bfb88eSYatharth Kochar sizeof(image_info_t)); 33448bfb88eSYatharth Kochar 33548bfb88eSYatharth Kochar INFO("BL1-FWU: Authentication was successful\n"); 33648bfb88eSYatharth Kochar 33748bfb88eSYatharth Kochar return 0; 33848bfb88eSYatharth Kochar } 33948bfb88eSYatharth Kochar 34048bfb88eSYatharth Kochar /******************************************************************************* 34148bfb88eSYatharth Kochar * This function is responsible for executing Secure images. 34248bfb88eSYatharth Kochar ******************************************************************************/ 34348bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id, 34448bfb88eSYatharth Kochar void **handle, 34548bfb88eSYatharth Kochar unsigned int flags) 34648bfb88eSYatharth Kochar { 34748bfb88eSYatharth Kochar /* Get the image descriptor. */ 34848bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 34948bfb88eSYatharth Kochar 35048bfb88eSYatharth Kochar /* 35148bfb88eSYatharth Kochar * Execution is NOT allowed if: 35228955d57SDan Handley * image_id is invalid OR 35348bfb88eSYatharth Kochar * Caller is from Secure world OR 35448bfb88eSYatharth Kochar * Image is Non-Secure OR 35548bfb88eSYatharth Kochar * Image is Non-Executable OR 35648bfb88eSYatharth Kochar * Image is NOT in AUTHENTICATED state. 35748bfb88eSYatharth Kochar */ 35848bfb88eSYatharth Kochar if ((!image_desc) || 359843ddee4SYatharth Kochar (GET_SECURITY_STATE(flags) == SECURE) || 360843ddee4SYatharth Kochar (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) || 361843ddee4SYatharth Kochar (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) || 36248bfb88eSYatharth Kochar (image_desc->state != IMAGE_STATE_AUTHENTICATED)) { 36348bfb88eSYatharth Kochar WARN("BL1-FWU: Execution not allowed due to invalid state/args\n"); 36448bfb88eSYatharth Kochar return -EPERM; 36548bfb88eSYatharth Kochar } 36648bfb88eSYatharth Kochar 36748bfb88eSYatharth Kochar INFO("BL1-FWU: Executing Secure image\n"); 36848bfb88eSYatharth Kochar 369*a4409008Sdp-arm #ifdef AARCH64 37048bfb88eSYatharth Kochar /* Save NS-EL1 system registers. */ 37148bfb88eSYatharth Kochar cm_el1_sysregs_context_save(NON_SECURE); 372*a4409008Sdp-arm #endif 37348bfb88eSYatharth Kochar 37448bfb88eSYatharth Kochar /* Prepare the image for execution. */ 37548bfb88eSYatharth Kochar bl1_prepare_next_image(image_id); 37648bfb88eSYatharth Kochar 37748bfb88eSYatharth Kochar /* Update the secure image id. */ 37848bfb88eSYatharth Kochar sec_exec_image_id = image_id; 37948bfb88eSYatharth Kochar 380*a4409008Sdp-arm #ifdef AARCH64 38148bfb88eSYatharth Kochar *handle = cm_get_context(SECURE); 382*a4409008Sdp-arm #else 383*a4409008Sdp-arm *handle = smc_get_ctx(SECURE); 384*a4409008Sdp-arm #endif 38548bfb88eSYatharth Kochar return 0; 38648bfb88eSYatharth Kochar } 38748bfb88eSYatharth Kochar 38848bfb88eSYatharth Kochar /******************************************************************************* 38928955d57SDan Handley * This function is responsible for resuming execution in the other security 39028955d57SDan Handley * world 39148bfb88eSYatharth Kochar ******************************************************************************/ 39228955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param, 39348bfb88eSYatharth Kochar void **handle, 39448bfb88eSYatharth Kochar unsigned int flags) 39548bfb88eSYatharth Kochar { 39648bfb88eSYatharth Kochar image_desc_t *image_desc; 39748bfb88eSYatharth Kochar unsigned int resume_sec_state; 398843ddee4SYatharth Kochar unsigned int caller_sec_state = GET_SECURITY_STATE(flags); 39948bfb88eSYatharth Kochar 40048bfb88eSYatharth Kochar /* Get the image descriptor for last executed secure image id. */ 40148bfb88eSYatharth Kochar image_desc = bl1_plat_get_image_desc(sec_exec_image_id); 40228955d57SDan Handley if (caller_sec_state == NON_SECURE) { 40328955d57SDan Handley if (!image_desc) { 40428955d57SDan Handley WARN("BL1-FWU: Resume not allowed due to no available" 40528955d57SDan Handley "secure image\n"); 40648bfb88eSYatharth Kochar return -EPERM; 40748bfb88eSYatharth Kochar } 40828955d57SDan Handley } else { 40928955d57SDan Handley /* image_desc must be valid for secure world callers */ 41028955d57SDan Handley assert(image_desc); 41128955d57SDan Handley } 41228955d57SDan Handley 413843ddee4SYatharth Kochar assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE); 414843ddee4SYatharth Kochar assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE); 41528955d57SDan Handley 41628955d57SDan Handley if (caller_sec_state == SECURE) { 41728955d57SDan Handley assert(image_desc->state == IMAGE_STATE_EXECUTED); 41848bfb88eSYatharth Kochar 41948bfb88eSYatharth Kochar /* Update the flags. */ 42048bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_INTERRUPTED; 42148bfb88eSYatharth Kochar resume_sec_state = NON_SECURE; 42248bfb88eSYatharth Kochar } else { 42328955d57SDan Handley assert(image_desc->state == IMAGE_STATE_INTERRUPTED); 42448bfb88eSYatharth Kochar 42548bfb88eSYatharth Kochar /* Update the flags. */ 42648bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_EXECUTED; 42748bfb88eSYatharth Kochar resume_sec_state = SECURE; 42848bfb88eSYatharth Kochar } 42948bfb88eSYatharth Kochar 430*a4409008Sdp-arm INFO("BL1-FWU: Resuming %s world context\n", 431*a4409008Sdp-arm (resume_sec_state == SECURE) ? "secure" : "normal"); 432*a4409008Sdp-arm 433*a4409008Sdp-arm #ifdef AARCH64 43448bfb88eSYatharth Kochar /* Save the EL1 system registers of calling world. */ 43528955d57SDan Handley cm_el1_sysregs_context_save(caller_sec_state); 43648bfb88eSYatharth Kochar 43748bfb88eSYatharth Kochar /* Restore the EL1 system registers of resuming world. */ 43848bfb88eSYatharth Kochar cm_el1_sysregs_context_restore(resume_sec_state); 43948bfb88eSYatharth Kochar 44048bfb88eSYatharth Kochar /* Update the next context. */ 44148bfb88eSYatharth Kochar cm_set_next_eret_context(resume_sec_state); 44248bfb88eSYatharth Kochar 44348bfb88eSYatharth Kochar *handle = cm_get_context(resume_sec_state); 444*a4409008Sdp-arm #else 445*a4409008Sdp-arm /* Update the next context. */ 446*a4409008Sdp-arm cm_set_next_context(cm_get_context(resume_sec_state)); 447*a4409008Sdp-arm 448*a4409008Sdp-arm /* Prepare the smc context for the next BL image. */ 449*a4409008Sdp-arm smc_set_next_ctx(resume_sec_state); 450*a4409008Sdp-arm 451*a4409008Sdp-arm *handle = smc_get_ctx(resume_sec_state); 452*a4409008Sdp-arm #endif 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 482*a4409008Sdp-arm INFO("BL1-FWU: Resuming Normal world context\n"); 483*a4409008Sdp-arm #ifdef AARCH64 48448bfb88eSYatharth Kochar /* 48548bfb88eSYatharth Kochar * Secure world is done so no need to save the context. 48648bfb88eSYatharth Kochar * Just restore the Non-Secure context. 48748bfb88eSYatharth Kochar */ 48848bfb88eSYatharth Kochar cm_el1_sysregs_context_restore(NON_SECURE); 48948bfb88eSYatharth Kochar 49048bfb88eSYatharth Kochar /* Update the next context. */ 49148bfb88eSYatharth Kochar cm_set_next_eret_context(NON_SECURE); 49248bfb88eSYatharth Kochar 49348bfb88eSYatharth Kochar *handle = cm_get_context(NON_SECURE); 494*a4409008Sdp-arm #else 495*a4409008Sdp-arm /* Update the next context. */ 496*a4409008Sdp-arm cm_set_next_context(cm_get_context(NON_SECURE)); 497*a4409008Sdp-arm 498*a4409008Sdp-arm /* Prepare the smc context for the next BL image. */ 499*a4409008Sdp-arm smc_set_next_ctx(NON_SECURE); 500*a4409008Sdp-arm 501*a4409008Sdp-arm *handle = smc_get_ctx(NON_SECURE); 502*a4409008Sdp-arm #endif 50348bfb88eSYatharth Kochar return 0; 50448bfb88eSYatharth Kochar } 50548bfb88eSYatharth Kochar 50648bfb88eSYatharth Kochar /******************************************************************************* 50748bfb88eSYatharth Kochar * This function provides the opportunity for users to perform any 50848bfb88eSYatharth Kochar * platform specific handling after the Firmware update is done. 50948bfb88eSYatharth Kochar ******************************************************************************/ 5101f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved) 51148bfb88eSYatharth Kochar { 51248bfb88eSYatharth Kochar NOTICE("BL1-FWU: *******FWU Process Completed*******\n"); 51348bfb88eSYatharth Kochar 51448bfb88eSYatharth Kochar /* 51548bfb88eSYatharth Kochar * Call platform done function. 51648bfb88eSYatharth Kochar */ 5171f37b944SDan Handley bl1_plat_fwu_done(client_cookie, reserved); 51848bfb88eSYatharth Kochar assert(0); 51948bfb88eSYatharth Kochar } 520