forked from trusty-ia/trusty_app_storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tipc_ns.c
338 lines (283 loc) · 6.91 KB
/
tipc_ns.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <uapi/err.h>
#include <lk/compiler.h>
#include <stdint.h>
#include <string.h>
#include <trusty_ipc.h>
#include <interface/storage/storage.h>
#include "ipc.h"
#include "rpmb.h"
#include "tipc_ns.h"
#define SS_ERR(args...) fprintf(stderr, "ss: " args)
#define SS_WARN(args...) fprintf(stderr, "ss: " args)
#define SS_DBG_IO(args...)
static inline int translate_error(enum storage_err err) {
switch (err) {
case STORAGE_NO_ERROR:
return NO_ERROR;
case STORAGE_ERR_NOT_VALID:
return ERR_NOT_VALID;
case STORAGE_ERR_GENERIC:
default:
return ERR_GENERIC;
}
}
static inline int check_response(enum storage_cmd cmd, struct storage_msg *msg,
size_t read_len)
{
if (read_len < sizeof(*msg)) {
SS_ERR("%s: invalid response size %ld\n", __func__, read_len);
return ERR_IO;
}
if ((enum storage_cmd)msg->cmd != (cmd | STORAGE_RESP_BIT)) {
SS_ERR("%s: invalid response (%d) for cmd (%d)\n", __func__,
msg->cmd, cmd);
return ERR_IO;
}
return translate_error(msg->result);
}
int rpmb_send(void *handle_,
void *reliable_write_buf, size_t reliable_write_size,
void *write_buf, size_t write_size,
void *read_buf, size_t read_size, bool sync)
{
SS_DBG_IO("%s: handle %p, rel_write size %zu, write size %zu, read size %zu\n",
__func__, handle_, reliable_write_size, write_size, read_size);
int rc;
handle_t *handlep = handle_;
handle_t handle = *handlep;
struct storage_rpmb_send_req req = {
.read_size = read_size,
.reliable_write_size = reliable_write_size,
.write_size = write_size,
};
struct storage_msg msg = {
.cmd = STORAGE_RPMB_SEND,
.flags = sync ? (STORAGE_MSG_FLAG_PRE_COMMIT | STORAGE_MSG_FLAG_POST_COMMIT) : 0,
.size = sizeof(msg) + sizeof(req) + reliable_write_size + write_size,
};
iovec_t rx_iov[] = {
{
.base = &msg,
.len = sizeof(msg),
},
{
.base = read_buf,
.len = read_size,
},
};
iovec_t tx_iov[] = {
{
.base = &msg,
.len = sizeof(msg),
},
{
.base = &req,
.len = sizeof(req),
},
{
.base = reliable_write_buf,
.len = reliable_write_size,
},
{
.base = write_buf,
.len = write_size
}
};
rc = sync_ipc_send_msg(handle, tx_iov, countof(tx_iov),
rx_iov, countof(rx_iov));
if (rc < 0) {
SS_ERR("%s: rpmb send failed, ipc failure: %d\n", __func__, rc);
return rc;
}
return check_response(STORAGE_RPMB_SEND, &msg, rc);
}
int ns_open_file(handle_t ipc_handle, const char *fname,
ns_handle_t *handlep, bool create)
{
SS_DBG_IO("%s: open %s create: %d\n", __func__, fname, create);
size_t fname_size = strlen(fname);
struct storage_file_open_resp resp;
struct storage_file_open_req req = {
.flags = create ? STORAGE_FILE_OPEN_CREATE : 0,
};
struct storage_msg msg = {
.cmd = STORAGE_FILE_OPEN,
.size = sizeof(msg) + sizeof(req) + fname_size,
};
iovec_t tx_iov[] = {
{
.base = &msg,
.len = sizeof(msg),
},
{
.base = &req,
.len = sizeof(req),
},
{
// TODO: change to const API when available
.base = (char *) fname,
.len = fname_size,
}
};
iovec_t rx_iov[] = {
{
.base = &msg,
.len = sizeof(msg),
},
{
.base = &resp,
.len = sizeof(resp),
}
};
int rc = sync_ipc_send_msg(ipc_handle, tx_iov, countof(tx_iov),
rx_iov, countof(rx_iov));
if (rc < 0) {
SS_ERR("%s: open failed, %d\n", __func__, rc);
return rc;
}
size_t bytes_read = (size_t) rc;
rc = check_response(STORAGE_FILE_OPEN, &msg, bytes_read);
if (rc != NO_ERROR) {
return rc;
}
if (bytes_read != sizeof(msg) + sizeof(resp)) {
SS_ERR("%s: open failed, invalid response size (%ld != %ld)\n",
__func__, bytes_read, sizeof(resp));
return ERR_NOT_VALID;
}
*handlep = resp.handle;
return NO_ERROR;
}
void ns_close_file(handle_t ipc_handle, ns_handle_t handle)
{
SS_DBG_IO("close handle: %llu\n", handle);
struct storage_file_close_req req = {
.handle = handle,
};
struct storage_msg msg = {
.cmd = STORAGE_FILE_CLOSE,
.size = sizeof(msg) + sizeof(req),
};
iovec_t iov[] = {
{
.base = &msg,
.len = sizeof(msg),
},
{
.base = &req,
.len = sizeof(req),
},
};
int rc = sync_ipc_send_msg(ipc_handle, iov, countof(iov), iov, 1);
if (rc < 0) {
SS_ERR("%s: close failed, %d\n", __func__, rc);
return;
}
rc = check_response(STORAGE_FILE_CLOSE, &msg, rc);
if (rc < 0) {
SS_ERR("%s: close failed, %d\n", __func__, rc);
}
}
int ns_read_pos(handle_t ipc_handle, ns_handle_t handle, ns_off_t pos, void *data, int data_size)
{
SS_DBG_IO("%s: handle %llu, pos %llu, size %d\n",
__func__, handle, pos, data_size);
struct storage_file_read_req req = {
.handle = handle,
.offset = pos,
.size = data_size,
};
struct storage_msg msg = {
.cmd = STORAGE_FILE_READ,
.size = sizeof(msg) + sizeof(req),
};
iovec_t tx_iov[] = {
{
.base = &msg,
.len = sizeof(msg),
},
{
.base = &req,
.len = sizeof(req),
},
};
iovec_t rx_iov[] = {
{
.base = &msg,
.len = sizeof(msg),
},
{
.base = data,
.len = data_size,
}
};
int rc = sync_ipc_send_msg(ipc_handle, tx_iov, countof(tx_iov),
rx_iov, countof(rx_iov));
if (rc < 0) {
SS_ERR("%s: read failed, %d\n", __func__, rc);
return rc;
}
if ((size_t) rc != sizeof(msg) + data_size) {
return ERR_NOT_VALID;
}
size_t data_len = rc;
rc = check_response(STORAGE_FILE_READ, &msg, data_len);
if (rc != NO_ERROR) {
return rc;
}
return data_len - sizeof(msg);
}
int ns_write_pos(handle_t ipc_handle, ns_handle_t handle, ns_off_t pos,
const void *data, int data_size)
{
SS_DBG_IO("%s: handle %llu, pos %llu, size %d\n",
__func__, handle, pos, data_size);
struct storage_file_write_req req = {
.handle = handle,
.offset = pos,
};
struct storage_msg msg = {
.cmd = STORAGE_FILE_WRITE,
.size = sizeof(msg) + sizeof(req) + data_size,
};
iovec_t iov[] = {
{
.base = &msg,
.len = sizeof(msg),
},
{
.base = &req,
.len = sizeof(req),
},
{
// TODO: use const API
.base = (void *) data,
.len = data_size,
}
};
int rc = sync_ipc_send_msg(ipc_handle, iov, countof(iov), iov, 1);
if (rc < 0) {
SS_ERR("%s: write failed, %d\n", __func__, rc);
return rc;
}
rc = check_response(STORAGE_FILE_WRITE, &msg, rc);
if (rc != NO_ERROR) {
return rc;
}
return data_size;
}