-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotocol.c
412 lines (343 loc) · 9.48 KB
/
protocol.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
/*
* Copyright (C) 2008 The Android Open Source Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
*
* 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 OWNER 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.
*/
#define min(a, b) \
({ typeof(a) _a = (a); typeof(b) _b = (b); (_a < _b) ? _a : _b; })
#define round_down(a, b) \
({ typeof(a) _a = (a); typeof(b) _b = (b); _a - (_a % _b); })
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sparse/sparse.h>
#include "fastboot.h"
static char ERROR[128];
char *fb_get_error(void)
{
return ERROR;
}
static int check_response(usb_handle *usb, unsigned int size, char *response)
{
unsigned char status[65];
int r;
for(;;) {
r = usb_read(usb, status, 64);
if(r < 0) {
sprintf(ERROR, "status read failed (%s)", strerror(errno));
usb_close(usb);
return -1;
}
status[r] = 0;
if(r < 4) {
sprintf(ERROR, "status malformed (%d bytes)", r);
usb_close(usb);
return -1;
}
if(!memcmp(status, "INFO", 4)) {
fprintf(stderr,"(bootloader) %s\n", status + 4);
continue;
}
if(!memcmp(status, "OKAY", 4)) {
if(response) {
strcpy(response, (char*) status + 4);
}
return 0;
}
if(!memcmp(status, "FAIL", 4)) {
if(r > 4) {
sprintf(ERROR, "remote: %s", status + 4);
} else {
strcpy(ERROR, "remote failure");
}
return -1;
}
if(!memcmp(status, "DATA", 4) && size > 0){
unsigned dsize = strtoul((char*) status + 4, 0, 16);
if(dsize > size) {
strcpy(ERROR, "data size too large");
usb_close(usb);
return -1;
}
return dsize;
}
strcpy(ERROR,"unknown status code");
usb_close(usb);
break;
}
return -1;
}
static int _command_start(usb_handle *usb, const char *cmd, unsigned size,
char *response)
{
int cmdsize = strlen(cmd);
if(response) {
response[0] = 0;
}
if(cmdsize > 64) {
sprintf(ERROR,"command too large");
return -1;
}
if(usb_write(usb, cmd, cmdsize) != cmdsize) {
sprintf(ERROR,"command write failed (%s)", strerror(errno));
usb_close(usb);
return -1;
}
return check_response(usb, size, response);
}
static int _command_data(usb_handle *usb, const void *data, unsigned size)
{
int r;
r = usb_write(usb, data, size);
if(r < 0) {
sprintf(ERROR, "data transfer failure (%s)", strerror(errno));
usb_close(usb);
return -1;
}
if(r != ((int) size)) {
sprintf(ERROR, "data transfer failure (short transfer)");
usb_close(usb);
return -1;
}
return r;
}
static int _command_end(usb_handle *usb)
{
int r;
r = check_response(usb, 0, 0);
if(r < 0) {
return -1;
}
return 0;
}
static int _command_send(usb_handle *usb, const char *cmd,
const void *data, unsigned size,
char *response)
{
int r;
if (size == 0) {
return -1;
}
r = _command_start(usb, cmd, size, response);
if (r < 0) {
return -1;
}
r = _command_data(usb, data, size);
if (r < 0) {
return -1;
}
r = _command_end(usb);
if(r < 0) {
return -1;
}
return size;
}
static int _command_send_no_data(usb_handle *usb, const char *cmd,
char *response)
{
return _command_start(usb, cmd, 0, response);
}
int fb_command(usb_handle *usb, const char *cmd)
{
return _command_send_no_data(usb, cmd, 0);
}
int fb_command_response(usb_handle *usb, const char *cmd, char *response)
{
return _command_send_no_data(usb, cmd, response);
}
int fb_download_data(usb_handle *usb, const void *data, unsigned size)
{
char cmd[64];
int r;
sprintf(cmd, "download:%08x", size);
r = _command_send(usb, cmd, data, size, 0);
if(r < 0) {
return -1;
} else {
return 0;
}
}
#define USB_BUF_SIZE 1024
static char usb_buf[USB_BUF_SIZE];
static int usb_buf_len;
static int fb_download_data_sparse_write(void *priv, const void *data, int len)
{
int r;
usb_handle *usb = priv;
int to_write;
const char *ptr = data;
if (usb_buf_len) {
to_write = min(USB_BUF_SIZE - usb_buf_len, len);
memcpy(usb_buf + usb_buf_len, ptr, to_write);
usb_buf_len += to_write;
ptr += to_write;
len -= to_write;
}
if (usb_buf_len == USB_BUF_SIZE) {
r = _command_data(usb, usb_buf, USB_BUF_SIZE);
if (r != USB_BUF_SIZE) {
return -1;
}
usb_buf_len = 0;
}
if (len > USB_BUF_SIZE) {
if (usb_buf_len > 0) {
sprintf(ERROR, "internal error: usb_buf not empty\n");
return -1;
}
to_write = round_down(len, USB_BUF_SIZE);
r = _command_data(usb, ptr, to_write);
if (r != to_write) {
return -1;
}
ptr += to_write;
len -= to_write;
}
if (len > 0) {
if (len > USB_BUF_SIZE) {
sprintf(ERROR, "internal error: too much left for usb_buf\n");
return -1;
}
memcpy(usb_buf, ptr, len);
usb_buf_len = len;
}
return 0;
}
static int fb_download_data_sparse_flush(usb_handle *usb)
{
int r;
if (usb_buf_len > 0) {
r = _command_data(usb, usb_buf, usb_buf_len);
if (r != usb_buf_len) {
return -1;
}
usb_buf_len = 0;
}
return 0;
}
int fb_download_data_sparse(usb_handle *usb, struct sparse_file *s)
{
char cmd[64];
int r;
int size = sparse_file_len(s, true, false);
if (size <= 0) {
return -1;
}
sprintf(cmd, "download:%08x", size);
r = _command_start(usb, cmd, size, 0);
if (r < 0) {
return -1;
}
r = sparse_file_callback(s, true, false, fb_download_data_sparse_write, usb);
if (r < 0) {
return -1;
}
fb_download_data_sparse_flush(usb);
return _command_end(usb);
}
#define DUMP_BUFSIZE 0x100000
static int dump_file(usb_handle *usb, const char *filename)
{
int r = -1;
FILE *file;
uint64_t filesize;
char *buf;
// allocate buf
buf = malloc(DUMP_BUFSIZE);
if (!buf) die("out of memory");
memset(buf, 0, DUMP_BUFSIZE);
// open file
file = fopen64(filename, "wb");
if(!file) die("open file failed");
// read header
r = usb_read(usb, buf, 20);
if(r < 0) {
sprintf(ERROR, "status read failed (%s)", strerror(errno));
usb_close(usb);
r = -1;
goto out;
}
buf[r] = 0;
// get filesize
if(sscanf(buf, "DATA%016llx", &filesize) != 1 ) {
sprintf(ERROR, "invalid protocol(%s)", buf);
usb_close(usb);
r = -1;
goto out;
}
uint64_t size_left = filesize;
uint64_t size_read = 0;
uint64_t size_chunk = size_left;
for(;;) {
if(size_chunk>DUMP_BUFSIZE)
size_chunk = DUMP_BUFSIZE;
// read chunk
r = usb_read(usb, buf, size_chunk);
if(r < 0) {
sprintf(ERROR, "data read failed (%s)", strerror(errno));
usb_close(usb);
r = -1;
break;
}
// chunk data to file
if(fwrite(buf, 1, r, file)<(size_t)r) {
sprintf(ERROR, "data write failed (%s)", strerror(errno));
usb_close(usb);
r = -1;
break;
}
// recalculate sizes
size_left -= r;
size_read += r;
size_chunk = size_left;
// print status
fwrite("\b\b\b\b\b\b\b", 1, 7, stderr);
fprintf(stderr, "%.2f%%", (double)size_read * 100.0 / (double)filesize);
// done
if (!size_left) {
r = 0;
break;
}
}
// clear status
fwrite("\b\b\b\b\b\b\b", 1, 7, stderr);
out:
if(file)
fclose(file);
if(buf)
free(buf);
return r;
}
int fb_pull_file(usb_handle *usb, const char *file_name)
{
int r;
r = dump_file(usb, file_name);
if (r < 0) {
return -1;
}
return _command_end(usb);
}