Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 1 | /* |
Zelalem | 2fe75a2 | 2020-02-12 10:37:03 -0600 | [diff] [blame] | 2 | * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved. |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 3 | * |
dp-arm | 82cb2c1 | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
Isla Mitchell | 2a4b4b7 | 2017-07-11 14:54:08 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 8 | #include <errno.h> |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 9 | #include <string.h> |
Antonio Nino Diaz | 09d40e0 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
| 11 | #include <platform_def.h> |
| 12 | |
| 13 | #include <arch_helpers.h> |
| 14 | #include <bl1/bl1.h> |
| 15 | #include <common/bl_common.h> |
| 16 | #include <common/debug.h> |
| 17 | #include <context.h> |
| 18 | #include <drivers/auth/auth_mod.h> |
| 19 | #include <lib/el3_runtime/context_mgmt.h> |
| 20 | #include <lib/utils.h> |
| 21 | #include <plat/common/platform.h> |
| 22 | #include <smccc_helpers.h> |
| 23 | |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 24 | #include "bl1_private.h" |
| 25 | |
| 26 | /* |
| 27 | * Function declarations. |
| 28 | */ |
| 29 | static int bl1_fwu_image_copy(unsigned int image_id, |
Roberto Vargas | 735181b | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 30 | uintptr_t image_src, |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 31 | unsigned int block_size, |
| 32 | unsigned int image_size, |
| 33 | unsigned int flags); |
| 34 | static int bl1_fwu_image_auth(unsigned int image_id, |
Roberto Vargas | 735181b | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 35 | uintptr_t image_src, |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 36 | unsigned int image_size, |
| 37 | unsigned int flags); |
| 38 | static int bl1_fwu_image_execute(unsigned int image_id, |
| 39 | void **handle, |
| 40 | unsigned int flags); |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 41 | static register_t bl1_fwu_image_resume(register_t image_param, |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 42 | void **handle, |
| 43 | unsigned int flags); |
| 44 | static int bl1_fwu_sec_image_done(void **handle, |
| 45 | unsigned int flags); |
Antonio Nino Diaz | 9d6fc3c | 2017-05-12 16:51:59 +0100 | [diff] [blame] | 46 | static int bl1_fwu_image_reset(unsigned int image_id, |
| 47 | unsigned int flags); |
Dan Handley | 1f37b94 | 2015-12-15 14:28:24 +0000 | [diff] [blame] | 48 | __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 49 | |
| 50 | /* |
| 51 | * This keeps track of last executed secure image id. |
| 52 | */ |
| 53 | static unsigned int sec_exec_image_id = INVALID_IMAGE_ID; |
| 54 | |
| 55 | /******************************************************************************* |
| 56 | * Top level handler for servicing FWU SMCs. |
| 57 | ******************************************************************************/ |
Zelalem | 2fe75a2 | 2020-02-12 10:37:03 -0600 | [diff] [blame] | 58 | u_register_t bl1_fwu_smc_handler(unsigned int smc_fid, |
| 59 | u_register_t x1, |
| 60 | u_register_t x2, |
| 61 | u_register_t x3, |
| 62 | u_register_t x4, |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 63 | void *cookie, |
| 64 | void *handle, |
| 65 | unsigned int flags) |
| 66 | { |
| 67 | |
| 68 | switch (smc_fid) { |
| 69 | case FWU_SMC_IMAGE_COPY: |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 70 | SMC_RET1(handle, bl1_fwu_image_copy((uint32_t)x1, x2, |
| 71 | (uint32_t)x3, (uint32_t)x4, flags)); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 72 | |
| 73 | case FWU_SMC_IMAGE_AUTH: |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 74 | SMC_RET1(handle, bl1_fwu_image_auth((uint32_t)x1, x2, |
| 75 | (uint32_t)x3, flags)); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 76 | |
| 77 | case FWU_SMC_IMAGE_EXECUTE: |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 78 | SMC_RET1(handle, bl1_fwu_image_execute((uint32_t)x1, &handle, |
| 79 | flags)); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 80 | |
| 81 | case FWU_SMC_IMAGE_RESUME: |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 82 | SMC_RET1(handle, bl1_fwu_image_resume((register_t)x1, &handle, |
| 83 | flags)); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 84 | |
| 85 | case FWU_SMC_SEC_IMAGE_DONE: |
| 86 | SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags)); |
| 87 | |
Antonio Nino Diaz | 9d6fc3c | 2017-05-12 16:51:59 +0100 | [diff] [blame] | 88 | case FWU_SMC_IMAGE_RESET: |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 89 | SMC_RET1(handle, bl1_fwu_image_reset((uint32_t)x1, flags)); |
Antonio Nino Diaz | 9d6fc3c | 2017-05-12 16:51:59 +0100 | [diff] [blame] | 90 | |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 91 | case FWU_SMC_UPDATE_DONE: |
Dan Handley | 1f37b94 | 2015-12-15 14:28:24 +0000 | [diff] [blame] | 92 | bl1_fwu_done((void *)x1, NULL); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 93 | |
| 94 | default: |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 95 | assert(false); /* Unreachable */ |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 96 | break; |
| 97 | } |
| 98 | |
Antonio Nino Diaz | 7a317a7 | 2017-04-04 17:08:32 +0100 | [diff] [blame] | 99 | SMC_RET1(handle, SMC_UNK); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /******************************************************************************* |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 103 | * Utility functions to keep track of the images that are loaded at any time. |
| 104 | ******************************************************************************/ |
| 105 | |
| 106 | #ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES |
| 107 | #define FWU_MAX_SIMULTANEOUS_IMAGES PLAT_FWU_MAX_SIMULTANEOUS_IMAGES |
| 108 | #else |
| 109 | #define FWU_MAX_SIMULTANEOUS_IMAGES 10 |
| 110 | #endif |
| 111 | |
Ambroise Vincent | 279faa6 | 2019-02-27 16:50:10 +0000 | [diff] [blame] | 112 | static unsigned int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = { |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 113 | [0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID |
| 114 | }; |
| 115 | |
| 116 | /* |
| 117 | * Adds an image_id to the bl1_fwu_loaded_ids array. |
| 118 | * Returns 0 on success, 1 on error. |
| 119 | */ |
Ambroise Vincent | 279faa6 | 2019-02-27 16:50:10 +0000 | [diff] [blame] | 120 | static int bl1_fwu_add_loaded_id(unsigned int image_id) |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 121 | { |
| 122 | int i; |
| 123 | |
| 124 | /* Check if the ID is already in the list */ |
| 125 | for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { |
| 126 | if (bl1_fwu_loaded_ids[i] == image_id) |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* Find an empty slot */ |
| 131 | for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { |
| 132 | if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) { |
| 133 | bl1_fwu_loaded_ids[i] = image_id; |
| 134 | return 0; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return 1; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Removes an image_id from the bl1_fwu_loaded_ids array. |
| 143 | * Returns 0 on success, 1 on error. |
| 144 | */ |
Ambroise Vincent | 279faa6 | 2019-02-27 16:50:10 +0000 | [diff] [blame] | 145 | static int bl1_fwu_remove_loaded_id(unsigned int image_id) |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 146 | { |
| 147 | int i; |
| 148 | |
| 149 | /* Find the ID */ |
| 150 | for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { |
| 151 | if (bl1_fwu_loaded_ids[i] == image_id) { |
| 152 | bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID; |
| 153 | return 0; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return 1; |
| 158 | } |
| 159 | |
| 160 | /******************************************************************************* |
| 161 | * This function checks if the specified image overlaps another image already |
| 162 | * loaded. It returns 0 if there is no overlap, a negative error code otherwise. |
| 163 | ******************************************************************************/ |
Ambroise Vincent | 279faa6 | 2019-02-27 16:50:10 +0000 | [diff] [blame] | 164 | static int bl1_fwu_image_check_overlaps(unsigned int image_id) |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 165 | { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 166 | const image_desc_t *desc, *checked_desc; |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 167 | const image_info_t *info, *checked_info; |
| 168 | |
| 169 | uintptr_t image_base, image_end; |
| 170 | uintptr_t checked_image_base, checked_image_end; |
| 171 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 172 | checked_desc = bl1_plat_get_image_desc(image_id); |
Manish V Badarkhe | 2d3b44e | 2025-04-01 17:27:52 +0100 | [diff] [blame] | 173 | |
| 174 | assert(checked_desc != NULL); |
| 175 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 176 | checked_info = &checked_desc->image_info; |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 177 | |
| 178 | /* Image being checked mustn't be empty. */ |
| 179 | assert(checked_info->image_size != 0); |
| 180 | |
| 181 | checked_image_base = checked_info->image_base; |
| 182 | checked_image_end = checked_image_base + checked_info->image_size - 1; |
Soby Mathew | ee05ae1 | 2017-06-15 16:11:48 +0100 | [diff] [blame] | 183 | /* No need to check for overflows, it's done in bl1_fwu_image_copy(). */ |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 184 | |
| 185 | for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) { |
| 186 | |
Soby Mathew | ee05ae1 | 2017-06-15 16:11:48 +0100 | [diff] [blame] | 187 | /* Skip INVALID_IMAGE_IDs and don't check image against itself */ |
| 188 | if ((bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) || |
| 189 | (bl1_fwu_loaded_ids[i] == image_id)) |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 190 | continue; |
| 191 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 192 | desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]); |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 193 | |
| 194 | /* Only check images that are loaded or being loaded. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 195 | assert ((desc != NULL) && (desc->state != IMAGE_STATE_RESET)); |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 196 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 197 | info = &desc->image_info; |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 198 | |
| 199 | /* There cannot be overlaps with an empty image. */ |
| 200 | if (info->image_size == 0) |
| 201 | continue; |
| 202 | |
| 203 | image_base = info->image_base; |
| 204 | image_end = image_base + info->image_size - 1; |
| 205 | /* |
| 206 | * Overflows cannot happen. It is checked in |
| 207 | * bl1_fwu_image_copy() when the image goes from RESET to |
| 208 | * COPYING or COPIED. |
| 209 | */ |
| 210 | assert (image_end > image_base); |
| 211 | |
| 212 | /* Check if there are overlaps. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 213 | if (!((image_end < checked_image_base) || |
| 214 | (checked_image_end < image_base))) { |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 215 | VERBOSE("Image with ID %d overlaps existing image with ID %d", |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 216 | checked_desc->image_id, desc->image_id); |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 217 | return -EPERM; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | /******************************************************************************* |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 225 | * This function is responsible for copying secure images in AP Secure RAM. |
| 226 | ******************************************************************************/ |
| 227 | static int bl1_fwu_image_copy(unsigned int image_id, |
| 228 | uintptr_t image_src, |
| 229 | unsigned int block_size, |
| 230 | unsigned int image_size, |
| 231 | unsigned int flags) |
| 232 | { |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 233 | uintptr_t dest_addr; |
Sandrine Bailleux | b38a9e5 | 2016-11-14 14:56:51 +0000 | [diff] [blame] | 234 | unsigned int remaining; |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 235 | image_desc_t *desc; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 236 | |
| 237 | /* Get the image descriptor. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 238 | desc = bl1_plat_get_image_desc(image_id); |
| 239 | if (desc == NULL) { |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 240 | WARN("BL1-FWU: Invalid image ID %u\n", image_id); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 241 | return -EPERM; |
| 242 | } |
| 243 | |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 244 | /* |
| 245 | * The request must originate from a non-secure caller and target a |
| 246 | * secure image. Any other scenario is invalid. |
| 247 | */ |
| 248 | if (GET_SECURITY_STATE(flags) == SECURE) { |
| 249 | WARN("BL1-FWU: Copy not allowed from secure world.\n"); |
| 250 | return -EPERM; |
| 251 | } |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 252 | if (GET_SECURITY_STATE(desc->ep_info.h.attr) == NON_SECURE) { |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 253 | WARN("BL1-FWU: Copy not allowed for non-secure images.\n"); |
| 254 | return -EPERM; |
| 255 | } |
| 256 | |
| 257 | /* Check whether the FWU state machine is in the correct state. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 258 | if ((desc->state != IMAGE_STATE_RESET) && |
| 259 | (desc->state != IMAGE_STATE_COPYING)) { |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 260 | WARN("BL1-FWU: Copy not allowed at this point of the FWU" |
| 261 | " process.\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 262 | return -EPERM; |
| 263 | } |
| 264 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 265 | if ((image_src == 0U) || (block_size == 0U) || |
Sandrine Bailleux | 949a52d | 2016-11-11 16:44:37 +0000 | [diff] [blame] | 266 | check_uptr_overflow(image_src, block_size - 1)) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 267 | WARN("BL1-FWU: Copy not allowed due to invalid image source" |
| 268 | " or block size\n"); |
| 269 | return -ENOMEM; |
| 270 | } |
| 271 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 272 | if (desc->state == IMAGE_STATE_COPYING) { |
Sandrine Bailleux | 1bfb706 | 2016-11-14 14:58:05 +0000 | [diff] [blame] | 273 | /* |
| 274 | * There must have been at least 1 copy operation for this image |
| 275 | * previously. |
| 276 | */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 277 | assert(desc->copied_size != 0U); |
Sandrine Bailleux | 1bfb706 | 2016-11-14 14:58:05 +0000 | [diff] [blame] | 278 | /* |
| 279 | * The image size must have been recorded in the 1st copy |
| 280 | * operation. |
| 281 | */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 282 | image_size = desc->image_info.image_size; |
Sandrine Bailleux | 1bfb706 | 2016-11-14 14:58:05 +0000 | [diff] [blame] | 283 | assert(image_size != 0); |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 284 | assert(desc->copied_size < image_size); |
Sandrine Bailleux | 1bfb706 | 2016-11-14 14:58:05 +0000 | [diff] [blame] | 285 | |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 286 | INFO("BL1-FWU: Continuing image copy in blocks\n"); |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 287 | } else { /* desc->state == IMAGE_STATE_RESET */ |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 288 | INFO("BL1-FWU: Initial call to copy an image\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 289 | |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 290 | /* |
| 291 | * image_size is relevant only for the 1st copy request, it is |
| 292 | * then ignored for subsequent calls for this image. |
| 293 | */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 294 | if (image_size == 0) { |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 295 | WARN("BL1-FWU: Copy not allowed due to invalid image" |
| 296 | " size\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 297 | return -ENOMEM; |
| 298 | } |
| 299 | |
Yatharth Kochar | 53d703a | 2016-11-11 13:57:50 +0000 | [diff] [blame] | 300 | /* Check that the image size to load is within limit */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 301 | if (image_size > desc->image_info.image_max_size) { |
Yatharth Kochar | 53d703a | 2016-11-11 13:57:50 +0000 | [diff] [blame] | 302 | WARN("BL1-FWU: Image size out of bounds\n"); |
| 303 | return -ENOMEM; |
| 304 | } |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 305 | |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 306 | /* Save the given image size. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 307 | desc->image_info.image_size = image_size; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 308 | |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 309 | /* Make sure the image doesn't overlap other images. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 310 | if (bl1_fwu_image_check_overlaps(image_id) != 0) { |
| 311 | desc->image_info.image_size = 0; |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 312 | WARN("BL1-FWU: This image overlaps another one\n"); |
| 313 | return -EPERM; |
| 314 | } |
| 315 | |
Sandrine Bailleux | b38a9e5 | 2016-11-14 14:56:51 +0000 | [diff] [blame] | 316 | /* |
| 317 | * copied_size must be explicitly initialized here because the |
| 318 | * FWU code doesn't necessarily do it when it resets the state |
| 319 | * machine. |
| 320 | */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 321 | desc->copied_size = 0; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Sandrine Bailleux | b38a9e5 | 2016-11-14 14:56:51 +0000 | [diff] [blame] | 324 | /* |
| 325 | * If the given block size is more than the total image size |
| 326 | * then clip the former to the latter. |
| 327 | */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 328 | remaining = image_size - desc->copied_size; |
Sandrine Bailleux | b38a9e5 | 2016-11-14 14:56:51 +0000 | [diff] [blame] | 329 | if (block_size > remaining) { |
| 330 | WARN("BL1-FWU: Block size is too big, clipping it.\n"); |
| 331 | block_size = remaining; |
| 332 | } |
| 333 | |
| 334 | /* Make sure the source image is mapped in memory. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 335 | if (bl1_plat_mem_check(image_src, block_size, flags) != 0) { |
Sandrine Bailleux | b38a9e5 | 2016-11-14 14:56:51 +0000 | [diff] [blame] | 336 | WARN("BL1-FWU: Source image is not mapped.\n"); |
| 337 | return -ENOMEM; |
| 338 | } |
| 339 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 340 | if (bl1_fwu_add_loaded_id(image_id) != 0) { |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 341 | WARN("BL1-FWU: Too many images loaded at the same time.\n"); |
| 342 | return -ENOMEM; |
| 343 | } |
| 344 | |
Soby Mathew | 566034f | 2018-02-08 17:45:12 +0000 | [diff] [blame] | 345 | /* Allow the platform to handle pre-image load before copying */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 346 | if (desc->state == IMAGE_STATE_RESET) { |
Soby Mathew | 566034f | 2018-02-08 17:45:12 +0000 | [diff] [blame] | 347 | if (bl1_plat_handle_pre_image_load(image_id) != 0) { |
| 348 | ERROR("BL1-FWU: Failure in pre-image load of image id %d\n", |
| 349 | image_id); |
| 350 | return -EPERM; |
| 351 | } |
| 352 | } |
| 353 | |
Sandrine Bailleux | b38a9e5 | 2016-11-14 14:56:51 +0000 | [diff] [blame] | 354 | /* Everything looks sane. Go ahead and copy the block of data. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 355 | dest_addr = desc->image_info.image_base + desc->copied_size; |
| 356 | (void)memcpy((void *) dest_addr, (const void *) image_src, block_size); |
Sandrine Bailleux | b38a9e5 | 2016-11-14 14:56:51 +0000 | [diff] [blame] | 357 | flush_dcache_range(dest_addr, block_size); |
| 358 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 359 | desc->copied_size += block_size; |
| 360 | desc->state = (block_size == remaining) ? |
Sandrine Bailleux | b38a9e5 | 2016-11-14 14:56:51 +0000 | [diff] [blame] | 361 | IMAGE_STATE_COPIED : IMAGE_STATE_COPYING; |
| 362 | |
| 363 | INFO("BL1-FWU: Copy operation successful.\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /******************************************************************************* |
| 368 | * This function is responsible for authenticating Normal/Secure images. |
| 369 | ******************************************************************************/ |
| 370 | static int bl1_fwu_image_auth(unsigned int image_id, |
| 371 | uintptr_t image_src, |
| 372 | unsigned int image_size, |
| 373 | unsigned int flags) |
| 374 | { |
| 375 | int result; |
| 376 | uintptr_t base_addr; |
| 377 | unsigned int total_size; |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 378 | image_desc_t *desc; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 379 | |
| 380 | /* Get the image descriptor. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 381 | desc = bl1_plat_get_image_desc(image_id); |
| 382 | if (desc == NULL) |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 383 | return -EPERM; |
| 384 | |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 385 | if (GET_SECURITY_STATE(flags) == SECURE) { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 386 | if (desc->state != IMAGE_STATE_RESET) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 387 | WARN("BL1-FWU: Authentication from secure world " |
| 388 | "while in invalid state\n"); |
| 389 | return -EPERM; |
| 390 | } |
| 391 | } else { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 392 | if (GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE) { |
| 393 | if (desc->state != IMAGE_STATE_COPIED) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 394 | WARN("BL1-FWU: Authentication of secure image " |
| 395 | "from non-secure world while not in copied state\n"); |
| 396 | return -EPERM; |
| 397 | } |
| 398 | } else { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 399 | if (desc->state != IMAGE_STATE_RESET) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 400 | WARN("BL1-FWU: Authentication of non-secure image " |
| 401 | "from non-secure world while in invalid state\n"); |
| 402 | return -EPERM; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 407 | if (desc->state == IMAGE_STATE_COPIED) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 408 | /* |
| 409 | * Image is in COPIED state. |
| 410 | * Use the stored address and size. |
| 411 | */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 412 | base_addr = desc->image_info.image_base; |
| 413 | total_size = desc->image_info.image_size; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 414 | } else { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 415 | if ((image_src == 0U) || (image_size == 0U) || |
Sandrine Bailleux | 949a52d | 2016-11-11 16:44:37 +0000 | [diff] [blame] | 416 | check_uptr_overflow(image_src, image_size - 1)) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 417 | WARN("BL1-FWU: Auth not allowed due to invalid" |
| 418 | " image source/size\n"); |
| 419 | return -ENOMEM; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * Image is in RESET state. |
| 424 | * Check the parameters and authenticate the source image in place. |
| 425 | */ |
Elyes Haouas | 9a90d72 | 2023-02-13 10:05:41 +0100 | [diff] [blame] | 426 | if (bl1_plat_mem_check(image_src, image_size, |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 427 | desc->ep_info.h.attr) != 0) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 428 | WARN("BL1-FWU: Authentication arguments source/size not mapped\n"); |
| 429 | return -ENOMEM; |
| 430 | } |
| 431 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 432 | if (bl1_fwu_add_loaded_id(image_id) != 0) { |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 433 | WARN("BL1-FWU: Too many images loaded at the same time.\n"); |
| 434 | return -ENOMEM; |
| 435 | } |
| 436 | |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 437 | base_addr = image_src; |
| 438 | total_size = image_size; |
| 439 | |
| 440 | /* Update the image size in the descriptor. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 441 | desc->image_info.image_size = total_size; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Authenticate the image. |
| 446 | */ |
| 447 | INFO("BL1-FWU: Authenticating image_id:%d\n", image_id); |
| 448 | result = auth_mod_verify_img(image_id, (void *)base_addr, total_size); |
| 449 | if (result != 0) { |
| 450 | WARN("BL1-FWU: Authentication Failed err=%d\n", result); |
| 451 | |
| 452 | /* |
| 453 | * Authentication has failed. |
| 454 | * Clear the memory if the image was copied. |
| 455 | * This is to prevent an attack where this contains |
| 456 | * some malicious code that can somehow be executed later. |
| 457 | */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 458 | if (desc->state == IMAGE_STATE_COPIED) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 459 | /* Clear the memory.*/ |
Douglas Raillard | 308d359 | 2016-12-02 13:51:54 +0000 | [diff] [blame] | 460 | zero_normalmem((void *)base_addr, total_size); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 461 | flush_dcache_range(base_addr, total_size); |
| 462 | |
| 463 | /* Indicate that image can be copied again*/ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 464 | desc->state = IMAGE_STATE_RESET; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 465 | } |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 466 | |
| 467 | /* |
| 468 | * Even if this fails it's ok because the ID isn't in the array. |
| 469 | * The image cannot be in RESET state here, it is checked at the |
| 470 | * beginning of the function. |
| 471 | */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 472 | (void)bl1_fwu_remove_loaded_id(image_id); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 473 | return -EAUTH; |
| 474 | } |
| 475 | |
| 476 | /* Indicate that image is in authenticated state. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 477 | desc->state = IMAGE_STATE_AUTHENTICATED; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 478 | |
Soby Mathew | 566034f | 2018-02-08 17:45:12 +0000 | [diff] [blame] | 479 | /* Allow the platform to handle post-image load */ |
| 480 | result = bl1_plat_handle_post_image_load(image_id); |
| 481 | if (result != 0) { |
| 482 | ERROR("BL1-FWU: Failure %d in post-image load of image id %d\n", |
| 483 | result, image_id); |
| 484 | /* |
| 485 | * Panic here as the platform handling of post-image load is |
| 486 | * not correct. |
| 487 | */ |
| 488 | plat_error_handler(result); |
| 489 | } |
| 490 | |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 491 | /* |
| 492 | * Flush image_info to memory so that other |
| 493 | * secure world images can see changes. |
| 494 | */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 495 | flush_dcache_range((uintptr_t)&desc->image_info, |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 496 | sizeof(image_info_t)); |
| 497 | |
| 498 | INFO("BL1-FWU: Authentication was successful\n"); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | /******************************************************************************* |
| 504 | * This function is responsible for executing Secure images. |
| 505 | ******************************************************************************/ |
| 506 | static int bl1_fwu_image_execute(unsigned int image_id, |
| 507 | void **handle, |
| 508 | unsigned int flags) |
| 509 | { |
| 510 | /* Get the image descriptor. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 511 | image_desc_t *desc = bl1_plat_get_image_desc(image_id); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 512 | |
| 513 | /* |
| 514 | * Execution is NOT allowed if: |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 515 | * image_id is invalid OR |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 516 | * Caller is from Secure world OR |
| 517 | * Image is Non-Secure OR |
| 518 | * Image is Non-Executable OR |
| 519 | * Image is NOT in AUTHENTICATED state. |
| 520 | */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 521 | if ((desc == NULL) || |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 522 | (GET_SECURITY_STATE(flags) == SECURE) || |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 523 | (GET_SECURITY_STATE(desc->ep_info.h.attr) == NON_SECURE) || |
| 524 | (EP_GET_EXE(desc->ep_info.h.attr) == NON_EXECUTABLE) || |
| 525 | (desc->state != IMAGE_STATE_AUTHENTICATED)) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 526 | WARN("BL1-FWU: Execution not allowed due to invalid state/args\n"); |
| 527 | return -EPERM; |
| 528 | } |
| 529 | |
| 530 | INFO("BL1-FWU: Executing Secure image\n"); |
| 531 | |
Julius Werner | 402b3cf | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 532 | #ifdef __aarch64__ |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 533 | /* Save NS-EL1 system registers. */ |
| 534 | cm_el1_sysregs_context_save(NON_SECURE); |
dp-arm | a440900 | 2017-02-15 11:07:55 +0000 | [diff] [blame] | 535 | #endif |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 536 | |
| 537 | /* Prepare the image for execution. */ |
| 538 | bl1_prepare_next_image(image_id); |
| 539 | |
| 540 | /* Update the secure image id. */ |
| 541 | sec_exec_image_id = image_id; |
| 542 | |
Julius Werner | 402b3cf | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 543 | #ifdef __aarch64__ |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 544 | *handle = cm_get_context(SECURE); |
dp-arm | a440900 | 2017-02-15 11:07:55 +0000 | [diff] [blame] | 545 | #else |
| 546 | *handle = smc_get_ctx(SECURE); |
| 547 | #endif |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | /******************************************************************************* |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 552 | * This function is responsible for resuming execution in the other security |
| 553 | * world |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 554 | ******************************************************************************/ |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 555 | static register_t bl1_fwu_image_resume(register_t image_param, |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 556 | void **handle, |
| 557 | unsigned int flags) |
| 558 | { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 559 | image_desc_t *desc; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 560 | unsigned int resume_sec_state; |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 561 | unsigned int caller_sec_state = GET_SECURITY_STATE(flags); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 562 | |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 563 | /* Get the image descriptor for last executed secure image id. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 564 | desc = bl1_plat_get_image_desc(sec_exec_image_id); |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 565 | if (caller_sec_state == NON_SECURE) { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 566 | if (desc == NULL) { |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 567 | WARN("BL1-FWU: Resume not allowed due to no available" |
| 568 | "secure image\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 569 | return -EPERM; |
| 570 | } |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 571 | } else { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 572 | /* desc must be valid for secure world callers */ |
| 573 | assert(desc != NULL); |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 574 | } |
| 575 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 576 | assert(GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE); |
| 577 | assert(EP_GET_EXE(desc->ep_info.h.attr) == EXECUTABLE); |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 578 | |
| 579 | if (caller_sec_state == SECURE) { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 580 | assert(desc->state == IMAGE_STATE_EXECUTED); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 581 | |
| 582 | /* Update the flags. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 583 | desc->state = IMAGE_STATE_INTERRUPTED; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 584 | resume_sec_state = NON_SECURE; |
| 585 | } else { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 586 | assert(desc->state == IMAGE_STATE_INTERRUPTED); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 587 | |
| 588 | /* Update the flags. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 589 | desc->state = IMAGE_STATE_EXECUTED; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 590 | resume_sec_state = SECURE; |
| 591 | } |
| 592 | |
dp-arm | a440900 | 2017-02-15 11:07:55 +0000 | [diff] [blame] | 593 | INFO("BL1-FWU: Resuming %s world context\n", |
| 594 | (resume_sec_state == SECURE) ? "secure" : "normal"); |
| 595 | |
Julius Werner | 402b3cf | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 596 | #ifdef __aarch64__ |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 597 | /* Save the EL1 system registers of calling world. */ |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 598 | cm_el1_sysregs_context_save(caller_sec_state); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 599 | |
| 600 | /* Restore the EL1 system registers of resuming world. */ |
| 601 | cm_el1_sysregs_context_restore(resume_sec_state); |
| 602 | |
| 603 | /* Update the next context. */ |
| 604 | cm_set_next_eret_context(resume_sec_state); |
| 605 | |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 606 | *handle = cm_get_context(resume_sec_state); |
dp-arm | a440900 | 2017-02-15 11:07:55 +0000 | [diff] [blame] | 607 | #else |
| 608 | /* Update the next context. */ |
| 609 | cm_set_next_context(cm_get_context(resume_sec_state)); |
| 610 | |
| 611 | /* Prepare the smc context for the next BL image. */ |
| 612 | smc_set_next_ctx(resume_sec_state); |
| 613 | |
| 614 | *handle = smc_get_ctx(resume_sec_state); |
| 615 | #endif |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 616 | return image_param; |
| 617 | } |
| 618 | |
| 619 | /******************************************************************************* |
| 620 | * This function is responsible for resuming normal world context. |
| 621 | ******************************************************************************/ |
| 622 | static int bl1_fwu_sec_image_done(void **handle, unsigned int flags) |
| 623 | { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 624 | image_desc_t *desc; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 625 | |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 626 | /* Make sure caller is from the secure world */ |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 627 | if (GET_SECURITY_STATE(flags) == NON_SECURE) { |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 628 | WARN("BL1-FWU: Image done not allowed from normal world\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 629 | return -EPERM; |
| 630 | } |
| 631 | |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 632 | /* Get the image descriptor for last executed secure image id */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 633 | desc = bl1_plat_get_image_desc(sec_exec_image_id); |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 634 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 635 | /* desc must correspond to a valid secure executing image */ |
| 636 | assert(desc != NULL); |
| 637 | assert(GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE); |
| 638 | assert(EP_GET_EXE(desc->ep_info.h.attr) == EXECUTABLE); |
| 639 | assert(desc->state == IMAGE_STATE_EXECUTED); |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 640 | |
Antonio Nino Diaz | 128daee | 2017-06-01 13:40:17 +0100 | [diff] [blame] | 641 | #if ENABLE_ASSERTIONS |
| 642 | int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id); |
| 643 | assert(rc == 0); |
| 644 | #else |
| 645 | bl1_fwu_remove_loaded_id(sec_exec_image_id); |
| 646 | #endif |
| 647 | |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 648 | /* Update the flags. */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 649 | desc->state = IMAGE_STATE_RESET; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 650 | sec_exec_image_id = INVALID_IMAGE_ID; |
| 651 | |
dp-arm | a440900 | 2017-02-15 11:07:55 +0000 | [diff] [blame] | 652 | INFO("BL1-FWU: Resuming Normal world context\n"); |
Julius Werner | 402b3cf | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 653 | #ifdef __aarch64__ |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 654 | /* |
| 655 | * Secure world is done so no need to save the context. |
| 656 | * Just restore the Non-Secure context. |
| 657 | */ |
| 658 | cm_el1_sysregs_context_restore(NON_SECURE); |
| 659 | |
| 660 | /* Update the next context. */ |
| 661 | cm_set_next_eret_context(NON_SECURE); |
| 662 | |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 663 | *handle = cm_get_context(NON_SECURE); |
dp-arm | a440900 | 2017-02-15 11:07:55 +0000 | [diff] [blame] | 664 | #else |
| 665 | /* Update the next context. */ |
| 666 | cm_set_next_context(cm_get_context(NON_SECURE)); |
| 667 | |
| 668 | /* Prepare the smc context for the next BL image. */ |
| 669 | smc_set_next_ctx(NON_SECURE); |
| 670 | |
| 671 | *handle = smc_get_ctx(NON_SECURE); |
| 672 | #endif |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | /******************************************************************************* |
| 677 | * This function provides the opportunity for users to perform any |
| 678 | * platform specific handling after the Firmware update is done. |
| 679 | ******************************************************************************/ |
Dan Handley | 1f37b94 | 2015-12-15 14:28:24 +0000 | [diff] [blame] | 680 | __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved) |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 681 | { |
| 682 | NOTICE("BL1-FWU: *******FWU Process Completed*******\n"); |
| 683 | |
| 684 | /* |
| 685 | * Call platform done function. |
| 686 | */ |
Dan Handley | 1f37b94 | 2015-12-15 14:28:24 +0000 | [diff] [blame] | 687 | bl1_plat_fwu_done(client_cookie, reserved); |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 688 | assert(false); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 689 | } |
Antonio Nino Diaz | 9d6fc3c | 2017-05-12 16:51:59 +0100 | [diff] [blame] | 690 | |
| 691 | /******************************************************************************* |
| 692 | * This function resets an image to IMAGE_STATE_RESET. It fails if the image is |
| 693 | * being executed. |
| 694 | ******************************************************************************/ |
| 695 | static int bl1_fwu_image_reset(unsigned int image_id, unsigned int flags) |
| 696 | { |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 697 | image_desc_t *desc = bl1_plat_get_image_desc(image_id); |
Antonio Nino Diaz | 9d6fc3c | 2017-05-12 16:51:59 +0100 | [diff] [blame] | 698 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 699 | if ((desc == NULL) || (GET_SECURITY_STATE(flags) == SECURE)) { |
Antonio Nino Diaz | 9d6fc3c | 2017-05-12 16:51:59 +0100 | [diff] [blame] | 700 | WARN("BL1-FWU: Reset not allowed due to invalid args\n"); |
| 701 | return -EPERM; |
| 702 | } |
| 703 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 704 | switch (desc->state) { |
Antonio Nino Diaz | 9d6fc3c | 2017-05-12 16:51:59 +0100 | [diff] [blame] | 705 | |
| 706 | case IMAGE_STATE_RESET: |
| 707 | /* Nothing to do. */ |
| 708 | break; |
| 709 | |
| 710 | case IMAGE_STATE_INTERRUPTED: |
| 711 | case IMAGE_STATE_AUTHENTICATED: |
| 712 | case IMAGE_STATE_COPIED: |
| 713 | case IMAGE_STATE_COPYING: |
| 714 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 715 | if (bl1_fwu_remove_loaded_id(image_id) != 0) { |
Antonio Nino Diaz | 9d6fc3c | 2017-05-12 16:51:59 +0100 | [diff] [blame] | 716 | WARN("BL1-FWU: Image reset couldn't find the image ID\n"); |
| 717 | return -EPERM; |
| 718 | } |
| 719 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 720 | if (desc->copied_size != 0U) { |
Soby Mathew | ee05ae1 | 2017-06-15 16:11:48 +0100 | [diff] [blame] | 721 | /* Clear the memory if the image is copied */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 722 | assert(GET_SECURITY_STATE(desc->ep_info.h.attr) |
| 723 | == SECURE); |
Soby Mathew | ee05ae1 | 2017-06-15 16:11:48 +0100 | [diff] [blame] | 724 | |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 725 | zero_normalmem((void *)desc->image_info.image_base, |
| 726 | desc->copied_size); |
| 727 | flush_dcache_range(desc->image_info.image_base, |
| 728 | desc->copied_size); |
Soby Mathew | ee05ae1 | 2017-06-15 16:11:48 +0100 | [diff] [blame] | 729 | } |
Antonio Nino Diaz | 9d6fc3c | 2017-05-12 16:51:59 +0100 | [diff] [blame] | 730 | |
| 731 | /* Reset status variables */ |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 732 | desc->copied_size = 0; |
| 733 | desc->image_info.image_size = 0; |
| 734 | desc->state = IMAGE_STATE_RESET; |
Antonio Nino Diaz | 9d6fc3c | 2017-05-12 16:51:59 +0100 | [diff] [blame] | 735 | |
| 736 | /* Clear authentication state */ |
| 737 | auth_img_flags[image_id] = 0; |
| 738 | |
| 739 | break; |
| 740 | |
| 741 | case IMAGE_STATE_EXECUTED: |
| 742 | default: |
John Powell | 3443a70 | 2020-03-20 14:21:05 -0500 | [diff] [blame] | 743 | assert(false); /* Unreachable */ |
Jonathan Wright | c9662db | 2018-03-13 13:54:03 +0000 | [diff] [blame] | 744 | break; |
Antonio Nino Diaz | 9d6fc3c | 2017-05-12 16:51:59 +0100 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | return 0; |
| 748 | } |