summaryrefslogtreecommitdiff
path: root/src/eeprom_main.c
blob: 22b2bb5d1ac01fcfba426c66126969c474c23a5f (plain)
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/*
 * Create an image file for the MTCDP ID EEPROM
 *
 * Copyright (C) 2013 by Multi-Tech Systems
 *
 * Author: James Maki <jmaki@multitech.com>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * 2012-07-30 JSR - Added wifi and bluetooth capa flags
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <linux/mts_eeprom.h>

#include "log.h"
#include "eeprom.h"

static int eeprom_size = 512;

static void mts_id_eeprom_inspect(struct mts_id_eeprom_layout *id_eeprom);
static void mts_ap_eeprom_inspect(struct mts_ap_eeprom_layout *ap_eeprom);

//Returns 0 on success, 1 on failure
static unsigned int charToInt(char ch, unsigned int* i) {
	if (ch >= '0' && ch <= '9') {
		*i = ch - '0';
	} else if (ch >= 'a' && ch <= 'f') {
		*i = ch - 'a' + 10;
	} else if (ch >= 'A' && ch <= 'F') {
		*i = ch - 'A' + 10;
	} else {
		return 1;
	}
	return 0;
}

//Retuns number of bytes converted, -1 on error
static int asciiHexToBin(const char* asciiHex, char* bin) {
	int count = 0;
	unsigned int upper, lower;
	while(asciiHex[0] && asciiHex[1])
	{
		if(charToInt(asciiHex[0], &upper) == 0 && charToInt(asciiHex[1], &lower) == 0) {
			*bin = (upper << 4) + lower;
			bin++;
			asciiHex += 2;
			count++;
		} else {
			return -1;
		}
	}
	return count;
}

static int hwaddr_aton(const char *str, uint8_t *buf, size_t len)
{
	size_t count = 0;

	for (;;) {
		unsigned acc;
		char ch;

		acc = 0;

		while ((ch = *str) != ':' && ch != 0) {
			if (ch >= '0' && ch <= '9') {
				ch -= '0';
			} else if (ch >= 'a' && ch <= 'f') {
				ch -= 'a' - 10;
			} else if (ch >= 'A' && ch <= 'F') {
				ch -= 'A' - 10;
			} else {
				return 0;
			}
			acc = (acc << 4) + ch;
			str++;
		}

		if (acc > 255) {
			return 0;
		}

		if (count >= len) {
			return 0;
		}

		buf[count] = acc;
		count++;

		if (ch == 0) {
			break;
		}
		str++;
	}

	if (count < len) {
		return 0;
	}

	return 1;
}

static int id_yaml_out(const char *name, struct mts_id_eeprom_layout *id_eeprom)
{
	int i;
	char buf[64] = {0};
	char* ptr;
	FILE *file;

	if (!strcmp(name, "-")) {
		file = stdout;
	} else {
		file = fopen(name, "w+");
		if (!file) {
			log_error("could not open %s: %m", name);
			return -1;
		}
	}

	fprintf(file, "---\n");
	fprintf(file, "\n");

	fprintf(file, "vendor-id: \"%.32s\"\n", id_eeprom->vendor_id);
	fprintf(file, "product-id: \"%.32s\"\n",  id_eeprom->product_id);
	fprintf(file, "device-id: \"%.32s\"\n",  id_eeprom->device_id);
	fprintf(file, "hw-version: \"%.32s\"\n",  id_eeprom->hw_version);
	fprintf(file, "mac-addr: \"%02X:%02X:%02X:%02X:%02X:%02X\"\n",
            id_eeprom->mac_addr[0],
            id_eeprom->mac_addr[1],
            id_eeprom->mac_addr[2],
            id_eeprom->mac_addr[3],
            id_eeprom->mac_addr[4],
            id_eeprom->mac_addr[5]);
	fprintf(file, "imei: \"%.32s\"\n",  id_eeprom->imei);
	fprintf(file, "capa-gps: %s\n", DEVICE_CAPA(id_eeprom->capa, CAPA_GPS) ? "true" : "false");
	fprintf(file, "capa-din: %s\n", DEVICE_CAPA(id_eeprom->capa, CAPA_DIN) ? "true" : "false");
	fprintf(file, "capa-dout: %s\n", DEVICE_CAPA(id_eeprom->capa, CAPA_DOUT) ? "true" : "false");
	fprintf(file, "capa-adc: %s\n", DEVICE_CAPA(id_eeprom->capa, CAPA_ADC) ? "true" : "false");
	fprintf(file, "capa-wifi: %s\n", DEVICE_CAPA(id_eeprom->capa, CAPA_WIFI) ? "true" : "false");
	fprintf(file, "capa-bluetooth: %s\n", DEVICE_CAPA(id_eeprom->capa, CAPA_BLUETOOTH) ? "true" : "false");

	fprintf(file, "capa: \"");
	
	for (i = 0; i < sizeof(id_eeprom->capa); i++) {
		fprintf(file, "\\x%02X", id_eeprom->capa[i]);
	}
	fprintf(file, "\"\n");

    fprintf(file, "mac-bluetooth: \"%02X:%02X:%02X:%02X:%02X:%02X\"\n",
            id_eeprom->mac_bluetooth[0],
            id_eeprom->mac_bluetooth[1],
            id_eeprom->mac_bluetooth[2],
            id_eeprom->mac_bluetooth[3],
            id_eeprom->mac_bluetooth[4],
            id_eeprom->mac_bluetooth[5]);
    fprintf(file, "mac-wifi: \"%02X:%02X:%02X:%02X:%02X:%02X\"\n",
            id_eeprom->mac_wifi[0],
            id_eeprom->mac_wifi[1],
            id_eeprom->mac_wifi[2],
            id_eeprom->mac_wifi[3],
            id_eeprom->mac_wifi[4],
            id_eeprom->mac_wifi[5]);
    	ptr = (char*)buf;
	for(i = 0; i < 16; i++) {
		ptr += sprintf(ptr, "%02X", id_eeprom->uuid[i]);
	}
	fprintf(file, "uuid: \"%s\"\n",  (char*)buf);
	fprintf(file, "...\n");

	fclose(file);

	return 0;
}

static int ap_yaml_out(const char *name, struct mts_ap_eeprom_layout *ap_eeprom)
{
	FILE *file;

	if (!strcmp(name, "-")) {
		file = stdout;
	} else {
		file = fopen(name, "w+");
		if (!file) {
			log_error("could not open %s: %m", name);
			return -1;
		}
	}

	fprintf(file, "---\n");
	fprintf(file, "\n");

	fprintf(file, "vendor-id: \"%.32s\"\n", ap_eeprom->vendor_id);
	fprintf(file, "product-id: \"%.32s\"\n",  ap_eeprom->product_id);
	fprintf(file, "device-id: \"%.32s\"\n",  ap_eeprom->device_id);
	fprintf(file, "hw-version: \"%.32s\"\n",  ap_eeprom->hw_version);
	fprintf(file, "mac-addr: \"%02X:%02X:%02X:%02X:%02X:%02X\"\n",
            ap_eeprom->mac_addr[0],
            ap_eeprom->mac_addr[1],
            ap_eeprom->mac_addr[2],
            ap_eeprom->mac_addr[3],
            ap_eeprom->mac_addr[4],
            ap_eeprom->mac_addr[5]);
	fprintf(file, "eui: \"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\"\n",
            ap_eeprom->eui[0],
            ap_eeprom->eui[1],
            ap_eeprom->eui[2],
            ap_eeprom->eui[3],
            ap_eeprom->eui[4],
            ap_eeprom->eui[5],
            ap_eeprom->eui[6],
            ap_eeprom->eui[7]);
	fprintf(file, "...\n");

	fclose(file);

	return 0;
}

static int bin_out(const char *name, char *eeprom)
{
	int fd;
	int tmp;

	if (!strcmp(name, "-")) {
		fd = fileno(stdout);
	} else {
		fd = open(name, O_CREAT | O_WRONLY, 0644);
		if (fd < 0) {
			log_error("could not open %s: %m", name);
			return fd;
		}
	}

	tmp = write(fd, eeprom, eeprom_size);
	if (tmp < 0) {
		log_error("writing %s failed: %m", name);
		return fd;
	} else if (tmp != eeprom_size) {
		log_error("only wrote %d bytes to %s", tmp, name);
		return -1;
	}

	close(fd);

	return tmp;
}

static void mts_id_eeprom_inspect(struct mts_id_eeprom_layout *id_eeprom)
{
	int i;
	char buf[64] = {0};
	char* ptr;

	log_info("sizeof: %u", sizeof(struct mts_id_eeprom_layout));
	log_info("vendor-id: %.32s", id_eeprom->vendor_id);
	log_info("product-id: %.32s",  id_eeprom->product_id);
	log_info("device-id: %.32s",  id_eeprom->device_id);
	log_info("hw-version: %.32s",  id_eeprom->hw_version);
	log_info("mac-addr: %02X:%02X:%02X:%02X:%02X:%02X",
            id_eeprom->mac_addr[0],
            id_eeprom->mac_addr[1],
            id_eeprom->mac_addr[2],
            id_eeprom->mac_addr[3],
            id_eeprom->mac_addr[4],
            id_eeprom->mac_addr[5]);
	log_info("imei: %.32s",  id_eeprom->imei);
	log_info("capa-gps: %s", DEVICE_CAPA(id_eeprom->capa, CAPA_GPS) ? "yes" : "no");
	log_info("capa-din: %s", DEVICE_CAPA(id_eeprom->capa, CAPA_DIN) ? "yes" : "no");
	log_info("capa-dout: %s", DEVICE_CAPA(id_eeprom->capa, CAPA_DOUT) ? "yes" : "no");
	log_info("capa-adc: %s", DEVICE_CAPA(id_eeprom->capa, CAPA_ADC) ? "yes" : "no");
	log_info("capa-wifi: %s", DEVICE_CAPA(id_eeprom->capa, CAPA_WIFI) ? "yes" : "no");
	log_info("capa-bluetooth: %s", DEVICE_CAPA(id_eeprom->capa, CAPA_BLUETOOTH) ? "yes" : "no");

	log_info("mac-bluetooth: %02X:%02X:%02X:%02X:%02X:%02X",
            id_eeprom->mac_bluetooth[0],
            id_eeprom->mac_bluetooth[1],
            id_eeprom->mac_bluetooth[2],
            id_eeprom->mac_bluetooth[3],
            id_eeprom->mac_bluetooth[4],
            id_eeprom->mac_bluetooth[5]);
	log_info("mac-wifi: %02X:%02X:%02X:%02X:%02X:%02X",
            id_eeprom->mac_wifi[0],
            id_eeprom->mac_wifi[1],
            id_eeprom->mac_wifi[2],
            id_eeprom->mac_wifi[3],
            id_eeprom->mac_wifi[4],
            id_eeprom->mac_wifi[5]);

	ptr = (char*)buf;
	for(i = 0; i < 16; i++) {
		ptr += sprintf(ptr, "%02X", id_eeprom->uuid[i]);
	}
	log_info("uuid: %s", (char*)buf);
}

static void mts_ap_eeprom_inspect(struct mts_ap_eeprom_layout *ap_eeprom)
{
	log_info("sizeof: %u", sizeof(struct mts_ap_eeprom_layout));
	log_info("vendor-id: %.32s", ap_eeprom->vendor_id);
	log_info("product-id: %.32s",  ap_eeprom->product_id);
	log_info("device-id: %.32s",  ap_eeprom->device_id);
	log_info("hw-version: %.32s",  ap_eeprom->hw_version);
	log_info("mac-addr: %02X:%02X:%02X:%02X:%02X:%02X",
            ap_eeprom->mac_addr[0],
            ap_eeprom->mac_addr[1],
            ap_eeprom->mac_addr[2],
            ap_eeprom->mac_addr[3],
            ap_eeprom->mac_addr[4],
            ap_eeprom->mac_addr[5]);
	log_info("eui: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
            ap_eeprom->eui[0],
            ap_eeprom->eui[1],
            ap_eeprom->eui[2],
            ap_eeprom->eui[3],
            ap_eeprom->eui[4],
            ap_eeprom->eui[5],
            ap_eeprom->eui[6],
            ap_eeprom->eui[7]);
}

static void print_version(const char *name) {
	printf("%s (" PACKAGE ") " VERSION " (" __DATE__ " " __TIME__ ")\n", name);
	printf("Copyright (C) 2015 by Multi-Tech Systems\n");
	printf(
"This program is free software; you may redistribute it under the terms of\n"
"the GNU General Public License version 2 or (at your option) any later version.\n"
"This program has absolutely no warranty.\n");
}

static void usage(FILE *out) {
	fprintf(out, "usage: mts-id-eeprom [ OPTIONS ... ]\n");
	fprintf(out, "where  OPTIONS := { \n");
	fprintf(out, "         --in-file <in-file> |\n");
	fprintf(out, "         --out-file <out-file> |\n");
	fprintf(out, "         --vendor-id <vendor-id> |\n");
	fprintf(out, "         --product-id <product-id> |\n");
	fprintf(out, "         --device-id <device-id> |\n");
	fprintf(out, "         --uuid <uuid> |\n");
	fprintf(out, "         --hw-version <hw-version> |\n");
	fprintf(out, "         --mac-addr <mac-addr> |\n");
	fprintf(out, "         --mac-bluetooth <mac-addr> |\n");
	fprintf(out, "         --mac-wifi <mac-addr> |\n");
	fprintf(out, "         --eui <EUI-64> |\n");
	fprintf(out, "         --imei <imei> |\n");
	fprintf(out, "         --capa-gps |\n");
	fprintf(out, "         --capa-din |\n");
	fprintf(out, "         --capa-dout |\n");
	fprintf(out, "         --capa-adc |\n");
	fprintf(out, "         --capa-wifi |\n");
	fprintf(out, "         --capa-bluetooth |\n");
	fprintf(out, "         --capa-clear (clears all flags) |\n");
	fprintf(out, "         --out-format { bin | yaml (default) } |\n");
	fprintf(out, "         --update |\n");
	fprintf(out, "         --accessory-card\n");
	fprintf(out, "       }\n");
	fprintf(out, "\n");
}

enum {
	CMD_OPT_IN_FILE = 128,
	CMD_OPT_OUT_FILE,
	CMD_OPT_VENDOR_ID,
	CMD_OPT_PRODUCT_ID,
	CMD_OPT_DEVICE_ID,
	CMD_OPT_UUID,
	CMD_OPT_HW_VERSION,
	CMD_OPT_MAC_ADDR,
	CMD_OPT_MAC_BLUETOOTH,
	CMD_OPT_MAC_WIFI,
	CMD_OPT_EUI,
	CMD_OPT_IMEI,
	CMD_OPT_CAPA_GPS,
	CMD_OPT_CAPA_DIN,
	CMD_OPT_CAPA_DOUT,
	CMD_OPT_CAPA_ADC,
	CMD_OPT_CAPA_WIFI,
	CMD_OPT_CAPA_BLUETOOTH,
	CMD_OPT_CAPA_CLEAR,
	CMD_OPT_OUT_FORMAT,
	CMD_OPT_UPDATE,
	CMD_OPT_ACCESSORY_CARD,
	CMD_OPT_VERSION,
	CMD_OPT_HELP,
};

static char *short_options = "f:";
static struct option long_options[] = {
	{"in-file", 1, NULL, CMD_OPT_IN_FILE},
	{"out-file", 1, NULL, CMD_OPT_OUT_FILE},
	{"vendor-id", 1, NULL, CMD_OPT_VENDOR_ID},
	{"product-id", 1, NULL, CMD_OPT_PRODUCT_ID},
	{"device-id", 1, NULL, CMD_OPT_DEVICE_ID},
	{"uuid", 1, NULL, CMD_OPT_UUID},
	{"hw-version", 1, NULL, CMD_OPT_HW_VERSION},
	{"mac-addr", 1, NULL, CMD_OPT_MAC_ADDR},
	{"mac-bluetooth", 1, NULL, CMD_OPT_MAC_BLUETOOTH},
	{"mac-wifi", 1, NULL, CMD_OPT_MAC_WIFI},
	{"eui", 1, NULL, CMD_OPT_EUI},
	{"imei", 1, NULL, CMD_OPT_IMEI},
	{"capa-gps", 0, NULL, CMD_OPT_CAPA_GPS},
	{"capa-din", 0, NULL, CMD_OPT_CAPA_DIN},
	{"capa-dout", 0, NULL, CMD_OPT_CAPA_DOUT},
	{"capa-adc", 0, NULL, CMD_OPT_CAPA_ADC},
	{"capa-wifi", 0, NULL, CMD_OPT_CAPA_WIFI},
	{"capa-bluetooth", 0, NULL, CMD_OPT_CAPA_BLUETOOTH},
	{"capa-clear", 0, NULL, CMD_OPT_CAPA_CLEAR},
	{"out-format", 1, NULL, CMD_OPT_OUT_FORMAT},
	{"update", 0, NULL, CMD_OPT_UPDATE},
	{"accessory-card", 0, NULL, CMD_OPT_ACCESSORY_CARD},
	{"version", 0, NULL, CMD_OPT_VERSION},
	{"help", 0, NULL, CMD_OPT_HELP},
	{0, 0, 0, 0},
};

int main(int argc, char *argv[]) {
	int i;
	int option_index;
	int tmp;
	int fd;

	char *in_file = NULL;
	char *out_file = "-";
	char *out_format = "yaml";
	int update = 0;
	int accessory_card = 0;

	char *vendor_id = NULL;
	char *product_id = NULL;
	char *device_id = NULL;
	char *uuid = NULL;
	char *hw_version = NULL;
	char *mac_addr = NULL;
	char *eui = NULL;

	struct mts_id_eeprom_layout id_eeprom;
	struct mts_ap_eeprom_layout ap_eeprom;

	memset(&id_eeprom, 0, eeprom_size);
	memset(&ap_eeprom, 0, eeprom_size);

#if 0
	strncpy(id_eeprom.vendor_id, VENDOR_ID_MULTITECH, sizeof(id_eeprom.vendor_id) - 1);
	strncpy(id_eeprom.product_id, PRODUCT_ID_MTCDP_E1_DK, sizeof(id_eeprom.product_id) - 1);
	strncpy(id_eeprom.device_id, "12345678", sizeof(id_eeprom.device_id) - 1);
	strncpy(id_eeprom.hw_version, HW_VERSION_MTCDP_1_0, sizeof(id_eeprom.hw_version) - 1);

	hwaddr_aton("00:d0:a0:02:0d:e1", id_eeprom.mac_addr, sizeof(id_eeprom.mac_addr));
	strncpy(id_eeprom.imei, "353626020834450", sizeof(id_eeprom.imei) - 1);

	DEVICE_CAPA_SET(id_eeprom.capa, CAPA_GPS);
	DEVICE_CAPA_SET(id_eeprom.capa, CAPA_DIN);
	DEVICE_CAPA_SET(id_eeprom.capa, CAPA_DOUT);
	DEVICE_CAPA_SET(id_eeprom.capa, CAPA_ADC);
	DEVICE_CAPA_SET(id_eeprom.capa, CAPA_WIFI);
	DEVICE_CAPA_SET(id_eeprom.capa, CAPA_BLUETOOTH);

    hwaddr_aton("00:d0:a0:02:0d:e2", id_eeprom.mac_bluetooth, sizeof(id_eeprom.mac_bluetooth));
    hwaddr_aton("00:d0:a0:02:0d:e3", id_eeprom.mac_wifi, sizeof(id_eeprom.mac_wifi));
#endif

	// parse options once to get input file and accessory_card flag only
	while((i = getopt_long(argc, argv, short_options, long_options, &option_index)) >= 0) {
		switch(i) {
		case 0:
			break;

		case CMD_OPT_IN_FILE:
			in_file = optarg;
			break;

		case CMD_OPT_ACCESSORY_CARD:
			accessory_card = 1;
			break;

		case CMD_OPT_VERSION:
			print_version("mts-id-eeprom");
			exit(0);
			break;

		case CMD_OPT_HELP:
			usage(stdout);
			exit(0);
			break;
		}
	}

	// read eeprom before parsing other options and modifying fields
	// this is needed if --update is specified
	if (in_file) {
		fd = open(in_file, O_RDONLY);
		if (fd < 0) {
			log_error("could not open in-file %s: %m", in_file);
			exit(1);
		}

		if (accessory_card) {
			tmp = read(fd, (char *) &ap_eeprom, eeprom_size);
		} else {
			tmp = read(fd, (char *) &id_eeprom, eeprom_size);
		}

		if (tmp < 0) {
			log_error("reading %s failed: %m", in_file);
			exit(1);
		} else if (tmp != eeprom_size) {
			log_error("only read %d bytes from %s", tmp, in_file);
			exit(1);
		}

		if (accessory_card) {
			log_info("loaded ap eeprom from %s successfully", in_file);
			mts_ap_eeprom_inspect(&ap_eeprom);
		} else {
			log_info("loaded id eeprom from %s successfully", in_file);
			mts_id_eeprom_inspect(&id_eeprom);
		}

		close(fd);
	}

	// reset getopt index and parse again
	optind = 0;

	while((i = getopt_long(argc, argv, short_options, long_options, &option_index)) >= 0) {
		switch(i) {
		case 0:
			break;

		case CMD_OPT_IN_FILE:
			// handled above
			break;

		case CMD_OPT_UPDATE:
			update = 1;
			break;

		case CMD_OPT_ACCESSORY_CARD:
			// handled above
			break;

		case CMD_OPT_OUT_FILE:
			out_file = optarg;
			break;

		case CMD_OPT_VENDOR_ID:
			vendor_id = optarg;
			break;

		case CMD_OPT_PRODUCT_ID:
			product_id = optarg;
			break;

		case CMD_OPT_DEVICE_ID:
			device_id = optarg;
			break;

		case CMD_OPT_UUID:
			if (accessory_card) {
				log_error("UUID not supported on accessory card eeprom");
				usage(stderr);
				exit(1);
			}
			if(asciiHexToBin(optarg, id_eeprom.uuid) != sizeof(id_eeprom.uuid)) {
				log_error("invalid uuid %s", optarg);
				usage(stderr);
				exit(1);
			}
			break;

		case CMD_OPT_HW_VERSION:
			hw_version = optarg;
			break;

		case CMD_OPT_MAC_ADDR:
			mac_addr = optarg;
			break;

		case CMD_OPT_MAC_BLUETOOTH:
			tmp = hwaddr_aton(optarg, id_eeprom.mac_bluetooth, sizeof(id_eeprom.mac_bluetooth));
			if (!tmp) {
				log_error("invalid mac-bluetooth %s", optarg);
				usage(stderr);
				exit(1);
			}
			break;

		case CMD_OPT_MAC_WIFI:
			tmp = hwaddr_aton(optarg, id_eeprom.mac_wifi, sizeof(id_eeprom.mac_wifi));
			if (!tmp) {
				log_error("invalid mac-wifi %s", optarg);
				usage(stderr);
				exit(1);
			}
			break;

		case CMD_OPT_EUI:
			eui = optarg;
			break;

		case CMD_OPT_IMEI:
			strncpy(id_eeprom.imei, optarg, sizeof(id_eeprom.imei) - 1);
			break;

		case CMD_OPT_CAPA_GPS:
			DEVICE_CAPA_SET(id_eeprom.capa, CAPA_GPS);
			break;

		case CMD_OPT_CAPA_DIN:
			DEVICE_CAPA_SET(id_eeprom.capa, CAPA_DIN);
			break;

		case CMD_OPT_CAPA_DOUT:
			DEVICE_CAPA_SET(id_eeprom.capa, CAPA_DOUT);
			break;

		case CMD_OPT_CAPA_ADC:
			DEVICE_CAPA_SET(id_eeprom.capa, CAPA_ADC);
			break;

		case CMD_OPT_CAPA_WIFI:
		    DEVICE_CAPA_SET(id_eeprom.capa, CAPA_WIFI);
		    break;

		case CMD_OPT_CAPA_BLUETOOTH:
		    DEVICE_CAPA_SET(id_eeprom.capa, CAPA_BLUETOOTH);
				break;

		case CMD_OPT_CAPA_CLEAR:
			memset(id_eeprom.capa, 0, sizeof(id_eeprom.capa));
			break;

		case CMD_OPT_OUT_FORMAT:
			if (strcmp(optarg, "bin") && strcmp(optarg, "yaml")) {
				log_error("invalid out-format %s", optarg);
				usage(stderr);
				exit(1);
			}
			out_format = optarg;

			break;

		default:
			usage(stderr);
			exit(1);
		}
	}

	/* these fields can apply to the on board EEPROM or the accessory card EEPROM */
	if (vendor_id) {
		if (! accessory_card) {
			strncpy(id_eeprom.vendor_id, vendor_id, sizeof(id_eeprom.vendor_id) - 1);
		} else {
			strncpy(ap_eeprom.vendor_id, vendor_id, sizeof(ap_eeprom.vendor_id) - 1);
		}
	}
	if (product_id) {
		if (! accessory_card) {
			strncpy(id_eeprom.product_id, product_id, sizeof(id_eeprom.product_id) - 1);
		} else {
			strncpy(ap_eeprom.product_id, product_id, sizeof(ap_eeprom.product_id) - 1);
		}
	}
	if (device_id) {
		if (! accessory_card) {
			strncpy(id_eeprom.device_id, device_id, sizeof(id_eeprom.device_id) - 1);
		} else {
			strncpy(ap_eeprom.device_id, device_id, sizeof(ap_eeprom.device_id) - 1);
		}
	}
	if (hw_version) {
		if (! accessory_card) {
			strncpy(id_eeprom.hw_version, hw_version, sizeof(id_eeprom.hw_version) - 1);
		} else {
			strncpy(ap_eeprom.hw_version, hw_version, sizeof(ap_eeprom.hw_version) - 1);
		}
	}
	if (mac_addr) {
		if (! accessory_card) {
			tmp = hwaddr_aton(mac_addr, id_eeprom.mac_addr, sizeof(id_eeprom.mac_addr));
		} else {
			tmp = hwaddr_aton(mac_addr, ap_eeprom.mac_addr, sizeof(ap_eeprom.mac_addr));
		}
		if (!tmp) {
			log_error("invalid mac-addr %s", mac_addr);
			usage(stderr);
			exit(1);
		}
	}
	if (eui) {
		if (!accessory_card) {
			log_error("EUI only supported on accessory card eeprom");
			usage(stderr);
			exit(1);
		}

		tmp = hwaddr_aton(eui, ap_eeprom.eui, sizeof(ap_eeprom.eui));
		if (!tmp) {
			log_error("invalid EUI %s", eui);
			usage(stderr);
			exit(1);
		}
	}


	// updating eeprom in place, force bin format
	if (update && in_file) {
		out_file = in_file;
		out_format = "bin";
	}

	if (out_file) {
		if (!strcmp(out_format, "bin")) {
			if (! accessory_card) {
				bin_out(out_file, (char*) &id_eeprom);
			} else {
				bin_out(out_file, (char*) &ap_eeprom);
			}
		} else if (!strcmp(out_format, "yaml")) {
			if (! accessory_card) {
				id_yaml_out(out_file, &id_eeprom);
			} else {
				ap_yaml_out(out_file, &ap_eeprom);
			}
		} else {
			log_error("un-handled out-format");
			exit(1);
		}

		/* print out what we wrote to the eeprom, unless stdout */
		if (strcmp(out_file, "-")) {
			log_info("Data written to %s", out_file);
			if (accessory_card) {
				mts_ap_eeprom_inspect(&ap_eeprom);
			} else {
				mts_id_eeprom_inspect(&id_eeprom);
			}
		}
	}

	return 0;
}