1 /*
2  * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "smm_variable_service_context.h"
8 #include <protocols/rpc/common/packed-c/encoding.h>
9 #include <service/crypto/client/psa/psa_crypto_client.h>
10 #include <psa/crypto.h>
11 
smm_variable_service_context(const char * sn)12 smm_variable_service_context::smm_variable_service_context(const char *sn) :
13 	standalone_service_context(sn, RPC_BUFFER_SIZE),
14 	m_smm_variable_provider(),
15 	m_persistent_store_client(),
16 	m_volatile_store(),
17 	m_storage_service_context(NULL),
18 	m_crypto_service_context(NULL),
19 	m_storage_session(NULL),
20 	m_crypto_session(NULL)
21 {
22 
23 }
24 
~smm_variable_service_context()25 smm_variable_service_context::~smm_variable_service_context()
26 {
27 
28 }
29 
do_init()30 void smm_variable_service_context::do_init()
31 {
32 	/* Initialize crypto backend session */
33 	m_crypto_service_context = service_locator_query("sn:trustedfirmware.org:crypto:0");
34 	if (m_crypto_service_context) {
35 		m_crypto_session = service_context_open(m_crypto_service_context);
36 		if (m_crypto_session) {
37 			psa_crypto_client_init(m_crypto_session);
38 			psa_crypto_init();
39 		}
40 	}
41 
42 	/* Initialize the persistent storage backend - uses protected storage service */
43 	struct storage_backend *peristent_backend = NULL;
44 
45 	/* Locate and open RPC session with the protected-storage service */
46 	m_storage_service_context =
47 		service_locator_query("sn:trustedfirmware.org:protected-storage:0");
48 
49 	if (m_storage_service_context) {
50 
51 		m_storage_session = service_context_open(m_storage_service_context);
52 
53 		if (m_storage_session) {
54 
55 			peristent_backend = secure_storage_client_init(
56 				&m_persistent_store_client, m_storage_session);
57 		}
58 	}
59 
60 	/* Initialize the volatile storage backend */
61 	struct storage_backend *volatile_backend  = mock_store_init(&m_volatile_store);
62 
63 	/* Initialize the smm_variable service provider */
64 	struct rpc_service_interface *service_iface = smm_variable_provider_init(
65 		&m_smm_variable_provider,
66  		0,		/* owner id */
67 		MAX_VARIABLES,
68 		peristent_backend,
69 		volatile_backend);
70 
71 	standalone_service_context::set_rpc_interface(service_iface);
72 }
73 
do_deinit()74 void smm_variable_service_context::do_deinit()
75 {
76 	if (m_storage_session) {
77 		service_context_close(m_storage_service_context, m_storage_session);
78 		m_storage_session = NULL;
79 	}
80 
81 	if (m_storage_service_context) {
82 		service_context_relinquish(m_storage_service_context);
83 		m_storage_service_context = NULL;
84 	}
85 
86 	smm_variable_provider_deinit(&m_smm_variable_provider);
87 	secure_storage_client_deinit(&m_persistent_store_client);
88 	mock_store_deinit(&m_volatile_store);
89 
90 	psa_crypto_client_deinit();
91 
92 	if (m_crypto_service_context && m_crypto_session) {
93 		service_context_close(m_crypto_service_context, m_crypto_session);
94 		m_crypto_session = NULL;
95 
96 		service_context_relinquish(m_crypto_service_context);
97 		m_crypto_service_context = NULL;
98 	}
99 }
100