svf.c 42.5 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
318
319
#define SVF_MIN_NUM_OF_OPTIONS			1
#define SVF_MAX_NUM_OF_OPTIONS			5
#define USAGE [-tap device.tap] <file> [quiet] [progress]
#define PRINT_USAGE	command_print(CMD_CTX, "svf USAGE")
320
	int command_num = 0;
321
	int ret = ERROR_OK;
Andrew Leech's avatar
Andrew Leech committed
322
323
324
325
326
327
328
329
	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;
330

Andrew Leech's avatar
Andrew Leech committed
331
	if ((CMD_ARGC < SVF_MIN_NUM_OF_OPTIONS) || (CMD_ARGC > SVF_MAX_NUM_OF_OPTIONS))
332
	{
Andrew Leech's avatar
Andrew Leech committed
333
		PRINT_USAGE;
334
335
336
		return ERROR_FAIL;
	}

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

Andrew Leech's avatar
Andrew Leech committed
373
	if (svf_fd == NULL)
374
	{
Andrew Leech's avatar
Andrew Leech committed
375
		PRINT_USAGE;
376
377
378
		return ERROR_FAIL;
	}

379
	// get time
Andrew Leech's avatar
Andrew Leech committed
380
	time_measure_ms = timeval_ms();
381

382
383
384
385
386
	// init
	svf_line_number = 1;
	svf_command_buffer_size = 0;

	svf_check_tdo_para_index = 0;
387
	svf_check_tdo_para = malloc(sizeof(struct svf_check_tdo_para) * SVF_CHECK_TDO_PARA_SIZE);
388
389
390
391
392
393
394
395
396
397
398
399
	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
400
	svf_tdi_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
401
402
403
404
405
406
	if (NULL == svf_tdi_buffer)
	{
		LOG_ERROR("not enough memory");
		ret = ERROR_FAIL;
		goto free_all;
	}
zwelch's avatar
zwelch committed
407
	svf_tdo_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
408
409
410
411
412
413
	if (NULL == svf_tdo_buffer)
	{
		LOG_ERROR("not enough memory");
		ret = ERROR_FAIL;
		goto free_all;
	}
zwelch's avatar
zwelch committed
414
	svf_mask_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
415
416
417
418
419
420
421
422
423
	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));
424

425
426
427
	// TAP_RESET
	jtag_add_tlr();

Andrew Leech's avatar
Andrew Leech committed
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
486
487
488
489
490
	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);
	}
491
	while (ERROR_OK == svf_read_command_from_file(svf_fd))
492
	{
Andrew Leech's avatar
Andrew Leech committed
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
		// 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
519
		if (ERROR_OK != svf_run_command(CMD_CTX, svf_command_buffer))
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
		{
			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;
	}

536
	// print time
Andrew Leech's avatar
Andrew Leech committed
537
538
539
540
541
542
543
544
545
	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);
	}
546

547
548
free_all:

Andrew Leech's avatar
Andrew Leech committed
549
	fclose(svf_fd);
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
587
588
589
590
591
	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)
	{
592
		command_print(CMD_CTX, "svf file programmed successfully for %d commands", command_num);
593
594
595
	}
	else
	{
596
		command_print(CMD_CTX, "svf file programmed failed");
597
598
599
600
601
	}

	return ret;
}

Andrew Leech's avatar
Andrew Leech committed
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
633
634
635
636
637
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);
}

638
#define SVFP_CMD_INC_CNT			1024
Andrew Leech's avatar
Andrew Leech committed
639
static int svf_read_command_from_file(FILE * fd)
640
{
David Brownell's avatar
David Brownell committed
641
	unsigned char ch;
Andrew Leech's avatar
Andrew Leech committed
642
643
644
	int i = 0;
	size_t cmd_pos = 0;
	int cmd_ok = 0, slash = 0, comment = 0;
645

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

Andrew Leech's avatar
Andrew Leech committed
717
718
719
			/* insert a space before '(' */
			if ('(' == ch)
				svf_command_buffer[cmd_pos++] = ' ';
720

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

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

	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)
{
744
	int pos = 0, num = 0, space_found = 1, in_bracket = 0;
745
746
747

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

	*num_of_argu = num;

	return ERROR_OK;
}

David Brownell's avatar
David Brownell committed
782
bool svf_tap_state_is_stable(tap_state_t state)
783
{
David Brownell's avatar
David Brownell committed
784
785
	return (TAP_RESET == state) || (TAP_IDLE == state)
			|| (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
}

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

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

848
	/* fill from LSB (end of str) to MSB (beginning of str) */
849
	for (i = 0; i < str_hbyte_len; i++)
850
	{
851
852
		ch = 0;
		while (str_len > 0)
853
		{
854
855
			ch = str[--str_len];

856
857
858
859
860
861
862
			/* 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))
863
			{
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
				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;
				}
879
			}
880

881
882
883
			ch = 0;
		}

884
885
		// write bin
		if (i % 2)
886
		{
887
888
			// MSB
			(*bin)[i / 2] |= ch << 4;
889
		}
890
		else
891
		{
892
893
894
			// LSB
			(*bin)[i / 2] = 0;
			(*bin)[i / 2] |= ch;
895
		}
896
	}
897

898
899
	/* consume optional leading '0' MSBs or whitespace */
	while (str_len > 0 && ((str[str_len - 1] == '0')
David Brownell's avatar
David Brownell committed
900
				|| isspace((int) str[str_len - 1])))
901
902
		str_len--;

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

	return ERROR_OK;
}

static int svf_check_tdo(void)
{
Øyvind Harboe's avatar
Øyvind Harboe committed
915
	int i, len, index_var;
916
917
918

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

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

	return ERROR_OK;
}

zwelch's avatar
zwelch committed
945
static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
{
	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;
}

962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
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;
}

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

	if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
	{
		return ERROR_FAIL;
For faster browsing, not all history is shown. View entire blame