blob: 28b9eb90711d3d44f60a42f650f6795757868e6c [file] [log] [blame]
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +01001/*
Soby Mathew2f38ce32018-02-08 17:45:12 +00002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +01005 */
6
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +01007#include <arch_helpers.h>
Isla Mitchell99305012017-07-11 14:54:08 +01008#include <assert.h>
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +01009#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>
Antonio Nino Diaz3c817f42018-03-21 10:49:27 +000018#include <smccc_helpers.h>
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +010019#include <string.h>
Sandrine Bailleuxb39d75f2016-11-11 16:44:37 +000020#include <utils.h>
Yatharth Kochar71c9a5e2015-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,
Roberto Vargasbe126ed2018-02-12 12:36:17 +000027 uintptr_t image_src,
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +010028 unsigned int block_size,
29 unsigned int image_size,
30 unsigned int flags);
31static int bl1_fwu_image_auth(unsigned int image_id,
Roberto Vargasbe126ed2018-02-12 12:36:17 +000032 uintptr_t image_src,
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +010033 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 Handley8ec76522015-12-15 10:52:33 +000038static register_t bl1_fwu_image_resume(register_t image_param,
Yatharth Kochar71c9a5e2015-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 Diaz0e8e7202017-05-12 16:51:59 +010043static int bl1_fwu_image_reset(unsigned int image_id,
44 unsigned int flags);
Dan Handley89f8f332015-12-15 14:28:24 +000045__dead2 static void bl1_fwu_done(void *client_cookie, void *reserved);
Yatharth Kochar71c9a5e2015-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 Diaz0e8e7202017-05-12 16:51:59 +010052/* Authentication status of each image. */
Roberto Vargasbe126ed2018-02-12 12:36:17 +000053extern unsigned int auth_img_flags[MAX_NUMBER_IDS];
Antonio Nino Diaz0e8e7202017-05-12 16:51:59 +010054
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +010055/*******************************************************************************
56 * Top level handler for servicing FWU SMCs.
57 ******************************************************************************/
58register_t bl1_fwu_smc_handler(unsigned int smc_fid,
59 register_t x1,
60 register_t x2,
61 register_t x3,
62 register_t x4,
63 void *cookie,
64 void *handle,
65 unsigned int flags)
66{
67
68 switch (smc_fid) {
69 case FWU_SMC_IMAGE_COPY:
70 SMC_RET1(handle, bl1_fwu_image_copy(x1, x2, x3, x4, flags));
71
72 case FWU_SMC_IMAGE_AUTH:
73 SMC_RET1(handle, bl1_fwu_image_auth(x1, x2, x3, flags));
74
75 case FWU_SMC_IMAGE_EXECUTE:
76 SMC_RET1(handle, bl1_fwu_image_execute(x1, &handle, flags));
77
78 case FWU_SMC_IMAGE_RESUME:
Dan Handley8ec76522015-12-15 10:52:33 +000079 SMC_RET1(handle, bl1_fwu_image_resume(x1, &handle, flags));
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +010080
81 case FWU_SMC_SEC_IMAGE_DONE:
82 SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
83
Antonio Nino Diaz0e8e7202017-05-12 16:51:59 +010084 case FWU_SMC_IMAGE_RESET:
85 SMC_RET1(handle, bl1_fwu_image_reset(x1, flags));
86
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +010087 case FWU_SMC_UPDATE_DONE:
Dan Handley89f8f332015-12-15 14:28:24 +000088 bl1_fwu_done((void *)x1, NULL);
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +010089
90 default:
Daniel Boulby8942a1b2018-06-22 14:16:03 +010091 assert(0); /* Unreachable */
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +010092 break;
93 }
94
Antonio Nino Diazacb29142017-04-04 17:08:32 +010095 SMC_RET1(handle, SMC_UNK);
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +010096}
97
98/*******************************************************************************
Antonio Nino Diazd6a277f2017-06-01 13:40:17 +010099 * Utility functions to keep track of the images that are loaded at any time.
100 ******************************************************************************/
101
102#ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
103#define FWU_MAX_SIMULTANEOUS_IMAGES PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
104#else
105#define FWU_MAX_SIMULTANEOUS_IMAGES 10
106#endif
107
108static int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = {
109 [0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID
110};
111
112/*
113 * Adds an image_id to the bl1_fwu_loaded_ids array.
114 * Returns 0 on success, 1 on error.
115 */
116static int bl1_fwu_add_loaded_id(int image_id)
117{
118 int i;
119
120 /* Check if the ID is already in the list */
121 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
122 if (bl1_fwu_loaded_ids[i] == image_id)
123 return 0;
124 }
125
126 /* Find an empty slot */
127 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
128 if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) {
129 bl1_fwu_loaded_ids[i] = image_id;
130 return 0;
131 }
132 }
133
134 return 1;
135}
136
137/*
138 * Removes an image_id from the bl1_fwu_loaded_ids array.
139 * Returns 0 on success, 1 on error.
140 */
141static int bl1_fwu_remove_loaded_id(int image_id)
142{
143 int i;
144
145 /* Find the ID */
146 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
147 if (bl1_fwu_loaded_ids[i] == image_id) {
148 bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID;
149 return 0;
150 }
151 }
152
153 return 1;
154}
155
156/*******************************************************************************
157 * This function checks if the specified image overlaps another image already
158 * loaded. It returns 0 if there is no overlap, a negative error code otherwise.
159 ******************************************************************************/
160static int bl1_fwu_image_check_overlaps(int image_id)
161{
162 const image_desc_t *image_desc, *checked_image_desc;
163 const image_info_t *info, *checked_info;
164
165 uintptr_t image_base, image_end;
166 uintptr_t checked_image_base, checked_image_end;
167
168 checked_image_desc = bl1_plat_get_image_desc(image_id);
169 checked_info = &checked_image_desc->image_info;
170
171 /* Image being checked mustn't be empty. */
172 assert(checked_info->image_size != 0);
173
174 checked_image_base = checked_info->image_base;
175 checked_image_end = checked_image_base + checked_info->image_size - 1;
Soby Mathewf088b342017-06-15 16:11:48 +0100176 /* No need to check for overflows, it's done in bl1_fwu_image_copy(). */
Antonio Nino Diazd6a277f2017-06-01 13:40:17 +0100177
178 for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
179
Soby Mathewf088b342017-06-15 16:11:48 +0100180 /* Skip INVALID_IMAGE_IDs and don't check image against itself */
181 if ((bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) ||
182 (bl1_fwu_loaded_ids[i] == image_id))
Antonio Nino Diazd6a277f2017-06-01 13:40:17 +0100183 continue;
184
185 image_desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]);
186
187 /* Only check images that are loaded or being loaded. */
Soby Mathewf088b342017-06-15 16:11:48 +0100188 assert (image_desc && image_desc->state != IMAGE_STATE_RESET);
Antonio Nino Diazd6a277f2017-06-01 13:40:17 +0100189
190 info = &image_desc->image_info;
191
192 /* There cannot be overlaps with an empty image. */
193 if (info->image_size == 0)
194 continue;
195
196 image_base = info->image_base;
197 image_end = image_base + info->image_size - 1;
198 /*
199 * Overflows cannot happen. It is checked in
200 * bl1_fwu_image_copy() when the image goes from RESET to
201 * COPYING or COPIED.
202 */
203 assert (image_end > image_base);
204
205 /* Check if there are overlaps. */
206 if (!(image_end < checked_image_base ||
207 checked_image_end < image_base)) {
208 VERBOSE("Image with ID %d overlaps existing image with ID %d",
209 checked_image_desc->image_id, image_desc->image_id);
210 return -EPERM;
211 }
212 }
213
214 return 0;
215}
216
217/*******************************************************************************
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100218 * This function is responsible for copying secure images in AP Secure RAM.
219 ******************************************************************************/
220static int bl1_fwu_image_copy(unsigned int image_id,
221 uintptr_t image_src,
222 unsigned int block_size,
223 unsigned int image_size,
224 unsigned int flags)
225{
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000226 uintptr_t dest_addr;
Sandrine Bailleux953488e2016-11-14 14:56:51 +0000227 unsigned int remaining;
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100228
229 /* Get the image descriptor. */
230 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000231 if (!image_desc) {
232 WARN("BL1-FWU: Invalid image ID %u\n", image_id);
233 return -EPERM;
234 }
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100235
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000236 /*
237 * The request must originate from a non-secure caller and target a
238 * secure image. Any other scenario is invalid.
239 */
240 if (GET_SECURITY_STATE(flags) == SECURE) {
241 WARN("BL1-FWU: Copy not allowed from secure world.\n");
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100242 return -EPERM;
243 }
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000244 if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) {
245 WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
246 return -EPERM;
247 }
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100248
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000249 /* Check whether the FWU state machine is in the correct state. */
250 if ((image_desc->state != IMAGE_STATE_RESET) &&
251 (image_desc->state != IMAGE_STATE_COPYING)) {
252 WARN("BL1-FWU: Copy not allowed at this point of the FWU"
253 " process.\n");
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100254 return -EPERM;
255 }
256
Sandrine Bailleuxb39d75f2016-11-11 16:44:37 +0000257 if ((!image_src) || (!block_size) ||
258 check_uptr_overflow(image_src, block_size - 1)) {
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100259 WARN("BL1-FWU: Copy not allowed due to invalid image source"
260 " or block size\n");
261 return -ENOMEM;
262 }
263
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100264 if (image_desc->state == IMAGE_STATE_COPYING) {
Sandrine Bailleuxbbc08222016-11-14 14:58:05 +0000265 /*
266 * There must have been at least 1 copy operation for this image
267 * previously.
268 */
269 assert(image_desc->copied_size != 0);
270 /*
271 * The image size must have been recorded in the 1st copy
272 * operation.
273 */
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000274 image_size = image_desc->image_info.image_size;
Sandrine Bailleuxbbc08222016-11-14 14:58:05 +0000275 assert(image_size != 0);
276 assert(image_desc->copied_size < image_size);
277
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100278 INFO("BL1-FWU: Continuing image copy in blocks\n");
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000279 } else { /* image_desc->state == IMAGE_STATE_RESET */
280 INFO("BL1-FWU: Initial call to copy an image\n");
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100281
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000282 /*
283 * image_size is relevant only for the 1st copy request, it is
284 * then ignored for subsequent calls for this image.
285 */
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100286 if (!image_size) {
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000287 WARN("BL1-FWU: Copy not allowed due to invalid image"
288 " size\n");
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100289 return -ENOMEM;
290 }
291
Yatharth Kochar8c0177f2016-11-11 13:57:50 +0000292#if LOAD_IMAGE_V2
293 /* Check that the image size to load is within limit */
294 if (image_size > image_desc->image_info.image_max_size) {
295 WARN("BL1-FWU: Image size out of bounds\n");
296 return -ENOMEM;
297 }
298#else
Sandrine Bailleuxb39d75f2016-11-11 16:44:37 +0000299 /*
300 * Check the image will fit into the free trusted RAM after BL1
301 * load.
302 */
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000303 const meminfo_t *mem_layout = bl1_plat_sec_mem_layout();
Sandrine Bailleuxb39d75f2016-11-11 16:44:37 +0000304 if (!is_mem_free(mem_layout->free_base, mem_layout->free_size,
305 image_desc->image_info.image_base,
306 image_size)) {
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000307 WARN("BL1-FWU: Copy not allowed due to insufficient"
308 " resources.\n");
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100309 return -ENOMEM;
310 }
Yatharth Kochar8c0177f2016-11-11 13:57:50 +0000311#endif
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100312
Sandrine Bailleux5ebc21e2016-11-11 15:56:20 +0000313 /* Save the given image size. */
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100314 image_desc->image_info.image_size = image_size;
315
Antonio Nino Diazd6a277f2017-06-01 13:40:17 +0100316 /* Make sure the image doesn't overlap other images. */
317 if (bl1_fwu_image_check_overlaps(image_id)) {
318 image_desc->image_info.image_size = 0;
319 WARN("BL1-FWU: This image overlaps another one\n");
320 return -EPERM;
321 }
322
Sandrine Bailleux953488e2016-11-14 14:56:51 +0000323 /*
324 * copied_size must be explicitly initialized here because the
325 * FWU code doesn't necessarily do it when it resets the state
326 * machine.
327 */
328 image_desc->copied_size = 0;
329 }
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100330
Sandrine Bailleux953488e2016-11-14 14:56:51 +0000331 /*
332 * If the given block size is more than the total image size
333 * then clip the former to the latter.
334 */
335 remaining = image_size - image_desc->copied_size;
336 if (block_size > remaining) {
337 WARN("BL1-FWU: Block size is too big, clipping it.\n");
338 block_size = remaining;
339 }
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100340
Sandrine Bailleux953488e2016-11-14 14:56:51 +0000341 /* Make sure the source image is mapped in memory. */
342 if (bl1_plat_mem_check(image_src, block_size, flags)) {
343 WARN("BL1-FWU: Source image is not mapped.\n");
344 return -ENOMEM;
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100345 }
346
Antonio Nino Diazd6a277f2017-06-01 13:40:17 +0100347 if (bl1_fwu_add_loaded_id(image_id)) {
348 WARN("BL1-FWU: Too many images loaded at the same time.\n");
349 return -ENOMEM;
350 }
351
Soby Mathew2f38ce32018-02-08 17:45:12 +0000352 /* Allow the platform to handle pre-image load before copying */
353 if (image_desc->state == IMAGE_STATE_RESET) {
354 if (bl1_plat_handle_pre_image_load(image_id) != 0) {
355 ERROR("BL1-FWU: Failure in pre-image load of image id %d\n",
356 image_id);
357 return -EPERM;
358 }
359 }
360
Sandrine Bailleux953488e2016-11-14 14:56:51 +0000361 /* Everything looks sane. Go ahead and copy the block of data. */
362 dest_addr = image_desc->image_info.image_base + image_desc->copied_size;
363 memcpy((void *) dest_addr, (const void *) image_src, block_size);
364 flush_dcache_range(dest_addr, block_size);
365
366 image_desc->copied_size += block_size;
367 image_desc->state = (block_size == remaining) ?
368 IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
369
370 INFO("BL1-FWU: Copy operation successful.\n");
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100371 return 0;
372}
373
374/*******************************************************************************
375 * This function is responsible for authenticating Normal/Secure images.
376 ******************************************************************************/
377static int bl1_fwu_image_auth(unsigned int image_id,
378 uintptr_t image_src,
379 unsigned int image_size,
380 unsigned int flags)
381{
382 int result;
383 uintptr_t base_addr;
384 unsigned int total_size;
385
386 /* Get the image descriptor. */
387 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
388 if (!image_desc)
389 return -EPERM;
390
Yatharth Kocharf11b29a2016-02-01 11:04:46 +0000391 if (GET_SECURITY_STATE(flags) == SECURE) {
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100392 if (image_desc->state != IMAGE_STATE_RESET) {
393 WARN("BL1-FWU: Authentication from secure world "
394 "while in invalid state\n");
395 return -EPERM;
396 }
397 } else {
Yatharth Kocharf11b29a2016-02-01 11:04:46 +0000398 if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) {
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100399 if (image_desc->state != IMAGE_STATE_COPIED) {
400 WARN("BL1-FWU: Authentication of secure image "
401 "from non-secure world while not in copied state\n");
402 return -EPERM;
403 }
404 } else {
405 if (image_desc->state != IMAGE_STATE_RESET) {
406 WARN("BL1-FWU: Authentication of non-secure image "
407 "from non-secure world while in invalid state\n");
408 return -EPERM;
409 }
410 }
411 }
412
413 if (image_desc->state == IMAGE_STATE_COPIED) {
414 /*
415 * Image is in COPIED state.
416 * Use the stored address and size.
417 */
418 base_addr = image_desc->image_info.image_base;
419 total_size = image_desc->image_info.image_size;
420 } else {
Sandrine Bailleuxb39d75f2016-11-11 16:44:37 +0000421 if ((!image_src) || (!image_size) ||
422 check_uptr_overflow(image_src, image_size - 1)) {
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100423 WARN("BL1-FWU: Auth not allowed due to invalid"
424 " image source/size\n");
425 return -ENOMEM;
426 }
427
428 /*
429 * Image is in RESET state.
430 * Check the parameters and authenticate the source image in place.
431 */
Dan Handley35e5f692015-12-14 16:26:43 +0000432 if (bl1_plat_mem_check(image_src, image_size, \
433 image_desc->ep_info.h.attr)) {
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100434 WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
435 return -ENOMEM;
436 }
437
Antonio Nino Diazd6a277f2017-06-01 13:40:17 +0100438 if (bl1_fwu_add_loaded_id(image_id)) {
439 WARN("BL1-FWU: Too many images loaded at the same time.\n");
440 return -ENOMEM;
441 }
442
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100443 base_addr = image_src;
444 total_size = image_size;
445
446 /* Update the image size in the descriptor. */
447 image_desc->image_info.image_size = total_size;
448 }
449
450 /*
451 * Authenticate the image.
452 */
453 INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
454 result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
455 if (result != 0) {
456 WARN("BL1-FWU: Authentication Failed err=%d\n", result);
457
458 /*
459 * Authentication has failed.
460 * Clear the memory if the image was copied.
461 * This is to prevent an attack where this contains
462 * some malicious code that can somehow be executed later.
463 */
464 if (image_desc->state == IMAGE_STATE_COPIED) {
465 /* Clear the memory.*/
Douglas Raillard21362a92016-12-02 13:51:54 +0000466 zero_normalmem((void *)base_addr, total_size);
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100467 flush_dcache_range(base_addr, total_size);
468
469 /* Indicate that image can be copied again*/
470 image_desc->state = IMAGE_STATE_RESET;
471 }
Antonio Nino Diazd6a277f2017-06-01 13:40:17 +0100472
473 /*
474 * Even if this fails it's ok because the ID isn't in the array.
475 * The image cannot be in RESET state here, it is checked at the
476 * beginning of the function.
477 */
478 bl1_fwu_remove_loaded_id(image_id);
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100479 return -EAUTH;
480 }
481
482 /* Indicate that image is in authenticated state. */
483 image_desc->state = IMAGE_STATE_AUTHENTICATED;
484
Soby Mathew2f38ce32018-02-08 17:45:12 +0000485 /* Allow the platform to handle post-image load */
486 result = bl1_plat_handle_post_image_load(image_id);
487 if (result != 0) {
488 ERROR("BL1-FWU: Failure %d in post-image load of image id %d\n",
489 result, image_id);
490 /*
491 * Panic here as the platform handling of post-image load is
492 * not correct.
493 */
494 plat_error_handler(result);
495 }
496
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100497 /*
498 * Flush image_info to memory so that other
499 * secure world images can see changes.
500 */
501 flush_dcache_range((unsigned long)&image_desc->image_info,
502 sizeof(image_info_t));
503
504 INFO("BL1-FWU: Authentication was successful\n");
505
506 return 0;
507}
508
509/*******************************************************************************
510 * This function is responsible for executing Secure images.
511 ******************************************************************************/
512static int bl1_fwu_image_execute(unsigned int image_id,
513 void **handle,
514 unsigned int flags)
515{
516 /* Get the image descriptor. */
517 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
518
519 /*
520 * Execution is NOT allowed if:
Dan Handley8ec76522015-12-15 10:52:33 +0000521 * image_id is invalid OR
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100522 * Caller is from Secure world OR
523 * Image is Non-Secure OR
524 * Image is Non-Executable OR
525 * Image is NOT in AUTHENTICATED state.
526 */
527 if ((!image_desc) ||
Yatharth Kocharf11b29a2016-02-01 11:04:46 +0000528 (GET_SECURITY_STATE(flags) == SECURE) ||
529 (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) ||
530 (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) ||
531 (image_desc->state != IMAGE_STATE_AUTHENTICATED)) {
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100532 WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
533 return -EPERM;
534 }
535
536 INFO("BL1-FWU: Executing Secure image\n");
537
dp-armcdd03cb2017-02-15 11:07:55 +0000538#ifdef AARCH64
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100539 /* Save NS-EL1 system registers. */
540 cm_el1_sysregs_context_save(NON_SECURE);
dp-armcdd03cb2017-02-15 11:07:55 +0000541#endif
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100542
543 /* Prepare the image for execution. */
544 bl1_prepare_next_image(image_id);
545
546 /* Update the secure image id. */
547 sec_exec_image_id = image_id;
548
dp-armcdd03cb2017-02-15 11:07:55 +0000549#ifdef AARCH64
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100550 *handle = cm_get_context(SECURE);
dp-armcdd03cb2017-02-15 11:07:55 +0000551#else
552 *handle = smc_get_ctx(SECURE);
553#endif
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100554 return 0;
555}
556
557/*******************************************************************************
Dan Handley8ec76522015-12-15 10:52:33 +0000558 * This function is responsible for resuming execution in the other security
559 * world
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100560 ******************************************************************************/
Dan Handley8ec76522015-12-15 10:52:33 +0000561static register_t bl1_fwu_image_resume(register_t image_param,
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100562 void **handle,
563 unsigned int flags)
564{
565 image_desc_t *image_desc;
566 unsigned int resume_sec_state;
Yatharth Kocharf11b29a2016-02-01 11:04:46 +0000567 unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100568
Dan Handley8ec76522015-12-15 10:52:33 +0000569 /* Get the image descriptor for last executed secure image id. */
570 image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
571 if (caller_sec_state == NON_SECURE) {
572 if (!image_desc) {
573 WARN("BL1-FWU: Resume not allowed due to no available"
574 "secure image\n");
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100575 return -EPERM;
576 }
Dan Handley8ec76522015-12-15 10:52:33 +0000577 } else {
578 /* image_desc must be valid for secure world callers */
579 assert(image_desc);
580 }
581
Yatharth Kocharf11b29a2016-02-01 11:04:46 +0000582 assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
583 assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
Dan Handley8ec76522015-12-15 10:52:33 +0000584
585 if (caller_sec_state == SECURE) {
586 assert(image_desc->state == IMAGE_STATE_EXECUTED);
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100587
588 /* Update the flags. */
589 image_desc->state = IMAGE_STATE_INTERRUPTED;
590 resume_sec_state = NON_SECURE;
591 } else {
Dan Handley8ec76522015-12-15 10:52:33 +0000592 assert(image_desc->state == IMAGE_STATE_INTERRUPTED);
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100593
594 /* Update the flags. */
595 image_desc->state = IMAGE_STATE_EXECUTED;
596 resume_sec_state = SECURE;
597 }
598
dp-armcdd03cb2017-02-15 11:07:55 +0000599 INFO("BL1-FWU: Resuming %s world context\n",
600 (resume_sec_state == SECURE) ? "secure" : "normal");
601
602#ifdef AARCH64
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100603 /* Save the EL1 system registers of calling world. */
Dan Handley8ec76522015-12-15 10:52:33 +0000604 cm_el1_sysregs_context_save(caller_sec_state);
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100605
606 /* Restore the EL1 system registers of resuming world. */
607 cm_el1_sysregs_context_restore(resume_sec_state);
608
609 /* Update the next context. */
610 cm_set_next_eret_context(resume_sec_state);
611
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100612 *handle = cm_get_context(resume_sec_state);
dp-armcdd03cb2017-02-15 11:07:55 +0000613#else
614 /* Update the next context. */
615 cm_set_next_context(cm_get_context(resume_sec_state));
616
617 /* Prepare the smc context for the next BL image. */
618 smc_set_next_ctx(resume_sec_state);
619
620 *handle = smc_get_ctx(resume_sec_state);
621#endif
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100622 return image_param;
623}
624
625/*******************************************************************************
626 * This function is responsible for resuming normal world context.
627 ******************************************************************************/
628static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
629{
Dan Handley8ec76522015-12-15 10:52:33 +0000630 image_desc_t *image_desc;
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100631
Dan Handley8ec76522015-12-15 10:52:33 +0000632 /* Make sure caller is from the secure world */
Yatharth Kocharf11b29a2016-02-01 11:04:46 +0000633 if (GET_SECURITY_STATE(flags) == NON_SECURE) {
Dan Handley8ec76522015-12-15 10:52:33 +0000634 WARN("BL1-FWU: Image done not allowed from normal world\n");
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100635 return -EPERM;
636 }
637
Dan Handley8ec76522015-12-15 10:52:33 +0000638 /* Get the image descriptor for last executed secure image id */
639 image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
640
641 /* image_desc must correspond to a valid secure executing image */
642 assert(image_desc);
Yatharth Kocharf11b29a2016-02-01 11:04:46 +0000643 assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
644 assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
Dan Handley8ec76522015-12-15 10:52:33 +0000645 assert(image_desc->state == IMAGE_STATE_EXECUTED);
646
Antonio Nino Diazd6a277f2017-06-01 13:40:17 +0100647#if ENABLE_ASSERTIONS
648 int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id);
649 assert(rc == 0);
650#else
651 bl1_fwu_remove_loaded_id(sec_exec_image_id);
652#endif
653
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100654 /* Update the flags. */
655 image_desc->state = IMAGE_STATE_RESET;
656 sec_exec_image_id = INVALID_IMAGE_ID;
657
dp-armcdd03cb2017-02-15 11:07:55 +0000658 INFO("BL1-FWU: Resuming Normal world context\n");
659#ifdef AARCH64
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100660 /*
661 * Secure world is done so no need to save the context.
662 * Just restore the Non-Secure context.
663 */
664 cm_el1_sysregs_context_restore(NON_SECURE);
665
666 /* Update the next context. */
667 cm_set_next_eret_context(NON_SECURE);
668
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100669 *handle = cm_get_context(NON_SECURE);
dp-armcdd03cb2017-02-15 11:07:55 +0000670#else
671 /* Update the next context. */
672 cm_set_next_context(cm_get_context(NON_SECURE));
673
674 /* Prepare the smc context for the next BL image. */
675 smc_set_next_ctx(NON_SECURE);
676
677 *handle = smc_get_ctx(NON_SECURE);
678#endif
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100679 return 0;
680}
681
682/*******************************************************************************
683 * This function provides the opportunity for users to perform any
684 * platform specific handling after the Firmware update is done.
685 ******************************************************************************/
Dan Handley89f8f332015-12-15 14:28:24 +0000686__dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100687{
688 NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
689
690 /*
691 * Call platform done function.
692 */
Dan Handley89f8f332015-12-15 14:28:24 +0000693 bl1_plat_fwu_done(client_cookie, reserved);
Yatharth Kochar71c9a5e2015-10-10 19:06:53 +0100694 assert(0);
695}
Antonio Nino Diaz0e8e7202017-05-12 16:51:59 +0100696
697/*******************************************************************************
698 * This function resets an image to IMAGE_STATE_RESET. It fails if the image is
699 * being executed.
700 ******************************************************************************/
701static int bl1_fwu_image_reset(unsigned int image_id, unsigned int flags)
702{
703 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
704
705 if ((!image_desc) || (GET_SECURITY_STATE(flags) == SECURE)) {
706 WARN("BL1-FWU: Reset not allowed due to invalid args\n");
707 return -EPERM;
708 }
709
710 switch (image_desc->state) {
711
712 case IMAGE_STATE_RESET:
713 /* Nothing to do. */
714 break;
715
716 case IMAGE_STATE_INTERRUPTED:
717 case IMAGE_STATE_AUTHENTICATED:
718 case IMAGE_STATE_COPIED:
719 case IMAGE_STATE_COPYING:
720
721 if (bl1_fwu_remove_loaded_id(image_id)) {
722 WARN("BL1-FWU: Image reset couldn't find the image ID\n");
723 return -EPERM;
724 }
725
Soby Mathewf088b342017-06-15 16:11:48 +0100726 if (image_desc->copied_size) {
727 /* Clear the memory if the image is copied */
728 assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
729
730 zero_normalmem((void *)image_desc->image_info.image_base,
731 image_desc->copied_size);
732 flush_dcache_range(image_desc->image_info.image_base,
733 image_desc->copied_size);
734 }
Antonio Nino Diaz0e8e7202017-05-12 16:51:59 +0100735
736 /* Reset status variables */
737 image_desc->copied_size = 0;
738 image_desc->image_info.image_size = 0;
739 image_desc->state = IMAGE_STATE_RESET;
740
741 /* Clear authentication state */
742 auth_img_flags[image_id] = 0;
743
744 break;
745
746 case IMAGE_STATE_EXECUTED:
747 default:
Daniel Boulby8942a1b2018-06-22 14:16:03 +0100748 assert(0); /* Unreachable */
Jonathan Wrightd5ff96c2018-03-13 13:54:03 +0000749 break;
Antonio Nino Diaz0e8e7202017-05-12 16:51:59 +0100750 }
751
752 return 0;
753}