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
|
/* SPDX-License-Identifier: MIT */
#include "uartproxy.h"
#include "assert.h"
#include "exception.h"
#include "iodev.h"
#include "proxy.h"
#include "string.h"
#include "types.h"
#include "utils.h"
#define REQ_SIZE 64
typedef struct {
u32 _pad;
u32 type;
union {
ProxyRequest prequest;
struct {
u64 addr;
u64 size;
u32 dchecksum;
} mrequest;
u64 features;
};
u32 checksum;
} UartRequest;
#define REPLY_SIZE 36
typedef struct {
u32 type;
s32 status;
union {
ProxyReply preply;
struct {
u32 dchecksum;
} mreply;
struct uartproxy_msg_start start;
u64 features;
};
u32 checksum;
u32 _dummy; // Not transferred
} UartReply;
typedef struct {
u32 type;
u16 len;
u16 event_type;
} UartEventHdr;
static_assert(sizeof(UartReply) == (REPLY_SIZE + 4), "Invalid UartReply size");
#define REQ_NOP 0x00AA55FF
#define REQ_PROXY 0x01AA55FF
#define REQ_MEMREAD 0x02AA55FF
#define REQ_MEMWRITE 0x03AA55FF
#define REQ_BOOT 0x04AA55FF
#define REQ_EVENT 0x05AA55FF
#define ST_OK 0
#define ST_BADCMD -1
#define ST_INVAL -2
#define ST_XFRERR -3
#define ST_CSUMERR -4
#define PROXY_FEAT_DISABLE_DATA_CSUMS 0x01
#define PROXY_FEAT_ALL (PROXY_FEAT_DISABLE_DATA_CSUMS)
static u32 iodev_proxy_buffer[IODEV_MAX];
#define CHECKSUM_INIT 0xDEADBEEF
#define CHECKSUM_FINAL 0xADDEDBAD
#define CHECKSUM_SENTINEL 0xD0DECADE
#define DATA_END_SENTINEL 0xB0CACC10
static bool disable_data_csums = false;
// I just totally pulled this out of my arse
// Noinline so that this can be bailed out by exc_guard = EXC_RETURN
// We assume this function does not use the stack
static u32 __attribute__((noinline)) checksum_block(void *start, u32 length, u32 init)
{
u32 sum = init;
u8 *d = (u8 *)start;
while (length--) {
sum *= 31337;
sum += (*d++) ^ 0x5A;
}
return sum;
}
static inline u32 checksum_start(void *start, u32 length)
{
return checksum_block(start, length, CHECKSUM_INIT);
}
static inline u32 checksum_add(void *start, u32 length, u32 sum)
{
return checksum_block(start, length, sum);
}
static inline u32 checksum_finish(u32 sum)
{
return sum ^ CHECKSUM_FINAL;
}
static inline u32 checksum(void *start, u32 length)
{
return checksum_finish(checksum_start(start, length));
}
static u64 data_checksum(void *start, u32 length)
{
if (disable_data_csums) {
return CHECKSUM_SENTINEL;
}
return checksum(start, length);
}
iodev_id_t uartproxy_iodev;
int uartproxy_run(struct uartproxy_msg_start *start)
{
int ret;
int running = 1;
size_t bytes;
u64 checksum_val;
u64 enabled_features = 0;
iodev_id_t iodev = IODEV_MAX;
UartRequest request;
UartReply reply = {REQ_BOOT};
if (!start) {
// Startup notification only goes out via UART
reply.checksum = checksum(&reply, REPLY_SIZE - 4);
iodev_write(IODEV_UART, &reply, REPLY_SIZE);
} else {
// Exceptions / hooks keep the current iodev
iodev = uartproxy_iodev;
reply.start = *start;
reply.checksum = checksum(&reply, REPLY_SIZE - 4);
iodev_write(iodev, &reply, REPLY_SIZE);
}
while (running) {
if (!start) {
// Look for commands from any iodev on startup
for (iodev = 0; iodev < IODEV_MAX;) {
u8 b;
if ((iodev_get_usage(iodev) & USAGE_UARTPROXY)) {
iodev_handle_events(iodev);
if (iodev_can_read(iodev) && iodev_read(iodev, &b, 1) == 1) {
iodev_proxy_buffer[iodev] >>= 8;
iodev_proxy_buffer[iodev] |= b << 24;
if ((iodev_proxy_buffer[iodev] & 0xffffff) == 0xAA55FF)
break;
}
}
iodev++;
if (iodev == IODEV_MAX)
iodev = 0;
}
} else {
// Stick to the current iodev for exceptions
do {
u8 b;
iodev_handle_events(iodev);
if (iodev_read(iodev, &b, 1) != 1) {
printf("Proxy: iodev read failed, exiting.\n");
return -1;
}
iodev_proxy_buffer[iodev] >>= 8;
iodev_proxy_buffer[iodev] |= b << 24;
} while ((iodev_proxy_buffer[iodev] & 0xffffff) != 0xAA55FF);
}
memset(&request, 0, sizeof(request));
request.type = iodev_proxy_buffer[iodev];
bytes = iodev_read(iodev, (&request.type) + 1, REQ_SIZE - 4);
if (bytes != REQ_SIZE - 4)
continue;
if (checksum(&(request.type), REQ_SIZE - 4) != request.checksum) {
memset(&reply, 0, sizeof(reply));
reply.type = request.type;
reply.status = ST_CSUMERR;
reply.checksum = checksum(&reply, REPLY_SIZE - 4);
iodev_write(iodev, &reply, REPLY_SIZE);
continue;
}
memset(&reply, 0, sizeof(reply));
reply.type = request.type;
reply.status = ST_OK;
uartproxy_iodev = iodev;
switch (request.type) {
case REQ_NOP:
enabled_features = request.features & PROXY_FEAT_ALL;
if (iodev == IODEV_UART) {
// Don't allow disabling checksums on UART
enabled_features &= ~PROXY_FEAT_DISABLE_DATA_CSUMS;
}
disable_data_csums = enabled_features & PROXY_FEAT_DISABLE_DATA_CSUMS;
reply.features = enabled_features;
break;
case REQ_PROXY:
ret = proxy_process(&request.prequest, &reply.preply);
if (ret != 0)
running = 0;
if (ret < 0)
printf("Proxy req error: %d\n", ret);
break;
case REQ_MEMREAD:
if (request.mrequest.size == 0)
break;
exc_count = 0;
exc_guard = GUARD_RETURN;
checksum_val = data_checksum((void *)request.mrequest.addr, request.mrequest.size);
exc_guard = GUARD_OFF;
if (exc_count)
reply.status = ST_XFRERR;
reply.mreply.dchecksum = checksum_val;
break;
case REQ_MEMWRITE:
exc_count = 0;
exc_guard = GUARD_SKIP;
if (request.mrequest.size != 0) {
// Probe for exception guard
// We can't do the whole buffer easily, because we'd drop UART data
write8(request.mrequest.addr, 0);
write8(request.mrequest.addr + request.mrequest.size - 1, 0);
}
exc_guard = GUARD_OFF;
if (exc_count) {
reply.status = ST_XFRERR;
break;
}
bytes = iodev_read(iodev, (void *)request.mrequest.addr, request.mrequest.size);
if (bytes != request.mrequest.size) {
reply.status = ST_XFRERR;
break;
}
checksum_val = data_checksum((void *)request.mrequest.addr, request.mrequest.size);
reply.mreply.dchecksum = checksum_val;
if (reply.mreply.dchecksum != request.mrequest.dchecksum) {
reply.status = ST_XFRERR;
break;
}
if (disable_data_csums) {
// Check the sentinel that should be present after the data
u32 sentinel = 0;
bytes = iodev_read(iodev, &sentinel, sizeof(sentinel));
if (bytes != sizeof(sentinel) || sentinel != DATA_END_SENTINEL) {
reply.status = ST_XFRERR;
break;
}
}
break;
default:
reply.status = ST_BADCMD;
break;
}
sysop("dsb sy");
sysop("isb");
reply.checksum = checksum(&reply, REPLY_SIZE - 4);
iodev_lock(uartproxy_iodev);
iodev_queue(iodev, &reply, REPLY_SIZE);
if ((request.type == REQ_MEMREAD) && (reply.status == ST_OK)) {
iodev_queue(iodev, (void *)request.mrequest.addr, request.mrequest.size);
if (disable_data_csums) {
// Since there is no checksum, put a sentinel after the data so the receiver
// can check that no packets were lost.
u32 sentinel = DATA_END_SENTINEL;
iodev_queue(iodev, &sentinel, sizeof(sentinel));
}
}
iodev_unlock(uartproxy_iodev);
// Flush all queued data
iodev_write(iodev, NULL, 0);
iodev_flush(iodev);
}
return ret;
}
void uartproxy_send_event(u16 event_type, void *data, u16 length)
{
UartEventHdr hdr;
u32 csum;
hdr.type = REQ_EVENT;
hdr.len = length;
hdr.event_type = event_type;
if (disable_data_csums) {
csum = CHECKSUM_SENTINEL;
} else {
csum = checksum_start(&hdr, sizeof(UartEventHdr));
csum = checksum_finish(checksum_add(data, length, csum));
}
iodev_lock(uartproxy_iodev);
iodev_queue(uartproxy_iodev, &hdr, sizeof(UartEventHdr));
iodev_queue(uartproxy_iodev, data, length);
iodev_write(uartproxy_iodev, &csum, sizeof(csum));
iodev_unlock(uartproxy_iodev);
}
|