1
2
3
7
8
9
10
11
17
24
25
29
30
31
32
36
37
38
39
40
41
42
43
44
45
46
47
54
55
56
60
61
62
63
64
65
66
67
68
75
76
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
110
111
112
113
114
115
116
117
118
120
121
122
123
128
129
131
132
133
134
135
136
137
138
139
140
141
145
146
147
148
149
150
151
152
153
155
159
160
164
165
169
170
174
175
176
177
178
179
180
181
182
183
185
186
187
188
189
190
191
192
193
195
196
197
198
199
200
201
202
203
204
208
209
213
214
215
216
217
218
219
220
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
256
257
258
259
261
265
266
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
308
309
310
311
312
313
317
318
319
320
321
322
323
324
325
326
327
328
329
330
334
335
336
337
338
339
341
342
343
344
345
346
347
348
349
350
351
352
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
380
381
382
383
384
385
386
387
388
389
393
394
395
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
433
434
438
439
447
448
449
450
451
452
453
454
455
456
461
462
463
467
468
469
470
471
472
473
474
475
476
477
478
479
480
484
485
486
487
488
489
490
491
492
493
494
495
496
497
508
509
510
511
512
513
517
518
519
520
521
522
523
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
573
580
587
593
600
601
602
603
604
605
606
607
608
609
615
622
626
627
628
629
630
631
632
633
634
635
636
640
641
642
646
647
648
649
650
651
652
653
657
658
659
660
661
662
663
664
671
672
673
674
675
676
677
678
679
680
681
684
685
686
687
688
689
690
691
692
693
/* ... */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <helper/log.h>
#include <helper/binarybuffer.h>
#include <helper/command.h>
#include <jtag/interface.h>
#include "libusb_helper.h"
5 includes
struct sequence {
int len;
void *tms;
void *tdo;
const void *tdi;
struct sequence *next;
...};
struct queue {
struct sequence *head;
struct sequence *tail;
...};
static struct sequence *queue_add_tail(struct queue *queue, int len)
{
if (len <= 0) {
LOG_ERROR("BUG: sequences with zero length are not allowed");
return NULL;
}if (len <= 0) { ... }
struct sequence *next;
next = malloc(sizeof(*next));
if (next) {
next->tms = calloc(1, DIV_ROUND_UP(len, 8));
if (next->tms) {
next->len = len;
next->tdo = NULL;
next->tdi = NULL;
next->next = NULL;
if (!queue->head) {
queue->head = next;
}if (!queue->head) { ... } else {
queue->tail->next = next;
}else { ... }
queue->tail = next;
}if (next->tms) { ... } else {
free(next);
next = NULL;
}else { ... }
}if (next) { ... }
if (!next)
LOG_ERROR("Not enough memory");
return next;
}{ ... }
static void queue_drop_head(struct queue *queue)
{
struct sequence *head = queue->head->next;
free(queue->head->tms);
free(queue->head);
queue->head = head;
}{ ... }
static void queue_free(struct queue *queue)
{
if (queue) {
while (queue->head)
queue_drop_head(queue);
free(queue);
}if (queue) { ... }
}{ ... }
static struct queue *queue_alloc(void)
{
struct queue *queue = malloc(sizeof(*queue));
if (queue)
queue->head = NULL;
else
LOG_ERROR("Not enough memory");
return queue;
}{ ... }
#define OSBDM_USB_BUFSIZE 64
#define OSBDM_USB_TIMEOUT 1000
#define OSBDM_USB_EP_WRITE 0x01
#define OSBDM_USB_EP_READ 0x82
#define OSBDM_CMD_INIT 0x11
/* ... */
#define OSBDM_CMD_SPECIAL 0x27
#define OSBDM_CMD_SPECIAL_SWAP 0x05
#define OSBDM_CMD_SPECIAL_SRST 0x01
#define OSBDM_SWAP_MAX (((OSBDM_USB_BUFSIZE - 6) / 5) * 16)
9 defines
/* ... */
static const uint16_t osbdm_vid[] = { 0x15a2, 0x15a2, 0x15a2, 0 };
static const uint16_t osbdm_pid[] = { 0x0042, 0x0058, 0x005e, 0 };
struct osbdm {
struct libusb_device_handle *devh;
uint8_t buffer[OSBDM_USB_BUFSIZE];
int count;
...};
/* ... */
static struct osbdm osbdm_context;
static int osbdm_send_and_recv(struct osbdm *osbdm)
{
int count, ret;
ret = jtag_libusb_bulk_write(osbdm->devh, OSBDM_USB_EP_WRITE,
(char *)osbdm->buffer, osbdm->count,
OSBDM_USB_TIMEOUT, &count);
if (ret || count != osbdm->count) {
LOG_ERROR("OSBDM communication error: can't write");
return ERROR_FAIL;
}if (ret || count != osbdm->count) { ... }
uint8_t cmd_saved = osbdm->buffer[0];
ret = jtag_libusb_bulk_read(osbdm->devh, OSBDM_USB_EP_READ,
(char *)osbdm->buffer, OSBDM_USB_BUFSIZE,
OSBDM_USB_TIMEOUT, &osbdm->count);
/* ... */
if (ret) {
LOG_ERROR("OSBDM communication error: can't read");
return ERROR_FAIL;
}if (ret) { ... }
if (osbdm->count < 2) {
LOG_ERROR("OSBDM communication error: reply too small");
return ERROR_FAIL;
}if (osbdm->count < 2) { ... }
if (osbdm->count != osbdm->buffer[1]) {
LOG_ERROR("OSBDM communication error: reply size mismatch");
return ERROR_FAIL;
}if (osbdm->count != osbdm->buffer[1]) { ... }
if (cmd_saved != osbdm->buffer[0]) {
LOG_ERROR("OSBDM communication error: reply command mismatch");
return ERROR_FAIL;
}if (cmd_saved != osbdm->buffer[0]) { ... }
return ERROR_OK;
}{ ... }
static int osbdm_srst(struct osbdm *osbdm, int srst)
{
osbdm->count = 0;
(void)memset(osbdm->buffer, 0, OSBDM_USB_BUFSIZE);
/* ... */
osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL;
osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL_SRST;
osbdm->buffer[osbdm->count++] = 0;
osbdm->buffer[osbdm->count++] = 0;
osbdm->buffer[osbdm->count++] = (srst ? 0 : 0x08);
/* ... */
if (osbdm_send_and_recv(osbdm) != ERROR_OK)
return ERROR_FAIL;
return ERROR_OK;
}{ ... }
static int osbdm_swap(struct osbdm *osbdm, void *tms, void *tdi,
void *tdo, int length)
{
if (length > OSBDM_SWAP_MAX) {
LOG_ERROR("BUG: bit sequence too long");
return ERROR_FAIL;
}if (length > OSBDM_SWAP_MAX) { ... }
if (length <= 0) {
LOG_ERROR("BUG: bit sequence equal or less than 0");
return ERROR_FAIL;
}if (length <= 0) { ... }
int swap_count = DIV_ROUND_UP(length, 16);
osbdm->count = 0;
(void)memset(osbdm->buffer, 0, OSBDM_USB_BUFSIZE);
/* ... */
osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL;
osbdm->buffer[osbdm->count++] = OSBDM_CMD_SPECIAL_SWAP;
osbdm->buffer[osbdm->count++] = 0;
osbdm->buffer[osbdm->count++] = 0;
osbdm->buffer[osbdm->count++] = 0;
osbdm->buffer[osbdm->count++] = (uint8_t)swap_count;
for (int bit_idx = 0; bit_idx < length; ) {
int bit_count = length - bit_idx;
if (bit_count > 16)
bit_count = 16;
osbdm->buffer[osbdm->count++] = (uint8_t)bit_count;
uint32_t tms_data = buf_get_u32(tms, bit_idx, bit_count);
uint32_t tdi_data = buf_get_u32(tdi, bit_idx, bit_count);
osbdm->buffer[osbdm->count++] = (uint8_t)(tdi_data >> 8);
osbdm->buffer[osbdm->count++] = (uint8_t)tdi_data;
osbdm->buffer[osbdm->count++] = (uint8_t)(tms_data >> 8);
osbdm->buffer[osbdm->count++] = (uint8_t)tms_data;
bit_idx += bit_count;
}for (int bit_idx = 0; bit_idx < length;) { ... }
assert(osbdm->count <= OSBDM_USB_BUFSIZE);
/* ... */
if (osbdm_send_and_recv(osbdm) != ERROR_OK)
return ERROR_FAIL;
/* ... */
if (((osbdm->buffer[2] << 8) | osbdm->buffer[3]) != 2 * swap_count) {
LOG_ERROR("OSBDM communication error: invalid swap command reply");
return ERROR_FAIL;
}if (((osbdm->buffer[2] << 8) | osbdm->buffer[3]) != 2 * swap_count) { ... }
/* ... */
uint8_t *buffer = osbdm->buffer + 4;
for (int bit_idx = 0; bit_idx < length; ) {
int bit_count = length - bit_idx;
if (bit_count > 16)
bit_count = 16;
uint32_t tdo_data = 0;
tdo_data |= (*buffer++) << 8;
tdo_data |= (*buffer++);
tdo_data >>= (16 - bit_count);
buf_set_u32(tdo, bit_idx, bit_count, tdo_data);
bit_idx += bit_count;
}for (int bit_idx = 0; bit_idx < length;) { ... }
return ERROR_OK;
}{ ... }
static int osbdm_flush(struct osbdm *osbdm, struct queue *queue)
{
uint8_t tms[DIV_ROUND_UP(OSBDM_SWAP_MAX, 8)];
uint8_t tdi[DIV_ROUND_UP(OSBDM_SWAP_MAX, 8)];
uint8_t tdo[DIV_ROUND_UP(OSBDM_SWAP_MAX, 8)];
int seq_back_len = 0;
while (queue->head) {
(void)memset(tms, 0, sizeof(tms));
(void)memset(tdi, 0, sizeof(tdi));
(void)memset(tdo, 0, sizeof(tdo));
int seq_len;
int swap_len;
struct sequence *seq;
/* ... */
seq = queue->head;
seq_len = seq_back_len;
swap_len = 0;
while (seq && swap_len != OSBDM_SWAP_MAX) {
/* ... */
int len = seq->len - seq_len;
if (len > OSBDM_SWAP_MAX - swap_len)
len = OSBDM_SWAP_MAX - swap_len;
buf_set_buf(seq->tms, seq_len, tms, swap_len, len);
if (seq->tdi)
buf_set_buf(seq->tdi, seq_len, tdi, swap_len, len);
swap_len += len;
seq_len += len;
if (seq_len == seq->len) {
seq = seq->next;
seq_len = 0;
}if (seq_len == seq->len) { ... }
}while (seq && swap_len != OSBDM_SWAP_MAX) { ... }
if (osbdm_swap(osbdm, tms, tdi, tdo, swap_len))
return ERROR_FAIL;
/* ... */
for (int swap_back_len = 0; swap_back_len < swap_len; ) {
int len = queue->head->len - seq_back_len;
if (len > swap_len - swap_back_len)
len = swap_len - swap_back_len;
if (queue->head->tdo)
buf_set_buf(tdo, swap_back_len, queue->head->tdo, seq_back_len, len);
swap_back_len += len;
seq_back_len += len;
if (seq_back_len == queue->head->len) {
queue_drop_head(queue);
seq_back_len = 0;
}if (seq_back_len == queue->head->len) { ... }
}for (int swap_back_len = 0; swap_back_len < swap_len;) { ... }
}while (queue->head) { ... }
return ERROR_OK;
}{ ... }
static int osbdm_open(struct osbdm *osbdm)
{
(void)memset(osbdm, 0, sizeof(*osbdm));
if (jtag_libusb_open(osbdm_vid, osbdm_pid, NULL, &osbdm->devh, NULL) != ERROR_OK)
return ERROR_FAIL;
if (libusb_claim_interface(osbdm->devh, 0) != ERROR_OK)
return ERROR_FAIL;
return ERROR_OK;
}{ ... }
static int osbdm_quit(void)
{
jtag_libusb_close(osbdm_context.devh);
return ERROR_OK;
}{ ... }
static int osbdm_add_pathmove(
struct queue *queue,
tap_state_t *path,
unsigned int num_states)
{
assert(num_states <= 32);
struct sequence *next = queue_add_tail(queue, num_states);
if (!next) {
LOG_ERROR("BUG: can't allocate bit sequence");
return ERROR_FAIL;
}if (!next) { ... }
uint32_t tms = 0;
for (unsigned int i = 0; i < num_states; i++) {
if (tap_state_transition(tap_get_state(), 1) == path[i]) {
tms |= (1 << i);
}if (tap_state_transition(tap_get_state(), 1) == path[i]) { ... } else if (tap_state_transition(tap_get_state(), 0) == path[i]) {
tms &= ~(1 << i);
}else if (tap_state_transition(tap_get_state(), 0) == path[i]) { ... } else {
LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition",
tap_state_name(tap_get_state()),
tap_state_name(path[i]));
return ERROR_FAIL;
}else { ... }
tap_set_state(path[i]);
}for (unsigned int i = 0; i < num_states; i++) { ... }
buf_set_u32(next->tms, 0, num_states, tms);
tap_set_end_state(tap_get_state());
return ERROR_OK;
}{ ... }
static int osbdm_add_statemove(
struct queue *queue,
tap_state_t new_state,
int skip_first)
{
int len = 0;
int tms = 0;
tap_set_end_state(new_state);
if (tap_get_end_state() == TAP_RESET) {
tms = 0xff;
len = 5;
}if (tap_get_end_state() == TAP_RESET) { ... } else if (tap_get_state() != tap_get_end_state()) {
tms = tap_get_tms_path(tap_get_state(), new_state);
len = tap_get_tms_path_len(tap_get_state(), new_state);
}else if (tap_get_state() != tap_get_end_state()) { ... }
if (len && skip_first) {
len--;
tms >>= 1;
}if (len && skip_first) { ... }
if (len) {
struct sequence *next = queue_add_tail(queue, len);
if (!next) {
LOG_ERROR("BUG: can't allocate bit sequence");
return ERROR_FAIL;
}if (!next) { ... }
buf_set_u32(next->tms, 0, len, tms);
}if (len) { ... }
tap_set_state(tap_get_end_state());
return ERROR_OK;
}{ ... }
static int osbdm_add_stableclocks(
struct queue *queue,
unsigned int count)
{
if (!tap_is_state_stable(tap_get_state())) {
LOG_ERROR("BUG: current state (%s) is not stable",
tap_state_name(tap_get_state()));
return ERROR_FAIL;
}if (!tap_is_state_stable(tap_get_state())) { ... }
struct sequence *next = queue_add_tail(queue, count);
if (!next) {
LOG_ERROR("BUG: can't allocate bit sequence");
return ERROR_FAIL;
}if (!next) { ... }
if (tap_get_state() == TAP_RESET)
(void)memset(next->tms, 0xff, DIV_ROUND_UP(count, 8));
return ERROR_OK;
}{ ... }
static int osbdm_add_tms(
struct queue *queue,
const uint8_t *tms,
int num_bits)
{
struct sequence *next = queue_add_tail(queue, num_bits);
if (!next) {
LOG_ERROR("BUG: can't allocate bit sequence");
return ERROR_FAIL;
}if (!next) { ... }
buf_set_buf(tms, 0, next->tms, 0, num_bits);
return ERROR_OK;
}{ ... }
static int osbdm_add_scan(
struct queue *queue,
struct scan_field *fields,
unsigned int num_fields,
tap_state_t end_state,
bool ir_scan)
{
if (ir_scan) {
if (tap_get_state() != TAP_IRSHIFT) {
if (osbdm_add_statemove(queue, TAP_IRSHIFT, 0) != ERROR_OK)
return ERROR_FAIL;
}if (tap_get_state() != TAP_IRSHIFT) { ... }
}if (ir_scan) { ... } else {
if (tap_get_state() != TAP_DRSHIFT) {
if (osbdm_add_statemove(queue, TAP_DRSHIFT, 0) != ERROR_OK)
return ERROR_FAIL;
}if (tap_get_state() != TAP_DRSHIFT) { ... }
}else { ... }
tap_set_end_state(end_state);
for (unsigned int idx = 0; idx < num_fields; idx++) {
struct sequence *next = queue_add_tail(queue, fields[idx].num_bits);
if (!next) {
LOG_ERROR("Can't allocate bit sequence");
return ERROR_FAIL;
}if (!next) { ... }
(void)memset(next->tms, 0, DIV_ROUND_UP(fields[idx].num_bits, 8));
next->tdi = fields[idx].out_value;
next->tdo = fields[idx].in_value;
}for (unsigned int idx = 0; idx < num_fields; idx++) { ... }
/* ... */
if (tap_get_state() != tap_get_end_state()) {
buf_set_u32(queue->tail->tms, queue->tail->len - 1, 1, 1);
if (osbdm_add_statemove(queue, tap_get_end_state(), 1) != ERROR_OK)
return ERROR_FAIL;
}if (tap_get_state() != tap_get_end_state()) { ... }
return ERROR_OK;
}{ ... }
static int osbdm_add_runtest(
struct queue *queue,
unsigned int num_cycles,
tap_state_t end_state)
{
if (osbdm_add_statemove(queue, TAP_IDLE, 0) != ERROR_OK)
return ERROR_FAIL;
if (osbdm_add_stableclocks(queue, num_cycles) != ERROR_OK)
return ERROR_FAIL;
if (osbdm_add_statemove(queue, end_state, 0) != ERROR_OK)
return ERROR_FAIL;
return ERROR_OK;
}{ ... }
static int osbdm_execute_command(
struct osbdm *osbdm,
struct queue *queue,
struct jtag_command *cmd)
{
int retval = ERROR_OK;
switch (cmd->type) {
case JTAG_RESET:
if (cmd->cmd.reset->trst) {
LOG_ERROR("BUG: nTRST signal is not supported");
retval = ERROR_FAIL;
}if (cmd->cmd.reset->trst) { ... } else {
retval = osbdm_flush(osbdm, queue);
if (retval == ERROR_OK)
retval = osbdm_srst(osbdm, cmd->cmd.reset->srst);
}else { ... }
break;
case JTAG_RESET:
case JTAG_PATHMOVE:
retval = osbdm_add_pathmove(
queue,
cmd->cmd.pathmove->path,
cmd->cmd.pathmove->num_states);
break;
case JTAG_PATHMOVE:
case JTAG_TLR_RESET:
retval = osbdm_add_statemove(
queue,
cmd->cmd.statemove->end_state,
0);
break;
case JTAG_TLR_RESET:
case JTAG_STABLECLOCKS:
retval = osbdm_add_stableclocks(
queue,
cmd->cmd.stableclocks->num_cycles);
break;
case JTAG_STABLECLOCKS:
case JTAG_TMS:
retval = osbdm_add_tms(
queue,
cmd->cmd.tms->bits,
cmd->cmd.tms->num_bits);
break;
case JTAG_TMS:
case JTAG_SCAN:
retval = osbdm_add_scan(
queue,
cmd->cmd.scan->fields,
cmd->cmd.scan->num_fields,
cmd->cmd.scan->end_state,
cmd->cmd.scan->ir_scan);
break;
case JTAG_SCAN:
case JTAG_SLEEP:
retval = osbdm_flush(osbdm, queue);
if (retval == ERROR_OK)
jtag_sleep(cmd->cmd.sleep->us);
break;
case JTAG_SLEEP:
case JTAG_RUNTEST:
retval = osbdm_add_runtest(
queue,
cmd->cmd.runtest->num_cycles,
cmd->cmd.runtest->end_state);
break;
case JTAG_RUNTEST:
default:
LOG_ERROR("BUG: unknown JTAG command type encountered");
retval = ERROR_FAIL;
break;default
}switch (cmd->type) { ... }
return retval;
}{ ... }
static int osbdm_execute_queue(struct jtag_command *cmd_queue)
{
int retval = ERROR_OK;
struct queue *queue = queue_alloc();
if (!queue) {
LOG_ERROR("BUG: can't allocate bit queue");
retval = ERROR_FAIL;
}if (!queue) { ... } else {
struct jtag_command *cmd = cmd_queue;
while (retval == ERROR_OK && cmd) {
retval = osbdm_execute_command(&osbdm_context, queue, cmd);
cmd = cmd->next;
}while (retval == ERROR_OK && cmd) { ... }
if (retval == ERROR_OK)
retval = osbdm_flush(&osbdm_context, queue);
queue_free(queue);
}else { ... }
if (retval != ERROR_OK) {
LOG_ERROR("FATAL: can't execute jtag command");
exit(-1);
}if (retval != ERROR_OK) { ... }
return retval;
}{ ... }
static int osbdm_init(void)
{
if (osbdm_open(&osbdm_context) != ERROR_OK) {
LOG_ERROR("Can't open OSBDM device");
return ERROR_FAIL;
}if (osbdm_open(&osbdm_context) != ERROR_OK) { ... } else {
LOG_DEBUG("OSBDM init");
}else { ... }
osbdm_context.count = 0;
osbdm_context.buffer[osbdm_context.count++] = OSBDM_CMD_INIT;
if (osbdm_send_and_recv(&osbdm_context) != ERROR_OK)
return ERROR_FAIL;
return ERROR_OK;
}{ ... }
static struct jtag_interface osbdm_interface = {
.execute_queue = osbdm_execute_queue,
...};
struct adapter_driver osbdm_adapter_driver = {
.name = "osbdm",
.transports = jtag_only,
.init = osbdm_init,
.quit = osbdm_quit,
.jtag_ops = &osbdm_interface,
...};