-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathupdater.c
165 lines (131 loc) · 3.02 KB
/
updater.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
#include "updater.h"
libusb_device_handle *devh;
libusb_context *ctx;
int devintf;
extern unsigned char firmware_fw_tp_update_hex[];
extern unsigned int firmware_fw_tp_update_hex_len;
extern unsigned char firmware_fw_iso_hex[];
extern unsigned int firmware_fw_iso_hex_len;
extern unsigned char firmware_fw_ansi_hex[];
extern unsigned int firmware_fw_ansi_hex_len;
extern unsigned char firmware_tpfw_bin[];
extern unsigned int firmware_tpfw_bin_len;
static int usage(const char *cmd)
{
printf("usage: %s <step-1|step-2> <iso|ansi>\n", cmd);
return -1;
}
static int convert()
{
int rc;
rc = convert_hex_data(
firmware_fw_tp_update_hex, firmware_fw_tp_update_hex_len,
"fw_tp_update.bin");
if (rc < 0) {
return rc;
}
rc = convert_hex_data(
firmware_fw_iso_hex, firmware_fw_iso_hex_len,
"fw.bin");
if (rc < 0) {
return rc;
}
return 0;
}
static int flash_tp()
{
int rc;
rc = write_tp_fw(firmware_tpfw_bin, firmware_tpfw_bin_len);
if (rc < 0) {
return rc;
}
return 0;
}
static int flash_tp_update()
{
int rc;
rc = write_kb_fw(firmware_fw_tp_update_hex, firmware_fw_tp_update_hex_len);
if (rc < 0) {
return rc;
}
return 0;
}
static int flash_kb_iso()
{
int rc;
rc = write_kb_fw(firmware_fw_iso_hex, firmware_fw_iso_hex_len);
if (rc < 0) {
return rc;
}
return 0;
}
static int flash_kb_ansi()
{
int rc;
rc = write_kb_fw(firmware_fw_ansi_hex, firmware_fw_ansi_hex_len);
if (rc < 0) {
return rc;
}
return 0;
}
static int step_1()
{
int rc;
printf("Running STEP-1...\n");
printf("[*] Flashing keyboard updater firmware...\n");
rc = flash_tp_update();
if (rc < 0) {
return rc;
}
printf("[*] Please reboot now, and run `step-2`.\n");
return 0;
}
static int step_2(int ansi_type)
{
int rc;
printf("Running STEP-2...\n");
printf("[*] Flashing touchpad firmware...\n");
rc = flash_tp();
if (rc < 0) {
return rc;
}
if (ansi_type) {
printf("[*] Flashing ANSI keyboard firmware...\n");
rc = flash_kb_ansi();
} else {
printf("[*] Flashing ISO keyboard firmware...\n");
rc = flash_kb_iso();
}
if (rc < 0) {
return rc;
}
printf("[*] All done! Your keyboard and touchpad should be updated.\n");
return 0;
}
int main(int argc, char *argv[])
{
int rc = 0;
if (argc != 3) {
rc = usage(argv[0]);
} else if (strcmp(argv[2], "iso") && strcmp(argv[2], "ansi")) {
rc = usage(argv[0]);
printf("* specify valid keyboard type\n");
} else if (!strcmp(argv[1], "convert")) {
rc = convert();
} else if (!strcmp(argv[1], "step-1")) {
rc = step_1();
} else if (!strcmp(argv[1], "step-2")) {
rc = step_2(!strcmp(argv[2], "ansi"));
} else if (!strcmp(argv[1], "flash-tp")) {
rc = flash_tp();
} else if (!strcmp(argv[1], "flash-tp-update")) {
rc = flash_tp_update();
} else if (!strcmp(argv[1], "flash-kb-iso")) {
rc = flash_kb_iso();
} else if (!strcmp(argv[1], "flash-kb-ansi")) {
rc = flash_kb_ansi();
} else {
rc = usage(argv[0]);
}
return rc;
}