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