Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 1 | /* |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2016, ARM Limited and Contributors. All rights reserved. |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this |
| 8 | * list of conditions and the following disclaimer. |
| 9 | * |
| 10 | * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * Neither the name of ARM nor the names of its contributors may be used |
| 15 | * to endorse or promote products derived from this software without specific |
| 16 | * prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #include <assert.h> |
| 32 | #include <arch_helpers.h> |
| 33 | #include <auth_mod.h> |
| 34 | #include <bl1.h> |
| 35 | #include <bl_common.h> |
| 36 | #include <context.h> |
| 37 | #include <context_mgmt.h> |
| 38 | #include <debug.h> |
| 39 | #include <errno.h> |
| 40 | #include <platform.h> |
| 41 | #include <platform_def.h> |
| 42 | #include <smcc_helpers.h> |
| 43 | #include <string.h> |
| 44 | #include "bl1_private.h" |
| 45 | |
| 46 | /* |
| 47 | * Function declarations. |
| 48 | */ |
| 49 | static int bl1_fwu_image_copy(unsigned int image_id, |
| 50 | uintptr_t image_addr, |
| 51 | unsigned int block_size, |
| 52 | unsigned int image_size, |
| 53 | unsigned int flags); |
| 54 | static int bl1_fwu_image_auth(unsigned int image_id, |
| 55 | uintptr_t image_addr, |
| 56 | unsigned int image_size, |
| 57 | unsigned int flags); |
| 58 | static int bl1_fwu_image_execute(unsigned int image_id, |
| 59 | void **handle, |
| 60 | unsigned int flags); |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 61 | static register_t bl1_fwu_image_resume(register_t image_param, |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 62 | void **handle, |
| 63 | unsigned int flags); |
| 64 | static int bl1_fwu_sec_image_done(void **handle, |
| 65 | unsigned int flags); |
Dan Handley | 1f37b94 | 2015-12-15 14:28:24 +0000 | [diff] [blame] | 66 | __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * This keeps track of last executed secure image id. |
| 70 | */ |
| 71 | static unsigned int sec_exec_image_id = INVALID_IMAGE_ID; |
| 72 | |
| 73 | /******************************************************************************* |
| 74 | * Top level handler for servicing FWU SMCs. |
| 75 | ******************************************************************************/ |
| 76 | register_t bl1_fwu_smc_handler(unsigned int smc_fid, |
| 77 | register_t x1, |
| 78 | register_t x2, |
| 79 | register_t x3, |
| 80 | register_t x4, |
| 81 | void *cookie, |
| 82 | void *handle, |
| 83 | unsigned int flags) |
| 84 | { |
| 85 | |
| 86 | switch (smc_fid) { |
| 87 | case FWU_SMC_IMAGE_COPY: |
| 88 | SMC_RET1(handle, bl1_fwu_image_copy(x1, x2, x3, x4, flags)); |
| 89 | |
| 90 | case FWU_SMC_IMAGE_AUTH: |
| 91 | SMC_RET1(handle, bl1_fwu_image_auth(x1, x2, x3, flags)); |
| 92 | |
| 93 | case FWU_SMC_IMAGE_EXECUTE: |
| 94 | SMC_RET1(handle, bl1_fwu_image_execute(x1, &handle, flags)); |
| 95 | |
| 96 | case FWU_SMC_IMAGE_RESUME: |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 97 | SMC_RET1(handle, bl1_fwu_image_resume(x1, &handle, flags)); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 98 | |
| 99 | case FWU_SMC_SEC_IMAGE_DONE: |
| 100 | SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags)); |
| 101 | |
| 102 | case FWU_SMC_UPDATE_DONE: |
Dan Handley | 1f37b94 | 2015-12-15 14:28:24 +0000 | [diff] [blame] | 103 | bl1_fwu_done((void *)x1, NULL); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 104 | /* We should never return from bl1_fwu_done() */ |
| 105 | |
| 106 | default: |
| 107 | assert(0); |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | SMC_RET0(handle); |
| 112 | } |
| 113 | |
| 114 | /******************************************************************************* |
| 115 | * This function is responsible for copying secure images in AP Secure RAM. |
| 116 | ******************************************************************************/ |
| 117 | static int bl1_fwu_image_copy(unsigned int image_id, |
| 118 | uintptr_t image_src, |
| 119 | unsigned int block_size, |
| 120 | unsigned int image_size, |
| 121 | unsigned int flags) |
| 122 | { |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 123 | uintptr_t dest_addr; |
Sandrine Bailleux | b38a9e5 | 2016-11-14 14:56:51 +0000 | [diff] [blame^] | 124 | unsigned int remaining; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 125 | |
| 126 | /* Get the image descriptor. */ |
| 127 | image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 128 | if (!image_desc) { |
| 129 | WARN("BL1-FWU: Invalid image ID %u\n", image_id); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 130 | return -EPERM; |
| 131 | } |
| 132 | |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 133 | /* |
| 134 | * The request must originate from a non-secure caller and target a |
| 135 | * secure image. Any other scenario is invalid. |
| 136 | */ |
| 137 | if (GET_SECURITY_STATE(flags) == SECURE) { |
| 138 | WARN("BL1-FWU: Copy not allowed from secure world.\n"); |
| 139 | return -EPERM; |
| 140 | } |
| 141 | if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) { |
| 142 | WARN("BL1-FWU: Copy not allowed for non-secure images.\n"); |
| 143 | return -EPERM; |
| 144 | } |
| 145 | |
| 146 | /* Check whether the FWU state machine is in the correct state. */ |
| 147 | if ((image_desc->state != IMAGE_STATE_RESET) && |
| 148 | (image_desc->state != IMAGE_STATE_COPYING)) { |
| 149 | WARN("BL1-FWU: Copy not allowed at this point of the FWU" |
| 150 | " process.\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 151 | return -EPERM; |
| 152 | } |
| 153 | |
| 154 | if ((!image_src) || (!block_size)) { |
| 155 | WARN("BL1-FWU: Copy not allowed due to invalid image source" |
| 156 | " or block size\n"); |
| 157 | return -ENOMEM; |
| 158 | } |
| 159 | |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 160 | if (image_desc->state == IMAGE_STATE_COPYING) { |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 161 | image_size = image_desc->image_info.image_size; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 162 | INFO("BL1-FWU: Continuing image copy in blocks\n"); |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 163 | } else { /* image_desc->state == IMAGE_STATE_RESET */ |
| 164 | INFO("BL1-FWU: Initial call to copy an image\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 165 | |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 166 | /* |
| 167 | * image_size is relevant only for the 1st copy request, it is |
| 168 | * then ignored for subsequent calls for this image. |
| 169 | */ |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 170 | if (!image_size) { |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 171 | WARN("BL1-FWU: Copy not allowed due to invalid image" |
| 172 | " size\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 173 | return -ENOMEM; |
| 174 | } |
| 175 | |
Yatharth Kochar | 53d703a | 2016-11-11 13:57:50 +0000 | [diff] [blame] | 176 | #if LOAD_IMAGE_V2 |
| 177 | /* Check that the image size to load is within limit */ |
| 178 | if (image_size > image_desc->image_info.image_max_size) { |
| 179 | WARN("BL1-FWU: Image size out of bounds\n"); |
| 180 | return -ENOMEM; |
| 181 | } |
| 182 | #else |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 183 | /* Find out how much free trusted ram remains after BL1 load */ |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 184 | const meminfo_t *mem_layout = bl1_plat_sec_mem_layout(); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 185 | if ((image_desc->image_info.image_base < mem_layout->free_base) || |
| 186 | (image_desc->image_info.image_base + image_size > |
| 187 | mem_layout->free_base + mem_layout->free_size)) { |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 188 | WARN("BL1-FWU: Copy not allowed due to insufficient" |
| 189 | " resources.\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 190 | return -ENOMEM; |
| 191 | } |
Yatharth Kochar | 53d703a | 2016-11-11 13:57:50 +0000 | [diff] [blame] | 192 | #endif |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 193 | |
Sandrine Bailleux | 9f1489e | 2016-11-11 15:56:20 +0000 | [diff] [blame] | 194 | /* Save the given image size. */ |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 195 | image_desc->image_info.image_size = image_size; |
| 196 | |
Sandrine Bailleux | b38a9e5 | 2016-11-14 14:56:51 +0000 | [diff] [blame^] | 197 | /* |
| 198 | * copied_size must be explicitly initialized here because the |
| 199 | * FWU code doesn't necessarily do it when it resets the state |
| 200 | * machine. |
| 201 | */ |
| 202 | image_desc->copied_size = 0; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Sandrine Bailleux | b38a9e5 | 2016-11-14 14:56:51 +0000 | [diff] [blame^] | 205 | /* |
| 206 | * If the given block size is more than the total image size |
| 207 | * then clip the former to the latter. |
| 208 | */ |
| 209 | remaining = image_size - image_desc->copied_size; |
| 210 | if (block_size > remaining) { |
| 211 | WARN("BL1-FWU: Block size is too big, clipping it.\n"); |
| 212 | block_size = remaining; |
| 213 | } |
| 214 | |
| 215 | /* Make sure the source image is mapped in memory. */ |
| 216 | if (bl1_plat_mem_check(image_src, block_size, flags)) { |
| 217 | WARN("BL1-FWU: Source image is not mapped.\n"); |
| 218 | return -ENOMEM; |
| 219 | } |
| 220 | |
| 221 | /* Everything looks sane. Go ahead and copy the block of data. */ |
| 222 | dest_addr = image_desc->image_info.image_base + image_desc->copied_size; |
| 223 | memcpy((void *) dest_addr, (const void *) image_src, block_size); |
| 224 | flush_dcache_range(dest_addr, block_size); |
| 225 | |
| 226 | image_desc->copied_size += block_size; |
| 227 | image_desc->state = (block_size == remaining) ? |
| 228 | IMAGE_STATE_COPIED : IMAGE_STATE_COPYING; |
| 229 | |
| 230 | INFO("BL1-FWU: Copy operation successful.\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | /******************************************************************************* |
| 235 | * This function is responsible for authenticating Normal/Secure images. |
| 236 | ******************************************************************************/ |
| 237 | static int bl1_fwu_image_auth(unsigned int image_id, |
| 238 | uintptr_t image_src, |
| 239 | unsigned int image_size, |
| 240 | unsigned int flags) |
| 241 | { |
| 242 | int result; |
| 243 | uintptr_t base_addr; |
| 244 | unsigned int total_size; |
| 245 | |
| 246 | /* Get the image descriptor. */ |
| 247 | image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); |
| 248 | if (!image_desc) |
| 249 | return -EPERM; |
| 250 | |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 251 | if (GET_SECURITY_STATE(flags) == SECURE) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 252 | if (image_desc->state != IMAGE_STATE_RESET) { |
| 253 | WARN("BL1-FWU: Authentication from secure world " |
| 254 | "while in invalid state\n"); |
| 255 | return -EPERM; |
| 256 | } |
| 257 | } else { |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 258 | if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 259 | if (image_desc->state != IMAGE_STATE_COPIED) { |
| 260 | WARN("BL1-FWU: Authentication of secure image " |
| 261 | "from non-secure world while not in copied state\n"); |
| 262 | return -EPERM; |
| 263 | } |
| 264 | } else { |
| 265 | if (image_desc->state != IMAGE_STATE_RESET) { |
| 266 | WARN("BL1-FWU: Authentication of non-secure image " |
| 267 | "from non-secure world while in invalid state\n"); |
| 268 | return -EPERM; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if (image_desc->state == IMAGE_STATE_COPIED) { |
| 274 | /* |
| 275 | * Image is in COPIED state. |
| 276 | * Use the stored address and size. |
| 277 | */ |
| 278 | base_addr = image_desc->image_info.image_base; |
| 279 | total_size = image_desc->image_info.image_size; |
| 280 | } else { |
| 281 | if ((!image_src) || (!image_size)) { |
| 282 | WARN("BL1-FWU: Auth not allowed due to invalid" |
| 283 | " image source/size\n"); |
| 284 | return -ENOMEM; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Image is in RESET state. |
| 289 | * Check the parameters and authenticate the source image in place. |
| 290 | */ |
Dan Handley | 03131c8 | 2015-12-14 16:26:43 +0000 | [diff] [blame] | 291 | if (bl1_plat_mem_check(image_src, image_size, \ |
| 292 | image_desc->ep_info.h.attr)) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 293 | WARN("BL1-FWU: Authentication arguments source/size not mapped\n"); |
| 294 | return -ENOMEM; |
| 295 | } |
| 296 | |
| 297 | base_addr = image_src; |
| 298 | total_size = image_size; |
| 299 | |
| 300 | /* Update the image size in the descriptor. */ |
| 301 | image_desc->image_info.image_size = total_size; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Authenticate the image. |
| 306 | */ |
| 307 | INFO("BL1-FWU: Authenticating image_id:%d\n", image_id); |
| 308 | result = auth_mod_verify_img(image_id, (void *)base_addr, total_size); |
| 309 | if (result != 0) { |
| 310 | WARN("BL1-FWU: Authentication Failed err=%d\n", result); |
| 311 | |
| 312 | /* |
| 313 | * Authentication has failed. |
| 314 | * Clear the memory if the image was copied. |
| 315 | * This is to prevent an attack where this contains |
| 316 | * some malicious code that can somehow be executed later. |
| 317 | */ |
| 318 | if (image_desc->state == IMAGE_STATE_COPIED) { |
| 319 | /* Clear the memory.*/ |
| 320 | memset((void *)base_addr, 0, total_size); |
| 321 | flush_dcache_range(base_addr, total_size); |
| 322 | |
| 323 | /* Indicate that image can be copied again*/ |
| 324 | image_desc->state = IMAGE_STATE_RESET; |
| 325 | } |
| 326 | return -EAUTH; |
| 327 | } |
| 328 | |
| 329 | /* Indicate that image is in authenticated state. */ |
| 330 | image_desc->state = IMAGE_STATE_AUTHENTICATED; |
| 331 | |
| 332 | /* |
| 333 | * Flush image_info to memory so that other |
| 334 | * secure world images can see changes. |
| 335 | */ |
| 336 | flush_dcache_range((unsigned long)&image_desc->image_info, |
| 337 | sizeof(image_info_t)); |
| 338 | |
| 339 | INFO("BL1-FWU: Authentication was successful\n"); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /******************************************************************************* |
| 345 | * This function is responsible for executing Secure images. |
| 346 | ******************************************************************************/ |
| 347 | static int bl1_fwu_image_execute(unsigned int image_id, |
| 348 | void **handle, |
| 349 | unsigned int flags) |
| 350 | { |
| 351 | /* Get the image descriptor. */ |
| 352 | image_desc_t *image_desc = bl1_plat_get_image_desc(image_id); |
| 353 | |
| 354 | /* |
| 355 | * Execution is NOT allowed if: |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 356 | * image_id is invalid OR |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 357 | * Caller is from Secure world OR |
| 358 | * Image is Non-Secure OR |
| 359 | * Image is Non-Executable OR |
| 360 | * Image is NOT in AUTHENTICATED state. |
| 361 | */ |
| 362 | if ((!image_desc) || |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 363 | (GET_SECURITY_STATE(flags) == SECURE) || |
| 364 | (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) || |
| 365 | (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) || |
| 366 | (image_desc->state != IMAGE_STATE_AUTHENTICATED)) { |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 367 | WARN("BL1-FWU: Execution not allowed due to invalid state/args\n"); |
| 368 | return -EPERM; |
| 369 | } |
| 370 | |
| 371 | INFO("BL1-FWU: Executing Secure image\n"); |
| 372 | |
| 373 | /* Save NS-EL1 system registers. */ |
| 374 | cm_el1_sysregs_context_save(NON_SECURE); |
| 375 | |
| 376 | /* Prepare the image for execution. */ |
| 377 | bl1_prepare_next_image(image_id); |
| 378 | |
| 379 | /* Update the secure image id. */ |
| 380 | sec_exec_image_id = image_id; |
| 381 | |
| 382 | *handle = cm_get_context(SECURE); |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | /******************************************************************************* |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 387 | * This function is responsible for resuming execution in the other security |
| 388 | * world |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 389 | ******************************************************************************/ |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 390 | static register_t bl1_fwu_image_resume(register_t image_param, |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 391 | void **handle, |
| 392 | unsigned int flags) |
| 393 | { |
| 394 | image_desc_t *image_desc; |
| 395 | unsigned int resume_sec_state; |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 396 | unsigned int caller_sec_state = GET_SECURITY_STATE(flags); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 397 | |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 398 | /* Get the image descriptor for last executed secure image id. */ |
| 399 | image_desc = bl1_plat_get_image_desc(sec_exec_image_id); |
| 400 | if (caller_sec_state == NON_SECURE) { |
| 401 | if (!image_desc) { |
| 402 | WARN("BL1-FWU: Resume not allowed due to no available" |
| 403 | "secure image\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 404 | return -EPERM; |
| 405 | } |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 406 | } else { |
| 407 | /* image_desc must be valid for secure world callers */ |
| 408 | assert(image_desc); |
| 409 | } |
| 410 | |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 411 | assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE); |
| 412 | assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE); |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 413 | |
| 414 | if (caller_sec_state == SECURE) { |
| 415 | assert(image_desc->state == IMAGE_STATE_EXECUTED); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 416 | |
| 417 | /* Update the flags. */ |
| 418 | image_desc->state = IMAGE_STATE_INTERRUPTED; |
| 419 | resume_sec_state = NON_SECURE; |
| 420 | } else { |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 421 | assert(image_desc->state == IMAGE_STATE_INTERRUPTED); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 422 | |
| 423 | /* Update the flags. */ |
| 424 | image_desc->state = IMAGE_STATE_EXECUTED; |
| 425 | resume_sec_state = SECURE; |
| 426 | } |
| 427 | |
| 428 | /* Save the EL1 system registers of calling world. */ |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 429 | cm_el1_sysregs_context_save(caller_sec_state); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 430 | |
| 431 | /* Restore the EL1 system registers of resuming world. */ |
| 432 | cm_el1_sysregs_context_restore(resume_sec_state); |
| 433 | |
| 434 | /* Update the next context. */ |
| 435 | cm_set_next_eret_context(resume_sec_state); |
| 436 | |
| 437 | INFO("BL1-FWU: Resuming %s world context\n", |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 438 | (resume_sec_state == SECURE) ? "secure" : "normal"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 439 | |
| 440 | *handle = cm_get_context(resume_sec_state); |
| 441 | return image_param; |
| 442 | } |
| 443 | |
| 444 | /******************************************************************************* |
| 445 | * This function is responsible for resuming normal world context. |
| 446 | ******************************************************************************/ |
| 447 | static int bl1_fwu_sec_image_done(void **handle, unsigned int flags) |
| 448 | { |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 449 | image_desc_t *image_desc; |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 450 | |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 451 | /* Make sure caller is from the secure world */ |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 452 | if (GET_SECURITY_STATE(flags) == NON_SECURE) { |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 453 | WARN("BL1-FWU: Image done not allowed from normal world\n"); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 454 | return -EPERM; |
| 455 | } |
| 456 | |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 457 | /* Get the image descriptor for last executed secure image id */ |
| 458 | image_desc = bl1_plat_get_image_desc(sec_exec_image_id); |
| 459 | |
| 460 | /* image_desc must correspond to a valid secure executing image */ |
| 461 | assert(image_desc); |
Yatharth Kochar | 843ddee | 2016-02-01 11:04:46 +0000 | [diff] [blame] | 462 | assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE); |
| 463 | assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE); |
Dan Handley | 28955d5 | 2015-12-15 10:52:33 +0000 | [diff] [blame] | 464 | assert(image_desc->state == IMAGE_STATE_EXECUTED); |
| 465 | |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 466 | /* Update the flags. */ |
| 467 | image_desc->state = IMAGE_STATE_RESET; |
| 468 | sec_exec_image_id = INVALID_IMAGE_ID; |
| 469 | |
| 470 | /* |
| 471 | * Secure world is done so no need to save the context. |
| 472 | * Just restore the Non-Secure context. |
| 473 | */ |
| 474 | cm_el1_sysregs_context_restore(NON_SECURE); |
| 475 | |
| 476 | /* Update the next context. */ |
| 477 | cm_set_next_eret_context(NON_SECURE); |
| 478 | |
| 479 | INFO("BL1-FWU: Resuming Normal world context\n"); |
| 480 | |
| 481 | *handle = cm_get_context(NON_SECURE); |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | /******************************************************************************* |
| 486 | * This function provides the opportunity for users to perform any |
| 487 | * platform specific handling after the Firmware update is done. |
| 488 | ******************************************************************************/ |
Dan Handley | 1f37b94 | 2015-12-15 14:28:24 +0000 | [diff] [blame] | 489 | __dead2 static void bl1_fwu_done(void *client_cookie, void *reserved) |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 490 | { |
| 491 | NOTICE("BL1-FWU: *******FWU Process Completed*******\n"); |
| 492 | |
| 493 | /* |
| 494 | * Call platform done function. |
| 495 | */ |
Dan Handley | 1f37b94 | 2015-12-15 14:28:24 +0000 | [diff] [blame] | 496 | bl1_plat_fwu_done(client_cookie, reserved); |
Yatharth Kochar | 48bfb88 | 2015-10-10 19:06:53 +0100 | [diff] [blame] | 497 | assert(0); |
| 498 | } |