Populate BL31 input parameters as per new spec

This patch is based on spec published at
https://github.com/ARM-software/tf-issues/issues/133

It rearranges the bl31_args struct into
bl31_params and bl31_plat_params which provide the
information needed for Trusted firmware and platform
specific data via x0 and x1

On the FVP platform BL3-1 params and BL3-1 plat params
and its constituents are stored at the start of TZDRAM.

The information about memory availability and size for
BL3-1, BL3-2 and BL3-3 is moved into platform specific data.

Change-Id: I8b32057a3d0dd3968ea26c2541a0714177820da9
diff --git a/common/bl_common.c b/common/bl_common.c
index 4144ae5..a2fa2d6 100644
--- a/common/bl_common.c
+++ b/common/bl_common.c
@@ -35,6 +35,7 @@
 #include <debug.h>
 #include <io_storage.h>
 #include <platform.h>
+#include <errno.h>
 #include <stdio.h>
 
 unsigned long page_align(unsigned long value, unsigned dir)
@@ -229,12 +230,15 @@
  * Generic function to load an image into the trusted RAM,
  * given a name, extents of free memory & whether the image should be loaded at
  * the bottom or top of the free memory. It updates the memory layout if the
- * load is successful.
+ * load is successful. It also updates the image information and the entry point
+ * information in the params passed
  ******************************************************************************/
-unsigned long load_image(meminfo_t *mem_layout,
+int load_image(meminfo_t *mem_layout,
 			 const char *image_name,
 			 unsigned int load_type,
-			 unsigned long fixed_addr)
+			 unsigned long fixed_addr,
+			 image_info_t *image_data,
+			 entry_point_info_t *entry_point_info)
 {
 	uintptr_t dev_handle;
 	uintptr_t image_handle;
@@ -248,13 +252,14 @@
 
 	assert(mem_layout != NULL);
 	assert(image_name != NULL);
+	assert(image_data->h.version >= VERSION_1);
 
 	/* Obtain a reference to the image by querying the platform layer */
 	io_result = plat_get_image_source(image_name, &dev_handle, &image_spec);
 	if (io_result != IO_SUCCESS) {
 		WARN("Failed to obtain reference to image '%s' (%i)\n",
 			image_name, io_result);
-		return 0;
+		return io_result;
 	}
 
 	/* Attempt to access the image */
@@ -262,7 +267,7 @@
 	if (io_result != IO_SUCCESS) {
 		WARN("Failed to access image '%s' (%i)\n",
 			image_name, io_result);
-		return 0;
+		return io_result;
 	}
 
 	/* Find the size of the image */
@@ -270,7 +275,7 @@
 	if ((io_result != IO_SUCCESS) || (image_size == 0)) {
 		WARN("Failed to determine the size of the image '%s' file (%i)\n",
 			image_name, io_result);
-		goto fail;
+		goto exit;
 	}
 
 	/* See if we have enough space */
@@ -278,7 +283,7 @@
 		WARN("Cannot load '%s' file: Not enough space.\n",
 			image_name);
 		dump_load_info(0, image_size, mem_layout);
-		goto fail;
+		goto exit;
 	}
 
 	switch (load_type) {
@@ -297,7 +302,8 @@
 		WARN("Cannot load '%s' file: Not enough space.\n",
 			image_name);
 		dump_load_info(image_base, image_size, mem_layout);
-		goto fail;
+		io_result = -ENOMEM;
+		goto exit;
 	  }
 
 	  /* Calculate the amount of extra memory used due to alignment */
@@ -315,10 +321,11 @@
 	  /* Page align base address and check whether the image still fits */
 	  if (image_base + image_size >
 	      mem_layout->free_base + mem_layout->free_size) {
-		  WARN("Cannot load '%s' file: Not enough space.\n",
-			  image_name);
-		  dump_load_info(image_base, image_size, mem_layout);
-		  goto fail;
+		WARN("Cannot load '%s' file: Not enough space.\n",
+		  image_name);
+		dump_load_info(image_base, image_size, mem_layout);
+		io_result = -ENOMEM;
+		goto exit;
 	  }
 
 	  /* Calculate the amount of extra memory used due to alignment */
@@ -383,14 +390,16 @@
 			WARN("Cannot load '%s' file: Not enough space.\n",
 				image_name);
 			dump_load_info(image_base, image_size, mem_layout);
-			goto fail;
+			io_result = -ENOMEM;
+			goto exit;
 		}
 
 		/* Check whether the fixed load address is page-aligned. */
 		if (!is_page_aligned(image_base)) {
 			WARN("Cannot load '%s' file at unaligned address 0x%lx\n",
 				image_name, fixed_addr);
-			goto fail;
+			io_result = -ENOMEM;
+			goto exit;
 		}
 
 		/*
@@ -440,9 +449,14 @@
 	io_result = io_read(image_handle, image_base, image_size, &bytes_read);
 	if ((io_result != IO_SUCCESS) || (bytes_read < image_size)) {
 		WARN("Failed to load '%s' file (%i)\n", image_name, io_result);
-		goto fail;
+		goto exit;
 	}
 
+	image_data->image_base = image_base;
+	image_data->image_size = image_size;
+
+	entry_point_info->pc = image_base;
+
 	/*
 	 * File has been successfully loaded. Update the free memory
 	 * data structure & flush the contents of the TZRAM so that
@@ -458,15 +472,12 @@
 		mem_layout->free_base += offset + image_size;
 
 exit:
-	io_result = io_close(image_handle);
+	io_close(image_handle);
 	/* Ignore improbable/unrecoverable error in 'close' */
 
 	/* TODO: Consider maintaining open device connection from this bootloader stage */
-	io_result = io_dev_close(dev_handle);
+	io_dev_close(dev_handle);
 	/* Ignore improbable/unrecoverable error in 'dev_close' */
 
-	return image_base;
-
-fail:	image_base = 0;
-	goto exit;
+	return io_result;
 }