blob: db98c1d3f5c41c08a71f002aee09529e77fb44a4 [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/its/its_frontend.h>
10#include <service_locator.h>
11#include <stdio.h>
12
Gabor Tothee2e7cb2024-10-07 17:02:56 +020013#include "libpsats.h"
Gabor Toth172659d2023-04-27 08:51:21 +020014#include "trace.h"
15
16static struct rpc_caller_session *rpc_session;
17static struct service_context *its_service_context;
18static struct secure_storage_client its_storage_client;
19
Gabor Tothee2e7cb2024-10-07 17:02:56 +020020LIBPSATS_EXPORTED psa_status_t libpsats_init_its_context(const char *service_name)
Gabor Toth172659d2023-04-27 08:51:21 +020021{
22 psa_status_t result = PSA_ERROR_GENERIC_ERROR;
23
24 if (rpc_session || its_service_context) {
25 EMSG("The client is already initialized\n");
26 return result;
27 }
28
29 service_locator_init();
30
31 its_service_context = service_locator_query(service_name);
32
33 if (!its_service_context) {
34 EMSG("Failed to discover service\n");
35 return result;
36 }
37
38 rpc_session = service_context_open(its_service_context);
39
40 if (!rpc_session) {
41 EMSG("Failed to open rpc session\n");
Gabor Tothee2e7cb2024-10-07 17:02:56 +020042 libpsats_deinit_its_context();
Gabor Toth172659d2023-04-27 08:51:21 +020043 return result;
44 }
45
46 struct storage_backend *its_storage_backend =
47 secure_storage_client_init(&its_storage_client, rpc_session);
48
49 if (!its_storage_backend) {
50 EMSG("Failed to initialize storage backend\n");
Gabor Tothee2e7cb2024-10-07 17:02:56 +020051 libpsats_deinit_its_context();
Gabor Toth172659d2023-04-27 08:51:21 +020052 return result;
53 }
54
55 result = psa_its_frontend_init(its_storage_backend);
56
57 return result;
58}
59
Gabor Tothee2e7cb2024-10-07 17:02:56 +020060LIBPSATS_EXPORTED void libpsats_deinit_its_context(void)
Gabor Toth172659d2023-04-27 08:51:21 +020061{
62 psa_its_frontend_init(NULL);
63 secure_storage_client_deinit(&its_storage_client);
64
65 if (its_service_context && rpc_session) {
66 service_context_close(its_service_context, rpc_session);
67 rpc_session = NULL;
68 }
69
70 if (its_service_context) {
71 service_context_relinquish(its_service_context);
72 its_service_context = NULL;
73 }
74}