forked from DediProgSW/SF100Linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpcmd.c
2112 lines (1903 loc) · 75.2 KB
/
dpcmd.c
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
#include "dpcmd.h"
#include "ChipInfoDb.h"
#include "FlashCommand.h"
#include "SerialFlash.h"
#include "board.h"
#include "project.h"
#include "usbdriver.h"
#include <fcntl.h>
#include <getopt.h>
#include <pthread.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/limits.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#define min(a, b) (a > b ? b : a)
extern unsigned char* pBufferForLastReadData[16];
extern unsigned char* pBufferforLoadedFile;
extern unsigned int g_uiFileChecksum;
extern unsigned long g_ulFileSize;
extern volatile bool g_bIsSF600[16];
extern volatile bool g_bIsSF700[16];
extern volatile bool g_bIsSF600PG2[16];
extern unsigned char g_BatchIndex;
unsigned int g_Vcc = vcc3_5V;
unsigned int g_Vpp = 9;
unsigned int g_uiAddr = 0;
size_t g_uiLen = 0;
unsigned int g_ucFill = 0xFF;
unsigned int g_ucTarget = 1;
unsigned int g_uiTimeout = 10000;
unsigned int g_ucSPIClock = clk_12M;
unsigned int g_uiBlink = 0;
unsigned int g_uiDevNum = 0;
//unsigned int g_uiDevSNNum = 0;
unsigned int g_uiDeviceID = 0;
unsigned int g_IO1Select = 0;
unsigned int g_IO4Select = 1;
char strTypeName[1024] = "\0";
bool g_bEnableVpp = false;
int g_StartupMode = STARTUP_APPLI_SF_1;
bool g_bStatus = true;
CHIP_INFO Chip_Info;
char* l_opt_arg;
struct CAddressRange DownloadAddrRange;
struct CAddressRange UploadAddrRange;
struct CAddressRange LockAddrrange;
unsigned int g_uiLockLen = 0;
unsigned int g_uiLockStart = 0;
char* g_parameter_read = "\0";
char* g_parameter_program = "\0";
char* g_parameter_loadfile_with_verify = "\0";
char* g_parameter_auto = "\0";
char* g_parameter_batch = "\0";
char* g_parameter_fsum = "\0";
char* g_parameter_raw = "\0";
char* g_parameter_raw_return = "\0";
char* g_parameter_addr = "0";
char* g_parameter_length = "\0";
char* g_parameter_fill = "FF";
char* g_parameter_type = "\0";
char* g_parameter_timeout = "300";
char* g_parameter_target = "1";
char* g_parameter_vcc = "NO";
//char* g_parameter_spi_clk="2";
char* g_parameter_blink = "0";
char* g_parameter_fw = "\0";
char g_LogPath[512] = { 0 };
/* to address by BUS:DEV; we'll parse the instructions from either the
* environment or command line options and
* usbdriver.c:FindUSBDevices() will acknowledge these settings */
extern unsigned g_usb_devnum;
extern unsigned g_usb_busnum;
unsigned long g_ucOperation;
struct memory_id g_ChipID;
char g_board_type[8];
char g_FPGA_ver[8];
char g_FW_ver[10];
char g_HW_ver[8];
int g_firmversion;
int g_CurrentSeriase = Seriase_25;
int m_isCanceled = 0;
int m_bProtectAfterWritenErase = 0;
int m_boEnReadQuadIO = 0;
int m_boEnWriteQuadIO = 0;
volatile bool g_is_operation_on_going = false;
bool g_is_operation_successful[16] = { false };
bool g_bDisplayTimer = true;
bool isSendFFsequence = false;
/* only print timing progress info if this many seconds ellapsed
* before the last time we did */
//static float g_tv_display_delta_s = 0.5;
//char* const short_options = "?Ldber:p:u:sf:I:R:a:l:vx:T:S:N:B:D:F:V:t:g:c:POik:";
//static const char* msg_info_unknownoption= "\nError: illegal option.";
//static const char* msg_err_lengthoverflow_abortion= "\nError: Length parameter exceeding file size, operation aborted\n";
//static const char* msg_err_addroverflow_abortion = "\nError: Exceeding chip size, operation aborted\n";
//static const char* msg_warning_optionignore_al = "\nWarning: either --addr or --length cannot be used with --auto, will be ignored\n";
//static const char* msg_err_openfile = "Error: Failed to open file.\n";
static const char* msg_err_communication = "Error: USB communication error or configuration file missing.\n";
static const char* msg_err_identifychip = "Error: chip not identified or chip is not supported.\n";
static const char* msg_err_timeout_abortion = "\nError: Abort abnormally due to timeout.\n*** You might need to un-plug the USB device to recover the state. ***\n";
static const char* msg_info_loading = "\nLoading file, please wait ...";
static const char* msg_info_checking = "\nChecking, please wait ...";
static const char* msg_info_erasing = "\nErasing, please wait ...";
static const char* msg_info_programming = "\nProgramming, please wait ...";
static const char* msg_info_reading = "\nReading, please wait ...";
static const char* msg_info_auto = "\nAuto Sequences, please wait ...";
static const char* msg_info_verifying = "\nVerifying, please wait ...";
static const char* msg_info_chipblank = "\nThe chip is blank";
static const char* msg_info_chipnotblank = "\nThe chip is NOT blank";
static const char* msg_info_eraseOK = "\nErase OK";
static const char* msg_info_erasefail = "\nError: Erase Failed";
static const char* msg_info_progOK = "\nProgram OK";
static const char* msg_info_progfail = "\nError: Program Failed";
static const char* msg_info_readOK = "\nRead OK";
static const char* msg_info_readfail = "\nRead Failed";
static const char* msg_info_autoOK = "\nAutomatic program OK";
static const char* msg_info_autofail = "\nError: Automatic program Failed";
static const char* msg_info_verifyOK = "\nVerify OK";
static const char* msg_info_verifyfail = "\nError: Verify Failed";
static const char* msg_info_firmwareupdate = "\nUpdating firmware, please wait...";
static const char* msg_info_firmwareupdateOK = "\nUpdate firmware OK";
static const char* msg_info_firmwareupdatefail = "\nError: Update firmware Failed";
char* const short_options = "?Ldber:p:u:z:sf:I:R:a:l:vx:T:S:N:B:t:g:c:PO:ik:1:4:U:E:";
struct option long_options[] = {
{ "help", 0, NULL, '?' },
{ "list", 0, NULL, 'L' },
{ "detect", 0, NULL, 'd' },
{ "check", 0, NULL, 'C' },
{ "blank", 0, NULL, 'b' },
{ "erase", 0, NULL, 'e' },
{ "read", 1, NULL, 'r' },
{ "prog", 1, NULL, 'p' },
{ "auto", 1, NULL, 'u' },
{ "batch", 1, NULL, 'z' },
{ "sum", 0, NULL, 's' },
{ "fsum", 1, NULL, 'f' },
{ "raw-instruction", 1, NULL, 'I' },
{ "raw-require-return", 1, NULL, 'R' },
{ "addr", 1, NULL, 'a' },
{ "length", 1, NULL, 'l' },
{ "verify", 0, NULL, 'v' },
{ "fill", 1, NULL, 'x' },
{ "type", 1, NULL, 'T' },
{ "lock-start", 1, NULL, 'S' },
{ "lock-length", 1, NULL, 'N' },
{ "blink", 1, NULL, 'B' },
{ "device", 1, NULL, 'D' },
{ "device-SN", 1, NULL, 'X' },
// { "fix-device", 1, NULL, 'F' },
{ "list-device-id", 1, NULL, 'V' },
{ "loadFile-with-verify", 1, NULL, 'W' },
{ "timeout", 1, NULL, 't' },
{ "target", 1, NULL, 'g' },
{ "vcc", 1, NULL, 'c' },
{ "vpp", 0, NULL, 'P' },
{ "log", 1, NULL, 'O' },
{ "silent", 0, NULL, 'i' },
{ "spi-clk", 1, NULL, 'k' },
{ "set-io1", 1, NULL, '1' },
{ "set-io4", 1, NULL, '4' },
{ "update-fw", 1, NULL, 'U' },
// { "display-delta", 1, NULL, 'E' },
{ "devnum", 1, NULL, 'G' },
{ "busnum", 1, NULL, 'H' },
{ 0, 0, 0, 0 },
};
int OpenUSB(void);
#if 0
int verbose = 1;
void print_endpoint(struct usb_endpoint_descriptor *endpoint)
{
printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
printf(" wMaxPacketSize: %d\n", endpoint->wMaxPacketSize);
printf(" bInterval: %d\n", endpoint->bInterval);
printf(" bRefresh: %d\n", endpoint->bRefresh);
printf(" bSynchAddress: %d\n", endpoint->bSynchAddress);
}
void print_altsetting(struct usb_interface_descriptor *interface)
{
int i;
printf(" bInterfaceNumber: %d\n", interface->bInterfaceNumber);
printf(" bAlternateSetting: %d\n", interface->bAlternateSetting);
printf(" bNumEndpoints: %d\n", interface->bNumEndpoints);
printf(" bInterfaceClass: %d\n", interface->bInterfaceClass);
printf(" bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
printf(" bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
printf(" iInterface: %d\n", interface->iInterface);
for (i = 0; i < interface->bNumEndpoints; i++)
print_endpoint(&interface->endpoint[i]);
}
void print_interface(struct usb_interface *interface)
{
int i;
for (i = 0; i < interface->num_altsetting; i++)
print_altsetting(&interface->altsetting[i]);
}
void print_configuration(struct usb_config_descriptor *config)
{
int i;
printf(" wTotalLength: %d\n", config->wTotalLength);
printf(" bNumInterfaces: %d\n", config->bNumInterfaces);
printf(" bConfigurationValue: %d\n", config->bConfigurationValue);
printf(" iConfiguration: %d\n", config->iConfiguration);
printf(" bmAttributes: %02xh\n", config->bmAttributes);
printf(" MaxPower: %d\n", config->MaxPower);
for (i = 0; i < config->bNumInterfaces; i++)
print_interface(&config->interface[i]);
}
int print_device(struct usb_device *dev, int level)
{
usb_dev_handle *udev;
char description[256];
char string[256];
int ret, i;
udev = usb_open(dev);
if (udev) {
if (dev->descriptor.iManufacturer) {
ret = usb_get_string_simple(udev, dev->descriptor.iManufacturer, string, sizeof(string));
if (ret > 0)
snprintf(description, sizeof(description), "%s - ", string);
else
snprintf(description, sizeof(description), "%04X - ",
dev->descriptor.idVendor);
} else
snprintf(description, sizeof(description), "%04X - ",
dev->descriptor.idVendor);
if (dev->descriptor.iProduct) {
ret = usb_get_string_simple(udev, dev->descriptor.iProduct, string, sizeof(string));
if (ret > 0)
snprintf(description + strlen(description), sizeof(description) -
strlen(description), "%s", string);
else
snprintf(description + strlen(description), sizeof(description) -
strlen(description), "%04X", dev->descriptor.idProduct);
} else
snprintf(description + strlen(description), sizeof(description) -
strlen(description), "%04X", dev->descriptor.idProduct);
} else
snprintf(description, sizeof(description), "%04X - %04X",
dev->descriptor.idVendor, dev->descriptor.idProduct);
printf("%.*sDev #%d: %s\n", level * 2, " ", dev->devnum,
description);
if (udev && verbose) {
if (dev->descriptor.iSerialNumber) {
ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof(string));
if (ret > 0)
printf("%.*s - Serial Number: %s\n", level * 2,
" ", string);
}
}
if (udev)
usb_close(udev);
if (verbose) {
if (!dev->config) {
printf(" Couldn't retrieve descriptors\n");
return 0;
}
for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
print_configuration(&dev->config[i]);
} else {
for (i = 0; i < dev->num_children; i++)
print_device(dev->children[i], level + 1);
}
return 0;
}
int Check(int argc, char *argv[])
{
struct usb_bus *bus;
if (argc > 1 && !strcmp(argv[1], "-v"))
verbose = 1;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_busses; bus; bus = bus->next) {
if (bus->root_dev && !verbose) {
printf("root device:");
print_device(bus->root_dev, 0);
}
else {
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next) {
printf("non-root device:");
print_device(dev, 0);
}
}
}
return 0;
}
#endif
int Sequence()
{
// *** the calling order in the following block must be kept as is ***
bool boResult = true;
boResult &= BlankCheck();
if (boResult == false) {
if (!(g_ucOperation & ERASE))
return EXCODE_FAIL_BLANK;
}
if ((!((g_ucOperation & BLANK) && (boResult == true)) || (!(g_ucOperation & BLANK)))) {
boResult = Erase();
if (boResult == false)
return EXCODE_FAIL_ERASE;
}
boResult &= Program();
if (boResult == false)
return EXCODE_FAIL_PROG;
boResult &= Read();
if (boResult == false)
return EXCODE_FAIL_READ;
boResult &= Auto();
if (boResult == false)
return EXCODE_FAIL_BATCH;
boResult &= CalChecksum();
if (boResult == true) {
if (g_ucOperation & CSUM) {
if (g_uiLen == 0) {
if (g_uiDevNum == 0)
return CRC32(pBufferForLastReadData[0], Chip_Info.ChipSizeInByte);
else
return CRC32(pBufferForLastReadData[g_uiDevNum - 1], Chip_Info.ChipSizeInByte);
} else {
if (g_uiDevNum == 0)
return CRC32(pBufferForLastReadData[0], g_uiLen);
else
return CRC32(pBufferForLastReadData[g_uiDevNum - 1], g_uiLen);
}
}
} else
return EXCODE_FAIL_CHKSUM;
boResult &= Verify();
if (boResult == false)
return EXCODE_FAIL_VERIFY;
boResult &= LoadFileWithVerify();
if (boResult == false)
return EXCODE_FAIL_LOADFILEWITHVERIFY;
return EXCODE_PASS;
}
void print_message_function(void* ptr)
{
char* message;
message = (char*)ptr;
printf("%s \n", message);
}
void GetLogPath(char* pBuf)
{
memset(pBuf, 0, 512);
if (readlink("/proc/self/exe", pBuf, 512) != -1) {
if (strlen(pBuf) < (511 - sizeof("/log.txt")))
strcat(pBuf, "/log.txt");
// printf("%s\n",pBuf);
}
}
void EnterStandaloneMode(int Index)
{
if ((g_bIsSF600[Index] == true) || is_SF700_Or_SF600PG2(Index))
LeaveSF600Standalone(false, Index);
}
void LeaveStandaloneMode(int Index)
{
if ((g_bIsSF600[Index] == true) || is_SF700_Or_SF600PG2(Index))
LeaveSF600Standalone(true, Index);
}
void WriteLog(int ErrorCode, bool Init)
{
FILE* FileOut = NULL; /* output files */
time_t rawtime;
struct tm* timeinfo;
char buffer[512] = { 0 };
char* Result[] = {
" PASS\n",
" FAIL\n",
" Erase Fail\n",
" Program Fail\n",
" Verify Fail\n",
" Read Fail\n",
" Blank Check Fail\n",
" Auto Batch Fail\n",
" Checksum Fail\n",
" Identify Fail\n",
" Unknow Fail\n"
};
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 80, "%Y-%b-%d %T", timeinfo);
if (Init) {
FileOut = fopen(g_LogPath, "wt");
if (FileOut != NULL)
fprintf(FileOut, "%s%s%s", buffer, " USB communication =", Result[ErrorCode]);
} else {
FileOut = fopen(g_LogPath, "a+t");
if (FileOut != NULL)
fprintf(FileOut, "%s%s", buffer, Result[ErrorCode]);
}
if (FileOut != NULL)
fclose(FileOut);
}
int GetConfigVer()
{
char file_line_buf[512];
char test[80];
char *pch, *tok;
bool bFound = 0;
int Ver = 0;
FILE* fp; /*Declare file pointer variable*/
if ((fp = openChipInfoDb()) == NULL)
return 1;
while (fgets(file_line_buf, 512, fp) != NULL) {
if (strstr(file_line_buf, "DediProgChipDatabase") && bFound == 0)
bFound = 1;
pch = strstr(file_line_buf, "Ver");
if (bFound == 1 && pch != NULL) {
memset(test, '\0', 80);
strcpy(test, pch + strlen("Ver"));
tok = strtok(test, "\"= \t");
Ver = strtol(tok, NULL, 10);
break;
}
}
fclose(fp);
return Ver;
}
int main(int argc, char* argv[])
{
#if 0 //Check all USB device capability //evy test SF700
Check(argc,argv);
return;
#endif
int c;
int iExitCode = EXCODE_PASS;
bool bDetect = false;
bool bDevice = false;
bool bDeviceSN = false;
unsigned long r;
char* env;
printf("\nDpCmd %s 1.14.20.%02d Engine Version:\nLast Built on %s\n\n",
#if defined(__APPLE__)
"macOS",
#elif defined(__FreeBSD__)
"FreeBSD",
#elif defined(__linux__)
"Linux",
#else
"Unknown",
#endif
GetConfigVer(), __DATE__); // 1. new feature.bug.configS
g_ucOperation = 0;
GetLogPath(g_LogPath);
if (argc == 1) {
cli_classic_usage(false);
return 0;
}
/*
* Obtain the (optional) bus number and device number to
* filter by from the environment.
*
* Why? Because the OpenUSB() initialization happens *before*
* the command line options are parsed (where we'd take
* --busnum and --devnum) and we want to enable a path to
* disallow the tool from touching any device which is not the
* one we want to.
*
* In the long term, rearranging the code would allow getting
* rid of this hack.
*/
env = getenv("DPCMD_USB_DEVNUM");
if (env) {
r = strtoul(env, NULL, 10);
if (r == ULONG_MAX || r >= 256) {
fprintf(stderr, "E: invalid USB device number in"
" DPCMD_USB_DEVNUM; expected 1-255\n");
return 1;
}
g_usb_devnum = (unsigned char)r;
}
env = getenv("DPCMD_USB_BUSNUM");
if (env) {
r = strtoul(env, NULL, 10);
if (r == ULONG_MAX || r >= 256) {
fprintf(stderr, "E: invalid USB bus number in"
" DPCMD_USB_BUSNUM\n");
return 1;
}
g_usb_busnum = (unsigned char)r;
}
while ((c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
switch (c) {
case '?':
cli_classic_usage(true);
CloseProject();
ExitProgram();
return EXCODE_PASS;
case 'L':
g_ucOperation |= LIST_TYPE;
break;
case 'd':
bDetect = true;
break;
case 'C':
g_ucOperation |= CHECK_INFO;
break;
case 'b':
g_ucOperation |= BLANK;
break;
case 'e':
g_ucOperation |= ERASE;
break;
case 'r':
g_parameter_read = optarg;
g_ucOperation |= READ_TO_FILE;
break;
case 'p':
g_parameter_program = optarg;
g_ucOperation |= PROGRAM;
break;
case 'u':
g_parameter_auto = optarg;
g_ucOperation |= BATCH;
g_BatchIndex = 2;
break;
case 'z':
g_parameter_batch = optarg;
g_ucOperation |= BATCH;
g_BatchIndex = 1;
break;
case 's': // display chip checksum
g_ucOperation |= CSUM;
break;
case 'f': //display file checksum
g_parameter_fsum = optarg;
g_ucOperation |= FSUM;
break;
case 'I':
g_parameter_raw = optarg;
break;
case 'R': //raw-require-return
g_parameter_raw_return = optarg;
break;
/* Optional Switches that add fine-tune ability to Basic Switches: */
case 'a':
g_parameter_addr = optarg;
g_uiAddr = 0;
sscanf(optarg, "%x", &g_uiAddr);
break;
case 'l':
g_uiLen = 0;
if (strstr(optarg, "0x"))
sscanf(optarg, "%zx", &g_uiLen);
else
sscanf(optarg, "%zu", &g_uiLen);
break;
case 'v':
g_ucOperation |= VERIFY;
break;
case 'x':
g_ucFill = 0xFF;
sscanf(optarg, "%x", &g_ucFill);
break;
case 'T':
g_parameter_type = optarg; //type
break;
case 'S': //lock start
sscanf(optarg, "%x", &g_uiLockStart);
// l_opt_arg = optarg;
// printf("hexadecimal starting address (with arg: %s)\n", l_opt_arg);
break;
case 'N': //lock length
sscanf(optarg, "%x", &g_uiLockLen);
break;
case 'B':
sscanf(optarg, "%d", &g_uiBlink);
g_ucOperation |= BLINK;
break;
case 'D': // device
bDevice = true;
sscanf(optarg, "%x", &g_uiDevNum);
break;
case 'X': // device-SN
l_opt_arg = optarg;
//printf("activate only the programmer connected to USBx (with arg: %s)\n", l_opt_arg);
bDeviceSN = true;
break;
case 'W':
// l_opt_arg = optarg;
// printf("Fix programmer serial number with programmer sequence. (with arg: %s)\n", l_opt_arg);
// break;
g_parameter_loadfile_with_verify = optarg;
g_ucOperation |= LOADFILEWITHVERIFY;
break;
case 'V':
sscanf(optarg, "%d", &g_uiDeviceID);
g_ucOperation |= DEVICE_ID; //list device id
break;
/* Miscellaneous options: */
case 't':
l_opt_arg = optarg;
sscanf(l_opt_arg, "%d", &g_uiTimeout);
break;
case 'g':
g_parameter_target = optarg;
sscanf(optarg, "%d", &g_ucTarget);
break;
case 'c':
g_parameter_vcc = optarg;
sscanf(optarg, "%d", &g_Vcc);
break;
case 'P':
g_bEnableVpp = true;
break;
case 'O': //Log file
strcpy(g_LogPath, optarg);
// printf("%s\n",g_LogPath);
break;
case 'i':
g_bDisplayTimer = false;
break;
case 'k':
l_opt_arg = optarg;
sscanf(l_opt_arg, "%d", &g_ucSPIClock);
break;
case '1':
l_opt_arg = optarg;
sscanf(l_opt_arg, "%d", &g_IO1Select);
break;
case '4':
l_opt_arg = optarg;
sscanf(l_opt_arg, "%d", &g_IO4Select);
break;
case 'U':
g_parameter_fw = optarg;
iExitCode = FirmwareUpdate();
goto Exit;
break;
#if 0
case 'E':
g_tv_display_delta_s = atof(optarg);
if (g_tv_display_delta_s <= 0) {
fprintf(stderr, "E: invalid number"
" of seconds for display delta"
" (%f); expect larger than"
" zero\n",
g_tv_display_delta_s);
return 1;
}
break;
#endif
case 'G':
r = strtoul(optarg, NULL, 10);
if (r == ULONG_MAX || r >= 256) {
fprintf(stderr, "E: invalid device"
" number; expected 1-255\n");
return 1;
}
g_usb_devnum = (unsigned char)r;
break;
case 'H':
r = strtoul(optarg, NULL, 10);
if (r == ULONG_MAX) {
fprintf(stderr, "E: invalid bus"
" number\n");
return 1;
}
g_usb_busnum = (unsigned)r;
break;
default:
break;
}
}
if (OpenUSB() == 0)
iExitCode = EXCODE_FAIL_USB;
LeaveStandaloneMode(0);
QueryBoard(0);
int dev_cnt = get_usb_dev_cnt();
if (CheckProgrammerInfo()) {
iExitCode = EXCODE_PASS;
goto Exit;
}
if (bDetect == true) {
if (bDevice == false) {
if (bDeviceSN) {
char* arg_temp = &l_opt_arg[2];
int arg_int = atoi(arg_temp);
for (int i = 0; i < dev_cnt; i++) {
int dwUID = ReadUID(i);
if (arg_int == dwUID) {
g_uiDevNum = i + 1;
break;
}
}
int dwUID = ReadUID(g_uiDevNum - 1);
if (is_SF700_Or_SF600PG2(g_uiDevNum - 1)) {
if (g_bIsSF700[g_uiDevNum - 1] == true) {
printf("\nDevice %d (SF7%05d):\tdetecting chip\n", g_uiDevNum, dwUID);
} else if (g_bIsSF600PG2[g_uiDevNum - 1] == true) {
printf("\nDevice %d (S6B%05d):\tdetecting chip\n", g_uiDevNum, dwUID);
}
} else if ((g_bIsSF600[g_uiDevNum - 1] == true) && (dwUID / 600000) > 0) {
printf("\nDevice %d (SF%06d):\tdetecting chip\n", g_uiDevNum, dwUID);
} else if ((dwUID / 100000) > 0) {
printf("\nDevice %d (SF%06d):\tdetecting chip\n", g_uiDevNum, dwUID);
} else {
printf("\nDevice %d (DP%06d):\tdetecting chip\n", g_uiDevNum, dwUID);
}
WriteLog(iExitCode, true);
Chip_Info = GetFirstDetectionMatch(strTypeName, g_uiDevNum - 1);
if (Chip_Info.UniqueID != 0) {
if (strlen(strTypeName)) {
printf("By reading the chip ID, the chip applies to [ %s ]\n\n", strTypeName);
printf("%s chip size is %zd bytes.\n", Chip_Info.TypeName, Chip_Info.ChipSizeInByte);
} else {
printf("%s", msg_err_identifychip);
iExitCode = EXCODE_FAIL_IDENTIFY;
}
} else {
printf("%s", msg_err_identifychip);
iExitCode = EXCODE_FAIL_IDENTIFY;
}
} else {
for (int i = 0; i < dev_cnt; i++) {
//printf("%s\n",g_LogPath);
int dwUID = ReadUID(i);
if (is_SF700_Or_SF600PG2(i)) {
if (g_bIsSF700[i] == true) {
printf("\nDevice %d (SF7%05d):\tdetecting chip\n", i + 1, dwUID);
} else if (g_bIsSF600PG2[i] == true) {
printf("\nDevice %d (S6B%05d):\tdetecting chip\n", i + 1, dwUID);
}
} else if ((g_bIsSF600[i] == true) && (dwUID / 600000) > 0) {
printf("\nDevice %d (SF%06d):\tdetecting chip\n", i + 1, dwUID);
} else if ((dwUID / 100000) > 0) {
printf("\nDevice %d (SF%06d):\tdetecting chip\n", i + 1, dwUID);
} else {
printf("\nDevice %d (DP%06d):\tdetecting chip\n", i + 1, dwUID);
}
WriteLog(iExitCode, true);
Chip_Info = GetFirstDetectionMatch(strTypeName, i);
if (Chip_Info.UniqueID != 0) {
if (strlen(strTypeName)) {
printf("By reading the chip ID, the chip applies to [ %s ]\n", strTypeName);
printf("%s chip size is %zd bytes.\n", Chip_Info.TypeName, Chip_Info.ChipSizeInByte);
} else {
printf("%s", msg_err_identifychip);
iExitCode = EXCODE_FAIL_IDENTIFY;
}
} else {
printf("%s", msg_err_identifychip);
iExitCode = EXCODE_FAIL_IDENTIFY;
}
}
}
} else if (g_uiDevNum != 0) {
WriteLog(iExitCode, true);
printf("%d,\tdetecting chip\n", g_uiDevNum);
Chip_Info = GetFirstDetectionMatch(strTypeName, g_uiDevNum - 1);
if (Chip_Info.UniqueID != 0) {
//printf("strlen(strTypeName)=%ld\n",strlen(strTypeName));
if (strlen(strTypeName)) {
printf(" \tBy reading the chip ID, the chip applies to [ %s ]\n\n", strTypeName);
printf(" \t%s chip size is %zd bytes.\n", Chip_Info.TypeName, Chip_Info.ChipSizeInByte);
} else {
printf("%s", msg_err_identifychip);
iExitCode = EXCODE_FAIL_IDENTIFY;
}
} else {
printf("%s", msg_err_identifychip);
iExitCode = EXCODE_FAIL_IDENTIFY;
}
} else {
printf("The number of programmer is not defined!\n");
iExitCode = EXCODE_FAIL_IDENTIFY;
}
goto Exit;
} else {
if (bDeviceSN) {
char* arg_temp = &l_opt_arg[2];
int arg_int = atoi(arg_temp);
for (int i = 0; i < dev_cnt; i++) {
int dwUID = ReadUID(i);
if (arg_int == dwUID) {
g_uiDevNum = i + 1;
break;
}
}
}
WriteLog(iExitCode, true);
}
if (iExitCode != EXCODE_PASS)
goto Exit;
iExitCode = Handler();
Exit:
printf("\n\n");
CloseProject();
ExitProgram();
WriteLog(iExitCode, false);
return iExitCode;
}
void ExitProgram(void)
{
for (int i = 0; i < get_usb_dev_cnt(); i++) {
if (pBufferForLastReadData[i] != NULL)
free(pBufferForLastReadData[i]);
}
if (pBufferforLoadedFile != NULL)
free(pBufferforLoadedFile);
}
int FirmwareUpdate()
{
FW_INFO Data;
FILE* pFile;
size_t result, filesize;
int fwVer;
// unsigned char vBuffer[8] ;
int fw[3];
pFile = fopen(g_parameter_fw, "rb");
if (pFile == NULL) {
printf("Can not open %s \n", g_parameter_fw);
return EXCODE_FAIL_UPDATE_FW;
}
// obtain file size:
fseek(pFile, 0, SEEK_END);
filesize = ftell(pFile);
if (filesize < sizeof(FW_INFO)) {
printf("File %s too small.\n", g_parameter_fw);
return -1;
}
fseek(pFile, 0, SEEK_SET);
// rewind (pFile);
// copy the file into the buffer:
result = fread((unsigned char*)&Data, 1, sizeof(FW_INFO), pFile);
if (result != sizeof(FW_INFO)) {
printf("File %s read problem.\n", g_parameter_fw);
return -1;
}
fclose(pFile);
if (Data.dwSignature != 0x44504657) {
printf("This file is not DediProg firmware format.\n");
return EXCODE_FAIL_UPDATE_FW;
}
sscanf(&Data.Version[0], "%d.%d.%d", &fw[0], &fw[1], &fw[2]);
fwVer = ((fw[0] << 16) | (fw[1] << 8) | fw[2]);
printf("Current Firmware is %x, New Firmware Version is %x\n", g_firmversion, fwVer);
if (strstr(g_board_type, Data.Programmer) == NULL)
printf("Error: This firmware for %s!\n", Data.Programmer);
if ((strstr(g_board_type, Data.Programmer) == NULL) || ((fwVer & 0xff0000) != (g_firmversion & 0xff0000))) {
printf("Error: This firmware can not be updated on this programmer!\n");
return EXCODE_FAIL_UPDATE_FW;
}
printf("%s", msg_info_firmwareupdate);
Run(UPDATE_FIRMWARE, g_uiDevNum);
if (Wait(msg_info_firmwareupdateOK, msg_info_firmwareupdatefail))
return 0;
return 0;
}
void cli_classic_usage(bool IsShowExample)
{
printf("Basic Usages:\n");
printf("./dpcmd -uxxx\n");
printf("./dpcmd --auto=xxx\n");
if (IsShowExample)
printf("Usage Examples:\n"
"1. dpcmd -r\"f:\\file.bin\", \n reads the chip and save it into a file \"file.bin\"\n"
"2. dpcmd -rSTDOUT -a0x100 -l0x23, \n reads 0x23 bytes starting from 0x100 and display it on the screen\n"
"3. dpcmd -ufile.bin, \n Update the whole chip with file.bin\n"
"4. Dpcmd -ufile.bin -a0x100, \n Replace the serial flash contents for the size of file.bin with file.bin.\n"
" (contents outside the replace area will be kept unchanged)\n"
"5. dpcmd -pfile.bin -a0x100, \n program file.bin into the serial flash starting from address 0x100\n"
" (the target area is assumed to be blank already)\n"
"6. dpcmd -pfile.bin -x0xaa, \n programs file.bin into the serial flash and fill the rest area with 0xaa\n"
" (the target area is assumed to be blank already)\n"
"Remark: -a, -l only works with -p, -r, -s and -u\n"
"Remark: -x only works with -p");
printf("(space is not needed between the switches and parameters. E.g. dpcmd -ubio.bin)\n");
printf(" -? [ --help ] show this help message\n"
" --list print supported chip list\n"
" --check get programmer information\n"
" -d [ --detect ] detect chip\n"
" -b [ --blank ] blank check\n"
" -e [ --erase ] erase entire chip\n"
" -r [ --read ] arg read chip contents and save to a bin/hex/s19 file\n"
" -p [ --prog ] arg program chip without erase\n"
" -u [ --auto ] arg automatically run the following sequence:\n"
" - Read the memory content\n"
" - Compare the memory content\n"
" - Erase only the sectors with some differences\n"
" - Program only the erased sectors with file data from address 0\n"
" -z [ --batch ] arg automatically run the following sequence:\n"
" - check if the chip is blank or not;\n"
" - erase the entire chip(if not blank);\n"
" - program a whole file starting from address 0\n"
" -s [ --sum ] display chip content checksum\n"
" -f [ --fsum ] arg display the file checksum\n"
" --raw-instruction arg issue raw serial flash instructions.\n"
" - use spaces("
") to delimite bytes.\n"
" - instructions must be enclosed in double quotation marks(\"\")\n"
" Example:\n"
" dpcmd --raw-instruction \"03 FF 00 12\"\n"
" --raw-require-return arg (=0) decimal bytes of result to return in decimal\n"
" after issuing raw instructions.\n"
" - used along with --raw-instruction only.\n"