-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawflash.c
485 lines (422 loc) · 12 KB
/
awflash.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* awflash.c, flash Allwinner A10 devices with Livesuit USB protocol.
* Copyright (c) 2012, Ithamar R. Adema <ithamar@upgrade-android.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* See README and COPYING for more details.
*/
#include <err.h>
#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <usb.h>
#define VENDORID_ALLWINNER 0x1f3a
#define PRODUCTID_SUN4I 0xefe8
#define _BLOCKSIZE (64UL * 1024UL)
static char *progname;
static int verbose = 0;
#ifdef DEBUG
static const char *sTabTab = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
#define DS "%.*s"
#define DA depth - 1, sTabTab
static void dump_hex(const uint32_t *data, int len, int depth)
{
char str[128];
char astr[32];
char *p;
int l;
int i;
for (i = 0; i < len; ) {
p = str;
l = sizeof(str);
for (; i < len && (p == str || (i % 16 != 0)); i++) {
snprintf(p, l - 1, "%02x ", data[i]);
l -= strlen(p);
p += strlen(p);
astr[i % 16] = isprint(data[i]) ? data[i] : '.';
astr[i % 16] = isprint(data[i]) ? data[i] : '.';
astr[(i % 16) + 1] = '\0';
}
printf(DS" %-48.48s %s\n", DA, str, astr);
}
}
#endif /* DEBUG */
static int
match_device(int vendorid, int productid)
{
return vendorid == VENDORID_ALLWINNER && productid == PRODUCTID_SUN4I;
}
static usb_dev_handle *
locate_device(void)
{
unsigned char located = 0;
struct usb_bus *bus;
struct usb_device *dev;
usb_dev_handle *device_handle = 0;
usb_find_busses();
usb_find_devices();
for (bus = usb_busses; bus; bus = bus->next) {
for (dev = bus->devices; dev; dev = dev->next) {
if (match_device(dev->descriptor.idVendor, dev->descriptor.idProduct)) {
located++;
device_handle = usb_open(dev);
if (verbose)
printf("Device Found @ Address %s\n", dev->filename);
}
}
}
return device_handle;
}
static void
usage(void)
{
fprintf(stderr, "usage: %s [-B] [-r size] offset file\n", progname);
exit(EXIT_FAILURE);
}
#define FEX_TIMEOUT 500
#define FEX_EP_OUT 1
#define FEX_EP_IN 2
#define FEX_DIR_IN 0x11
#define FEX_DIR_OUT 0x12
#define FEX_CMAGIC "AWUC"
#define FEX_SMAGIC "AWUS"
#define FEX_ACK_LEN 13
static int
fex_xfer(usb_dev_handle *hdl, void *buffer, size_t len, uint8_t dir)
{
char ackbuf[FEX_ACK_LEN];
int status;
struct cmd {
char magic[4];
uint32_t pad0;
uint32_t len1;
uint8_t pad1[3];
uint8_t val_0c;
uint8_t dir;
uint8_t pad2;
uint32_t len2;
uint8_t pad3[10];
} __attribute__((packed)) cmd;
memset(&cmd, 0, sizeof(cmd));
memcpy(cmd.magic, FEX_CMAGIC, 4);
cmd.len1 = cmd.len2 = len;
cmd.val_0c = 0x0c;
cmd.dir = dir;
status = usb_bulk_write(hdl, FEX_EP_OUT, (void*)&cmd, sizeof(cmd), FEX_TIMEOUT);
if (status != sizeof(cmd)) {
if (verbose) printf("%s: cmd write failed (%d/%lu)!\n", __func__, status, sizeof(cmd));
return (status < 0) ? status : -EIO;
}
if (dir == FEX_DIR_IN) {
status = usb_bulk_read(hdl, FEX_EP_IN, buffer, len, FEX_TIMEOUT);
if (status != len) {
if (verbose) printf("%s: data read failed (%d/%lu)!\n", __func__, status, len);
return (status < 0) ? status : -EIO;
}
} else if (dir == FEX_DIR_OUT) {
status = usb_bulk_write(hdl, FEX_EP_OUT, buffer, len, FEX_TIMEOUT);
if (status != len) {
if (verbose) printf("%s: data write failed (%d/%lu)!\n", __func__, status, len);
return (status < 0) ? status : -EIO;
}
}
status = usb_bulk_read(hdl, FEX_EP_IN, ackbuf, FEX_ACK_LEN, FEX_TIMEOUT);
if (status != FEX_ACK_LEN) {
if (verbose) printf("%s: status read failed (%d/%d)!\n", __func__, status, FEX_ACK_LEN);
return (status < 0) ? status : -EIO;
}
if (memcmp(ackbuf, FEX_SMAGIC, 4) != 0) {
if (verbose) printf("%s: Invalid response magic!\n", __func__);
return -EINVAL;
}
return 0;
}
#define AW_FEL_VERSION 0x0001
#define AW_FEL_1_WRITE 0x0101
#define AW_FEL_1_EXEC 0x0102
#define AW_FEL_1_READ 0x0103
static int
fex_command(usb_dev_handle *hdl, uint32_t cmd, uint32_t arg1, uint32_t arg2, uint32_t arg3, void *data, size_t len)
{
uint32_t cmdbuffer[4] = { cmd, arg1, arg2, arg3 };
int status = fex_xfer(hdl, cmdbuffer, 16, FEX_DIR_OUT);
if (status < 0)
return status;
switch(cmd) {
case AW_FEL_VERSION:
if (data != NULL) {
status = fex_xfer(hdl, data, len, FEX_DIR_IN);
if (status < 0)
return status;
if (strncmp(data, "AWUSBFEX", 8) != 0) {
if (verbose) printf("%s: non-AWUSBFEX response!\n", __func__);
return -EINVAL;
}
} else {
if (verbose) printf("%s: No buffer passed for cmd 0x0001!\n", __func__);
return -EINVAL;
}
break;
case 0x0002:
break;
case 0x0003:
if (data != NULL) {
status = fex_xfer(hdl, data, len, FEX_DIR_IN);
if (status < 0)
return status;
} else {
if (verbose) printf("%s: No buffer passed for cmd 0x0003!\n", __func__);
return -EINVAL;
}
break;
case 0x0004:
if (data != NULL) {
status = fex_xfer(hdl, data, len, FEX_DIR_IN);
if (status < 0)
return status;
} else {
if (verbose) printf("%s: No buffer passed for command 0x0004!\n", __func__);
return -EINVAL;
}
break;
case 0x0010:
break;
case AW_FEL_1_WRITE:
if (data != NULL && len != 0) {
status = fex_xfer(hdl, data, len, FEX_DIR_OUT);
if (status < 0)
return status;
}
break;
case AW_FEL_1_EXEC:
break;
case AW_FEL_1_READ:
if (data != NULL) {
status = fex_xfer(hdl, data, len, FEX_DIR_IN);
if (status < 0)
return status;
}
break;
case 0x0201:
if (data != NULL && len != 0) {
switch((((uint8_t*)cmdbuffer)[13] >> 4) & 3) {
case 0:
case 1:
status = fex_xfer(hdl, data, len, FEX_DIR_OUT);
if (status < 0)
return status;
break;
case 2:
status = fex_xfer(hdl, data, len, FEX_DIR_IN);
if (status < 0)
return status;
break;
case 3:
if (verbose) printf("%s: Invalid subcommand 3 in command 0x0201!\n", __func__);
return -EINVAL;
}
}
break;
case 0x0202:
if (data != NULL) {
status = fex_xfer(hdl, data, len, FEX_DIR_OUT);
if (status < 0)
return status;
} else {
if (verbose) printf("%s: No buffer passed for command 0x0202!\n", __func__);
return -EINVAL;
}
break;
case 0x0203:
{
uint8_t subcmd = ((uint8_t*)cmdbuffer)[4];
if (subcmd & 0xF0) {
if ((subcmd & 0xF0) != 0x10) {
if (verbose) printf("%s: Unrecognized subcommand 0x%x for command 0x0203!\n",
__func__, subcmd);
return -EINVAL;
} else {
status = fex_xfer(hdl, data, len, (subcmd & 8) ? FEX_DIR_OUT : FEX_DIR_IN);
if (status < 0)
return status;
}
} else {
if (data != NULL) {
status = fex_xfer(hdl, data, len, FEX_DIR_IN);
if (status < 0)
return status;
} else {
if (verbose) printf("%s: No buffer specified for command 0x0203!\n", __func__);
return -EINVAL;
}
}
}
break;
default:
if (verbose) printf("%s: Unknown command 0x%04x passed!\n", __func__, cmd);
return -EINVAL;
}
uint8_t statusbuf[8];
status = fex_xfer(hdl, statusbuf, 8, FEX_DIR_IN);
if (status < 0)
return status;
if (statusbuf[4] != 0) {
printf("%s: Invalid status %d!\n", __func__, statusbuf[4]);
return -EINVAL;
}
return 0;
}
void
fex_write_file(uint32_t addr, char *filename, size_t len) {
(void)addr;
(void)filename;
(void)len;
}
/* Return protocol version; or negative error code */
static int
aw_fel_get_version(usb_dev_handle *usb) {
struct aw_fel_version {
char signature[8];
uint32_t unknown_08; /* 0x00162300 */
uint32_t unknown_0a; /* 1 */
uint16_t protocol; /* 1 */
uint8_t unknown_12; /* 0x44 */
uint8_t unknown_13; /* 0x08 */
uint32_t scratchpad; /* 0x7e00 */
uint32_t pad[2]; /* unused */
} __attribute__((packed)) buf;
int status;
status = fex_command(usb, AW_FEL_VERSION, 0, 0, 0, &buf, sizeof(buf));
if (status < 0)
return status;
return buf.protocol;
}
int
main(int argc, char *argv[])
{
usb_dev_handle *usb;
int open_status;
uint8_t *buf;
int ch, img;
progname = argv[0];
while ((ch = getopt(argc, argv, "v")) != -1) {
switch (ch) {
case 'v':
verbose++;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 2)
usage();
img = open(argv[1], O_RDONLY, 0);
if (img == -1)
err(EXIT_FAILURE, "%s", argv[1]);
if ((buf = malloc(_BLOCKSIZE)) == NULL)
err(EXIT_FAILURE, "malloc");
memset(buf, 0, _BLOCKSIZE);
usb_init();
if (verbose)
usb_set_debug(verbose);
if ((usb = locate_device()) == 0) {
err(EXIT_FAILURE, "Could not find device\n");
return (-1);
}
open_status = usb_set_configuration(usb,1);
if (verbose)
printf("conf_stat=%d\n",open_status);
open_status = usb_claim_interface(usb,0);
if (verbose)
printf("claim_stat=%d\n",open_status);
open_status = usb_set_altinterface(usb,0);
if (verbose)
printf("alt_stat=%d\n",open_status);
/* Check device mode (FEX protocol version) */
if (aw_fel_get_version(usb) == 1) {
/* We're version 1, so lets load the v2 protocol */
char buffer[0x100];
aw_fel_get_version(usb);
if (fex_command(usb, AW_FEL_1_READ, 0x7e00, sizeof(buffer), 0, buffer, sizeof(buffer)) < 0 ||
*(uint32_t*)buffer == 0) {
fprintf(stderr, "Error: device previously failed to switch; please power off/on device\n");
exit(EXIT_FAILURE);
}
aw_fel_get_version(usb);
*(uint32_t*)buffer = 0;
if (fex_command(usb, AW_FEL_1_WRITE, 0x7e00, sizeof(buffer), 0, buffer, sizeof(buffer)) < 0) {
fprintf(stderr, "Error: unable to write command to prepare to switch\n");
exit(EXIT_FAILURE);
}
fex_command(usb, AW_FEL_1_WRITE, 0x7010, 180, 0, buffer, 180);
memset(buffer, 0, 16);
fex_command(usb, AW_FEL_1_WRITE, 0x7210, 16, 0, buffer, 16);
fex_write_file(0x7220, "fex_1_1.fex", 0xae0);
fex_command(usb, AW_FEL_1_EXEC, 0x7220, 0, 0, NULL, 0);
fex_command(usb, AW_FEL_1_READ, 0x7210, 16, 0, buffer, 16);
if (memcmp(buffer, "DRAM\x01", 5) != 0) {
fprintf(stderr, "Error: could not find DRAM magic!\n");
exit(EXIT_FAILURE);
}
fex_write_file(0x2000, "fes_1-2.fex", 0);
fex_command(usb, AW_FEL_1_EXEC, 0x2000, 0, 0, NULL, 0);
}
close(img);
return 0;
}
/*
0x00002000 0x00000c1c fes_1-2.fex load area
0x00007010 0x000000b4 see sys_para.txt
0x00007210 0x00000010 "DRAM" magic
0x00007220 0x00000ae0 fex_1_1.fex load area
0x00007e00 0x00000100 marker block containing 0xcc values (first 4 bytes 0 after successful switch)
0x00023000 0x00010000
0x40100000 0x00002000
0x40200000 0x00010000 fes.fex load area???
0x40210000 0x00003c40 all 0x05 bytes
*/
/*
A10 flash sequence:
AW_FEL_VERSION
AW_FEL_VERSION
AW_FEL_1_READ 0x00007e00 0x100 (reads back block of 0xcc)
AW_FEL_VERSION
AW_FEL_1_WRITE 0x00007e00 0x100 (writes block of 0xcc; first 4 bytes 0x00)
AW_FEL_1_WRITE 0x00007010 0xb4 (board & dram config)
AW_FEL_1_WRITE 0x00007210 0x10 (all zeroes)
AW_FEL_1_WRITE 0x00007220 0xae0 (fex_1_1.fex padded with zeroes at end)
AW_FEL_1_EXEC 0x00007220
AW_FEL_1_READ 0x00007210 0x10 (reads back "DRAM" padded with zeroes at end)
AW_FEL_1_WRITE 0x00007210 0x10 (all zeroes)
AW_FEL_1_WRITE 0x00002000 0xc1c (fes_1-2.fex)
AW_FEL_1_EXEC 0x00002000
AW_FEL_1_READ 0x00007210 0x10 ("DRAM\x01" padded with zeroes at end)
AW_FEL_1_READ 0x00007010 0xb4 ()
AW_FEL_1_WRITE 0x40100000 0x2000 ()
AW_FEL_1_READ 0x40100000 0x2000 ()
AW_FEL_1_WRITE 0x00007210 0x10 (all zeroes)
AW_FEL_1_WRITE 0x00023000 0x10000 (all zeroes)
AW_FEL_1_WRITE 0x40200000 0x10000 (fes.fex?)
AW_FEL_1_WRITE 0x00023000 0x10000 (all zeroes)
AW_FEL_1_WRITE 0x00033000 0x3c40 (all zeroes)
AW_FEL_1_WRITE 0x40210000 0x3c40 (all 0x05 bytes)
AW_FEL_1_WRITE 0x00033000 0x3c40 (all zeroes)
AW_FEL_1_WRITE 0x00023000 0x5d0 (all zeroes)
AW_FEL_1_WRITE 0x00007220 0x5d0 (fes_2.fex)
AW_FEL_1_WRITE 0x00023000 0x5d0 (all zeroes)
AW_FEL_1_EXEC 0x00007220
<device reset>
AW_FEL_VERSION
AW_FEL_VERSION
*/