SPM: Load image and RD from SP package

Load SP and RD from package instead of relying on RD being already
loaded in memory and the SP being loaded as a BL32 image.

Change-Id: I18d4fbf4597656c6a7e878e1d7c01a8a324f3f8a
Signed-off-by: Antonio Nino Diaz <antonio.ninodiaz@arm.com>
diff --git a/services/std_svc/spm/spm_main.c b/services/std_svc/spm/spm_main.c
index ad1262c..6de4858 100644
--- a/services/std_svc/spm/spm_main.c
+++ b/services/std_svc/spm/spm_main.c
@@ -157,7 +157,10 @@
  ******************************************************************************/
 int32_t spm_setup(void)
 {
+	int rc;
 	sp_context_t *ctx;
+	void *sp_base, *rd_base;
+	size_t sp_size, rd_size;
 
 	/* Disable MMU at EL1 (initialized by BL2) */
 	disable_mmu_icache_el1();
@@ -167,9 +170,26 @@
 
 	ctx = &sp_ctx;
 
+	rc = plat_spm_sp_get_next_address(&sp_base, &sp_size,
+					  &rd_base, &rd_size);
+	if (rc != 0) {
+		ERROR("No Secure Partition found.\n");
+		panic();
+	}
+
 	/* Assign translation tables context. */
 	ctx->xlat_ctx_handle = spm_get_sp_xlat_context();
 
+	/* Save location of the image in physical memory */
+	ctx->image_base = (uintptr_t)sp_base;
+	ctx->image_size = sp_size;
+
+	rc = plat_spm_sp_rd_load(&ctx->rd, rd_base, rd_size);
+	if (rc < 0) {
+		ERROR("Error while loading RD blob.\n");
+		panic();
+	}
+
 	spm_sp_setup(ctx);
 
 	/* Register init function for deferred init.  */
diff --git a/services/std_svc/spm/spm_private.h b/services/std_svc/spm/spm_private.h
index ec3f48e..ee13e94 100644
--- a/services/std_svc/spm/spm_private.h
+++ b/services/std_svc/spm/spm_private.h
@@ -32,6 +32,7 @@
 #ifndef __ASSEMBLY__
 
 #include <spinlock.h>
+#include <sp_res_desc.h>
 #include <stdint.h>
 #include <xlat_tables_v2.h>
 
@@ -42,9 +43,14 @@
 } sp_state_t;
 
 typedef struct sp_context {
+	/* Location of the image in physical memory */
+	unsigned long long image_base;
+	size_t image_size;
+
 	uint64_t c_rt_ctx;
 	cpu_context_t cpu_ctx;
 	xlat_ctx_t *xlat_ctx_handle;
+	struct sp_res_desc rd;
 
 	sp_state_t state;
 	spinlock_t state_lock;