Implement load_image in terms of IO abstraction

The modified implementation uses the IO abstraction rather than
making direct semi-hosting calls.  The semi-hosting driver is now
registered for the FVP platform during initialisation of each boot
stage where it is used.  Additionally, the FVP platform includes a
straightforward implementation of 'plat_get_image_source' which
provides a generic means for the 'load_image' function to determine
how to access the image data.

Change-Id: Ia34457b471dbee990c7b3c79de7aee4ceea51aa6
diff --git a/plat/fvp/bl1_plat_setup.c b/plat/fvp/bl1_plat_setup.c
index 428b1b3..3daa480 100644
--- a/plat/fvp/bl1_plat_setup.c
+++ b/plat/fvp/bl1_plat_setup.c
@@ -110,6 +110,9 @@
  ******************************************************************************/
 void bl1_platform_setup(void)
 {
+	/* Initialise the IO layer and register platform IO devices */
+	io_setup();
+
 	/* Enable and initialize the System level generic timer */
 	mmio_write_32(SYS_CNTCTL_BASE + CNTCR_OFF, CNTCR_EN);
 
@@ -119,6 +122,7 @@
 	return;
 }
 
+
 /*******************************************************************************
  * Perform the very early platform specific architecture setup here. At the
  * moment this only does basic initialization. Later architectural setup
diff --git a/plat/fvp/bl2_plat_setup.c b/plat/fvp/bl2_plat_setup.c
index 567c7d7..4efb436 100644
--- a/plat/fvp/bl2_plat_setup.c
+++ b/plat/fvp/bl2_plat_setup.c
@@ -105,6 +105,9 @@
  ******************************************************************************/
 void bl2_platform_setup()
 {
+	/* Initialise the IO layer and register platform IO devices */
+	io_setup();
+
 	/* Use the Trusted DRAM for passing args to BL31 */
 	bl2_el_change_mem_ptr = (unsigned char **) TZDRAM_BASE;
 }
diff --git a/plat/fvp/plat_io_storage.c b/plat/fvp/plat_io_storage.c
new file mode 100644
index 0000000..e476106
--- /dev/null
+++ b/plat/fvp/plat_io_storage.c
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * Neither the name of ARM nor the names of its contributors may be used
+ * to endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <assert.h>
+#include <string.h>
+#include "platform.h"
+#include "io_storage.h"
+#include "io_driver.h"
+#include "io_semihosting.h"
+#include "semihosting.h"	/* For FOPEN_MODE_... */
+#include "debug.h"
+
+
+/* IO devices */
+static struct io_plat_data io_data;
+static struct io_dev_connector *sh_dev_con;
+static void *const sh_dev_spec;
+static void *const sh_init_params;
+static io_dev_handle sh_dev_handle;
+
+static io_file_spec bl2_image_spec = {
+	.path = BL2_IMAGE_NAME,
+	.mode = FOPEN_MODE_R
+};
+
+static io_file_spec bl31_image_spec = {
+	.path = BL31_IMAGE_NAME,
+	.mode = FOPEN_MODE_R
+};
+
+
+/* Set up the IO devices present on this platform, ready for use */
+void io_setup(void)
+{
+	/* Initialise the IO layer */
+	io_init(&io_data);
+
+	/* Register a semi-hosting device */
+	int io_result = register_io_dev_sh(&sh_dev_con);
+	assert(io_result == IO_SUCCESS);
+
+	/* Open a connection to the semi-hosting device and cache the handle */
+	io_result = io_dev_open(sh_dev_con, sh_dev_spec, &sh_dev_handle);
+	assert(io_result == IO_SUCCESS);
+
+	/* Ignore improbable errors in release builds */
+	(void)io_result;
+}
+
+
+/* Return an IO device handle and specification which can be used to access
+ * an image */
+int plat_get_image_source(const char *image_name, io_dev_handle *dev_handle,
+				void **image_spec)
+{
+	int result = IO_FAIL;
+	assert((image_name != NULL) && (dev_handle != NULL) &&
+			(image_spec != NULL));
+
+	if (strcmp(BL2_IMAGE_NAME, image_name) == 0) {
+		result = io_dev_init(sh_dev_handle, sh_init_params);
+		if (result == IO_SUCCESS) {
+			*dev_handle = sh_dev_handle;
+			*(io_file_spec **)image_spec = &bl2_image_spec;
+		}
+	} else if (strcmp(BL31_IMAGE_NAME, image_name) == 0) {
+		result = io_dev_init(sh_dev_handle, sh_init_params);
+		if (result == IO_SUCCESS) {
+			*dev_handle = sh_dev_handle;
+			*(io_file_spec **)image_spec = &bl31_image_spec;
+		}
+	} else
+		assert(0);
+
+	return result;
+}
diff --git a/plat/fvp/platform.h b/plat/fvp/platform.h
index 1b0a736..ece882f 100644
--- a/plat/fvp/platform.h
+++ b/plat/fvp/platform.h
@@ -35,6 +35,7 @@
 #include <mmio.h>
 #include <psci.h>
 #include <bl_common.h>
+#include "io_storage.h"
 
 
 /*******************************************************************************
@@ -347,6 +348,11 @@
 extern unsigned int plat_get_aff_count(unsigned int, unsigned long);
 extern unsigned int plat_get_aff_state(unsigned int, unsigned long);
 
+/* Declarations for plat_io_storage.c */
+extern void io_setup(void);
+extern int plat_get_image_source(const char *image_name,
+		io_dev_handle *dev_handle, void **image_spec);
+
 #endif /*__ASSEMBLY__*/
 
 #endif /* __PLATFORM_H__ */
diff --git a/plat/fvp/platform.mk b/plat/fvp/platform.mk
index 5da2acd..2efc7bc 100644
--- a/plat/fvp/platform.mk
+++ b/plat/fvp/platform.mk
@@ -35,14 +35,16 @@
 PLAT_BL1_C_VPATH	:=	drivers/arm/interconnect/cci-400	\
 				drivers/arm/peripherals/pl011		\
 				lib/semihosting				\
-				lib/stdlib
+				lib/stdlib				\
+				drivers/io
 
 PLAT_BL1_S_VPATH	:=	lib/semihosting/${ARCH}
 
 PLAT_BL2_C_VPATH	:=	drivers/arm/interconnect/cci-400	\
 				drivers/arm/peripherals/pl011		\
 				lib/stdlib				\
-				lib/semihosting
+				lib/semihosting				\
+				drivers/io
 
 PLAT_BL2_S_VPATH	:=	lib/semihosting/${ARCH}
 
@@ -50,7 +52,8 @@
 				drivers/arm/peripherals/pl011		\
 				lib/semihosting				\
 				lib/stdlib				\
-				drivers/power
+				drivers/power				\
+				drivers/io
 
 PLAT_BL31_S_VPATH	:=	lib/semihosting/${ARCH}
 
@@ -58,7 +61,9 @@
 				mmio.o					\
 				pl011.o					\
 				semihosting.o				\
-				sysreg_helpers.o
+				sysreg_helpers.o			\
+				plat_io_storage.o			\
+				io_semihosting.o
 
 BL1_OBJS		+=	bl1_plat_setup.o			\
 				bl1_plat_helpers.o			\