-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinga_tool.c
547 lines (443 loc) · 16.8 KB
/
inga_tool.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
/* Reset INGA nodes and configure their FTDI
*
* (C) 2011 Daniel Willmann <[email protected]>
* (C) 2014 Sven Hesse <[email protected]>
* 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.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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.
*
*/
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <popt.h>
#include "inga_usb.h"
enum {
MODE_RESET,
MODE_UPDATE_EEPROM,
MODE_READ_SERIAL,
MODE_EUI64,
};
struct ftdi_eemem_info {
const char *manufacturer;
const char *product;
const char *serial;
int chip_type;
int vendor_id;
int product_id;
int max_power;
int cbus[5];
};
struct config_t {
struct inga_usb_config_t usb;
int mode;
int verbose;
int eep_cbusio;
struct ftdi_eemem_info eep;
};
#define VERBOSE(args ...) \
if (cfg->verbose) \
fprintf(stderr, ## args)
void inga_reset(struct config_t *cfg)
{
int rc;
struct inga_usb_ftdi_t *ftdi;
struct inga_usb_device_t *usbdev = NULL;
if (inga_usb_ftdi_init(&ftdi) < 0) {
fprintf(stderr, "ftdi_init failed\n");
exit(EXIT_FAILURE);
}
/* Find the USB device for the path */
usbdev = inga_usb_find_device(&cfg->usb, cfg->verbose);
if (!usbdev) {
fprintf(stderr, "Could not find device\n");
exit(EXIT_FAILURE);
}
rc = inga_usb_ftdi_open(ftdi, usbdev);
if (rc < 0) {
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", rc, inga_usb_ftdi_get_error_string(ftdi));
exit(EXIT_FAILURE);
}
printf("Resetting INGA node...");
fflush(stdout);
/* Set CBUS3 to output and high */
inga_usb_ftdi_set_bitmode(ftdi, 0x88, BITMODE_CBUS);
/* sleep(1); */
/* Set CBUS3 to output and low */
inga_usb_ftdi_set_bitmode(ftdi, 0x80, BITMODE_CBUS);
VERBOSE("done\n");
inga_usb_ftdi_set_bitmode(ftdi, 0, BITMODE_RESET);
out:
VERBOSE("Resetting USB device\n");
inga_usb_ftdi_reset(ftdi);
inga_usb_ftdi_close(ftdi);
inga_usb_ftdi_deinit(ftdi);
inga_usb_free_device(usbdev);
}
void inga_read_eeprom(struct config_t *cfg, struct inga_usb_ftdi_t *ftdi, struct ftdi_eemem_info *info)
{
int rc;
struct inga_usb_device_t *usbdev = NULL;
if (inga_usb_ftdi_init(&ftdi) < 0) {
fprintf(stderr, "ftdi_init failed\n");
exit(EXIT_FAILURE);
}
/* Find the USB device for the path */
usbdev = inga_usb_find_device(&cfg->usb, cfg->verbose);
if (!usbdev) {
fprintf(stderr, "Could not find device\n");
exit(EXIT_FAILURE);
}
rc = inga_usb_ftdi_open(ftdi, usbdev);
if (rc < 0) {
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", rc, inga_usb_ftdi_get_error_string(ftdi));
exit(EXIT_FAILURE);
}
VERBOSE("Reading out EEPROM image...");
fflush(stdout);
rc = inga_usb_ftdi_eeprom_read(ftdi);
if (rc < 0) {
fprintf(stderr, "\nCould not read and decode EEPROM: %i\n", rc);
exit(EXIT_FAILURE);
}
VERBOSE("done\n\n");
if ((inga_usb_ftdi_eeprom_get_value(ftdi, CHIP_TYPE, &info->chip_type) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, VENDOR_ID, &info->vendor_id) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, PRODUCT_ID, &info->product_id) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, MAX_POWER, &info->max_power) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, CBUS_FUNCTION_0, &info->cbus[0]) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, CBUS_FUNCTION_1, &info->cbus[1]) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, CBUS_FUNCTION_2, &info->cbus[2]) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, CBUS_FUNCTION_3, &info->cbus[3]) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, CBUS_FUNCTION_4, &info->cbus[4]) < 0)) {
fprintf(stderr, "Could not read the EEPROM values\n");
exit(EXIT_FAILURE);
}
if ((inga_usb_ftdi_eeprom_get_string(ftdi, MANUFACTURER_STRING, &info->manufacturer) < 0) ||
(inga_usb_ftdi_eeprom_get_string(ftdi, PRODUCT_STRING, &info->product) < 0) ||
(inga_usb_ftdi_eeprom_get_string(ftdi, SERIAL_STRING, &info->serial) < 0)) {
fprintf(stderr, "Could not read the EEPROM manufacturer, product and serial strings\n");
exit(EXIT_FAILURE);
}
VERBOSE("Resetting USB device\n");
inga_usb_ftdi_reset(ftdi);
inga_usb_ftdi_close(ftdi);
inga_usb_ftdi_deinit(ftdi);
inga_usb_free_device(usbdev);
}
void inga_print_eeinfo(struct config_t *cfg)
{
struct inga_usb_ftdi_t *ftdi;
struct ftdi_eemem_info info;
inga_read_eeprom(cfg, ftdi, &info);
printf("EEPROM config:\n"
"Chip type: %i\n"
"VID: 0x%04x\n"
"PID: 0x%04x\n"
"Manufacturer: %s\n"
"Product: %s\n"
"Serial: %s\n"
"Max power: %i\n"
"CBUS: %1x%1x %1x%1x 0%1x\n\n",
info.chip_type, info.vendor_id, info.product_id, info.manufacturer, info.product, info.serial, info.max_power * 2,
info.cbus[1], info.cbus[0], info.cbus[3], info.cbus[2], info.cbus[4]);
}
uint64_t inga_serial_to_id(const char *serial)
{
int i = 0;
int character = 0;
uint64_t return_value = 0;
for (i = 0; i < 8; i++) {
character = serial[i] - 48; // "0" == 48, Z == 42
if (character > 42) {
character = 42;
}
return_value = (return_value << 6) | (character & 0x3F);
}
/* Insert FFFE as required by http://msdn.microsoft.com/en-us/library/aa915616.aspx */
return_value = ((return_value & 0xFFFFFF000000) << 16) | (((uint64_t) 0xFEFF) << 24) | (return_value & 0x00000000FFFFF);
/* Insert 00 */
return_value &= 0xFFFFFFFFFFFFFF7F;
return return_value;
}
void inga_eui64(struct config_t *cfg)
{
struct inga_usb_ftdi_t *ftdi;
struct ftdi_eemem_info info;
const char *serial = NULL;
uint8_t *pointer = NULL;
inga_read_eeprom(cfg, ftdi, &info);
/* Obtain EUI64 */
uint64_t id = inga_serial_to_id(info.serial);
pointer = (uint8_t *) &id;
printf("EUI64: ");
int i;
for (i = 0; i < 8; i++) {
printf("%02X", pointer[i]);
if (i < 7) {
printf(":");
}
}
printf("\n");
}
void inga_eeprom(struct config_t *cfg)
{
int rc;
struct inga_usb_ftdi_t *ftdi;
struct inga_usb_device_t *usbdev = NULL;
const char *manufacturer = NULL, *product = NULL, *serial = NULL;
int chip_type, vid, pid, max_power, cbus[5];
if (inga_usb_ftdi_init(&ftdi) < 0) {
fprintf(stderr, "ftdi_init failed\n");
exit(EXIT_FAILURE);
}
/* Find the USB device for the path */
usbdev = inga_usb_find_device(&cfg->usb, cfg->verbose);
if (!usbdev) {
fprintf(stderr, "Could not find device\n");
exit(EXIT_FAILURE);
}
rc = inga_usb_ftdi_open(ftdi, usbdev);
if (rc < 0) {
fprintf(stderr, "unable to open ftdi device: %d (%s)\n", rc, inga_usb_ftdi_get_error_string(ftdi));
exit(EXIT_FAILURE);
}
printf("Reading out EEPROM image...");
fflush(stdout);
rc = inga_usb_ftdi_eeprom_read(ftdi);
if (rc < 0) {
fprintf(stderr, "\nCould not read and decode EEPROM: %i\n", rc);
exit(EXIT_FAILURE);
}
VERBOSE("done\n\n");
if ((inga_usb_ftdi_eeprom_get_value(ftdi, CHIP_TYPE, &chip_type) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, VENDOR_ID, &vid) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, PRODUCT_ID, &pid) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, MAX_POWER, &max_power) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, CBUS_FUNCTION_0, &cbus[0]) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, CBUS_FUNCTION_1, &cbus[1]) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, CBUS_FUNCTION_2, &cbus[2]) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, CBUS_FUNCTION_3, &cbus[3]) < 0) ||
(inga_usb_ftdi_eeprom_get_value(ftdi, CBUS_FUNCTION_4, &cbus[4]) < 0)) {
fprintf(stderr, "Could not read the EEPROM values\n");
exit(EXIT_FAILURE);
}
if ((inga_usb_ftdi_eeprom_get_string(ftdi, MANUFACTURER_STRING, &manufacturer) < 0) ||
(inga_usb_ftdi_eeprom_get_string(ftdi, PRODUCT_STRING, &product) < 0) ||
(inga_usb_ftdi_eeprom_get_string(ftdi, SERIAL_STRING, &serial) < 0)) {
fprintf(stderr, "Could not read the EEPROM manufacturer, product and serial strings\n");
exit(EXIT_FAILURE);
}
VERBOSE("\nEEPROM config:\n"
"Chip type: %i\n"
"VID: 0x%04x\n"
"PID: 0x%04x\n"
"Manufacturer: %s\n"
"Product: %s\n"
"Serial: %s\n"
"Max power: %i\n"
"CBUS: %1x%1x %1x%1x 0%1x\n\n",
chip_type, vid, pid, manufacturer, product, serial, max_power * 2,
cbus[1], cbus[0], cbus[3], cbus[2], cbus[4]);
vid = cfg->eep.vendor_id ? cfg->eep.vendor_id : vid;
pid = cfg->eep.product_id ? cfg->eep.product_id : pid;
manufacturer = cfg->eep.manufacturer ? cfg->eep.manufacturer : manufacturer;
product = cfg->eep.product ? cfg->eep.product : product;
serial = cfg->eep.serial ? cfg->eep.serial : serial;
max_power = (cfg->eep.max_power > 0) ? (cfg->eep.max_power / 2) : max_power;
cbus[0] = cfg->eep.cbus[0];
cbus[1] = cfg->eep.cbus[1];
cbus[2] = cfg->eep.cbus[2];
cbus[3] = cfg->eep.cbus[3];
cbus[4] = cfg->eep.cbus[4];
VERBOSE("\nUpdating with EEPROM config:\n"
"Chip type: %i\n"
"VID: 0x%04x\n"
"PID: 0x%04x\n"
"Manufacturer: %s\n"
"Product: %s\n"
"Serial: %s\n"
"Max power: %i\n"
"CBUS: %1x%1x %1x%1x 0%1x\n\n",
chip_type, vid, pid, manufacturer, product, serial, max_power * 2,
cbus[1], cbus[0], cbus[3], cbus[2], cbus[4]);
if ((inga_usb_ftdi_eeprom_set_value(ftdi, CHIP_TYPE, chip_type) < 0) ||
(inga_usb_ftdi_eeprom_set_value(ftdi, VENDOR_ID, vid) < 0) ||
(inga_usb_ftdi_eeprom_set_value(ftdi, PRODUCT_ID, pid) < 0) ||
(inga_usb_ftdi_eeprom_set_value(ftdi, MAX_POWER, max_power) < 0) ||
(inga_usb_ftdi_eeprom_set_value(ftdi, CBUS_FUNCTION_0, cbus[0]) < 0) ||
(inga_usb_ftdi_eeprom_set_value(ftdi, CBUS_FUNCTION_1, cbus[1]) < 0) ||
(inga_usb_ftdi_eeprom_set_value(ftdi, CBUS_FUNCTION_2, cbus[2]) < 0) ||
(inga_usb_ftdi_eeprom_set_value(ftdi, CBUS_FUNCTION_3, cbus[3]) < 0) ||
(inga_usb_ftdi_eeprom_set_value(ftdi, CBUS_FUNCTION_4, cbus[4]) < 0)) {
fprintf(stderr, "Could not write the EEPROM values\n");
exit(EXIT_FAILURE);
}
if ((inga_usb_ftdi_eeprom_set_string(ftdi, MANUFACTURER_STRING, manufacturer) < 0) ||
(inga_usb_ftdi_eeprom_set_string(ftdi, PRODUCT_STRING, product) < 0) ||
(inga_usb_ftdi_eeprom_set_string(ftdi, SERIAL_STRING, serial) < 0)) {
fprintf(stderr, "Could not write the EEPROM manufacturer, product and serial strings\n");
exit(EXIT_FAILURE);
}
printf("Writing updated EEPROM image...");
fflush(stdout);
rc = inga_usb_ftdi_eeprom_write(ftdi);
if (rc < 0) {
fprintf(stderr, "Could not build and write EEPROM: %i\n", rc);
exit(EXIT_FAILURE);
}
sleep(10);
VERBOSE("done\n");
out:
VERBOSE("Resetting USB device\n");
inga_usb_ftdi_reset(ftdi);
inga_usb_ftdi_close(ftdi);
inga_usb_ftdi_deinit(ftdi);
inga_usb_free_device(usbdev);
}
void usage(poptContext poptc, int exitcode, char *error, char *addl)
{
poptPrintUsage(poptc, stderr, 0);
if (error) {
fprintf(stderr, "%s: %s\n", error, addl);
}
exit(exitcode);
}
void parse_options(int argc, const char **argv, struct config_t *cfg)
{
int rc;
char *tmp;
poptContext poptc;
struct poptOption options[] = {
{ "reset", 'r', POPT_ARG_VAL, &cfg->mode, MODE_RESET, "Reset INGA node (default)",
NULL },
{ "flash", 'f', POPT_ARG_VAL, &cfg->mode, MODE_UPDATE_EEPROM, "Update FTDI EEPROM of INGA",
NULL },
{ "serial", 's', POPT_ARG_VAL, &cfg->mode, MODE_READ_SERIAL, "Read the FTDI serial from its EEPROM",
NULL },
{ "device", 'd', POPT_ARG_STRING, &cfg->usb.device_path, 0, "Path to the serial device",
"pathname" },
{ "usbserial", 'u', POPT_ARG_STRING, &cfg->usb.device_serial, 0, "Serial of the FTDI to use",
"usbserial" },
{ "id", 'i', POPT_ARG_STRING, &cfg->usb.device_id, 0, "Limit choice to USB device id",
"device_id" },
{ "verbose", 'v', POPT_ARG_NONE, &cfg->verbose, 0, "Be verbose",
NULL },
{ "max-power", 'p', POPT_ARG_INT, &cfg->eep.max_power, 0, "Maximum current to draw from USB (mA)",
"current" },
{ "eui64", 'e', POPT_ARG_VAL, &cfg->mode, MODE_EUI64, "Print out INGAs EUI64",
NULL },
POPT_AUTOHELP
{ NULL, 0, 0, NULL, 0 }
};
cfg->mode = MODE_RESET;
poptc = poptGetContext(NULL, argc, argv, options, 0);
// printf("%p\n", poptc);
int i = 0;
while ((rc = poptGetNextOpt(poptc)) >= 0) {
i++;
printf("OPT: %d\n", rc);
if(i > 1000) {
exit(-1);
}
}
if (poptPeekArg(poptc) != NULL) {
poptPrintUsage(poptc, stderr, 0);
exit(EXIT_FAILURE);
}
if (rc < -1) {
/* Option parsing error */
fprintf(stderr, "%s: %s\n", poptBadOption(poptc, POPT_BADOPTION_NOALIAS), poptStrerror(rc));
exit(EXIT_FAILURE);
}
if (!cfg->usb.device_path && !cfg->usb.device_serial) {
usage(poptc, 1, "At least one of --device or --serial has to be set", "");
}
poptFreeContext(poptc);
if (!cfg->usb.device_path) {
return;
}
/* Resolve symlinks/relative paths */
tmp = realpath(cfg->usb.device_path, NULL);
if (!tmp) {
perror("Could not open device");
exit(EXIT_FAILURE);
}
VERBOSE("Resolving path %s to %s\n", cfg->usb.device_path, tmp);
free(cfg->usb.device_path);
cfg->usb.device_path = tmp;
}
struct config_t *init_config(void)
{
struct config_t *cfg = malloc(sizeof(struct config_t));
if (!cfg) {
return NULL;
}
memset(cfg, 0, sizeof(struct config_t));
/* Set some sensible defaults
* Enable CBUS3 IO, keep vendor/product ID as is,
* set manufacturer to IBR and product string to INGA,
* keep serial, don't change the max power setting */
cfg->eep.cbus[0] = CBUS_TXLED;
cfg->eep.cbus[1] = CBUS_RXLED;
cfg->eep.cbus[2] = CBUS_TXDEN;
cfg->eep.cbus[3] = CBUS_IOMODE;
cfg->eep.cbus[4] = CBUS_SLEEP;
cfg->eep.manufacturer = "IBR";
cfg->eep.product = "INGA";
cfg->eep.max_power = -1;
return cfg;
}
int main(int argc, const char **argv)
{
struct config_t *cfg;
cfg = init_config();
if (!cfg) {
fprintf(stderr, "Could not initialize config\n");
exit(EXIT_FAILURE);
}
printf("Inga_tool started\n");
parse_options(argc, argv, cfg);
VERBOSE("Config: path: %s, serial: %s, id: %s\n", cfg->usb.device_path, cfg->usb.device_serial, cfg->usb.device_id);
if (cfg->mode == MODE_RESET) {
inga_reset(cfg);
}
else if (cfg->mode == MODE_UPDATE_EEPROM) {
inga_eeprom(cfg);
}
else if (cfg->mode == MODE_READ_SERIAL) {
inga_print_eeinfo(cfg);
}
else if (cfg->mode == MODE_EUI64) {
inga_eui64(cfg);
}
exit(EXIT_SUCCESS);
}