Implementation error in SVF support in OpenOCD

Is there a bugtrack system for OpenOCD??? I can’t fine one so I am reporting this here…

Looking at svf.c, the code wrongly assumes that there is always a space delimiter e.g. TDI(123) when it can be TDI(123).

This is a fundamental mistake. See the spec for SVF, here http://www.asset-intertech.com/support/svf.pdf this makes no such assumption about the use of spaces and does not use them in it’s examples.

The code is written such that every keyword is paired with a value with a space delimiter in between but this is not always the case. I was going to fix it, but so far as I can see the parser needs a complete rewrite to remove this fixed pairing and I have not got time to do that right now.

BTW: I noticed this as Actel’s SVF output does not have spaces.

For now I will write a post processor that will convert non-space delimited to space delimited.

The parser also assumes that both a semicolon ‘;’ and a LF are both line endings when only the semicolon should be considered (or why bother with it at all).

import sys

import os

“”"OOCDFixupSVF.py a simple python script to fixup (in my case Actel) SVF files to

work with OpenOCD v0.2-0.4.0"“”

“”“by Jason Morgan - jason dot morgan at vpnsolutions dot uk dot com”“”

“”"OpenOCD now contains an SVF player, but is’s implementation is not very solid

e.g. it interprets LF as a line end as well as a semicolon, meaning

lines can’t be split.

It also requires a space between TDI, TDO, MASK and SMASK, and its paramater in

brackets even though that is not required by the specification.

e.g. TDI(123) and TDI(123) should both work, but they do not.

This program is a quick hack to reformat SVF scripts into the correct form.

“”"

infile=sys.argv[1]

outfile=sys.argv[2]

if len(sys.argv)!=3:

print “Usage OOCDfixupxvf ”

sys.exit(1)

if os.path.exists(infile):

fi=open(infile,“r”)

else:

print “Input file %s not found”%infile

sys.exit(1)

if infile==outfile:

print “Can’t use the same file name for input and output”

sys.exit(1)

fo=open(outfile,“w”)

kwds=[“TDI”,“TDO”,“ASK”]

linecount=0

l=fi.readline()

linecount+=1

while(l!=“”):

li=l.strip()

##If the line is a comment or is empty write it out

if len(li)==0 or li[0]==“!”:

fo.writelines(li+“\n”)

else:

#if the end of the line is not semicolon append another line

while(li[-1]!=“;”):

print “Joining line \n%s\n”%repr(li)

#Join lines, if the end of a line is a bracket, then add a space

#otherwise don’t add a space

if li[-1]==“)”:

pad=" "

else:

pad=“”

li=li+pad+fi.readline().strip()

linecount+=1

#Now we have a complete, terminated line, replace the keywords

for kw in kwds:

li=li.upper().replace(“%s(”%kw,“%s (”%kw)

#replace tabs with spaces

li=li.replace(“\t”," ")

#replace multiple spaces with single spaces

len1=1

len2=0

while(len1!=len2):

len1=len(li)

li=li.replace(" “,” ")

len2=len(li)

if len2!=len1:print “Removed %d extra spaces”%(len2-len1)

#then write out the line, insert \n after each semicolon for readability

lo=li.replace(“;”,“;\n”)

fo.write(lo)

l=fi.readline()

linecount+=1

if(linecount%100==0):print “.”,

print “\ndone”

print “in : %s”%infile

print “out: %s”%outfile

print “Processed %d lines”%linecount

Yes, you are right.

I implemented SVF support according to the SVF file generated by Altera, and there IS a space.

But it can be fixed by a simple patch instead of rewrite the whole parser.

I’ll try to fix it.

checking LR for line ending is ONLY because that it will provide a position in the file where the command is parsing.

I tried a file that as altered with spaces but where long TDI sequences are split over several lines this also failed due to line ending. When I removed the line ends it then worked (but then failed for another reason, I think there is also a bug in the libftdi driver that makes the jtagkey hardware give up after a few thousand lines)

Can you provide me the SVF file for test?

I tested it OK to use:

SIR 10 TDI (2

C

C);

SIR 10 TDI (2CC);

SIR 10 TDI(2CC);

Can you try this patch:

diff --git a/src/svf/svf.c b/src/svf/svf.c

index 7cb2200…9a62933 100644

— a/src/svf/svf.c

+++ b/src/svf/svf.c

@@ -504,7 +504,8 @@ static int svf_read_command_from_file(int fd)

default:

if (!comment)

{

  • if (cmd_pos >= svf_command_buffer_size - 1)
  • // ensure there are 2 bytes available

  • if ((cmd_pos + 1) >= svf_command_buffer_size - 1)

{

tmp_buffer = (char*)malloc(svf_command_buffer_size + SVFP_CMD_INC_CNT); // 1 more byte for ‘\0’

if (NULL == tmp_buffer)

@@ -524,6 +525,11 @@ static int svf_read_command_from_file(int fd)

svf_command_buffer_size += SVFP_CMD_INC_CNT;

tmp_buffer = NULL;

}

  • // insert a space before ‘(’

  • if (‘(’ == ch)

  • {

  • svf_command_buffer[cmd_pos++] = ’ ';

  • }

svf_command_buffer[cmd_pos++] = (char)toupper(ch);

}

break;