This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
rkflash.c
343 lines (287 loc) · 7.62 KB
/
rkflash.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
/*-
* Copyright (c) 2010, 2011 FUKAUMI Naoki.
* Copyright (c) 2011 Ithamar R. Adema. (added libusb support)
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 <err.h>
#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct cmd {
uint8_t hdr[4]; /* U S B C */
uint32_t cid; /* command id */
uint8_t pad1[4];
uint8_t flag;
uint8_t cmd[4];
uint8_t off[4]; /* 1 sector = 512 byte */
uint8_t pad2[2];
uint8_t size; /* 1 sector = 512 byte */
uint8_t pad3[7];
} __attribute__ ((packed));
struct ret {
uint8_t hdr[4]; /* U S B S */
uint32_t cid; /* command id */
uint8_t pad[5];
} __attribute__ ((packed));
#define SETCMD_INIT(c) \
do { \
(c).cid++; \
(c).flag = 0x80; \
(c).cmd[0] = 0x00; \
(c).cmd[1] = 0x06; \
(c).cmd[2] = 0x00; \
(c).cmd[3] = 0x00; \
(c).off[0] = 0x00; \
(c).off[1] = 0x00; \
(c).off[2] = 0x00; \
(c).off[3] = 0x00; \
(c).size = 0x00; \
} while(/* CONSTCOND */0)
#define SETCMD_UNKNOWN(c) \
do { \
(c).cid++; \
(c).flag = 0x00; \
(c).cmd[0] = 0x00; \
(c).cmd[1] = 0x06; \
(c).cmd[2] = 0x16; \
(c).cmd[3] = 0x00; \
(c).off[0] = 0x00; \
(c).off[1] = 0x00; \
(c).off[2] = 0x00; \
(c).off[3] = 0x00; \
(c).size = 0x00; \
} while(/* CONSTCOND */0)
#define SETCMD_BOOT(c) \
do { \
(c).cid++; \
(c).flag = 0x00; \
(c).cmd[0] = 0x00; \
(c).cmd[1] = 0x06; \
(c).cmd[2] = 0xff; \
(c).cmd[3] = 0x00; \
(c).off[0] = 0x00; \
(c).off[1] = 0x00; \
(c).off[2] = 0x00; \
(c).off[3] = 0x00; \
(c).size = 0x00; \
} while(/* CONSTCOND */0)
#define SETCMD_WRITE(c, off) \
do { \
(c).cid++; \
(c).flag = 0x00; \
(c).cmd[0] = 0x00; \
(c).cmd[1] = 0x0a; \
(c).cmd[2] = 0x15; \
(c).cmd[3] = 0x00; \
(c).off[0] = ((off) >> 24) & 0xff; \
(c).off[1] = ((off) >> 16) & 0xff; \
(c).off[2] = ((off) >> 8) & 0xff; \
(c).off[3] = ((off) >> 0) & 0xff; \
(c).size = 0x20; \
} while(/* CONSTCOND */0)
#define SETCMD_READ(c, off) \
do { \
(c).cid++; \
(c).flag = 0x80; \
(c).cmd[0] = 0x00; \
(c).cmd[1] = 0x0a; \
(c).cmd[2] = 0x14; \
(c).cmd[3] = 0x00; \
(c).off[0] = ((off) >> 24) & 0xff; \
(c).off[1] = ((off) >> 16) & 0xff; \
(c).off[2] = ((off) >> 8) & 0xff; \
(c).off[3] = ((off) >> 0) & 0xff; \
(c).size = 0x20; \
} while(/* CONSTCOND */0)
static int verbose = 0;
#ifdef HAS_LIBUSB
#include <usb.h>
static usb_dev_handle *xsv_handle;
static int
bulk_init(const char *device)
{
struct usb_bus *bus;
struct usb_device *dev;
usb_init();
if (verbose)
usb_set_debug(verbose);
usb_find_busses();
usb_find_devices();
xsv_handle = NULL;
for (bus = usb_busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (device && strcmp(dev->filename, device) != 0)
continue;
if (dev->descriptor.idVendor == 0x2207 &&
(dev->descriptor.idProduct == 0x290a ||
dev->descriptor.idProduct == 0x281a)) {
xsv_handle = usb_open(dev);
if (verbose) {
printf("using rockchip device @ %s\n",
dev->filename);
}
break;
}
}
}
if (xsv_handle) {
usb_set_configuration(xsv_handle,1);
usb_claim_interface(xsv_handle,0);
usb_set_altinterface(xsv_handle,0);
return 0;
} else {
return -1;
}
}
#define BULK_SEND_EP2(buf, size) usb_bulk_write(xsv_handle, 2, (void*)buf, size, 500)
#define BULK_RECV_EP1(buf, size) usb_bulk_read(xsv_handle, 1, (void*)buf, size, 500)
#else
static int fd_in, fd_out;
static int
bulk_init(const char *device)
{
if (device == NULL) {
fd_in = STDIN_FILENO;
fd_out = STDOUT_FILENO;
} else
fd_in = fd_out = open(device, O_RDWR);
return 0;
}
#define BULK_SEND_EP2(buf, size) write(fd_out, (buf), (size))
#define BULK_RECV_EP1(buf, size) read(fd_in, (buf), (size))
#endif
#define _BLOCKSIZE (16 * 1024)
static char *progname;
static void
usage(void)
{
fprintf(stderr, "usage: %s [-D device] [-v] [-B] [-r size] offset file\n", progname);
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
struct cmd c;
struct ret r;
uint32_t off;
int32_t size;
uint8_t *buf;
int ch, img, boot;
const char *device = NULL;
int res;
progname = argv[0];
boot = 0;
size = 0;
while ((ch = getopt(argc, argv, "vBr:D:")) != -1) {
switch (ch) {
case 'v':
verbose++;
break;
case 'B':
boot = 1;
break;
case 'D':
device = strdup(optarg);
break;
case 'r':
size = strtoul(optarg, NULL, 0);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (bulk_init(device) != 0)
errx(EXIT_FAILURE, "Failure finding device\n");
if (argc != 2)
usage();
if ((off = strtoul(argv[0], NULL, 0)) & 0x1f)
errx(EXIT_FAILURE, "offset must be multiple of 0x20\n");
if (size == 0)
img = open(argv[1], O_RDONLY);
else
img = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (img == -1)
err(EXIT_FAILURE, "%s", argv[1]);
if ((buf = malloc(_BLOCKSIZE)) == NULL)
err(EXIT_FAILURE, "malloc");
memset(buf, 0, _BLOCKSIZE);
memset(&c, 0, sizeof(c));
c.hdr[0] = 'U';
c.hdr[1] = 'S';
c.hdr[2] = 'B';
c.hdr[3] = 'C';
do {
SETCMD_INIT(c);
BULK_SEND_EP2(&c, sizeof(c));
res = BULK_RECV_EP1(&r, sizeof(r));
} while(res < 0);
usleep(20 * 1000);
#if 0
SETCMD_UNKNOWN(c);
BULK_SEND_EP2(&c, sizeof(c));
BULK_RECV_EP1(&r, sizeof(r));
usleep(40 * 1000);
#endif
if (size == 0) {
while (read(img, buf, _BLOCKSIZE) > 0) {
if (verbose)
fprintf(stderr, "writing offset 0x%08x\n", off);
SETCMD_WRITE(c, off);
BULK_SEND_EP2(&c, sizeof(c));
BULK_SEND_EP2(buf, _BLOCKSIZE);
if (BULK_RECV_EP1(&r, sizeof(r)) < 0)
fprintf(stderr, "error writing offset 0x%08x\n", off);
off += 0x20;
memset(buf, 0, _BLOCKSIZE);
}
} else {
while (size > 0) {
if (verbose)
fprintf(stderr, "reading offset 0x%08x\n", off);
SETCMD_READ(c, off);
BULK_SEND_EP2(&c, sizeof(c));
BULK_RECV_EP1(buf, _BLOCKSIZE);
if (BULK_RECV_EP1(&r, sizeof(r)) < 0)
fprintf(stderr, "error reading offset 0x%08x\n", off);
write(img, buf, _BLOCKSIZE);
off += 0x20;
size -= 0x20;
}
}
if (boot) {
fprintf(stderr, "booting\n");
usleep(20 * 1000);
SETCMD_BOOT(c);
BULK_SEND_EP2(&c, sizeof(c));
BULK_RECV_EP1(&r, sizeof(r));
}
free(buf);
close(img);
return EXIT_SUCCESS;
}