-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemukb.c
345 lines (278 loc) · 6.78 KB
/
emukb.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
#include <stdbool.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <inttypes.h>
#include <termios.h>
#include "libcstuff.h"
static size_t emukb_injected_count = 0;
extern const char _binary_report_descriptor_kb_bin_start[];
extern int _binary_report_descriptor_kb_bin_size;
const intptr_t _binary_report_descriptor_kb_bin_size_v =
(intptr_t) &_binary_report_descriptor_kb_bin_size;
static const char *cmds[] = {
"mkdir /config/usb_gadget/kb",
"echo 0x1234 >/config/usb_gadget/kb/idVendor",
"echo 0x5678 >/config/usb_gadget/kb/idProduct",
"echo 0x0100 >/config/usb_gadget/kb/bcdDevice",
"echo 0x0110 >/config/usb_gadget/kb/bcdUSB",
"mkdir /config/usb_gadget/kb/configs/c.1",
// TODO strings
"echo 500 >/config/usb_gadget/kb/configs/c.1/MaxPower",
"mkdir /config/usb_gadget/kb/functions/hid.usb0",
"echo 1 >/config/usb_gadget/kb/functions/hid.usb0/subclass", // boot interface subclass
"echo 1 >/config/usb_gadget/kb/functions/hid.usb0/protocol", // keyboard
"echo 8 >/config/usb_gadget/kb/functions/hid.usb0/report_length",
NULL, // marker to set the record descriptor
"ln -s /config/usb_gadget/kb/functions/hid.usb0 /config/usb_gadget/kb/configs/c.1",
//"echo musb-hdrc.0.auto >/config/usb_gadget/kb/UDC",
};
static const char *remove_cmds[] = {
"rm /config/usb_gadget/kb/configs/c.1/hid.usb0",
"rmdir /config/usb_gadget/kb/functions/hid.usb0",
};
static int emukb_fd = -1;
// Find device controllers
//"ls /sys/class/udc"
bool
emukb_unregister(void)
{
/* Don't care about errors here */
int i;
for (i = 0; i < array_len(remove_cmds); i++) {
DEBUG("running command %s", remove_cmds[i]);
if (system(remove_cmds[i]) != 0) {
DEBUG("failed to run command [%s] but ignoring", remove_cmds[i]);
}
}
return true;
}
static bool write_string_to_file(const char *file, const char *str, size_t len)
{
int fd = open(file, O_WRONLY | O_TRUNC);
if (fd == -1) {
PERROR("open");
return false;
}
if (write_complete(fd, str, len) == -1) {
ERROR("failed to write string to file %s", file);
return false;
}
return true;
}
bool
emukb_register_post_enable(void)
{
const char *gadget_device = "/dev/hidg0";
int fd = open(gadget_device, O_RDWR);
if (fd == -1) {
PERROR("failed to open() %s", gadget_device);
return false;
}
emukb_fd = fd;
return true;
}
bool
emukb_register_pre_enable(void)
{
int i;
for (i = 0; i < array_len(cmds); i++) {
if (cmds[i] == NULL) {
if (!write_string_to_file(
"/config/usb_gadget/kb/functions/hid.usb0/report_desc",
_binary_report_descriptor_kb_bin_start,
_binary_report_descriptor_kb_bin_size_v))
{
PERROR("failed to write report descriptor");
return false;
}
continue;
}
VERBOSE("running command %s", cmds[i]);
if (system(cmds[i]) != 0) {
ERROR("failed to run command [%s]", cmds[i]);
return false;
}
}
return true;
}
int auxfd = -1;
bool emukb_register_auxiliary(const char *filename, int speed)
{
auxfd = open(filename, O_WRONLY);
if (auxfd == -1) {
PERROR("open");
return false;
}
struct termios tio;
if (tcgetattr(auxfd, &tio) == -1) {
PERROR("tcgetattr");
return false;
}
if (cfsetspeed(&tio, speed) == -1) {
PERROR("cfsetspeed");
return false;
}
if (tcsetattr(auxfd, TCSANOW, &tio) == -1) {
PERROR("tcsetattr");
return false;
}
/* Reset the controller's serial link */
if (write(auxfd, "\n", 1) == -1) {
PERROR("write");
return false;
}
return true;
}
static bool emukb_send_report_aux(void *report, size_t len)
{
int result;
char *reportc = (char *)report;
char fancy_report[3 * len + 1];
sprintf(fancy_report, "K %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx\n",
reportc[0],
reportc[1],
reportc[2],
reportc[3],
reportc[4],
reportc[5],
reportc[6],
reportc[7]);
DEBUG("Sending report %s", fancy_report);
result = write_complete(auxfd, fancy_report, strlen(fancy_report));
if (result == -1) {
PERROR("write_complete");
return false;
}
if (result == 0) {
ERROR("got end of file from gadget");
return false;
}
return true;
}
static bool emukb_send_report_native(void *report, size_t len)
{
int result;
char *reportc = (char *)report;
char fancy_report[3 * len + 1];
sprintf(fancy_report, "%hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
reportc[0],
reportc[1],
reportc[2],
reportc[3],
reportc[4],
reportc[5],
reportc[6],
reportc[7]);
DEBUG("Sending report %s", fancy_report);
result = write_complete(emukb_fd, report, len);
if (result == -1) {
PERROR("write_complete");
return false;
}
if (result == 0) {
ERROR("got end of file from gadget");
return false;
}
return true;
}
bool emukb_use_aux = false;
bool emukb_send_report(void *report, size_t len)
{
if (emukb_use_aux) {
return emukb_send_report_aux(report, len);
} else {
return emukb_send_report_native(report, len);
}
}
char usb2ascii_array[] = {
0, 0, 0, 0, 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
'3', '4', '5', '6', '7', '8', '9', '0',
'\n', 27, '\b', '\t', ' ', '-', '=', '[',
']', '\\', 0, ';', '\'', '`', ',', '.',
'/', 0, 0, 0, 0, 0, 0, 0,
};
char usb2ascii_shifted_array[] = {
0, 0, 0, 0, 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2',
'3', '4', '5', '6', '7', '8', '9', '0',
'\n', 27, '\b', '\t', ' ', '_', '+', '{',
'}', '|', 0, ':', '"', '~', '<', '>',
0, 0, 0, 0, 0, 0, 0, 0,
};
bool ascii2usb(char c, uint8_t *modifiers_out, char *co)
{
int i;
*modifiers_out = 0;
for (i = 0; i < 64; i++) {
if (usb2ascii_array[i] == c) {
*co = i;
return true;
}
}
for (i = 0; i < 64; i++) {
if (usb2ascii_shifted_array[i] == c) {
*modifiers_out = 2; // left shift
*co = i;
return true;
}
}
return true;
}
bool usb2ascii(uint8_t c, char *co)
{
*co = usb2ascii_array[(int)c];
return true;
}
bool emukb_inject_notrack(const char *text)
{
char report[8];
const char *p = text;
for (;;) {
if (*p == 0) {
break;
}
memset(report, 0, sizeof(report));
uint8_t modifiers;
ascii2usb(*p, &modifiers, &report[2]);
report[0] = modifiers;
if (!emukb_send_report(report, 8)) {
ERROR("failed to send report");
return false;
}
memset(report, 0, sizeof(report));
if (!emukb_send_report(report, 8)) {
ERROR("failed to send report");
return false;
}
p++;
}
return true;
}
bool emukb_inject(const char *text)
{
emukb_injected_count += strlen(text);
return emukb_inject_notrack(text);
}
bool emukb_erase_chars(size_t n_chars)
{
int i;
for (i = 0; i < n_chars + 1; i++) {
emukb_inject("\b");
}
return true;
}
bool emukb_erase_injected(void)
{
int i;
for (i = 0; i < emukb_injected_count; i++) {
emukb_inject_notrack("\b");
}
emukb_injected_count = 0;
return true;
}