-
Notifications
You must be signed in to change notification settings - Fork 21
/
usb_touchpad.c
216 lines (169 loc) · 5.26 KB
/
usb_touchpad.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
#include "updater.h"
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define SHORTREPOID 0x05
#define LONGREPOID 0x06
#define SHORTDEVLENGHT 6
#define LONGDEVLENGHT 1040
#define STATUSCMD 0xA1
#define CHECKCHECKSUM 0xF0
#define ENTERBOOTLOADER 0xF1
#define ICERASE 0xF2
#define PROGRAM 0xF3
#define VERIFY1KDATA 0xF4
#define VERIFY_CHECKSUM 0xF5
#define PROGRAMPASS 0xF6
#define ENDPROGRAM 0xF7
#define I2C_PASS 0xFA
#define I2C_FAIL 0xFB
#define I2C_WAIT 0xFC
#define I2C_TIMEOUT 0xFD
#define CHECKCHECKSUM_PASS 0xE0
#define ENTERBOOTLOADER_PASS 0xE1
#define ICERASE_PASS 0xE2
#define PROGRAM_PASS 0xE3
#define VERIFY1KDATA_PASS 0xE4
#define VERIFY_CHECKSUM_PASS 0xE5
#define PROGRAMPASS_PASS 0xE6
#define ENDPROGRAM_PASS 0xE7
#define CHECKCHECKSUM_FAIL 0xD0
#define ENTERBOOTLOADER_FAIL 0xD1
#define ICERASE_FAIL 0xD2
#define PROGRAM_FAIL 0xD3
#define VERIFY1KDATA_FAIL 0xD4
#define VERIFY_CHECKSUM_FAIL 0xD5
#define PROGRAMPASS_FAIL 0xD6
#define ENDPROGRAM_FAIL 0xD7
#define PROGMODE 0x0b
static int touchpad_verify(int type, int pass, int sendcmd)
{
if (sendcmd) {
unsigned char data[6] = {
SHORTREPOID, STATUSCMD, type
};
int rc = libusb_control_transfer(devh, 0x21, 0x09, 0x0305, 1, data, sizeof(data), 1000);
if(rc < 0) {
return -1;
}
}
if (1) { // receive command
unsigned char data[6] = {};
int rc = libusb_control_transfer(devh, 0xa1, 0x01, 0x0305, 1, data, sizeof(data), 2000);
if(rc < 0) {
return -1;
}
if (data[0] != SHORTREPOID || data[1] != pass) {
printf(">>> Verify mismatch: type=%02x, pass=%02x, received=%02x\n", type, pass, data[1]);
return -1;
}
}
return 0;
}
int try_touchpad_verify(int type, int pass, int sendcmd)
{
int try;
for (try = 0; try < 100; try++) {
usleep(50*1000);
int rc = touchpad_verify(type, pass, sendcmd);
if (rc == 0) {
break;
}
}
if (try == 100) {
printf(">>> Touchpad verify (type=%d, pass=%d) data failed\n", type, pass);
return -1;
}
return 0;
}
int write_tp_fw(const unsigned char *fw, int fw_length)
{
int block_size = 1024;
int try;
int rc;
if (fw_length < 24576) {
printf("[*] Touchpad firmware needs to be %d, and is %d\n",
fw_length, 24576);
return -1;
}
// cap to 24k
fw_length = 24 * 1024;
printf("[*] Opening in touchpad mode\n");
for (try = 0; try < 3; try++) {
rc = open_touchpad_mode();
if (rc >= 0) {
break;
}
usleep(500*1000);
}
if (try >= 3) {
printf(">>> Failed to open in touchpad mode\n");
goto finish;
}
rc = try_touchpad_verify(ICERASE, ICERASE_PASS, 0);
if (rc < 0) {
printf(">>> Touchpad erase failed\n");
goto finish;
}
usleep(10000);
for(int offset = 0; offset < fw_length; offset += block_size)
{
unsigned char data[16 + block_size];
unsigned char *ptr = data;
*ptr++ = 0x06;
*ptr++ = 0xD0;
*ptr++ = 0xCC;
*ptr++ = 0xCC;
*ptr++ = offset & 0xFF;
*ptr++ = (offset >> 8) & 0xFF;
*ptr++ = 0xCC;
*ptr++ = 0xCC;
memset(ptr, 0, block_size);
memcpy(ptr, &fw[offset], MIN(block_size, fw_length-offset));
ptr += block_size;
*ptr++ = 0xEE;
*ptr++ = 0xD2;
*ptr++ = 0xCC;
*ptr++ = 0xCC;
*ptr++ = 0xCC;
*ptr++ = 0xCC;
*ptr++ = 0xCC;
*ptr++ = 0xCC;
printf(">>> Writing offset:%d length:%d...\n", offset, block_size);
rc = libusb_control_transfer(devh, 0x21, 0x09, 0x0306, 1, data, sizeof(data), 1000);
if (rc < 0) {
printf(">>> Write failed\n");
goto finish;
}
usleep(150*1000);
printf(">>> Verifying '1k-data'...\n");
rc = try_touchpad_verify(VERIFY1KDATA, VERIFY1KDATA_PASS, 1);
if (rc < 0) {
printf(">>> Touchpad verify data failed\n");
goto finish;
}
}
usleep(50*1000);
printf("[*] Verifying 'end-program'...\n");
rc = try_touchpad_verify(ENDPROGRAM, ENDPROGRAM_PASS, 1);
if (rc < 0) {
printf(">>> Touchpad end program verify\n");
goto finish;
}
usleep(50*1000);
printf("[*] Verifying 'checksum'...\n");
rc = try_touchpad_verify(VERIFY_CHECKSUM, VERIFY_CHECKSUM_PASS, 1);
if (rc < 0) {
printf(">>> Touchpad end program verify\n");
goto finish;
}
usleep(50*1000);
printf("[*] Verifying 'program'...\n");
rc = try_touchpad_verify(PROGRAMPASS, 0, 1);
if (rc < 0) {
printf(">>> Touchpad end program verify\n");
goto finish;
}
printf("[*] Finished succesfully!\n");
finish:
close_usb();
return rc;
}