148bfb88eSYatharth Kochar /* 2*566034fcSSoby Mathew * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. 348bfb88eSYatharth Kochar * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 548bfb88eSYatharth Kochar */ 648bfb88eSYatharth Kochar 748bfb88eSYatharth Kochar #include <arch_helpers.h> 82a4b4b71SIsla Mitchell #include <assert.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); 439d6fc3c3SAntonio Nino Diaz static int bl1_fwu_image_reset(unsigned int image_id, 449d6fc3c3SAntonio Nino Diaz unsigned int flags); 451f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved); 4648bfb88eSYatharth Kochar 4748bfb88eSYatharth Kochar /* 4848bfb88eSYatharth Kochar * This keeps track of last executed secure image id. 4948bfb88eSYatharth Kochar */ 5048bfb88eSYatharth Kochar static unsigned int sec_exec_image_id = INVALID_IMAGE_ID; 5148bfb88eSYatharth Kochar 529d6fc3c3SAntonio Nino Diaz /* Authentication status of each image. */ 539d6fc3c3SAntonio Nino Diaz extern unsigned int auth_img_flags[]; 549d6fc3c3SAntonio Nino Diaz 5548bfb88eSYatharth Kochar /******************************************************************************* 5648bfb88eSYatharth Kochar * Top level handler for servicing FWU SMCs. 5748bfb88eSYatharth Kochar ******************************************************************************/ 5848bfb88eSYatharth Kochar register_t bl1_fwu_smc_handler(unsigned int smc_fid, 5948bfb88eSYatharth Kochar register_t x1, 6048bfb88eSYatharth Kochar register_t x2, 6148bfb88eSYatharth Kochar register_t x3, 6248bfb88eSYatharth Kochar register_t x4, 6348bfb88eSYatharth Kochar void *cookie, 6448bfb88eSYatharth Kochar void *handle, 6548bfb88eSYatharth Kochar unsigned int flags) 6648bfb88eSYatharth Kochar { 6748bfb88eSYatharth Kochar 6848bfb88eSYatharth Kochar switch (smc_fid) { 6948bfb88eSYatharth Kochar case FWU_SMC_IMAGE_COPY: 7048bfb88eSYatharth Kochar SMC_RET1(handle, bl1_fwu_image_copy(x1, x2, x3, x4, flags)); 7148bfb88eSYatharth Kochar 7248bfb88eSYatharth Kochar case FWU_SMC_IMAGE_AUTH: 7348bfb88eSYatharth Kochar SMC_RET1(handle, bl1_fwu_image_auth(x1, x2, x3, flags)); 7448bfb88eSYatharth Kochar 7548bfb88eSYatharth Kochar case FWU_SMC_IMAGE_EXECUTE: 7648bfb88eSYatharth Kochar SMC_RET1(handle, bl1_fwu_image_execute(x1, &handle, flags)); 7748bfb88eSYatharth Kochar 7848bfb88eSYatharth Kochar case FWU_SMC_IMAGE_RESUME: 7928955d57SDan Handley SMC_RET1(handle, bl1_fwu_image_resume(x1, &handle, flags)); 8048bfb88eSYatharth Kochar 8148bfb88eSYatharth Kochar case FWU_SMC_SEC_IMAGE_DONE: 8248bfb88eSYatharth Kochar SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags)); 8348bfb88eSYatharth Kochar 849d6fc3c3SAntonio Nino Diaz case FWU_SMC_IMAGE_RESET: 859d6fc3c3SAntonio Nino Diaz SMC_RET1(handle, bl1_fwu_image_reset(x1, flags)); 869d6fc3c3SAntonio Nino Diaz 8748bfb88eSYatharth Kochar case FWU_SMC_UPDATE_DONE: 881f37b944SDan Handley bl1_fwu_done((void *)x1, NULL); 8948bfb88eSYatharth Kochar /* We should never return from bl1_fwu_done() */ 9048bfb88eSYatharth Kochar 9148bfb88eSYatharth Kochar default: 9248bfb88eSYatharth Kochar assert(0); 9348bfb88eSYatharth Kochar break; 9448bfb88eSYatharth Kochar } 9548bfb88eSYatharth Kochar 967a317a70SAntonio Nino Diaz SMC_RET1(handle, SMC_UNK); 9748bfb88eSYatharth Kochar } 9848bfb88eSYatharth Kochar 9948bfb88eSYatharth Kochar /******************************************************************************* 100128daee2SAntonio Nino Diaz * Utility functions to keep track of the images that are loaded at any time. 101128daee2SAntonio Nino Diaz ******************************************************************************/ 102128daee2SAntonio Nino Diaz 103128daee2SAntonio Nino Diaz #ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES 104128daee2SAntonio Nino Diaz #define FWU_MAX_SIMULTANEOUS_IMAGES PLAT_FWU_MAX_SIMULTANEOUS_IMAGES 105128daee2SAntonio Nino Diaz #else 106128daee2SAntonio Nino Diaz #define FWU_MAX_SIMULTANEOUS_IMAGES 10 107128daee2SAntonio Nino Diaz #endif 108128daee2SAntonio Nino Diaz 109128daee2SAntonio Nino Diaz static int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = { 110128daee2SAntonio Nino Diaz [0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID 111128daee2SAntonio Nino Diaz }; 112128daee2SAntonio Nino Diaz 113128daee2SAntonio Nino Diaz /* 114128daee2SAntonio Nino Diaz * Adds an image_id to the bl1_fwu_loaded_ids array. 115128daee2SAntonio Nino Diaz * Returns 0 on success, 1 on error. 116128daee2SAntonio Nino Diaz */ 117128daee2SAntonio Nino Diaz static int bl1_fwu_add_loaded_id(int image_id) 118128daee2SAntonio Nino Diaz { 119128daee2SAntonio Nino Diaz int i; 120128daee2SAntonio Nino Diaz 121128daee2SAntonio Nino Diaz /* Check if the ID is already in the list */ 122128daee2SAntonio Nino Diaz for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { 123128daee2SAntonio Nino Diaz if (bl1_fwu_loaded_ids[i] == image_id) 124128daee2SAntonio Nino Diaz return 0; 125128daee2SAntonio Nino Diaz } 126128daee2SAntonio Nino Diaz 127128daee2SAntonio Nino Diaz /* Find an empty slot */ 128128daee2SAntonio Nino Diaz for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { 129128daee2SAntonio Nino Diaz if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) { 130128daee2SAntonio Nino Diaz bl1_fwu_loaded_ids[i] = image_id; 131128daee2SAntonio Nino Diaz return 0; 132128daee2SAntonio Nino Diaz } 133128daee2SAntonio Nino Diaz } 134128daee2SAntonio Nino Diaz 135128daee2SAntonio Nino Diaz return 1; 136128daee2SAntonio Nino Diaz } 137128daee2SAntonio Nino Diaz 138128daee2SAntonio Nino Diaz /* 139128daee2SAntonio Nino Diaz * Removes an image_id from the bl1_fwu_loaded_ids array. 140128daee2SAntonio Nino Diaz * Returns 0 on success, 1 on error. 141128daee2SAntonio Nino Diaz */ 142128daee2SAntonio Nino Diaz static int bl1_fwu_remove_loaded_id(int image_id) 143128daee2SAntonio Nino Diaz { 144128daee2SAntonio Nino Diaz int i; 145128daee2SAntonio Nino Diaz 146128daee2SAntonio Nino Diaz /* Find the ID */ 147128daee2SAntonio Nino Diaz for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { 148128daee2SAntonio Nino Diaz if (bl1_fwu_loaded_ids[i] == image_id) { 149128daee2SAntonio Nino Diaz bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID; 150128daee2SAntonio Nino Diaz return 0; 151128daee2SAntonio Nino Diaz } 152128daee2SAntonio Nino Diaz } 153128daee2SAntonio Nino Diaz 154128daee2SAntonio Nino Diaz return 1; 155128daee2SAntonio Nino Diaz } 156128daee2SAntonio Nino Diaz 157128daee2SAntonio Nino Diaz /******************************************************************************* 158128daee2SAntonio Nino Diaz * This function checks if the specified image overlaps another image already 159128daee2SAntonio Nino Diaz * loaded. It returns 0 if there is no overlap, a negative error code otherwise. 160128daee2SAntonio Nino Diaz ******************************************************************************/ 161128daee2SAntonio Nino Diaz static int bl1_fwu_image_check_overlaps(int image_id) 162128daee2SAntonio Nino Diaz { 163128daee2SAntonio Nino Diaz const image_desc_t *image_desc, *checked_image_desc; 164128daee2SAntonio Nino Diaz const image_info_t *info, *checked_info; 165128daee2SAntonio Nino Diaz 166128daee2SAntonio Nino Diaz uintptr_t image_base, image_end; 167128daee2SAntonio Nino Diaz uintptr_t checked_image_base, checked_image_end; 168128daee2SAntonio Nino Diaz 169128daee2SAntonio Nino Diaz checked_image_desc = bl1_plat_get_image_desc(image_id); 170128daee2SAntonio Nino Diaz checked_info = &checked_image_desc->image_info; 171128daee2SAntonio Nino Diaz 172128daee2SAntonio Nino Diaz /* Image being checked mustn't be empty. */ 173128daee2SAntonio Nino Diaz assert(checked_info->image_size != 0); 174128daee2SAntonio Nino Diaz 175128daee2SAntonio Nino Diaz checked_image_base = checked_info->image_base; 176128daee2SAntonio Nino Diaz checked_image_end = checked_image_base + checked_info->image_size - 1; 177ee05ae16SSoby Mathew /* No need to check for overflows, it's done in bl1_fwu_image_copy(). */ 178128daee2SAntonio Nino Diaz 179128daee2SAntonio Nino Diaz for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { 180128daee2SAntonio Nino Diaz 181ee05ae16SSoby Mathew /* Skip INVALID_IMAGE_IDs and don't check image against itself */ 182ee05ae16SSoby Mathew if ((bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) || 183ee05ae16SSoby Mathew (bl1_fwu_loaded_ids[i] == image_id)) 184128daee2SAntonio Nino Diaz continue; 185128daee2SAntonio Nino Diaz 186128daee2SAntonio Nino Diaz image_desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]); 187128daee2SAntonio Nino Diaz 188128daee2SAntonio Nino Diaz /* Only check images that are loaded or being loaded. */ 189ee05ae16SSoby Mathew assert (image_desc && image_desc->state != IMAGE_STATE_RESET); 190128daee2SAntonio Nino Diaz 191128daee2SAntonio Nino Diaz info = &image_desc->image_info; 192128daee2SAntonio Nino Diaz 193128daee2SAntonio Nino Diaz /* There cannot be overlaps with an empty image. */ 194128daee2SAntonio Nino Diaz if (info->image_size == 0) 195128daee2SAntonio Nino Diaz continue; 196128daee2SAntonio Nino Diaz 197128daee2SAntonio Nino Diaz image_base = info->image_base; 198128daee2SAntonio Nino Diaz image_end = image_base + info->image_size - 1; 199128daee2SAntonio Nino Diaz /* 200128daee2SAntonio Nino Diaz * Overflows cannot happen. It is checked in 201128daee2SAntonio Nino Diaz * bl1_fwu_image_copy() when the image goes from RESET to 202128daee2SAntonio Nino Diaz * COPYING or COPIED. 203128daee2SAntonio Nino Diaz */ 204128daee2SAntonio Nino Diaz assert (image_end > image_base); 205128daee2SAntonio Nino Diaz 206128daee2SAntonio Nino Diaz /* Check if there are overlaps. */ 207128daee2SAntonio Nino Diaz if (!(image_end < checked_image_base || 208128daee2SAntonio Nino Diaz checked_image_end < image_base)) { 209128daee2SAntonio Nino Diaz VERBOSE("Image with ID %d overlaps existing image with ID %d", 210128daee2SAntonio Nino Diaz checked_image_desc->image_id, image_desc->image_id); 211128daee2SAntonio Nino Diaz return -EPERM; 212128daee2SAntonio Nino Diaz } 213128daee2SAntonio Nino Diaz } 214128daee2SAntonio Nino Diaz 215128daee2SAntonio Nino Diaz return 0; 216128daee2SAntonio Nino Diaz } 217128daee2SAntonio Nino Diaz 218128daee2SAntonio Nino Diaz /******************************************************************************* 21948bfb88eSYatharth Kochar * This function is responsible for copying secure images in AP Secure RAM. 22048bfb88eSYatharth Kochar ******************************************************************************/ 22148bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id, 22248bfb88eSYatharth Kochar uintptr_t image_src, 22348bfb88eSYatharth Kochar unsigned int block_size, 22448bfb88eSYatharth Kochar unsigned int image_size, 22548bfb88eSYatharth Kochar unsigned int flags) 22648bfb88eSYatharth Kochar { 2279f1489e4SSandrine Bailleux uintptr_t dest_addr; 228b38a9e5cSSandrine Bailleux unsigned int remaining; 22948bfb88eSYatharth Kochar 23048bfb88eSYatharth Kochar /* Get the image descriptor. */ 23148bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 2329f1489e4SSandrine Bailleux if (!image_desc) { 2339f1489e4SSandrine Bailleux WARN("BL1-FWU: Invalid image ID %u\n", image_id); 23448bfb88eSYatharth Kochar return -EPERM; 23548bfb88eSYatharth Kochar } 23648bfb88eSYatharth Kochar 2379f1489e4SSandrine Bailleux /* 2389f1489e4SSandrine Bailleux * The request must originate from a non-secure caller and target a 2399f1489e4SSandrine Bailleux * secure image. Any other scenario is invalid. 2409f1489e4SSandrine Bailleux */ 2419f1489e4SSandrine Bailleux if (GET_SECURITY_STATE(flags) == SECURE) { 2429f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed from secure world.\n"); 2439f1489e4SSandrine Bailleux return -EPERM; 2449f1489e4SSandrine Bailleux } 2459f1489e4SSandrine Bailleux if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) { 2469f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed for non-secure images.\n"); 2479f1489e4SSandrine Bailleux return -EPERM; 2489f1489e4SSandrine Bailleux } 2499f1489e4SSandrine Bailleux 2509f1489e4SSandrine Bailleux /* Check whether the FWU state machine is in the correct state. */ 2519f1489e4SSandrine Bailleux if ((image_desc->state != IMAGE_STATE_RESET) && 2529f1489e4SSandrine Bailleux (image_desc->state != IMAGE_STATE_COPYING)) { 2539f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed at this point of the FWU" 2549f1489e4SSandrine Bailleux " process.\n"); 25548bfb88eSYatharth Kochar return -EPERM; 25648bfb88eSYatharth Kochar } 25748bfb88eSYatharth Kochar 258949a52d2SSandrine Bailleux if ((!image_src) || (!block_size) || 259949a52d2SSandrine Bailleux check_uptr_overflow(image_src, block_size - 1)) { 26048bfb88eSYatharth Kochar WARN("BL1-FWU: Copy not allowed due to invalid image source" 26148bfb88eSYatharth Kochar " or block size\n"); 26248bfb88eSYatharth Kochar return -ENOMEM; 26348bfb88eSYatharth Kochar } 26448bfb88eSYatharth Kochar 26548bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPYING) { 2661bfb7068SSandrine Bailleux /* 2671bfb7068SSandrine Bailleux * There must have been at least 1 copy operation for this image 2681bfb7068SSandrine Bailleux * previously. 2691bfb7068SSandrine Bailleux */ 2701bfb7068SSandrine Bailleux assert(image_desc->copied_size != 0); 2711bfb7068SSandrine Bailleux /* 2721bfb7068SSandrine Bailleux * The image size must have been recorded in the 1st copy 2731bfb7068SSandrine Bailleux * operation. 2741bfb7068SSandrine Bailleux */ 2759f1489e4SSandrine Bailleux image_size = image_desc->image_info.image_size; 2761bfb7068SSandrine Bailleux assert(image_size != 0); 2771bfb7068SSandrine Bailleux assert(image_desc->copied_size < image_size); 2781bfb7068SSandrine Bailleux 27948bfb88eSYatharth Kochar INFO("BL1-FWU: Continuing image copy in blocks\n"); 2809f1489e4SSandrine Bailleux } else { /* image_desc->state == IMAGE_STATE_RESET */ 2819f1489e4SSandrine Bailleux INFO("BL1-FWU: Initial call to copy an image\n"); 28248bfb88eSYatharth Kochar 2839f1489e4SSandrine Bailleux /* 2849f1489e4SSandrine Bailleux * image_size is relevant only for the 1st copy request, it is 2859f1489e4SSandrine Bailleux * then ignored for subsequent calls for this image. 2869f1489e4SSandrine Bailleux */ 28748bfb88eSYatharth Kochar if (!image_size) { 2889f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed due to invalid image" 2899f1489e4SSandrine Bailleux " size\n"); 29048bfb88eSYatharth Kochar return -ENOMEM; 29148bfb88eSYatharth Kochar } 29248bfb88eSYatharth Kochar 29353d703a5SYatharth Kochar #if LOAD_IMAGE_V2 29453d703a5SYatharth Kochar /* Check that the image size to load is within limit */ 29553d703a5SYatharth Kochar if (image_size > image_desc->image_info.image_max_size) { 29653d703a5SYatharth Kochar WARN("BL1-FWU: Image size out of bounds\n"); 29753d703a5SYatharth Kochar return -ENOMEM; 29853d703a5SYatharth Kochar } 29953d703a5SYatharth Kochar #else 300949a52d2SSandrine Bailleux /* 301949a52d2SSandrine Bailleux * Check the image will fit into the free trusted RAM after BL1 302949a52d2SSandrine Bailleux * load. 303949a52d2SSandrine Bailleux */ 3049f1489e4SSandrine Bailleux const meminfo_t *mem_layout = bl1_plat_sec_mem_layout(); 305949a52d2SSandrine Bailleux if (!is_mem_free(mem_layout->free_base, mem_layout->free_size, 306949a52d2SSandrine Bailleux image_desc->image_info.image_base, 307949a52d2SSandrine Bailleux image_size)) { 3089f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed due to insufficient" 3099f1489e4SSandrine Bailleux " resources.\n"); 31048bfb88eSYatharth Kochar return -ENOMEM; 31148bfb88eSYatharth Kochar } 31253d703a5SYatharth Kochar #endif 31348bfb88eSYatharth Kochar 3149f1489e4SSandrine Bailleux /* Save the given image size. */ 31548bfb88eSYatharth Kochar image_desc->image_info.image_size = image_size; 31648bfb88eSYatharth Kochar 317128daee2SAntonio Nino Diaz /* Make sure the image doesn't overlap other images. */ 318128daee2SAntonio Nino Diaz if (bl1_fwu_image_check_overlaps(image_id)) { 319128daee2SAntonio Nino Diaz image_desc->image_info.image_size = 0; 320128daee2SAntonio Nino Diaz WARN("BL1-FWU: This image overlaps another one\n"); 321128daee2SAntonio Nino Diaz return -EPERM; 322128daee2SAntonio Nino Diaz } 323128daee2SAntonio Nino Diaz 324b38a9e5cSSandrine Bailleux /* 325b38a9e5cSSandrine Bailleux * copied_size must be explicitly initialized here because the 326b38a9e5cSSandrine Bailleux * FWU code doesn't necessarily do it when it resets the state 327b38a9e5cSSandrine Bailleux * machine. 328b38a9e5cSSandrine Bailleux */ 329b38a9e5cSSandrine Bailleux image_desc->copied_size = 0; 330b38a9e5cSSandrine Bailleux } 331b38a9e5cSSandrine Bailleux 332b38a9e5cSSandrine Bailleux /* 333b38a9e5cSSandrine Bailleux * If the given block size is more than the total image size 334b38a9e5cSSandrine Bailleux * then clip the former to the latter. 335b38a9e5cSSandrine Bailleux */ 336b38a9e5cSSandrine Bailleux remaining = image_size - image_desc->copied_size; 337b38a9e5cSSandrine Bailleux if (block_size > remaining) { 338b38a9e5cSSandrine Bailleux WARN("BL1-FWU: Block size is too big, clipping it.\n"); 339b38a9e5cSSandrine Bailleux block_size = remaining; 340b38a9e5cSSandrine Bailleux } 341b38a9e5cSSandrine Bailleux 342b38a9e5cSSandrine Bailleux /* Make sure the source image is mapped in memory. */ 343b38a9e5cSSandrine Bailleux if (bl1_plat_mem_check(image_src, block_size, flags)) { 344b38a9e5cSSandrine Bailleux WARN("BL1-FWU: Source image is not mapped.\n"); 345b38a9e5cSSandrine Bailleux return -ENOMEM; 346b38a9e5cSSandrine Bailleux } 347b38a9e5cSSandrine Bailleux 348128daee2SAntonio Nino Diaz if (bl1_fwu_add_loaded_id(image_id)) { 349128daee2SAntonio Nino Diaz WARN("BL1-FWU: Too many images loaded at the same time.\n"); 350128daee2SAntonio Nino Diaz return -ENOMEM; 351128daee2SAntonio Nino Diaz } 352128daee2SAntonio Nino Diaz 353*566034fcSSoby Mathew /* Allow the platform to handle pre-image load before copying */ 354*566034fcSSoby Mathew if (image_desc->state == IMAGE_STATE_RESET) { 355*566034fcSSoby Mathew if (bl1_plat_handle_pre_image_load(image_id) != 0) { 356*566034fcSSoby Mathew ERROR("BL1-FWU: Failure in pre-image load of image id %d\n", 357*566034fcSSoby Mathew image_id); 358*566034fcSSoby Mathew return -EPERM; 359*566034fcSSoby Mathew } 360*566034fcSSoby Mathew } 361*566034fcSSoby Mathew 362b38a9e5cSSandrine Bailleux /* Everything looks sane. Go ahead and copy the block of data. */ 363b38a9e5cSSandrine Bailleux dest_addr = image_desc->image_info.image_base + image_desc->copied_size; 3649f1489e4SSandrine Bailleux memcpy((void *) dest_addr, (const void *) image_src, block_size); 3659f1489e4SSandrine Bailleux flush_dcache_range(dest_addr, block_size); 36648bfb88eSYatharth Kochar 367b38a9e5cSSandrine Bailleux image_desc->copied_size += block_size; 368b38a9e5cSSandrine Bailleux image_desc->state = (block_size == remaining) ? 369b38a9e5cSSandrine Bailleux IMAGE_STATE_COPIED : IMAGE_STATE_COPYING; 37048bfb88eSYatharth Kochar 371b38a9e5cSSandrine Bailleux INFO("BL1-FWU: Copy operation successful.\n"); 37248bfb88eSYatharth Kochar return 0; 37348bfb88eSYatharth Kochar } 37448bfb88eSYatharth Kochar 37548bfb88eSYatharth Kochar /******************************************************************************* 37648bfb88eSYatharth Kochar * This function is responsible for authenticating Normal/Secure images. 37748bfb88eSYatharth Kochar ******************************************************************************/ 37848bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id, 37948bfb88eSYatharth Kochar uintptr_t image_src, 38048bfb88eSYatharth Kochar unsigned int image_size, 38148bfb88eSYatharth Kochar unsigned int flags) 38248bfb88eSYatharth Kochar { 38348bfb88eSYatharth Kochar int result; 38448bfb88eSYatharth Kochar uintptr_t base_addr; 38548bfb88eSYatharth Kochar unsigned int total_size; 38648bfb88eSYatharth Kochar 38748bfb88eSYatharth Kochar /* Get the image descriptor. */ 38848bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 38948bfb88eSYatharth Kochar if (!image_desc) 39048bfb88eSYatharth Kochar return -EPERM; 39148bfb88eSYatharth Kochar 392843ddee4SYatharth Kochar if (GET_SECURITY_STATE(flags) == SECURE) { 39348bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_RESET) { 39448bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication from secure world " 39548bfb88eSYatharth Kochar "while in invalid state\n"); 39648bfb88eSYatharth Kochar return -EPERM; 39748bfb88eSYatharth Kochar } 39848bfb88eSYatharth Kochar } else { 399843ddee4SYatharth Kochar if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) { 40048bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_COPIED) { 40148bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication of secure image " 40248bfb88eSYatharth Kochar "from non-secure world while not in copied state\n"); 40348bfb88eSYatharth Kochar return -EPERM; 40448bfb88eSYatharth Kochar } 40548bfb88eSYatharth Kochar } else { 40648bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_RESET) { 40748bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication of non-secure image " 40848bfb88eSYatharth Kochar "from non-secure world while in invalid state\n"); 40948bfb88eSYatharth Kochar return -EPERM; 41048bfb88eSYatharth Kochar } 41148bfb88eSYatharth Kochar } 41248bfb88eSYatharth Kochar } 41348bfb88eSYatharth Kochar 41448bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPIED) { 41548bfb88eSYatharth Kochar /* 41648bfb88eSYatharth Kochar * Image is in COPIED state. 41748bfb88eSYatharth Kochar * Use the stored address and size. 41848bfb88eSYatharth Kochar */ 41948bfb88eSYatharth Kochar base_addr = image_desc->image_info.image_base; 42048bfb88eSYatharth Kochar total_size = image_desc->image_info.image_size; 42148bfb88eSYatharth Kochar } else { 422949a52d2SSandrine Bailleux if ((!image_src) || (!image_size) || 423949a52d2SSandrine Bailleux check_uptr_overflow(image_src, image_size - 1)) { 42448bfb88eSYatharth Kochar WARN("BL1-FWU: Auth not allowed due to invalid" 42548bfb88eSYatharth Kochar " image source/size\n"); 42648bfb88eSYatharth Kochar return -ENOMEM; 42748bfb88eSYatharth Kochar } 42848bfb88eSYatharth Kochar 42948bfb88eSYatharth Kochar /* 43048bfb88eSYatharth Kochar * Image is in RESET state. 43148bfb88eSYatharth Kochar * Check the parameters and authenticate the source image in place. 43248bfb88eSYatharth Kochar */ 43303131c85SDan Handley if (bl1_plat_mem_check(image_src, image_size, \ 43403131c85SDan Handley image_desc->ep_info.h.attr)) { 43548bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication arguments source/size not mapped\n"); 43648bfb88eSYatharth Kochar return -ENOMEM; 43748bfb88eSYatharth Kochar } 43848bfb88eSYatharth Kochar 439128daee2SAntonio Nino Diaz if (bl1_fwu_add_loaded_id(image_id)) { 440128daee2SAntonio Nino Diaz WARN("BL1-FWU: Too many images loaded at the same time.\n"); 441128daee2SAntonio Nino Diaz return -ENOMEM; 442128daee2SAntonio Nino Diaz } 443128daee2SAntonio Nino Diaz 44448bfb88eSYatharth Kochar base_addr = image_src; 44548bfb88eSYatharth Kochar total_size = image_size; 44648bfb88eSYatharth Kochar 44748bfb88eSYatharth Kochar /* Update the image size in the descriptor. */ 44848bfb88eSYatharth Kochar image_desc->image_info.image_size = total_size; 44948bfb88eSYatharth Kochar } 45048bfb88eSYatharth Kochar 45148bfb88eSYatharth Kochar /* 45248bfb88eSYatharth Kochar * Authenticate the image. 45348bfb88eSYatharth Kochar */ 45448bfb88eSYatharth Kochar INFO("BL1-FWU: Authenticating image_id:%d\n", image_id); 45548bfb88eSYatharth Kochar result = auth_mod_verify_img(image_id, (void *)base_addr, total_size); 45648bfb88eSYatharth Kochar if (result != 0) { 45748bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication Failed err=%d\n", result); 45848bfb88eSYatharth Kochar 45948bfb88eSYatharth Kochar /* 46048bfb88eSYatharth Kochar * Authentication has failed. 46148bfb88eSYatharth Kochar * Clear the memory if the image was copied. 46248bfb88eSYatharth Kochar * This is to prevent an attack where this contains 46348bfb88eSYatharth Kochar * some malicious code that can somehow be executed later. 46448bfb88eSYatharth Kochar */ 46548bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPIED) { 46648bfb88eSYatharth Kochar /* Clear the memory.*/ 467308d359bSDouglas Raillard zero_normalmem((void *)base_addr, total_size); 46848bfb88eSYatharth Kochar flush_dcache_range(base_addr, total_size); 46948bfb88eSYatharth Kochar 47048bfb88eSYatharth Kochar /* Indicate that image can be copied again*/ 47148bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_RESET; 47248bfb88eSYatharth Kochar } 473128daee2SAntonio Nino Diaz 474128daee2SAntonio Nino Diaz /* 475128daee2SAntonio Nino Diaz * Even if this fails it's ok because the ID isn't in the array. 476128daee2SAntonio Nino Diaz * The image cannot be in RESET state here, it is checked at the 477128daee2SAntonio Nino Diaz * beginning of the function. 478128daee2SAntonio Nino Diaz */ 479128daee2SAntonio Nino Diaz bl1_fwu_remove_loaded_id(image_id); 48048bfb88eSYatharth Kochar return -EAUTH; 48148bfb88eSYatharth Kochar } 48248bfb88eSYatharth Kochar 48348bfb88eSYatharth Kochar /* Indicate that image is in authenticated state. */ 48448bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_AUTHENTICATED; 48548bfb88eSYatharth Kochar 486*566034fcSSoby Mathew /* Allow the platform to handle post-image load */ 487*566034fcSSoby Mathew result = bl1_plat_handle_post_image_load(image_id); 488*566034fcSSoby Mathew if (result != 0) { 489*566034fcSSoby Mathew ERROR("BL1-FWU: Failure %d in post-image load of image id %d\n", 490*566034fcSSoby Mathew result, image_id); 491*566034fcSSoby Mathew /* 492*566034fcSSoby Mathew * Panic here as the platform handling of post-image load is 493*566034fcSSoby Mathew * not correct. 494*566034fcSSoby Mathew */ 495*566034fcSSoby Mathew plat_error_handler(result); 496*566034fcSSoby Mathew } 497*566034fcSSoby Mathew 49848bfb88eSYatharth Kochar /* 49948bfb88eSYatharth Kochar * Flush image_info to memory so that other 50048bfb88eSYatharth Kochar * secure world images can see changes. 50148bfb88eSYatharth Kochar */ 50248bfb88eSYatharth Kochar flush_dcache_range((unsigned long)&image_desc->image_info, 50348bfb88eSYatharth Kochar sizeof(image_info_t)); 50448bfb88eSYatharth Kochar 50548bfb88eSYatharth Kochar INFO("BL1-FWU: Authentication was successful\n"); 50648bfb88eSYatharth Kochar 50748bfb88eSYatharth Kochar return 0; 50848bfb88eSYatharth Kochar } 50948bfb88eSYatharth Kochar 51048bfb88eSYatharth Kochar /******************************************************************************* 51148bfb88eSYatharth Kochar * This function is responsible for executing Secure images. 51248bfb88eSYatharth Kochar ******************************************************************************/ 51348bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id, 51448bfb88eSYatharth Kochar void **handle, 51548bfb88eSYatharth Kochar unsigned int flags) 51648bfb88eSYatharth Kochar { 51748bfb88eSYatharth Kochar /* Get the image descriptor. */ 51848bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 51948bfb88eSYatharth Kochar 52048bfb88eSYatharth Kochar /* 52148bfb88eSYatharth Kochar * Execution is NOT allowed if: 52228955d57SDan Handley * image_id is invalid OR 52348bfb88eSYatharth Kochar * Caller is from Secure world OR 52448bfb88eSYatharth Kochar * Image is Non-Secure OR 52548bfb88eSYatharth Kochar * Image is Non-Executable OR 52648bfb88eSYatharth Kochar * Image is NOT in AUTHENTICATED state. 52748bfb88eSYatharth Kochar */ 52848bfb88eSYatharth Kochar if ((!image_desc) || 529843ddee4SYatharth Kochar (GET_SECURITY_STATE(flags) == SECURE) || 530843ddee4SYatharth Kochar (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) || 531843ddee4SYatharth Kochar (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) || 53248bfb88eSYatharth Kochar (image_desc->state != IMAGE_STATE_AUTHENTICATED)) { 53348bfb88eSYatharth Kochar WARN("BL1-FWU: Execution not allowed due to invalid state/args\n"); 53448bfb88eSYatharth Kochar return -EPERM; 53548bfb88eSYatharth Kochar } 53648bfb88eSYatharth Kochar 53748bfb88eSYatharth Kochar INFO("BL1-FWU: Executing Secure image\n"); 53848bfb88eSYatharth Kochar 539a4409008Sdp-arm #ifdef AARCH64 54048bfb88eSYatharth Kochar /* Save NS-EL1 system registers. */ 54148bfb88eSYatharth Kochar cm_el1_sysregs_context_save(NON_SECURE); 542a4409008Sdp-arm #endif 54348bfb88eSYatharth Kochar 54448bfb88eSYatharth Kochar /* Prepare the image for execution. */ 54548bfb88eSYatharth Kochar bl1_prepare_next_image(image_id); 54648bfb88eSYatharth Kochar 54748bfb88eSYatharth Kochar /* Update the secure image id. */ 54848bfb88eSYatharth Kochar sec_exec_image_id = image_id; 54948bfb88eSYatharth Kochar 550a4409008Sdp-arm #ifdef AARCH64 55148bfb88eSYatharth Kochar *handle = cm_get_context(SECURE); 552a4409008Sdp-arm #else 553a4409008Sdp-arm *handle = smc_get_ctx(SECURE); 554a4409008Sdp-arm #endif 55548bfb88eSYatharth Kochar return 0; 55648bfb88eSYatharth Kochar } 55748bfb88eSYatharth Kochar 55848bfb88eSYatharth Kochar /******************************************************************************* 55928955d57SDan Handley * This function is responsible for resuming execution in the other security 56028955d57SDan Handley * world 56148bfb88eSYatharth Kochar ******************************************************************************/ 56228955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param, 56348bfb88eSYatharth Kochar void **handle, 56448bfb88eSYatharth Kochar unsigned int flags) 56548bfb88eSYatharth Kochar { 56648bfb88eSYatharth Kochar image_desc_t *image_desc; 56748bfb88eSYatharth Kochar unsigned int resume_sec_state; 568843ddee4SYatharth Kochar unsigned int caller_sec_state = GET_SECURITY_STATE(flags); 56948bfb88eSYatharth Kochar 57048bfb88eSYatharth Kochar /* Get the image descriptor for last executed secure image id. */ 57148bfb88eSYatharth Kochar image_desc = bl1_plat_get_image_desc(sec_exec_image_id); 57228955d57SDan Handley if (caller_sec_state == NON_SECURE) { 57328955d57SDan Handley if (!image_desc) { 57428955d57SDan Handley WARN("BL1-FWU: Resume not allowed due to no available" 57528955d57SDan Handley "secure image\n"); 57648bfb88eSYatharth Kochar return -EPERM; 57748bfb88eSYatharth Kochar } 57828955d57SDan Handley } else { 57928955d57SDan Handley /* image_desc must be valid for secure world callers */ 58028955d57SDan Handley assert(image_desc); 58128955d57SDan Handley } 58228955d57SDan Handley 583843ddee4SYatharth Kochar assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE); 584843ddee4SYatharth Kochar assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE); 58528955d57SDan Handley 58628955d57SDan Handley if (caller_sec_state == SECURE) { 58728955d57SDan Handley assert(image_desc->state == IMAGE_STATE_EXECUTED); 58848bfb88eSYatharth Kochar 58948bfb88eSYatharth Kochar /* Update the flags. */ 59048bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_INTERRUPTED; 59148bfb88eSYatharth Kochar resume_sec_state = NON_SECURE; 59248bfb88eSYatharth Kochar } else { 59328955d57SDan Handley assert(image_desc->state == IMAGE_STATE_INTERRUPTED); 59448bfb88eSYatharth Kochar 59548bfb88eSYatharth Kochar /* Update the flags. */ 59648bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_EXECUTED; 59748bfb88eSYatharth Kochar resume_sec_state = SECURE; 59848bfb88eSYatharth Kochar } 59948bfb88eSYatharth Kochar 600a4409008Sdp-arm INFO("BL1-FWU: Resuming %s world context\n", 601a4409008Sdp-arm (resume_sec_state == SECURE) ? "secure" : "normal"); 602a4409008Sdp-arm 603a4409008Sdp-arm #ifdef AARCH64 60448bfb88eSYatharth Kochar /* Save the EL1 system registers of calling world. */ 60528955d57SDan Handley cm_el1_sysregs_context_save(caller_sec_state); 60648bfb88eSYatharth Kochar 60748bfb88eSYatharth Kochar /* Restore the EL1 system registers of resuming world. */ 60848bfb88eSYatharth Kochar cm_el1_sysregs_context_restore(resume_sec_state); 60948bfb88eSYatharth Kochar 61048bfb88eSYatharth Kochar /* Update the next context. */ 61148bfb88eSYatharth Kochar cm_set_next_eret_context(resume_sec_state); 61248bfb88eSYatharth Kochar 61348bfb88eSYatharth Kochar *handle = cm_get_context(resume_sec_state); 614a4409008Sdp-arm #else 615a4409008Sdp-arm /* Update the next context. */ 616a4409008Sdp-arm cm_set_next_context(cm_get_context(resume_sec_state)); 617a4409008Sdp-arm 618a4409008Sdp-arm /* Prepare the smc context for the next BL image. */ 619a4409008Sdp-arm smc_set_next_ctx(resume_sec_state); 620a4409008Sdp-arm 621a4409008Sdp-arm *handle = smc_get_ctx(resume_sec_state); 622a4409008Sdp-arm #endif 62348bfb88eSYatharth Kochar return image_param; 62448bfb88eSYatharth Kochar } 62548bfb88eSYatharth Kochar 62648bfb88eSYatharth Kochar /******************************************************************************* 62748bfb88eSYatharth Kochar * This function is responsible for resuming normal world context. 62848bfb88eSYatharth Kochar ******************************************************************************/ 62948bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle, unsigned int flags) 63048bfb88eSYatharth Kochar { 63128955d57SDan Handley image_desc_t *image_desc; 63248bfb88eSYatharth Kochar 63328955d57SDan Handley /* Make sure caller is from the secure world */ 634843ddee4SYatharth Kochar if (GET_SECURITY_STATE(flags) == NON_SECURE) { 63528955d57SDan Handley WARN("BL1-FWU: Image done not allowed from normal world\n"); 63648bfb88eSYatharth Kochar return -EPERM; 63748bfb88eSYatharth Kochar } 63848bfb88eSYatharth Kochar 63928955d57SDan Handley /* Get the image descriptor for last executed secure image id */ 64028955d57SDan Handley image_desc = bl1_plat_get_image_desc(sec_exec_image_id); 64128955d57SDan Handley 64228955d57SDan Handley /* image_desc must correspond to a valid secure executing image */ 64328955d57SDan Handley assert(image_desc); 644843ddee4SYatharth Kochar assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE); 645843ddee4SYatharth Kochar assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE); 64628955d57SDan Handley assert(image_desc->state == IMAGE_STATE_EXECUTED); 64728955d57SDan Handley 648128daee2SAntonio Nino Diaz #if ENABLE_ASSERTIONS 649128daee2SAntonio Nino Diaz int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id); 650128daee2SAntonio Nino Diaz assert(rc == 0); 651128daee2SAntonio Nino Diaz #else 652128daee2SAntonio Nino Diaz bl1_fwu_remove_loaded_id(sec_exec_image_id); 653128daee2SAntonio Nino Diaz #endif 654128daee2SAntonio Nino Diaz 65548bfb88eSYatharth Kochar /* Update the flags. */ 65648bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_RESET; 65748bfb88eSYatharth Kochar sec_exec_image_id = INVALID_IMAGE_ID; 65848bfb88eSYatharth Kochar 659a4409008Sdp-arm INFO("BL1-FWU: Resuming Normal world context\n"); 660a4409008Sdp-arm #ifdef AARCH64 66148bfb88eSYatharth Kochar /* 66248bfb88eSYatharth Kochar * Secure world is done so no need to save the context. 66348bfb88eSYatharth Kochar * Just restore the Non-Secure context. 66448bfb88eSYatharth Kochar */ 66548bfb88eSYatharth Kochar cm_el1_sysregs_context_restore(NON_SECURE); 66648bfb88eSYatharth Kochar 66748bfb88eSYatharth Kochar /* Update the next context. */ 66848bfb88eSYatharth Kochar cm_set_next_eret_context(NON_SECURE); 66948bfb88eSYatharth Kochar 67048bfb88eSYatharth Kochar *handle = cm_get_context(NON_SECURE); 671a4409008Sdp-arm #else 672a4409008Sdp-arm /* Update the next context. */ 673a4409008Sdp-arm cm_set_next_context(cm_get_context(NON_SECURE)); 674a4409008Sdp-arm 675a4409008Sdp-arm /* Prepare the smc context for the next BL image. */ 676a4409008Sdp-arm smc_set_next_ctx(NON_SECURE); 677a4409008Sdp-arm 678a4409008Sdp-arm *handle = smc_get_ctx(NON_SECURE); 679a4409008Sdp-arm #endif 68048bfb88eSYatharth Kochar return 0; 68148bfb88eSYatharth Kochar } 68248bfb88eSYatharth Kochar 68348bfb88eSYatharth Kochar /******************************************************************************* 68448bfb88eSYatharth Kochar * This function provides the opportunity for users to perform any 68548bfb88eSYatharth Kochar * platform specific handling after the Firmware update is done. 68648bfb88eSYatharth Kochar ******************************************************************************/ 6871f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved) 68848bfb88eSYatharth Kochar { 68948bfb88eSYatharth Kochar NOTICE("BL1-FWU: *******FWU Process Completed*******\n"); 69048bfb88eSYatharth Kochar 69148bfb88eSYatharth Kochar /* 69248bfb88eSYatharth Kochar * Call platform done function. 69348bfb88eSYatharth Kochar */ 6941f37b944SDan Handley bl1_plat_fwu_done(client_cookie, reserved); 69548bfb88eSYatharth Kochar assert(0); 69648bfb88eSYatharth Kochar } 6979d6fc3c3SAntonio Nino Diaz 6989d6fc3c3SAntonio Nino Diaz /******************************************************************************* 6999d6fc3c3SAntonio Nino Diaz * This function resets an image to IMAGE_STATE_RESET. It fails if the image is 7009d6fc3c3SAntonio Nino Diaz * being executed. 7019d6fc3c3SAntonio Nino Diaz ******************************************************************************/ 7029d6fc3c3SAntonio Nino Diaz static int bl1_fwu_image_reset(unsigned int image_id, unsigned int flags) 7039d6fc3c3SAntonio Nino Diaz { 7049d6fc3c3SAntonio Nino Diaz image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 7059d6fc3c3SAntonio Nino Diaz 7069d6fc3c3SAntonio Nino Diaz if ((!image_desc) || (GET_SECURITY_STATE(flags) == SECURE)) { 7079d6fc3c3SAntonio Nino Diaz WARN("BL1-FWU: Reset not allowed due to invalid args\n"); 7089d6fc3c3SAntonio Nino Diaz return -EPERM; 7099d6fc3c3SAntonio Nino Diaz } 7109d6fc3c3SAntonio Nino Diaz 7119d6fc3c3SAntonio Nino Diaz switch (image_desc->state) { 7129d6fc3c3SAntonio Nino Diaz 7139d6fc3c3SAntonio Nino Diaz case IMAGE_STATE_RESET: 7149d6fc3c3SAntonio Nino Diaz /* Nothing to do. */ 7159d6fc3c3SAntonio Nino Diaz break; 7169d6fc3c3SAntonio Nino Diaz 7179d6fc3c3SAntonio Nino Diaz case IMAGE_STATE_INTERRUPTED: 7189d6fc3c3SAntonio Nino Diaz case IMAGE_STATE_AUTHENTICATED: 7199d6fc3c3SAntonio Nino Diaz case IMAGE_STATE_COPIED: 7209d6fc3c3SAntonio Nino Diaz case IMAGE_STATE_COPYING: 7219d6fc3c3SAntonio Nino Diaz 7229d6fc3c3SAntonio Nino Diaz if (bl1_fwu_remove_loaded_id(image_id)) { 7239d6fc3c3SAntonio Nino Diaz WARN("BL1-FWU: Image reset couldn't find the image ID\n"); 7249d6fc3c3SAntonio Nino Diaz return -EPERM; 7259d6fc3c3SAntonio Nino Diaz } 7269d6fc3c3SAntonio Nino Diaz 727ee05ae16SSoby Mathew if (image_desc->copied_size) { 728ee05ae16SSoby Mathew /* Clear the memory if the image is copied */ 729ee05ae16SSoby Mathew assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE); 730ee05ae16SSoby Mathew 7319d6fc3c3SAntonio Nino Diaz zero_normalmem((void *)image_desc->image_info.image_base, 7329d6fc3c3SAntonio Nino Diaz image_desc->copied_size); 7339d6fc3c3SAntonio Nino Diaz flush_dcache_range(image_desc->image_info.image_base, 7349d6fc3c3SAntonio Nino Diaz image_desc->copied_size); 735ee05ae16SSoby Mathew } 7369d6fc3c3SAntonio Nino Diaz 7379d6fc3c3SAntonio Nino Diaz /* Reset status variables */ 7389d6fc3c3SAntonio Nino Diaz image_desc->copied_size = 0; 7399d6fc3c3SAntonio Nino Diaz image_desc->image_info.image_size = 0; 7409d6fc3c3SAntonio Nino Diaz image_desc->state = IMAGE_STATE_RESET; 7419d6fc3c3SAntonio Nino Diaz 7429d6fc3c3SAntonio Nino Diaz /* Clear authentication state */ 7439d6fc3c3SAntonio Nino Diaz auth_img_flags[image_id] = 0; 7449d6fc3c3SAntonio Nino Diaz 7459d6fc3c3SAntonio Nino Diaz break; 7469d6fc3c3SAntonio Nino Diaz 7479d6fc3c3SAntonio Nino Diaz case IMAGE_STATE_EXECUTED: 7489d6fc3c3SAntonio Nino Diaz default: 7499d6fc3c3SAntonio Nino Diaz assert(0); 7509d6fc3c3SAntonio Nino Diaz } 7519d6fc3c3SAntonio Nino Diaz 7529d6fc3c3SAntonio Nino Diaz return 0; 7539d6fc3c3SAntonio Nino Diaz } 754