-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathxc3sprog.cpp
1890 lines (1745 loc) · 53.2 KB
/
xc3sprog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
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
486
487
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
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
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Spartan3 JTAG programmer
Copyright (C) 2004 Andrew Rogers
2005-2011 Uwe Bonnes [email protected]
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
Changes:
Dmitry Teytelman [[email protected]] 14 Jun 2006 [applied 13 Aug 2006]:
Code cleanup for clean -Wall compile.
Added support for FT2232 driver.
Verbose support added.
Installable device database location.
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <list>
#include <memory>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include "io_exception.h"
#include "ioparport.h"
#include "iofx2.h"
#include "ioftdi.h"
#include "ioxpc.h"
#include "sysfs.h"
#include "bitfile.h"
#include "jtag.h"
#include "devicedb.h"
#include "cabledb.h"
#include "progalgxcf.h"
#include "progalgxcfp.h"
#include "javr.h"
#include "progalgxc3s.h"
#include "jedecfile.h"
#include "mapfile_xc2c.h"
#include "progalgxc95x.h"
#include "progalgxc2c.h"
#include "progalgavr.h"
#include "progalgspiflash.h"
#include "progalgnvm.h"
#include "utilities.h"
using namespace std;
#define MAXPOSITIONS 8
#define IDCODE_TO_FAMILY(id) ((id>>21) & 0x7f)
#define IDCODE_TO_MANUFACTURER(id) ((id>>1) & 0x3ff)
#define MANUFACTURER_ATMEL 0x01f
#define MANUFACTURER_XILINX 0x049
int do_exit = 0;
void ctrl_c(int sig)
{
do_exit = 1;
}
int programXC3S(Jtag &g, int argc, char **args, bool verbose,
bool reconfig, int family);
int programXCF(Jtag &jtag, DeviceDB &db, int argc, char **args,
bool verbose, bool erase, bool reconfigure,
const char *device, int *chainpositions, int nchainpos,
const vector<string>& xcfopts);
int programXC95X(Jtag &jtag, unsigned long id, int argc, char **args,
bool verbose, bool erase, const char *device);
int programXC2C(Jtag &jtag, unsigned int id, int argc, char ** args,
bool verbose, bool erase, const char *mapdir,
const char *device);
int programSPI(Jtag &jtag, int argc, char ** args, bool verbose, bool erase,
bool reconfig, int test_count,
char *bscanfile,int family, const char *device);
int programXMega(Jtag *jtag, unsigned long id, int argc, char **args,
bool verbose, bool erase, bool reconfigure,
const char *device);
/* Excercise the IR Chain for at least 10000 Times
If we read a different pattern, print the pattern for for optical
comparision and read for at least 100000 times more
If we found no chain, simple rerun the chain detection
This may result in an endless loop to facilitate debugging with a scope etc
*/
void test_IRChain(Jtag *jtag, IOBase *io,DeviceDB &db , int test_count)
{
int num=jtag->getChain();
int failed = 0;
int len = 0;
int i, j, k;
unsigned char ir_in[256];
unsigned char ir_out[256];
unsigned char din[256];
unsigned char dout[256];
unsigned char dcmp[256];
memset(din, 0xaa, 256);
int run_irtest = 0;
if(num == 0)
/* the chain is not functional and we have no idea
* what parts to look for
*/
{
fprintf(stderr, "Running getChain %d times\n", test_count);
k=0;
for(i=0; i<test_count; i++)
{
if (jtag->getChain(true)> 0)
{
if(k%1000 == 1)
{
fprintf(stderr,".");
fflush(stderr);
}
k++;
}
}
return;
}
if(num >8)
fprintf(stderr, "Found %d devices\n", num);
/* Read the IDCODE via the IDCODE command */
(void) signal (SIGINT, ctrl_c);
for(i=0; i<num; i++)
{
jtag->setTapState(Jtag::TEST_LOGIC_RESET);
jtag->selectDevice(i);
DeviceID id = jtag->getDeviceID(i);
int irlen = db.idToIRLength(id);
if (irlen == 0)
{
run_irtest++;
break;
}
uint32_t idcmd = db.idToIDCmd(id);
for (j = 0; j < irlen; j = j+8)
ir_in[j>>3] = (idcmd>>j) & 0xff;
jtag->shiftIR(ir_in, ir_out);
jtag->cycleTCK(1);
jtag->shiftDR(NULL, &dout[i*4], 32);
if (jtag->byteArrayToLong(dout+i*4) != id)
{
fprintf(stderr, "IDCODE mismatch pos %d Read 0x%08lx vs 0x%08lx\n",
i, jtag->byteArrayToLong(dout+i*4), (unsigned long)id);
run_irtest++;
}
}
if(run_irtest)
{ /* ID Code did fail, to simple shift the IR chain */
fprintf(stderr, "Running IR_TEST %d times\n", test_count);
/* exercise the chain */
for(i=0; i<num; i++)
len += db.idToIRLength(jtag->getDeviceID(i));
fprintf(stderr, "IR len = %d\n", len);
jtag->setTapState(Jtag::TEST_LOGIC_RESET);
jtag->setTapState(Jtag::SHIFT_IR);
io->shiftTDITDO(din,dout,len,true);
jtag->nextTapState(true);
fprintf(stderr, "0x");
for(i=0; i <len>>3; i++)
fprintf(stderr, "%02x", dout[i]);
fprintf(stderr, " binary ");
k=len-1;
for(i = 0; i<num; i++)
{
int irlen = db.idToIRLength(jtag->getDeviceID(i));
for(j=0; j<irlen; j++)
{
fprintf(stderr, "%c",
(((dout[k>>3]>>(k&0x7)) &0x01) == 0x01)?'1':'0');
k--;
}
fprintf(stderr, " ");
}
fflush(stderr);
for(i=0; i<test_count&& !do_exit; i++)
{
jtag->setTapState(Jtag::SELECT_DR_SCAN);
jtag->setTapState(Jtag::SHIFT_IR);
io->shiftTDITDO(din,dcmp,len,true);
jtag->nextTapState(true);
if (memcmp(dout, dcmp, (len+1)>>3) !=0)
{
fprintf(stderr, "mismatch run %d\n", i);
failed++;
for(j=0; j <len>>3; j++)
fprintf(stderr, "%02x", dcmp[j]);
fprintf(stderr, " "); k=len-1;
for(i = 0; i<num; i++)
{
int irlen = db.idToIRLength(jtag->getDeviceID(i));
for(j=0; j<irlen; j++)
{
fprintf(stderr, "%c",
(((dcmp[k>>3]>>(k&0x7)) &0x01) == 0x01)?'1':'0');
k--;
}
fprintf(stderr, " ");
}
}
fflush(stderr);
if(i%1000 == 999)
{
fprintf(stderr, ".");
fflush(stderr);
}
}
fprintf(stderr, "\n");
}
else
{
fprintf(stderr, "Reading ID_CODE %d times\n", test_count);
memset(ir_in, 0, 256);
/* exercise the chain */
for(i=num-1; i>=0 && !do_exit; i--)
{
int irlen = db.idToIRLength(jtag->getDeviceID(i));
uint32_t idcmd = db.idToIDCmd(jtag->getDeviceID(i));
for(j=0; j<irlen; j++)
{
char l = (idcmd & (1<<j))?1:0;
ir_in[len>>3] |= ((l)?(1<<(len & 0x7)):0);
len++;
jtag->longToByteArray(jtag->getDeviceID(i), dcmp+((num -1 -i)*4));
}
}
fprintf(stderr, "Sending %d bits IDCODE Commands: 0x", len);
for(i=0; i <len; i+=8)
fprintf(stderr, "%02x", ir_in[i>>3]);
fprintf(stderr, "\n");
fprintf(stderr, "Expecting %d IDCODES :", num);
for(i=num-1; i >= 0; i--)
fprintf(stderr, " 0x%08lx", (unsigned long) jtag->getDeviceID(i));
jtag->tapTestLogicReset();
for(i=0; i<test_count&& !do_exit; i++)
{
jtag->setTapState(Jtag::SHIFT_IR);
io->shiftTDI(ir_in,len,true);
jtag->nextTapState(true);
jtag->setTapState(Jtag::SHIFT_DR);
io->shiftTDITDO(NULL,dout,num*32,true);
jtag->nextTapState(true);
jtag->setTapState(Jtag::TEST_LOGIC_RESET);
if(memcmp(dout, dcmp, num*4) !=0)
{
fprintf(stderr, "\nMismatch run %8d:", i+1);
failed++;
for(j=num-1; j>=0; j--)
fprintf(stderr," 0x%08lx", jtag->byteArrayToLong(dout+j*4));
fflush(stderr);
}
if(i%1000 == 999)
{
fprintf(stderr, ".");
fflush(stderr);
}
}
fprintf(stderr, "\n");
}
if (failed)
fprintf(stderr, "Failed %8d/%8d\n", failed, i);
signal (SIGINT, SIG_DFL);
}
int init_chain(Jtag &jtag, DeviceDB &db)
{
int num = jtag.getChain();
if (num == 0)
{
fprintf(stderr,"No JTAG Chain found\n");
return 0;
}
// Synchronise database with chain of devices.
for (int i=0; i<num; i++){
unsigned long id = jtag.getDeviceID(i);
int length = db.idToIRLength(id);
if (length > 0)
jtag.setDeviceIRLength(i,length);
else
{
fprintf(stderr,"Cannot find device having IDCODE=%07lx Revision %c\n",
id & 0x0fffffff, (int)(id >>28) + 'A');
return 0;
}
}
return num;
}
static int last_pos = -1;
unsigned long get_id(Jtag &jtag, DeviceDB &db, int chainpos)
{
bool verbose = jtag.getVerbose();
int num = jtag.getChain();
if (jtag.selectDevice(chainpos)<0)
{
fprintf(stderr, "Invalid chain position %d, must be >= 0 and < %d\n",
chainpos, num);
return 0;
}
unsigned long id = jtag.getDeviceID(chainpos);
if (verbose && (last_pos != chainpos))
{
fprintf(stderr, "JTAG chainpos: %d Device IDCODE = 0x%08lx\tDesc: %s\n",
chainpos, id, db.idToDescription(id));
fflush(stderr);
last_pos = chainpos;
}
return id;
}
void usage(bool all_options)
{
fprintf(stderr, "usage:\txc3sprog -c cable [options] <file0spec> <file1spec> ...\n");
fprintf(stderr, "\tList of known cables is given with -c follow by no or invalid cablename\n");
fprintf(stderr, "\tfilespec is filename:action:offset:style:length\n");
fprintf(stderr, "\taction on of 'w|W|v|r|R'\n");
fprintf(stderr, "\tw: erase whole area, write and verify\n");
fprintf(stderr, "\tW: Write with auto-sector erase and verify\n");
fprintf(stderr, "\tv: Verify device against filename\n");
fprintf(stderr, "\tr: Read from device,write to file, don't overwrite existing file\n");
fprintf(stderr, "\tR: Read from device and write to file, overwrite existing file\n");
fprintf(stderr, "\tDefault action is 'w'\n\n");
fprintf(stderr, "\tDefault offset is 0\n\n");
fprintf(stderr, "\tstyle: One of BIT|BIN|BPI|MCS|IHEX|HEX\n");
fprintf(stderr, "\tBIT: Xilinx .bit format\n");
fprintf(stderr, "\tBIN: Binary format\n");
fprintf(stderr, "\tBPI: Binary format not bit reversed\n");
fprintf(stderr, "\tMCS: Intel Hex File, LSB first\n");
fprintf(stderr, "\tIHEX: INTEL Hex format, MSB first (Use for Xilinx .mcs files!)\n");
fprintf(stderr, "\tHEX: Hex dump format\n");
fprintf(stderr, "\tDefault for FPGA|SPI|XCF is BIT\n");
fprintf(stderr, "\tDefault for CPLD is JED\n");
fprintf(stderr, "\tDefault for XMEGA is IHEX\n");
fprintf(stderr, "\tDefault length is whole device\n");
if (!all_options) exit(255);
fprintf(stderr, "\nPossible options:\n");
#define OPT(arg, desc) \
fprintf(stderr, " %-8s %s\n", (arg), (desc))
OPT("-p val[,val...]", "Use device at JTAG Chain position <val>.");
OPT("", "Default (0) is device connected to JTAG Adapter TDO.");
OPT("-e", "Erase whole device.");
OPT("-h", "Print this help.");
OPT("-I[file]", "Work on connected SPI Flash (ISF Mode),");
OPT("" , "after loading 'bscan_spi' bitfile if given.");
OPT("-j", "Detect JTAG chain, nothing else (default action).");
OPT("-l", "Program lockbits if defined in fusefile.");
OPT("-m <dir>", "Directory with XC2C mapfiles.");
OPT("-R", "Try to reconfigure device(No other action!).");
OPT("-T val", "Test chain 'val' times (0 = forever) or 10000 times"
" default.");
OPT("-J val", "Run at max with given JTAG Frequency, 0(default) means max. Rate of device");
OPT("", "Only used for FTDI cables for now");
OPT("-D", "Dump internal devlist and cablelist to files");
OPT("" , "In ISF Mode, test the SPI connection.");
OPT("-X opts", "Set options for XCFxxP programming");
OPT("-v", "Verbose output.");
fprintf(stderr, "\nProgrammer specific options:\n");
/* Parallel cable */
OPT("-d", "(pp only ) Parallel port device.");
/* USB devices */
OPT("-s num" , "(usb devices only) Serial number string.");
OPT("-L ", "(ftdi only ) Don't use LibUSB.");
fprintf(stderr, "\nDevice specific options:\n");
OPT("-E file", "(AVR only) EEPROM file.");
OPT("-F file", "(AVR only) File with fuse bits.");
#undef OPT
exit(255);
}
/* Parse a filename in the form
* aaaa.bb:action:0x10000|section:0x10000:rawhex:0x1000
* for name, action, offset|area, style, length
*
* return the Open File
*
* possible action
* w: erase whole area, write and verify
* W: Write with auto-sector erase and verify
* v: Verify device against filename
* r: Read from device and write to file, don't overwrite exixting file
* R: Read from device and write to file, overwrite exixting file
*
* possible sections:
* f: Flash
* a:
*
*/
FILE *getFile_and_Attribute_from_name(
char *name, char * action, char * section,
unsigned int *offset, FILE_STYLE *style, unsigned int *length)
{
FILE *ret;
char filename[256];
char *p = name, *q;
int len;
char localaction = 'w';
char localsection = 'a';
unsigned int localoffset = 0;
FILE_STYLE localstyle=STYLE_BIT;
unsigned int locallength = 0;
if(!p)
return NULL;
else
{
q = strchr(p,':');
#if defined(__WIN32__)
if (p[1] == ':') {
/* Assume we have a DOS path.
* Look for next colon or end-of-string.
*/
q = strchr(p + 2, ':');
}
#endif
if (q)
len = q-p;
else
len = strlen(p);
if (len>0)
{
int num = (len>255)?255:len;
strncpy(filename, p, num);
filename[num] = 0;
}
else
return NULL;
p = q;
if(p)
p ++;
}
/* Action*/
if(p)
{
q = strchr(p,':');
if (q)
len = q-p;
else
len = strlen(p);
if (len == 1)
localaction = *p;
else
localaction = 'w';
if (action)
{
if(localaction == 'W')
*action = localaction;
else
*action = tolower(localaction);
}
p = q;
if(p)
p ++;
}
/*Offset/Area*/
if(p)
{
q = strchr(p,':');
if (q)
len = q-p;
else
len = strlen(p);
if (!isdigit(*p))
{
localsection = *p;
if (section)
*section = localsection;
p++;
}
localoffset = strtol(p, NULL, 0);
if (offset)
*offset = localoffset;
p = q;
if(p)
p ++;
}
/*Style*/
if(p )
{
int res = 0;
q = strchr(p,':');
if (q)
len = q-p;
else
len = strlen(p);
if (len)
res = BitFile::styleFromString(p, &localstyle);
if(res)
{
fprintf(stderr, "\nUnknown format \"%*s\"\n", len, p);
return 0;
}
if (len && style)
*style = localstyle;
p = q;
if(p)
p ++;
}
/*Length*/
if(p)
{
locallength = strtol(p, NULL, 0);
p = strchr(p,':');
if (length)
*length = locallength;
if(p)
p ++;
}
if (tolower(localaction) == 'r')
{
if (!(strcasecmp(filename,"stdout")))
ret= stdout;
else
{
int res;
struct stat stats;
res = stat(filename, &stats);
if ((res == 0) && (localaction == 'r') && stats.st_size !=0)
{
fprintf(stderr, "File %s already exists. Aborting\n", filename);
return NULL;
}
ret=fopen(filename,"wb");
if(!ret)
fprintf(stderr, "Unable to open File %s. Aborting\n", filename);
}
}
else
{
#if 0
if (!(strcasecmp(filename,"stdin")))
ret = stdin;
else
#endif
{
ret = fopen(filename,"rb");
if(!ret)
fprintf(stderr, "Can't open datafile %s: %s\n", filename,
strerror(errno));
}
}
return ret;
}
void dump_lists(CableDB *cabledb, DeviceDB *db)
{
int fd;
FILE *fp_out;
char outname[17] = "cablelist.txt";
fd = open(outname, O_WRONLY|O_CREAT|O_EXCL, 0644);
if (fd <0 )
{
sprintf(outname,"cablelist.txt.1");
fd = open(outname, O_WRONLY|O_CREAT, 0644);
}
if (fd <0 )
{
fprintf(stderr, "Error creating file\n");
}
else
{
fprintf(stderr, "Dumping internal cablelist to %s\n", outname);
fp_out = fdopen(fd, "w");
if (fp_out)
{
fprintf(fp_out, "%-20s%-8s%-10sOptString\n", "#Alias", "Type", "Frequency");
cabledb->dumpCables(fp_out);
fclose(fp_out);
}
}
sprintf(outname,"devlist.txt");
fd = open(outname, O_WRONLY|O_CREAT|O_EXCL, 0644);
if (fd <0 )
{
sprintf(outname,"devlist.txt.1");
fd = open(outname, O_WRONLY|O_CREAT, 0644);
}
if (fd <0 )
{
fprintf(stderr, "Error creating file\n");
}
else
{
fprintf(stderr, "Dumping internal devicelist to %s\n", outname);
fp_out = fdopen(fd, "w");
if (fp_out)
{
fprintf(fp_out, "# IDCODE IR_len ID_Cmd Text\n");
db->dumpDevices(fp_out);
fclose(fp_out);
}
}
exit(0);
}
int main(int argc, char **args)
{
bool verbose = false;
bool dump = false;
bool verify = false;
bool lock = false;
bool detectchain = false;
bool chaintest = false;
bool spiflash = false;
bool reconfigure = false;
bool erase = false;
bool use_ftd2xx = false;
unsigned int jtag_freq= 0;
unsigned long id;
struct cable_t cable;
char const *dev = 0;
char const *eepromfile= 0;
char const *fusefile = 0;
char const *mapdir = 0;
FILE_STYLE in_style = STYLE_BIT;
FILE_STYLE out_style = STYLE_BIT;
int chainpos = 0;
int nchainpos = 1;
int chainpositions[MAXPOSITIONS] = {0};
vector<string> xcfopts;
int test_count = 0;
char const *serial = 0;
char *bscanfile = 0;
char *cablename = 0;
char osname[OSNAME_LEN];
DeviceDB db(NULL);
CableDB cabledb(NULL);
std::auto_ptr<IOBase> io;
int res;
get_os_name(osname, sizeof(osname));
// Produce release info from SVN tags
fprintf(stderr, "XC3SPROG (c) 2004-2011 xc3sprog project $Rev: 774 $ OS: %s\n"
"Free software: If you contribute nothing, expect nothing!\n"
"Feedback on success/failure/enhancement requests:\n"
"\thttp://sourceforge.net/mail/?group_id=170565 \n"
"Check Sourceforge for updates:\n"
"\thttp://sourceforge.net/projects/xc3sprog/develop\n\n",
osname);
// Start from parsing command line arguments
while(true) {
int c = getopt(argc, args, "?hCLc:d:DeE:F:i:I::jJ:Lm:o:p:Rs:S:T::vX:");
switch(c)
{
case -1:
goto args_done;
case 'v':
verbose = true;
break;
case 'C':
verify = true;
break;
case 'I':
spiflash = true;
bscanfile = optarg;
break;
case 'j':
detectchain = true;
break;
case 'L':
use_ftd2xx = true;
break;
case 'R':
reconfigure = true;
break;
case 'T':
chaintest = true;
if(optarg == 0)
test_count = 10000;
else
test_count = atoi(optarg);
if (test_count == 0)
test_count = INT_MAX;
break;
case 'J':
jtag_freq = atoi(optarg);
break;
case 'l':
lock = true;
break;
case 'c':
cablename = optarg;
break;
case 'm':
mapdir = optarg;
break;
case 'e':
erase = true;
break;
case 'D':
dump = true;
break;
case 'E':
eepromfile = optarg;
break;
case 'F':
fusefile = optarg;
break;
case 'o':
if (BitFile::styleFromString(optarg, &out_style) != 0)
{
fprintf(stderr, "\nUnknown format \"%s\"\n", optarg);
usage(false);
}
break;
case 'i':
if (BitFile::styleFromString(optarg, &in_style) != 0)
{
fprintf(stderr, "\nUnknown format \"%s\"\n", optarg);
usage(false);
}
break;
case 'd':
dev = optarg;
break;
case 'p':
{
char *p = optarg, *q;
for (nchainpos = 0; nchainpos <= MAXPOSITIONS; )
{
chainpositions[nchainpos] = strtoul(p, &q, 10);
if (p == q)
break;
p = q;
nchainpos++;
if (*p == ',')
p++;
else
break;
}
if (*p != '\0')
{
fprintf(stderr, "Invalid position specification \"%s\"\n", optarg);
usage(false);
}
}
chainpos = chainpositions[0];
break;
case 's':
serial = optarg;
break;
case 'X':
{
vector<string> new_opts = splitString(string(optarg), ',');
xcfopts.insert(xcfopts.end(), new_opts.begin(), new_opts.end());
break;
}
case '?':
case 'h':
default:
if (optopt == 'c')
{
fprintf(stdout, "Known Cables\n");
cabledb.dumpCables(stderr);
exit(1);
}
fprintf(stderr, "Unknown option -%c\n", c);
usage(true);
}
}
args_done:
argc -= optind;
args += optind;
if (dump)
dump_lists(&cabledb, &db);
if((argc < 0) || (cablename == 0)) usage(true);
if(argc < 1 && !reconfigure && !erase) detectchain = true;
if (verbose)
{
fprintf(stderr, "Using %s\n", db.getFile().c_str());
fprintf(stderr, "Using %s\n", cabledb.getFile().c_str());
}
res = cabledb.getCable(cablename, &cable);
if(res)
{
fprintf(stderr,"Can't find description for a cable named %s\n",
cablename);
fprintf(stdout, "Known Cables\n");
cabledb.dumpCables(stderr);
exit(1);
}
res = getIO( &io, &cable, dev, serial, verbose, use_ftd2xx, jtag_freq);
if (res) /* some error happend*/
{
if (res == 1) exit(1);
else usage(false);
}
Jtag jtag = Jtag(io.get());
jtag.setVerbose(verbose);
if (init_chain(jtag, db))
id = get_id (jtag, db, chainpos);
else
id = 0;
if(chaintest && !spiflash)
test_IRChain(&jtag, io.get(), db, test_count);
if (detectchain && !spiflash)
{
detect_chain(&jtag, &db);
return 0;
}
if (id == 0)
return 2;
unsigned int family = IDCODE_TO_FAMILY(id);
unsigned int manufacturer = IDCODE_TO_MANUFACTURER(id);
if (nchainpos != 1 &&
(manufacturer != MANUFACTURER_XILINX || family != FAMILY_XCF))
{
fprintf(stderr, "Multiple positions only supported in case of XCF\n");
usage(false);
}
if(spiflash)
return programSPI(jtag, argc, args, verbose, erase,
reconfigure, test_count,
bscanfile, family, db.idToDescription(id));
else if (manufacturer == MANUFACTURER_XILINX)
{
/* Probably XC4V and XC5V should work too. No devices to test at IKDA */
if( (family == FAMILY_XC2S) ||
(family == FAMILY_XC2SE) ||
(family == FAMILY_XC4VLX) ||
(family == FAMILY_XC4VFX) ||
(family == FAMILY_XC4VSX) ||
(family == FAMILY_XC3S) ||
(family == FAMILY_XC3SE) ||
(family == FAMILY_XC3SA) ||
(family == FAMILY_XC3SAN) ||
(family == FAMILY_XC3SD) ||
(family == FAMILY_XC6S) ||
(family == FAMILY_XC2V) ||
(family == FAMILY_XC5VLX) ||
(family == FAMILY_XC5VLXT) ||
(family == FAMILY_XC5VSXT) ||
(family == FAMILY_XC5VFXT) ||
(family == FAMILY_XC5VTXT) ||
(family == FAMILY_XC7)
)
return programXC3S(jtag, argc, args, verbose,
reconfigure, family);
else if (family == FAMILY_XCF)
{
return programXCF(jtag, db, argc, args, verbose,
erase, reconfigure,
db.idToDescription(id),
chainpositions, nchainpos, xcfopts);
}
else if( family == 0x4b) /* XC95XL XC95XV*/
{
return programXC95X(jtag, id, argc,args, verbose, erase,
db.idToDescription(id));
}
else if ((family & 0x7e) == 0x36) /* XC2C */
{
return programXC2C(jtag, id, argc, args, verbose, erase,
mapdir, db.idToDescription(id));
}
else
{
fprintf(stderr,
"Sorry, can't program Xilinx device '%s' from family 0x%02x "
"A more recent release may be able to.\n",
db.idToDescription(id), family);
return 1;
}
}
else if ( manufacturer == MANUFACTURER_ATMEL)
{
switch(db.idToIDCmd(id))
{
case 3:
return programXMega(&jtag, id, argc, args, verbose, erase, reconfigure,
db.idToDescription(id));
default:
return jAVR (jtag, id, args[0],verify, lock, eepromfile, fusefile);
}
}
else
fprintf(stderr,
"Sorry, can't program device '%s' from manufacturer 0x%02x "
"A more recent release may be able to.\n",
db.idToDescription(id), manufacturer);
return 1;
}
ProgAlg * makeProgAlg(Jtag &jtag, unsigned long id,
const vector<string>& xcfopts, bool checkopts)
{
if ((id & 0x000f0000) == 0x00050000)
{
// XCFxxP
ProgAlgXCFP *alg = new ProgAlgXCFP(jtag, id);
for (size_t i = 0; i < xcfopts.size(); i++)
{
const char *opt = xcfopts[i].c_str();
if (strcasecmp(opt, "parallel") == 0)
alg->setParallelMode(true);
else if (strcasecmp(opt, "serial") == 0)
alg->setParallelMode(false);
else if (strcasecmp(opt, "master") == 0)
alg->setMasterMode(true);
else if (strcasecmp(opt, "slave") == 0)
alg->setMasterMode(false);
else if (strcasecmp(opt, "fastclk") == 0)
alg->setFastClock(true);
else if (strcasecmp(opt, "slowclk") == 0)
alg->setFastClock(false);
else if (strcasecmp(opt, "extclk") == 0)
alg->setExternalClock(true);
else if (strcasecmp(opt, "intclk") == 0)
alg->setExternalClock(false);
else if (checkopts)
fprintf(stderr, "Ignoring unknown option '%s' for XCFxxP device\n", xcfopts[i].c_str());
}
return alg;
}
else
{
// XCFxxS
if (checkopts)
{
for (size_t i = 0; i < xcfopts.size(); i++)
fprintf(stderr, "Ignoring unknown option '%s' for XCFxxS device\n", xcfopts[i].c_str());
}
return new ProgAlgXCF(jtag, (id & 0x000ff000) >> 12);
}
}
int programXC3S(Jtag &jtag, int argc, char** args,
bool verbose, bool reconfig, int family)
{
ProgAlgXC3S alg(jtag, family);