blob: 01d41d1da3bba9e860c40809c8d738abb631f2a0 [file] [log] [blame]
Gabor Toth172659d2023-04-27 08:51:21 +02001/*
2 * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <protocols/rpc/common/packed-c/encoding.h>
8#include <service/secure_storage/backend/secure_storage_client/secure_storage_client.h>
9#include <service/secure_storage/frontend/psa/ps/ps_frontend.h>
10#include <service_locator.h>
11#include <stdio.h>
12
13#include "libpsa.h"
14#include "trace.h"
15
16static struct rpc_caller_session *rpc_session;
17static struct service_context *ps_service_context;
18static struct secure_storage_client ps_storage_client;
19
20LIBPSA_EXPORTED psa_status_t libpsa_init_ps_context(const char *service_name)
21{
22 psa_status_t result = PSA_ERROR_GENERIC_ERROR;
23
24 if (rpc_session || ps_service_context) {
25 EMSG("The client is already initialized\n");
26 return result;
27 }
28
29 service_locator_init();
30
31 ps_service_context = service_locator_query(service_name);
32
33 if (!ps_service_context) {
34 EMSG("Failed to discover service\n");
35 return result;
36 }
37
38 rpc_session = service_context_open(ps_service_context);
39
40 if (!rpc_session) {
41 EMSG("Failed to open rpc session\n");
42 libpsa_deinit_ps_context();
43 return result;
44 }
45
46 struct storage_backend *ps_storage_backend =
47 secure_storage_client_init(&ps_storage_client, rpc_session);
48
49 if (!ps_storage_backend) {
50 EMSG("Failed to initialize storage backend\n");
51 libpsa_deinit_ps_context();
52 return result;
53 }
54
55 result = psa_ps_frontend_init(ps_storage_backend);
56
57 return result;
58}
59
60LIBPSA_EXPORTED void libpsa_deinit_ps_context(void)
61{
62 psa_ps_frontend_init(NULL);
63 secure_storage_client_deinit(&ps_storage_client);
64
65 if (ps_service_context && rpc_session) {
66 service_context_close(ps_service_context, rpc_session);
67 rpc_session = NULL;
68 }
69
70 if (ps_service_context) {
71 service_context_relinquish(ps_service_context);
72 ps_service_context = NULL;
73 }
74}