xref: /rk3399_ARM-atf/common/bl_common.c (revision 7260022636e3b0d3ef641cbda135d98f9a7df177)
14f6ad66aSAchin Gupta /*
24c0d0390SSoby Mathew  * Copyright (c) 2013-2016, 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>
417b6d330cSSandrine Bailleux #include <utils.h>
423ca9928dSSoby Mathew #include <xlat_tables.h>
434f6ad66aSAchin Gupta 
444c0d0390SSoby Mathew uintptr_t page_align(uintptr_t value, unsigned dir)
454f6ad66aSAchin Gupta {
464f6ad66aSAchin Gupta 	/* Round up the limit to the next page boundary */
474c0d0390SSoby Mathew 	if (value & (PAGE_SIZE - 1)) {
484c0d0390SSoby Mathew 		value &= ~(PAGE_SIZE - 1);
494f6ad66aSAchin Gupta 		if (dir == UP)
504c0d0390SSoby Mathew 			value += PAGE_SIZE;
514f6ad66aSAchin Gupta 	}
524f6ad66aSAchin Gupta 
534f6ad66aSAchin Gupta 	return value;
544f6ad66aSAchin Gupta }
554f6ad66aSAchin Gupta 
56*72600226SYatharth Kochar #if !LOAD_IMAGE_V2
578f55dfb4SSandrine Bailleux /******************************************************************************
588f55dfb4SSandrine Bailleux  * Determine whether the memory region delimited by 'addr' and 'size' is free,
598f55dfb4SSandrine Bailleux  * given the extents of free memory.
607b6d330cSSandrine Bailleux  * Return 1 if it is free, 0 if it is not free or if the input values are
617b6d330cSSandrine Bailleux  * invalid.
628f55dfb4SSandrine Bailleux  *****************************************************************************/
634c0d0390SSoby Mathew static int is_mem_free(uintptr_t free_base, size_t free_size,
644c0d0390SSoby Mathew 		uintptr_t addr, size_t size)
654f6ad66aSAchin Gupta {
667b6d330cSSandrine Bailleux 	uintptr_t free_end, requested_end;
677b6d330cSSandrine Bailleux 
687b6d330cSSandrine Bailleux 	/*
697b6d330cSSandrine Bailleux 	 * Handle corner cases first.
707b6d330cSSandrine Bailleux 	 *
717b6d330cSSandrine Bailleux 	 * The order of the 2 tests is important, because if there's no space
727b6d330cSSandrine Bailleux 	 * left (i.e. free_size == 0) but we don't ask for any memory
737b6d330cSSandrine Bailleux 	 * (i.e. size == 0) then we should report that the memory is free.
747b6d330cSSandrine Bailleux 	 */
757b6d330cSSandrine Bailleux 	if (size == 0)
767b6d330cSSandrine Bailleux 		return 1;	/* A zero-byte region is always free */
777b6d330cSSandrine Bailleux 	if (free_size == 0)
787b6d330cSSandrine Bailleux 		return 0;
797b6d330cSSandrine Bailleux 
807b6d330cSSandrine Bailleux 	/*
817b6d330cSSandrine Bailleux 	 * Check that the end addresses don't overflow.
827b6d330cSSandrine Bailleux 	 * If they do, consider that this memory region is not free, as this
837b6d330cSSandrine Bailleux 	 * is an invalid scenario.
847b6d330cSSandrine Bailleux 	 */
857b6d330cSSandrine Bailleux 	if (check_uptr_overflow(free_base, free_size - 1))
867b6d330cSSandrine Bailleux 		return 0;
877b6d330cSSandrine Bailleux 	free_end = free_base + (free_size - 1);
887b6d330cSSandrine Bailleux 
897b6d330cSSandrine Bailleux 	if (check_uptr_overflow(addr, size - 1))
907b6d330cSSandrine Bailleux 		return 0;
917b6d330cSSandrine Bailleux 	requested_end = addr + (size - 1);
927b6d330cSSandrine Bailleux 
937b6d330cSSandrine Bailleux 	/*
947b6d330cSSandrine Bailleux 	 * Finally, check that the requested memory region lies within the free
957b6d330cSSandrine Bailleux 	 * region.
967b6d330cSSandrine Bailleux 	 */
977b6d330cSSandrine Bailleux 	return (addr >= free_base) && (requested_end <= free_end);
984f6ad66aSAchin Gupta }
994f6ad66aSAchin Gupta 
1008f55dfb4SSandrine Bailleux /******************************************************************************
1018f55dfb4SSandrine Bailleux  * Inside a given memory region, determine whether a sub-region of memory is
1028f55dfb4SSandrine Bailleux  * closer from the top or the bottom of the encompassing region. Return the
1038f55dfb4SSandrine Bailleux  * size of the smallest chunk of free memory surrounding the sub-region in
1048f55dfb4SSandrine Bailleux  * 'small_chunk_size'.
1058f55dfb4SSandrine Bailleux  *****************************************************************************/
1064c0d0390SSoby Mathew static unsigned int choose_mem_pos(uintptr_t mem_start, uintptr_t mem_end,
1074c0d0390SSoby Mathew 				  uintptr_t submem_start, uintptr_t submem_end,
1088f55dfb4SSandrine Bailleux 				  size_t *small_chunk_size)
1098f55dfb4SSandrine Bailleux {
1108f55dfb4SSandrine Bailleux 	size_t top_chunk_size, bottom_chunk_size;
1114f6ad66aSAchin Gupta 
1128f55dfb4SSandrine Bailleux 	assert(mem_start <= submem_start);
1138f55dfb4SSandrine Bailleux 	assert(submem_start <= submem_end);
1148f55dfb4SSandrine Bailleux 	assert(submem_end <= mem_end);
1158f55dfb4SSandrine Bailleux 	assert(small_chunk_size != NULL);
1168f55dfb4SSandrine Bailleux 
1178f55dfb4SSandrine Bailleux 	top_chunk_size = mem_end - submem_end;
1188f55dfb4SSandrine Bailleux 	bottom_chunk_size = submem_start - mem_start;
1198f55dfb4SSandrine Bailleux 
1208f55dfb4SSandrine Bailleux 	if (top_chunk_size < bottom_chunk_size) {
1218f55dfb4SSandrine Bailleux 		*small_chunk_size = top_chunk_size;
1228f55dfb4SSandrine Bailleux 		return TOP;
1238f55dfb4SSandrine Bailleux 	} else {
1248f55dfb4SSandrine Bailleux 		*small_chunk_size = bottom_chunk_size;
1258f55dfb4SSandrine Bailleux 		return BOTTOM;
1268f55dfb4SSandrine Bailleux 	}
1278f55dfb4SSandrine Bailleux }
1288f55dfb4SSandrine Bailleux 
1298f55dfb4SSandrine Bailleux /******************************************************************************
1308f55dfb4SSandrine Bailleux  * Reserve the memory region delimited by 'addr' and 'size'. The extents of free
1318f55dfb4SSandrine Bailleux  * memory are passed in 'free_base' and 'free_size' and they will be updated to
1328f55dfb4SSandrine Bailleux  * reflect the memory usage.
1337b6d330cSSandrine Bailleux  * The caller must ensure the memory to reserve is free and that the addresses
1347b6d330cSSandrine Bailleux  * and sizes passed in arguments are sane.
1358f55dfb4SSandrine Bailleux  *****************************************************************************/
1364c0d0390SSoby Mathew void reserve_mem(uintptr_t *free_base, size_t *free_size,
1374c0d0390SSoby Mathew 		 uintptr_t addr, size_t size)
1388f55dfb4SSandrine Bailleux {
1398f55dfb4SSandrine Bailleux 	size_t discard_size;
1408f55dfb4SSandrine Bailleux 	size_t reserved_size;
1418f55dfb4SSandrine Bailleux 	unsigned int pos;
1428f55dfb4SSandrine Bailleux 
1438f55dfb4SSandrine Bailleux 	assert(free_base != NULL);
1448f55dfb4SSandrine Bailleux 	assert(free_size != NULL);
1458f55dfb4SSandrine Bailleux 	assert(is_mem_free(*free_base, *free_size, addr, size));
1468f55dfb4SSandrine Bailleux 
1477b6d330cSSandrine Bailleux 	if (size == 0) {
1487b6d330cSSandrine Bailleux 		WARN("Nothing to allocate, requested size is zero\n");
1497b6d330cSSandrine Bailleux 		return;
1507b6d330cSSandrine Bailleux 	}
1517b6d330cSSandrine Bailleux 
1527b6d330cSSandrine Bailleux 	pos = choose_mem_pos(*free_base, *free_base + (*free_size - 1),
1537b6d330cSSandrine Bailleux 			     addr, addr + (size - 1),
1548f55dfb4SSandrine Bailleux 			     &discard_size);
1558f55dfb4SSandrine Bailleux 
1568f55dfb4SSandrine Bailleux 	reserved_size = size + discard_size;
1578f55dfb4SSandrine Bailleux 	*free_size -= reserved_size;
1588f55dfb4SSandrine Bailleux 
1598f55dfb4SSandrine Bailleux 	if (pos == BOTTOM)
1608f55dfb4SSandrine Bailleux 		*free_base = addr + size;
1618f55dfb4SSandrine Bailleux 
1624c0d0390SSoby Mathew 	VERBOSE("Reserved 0x%zx bytes (discarded 0x%zx bytes %s)\n",
1638f55dfb4SSandrine Bailleux 	     reserved_size, discard_size,
1648f55dfb4SSandrine Bailleux 	     pos == TOP ? "above" : "below");
1654f6ad66aSAchin Gupta }
1664f6ad66aSAchin Gupta 
1674c0d0390SSoby Mathew static void dump_load_info(uintptr_t image_load_addr,
1684c0d0390SSoby Mathew 			   size_t image_size,
169fb037bfbSDan Handley 			   const meminfo_t *mem_layout)
1704f6ad66aSAchin Gupta {
1714c0d0390SSoby Mathew 	INFO("Trying to load image at address %p, size = 0x%zx\n",
1724c0d0390SSoby Mathew 		(void *)image_load_addr, image_size);
1736ad2e461SDan Handley 	INFO("Current memory layout:\n");
1747b6d330cSSandrine Bailleux 	INFO("  total region = [base = %p, size = 0x%zx]\n",
1757b6d330cSSandrine Bailleux 		(void *) mem_layout->total_base, mem_layout->total_size);
1767b6d330cSSandrine Bailleux 	INFO("  free region = [base = %p, size = 0x%zx]\n",
1777b6d330cSSandrine Bailleux 		(void *) mem_layout->free_base, mem_layout->free_size);
1784f6ad66aSAchin Gupta }
179*72600226SYatharth Kochar #endif /* LOAD_IMAGE_V2 */
1804f6ad66aSAchin Gupta 
181ee9ad785SRyan Harkin /* Generic function to return the size of an image */
1824c0d0390SSoby Mathew size_t image_size(unsigned int image_id)
183ee9ad785SRyan Harkin {
184625de1d4SDan Handley 	uintptr_t dev_handle;
185625de1d4SDan Handley 	uintptr_t image_handle;
186625de1d4SDan Handley 	uintptr_t image_spec;
187ee9ad785SRyan Harkin 	size_t image_size = 0;
188e098e244SJuan Castillo 	int io_result;
189ee9ad785SRyan Harkin 
190ee9ad785SRyan Harkin 	/* Obtain a reference to the image by querying the platform layer */
19116948ae1SJuan Castillo 	io_result = plat_get_image_source(image_id, &dev_handle, &image_spec);
192e098e244SJuan Castillo 	if (io_result != 0) {
19316948ae1SJuan Castillo 		WARN("Failed to obtain reference to image id=%u (%i)\n",
19416948ae1SJuan Castillo 			image_id, io_result);
195ee9ad785SRyan Harkin 		return 0;
196ee9ad785SRyan Harkin 	}
197ee9ad785SRyan Harkin 
198ee9ad785SRyan Harkin 	/* Attempt to access the image */
199ee9ad785SRyan Harkin 	io_result = io_open(dev_handle, image_spec, &image_handle);
200e098e244SJuan Castillo 	if (io_result != 0) {
20116948ae1SJuan Castillo 		WARN("Failed to access image id=%u (%i)\n",
20216948ae1SJuan Castillo 			image_id, io_result);
203ee9ad785SRyan Harkin 		return 0;
204ee9ad785SRyan Harkin 	}
205ee9ad785SRyan Harkin 
206ee9ad785SRyan Harkin 	/* Find the size of the image */
207ee9ad785SRyan Harkin 	io_result = io_size(image_handle, &image_size);
208e098e244SJuan Castillo 	if ((io_result != 0) || (image_size == 0)) {
20916948ae1SJuan Castillo 		WARN("Failed to determine the size of the image id=%u (%i)\n",
21016948ae1SJuan Castillo 			image_id, io_result);
211ee9ad785SRyan Harkin 	}
212ee9ad785SRyan Harkin 	io_result = io_close(image_handle);
213ee9ad785SRyan Harkin 	/* Ignore improbable/unrecoverable error in 'close' */
214ee9ad785SRyan Harkin 
215ee9ad785SRyan Harkin 	/* TODO: Consider maintaining open device connection from this
216ee9ad785SRyan Harkin 	 * bootloader stage
217ee9ad785SRyan Harkin 	 */
218ee9ad785SRyan Harkin 	io_result = io_dev_close(dev_handle);
219ee9ad785SRyan Harkin 	/* Ignore improbable/unrecoverable error in 'dev_close' */
220ee9ad785SRyan Harkin 
221ee9ad785SRyan Harkin 	return image_size;
222ee9ad785SRyan Harkin }
2238f55dfb4SSandrine Bailleux 
224*72600226SYatharth Kochar #if LOAD_IMAGE_V2
225*72600226SYatharth Kochar 
226*72600226SYatharth Kochar /*******************************************************************************
227*72600226SYatharth Kochar  * Generic function to load an image at a specific address given
228*72600226SYatharth Kochar  * an image ID and extents of free memory.
229*72600226SYatharth Kochar  *
230*72600226SYatharth Kochar  * If the load is successful then the image information is updated.
231*72600226SYatharth Kochar  *
232*72600226SYatharth Kochar  * Returns 0 on success, a negative error code otherwise.
233*72600226SYatharth Kochar  ******************************************************************************/
234*72600226SYatharth Kochar int load_image(unsigned int image_id, image_info_t *image_data)
235*72600226SYatharth Kochar {
236*72600226SYatharth Kochar 	uintptr_t dev_handle;
237*72600226SYatharth Kochar 	uintptr_t image_handle;
238*72600226SYatharth Kochar 	uintptr_t image_spec;
239*72600226SYatharth Kochar 	uintptr_t image_base;
240*72600226SYatharth Kochar 	size_t image_size;
241*72600226SYatharth Kochar 	size_t bytes_read;
242*72600226SYatharth Kochar 	int io_result;
243*72600226SYatharth Kochar 
244*72600226SYatharth Kochar 	assert(image_data != NULL);
245*72600226SYatharth Kochar 	assert(image_data->h.version >= VERSION_2);
246*72600226SYatharth Kochar 
247*72600226SYatharth Kochar 	image_base = image_data->image_base;
248*72600226SYatharth Kochar 
249*72600226SYatharth Kochar 	/* Obtain a reference to the image by querying the platform layer */
250*72600226SYatharth Kochar 	io_result = plat_get_image_source(image_id, &dev_handle, &image_spec);
251*72600226SYatharth Kochar 	if (io_result != 0) {
252*72600226SYatharth Kochar 		WARN("Failed to obtain reference to image id=%u (%i)\n",
253*72600226SYatharth Kochar 			image_id, io_result);
254*72600226SYatharth Kochar 		return io_result;
255*72600226SYatharth Kochar 	}
256*72600226SYatharth Kochar 
257*72600226SYatharth Kochar 	/* Attempt to access the image */
258*72600226SYatharth Kochar 	io_result = io_open(dev_handle, image_spec, &image_handle);
259*72600226SYatharth Kochar 	if (io_result != 0) {
260*72600226SYatharth Kochar 		WARN("Failed to access image id=%u (%i)\n",
261*72600226SYatharth Kochar 			image_id, io_result);
262*72600226SYatharth Kochar 		return io_result;
263*72600226SYatharth Kochar 	}
264*72600226SYatharth Kochar 
265*72600226SYatharth Kochar 	INFO("Loading image id=%u at address %p\n", image_id,
266*72600226SYatharth Kochar 		(void *) image_base);
267*72600226SYatharth Kochar 
268*72600226SYatharth Kochar 	/* Find the size of the image */
269*72600226SYatharth Kochar 	io_result = io_size(image_handle, &image_size);
270*72600226SYatharth Kochar 	if ((io_result != 0) || (image_size == 0)) {
271*72600226SYatharth Kochar 		WARN("Failed to determine the size of the image id=%u (%i)\n",
272*72600226SYatharth Kochar 			image_id, io_result);
273*72600226SYatharth Kochar 		goto exit;
274*72600226SYatharth Kochar 	}
275*72600226SYatharth Kochar 
276*72600226SYatharth Kochar 	/* Check that the image size to load is within limit */
277*72600226SYatharth Kochar 	if (image_size > image_data->image_max_size) {
278*72600226SYatharth Kochar 		WARN("Image id=%u size out of bounds\n", image_id);
279*72600226SYatharth Kochar 		io_result = -EFBIG;
280*72600226SYatharth Kochar 		goto exit;
281*72600226SYatharth Kochar 	}
282*72600226SYatharth Kochar 
283*72600226SYatharth Kochar 	image_data->image_size = image_size;
284*72600226SYatharth Kochar 
285*72600226SYatharth Kochar 	/* We have enough space so load the image now */
286*72600226SYatharth Kochar 	/* TODO: Consider whether to try to recover/retry a partially successful read */
287*72600226SYatharth Kochar 	io_result = io_read(image_handle, image_base, image_size, &bytes_read);
288*72600226SYatharth Kochar 	if ((io_result != 0) || (bytes_read < image_size)) {
289*72600226SYatharth Kochar 		WARN("Failed to load image id=%u (%i)\n", image_id, io_result);
290*72600226SYatharth Kochar 		goto exit;
291*72600226SYatharth Kochar 	}
292*72600226SYatharth Kochar 
293*72600226SYatharth Kochar #if !TRUSTED_BOARD_BOOT
294*72600226SYatharth Kochar 	/*
295*72600226SYatharth Kochar 	 * File has been successfully loaded.
296*72600226SYatharth Kochar 	 * Flush the image to main memory so that it can be executed later by
297*72600226SYatharth Kochar 	 * any CPU, regardless of cache and MMU state.
298*72600226SYatharth Kochar 	 * When TBB is enabled the image is flushed later, after image
299*72600226SYatharth Kochar 	 * authentication.
300*72600226SYatharth Kochar 	 */
301*72600226SYatharth Kochar 	flush_dcache_range(image_base, image_size);
302*72600226SYatharth Kochar #endif /* TRUSTED_BOARD_BOOT */
303*72600226SYatharth Kochar 
304*72600226SYatharth Kochar 	INFO("Image id=%u loaded: %p - %p\n", image_id, (void *) image_base,
305*72600226SYatharth Kochar 	     (void *) (image_base + image_size));
306*72600226SYatharth Kochar 
307*72600226SYatharth Kochar exit:
308*72600226SYatharth Kochar 	io_close(image_handle);
309*72600226SYatharth Kochar 	/* Ignore improbable/unrecoverable error in 'close' */
310*72600226SYatharth Kochar 
311*72600226SYatharth Kochar 	/* TODO: Consider maintaining open device connection from this bootloader stage */
312*72600226SYatharth Kochar 	io_dev_close(dev_handle);
313*72600226SYatharth Kochar 	/* Ignore improbable/unrecoverable error in 'dev_close' */
314*72600226SYatharth Kochar 
315*72600226SYatharth Kochar 	return io_result;
316*72600226SYatharth Kochar }
317*72600226SYatharth Kochar 
318*72600226SYatharth Kochar /*******************************************************************************
319*72600226SYatharth Kochar  * Generic function to load and authenticate an image. The image is actually
320*72600226SYatharth Kochar  * loaded by calling the 'load_image()' function. Therefore, it returns the
321*72600226SYatharth Kochar  * same error codes if the loading operation failed, or -EAUTH if the
322*72600226SYatharth Kochar  * authentication failed. In addition, this function uses recursion to
323*72600226SYatharth Kochar  * authenticate the parent images up to the root of trust.
324*72600226SYatharth Kochar  ******************************************************************************/
325*72600226SYatharth Kochar int load_auth_image(unsigned int image_id, image_info_t *image_data)
326*72600226SYatharth Kochar {
327*72600226SYatharth Kochar 	int rc;
328*72600226SYatharth Kochar 
329*72600226SYatharth Kochar #if TRUSTED_BOARD_BOOT
330*72600226SYatharth Kochar 	unsigned int parent_id;
331*72600226SYatharth Kochar 
332*72600226SYatharth Kochar 	/* Use recursion to authenticate parent images */
333*72600226SYatharth Kochar 	rc = auth_mod_get_parent_id(image_id, &parent_id);
334*72600226SYatharth Kochar 	if (rc == 0) {
335*72600226SYatharth Kochar 		rc = load_auth_image(parent_id, image_data);
336*72600226SYatharth Kochar 		if (rc != 0) {
337*72600226SYatharth Kochar 			return rc;
338*72600226SYatharth Kochar 		}
339*72600226SYatharth Kochar 	}
340*72600226SYatharth Kochar #endif /* TRUSTED_BOARD_BOOT */
341*72600226SYatharth Kochar 
342*72600226SYatharth Kochar 	/* Load the image */
343*72600226SYatharth Kochar 	rc = load_image(image_id, image_data);
344*72600226SYatharth Kochar 	if (rc != 0) {
345*72600226SYatharth Kochar 		return rc;
346*72600226SYatharth Kochar 	}
347*72600226SYatharth Kochar 
348*72600226SYatharth Kochar #if TRUSTED_BOARD_BOOT
349*72600226SYatharth Kochar 	/* Authenticate it */
350*72600226SYatharth Kochar 	rc = auth_mod_verify_img(image_id,
351*72600226SYatharth Kochar 				 (void *)image_data->image_base,
352*72600226SYatharth Kochar 				 image_data->image_size);
353*72600226SYatharth Kochar 	if (rc != 0) {
354*72600226SYatharth Kochar 		memset((void *)image_data->image_base, 0x00,
355*72600226SYatharth Kochar 		       image_data->image_size);
356*72600226SYatharth Kochar 		flush_dcache_range(image_data->image_base,
357*72600226SYatharth Kochar 				   image_data->image_size);
358*72600226SYatharth Kochar 		return -EAUTH;
359*72600226SYatharth Kochar 	}
360*72600226SYatharth Kochar 
361*72600226SYatharth Kochar 	/*
362*72600226SYatharth Kochar 	 * File has been successfully loaded and authenticated.
363*72600226SYatharth Kochar 	 * Flush the image to main memory so that it can be executed later by
364*72600226SYatharth Kochar 	 * any CPU, regardless of cache and MMU state.
365*72600226SYatharth Kochar 	 */
366*72600226SYatharth Kochar 	flush_dcache_range(image_data->image_base, image_data->image_size);
367*72600226SYatharth Kochar #endif /* TRUSTED_BOARD_BOOT */
368*72600226SYatharth Kochar 
369*72600226SYatharth Kochar 	return 0;
370*72600226SYatharth Kochar }
371*72600226SYatharth Kochar 
372*72600226SYatharth Kochar #else /* LOAD_IMAGE_V2 */
373*72600226SYatharth Kochar 
3744f6ad66aSAchin Gupta /*******************************************************************************
375a6b995fbSSandrine Bailleux  * Generic function to load an image at a specific address given an image ID and
376a6b995fbSSandrine Bailleux  * extents of free memory.
377a6b995fbSSandrine Bailleux  *
378a6b995fbSSandrine Bailleux  * If the load is successful then the image information is updated.
379a6b995fbSSandrine Bailleux  *
380a6b995fbSSandrine Bailleux  * If the entry_point_info argument is not NULL then this function also updates:
381a6b995fbSSandrine Bailleux  * - the memory layout to mark the memory as reserved;
382a6b995fbSSandrine Bailleux  * - the entry point information.
383a6b995fbSSandrine Bailleux  *
384a6b995fbSSandrine Bailleux  * The caller might pass a NULL pointer for the entry point if they are not
385a6b995fbSSandrine Bailleux  * interested in this information. This is typically the case for non-executable
386a6b995fbSSandrine Bailleux  * images (e.g. certificates) and executable images that won't ever be executed
387a6b995fbSSandrine Bailleux  * on the application processor (e.g. additional microcontroller firmware).
388a6b995fbSSandrine Bailleux  *
3898f55dfb4SSandrine Bailleux  * Returns 0 on success, a negative error code otherwise.
3904f6ad66aSAchin Gupta  ******************************************************************************/
3914112bfa0SVikram Kanigiri int load_image(meminfo_t *mem_layout,
39216948ae1SJuan Castillo 	       unsigned int image_id,
3931779ba6bSJuan Castillo 	       uintptr_t image_base,
3944112bfa0SVikram Kanigiri 	       image_info_t *image_data,
3954112bfa0SVikram Kanigiri 	       entry_point_info_t *entry_point_info)
3964f6ad66aSAchin Gupta {
397625de1d4SDan Handley 	uintptr_t dev_handle;
398625de1d4SDan Handley 	uintptr_t image_handle;
399625de1d4SDan Handley 	uintptr_t image_spec;
4008f55dfb4SSandrine Bailleux 	size_t image_size;
4018f55dfb4SSandrine Bailleux 	size_t bytes_read;
40278460a05SJuan Castillo 	int io_result;
4034f6ad66aSAchin Gupta 
4049d72b4eaSJames Morrissey 	assert(mem_layout != NULL);
4058f55dfb4SSandrine Bailleux 	assert(image_data != NULL);
406*72600226SYatharth Kochar 	assert(image_data->h.version == VERSION_1);
4079d72b4eaSJames Morrissey 
4089d72b4eaSJames Morrissey 	/* Obtain a reference to the image by querying the platform layer */
40916948ae1SJuan Castillo 	io_result = plat_get_image_source(image_id, &dev_handle, &image_spec);
41078460a05SJuan Castillo 	if (io_result != 0) {
41116948ae1SJuan Castillo 		WARN("Failed to obtain reference to image id=%u (%i)\n",
41216948ae1SJuan Castillo 			image_id, io_result);
4134112bfa0SVikram Kanigiri 		return io_result;
4144f6ad66aSAchin Gupta 	}
4154f6ad66aSAchin Gupta 
4169d72b4eaSJames Morrissey 	/* Attempt to access the image */
4179d72b4eaSJames Morrissey 	io_result = io_open(dev_handle, image_spec, &image_handle);
41878460a05SJuan Castillo 	if (io_result != 0) {
41916948ae1SJuan Castillo 		WARN("Failed to access image id=%u (%i)\n",
42016948ae1SJuan Castillo 			image_id, io_result);
4214112bfa0SVikram Kanigiri 		return io_result;
4224f6ad66aSAchin Gupta 	}
4234f6ad66aSAchin Gupta 
424f0dd061aSAntonio Nino Diaz 	INFO("Loading image id=%u at address %p\n", image_id,
425f0dd061aSAntonio Nino Diaz 		(void *) image_base);
4268f55dfb4SSandrine Bailleux 
4279d72b4eaSJames Morrissey 	/* Find the size of the image */
4289d72b4eaSJames Morrissey 	io_result = io_size(image_handle, &image_size);
42978460a05SJuan Castillo 	if ((io_result != 0) || (image_size == 0)) {
43016948ae1SJuan Castillo 		WARN("Failed to determine the size of the image id=%u (%i)\n",
43116948ae1SJuan Castillo 			image_id, io_result);
4324112bfa0SVikram Kanigiri 		goto exit;
4339d72b4eaSJames Morrissey 	}
4349d72b4eaSJames Morrissey 
4358f55dfb4SSandrine Bailleux 	/* Check that the memory where the image will be loaded is free */
4368f55dfb4SSandrine Bailleux 	if (!is_mem_free(mem_layout->free_base, mem_layout->free_size,
4378f55dfb4SSandrine Bailleux 			 image_base, image_size)) {
4387b6d330cSSandrine Bailleux 		WARN("Failed to reserve region [base = %p, size = 0x%zx]\n",
4397b6d330cSSandrine Bailleux 		     (void *) image_base, image_size);
4409d72b4eaSJames Morrissey 		dump_load_info(image_base, image_size, mem_layout);
4414112bfa0SVikram Kanigiri 		io_result = -ENOMEM;
4424112bfa0SVikram Kanigiri 		goto exit;
4434f6ad66aSAchin Gupta 	}
4444f6ad66aSAchin Gupta 
4454f6ad66aSAchin Gupta 	/* We have enough space so load the image now */
4469d72b4eaSJames Morrissey 	/* TODO: Consider whether to try to recover/retry a partially successful read */
447625de1d4SDan Handley 	io_result = io_read(image_handle, image_base, image_size, &bytes_read);
44878460a05SJuan Castillo 	if ((io_result != 0) || (bytes_read < image_size)) {
44916948ae1SJuan Castillo 		WARN("Failed to load image id=%u (%i)\n", image_id, io_result);
4504112bfa0SVikram Kanigiri 		goto exit;
4514f6ad66aSAchin Gupta 	}
4524f6ad66aSAchin Gupta 
453a6b995fbSSandrine Bailleux 	image_data->image_base = image_base;
454a6b995fbSSandrine Bailleux 	image_data->image_size = image_size;
455a6b995fbSSandrine Bailleux 
4568f55dfb4SSandrine Bailleux 	/*
4578f55dfb4SSandrine Bailleux 	 * Update the memory usage info.
4588f55dfb4SSandrine Bailleux 	 * This is done after the actual loading so that it is not updated when
4598f55dfb4SSandrine Bailleux 	 * the load is unsuccessful.
460c5fb47c3SJuan Castillo 	 * If the caller does not provide an entry point, bypass the memory
461c5fb47c3SJuan Castillo 	 * reservation.
4628f55dfb4SSandrine Bailleux 	 */
463c5fb47c3SJuan Castillo 	if (entry_point_info != NULL) {
4648f55dfb4SSandrine Bailleux 		reserve_mem(&mem_layout->free_base, &mem_layout->free_size,
4658f55dfb4SSandrine Bailleux 				image_base, image_size);
466a6b995fbSSandrine Bailleux 		entry_point_info->pc = image_base;
467c5fb47c3SJuan Castillo 	} else {
4687b6d330cSSandrine Bailleux 		INFO("Skip reserving region [base = %p, size = 0x%zx]\n",
4697b6d330cSSandrine Bailleux 		     (void *) image_base, image_size);
470c5fb47c3SJuan Castillo 	}
4718f55dfb4SSandrine Bailleux 
472ad4494dcSDan Handley #if !TRUSTED_BOARD_BOOT
4734f6ad66aSAchin Gupta 	/*
4748f55dfb4SSandrine Bailleux 	 * File has been successfully loaded.
475ad4494dcSDan Handley 	 * Flush the image to main memory so that it can be executed later by
476ad4494dcSDan Handley 	 * any CPU, regardless of cache and MMU state.
477ad4494dcSDan Handley 	 * When TBB is enabled the image is flushed later, after image
478ad4494dcSDan Handley 	 * authentication.
4794f6ad66aSAchin Gupta 	 */
4809d72b4eaSJames Morrissey 	flush_dcache_range(image_base, image_size);
481ad4494dcSDan Handley #endif /* TRUSTED_BOARD_BOOT */
4824f6ad66aSAchin Gupta 
4837b6d330cSSandrine Bailleux 	INFO("Image id=%u loaded at address %p, size = 0x%zx\n", image_id,
4847b6d330cSSandrine Bailleux 		(void *) image_base, image_size);
4859d72b4eaSJames Morrissey 
4869d72b4eaSJames Morrissey exit:
4874112bfa0SVikram Kanigiri 	io_close(image_handle);
4889d72b4eaSJames Morrissey 	/* Ignore improbable/unrecoverable error in 'close' */
4899d72b4eaSJames Morrissey 
4909d72b4eaSJames Morrissey 	/* TODO: Consider maintaining open device connection from this bootloader stage */
4914112bfa0SVikram Kanigiri 	io_dev_close(dev_handle);
4929d72b4eaSJames Morrissey 	/* Ignore improbable/unrecoverable error in 'dev_close' */
4934f6ad66aSAchin Gupta 
4944112bfa0SVikram Kanigiri 	return io_result;
4954f6ad66aSAchin Gupta }
4961779ba6bSJuan Castillo 
4971779ba6bSJuan Castillo /*******************************************************************************
4981779ba6bSJuan Castillo  * Generic function to load and authenticate an image. The image is actually
499*72600226SYatharth Kochar  * loaded by calling the 'load_image()' function. Therefore, it returns the
500*72600226SYatharth Kochar  * same error codes if the loading operation failed, or -EAUTH if the
501*72600226SYatharth Kochar  * authentication failed. In addition, this function uses recursion to
502*72600226SYatharth Kochar  * authenticate the parent images up to the root of trust.
5031779ba6bSJuan Castillo  ******************************************************************************/
5041779ba6bSJuan Castillo int load_auth_image(meminfo_t *mem_layout,
5051779ba6bSJuan Castillo 		    unsigned int image_id,
5061779ba6bSJuan Castillo 		    uintptr_t image_base,
5071779ba6bSJuan Castillo 		    image_info_t *image_data,
5081779ba6bSJuan Castillo 		    entry_point_info_t *entry_point_info)
5091779ba6bSJuan Castillo {
5101779ba6bSJuan Castillo 	int rc;
5111779ba6bSJuan Castillo 
5121779ba6bSJuan Castillo #if TRUSTED_BOARD_BOOT
5131779ba6bSJuan Castillo 	unsigned int parent_id;
5141779ba6bSJuan Castillo 
5151779ba6bSJuan Castillo 	/* Use recursion to authenticate parent images */
5161779ba6bSJuan Castillo 	rc = auth_mod_get_parent_id(image_id, &parent_id);
5171779ba6bSJuan Castillo 	if (rc == 0) {
5181779ba6bSJuan Castillo 		rc = load_auth_image(mem_layout, parent_id, image_base,
5191779ba6bSJuan Castillo 				     image_data, NULL);
52078460a05SJuan Castillo 		if (rc != 0) {
5211779ba6bSJuan Castillo 			return rc;
5221779ba6bSJuan Castillo 		}
5231779ba6bSJuan Castillo 	}
5241779ba6bSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */
5251779ba6bSJuan Castillo 
5261779ba6bSJuan Castillo 	/* Load the image */
5271779ba6bSJuan Castillo 	rc = load_image(mem_layout, image_id, image_base, image_data,
5281779ba6bSJuan Castillo 			entry_point_info);
52978460a05SJuan Castillo 	if (rc != 0) {
53078460a05SJuan Castillo 		return rc;
5311779ba6bSJuan Castillo 	}
5321779ba6bSJuan Castillo 
5331779ba6bSJuan Castillo #if TRUSTED_BOARD_BOOT
5341779ba6bSJuan Castillo 	/* Authenticate it */
5351779ba6bSJuan Castillo 	rc = auth_mod_verify_img(image_id,
5361779ba6bSJuan Castillo 				 (void *)image_data->image_base,
5371779ba6bSJuan Castillo 				 image_data->image_size);
5381779ba6bSJuan Castillo 	if (rc != 0) {
539fedbc049SJuan Castillo 		memset((void *)image_data->image_base, 0x00,
540fedbc049SJuan Castillo 		       image_data->image_size);
541fedbc049SJuan Castillo 		flush_dcache_range(image_data->image_base,
542fedbc049SJuan Castillo 				   image_data->image_size);
54378460a05SJuan Castillo 		return -EAUTH;
5441779ba6bSJuan Castillo 	}
545ad4494dcSDan Handley 	/*
546ad4494dcSDan Handley 	 * File has been successfully loaded and authenticated.
547ad4494dcSDan Handley 	 * Flush the image to main memory so that it can be executed later by
548ad4494dcSDan Handley 	 * any CPU, regardless of cache and MMU state.
549ad4494dcSDan Handley 	 */
550ad4494dcSDan Handley 	flush_dcache_range(image_data->image_base, image_data->image_size);
5511779ba6bSJuan Castillo #endif /* TRUSTED_BOARD_BOOT */
5521779ba6bSJuan Castillo 
55378460a05SJuan Castillo 	return 0;
5541779ba6bSJuan Castillo }
55568a68c92SSandrine Bailleux 
556*72600226SYatharth Kochar #endif /* LOAD_IMAGE_V2 */
557*72600226SYatharth Kochar 
55868a68c92SSandrine Bailleux /*******************************************************************************
55968a68c92SSandrine Bailleux  * Print the content of an entry_point_info_t structure.
56068a68c92SSandrine Bailleux  ******************************************************************************/
56168a68c92SSandrine Bailleux void print_entry_point_info(const entry_point_info_t *ep_info)
56268a68c92SSandrine Bailleux {
5634c0d0390SSoby Mathew 	INFO("Entry point address = %p\n", (void *)ep_info->pc);
5644c0d0390SSoby Mathew 	INFO("SPSR = 0x%x\n", ep_info->spsr);
56568a68c92SSandrine Bailleux 
56668a68c92SSandrine Bailleux #define PRINT_IMAGE_ARG(n)					\
56768a68c92SSandrine Bailleux 	VERBOSE("Argument #" #n " = 0x%llx\n",			\
56868a68c92SSandrine Bailleux 		(unsigned long long) ep_info->args.arg##n)
56968a68c92SSandrine Bailleux 
57068a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(0);
57168a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(1);
57268a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(2);
57368a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(3);
57451c79b73SSoby Mathew #ifndef AARCH32
57568a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(4);
57668a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(5);
57768a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(6);
57868a68c92SSandrine Bailleux 	PRINT_IMAGE_ARG(7);
57951c79b73SSoby Mathew #endif
58068a68c92SSandrine Bailleux #undef PRINT_IMAGE_ARG
58168a68c92SSandrine Bailleux }
582