svf.c 42.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Copyright (C) 2009 by Simon Qian
 * SimonQian@SimonQian.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33


/* The specification for SVF is available here:
 * http://www.asset-intertech.com/support/svf.pdf
 * Below, this document is refered to as the "SVF spec".
 *
 * The specification for XSVF is available here:
 * http://www.xilinx.com/support/documentation/application_notes/xapp503.pdf
 * Below, this document is refered to as the "XSVF spec".
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

34
#include <jtag/jtag.h>
David Brownell's avatar
David Brownell committed
35
#include "svf.h"
36
#include <helper/time_support.h>
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57


// SVF command
typedef enum
{
	ENDDR,
	ENDIR,
	FREQUENCY,
	HDR,
	HIR,
	PIO,
	PIOMAP,
	RUNTEST,
	SDR,
	SIR,
	STATE,
	TDR,
	TIR,
	TRST,
}svf_command_t;

58
static const char *svf_command_name[14] =
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
{
	"ENDDR",
	"ENDIR",
	"FREQUENCY",
	"HDR",
	"HIR",
	"PIO",
	"PIOMAP",
	"RUNTEST",
	"SDR",
	"SIR",
	"STATE",
	"TDR",
	"TIR",
	"TRST"
};

typedef enum
{
	TRST_ON,
	TRST_OFF,
	TRST_Z,
	TRST_ABSENT
}trst_mode_t;

84
static const char *svf_trst_mode_name[4] =
85
86
87
88
89
90
91
{
	"ON",
	"OFF",
	"Z",
	"ABSENT"
};

92
struct svf_statemove
93
94
95
{
	tap_state_t from;
	tap_state_t to;
96
	uint32_t num_of_moves;
97
	tap_state_t paths[8];
98
};
99

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * These paths are from the SVF specification for the STATE command, to be
 * used when the STATE command only includes the final state.  The first
 * element of the path is the "from" (current) state, and the last one is
 * the "to" (target) state.
 *
 * All specified paths are the shortest ones in the JTAG spec, and are thus
 * not (!!) exact matches for the paths used elsewhere in OpenOCD.  Note
 * that PAUSE-to-PAUSE transitions all go through UPDATE and then CAPTURE,
 * which has specific effects on the various registers; they are not NOPs.
 *
 * Paths to RESET are disabled here.  As elsewhere in OpenOCD, and in XSVF
 * and many SVF implementations, we don't want to risk missing that state.
 * To get to RESET, always we ignore the current state.
 */
115
static const struct svf_statemove svf_statemoves[] =
116
117
118
119
120
121
{
	// from			to				num_of_moves,	paths[8]
//	{TAP_RESET,		TAP_RESET,		1,				{TAP_RESET}},
	{TAP_RESET,		TAP_IDLE,		2,				{TAP_RESET, TAP_IDLE}},
	{TAP_RESET,		TAP_DRPAUSE,	6,				{TAP_RESET, TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
	{TAP_RESET,		TAP_IRPAUSE,	7,				{TAP_RESET, TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
122

123
124
125
126
//	{TAP_IDLE,		TAP_RESET,		4,				{TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
	{TAP_IDLE,		TAP_IDLE,		1,				{TAP_IDLE}},
	{TAP_IDLE,		TAP_DRPAUSE,	5,				{TAP_IDLE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
	{TAP_IDLE,		TAP_IRPAUSE,	6,				{TAP_IDLE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
127

128
129
130
131
//	{TAP_DRPAUSE,	TAP_RESET,		6,				{TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
	{TAP_DRPAUSE,	TAP_IDLE,		4,				{TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_IDLE}},
	{TAP_DRPAUSE,	TAP_DRPAUSE,	7,				{TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
	{TAP_DRPAUSE,	TAP_IRPAUSE,	8,				{TAP_DRPAUSE, TAP_DREXIT2, TAP_DRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}},
132

133
134
135
136
137
138
//	{TAP_IRPAUSE,	TAP_RESET,		6,				{TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_RESET}},
	{TAP_IRPAUSE,	TAP_IDLE,		4,				{TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_IDLE}},
	{TAP_IRPAUSE,	TAP_DRPAUSE,	7,				{TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_DRCAPTURE, TAP_DREXIT1, TAP_DRPAUSE}},
	{TAP_IRPAUSE,	TAP_IRPAUSE,	8,				{TAP_IRPAUSE, TAP_IREXIT2, TAP_IRUPDATE, TAP_DRSELECT, TAP_IRSELECT, TAP_IRCAPTURE, TAP_IREXIT1, TAP_IRPAUSE}}
};

139
140
141
142
143

#define XXR_TDI						(1 << 0)
#define XXR_TDO						(1 << 1)
#define XXR_MASK					(1 << 2)
#define XXR_SMASK					(1 << 3)
144
struct svf_xxr_para
145
146
147
{
	int len;
	int data_mask;
zwelch's avatar
zwelch committed
148
149
150
151
	uint8_t *tdi;
	uint8_t *tdo;
	uint8_t *mask;
	uint8_t *smask;
152
};
153

154
struct svf_para
155
156
157
158
159
160
161
162
{
	float frequency;
	tap_state_t ir_end_state;
	tap_state_t dr_end_state;
	tap_state_t runtest_run_state;
	tap_state_t runtest_end_state;
	trst_mode_t trst_mode;

163
164
165
166
167
168
169
	struct svf_xxr_para hir_para;
	struct svf_xxr_para hdr_para;
	struct svf_xxr_para tir_para;
	struct svf_xxr_para tdr_para;
	struct svf_xxr_para sir_para;
	struct svf_xxr_para sdr_para;
};
170

171
172
static struct svf_para svf_para;
static const struct svf_para svf_para_init =
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
{
//	frequency,	ir_end_state,	dr_end_state,	runtest_run_state,	runtest_end_state,	trst_mode
	0,			TAP_IDLE,		TAP_IDLE,		TAP_IDLE,			TAP_IDLE,			TRST_Z,
//	hir_para
//	{len,	data_mask,	tdi,	tdo,	mask,	smask},
	{0,		0,			NULL,	NULL,	NULL,	NULL},
//	hdr_para
//	{len,	data_mask,	tdi,	tdo,	mask,	smask},
	{0,		0,			NULL,	NULL,	NULL,	NULL},
//	tir_para
//	{len,	data_mask,	tdi,	tdo,	mask,	smask},
	{0,		0,			NULL,	NULL,	NULL,	NULL},
//	tdr_para
//	{len,	data_mask,	tdi,	tdo,	mask,	smask},
	{0,		0,			NULL,	NULL,	NULL,	NULL},
//	sir_para
//	{len,	data_mask,	tdi,	tdo,	mask,	smask},
	{0,		0,			NULL,	NULL,	NULL,	NULL},
//	sdr_para
//	{len,	data_mask,	tdi,	tdo,	mask,	smask},
	{0,		0,			NULL,	NULL,	NULL,	NULL},
};

196
struct svf_check_tdo_para
197
198
199
200
201
202
{
	int line_num;		// used to record line number of the check operation
						// so more information could be printed
	int enabled;		// check is enabled or not
	int buffer_offset;	// buffer_offset to buffers
	int bit_len;		// bit length to check
203
};
204
205

#define SVF_CHECK_TDO_PARA_SIZE	1024
206
static struct svf_check_tdo_para *svf_check_tdo_para = NULL;
207
208
static int svf_check_tdo_para_index = 0;

Andrew Leech's avatar
Andrew Leech committed
209
static int svf_read_command_from_file(FILE * fd);
210
static int svf_check_tdo(void);
zwelch's avatar
zwelch committed
211
static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len);
212
static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str);
213

Andrew Leech's avatar
Andrew Leech committed
214
215
216
static FILE * svf_fd = NULL;
static char * svf_read_line = NULL;
static size_t svf_read_line_size = 0;
217
static char *svf_command_buffer = NULL;
Andrew Leech's avatar
Andrew Leech committed
218
static size_t svf_command_buffer_size = 0;
219
static int svf_line_number = 1;
Andrew Leech's avatar
Andrew Leech committed
220
static int svf_getline (char **lineptr, size_t *n, FILE *stream);
221

222
#define SVF_MAX_BUFFER_SIZE_TO_COMMIT	(1024 * 1024)
zwelch's avatar
zwelch committed
223
static uint8_t *svf_tdi_buffer = NULL, *svf_tdo_buffer = NULL, *svf_mask_buffer = NULL;
224
static int svf_buffer_index = 0, svf_buffer_size = 0;
225
static int svf_quiet = 0;
226
static int svf_nil = 0;
227

Andrew Leech's avatar
Andrew Leech committed
228
229
230
231
232
233
234
235
236
// Targetting particular tap
static int svf_tap_is_specified = 0;
static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi);

// Progress Indicator
static int svf_progress_enabled = 0;
static long svf_total_lines = 0;
static int svf_percentage = 0;
static int svf_last_printed_percentage = -1;
237

238
static void svf_free_xxd_para(struct svf_xxr_para *para)
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
{
	if (NULL != para)
	{
		if (para->tdi != NULL)
		{
			free(para->tdi);
			para->tdi = NULL;
		}
		if (para->tdo != NULL)
		{
			free(para->tdo);
			para->tdo = NULL;
		}
		if (para->mask != NULL)
		{
			free(para->mask);
			para->mask = NULL;
		}
		if (para->smask != NULL)
		{
			free(para->smask);
			para->smask = NULL;
		}
	}
}

265
static unsigned svf_get_mask_u32(int bitlen)
zwelch's avatar
zwelch committed
266
{
267
	uint32_t bitmask;
zwelch's avatar
zwelch committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

	if (bitlen < 0)
	{
		bitmask = 0;
	}
	else if (bitlen >= 32)
	{
		bitmask = 0xFFFFFFFF;
	}
	else
	{
		bitmask = (1 << bitlen) - 1;
	}

	return bitmask;
}

David Brownell's avatar
David Brownell committed
285
int svf_add_statemove(tap_state_t state_to)
286
287
{
	tap_state_t state_from = cmd_queue_cur_state;
Øyvind Harboe's avatar
Øyvind Harboe committed
288
	unsigned index_var;
289

290
291
	/* when resetting, be paranoid and ignore current state */
	if (state_to == TAP_RESET) {
292
293
294
		if (svf_nil)
			return ERROR_OK;

295
296
297
298
		jtag_add_tlr();
		return ERROR_OK;
	}

Øyvind Harboe's avatar
Øyvind Harboe committed
299
	for (index_var = 0; index_var < ARRAY_SIZE(svf_statemoves); index_var++)
300
	{
Øyvind Harboe's avatar
Øyvind Harboe committed
301
302
		if ((svf_statemoves[index_var].from == state_from)
			&& (svf_statemoves[index_var].to == state_to))
303
		{
304
305
306
307
			if (svf_nil)
			{
				continue;
			}
308
			/* recorded path includes current state ... avoid extra TCKs! */
Øyvind Harboe's avatar
Øyvind Harboe committed
309
310
311
			if (svf_statemoves[index_var].num_of_moves > 1)
				jtag_add_pathmove(svf_statemoves[index_var].num_of_moves - 1,
						svf_statemoves[index_var].paths + 1);
312
			else
Øyvind Harboe's avatar
Øyvind Harboe committed
313
314
				jtag_add_pathmove(svf_statemoves[index_var].num_of_moves,
						svf_statemoves[index_var].paths);
315
316
317
			return ERROR_OK;
		}
	}
318
	LOG_ERROR("SVF: can not move to %s", tap_state_name(state_to));
319
320
321
	return ERROR_FAIL;
}

322
COMMAND_HANDLER(handle_svf_command)
323
{
Andrew Leech's avatar
Andrew Leech committed
324
325
#define SVF_MIN_NUM_OF_OPTIONS			1
#define SVF_MAX_NUM_OF_OPTIONS			5
326
	int command_num = 0;
327
	int ret = ERROR_OK;
Andrew Leech's avatar
Andrew Leech committed
328
329
330
331
332
333
334
335
	long long time_measure_ms;
	int time_measure_s, time_measure_m;

	/* use NULL to indicate a "plain" svf file which accounts for
	   any additional devices in the scan chain, otherwise the device
	   that should be affected
	*/
	struct jtag_tap *tap = NULL;
336

Andrew Leech's avatar
Andrew Leech committed
337
	if ((CMD_ARGC < SVF_MIN_NUM_OF_OPTIONS) || (CMD_ARGC > SVF_MAX_NUM_OF_OPTIONS))
338
	{
339
		return ERROR_COMMAND_SYNTAX_ERROR;
340
341
	}

Andrew Leech's avatar
Andrew Leech committed
342
	// parse command line
343
	svf_quiet = 0;
344
	svf_nil = 0;
Andrew Leech's avatar
Andrew Leech committed
345
	for (unsigned int i = 0; i < CMD_ARGC; i++)
346
	{
Andrew Leech's avatar
Andrew Leech committed
347
348
349
350
351
352
353
354
355
356
357
		if (strcmp(CMD_ARGV[i], "-tap") == 0)
		{
			tap = jtag_tap_by_string(CMD_ARGV[i+1]);
			if (!tap)
			{
				command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i+1]);
				return ERROR_FAIL;
			}
			i++;
		}
		else if ((strcmp(CMD_ARGV[i], "quiet") == 0) || (strcmp(CMD_ARGV[i], "-quiet") == 0))
358
359
360
		{
			svf_quiet = 1;
		}
361
362
363
364
		else if ((strcmp(CMD_ARGV[i], "nil") == 0) || (strcmp(CMD_ARGV[i], "-nil") == 0))
		{
			svf_nil = 1;
		}
Andrew Leech's avatar
Andrew Leech committed
365
		else if ((strcmp(CMD_ARGV[i], "progress") == 0) || (strcmp(CMD_ARGV[i], "-progress") == 0))
366
		{
Andrew Leech's avatar
Andrew Leech committed
367
368
369
370
371
372
			svf_progress_enabled = 1;
		}
		else if ((svf_fd = fopen(CMD_ARGV[i], "r")) == NULL)
		{
			int err = errno;
			command_print(CMD_CTX, "open(\"%s\"): %s", CMD_ARGV[i], strerror(err));
373
			// no need to free anything now
374
			return ERROR_COMMAND_SYNTAX_ERROR;
375
		}
Andrew Leech's avatar
Andrew Leech committed
376
377
378
379
		else
		{
			LOG_USER("svf processing file: \"%s\"", CMD_ARGV[i]);
		}
380
381
	}

Andrew Leech's avatar
Andrew Leech committed
382
	if (svf_fd == NULL)
383
	{
384
		return ERROR_COMMAND_SYNTAX_ERROR;
385
386
	}

387
	// get time
Andrew Leech's avatar
Andrew Leech committed
388
	time_measure_ms = timeval_ms();
389

390
391
392
393
394
	// init
	svf_line_number = 1;
	svf_command_buffer_size = 0;

	svf_check_tdo_para_index = 0;
395
	svf_check_tdo_para = malloc(sizeof(struct svf_check_tdo_para) * SVF_CHECK_TDO_PARA_SIZE);
396
397
398
399
400
401
402
403
404
	if (NULL == svf_check_tdo_para)
	{
		LOG_ERROR("not enough memory");
		ret = ERROR_FAIL;
		goto free_all;
	}

	svf_buffer_index = 0;
	// double the buffer size
Uwe Hermann's avatar
Uwe Hermann committed
405
	// in case current command cannot be committed, and next command is a bit scan command
406
407
	// here is 32K bits for this big scan command, it should be enough
	// buffer will be reallocated if buffer size is not enough
zwelch's avatar
zwelch committed
408
	svf_tdi_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
409
410
411
412
413
414
	if (NULL == svf_tdi_buffer)
	{
		LOG_ERROR("not enough memory");
		ret = ERROR_FAIL;
		goto free_all;
	}
zwelch's avatar
zwelch committed
415
	svf_tdo_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
416
417
418
419
420
421
	if (NULL == svf_tdo_buffer)
	{
		LOG_ERROR("not enough memory");
		ret = ERROR_FAIL;
		goto free_all;
	}
zwelch's avatar
zwelch committed
422
	svf_mask_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
423
424
425
426
427
428
429
430
431
	if (NULL == svf_mask_buffer)
	{
		LOG_ERROR("not enough memory");
		ret = ERROR_FAIL;
		goto free_all;
	}
	svf_buffer_size = 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT;

	memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
432

433
434
435
436
437
	if (!svf_nil)
	{
		// TAP_RESET
		jtag_add_tlr();
	}
438

Andrew Leech's avatar
Andrew Leech committed
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
	if (tap)
	{
		/* Tap is specified, set header/trailer paddings */
		int header_ir_len = 0, header_dr_len = 0, trailer_ir_len = 0, trailer_dr_len = 0;
		struct jtag_tap *check_tap;

		svf_tap_is_specified = 1;

		for (check_tap = jtag_all_taps(); check_tap; check_tap = check_tap->next_tap) {
			if (check_tap->abs_chain_position < tap->abs_chain_position)
			{
				//Header
				header_ir_len += check_tap->ir_length;
				header_dr_len ++;
			}
			else if (check_tap->abs_chain_position > tap->abs_chain_position)
			{
				//Trailer
				trailer_ir_len += check_tap->ir_length;
				trailer_dr_len ++;
			}
		}

		// HDR %d TDI (0)
		if (ERROR_OK != svf_set_padding(&svf_para.hdr_para, header_dr_len, 0))
		{
			LOG_ERROR("failed to set data header");
			return ERROR_FAIL;
		}

		// HIR %d TDI (0xFF)
		if (ERROR_OK != svf_set_padding(&svf_para.hir_para, header_ir_len, 0xFF))
		{
			LOG_ERROR("failed to set instruction header");
			return ERROR_FAIL;
		}

		// TDR %d TDI (0)
		if (ERROR_OK != svf_set_padding(&svf_para.tdr_para, trailer_dr_len, 0))
		{
			LOG_ERROR("failed to set data trailer");
			return ERROR_FAIL;
		}

		// TIR %d TDI (0xFF)
		if (ERROR_OK != svf_set_padding(&svf_para.tir_para, trailer_ir_len, 0xFF))
		{
			LOG_ERROR("failed to set instruction trailer");
			return ERROR_FAIL;
		}

	}

	if (svf_progress_enabled)
	{
		// Count total lines in file.
		while ( ! feof (svf_fd) )
		 {
		   svf_getline (&svf_command_buffer, &svf_command_buffer_size, svf_fd);
		   svf_total_lines++;
		 }
		rewind(svf_fd);
	}
502
	while (ERROR_OK == svf_read_command_from_file(svf_fd))
503
	{
Andrew Leech's avatar
Andrew Leech committed
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
		// Log Output
		if (svf_quiet)
		{
			if (svf_progress_enabled)
			{
				svf_percentage = ((svf_line_number * 20) / svf_total_lines) * 5;
				if (svf_last_printed_percentage != svf_percentage)
				{
					LOG_USER_N("\r%d%%    ", svf_percentage);
					svf_last_printed_percentage = svf_percentage;
				}
			}
		}
		else
		{
			if (svf_progress_enabled)
			{
				svf_percentage = ((svf_line_number * 20) / svf_total_lines) * 5;
				LOG_USER_N("%3d%%  %s", svf_percentage, svf_read_line);
			}
			else
			{
				LOG_USER_N("%s",svf_read_line);
			}
		}
			// Run Command
530
		if (ERROR_OK != svf_run_command(CMD_CTX, svf_command_buffer))
531
532
533
534
535
536
537
		{
			LOG_ERROR("fail to run command at line %d", svf_line_number);
			ret = ERROR_FAIL;
			break;
		}
		command_num++;
	}
538
539

	if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
540
541
542
543
544
545
546
547
	{
		ret = ERROR_FAIL;
	}
	else if (ERROR_OK != svf_check_tdo())
	{
		ret = ERROR_FAIL;
	}

548
	// print time
Andrew Leech's avatar
Andrew Leech committed
549
550
551
552
553
554
555
556
557
	time_measure_ms = timeval_ms() - time_measure_ms;
	time_measure_s = time_measure_ms / 1000;
	time_measure_ms %= 1000;
	time_measure_m = time_measure_s / 60;
	time_measure_s %= 60;
	if (time_measure_ms < 1000)
	{
		command_print(CMD_CTX, "\r\nTime used: %dm%ds%lldms ", time_measure_m, time_measure_s, time_measure_ms);
	}
558

559
560
free_all:

Andrew Leech's avatar
Andrew Leech committed
561
	fclose(svf_fd);
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
	svf_fd = 0;

	// free buffers
	if (svf_command_buffer)
	{
		free(svf_command_buffer);
		svf_command_buffer = NULL;
		svf_command_buffer_size = 0;
	}
	if (svf_check_tdo_para)
	{
		free(svf_check_tdo_para);
		svf_check_tdo_para = NULL;
		svf_check_tdo_para_index = 0;
	}
	if (svf_tdi_buffer)
	{
		free(svf_tdi_buffer);
		svf_tdi_buffer = NULL;
	}
	if (svf_tdo_buffer)
	{
		free(svf_tdo_buffer);
		svf_tdo_buffer = NULL;
	}
	if (svf_mask_buffer)
	{
		free(svf_mask_buffer);
		svf_mask_buffer = NULL;
	}
	svf_buffer_index = 0;
	svf_buffer_size = 0;

	svf_free_xxd_para(&svf_para.hdr_para);
	svf_free_xxd_para(&svf_para.hir_para);
	svf_free_xxd_para(&svf_para.tdr_para);
	svf_free_xxd_para(&svf_para.tir_para);
	svf_free_xxd_para(&svf_para.sdr_para);
	svf_free_xxd_para(&svf_para.sir_para);

	if (ERROR_OK == ret)
	{
604
		command_print(CMD_CTX, "svf file programmed successfully for %d commands", command_num);
605
606
607
	}
	else
	{
608
		command_print(CMD_CTX, "svf file programmed failed");
609
610
611
612
613
	}

	return ret;
}

Andrew Leech's avatar
Andrew Leech committed
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
static int svf_getline (char **lineptr, size_t *n, FILE *stream)
{
#define MIN_CHUNK 16	//Buffer is increased by this size each time as required
  size_t i = 0;

  if (*lineptr == NULL)
    {
      *n = MIN_CHUNK;
      *lineptr = (char *)malloc (*n);
      if (!*lineptr)
        {
		  return -1;
        }
    }

	(*lineptr)[0] = fgetc(stream);
	while ((*lineptr)[i] != '\n')
	{
		(*lineptr)[++i] = fgetc(stream);
		if (feof(stream))
		{
			(*lineptr)[0] = 0;
			return -1;
		}
		if ((i + 2) > *n)
		{
			*n += MIN_CHUNK;
			*lineptr = realloc(*lineptr, *n);
		}
	}

	(*lineptr)[++i] = 0;

	return sizeof(*lineptr);
}

650
#define SVFP_CMD_INC_CNT			1024
Andrew Leech's avatar
Andrew Leech committed
651
static int svf_read_command_from_file(FILE * fd)
652
{
David Brownell's avatar
David Brownell committed
653
	unsigned char ch;
Andrew Leech's avatar
Andrew Leech committed
654
655
656
	int i = 0;
	size_t cmd_pos = 0;
	int cmd_ok = 0, slash = 0, comment = 0;
657

Andrew Leech's avatar
Andrew Leech committed
658
659
660
661
662
663
664
	if (svf_getline (&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
	{
		return ERROR_FAIL;
	}
	svf_line_number++;
	ch = svf_read_line[0];
	while (!cmd_ok && (ch != 0))
665
	{
zwelch's avatar
zwelch committed
666
		switch (ch)
667
668
669
		{
		case '!':
			slash = 0;
Andrew Leech's avatar
Andrew Leech committed
670
671
672
673
674
675
			if (svf_getline (&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
			{
				return ERROR_FAIL;
			}
			svf_line_number++;
			i = -1;
676
677
678
679
			break;
		case '/':
			if (++slash == 2)
			{
Andrew Leech's avatar
Andrew Leech committed
680
681
682
683
684
685
686
				slash = 0;
				if (svf_getline (&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
				{
					return ERROR_FAIL;
				}
				svf_line_number++;
				i = -1;
687
688
689
690
			}
			break;
		case ';':
			slash = 0;
Andrew Leech's avatar
Andrew Leech committed
691
			cmd_ok = 1;
692
693
694
			break;
		case '\n':
			svf_line_number++;
Andrew Leech's avatar
Andrew Leech committed
695
696
697
698
699
			if (svf_getline (&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
			{
				return ERROR_FAIL;
			}
			i = -1;
700
701
702
		case '\r':
			slash = 0;
			comment = 0;
703
704
705
			/* Don't save '\r' and '\n' if no data is parsed */
			if (!cmd_pos)
				break;
706
		default:
Andrew Leech's avatar
Andrew Leech committed
707
708
709
710
711
712
713
714
715
716
717
718
719
			/* The parsing code currently expects a space
			 * before parentheses -- "TDI (123)".  Also a
			 * space afterwards -- "TDI (123) TDO(456)".
			 * But such spaces are optional... instead of
			 * parser updates, cope with that by adding the
			 * spaces as needed.
			 *
			 * Ensure there are 3 bytes available, for:
			 *  - current character
			 *  - added space.
			 *  - terminating NUL ('\0')
			 */
			if ((cmd_pos + 2) >= svf_command_buffer_size)
720
			{
Andrew Leech's avatar
Andrew Leech committed
721
722
				svf_command_buffer = realloc(svf_command_buffer, (cmd_pos + 2));
				if (svf_command_buffer == NULL)
723
				{
Andrew Leech's avatar
Andrew Leech committed
724
725
					LOG_ERROR("not enough memory");
					return ERROR_FAIL;
726
				}
Andrew Leech's avatar
Andrew Leech committed
727
			}
728

Andrew Leech's avatar
Andrew Leech committed
729
730
731
			/* insert a space before '(' */
			if ('(' == ch)
				svf_command_buffer[cmd_pos++] = ' ';
732

Andrew Leech's avatar
Andrew Leech committed
733
			svf_command_buffer[cmd_pos++] = (char)toupper(ch);
734

Andrew Leech's avatar
Andrew Leech committed
735
736
737
			/* insert a space after ')' */
			if (')' == ch)
				svf_command_buffer[cmd_pos++] = ' ';
738
739
			break;
		}
Andrew Leech's avatar
Andrew Leech committed
740
		ch = svf_read_line[++i];
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
	}

	if (cmd_ok)
	{
		svf_command_buffer[cmd_pos] = '\0';
		return ERROR_OK;
	}
	else
	{
		return ERROR_FAIL;
	}
}

static int svf_parse_cmd_string(char *str, int len, char **argus, int *num_of_argu)
{
756
	int pos = 0, num = 0, space_found = 1, in_bracket = 0;
757
758
759

	while (pos < len)
	{
zwelch's avatar
zwelch committed
760
		switch (str[pos])
761
762
763
764
765
		{
		case '!':
		case '/':
			LOG_ERROR("fail to parse svf command");
			return ERROR_FAIL;
766
767
768
769
770
771
		case '(':
			in_bracket = 1;
			goto parse_char;
		case ')':
			in_bracket = 0;
			goto parse_char;
772
		default:
773
parse_char:
David Brownell's avatar
David Brownell committed
774
			if (!in_bracket && isspace((int) str[pos]))
775
776
777
778
779
			{
				space_found = 1;
				str[pos] = '\0';
			}
			else if (space_found)
780
781
782
783
784
785
786
787
788
789
790
791
792
793
			{
				argus[num++] = &str[pos];
				space_found = 0;
			}
			break;
		}
		pos++;
	}

	*num_of_argu = num;

	return ERROR_OK;
}

David Brownell's avatar
David Brownell committed
794
bool svf_tap_state_is_stable(tap_state_t state)
795
{
David Brownell's avatar
David Brownell committed
796
797
	return (TAP_RESET == state) || (TAP_IDLE == state)
			|| (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
}

static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
{
	int i;

	for (i = 0; i < num_of_element; i++)
	{
		if (!strcmp(str, strs[i]))
		{
			return i;
		}
	}
	return 0xFF;
}

zwelch's avatar
zwelch committed
814
static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
815
816
817
818
819
820
821
822
823
824
{
	int new_byte_len = (new_bit_len + 7) >> 3;

	if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3)))
	{
		if (*arr != NULL)
		{
			free(*arr);
			*arr = NULL;
		}
zwelch's avatar
zwelch committed
825
		*arr = (uint8_t*)malloc(new_byte_len);
826
827
828
829
830
831
832
833
834
835
		if (NULL == *arr)
		{
			LOG_ERROR("not enough memory");
			return ERROR_FAIL;
		}
		memset(*arr, 0, new_byte_len);
	}
	return ERROR_OK;
}

Andrew Leech's avatar
Andrew Leech committed
836
837
838
839
840
841
842
843
844
845
846
847
848
static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi)
{
	int error = ERROR_OK;
	error |= svf_adjust_array_length(&para->tdi, para->len, len);
	memset(para->tdi, tdi, (len + 7) >> 3);
	error |= svf_adjust_array_length(&para->tdo, para->len, len);
	error |= svf_adjust_array_length(&para->mask, para->len, len);
	para->len = len;
	para->data_mask = XXR_TDI;

	return error;
}

zwelch's avatar
zwelch committed
849
static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
850
{
851
	int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
852
	uint8_t ch = 0;
853
854
855
856
857
858
859

	if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len))
	{
		LOG_ERROR("fail to adjust length of array");
		return ERROR_FAIL;
	}

860
	/* fill from LSB (end of str) to MSB (beginning of str) */
861
	for (i = 0; i < str_hbyte_len; i++)
862
	{
863
864
		ch = 0;
		while (str_len > 0)
865
		{
866
867
			ch = str[--str_len];

868
869
870
871
872
873
874
			/* Skip whitespace.  The SVF specification (rev E) is
			 * deficient in terms of basic lexical issues like
			 * where whitespace is allowed.  Long bitstrings may
			 * require line ends for correctness, since there is
			 * a hard limit on line length.
			 */
			if (!isspace(ch))
875
			{
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
				if ((ch >= '0') && (ch <= '9'))
				{
					ch = ch - '0';
					break;
				}
				else if ((ch >= 'A') && (ch <= 'F'))
				{
					ch = ch - 'A' + 10;
					break;
				}
				else
				{
					LOG_ERROR("invalid hex string");
					return ERROR_FAIL;
				}
891
			}
892

893
894
895
			ch = 0;
		}

896
897
		// write bin
		if (i % 2)
898
		{
899
900
			// MSB
			(*bin)[i / 2] |= ch << 4;
901
		}
902
		else
903
		{
904
905
906
			// LSB
			(*bin)[i / 2] = 0;
			(*bin)[i / 2] |= ch;
907
		}
908
	}
909

910
911
	/* consume optional leading '0' MSBs or whitespace */
	while (str_len > 0 && ((str[str_len - 1] == '0')
David Brownell's avatar
David Brownell committed
912
				|| isspace((int) str[str_len - 1])))
913
914
		str_len--;

915
	/* check validity: we must have consumed everything */
916
	if (str_len > 0 || (ch & ~((2 << ((bit_len - 1) % 4)) - 1)) != 0)
917
	{
918
		LOG_ERROR("value execeeds length");
919
		return ERROR_FAIL;
920
921
922
923
924
925
926
	}

	return ERROR_OK;
}

static int svf_check_tdo(void)
{
Øyvind Harboe's avatar
Øyvind Harboe committed
927
	int i, len, index_var;
928
929
930

	for (i = 0; i < svf_check_tdo_para_index; i++)
	{
Øyvind Harboe's avatar
Øyvind Harboe committed
931
		index_var = svf_check_tdo_para[i].buffer_offset;
zwelch's avatar
zwelch committed
932
		len = svf_check_tdo_para[i].bit_len;
933
		if ((svf_check_tdo_para[i].enabled)
Øyvind Harboe's avatar
Øyvind Harboe committed
934
			&& buf_cmp_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var], &svf_mask_buffer[index_var], len))
935
		{
zwelch's avatar
zwelch committed
936
937
			unsigned bitmask;
			unsigned received, expected, tapmask;
zwelch's avatar
zwelch committed
938
939
			bitmask = svf_get_mask_u32(svf_check_tdo_para[i].bit_len);

Øyvind Harboe's avatar
Øyvind Harboe committed
940
941
942
			memcpy(&received, svf_tdi_buffer + index_var, sizeof(unsigned));
			memcpy(&expected, svf_tdo_buffer + index_var, sizeof(unsigned));
			memcpy(&tapmask, svf_mask_buffer + index_var, sizeof(unsigned));
943
			LOG_ERROR("tdo check error at line %d",
zwelch's avatar
zwelch committed
944
					  svf_check_tdo_para[i].line_num);
945
946
947
			LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
					  received & bitmask,
					  expected & bitmask,
zwelch's avatar
zwelch committed
948
949
					  tapmask & bitmask);
			return ERROR_FAIL;
950
951
952
953
954
955
956
		}
	}
	svf_check_tdo_para_index = 0;

	return ERROR_OK;
}

zwelch's avatar
zwelch committed
957
static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
{
	if (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE)
	{
		LOG_ERROR("toooooo many operation undone");
		return ERROR_FAIL;
	}

	svf_check_tdo_para[svf_check_tdo_para_index].line_num = svf_line_number;
	svf_check_tdo_para[svf_check_tdo_para_index].bit_len = bit_len;
	svf_check_tdo_para[svf_check_tdo_para_index].enabled = enabled;
	svf_check_tdo_para[svf_check_tdo_para_index].buffer_offset = buffer_offset;
	svf_check_tdo_para_index++;

	return ERROR_OK;
}

974
975
static int svf_execute_tap(void)
{
976
	if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
977
978
979
980
981
982
983
984
985
986
987
988
989
	{
		return ERROR_FAIL;
	}
	else if (ERROR_OK != svf_check_tdo())
	{
		return ERROR_FAIL;
	}

	svf_buffer_index = 0;

	return ERROR_OK;
}

990
static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
991
992
993
994
995
996
997
998
999
1000
1001
{
	char *argus[256], command;
	int num_of_argu = 0, i;

	// tmp variable
	int i_tmp;

	// for RUNTEST
	int run_count;
	float min_time, max_time;
	// for XXR
1002
	struct svf_xxr_para *xxr_para_tmp;
zwelch's avatar
zwelch committed
1003
	uint8_t **pbuffer_tmp;
1004
	struct scan_field field;
1005
1006
	// for STATE
	tap_state_t *path = NULL, state;
Andrew Leech's avatar
Andrew Leech committed
1007
1008
	// flag padding commands skipped due to -tap command
	int padding_command_skipped = 0;
1009
1010
1011
1012
1013
1014

	if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
	{
		return ERROR_FAIL;
	}

1015
1016
1017
1018
1019
	/* NOTE: we're a bit loose here, because we ignore case in
	 * TAP state names (instead of insisting on uppercase).
	 */

	command = svf_find_string_in_array(argus[0],
1020
			(char **)svf_command_name, ARRAY_SIZE(svf_command_name));
zwelch's avatar
zwelch committed
1021
	switch (command)
1022
1023
1024
1025
1026
1027
1028
1029
	{
	case ENDDR:
	case ENDIR:
		if (num_of_argu != 2)
		{
			LOG_ERROR("invalid parameter of %s", argus[0]);
			return ERROR_FAIL;
		}
1030
1031
1032

		i_tmp = tap_state_by_name(argus[1]);

1033
1034
1035
1036
1037
		if (svf_tap_state_is_stable(i_tmp))
		{
			if (command == ENDIR)
			{
				svf_para.ir_end_state = i_tmp;
1038
1039
				LOG_DEBUG("\tIR end_state = %s",
						tap_state_name(i_tmp));
1040
1041
1042
1043
			}
			else
			{
				svf_para.dr_end_state = i_tmp;
1044
1045
				LOG_DEBUG("\tDR end_state = %s",
						tap_state_name(i_tmp));
1046
1047
1048
1049
			}
		}
		else
		{
1050
1051
			LOG_ERROR("%s: %s is not a stable state",
					argus[0], argus[1]);
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
			return ERROR_FAIL;
		}
		break;
	case FREQUENCY:
		if ((num_of_argu != 1) && (num_of_argu != 3))
		{
			LOG_ERROR("invalid parameter of %s", argus[0]);
			return ERROR_FAIL;
		}
		if (1 == num_of_argu)
		{
			// TODO: set jtag speed to full speed
			svf_para.frequency = 0;
		}
		else
		{
			if (strcmp(argus[2], "HZ"))
			{
				LOG_ERROR("HZ not found in FREQUENCY command");
				return ERROR_FAIL;
			}
1073
1074
1075
1076
			if (ERROR_OK != svf_execute_tap())
			{
				return ERROR_FAIL;
			}
1077
1078
1079
1080
			svf_para.frequency = atof(argus[1]);
			// TODO: set jtag speed to
			if (svf_para.frequency > 0)
			{
1081
				command_run_linef(cmd_ctx, "adapter_khz %d", (int)svf_para.frequency / 1000);
1082
1083
1084
1085
1086
				LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
			}
		}
		break;
	case HDR:
Andrew Leech's avatar
Andrew Leech committed
1087
1088
1089
1090
1091
		if (svf_tap_is_specified)
		{
			padding_command_skipped = 1;
			break;
		}
1092
1093
1094
		xxr_para_tmp = &svf_para.hdr_para;
		goto XXR_common;
	case HIR:
Andrew Leech's avatar
Andrew Leech committed
1095
1096
1097
1098
1099
		if (svf_tap_is_specified)
		{
			padding_command_skipped = 1;
			break;
		}
1100
1101
1102
		xxr_para_tmp = &svf_para.hir_para;
		goto XXR_common;
	case TDR:
Andrew Leech's avatar
Andrew Leech committed
1103
1104
1105
1106
1107
		if (svf_tap_is_specified)
		{
			padding_command_skipped = 1;
			break;
		}
1108
1109
1110
		xxr_para_tmp = &svf_para.tdr_para;
		goto XXR_common;
	case TIR:
Andrew Leech's avatar
Andrew Leech committed
1111
1112
1113
1114
1115
		if (svf_tap_is_specified)
		{
			padding_command_skipped = 1;
			break;
		}
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
		xxr_para_tmp = &svf_para.tir_para;
		goto XXR_common;
	case SDR:
		xxr_para_tmp = &svf_para.sdr_para;
		goto XXR_common;
	case SIR:
		xxr_para_tmp = &svf_para.sir_para;
		goto XXR_common;
		XXR_common:
		// XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)]
		if ((num_of_argu > 10) || (num_of_argu % 2))
		{
			LOG_ERROR("invalid parameter of %s", argus[0]);
			return ERROR_FAIL;
		}
		i_tmp = xxr_para_tmp->len;
		xxr_para_tmp->len = atoi(argus[1]);
		LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
		xxr_para_tmp->data_mask = 0;
		for (i = 2; i < num_of_argu; i += 2)
		{
1137
			if ((strlen(argus[i + 1]) < 3) || (argus[i + 1][0] != '(') || (argus[i + 1][strlen(argus[i + 1]) - 1] != ')'))
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
			{
				LOG_ERROR("data section error");
				return ERROR_FAIL;
			}
			argus[i + 1][strlen(argus[i + 1]) - 1] = '\0';
			// TDI, TDO, MASK, SMASK
			if (!strcmp(argus[i], "TDI"))
			{
				// TDI
				pbuffer_tmp = &xxr_para_tmp->tdi;
				xxr_para_tmp->data_mask |= XXR_TDI;
			}
			else if (!strcmp(argus[i], "TDO"))
			{
				// TDO
				pbuffer_tmp = &xxr_para_tmp->tdo;
				xxr_para_tmp->data_mask |= XXR_TDO;
			}
			else if (!strcmp(argus[i], "MASK"))
			{
				// MASK
				pbuffer_tmp = &xxr_para_tmp->mask;
				xxr_para_tmp->data_mask |= XXR_MASK;
			}
			else if (!strcmp(argus[i], "SMASK"))
			{
				// SMASK
				pbuffer_tmp = &xxr_para_tmp->smask;
				xxr_para_tmp->data_mask |= XXR_SMASK;
			}
			else
			{
				LOG_ERROR("unknow parameter: %s", argus[i]);
				return ERROR_FAIL;
			}
			if (ERROR_OK != svf_copy_hexstring_to_binary(&argus[i + 1][1], pbuffer_tmp, i_tmp, xxr_para_tmp->len))
			{
				LOG_ERROR("fail to parse hex value");
				return ERROR_FAIL;
			}
zwelch's avatar
zwelch committed
1178
			LOG_DEBUG("\t%s = 0x%X", argus[i], (**(int**)pbuffer_tmp) & svf_get_mask_u32(xxr_para_tmp->len));
1179
		}
1180
		// If a command changes the length of the last scan of the same type and the MASK parameter is absent,
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
		// the mask pattern used is all cares
		if (!(xxr_para_tmp->data_mask & XXR_MASK) && (i_tmp != xxr_para_tmp->len))
		{
			// MASK not defined and length changed
			if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
			{
				LOG_ERROR("fail to adjust length of array");
				return ERROR_FAIL;
			}
			buf_set_ones(xxr_para_tmp->mask, xxr_para_tmp->len);
		}
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
		// If TDO is absent, no comparison is needed, set the mask to 0
		if (!(xxr_para_tmp->data_mask & XXR_TDO))
		{
			if (NULL == xxr_para_tmp->tdo)
			{
				if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp, xxr_para_tmp->len))
				{
					LOG_ERROR("fail to adjust length of array");
					return ERROR_FAIL;
				}
			}
			if (NULL == xxr_para_tmp->mask)
			{
				if (ERROR_OK != svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp, xxr_para_tmp->len))
				{
					LOG_ERROR("fail to adjust length of array");
					return ERROR_FAIL;
				}
			}
			memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
		}
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
		// do scan if necessary
		if (SDR == command)
		{
			// check buffer size first, reallocate if necessary
			i = svf_para.hdr_para.len + svf_para.sdr_para.len + svf_para.tdr_para.len;
			if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
			{
#if 1
				// simply print error message
				LOG_ERROR("buffer is not enough, report to author");
				return ERROR_FAIL;
#else
zwelch's avatar
zwelch committed
1225
				uint8_t *buffer_tmp;
1226
1227

				// reallocate buffer
zwelch's avatar
zwelch committed
1228
				buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
				if (NULL == buffer_tmp)
				{
					LOG_ERROR("not enough memory");
					return ERROR_FAIL;
				}
				memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
				// svf_tdi_buffer isn't NULL here
				free(svf_tdi_buffer);
				svf_tdi_buffer = buffer_tmp;

zwelch's avatar
zwelch committed
1239
				buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
				if (NULL == buffer_tmp)
				{
					LOG_ERROR("not enough memory");
					return ERROR_FAIL;
				}
				memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
				// svf_tdo_buffer isn't NULL here
				free(svf_tdo_buffer);
				svf_tdo_buffer = buffer_tmp;

zwelch's avatar
zwelch committed
1250
				buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
				if (NULL == buffer_tmp)
				{
					LOG_ERROR("not enough memory");
					return ERROR_FAIL;
				}
				memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
				// svf_mask_buffer isn't NULL here
				free(svf_mask_buffer);
				svf_mask_buffer = buffer_tmp;

				buffer_tmp = NULL;
				svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
#endif
			}

			// assemble dr data
			i = 0;
			buf_set_buf(svf_para.hdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
			i += svf_para.hdr_para.len;
			buf_set_buf(svf_para.sdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
			i += svf_para.sdr_para.len;
			buf_set_buf(svf_para.tdr_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
			i += svf_para.tdr_para.len;

			// add check data
			if (svf_para.sdr_para.data_mask & XXR_TDO)
			{
				// assemble dr mask data
				i = 0;
				buf_set_buf(svf_para.hdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
				i += svf_para.hdr_para.len;
				buf_set_buf(svf_para.sdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
				i += svf_para.sdr_para.len;
				buf_set_buf(svf_para.tdr_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
				i += svf_para.tdr_para.len;
				// assemble dr check data
				i = 0;
				buf_set_buf(svf_para.hdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hdr_para.len);
				i += svf_para.hdr_para.len;
				buf_set_buf(svf_para.sdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sdr_para.len);
				i += svf_para.sdr_para.len;
				buf_set_buf(svf_para.tdr_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tdr_para.len);
				i += svf_para.tdr_para.len;

				svf_add_check_para(1, svf_buffer_index, i);
			}
			else
			{
				svf_add_check_para(0, svf_buffer_index, i);
			}
			field.num_bits = i;
			field.out_value = &svf_tdi_buffer[svf_buffer_index];
			field.in_value = &svf_tdi_buffer[svf_buffer_index];
1304
1305
1306
1307
1308
			if (!svf_nil)
			{
				/* NOTE:  doesn't use SVF-specified state paths */
				jtag_add_plain_dr_scan(field.num_bits, field.out_value, field.in_value, svf_para.dr_end_state);
			}
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322

			svf_buffer_index += (i + 7) >> 3;
		}
		else if (SIR == command)
		{
			// check buffer size first, reallocate if necessary
			i = svf_para.hir_para.len + svf_para.sir_para.len + svf_para.tir_para.len;
			if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3))
			{
#if 1
				// simply print error message
				LOG_ERROR("buffer is not enough, report to author");
				return ERROR_FAIL;
#else
zwelch's avatar
zwelch committed
1323
				uint8_t *buffer_tmp;
1324
1325

				// reallocate buffer
zwelch's avatar
zwelch committed
1326
				buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
				if (NULL == buffer_tmp)
				{
					LOG_ERROR("not enough memory");
					return ERROR_FAIL;
				}
				memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
				// svf_tdi_buffer isn't NULL here
				free(svf_tdi_buffer);
				svf_tdi_buffer = buffer_tmp;

zwelch's avatar
zwelch committed
1337
				buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
				if (NULL == buffer_tmp)
				{
					LOG_ERROR("not enough memory");
					return ERROR_FAIL;
				}
				memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
				// svf_tdo_buffer isn't NULL here
				free(svf_tdo_buffer);
				svf_tdo_buffer = buffer_tmp;

zwelch's avatar
zwelch committed
1348
				buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
				if (NULL == buffer_tmp)
				{
					LOG_ERROR("not enough memory");
					return ERROR_FAIL;
				}
				memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
				// svf_mask_buffer isn't NULL here
				free(svf_mask_buffer);
				svf_mask_buffer = buffer_tmp;

				buffer_tmp = NULL;
				svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
#endif
			}

			// assemble ir data
			i = 0;
			buf_set_buf(svf_para.hir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.hir_para.len);
			i += svf_para.hir_para.len;
			buf_set_buf(svf_para.sir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.sir_para.len);
			i += svf_para.sir_para.len;
			buf_set_buf(svf_para.tir_para.tdi, 0, &svf_tdi_buffer[svf_buffer_index], i, svf_para.tir_para.len);
			i += svf_para.tir_para.len;

			// add check data
			if (svf_para.sir_para.data_mask & XXR_TDO)
			{
				// assemble dr mask data
				i = 0;
				buf_set_buf(svf_para.hir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.hir_para.len);
				i += svf_para.hir_para.len;
				buf_set_buf(svf_para.sir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.sir_para.len);
				i += svf_para.sir_para.len;
				buf_set_buf(svf_para.tir_para.mask, 0, &svf_mask_buffer[svf_buffer_index], i, svf_para.tir_para.len);
				i += svf_para.tir_para.len;
				// assemble dr check data
				i = 0;
				buf_set_buf(svf_para.hir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.hir_para.len);
				i += svf_para.hir_para.len;
				buf_set_buf(svf_para.sir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.sir_para.len);
				i += svf_para.sir_para.len;
				buf_set_buf(svf_para.tir_para.tdo, 0, &svf_tdo_buffer[svf_buffer_index], i, svf_para.tir_para.len);
				i += svf_para.tir_para.len;

				svf_add_check_para(1, svf_buffer_index, i);
			}
			else
			{
				svf_add_check_para(0, svf_buffer_index, i);
			}
			field.num_bits = i;
			field.out_value = &svf_tdi_buffer[svf_buffer_index];
			field.in_value = &svf_tdi_buffer[svf_buffer_index];
1402
1403
1404
1405
1406
1407
			if (!svf_nil)
			{
				/* NOTE:  doesn't use SVF-specified state paths */
				jtag_add_plain_ir_scan(field.num_bits, field.out_value, field.in_value,
						svf_para.ir_end_state);
			}
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429

			svf_buffer_index += (i + 7) >> 3;
		}
		break;
	case PIO:
	case PIOMAP:
		LOG_ERROR("PIO and PIOMAP are not supported");
		return ERROR_FAIL;
		break;
	case RUNTEST:
		// RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time SEC]] [ENDSTATE end_state]
		// RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE end_state]
		if ((num_of_argu < 3) && (num_of_argu > 11))
		{
			LOG_ERROR("invalid parameter of %s", argus[0]);
			return ERROR_FAIL;
		}
		// init
		run_count = 0;
		min_time = 0;
		max_time = 0;
		i = 1;
1430

1431
		// run_state
1432
1433
		i_tmp = tap_state_by_name(argus[i]);
		if (i_tmp != TAP_INVALID)
1434
1435
1436
1437
1438
		{
			if (svf_tap_state_is_stable(i_tmp))
			{
				svf_para.runtest_run_state = i_tmp;

1439
1440
1441
				/* When a run_state is specified, the new
				 * run_state becomes the default end_state.
				 */
1442
				svf_para.runtest_end_state = i_tmp;
1443
1444
				LOG_DEBUG("\trun_state = %s",
						tap_state_name(i_tmp));
1445
1446
1447
1448
				i++;
			}
			else
			{
1449
1450
				LOG_ERROR("%s: %s is not a stable state",
					argus[0], tap_state_name(i_tmp));
1451
1452
1453
				return ERROR_FAIL;
			}
		}
1454

1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
		// run_count run_clk
		if (((i + 2) <= num_of_argu) && strcmp(argus[i + 1], "SEC"))
		{
			if (!strcmp(argus[i + 1], "TCK"))
			{
				// clock source is TCK
				run_count = atoi(argus[i]);
				LOG_DEBUG("\trun_count@TCK = %d", run_count);
			}
			else
			{
				LOG_ERROR("%s not supported for clock", argus[i + 1]);
				return ERROR_FAIL;
			}
			i += 2;
		}
		// min_time SEC
		if (((i + 2) <= num_of_argu) && !strcmp(argus[i + 1], "SEC"))
		{
			min_time = atof(argus[i]);
			LOG_DEBUG("\tmin_time = %fs", min_time);
			i += 2;
		}
		// MAXIMUM max_time SEC
		if (((i + 3) <= num_of_argu) && !strcmp(argus[i], "MAXIMUM") && !strcmp(argus[i + 2], "SEC"))
		{
			max_time = atof(argus[i + 1]);
			LOG_DEBUG("\tmax_time = %fs", max_time);
			i += 3;
		}
		// ENDSTATE end_state
		if (((i + 2) <= num_of_argu) && !strcmp(argus[i], "ENDSTATE"))
		{
1488
1489
			i_tmp = tap_state_by_name(argus[i + 1]);

1490
1491
1492
			if (svf_tap_state_is_stable(i_tmp))
			{
				svf_para.runtest_end_state = i_tmp;