svf.c 42.3 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	(4 * 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

Andrew Leech's avatar
Andrew Leech committed
227
228
229
230
231
232
233
234
235
// 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;
236

237
static void svf_free_xxd_para(struct svf_xxr_para *para)
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
{
	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;
		}
	}
}

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

	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
284
int svf_add_statemove(tap_state_t state_to)
285
286
{
	tap_state_t state_from = cmd_queue_cur_state;
Øyvind Harboe's avatar
Øyvind Harboe committed
287
	unsigned index_var;
288

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

Øyvind Harboe's avatar
Øyvind Harboe committed
295
	for (index_var = 0; index_var < ARRAY_SIZE(svf_statemoves); index_var++)
296
	{
Øyvind Harboe's avatar
Øyvind Harboe committed
297
298
		if ((svf_statemoves[index_var].from == state_from)
			&& (svf_statemoves[index_var].to == state_to))
299
		{
300
			/* recorded path includes current state ... avoid extra TCKs! */
Øyvind Harboe's avatar
Øyvind Harboe committed
301
302
303
			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);
304
			else
Øyvind Harboe's avatar
Øyvind Harboe committed
305
306
				jtag_add_pathmove(svf_statemoves[index_var].num_of_moves,
						svf_statemoves[index_var].paths);
307
308
309
			return ERROR_OK;
		}
	}
310
	LOG_ERROR("SVF: can not move to %s", tap_state_name(state_to));
311
312
313
	return ERROR_FAIL;
}

314
COMMAND_HANDLER(handle_svf_command)
315
{
Andrew Leech's avatar
Andrew Leech committed
316
317
#define SVF_MIN_NUM_OF_OPTIONS			1
#define SVF_MAX_NUM_OF_OPTIONS			5
318
	int command_num = 0;
319
	int ret = ERROR_OK;
Andrew Leech's avatar
Andrew Leech committed
320
321
322
323
324
325
326
327
	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;
328

Andrew Leech's avatar
Andrew Leech committed
329
	if ((CMD_ARGC < SVF_MIN_NUM_OF_OPTIONS) || (CMD_ARGC > SVF_MAX_NUM_OF_OPTIONS))
330
	{
331
		return ERROR_COMMAND_SYNTAX_ERROR;
332
333
	}

Andrew Leech's avatar
Andrew Leech committed
334
	// parse command line
335
	svf_quiet = 0;
Andrew Leech's avatar
Andrew Leech committed
336
	for (unsigned int i = 0; i < CMD_ARGC; i++)
337
	{
Andrew Leech's avatar
Andrew Leech committed
338
339
340
341
342
343
344
345
346
347
348
		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))
349
350
351
		{
			svf_quiet = 1;
		}
Andrew Leech's avatar
Andrew Leech committed
352
		else if ((strcmp(CMD_ARGV[i], "progress") == 0) || (strcmp(CMD_ARGV[i], "-progress") == 0))
353
		{
Andrew Leech's avatar
Andrew Leech committed
354
355
356
357
358
359
			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));
360
			// no need to free anything now
361
			return ERROR_COMMAND_SYNTAX_ERROR;
362
		}
Andrew Leech's avatar
Andrew Leech committed
363
364
365
366
		else
		{
			LOG_USER("svf processing file: \"%s\"", CMD_ARGV[i]);
		}
367
368
	}

Andrew Leech's avatar
Andrew Leech committed
369
	if (svf_fd == NULL)
370
	{
371
		return ERROR_COMMAND_SYNTAX_ERROR;
372
373
	}

374
	// get time
Andrew Leech's avatar
Andrew Leech committed
375
	time_measure_ms = timeval_ms();
376

377
378
379
380
381
	// init
	svf_line_number = 1;
	svf_command_buffer_size = 0;

	svf_check_tdo_para_index = 0;
382
	svf_check_tdo_para = malloc(sizeof(struct svf_check_tdo_para) * SVF_CHECK_TDO_PARA_SIZE);
383
384
385
386
387
388
389
390
391
392
393
394
	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
	// in case current command cannot be commited, and next command is a bit scan command
	// 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
395
	svf_tdi_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
396
397
398
399
400
401
	if (NULL == svf_tdi_buffer)
	{
		LOG_ERROR("not enough memory");
		ret = ERROR_FAIL;
		goto free_all;
	}
zwelch's avatar
zwelch committed
402
	svf_tdo_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
403
404
405
406
407
408
	if (NULL == svf_tdo_buffer)
	{
		LOG_ERROR("not enough memory");
		ret = ERROR_FAIL;
		goto free_all;
	}
zwelch's avatar
zwelch committed
409
	svf_mask_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
410
411
412
413
414
415
416
417
418
	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));
419

420
421
422
	// TAP_RESET
	jtag_add_tlr();

Andrew Leech's avatar
Andrew Leech committed
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
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
	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);
	}
486
	while (ERROR_OK == svf_read_command_from_file(svf_fd))
487
	{
Andrew Leech's avatar
Andrew Leech committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
		// 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
514
		if (ERROR_OK != svf_run_command(CMD_CTX, svf_command_buffer))
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
		{
			LOG_ERROR("fail to run command at line %d", svf_line_number);
			ret = ERROR_FAIL;
			break;
		}
		command_num++;
	}
	if (ERROR_OK != jtag_execute_queue())
	{
		ret = ERROR_FAIL;
	}
	else if (ERROR_OK != svf_check_tdo())
	{
		ret = ERROR_FAIL;
	}

531
	// print time
Andrew Leech's avatar
Andrew Leech committed
532
533
534
535
536
537
538
539
540
	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);
	}
541

542
543
free_all:

Andrew Leech's avatar
Andrew Leech committed
544
	fclose(svf_fd);
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
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
	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)
	{
587
		command_print(CMD_CTX, "svf file programmed successfully for %d commands", command_num);
588
589
590
	}
	else
	{
591
		command_print(CMD_CTX, "svf file programmed failed");
592
593
594
595
596
	}

	return ret;
}

Andrew Leech's avatar
Andrew Leech committed
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
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);
}

633
#define SVFP_CMD_INC_CNT			1024
Andrew Leech's avatar
Andrew Leech committed
634
static int svf_read_command_from_file(FILE * fd)
635
{
David Brownell's avatar
David Brownell committed
636
	unsigned char ch;
Andrew Leech's avatar
Andrew Leech committed
637
638
639
	int i = 0;
	size_t cmd_pos = 0;
	int cmd_ok = 0, slash = 0, comment = 0;
640

Andrew Leech's avatar
Andrew Leech committed
641
642
643
644
645
646
647
	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))
648
	{
zwelch's avatar
zwelch committed
649
		switch (ch)
650
651
652
		{
		case '!':
			slash = 0;
Andrew Leech's avatar
Andrew Leech committed
653
654
655
656
657
658
			if (svf_getline (&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
			{
				return ERROR_FAIL;
			}
			svf_line_number++;
			i = -1;
659
660
661
662
			break;
		case '/':
			if (++slash == 2)
			{
Andrew Leech's avatar
Andrew Leech committed
663
664
665
666
667
668
669
				slash = 0;
				if (svf_getline (&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
				{
					return ERROR_FAIL;
				}
				svf_line_number++;
				i = -1;
670
671
672
673
			}
			break;
		case ';':
			slash = 0;
Andrew Leech's avatar
Andrew Leech committed
674
			cmd_ok = 1;
675
676
677
			break;
		case '\n':
			svf_line_number++;
Andrew Leech's avatar
Andrew Leech committed
678
679
680
681
682
			if (svf_getline (&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
			{
				return ERROR_FAIL;
			}
			i = -1;
683
684
685
		case '\r':
			slash = 0;
			comment = 0;
686
687
688
			/* Don't save '\r' and '\n' if no data is parsed */
			if (!cmd_pos)
				break;
689
		default:
Andrew Leech's avatar
Andrew Leech committed
690
691
692
693
694
695
696
697
698
699
700
701
702
			/* 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)
703
			{
Andrew Leech's avatar
Andrew Leech committed
704
705
				svf_command_buffer = realloc(svf_command_buffer, (cmd_pos + 2));
				if (svf_command_buffer == NULL)
706
				{
Andrew Leech's avatar
Andrew Leech committed
707
708
					LOG_ERROR("not enough memory");
					return ERROR_FAIL;
709
				}
Andrew Leech's avatar
Andrew Leech committed
710
			}
711

Andrew Leech's avatar
Andrew Leech committed
712
713
714
			/* insert a space before '(' */
			if ('(' == ch)
				svf_command_buffer[cmd_pos++] = ' ';
715

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

Andrew Leech's avatar
Andrew Leech committed
718
719
720
			/* insert a space after ')' */
			if (')' == ch)
				svf_command_buffer[cmd_pos++] = ' ';
721
722
			break;
		}
Andrew Leech's avatar
Andrew Leech committed
723
		ch = svf_read_line[++i];
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
	}

	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)
{
739
	int pos = 0, num = 0, space_found = 1, in_bracket = 0;
740
741
742

	while (pos < len)
	{
zwelch's avatar
zwelch committed
743
		switch (str[pos])
744
745
746
747
748
		{
		case '!':
		case '/':
			LOG_ERROR("fail to parse svf command");
			return ERROR_FAIL;
749
750
751
752
753
754
		case '(':
			in_bracket = 1;
			goto parse_char;
		case ')':
			in_bracket = 0;
			goto parse_char;
755
		default:
756
parse_char:
David Brownell's avatar
David Brownell committed
757
			if (!in_bracket && isspace((int) str[pos]))
758
759
760
761
762
			{
				space_found = 1;
				str[pos] = '\0';
			}
			else if (space_found)
763
764
765
766
767
768
769
770
771
772
773
774
775
776
			{
				argus[num++] = &str[pos];
				space_found = 0;
			}
			break;
		}
		pos++;
	}

	*num_of_argu = num;

	return ERROR_OK;
}

David Brownell's avatar
David Brownell committed
777
bool svf_tap_state_is_stable(tap_state_t state)
778
{
David Brownell's avatar
David Brownell committed
779
780
	return (TAP_RESET == state) || (TAP_IDLE == state)
			|| (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
}

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
797
static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_len)
798
799
800
801
802
803
804
805
806
807
{
	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
808
		*arr = (uint8_t*)malloc(new_byte_len);
809
810
811
812
813
814
815
816
817
818
		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
819
820
821
822
823
824
825
826
827
828
829
830
831
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
832
static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_len, int bit_len)
833
{
834
	int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
835
	uint8_t ch = 0;
836
837
838
839
840
841
842

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

843
	/* fill from LSB (end of str) to MSB (beginning of str) */
844
	for (i = 0; i < str_hbyte_len; i++)
845
	{
846
847
		ch = 0;
		while (str_len > 0)
848
		{
849
850
			ch = str[--str_len];

851
852
853
854
855
856
857
			/* 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))
858
			{
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
				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;
				}
874
			}
875

876
877
878
			ch = 0;
		}

879
880
		// write bin
		if (i % 2)
881
		{
882
883
			// MSB
			(*bin)[i / 2] |= ch << 4;
884
		}
885
		else
886
		{
887
888
889
			// LSB
			(*bin)[i / 2] = 0;
			(*bin)[i / 2] |= ch;
890
		}
891
	}
892

893
894
	/* consume optional leading '0' MSBs or whitespace */
	while (str_len > 0 && ((str[str_len - 1] == '0')
David Brownell's avatar
David Brownell committed
895
				|| isspace((int) str[str_len - 1])))
896
897
		str_len--;

898
	/* check validity: we must have consumed everything */
899
	if (str_len > 0 || (ch & ~((2 << ((bit_len - 1) % 4)) - 1)) != 0)
900
	{
901
		LOG_ERROR("value execeeds length");
902
		return ERROR_FAIL;
903
904
905
906
907
908
909
	}

	return ERROR_OK;
}

static int svf_check_tdo(void)
{
Øyvind Harboe's avatar
Øyvind Harboe committed
910
	int i, len, index_var;
911
912
913

	for (i = 0; i < svf_check_tdo_para_index; i++)
	{
Øyvind Harboe's avatar
Øyvind Harboe committed
914
		index_var = svf_check_tdo_para[i].buffer_offset;
zwelch's avatar
zwelch committed
915
		len = svf_check_tdo_para[i].bit_len;
916
		if ((svf_check_tdo_para[i].enabled)
Øyvind Harboe's avatar
Øyvind Harboe committed
917
			&& buf_cmp_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var], &svf_mask_buffer[index_var], len))
918
		{
zwelch's avatar
zwelch committed
919
920
			unsigned bitmask;
			unsigned received, expected, tapmask;
zwelch's avatar
zwelch committed
921
922
			bitmask = svf_get_mask_u32(svf_check_tdo_para[i].bit_len);

Øyvind Harboe's avatar
Øyvind Harboe committed
923
924
925
			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));
926
			LOG_ERROR("tdo check error at line %d",
zwelch's avatar
zwelch committed
927
					  svf_check_tdo_para[i].line_num);
928
929
930
			LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
					  received & bitmask,
					  expected & bitmask,
zwelch's avatar
zwelch committed
931
932
					  tapmask & bitmask);
			return ERROR_FAIL;
933
934
935
936
937
938
939
		}
	}
	svf_check_tdo_para_index = 0;

	return ERROR_OK;
}

zwelch's avatar
zwelch committed
940
static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
{
	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;
}

957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
static int svf_execute_tap(void)
{
	if (ERROR_OK != jtag_execute_queue())
	{
		return ERROR_FAIL;
	}
	else if (ERROR_OK != svf_check_tdo())
	{
		return ERROR_FAIL;
	}

	svf_buffer_index = 0;

	return ERROR_OK;
}

973
static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
974
975
976
977
978
979
980
981
982
983
984
{
	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
985
	struct svf_xxr_para *xxr_para_tmp;
zwelch's avatar
zwelch committed
986
	uint8_t **pbuffer_tmp;
987
	struct scan_field field;
988
989
	// for STATE
	tap_state_t *path = NULL, state;
Andrew Leech's avatar
Andrew Leech committed
990
991
	// flag padding commands skipped due to -tap command
	int padding_command_skipped = 0;
992
993
994
995
996
997

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

998
999
1000
	/* NOTE: we're a bit loose here, because we ignore case in
	 * TAP state names (instead of insisting on uppercase).
	 */
For faster browsing, not all history is shown. View entire blame