xref: /rk3399_ARM-atf/common/bl_common.c (revision a6b995fba3bd0429dcb9c059311cea40e63d6a41)
14f6ad66aSAchin Gupta /*
21b70db06SDan Handley  * Copyright (c) 2013-2015, ARM Limited and Contributors. All rights reserved.
34f6ad66aSAchin Gupta  *
44f6ad66aSAchin Gupta  * Redistribution and use in source and binary forms, with or without
54f6ad66aSAchin Gupta  * modification, are permitted provided that the following conditions are met:
64f6ad66aSAchin Gupta  *
74f6ad66aSAchin Gupta  * Redistributions of source code must retain the above copyright notice, this
84f6ad66aSAchin Gupta  * list of conditions and the following disclaimer.
94f6ad66aSAchin Gupta  *
104f6ad66aSAchin Gupta  * Redistributions in binary form must reproduce the above copyright notice,
114f6ad66aSAchin Gupta  * this list of conditions and the following disclaimer in the documentation
124f6ad66aSAchin Gupta  * and/or other materials provided with the distribution.
134f6ad66aSAchin Gupta  *
144f6ad66aSAchin Gupta  * Neither the name of ARM nor the names of its contributors may be used
154f6ad66aSAchin Gupta  * to endorse or promote products derived from this software without specific
164f6ad66aSAchin Gupta  * prior written permission.
174f6ad66aSAchin Gupta  *
184f6ad66aSAchin Gupta  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
194f6ad66aSAchin Gupta  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
204f6ad66aSAchin Gupta  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
214f6ad66aSAchin Gupta  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
224f6ad66aSAchin Gupta  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
234f6ad66aSAchin Gupta  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
244f6ad66aSAchin Gupta  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
254f6ad66aSAchin Gupta  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
264f6ad66aSAchin Gupta  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
274f6ad66aSAchin Gupta  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
284f6ad66aSAchin Gupta  * POSSIBILITY OF SUCH DAMAGE.
294f6ad66aSAchin Gupta  */
304f6ad66aSAchin Gupta 
3197043ac9SDan Handley #include <arch.h>
324f6ad66aSAchin Gupta #include <arch_helpers.h>
3397043ac9SDan Handley #include <assert.h>
341779ba6bSJuan Castillo #include <auth_mod.h>
354f6ad66aSAchin Gupta #include <bl_common.h>
3635e98e55SDan Handley #include <debug.h>
378f55dfb4SSandrine Bailleux #include <errno.h>
3897043ac9SDan Handley #include <io_storage.h>
3997043ac9SDan Handley #include <platform.h>
40fedbc049SJuan Castillo #include <string.h>
413ca9928dSSoby Mathew #include <xlat_tables.h>
424f6ad66aSAchin Gupta 
434f6ad66aSAchin Gupta unsigned long page_align(unsigned long value, unsigned dir)
444f6ad66aSAchin Gupta {
454f6ad66aSAchin Gupta 	unsigned long page_size = 1 << FOUR_KB_SHIFT;
464f6ad66aSAchin Gupta 
474f6ad66aSAchin Gupta 	/* Round up the limit to the next page boundary */
484f6ad66aSAchin Gupta 	if (value & (page_size - 1)) {
494f6ad66aSAchin Gupta 		value &= ~(page_size - 1);
504f6ad66aSAchin Gupta 		if (dir == UP)
514f6ad66aSAchin Gupta 			value += page_size;
524f6ad66aSAchin Gupta 	}
534f6ad66aSAchin Gupta 
544f6ad66aSAchin Gupta 	return value;
554f6ad66aSAchin Gupta }
564f6ad66aSAchin Gupta 
574f6ad66aSAchin Gupta static inline unsigned int is_page_aligned (unsigned long addr) {
584f6ad66aSAchin Gupta 	const unsigned long page_size = 1 << FOUR_KB_SHIFT;
594f6ad66aSAchin Gupta 
604f6ad66aSAchin Gupta 	return (addr & (page_size - 1)) == 0;
614f6ad66aSAchin Gupta }
624f6ad66aSAchin Gupta 
638f55dfb4SSandrine Bailleux /******************************************************************************
648f55dfb4SSandrine Bailleux  * Determine whether the memory region delimited by 'addr' and 'size' is free,
658f55dfb4SSandrine Bailleux  * given the extents of free memory.
668f55dfb4SSandrine Bailleux  * Return 1 if it is free, 0 otherwise.
678f55dfb4SSandrine Bailleux  *****************************************************************************/
688f55dfb4SSandrine Bailleux static int is_mem_free(uint64_t free_base, size_t free_size,
698f55dfb4SSandrine Bailleux 		       uint64_t addr, size_t size)
704f6ad66aSAchin Gupta {
718f55dfb4SSandrine Bailleux 	return (addr >= free_base) && (addr + size <= free_base + free_size);
724f6ad66aSAchin Gupta }
734f6ad66aSAchin Gupta 
748f55dfb4SSandrine Bailleux /******************************************************************************
758f55dfb4SSandrine Bailleux  * Inside a given memory region, determine whether a sub-region of memory is
768f55dfb4SSandrine Bailleux  * closer from the top or the bottom of the encompassing region. Return the
778f55dfb4SSandrine Bailleux  * size of the smallest chunk of free memory surrounding the sub-region in
788f55dfb4SSandrine Bailleux  * 'small_chunk_size'.
798f55dfb4SSandrine Bailleux  *****************************************************************************/
808f55dfb4SSandrine Bailleux static unsigned int choose_mem_pos(uint64_t mem_start, uint64_t mem_end,
818f55dfb4SSandrine Bailleux 				   uint64_t submem_start, uint64_t submem_end,
828f55dfb4SSandrine Bailleux 				   size_t *small_chunk_size)
838f55dfb4SSandrine Bailleux {
848f55dfb4SSandrine Bailleux 	size_t top_chunk_size, bottom_chunk_size;
854f6ad66aSAchin Gupta 
868f55dfb4SSandrine Bailleux 	assert(mem_start <= submem_start);
878f55dfb4SSandrine Bailleux 	assert(submem_start <= submem_end);
888f55dfb4SSandrine Bailleux 	assert(submem_end <= mem_end);
898f55dfb4SSandrine Bailleux 	assert(small_chunk_size != NULL);
908f55dfb4SSandrine Bailleux 
918f55dfb4SSandrine Bailleux 	top_chunk_size = mem_end - submem_end;
928f55dfb4SSandrine Bailleux 	bottom_chunk_size = submem_start - mem_start;
938f55dfb4SSandrine Bailleux 
948f55dfb4SSandrine Bailleux 	if (top_chunk_size < bottom_chunk_size) {
958f55dfb4SSandrine Bailleux 		*small_chunk_size = top_chunk_size;
968f55dfb4SSandrine Bailleux 		return TOP;
978f55dfb4SSandrine Bailleux 	} else {
988f55dfb4SSandrine Bailleux 		*small_chunk_size = bottom_chunk_size;
998f55dfb4SSandrine Bailleux 		return BOTTOM;
1008f55dfb4SSandrine Bailleux 	}
1018f55dfb4SSandrine Bailleux }
1028f55dfb4SSandrine Bailleux 
1038f55dfb4SSandrine Bailleux /******************************************************************************
1048f55dfb4SSandrine Bailleux  * Reserve the memory region delimited by 'addr' and 'size'. The extents of free
1058f55dfb4SSandrine Bailleux  * memory are passed in 'free_base' and 'free_size' and they will be updated to
1068f55dfb4SSandrine Bailleux  * reflect the memory usage.
1078f55dfb4SSandrine Bailleux  * The caller must ensure the memory to reserve is free.
1088f55dfb4SSandrine Bailleux  *****************************************************************************/
1098f55dfb4SSandrine Bailleux void reserve_mem(uint64_t *free_base, size_t *free_size,
1108f55dfb4SSandrine Bailleux 		 uint64_t addr, size_t size)
1118f55dfb4SSandrine Bailleux {
1128f55dfb4SSandrine Bailleux 	size_t discard_size;
1138f55dfb4SSandrine Bailleux 	size_t reserved_size;
1148f55dfb4SSandrine Bailleux 	unsigned int pos;
1158f55dfb4SSandrine Bailleux 
1168f55dfb4SSandrine Bailleux 	assert(free_base != NULL);
1178f55dfb4SSandrine Bailleux 	assert(free_size != NULL);
1188f55dfb4SSandrine Bailleux 	assert(is_mem_free(*free_base, *free_size, addr, size));
1198f55dfb4SSandrine Bailleux 
1208f55dfb4SSandrine Bailleux 	pos = choose_mem_pos(*free_base, *free_base + *free_size,
1218f55dfb4SSandrine Bailleux 			     addr, addr + size,
1228f55dfb4SSandrine Bailleux 			     &discard_size);
1238f55dfb4SSandrine Bailleux 
1248f55dfb4SSandrine Bailleux 	reserved_size = size + discard_size;
1258f55dfb4SSandrine Bailleux 	*free_size -= reserved_size;
1268f55dfb4SSandrine Bailleux 
1278f55dfb4SSandrine Bailleux 	if (pos == BOTTOM)
1288f55dfb4SSandrine Bailleux 		*free_base = addr + size;
1298f55dfb4SSandrine Bailleux 
1301b70db06SDan Handley 	VERBOSE("Reserved 0x%lx bytes (discarded 0x%lx bytes %s)\n",
1318f55dfb4SSandrine Bailleux 	     reserved_size, discard_size,
1328f55dfb4SSandrine Bailleux 	     pos == TOP ? "above" : "below");
1334f6ad66aSAchin Gupta }
1344f6ad66aSAchin Gupta 
1354f6ad66aSAchin Gupta static void dump_load_info(unsigned long image_load_addr,
1364f6ad66aSAchin Gupta 			   unsigned long image_size,
137fb037bfbSDan Handley 			   const meminfo_t *mem_layout)
1384f6ad66aSAchin Gupta {
1396ad2e461SDan Handley 	INFO("Trying to load image at address 0x%lx, size = 0x%lx\n",
1404f6ad66aSAchin Gupta 		image_load_addr, image_size);
1416ad2e461SDan Handley 	INFO("Current memory layout:\n");
1426ad2e461SDan Handley 	INFO("  total region = [0x%lx, 0x%lx]\n", mem_layout->total_base,
1434f6ad66aSAchin Gupta 			mem_layout->total_base + mem_layout->total_size);
1446ad2e461SDan Handley 	INFO("  free region = [0x%lx, 0x%lx]\n", mem_layout->free_base,
1454f6ad66aSAchin Gupta 			mem_layout->free_base + mem_layout->free_size);
1464f6ad66aSAchin Gupta }
1474f6ad66aSAchin Gupta 
148ee9ad785SRyan Harkin /* Generic function to return the size of an image */
14916948ae1SJuan Castillo unsigned long image_size(unsigned int image_id)
150ee9ad785SRyan Harkin {
151625de1d4SDan Handley 	uintptr_t dev_handle;
152625de1d4SDan Handley 	uintptr_t image_handle;
153625de1d4SDan Handley 	uintptr_t image_spec;
154ee9ad785SRyan Harkin 	size_t image_size = 0;
155e098e244SJuan Castillo 	int io_result;
156ee9ad785SRyan Harkin 
157ee9ad785SRyan Harkin 	/* Obtain a reference to the image by querying the platform layer */
15816948ae1SJuan Castillo 	io_result = plat_get_image_source(image_id, &dev_handle, &image_spec);
159e098e244SJuan Castillo 	if (io_result != 0) {
16016948ae1SJuan Castillo 		WARN("Failed to obtain reference to image id=%u (%i)\n",
16116948ae1SJuan Castillo 			image_id, io_result);
162ee9ad785SRyan Harkin 		return 0;
163ee9ad785SRyan Harkin 	}
164ee9ad785SRyan Harkin 
165ee9ad785SRyan Harkin 	/* Attempt to access the image */
166ee9ad785SRyan Harkin 	io_result = io_open(dev_handle, image_spec, &image_handle);
167e098e244SJuan Castillo 	if (io_result != 0) {
16816948ae1SJuan Castillo 		WARN("Failed to access image id=%u (%i)\n",
16916948ae1SJuan Castillo 			image_id, io_result);
170ee9ad785SRyan Harkin 		return 0;
171ee9ad785SRyan Harkin 	}
172ee9ad785SRyan Harkin 
173ee9ad785SRyan Harkin 	/* Find the size of the image */
174ee9ad785SRyan Harkin 	io_result = io_size(image_handle, &image_size);
175e098e244SJuan Castillo 	if ((io_result != 0) || (image_size == 0)) {
17616948ae1SJuan Castillo 		WARN("Failed to determine the size of the image id=%u (%i)\n",
17716948ae1SJuan Castillo 			image_id, io_result);
178ee9ad785SRyan Harkin 	}
179ee9ad785SRyan Harkin 	io_result = io_close(image_handle);
180ee9ad785SRyan Harkin 	/* Ignore improbable/unrecoverable error in 'close' */
181ee9ad785SRyan Harkin 
182ee9ad785SRyan Harkin 	/* TODO: Consider maintaining open device connection from this
183ee9ad785SRyan Harkin 	 * bootloader stage
184ee9ad785SRyan Harkin 	 */
185ee9ad785SRyan Harkin 	io_result = io_dev_close(dev_handle);
186ee9ad785SRyan Harkin 	/* Ignore improbable/unrecoverable error in 'dev_close' */
187ee9ad785SRyan Harkin 
188ee9ad785SRyan Harkin 	return image_size;
189ee9ad785SRyan Harkin }
1908f55dfb4SSandrine Bailleux 
1914f6ad66aSAchin Gupta /*******************************************************************************
192*a6b995fbSSandrine Bailleux  * Generic function to load an image at a specific address given an image ID and
193*a6b995fbSSandrine Bailleux  * extents of free memory.
194*a6b995fbSSandrine Bailleux  *
195*a6b995fbSSandrine Bailleux  * If the load is successful then the image information is updated.
196*a6b995fbSSandrine Bailleux  *
197*a6b995fbSSandrine Bailleux  * If the entry_point_info argument is not NULL then this function also updates:
198*a6b995fbSSandrine Bailleux  * - the memory layout to mark the memory as reserved;
199*a6b995fbSSandrine Bailleux  * - the entry point information.
200*a6b995fbSSandrine Bailleux  *
201*a6b995fbSSandrine Bailleux  * The caller might pass a NULL pointer for the entry point if they are not
202*a6b995fbSSandrine Bailleux  * interested in this information. This is typically the case for non-executable
203*a6b995fbSSandrine Bailleux  * images (e.g. certificates) and executable images that won't ever be executed
204*a6b995fbSSandrine Bailleux  * on the application processor (e.g. additional microcontroller firmware).
205*a6b995fbSSandrine Bailleux  *
2068f55dfb4SSandrine Bailleux  * Returns 0 on success, a negative error code otherwise.
2074f6ad66aSAchin Gupta  ******************************************************************************/
2084112bfa0SVikram Kanigiri int load_image(meminfo_t *mem_layout,
20916948ae1SJuan Castillo 	       unsigned int image_id,
2101779ba6bSJuan Castillo 	       uintptr_t image_base,
2114112bfa0SVikram Kanigiri 	       image_info_t *image_data,
2124112bfa0SVikram Kanigiri 	       entry_point_info_t *entry_point_info)
2134f6ad66aSAchin Gupta {
214625de1d4SDan Handley 	uintptr_t dev_handle;
215625de1d4SDan Handley 	uintptr_t image_handle;
216625de1d4SDan Handley 	uintptr_t image_spec;
2178f55dfb4SSandrine Bailleux 	size_t image_size;
2188f55dfb4SSandrine Bailleux 	size_t bytes_read;
21978460a05SJuan Castillo 	int io_result;
2204f6ad66aSAchin Gupta 
2219d72b4eaSJames Morrissey 	assert(mem_layout != NULL);
2228f55dfb4SSandrine Bailleux 	assert(image_data != NULL);
2234112bfa0SVikram Kanigiri 	assert(image_data->h.version >= VERSION_1);
2249d72b4eaSJames Morrissey 
2259d72b4eaSJames Morrissey 	/* Obtain a reference to the image by querying the platform layer */
22616948ae1SJuan Castillo 	io_result = plat_get_image_source(image_id, &dev_handle, &image_spec);
22778460a05SJuan Castillo 	if (io_result != 0) {
22816948ae1SJuan Castillo 		WARN("Failed to obtain reference to image id=%u (%i)\n",
22916948ae1SJuan Castillo 			image_id, io_result);
2304112bfa0SVikram Kanigiri 		return io_result;
2314f6ad66aSAchin Gupta 	}
2324f6ad66aSAchin Gupta 
2339d72b4eaSJames Morrissey 	/* Attempt to access the image */
2349d72b4eaSJames Morrissey 	io_result = io_open(dev_handle, image_spec, &image_handle);
23578460a05SJuan Castillo 	if (io_result != 0) {
23616948ae1SJuan Castillo 		WARN("Failed to access image id=%u (%i)\n",
23716948ae1SJuan Castillo 			image_id, io_result);
2384112bfa0SVikram Kanigiri 		return io_result;
2394f6ad66aSAchin Gupta 	}
2404f6ad66aSAchin Gupta 
241f0dd061aSAntonio Nino Diaz 	INFO("Loading image id=%u at address %p\n", image_id,
242f0dd061aSAntonio Nino Diaz 		(void *) image_base);
2438f55dfb4SSandrine Bailleux 
2449d72b4eaSJames Morrissey 	/* Find the size of the image */
2459d72b4eaSJames Morrissey 	io_result = io_size(image_handle, &image_size);
24678460a05SJuan Castillo 	if ((io_result != 0) || (image_size == 0)) {
24716948ae1SJuan Castillo 		WARN("Failed to determine the size of the image id=%u (%i)\n",
24816948ae1SJuan Castillo 			image_id, io_result);
2494112bfa0SVikram Kanigiri 		goto exit;
2509d72b4eaSJames Morrissey 	}
2519d72b4eaSJames Morrissey 
2528f55dfb4SSandrine Bailleux 	/* Check that the memory where the image will be loaded is free */
2538f55dfb4SSandrine Bailleux 	if (!is_mem_free(mem_layout->free_base, mem_layout->free_size,
2548f55dfb4SSandrine Bailleux 			 image_base, image_size)) {
255f0dd061aSAntonio Nino Diaz 		WARN("Failed to reserve memory: %p - %p\n", (void *) image_base,
256f0dd061aSAntonio Nino Diaz 		     (void *) (image_base + image_size));
2579d72b4eaSJames Morrissey 		dump_load_info(image_base, image_size, mem_layout);
2584112bfa0SVikram Kanigiri 		io_result = -ENOMEM;
2594112bfa0SVikram Kanigiri 		goto exit;
2604f6ad66aSAchin Gupta 	}
2614f6ad66aSAchin Gupta 
2624f6ad66aSAchin Gupta 	/* We have enough space so load the image now */
2639d72b4eaSJames Morrissey 	/* TODO: Consider whether to try to recover/retry a partially successful read */
264625de1d4SDan Handley 	io_result = io_read(image_handle, image_base, image_size, &bytes_read);
26578460a05SJuan Castillo 	if ((io_result != 0) || (bytes_read < image_size)) {
26616948ae1SJuan Castillo 		WARN("Failed to load image id=%u (%i)\n", image_id, io_result);
2674112bfa0SVikram Kanigiri 		goto exit;
2684f6ad66aSAchin Gupta 	}
2694f6ad66aSAchin Gupta 
270*a6b995fbSSandrine Bailleux 	image_data->image_base = image_base;
271*a6b995fbSSandrine Bailleux 	image_data->image_size = image_size;
272*a6b995fbSSandrine Bailleux 
2738f55dfb4SSandrine Bailleux 	/*
2748f55dfb4SSandrine Bailleux 	 * Update the memory usage info.
2758f55dfb4SSandrine Bailleux 	 * This is done after the actual loading so that it is not updated when
2768f55dfb4SSandrine Bailleux 	 * the load is unsuccessful.
277c5fb47c3SJuan Castillo 	 * If the caller does not provide an entry point, bypass the memory
278c5fb47c3SJuan Castillo 	 * reservation.
2798f55dfb4SSandrine Bailleux 	 */
280c5fb47c3SJuan Castillo 	if (entry_point_info != NULL) {
2818f55dfb4SSandrine Bailleux 		reserve_mem(&mem_layout->free_base, &mem_layout->free_size,
2828f55dfb4SSandrine Bailleux 				image_base, image_size);
283*a6b995fbSSandrine Bailleux 		entry_point_info->pc = image_base;
284c5fb47c3SJuan Castillo 	} else {
285f0dd061aSAntonio Nino Diaz 		INFO("Skip reserving memory: %p - %p\n", (void *) image_base,
286f0dd061aSAntonio Nino Diaz 		     (void *) (image_base + image_size));
287c5fb47c3SJuan Castillo 	}
2888f55dfb4SSandrine Bailleux 
2894f6ad66aSAchin Gupta 	/*
2908f55dfb4SSandrine Bailleux 	 * File has been successfully loaded.
291*a6b995fbSSandrine Bailleux 	 * Flush the image in Trusted SRAM so that the next exception level can
292*a6b995fbSSandrine Bailleux 	 * see it.
2934f6ad66aSAchin Gupta 	 */
2949d72b4eaSJames Morrissey 	flush_dcache_range(image_base, image_size);
2954f6ad66aSAchin Gupta 
296f0dd061aSAntonio Nino Diaz 	INFO("Image id=%u loaded: %p - %p\n", image_id, (void *) image_base,
297f0dd061aSAntonio Nino Diaz 	     (void *) (image_base + image_size));
2989d72b4eaSJames Morrissey 
2999d72b4eaSJames Morrissey exit:
3004112bfa0SVikram Kanigiri 	io_close(image_handle);
3019d72b4eaSJames Morrissey 	/* Ignore improbable/unrecoverable error in 'close' */
3029d72b4eaSJames Morrissey 
3039d72b4eaSJames Morrissey 	/* TODO: Consider maintaining open device connection from this bootloader stage */
3044112bfa0SVikram Kanigiri 	io_dev_close(dev_handle);
3059d72b4eaSJames Morrissey 	/* Ignore improbable/unrecoverable error in 'dev_close' */
3064f6ad66aSAchin Gupta 
3074112bfa0SVikram Kanigiri 	return io_result;
3084f6ad66aSAchin Gupta }
3091779ba6bSJuan Castillo 
3101779ba6bSJuan Castillo /*******************************************************************************
3111779ba6bSJuan Castillo  * Generic function to load and authenticate an image. The image is actually
3121779ba6bSJuan Castillo  * loaded by calling the 'load_image()' function. In addition, this function
3131779ba6bSJuan Castillo  * uses recursion to authenticate the parent images up to the root of trust.
3141779ba6bSJuan Castillo  ******************************************************************************/
3151779ba6bSJuan Castillo int load_auth_image(meminfo_t *mem_layout,
3161779ba6bSJuan Castillo 		    unsigned int image_id,
3171779ba6bSJuan Castillo 		    uintptr_t image_base,
3181779ba6bSJuan Castillo 		    image_info_t *image_data,
3191779ba6bSJuan Castillo 		    entry_point_info_t *entry_point_info)
3201779ba6bSJuan Castillo {
3211779ba6bSJuan Castillo 	int rc;
3221779ba6bSJuan Castillo 
3231779ba6bSJuan Castillo #if TRUSTED_BOARD_BOOT
3241779ba6bSJuan Castillo 	unsigned int parent_id;
3251779ba6bSJuan Castillo 
3261779ba6bSJuan Castillo 	/* Use recursion to authenticate parent images */
3271779ba6bSJuan Castillo 	rc = auth_mod_get_parent_id(image_id, &parent_id);
3281779ba6bSJuan Castillo 	if (rc == 0) {
3291779ba6bSJuan Castillo 		rc = load_auth_image(mem_layout, parent_id, image_base,
3301779ba6bSJuan Castillo 				     image_data, NULL);
33178460a05SJuan Castillo 		if (rc != 0) {
3321779ba6bSJuan Castillo 			return rc;
3331779ba6bSJuan Castillo 		}
3341779ba6bSJuan Castillo 	}
3351779ba6bSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */
3361779ba6bSJuan Castillo 
3371779ba6bSJuan Castillo 	/* Load the image */
3381779ba6bSJuan Castillo 	rc = load_image(mem_layout, image_id, image_base, image_data,
3391779ba6bSJuan Castillo 			entry_point_info);
34078460a05SJuan Castillo 	if (rc != 0) {
34178460a05SJuan Castillo 		return rc;
3421779ba6bSJuan Castillo 	}
3431779ba6bSJuan Castillo 
3441779ba6bSJuan Castillo #if TRUSTED_BOARD_BOOT
3451779ba6bSJuan Castillo 	/* Authenticate it */
3461779ba6bSJuan Castillo 	rc = auth_mod_verify_img(image_id,
3471779ba6bSJuan Castillo 				 (void *)image_data->image_base,
3481779ba6bSJuan Castillo 				 image_data->image_size);
3491779ba6bSJuan Castillo 	if (rc != 0) {
350fedbc049SJuan Castillo 		memset((void *)image_data->image_base, 0x00,
351fedbc049SJuan Castillo 		       image_data->image_size);
352fedbc049SJuan Castillo 		flush_dcache_range(image_data->image_base,
353fedbc049SJuan Castillo 				   image_data->image_size);
35478460a05SJuan Castillo 		return -EAUTH;
3551779ba6bSJuan Castillo 	}
3561779ba6bSJuan Castillo 
3571779ba6bSJuan Castillo 	/* After working with data, invalidate the data cache */
3581779ba6bSJuan Castillo 	inv_dcache_range(image_data->image_base,
3591779ba6bSJuan Castillo 			(size_t)image_data->image_size);
3601779ba6bSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */
3611779ba6bSJuan Castillo 
36278460a05SJuan Castillo 	return 0;
3631779ba6bSJuan Castillo }
36468a68c92SSandrine Bailleux 
36568a68c92SSandrine Bailleux /*******************************************************************************
36668a68c92SSandrine Bailleux  * Print the content of an entry_point_info_t structure.
36768a68c92SSandrine Bailleux  ******************************************************************************/
36868a68c92SSandrine Bailleux void print_entry_point_info(const entry_point_info_t *ep_info)
36968a68c92SSandrine Bailleux {
37068a68c92SSandrine Bailleux 	INFO("Entry point address = 0x%llx\n",
37168a68c92SSandrine Bailleux 		(unsigned long long) ep_info->pc);
37268a68c92SSandrine Bailleux 	INFO("SPSR = 0x%lx\n", (unsigned long) ep_info->spsr);
37368a68c92SSandrine Bailleux 
37468a68c92SSandrine Bailleux #define PRINT_IMAGE_ARG(n)					\
37568a68c92SSandrine Bailleux 	VERBOSE("Argument #" #n " = 0x%llx\n",			\
37668a68c92SSandrine Bailleux 		(unsigned long long) ep_info->args.arg##n)
37768a68c92SSandrine Bailleux 
37868a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(0);
37968a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(1);
38068a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(2);
38168a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(3);
38268a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(4);
38368a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(5);
38468a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(6);
38568a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(7);
38668a68c92SSandrine Bailleux #undef PRINT_IMAGE_ARG
38768a68c92SSandrine Bailleux }
388