-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxperable.c
More file actions
342 lines (298 loc) · 8.84 KB
/
xperable.c
File metadata and controls
342 lines (298 loc) · 8.84 KB
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
// 元のコード https://github.com/j4nn/xperable
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <errno.h>
#include <libusb-1.0/libusb.h>
struct fbusb;
enum {
LOG_ERR = 1,
LOG_NFO,
LOG_DBG,
};
int verbosity = LOG_NFO;
static unsigned char rxbuff[1024 * 1024 * 64];
static void fbusb_log(struct fbusb *dev, int ep, void *buff, int len, int done, int res){
int i;
const char *dir;
const char *status;
const unsigned char *ptr = buff;
dir = (ep & 0x80) ? "<-" : "->";
switch (res){
case LIBUSB_SUCCESS: status = "OK"; break;
case LIBUSB_ERROR_IO: status = "IO"; break;
case LIBUSB_ERROR_INVALID_PARAM: status = "IP"; break;
case LIBUSB_ERROR_ACCESS: status = "AC"; break;
case LIBUSB_ERROR_NO_DEVICE: status = "ND"; break;
case LIBUSB_ERROR_NOT_FOUND: status = "NF"; break;
case LIBUSB_ERROR_BUSY: status = "BS"; break;
case LIBUSB_ERROR_TIMEOUT: status = "TO"; break;
case LIBUSB_ERROR_OVERFLOW: status = "OF"; break;
case LIBUSB_ERROR_PIPE: status = "PI"; break;
case LIBUSB_ERROR_INTERRUPTED: status = "IN"; break;
case LIBUSB_ERROR_NO_MEM: status = "NM"; break;
case LIBUSB_ERROR_NOT_SUPPORTED: status = "NS"; break;
case LIBUSB_ERROR_OTHER: status = "OT"; break;
default: status = "??"; break;
}
printf(" {%08x%s%08x:%s}", len, dir, done, status);
if (verbosity >= LOG_DBG){
for (i = 0; i < 16; i++){
if (i < done){
printf(" %02x", ptr[i]);
}else{
printf(" ");
}
}
}
printf(" \"");
for (i = 0; i < 64 && i < done; i++){
printf("%c", isprint(ptr[i]) ? ptr[i] : '.');
}
printf("\"\n");
}
// "fbusb.h" //
struct fbusb;
enum
{
FASTBOOT_OKAY,
FASTBOOT_FAIL,
FASTBOOT_DATA,
FASTBOOT_INFO,
FASTBOOT_UNKNOWN
};
static const char *fastboot_results[] = {
[FASTBOOT_OKAY] = "OKAY",
[FASTBOOT_FAIL] = "FAIL",
[FASTBOOT_DATA] = "DATA",
[FASTBOOT_INFO] = "INFO",
[FASTBOOT_UNKNOWN] = "UNKN",
};
struct fbusb
{
libusb_device_handle *h;
int iface;
int epi;
int epo;
int maxsize;
int timeout;
};
int fastboot_parse_result(const char *status)
{
int i;
for (i = 0; i < FASTBOOT_UNKNOWN; i++)
if (strncmp(status, fastboot_results[i], 4) == 0)
return i;
return i;
}
static int fbusb_transfer(struct fbusb *dev, void *buff, int size, int ep)
{
int res, idx;
int len, done;
int transferred = 0;
for (idx = 0; idx < size; idx += done)
{
len = size - idx;
if (dev->maxsize > 0)
{
if (len > dev->maxsize)
len = dev->maxsize;
}
done = 0;
res = libusb_bulk_transfer(dev->h, ep, buff + idx, len, &done, dev->timeout);
transferred += done;
fbusb_log(dev, ep, buff + idx, len, done, res);
if (res != 0 && transferred == 0)
{
printf("libusb_bulk_transfer failed: %s ep=0x%02x len=0x%04x size=0x%04x\n", libusb_strerror(res), ep, len, size);
return -1;
}
if (done < len)
break;
}
return transferred;
}
int fbusb_send(struct fbusb *dev, void *buff, int size)
{
return fbusb_transfer(dev, buff, size, dev->epo);
}
int fbusb_recv(struct fbusb *dev, void *buff, int size)
{
return fbusb_transfer(dev, buff, size, dev->epi);
}
int fbusb_bufcmd_resp(struct fbusb *dev, void *rsp, int *rspsz)
{
int res;
int received;
char *s = rsp;
if (rspsz == NULL || *rspsz < 4)
return -1;
memset(rsp, 0, *rspsz);
received = fbusb_recv(dev, rsp, *rspsz);
if (received >= 4)
{
res = fastboot_parse_result(rsp);
if (res < FASTBOOT_UNKNOWN)
{
received -= 4;
memmove(rsp, rsp + 4, received);
memset(s + received, 0, 4);
*rspsz = received;
return res;
}
printf("fbusb_bufcmd_resp recv unknown fastboot response: '%c%c%c%c' (rspsz=0x%04x received=0x%04x)\n", s[0], s[1], s[2], s[3], *rspsz, received);
*rspsz = received;
return FASTBOOT_UNKNOWN;
}
if (received < 0)
{
printf("fbusb_bufcmd_resp recv failed (rspsz=0x%04x)\n", *rspsz);
*rspsz = 0;
return received;
}
printf("fbusb_bufcmd_resp recv invalid fastboot response: received=0x%04x (rspsz=0x%04x)\n", received, *rspsz);
*rspsz = received;
return FASTBOOT_UNKNOWN;
}
int fbusb_bufcmd(struct fbusb *dev, void *req, int reqsz, void *rsp, int *rspsz)
{
int res = fbusb_send(dev, req, reqsz);
if (res != reqsz)
{
*rspsz = 0;
if (res > 0)
{
printf("[E] fbusb_bufcmd send incomplete: reqsz=0x%02x res=0x%04x\n", reqsz, res);
return -1;
}
printf("[E] fbusb_bufcmd send failed: reqsz=0x%02x res=0x%04x\n", reqsz, res);
return -1;
}
return fbusb_bufcmd_resp(dev, rsp, rspsz);
}
int fbusb_strcmd(struct fbusb *dev, const char *req, char *rsp, int rspmaxsize)
{
int res;
int rspsz = rspmaxsize - 1;
if (rspsz < 4)
return -1;
res = fbusb_bufcmd(dev, (void *)req, strlen(req), rsp, &rspsz);
if (res >= 0)
if (rspsz >= 0 && rspsz < rspmaxsize)
rsp[rspsz] = '\0';
return res;
}
int fbusb_strcmd_resp(struct fbusb *dev, char *rsp, int rspmaxsize)
{
int res;
int rspsz = rspmaxsize - 1;
if (rspsz < 4)
return -1;
res = fbusb_bufcmd_resp(dev, rsp, &rspsz);
if (res >= 0)
if (rspsz >= 0 && rspsz < rspmaxsize)
rsp[rspsz] = '\0';
return res;
}
// e "fbusb.h" //
static int getvar_all(struct fbusb *dev){
int res = fbusb_strcmd(dev, "getvar:all", rxbuff, 65);
while (res == FASTBOOT_INFO){
res = 0;
if(strncmp(rxbuff, "version-bootloader:", 19) == 0){
res = 1;
}
if (verbosity >= LOG_NFO && (res == 1 || strncmp(rxbuff, "unlocked:", 9) == 0 || strncmp(rxbuff, "version-baseband:", 17) == 0 || strncmp(rxbuff, "secure:", 7) == 0 || strncmp(rxbuff, "product:", 8) == 0) || verbosity >= LOG_DBG)
{
printf("%s\n", rxbuff);
}
res = fbusb_strcmd_resp(dev, rxbuff, 65);
}
if (res != FASTBOOT_OKAY)
{
if (res > 0)
{
printf("getvar all failed: %s\n", rxbuff);
}
else
{
printf("getvar all protocol error, res=%d\n", res);
}
}
return res;
}
struct fbusb *fbusb_init(int vid, int pid, int iface, int epi, int epo){
int res;
struct fbusb *dev;
libusb_device_handle *h;
if (res = libusb_init(NULL) < 0){
printf("[E] libusb_init failed: %s\n", libusb_strerror(res));
return NULL;
}
// | 端末の接続を待機中... | waiting for device connection. |
for(uint8_t i = 0; i <= 10; ++i){
h = libusb_open_device_with_vid_pid(NULL, vid, pid);
if(h != NULL){
break;
}
printf("[E] libusb_open_device_with_vid_pid (%04x:%04x) failed (%u)\n", vid, pid, i);
sleep(1);
}
if(h == NULL){
libusb_exit(NULL);
return NULL;
}
// https://developer.mozilla.org/en-US/docs/Web/API/USBDevice/deviceClass
// https://developer.mozilla.org/en-US/docs/Web/API/USBDevice/deviceProtocol
// https://developer.mozilla.org/en-US/docs/Web/API/USBDevice/deviceSubclass
if (libusb_kernel_driver_active(h, 0) == 1){
if(libusb_detach_kernel_driver(h, 0) != 0){
printf("[E] libusb_detach_kernel_driver failed\n");
libusb_close(h);
libusb_exit(NULL);
return NULL;
}
}
// https://developer.mozilla.org/en-US/docs/Web/API/USBDevice/claimInterface
res = libusb_claim_interface(h, iface);
if (res < 0)
{
printf("[E] libusb_claim_interface failed: %s\n", libusb_strerror(res));
libusb_close(h);
libusb_exit(NULL);
return NULL;
}
dev = calloc(1, sizeof(struct fbusb));
if (dev != NULL)
{
dev->h = h;
dev->iface = iface;
dev->epi = epi;
dev->epo = epo;
dev->maxsize = 16 * 1024 * 1024;
dev->timeout = 5000;
return dev;
}
libusb_release_interface(h, iface);
libusb_close(h);
libusb_exit(NULL);
return NULL;
}
int main(){
const int vendor_id = 0x0fce, product_id = 0x0dde, inter_face = 0, endpoint_in = 0x81, endpoint_out = 0x01;
struct fbusb *dev = fbusb_init(vendor_id, product_id, inter_face, endpoint_in, endpoint_out);
if(dev == NULL){
return 1;
}
getvar_all(dev);
libusb_release_interface(dev->h, dev->iface);
libusb_close(dev->h);
libusb_exit(NULL);
free(dev);
return 0;
}