-
Notifications
You must be signed in to change notification settings - Fork 76
/
fel.c
434 lines (397 loc) · 10.1 KB
/
fel.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
#include <fel.h>
extern struct chip_t a10;
extern struct chip_t a13_a10s_r8;
extern struct chip_t a20;
extern struct chip_t a23;
extern struct chip_t a31;
extern struct chip_t a33_r16;
extern struct chip_t a40i_r40;
extern struct chip_t a50;
extern struct chip_t a523_a527_t527;
extern struct chip_t a64;
extern struct chip_t a80;
extern struct chip_t a83t;
extern struct chip_t d1_f133;
extern struct chip_t f1c100s_f1c200s_f1c500s;
extern struct chip_t h2_h3;
extern struct chip_t h5;
extern struct chip_t h6;
extern struct chip_t h616;
extern struct chip_t r128;
extern struct chip_t r328;
extern struct chip_t r329;
extern struct chip_t r528_t113;
extern struct chip_t r818;
extern struct chip_t v3s_s3;
extern struct chip_t v536;
extern struct chip_t v831;
extern struct chip_t v851_v853;
static struct chip_t * chips[] = {
&a10,
&a13_a10s_r8,
&a20,
&a23,
&a31,
&a33_r16,
&a40i_r40,
&a50,
&a523_a527_t527,
&a64,
&a80,
&a83t,
&d1_f133,
&f1c100s_f1c200s_f1c500s,
&h2_h3,
&h5,
&h6,
&h616,
&r128,
&r328,
&r329,
&r528_t113,
&r818,
&v3s_s3,
&v536,
&v831,
&v851_v853,
};
struct usb_request_t {
char magic[8];
uint32_t length;
uint32_t unknown1;
uint16_t request;
uint32_t length2;
char pad[10];
} __attribute__((packed));
struct fel_request_t {
uint32_t request;
uint32_t address;
uint32_t length;
uint32_t pad;
} __attribute__((packed));
static inline void usb_bulk_send(libusb_device_handle * hdl, int ep, const char * buf, size_t len)
{
size_t max_chunk = 128 * 1024;
size_t chunk;
int r, bytes;
while(len > 0)
{
chunk = len < max_chunk ? len : max_chunk;
r = libusb_bulk_transfer(hdl, ep, (void *)buf, chunk, &bytes, 10000);
if(r != 0)
{
printf("usb bulk send error\r\n");
exit(-1);
}
len -= bytes;
buf += bytes;
}
}
static inline void usb_bulk_recv(libusb_device_handle * hdl, int ep, char * buf, size_t len)
{
int r, bytes;
while(len > 0)
{
r = libusb_bulk_transfer(hdl, ep, (void *)buf, len, &bytes, 10000);
if(r != 0)
{
printf("usb bulk recv error\r\n");
exit(-1);
}
len -= bytes;
buf += bytes;
}
}
static inline void send_usb_request(struct xfel_ctx_t * ctx, int type, size_t length)
{
struct usb_request_t req = {
.magic = "AWUC",
.request = cpu_to_le16(type),
.length = cpu_to_le32(length),
.unknown1 = cpu_to_le32(0x0c000000)
};
req.length2 = req.length;
usb_bulk_send(ctx->hdl, ctx->epout, (const char *)&req, sizeof(struct usb_request_t));
}
static inline void read_usb_response(struct xfel_ctx_t * ctx)
{
char buf[13];
usb_bulk_recv(ctx->hdl, ctx->epin, (char *)buf, sizeof(buf));
assert(strcmp(buf, "AWUS") == 0);
}
static inline void usb_write(struct xfel_ctx_t * ctx, const void * buf, size_t len)
{
send_usb_request(ctx, 0x12, len);
usb_bulk_send(ctx->hdl, ctx->epout, (const char *)buf, len);
read_usb_response(ctx);
}
static inline void usb_read(struct xfel_ctx_t * ctx, const void * data, size_t len)
{
send_usb_request(ctx, 0x11, len);
usb_bulk_send(ctx->hdl, ctx->epin, (const char *)data, len);
read_usb_response(ctx);
}
static inline void send_fel_request(struct xfel_ctx_t * ctx, int type, uint32_t addr, uint32_t length)
{
struct fel_request_t req = {
.request = cpu_to_le32(type),
.address = cpu_to_le32(addr),
.length = cpu_to_le32(length)
};
usb_write(ctx, &req, sizeof(struct fel_request_t));
}
static inline void read_fel_status(struct xfel_ctx_t * ctx)
{
char buf[8];
usb_read(ctx, buf, sizeof(buf));
}
static inline int fel_version(struct xfel_ctx_t * ctx)
{
int i;
send_fel_request(ctx, 0x001, 0, 0);
usb_read(ctx, &ctx->version, sizeof(ctx->version));
read_fel_status(ctx);
ctx->version.id = le32_to_cpu(ctx->version.id);
ctx->version.firmware = le32_to_cpu(ctx->version.firmware);
ctx->version.protocol = le16_to_cpu(ctx->version.protocol);
ctx->version.scratchpad = le32_to_cpu(ctx->version.scratchpad);
for(i = 0; i < ARRAY_SIZE(chips); i++)
{
ctx->chip = chips[i];
if(fel_chip_detect(ctx, ctx->version.id))
return 1;
}
printf("WARNING: Not yet support this device ID 0x%08x\r\n", ctx->version.id);
return 0;
}
int fel_init(struct xfel_ctx_t * ctx)
{
if(ctx && ctx->hdl)
{
struct libusb_config_descriptor * config;
int if_idx, set_idx, ep_idx;
const struct libusb_interface * iface;
const struct libusb_interface_descriptor * setting;
const struct libusb_endpoint_descriptor * ep;
if(libusb_kernel_driver_active(ctx->hdl, 0))
libusb_detach_kernel_driver(ctx->hdl, 0);
if(libusb_claim_interface(ctx->hdl, 0) == 0)
{
if(libusb_get_active_config_descriptor(libusb_get_device(ctx->hdl), &config) == 0)
{
for(if_idx = 0; if_idx < config->bNumInterfaces; if_idx++)
{
iface = config->interface + if_idx;
for(set_idx = 0; set_idx < iface->num_altsetting; set_idx++)
{
setting = iface->altsetting + set_idx;
for(ep_idx = 0; ep_idx < setting->bNumEndpoints; ep_idx++)
{
ep = setting->endpoint + ep_idx;
if((ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) != LIBUSB_TRANSFER_TYPE_BULK)
continue;
if((ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN)
ctx->epin = ep->bEndpointAddress;
else
ctx->epout = ep->bEndpointAddress;
}
}
}
libusb_free_config_descriptor(config);
return fel_version(ctx);
}
}
}
return 0;
}
void fel_exec(struct xfel_ctx_t * ctx, uint32_t addr)
{
send_fel_request(ctx, 0x102, addr, 0);
read_fel_status(ctx);
}
static inline void fel_read_raw(struct xfel_ctx_t * ctx, uint32_t addr, void * buf, size_t len)
{
send_fel_request(ctx, 0x103, addr, len);
usb_read(ctx, buf, len);
read_fel_status(ctx);
}
static inline void fel_write_raw(struct xfel_ctx_t * ctx, uint32_t addr, void * buf, size_t len)
{
send_fel_request(ctx, 0x101, addr, len);
usb_write(ctx, buf, len);
read_fel_status(ctx);
}
uint32_t fel_read32(struct xfel_ctx_t * ctx, uint32_t addr)
{
uint32_t val;
fel_read_raw(ctx, addr, &val, sizeof(uint32_t));
return val;
}
void fel_write32(struct xfel_ctx_t * ctx, uint32_t addr, uint32_t val)
{
fel_write_raw(ctx, addr, &val, sizeof(uint32_t));
}
void fel_read(struct xfel_ctx_t * ctx, uint32_t addr, void * buf, size_t len)
{
size_t n;
while(len > 0)
{
n = len > 65536 ? 65536 : len;
fel_read_raw(ctx, addr, buf, n);
addr += n;
buf += n;
len -= n;
}
}
void fel_write(struct xfel_ctx_t * ctx, uint32_t addr, void * buf, size_t len)
{
size_t n;
while(len > 0)
{
n = len > 65536 ? 65536 : len;
fel_write_raw(ctx, addr, buf, n);
addr += n;
buf += n;
len -= n;
}
}
void fel_read_progress(struct xfel_ctx_t * ctx, uint32_t addr, void * buf, size_t len)
{
struct progress_t p;
size_t n;
progress_start(&p, len);
while(len > 0)
{
n = len > 65536 ? 65536 : len;
fel_read_raw(ctx, addr, buf, n);
addr += n;
buf += n;
len -= n;
progress_update(&p, n);
}
progress_stop(&p);
}
void fel_write_progress(struct xfel_ctx_t * ctx, uint32_t addr, void * buf, size_t len)
{
struct progress_t p;
size_t n;
progress_start(&p, len);
while(len > 0)
{
n = len > 65536 ? 65536 : len;
fel_write_raw(ctx, addr, buf, n);
addr += n;
buf += n;
len -= n;
progress_update(&p, n);
}
progress_stop(&p);
}
int fel_spi_init(struct xfel_ctx_t * ctx, uint32_t * swapbuf, uint32_t * swaplen, uint32_t * cmdlen)
{
uint8_t cbuf[2];
if(!fel_chip_spi_init(ctx, swapbuf, swaplen, cmdlen))
return 0;
cbuf[0] = SPI_CMD_INIT;
cbuf[1] = SPI_CMD_END;
if(!fel_chip_spi_run(ctx, cbuf, sizeof(cbuf)))
return 0;
return 1;
}
int fel_spi_xfer(struct xfel_ctx_t * ctx, uint32_t swapbuf, uint32_t swaplen, uint32_t cmdlen, void * txbuf, uint32_t txlen, void * rxbuf, uint32_t rxlen)
{
uint8_t cbuf[256];
uint32_t clen;
uint32_t n;
if((txlen <= swaplen) && (rxlen <= swaplen))
{
clen = 0;
cbuf[clen++] = SPI_CMD_SELECT;
if(txlen > 0)
{
cbuf[clen++] = SPI_CMD_TXBUF;
cbuf[clen++] = (swapbuf >> 0) & 0xff;
cbuf[clen++] = (swapbuf >> 8) & 0xff;
cbuf[clen++] = (swapbuf >> 16) & 0xff;
cbuf[clen++] = (swapbuf >> 24) & 0xff;
cbuf[clen++] = (txlen >> 0) & 0xff;
cbuf[clen++] = (txlen >> 8) & 0xff;
cbuf[clen++] = (txlen >> 16) & 0xff;
cbuf[clen++] = (txlen >> 24) & 0xff;
}
if(rxlen > 0)
{
cbuf[clen++] = SPI_CMD_RXBUF;
cbuf[clen++] = (swapbuf >> 0) & 0xff;
cbuf[clen++] = (swapbuf >> 8) & 0xff;
cbuf[clen++] = (swapbuf >> 16) & 0xff;
cbuf[clen++] = (swapbuf >> 24) & 0xff;
cbuf[clen++] = (rxlen >> 0) & 0xff;
cbuf[clen++] = (rxlen >> 8) & 0xff;
cbuf[clen++] = (rxlen >> 16) & 0xff;
cbuf[clen++] = (rxlen >> 24) & 0xff;
}
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_END;
if(txlen > 0)
fel_write(ctx, swapbuf, txbuf, txlen);
if((clen > cmdlen) || !fel_chip_spi_run(ctx, cbuf, clen))
return 0;
if(rxlen > 0)
fel_read(ctx, swapbuf, rxbuf, rxlen);
}
else
{
clen = 0;
cbuf[clen++] = SPI_CMD_SELECT;
cbuf[clen++] = SPI_CMD_END;
if(!fel_chip_spi_run(ctx, cbuf, clen))
return 0;
while(txlen > 0)
{
n = txlen > swaplen ? swaplen : txlen;
clen = 0;
cbuf[clen++] = SPI_CMD_TXBUF;
cbuf[clen++] = (swapbuf >> 0) & 0xff;
cbuf[clen++] = (swapbuf >> 8) & 0xff;
cbuf[clen++] = (swapbuf >> 16) & 0xff;
cbuf[clen++] = (swapbuf >> 24) & 0xff;
cbuf[clen++] = (n >> 0) & 0xff;
cbuf[clen++] = (n >> 8) & 0xff;
cbuf[clen++] = (n >> 16) & 0xff;
cbuf[clen++] = (n >> 24) & 0xff;
cbuf[clen++] = SPI_CMD_END;
fel_write(ctx, swapbuf, txbuf, n);
if(!fel_chip_spi_run(ctx, cbuf, clen))
return 0;
txbuf += n;
txlen -= n;
}
while(rxlen > 0)
{
n = rxlen > swaplen ? swaplen : rxlen;
clen = 0;
cbuf[clen++] = SPI_CMD_RXBUF;
cbuf[clen++] = (swapbuf >> 0) & 0xff;
cbuf[clen++] = (swapbuf >> 8) & 0xff;
cbuf[clen++] = (swapbuf >> 16) & 0xff;
cbuf[clen++] = (swapbuf >> 24) & 0xff;
cbuf[clen++] = (n >> 0) & 0xff;
cbuf[clen++] = (n >> 8) & 0xff;
cbuf[clen++] = (n >> 16) & 0xff;
cbuf[clen++] = (n >> 24) & 0xff;
cbuf[clen++] = SPI_CMD_END;
if(!fel_chip_spi_run(ctx, cbuf, clen))
return 0;
fel_read(ctx, swapbuf, rxbuf, n);
rxbuf += n;
rxlen -= n;
}
clen = 0;
cbuf[clen++] = SPI_CMD_DESELECT;
cbuf[clen++] = SPI_CMD_END;
if((clen > cmdlen) || !fel_chip_spi_run(ctx, cbuf, clen))
return 0;
}
return 1;
}