refactor(cactus): map boot information regions

Map the boot contents regions and add optional mapping for boot info
descriptor contents depending on whether they will be accessed.

Signed-off-by: J-Alves <joao.alves@arm.com>
Signed-off-by: Kathleen Capella <kathleen.capella@arm.com>
Change-Id: I3dcbff07e9ee33008fa7f64fc74b06a1c42aa5dd
diff --git a/spm/cactus/cactus_main.c b/spm/cactus/cactus_main.c
index 48af7ca..75b2fb0 100644
--- a/spm/cactus/cactus_main.c
+++ b/spm/cactus/cactus_main.c
@@ -223,7 +223,10 @@
 			(SP_RX_TX_SIZE / 2),
 			MT_RW_DATA);
 
-	mmap_add(cactus_mmap);
+	/* Skip additional mapping for cactus StMM. */
+	if (vm_id != SP_ID(5)) {
+		mmap_add(cactus_mmap);
+	}
 	init_xlat_tables();
 }
 
@@ -237,6 +240,36 @@
 	return ffa_service_call(&args);
 }
 
+static void cactus_map_boot_info(struct ffa_boot_info_header *boot_info_header,
+		bool do_desc_access)
+{
+	struct ffa_boot_info_desc *boot_info_desc;
+
+	assert(boot_info_header != NULL);
+
+	boot_info_desc = boot_info_header->boot_info;
+	/*
+	 * TODO: Currently just validating that cactus can
+	 * access the boot info descriptors. By default, allocate one page
+	 * for boot info. In case we want to use the boot info contents, we should check the
+	 * blob and remap if the size is bigger than one page.
+	 * Only then access the contents.
+	 */
+	mmap_add_dynamic_region(
+		(unsigned long long)boot_info_header,
+		(uintptr_t)boot_info_header,
+		PAGE_SIZE, MT_RO_DATA);
+
+	if (do_desc_access) {
+		for (uint32_t i = 0; i < boot_info_header->desc_count; i++) {
+			mmap_add_dynamic_region(
+				(unsigned long long)boot_info_desc[i].content,
+				(uintptr_t)boot_info_desc[i].content,
+				PAGE_SIZE, MT_RO_DATA);
+		}
+	}
+}
+
 void __dead2 cactus_main(bool primary_cold_boot,
 			 struct ffa_boot_info_header *boot_info_header)
 {
@@ -268,17 +301,7 @@
 		sp_handler_spin_lock_init();
 
 		if (boot_info_header != NULL) {
-			/*
-			 * TODO: Currently just validating that cactus can
-			 * access the boot info descriptors. In case we want to
-			 * use the boot info contents, we should check the
-			 * blob and remap if the size is bigger than one page.
-			 * Only then access the contents.
-			 */
-			mmap_add_dynamic_region(
-				(unsigned long long)boot_info_header,
-				(uintptr_t)boot_info_header,
-				PAGE_SIZE, MT_RO_DATA);
+			cactus_map_boot_info(boot_info_header, ffa_id == SP_ID(5));
 		}
 	}
 
@@ -304,10 +327,16 @@
 	NOTICE("Booting Secure Partition (ID: %x)\n%s\n%s\n",
 		ffa_id, build_message, version_string);
 
-	if (ffa_id == SP_ID(1)) {
+	/*
+	 * Print FF-A boot info if requested in manifest via FF-A boot info
+	 * protocol.
+	 */
+	if (ffa_id == SP_ID(1) || ffa_id == SP_ID(5)) {
 		cactus_print_boot_info(boot_info_header);
 	}
 
+	cactus_print_memory_layout(ffa_id);
+
 	/*
 	 * Cactus-tertiary and cactus-stmm make use of FFA_RXTX_MAP API
 	 * instead of specifying `rx_tx-info` in their manifests, as done by the
@@ -323,7 +352,6 @@
 		}
 	}
 
-	cactus_print_memory_layout(ffa_id);
 
 	ret = register_secondary_entrypoint();