-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
executable file
·196 lines (164 loc) · 3.95 KB
/
main.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <libusb-1.0/libusb.h>
uint8_t chipID;
uint8_t u8Buff[64];
libusb_device_handle *h;
int v2_detect(void);
int v2_write(uint8_t* pReadBuff, int fileSize);
int v2_verify(uint8_t* pReadBuff, int fileSize);
void v2_reset(void);
int v1_detect(void);
int v1_write(uint8_t* pReadBuff, int fileSize);
int v1_verify(uint8_t* pReadBuff, int fileSize);
void v1_reset(void);
uint32_t Write(uint8_t *p8Buff, uint8_t u8Length)
{
int len;
if (libusb_bulk_transfer(h, 0x02, (unsigned char*)p8Buff, u8Length, &len, 5000) != 0) {
return 0;
} else {
return 1;
}
return 0;
}
uint32_t Read(uint8_t *p8Buff, uint8_t u8Length)
{
int len;
if (libusb_bulk_transfer(h, 0x82, (unsigned char*)p8Buff, u8Length, &len, 5000) != 0) {
return 0;
} else {
return 1;
}
return 0;
}
uint8_t* LoadFile(int maxSize, char* fname, int* fsize)
{
FILE *f;
uint8_t* pReadBuff = (uint8_t*)malloc(maxSize);
int r;
if (pReadBuff == NULL) {
return 0;
}
memset(pReadBuff, 0xFF, maxSize);
f = fopen(fname, "rb");
if (f == NULL) {
return 0;
}
r = fread(pReadBuff, 1, maxSize, f);
if (r < 1) {
fclose(f);
return 0;
}
fclose(f);
*fsize = r;
return pReadBuff;
}
static int protocol_detect(void) {
/* detect protocol: v1 returns 0xFF, v2 returnx 0xa2 */
static char detect_prot_cmd[] = {0xa2, 0x1, 0x00};
char detect_result[6] = {0};
if (!Write(detect_prot_cmd, sizeof(detect_prot_cmd))) {
printf("Send Init: Fail\n");
return -1;
}
if (!Read(detect_result, sizeof(detect_result))) {
printf("Read Init: Fail\n");
return -1;
}
return detect_result[0] & 0xFF;
}
static int get_max_flash_size(int id) {
switch (id) {
case 0x59: return 61440;
case 0x52:
case 0x54: return 14336;
default: return 10240;
}
}
int main(int argc, char const *argv[])
{
int ret = 1;
int maxSize;
uint8_t* pReadBuff;
int fileSize = 0;
int (*ch_detect)(void);
int (*ch_write)(uint8_t* pReadBuff, int fileSize);
int (*ch_verify)(uint8_t* pReadBuff, int fileSize);
void (*ch_reset)(void);
printf("CH55x Programmer by VNPro & rgwan & olin\n");
if (argc != 2) {
printf("------------------------------------------------------------------\n");
printf("usage: chprog flash_file.bin\n");
printf("------------------------------------------------------------------\n");
}
if (argc == 2) {
/* load flash file */
pReadBuff = LoadFile(64 * 1024, (char*)argv[1], &fileSize);
if (!pReadBuff) {
printf("Error: failed to read file (%s)\n", argv[1]);
return 1;
} else {
printf ("File read (%s): %i bytes\n", argv[1], fileSize);
}
}
libusb_init(NULL);
libusb_set_debug(NULL, 3);
h = libusb_open_device_with_vid_pid(NULL, 0x4348, 0x55e0);
if (h == NULL) {
printf("Error: CH55x USB device not found.\n");
return 1;
}
if (libusb_claim_interface(h, 0)) {
printf("Failed to claim interface 0\n");
return 1;
}
ch_detect = v2_detect;
ch_write = v2_write;
ch_verify = v2_verify;
ch_reset = v2_reset;
// v2 prot. returns 0xa2, v1 prot. returns 0xff
if (protocol_detect() != 0xa2) {
ch_detect = v1_detect;
ch_write = v1_write;
ch_verify = v1_verify;
ch_reset = v1_reset;
printf("Using v1 protocol\n");
} else {
printf("Using v2 protocol\n");
}
// detect chip and print bootloader info
if (ch_detect()) {
goto finish;
}
// no file
if (fileSize < 1) {
goto finish;
}
maxSize = get_max_flash_size(chipID);
if (fileSize > maxSize) {
printf("FW size is too big to fit the flash: %i max: %i\n", fileSize, maxSize);
goto finish;
}
// write the firmware to the chip
if (ch_write(pReadBuff, fileSize)) {
goto finish;
}
// verify written data
if (ch_verify(pReadBuff, fileSize)) {
goto finish;
}
printf("Write & verify complete. All seems OK.\n");
ch_reset();
ret = 0;
finish:
if (h)
{
libusb_release_interface(h, 0);
libusb_close(h);
}
libusb_exit(NULL);
return ret;
}