Create block storage SP deployment

Adds a deployment of the block storage service provider, running
within a secure partition. Uses a ram block store for backend
storage.

Signed-off-by: Julian Hall <julian.hall@arm.com>
Change-Id: I01f8876cdd9357a0045fc986c54ff252a75a2fc3
diff --git a/components/service/locator/linux/ffa/linuxffa_location_strategy.c b/components/service/locator/linux/ffa/linuxffa_location_strategy.c
index d1547a3..f02acfb 100644
--- a/components/service/locator/linux/ffa/linuxffa_location_strategy.c
+++ b/components/service/locator/linux/ffa/linuxffa_location_strategy.c
@@ -102,8 +102,8 @@
 /*
  * Returns a list of partition UUIDs and interface IDs to identify partitions that
  * could potentially host the requested service.  This mapping is based trustedfirmware.org
- * ffa partition UUIDs. There may be multiple UUIDs because of different depeloyment decisions
- * such as dedicated SP, SP hosting multple services.
+ * ffa partition UUIDs. There may be multiple UUIDs because of different deployment decisions
+ * such as dedicated SP, SP hosting multiple services.
  */
 static size_t suggest_tf_org_partition_uuids(const char *sn,
 	struct ffa_service_location *candidates, size_t candidate_limit)
@@ -122,6 +122,7 @@
 		{"protected-storage",           "751bf801-3dde-4768-a514-0f10aeed1790",	0},
 		{"test-runner",                 "33c75baf-ac6a-4fe4-8ac7-e9909bee2d17",	0},
 		{"attestation",                 "a1baf155-8876-4695-8f7c-54955e8db974",	0},
+		{"block-storage",               "63646e80-eb52-462f-ac4f-8cdf3987519c",	0},
 
 		/* Secure Enclave proxy accessed services */
 		{"crypto",                      "46bb39d1-b4d9-45b5-88ff-040027dab249",	SE_PROXY_INTERFACE_ID_CRYPTO},
diff --git a/deployments/block-storage/block-storage.cmake b/deployments/block-storage/block-storage.cmake
new file mode 100644
index 0000000..d751a57
--- /dev/null
+++ b/deployments/block-storage/block-storage.cmake
@@ -0,0 +1,43 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+add_components(TARGET "block-storage"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"components/common/fdt"
+		"components/common/tlv"
+		"components/common/uuid"
+		"components/common/endian"
+		"components/common/trace"
+		"components/common/utils"
+		"components/config/ramstore"
+		"components/config/loader/sp"
+		"components/messaging/ffa/libsp"
+		"components/rpc/common/interface"
+		"components/rpc/ffarpc/endpoint"
+		"components/service/common/include"
+		"components/service/common/provider"
+		"components/service/block_storage/block_store"
+		"components/service/block_storage/block_store/device"
+		"components/service/block_storage/block_store/device/ram"
+		"components/service/block_storage/block_store/partitioned"
+		"components/service/block_storage/provider"
+		"components/service/block_storage/provider/serializer/packed-c"
+		"components/service/block_storage/config/ref"
+		"components/service/block_storage/factory/ref_ram"
+)
+
+target_sources(block-storage PRIVATE
+	${CMAKE_CURRENT_LIST_DIR}/common/block_storage_sp.c
+)
+
+#################################################################
+
+target_include_directories(block-storage PRIVATE
+	${TS_ROOT}
+	${TS_ROOT}/components
+)
diff --git a/deployments/block-storage/common/block_storage_sp.c b/deployments/block-storage/common/block_storage_sp.c
new file mode 100644
index 0000000..ccefaaa
--- /dev/null
+++ b/deployments/block-storage/common/block_storage_sp.c
@@ -0,0 +1,120 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ */
+
+#include "rpc/ffarpc/endpoint/ffarpc_call_ep.h"
+#include "protocols/rpc/common/packed-c/status.h"
+#include "config/ramstore/config_ramstore.h"
+#include "config/loader/sp/sp_config_loader.h"
+#include "service/block_storage/provider/block_storage_provider.h"
+#include "service/block_storage/provider/serializer/packed-c/packedc_block_storage_serializer.h"
+#include "service/block_storage/factory/block_store_factory.h"
+#include "sp_api.h"
+#include "sp_discovery.h"
+#include "sp_messaging.h"
+#include "sp_rxtx.h"
+#include "trace.h"
+
+
+static bool sp_init(uint16_t *own_sp_id);
+
+void __noreturn sp_main(struct ffa_init_info *init_info)
+{
+	struct ffa_call_ep ffarpc_call_ep = { 0 };
+	struct block_storage_provider service_provider = { 0 };
+	struct block_store *backend = NULL;
+	struct rpc_interface *service_iface = NULL;
+	struct sp_msg req_msg = { 0 };
+	struct sp_msg resp_msg = { 0 };
+	uint16_t own_id = 0;
+	sp_result result = SP_RESULT_INTERNAL_ERROR;
+
+	/* Boot phase */
+	if (!sp_init(&own_id)) {
+		EMSG("Failed to init SP");
+		goto fatal_error;
+	}
+
+	config_ramstore_init();
+
+	if (!sp_config_load(init_info)) {
+		EMSG("Failed to load SP config");
+		goto fatal_error;
+	}
+
+	/* Initialise the service provider and backend block store */
+	backend = block_store_factory_create();
+	if (!backend) {
+		EMSG("Failed to create block store");
+		goto fatal_error;
+	}
+
+	service_iface = block_storage_provider_init(
+		&service_provider,
+		backend);
+	if (!service_iface) {
+		EMSG("Failed to init service provider");
+		goto fatal_error;
+	}
+
+	block_storage_provider_register_serializer(
+		&service_provider,
+		TS_RPC_ENCODING_PACKED_C,
+		packedc_block_storage_serializer_instance());
+
+	/* Associate service interface with FFA call endpoint */
+	ffa_call_ep_init(&ffarpc_call_ep, service_iface, own_id);
+
+	/* End of boot phase */
+	result = sp_msg_wait(&req_msg);
+	if (result != SP_RESULT_OK) {
+		EMSG("Failed to send message wait %d", result);
+		goto fatal_error;
+	}
+
+	while (1) {
+		ffa_call_ep_receive(&ffarpc_call_ep, &req_msg, &resp_msg);
+
+		result = sp_msg_send_direct_resp(&resp_msg, &req_msg);
+		if (result != SP_RESULT_OK) {
+			EMSG("Failed to send direct response %d", result);
+			result = sp_msg_wait(&req_msg);
+			if (result != SP_RESULT_OK) {
+				EMSG("Failed to send message wait %d", result);
+				goto fatal_error;
+			}
+		}
+	}
+
+fatal_error:
+	/* SP is not viable */
+	EMSG("Block storage SP error");
+	while (1) {}
+}
+
+void sp_interrupt_handler(uint32_t interrupt_id)
+{
+	(void)interrupt_id;
+}
+
+static bool sp_init(uint16_t *own_id)
+{
+	sp_result sp_res = SP_RESULT_INTERNAL_ERROR;
+	static uint8_t tx_buffer[4096] __aligned(4096);
+	static uint8_t rx_buffer[4096] __aligned(4096);
+
+	sp_res = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
+	if (sp_res != SP_RESULT_OK) {
+		EMSG("Failed to map RXTX buffers: %d", sp_res);
+		return false;
+	}
+
+	sp_res = sp_discovery_own_id_get(own_id);
+	if (sp_res != SP_RESULT_OK) {
+		EMSG("Failed to query own ID: %d", sp_res);
+		return false;
+	}
+
+	return true;
+}
diff --git a/deployments/block-storage/common/block_storage_sp.h b/deployments/block-storage/common/block_storage_sp.h
new file mode 100644
index 0000000..97ec07d
--- /dev/null
+++ b/deployments/block-storage/common/block_storage_sp.h
@@ -0,0 +1,10 @@
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef BLOCK_STORAGE_SP_H
+#define BLOCK_STORAGE_SP_H
+
+#endif /* BLOCK_STORAGE_SP_H */
diff --git a/deployments/block-storage/opteesp/CMakeLists.txt b/deployments/block-storage/opteesp/CMakeLists.txt
new file mode 100644
index 0000000..fa84397
--- /dev/null
+++ b/deployments/block-storage/opteesp/CMakeLists.txt
@@ -0,0 +1,88 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
+
+# Set default platform.
+set(TS_PLATFORM "arm/fvp/fvp_base_revc-2xaemv8a" CACHE STRING "Target platform location.")
+include(../../deployment.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  The CMakeLists.txt for building the block_storage deployment for opteesp
+#
+#  Builds the block_storage service provider for running in an SEL0 secure partition
+#  hosted by OPTEE in the role of SPM.
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/environments/opteesp/env.cmake)
+project(trusted-services LANGUAGES C ASM)
+add_executable(block-storage)
+target_include_directories(block-storage PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+set(SP_UUID_CANON "63646e80-eb52-462f-ac4f-8cdf3987519c")
+set(SP_HEAP_SIZE "120 * 1024" CACHE STRING "SP heap size in bytes")
+set(TRACE_PREFIX "BLOCK" CACHE STRING "Trace prefix")
+include(${TS_ROOT}/tools/cmake/common/TargetCompileDefinitions.cmake)
+set_target_uuids(
+	SP_UUID ${SP_UUID_CANON}
+	SP_NAME "block-storage"
+)
+
+#-------------------------------------------------------------------------------
+#  Components that are specific to deployment in the opteesp environment.
+#
+#-------------------------------------------------------------------------------
+add_components(TARGET "block-storage"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		"environments/opteesp"
+)
+
+include(../block-storage.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  Set target platform to provide drivers needed by the deployment
+#
+#-------------------------------------------------------------------------------
+add_platform(TARGET "block-storage")
+
+#################################################################
+
+target_compile_definitions(block-storage PRIVATE
+	ARM64=1
+)
+
+target_include_directories(block-storage PRIVATE
+	${TS_ROOT}/deployments/block-storage/opteesp
+)
+
+if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
+	target_compile_options(block-storage PRIVATE
+		-std=c99
+	)
+
+endif()
+
+compiler_generate_stripped_elf(TARGET block-storage NAME "${SP_UUID_CANON}.stripped.elf" RES STRIPPED_ELF)
+
+######################################## install
+if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
+	set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install CACHE PATH "location to install build output to." FORCE)
+endif()
+
+install(TARGETS block-storage
+			PUBLIC_HEADER DESTINATION ${TS_ENV}/include
+			RUNTIME DESTINATION ${TS_ENV}/bin
+		)
+install(FILES ${STRIPPED_ELF} DESTINATION ${TS_ENV}/bin)
+
+include(${TS_ROOT}/tools/cmake/common/ExportSp.cmake)
+export_sp(
+	SP_UUID_CANON ${SP_UUID_CANON}
+	SP_UUID_LE ${SP_UUID_LE}
+	SP_NAME "block-storage"
+	MK_IN ${TS_ROOT}/environments/opteesp/sp.mk.in
+	DTS_IN ${CMAKE_CURRENT_LIST_DIR}/default_block-storage.dts.in
+	JSON_IN ${TS_ROOT}/environments/opteesp/sp_pkg.json.in
+)
diff --git a/deployments/block-storage/opteesp/default_block-storage.dts.in b/deployments/block-storage/opteesp/default_block-storage.dts.in
new file mode 100644
index 0000000..0d30738
--- /dev/null
+++ b/deployments/block-storage/opteesp/default_block-storage.dts.in
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+@DTS_TAG@
+
+@DTS_NODE@ {
+	compatible = "arm,ffa-manifest-1.0";
+	ffa-version = <0x00010000>; /* 31:16 - Major, 15:0 - Minor */
+	uuid = <@EXPORT_SP_UUID_DT@>;
+	description = "BlockStorage";
+	execution-ctx-count = <1>;
+	exception-level = <1>; /* S-EL0 */
+	execution-state = <0>; /* AArch64 */
+	xlat-granule = <0>; /* 4KiB */
+	messaging-method = <3>; /* Direct messaging only */
+	legacy-elf-format = <1>;
+};
diff --git a/deployments/block-storage/opteesp/optee_sp_user_defines.h b/deployments/block-storage/opteesp/optee_sp_user_defines.h
new file mode 100644
index 0000000..798e873
--- /dev/null
+++ b/deployments/block-storage/opteesp/optee_sp_user_defines.h
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ */
+
+#ifndef OPTEE_SP_USER_DEFINES_H
+#define OPTEE_SP_USER_DEFINES_H
+
+#define OPTEE_SP_FLAGS			0
+
+/* Provisioned stack size */
+#define OPTEE_SP_STACK_SIZE			(64 * 1024)
+
+#endif /* SP_HEADER_DEFINES_H */
diff --git a/deployments/block-storage/sp/CMakeLists.txt b/deployments/block-storage/sp/CMakeLists.txt
new file mode 100644
index 0000000..ff598a8
--- /dev/null
+++ b/deployments/block-storage/sp/CMakeLists.txt
@@ -0,0 +1,87 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
+
+# Set default platform.
+set(TS_PLATFORM "arm/fvp/fvp_base_revc-2xaemv8a" CACHE STRING "Target platform location.")
+include(../../deployment.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  The CMakeLists.txt for building the block-storage deployment for generic sp
+#  environment.
+#
+#  Builds the block storage service provider for running in an SEL0 secure partition
+#  hosted by any SPM.
+#-------------------------------------------------------------------------------
+include(${TS_ROOT}/environments/sp/env.cmake)
+set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type")
+project(trusted-services LANGUAGES C ASM)
+add_executable(block-storage)
+target_include_directories(block-storage PRIVATE "${TOP_LEVEL_INCLUDE_DIRS}")
+
+set(SP_NAME "block-storage")
+set(SP_UUID_CANON "63646e80-eb52-462f-ac4f-8cdf3987519c")
+set(SP_UUID_LE "0x806e6463 0x2f4652eb 0xdf8c4fac 0x9c518739")
+set(SP_HEAP_SIZE "120 * 1024" CACHE STRING "SP heap size in bytes")
+set(SP_STACK_SIZE "64 * 1024" CACHE STRING "Stack size")
+set(TRACE_PREFIX "BLOCK" CACHE STRING "Trace prefix")
+
+#-------------------------------------------------------------------------------
+#  Components that are specific to deployment in the opteesp environment.
+#
+#-------------------------------------------------------------------------------
+add_components(TARGET "block-storage"
+	BASE_DIR ${TS_ROOT}
+	COMPONENTS
+		environments/sp
+)
+
+include(../block-storage.cmake REQUIRED)
+
+#-------------------------------------------------------------------------------
+#  Set target platform to provide drivers needed by the deployment
+#
+#-------------------------------------------------------------------------------
+add_platform(TARGET "block-storage")
+
+#################################################################
+
+target_compile_definitions(block-storage PRIVATE
+	ARM64=1
+)
+
+if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
+	target_compile_options(block-storage PRIVATE
+		-std=c99
+	)
+
+endif()
+
+compiler_generate_binary_output(TARGET block-storage NAME "${SP_UUID_CANON}.bin" SP_BINARY)
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${SP_UUID_CANON}.bin DESTINATION ${TS_ENV}/bin)
+
+include(${TS_ROOT}/tools/cmake/common/ExportMemoryRegionsToManifest.cmake REQUIRED)
+export_memory_regions_to_manifest(TARGET block-storage NAME "${SP_UUID_CANON}_memory_regions.dtsi" RES EXPORT_MEMORY_REGIONS_DTSI)
+
+######################################## install
+if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
+	set(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR}/install CACHE PATH "location to install build output to." FORCE)
+endif()
+install(TARGETS block-storage
+			PUBLIC_HEADER DESTINATION ${TS_ENV}/include
+			RUNTIME DESTINATION ${TS_ENV}/bin
+		)
+
+include(${TS_ROOT}/tools/cmake/common/ExportSp.cmake REQUIRED)
+export_sp(
+	SP_UUID_CANON ${SP_UUID_CANON}
+	SP_UUID_LE ${SP_UUID_LE}
+	SP_NAME ${SP_NAME}
+	DTS_IN ${CMAKE_CURRENT_LIST_DIR}/default_${SP_NAME}.dts.in
+	DTS_MEM_REGIONS ${SP_UUID_CANON}_memory_regions.dtsi
+	JSON_IN ${TS_ROOT}/environments/sp/sp_pkg.json.in
+)
diff --git a/deployments/block-storage/sp/default_block-storage.dts.in b/deployments/block-storage/sp/default_block-storage.dts.in
new file mode 100644
index 0000000..fd4b539
--- /dev/null
+++ b/deployments/block-storage/sp/default_block-storage.dts.in
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+@DTS_TAG@
+
+@DTS_NODE@ {
+	compatible = "arm,ffa-manifest-1.0";
+	ffa-version = <0x00010000>; /* 31:16 - Major, 15:0 - Minor */
+	uuid = <@EXPORT_SP_UUID_DT@>;
+	description = "BlockStorage";
+	execution-ctx-count = <1>;
+	exception-level = <1>; /* S-EL0 */
+	execution-state = <0>; /* AArch64 */
+	xlat-granule = <0>; /* 4KiB */
+	messaging-method = <3>; /* Direct messaging only */
+
+	memory-regions {
+		compatible = "arm,ffa-manifest-memory-regions";
+
+		#include "@EXPORT_DTS_MEM_REGIONS@"
+	};
+};