blob: 2234ec0094da020f36338e7fbfb0cf9258f5d43e [file] [log] [blame]
Balint Dobszay72c3f042020-11-23 18:23:57 +01001/*
Imre Kisd0ed5c22021-12-15 17:05:47 +01002 * Copyright (c) 2020-2022, Arm Limited and Contributors. All rights reserved.
Balint Dobszay72c3f042020-11-23 18:23:57 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Imre Kis0fc366e2023-08-10 10:51:01 +02007#include "components/rpc/ts_rpc/caller/sp/ts_rpc_caller_sp.h"
Imre Kis43dc08e2022-07-05 16:44:03 +02008#include "components/service/secure_storage/frontend/psa/its/its_frontend.h"
9#include "service/secure_storage/backend/secure_storage_client/secure_storage_client.h"
10#include "psa/internal_trusted_storage.h"
11#include "sp_api.h"
Imre Kis45df16f2023-03-24 13:27:10 +010012#include "sp_discovery.h"
Imre Kis76e8a3c2021-04-16 16:54:17 +020013#include "sp_messaging.h"
Imre Kis43dc08e2022-07-05 16:44:03 +020014#include "sp_rxtx.h"
15#include "trace.h"
16#include "string.h"
Balint Dobszay72c3f042020-11-23 18:23:57 +010017
18#define SP_ITS_UUID_BYTES \
19 { 0x48, 0xef, 0x1e, 0xdc, 0x7a, 0xb1, 0xcf, 0x4c, \
20 0xac, 0x8b, 0xdf, 0xcf, 0xf7, 0x71, 0x1b, 0x14, }
21
Balint Dobszay72c3f042020-11-23 18:23:57 +010022static uint8_t tx_buffer[4096] __aligned(4096);
23static uint8_t rx_buffer[4096] __aligned(4096);
Imre Kis0fc366e2023-08-10 10:51:01 +020024static const struct rpc_uuid its_uuid = { .uuid = SP_ITS_UUID_BYTES };
Balint Dobszay72c3f042020-11-23 18:23:57 +010025
26static const psa_storage_uid_t test_data_uid = 0x12345678;
27static const uint8_t test_data[] = {
28 0xc3, 0xe2, 0xf8, 0x1c, 0xe0, 0x87, 0x8a, 0x14, 0xbf, 0x59, 0xa3, 0xff,
29 0x96, 0x50, 0x25, 0x95, 0x76, 0xdc, 0xbe, 0xe6, 0x45, 0x45, 0x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56, 0x7a,
45 0xe2, 0xb3, 0xd2, 0xbf, 0xc3, 0x0c, 0x06, 0x8e, 0x5f, 0xdc, 0xd3, 0x1f,
46 0x85, 0x74, 0x38, 0x96, 0x5e, 0x1b, 0xe5, 0xa0, 0xc0, 0xfb, 0x90, 0xb7,
47 0x14, 0x16, 0x1b, 0xbe, 0xd8, 0x5c, 0x8e, 0xcc, 0x74, 0x71, 0x8c, 0x34,
48 0xc0, 0xbc, 0x24, 0xf5, 0xb9, 0x9b, 0xa1, 0x59, 0xe8, 0x54, 0xd0, 0xe9,
49 0x01, 0xa9, 0x6e, 0x27,
50};
51static const size_t test_data_size = sizeof(test_data);
52static uint8_t check_data[256] = { 0 };
53static size_t check_data_size = 0;
54
55static void run_its_test(void)
56{
Imre Kis43dc08e2022-07-05 16:44:03 +020057 psa_status_t its_status = PSA_ERROR_GENERIC_ERROR;
58 struct psa_storage_info_t info = { 0 };
Balint Dobszay72c3f042020-11-23 18:23:57 +010059
60 /* Write data */
61 its_status = psa_its_set(test_data_uid, test_data_size, test_data,
62 PSA_STORAGE_FLAG_NONE);
63
64 if (its_status != PSA_SUCCESS) {
65 EMSG("Error: %d", its_status);
66 }
67
68 /* Read back and compare */
69 its_status = psa_its_get(test_data_uid, 0, test_data_size,
70 check_data, &check_data_size);
71
72 if (its_status != PSA_SUCCESS || check_data_size != test_data_size) {
73 EMSG("Error: %d", its_status);
74 }
75
76 if(memcmp(test_data, check_data, test_data_size)) {
77 EMSG("Data check failed");
78 }
79
80 memset(check_data, 0, test_data_size);
81
82 /* Get info of entry */
83 psa_its_get_info(test_data_uid, &info);
84 if (its_status != PSA_SUCCESS) {
85 EMSG("Error: %d", its_status);
86 }
87
88 /* Write data with new uid */
89 its_status = psa_its_set(test_data_uid + 1, test_data_size, test_data,
90 PSA_STORAGE_FLAG_NONE);
91
92 if (its_status != PSA_SUCCESS) {
93 EMSG("Error: %d", its_status);
94 }
95
96 /* Read back and compare */
97 its_status = psa_its_get(test_data_uid + 1, 0, test_data_size,
98 check_data, &check_data_size);
99
100 if (its_status != PSA_SUCCESS || check_data_size != test_data_size) {
101 EMSG("Error: %d", its_status);
102 }
103
104 if(memcmp(test_data, check_data, test_data_size)) {
105 EMSG("Data check failed");
106 }
107
108 /* Delete entry */
109 its_status = psa_its_remove(test_data_uid + 1);
110 if (its_status != PSA_SUCCESS) {
111 EMSG("Error: %d", its_status);
112 }
113
114 /* Check if really deleted */
115 its_status = psa_its_get(test_data_uid + 1, 0, test_data_size,
116 check_data, &check_data_size);
117
118 if (its_status != PSA_ERROR_DOES_NOT_EXIST) {
119 EMSG("Error: %d", its_status);
120 }
121
122 IMSG("ITS test done");
123}
124
Balint Dobszay4f9d8e32023-04-13 13:55:08 +0200125void __noreturn sp_main(union ffa_boot_info *boot_info) {
Balint Dobszay72c3f042020-11-23 18:23:57 +0100126
Imre Kis43dc08e2022-07-05 16:44:03 +0200127 sp_result result = SP_RESULT_INTERNAL_ERROR;
Imre Kis76e8a3c2021-04-16 16:54:17 +0200128 struct sp_msg req_msg = { 0 };
Imre Kis0fc366e2023-08-10 10:51:01 +0200129 struct rpc_caller_interface caller = { 0 };
130 struct rpc_caller_session session = { 0 };
Imre Kis43dc08e2022-07-05 16:44:03 +0200131 struct secure_storage_client secure_storage_client = {0};
132 struct storage_backend *storage_backend = NULL;
Imre Kis0fc366e2023-08-10 10:51:01 +0200133 rpc_status_t rpc_status = RPC_ERROR_INTERNAL;
Imre Kis43dc08e2022-07-05 16:44:03 +0200134 psa_status_t psa_status = PSA_ERROR_GENERIC_ERROR;
Balint Dobszay72c3f042020-11-23 18:23:57 +0100135
136 /* Boot */
Balint Dobszay4f9d8e32023-04-13 13:55:08 +0200137 (void)boot_info;
Balint Dobszay72c3f042020-11-23 18:23:57 +0100138 IMSG("Test SP started");
139
Imre Kis43dc08e2022-07-05 16:44:03 +0200140 result = sp_rxtx_buffer_map(tx_buffer, rx_buffer, sizeof(rx_buffer));
141 if (result != SP_RESULT_OK) {
142 EMSG("Failed to map RXTX buffers: %d", result);
143 goto fatal_error;
Balint Dobszay72c3f042020-11-23 18:23:57 +0100144 }
145
Imre Kis0fc366e2023-08-10 10:51:01 +0200146 rpc_status = ts_rpc_caller_sp_init(&caller);
147 if (rpc_status != RPC_SUCCESS) {
148 EMSG("Failed initialize RPC caller");
Imre Kis43dc08e2022-07-05 16:44:03 +0200149 goto fatal_error;
Balint Dobszay72c3f042020-11-23 18:23:57 +0100150 }
Imre Kis43dc08e2022-07-05 16:44:03 +0200151
Imre Kis0fc366e2023-08-10 10:51:01 +0200152 rpc_status = rpc_caller_session_find_and_open(&session, &caller, &its_uuid, 4096);
153 if (rpc_status != RPC_SUCCESS) {
154 EMSG("Failed to open RPC session");
Imre Kis43dc08e2022-07-05 16:44:03 +0200155 goto fatal_error;
156 }
Balint Dobszay72c3f042020-11-23 18:23:57 +0100157
Imre Kis0fc366e2023-08-10 10:51:01 +0200158 storage_backend = secure_storage_client_init(&secure_storage_client, &session);
Imre Kis43dc08e2022-07-05 16:44:03 +0200159 if (!storage_backend) {
160 EMSG("Failed to initialize secure storage client");
161 goto fatal_error;
162 }
163
164 psa_status = psa_its_frontend_init(storage_backend);
165 if (psa_status != PSA_SUCCESS) {
166 EMSG("Failed init PSA ITS frontend: %d", psa_status);
167 goto fatal_error;
168 }
Balint Dobszay72c3f042020-11-23 18:23:57 +0100169
170 /*
171 * This is not thorough testing of the ITS SP!
172 * Only some basic functionality checks.
173 */
174 run_its_test();
175
Imre Kis0fc366e2023-08-10 10:51:01 +0200176 rpc_status = rpc_caller_session_close(&session);
177 if (rpc_status != RPC_SUCCESS) {
178 EMSG("Failed to close RPC session");
179 goto fatal_error;
180 }
181
182 rpc_status = ts_rpc_caller_sp_deinit(&caller);
183 if (rpc_status != RPC_SUCCESS) {
184 EMSG("Failed to close RPC caller");
Imre Kis43dc08e2022-07-05 16:44:03 +0200185 goto fatal_error;
Balint Dobszay72c3f042020-11-23 18:23:57 +0100186 }
187
188 /* End of boot phase */
Imre Kis76e8a3c2021-04-16 16:54:17 +0200189 sp_msg_wait(&req_msg);
Balint Dobszay72c3f042020-11-23 18:23:57 +0100190
Imre Kis43dc08e2022-07-05 16:44:03 +0200191fatal_error:
Balint Dobszay72c3f042020-11-23 18:23:57 +0100192 EMSG("Test SP error");
193 while (1) {}
194}
195
196void sp_interrupt_handler(uint32_t interrupt_id) {
197 (void)interrupt_id;
198}