blob: 7ef184c11deb52c9358c223fe164eb31b2a57b67 [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 Bailleux1bfb7062016-11-14 14:58:05 +0000161 /*
162 * There must have been at least 1 copy operation for this image
163 * previously.
164 */
165 assert(image_desc->copied_size != 0);
166 /*
167 * The image size must have been recorded in the 1st copy
168 * operation.
169 */
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000170 image_size = image_desc->image_info.image_size;
Sandrine Bailleux1bfb7062016-11-14 14:58:05 +0000171 assert(image_size != 0);
172 assert(image_desc->copied_size < image_size);
173
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100174 INFO("BL1-FWU: Continuing image copy in blocks\n");
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000175 } else { /* image_desc->state == IMAGE_STATE_RESET */
176 INFO("BL1-FWU: Initial call to copy an image\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100177
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000178 /*
179 * image_size is relevant only for the 1st copy request, it is
180 * then ignored for subsequent calls for this image.
181 */
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100182 if (!image_size) {
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000183 WARN("BL1-FWU: Copy not allowed due to invalid image"
184 " size\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100185 return -ENOMEM;
186 }
187
Yatharth Kochar53d703a2016-11-11 13:57:50 +0000188#if LOAD_IMAGE_V2
189 /* Check that the image size to load is within limit */
190 if (image_size > image_desc->image_info.image_max_size) {
191 WARN("BL1-FWU: Image size out of bounds\n");
192 return -ENOMEM;
193 }
194#else
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100195 /* Find out how much free trusted ram remains after BL1 load */
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000196 const meminfo_t *mem_layout = bl1_plat_sec_mem_layout();
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100197 if ((image_desc->image_info.image_base < mem_layout->free_base) ||
198 (image_desc->image_info.image_base + image_size >
199 mem_layout->free_base + mem_layout->free_size)) {
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000200 WARN("BL1-FWU: Copy not allowed due to insufficient"
201 " resources.\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100202 return -ENOMEM;
203 }
Yatharth Kochar53d703a2016-11-11 13:57:50 +0000204#endif
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100205
Sandrine Bailleux9f1489e2016-11-11 15:56:20 +0000206 /* Save the given image size. */
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100207 image_desc->image_info.image_size = image_size;
208
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000209 /*
210 * copied_size must be explicitly initialized here because the
211 * FWU code doesn't necessarily do it when it resets the state
212 * machine.
213 */
214 image_desc->copied_size = 0;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100215 }
216
Sandrine Bailleuxb38a9e52016-11-14 14:56:51 +0000217 /*
218 * If the given block size is more than the total image size
219 * then clip the former to the latter.
220 */
221 remaining = image_size - image_desc->copied_size;
222 if (block_size > remaining) {
223 WARN("BL1-FWU: Block size is too big, clipping it.\n");
224 block_size = remaining;
225 }
226
227 /* Make sure the source image is mapped in memory. */
228 if (bl1_plat_mem_check(image_src, block_size, flags)) {
229 WARN("BL1-FWU: Source image is not mapped.\n");
230 return -ENOMEM;
231 }
232
233 /* Everything looks sane. Go ahead and copy the block of data. */
234 dest_addr = image_desc->image_info.image_base + image_desc->copied_size;
235 memcpy((void *) dest_addr, (const void *) image_src, block_size);
236 flush_dcache_range(dest_addr, block_size);
237
238 image_desc->copied_size += block_size;
239 image_desc->state = (block_size == remaining) ?
240 IMAGE_STATE_COPIED : IMAGE_STATE_COPYING;
241
242 INFO("BL1-FWU: Copy operation successful.\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100243 return 0;
244}
245
246/*******************************************************************************
247 * This function is responsible for authenticating Normal/Secure images.
248 ******************************************************************************/
249static int bl1_fwu_image_auth(unsigned int image_id,
250 uintptr_t image_src,
251 unsigned int image_size,
252 unsigned int flags)
253{
254 int result;
255 uintptr_t base_addr;
256 unsigned int total_size;
257
258 /* Get the image descriptor. */
259 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
260 if (!image_desc)
261 return -EPERM;
262
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000263 if (GET_SECURITY_STATE(flags) == SECURE) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100264 if (image_desc->state != IMAGE_STATE_RESET) {
265 WARN("BL1-FWU: Authentication from secure world "
266 "while in invalid state\n");
267 return -EPERM;
268 }
269 } else {
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000270 if (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100271 if (image_desc->state != IMAGE_STATE_COPIED) {
272 WARN("BL1-FWU: Authentication of secure image "
273 "from non-secure world while not in copied state\n");
274 return -EPERM;
275 }
276 } else {
277 if (image_desc->state != IMAGE_STATE_RESET) {
278 WARN("BL1-FWU: Authentication of non-secure image "
279 "from non-secure world while in invalid state\n");
280 return -EPERM;
281 }
282 }
283 }
284
285 if (image_desc->state == IMAGE_STATE_COPIED) {
286 /*
287 * Image is in COPIED state.
288 * Use the stored address and size.
289 */
290 base_addr = image_desc->image_info.image_base;
291 total_size = image_desc->image_info.image_size;
292 } else {
293 if ((!image_src) || (!image_size)) {
294 WARN("BL1-FWU: Auth not allowed due to invalid"
295 " image source/size\n");
296 return -ENOMEM;
297 }
298
299 /*
300 * Image is in RESET state.
301 * Check the parameters and authenticate the source image in place.
302 */
Dan Handley03131c82015-12-14 16:26:43 +0000303 if (bl1_plat_mem_check(image_src, image_size, \
304 image_desc->ep_info.h.attr)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100305 WARN("BL1-FWU: Authentication arguments source/size not mapped\n");
306 return -ENOMEM;
307 }
308
309 base_addr = image_src;
310 total_size = image_size;
311
312 /* Update the image size in the descriptor. */
313 image_desc->image_info.image_size = total_size;
314 }
315
316 /*
317 * Authenticate the image.
318 */
319 INFO("BL1-FWU: Authenticating image_id:%d\n", image_id);
320 result = auth_mod_verify_img(image_id, (void *)base_addr, total_size);
321 if (result != 0) {
322 WARN("BL1-FWU: Authentication Failed err=%d\n", result);
323
324 /*
325 * Authentication has failed.
326 * Clear the memory if the image was copied.
327 * This is to prevent an attack where this contains
328 * some malicious code that can somehow be executed later.
329 */
330 if (image_desc->state == IMAGE_STATE_COPIED) {
331 /* Clear the memory.*/
332 memset((void *)base_addr, 0, total_size);
333 flush_dcache_range(base_addr, total_size);
334
335 /* Indicate that image can be copied again*/
336 image_desc->state = IMAGE_STATE_RESET;
337 }
338 return -EAUTH;
339 }
340
341 /* Indicate that image is in authenticated state. */
342 image_desc->state = IMAGE_STATE_AUTHENTICATED;
343
344 /*
345 * Flush image_info to memory so that other
346 * secure world images can see changes.
347 */
348 flush_dcache_range((unsigned long)&image_desc->image_info,
349 sizeof(image_info_t));
350
351 INFO("BL1-FWU: Authentication was successful\n");
352
353 return 0;
354}
355
356/*******************************************************************************
357 * This function is responsible for executing Secure images.
358 ******************************************************************************/
359static int bl1_fwu_image_execute(unsigned int image_id,
360 void **handle,
361 unsigned int flags)
362{
363 /* Get the image descriptor. */
364 image_desc_t *image_desc = bl1_plat_get_image_desc(image_id);
365
366 /*
367 * Execution is NOT allowed if:
Dan Handley28955d52015-12-15 10:52:33 +0000368 * image_id is invalid OR
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100369 * Caller is from Secure world OR
370 * Image is Non-Secure OR
371 * Image is Non-Executable OR
372 * Image is NOT in AUTHENTICATED state.
373 */
374 if ((!image_desc) ||
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000375 (GET_SECURITY_STATE(flags) == SECURE) ||
376 (GET_SECURITY_STATE(image_desc->ep_info.h.attr) == NON_SECURE) ||
377 (EP_GET_EXE(image_desc->ep_info.h.attr) == NON_EXECUTABLE) ||
378 (image_desc->state != IMAGE_STATE_AUTHENTICATED)) {
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100379 WARN("BL1-FWU: Execution not allowed due to invalid state/args\n");
380 return -EPERM;
381 }
382
383 INFO("BL1-FWU: Executing Secure image\n");
384
385 /* Save NS-EL1 system registers. */
386 cm_el1_sysregs_context_save(NON_SECURE);
387
388 /* Prepare the image for execution. */
389 bl1_prepare_next_image(image_id);
390
391 /* Update the secure image id. */
392 sec_exec_image_id = image_id;
393
394 *handle = cm_get_context(SECURE);
395 return 0;
396}
397
398/*******************************************************************************
Dan Handley28955d52015-12-15 10:52:33 +0000399 * This function is responsible for resuming execution in the other security
400 * world
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100401 ******************************************************************************/
Dan Handley28955d52015-12-15 10:52:33 +0000402static register_t bl1_fwu_image_resume(register_t image_param,
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100403 void **handle,
404 unsigned int flags)
405{
406 image_desc_t *image_desc;
407 unsigned int resume_sec_state;
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000408 unsigned int caller_sec_state = GET_SECURITY_STATE(flags);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100409
Dan Handley28955d52015-12-15 10:52:33 +0000410 /* Get the image descriptor for last executed secure image id. */
411 image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
412 if (caller_sec_state == NON_SECURE) {
413 if (!image_desc) {
414 WARN("BL1-FWU: Resume not allowed due to no available"
415 "secure image\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100416 return -EPERM;
417 }
Dan Handley28955d52015-12-15 10:52:33 +0000418 } else {
419 /* image_desc must be valid for secure world callers */
420 assert(image_desc);
421 }
422
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000423 assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
424 assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
Dan Handley28955d52015-12-15 10:52:33 +0000425
426 if (caller_sec_state == SECURE) {
427 assert(image_desc->state == IMAGE_STATE_EXECUTED);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100428
429 /* Update the flags. */
430 image_desc->state = IMAGE_STATE_INTERRUPTED;
431 resume_sec_state = NON_SECURE;
432 } else {
Dan Handley28955d52015-12-15 10:52:33 +0000433 assert(image_desc->state == IMAGE_STATE_INTERRUPTED);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100434
435 /* Update the flags. */
436 image_desc->state = IMAGE_STATE_EXECUTED;
437 resume_sec_state = SECURE;
438 }
439
440 /* Save the EL1 system registers of calling world. */
Dan Handley28955d52015-12-15 10:52:33 +0000441 cm_el1_sysregs_context_save(caller_sec_state);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100442
443 /* Restore the EL1 system registers of resuming world. */
444 cm_el1_sysregs_context_restore(resume_sec_state);
445
446 /* Update the next context. */
447 cm_set_next_eret_context(resume_sec_state);
448
449 INFO("BL1-FWU: Resuming %s world context\n",
Dan Handley28955d52015-12-15 10:52:33 +0000450 (resume_sec_state == SECURE) ? "secure" : "normal");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100451
452 *handle = cm_get_context(resume_sec_state);
453 return image_param;
454}
455
456/*******************************************************************************
457 * This function is responsible for resuming normal world context.
458 ******************************************************************************/
459static int bl1_fwu_sec_image_done(void **handle, unsigned int flags)
460{
Dan Handley28955d52015-12-15 10:52:33 +0000461 image_desc_t *image_desc;
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100462
Dan Handley28955d52015-12-15 10:52:33 +0000463 /* Make sure caller is from the secure world */
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000464 if (GET_SECURITY_STATE(flags) == NON_SECURE) {
Dan Handley28955d52015-12-15 10:52:33 +0000465 WARN("BL1-FWU: Image done not allowed from normal world\n");
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100466 return -EPERM;
467 }
468
Dan Handley28955d52015-12-15 10:52:33 +0000469 /* Get the image descriptor for last executed secure image id */
470 image_desc = bl1_plat_get_image_desc(sec_exec_image_id);
471
472 /* image_desc must correspond to a valid secure executing image */
473 assert(image_desc);
Yatharth Kochar843ddee2016-02-01 11:04:46 +0000474 assert(GET_SECURITY_STATE(image_desc->ep_info.h.attr) == SECURE);
475 assert(EP_GET_EXE(image_desc->ep_info.h.attr) == EXECUTABLE);
Dan Handley28955d52015-12-15 10:52:33 +0000476 assert(image_desc->state == IMAGE_STATE_EXECUTED);
477
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100478 /* Update the flags. */
479 image_desc->state = IMAGE_STATE_RESET;
480 sec_exec_image_id = INVALID_IMAGE_ID;
481
482 /*
483 * Secure world is done so no need to save the context.
484 * Just restore the Non-Secure context.
485 */
486 cm_el1_sysregs_context_restore(NON_SECURE);
487
488 /* Update the next context. */
489 cm_set_next_eret_context(NON_SECURE);
490
491 INFO("BL1-FWU: Resuming Normal world context\n");
492
493 *handle = cm_get_context(NON_SECURE);
494 return 0;
495}
496
497/*******************************************************************************
498 * This function provides the opportunity for users to perform any
499 * platform specific handling after the Firmware update is done.
500 ******************************************************************************/
Dan Handley1f37b942015-12-15 14:28:24 +0000501__dead2 static void bl1_fwu_done(void *client_cookie, void *reserved)
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100502{
503 NOTICE("BL1-FWU: *******FWU Process Completed*******\n");
504
505 /*
506 * Call platform done function.
507 */
Dan Handley1f37b942015-12-15 14:28:24 +0000508 bl1_plat_fwu_done(client_cookie, reserved);
Yatharth Kochar48bfb882015-10-10 19:06:53 +0100509 assert(0);
510}