Add smm-variable standalone service context

To support service-level testing of the smm-variable service in
a native PC environment, a standalone service context has been
added that allows the smm-variable service to be discovered
and used via the service locator API exposed by libts.

Signed-off-by: Julian Hall <julian.hall@arm.com>
Change-Id: I1998c0a80f325938498f5edf7b36abd170e06cc8
diff --git a/components/service/locator/standalone/services/smm-variable/component.cmake b/components/service/locator/standalone/services/smm-variable/component.cmake
new file mode 100644
index 0000000..482d655
--- /dev/null
+++ b/components/service/locator/standalone/services/smm-variable/component.cmake
@@ -0,0 +1,13 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+if (NOT DEFINED TGT)
+	message(FATAL_ERROR "mandatory parameter TGT is not defined.")
+endif()
+
+target_sources(${TGT} PRIVATE
+	"${CMAKE_CURRENT_LIST_DIR}/smm_variable_service_context.cpp"
+	)
diff --git a/components/service/locator/standalone/services/smm-variable/smm_variable_service_context.cpp b/components/service/locator/standalone/services/smm-variable/smm_variable_service_context.cpp
new file mode 100644
index 0000000..3669792
--- /dev/null
+++ b/components/service/locator/standalone/services/smm-variable/smm_variable_service_context.cpp
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include "smm_variable_service_context.h"
+#include <protocols/rpc/common/packed-c/encoding.h>
+
+smm_variable_service_context::smm_variable_service_context(const char *sn) :
+	standalone_service_context(sn),
+	m_smm_variable_provider(),
+	m_persistent_store_client(),
+	m_volatile_store(),
+	m_storage_service_context(NULL),
+	m_storage_session_handle(NULL)
+{
+
+}
+
+smm_variable_service_context::~smm_variable_service_context()
+{
+
+}
+
+void smm_variable_service_context::do_init()
+{
+    /* Initialize the persistent storage backend - uses protected storage service */
+	struct storage_backend *peristent_backend = NULL;
+    struct rpc_caller *storage_caller = NULL;
+    int status = 0;
+
+    /* Locate and open RPC session with the protected-storage service */
+    m_storage_service_context =
+		service_locator_query("sn:trustedfirmware.org:protected-storage:0", &status);
+
+    if (m_storage_service_context) {
+
+        m_storage_session_handle = service_context_open(
+			m_storage_service_context, TS_RPC_ENCODING_PACKED_C,
+			&storage_caller);
+
+        if (m_storage_session_handle) {
+
+            peristent_backend = secure_storage_client_init(
+				&m_persistent_store_client, storage_caller);
+        }
+    }
+
+	/* Initialize the volatile storage backend */
+	struct storage_backend *volatile_backend  = mock_store_init(&m_volatile_store);
+
+	/* Initialize the smm_variable service provider */
+	struct rpc_interface *service_iface = smm_variable_provider_init(
+		&m_smm_variable_provider,
+ 		0,		/* owner id */
+		MAX_VARIABLES,
+		peristent_backend,
+		volatile_backend);
+
+	standalone_service_context::set_rpc_interface(service_iface);
+}
+
+void smm_variable_service_context::do_deinit()
+{
+    if (m_storage_session_handle) {
+        service_context_close(m_storage_service_context, m_storage_session_handle);
+        m_storage_session_handle = NULL;
+    }
+
+    if (m_storage_service_context) {
+        service_context_relinquish(m_storage_service_context);
+        m_storage_service_context = NULL;
+    }
+
+	smm_variable_provider_deinit(&m_smm_variable_provider);
+    secure_storage_client_deinit(&m_persistent_store_client);
+    mock_store_deinit(&m_volatile_store);
+}
diff --git a/components/service/locator/standalone/services/smm-variable/smm_variable_service_context.h b/components/service/locator/standalone/services/smm-variable/smm_variable_service_context.h
new file mode 100644
index 0000000..ebdb7b6
--- /dev/null
+++ b/components/service/locator/standalone/services/smm-variable/smm_variable_service_context.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef STANDALONE_SMM_VARIABLE_SERVICE_CONTEXT_H
+#define STANDALONE_SMM_VARIABLE_SERVICE_CONTEXT_H
+
+#include <stddef.h>
+#include <stdint.h>
+#include <service/locator/standalone/standalone_service_context.h>
+#include <service/smm_variable/provider/smm_variable_provider.h>
+#include <service/secure_storage/backend/secure_storage_client/secure_storage_client.h>
+#include <service/secure_storage/backend/mock_store/mock_store.h>
+
+class smm_variable_service_context : public standalone_service_context
+{
+public:
+	smm_variable_service_context(const char *sn);
+	virtual ~smm_variable_service_context();
+
+private:
+
+	void do_init();
+	void do_deinit();
+
+	static const size_t MAX_VARIABLES = 40;
+
+	struct smm_variable_provider m_smm_variable_provider;
+	struct secure_storage_client m_persistent_store_client;
+	struct mock_store m_volatile_store;
+	struct service_context *m_storage_service_context;
+	rpc_session_handle m_storage_session_handle;
+};
+
+#endif /* STANDALONE_SMM_VARIABLE_SERVICE_CONTEXT_H */
diff --git a/components/service/locator/standalone/standalone_env.cpp b/components/service/locator/standalone/standalone_env.cpp
index ab1a1be..7f7ce13 100644
--- a/components/service/locator/standalone/standalone_env.cpp
+++ b/components/service/locator/standalone/standalone_env.cpp
@@ -10,25 +10,29 @@
 #include <service/locator/standalone/services/protected-storage/ps_service_context.h>
 #include <service/locator/standalone/services/test-runner/test_runner_service_context.h>
 #include <service/locator/standalone/services/attestation/attestation_service_context.h>
+#include <service/locator/standalone/services/smm-variable/smm_variable_service_context.h>
 #include "standalone_location_strategy.h"
 #include "standalone_service_registry.h"
 
 void service_locator_envinit(void)
 {
-    static crypto_service_context crypto_context("sn:trustedfirmware.org:crypto:0");
-    standalone_service_registry::instance()->regsiter_service_instance(&crypto_context);
+	static crypto_service_context crypto_context("sn:trustedfirmware.org:crypto:0");
+	standalone_service_registry::instance()->regsiter_service_instance(&crypto_context);
 
-    static its_service_context its_service_context("sn:trustedfirmware.org:internal-trusted-storage:0");
-    standalone_service_registry::instance()->regsiter_service_instance(&its_service_context);
+	static its_service_context its_service_context("sn:trustedfirmware.org:internal-trusted-storage:0");
+	standalone_service_registry::instance()->regsiter_service_instance(&its_service_context);
 
-    static ps_service_context ps_service_context("sn:trustedfirmware.org:protected-storage:0");
-    standalone_service_registry::instance()->regsiter_service_instance(&ps_service_context);
+	static ps_service_context ps_service_context("sn:trustedfirmware.org:protected-storage:0");
+	standalone_service_registry::instance()->regsiter_service_instance(&ps_service_context);
 
-    static test_runner_service_context test_runner_context("sn:trustedfirmware.org:test-runner:0");
-    standalone_service_registry::instance()->regsiter_service_instance(&test_runner_context);
+	static test_runner_service_context test_runner_context("sn:trustedfirmware.org:test-runner:0");
+	standalone_service_registry::instance()->regsiter_service_instance(&test_runner_context);
 
-    static attestation_service_context attestation_context("sn:trustedfirmware.org:attestation:0");
-    standalone_service_registry::instance()->regsiter_service_instance(&attestation_context);
+	static attestation_service_context attestation_context("sn:trustedfirmware.org:attestation:0");
+	standalone_service_registry::instance()->regsiter_service_instance(&attestation_context);
 
-    service_locator_register_strategy(standalone_location_strategy());
+	static smm_variable_service_context smm_variable_context("sn:trustedfirmware.org:smm-variable:0");
+	standalone_service_registry::instance()->regsiter_service_instance(&smm_variable_context);
+
+	service_locator_register_strategy(standalone_location_strategy());
 }
diff --git a/deployments/component-test/component-test.cmake b/deployments/component-test/component-test.cmake
index 2fdc857..a0233c3 100644
--- a/deployments/component-test/component-test.cmake
+++ b/deployments/component-test/component-test.cmake
@@ -45,6 +45,7 @@
 		"components/service/locator/standalone/services/protected-storage"
 		"components/service/locator/standalone/services/test-runner"
 		"components/service/locator/standalone/services/attestation"
+		"components/service/locator/standalone/services/smm-variable"
 		"components/service/discovery/client"
 		"components/service/discovery/provider"
 		"components/service/discovery/provider/serializer/packed-c"
diff --git a/deployments/libts/linux-pc/CMakeLists.txt b/deployments/libts/linux-pc/CMakeLists.txt
index 5f58aa9..fc98407 100644
--- a/deployments/libts/linux-pc/CMakeLists.txt
+++ b/deployments/libts/linux-pc/CMakeLists.txt
@@ -32,6 +32,7 @@
 		"components/rpc/direct"
 		"components/common/tlv"
 		"components/common/endian"
+		"components/common/utils"
 		"components/config/ramstore"
 		"components/service/common/include"
 		"components/service/common/client"
@@ -45,6 +46,7 @@
 		"components/service/locator/standalone/services/protected-storage"
 		"components/service/locator/standalone/services/test-runner"
 		"components/service/locator/standalone/services/attestation"
+		"components/service/locator/standalone/services/smm-variable"
 		"components/service/attestation/include"
 		"components/service/attestation/claims"
 		"components/service/attestation/claims/sources/boot_seed_generator"
@@ -82,6 +84,8 @@
 		"components/service/test_runner/provider/serializer/packed-c"
 		"components/service/test_runner/provider/backend/mock"
 		"components/service/test_runner/provider/backend/simple_c"
+		"components/service/smm_variable/backend"
+		"components/service/smm_variable/provider"
 		"protocols/rpc/common/packed-c"
 		"protocols/service/crypto/packed-c"
 		"protocols/service/crypto/protobuf"