blob: 43bb4d2baed47758466d0a1168405c6059f956b3 [file] [log] [blame]
Yatharth Kochar48bfb882015-10-10 19:06:53 +01001/*
Douglas Raillard308d3592016-12-02 13:51:54 +00002 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
Yatharth Kochar48bfb882015-10-10 19:06:53 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Yatharth Kochar48bfb882015-10-10 19:06:53 +01005 */
6
7#include <assert.h>
8#include <arch_helpers.h>
9#include <auth_mod.h>
10#include <bl1.h>
11#include <bl_common.h>
12#include <context.h>
13#include <context_mgmt.h>
14#include <debug.h>
15#include <errno.h>
16#include <platform.h>
17#include <platform_def.h>
18#include <smcc_helpers.h>
19#include <string.h>
Sandrine Bailleux949a52d2016-11-11 16:44:37 +000020#include <utils.h>
Yatharth Kochar48bfb882015-10-10 19:06:53 +010021#include "bl1_private.h"
22
23/*
24 * Function declarations.
25 */
26static int bl1_fwu_image_copy(unsigned int image_id,
27 uintptr_t image_addr,
28 unsigned int block_size,
29 unsigned int image_size,
30 unsigned int flags);
31static int bl1_fwu_image_auth(unsigned int image_id,
32 uintptr_t image_addr,
33 unsigned int image_size,
34 unsigned int flags);
35static int bl1_fwu_image_execute(unsigned int image_id,
36 void **handle,
37 unsigned int flags);
Dan Handley28955d52015-12-15 10:52:33 +000038static register_t bl1_fwu_image_resume(register_t image_param,
Yatharth Kochar48bfb882015-10-10 19:06:53 +010039 void **handle,
40 unsigned int flags);
41static int bl1_fwu_sec_image_done(void **handle,
42 unsigned int flags);
Dan Handley1f37b942015-12-15 14:28:24 +000043__dead2 static void bl1_fwu_done(void *client_cookie, void *reserved);
Yatharth Kochar48bfb882015-10-10 19:06:53 +010044
45/*
46 * This keeps track of last executed secure image id.
47 */
48static unsigned int sec_exec_image_id = INVALID_IMAGE_ID;
49
dp-arma4409002017-02-15 11:07:55 +000050void cm_set_next_context(void *cpu_context);
51
Yatharth Kochar48bfb882015-10-10 19:06:53 +010052/*******************************************************************************
53 * Top level handler for servicing FWU SMCs.
54 ******************************************************************************/
55register_t bl1_fwu_smc_handler(unsigned int smc_fid,
56 register_t x1,
57 register_t x2,
58 register_t x3,
59 register_t x4,
60 void *cookie,
61 void *handle,
62 unsigned int flags)
63{
64
65 switch (smc_fid) {
66 case FWU_SMC_IMAGE_COPY:
67 SMC_RET1(handle, bl1_fwu_image_copy(x1, x2, x3, x4, flags));
68
69 case FWU_SMC_IMAGE_AUTH:
70 SMC_RET1(handle, bl1_fwu_image_auth(x1, x2, x3, flags));
71
72 case FWU_SMC_IMAGE_EXECUTE:
73 SMC_RET1(handle, bl1_fwu_image_execute(x1, &handle, flags));
74
75 case FWU_SMC_IMAGE_RESUME:
Dan Handley28955d52015-12-15 10:52:33 +000076 SMC_RET1(handle, bl1_fwu_image_resume(x1, &handle, flags));
Yatharth Kochar48bfb882015-10-10 19:06:53 +010077
78 case FWU_SMC_SEC_IMAGE_DONE:
79 SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
80
81 case FWU_SMC_UPDATE_DONE:
Dan Handley1f37b942015-12-15 14:28:24 +000082 bl1_fwu_done((void *)x1, NULL);
Yatharth Kochar48bfb882015-10-10 19:06:53 +010083 /* We should never return from bl1_fwu_done() */
84
85 default:
86 assert(0);
87 break;
88 }
89
Antonio Nino Diaz7a317a72017-04-04 17:08:32 +010090 SMC_RET1(handle, SMC_UNK);
Yatharth Kochar48bfb882015-10-10 19:06:53 +010091}
92
93/*******************************************************************************
Antonio Nino Diaz128daee2017-06-01 13:40:17 +010094 * Utility functions to keep track of the images that are loaded at any time.
95 ******************************************************************************/
96
97#ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
98#define FWU_MAX_SIMULTANEOUS_IMAGES PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
99#else
100#define FWU_MAX_SIMULTANEOUS_IMAGES 10
101#endif
102
103static int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = {
104 [0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID
105};
106
107/*
108 * Adds an image_id to the bl1_fwu_loaded_ids array.
109 * Returns 0 on success, 1 on error.
110 */
111static int bl1_fwu_add_loaded_id(int image_id)
112{
113 int i;
114
115 /* Check if the ID is already in the list */
116 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
117 if (bl1_fwu_loaded_ids[i] == image_id)
118 return 0;
119 }
120
121 /* Find an empty slot */
122 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
123 if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) {
124 bl1_fwu_loaded_ids[i] = image_id;
125 return 0;
126 }
127 }
128
129 return 1;
130}
131
132/*
133 * Removes an image_id from the bl1_fwu_loaded_ids array.
134 * Returns 0 on success, 1 on error.
135 */
136static int bl1_fwu_remove_loaded_id(int image_id)
137{
138 int i;
139
140 /* Find the ID */
141 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
142 if (bl1_fwu_loaded_ids[i] == image_id) {
143 bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID;
144 return 0;
145 }
146 }
147
148 return 1;
149}
150
151/*******************************************************************************
152 * This function checks if the specified image overlaps another image already
153 * loaded. It returns 0 if there is no overlap, a negative error code otherwise.
154 ******************************************************************************/
155static int bl1_fwu_image_check_overlaps(int image_id)
156{
157 const image_desc_t *image_desc, *checked_image_desc;
158 const image_info_t *info, *checked_info;
159
160 uintptr_t image_base, image_end;
161 uintptr_t checked_image_base, checked_image_end;
162
163 checked_image_desc = bl1_plat_get_image_desc(image_id);
164 checked_info = &checked_image_desc->image_info;
165
166 /* Image being checked mustn't be empty. */
167 assert(checked_info->image_size != 0);
168
169 checked_image_base = checked_info->image_base;
170 checked_image_end = checked_image_base + checked_info->image_size - 1;
171 /* No need to check for overlaps, it's done in bl1_fwu_image_copy(). */
172
173 for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
174
175 /* Don't check image against itself. */
176 if (bl1_fwu_loaded_ids[i] == image_id)
177 continue;
178
179 image_desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]);
180
181 /* Only check images that are loaded or being loaded. */
182 assert (image_desc->state != IMAGE_STATE_RESET);
183
184 info = &image_desc->image_info;
185
186 /* There cannot be overlaps with an empty image. */
187 if (info->image_size == 0)
188 continue;
189
190 image_base = info->image_base;
191 image_end = image_base + info->image_size - 1;
192 /*
193 * Overflows cannot happen. It is checked in
194 * bl1_fwu_image_copy() when the image goes from RESET to
195 * COPYING or COPIED.
196 */
197 assert (image_end > image_base);
198
199 /* Check if there are overlaps. */
200 if (!(image_end < checked_image_base ||
201 checked_image_end < image_base)) {
202 VERBOSE("Image with ID %d overlaps existing image with ID %d",
203 checked_image_desc->image_id, image_desc->image_id);
204 return -EPERM;
205 }
206 }
207
208 return 0;
209}
210
211/*******************************************************************************
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100212 * This function is responsible for copying secure images in AP Secure RAM.
213 ******************************************************************************/
214static int bl1_fwu_image_copy(unsigned int image_id,
215 uintptr_t image_src,
216 unsigned int block_size,
217 unsigned int image_size,
218 unsigned int flags)
219{
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000220 uintptr_t dest_addr;
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000221 unsigned int remaining;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100222
223 /* Get the image descriptor. */
224 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000225 if (!image_desc) {
226 WARN("BL1-FWU: Invalid image ID %u\n", image_id);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100227 return -EPERM;
228 }
229
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000230 /*
231 * The request must originate from a non-secure caller and target a
232 * secure image. Any other scenario is invalid.
233 */
234 if (GET_SECURITY_STATE(flags) == SECURE) {
235 WARN("BL1-FWU: Copy not allowed from secure world.\n");
236 return -EPERM;
237 }
238 if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) {
239 WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
240 return -EPERM;
241 }
242
243 /* Check whether the FWU state machine is in the correct state. */
244 if ((image_desc->state != IMAGE_STATE_RESET) &&
245 (image_desc->state != IMAGE_STATE_COPYING)) {
246 WARN("BL1-FWU: Copy not allowed at this point of the FWU"
247 " process.\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100248 return -EPERM;
249 }
250
Sandrine Bailleux949a52d2016-11-11 16:44:37 +0000251 if ((!image_src) || (!block_size) ||
252 check_uptr_overflow(image_src, block_size - 1)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100253 WARN("BL1-FWU: Copy not allowed due to invalid image source"
254 " or block size\n");
255 return -ENOMEM;
256 }
257
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100258 if (image_desc->state == IMAGE_STATE_COPYING) {
Sandrine Bailleux1bfb7062016-11-14 14:58:05 +0000259 /*
260 * There must have been at least 1 copy operation for this image
261 * previously.
262 */
263 assert(image_desc->copied_size != 0);
264 /*
265 * The image size must have been recorded in the 1st copy
266 * operation.
267 */
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000268 image_size = image_desc->image_info.image_size;
Sandrine Bailleux1bfb7062016-11-14 14:58:05 +0000269 assert(image_size != 0);
270 assert(image_desc->copied_size < image_size);
271
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100272 INFO("BL1-FWU: Continuing image copy in blocks\n");
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000273 } else { /* image_desc->state == IMAGE_STATE_RESET */
274 INFO("BL1-FWU: Initial call to copy an image\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100275
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000276 /*
277 * image_size is relevant only for the 1st copy request, it is
278 * then ignored for subsequent calls for this image.
279 */
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100280 if (!image_size) {
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000281 WARN("BL1-FWU: Copy not allowed due to invalid image"
282 " size\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100283 return -ENOMEM;
284 }
285
Yatharth Kochar53d703a2016-11-11 13:57:50 +0000286#if LOAD_IMAGE_V2
287 /* Check that the image size to load is within limit */
288 if (image_size > image_desc->image_info.image_max_size) {
289 WARN("BL1-FWU: Image size out of bounds\n");
290 return -ENOMEM;
291 }
292#else
Sandrine Bailleux949a52d2016-11-11 16:44:37 +0000293 /*
294 * Check the image will fit into the free trusted RAM after BL1
295 * load.
296 */
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000297 const meminfo_t *mem_layout = bl1_plat_sec_mem_layout();
Sandrine Bailleux949a52d2016-11-11 16:44:37 +0000298 if (!is_mem_free(mem_layout->free_base, mem_layout->free_size,
299 image_desc->image_info.image_base,
300 image_size)) {
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000301 WARN("BL1-FWU: Copy not allowed due to insufficient"
302 " resources.\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100303 return -ENOMEM;
304 }
Yatharth Kochar53d703a2016-11-11 13:57:50 +0000305#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100306
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000307 /* Save the given image size. */
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100308 image_desc->image_info.image_size = image_size;
309
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100310 /* Make sure the image doesn't overlap other images. */
311 if (bl1_fwu_image_check_overlaps(image_id)) {
312 image_desc->image_info.image_size = 0;
313 WARN("BL1-FWU: This image overlaps another one\n");
314 return -EPERM;
315 }
316
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000317 /*
318 * copied_size must be explicitly initialized here because the
319 * FWU code doesn't necessarily do it when it resets the state
320 * machine.
321 */
322 image_desc->copied_size = 0;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100323 }
324
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000325 /*
326 * If the given block size is more than the total image size
327 * then clip the former to the latter.
328 */
329 remaining = image_size - image_desc->copied_size;
330 if (block_size > remaining) {
331 WARN("BL1-FWU: Block size is too big, clipping it.\n");
332 block_size = remaining;
333 }
334
335 /* Make sure the source image is mapped in memory. */
336 if (bl1_plat_mem_check(image_src, block_size, flags)) {
337 WARN("BL1-FWU: Source image is not mapped.\n");
338 return -ENOMEM;
339 }
340
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100341 if (bl1_fwu_add_loaded_id(image_id)) {
342 WARN("BL1-FWU: Too many images loaded at the same time.\n");
343 return -ENOMEM;
344 }
345
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000346 /* Everything looks sane. Go ahead and copy the block of data. */
347 dest_addr = image_desc->image_info.image_base + image_desc->copied_size;
348 memcpy((void *) dest_addr, (const void *) image_src, block_size);
349 flush_dcache_range(dest_addr, block_size);
350
351 image_desc->copied_size += block_size;
352 image_desc->state = (block_size == remaining) ?
353 IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
354
355 INFO("BL1-FWU: Copy operation successful.\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100356 return 0;
357}
358
359/*******************************************************************************
360 * This function is responsible for authenticating Normal/Secure images.
361 ******************************************************************************/
362static int bl1_fwu_image_auth(unsigned int image_id,
363 uintptr_t image_src,
364 unsigned int image_size,
365 unsigned int flags)
366{
367 int result;
368 uintptr_t base_addr;
369 unsigned int total_size;
370
371 /* Get the image descriptor. */
372 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
373 if (!image_desc)
374 return -EPERM;
375
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000376 if (GET_SECURITY_STATE(flags) == SECURE) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100377 if (image_desc->state != IMAGE_STATE_RESET) {
378 WARN("BL1-FWU: Authentication from secure world "
379 "while in invalid state\n");
380 return -EPERM;
381 }
382 } else {
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000383 if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100384 if (image_desc->state != IMAGE_STATE_COPIED) {
385 WARN("BL1-FWU: Authentication of secure image "
386 "from non-secure world while not in copied state\n");
387 return -EPERM;
388 }
389 } else {
390 if (image_desc->state != IMAGE_STATE_RESET) {
391 WARN("BL1-FWU: Authentication of non-secure image "
392 "from non-secure world while in invalid state\n");
393 return -EPERM;
394 }
395 }
396 }
397
398 if (image_desc->state == IMAGE_STATE_COPIED) {
399 /*
400 * Image is in COPIED state.
401 * Use the stored address and size.
402 */
403 base_addr = image_desc->image_info.image_base;
404 total_size = image_desc->image_info.image_size;
405 } else {
Sandrine Bailleux949a52d2016-11-11 16:44:37 +0000406 if ((!image_src) || (!image_size) ||
407 check_uptr_overflow(image_src, image_size - 1)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100408 WARN("BL1-FWU: Auth not allowed due to invalid"
409 " image source/size\n");
410 return -ENOMEM;
411 }
412
413 /*
414 * Image is in RESET state.
415 * Check the parameters and authenticate the source image in place.
416 */
Dan Handley03131c82015-12-14 16:26:43 +0000417 if (bl1_plat_mem_check(image_src, image_size, \
418 image_desc->ep_info.h.attr)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100419 WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
420 return -ENOMEM;
421 }
422
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100423 if (bl1_fwu_add_loaded_id(image_id)) {
424 WARN("BL1-FWU: Too many images loaded at the same time.\n");
425 return -ENOMEM;
426 }
427
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100428 base_addr = image_src;
429 total_size = image_size;
430
431 /* Update the image size in the descriptor. */
432 image_desc->image_info.image_size = total_size;
433 }
434
435 /*
436 * Authenticate the image.
437 */
438 INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
439 result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
440 if (result != 0) {
441 WARN("BL1-FWU: Authentication Failed err=%d\n", result);
442
443 /*
444 * Authentication has failed.
445 * Clear the memory if the image was copied.
446 * This is to prevent an attack where this contains
447 * some malicious code that can somehow be executed later.
448 */
449 if (image_desc->state == IMAGE_STATE_COPIED) {
450 /* Clear the memory.*/
Douglas Raillard308d3592016-12-02 13:51:54 +0000451 zero_normalmem((void *)base_addr, total_size);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100452 flush_dcache_range(base_addr, total_size);
453
454 /* Indicate that image can be copied again*/
455 image_desc->state = IMAGE_STATE_RESET;
456 }
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100457
458 /*
459 * Even if this fails it's ok because the ID isn't in the array.
460 * The image cannot be in RESET state here, it is checked at the
461 * beginning of the function.
462 */
463 bl1_fwu_remove_loaded_id(image_id);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100464 return -EAUTH;
465 }
466
467 /* Indicate that image is in authenticated state. */
468 image_desc->state = IMAGE_STATE_AUTHENTICATED;
469
470 /*
471 * Flush image_info to memory so that other
472 * secure world images can see changes.
473 */
474 flush_dcache_range((unsigned long)&image_desc->image_info,
475 sizeof(image_info_t));
476
477 INFO("BL1-FWU: Authentication was successful\n");
478
479 return 0;
480}
481
482/*******************************************************************************
483 * This function is responsible for executing Secure images.
484 ******************************************************************************/
485static int bl1_fwu_image_execute(unsigned int image_id,
486 void **handle,
487 unsigned int flags)
488{
489 /* Get the image descriptor. */
490 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
491
492 /*
493 * Execution is NOT allowed if:
Dan Handley28955d52015-12-15 10:52:33 +0000494 * image_id is invalid OR
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100495 * Caller is from Secure world OR
496 * Image is Non-Secure OR
497 * Image is Non-Executable OR
498 * Image is NOT in AUTHENTICATED state.
499 */
500 if ((!image_desc) ||
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000501 (GET_SECURITY_STATE(flags) == SECURE) ||
502 (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) ||
503 (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) ||
504 (image_desc->state != IMAGE_STATE_AUTHENTICATED)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100505 WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
506 return -EPERM;
507 }
508
509 INFO("BL1-FWU: Executing Secure image\n");
510
dp-arma4409002017-02-15 11:07:55 +0000511#ifdef AARCH64
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100512 /* Save NS-EL1 system registers. */
513 cm_el1_sysregs_context_save(NON_SECURE);
dp-arma4409002017-02-15 11:07:55 +0000514#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100515
516 /* Prepare the image for execution. */
517 bl1_prepare_next_image(image_id);
518
519 /* Update the secure image id. */
520 sec_exec_image_id = image_id;
521
dp-arma4409002017-02-15 11:07:55 +0000522#ifdef AARCH64
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100523 *handle = cm_get_context(SECURE);
dp-arma4409002017-02-15 11:07:55 +0000524#else
525 *handle = smc_get_ctx(SECURE);
526#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100527 return 0;
528}
529
530/*******************************************************************************
Dan Handley28955d52015-12-15 10:52:33 +0000531 * This function is responsible for resuming execution in the other security
532 * world
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100533 ******************************************************************************/
Dan Handley28955d52015-12-15 10:52:33 +0000534static register_t bl1_fwu_image_resume(register_t image_param,
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100535 void **handle,
536 unsigned int flags)
537{
538 image_desc_t *image_desc;
539 unsigned int resume_sec_state;
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000540 unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100541
Dan Handley28955d52015-12-15 10:52:33 +0000542 /* Get the image descriptor for last executed secure image id. */
543 image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
544 if (caller_sec_state == NON_SECURE) {
545 if (!image_desc) {
546 WARN("BL1-FWU: Resume not allowed due to no available"
547 "secure image\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100548 return -EPERM;
549 }
Dan Handley28955d52015-12-15 10:52:33 +0000550 } else {
551 /* image_desc must be valid for secure world callers */
552 assert(image_desc);
553 }
554
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000555 assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
556 assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
Dan Handley28955d52015-12-15 10:52:33 +0000557
558 if (caller_sec_state == SECURE) {
559 assert(image_desc->state == IMAGE_STATE_EXECUTED);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100560
561 /* Update the flags. */
562 image_desc->state = IMAGE_STATE_INTERRUPTED;
563 resume_sec_state = NON_SECURE;
564 } else {
Dan Handley28955d52015-12-15 10:52:33 +0000565 assert(image_desc->state == IMAGE_STATE_INTERRUPTED);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100566
567 /* Update the flags. */
568 image_desc->state = IMAGE_STATE_EXECUTED;
569 resume_sec_state = SECURE;
570 }
571
dp-arma4409002017-02-15 11:07:55 +0000572 INFO("BL1-FWU: Resuming %s world context\n",
573 (resume_sec_state == SECURE) ? "secure" : "normal");
574
575#ifdef AARCH64
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100576 /* Save the EL1 system registers of calling world. */
Dan Handley28955d52015-12-15 10:52:33 +0000577 cm_el1_sysregs_context_save(caller_sec_state);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100578
579 /* Restore the EL1 system registers of resuming world. */
580 cm_el1_sysregs_context_restore(resume_sec_state);
581
582 /* Update the next context. */
583 cm_set_next_eret_context(resume_sec_state);
584
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100585 *handle = cm_get_context(resume_sec_state);
dp-arma4409002017-02-15 11:07:55 +0000586#else
587 /* Update the next context. */
588 cm_set_next_context(cm_get_context(resume_sec_state));
589
590 /* Prepare the smc context for the next BL image. */
591 smc_set_next_ctx(resume_sec_state);
592
593 *handle = smc_get_ctx(resume_sec_state);
594#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100595 return image_param;
596}
597
598/*******************************************************************************
599 * This function is responsible for resuming normal world context.
600 ******************************************************************************/
601static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
602{
Dan Handley28955d52015-12-15 10:52:33 +0000603 image_desc_t *image_desc;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100604
Dan Handley28955d52015-12-15 10:52:33 +0000605 /* Make sure caller is from the secure world */
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000606 if (GET_SECURITY_STATE(flags) == NON_SECURE) {
Dan Handley28955d52015-12-15 10:52:33 +0000607 WARN("BL1-FWU: Image done not allowed from normal world\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100608 return -EPERM;
609 }
610
Dan Handley28955d52015-12-15 10:52:33 +0000611 /* Get the image descriptor for last executed secure image id */
612 image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
613
614 /* image_desc must correspond to a valid secure executing image */
615 assert(image_desc);
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000616 assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
617 assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
Dan Handley28955d52015-12-15 10:52:33 +0000618 assert(image_desc->state == IMAGE_STATE_EXECUTED);
619
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100620#if ENABLE_ASSERTIONS
621 int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id);
622 assert(rc == 0);
623#else
624 bl1_fwu_remove_loaded_id(sec_exec_image_id);
625#endif
626
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100627 /* Update the flags. */
628 image_desc->state = IMAGE_STATE_RESET;
629 sec_exec_image_id = INVALID_IMAGE_ID;
630
dp-arma4409002017-02-15 11:07:55 +0000631 INFO("BL1-FWU: Resuming Normal world context\n");
632#ifdef AARCH64
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100633 /*
634 * Secure world is done so no need to save the context.
635 * Just restore the Non-Secure context.
636 */
637 cm_el1_sysregs_context_restore(NON_SECURE);
638
639 /* Update the next context. */
640 cm_set_next_eret_context(NON_SECURE);
641
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100642 *handle = cm_get_context(NON_SECURE);
dp-arma4409002017-02-15 11:07:55 +0000643#else
644 /* Update the next context. */
645 cm_set_next_context(cm_get_context(NON_SECURE));
646
647 /* Prepare the smc context for the next BL image. */
648 smc_set_next_ctx(NON_SECURE);
649
650 *handle = smc_get_ctx(NON_SECURE);
651#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100652 return 0;
653}
654
655/*******************************************************************************
656 * This function provides the opportunity for users to perform any
657 * platform specific handling after the Firmware update is done.
658 ******************************************************************************/
Dan Handley1f37b942015-12-15 14:28:24 +0000659__dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100660{
661 NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
662
663 /*
664 * Call platform done function.
665 */
Dan Handley1f37b942015-12-15 14:28:24 +0000666 bl1_plat_fwu_done(client_cookie, reserved);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100667 assert(0);
668}