-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathantumbratool.c
572 lines (482 loc) · 14.9 KB
/
antumbratool.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/* Copyright (c) 2015 Antumbra
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <getopt.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "libantumbra.h"
#include "usage.h"
static AnCtx *ctx;
static AnLogLevel log_level;
static int device_num;
static const struct option options[] = {
{"help", no_argument, NULL, 'h'},
{"device", required_argument, NULL, 'd'},
{"verbose", no_argument, NULL, 'v'},
{"quiet", no_argument, NULL, 'q'},
{NULL, 0, NULL, 0}
};
static void validate_flash_info(AnFlashInfo *info)
{
if (info->pagesize > 0x4000) {
An_LOG(ctx, AnLog_ERROR,
"page size is unexpectedly large (>%d bytes)", 0x4000);
exit(1);
}
if (info->numpages > 0x400) {
An_LOG(ctx, AnLog_ERROR, "too many pages (>%d)", 0x400);
exit(1);
}
}
static uint8_t *read_all_flash(AnDevice *dev, AnFlashInfo *info)
{
uint8_t *flash = malloc(info->pagesize * info->numpages);
if (!flash) {
An_LOG(ctx, AnLog_ERROR, "malloc failed");
exit(1);
}
for (uint32_t ipage = 0; ipage < info->numpages; ++ipage) {
if (AnFlash_ReadPage_S(ctx, dev, info, ipage,
flash + ipage * info->pagesize))
exit(1);
}
return flash;
}
static void write_all_flash(AnDevice *dev, AnFlashInfo *info,
const uint8_t *flash, size_t srcsize)
{
uint8_t page[info->pagesize];
for (uint32_t off = 0, ipage = 0;
off < srcsize;
off += info->pagesize, ++ipage) {
size_t realpagesize = (srcsize - off < info->pagesize ?
srcsize - off : info->pagesize);
memcpy(page, flash + off, realpagesize);
memset(page + realpagesize, 0, sizeof page - realpagesize);
if (AnFlash_WritePage_S(ctx, dev, info, ipage, page))
exit(1);
}
}
static uint8_t *read_all_file(FILE *f, size_t *lenout)
{
uint8_t *buf = NULL;
size_t bufsz = 0, buflen = 0;
size_t nread;
uint8_t readbuf[512];
while ((nread = fread(readbuf, 1, sizeof readbuf, f)) != 0) {
if (buflen + nread > bufsz) {
size_t newbufsz = bufsz > 0 ? 2 * bufsz : sizeof readbuf;
if (newbufsz > 0x100000) {
An_LOG(ctx, AnLog_ERROR,
"abort file read: size exceeds %d", 0x100000);
exit(1);
}
uint8_t *newbuf = realloc(buf, newbufsz);
if (!newbuf) {
An_LOG(ctx, AnLog_ERROR, "realloc failed");
exit(1);
}
buf = newbuf;
bufsz = newbufsz;
}
memcpy(buf + buflen, readbuf, nread);
buflen += nread;
}
if (ferror(f)) {
An_LOG(ctx, AnLog_ERROR, "fread error");
exit(1);
}
*lenout = buflen;
return buf;
}
static void write_all_file(const uint8_t *buf, size_t len, FILE *f)
{
if (fwrite(buf, 1, len, f) < len) {
An_LOG(ctx, AnLog_ERROR, "short fwrite");
exit(1);
}
}
static uint8_t *read_eeprom(AnDevice *dev, AnEepromInfo *info)
{
uint8_t *eeprom = malloc(info->size);
if (!eeprom) {
An_LOG(ctx, AnLog_ERROR, "malloc failed");
exit(1);
}
for (uint16_t off = 0; off < info->size; off += 48) {
uint8_t len = (info->size - off < 48 ?
info->size - off : 48);
if (AnEeprom_Read_S(ctx, dev, info, off, len, eeprom + off))
exit(1);
}
return eeprom;
}
static void write_eeprom(AnDevice *dev, AnEepromInfo *info,
const uint8_t *eeprom, uint16_t size)
{
for (uint16_t off = 0; off < size; off += 48) {
uint8_t len = (size - off < 48 ?
size - off : 48);
if (AnEeprom_Write_S(ctx, dev, info, off, len, eeprom + off))
exit(1);
}
}
static FILE *try_open(const char *name, const char *mode)
{
FILE *f = fopen(name, mode);
if (!f) {
fprintf(stderr, "%s: %s\n", name, strerror(errno));
exit(1);
}
return f;
}
static void strip_unprint(char *s)
{
for (int i = 0, len = strlen(s); i < len; ++i) {
if (!(isgraph(s[i]) || s[i] == ' '))
s[i] = '?';
}
}
static void dump_dev_info(AnDevice *dev)
{
char hwid[57] = {0};
if (AnCore_HardwareId_S(ctx, dev, hwid, sizeof hwid))
return;
strip_unprint(hwid);
printf(" hwid \"%s\"\n", hwid);
char implid[57] = {0};
if (AnCore_ImplementationId_S(ctx, dev, implid, sizeof implid))
return;
strip_unprint(implid);
printf(" implid \"%s\"\n", implid);
uint8_t devid[56];
char devidstr[2 * sizeof devid + 1] = {0};
if (AnCore_DeviceId_S(ctx, dev, devid, sizeof devid))
return;
for (int i = 0; i < sizeof devid; ++i)
sprintf(devidstr + 2 * i, "%02x", (unsigned int)devid[i]);
printf(" devid %s\n", devidstr);
}
static void cmd_list(int argc, char **argv, AnDeviceInfo **devs, size_t ndevs)
{
for (int i = 0; i < ndevs; ++i) {
uint8_t bus, addr;
uint16_t vid, pid;
AnDeviceInfo_UsbInfo(devs[i], &bus, &addr, &vid, &pid);
printf("%d: bus 0x%02x addr 0x%02x vid 0x%04x pid 0x%04x\n",
i, (unsigned int)bus, (unsigned int)addr,
(unsigned int)vid, (unsigned int)pid);
AnDevice *dev;
if (!AnDevice_Open(ctx, devs[i], &dev))
dump_dev_info(dev);
AnDevice_Close(ctx, dev);
}
}
static void cmd_flashread(int argc, char **argv, AnDevice *dev)
{
FILE *outf = argc ? try_open(argv[0], "wb") : stdout;
AnFlashInfo info;
if (AnFlash_Info_S(ctx, dev, &info))
exit(1);
validate_flash_info(&info);
uint8_t *flash = read_all_flash(dev, &info);
write_all_file(flash, info.pagesize * info.numpages, outf);
free(flash);
if (outf != stdout)
fclose(outf);
}
static void cmd_flashwrite(int argc, char **argv, AnDevice *dev)
{
FILE *inf = argc ? try_open(argv[0], "rb") : stdin;
AnFlashInfo info;
if (AnFlash_Info_S(ctx, dev, &info))
exit(1);
validate_flash_info(&info);
size_t wsize;
uint8_t *wflash = read_all_file(inf, &wsize);
write_all_flash(dev, &info, wflash, wsize);
uint8_t *rflash = read_all_flash(dev, &info);
for (unsigned int i = 0; i < wsize; ++i) {
if (wflash[i] != rflash[i]) {
An_LOG(ctx, AnLog_ERROR,
"flash verification mismatch at offset 0x%08x", i);
exit(1);
}
}
free(rflash);
free(wflash);
if (inf != stdin)
fclose(inf);
}
static void cmd_eepromread(int argc, char **argv, AnDevice *dev)
{
FILE *outf = argc ? try_open(argv[0], "wb") : stdout;
AnEepromInfo info;
if (AnEeprom_Info_S(ctx, dev, &info))
exit(1);
uint8_t *eeprom = read_eeprom(dev, &info);
write_all_file(eeprom, info.size, outf);
free(eeprom);
if (outf != stdout)
fclose(outf);
}
static void cmd_eepromwrite(int argc, char **argv, AnDevice *dev)
{
FILE *inf = argc ? try_open(argv[0], "rb") : stdin;
AnEepromInfo info;
if (AnEeprom_Info_S(ctx, dev, &info))
exit(1);
size_t size;
uint8_t *eeprom = read_all_file(inf, &size);
if (size > 0xffff) {
An_LOG(ctx, AnLog_ERROR, "file exceeds max EEPROM size %d\n", 0xffff);
exit(1);
}
write_eeprom(dev, &info, eeprom, size);
free(eeprom);
if (inf != stdin)
fclose(inf);
}
static void cmd_bootset(int argc, char **argv, AnDevice *dev)
{
bool ldrp;
if (!strcmp(argv[0], "main"))
ldrp = false;
else if (!strcmp(argv[0], "loader"))
ldrp = true;
else {
fputs(usage_msg, stderr);
exit(1);
}
if (AnBoot_SetForceLoader_S(ctx, dev, ldrp))
exit(1);
}
static void cmd_reset(int argc, char **argv, AnDevice *dev)
{
if (AnCore_Reset_S(ctx, dev))
exit(1);
}
static void cmd_lightset(int argc, char **argv, AnDevice *dev)
{
AnLightInfo info;
if (AnLight_Info_S(ctx, dev, &info))
exit(1);
if (AnLight_Set_S(ctx, dev, &info,
strtol(argv[0], NULL, 0),
strtol(argv[1], NULL, 0),
strtol(argv[2], NULL, 0)))
exit(1);
}
static void cmd_raw(int argc, char **argv, AnDevice *dev)
{
uint8_t outdata[64];
uint8_t indata[64];
memset(outdata, 0, sizeof outdata);
int outdataoff = 0;
for (int i = 0; i < argc; ++i) {
size_t len = strlen(argv[i]);
for (int j = 0; j < (len & ~1); j += 2) {
char hexbyte[3] = {argv[i][j], argv[i][j+1], 0};
char *endp;
uint8_t val = strtol(hexbyte, &endp, 16);
if (*endp) {
An_LOG(ctx, AnLog_ERROR, "invalid hex data");
exit(1);
}
if (outdataoff >= sizeof outdata) {
An_LOG(ctx, AnLog_ERROR, "input hex data too long");
exit(1);
}
outdata[outdataoff++] = val;
}
}
AnCmd_SendRaw_S(ctx, dev, outdata, sizeof outdata);
AnCmd_RecvRaw_S(ctx, dev, indata, sizeof indata);
char indatastr[129];
for (int i = 0; i < sizeof indata; ++i)
sprintf(indatastr + 2 * i, "%02x", indata[i]);
printf("%s\n", indatastr);
}
static void cmd_tempreadraw(int argc, char **argv, AnDevice *dev)
{
uint32_t raw;
if (AnTemp_ReadRaw_S(ctx, dev, &raw))
exit(1);
printf("%u\n", raw);
}
static void cmd_tempreadtemp(int argc, char **argv, AnDevice *dev)
{
uint32_t temp;
if (AnTemp_ReadTemp_S(ctx, dev, &temp))
exit(1);
printf("%u mK\n", temp);
}
static void cmd_tempreadcal(int argc, char **argv, AnDevice *dev)
{
AnTempCal cal;
if (AnTemp_ReadCal_S(ctx, dev, &cal))
exit(1);
printf("A: %u = %u mK\nB: %u = %u mK\n", cal.a_sensor, cal.a_temp, cal.b_sensor, cal.b_temp);
}
static uint32_t parseu32(const char *s)
{
char *endp;
uint32_t n = strtol(s, &endp, 0);
if (endp == s || *endp) {
fprintf(stderr, "invalid integer: %s\n", s);
exit(1);
}
return n;
}
static void cmd_tempwritecal(int argc, char **argv, AnDevice *dev)
{
AnTempCal cal = {
.a_sensor = parseu32(argv[0]),
.a_temp = parseu32(argv[1]),
.b_sensor = parseu32(argv[2]),
.b_temp = parseu32(argv[3]),
};
if (AnTemp_WriteCal_S(ctx, dev, &cal))
exit(1);
}
static void cmd_diagnostic(int argc, char **argv, AnDevice *dev)
{
uint8_t diag[56];
char diagstr[2 * sizeof diag + 1] = {0};
if (AnCore_Diagnostic_S(ctx, dev, diag, sizeof diag))
return;
for (int i = 0; i < sizeof diag; ++i)
sprintf(diagstr + 2 * i, "%02x", (unsigned int)diag[i]);
printf("%s\n", diagstr);
}
#define CMD_NODEV 0
#define CMD_LISTONLY 1
#define CMD_USEDEV 2
typedef void (*call_nodev)(int argc, char **argv);
typedef void (*call_listonly)(int argc, char **argv, AnDeviceInfo **devs,
size_t ndevs);
typedef void (*call_usedev)(int argc, char **argv, AnDevice *dev);
struct cmd {
const char *name;
int type;
int argc; /* -1 to ignore argc */
void *call;
};
static const struct cmd commands[] = {
{"list", CMD_LISTONLY, 0, &cmd_list},
{"flash-read", CMD_USEDEV, -1, &cmd_flashread},
{"flash-write", CMD_USEDEV, -1, &cmd_flashwrite},
{"eeprom-read", CMD_USEDEV, -1, &cmd_eepromread},
{"eeprom-write", CMD_USEDEV, -1, &cmd_eepromwrite},
{"boot-set", CMD_USEDEV, 1, &cmd_bootset},
{"reset", CMD_USEDEV, 0, &cmd_reset},
{"light-set", CMD_USEDEV, 3, &cmd_lightset},
{"raw", CMD_USEDEV, -1, &cmd_raw},
{"temp-readraw", CMD_USEDEV, 0, &cmd_tempreadraw},
{"temp-readtemp", CMD_USEDEV, 0, &cmd_tempreadtemp},
{"temp-readcal", CMD_USEDEV, 0, &cmd_tempreadcal},
{"temp-writecal", CMD_USEDEV, 4, &cmd_tempwritecal},
{"diagnostic", CMD_USEDEV, 0, &cmd_diagnostic},
};
static const struct cmd *match_cmd(const char *name)
{
for (int i = 0; i < sizeof commands / sizeof *commands; ++i) {
if (!strcmp(commands[i].name, name))
return &commands[i];
}
return NULL;
}
static void dispatch_cmd(char *name, int argc, char **argv)
{
const struct cmd *cmd = match_cmd(name);
if (!cmd) {
fprintf(stderr, "unknown command: %s\n", name);
exit(1);
}
if (cmd->argc != -1 && argc != cmd->argc) {
fputs(usage_msg, stderr);
exit(1);
}
size_t ndevs;
AnDeviceInfo **devs;
if (cmd->type != CMD_NODEV) {
if (AnCtx_Init(&ctx)) {
fputs("AnCtx init failed\n", stderr);
exit(1);
}
AnLog_SetLogging(ctx, log_level, stderr);
if (AnDevice_GetList(ctx, &devs, &ndevs))
exit(1);
}
if (cmd->type == CMD_NODEV)
(*(call_nodev)cmd->call)(argc, argv);
else if (cmd->type == CMD_LISTONLY)
(*(call_listonly)cmd->call)(argc, argv, devs, ndevs);
else if (cmd->type == CMD_USEDEV) {
AnDevice *dev;
if (device_num >= ndevs) {
fprintf(stderr, "device %d does not exist\n", device_num);
exit(1);
}
if (AnDevice_Open(ctx, devs[device_num], &dev))
exit(1);
(*(call_usedev)cmd->call)(argc, argv, dev);
AnDevice_Close(ctx, dev);
}
AnDevice_FreeList(devs);
AnCtx_Deinit(ctx);
}
int main(int argc, char **argv)
{
device_num = 0;
log_level = AnLog_WARN;
for (int c; (c = getopt_long(argc, argv, "hd:vq", options, NULL)) != -1;) {
switch(c) {
case 'h':
fputs(usage_msg, stdout);
exit(0);
break;
case 'd':
{
char *endptr;
device_num = strtol(optarg, &endptr, 10);
if (endptr == optarg || *endptr || device_num < 0) {
fprintf(stderr, "invalid device number: %s\n", optarg);
exit(1);
}
}
break;
case 'v':
++log_level;
break;
case 'q':
log_level = AnLog_NONE;
break;
default:
fputs(usage_msg, stderr);
exit(1);
break;
}
}
if (optind == argc) {
fputs(usage_msg, stderr);
exit(1);
}
dispatch_cmd(argv[optind], argc - optind - 1, argv + optind + 1);
return 0;
}