-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwch-ch56x-isp.c
1069 lines (915 loc) · 23.5 KB
/
wch-ch56x-isp.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
/* SPDX-License-Identifier: GPL-2.0-only */
/* SPDX-FileCopyrightText: 2022 Jules Maselbas */
/* SPDX-FileCopyrightText: 2022-2023 Benjamin Vernoux */
#include <unistd.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <libusb.h>
#include <cargs.h>
#define __noreturn __attribute__((noreturn))
//#define DEBUG
#ifdef DEBUG
#define dbg_printf printf
#else
#define dbg_printf(...) do { ; } while(0)
#endif
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
#define BIT(x) (1UL << (x))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define LEN(a) (sizeof(a) / sizeof(*a))
#define ALIGN(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
#include "devices.h"
#define MAX_PACKET_SIZE 64
#define SECTOR_SIZE 1024
/*
* All readable and writable registers.
* - `RDPR`: Read Protection
* - `USER`: User Config Byte (normally in Register Map datasheet)
* - `WPR`: Write Protection Mask, 1=unprotected, 0=protected
*
* | BYTE0 | BYTE1 | BYTE2 | BYTE3 |
* |--------|--------|--------|--------|
* | RDPR | nRDPR | USER | nUSER |
* | DATA0 | nDATA0 | DATA1 | nDATA1 |
* | WPR0 | WPR1 | WPR2 | WPR3 |
*/
#define CFG_MASK_RDPR_USER_DATA_WPR 0x07
#define CFG_MASK_BTVER 0x08 /* Bootloader version, in the format of `[0x00, major, minor, 0x00]` */
#define CFG_MASK_UID 0x10 /* Device Unique ID */
#define CFG_MASK_ALL 0x1f /* All mask bits of CFGs */
#define CMD_IDENTIFY 0xa1
#define CMD_ISP_END 0xa2
#define CMD_ISP_KEY 0xa3
#define CMD_ERASE 0xa4
#define CMD_PROGRAM 0xa5
#define CMD_VERIFY 0xa6
#define CMD_READ_CONFIG 0xa7
#define CMD_WRITE_CONFIG 0xa8
#define CMD_DATA_ERASE 0xa9
#define CMD_DATA_PROGRAM 0xaa
#define CMD_DATA_READ 0xab
#define CMD_WRITE_OTP 0xc3
#define CMD_READ_OTP 0xc4
#define CMD_SET_BAUD 0xc5
#define BTVER_2_7 (0x00020700)
#define ISP_VID 0x4348
#define ISP_PID 0x55e0
#define ISP_EP_OUT (2 | LIBUSB_ENDPOINT_OUT)
#define ISP_EP_IN (2 | LIBUSB_ENDPOINT_IN)
__noreturn static void die(const char *errstr, ...);
__noreturn static void version_print(void);
static void usb_init(void);
static void usb_fini(void);
static size_t usb_send(size_t len, u8 *buf);
static size_t usb_recv(size_t len, u8 *buf);
static void isp_init(void);
static void isp_fini(void);
static size_t isp_send_cmd(u8 cmd, u16 len, const u8 *data);
static size_t isp_recv_cmd(u8 cmd, u16 len, u8 *data);
libusb_context *usb = NULL;
libusb_device_handle *dev = NULL;
unsigned int interface;
unsigned int kernel;
#define CURR_TIME_SIZE (40)
char currTime[CURR_TIME_SIZE+1] = "";
struct timeval start_tv;
struct timeval curr_tv;
void *flash_file_bin = NULL;
FILE *flash_file_fp = NULL;
static u8 dev_id;
static u8 dev_type;
static u8 dev_uid[8];
static u32 dev_btver;
static size_t dev_uid_len;
static u8 isp_key[30]; /* all zero key */
static u8 xor_key[8];
static struct dev *dev_db;
static char *argv0;
/**
* This is the main configuration of all options available.
*/
static struct cag_option options[] =
{
{
.identifier = 'V',
.access_letters = "V",
.access_name = "Version",
.value_name = NULL,
.description = "Print version"
},
{
.identifier = 'h',
.access_letters = "h",
.access_name = "help",
.value_name = NULL,
.description = "Show help"
},
{
.identifier = 'c',
.access_letters = "c",
.access_name = "config",
.value_name = NULL,
.description = "Print CH569 config(after isp_init and before isp_fini)"
},
{
.identifier = 'p',
.access_letters = "p",
.access_name = "progress",
.value_name = NULL,
.description = "Display progress"
},
{
.identifier = 'v',
.access_letters = "v",
.access_name = "verify",
.value_name = NULL,
.description = "Do verify after erase/program"
},
{
.identifier = 'r',
.access_letters = "r",
.access_name = "reset",
.value_name = NULL,
.description = "Reset MCU at end"
},
{
.identifier = 'd',
.access_letters = "d",
.access_name = "mcudebug",
.value_name = "VALUE",
.description = "Set MCU debug mode on or off"
},
{
.identifier = 'f',
.access_letters = "f",
.access_name = "flashfile",
.value_name = "VALUE",
.description = "Flash file (binary file *.bin)"
}
};
/**
* This is a custom project configuration structure where you can store the
* parsed information.
*/
struct cag_configuration
{
int do_progress;
int do_reset;
int do_verify;
int do_show_config;
int do_mcu_debug;
const char* mcu_debug_mode;
int do_flash_file;
const char* flash_file;
};
static struct cag_configuration config = { 0 }; /* Default values for the option(s) */
float TimevalDiff(const struct timeval *a, const struct timeval *b)
{
return (a->tv_sec - b->tv_sec) + 1e-6f * (a->tv_usec - b->tv_usec);
}
void get_CurrentTime(char* date_time_ms, int date_time_ms_max_size)
{
#define CURRENT_TIME_SIZE (30)
char currentTime[CURRENT_TIME_SIZE+1] = "";
time_t rawtime;
struct tm * timeinfo;
struct timeval curTime;
gettimeofday(&curTime, NULL);
int milli = curTime.tv_usec / 1000;
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(currentTime, CURRENT_TIME_SIZE, "%Y-%m-%d %H:%M:%S", timeinfo);
snprintf(date_time_ms, (date_time_ms_max_size-1), "%s.%03d", currentTime, milli);
}
void printf_timing(const char *fmt, ...)
{
va_list args;
gettimeofday(&curr_tv, NULL);
get_CurrentTime(currTime, CURR_TIME_SIZE);
printf("%s (%05.03f s) ", currTime, TimevalDiff(&curr_tv, &start_tv));
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
void print_hex(const uint8_t* data, uint8_t size)
{
uint8_t ascii[17];
uint8_t i, j;
ascii[16] = '\0';
for (i = 0; i < size; ++i)
{
printf("%02X ", data[i]);
if (data[i] >= 0x20 && data[i] <= 0x7f)
{
ascii[i % 16] = data[i];
}
else
{
ascii[i % 16] = '.';
}
if ((i+1) % 8 == 0 || i+1 == size)
{
printf(" ");
if ((i+1) % 16 == 0)
{
printf("| %s \r\n", ascii);
}
else if (i+1 == size)
{
ascii[(i+1) % 16] = '\0';
if ((i+1) % 16 <= 8)
{
printf(" ");
}
for (j = (i+1) % 16; j < 16; ++j)
{
printf(" ");
}
printf("| %s \r\n", ascii);
}
}
}
}
static void
die(const char *errstr, ...)
{
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
usb_fini();
exit(1);
}
static size_t
usb_send(size_t len, u8 *buf)
{
int got = 0;
int ret = libusb_bulk_transfer(dev, ISP_EP_OUT, buf, len, &got, 10000);
if (ret)
die("send failed: %s\n", libusb_strerror(ret));
return got;
}
static size_t
usb_recv(size_t len, u8 *buf)
{
int got = 0;
int ret = libusb_bulk_transfer(dev, ISP_EP_IN, buf, len, &got, 10000);
if (ret)
die("recv failed: %s\n", libusb_strerror(ret));
if (got < 0)
die("recv failed: got %d\n", got);
if (got > len)
got = len;
return got;
}
static size_t
isp_send_cmd(u8 cmd, u16 len, const u8 *data)
{
u8 buf[64];
if ((len + 3) > sizeof(buf))
die("isp_send_cmd: invalid argument, length %d\n", len);
buf[0] = cmd;
/* length is sent in little endian... but it doesn't really matter
* as the usb maxpacket size is 64, thus len should never be greater
* than 61 (64 minus the 3 bytes header). */
buf[1] = (len >> 0) & 0xff;
buf[2] = (len >> 8) & 0xff;
if (len != 0)
memcpy(&buf[3], data, len);
dbg_printf("isp send cmd %.2x len %.2x%.2x:\n", buf[0], buf[2], buf[1]);
#ifdef DEBUG
print_hex(data, len);
#endif
return usb_send(len + 3, buf);
}
static size_t
isp_recv_cmd(u8 cmd, u16 len, u8 *data)
{
u8 buf[64];
size_t got;
u16 hdrlen;
if ((len + 4) > sizeof(buf))
die("isp_recv_cmd: invalid argument, length %d\n", len);
got = usb_recv(len + 4, buf);
if (got < 4)
die("isp_recv_cmd: not enough data recv\n");
if (buf[0] != cmd)
die("isp_recv_cmd: got wrong command %#.x (exp %#.x)\n", buf[0], cmd);
if (buf[1])
die("isp_recv_cmd: cmd error %#.x\n", buf[1]);
got -= 4;
hdrlen = buf[2] | (buf[3] << 8);
if (hdrlen != got)
die("isp_recv_cmd: length mismatch, got %zu (hdr %u)\n", got, hdrlen);
len = MIN(len, got);
if (data != NULL)
memcpy(data, buf + 4, len);
dbg_printf("isp recv cmd %.2x status %.2x len %.2x%.2x:\n", buf[0], buf[1], buf[3], buf[2]);
#ifdef DEBUG
print_hex(data, len);
#endif
return got;
}
static void
cmd_identify(u8 *dev_id, u8 *dev_type)
{
u8 buf[] = "\0\0MCU ISP & WCH.CN";
u8 ids[2];
buf[0] = 0; // dev_id
buf[1] = 0; // dev_type
#ifdef DEBUG
printf("cmd_identify isp_send_cmd()/isp_recv_cmd()\n");
#endif
/* do not send the terminating nul byte, hence sizeof(buf) - 1 */
isp_send_cmd(CMD_IDENTIFY, sizeof(buf) - 1, buf);
isp_recv_cmd(CMD_IDENTIFY, sizeof(ids), ids);
if (dev_id)
*dev_id = ids[0];
if (dev_type)
*dev_type = ids[1];
#ifdef DEBUG
printf("\n");
#endif
}
static void
cmd_isp_key(size_t len, const u8 *key, u8 *sum)
{
u8 rsp[2];
#ifdef DEBUG
printf("cmd_isp_key isp_send_cmd()/isp_recv_cmd()\n");
#endif
isp_send_cmd(CMD_ISP_KEY, len, key);
isp_recv_cmd(CMD_ISP_KEY, sizeof(rsp), rsp);
if (sum)
*sum = rsp[0];
#ifdef DEBUG
printf("\n");
#endif
}
static void
cmd_isp_end(u8 reason)
{
u8 buf[2];
#ifdef DEBUG
printf("cmd_isp_end isp_send_cmd()/isp_recv_cmd()\n");
#endif
printf_timing("Reset MCU start\n");
isp_send_cmd(CMD_ISP_END, sizeof(reason), &reason);
if (reason == 0) /* the device is not expected to respond */
isp_recv_cmd(CMD_ISP_END, sizeof(buf), buf);
printf_timing("Reset MCU end\n");
#ifdef DEBUG
printf("\n");
#endif
}
static void
cmd_erase(u32 sectors)
{
u8 sec[4];
sec[0] = (sectors >> 0) & 0xff;
sec[1] = (sectors >> 8) & 0xff;
sec[2] = (sectors >> 16) & 0xff;
sec[3] = (sectors >> 24) & 0xff;
isp_send_cmd(CMD_ERASE, sizeof(sec), sec);
isp_recv_cmd(CMD_ERASE, 2, sec); /* receive two 0 bytes */
}
static size_t
cmd_program(uint32_t addr, size_t len, const u8 *data, const u8 key[8])
{
u8 unk[61];
u8 rsp[2];
size_t i;
unk[0] = (addr >> 0) & 0xff;
unk[1] = (addr >> 8) & 0xff;
unk[2] = (addr >> 16) & 0xff;
unk[3] = (addr >> 24) & 0xff;
unk[4] = 0; /* carefully choosen random number */
len = MIN(sizeof(unk) - 5, len);
for (i = 0; i < len; i++)
unk[5 + i] = data[i] ^ key[i % 8];
isp_send_cmd(CMD_PROGRAM, len + 5, unk);
isp_recv_cmd(CMD_PROGRAM, sizeof(rsp), rsp);
if (rsp[0] != 0 || rsp[1] != 0)
die("Fail to program chunk @ %#x error: %.2x %.2x\n", addr, rsp[0], rsp[1]);
return len;
}
static size_t
cmd_verify(uint32_t addr, size_t len, const u8 *data, const u8 key[8])
{
u8 unk[61];
u8 rsp[2];
size_t i;
unk[0] = (addr >> 0) & 0xff;
unk[1] = (addr >> 8) & 0xff;
unk[2] = (addr >> 16) & 0xff;
unk[3] = (addr >> 24) & 0xff;
unk[4] = 0; /* carefully choosen random number */
len = MIN(sizeof(unk) - 5, len);
for (i = 0; i < len; i++)
unk[5 + i] = data[i] ^ key[i % 8];
isp_send_cmd(CMD_VERIFY, len + 5, unk);
isp_recv_cmd(CMD_VERIFY, sizeof(rsp), rsp);
if (rsp[0] != 0 || rsp[1] != 0)
die("Fail to verify chunk @ %#x error: %.2x %.2x (len=%lld)\n", addr, rsp[0], rsp[1], len);
return len;
}
static size_t
cmd_read_conf(u16 cfgmask, size_t len, u8 *cfg)
{
u8 buf[60];
u8 req[2];
u16 mask;
size_t got;
req[0] = (cfgmask >> 0) & 0xff;
req[1] = (cfgmask >> 8) & 0xff;
#ifdef DEBUG
printf("cmd_read_conf isp_send_cmd()/isp_recv_cmd()\n");
#endif
isp_send_cmd(CMD_READ_CONFIG, sizeof(req), req);
got = isp_recv_cmd(CMD_READ_CONFIG, sizeof(buf), buf);
if (got < 2)
die("read conf fail: not received enough bytes\n");
mask = buf[0] | (buf[1] << 8);
if (cfgmask != mask)
die("read conf fail: received conf does not match\n");
len = MIN(got - 2, len);
memcpy(cfg, &buf[2], len);
return len;
}
u8 cmd_debug_mode(u8 enable)
{
u8 req[] = { 0x1f, 0x00 };
const u8 enable_debug_req = 0xe5;
const u8 disable_debug_req = 0x45;
u8 buf[60];
size_t got;
#ifdef DEBUG
printf("cmd_enable_debug_mode isp_send_cmd()\n");
#endif
isp_send_cmd(CMD_READ_CONFIG, sizeof(req), req);
got = isp_recv_cmd(CMD_READ_CONFIG, sizeof(buf), buf);
if (got < 2)
die("read conf fail: not received enough bytes\n");
buf[10] = enable ? enable_debug_req : disable_debug_req;
isp_send_cmd(CMD_WRITE_CONFIG, 14, buf);
got = usb_recv(6, buf);
if (got != 6)
die("send config command: wrong response length\n");
#ifdef DEBUG
print_hex(buf, 6);
#endif
return buf[4] == 0; // success if this byte is zero
}
u8 cmd_set_flash_size(size_t flash_file_size_bytes)
{
u8 req[] = { 0x1f, 0x00 };
u8 buf[60];
size_t got;
u8 flash_size_config_kb;
if (flash_file_size_bytes < SZ_32K)
{
flash_size_config_kb = 32;
}
else if (flash_file_size_bytes < SZ_64K)
{
flash_size_config_kb = 64;
}
else if (flash_file_size_bytes < SZ_96K)
{
flash_size_config_kb = 96;
}
else
{
die("invalid flash file size only < 96k are supported\n");
}
isp_send_cmd(CMD_READ_CONFIG, sizeof(req), req);
got = isp_recv_cmd(CMD_READ_CONFIG, sizeof(buf), buf);
if (got < 2)
die("read conf fail: not received enough bytes\n");
u8 flash_size_config = 0x0f;
switch (flash_size_config_kb)
{
case 32:
flash_size_config = 0xcf;
break;
case 64:
flash_size_config = 0x4f;
break;
case 96:
flash_size_config = 0x0f;
break;
default:
break;
}
/* If flash size config already set avoid to set it again */
if(flash_size_config == buf[13])
{
printf_timing("flash size already set to %d KB\n", flash_size_config_kb);
return 1;
}
printf_timing("flash size set to %d KB\n", flash_size_config_kb);
buf[13] = flash_size_config;
isp_send_cmd(CMD_WRITE_CONFIG, 14, buf);
got = usb_recv(6, buf);
if (got != 6)
die("send config command: wrong response length\n");
return buf[4] == 0; // success if this byte is zero
}
static u32 read_btver(void)
{
u8 buf[4];
#ifdef DEBUG
printf("read_btver\n");
#endif
/* format: [major, major, minor, minor] */
cmd_read_conf(CFG_MASK_BTVER, sizeof(buf), buf);
return ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
}
static void
usb_init(void)
{
int err;
const struct libusb_version* version;
version = libusb_get_version();
printf_timing("Using libusb v%d.%d.%d.%d\n", version->major, version->minor, version->micro, version->nano);
err = libusb_init(&usb);
if (err)
die("libusb_init: %s\n", libusb_strerror(err));
dev = libusb_open_device_with_vid_pid(usb, ISP_VID, ISP_PID);
if (!dev)
die("Fail to open device %4x:%4x\n", ISP_VID, ISP_PID);
#ifdef __linux__
kernel = libusb_kernel_driver_active(dev, 0);
if (kernel == 1)
{
if (libusb_detach_kernel_driver(dev, 0))
die("Couldn't detach kernel driver!\n");
}
#endif
err = libusb_claim_interface(dev, 0);
if (err)
die("libusb_claim_interface: %s\n", libusb_strerror(err));
}
static void
usb_fini(void)
{
int err = 0;
if(flash_file_fp)
fclose(flash_file_fp);
if(flash_file_bin)
free(flash_file_bin);
if (dev)
err = libusb_release_interface(dev, 0);
if (err)
fprintf(stderr, "libusb_release_interface: err=%d %s\n", err, libusb_strerror(err));
#ifdef __linux__
if (kernel == 1)
libusb_attach_kernel_driver(dev, 0);
#endif
if(dev)
libusb_close(dev);
if (usb)
libusb_exit(usb);
}
static size_t
db_flash_size(void)
{
if (dev_db && dev_db->flash_size > 0)
return dev_db->flash_size;
return -1;
}
static size_t
db_flash_sector_size(void)
{
if (dev_db && dev_db->flash_sector_size > 0)
return dev_db->flash_sector_size;
return SECTOR_SIZE;
}
static void
isp_init(void)
{
size_t i;
u8 sum;
u8 rsp;
/* get the device type and id */
cmd_identify(&dev_id, &dev_type);
/* match the detected device */
for (i = 0; i < LEN(devices); i++)
{
if (devices[i].type == dev_type && devices[i].id == dev_id)
{
dev_db = &devices[i];
break;
}
}
if (dev_db)
{
printf_timing("Found chip: %s [0x%.2x%.2x] Flash %dK\n", dev_db->name,
dev_type, dev_id, dev_db->flash_size / SZ_1K);
}
else
{
printf_timing("Unknown chip: [0x%.2x%.2x]", dev_type, dev_id);
}
/* get the device uid */
dev_uid_len = cmd_read_conf(CFG_MASK_UID, sizeof(dev_uid), dev_uid);
printf_timing("Chip uid: ");
for (i = 0; i < dev_uid_len; i++)
printf("%.2X ", dev_uid[i]);
puts("");
/* get the bootloader version */
dev_btver = read_btver();
printf_timing("bootloader: v%d%d.%d%d (0x%08X)\n",
dev_btver >> 24, ((dev_btver & 0x00FF0000) >> 16),
((dev_btver & 0x0000FF00) >> 8), dev_btver & 0xFF,
dev_btver);
/* initialize xor_key */
for (sum = 0, i = 0; i < dev_uid_len; i++)
sum += dev_uid[i];
memset(xor_key, sum, sizeof(xor_key));
xor_key[7] = xor_key[0] + dev_id;
/* send the isp key */
cmd_isp_key(sizeof(isp_key), isp_key, &rsp);
if (dev_btver >= BTVER_2_7)
{
/* bootloader version 2.7 (and maybe onward) simply send zero */
sum = 0;
}
else
{
/* bootloader version 2.6 (and maybe prior versions) send back
* the a checksum of the xor_key. This response can be used to
* make sure we are in sync. */
for (sum = 0, i = 0; i < sizeof(xor_key); i++)
sum += xor_key[i];
}
if (rsp != sum)
die("failed set isp key, wrong reply, got %x (exp %x)\n", rsp, sum);
}
static void
progress_bar(const char *act, size_t current, size_t total)
{
static char f[] = "####################################################";
static char e[] = " ";
int l = strlen(f);
int n = (current * l) / total;
if (!config.do_progress)
return;
printf_timing("\r[%.*s%.*s] %s %lld/%lld", n, f, l - n, e, act, current, total);
if (current == total)
puts("");
fflush(stdout);
}
static void
isp_flash(size_t size, const u8 *data)
{
size_t sector_size = db_flash_sector_size();
u32 nr_sectors = ALIGN(size, sector_size) / sector_size;
u32 max_sectors = dev_db->flash_size / sector_size;
size_t off = 0;
size_t rem = size;
size_t len;
printf_timing("isp_flash start (max sectors=%d)\n", max_sectors);
nr_sectors = MIN(nr_sectors, max_sectors);
printf_timing("cmd_erase %d sectors start\n", nr_sectors);
cmd_erase(nr_sectors);
printf_timing("cmd_erase end\n");
printf_timing("cmd_program start\n");
while (off < size)
{
progress_bar("write", off, size);
len = cmd_program(off, rem, data + off, xor_key);
off += len;
rem -= len;
}
cmd_program(off, 0, NULL, xor_key);
progress_bar("write", size, size);
printf_timing("cmd_program end with success\n");
printf_timing("isp_flash end\n");
}
static void
isp_verify(size_t size, const u8 *data)
{
size_t off = 0;
size_t rem = size;
size_t len;
printf_timing("isp_verify start\n");
while (off < size)
{
progress_bar("verify", off, size);
len = cmd_verify(off, rem, data + off, xor_key);
off += len;
rem -= len;
}
progress_bar("verify", size, size);
printf_timing("isp_verify end with success\n");
}
static void
isp_fini(void)
{
if (config.do_reset)
cmd_isp_end(1);
}
static size_t
f_size(FILE *fp)
{
size_t prev=ftell(fp);
fseek(fp, 0L, SEEK_END);
size_t sz=ftell(fp);
fseek(fp,prev,SEEK_SET); //go back to where we were
return sz;
}
static size_t
flash_file_size(const char *name)
{
size_t size;
flash_file_fp = fopen(name, "rb");
if (flash_file_fp == NULL)
die("%s: %s\n", name, strerror(errno));
size = f_size(flash_file_fp);
fclose(flash_file_fp);
flash_file_fp = NULL;
return size;
}
static void
flash_file(const char *name)
{
size_t size;
size_t size_align;
size_t ret;
flash_file_fp = fopen(name, "rb");
if (flash_file_fp == NULL)
die("%s: %s\n", name, strerror(errno));
size = f_size(flash_file_fp);
size_align = ALIGN(size, 64);
if (size_align > db_flash_size())
die("%s: file too big, flash size is %zu", name, db_flash_size());
flash_file_bin = malloc(size_align);
if (flash_file_bin == NULL)
{
die("flash_file Memory error\n");
}
memset(flash_file_bin, 0, size_align);
// Copy the file into the buffer flash_file_bin
ret = fread(flash_file_bin, 1, size, flash_file_fp);
if (ret != size)
{
die("flash_file reading error\n");
}
printf_timing("Flash file: %s\n", name);
printf_timing("File length: %lld (size aligned: %lld)\n", size, size_align);
isp_flash(size_align, flash_file_bin);
if (config.do_verify)
isp_verify(size_align, flash_file_bin);
/* Note flash_file_fp & flash_file_bin handles are closed/freed by usb_fini() */
}
static void
ch569_print_config(const char* message, size_t len, const u8 *cfg)
{
u32 nv;
if (len < 12)
return;
nv = (cfg[8] << 0) | (cfg[9] << 8) | (cfg[10] << 16) | (cfg[11] << 24);
printf("%s nv=0x%08X\n", message, nv);
printf("[4] RESET_EN %d: %s\n", !!(nv & BIT(4)),
(nv & BIT(4)) ? "enabled" : "disabled");
printf("[5] DEBUG_EN %d: %s\n", !!(nv & BIT(5)),
(nv & BIT(5)) ? "enabled" : "disabled");
printf("[6] BOOT_EN %d: %s\n", !!(nv & BIT(6)),
(nv & BIT(6)) ? "enabled" : "disabled");
printf("[7] CODE_READ_EN %d: %s\n", !!(nv & BIT(7)),
(nv & BIT(7)) ? "enabled" : "disabled");
printf("[29] LOCKUP_RST_EN %d: %s\n", !!(nv & BIT(29)),
(nv & BIT(29)) ? "enabled" : "disabled");
printf("[31:30] USER_MEM 0x%02X: %s\n", (nv >> 30) & 0b11,
((nv >> 30) & 0b11) == 0 ? "RAMX 32KB + ROM 96KB" :
((nv >> 30) & 0b11) == 1 ? "RAMX 64KB + ROM 64KB" :
"RAMX 96KB + ROM 32KB");
}
static void
config_show(const char* message)
{
u8 cfg[16];
size_t len;
len = cmd_read_conf(0x7, sizeof(cfg), cfg);
ch569_print_config(message, len, cfg);
}
static void
version_print(void)
{
printf("%s v%s\n", argv0, VERSION);
die("");
}
int
main(int argc, char *argv[])
{
char identifier;
cag_option_context context;
argv0 = argv[0];
/* Prepare the context and iterate over all options */
cag_option_prepare(&context, options, CAG_ARRAY_SIZE(options), argc, argv);
while (cag_option_fetch(&context))
{
identifier = cag_option_get(&context);
switch (identifier)
{
case 'V':
version_print();
case 'h':
printf("Usage: wch-ch56x-isp [OPTION]...\n");
cag_option_print(options, CAG_ARRAY_SIZE(options), stdout);
return EXIT_SUCCESS;
case 'p':
config.do_progress = 1;
break;
case 'r':
config.do_reset = 1;
break;
case 'v':
config.do_verify = 1;
break;
case 'c':
config.do_show_config = 1;
break;
case 'd':
config.do_mcu_debug = 1;
config.mcu_debug_mode = cag_option_get_value(&context);
break;
case 'f':
config.do_flash_file = 1;
config.flash_file = cag_option_get_value(&context);
break;
}
}
printf("%s v%s\n", argv0, VERSION);
gettimeofday(&start_tv, NULL);
get_CurrentTime(currTime, CURR_TIME_SIZE);
printf("Start time %s\n", currTime);
usb_init();