-
Notifications
You must be signed in to change notification settings - Fork 189
/
sep.c
68 lines (54 loc) · 1.5 KB
/
sep.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
/* SPDX-License-Identifier: MIT */
#include <string.h>
#include "asc.h"
#include "sep.h"
#include "types.h"
#include "utils.h"
#define SEP_MSG_EP GENMASK(7, 0)
#define SEP_MSG_CMD GENMASK(23, 16)
#define SEP_MSG_DATA GENMASK(63, 32)
#define SEP_EP_ROM 0xff
#define SEP_MSG_GETRAND 16
#define SEP_REPLY_GETRAND 116
#define SEP_TIMEOUT 1000
static asc_dev_t *sep_asc = NULL;
int sep_init(void)
{
if (!sep_asc)
sep_asc = asc_init("/arm-io/sep");
if (!sep_asc)
return -1;
return 0;
}
size_t sep_get_random(void *buffer, size_t len)
{
const struct asc_message msg_getrand = {.msg0 = FIELD_PREP(SEP_MSG_EP, SEP_EP_ROM) |
FIELD_PREP(SEP_MSG_CMD, SEP_MSG_GETRAND)};
int ret;
size_t done = 0;
ret = sep_init();
if (ret)
return 0;
while (len) {
struct asc_message reply;
u32 rng;
size_t copy;
if (!asc_send(sep_asc, &msg_getrand))
return done;
if (!asc_recv_timeout(sep_asc, &reply, SEP_TIMEOUT))
return done;
if (FIELD_GET(SEP_MSG_CMD, reply.msg0) != SEP_REPLY_GETRAND) {
printf("SEP: unexpected getrand reply: %016lx\n", reply.msg0);
return done;
}
rng = FIELD_GET(SEP_MSG_DATA, reply.msg0);
copy = sizeof(rng);
if (copy > len)
copy = len;
memcpy(buffer, &rng, copy);
done += copy;
len -= copy;
buffer += copy;
}
return done;
}