forked from fantomgs/xvcd-jlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xvcd.c
414 lines (358 loc) · 10.3 KB
/
xvcd.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// xilinx virtual cable demon for jlink jtag
// use connection string in Impact and another
// xilinx_xvc host=localhost:2542 disableversioncheck=true
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <winsock2.h>
#include <time.h>
#include "io_jlink.h"
#include "JlinkUtil.h"
//
// Listen on port 2542.
//
#define DEFAULT_PORT 2542
#define DEFAULT_JTAG_SPEED 1000 // 1 Mhz
static int jtag_state;
static int verbose;
//
// JTAG state machine.
//
enum {
test_logic_reset, run_test_idle,
select_dr_scan, capture_dr, shift_dr,
exit1_dr, pause_dr, exit2_dr, update_dr,
select_ir_scan, capture_ir, shift_ir,
exit1_ir, pause_ir, exit2_ir, update_ir,
num_states
};
static int jtag_step(int state, int tms) {
static const int next_state[num_states][2] =
{
[test_logic_reset] = {run_test_idle, test_logic_reset},
[run_test_idle] = {run_test_idle, select_dr_scan},
[select_dr_scan] = {capture_dr, select_ir_scan},
[capture_dr] = {shift_dr, exit1_dr},
[shift_dr] = {shift_dr, exit1_dr},
[exit1_dr] = {pause_dr, update_dr},
[pause_dr] = {pause_dr, exit2_dr},
[exit2_dr] = {shift_dr, update_dr},
[update_dr] = {run_test_idle, select_dr_scan},
[select_ir_scan] = {capture_ir, test_logic_reset},
[capture_ir] = {shift_ir, exit1_ir},
[shift_ir] = {shift_ir, exit1_ir},
[exit1_ir] = {pause_ir, update_ir},
[pause_ir] = {pause_ir, exit2_ir},
[exit2_ir] = {shift_ir, update_ir},
[update_ir] = {run_test_idle, select_dr_scan}
};
return next_state[state][tms];
}
static int sread(SOCKET fd, void *target, int len) {
char *t = target;
while (len) {
int r = recv(fd, t, len, 0);
if (r == SOCKET_ERROR || r == 0) {
int err = WSAGetLastError();
printf( "Recv failed with error: %d\n", err );
if(r == 0) {
puts("Seems like connection was gracefully closed. Exiting");
}
return r;
}
t += r;
len -= r;
}
return 1;
}
//
// handle_data(fd) handles JTAG shift instructions.
// To allow multiple programs to access the JTAG chain
// at the same time, we only allow switching between
// different clients only when we're in run_test_idle
// after going test_logic_reset. This ensures that one
// client can't disrupt the other client's IR or state.
//
int handle_data(SOCKET fd) {
int i;
int seen_tlr = 0;
const char xvcInfo[] = "xvcServer_v1.0:2048\n";
do {
char cmd[16];
unsigned char buffer[8192], result[1024];
memset(cmd, 0, 16);
if (sread(fd, cmd, 2) != 1)
return 1;
if (memcmp(cmd, "ge", 2) == 0) {
if (sread(fd, cmd, 6) != 1)
return 1;
memcpy(result, xvcInfo, strlen(xvcInfo));
if (send(fd, result, strlen(xvcInfo), 0) != strlen(xvcInfo)) {
perror("write1");
return 1;
}
if (verbose) {
printf("%u : Received command: 'getinfo'\n", (int)time(NULL));
printf("\t Replied with %s\n", xvcInfo);
}
break;
} else if (memcmp(cmd, "se", 2) == 0) {
if (sread(fd, cmd, 9) != 1)
return 1;
memcpy(result, cmd + 5, 4);
if (send(fd, result, 4, 0) != 4) {
perror("write2");
return 1;
}
if (verbose) {
printf("%u : Received command: 'settck'\n", (int)time(NULL));
printf("\t Replied with '%.*s'\n\n", 4, cmd + 5);
}
break;
} else if (memcmp(cmd, "sh", 2) == 0) {
if (sread(fd, cmd, 4) != 1)
return 1;
if (verbose) {
printf("%u : Received command: 'shift'\n", (int)time(NULL));
}
} else {
fprintf(stderr, "invalid cmd '%s'\n", cmd);
return 1;
}
unsigned int len;
if (sread(fd, &len, 4) != 1) {
fprintf(stderr, "reading length failed\n");
return 1;
}
unsigned int nr_bytes = (len + 7) / 8;
if (nr_bytes * 2 > sizeof(buffer)) {
fprintf(stderr, "buffer size exceeded %d[%d]\n", nr_bytes, sizeof(buffer));
return 1;
}
if (sread(fd, buffer, nr_bytes * 2) != 1) {
fprintf(stderr, "reading data failed\n");
return 1;
}
memset(result, 0, nr_bytes);
if (verbose) {
printf("#");
for (i = 0; i < nr_bytes * 2; ++i)
printf("%02x ", buffer[i]);
printf("\n");
}
//
// Only allow exiting if the state is rti and the IR
// has the default value (IDCODE) by going through test_logic_reset.
// As soon as going through capture_dr or capture_ir no exit is
// allowed as this will change DR/IR.
//
seen_tlr = (seen_tlr || jtag_state == test_logic_reset) && (jtag_state != capture_dr) && (jtag_state != capture_ir);
//
// Due to a weird bug(??) xilinx impacts goes through another "capture_ir"/"capture_dr" cycle after
// reading IR/DR which unfortunately sets IR to the read-out IR value.
// Just ignore these transactions.
//
if ((jtag_state == exit1_ir && len == 5 && buffer[0] == 0x17) ||
(jtag_state == exit1_dr && len == 4 && buffer[0] == 0x0b)) {
if (verbose)
printf("ignoring bogus jtag state movement in jtag_state %d\n", jtag_state);
} else {
for (i = 0; i < len; ++i) {
//
// Do the actual cycle.
//
int tms = !!(buffer[i / 8] & (1 << (i & 7)));
//
// Track the state.
//
jtag_state = jtag_step(jtag_state, tms);
}
if (io_scan(buffer, buffer + nr_bytes, result, len) < 0) {
fprintf(stderr, "io_scan failed\n");
exit(1);
}
if(verbose) {
printTDO(result, len);
}
}
if (send(fd, (const char*)result, nr_bytes, 0) != nr_bytes) {
printf("Send failed with error: %d\n", WSAGetLastError());
return 1;
}
if (verbose) {
printf("jtag state %d\n", jtag_state);
}
} while (!(seen_tlr && jtag_state == run_test_idle));
if(verbose) {
puts("exit handle_data");
}
return 0;
}
int main(int argc, char **argv) {
int ret = 0;
int i;
SOCKET s;
int c;
struct sockaddr_in address;
unsigned short port = DEFAULT_PORT;
unsigned short jtagSpeed = DEFAULT_JTAG_SPEED;
unsigned int coreId = 0;
opterr = 0;
while ((c = getopt(argc, argv, "vs:p:i:")) != -1)
switch (c) {
case 'v':
verbose = 1;
break;
case 's':
jtagSpeed = (unsigned short)strtoul(optarg, NULL, 0);
if(verbose) {
printf("Will use jtag speed %d kHz\n", jtagSpeed);
}
break;
case 'p':
port = (unsigned short)strtoul(optarg, NULL, 0);
if(verbose) {
printf("Will use port %d\n", port);
}
break;
case 'i':
coreId = strtoul(optarg, NULL, 0);
if(verbose) {
printf("Will search core id 0x%.8X\n", coreId);
}
break;
case '?':
fprintf(stderr, "usage: %s [-v] [-p port] [-s jtag_speed_in_kHz] [-i core_id]\n", *argv);
return 1;
}
if (io_init(jtagSpeed, coreId)) {
fprintf(stderr, "io_init failed\n");
return 1;
}
WSADATA wsaData;
int iResult;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
s = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (s == INVALID_SOCKET) {
printf("Error at socket(): %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
i = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&i, sizeof i);
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
address.sin_family = AF_INET;
iResult = bind(s, (struct sockaddr *) &address, sizeof(address));
if (iResult == SOCKET_ERROR) {
printf("Bind failed with error: %d\n", WSAGetLastError());
closesocket(s);
WSACleanup();
return 1;
}
if ( listen( s, SOMAXCONN ) == SOCKET_ERROR ) {
printf( "Listen failed with error: %d\n", WSAGetLastError() );
closesocket(s);
WSACleanup();
return 1;
}
fd_set conn;
int maxfd = 0;
FD_ZERO(&conn);
FD_SET(s, &conn);
maxfd = s;
if (verbose) {
puts("Started");
}
while (1) {
fd_set read = conn, except = conn;
SOCKET fd;
//
// Look for work to do.
//
if (select(maxfd + 1, &read, 0, &except, 0) == SOCKET_ERROR) {
printf( "Select failed with error: %d\n", WSAGetLastError() );
break;
}
for (fd = 0; fd <= maxfd; ++fd) {
if (FD_ISSET(fd, &read)) {
//
// Readable listen socket? Accept connection.
//
if (fd == s) {
SOCKET newfd;
int nsize = sizeof(address);
newfd = accept(s, (struct sockaddr*)&address, &nsize);
if (verbose) {
printf("connection accepted - fd %d\n", newfd);
}
if (newfd == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
} else {
puts("setting TCP_NODELAY to 1");
int flag = 1;
int optResult = setsockopt(newfd,
IPPROTO_TCP,
TCP_NODELAY,
(char *)&flag,
sizeof(flag));
if (optResult < 0) {
perror("TCP_NODELAY error");
}
if (newfd > maxfd) {
maxfd = newfd;
}
FD_SET(newfd, &conn);
}
}
//
// Otherwise, do work.
//
else {
int r;
if(verbose) {
puts("start handle_data");
}
r = handle_data(fd);
if(verbose) {
puts("stop handle_data");
}
if (r) {
//
// Close connection when required.
//
if (verbose)
printf("connection closed - fd %d\n", fd);
closesocket(fd);
FD_CLR(fd, &conn);
}
}
}
//
// Abort connection?
//
else if (FD_ISSET(fd, &except)) {
if (verbose) {
printf("connection aborted - fd %d\n", fd);
}
closesocket(fd);
FD_CLR(fd, &conn);
if (fd == s)
break;
}
}
}
closesocket(s);
WSACleanup();
//
// Un-map IOs.
//
io_close();
return 0;
}