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 50a4409008Sdp-arm void cm_set_next_context(void *cpu_context); 51a4409008Sdp-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 /******************************************************************************* 94*128daee2SAntonio Nino Diaz * Utility functions to keep track of the images that are loaded at any time. 95*128daee2SAntonio Nino Diaz ******************************************************************************/ 96*128daee2SAntonio Nino Diaz 97*128daee2SAntonio Nino Diaz #ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES 98*128daee2SAntonio Nino Diaz #define FWU_MAX_SIMULTANEOUS_IMAGES PLAT_FWU_MAX_SIMULTANEOUS_IMAGES 99*128daee2SAntonio Nino Diaz #else 100*128daee2SAntonio Nino Diaz #define FWU_MAX_SIMULTANEOUS_IMAGES 10 101*128daee2SAntonio Nino Diaz #endif 102*128daee2SAntonio Nino Diaz 103*128daee2SAntonio Nino Diaz static int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = { 104*128daee2SAntonio Nino Diaz [0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID 105*128daee2SAntonio Nino Diaz }; 106*128daee2SAntonio Nino Diaz 107*128daee2SAntonio Nino Diaz /* 108*128daee2SAntonio Nino Diaz * Adds an image_id to the bl1_fwu_loaded_ids array. 109*128daee2SAntonio Nino Diaz * Returns 0 on success, 1 on error. 110*128daee2SAntonio Nino Diaz */ 111*128daee2SAntonio Nino Diaz static int bl1_fwu_add_loaded_id(int image_id) 112*128daee2SAntonio Nino Diaz { 113*128daee2SAntonio Nino Diaz int i; 114*128daee2SAntonio Nino Diaz 115*128daee2SAntonio Nino Diaz /* Check if the ID is already in the list */ 116*128daee2SAntonio Nino Diaz for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { 117*128daee2SAntonio Nino Diaz if (bl1_fwu_loaded_ids[i] == image_id) 118*128daee2SAntonio Nino Diaz return 0; 119*128daee2SAntonio Nino Diaz } 120*128daee2SAntonio Nino Diaz 121*128daee2SAntonio Nino Diaz /* Find an empty slot */ 122*128daee2SAntonio Nino Diaz for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { 123*128daee2SAntonio Nino Diaz if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) { 124*128daee2SAntonio Nino Diaz bl1_fwu_loaded_ids[i] = image_id; 125*128daee2SAntonio Nino Diaz return 0; 126*128daee2SAntonio Nino Diaz } 127*128daee2SAntonio Nino Diaz } 128*128daee2SAntonio Nino Diaz 129*128daee2SAntonio Nino Diaz return 1; 130*128daee2SAntonio Nino Diaz } 131*128daee2SAntonio Nino Diaz 132*128daee2SAntonio Nino Diaz /* 133*128daee2SAntonio Nino Diaz * Removes an image_id from the bl1_fwu_loaded_ids array. 134*128daee2SAntonio Nino Diaz * Returns 0 on success, 1 on error. 135*128daee2SAntonio Nino Diaz */ 136*128daee2SAntonio Nino Diaz static int bl1_fwu_remove_loaded_id(int image_id) 137*128daee2SAntonio Nino Diaz { 138*128daee2SAntonio Nino Diaz int i; 139*128daee2SAntonio Nino Diaz 140*128daee2SAntonio Nino Diaz /* Find the ID */ 141*128daee2SAntonio Nino Diaz for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { 142*128daee2SAntonio Nino Diaz if (bl1_fwu_loaded_ids[i] == image_id) { 143*128daee2SAntonio Nino Diaz bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID; 144*128daee2SAntonio Nino Diaz return 0; 145*128daee2SAntonio Nino Diaz } 146*128daee2SAntonio Nino Diaz } 147*128daee2SAntonio Nino Diaz 148*128daee2SAntonio Nino Diaz return 1; 149*128daee2SAntonio Nino Diaz } 150*128daee2SAntonio Nino Diaz 151*128daee2SAntonio Nino Diaz /******************************************************************************* 152*128daee2SAntonio Nino Diaz * This function checks if the specified image overlaps another image already 153*128daee2SAntonio Nino Diaz * loaded. It returns 0 if there is no overlap, a negative error code otherwise. 154*128daee2SAntonio Nino Diaz ******************************************************************************/ 155*128daee2SAntonio Nino Diaz static int bl1_fwu_image_check_overlaps(int image_id) 156*128daee2SAntonio Nino Diaz { 157*128daee2SAntonio Nino Diaz const image_desc_t *image_desc, *checked_image_desc; 158*128daee2SAntonio Nino Diaz const image_info_t *info, *checked_info; 159*128daee2SAntonio Nino Diaz 160*128daee2SAntonio Nino Diaz uintptr_t image_base, image_end; 161*128daee2SAntonio Nino Diaz uintptr_t checked_image_base, checked_image_end; 162*128daee2SAntonio Nino Diaz 163*128daee2SAntonio Nino Diaz checked_image_desc = bl1_plat_get_image_desc(image_id); 164*128daee2SAntonio Nino Diaz checked_info = &checked_image_desc->image_info; 165*128daee2SAntonio Nino Diaz 166*128daee2SAntonio Nino Diaz /* Image being checked mustn't be empty. */ 167*128daee2SAntonio Nino Diaz assert(checked_info->image_size != 0); 168*128daee2SAntonio Nino Diaz 169*128daee2SAntonio Nino Diaz checked_image_base = checked_info->image_base; 170*128daee2SAntonio Nino Diaz checked_image_end = checked_image_base + checked_info->image_size - 1; 171*128daee2SAntonio Nino Diaz /* No need to check for overlaps, it's done in bl1_fwu_image_copy(). */ 172*128daee2SAntonio Nino Diaz 173*128daee2SAntonio Nino Diaz for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { 174*128daee2SAntonio Nino Diaz 175*128daee2SAntonio Nino Diaz /* Don't check image against itself. */ 176*128daee2SAntonio Nino Diaz if (bl1_fwu_loaded_ids[i] == image_id) 177*128daee2SAntonio Nino Diaz continue; 178*128daee2SAntonio Nino Diaz 179*128daee2SAntonio Nino Diaz image_desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]); 180*128daee2SAntonio Nino Diaz 181*128daee2SAntonio Nino Diaz /* Only check images that are loaded or being loaded. */ 182*128daee2SAntonio Nino Diaz assert (image_desc->state != IMAGE_STATE_RESET); 183*128daee2SAntonio Nino Diaz 184*128daee2SAntonio Nino Diaz info = &image_desc->image_info; 185*128daee2SAntonio Nino Diaz 186*128daee2SAntonio Nino Diaz /* There cannot be overlaps with an empty image. */ 187*128daee2SAntonio Nino Diaz if (info->image_size == 0) 188*128daee2SAntonio Nino Diaz continue; 189*128daee2SAntonio Nino Diaz 190*128daee2SAntonio Nino Diaz image_base = info->image_base; 191*128daee2SAntonio Nino Diaz image_end = image_base + info->image_size - 1; 192*128daee2SAntonio Nino Diaz /* 193*128daee2SAntonio Nino Diaz * Overflows cannot happen. It is checked in 194*128daee2SAntonio Nino Diaz * bl1_fwu_image_copy() when the image goes from RESET to 195*128daee2SAntonio Nino Diaz * COPYING or COPIED. 196*128daee2SAntonio Nino Diaz */ 197*128daee2SAntonio Nino Diaz assert (image_end > image_base); 198*128daee2SAntonio Nino Diaz 199*128daee2SAntonio Nino Diaz /* Check if there are overlaps. */ 200*128daee2SAntonio Nino Diaz if (!(image_end < checked_image_base || 201*128daee2SAntonio Nino Diaz checked_image_end < image_base)) { 202*128daee2SAntonio Nino Diaz VERBOSE("Image with ID %d overlaps existing image with ID %d", 203*128daee2SAntonio Nino Diaz checked_image_desc->image_id, image_desc->image_id); 204*128daee2SAntonio Nino Diaz return -EPERM; 205*128daee2SAntonio Nino Diaz } 206*128daee2SAntonio Nino Diaz } 207*128daee2SAntonio Nino Diaz 208*128daee2SAntonio Nino Diaz return 0; 209*128daee2SAntonio Nino Diaz } 210*128daee2SAntonio Nino Diaz 211*128daee2SAntonio Nino Diaz /******************************************************************************* 21248bfb88eSYatharth Kochar * This function is responsible for copying secure images in AP Secure RAM. 21348bfb88eSYatharth Kochar ******************************************************************************/ 21448bfb88eSYatharth Kochar static int bl1_fwu_image_copy(unsigned int image_id, 21548bfb88eSYatharth Kochar uintptr_t image_src, 21648bfb88eSYatharth Kochar unsigned int block_size, 21748bfb88eSYatharth Kochar unsigned int image_size, 21848bfb88eSYatharth Kochar unsigned int flags) 21948bfb88eSYatharth Kochar { 2209f1489e4SSandrine Bailleux uintptr_t dest_addr; 221b38a9e5cSSandrine Bailleux unsigned int remaining; 22248bfb88eSYatharth Kochar 22348bfb88eSYatharth Kochar /* Get the image descriptor. */ 22448bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 2259f1489e4SSandrine Bailleux if (!image_desc) { 2269f1489e4SSandrine Bailleux WARN("BL1-FWU: Invalid image ID %u\n", image_id); 22748bfb88eSYatharth Kochar return -EPERM; 22848bfb88eSYatharth Kochar } 22948bfb88eSYatharth Kochar 2309f1489e4SSandrine Bailleux /* 2319f1489e4SSandrine Bailleux * The request must originate from a non-secure caller and target a 2329f1489e4SSandrine Bailleux * secure image. Any other scenario is invalid. 2339f1489e4SSandrine Bailleux */ 2349f1489e4SSandrine Bailleux if (GET_SECURITY_STATE(flags) == SECURE) { 2359f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed from secure world.\n"); 2369f1489e4SSandrine Bailleux return -EPERM; 2379f1489e4SSandrine Bailleux } 2389f1489e4SSandrine Bailleux if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) { 2399f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed for non-secure images.\n"); 2409f1489e4SSandrine Bailleux return -EPERM; 2419f1489e4SSandrine Bailleux } 2429f1489e4SSandrine Bailleux 2439f1489e4SSandrine Bailleux /* Check whether the FWU state machine is in the correct state. */ 2449f1489e4SSandrine Bailleux if ((image_desc->state != IMAGE_STATE_RESET) && 2459f1489e4SSandrine Bailleux (image_desc->state != IMAGE_STATE_COPYING)) { 2469f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed at this point of the FWU" 2479f1489e4SSandrine Bailleux " process.\n"); 24848bfb88eSYatharth Kochar return -EPERM; 24948bfb88eSYatharth Kochar } 25048bfb88eSYatharth Kochar 251949a52d2SSandrine Bailleux if ((!image_src) || (!block_size) || 252949a52d2SSandrine Bailleux check_uptr_overflow(image_src, block_size - 1)) { 25348bfb88eSYatharth Kochar WARN("BL1-FWU: Copy not allowed due to invalid image source" 25448bfb88eSYatharth Kochar " or block size\n"); 25548bfb88eSYatharth Kochar return -ENOMEM; 25648bfb88eSYatharth Kochar } 25748bfb88eSYatharth Kochar 25848bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPYING) { 2591bfb7068SSandrine Bailleux /* 2601bfb7068SSandrine Bailleux * There must have been at least 1 copy operation for this image 2611bfb7068SSandrine Bailleux * previously. 2621bfb7068SSandrine Bailleux */ 2631bfb7068SSandrine Bailleux assert(image_desc->copied_size != 0); 2641bfb7068SSandrine Bailleux /* 2651bfb7068SSandrine Bailleux * The image size must have been recorded in the 1st copy 2661bfb7068SSandrine Bailleux * operation. 2671bfb7068SSandrine Bailleux */ 2689f1489e4SSandrine Bailleux image_size = image_desc->image_info.image_size; 2691bfb7068SSandrine Bailleux assert(image_size != 0); 2701bfb7068SSandrine Bailleux assert(image_desc->copied_size < image_size); 2711bfb7068SSandrine Bailleux 27248bfb88eSYatharth Kochar INFO("BL1-FWU: Continuing image copy in blocks\n"); 2739f1489e4SSandrine Bailleux } else { /* image_desc->state == IMAGE_STATE_RESET */ 2749f1489e4SSandrine Bailleux INFO("BL1-FWU: Initial call to copy an image\n"); 27548bfb88eSYatharth Kochar 2769f1489e4SSandrine Bailleux /* 2779f1489e4SSandrine Bailleux * image_size is relevant only for the 1st copy request, it is 2789f1489e4SSandrine Bailleux * then ignored for subsequent calls for this image. 2799f1489e4SSandrine Bailleux */ 28048bfb88eSYatharth Kochar if (!image_size) { 2819f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed due to invalid image" 2829f1489e4SSandrine Bailleux " size\n"); 28348bfb88eSYatharth Kochar return -ENOMEM; 28448bfb88eSYatharth Kochar } 28548bfb88eSYatharth Kochar 28653d703a5SYatharth Kochar #if LOAD_IMAGE_V2 28753d703a5SYatharth Kochar /* Check that the image size to load is within limit */ 28853d703a5SYatharth Kochar if (image_size > image_desc->image_info.image_max_size) { 28953d703a5SYatharth Kochar WARN("BL1-FWU: Image size out of bounds\n"); 29053d703a5SYatharth Kochar return -ENOMEM; 29153d703a5SYatharth Kochar } 29253d703a5SYatharth Kochar #else 293949a52d2SSandrine Bailleux /* 294949a52d2SSandrine Bailleux * Check the image will fit into the free trusted RAM after BL1 295949a52d2SSandrine Bailleux * load. 296949a52d2SSandrine Bailleux */ 2979f1489e4SSandrine Bailleux const meminfo_t *mem_layout = bl1_plat_sec_mem_layout(); 298949a52d2SSandrine Bailleux if (!is_mem_free(mem_layout->free_base, mem_layout->free_size, 299949a52d2SSandrine Bailleux image_desc->image_info.image_base, 300949a52d2SSandrine Bailleux image_size)) { 3019f1489e4SSandrine Bailleux WARN("BL1-FWU: Copy not allowed due to insufficient" 3029f1489e4SSandrine Bailleux " resources.\n"); 30348bfb88eSYatharth Kochar return -ENOMEM; 30448bfb88eSYatharth Kochar } 30553d703a5SYatharth Kochar #endif 30648bfb88eSYatharth Kochar 3079f1489e4SSandrine Bailleux /* Save the given image size. */ 30848bfb88eSYatharth Kochar image_desc->image_info.image_size = image_size; 30948bfb88eSYatharth Kochar 310*128daee2SAntonio Nino Diaz /* Make sure the image doesn't overlap other images. */ 311*128daee2SAntonio Nino Diaz if (bl1_fwu_image_check_overlaps(image_id)) { 312*128daee2SAntonio Nino Diaz image_desc->image_info.image_size = 0; 313*128daee2SAntonio Nino Diaz WARN("BL1-FWU: This image overlaps another one\n"); 314*128daee2SAntonio Nino Diaz return -EPERM; 315*128daee2SAntonio Nino Diaz } 316*128daee2SAntonio Nino Diaz 317b38a9e5cSSandrine Bailleux /* 318b38a9e5cSSandrine Bailleux * copied_size must be explicitly initialized here because the 319b38a9e5cSSandrine Bailleux * FWU code doesn't necessarily do it when it resets the state 320b38a9e5cSSandrine Bailleux * machine. 321b38a9e5cSSandrine Bailleux */ 322b38a9e5cSSandrine Bailleux image_desc->copied_size = 0; 323b38a9e5cSSandrine Bailleux } 324b38a9e5cSSandrine Bailleux 325b38a9e5cSSandrine Bailleux /* 326b38a9e5cSSandrine Bailleux * If the given block size is more than the total image size 327b38a9e5cSSandrine Bailleux * then clip the former to the latter. 328b38a9e5cSSandrine Bailleux */ 329b38a9e5cSSandrine Bailleux remaining = image_size - image_desc->copied_size; 330b38a9e5cSSandrine Bailleux if (block_size > remaining) { 331b38a9e5cSSandrine Bailleux WARN("BL1-FWU: Block size is too big, clipping it.\n"); 332b38a9e5cSSandrine Bailleux block_size = remaining; 333b38a9e5cSSandrine Bailleux } 334b38a9e5cSSandrine Bailleux 335b38a9e5cSSandrine Bailleux /* Make sure the source image is mapped in memory. */ 336b38a9e5cSSandrine Bailleux if (bl1_plat_mem_check(image_src, block_size, flags)) { 337b38a9e5cSSandrine Bailleux WARN("BL1-FWU: Source image is not mapped.\n"); 338b38a9e5cSSandrine Bailleux return -ENOMEM; 339b38a9e5cSSandrine Bailleux } 340b38a9e5cSSandrine Bailleux 341*128daee2SAntonio Nino Diaz if (bl1_fwu_add_loaded_id(image_id)) { 342*128daee2SAntonio Nino Diaz WARN("BL1-FWU: Too many images loaded at the same time.\n"); 343*128daee2SAntonio Nino Diaz return -ENOMEM; 344*128daee2SAntonio Nino Diaz } 345*128daee2SAntonio Nino Diaz 346b38a9e5cSSandrine Bailleux /* Everything looks sane. Go ahead and copy the block of data. */ 347b38a9e5cSSandrine Bailleux dest_addr = image_desc->image_info.image_base + image_desc->copied_size; 3489f1489e4SSandrine Bailleux memcpy((void *) dest_addr, (const void *) image_src, block_size); 3499f1489e4SSandrine Bailleux flush_dcache_range(dest_addr, block_size); 35048bfb88eSYatharth Kochar 351b38a9e5cSSandrine Bailleux image_desc->copied_size += block_size; 352b38a9e5cSSandrine Bailleux image_desc->state = (block_size == remaining) ? 353b38a9e5cSSandrine Bailleux IMAGE_STATE_COPIED : IMAGE_STATE_COPYING; 35448bfb88eSYatharth Kochar 355b38a9e5cSSandrine Bailleux INFO("BL1-FWU: Copy operation successful.\n"); 35648bfb88eSYatharth Kochar return 0; 35748bfb88eSYatharth Kochar } 35848bfb88eSYatharth Kochar 35948bfb88eSYatharth Kochar /******************************************************************************* 36048bfb88eSYatharth Kochar * This function is responsible for authenticating Normal/Secure images. 36148bfb88eSYatharth Kochar ******************************************************************************/ 36248bfb88eSYatharth Kochar static int bl1_fwu_image_auth(unsigned int image_id, 36348bfb88eSYatharth Kochar uintptr_t image_src, 36448bfb88eSYatharth Kochar unsigned int image_size, 36548bfb88eSYatharth Kochar unsigned int flags) 36648bfb88eSYatharth Kochar { 36748bfb88eSYatharth Kochar int result; 36848bfb88eSYatharth Kochar uintptr_t base_addr; 36948bfb88eSYatharth Kochar unsigned int total_size; 37048bfb88eSYatharth Kochar 37148bfb88eSYatharth Kochar /* Get the image descriptor. */ 37248bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 37348bfb88eSYatharth Kochar if (!image_desc) 37448bfb88eSYatharth Kochar return -EPERM; 37548bfb88eSYatharth Kochar 376843ddee4SYatharth Kochar if (GET_SECURITY_STATE(flags) == SECURE) { 37748bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_RESET) { 37848bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication from secure world " 37948bfb88eSYatharth Kochar "while in invalid state\n"); 38048bfb88eSYatharth Kochar return -EPERM; 38148bfb88eSYatharth Kochar } 38248bfb88eSYatharth Kochar } else { 383843ddee4SYatharth Kochar if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) { 38448bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_COPIED) { 38548bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication of secure image " 38648bfb88eSYatharth Kochar "from non-secure world while not in copied state\n"); 38748bfb88eSYatharth Kochar return -EPERM; 38848bfb88eSYatharth Kochar } 38948bfb88eSYatharth Kochar } else { 39048bfb88eSYatharth Kochar if (image_desc->state != IMAGE_STATE_RESET) { 39148bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication of non-secure image " 39248bfb88eSYatharth Kochar "from non-secure world while in invalid state\n"); 39348bfb88eSYatharth Kochar return -EPERM; 39448bfb88eSYatharth Kochar } 39548bfb88eSYatharth Kochar } 39648bfb88eSYatharth Kochar } 39748bfb88eSYatharth Kochar 39848bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPIED) { 39948bfb88eSYatharth Kochar /* 40048bfb88eSYatharth Kochar * Image is in COPIED state. 40148bfb88eSYatharth Kochar * Use the stored address and size. 40248bfb88eSYatharth Kochar */ 40348bfb88eSYatharth Kochar base_addr = image_desc->image_info.image_base; 40448bfb88eSYatharth Kochar total_size = image_desc->image_info.image_size; 40548bfb88eSYatharth Kochar } else { 406949a52d2SSandrine Bailleux if ((!image_src) || (!image_size) || 407949a52d2SSandrine Bailleux check_uptr_overflow(image_src, image_size - 1)) { 40848bfb88eSYatharth Kochar WARN("BL1-FWU: Auth not allowed due to invalid" 40948bfb88eSYatharth Kochar " image source/size\n"); 41048bfb88eSYatharth Kochar return -ENOMEM; 41148bfb88eSYatharth Kochar } 41248bfb88eSYatharth Kochar 41348bfb88eSYatharth Kochar /* 41448bfb88eSYatharth Kochar * Image is in RESET state. 41548bfb88eSYatharth Kochar * Check the parameters and authenticate the source image in place. 41648bfb88eSYatharth Kochar */ 41703131c85SDan Handley if (bl1_plat_mem_check(image_src, image_size, \ 41803131c85SDan Handley image_desc->ep_info.h.attr)) { 41948bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication arguments source/size not mapped\n"); 42048bfb88eSYatharth Kochar return -ENOMEM; 42148bfb88eSYatharth Kochar } 42248bfb88eSYatharth Kochar 423*128daee2SAntonio Nino Diaz if (bl1_fwu_add_loaded_id(image_id)) { 424*128daee2SAntonio Nino Diaz WARN("BL1-FWU: Too many images loaded at the same time.\n"); 425*128daee2SAntonio Nino Diaz return -ENOMEM; 426*128daee2SAntonio Nino Diaz } 427*128daee2SAntonio Nino Diaz 42848bfb88eSYatharth Kochar base_addr = image_src; 42948bfb88eSYatharth Kochar total_size = image_size; 43048bfb88eSYatharth Kochar 43148bfb88eSYatharth Kochar /* Update the image size in the descriptor. */ 43248bfb88eSYatharth Kochar image_desc->image_info.image_size = total_size; 43348bfb88eSYatharth Kochar } 43448bfb88eSYatharth Kochar 43548bfb88eSYatharth Kochar /* 43648bfb88eSYatharth Kochar * Authenticate the image. 43748bfb88eSYatharth Kochar */ 43848bfb88eSYatharth Kochar INFO("BL1-FWU: Authenticating image_id:%d\n", image_id); 43948bfb88eSYatharth Kochar result = auth_mod_verify_img(image_id, (void *)base_addr, total_size); 44048bfb88eSYatharth Kochar if (result != 0) { 44148bfb88eSYatharth Kochar WARN("BL1-FWU: Authentication Failed err=%d\n", result); 44248bfb88eSYatharth Kochar 44348bfb88eSYatharth Kochar /* 44448bfb88eSYatharth Kochar * Authentication has failed. 44548bfb88eSYatharth Kochar * Clear the memory if the image was copied. 44648bfb88eSYatharth Kochar * This is to prevent an attack where this contains 44748bfb88eSYatharth Kochar * some malicious code that can somehow be executed later. 44848bfb88eSYatharth Kochar */ 44948bfb88eSYatharth Kochar if (image_desc->state == IMAGE_STATE_COPIED) { 45048bfb88eSYatharth Kochar /* Clear the memory.*/ 451308d359bSDouglas Raillard zero_normalmem((void *)base_addr, total_size); 45248bfb88eSYatharth Kochar flush_dcache_range(base_addr, total_size); 45348bfb88eSYatharth Kochar 45448bfb88eSYatharth Kochar /* Indicate that image can be copied again*/ 45548bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_RESET; 45648bfb88eSYatharth Kochar } 457*128daee2SAntonio Nino Diaz 458*128daee2SAntonio Nino Diaz /* 459*128daee2SAntonio Nino Diaz * Even if this fails it's ok because the ID isn't in the array. 460*128daee2SAntonio Nino Diaz * The image cannot be in RESET state here, it is checked at the 461*128daee2SAntonio Nino Diaz * beginning of the function. 462*128daee2SAntonio Nino Diaz */ 463*128daee2SAntonio Nino Diaz bl1_fwu_remove_loaded_id(image_id); 46448bfb88eSYatharth Kochar return -EAUTH; 46548bfb88eSYatharth Kochar } 46648bfb88eSYatharth Kochar 46748bfb88eSYatharth Kochar /* Indicate that image is in authenticated state. */ 46848bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_AUTHENTICATED; 46948bfb88eSYatharth Kochar 47048bfb88eSYatharth Kochar /* 47148bfb88eSYatharth Kochar * Flush image_info to memory so that other 47248bfb88eSYatharth Kochar * secure world images can see changes. 47348bfb88eSYatharth Kochar */ 47448bfb88eSYatharth Kochar flush_dcache_range((unsigned long)&image_desc->image_info, 47548bfb88eSYatharth Kochar sizeof(image_info_t)); 47648bfb88eSYatharth Kochar 47748bfb88eSYatharth Kochar INFO("BL1-FWU: Authentication was successful\n"); 47848bfb88eSYatharth Kochar 47948bfb88eSYatharth Kochar return 0; 48048bfb88eSYatharth Kochar } 48148bfb88eSYatharth Kochar 48248bfb88eSYatharth Kochar /******************************************************************************* 48348bfb88eSYatharth Kochar * This function is responsible for executing Secure images. 48448bfb88eSYatharth Kochar ******************************************************************************/ 48548bfb88eSYatharth Kochar static int bl1_fwu_image_execute(unsigned int image_id, 48648bfb88eSYatharth Kochar void **handle, 48748bfb88eSYatharth Kochar unsigned int flags) 48848bfb88eSYatharth Kochar { 48948bfb88eSYatharth Kochar /* Get the image descriptor. */ 49048bfb88eSYatharth Kochar image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); 49148bfb88eSYatharth Kochar 49248bfb88eSYatharth Kochar /* 49348bfb88eSYatharth Kochar * Execution is NOT allowed if: 49428955d57SDan Handley * image_id is invalid OR 49548bfb88eSYatharth Kochar * Caller is from Secure world OR 49648bfb88eSYatharth Kochar * Image is Non-Secure OR 49748bfb88eSYatharth Kochar * Image is Non-Executable OR 49848bfb88eSYatharth Kochar * Image is NOT in AUTHENTICATED state. 49948bfb88eSYatharth Kochar */ 50048bfb88eSYatharth Kochar if ((!image_desc) || 501843ddee4SYatharth Kochar (GET_SECURITY_STATE(flags) == SECURE) || 502843ddee4SYatharth Kochar (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) || 503843ddee4SYatharth Kochar (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) || 50448bfb88eSYatharth Kochar (image_desc->state != IMAGE_STATE_AUTHENTICATED)) { 50548bfb88eSYatharth Kochar WARN("BL1-FWU: Execution not allowed due to invalid state/args\n"); 50648bfb88eSYatharth Kochar return -EPERM; 50748bfb88eSYatharth Kochar } 50848bfb88eSYatharth Kochar 50948bfb88eSYatharth Kochar INFO("BL1-FWU: Executing Secure image\n"); 51048bfb88eSYatharth Kochar 511a4409008Sdp-arm #ifdef AARCH64 51248bfb88eSYatharth Kochar /* Save NS-EL1 system registers. */ 51348bfb88eSYatharth Kochar cm_el1_sysregs_context_save(NON_SECURE); 514a4409008Sdp-arm #endif 51548bfb88eSYatharth Kochar 51648bfb88eSYatharth Kochar /* Prepare the image for execution. */ 51748bfb88eSYatharth Kochar bl1_prepare_next_image(image_id); 51848bfb88eSYatharth Kochar 51948bfb88eSYatharth Kochar /* Update the secure image id. */ 52048bfb88eSYatharth Kochar sec_exec_image_id = image_id; 52148bfb88eSYatharth Kochar 522a4409008Sdp-arm #ifdef AARCH64 52348bfb88eSYatharth Kochar *handle = cm_get_context(SECURE); 524a4409008Sdp-arm #else 525a4409008Sdp-arm *handle = smc_get_ctx(SECURE); 526a4409008Sdp-arm #endif 52748bfb88eSYatharth Kochar return 0; 52848bfb88eSYatharth Kochar } 52948bfb88eSYatharth Kochar 53048bfb88eSYatharth Kochar /******************************************************************************* 53128955d57SDan Handley * This function is responsible for resuming execution in the other security 53228955d57SDan Handley * world 53348bfb88eSYatharth Kochar ******************************************************************************/ 53428955d57SDan Handley static register_t bl1_fwu_image_resume(register_t image_param, 53548bfb88eSYatharth Kochar void **handle, 53648bfb88eSYatharth Kochar unsigned int flags) 53748bfb88eSYatharth Kochar { 53848bfb88eSYatharth Kochar image_desc_t *image_desc; 53948bfb88eSYatharth Kochar unsigned int resume_sec_state; 540843ddee4SYatharth Kochar unsigned int caller_sec_state = GET_SECURITY_STATE(flags); 54148bfb88eSYatharth Kochar 54248bfb88eSYatharth Kochar /* Get the image descriptor for last executed secure image id. */ 54348bfb88eSYatharth Kochar image_desc = bl1_plat_get_image_desc(sec_exec_image_id); 54428955d57SDan Handley if (caller_sec_state == NON_SECURE) { 54528955d57SDan Handley if (!image_desc) { 54628955d57SDan Handley WARN("BL1-FWU: Resume not allowed due to no available" 54728955d57SDan Handley "secure image\n"); 54848bfb88eSYatharth Kochar return -EPERM; 54948bfb88eSYatharth Kochar } 55028955d57SDan Handley } else { 55128955d57SDan Handley /* image_desc must be valid for secure world callers */ 55228955d57SDan Handley assert(image_desc); 55328955d57SDan Handley } 55428955d57SDan Handley 555843ddee4SYatharth Kochar assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE); 556843ddee4SYatharth Kochar assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE); 55728955d57SDan Handley 55828955d57SDan Handley if (caller_sec_state == SECURE) { 55928955d57SDan Handley assert(image_desc->state == IMAGE_STATE_EXECUTED); 56048bfb88eSYatharth Kochar 56148bfb88eSYatharth Kochar /* Update the flags. */ 56248bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_INTERRUPTED; 56348bfb88eSYatharth Kochar resume_sec_state = NON_SECURE; 56448bfb88eSYatharth Kochar } else { 56528955d57SDan Handley assert(image_desc->state == IMAGE_STATE_INTERRUPTED); 56648bfb88eSYatharth Kochar 56748bfb88eSYatharth Kochar /* Update the flags. */ 56848bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_EXECUTED; 56948bfb88eSYatharth Kochar resume_sec_state = SECURE; 57048bfb88eSYatharth Kochar } 57148bfb88eSYatharth Kochar 572a4409008Sdp-arm INFO("BL1-FWU: Resuming %s world context\n", 573a4409008Sdp-arm (resume_sec_state == SECURE) ? "secure" : "normal"); 574a4409008Sdp-arm 575a4409008Sdp-arm #ifdef AARCH64 57648bfb88eSYatharth Kochar /* Save the EL1 system registers of calling world. */ 57728955d57SDan Handley cm_el1_sysregs_context_save(caller_sec_state); 57848bfb88eSYatharth Kochar 57948bfb88eSYatharth Kochar /* Restore the EL1 system registers of resuming world. */ 58048bfb88eSYatharth Kochar cm_el1_sysregs_context_restore(resume_sec_state); 58148bfb88eSYatharth Kochar 58248bfb88eSYatharth Kochar /* Update the next context. */ 58348bfb88eSYatharth Kochar cm_set_next_eret_context(resume_sec_state); 58448bfb88eSYatharth Kochar 58548bfb88eSYatharth Kochar *handle = cm_get_context(resume_sec_state); 586a4409008Sdp-arm #else 587a4409008Sdp-arm /* Update the next context. */ 588a4409008Sdp-arm cm_set_next_context(cm_get_context(resume_sec_state)); 589a4409008Sdp-arm 590a4409008Sdp-arm /* Prepare the smc context for the next BL image. */ 591a4409008Sdp-arm smc_set_next_ctx(resume_sec_state); 592a4409008Sdp-arm 593a4409008Sdp-arm *handle = smc_get_ctx(resume_sec_state); 594a4409008Sdp-arm #endif 59548bfb88eSYatharth Kochar return image_param; 59648bfb88eSYatharth Kochar } 59748bfb88eSYatharth Kochar 59848bfb88eSYatharth Kochar /******************************************************************************* 59948bfb88eSYatharth Kochar * This function is responsible for resuming normal world context. 60048bfb88eSYatharth Kochar ******************************************************************************/ 60148bfb88eSYatharth Kochar static int bl1_fwu_sec_image_done(void **handle, unsigned int flags) 60248bfb88eSYatharth Kochar { 60328955d57SDan Handley image_desc_t *image_desc; 60448bfb88eSYatharth Kochar 60528955d57SDan Handley /* Make sure caller is from the secure world */ 606843ddee4SYatharth Kochar if (GET_SECURITY_STATE(flags) == NON_SECURE) { 60728955d57SDan Handley WARN("BL1-FWU: Image done not allowed from normal world\n"); 60848bfb88eSYatharth Kochar return -EPERM; 60948bfb88eSYatharth Kochar } 61048bfb88eSYatharth Kochar 61128955d57SDan Handley /* Get the image descriptor for last executed secure image id */ 61228955d57SDan Handley image_desc = bl1_plat_get_image_desc(sec_exec_image_id); 61328955d57SDan Handley 61428955d57SDan Handley /* image_desc must correspond to a valid secure executing image */ 61528955d57SDan Handley assert(image_desc); 616843ddee4SYatharth Kochar assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE); 617843ddee4SYatharth Kochar assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE); 61828955d57SDan Handley assert(image_desc->state == IMAGE_STATE_EXECUTED); 61928955d57SDan Handley 620*128daee2SAntonio Nino Diaz #if ENABLE_ASSERTIONS 621*128daee2SAntonio Nino Diaz int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id); 622*128daee2SAntonio Nino Diaz assert(rc == 0); 623*128daee2SAntonio Nino Diaz #else 624*128daee2SAntonio Nino Diaz bl1_fwu_remove_loaded_id(sec_exec_image_id); 625*128daee2SAntonio Nino Diaz #endif 626*128daee2SAntonio Nino Diaz 62748bfb88eSYatharth Kochar /* Update the flags. */ 62848bfb88eSYatharth Kochar image_desc->state = IMAGE_STATE_RESET; 62948bfb88eSYatharth Kochar sec_exec_image_id = INVALID_IMAGE_ID; 63048bfb88eSYatharth Kochar 631a4409008Sdp-arm INFO("BL1-FWU: Resuming Normal world context\n"); 632a4409008Sdp-arm #ifdef AARCH64 63348bfb88eSYatharth Kochar /* 63448bfb88eSYatharth Kochar * Secure world is done so no need to save the context. 63548bfb88eSYatharth Kochar * Just restore the Non-Secure context. 63648bfb88eSYatharth Kochar */ 63748bfb88eSYatharth Kochar cm_el1_sysregs_context_restore(NON_SECURE); 63848bfb88eSYatharth Kochar 63948bfb88eSYatharth Kochar /* Update the next context. */ 64048bfb88eSYatharth Kochar cm_set_next_eret_context(NON_SECURE); 64148bfb88eSYatharth Kochar 64248bfb88eSYatharth Kochar *handle = cm_get_context(NON_SECURE); 643a4409008Sdp-arm #else 644a4409008Sdp-arm /* Update the next context. */ 645a4409008Sdp-arm cm_set_next_context(cm_get_context(NON_SECURE)); 646a4409008Sdp-arm 647a4409008Sdp-arm /* Prepare the smc context for the next BL image. */ 648a4409008Sdp-arm smc_set_next_ctx(NON_SECURE); 649a4409008Sdp-arm 650a4409008Sdp-arm *handle = smc_get_ctx(NON_SECURE); 651a4409008Sdp-arm #endif 65248bfb88eSYatharth Kochar return 0; 65348bfb88eSYatharth Kochar } 65448bfb88eSYatharth Kochar 65548bfb88eSYatharth Kochar /******************************************************************************* 65648bfb88eSYatharth Kochar * This function provides the opportunity for users to perform any 65748bfb88eSYatharth Kochar * platform specific handling after the Firmware update is done. 65848bfb88eSYatharth Kochar ******************************************************************************/ 6591f37b944SDan Handley __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved) 66048bfb88eSYatharth Kochar { 66148bfb88eSYatharth Kochar NOTICE("BL1-FWU: *******FWU Process Completed*******\n"); 66248bfb88eSYatharth Kochar 66348bfb88eSYatharth Kochar /* 66448bfb88eSYatharth Kochar * Call platform done function. 66548bfb88eSYatharth Kochar */ 6661f37b944SDan Handley bl1_plat_fwu_done(client_cookie, reserved); 66748bfb88eSYatharth Kochar assert(0); 66848bfb88eSYatharth Kochar } 669