blob: 85eee1ade3ee6997651b3278cdfb11c88b66bc39 [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);
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +010043static int bl1_fwu_image_reset(unsigned int image_id,
44 unsigned int flags);
Dan Handley1f37b942015-12-15 14:28:24 +000045__dead2 static void bl1_fwu_done(void *client_cookie, void *reserved);
Yatharth Kochar48bfb882015-10-10 19:06:53 +010046
47/*
48 * This keeps track of last executed secure image id.
49 */
50static unsigned int sec_exec_image_id = INVALID_IMAGE_ID;
51
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +010052/* Authentication status of each image. */
53extern unsigned int auth_img_flags[];
54
dp-arma4409002017-02-15 11:07:55 +000055void cm_set_next_context(void *cpu_context);
56
Yatharth Kochar48bfb882015-10-10 19:06:53 +010057/*******************************************************************************
58 * Top level handler for servicing FWU SMCs.
59 ******************************************************************************/
60register_t bl1_fwu_smc_handler(unsigned int smc_fid,
61 register_t x1,
62 register_t x2,
63 register_t x3,
64 register_t x4,
65 void *cookie,
66 void *handle,
67 unsigned int flags)
68{
69
70 switch (smc_fid) {
71 case FWU_SMC_IMAGE_COPY:
72 SMC_RET1(handle, bl1_fwu_image_copy(x1, x2, x3, x4, flags));
73
74 case FWU_SMC_IMAGE_AUTH:
75 SMC_RET1(handle, bl1_fwu_image_auth(x1, x2, x3, flags));
76
77 case FWU_SMC_IMAGE_EXECUTE:
78 SMC_RET1(handle, bl1_fwu_image_execute(x1, &handle, flags));
79
80 case FWU_SMC_IMAGE_RESUME:
Dan Handley28955d52015-12-15 10:52:33 +000081 SMC_RET1(handle, bl1_fwu_image_resume(x1, &handle, flags));
Yatharth Kochar48bfb882015-10-10 19:06:53 +010082
83 case FWU_SMC_SEC_IMAGE_DONE:
84 SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
85
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +010086 case FWU_SMC_IMAGE_RESET:
87 SMC_RET1(handle, bl1_fwu_image_reset(x1, flags));
88
Yatharth Kochar48bfb882015-10-10 19:06:53 +010089 case FWU_SMC_UPDATE_DONE:
Dan Handley1f37b942015-12-15 14:28:24 +000090 bl1_fwu_done((void *)x1, NULL);
Yatharth Kochar48bfb882015-10-10 19:06:53 +010091 /* We should never return from bl1_fwu_done() */
92
93 default:
94 assert(0);
95 break;
96 }
97
Antonio Nino Diaz7a317a72017-04-04 17:08:32 +010098 SMC_RET1(handle, SMC_UNK);
Yatharth Kochar48bfb882015-10-10 19:06:53 +010099}
100
101/*******************************************************************************
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100102 * Utility functions to keep track of the images that are loaded at any time.
103 ******************************************************************************/
104
105#ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
106#define FWU_MAX_SIMULTANEOUS_IMAGES PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
107#else
108#define FWU_MAX_SIMULTANEOUS_IMAGES 10
109#endif
110
111static int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = {
112 [0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID
113};
114
115/*
116 * Adds an image_id to the bl1_fwu_loaded_ids array.
117 * Returns 0 on success, 1 on error.
118 */
119static int bl1_fwu_add_loaded_id(int image_id)
120{
121 int i;
122
123 /* Check if the ID is already in the list */
124 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
125 if (bl1_fwu_loaded_ids[i] == image_id)
126 return 0;
127 }
128
129 /* Find an empty slot */
130 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
131 if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) {
132 bl1_fwu_loaded_ids[i] = image_id;
133 return 0;
134 }
135 }
136
137 return 1;
138}
139
140/*
141 * Removes an image_id from the bl1_fwu_loaded_ids array.
142 * Returns 0 on success, 1 on error.
143 */
144static int bl1_fwu_remove_loaded_id(int image_id)
145{
146 int i;
147
148 /* Find the ID */
149 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
150 if (bl1_fwu_loaded_ids[i] == image_id) {
151 bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID;
152 return 0;
153 }
154 }
155
156 return 1;
157}
158
159/*******************************************************************************
160 * This function checks if the specified image overlaps another image already
161 * loaded. It returns 0 if there is no overlap, a negative error code otherwise.
162 ******************************************************************************/
163static int bl1_fwu_image_check_overlaps(int image_id)
164{
165 const image_desc_t *image_desc, *checked_image_desc;
166 const image_info_t *info, *checked_info;
167
168 uintptr_t image_base, image_end;
169 uintptr_t checked_image_base, checked_image_end;
170
171 checked_image_desc = bl1_plat_get_image_desc(image_id);
172 checked_info = &checked_image_desc->image_info;
173
174 /* Image being checked mustn't be empty. */
175 assert(checked_info->image_size != 0);
176
177 checked_image_base = checked_info->image_base;
178 checked_image_end = checked_image_base + checked_info->image_size - 1;
Soby Mathewee05ae12017-06-15 16:11:48 +0100179 /* No need to check for overflows, it's done in bl1_fwu_image_copy(). */
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100180
181 for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
182
Soby Mathewee05ae12017-06-15 16:11:48 +0100183 /* Skip INVALID_IMAGE_IDs and don't check image against itself */
184 if ((bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) ||
185 (bl1_fwu_loaded_ids[i] == image_id))
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100186 continue;
187
188 image_desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]);
189
190 /* Only check images that are loaded or being loaded. */
Soby Mathewee05ae12017-06-15 16:11:48 +0100191 assert (image_desc && image_desc->state != IMAGE_STATE_RESET);
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100192
193 info = &image_desc->image_info;
194
195 /* There cannot be overlaps with an empty image. */
196 if (info->image_size == 0)
197 continue;
198
199 image_base = info->image_base;
200 image_end = image_base + info->image_size - 1;
201 /*
202 * Overflows cannot happen. It is checked in
203 * bl1_fwu_image_copy() when the image goes from RESET to
204 * COPYING or COPIED.
205 */
206 assert (image_end > image_base);
207
208 /* Check if there are overlaps. */
209 if (!(image_end < checked_image_base ||
210 checked_image_end < image_base)) {
211 VERBOSE("Image with ID %d overlaps existing image with ID %d",
212 checked_image_desc->image_id, image_desc->image_id);
213 return -EPERM;
214 }
215 }
216
217 return 0;
218}
219
220/*******************************************************************************
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100221 * This function is responsible for copying secure images in AP Secure RAM.
222 ******************************************************************************/
223static int bl1_fwu_image_copy(unsigned int image_id,
224 uintptr_t image_src,
225 unsigned int block_size,
226 unsigned int image_size,
227 unsigned int flags)
228{
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000229 uintptr_t dest_addr;
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000230 unsigned int remaining;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100231
232 /* Get the image descriptor. */
233 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000234 if (!image_desc) {
235 WARN("BL1-FWU: Invalid image ID %u\n", image_id);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100236 return -EPERM;
237 }
238
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000239 /*
240 * The request must originate from a non-secure caller and target a
241 * secure image. Any other scenario is invalid.
242 */
243 if (GET_SECURITY_STATE(flags) == SECURE) {
244 WARN("BL1-FWU: Copy not allowed from secure world.\n");
245 return -EPERM;
246 }
247 if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) {
248 WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
249 return -EPERM;
250 }
251
252 /* Check whether the FWU state machine is in the correct state. */
253 if ((image_desc->state != IMAGE_STATE_RESET) &&
254 (image_desc->state != IMAGE_STATE_COPYING)) {
255 WARN("BL1-FWU: Copy not allowed at this point of the FWU"
256 " process.\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100257 return -EPERM;
258 }
259
Sandrine Bailleux949a52d2016-11-11 16:44:37 +0000260 if ((!image_src) || (!block_size) ||
261 check_uptr_overflow(image_src, block_size - 1)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100262 WARN("BL1-FWU: Copy not allowed due to invalid image source"
263 " or block size\n");
264 return -ENOMEM;
265 }
266
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100267 if (image_desc->state == IMAGE_STATE_COPYING) {
Sandrine Bailleux1bfb7062016-11-14 14:58:05 +0000268 /*
269 * There must have been at least 1 copy operation for this image
270 * previously.
271 */
272 assert(image_desc->copied_size != 0);
273 /*
274 * The image size must have been recorded in the 1st copy
275 * operation.
276 */
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000277 image_size = image_desc->image_info.image_size;
Sandrine Bailleux1bfb7062016-11-14 14:58:05 +0000278 assert(image_size != 0);
279 assert(image_desc->copied_size < image_size);
280
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100281 INFO("BL1-FWU: Continuing image copy in blocks\n");
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000282 } else { /* image_desc->state == IMAGE_STATE_RESET */
283 INFO("BL1-FWU: Initial call to copy an image\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100284
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000285 /*
286 * image_size is relevant only for the 1st copy request, it is
287 * then ignored for subsequent calls for this image.
288 */
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100289 if (!image_size) {
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000290 WARN("BL1-FWU: Copy not allowed due to invalid image"
291 " size\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100292 return -ENOMEM;
293 }
294
Yatharth Kochar53d703a2016-11-11 13:57:50 +0000295#if LOAD_IMAGE_V2
296 /* Check that the image size to load is within limit */
297 if (image_size > image_desc->image_info.image_max_size) {
298 WARN("BL1-FWU: Image size out of bounds\n");
299 return -ENOMEM;
300 }
301#else
Sandrine Bailleux949a52d2016-11-11 16:44:37 +0000302 /*
303 * Check the image will fit into the free trusted RAM after BL1
304 * load.
305 */
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000306 const meminfo_t *mem_layout = bl1_plat_sec_mem_layout();
Sandrine Bailleux949a52d2016-11-11 16:44:37 +0000307 if (!is_mem_free(mem_layout->free_base, mem_layout->free_size,
308 image_desc->image_info.image_base,
309 image_size)) {
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000310 WARN("BL1-FWU: Copy not allowed due to insufficient"
311 " resources.\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100312 return -ENOMEM;
313 }
Yatharth Kochar53d703a2016-11-11 13:57:50 +0000314#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100315
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000316 /* Save the given image size. */
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100317 image_desc->image_info.image_size = image_size;
318
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100319 /* Make sure the image doesn't overlap other images. */
320 if (bl1_fwu_image_check_overlaps(image_id)) {
321 image_desc->image_info.image_size = 0;
322 WARN("BL1-FWU: This image overlaps another one\n");
323 return -EPERM;
324 }
325
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000326 /*
327 * copied_size must be explicitly initialized here because the
328 * FWU code doesn't necessarily do it when it resets the state
329 * machine.
330 */
331 image_desc->copied_size = 0;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100332 }
333
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000334 /*
335 * If the given block size is more than the total image size
336 * then clip the former to the latter.
337 */
338 remaining = image_size - image_desc->copied_size;
339 if (block_size > remaining) {
340 WARN("BL1-FWU: Block size is too big, clipping it.\n");
341 block_size = remaining;
342 }
343
344 /* Make sure the source image is mapped in memory. */
345 if (bl1_plat_mem_check(image_src, block_size, flags)) {
346 WARN("BL1-FWU: Source image is not mapped.\n");
347 return -ENOMEM;
348 }
349
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100350 if (bl1_fwu_add_loaded_id(image_id)) {
351 WARN("BL1-FWU: Too many images loaded at the same time.\n");
352 return -ENOMEM;
353 }
354
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000355 /* Everything looks sane. Go ahead and copy the block of data. */
356 dest_addr = image_desc->image_info.image_base + image_desc->copied_size;
357 memcpy((void *) dest_addr, (const void *) image_src, block_size);
358 flush_dcache_range(dest_addr, block_size);
359
360 image_desc->copied_size += block_size;
361 image_desc->state = (block_size == remaining) ?
362 IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
363
364 INFO("BL1-FWU: Copy operation successful.\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100365 return 0;
366}
367
368/*******************************************************************************
369 * This function is responsible for authenticating Normal/Secure images.
370 ******************************************************************************/
371static int bl1_fwu_image_auth(unsigned int image_id,
372 uintptr_t image_src,
373 unsigned int image_size,
374 unsigned int flags)
375{
376 int result;
377 uintptr_t base_addr;
378 unsigned int total_size;
379
380 /* Get the image descriptor. */
381 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
382 if (!image_desc)
383 return -EPERM;
384
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000385 if (GET_SECURITY_STATE(flags) == SECURE) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100386 if (image_desc->state != IMAGE_STATE_RESET) {
387 WARN("BL1-FWU: Authentication from secure world "
388 "while in invalid state\n");
389 return -EPERM;
390 }
391 } else {
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000392 if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100393 if (image_desc->state != IMAGE_STATE_COPIED) {
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 {
399 if (image_desc->state != IMAGE_STATE_RESET) {
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
407 if (image_desc->state == IMAGE_STATE_COPIED) {
408 /*
409 * Image is in COPIED state.
410 * Use the stored address and size.
411 */
412 base_addr = image_desc->image_info.image_base;
413 total_size = image_desc->image_info.image_size;
414 } else {
Sandrine Bailleux949a52d2016-11-11 16:44:37 +0000415 if ((!image_src) || (!image_size) ||
416 check_uptr_overflow(image_src, image_size - 1)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100417 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 */
Dan Handley03131c82015-12-14 16:26:43 +0000426 if (bl1_plat_mem_check(image_src, image_size, \
427 image_desc->ep_info.h.attr)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100428 WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
429 return -ENOMEM;
430 }
431
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100432 if (bl1_fwu_add_loaded_id(image_id)) {
433 WARN("BL1-FWU: Too many images loaded at the same time.\n");
434 return -ENOMEM;
435 }
436
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100437 base_addr = image_src;
438 total_size = image_size;
439
440 /* Update the image size in the descriptor. */
441 image_desc->image_info.image_size = total_size;
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 */
458 if (image_desc->state == IMAGE_STATE_COPIED) {
459 /* Clear the memory.*/
Douglas Raillard308d3592016-12-02 13:51:54 +0000460 zero_normalmem((void *)base_addr, total_size);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100461 flush_dcache_range(base_addr, total_size);
462
463 /* Indicate that image can be copied again*/
464 image_desc->state = IMAGE_STATE_RESET;
465 }
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100466
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 */
472 bl1_fwu_remove_loaded_id(image_id);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100473 return -EAUTH;
474 }
475
476 /* Indicate that image is in authenticated state. */
477 image_desc->state = IMAGE_STATE_AUTHENTICATED;
478
479 /*
480 * Flush image_info to memory so that other
481 * secure world images can see changes.
482 */
483 flush_dcache_range((unsigned long)&image_desc->image_info,
484 sizeof(image_info_t));
485
486 INFO("BL1-FWU: Authentication was successful\n");
487
488 return 0;
489}
490
491/*******************************************************************************
492 * This function is responsible for executing Secure images.
493 ******************************************************************************/
494static int bl1_fwu_image_execute(unsigned int image_id,
495 void **handle,
496 unsigned int flags)
497{
498 /* Get the image descriptor. */
499 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
500
501 /*
502 * Execution is NOT allowed if:
Dan Handley28955d52015-12-15 10:52:33 +0000503 * image_id is invalid OR
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100504 * Caller is from Secure world OR
505 * Image is Non-Secure OR
506 * Image is Non-Executable OR
507 * Image is NOT in AUTHENTICATED state.
508 */
509 if ((!image_desc) ||
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000510 (GET_SECURITY_STATE(flags) == SECURE) ||
511 (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) ||
512 (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) ||
513 (image_desc->state != IMAGE_STATE_AUTHENTICATED)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100514 WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
515 return -EPERM;
516 }
517
518 INFO("BL1-FWU: Executing Secure image\n");
519
dp-arma4409002017-02-15 11:07:55 +0000520#ifdef AARCH64
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100521 /* Save NS-EL1 system registers. */
522 cm_el1_sysregs_context_save(NON_SECURE);
dp-arma4409002017-02-15 11:07:55 +0000523#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100524
525 /* Prepare the image for execution. */
526 bl1_prepare_next_image(image_id);
527
528 /* Update the secure image id. */
529 sec_exec_image_id = image_id;
530
dp-arma4409002017-02-15 11:07:55 +0000531#ifdef AARCH64
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100532 *handle = cm_get_context(SECURE);
dp-arma4409002017-02-15 11:07:55 +0000533#else
534 *handle = smc_get_ctx(SECURE);
535#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100536 return 0;
537}
538
539/*******************************************************************************
Dan Handley28955d52015-12-15 10:52:33 +0000540 * This function is responsible for resuming execution in the other security
541 * world
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100542 ******************************************************************************/
Dan Handley28955d52015-12-15 10:52:33 +0000543static register_t bl1_fwu_image_resume(register_t image_param,
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100544 void **handle,
545 unsigned int flags)
546{
547 image_desc_t *image_desc;
548 unsigned int resume_sec_state;
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000549 unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100550
Dan Handley28955d52015-12-15 10:52:33 +0000551 /* Get the image descriptor for last executed secure image id. */
552 image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
553 if (caller_sec_state == NON_SECURE) {
554 if (!image_desc) {
555 WARN("BL1-FWU: Resume not allowed due to no available"
556 "secure image\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100557 return -EPERM;
558 }
Dan Handley28955d52015-12-15 10:52:33 +0000559 } else {
560 /* image_desc must be valid for secure world callers */
561 assert(image_desc);
562 }
563
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000564 assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
565 assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
Dan Handley28955d52015-12-15 10:52:33 +0000566
567 if (caller_sec_state == SECURE) {
568 assert(image_desc->state == IMAGE_STATE_EXECUTED);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100569
570 /* Update the flags. */
571 image_desc->state = IMAGE_STATE_INTERRUPTED;
572 resume_sec_state = NON_SECURE;
573 } else {
Dan Handley28955d52015-12-15 10:52:33 +0000574 assert(image_desc->state == IMAGE_STATE_INTERRUPTED);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100575
576 /* Update the flags. */
577 image_desc->state = IMAGE_STATE_EXECUTED;
578 resume_sec_state = SECURE;
579 }
580
dp-arma4409002017-02-15 11:07:55 +0000581 INFO("BL1-FWU: Resuming %s world context\n",
582 (resume_sec_state == SECURE) ? "secure" : "normal");
583
584#ifdef AARCH64
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100585 /* Save the EL1 system registers of calling world. */
Dan Handley28955d52015-12-15 10:52:33 +0000586 cm_el1_sysregs_context_save(caller_sec_state);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100587
588 /* Restore the EL1 system registers of resuming world. */
589 cm_el1_sysregs_context_restore(resume_sec_state);
590
591 /* Update the next context. */
592 cm_set_next_eret_context(resume_sec_state);
593
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100594 *handle = cm_get_context(resume_sec_state);
dp-arma4409002017-02-15 11:07:55 +0000595#else
596 /* Update the next context. */
597 cm_set_next_context(cm_get_context(resume_sec_state));
598
599 /* Prepare the smc context for the next BL image. */
600 smc_set_next_ctx(resume_sec_state);
601
602 *handle = smc_get_ctx(resume_sec_state);
603#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100604 return image_param;
605}
606
607/*******************************************************************************
608 * This function is responsible for resuming normal world context.
609 ******************************************************************************/
610static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
611{
Dan Handley28955d52015-12-15 10:52:33 +0000612 image_desc_t *image_desc;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100613
Dan Handley28955d52015-12-15 10:52:33 +0000614 /* Make sure caller is from the secure world */
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000615 if (GET_SECURITY_STATE(flags) == NON_SECURE) {
Dan Handley28955d52015-12-15 10:52:33 +0000616 WARN("BL1-FWU: Image done not allowed from normal world\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100617 return -EPERM;
618 }
619
Dan Handley28955d52015-12-15 10:52:33 +0000620 /* Get the image descriptor for last executed secure image id */
621 image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
622
623 /* image_desc must correspond to a valid secure executing image */
624 assert(image_desc);
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000625 assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
626 assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
Dan Handley28955d52015-12-15 10:52:33 +0000627 assert(image_desc->state == IMAGE_STATE_EXECUTED);
628
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100629#if ENABLE_ASSERTIONS
630 int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id);
631 assert(rc == 0);
632#else
633 bl1_fwu_remove_loaded_id(sec_exec_image_id);
634#endif
635
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100636 /* Update the flags. */
637 image_desc->state = IMAGE_STATE_RESET;
638 sec_exec_image_id = INVALID_IMAGE_ID;
639
dp-arma4409002017-02-15 11:07:55 +0000640 INFO("BL1-FWU: Resuming Normal world context\n");
641#ifdef AARCH64
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100642 /*
643 * Secure world is done so no need to save the context.
644 * Just restore the Non-Secure context.
645 */
646 cm_el1_sysregs_context_restore(NON_SECURE);
647
648 /* Update the next context. */
649 cm_set_next_eret_context(NON_SECURE);
650
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100651 *handle = cm_get_context(NON_SECURE);
dp-arma4409002017-02-15 11:07:55 +0000652#else
653 /* Update the next context. */
654 cm_set_next_context(cm_get_context(NON_SECURE));
655
656 /* Prepare the smc context for the next BL image. */
657 smc_set_next_ctx(NON_SECURE);
658
659 *handle = smc_get_ctx(NON_SECURE);
660#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100661 return 0;
662}
663
664/*******************************************************************************
665 * This function provides the opportunity for users to perform any
666 * platform specific handling after the Firmware update is done.
667 ******************************************************************************/
Dan Handley1f37b942015-12-15 14:28:24 +0000668__dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100669{
670 NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
671
672 /*
673 * Call platform done function.
674 */
Dan Handley1f37b942015-12-15 14:28:24 +0000675 bl1_plat_fwu_done(client_cookie, reserved);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100676 assert(0);
677}
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +0100678
679/*******************************************************************************
680 * This function resets an image to IMAGE_STATE_RESET. It fails if the image is
681 * being executed.
682 ******************************************************************************/
683static int bl1_fwu_image_reset(unsigned int image_id, unsigned int flags)
684{
685 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
686
687 if ((!image_desc) || (GET_SECURITY_STATE(flags) == SECURE)) {
688 WARN("BL1-FWU: Reset not allowed due to invalid args\n");
689 return -EPERM;
690 }
691
692 switch (image_desc->state) {
693
694 case IMAGE_STATE_RESET:
695 /* Nothing to do. */
696 break;
697
698 case IMAGE_STATE_INTERRUPTED:
699 case IMAGE_STATE_AUTHENTICATED:
700 case IMAGE_STATE_COPIED:
701 case IMAGE_STATE_COPYING:
702
703 if (bl1_fwu_remove_loaded_id(image_id)) {
704 WARN("BL1-FWU: Image reset couldn't find the image ID\n");
705 return -EPERM;
706 }
707
Soby Mathewee05ae12017-06-15 16:11:48 +0100708 if (image_desc->copied_size) {
709 /* Clear the memory if the image is copied */
710 assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
711
712 zero_normalmem((void *)image_desc->image_info.image_base,
713 image_desc->copied_size);
714 flush_dcache_range(image_desc->image_info.image_base,
715 image_desc->copied_size);
716 }
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +0100717
718 /* Reset status variables */
719 image_desc->copied_size = 0;
720 image_desc->image_info.image_size = 0;
721 image_desc->state = IMAGE_STATE_RESET;
722
723 /* Clear authentication state */
724 auth_img_flags[image_id] = 0;
725
726 break;
727
728 case IMAGE_STATE_EXECUTED:
729 default:
730 assert(0);
731 }
732
733 return 0;
734}