forked from linux-msm/cdba
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastboot.c
463 lines (360 loc) · 9.64 KB
/
fastboot.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
* Copyright (c) 2016-2018, Linaro Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <linux/usbdevice_fs.h>
#include <linux/usb/ch9.h>
#include <sys/ioctl.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libudev.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "bad.h"
#include "fastboot.h"
#define MAX_USBFS_BULK_SIZE (16*1024)
struct fastboot {
const char *serial;
int fd;
unsigned ep_in;
unsigned ep_out;
const char *dev_path;
void *data;
struct fastboot_ops *ops;
int state;
struct udev_monitor *mon;
};
enum {
FASTBOOT_STATE_START,
FASTBOOT_STATE_OPENED,
FASTBOOT_STATE_CLOSED,
};
static int fastboot_read(struct fastboot *fb, char *buf, size_t len)
{
struct usbdevfs_bulktransfer bulk = {0};
char status[65];
int n;
for (;;) {
bulk.ep = fb->ep_in;
bulk.len = 64;
bulk.data = status;
bulk.timeout = 1000;
n = ioctl(fb->fd, USBDEVFS_BULK, &bulk);
if (n < 0) {
warn("failed to receive usb bulk transfer");
return -ENXIO;
}
status[n] = '\0';
if (n < 4) {
warn("malformed response from fastboot");
return -1;
}
if (strncmp(status, "INFO", 4) == 0) {
fb->ops->info(fb, status + 4, n - 4);
} else if (strncmp(status, "OKAY", 4) == 0) {
if (buf) {
strncpy(buf, status + 4, len);
buf[len - 1] = '\0';
}
return n - 4;
} else if (strncmp(status, "FAIL", 4) == 0) {
fprintf(stderr, "%s\n", status + 4);
return -ENXIO;
} else if (strncmp(status, "DATA", 4) == 0) {
return strtol(status + 4, NULL, 16);
}
}
return 0;
}
static int fastboot_write(struct fastboot *fb, const void *data, size_t len)
{
struct usbdevfs_bulktransfer bulk = {0};
size_t count = 0;
int n;
do {
bulk.ep = fb->ep_out;
bulk.len = MIN(len, MAX_USBFS_BULK_SIZE);
bulk.data = (void*)data;
bulk.timeout = 1000;
n = ioctl(fb->fd, USBDEVFS_BULK, &bulk);
if (n < 0) {
warn("failed to send usb bulk transfer");
return -1;
}
data += n;
len -= n;
count += n;
} while (len > 0);
return count;
}
static int parse_usb_desc(int usbfd, unsigned *ep_in, unsigned *ep_out)
{
const struct usb_interface_descriptor *ifc;
const struct usb_endpoint_descriptor *ept;
const struct usb_device_descriptor *dev;
const struct usb_config_descriptor *cfg;
const struct usb_descriptor_header *hdr;
unsigned type;
unsigned out;
unsigned in;
unsigned k;
unsigned l;
ssize_t n;
void *ptr;
void *end;
char desc[1024];
int ret;
int id;
n = read(usbfd, desc, sizeof(desc));
if (n < 0)
return n;
ptr = (void*)desc;
end = ptr + n;
dev = ptr;
ptr += dev->bLength;
if (ptr >= end || dev->bDescriptorType != USB_DT_DEVICE)
return -EINVAL;
cfg = ptr;
ptr += cfg->bLength;
if (ptr >= end || cfg->bDescriptorType != USB_DT_CONFIG)
return -EINVAL;
for (k = 0; k < cfg->bNumInterfaces; k++) {
if (ptr >= end)
return -EINVAL;
do {
ifc = ptr;
if (ifc->bLength < USB_DT_INTERFACE_SIZE)
return -EINVAL;
ptr += ifc->bLength;
} while (ptr < end && ifc->bDescriptorType != USB_DT_INTERFACE);
in = -1;
out = -1;
for (l = 0; l < ifc->bNumEndpoints; l++) {
if (ptr >= end)
return -EINVAL;
do {
ept = ptr;
if (ept->bLength < USB_DT_ENDPOINT_SIZE)
return -EINVAL;
ptr += ept->bLength;
} while (ptr < end && ept->bDescriptorType != USB_DT_ENDPOINT);
type = ept->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
if (type != USB_ENDPOINT_XFER_BULK)
continue;
if (ept->bEndpointAddress & USB_DIR_IN)
in = ept->bEndpointAddress;
else
out = ept->bEndpointAddress;
if (ptr >= end)
break;
hdr = ptr;
if (hdr->bDescriptorType == USB_DT_SS_ENDPOINT_COMP)
ptr += USB_DT_SS_EP_COMP_SIZE;
}
if (ifc->bInterfaceClass != 0xff)
continue;
if (ifc->bInterfaceSubClass != 0x42)
continue;
if (ifc->bInterfaceProtocol != 0x03)
continue;
id = ifc->bInterfaceNumber;
ret = ioctl(usbfd, USBDEVFS_CLAIMINTERFACE, &id);
if (ret < 0) {
warn("failed to claim interface");
continue;
}
*ep_in = in;
*ep_out = out;
return 0;
}
return -ENOENT;
}
static int handle_udev_event(int fd, void *data)
{
struct fastboot *fastboot = data;
struct udev_device* dev;
const char *dev_path;
const char *dev_node;
const char *action;
const char *serial;
unsigned ep_out;
unsigned ep_in;
int usbfd;
int ret;
dev = udev_monitor_receive_device(fastboot->mon);
action = udev_device_get_action(dev);
dev_node = udev_device_get_devnode(dev);
dev_path = udev_device_get_devpath(dev);
// vid = udev_device_get_sysattr_value(dev, "idVendor");
// pid = udev_device_get_sysattr_value(dev, "idProduct");
serial = udev_device_get_sysattr_value(dev, "serial");
if (!action || !dev_path)
goto unref_dev;
if (!strcmp(action, "add")) {
if (!serial || strcmp(serial, fastboot->serial))
goto unref_dev;
usbfd = open(dev_node, O_RDWR);
if (usbfd < 0)
goto unref_dev;
ret = parse_usb_desc(usbfd, &ep_in, &ep_out);
if (ret < 0) {
close(usbfd);
goto unref_dev;
}
fastboot->ep_in = ep_in;
fastboot->ep_out = ep_out;
fastboot->fd = usbfd;
fastboot->dev_path = strdup(dev_path);
fastboot->state = FASTBOOT_STATE_OPENED;
if (fastboot->ops && fastboot->ops->opened)
fastboot->ops->opened(fastboot, fastboot->data);
} else if (!strcmp(action, "remove")) {
if (!fastboot->dev_path || strcmp(dev_path, fastboot->dev_path))
goto unref_dev;
close(fastboot->fd);
fastboot->fd = -1;
fastboot->dev_path = NULL;
if (fastboot->ops && fastboot->ops->disconnect)
fastboot->ops->disconnect(fastboot->data);
fastboot->state = FASTBOOT_STATE_CLOSED;
}
unref_dev:
udev_device_unref(dev);
return 0;
}
struct fastboot *fastboot_open(const char *serial, struct fastboot_ops *ops, void *data)
{
struct fastboot *fb;
struct udev* udev;
int fd;
udev = udev_new();
if (!udev)
err(1, "udev_new() failed");
fb = calloc(1, sizeof(struct fastboot));
if (!fb)
err(1, "failed to allocate fastboot structure");
fb->serial = serial;
fb->ops = ops;
fb->data = data;
fb->state = FASTBOOT_STATE_START;
fb->mon = udev_monitor_new_from_netlink(udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(fb->mon, "usb", NULL);
udev_monitor_enable_receiving(fb->mon);
fd = udev_monitor_get_fd(fb->mon);
watch_add_readfd(fd, handle_udev_event, fb);
return fb;
}
int fastboot_getvar(struct fastboot *fb, const char *var, char *buf, size_t len)
{
char cmd[128];
int n;
n = snprintf(cmd, sizeof(cmd), "getvar:%s", var);
fastboot_write(fb, cmd, n);
return fastboot_read(fb, buf, len);
}
int fastboot_download(struct fastboot *fb, const void *data, size_t len)
{
size_t xfer;
ssize_t n;
size_t offset = 0;
void *buf;
char cmd[32];
int ret = 0;
buf = malloc(MAX_USBFS_BULK_SIZE);
if (!buf)
err(1, "failed to allocate usb scratch buffer");
n = sprintf(cmd, "download:%08x", (unsigned int)len);
fastboot_write(fb, cmd, n);
n = fastboot_read(fb, buf, MAX_USBFS_BULK_SIZE);
if (n < 0) {
fprintf(stderr, "remote rejected download request\n");
return -1;
}
while (len > 0) {
xfer = MIN(len, MAX_USBFS_BULK_SIZE);
ret = fastboot_write(fb, data + offset, xfer);
if (ret < 0)
goto out;
offset += xfer;
len -= xfer;
}
ret = fastboot_read(fb, NULL, 0);
out:
free(buf);
return ret;
}
int fastboot_boot(struct fastboot *fb)
{
char buf[80];
int n;
fastboot_write(fb, "boot", 4);
n = fastboot_read(fb, buf, sizeof(buf));
if (n >= 0)
fprintf(stderr, "%s\n", buf);
return 0;
}
int fastboot_erase(struct fastboot *fb, const char *partition)
{
char buf[80];
int n;
n = sprintf(buf, "erase:%s", partition);
fastboot_write(fb, buf, n);
fastboot_read(fb, buf, sizeof(buf));
return 0;
}
int fastboot_set_active(struct fastboot *fb, const char *active)
{
char buf[80];
int n;
n = sprintf(buf, "set_active:%s", active);
fastboot_write(fb, buf, n);
fastboot_read(fb, buf, sizeof(buf));
return 0;
}
int fastboot_flash(struct fastboot *fb, const char *partition)
{
char buf[80];
int n;
n = sprintf(buf, "flash:%s", partition);
fastboot_write(fb, buf, n);
fastboot_read(fb, buf, sizeof(buf));
return 0;
}
int fastboot_reboot(struct fastboot *fb)
{
char buf[80];
fastboot_write(fb, "reboot", 6);
fastboot_read(fb, buf, sizeof(buf));
return 0;
}