blob: cad4de90e82cb78b7cc3279c1f70bdec5403f088 [file] [log] [blame]
Achin Gupta4f6ad662013-10-25 09:08:21 +01001/*
Douglas Raillard21362a92016-12-02 13:51:54 +00002 * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
Achin Gupta4f6ad662013-10-25 09:08:21 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta4f6ad662013-10-25 09:08:21 +01005 */
6
Dan Handley2bd4ef22014-04-09 13:14:54 +01007#include <arch.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +01008#include <arch_helpers.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +01009#include <assert.h>
Juan Castilloa08a5e72015-05-19 11:54:12 +010010#include <auth_mod.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010011#include <bl_common.h>
Dan Handley714a0d22014-04-09 13:13:04 +010012#include <debug.h>
Sandrine Bailleux467d0572014-06-24 14:02:34 +010013#include <errno.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010014#include <io_storage.h>
15#include <platform.h>
Juan Castillo97dbcf12015-08-17 10:43:27 +010016#include <string.h>
Sandrine Bailleux4ec7e2d2016-07-12 09:12:24 +010017#include <utils.h>
Antonio Nino Diaz4ef91f12017-02-20 14:22:22 +000018#include <xlat_tables_defs.h>
Achin Gupta4f6ad662013-10-25 09:08:21 +010019
Soby Mathewa0fedc42016-06-16 14:52:04 +010020uintptr_t page_align(uintptr_t value, unsigned dir)
Achin Gupta4f6ad662013-10-25 09:08:21 +010021{
Achin Gupta4f6ad662013-10-25 09:08:21 +010022 /* Round up the limit to the next page boundary */
Soby Mathewa0fedc42016-06-16 14:52:04 +010023 if (value & (PAGE_SIZE - 1)) {
24 value &= ~(PAGE_SIZE - 1);
Achin Gupta4f6ad662013-10-25 09:08:21 +010025 if (dir == UP)
Soby Mathewa0fedc42016-06-16 14:52:04 +010026 value += PAGE_SIZE;
Achin Gupta4f6ad662013-10-25 09:08:21 +010027 }
28
29 return value;
30}
31
Sandrine Bailleux467d0572014-06-24 14:02:34 +010032/******************************************************************************
33 * Determine whether the memory region delimited by 'addr' and 'size' is free,
34 * given the extents of free memory.
Sandrine Bailleux4ec7e2d2016-07-12 09:12:24 +010035 * Return 1 if it is free, 0 if it is not free or if the input values are
36 * invalid.
Sandrine Bailleux467d0572014-06-24 14:02:34 +010037 *****************************************************************************/
Sandrine Bailleuxb0e529b2016-11-08 14:27:10 +000038int is_mem_free(uintptr_t free_base, size_t free_size,
Soby Mathewa0fedc42016-06-16 14:52:04 +010039 uintptr_t addr, size_t size)
Sandrine Bailleux467d0572014-06-24 14:02:34 +010040{
Sandrine Bailleux4ec7e2d2016-07-12 09:12:24 +010041 uintptr_t free_end, requested_end;
42
43 /*
44 * Handle corner cases first.
45 *
46 * The order of the 2 tests is important, because if there's no space
47 * left (i.e. free_size == 0) but we don't ask for any memory
48 * (i.e. size == 0) then we should report that the memory is free.
49 */
50 if (size == 0)
51 return 1; /* A zero-byte region is always free */
52 if (free_size == 0)
53 return 0;
54
55 /*
56 * Check that the end addresses don't overflow.
57 * If they do, consider that this memory region is not free, as this
58 * is an invalid scenario.
59 */
60 if (check_uptr_overflow(free_base, free_size - 1))
61 return 0;
62 free_end = free_base + (free_size - 1);
63
64 if (check_uptr_overflow(addr, size - 1))
65 return 0;
66 requested_end = addr + (size - 1);
67
68 /*
69 * Finally, check that the requested memory region lies within the free
70 * region.
71 */
72 return (addr >= free_base) && (requested_end <= free_end);
Sandrine Bailleux467d0572014-06-24 14:02:34 +010073}
Achin Gupta4f6ad662013-10-25 09:08:21 +010074
Sandrine Bailleuxb0e529b2016-11-08 14:27:10 +000075#if !LOAD_IMAGE_V2
Sandrine Bailleux467d0572014-06-24 14:02:34 +010076/******************************************************************************
77 * Inside a given memory region, determine whether a sub-region of memory is
78 * closer from the top or the bottom of the encompassing region. Return the
79 * size of the smallest chunk of free memory surrounding the sub-region in
80 * 'small_chunk_size'.
81 *****************************************************************************/
Soby Mathewa0fedc42016-06-16 14:52:04 +010082static unsigned int choose_mem_pos(uintptr_t mem_start, uintptr_t mem_end,
83 uintptr_t submem_start, uintptr_t submem_end,
84 size_t *small_chunk_size)
Achin Gupta4f6ad662013-10-25 09:08:21 +010085{
Sandrine Bailleux467d0572014-06-24 14:02:34 +010086 size_t top_chunk_size, bottom_chunk_size;
87
88 assert(mem_start <= submem_start);
89 assert(submem_start <= submem_end);
90 assert(submem_end <= mem_end);
91 assert(small_chunk_size != NULL);
Achin Gupta4f6ad662013-10-25 09:08:21 +010092
Sandrine Bailleux467d0572014-06-24 14:02:34 +010093 top_chunk_size = mem_end - submem_end;
94 bottom_chunk_size = submem_start - mem_start;
Achin Gupta4f6ad662013-10-25 09:08:21 +010095
Sandrine Bailleux467d0572014-06-24 14:02:34 +010096 if (top_chunk_size < bottom_chunk_size) {
97 *small_chunk_size = top_chunk_size;
98 return TOP;
Achin Gupta4f6ad662013-10-25 09:08:21 +010099 } else {
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100100 *small_chunk_size = bottom_chunk_size;
101 return BOTTOM;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100102 }
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100103}
104
105/******************************************************************************
106 * Reserve the memory region delimited by 'addr' and 'size'. The extents of free
107 * memory are passed in 'free_base' and 'free_size' and they will be updated to
108 * reflect the memory usage.
Sandrine Bailleux4ec7e2d2016-07-12 09:12:24 +0100109 * The caller must ensure the memory to reserve is free and that the addresses
110 * and sizes passed in arguments are sane.
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100111 *****************************************************************************/
Soby Mathewa0fedc42016-06-16 14:52:04 +0100112void reserve_mem(uintptr_t *free_base, size_t *free_size,
113 uintptr_t addr, size_t size)
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100114{
115 size_t discard_size;
116 size_t reserved_size;
117 unsigned int pos;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100118
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100119 assert(free_base != NULL);
120 assert(free_size != NULL);
121 assert(is_mem_free(*free_base, *free_size, addr, size));
Achin Gupta4f6ad662013-10-25 09:08:21 +0100122
Sandrine Bailleux4ec7e2d2016-07-12 09:12:24 +0100123 if (size == 0) {
124 WARN("Nothing to allocate, requested size is zero\n");
125 return;
126 }
127
128 pos = choose_mem_pos(*free_base, *free_base + (*free_size - 1),
129 addr, addr + (size - 1),
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100130 &discard_size);
131
132 reserved_size = size + discard_size;
133 *free_size -= reserved_size;
134
135 if (pos == BOTTOM)
136 *free_base = addr + size;
137
Soby Mathewa0fedc42016-06-16 14:52:04 +0100138 VERBOSE("Reserved 0x%zx bytes (discarded 0x%zx bytes %s)\n",
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100139 reserved_size, discard_size,
140 pos == TOP ? "above" : "below");
Achin Gupta4f6ad662013-10-25 09:08:21 +0100141}
142
Soby Mathewa0fedc42016-06-16 14:52:04 +0100143static void dump_load_info(uintptr_t image_load_addr,
144 size_t image_size,
Dan Handleye2712bc2014-04-10 15:37:22 +0100145 const meminfo_t *mem_layout)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100146{
Soby Mathewa0fedc42016-06-16 14:52:04 +0100147 INFO("Trying to load image at address %p, size = 0x%zx\n",
148 (void *)image_load_addr, image_size);
Dan Handley91b624e2014-07-29 17:14:00 +0100149 INFO("Current memory layout:\n");
Sandrine Bailleux4ec7e2d2016-07-12 09:12:24 +0100150 INFO(" total region = [base = %p, size = 0x%zx]\n",
151 (void *) mem_layout->total_base, mem_layout->total_size);
152 INFO(" free region = [base = %p, size = 0x%zx]\n",
153 (void *) mem_layout->free_base, mem_layout->free_size);
Achin Gupta4f6ad662013-10-25 09:08:21 +0100154}
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100155#endif /* LOAD_IMAGE_V2 */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100156
Ryan Harkin87274c42014-02-04 11:43:57 +0000157/* Generic function to return the size of an image */
Soby Mathewa0fedc42016-06-16 14:52:04 +0100158size_t image_size(unsigned int image_id)
Ryan Harkin87274c42014-02-04 11:43:57 +0000159{
Dan Handleya4cb68e2014-04-23 13:47:06 +0100160 uintptr_t dev_handle;
161 uintptr_t image_handle;
162 uintptr_t image_spec;
Ryan Harkin87274c42014-02-04 11:43:57 +0000163 size_t image_size = 0;
Juan Castillo6e762062015-11-02 10:47:01 +0000164 int io_result;
Ryan Harkin87274c42014-02-04 11:43:57 +0000165
Ryan Harkin87274c42014-02-04 11:43:57 +0000166 /* Obtain a reference to the image by querying the platform layer */
Juan Castillo3a66aca2015-04-13 17:36:19 +0100167 io_result = plat_get_image_source(image_id, &dev_handle, &image_spec);
Juan Castillo6e762062015-11-02 10:47:01 +0000168 if (io_result != 0) {
Juan Castillo3a66aca2015-04-13 17:36:19 +0100169 WARN("Failed to obtain reference to image id=%u (%i)\n",
170 image_id, io_result);
Ryan Harkin87274c42014-02-04 11:43:57 +0000171 return 0;
172 }
173
174 /* Attempt to access the image */
175 io_result = io_open(dev_handle, image_spec, &image_handle);
Juan Castillo6e762062015-11-02 10:47:01 +0000176 if (io_result != 0) {
Juan Castillo3a66aca2015-04-13 17:36:19 +0100177 WARN("Failed to access image id=%u (%i)\n",
178 image_id, io_result);
Ryan Harkin87274c42014-02-04 11:43:57 +0000179 return 0;
180 }
181
182 /* Find the size of the image */
183 io_result = io_size(image_handle, &image_size);
Juan Castillo6e762062015-11-02 10:47:01 +0000184 if ((io_result != 0) || (image_size == 0)) {
Juan Castillo3a66aca2015-04-13 17:36:19 +0100185 WARN("Failed to determine the size of the image id=%u (%i)\n",
186 image_id, io_result);
Ryan Harkin87274c42014-02-04 11:43:57 +0000187 }
188 io_result = io_close(image_handle);
189 /* Ignore improbable/unrecoverable error in 'close' */
190
191 /* TODO: Consider maintaining open device connection from this
192 * bootloader stage
193 */
194 io_result = io_dev_close(dev_handle);
195 /* Ignore improbable/unrecoverable error in 'dev_close' */
196
197 return image_size;
198}
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100199
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100200#if LOAD_IMAGE_V2
201
202/*******************************************************************************
203 * Generic function to load an image at a specific address given
204 * an image ID and extents of free memory.
205 *
206 * If the load is successful then the image information is updated.
207 *
208 * Returns 0 on success, a negative error code otherwise.
209 ******************************************************************************/
210int load_image(unsigned int image_id, image_info_t *image_data)
211{
212 uintptr_t dev_handle;
213 uintptr_t image_handle;
214 uintptr_t image_spec;
215 uintptr_t image_base;
216 size_t image_size;
217 size_t bytes_read;
218 int io_result;
219
220 assert(image_data != NULL);
221 assert(image_data->h.version >= VERSION_2);
222
223 image_base = image_data->image_base;
224
225 /* Obtain a reference to the image by querying the platform layer */
226 io_result = plat_get_image_source(image_id, &dev_handle, &image_spec);
227 if (io_result != 0) {
228 WARN("Failed to obtain reference to image id=%u (%i)\n",
229 image_id, io_result);
230 return io_result;
231 }
232
233 /* Attempt to access the image */
234 io_result = io_open(dev_handle, image_spec, &image_handle);
235 if (io_result != 0) {
236 WARN("Failed to access image id=%u (%i)\n",
237 image_id, io_result);
238 return io_result;
239 }
240
241 INFO("Loading image id=%u at address %p\n", image_id,
242 (void *) image_base);
243
244 /* Find the size of the image */
245 io_result = io_size(image_handle, &image_size);
246 if ((io_result != 0) || (image_size == 0)) {
247 WARN("Failed to determine the size of the image id=%u (%i)\n",
248 image_id, io_result);
249 goto exit;
250 }
251
252 /* Check that the image size to load is within limit */
253 if (image_size > image_data->image_max_size) {
254 WARN("Image id=%u size out of bounds\n", image_id);
255 io_result = -EFBIG;
256 goto exit;
257 }
258
259 image_data->image_size = image_size;
260
261 /* We have enough space so load the image now */
262 /* TODO: Consider whether to try to recover/retry a partially successful read */
263 io_result = io_read(image_handle, image_base, image_size, &bytes_read);
264 if ((io_result != 0) || (bytes_read < image_size)) {
265 WARN("Failed to load image id=%u (%i)\n", image_id, io_result);
266 goto exit;
267 }
268
269#if !TRUSTED_BOARD_BOOT
270 /*
271 * File has been successfully loaded.
272 * Flush the image to main memory so that it can be executed later by
273 * any CPU, regardless of cache and MMU state.
274 * When TBB is enabled the image is flushed later, after image
275 * authentication.
276 */
277 flush_dcache_range(image_base, image_size);
278#endif /* TRUSTED_BOARD_BOOT */
279
280 INFO("Image id=%u loaded: %p - %p\n", image_id, (void *) image_base,
281 (void *) (image_base + image_size));
282
283exit:
284 io_close(image_handle);
285 /* Ignore improbable/unrecoverable error in 'close' */
286
287 /* TODO: Consider maintaining open device connection from this bootloader stage */
288 io_dev_close(dev_handle);
289 /* Ignore improbable/unrecoverable error in 'dev_close' */
290
291 return io_result;
292}
293
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000294static int load_auth_image_internal(unsigned int image_id,
295 image_info_t *image_data,
296 int is_parent_image)
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100297{
298 int rc;
299
300#if TRUSTED_BOARD_BOOT
301 unsigned int parent_id;
302
303 /* Use recursion to authenticate parent images */
304 rc = auth_mod_get_parent_id(image_id, &parent_id);
305 if (rc == 0) {
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000306 rc = load_auth_image_internal(parent_id, image_data, 1);
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100307 if (rc != 0) {
308 return rc;
309 }
310 }
311#endif /* TRUSTED_BOARD_BOOT */
312
313 /* Load the image */
314 rc = load_image(image_id, image_data);
315 if (rc != 0) {
316 return rc;
317 }
318
319#if TRUSTED_BOARD_BOOT
320 /* Authenticate it */
321 rc = auth_mod_verify_img(image_id,
322 (void *)image_data->image_base,
323 image_data->image_size);
324 if (rc != 0) {
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000325 /* Authentication error, zero memory and flush it right away. */
Douglas Raillard21362a92016-12-02 13:51:54 +0000326 zero_normalmem((void *)image_data->image_base,
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100327 image_data->image_size);
328 flush_dcache_range(image_data->image_base,
329 image_data->image_size);
330 return -EAUTH;
331 }
332
333 /*
334 * File has been successfully loaded and authenticated.
335 * Flush the image to main memory so that it can be executed later by
336 * any CPU, regardless of cache and MMU state.
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000337 * Do it only for child images, not for the parents (certificates).
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100338 */
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000339 if (!is_parent_image) {
340 flush_dcache_range(image_data->image_base,
341 image_data->image_size);
342 }
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100343#endif /* TRUSTED_BOARD_BOOT */
344
345 return 0;
346}
347
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000348/*******************************************************************************
349 * Generic function to load and authenticate an image. The image is actually
350 * loaded by calling the 'load_image()' function. Therefore, it returns the
351 * same error codes if the loading operation failed, or -EAUTH if the
352 * authentication failed. In addition, this function uses recursion to
353 * authenticate the parent images up to the root of trust.
354 ******************************************************************************/
355int load_auth_image(unsigned int image_id, image_info_t *image_data)
356{
357 return load_auth_image_internal(image_id, image_data, 0);
358}
359
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100360#else /* LOAD_IMAGE_V2 */
361
Achin Gupta4f6ad662013-10-25 09:08:21 +0100362/*******************************************************************************
Sandrine Bailleux8eaf46e2016-05-27 14:08:10 +0100363 * Generic function to load an image at a specific address given an image ID and
364 * extents of free memory.
365 *
366 * If the load is successful then the image information is updated.
367 *
368 * If the entry_point_info argument is not NULL then this function also updates:
369 * - the memory layout to mark the memory as reserved;
370 * - the entry point information.
371 *
372 * The caller might pass a NULL pointer for the entry point if they are not
373 * interested in this information. This is typically the case for non-executable
374 * images (e.g. certificates) and executable images that won't ever be executed
375 * on the application processor (e.g. additional microcontroller firmware).
376 *
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100377 * Returns 0 on success, a negative error code otherwise.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100378 ******************************************************************************/
Vikram Kanigirida567432014-04-15 18:08:08 +0100379int load_image(meminfo_t *mem_layout,
Juan Castillo3a66aca2015-04-13 17:36:19 +0100380 unsigned int image_id,
Juan Castilloa08a5e72015-05-19 11:54:12 +0100381 uintptr_t image_base,
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100382 image_info_t *image_data,
383 entry_point_info_t *entry_point_info)
Achin Gupta4f6ad662013-10-25 09:08:21 +0100384{
Dan Handleya4cb68e2014-04-23 13:47:06 +0100385 uintptr_t dev_handle;
386 uintptr_t image_handle;
387 uintptr_t image_spec;
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100388 size_t image_size;
389 size_t bytes_read;
Juan Castilloec813f52015-10-01 18:37:40 +0100390 int io_result;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100391
James Morrissey9d72b4e2014-02-10 17:04:32 +0000392 assert(mem_layout != NULL);
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100393 assert(image_data != NULL);
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100394 assert(image_data->h.version == VERSION_1);
James Morrissey9d72b4e2014-02-10 17:04:32 +0000395
396 /* Obtain a reference to the image by querying the platform layer */
Juan Castillo3a66aca2015-04-13 17:36:19 +0100397 io_result = plat_get_image_source(image_id, &dev_handle, &image_spec);
Juan Castilloec813f52015-10-01 18:37:40 +0100398 if (io_result != 0) {
Juan Castillo3a66aca2015-04-13 17:36:19 +0100399 WARN("Failed to obtain reference to image id=%u (%i)\n",
400 image_id, io_result);
Vikram Kanigirida567432014-04-15 18:08:08 +0100401 return io_result;
James Morrissey9d72b4e2014-02-10 17:04:32 +0000402 }
403
404 /* Attempt to access the image */
405 io_result = io_open(dev_handle, image_spec, &image_handle);
Juan Castilloec813f52015-10-01 18:37:40 +0100406 if (io_result != 0) {
Juan Castillo3a66aca2015-04-13 17:36:19 +0100407 WARN("Failed to access image id=%u (%i)\n",
408 image_id, io_result);
Vikram Kanigirida567432014-04-15 18:08:08 +0100409 return io_result;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100410 }
411
Antonio Nino Diazb3a0a7b2016-02-02 12:03:38 +0000412 INFO("Loading image id=%u at address %p\n", image_id,
413 (void *) image_base);
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100414
James Morrissey9d72b4e2014-02-10 17:04:32 +0000415 /* Find the size of the image */
416 io_result = io_size(image_handle, &image_size);
Juan Castilloec813f52015-10-01 18:37:40 +0100417 if ((io_result != 0) || (image_size == 0)) {
Juan Castillo3a66aca2015-04-13 17:36:19 +0100418 WARN("Failed to determine the size of the image id=%u (%i)\n",
419 image_id, io_result);
Vikram Kanigirida567432014-04-15 18:08:08 +0100420 goto exit;
James Morrissey9d72b4e2014-02-10 17:04:32 +0000421 }
422
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100423 /* Check that the memory where the image will be loaded is free */
424 if (!is_mem_free(mem_layout->free_base, mem_layout->free_size,
425 image_base, image_size)) {
Sandrine Bailleux4ec7e2d2016-07-12 09:12:24 +0100426 WARN("Failed to reserve region [base = %p, size = 0x%zx]\n",
427 (void *) image_base, image_size);
Vikram Kanigirida567432014-04-15 18:08:08 +0100428 dump_load_info(image_base, image_size, mem_layout);
429 io_result = -ENOMEM;
430 goto exit;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100431 }
432
433 /* We have enough space so load the image now */
James Morrissey9d72b4e2014-02-10 17:04:32 +0000434 /* TODO: Consider whether to try to recover/retry a partially successful read */
Dan Handleya4cb68e2014-04-23 13:47:06 +0100435 io_result = io_read(image_handle, image_base, image_size, &bytes_read);
Juan Castilloec813f52015-10-01 18:37:40 +0100436 if ((io_result != 0) || (bytes_read < image_size)) {
Juan Castillo3a66aca2015-04-13 17:36:19 +0100437 WARN("Failed to load image id=%u (%i)\n", image_id, io_result);
Vikram Kanigirida567432014-04-15 18:08:08 +0100438 goto exit;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100439 }
440
Sandrine Bailleux8eaf46e2016-05-27 14:08:10 +0100441 image_data->image_base = image_base;
442 image_data->image_size = image_size;
443
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100444 /*
445 * Update the memory usage info.
446 * This is done after the actual loading so that it is not updated when
447 * the load is unsuccessful.
Juan Castillo09a55a82015-01-19 16:51:21 +0000448 * If the caller does not provide an entry point, bypass the memory
449 * reservation.
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100450 */
Juan Castillo09a55a82015-01-19 16:51:21 +0000451 if (entry_point_info != NULL) {
452 reserve_mem(&mem_layout->free_base, &mem_layout->free_size,
453 image_base, image_size);
Sandrine Bailleux8eaf46e2016-05-27 14:08:10 +0100454 entry_point_info->pc = image_base;
Juan Castillo09a55a82015-01-19 16:51:21 +0000455 } else {
Sandrine Bailleux4ec7e2d2016-07-12 09:12:24 +0100456 INFO("Skip reserving region [base = %p, size = 0x%zx]\n",
457 (void *) image_base, image_size);
Juan Castillo09a55a82015-01-19 16:51:21 +0000458 }
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100459
Dan Handley9c96ed52016-07-28 14:38:03 +0100460#if !TRUSTED_BOARD_BOOT
Achin Gupta4f6ad662013-10-25 09:08:21 +0100461 /*
Sandrine Bailleux467d0572014-06-24 14:02:34 +0100462 * File has been successfully loaded.
Dan Handley9c96ed52016-07-28 14:38:03 +0100463 * Flush the image to main memory so that it can be executed later by
464 * any CPU, regardless of cache and MMU state.
465 * When TBB is enabled the image is flushed later, after image
466 * authentication.
Achin Gupta4f6ad662013-10-25 09:08:21 +0100467 */
James Morrissey9d72b4e2014-02-10 17:04:32 +0000468 flush_dcache_range(image_base, image_size);
Dan Handley9c96ed52016-07-28 14:38:03 +0100469#endif /* TRUSTED_BOARD_BOOT */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100470
Sandrine Bailleux4ec7e2d2016-07-12 09:12:24 +0100471 INFO("Image id=%u loaded at address %p, size = 0x%zx\n", image_id,
472 (void *) image_base, image_size);
James Morrissey9d72b4e2014-02-10 17:04:32 +0000473
474exit:
Vikram Kanigirida567432014-04-15 18:08:08 +0100475 io_close(image_handle);
James Morrissey9d72b4e2014-02-10 17:04:32 +0000476 /* Ignore improbable/unrecoverable error in 'close' */
477
478 /* TODO: Consider maintaining open device connection from this bootloader stage */
Vikram Kanigirida567432014-04-15 18:08:08 +0100479 io_dev_close(dev_handle);
James Morrissey9d72b4e2014-02-10 17:04:32 +0000480 /* Ignore improbable/unrecoverable error in 'dev_close' */
Achin Gupta4f6ad662013-10-25 09:08:21 +0100481
Vikram Kanigirida567432014-04-15 18:08:08 +0100482 return io_result;
Achin Gupta4f6ad662013-10-25 09:08:21 +0100483}
Juan Castilloa08a5e72015-05-19 11:54:12 +0100484
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000485static int load_auth_image_internal(meminfo_t *mem_layout,
486 unsigned int image_id,
487 uintptr_t image_base,
488 image_info_t *image_data,
489 entry_point_info_t *entry_point_info,
490 int is_parent_image)
Juan Castilloa08a5e72015-05-19 11:54:12 +0100491{
492 int rc;
493
494#if TRUSTED_BOARD_BOOT
495 unsigned int parent_id;
496
497 /* Use recursion to authenticate parent images */
498 rc = auth_mod_get_parent_id(image_id, &parent_id);
499 if (rc == 0) {
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000500 rc = load_auth_image_internal(mem_layout, parent_id, image_base,
501 image_data, NULL, 1);
Juan Castilloec813f52015-10-01 18:37:40 +0100502 if (rc != 0) {
Juan Castilloa08a5e72015-05-19 11:54:12 +0100503 return rc;
504 }
505 }
506#endif /* TRUSTED_BOARD_BOOT */
507
508 /* Load the image */
509 rc = load_image(mem_layout, image_id, image_base, image_data,
510 entry_point_info);
Juan Castilloec813f52015-10-01 18:37:40 +0100511 if (rc != 0) {
512 return rc;
Juan Castilloa08a5e72015-05-19 11:54:12 +0100513 }
514
515#if TRUSTED_BOARD_BOOT
516 /* Authenticate it */
517 rc = auth_mod_verify_img(image_id,
518 (void *)image_data->image_base,
519 image_data->image_size);
520 if (rc != 0) {
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000521 /* Authentication error, zero memory and flush it right away. */
Douglas Raillard21362a92016-12-02 13:51:54 +0000522 zero_normalmem((void *)image_data->image_base,
Juan Castillo97dbcf12015-08-17 10:43:27 +0100523 image_data->image_size);
524 flush_dcache_range(image_data->image_base,
525 image_data->image_size);
Juan Castilloec813f52015-10-01 18:37:40 +0100526 return -EAUTH;
Juan Castilloa08a5e72015-05-19 11:54:12 +0100527 }
Dan Handley9c96ed52016-07-28 14:38:03 +0100528 /*
529 * File has been successfully loaded and authenticated.
530 * Flush the image to main memory so that it can be executed later by
531 * any CPU, regardless of cache and MMU state.
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000532 * Do it only for child images, not for the parents (certificates).
Dan Handley9c96ed52016-07-28 14:38:03 +0100533 */
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000534 if (!is_parent_image) {
535 flush_dcache_range(image_data->image_base,
536 image_data->image_size);
537 }
Juan Castilloa08a5e72015-05-19 11:54:12 +0100538#endif /* TRUSTED_BOARD_BOOT */
539
Juan Castilloec813f52015-10-01 18:37:40 +0100540 return 0;
Juan Castilloa08a5e72015-05-19 11:54:12 +0100541}
Sandrine Bailleuxb2e224c2015-09-28 17:03:06 +0100542
Antonio Nino Diazd7db8702016-11-22 10:58:30 +0000543/*******************************************************************************
544 * Generic function to load and authenticate an image. The image is actually
545 * loaded by calling the 'load_image()' function. Therefore, it returns the
546 * same error codes if the loading operation failed, or -EAUTH if the
547 * authentication failed. In addition, this function uses recursion to
548 * authenticate the parent images up to the root of trust.
549 ******************************************************************************/
550int load_auth_image(meminfo_t *mem_layout,
551 unsigned int image_id,
552 uintptr_t image_base,
553 image_info_t *image_data,
554 entry_point_info_t *entry_point_info)
555{
556 return load_auth_image_internal(mem_layout, image_id, image_base,
557 image_data, entry_point_info, 0);
558}
559
Yatharth Kochar3345a8d2016-09-12 16:08:41 +0100560#endif /* LOAD_IMAGE_V2 */
561
Sandrine Bailleuxb2e224c2015-09-28 17:03:06 +0100562/*******************************************************************************
563 * Print the content of an entry_point_info_t structure.
564 ******************************************************************************/
565void print_entry_point_info(const entry_point_info_t *ep_info)
566{
Soby Mathewa0fedc42016-06-16 14:52:04 +0100567 INFO("Entry point address = %p\n", (void *)ep_info->pc);
568 INFO("SPSR = 0x%x\n", ep_info->spsr);
Sandrine Bailleuxb2e224c2015-09-28 17:03:06 +0100569
570#define PRINT_IMAGE_ARG(n) \
571 VERBOSE("Argument #" #n " = 0x%llx\n", \
572 (unsigned long long) ep_info->args.arg##n)
573
574 PRINT_IMAGE_ARG(0);
575 PRINT_IMAGE_ARG(1);
576 PRINT_IMAGE_ARG(2);
577 PRINT_IMAGE_ARG(3);
Soby Mathewcdf58cb2016-08-30 13:07:31 +0100578#ifndef AARCH32
Sandrine Bailleuxb2e224c2015-09-28 17:03:06 +0100579 PRINT_IMAGE_ARG(4);
580 PRINT_IMAGE_ARG(5);
581 PRINT_IMAGE_ARG(6);
582 PRINT_IMAGE_ARG(7);
Soby Mathewcdf58cb2016-08-30 13:07:31 +0100583#endif
Sandrine Bailleuxb2e224c2015-09-28 17:03:06 +0100584#undef PRINT_IMAGE_ARG
585}