blob: 167a22e71d163ee2b51cee23054e9ae9ca2f1f61 [file] [log] [blame]
Yatharth Kochar48bfb882015-10-10 19:06:53 +01001/*
Zelalem2fe75a22020-02-12 10:37:03 -06002 * Copyright (c) 2015-2020, 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
Isla Mitchell2a4b4b712017-07-11 14:54:08 +01007#include <assert.h>
Yatharth Kochar48bfb882015-10-10 19:06:53 +01008#include <errno.h>
Yatharth Kochar48bfb882015-10-10 19:06:53 +01009#include <string.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000010
11#include <platform_def.h>
12
13#include <arch_helpers.h>
14#include <bl1/bl1.h>
15#include <common/bl_common.h>
16#include <common/debug.h>
17#include <context.h>
18#include <drivers/auth/auth_mod.h>
19#include <lib/el3_runtime/context_mgmt.h>
20#include <lib/utils.h>
21#include <plat/common/platform.h>
22#include <smccc_helpers.h>
23
Yatharth Kochar48bfb882015-10-10 19:06:53 +010024#include "bl1_private.h"
25
26/*
27 * Function declarations.
28 */
29static int bl1_fwu_image_copy(unsigned int image_id,
Roberto Vargas735181b2018-02-12 12:36:17 +000030 uintptr_t image_src,
Yatharth Kochar48bfb882015-10-10 19:06:53 +010031 unsigned int block_size,
32 unsigned int image_size,
33 unsigned int flags);
34static int bl1_fwu_image_auth(unsigned int image_id,
Roberto Vargas735181b2018-02-12 12:36:17 +000035 uintptr_t image_src,
Yatharth Kochar48bfb882015-10-10 19:06:53 +010036 unsigned int image_size,
37 unsigned int flags);
38static int bl1_fwu_image_execute(unsigned int image_id,
39 void **handle,
40 unsigned int flags);
Dan Handley28955d52015-12-15 10:52:33 +000041static register_t bl1_fwu_image_resume(register_t image_param,
Yatharth Kochar48bfb882015-10-10 19:06:53 +010042 void **handle,
43 unsigned int flags);
44static int bl1_fwu_sec_image_done(void **handle,
45 unsigned int flags);
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +010046static int bl1_fwu_image_reset(unsigned int image_id,
47 unsigned int flags);
Dan Handley1f37b942015-12-15 14:28:24 +000048__dead2 static void bl1_fwu_done(void *client_cookie, void *reserved);
Yatharth Kochar48bfb882015-10-10 19:06:53 +010049
50/*
51 * This keeps track of last executed secure image id.
52 */
53static unsigned int sec_exec_image_id = INVALID_IMAGE_ID;
54
55/*******************************************************************************
56 * Top level handler for servicing FWU SMCs.
57 ******************************************************************************/
Zelalem2fe75a22020-02-12 10:37:03 -060058u_register_t bl1_fwu_smc_handler(unsigned int smc_fid,
59 u_register_t x1,
60 u_register_t x2,
61 u_register_t x3,
62 u_register_t x4,
Yatharth Kochar48bfb882015-10-10 19:06:53 +010063 void *cookie,
64 void *handle,
65 unsigned int flags)
66{
67
68 switch (smc_fid) {
69 case FWU_SMC_IMAGE_COPY:
John Powell3443a702020-03-20 14:21:05 -050070 SMC_RET1(handle, bl1_fwu_image_copy((uint32_t)x1, x2,
71 (uint32_t)x3, (uint32_t)x4, flags));
Yatharth Kochar48bfb882015-10-10 19:06:53 +010072
73 case FWU_SMC_IMAGE_AUTH:
John Powell3443a702020-03-20 14:21:05 -050074 SMC_RET1(handle, bl1_fwu_image_auth((uint32_t)x1, x2,
75 (uint32_t)x3, flags));
Yatharth Kochar48bfb882015-10-10 19:06:53 +010076
77 case FWU_SMC_IMAGE_EXECUTE:
John Powell3443a702020-03-20 14:21:05 -050078 SMC_RET1(handle, bl1_fwu_image_execute((uint32_t)x1, &handle,
79 flags));
Yatharth Kochar48bfb882015-10-10 19:06:53 +010080
81 case FWU_SMC_IMAGE_RESUME:
John Powell3443a702020-03-20 14:21:05 -050082 SMC_RET1(handle, bl1_fwu_image_resume((register_t)x1, &handle,
83 flags));
Yatharth Kochar48bfb882015-10-10 19:06:53 +010084
85 case FWU_SMC_SEC_IMAGE_DONE:
86 SMC_RET1(handle, bl1_fwu_sec_image_done(&handle, flags));
87
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +010088 case FWU_SMC_IMAGE_RESET:
John Powell3443a702020-03-20 14:21:05 -050089 SMC_RET1(handle, bl1_fwu_image_reset((uint32_t)x1, flags));
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +010090
Yatharth Kochar48bfb882015-10-10 19:06:53 +010091 case FWU_SMC_UPDATE_DONE:
Dan Handley1f37b942015-12-15 14:28:24 +000092 bl1_fwu_done((void *)x1, NULL);
Yatharth Kochar48bfb882015-10-10 19:06:53 +010093
94 default:
John Powell3443a702020-03-20 14:21:05 -050095 assert(false); /* Unreachable */
Yatharth Kochar48bfb882015-10-10 19:06:53 +010096 break;
97 }
98
Antonio Nino Diaz7a317a72017-04-04 17:08:32 +010099 SMC_RET1(handle, SMC_UNK);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100100}
101
102/*******************************************************************************
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100103 * Utility functions to keep track of the images that are loaded at any time.
104 ******************************************************************************/
105
106#ifdef PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
107#define FWU_MAX_SIMULTANEOUS_IMAGES PLAT_FWU_MAX_SIMULTANEOUS_IMAGES
108#else
109#define FWU_MAX_SIMULTANEOUS_IMAGES 10
110#endif
111
Ambroise Vincent279faa62019-02-27 16:50:10 +0000112static unsigned int bl1_fwu_loaded_ids[FWU_MAX_SIMULTANEOUS_IMAGES] = {
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100113 [0 ... FWU_MAX_SIMULTANEOUS_IMAGES-1] = INVALID_IMAGE_ID
114};
115
116/*
117 * Adds an image_id to the bl1_fwu_loaded_ids array.
118 * Returns 0 on success, 1 on error.
119 */
Ambroise Vincent279faa62019-02-27 16:50:10 +0000120static int bl1_fwu_add_loaded_id(unsigned int image_id)
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100121{
122 int i;
123
124 /* Check if the ID is already in the list */
125 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
126 if (bl1_fwu_loaded_ids[i] == image_id)
127 return 0;
128 }
129
130 /* Find an empty slot */
131 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
132 if (bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) {
133 bl1_fwu_loaded_ids[i] = image_id;
134 return 0;
135 }
136 }
137
138 return 1;
139}
140
141/*
142 * Removes an image_id from the bl1_fwu_loaded_ids array.
143 * Returns 0 on success, 1 on error.
144 */
Ambroise Vincent279faa62019-02-27 16:50:10 +0000145static int bl1_fwu_remove_loaded_id(unsigned int image_id)
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100146{
147 int i;
148
149 /* Find the ID */
150 for (i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
151 if (bl1_fwu_loaded_ids[i] == image_id) {
152 bl1_fwu_loaded_ids[i] = INVALID_IMAGE_ID;
153 return 0;
154 }
155 }
156
157 return 1;
158}
159
160/*******************************************************************************
161 * This function checks if the specified image overlaps another image already
162 * loaded. It returns 0 if there is no overlap, a negative error code otherwise.
163 ******************************************************************************/
Ambroise Vincent279faa62019-02-27 16:50:10 +0000164static int bl1_fwu_image_check_overlaps(unsigned int image_id)
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100165{
John Powell3443a702020-03-20 14:21:05 -0500166 const image_desc_t *desc, *checked_desc;
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100167 const image_info_t *info, *checked_info;
168
169 uintptr_t image_base, image_end;
170 uintptr_t checked_image_base, checked_image_end;
171
John Powell3443a702020-03-20 14:21:05 -0500172 checked_desc = bl1_plat_get_image_desc(image_id);
Manish V Badarkhe2d3b44e2025-04-01 17:27:52 +0100173
174 assert(checked_desc != NULL);
175
John Powell3443a702020-03-20 14:21:05 -0500176 checked_info = &checked_desc->image_info;
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100177
178 /* Image being checked mustn't be empty. */
179 assert(checked_info->image_size != 0);
180
181 checked_image_base = checked_info->image_base;
182 checked_image_end = checked_image_base + checked_info->image_size - 1;
Soby Mathewee05ae12017-06-15 16:11:48 +0100183 /* No need to check for overflows, it's done in bl1_fwu_image_copy(). */
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100184
185 for (int i = 0; i < FWU_MAX_SIMULTANEOUS_IMAGES; i++) {
186
Soby Mathewee05ae12017-06-15 16:11:48 +0100187 /* Skip INVALID_IMAGE_IDs and don't check image against itself */
188 if ((bl1_fwu_loaded_ids[i] == INVALID_IMAGE_ID) ||
189 (bl1_fwu_loaded_ids[i] == image_id))
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100190 continue;
191
John Powell3443a702020-03-20 14:21:05 -0500192 desc = bl1_plat_get_image_desc(bl1_fwu_loaded_ids[i]);
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100193
194 /* Only check images that are loaded or being loaded. */
John Powell3443a702020-03-20 14:21:05 -0500195 assert ((desc != NULL) && (desc->state != IMAGE_STATE_RESET));
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100196
John Powell3443a702020-03-20 14:21:05 -0500197 info = &desc->image_info;
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100198
199 /* There cannot be overlaps with an empty image. */
200 if (info->image_size == 0)
201 continue;
202
203 image_base = info->image_base;
204 image_end = image_base + info->image_size - 1;
205 /*
206 * Overflows cannot happen. It is checked in
207 * bl1_fwu_image_copy() when the image goes from RESET to
208 * COPYING or COPIED.
209 */
210 assert (image_end > image_base);
211
212 /* Check if there are overlaps. */
John Powell3443a702020-03-20 14:21:05 -0500213 if (!((image_end < checked_image_base) ||
214 (checked_image_end < image_base))) {
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100215 VERBOSE("Image with ID %d overlaps existing image with ID %d",
John Powell3443a702020-03-20 14:21:05 -0500216 checked_desc->image_id, desc->image_id);
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100217 return -EPERM;
218 }
219 }
220
221 return 0;
222}
223
224/*******************************************************************************
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100225 * This function is responsible for copying secure images in AP Secure RAM.
226 ******************************************************************************/
227static int bl1_fwu_image_copy(unsigned int image_id,
228 uintptr_t image_src,
229 unsigned int block_size,
230 unsigned int image_size,
231 unsigned int flags)
232{
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000233 uintptr_t dest_addr;
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000234 unsigned int remaining;
John Powell3443a702020-03-20 14:21:05 -0500235 image_desc_t *desc;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100236
237 /* Get the image descriptor. */
John Powell3443a702020-03-20 14:21:05 -0500238 desc = bl1_plat_get_image_desc(image_id);
239 if (desc == NULL) {
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000240 WARN("BL1-FWU: Invalid image ID %u\n", image_id);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100241 return -EPERM;
242 }
243
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000244 /*
245 * The request must originate from a non-secure caller and target a
246 * secure image. Any other scenario is invalid.
247 */
248 if (GET_SECURITY_STATE(flags) == SECURE) {
249 WARN("BL1-FWU: Copy not allowed from secure world.\n");
250 return -EPERM;
251 }
John Powell3443a702020-03-20 14:21:05 -0500252 if (GET_SECURITY_STATE(desc->ep_info.h.attr) == NON_SECURE) {
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000253 WARN("BL1-FWU: Copy not allowed for non-secure images.\n");
254 return -EPERM;
255 }
256
257 /* Check whether the FWU state machine is in the correct state. */
John Powell3443a702020-03-20 14:21:05 -0500258 if ((desc->state != IMAGE_STATE_RESET) &&
259 (desc->state != IMAGE_STATE_COPYING)) {
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000260 WARN("BL1-FWU: Copy not allowed at this point of the FWU"
261 " process.\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100262 return -EPERM;
263 }
264
John Powell3443a702020-03-20 14:21:05 -0500265 if ((image_src == 0U) || (block_size == 0U) ||
Sandrine Bailleux949a52d2016-11-11 16:44:37 +0000266 check_uptr_overflow(image_src, block_size - 1)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100267 WARN("BL1-FWU: Copy not allowed due to invalid image source"
268 " or block size\n");
269 return -ENOMEM;
270 }
271
John Powell3443a702020-03-20 14:21:05 -0500272 if (desc->state == IMAGE_STATE_COPYING) {
Sandrine Bailleux1bfb7062016-11-14 14:58:05 +0000273 /*
274 * There must have been at least 1 copy operation for this image
275 * previously.
276 */
John Powell3443a702020-03-20 14:21:05 -0500277 assert(desc->copied_size != 0U);
Sandrine Bailleux1bfb7062016-11-14 14:58:05 +0000278 /*
279 * The image size must have been recorded in the 1st copy
280 * operation.
281 */
John Powell3443a702020-03-20 14:21:05 -0500282 image_size = desc->image_info.image_size;
Sandrine Bailleux1bfb7062016-11-14 14:58:05 +0000283 assert(image_size != 0);
John Powell3443a702020-03-20 14:21:05 -0500284 assert(desc->copied_size < image_size);
Sandrine Bailleux1bfb7062016-11-14 14:58:05 +0000285
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100286 INFO("BL1-FWU: Continuing image copy in blocks\n");
John Powell3443a702020-03-20 14:21:05 -0500287 } else { /* desc->state == IMAGE_STATE_RESET */
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000288 INFO("BL1-FWU: Initial call to copy an image\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100289
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000290 /*
291 * image_size is relevant only for the 1st copy request, it is
292 * then ignored for subsequent calls for this image.
293 */
John Powell3443a702020-03-20 14:21:05 -0500294 if (image_size == 0) {
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000295 WARN("BL1-FWU: Copy not allowed due to invalid image"
296 " size\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100297 return -ENOMEM;
298 }
299
Yatharth Kochar53d703a2016-11-11 13:57:50 +0000300 /* Check that the image size to load is within limit */
John Powell3443a702020-03-20 14:21:05 -0500301 if (image_size > desc->image_info.image_max_size) {
Yatharth Kochar53d703a2016-11-11 13:57:50 +0000302 WARN("BL1-FWU: Image size out of bounds\n");
303 return -ENOMEM;
304 }
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100305
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000306 /* Save the given image size. */
John Powell3443a702020-03-20 14:21:05 -0500307 desc->image_info.image_size = image_size;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100308
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100309 /* Make sure the image doesn't overlap other images. */
John Powell3443a702020-03-20 14:21:05 -0500310 if (bl1_fwu_image_check_overlaps(image_id) != 0) {
311 desc->image_info.image_size = 0;
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100312 WARN("BL1-FWU: This image overlaps another one\n");
313 return -EPERM;
314 }
315
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000316 /*
317 * copied_size must be explicitly initialized here because the
318 * FWU code doesn't necessarily do it when it resets the state
319 * machine.
320 */
John Powell3443a702020-03-20 14:21:05 -0500321 desc->copied_size = 0;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100322 }
323
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000324 /*
325 * If the given block size is more than the total image size
326 * then clip the former to the latter.
327 */
John Powell3443a702020-03-20 14:21:05 -0500328 remaining = image_size - desc->copied_size;
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000329 if (block_size > remaining) {
330 WARN("BL1-FWU: Block size is too big, clipping it.\n");
331 block_size = remaining;
332 }
333
334 /* Make sure the source image is mapped in memory. */
John Powell3443a702020-03-20 14:21:05 -0500335 if (bl1_plat_mem_check(image_src, block_size, flags) != 0) {
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000336 WARN("BL1-FWU: Source image is not mapped.\n");
337 return -ENOMEM;
338 }
339
John Powell3443a702020-03-20 14:21:05 -0500340 if (bl1_fwu_add_loaded_id(image_id) != 0) {
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100341 WARN("BL1-FWU: Too many images loaded at the same time.\n");
342 return -ENOMEM;
343 }
344
Soby Mathew566034f2018-02-08 17:45:12 +0000345 /* Allow the platform to handle pre-image load before copying */
John Powell3443a702020-03-20 14:21:05 -0500346 if (desc->state == IMAGE_STATE_RESET) {
Soby Mathew566034f2018-02-08 17:45:12 +0000347 if (bl1_plat_handle_pre_image_load(image_id) != 0) {
348 ERROR("BL1-FWU: Failure in pre-image load of image id %d\n",
349 image_id);
350 return -EPERM;
351 }
352 }
353
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000354 /* Everything looks sane. Go ahead and copy the block of data. */
John Powell3443a702020-03-20 14:21:05 -0500355 dest_addr = desc->image_info.image_base + desc->copied_size;
356 (void)memcpy((void *) dest_addr, (const void *) image_src, block_size);
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000357 flush_dcache_range(dest_addr, block_size);
358
John Powell3443a702020-03-20 14:21:05 -0500359 desc->copied_size += block_size;
360 desc->state = (block_size == remaining) ?
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000361 IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
362
363 INFO("BL1-FWU: Copy operation successful.\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100364 return 0;
365}
366
367/*******************************************************************************
368 * This function is responsible for authenticating Normal/Secure images.
369 ******************************************************************************/
370static int bl1_fwu_image_auth(unsigned int image_id,
371 uintptr_t image_src,
372 unsigned int image_size,
373 unsigned int flags)
374{
375 int result;
376 uintptr_t base_addr;
377 unsigned int total_size;
John Powell3443a702020-03-20 14:21:05 -0500378 image_desc_t *desc;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100379
380 /* Get the image descriptor. */
John Powell3443a702020-03-20 14:21:05 -0500381 desc = bl1_plat_get_image_desc(image_id);
382 if (desc == NULL)
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100383 return -EPERM;
384
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000385 if (GET_SECURITY_STATE(flags) == SECURE) {
John Powell3443a702020-03-20 14:21:05 -0500386 if (desc->state != IMAGE_STATE_RESET) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100387 WARN("BL1-FWU: Authentication from secure world "
388 "while in invalid state\n");
389 return -EPERM;
390 }
391 } else {
John Powell3443a702020-03-20 14:21:05 -0500392 if (GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE) {
393 if (desc->state != IMAGE_STATE_COPIED) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100394 WARN("BL1-FWU: Authentication of secure image "
395 "from non-secure world while not in copied state\n");
396 return -EPERM;
397 }
398 } else {
John Powell3443a702020-03-20 14:21:05 -0500399 if (desc->state != IMAGE_STATE_RESET) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100400 WARN("BL1-FWU: Authentication of non-secure image "
401 "from non-secure world while in invalid state\n");
402 return -EPERM;
403 }
404 }
405 }
406
John Powell3443a702020-03-20 14:21:05 -0500407 if (desc->state == IMAGE_STATE_COPIED) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100408 /*
409 * Image is in COPIED state.
410 * Use the stored address and size.
411 */
John Powell3443a702020-03-20 14:21:05 -0500412 base_addr = desc->image_info.image_base;
413 total_size = desc->image_info.image_size;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100414 } else {
John Powell3443a702020-03-20 14:21:05 -0500415 if ((image_src == 0U) || (image_size == 0U) ||
Sandrine Bailleux949a52d2016-11-11 16:44:37 +0000416 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 */
Elyes Haouas9a90d722023-02-13 10:05:41 +0100426 if (bl1_plat_mem_check(image_src, image_size,
John Powell3443a702020-03-20 14:21:05 -0500427 desc->ep_info.h.attr) != 0) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100428 WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
429 return -ENOMEM;
430 }
431
John Powell3443a702020-03-20 14:21:05 -0500432 if (bl1_fwu_add_loaded_id(image_id) != 0) {
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100433 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. */
John Powell3443a702020-03-20 14:21:05 -0500441 desc->image_info.image_size = total_size;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100442 }
443
444 /*
445 * Authenticate the image.
446 */
447 INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
448 result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
449 if (result != 0) {
450 WARN("BL1-FWU: Authentication Failed err=%d\n", result);
451
452 /*
453 * Authentication has failed.
454 * Clear the memory if the image was copied.
455 * This is to prevent an attack where this contains
456 * some malicious code that can somehow be executed later.
457 */
John Powell3443a702020-03-20 14:21:05 -0500458 if (desc->state == IMAGE_STATE_COPIED) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100459 /* 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*/
John Powell3443a702020-03-20 14:21:05 -0500464 desc->state = IMAGE_STATE_RESET;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100465 }
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 */
John Powell3443a702020-03-20 14:21:05 -0500472 (void)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. */
John Powell3443a702020-03-20 14:21:05 -0500477 desc->state = IMAGE_STATE_AUTHENTICATED;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100478
Soby Mathew566034f2018-02-08 17:45:12 +0000479 /* Allow the platform to handle post-image load */
480 result = bl1_plat_handle_post_image_load(image_id);
481 if (result != 0) {
482 ERROR("BL1-FWU: Failure %d in post-image load of image id %d\n",
483 result, image_id);
484 /*
485 * Panic here as the platform handling of post-image load is
486 * not correct.
487 */
488 plat_error_handler(result);
489 }
490
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100491 /*
492 * Flush image_info to memory so that other
493 * secure world images can see changes.
494 */
John Powell3443a702020-03-20 14:21:05 -0500495 flush_dcache_range((uintptr_t)&desc->image_info,
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100496 sizeof(image_info_t));
497
498 INFO("BL1-FWU: Authentication was successful\n");
499
500 return 0;
501}
502
503/*******************************************************************************
504 * This function is responsible for executing Secure images.
505 ******************************************************************************/
506static int bl1_fwu_image_execute(unsigned int image_id,
507 void **handle,
508 unsigned int flags)
509{
510 /* Get the image descriptor. */
John Powell3443a702020-03-20 14:21:05 -0500511 image_desc_t *desc = bl1_plat_get_image_desc(image_id);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100512
513 /*
514 * Execution is NOT allowed if:
Dan Handley28955d52015-12-15 10:52:33 +0000515 * image_id is invalid OR
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100516 * Caller is from Secure world OR
517 * Image is Non-Secure OR
518 * Image is Non-Executable OR
519 * Image is NOT in AUTHENTICATED state.
520 */
John Powell3443a702020-03-20 14:21:05 -0500521 if ((desc == NULL) ||
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000522 (GET_SECURITY_STATE(flags) == SECURE) ||
John Powell3443a702020-03-20 14:21:05 -0500523 (GET_SECURITY_STATE(desc->ep_info.h.attr) == NON_SECURE) ||
524 (EP_GET_EXE(desc->ep_info.h.attr) == NON_EXECUTABLE) ||
525 (desc->state != IMAGE_STATE_AUTHENTICATED)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100526 WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
527 return -EPERM;
528 }
529
530 INFO("BL1-FWU: Executing Secure image\n");
531
Julius Werner402b3cf2019-07-09 14:02:43 -0700532#ifdef __aarch64__
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100533 /* Save NS-EL1 system registers. */
534 cm_el1_sysregs_context_save(NON_SECURE);
dp-arma4409002017-02-15 11:07:55 +0000535#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100536
537 /* Prepare the image for execution. */
538 bl1_prepare_next_image(image_id);
539
540 /* Update the secure image id. */
541 sec_exec_image_id = image_id;
542
Julius Werner402b3cf2019-07-09 14:02:43 -0700543#ifdef __aarch64__
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100544 *handle = cm_get_context(SECURE);
dp-arma4409002017-02-15 11:07:55 +0000545#else
546 *handle = smc_get_ctx(SECURE);
547#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100548 return 0;
549}
550
551/*******************************************************************************
Dan Handley28955d52015-12-15 10:52:33 +0000552 * This function is responsible for resuming execution in the other security
553 * world
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100554 ******************************************************************************/
Dan Handley28955d52015-12-15 10:52:33 +0000555static register_t bl1_fwu_image_resume(register_t image_param,
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100556 void **handle,
557 unsigned int flags)
558{
John Powell3443a702020-03-20 14:21:05 -0500559 image_desc_t *desc;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100560 unsigned int resume_sec_state;
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000561 unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100562
Dan Handley28955d52015-12-15 10:52:33 +0000563 /* Get the image descriptor for last executed secure image id. */
John Powell3443a702020-03-20 14:21:05 -0500564 desc = bl1_plat_get_image_desc(sec_exec_image_id);
Dan Handley28955d52015-12-15 10:52:33 +0000565 if (caller_sec_state == NON_SECURE) {
John Powell3443a702020-03-20 14:21:05 -0500566 if (desc == NULL) {
Dan Handley28955d52015-12-15 10:52:33 +0000567 WARN("BL1-FWU: Resume not allowed due to no available"
568 "secure image\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100569 return -EPERM;
570 }
Dan Handley28955d52015-12-15 10:52:33 +0000571 } else {
John Powell3443a702020-03-20 14:21:05 -0500572 /* desc must be valid for secure world callers */
573 assert(desc != NULL);
Dan Handley28955d52015-12-15 10:52:33 +0000574 }
575
John Powell3443a702020-03-20 14:21:05 -0500576 assert(GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE);
577 assert(EP_GET_EXE(desc->ep_info.h.attr) == EXECUTABLE);
Dan Handley28955d52015-12-15 10:52:33 +0000578
579 if (caller_sec_state == SECURE) {
John Powell3443a702020-03-20 14:21:05 -0500580 assert(desc->state == IMAGE_STATE_EXECUTED);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100581
582 /* Update the flags. */
John Powell3443a702020-03-20 14:21:05 -0500583 desc->state = IMAGE_STATE_INTERRUPTED;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100584 resume_sec_state = NON_SECURE;
585 } else {
John Powell3443a702020-03-20 14:21:05 -0500586 assert(desc->state == IMAGE_STATE_INTERRUPTED);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100587
588 /* Update the flags. */
John Powell3443a702020-03-20 14:21:05 -0500589 desc->state = IMAGE_STATE_EXECUTED;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100590 resume_sec_state = SECURE;
591 }
592
dp-arma4409002017-02-15 11:07:55 +0000593 INFO("BL1-FWU: Resuming %s world context\n",
594 (resume_sec_state == SECURE) ? "secure" : "normal");
595
Julius Werner402b3cf2019-07-09 14:02:43 -0700596#ifdef __aarch64__
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100597 /* Save the EL1 system registers of calling world. */
Dan Handley28955d52015-12-15 10:52:33 +0000598 cm_el1_sysregs_context_save(caller_sec_state);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100599
600 /* Restore the EL1 system registers of resuming world. */
601 cm_el1_sysregs_context_restore(resume_sec_state);
602
603 /* Update the next context. */
604 cm_set_next_eret_context(resume_sec_state);
605
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100606 *handle = cm_get_context(resume_sec_state);
dp-arma4409002017-02-15 11:07:55 +0000607#else
608 /* Update the next context. */
609 cm_set_next_context(cm_get_context(resume_sec_state));
610
611 /* Prepare the smc context for the next BL image. */
612 smc_set_next_ctx(resume_sec_state);
613
614 *handle = smc_get_ctx(resume_sec_state);
615#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100616 return image_param;
617}
618
619/*******************************************************************************
620 * This function is responsible for resuming normal world context.
621 ******************************************************************************/
622static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
623{
John Powell3443a702020-03-20 14:21:05 -0500624 image_desc_t *desc;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100625
Dan Handley28955d52015-12-15 10:52:33 +0000626 /* Make sure caller is from the secure world */
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000627 if (GET_SECURITY_STATE(flags) == NON_SECURE) {
Dan Handley28955d52015-12-15 10:52:33 +0000628 WARN("BL1-FWU: Image done not allowed from normal world\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100629 return -EPERM;
630 }
631
Dan Handley28955d52015-12-15 10:52:33 +0000632 /* Get the image descriptor for last executed secure image id */
John Powell3443a702020-03-20 14:21:05 -0500633 desc = bl1_plat_get_image_desc(sec_exec_image_id);
Dan Handley28955d52015-12-15 10:52:33 +0000634
John Powell3443a702020-03-20 14:21:05 -0500635 /* desc must correspond to a valid secure executing image */
636 assert(desc != NULL);
637 assert(GET_SECURITY_STATE(desc->ep_info.h.attr) == SECURE);
638 assert(EP_GET_EXE(desc->ep_info.h.attr) == EXECUTABLE);
639 assert(desc->state == IMAGE_STATE_EXECUTED);
Dan Handley28955d52015-12-15 10:52:33 +0000640
Antonio Nino Diaz128daee2017-06-01 13:40:17 +0100641#if ENABLE_ASSERTIONS
642 int rc = bl1_fwu_remove_loaded_id(sec_exec_image_id);
643 assert(rc == 0);
644#else
645 bl1_fwu_remove_loaded_id(sec_exec_image_id);
646#endif
647
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100648 /* Update the flags. */
John Powell3443a702020-03-20 14:21:05 -0500649 desc->state = IMAGE_STATE_RESET;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100650 sec_exec_image_id = INVALID_IMAGE_ID;
651
dp-arma4409002017-02-15 11:07:55 +0000652 INFO("BL1-FWU: Resuming Normal world context\n");
Julius Werner402b3cf2019-07-09 14:02:43 -0700653#ifdef __aarch64__
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100654 /*
655 * Secure world is done so no need to save the context.
656 * Just restore the Non-Secure context.
657 */
658 cm_el1_sysregs_context_restore(NON_SECURE);
659
660 /* Update the next context. */
661 cm_set_next_eret_context(NON_SECURE);
662
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100663 *handle = cm_get_context(NON_SECURE);
dp-arma4409002017-02-15 11:07:55 +0000664#else
665 /* Update the next context. */
666 cm_set_next_context(cm_get_context(NON_SECURE));
667
668 /* Prepare the smc context for the next BL image. */
669 smc_set_next_ctx(NON_SECURE);
670
671 *handle = smc_get_ctx(NON_SECURE);
672#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100673 return 0;
674}
675
676/*******************************************************************************
677 * This function provides the opportunity for users to perform any
678 * platform specific handling after the Firmware update is done.
679 ******************************************************************************/
Dan Handley1f37b942015-12-15 14:28:24 +0000680__dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100681{
682 NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
683
684 /*
685 * Call platform done function.
686 */
Dan Handley1f37b942015-12-15 14:28:24 +0000687 bl1_plat_fwu_done(client_cookie, reserved);
John Powell3443a702020-03-20 14:21:05 -0500688 assert(false);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100689}
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +0100690
691/*******************************************************************************
692 * This function resets an image to IMAGE_STATE_RESET. It fails if the image is
693 * being executed.
694 ******************************************************************************/
695static int bl1_fwu_image_reset(unsigned int image_id, unsigned int flags)
696{
John Powell3443a702020-03-20 14:21:05 -0500697 image_desc_t *desc = bl1_plat_get_image_desc(image_id);
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +0100698
John Powell3443a702020-03-20 14:21:05 -0500699 if ((desc == NULL) || (GET_SECURITY_STATE(flags) == SECURE)) {
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +0100700 WARN("BL1-FWU: Reset not allowed due to invalid args\n");
701 return -EPERM;
702 }
703
John Powell3443a702020-03-20 14:21:05 -0500704 switch (desc->state) {
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +0100705
706 case IMAGE_STATE_RESET:
707 /* Nothing to do. */
708 break;
709
710 case IMAGE_STATE_INTERRUPTED:
711 case IMAGE_STATE_AUTHENTICATED:
712 case IMAGE_STATE_COPIED:
713 case IMAGE_STATE_COPYING:
714
John Powell3443a702020-03-20 14:21:05 -0500715 if (bl1_fwu_remove_loaded_id(image_id) != 0) {
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +0100716 WARN("BL1-FWU: Image reset couldn't find the image ID\n");
717 return -EPERM;
718 }
719
John Powell3443a702020-03-20 14:21:05 -0500720 if (desc->copied_size != 0U) {
Soby Mathewee05ae12017-06-15 16:11:48 +0100721 /* Clear the memory if the image is copied */
John Powell3443a702020-03-20 14:21:05 -0500722 assert(GET_SECURITY_STATE(desc->ep_info.h.attr)
723 == SECURE);
Soby Mathewee05ae12017-06-15 16:11:48 +0100724
John Powell3443a702020-03-20 14:21:05 -0500725 zero_normalmem((void *)desc->image_info.image_base,
726 desc->copied_size);
727 flush_dcache_range(desc->image_info.image_base,
728 desc->copied_size);
Soby Mathewee05ae12017-06-15 16:11:48 +0100729 }
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +0100730
731 /* Reset status variables */
John Powell3443a702020-03-20 14:21:05 -0500732 desc->copied_size = 0;
733 desc->image_info.image_size = 0;
734 desc->state = IMAGE_STATE_RESET;
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +0100735
736 /* Clear authentication state */
737 auth_img_flags[image_id] = 0;
738
739 break;
740
741 case IMAGE_STATE_EXECUTED:
742 default:
John Powell3443a702020-03-20 14:21:05 -0500743 assert(false); /* Unreachable */
Jonathan Wrightc9662db2018-03-13 13:54:03 +0000744 break;
Antonio Nino Diaz9d6fc3c2017-05-12 16:51:59 +0100745 }
746
747 return 0;
748}