summaryrefslogtreecommitdiff
path: root/build_rom.c
blob: 70a15830e86dc598b761ffcf1b1cb7e445ba54a9 (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
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifndef OLD_DB_FORMAT
# include <sqlite3.h>
#endif

#define __packed __attribute__((__packed__))

#define MAC_NUM 3

#define XIOH_DATA_OFFSET 0x100000
#define XIOH_DATA_VERSION 2
#define FIRST_SERIAL_NUMBER 42
static const char xioh_data_magic[] = {'X','I','O','H'};

enum xioh_hardware_version {
	XIOH_HARDWARE_VERSION_4 = 1,
	XIOH_HARDWARE_VERSION_5 = 2,
};

#define XIOH_HARDWARE_VERSION XIOH_HARDWARE_VERSION_5

typedef union mac {
	uint8_t b[6];
	uint64_t n:48;
} __packed mac;

struct serial {
	uint16_t date;
	unsigned version:4;
	unsigned number:12;
} __packed;

struct xioh_data {
	char magic[4];
	uint16_t version;
	uint8_t cksum;
	uint8_t reserved;
	struct serial serial;
	mac addr[MAC_NUM];
} __packed;

void record_use(char *fname, struct xioh_data *xioh_data);
bool mac_used(const char *fname, mac *addr);
bool serial_used(const char *fname, int serial);
bool parse_mac(char* src, mac *dst);
mac mac_between(mac from, mac to);
unsigned get_next_serial(void);
void set_next_serial(unsigned serial);
void choose_macs(mac from, mac to, struct xioh_data *xioh_data);
void print_mac_le(mac mac);
void print_mac_be(mac mac);
void reverse(uint8_t *t, int n);
void write_serial(struct serial *serial);
void dump_xioh_data(struct xioh_data *xioh_data);
void write_xioh_data(const char *fname, struct xioh_data *xioh_data);
void write_standalone(const char *fname, struct xioh_data *xioh_data);
uint8_t cksum(const uint8_t* addr, size_t sz);

int rand_range(int max) { return (int)(rand()/(double)RAND_MAX*max); }
unsigned date(void) { return time(NULL) / 86400; }

static char *next_serial = "next_serial.txt";

#ifdef OLD_DB_FORMAT
static char *db = "db.bin";
#else
static char *db = "db.sqlite";
#endif

static void print_usage(char *name)
{
	fprintf(stderr, "Usage: %s [options]\n"
			"\t-c FILE\tCoreboot ROM file where the addresses are "
				"to be written\n"
			"\t-s FILE\tCreate a file for use with cbfstool\n"
			"\t-S FILE\tGet and store the next serial number in "
				"this file\n"
			"\t-d FILE\tDatabase file for used serial numbers and "
				"MAC addresses\n"
			"\t-m MAC\tUse this MAC address + the 2 next\n"
			"\t-f MAC\tUse random MAC addresses (start of range)\n"
			"\t-t MAC\tUse random MAC addresses (end of range)\n"
			"\t-n NUM\tUse this number for the serial\n"
			, name);
}

int main(int argc, char *argv[])
{
	int i;
	struct xioh_data xioh_data;
	char *fname = "coreboot.rom";
	char *standalone = NULL;
	mac from, to;
	bool hasfrom = false, hasto = false;
	enum { MODE_UNSET, RANDOM, CONTINUOUS } mode = MODE_UNSET;

	srand(time(NULL)+getpid());

	while (true) {
		int c = getopt(argc, argv, "s:m:f:t:n:c:d:S:");

		if (c == -1) break;

		switch (c) {
		case 'c':
			fname = optarg;
			break;
		case 's':
			standalone = optarg;
			break;
		case 'd':
			db = optarg;
			break;
		case 'S':
			next_serial = optarg;
			break;
		case 'm':
			mode = CONTINUOUS;
			parse_mac(optarg, &xioh_data.addr[0]);
			break;
		case 'f':
			mode = RANDOM;
			hasfrom = true;
			parse_mac(optarg, &from);
			break;
		case 't':
			mode = RANDOM;
			hasto = true;
			parse_mac(optarg, &to);
			break;
		case 'n':
			{
				char *endptr;
				int next = strtol(optarg, &endptr, 0);
				if (!*optarg || *endptr || next < 0) {
					fprintf(stderr, "-n requires a positive number\n");
					exit(1);
				}

				set_next_serial(next);
			}
			break;
		default:
			print_usage(argv[0]);
			exit(1);
		}
	}

	switch (mode) {
	case CONTINUOUS:
		for(i = 1; i < MAC_NUM; i++)
			do xioh_data.addr[i].n = xioh_data.addr[i-1].n + 1;
			while (mac_used(db, &xioh_data.addr[i]));
		break;
	case RANDOM:
		if (!(hasfrom && hasto)) {
			fprintf(stderr, "Needs both -f and -t\n");
			exit(1);
		}
		choose_macs(from, to, &xioh_data);
		break;
	case MODE_UNSET:
		print_usage(argv[0]);
		exit(1);
	}

	memcpy(xioh_data.magic, xioh_data_magic, sizeof(xioh_data.magic));
	xioh_data.version = XIOH_DATA_VERSION;
	write_serial(&xioh_data.serial);

	for (i = 0; i < MAC_NUM; i++)
		reverse(xioh_data.addr[i].b, 6);

	xioh_data.reserved = 0;
	xioh_data.cksum = 0;
	xioh_data.cksum = cksum((uint8_t*)&xioh_data, sizeof(xioh_data));

	dump_xioh_data(&xioh_data);

	if (standalone)
		write_standalone(standalone, &xioh_data);
	else
		write_xioh_data(fname, &xioh_data);

	record_use(db, &xioh_data);

	return 0;
}

uint8_t cksum(const uint8_t* addr, size_t sz)
{
	uint8_t r = 0;
	for (; sz; sz--)
		r ^= *addr++;
	return r;
}

void write_standalone(const char *fname, struct xioh_data *xioh_data)
{
	FILE *f = fopen(fname, "w");
	if (!f) {
		printf("Can't open file \"%s\": %s\n", fname, strerror(errno));
		exit(errno);
	}

	fwrite(xioh_data, sizeof(*xioh_data), 1, f);
	fclose(f);
}

void write_xioh_data(const char *fname, struct xioh_data *xioh_data)
{
	FILE *rom = fopen(fname, "r+");
	if (!rom) {
		printf("Can't open file \"%s\": %s\n", fname, strerror(errno));
		exit(errno);
	}

	if(fseek(rom, XIOH_DATA_OFFSET, SEEK_SET)) {
		printf("Can't seek: %s\n", strerror(errno));
		exit(errno);
	}

	fwrite(xioh_data, sizeof(*xioh_data), 1, rom);
	fclose(rom);
}

void dump_xioh_data(struct xioh_data *xioh_data)
{
	int i;
	time_t time = xioh_data->serial.date * 86400;
	char *timestr = ctime(&time);
	timestr[strlen(timestr)-1] = '\0';

	printf("Magic: %c%c%c%c\n"
	       "Version: %u\n"
	       "Checksum: %02x\n"
	       "Serial:\n"
	       "\tDate: %04x (%s)\n"
	       "\tHardware version: %u\n"
	       "\tNumber: %u\n",
	       xioh_data->magic[0], xioh_data->magic[1],
	       xioh_data->magic[2], xioh_data->magic[3],
	       xioh_data->version,
	       xioh_data->cksum,
	       xioh_data->serial.date, timestr,
	       xioh_data->serial.version, xioh_data->serial.number);

	printf("MAC addresses:\n");
	for (i = 0; i < MAC_NUM; i++) {
		printf("\t");
		print_mac_be(xioh_data->addr[i]);
		printf("\n");
	}
}

void choose_macs(mac from, mac to, struct xioh_data *xioh_data)
{
	int i, j;
	for(i = 0; i < MAC_NUM; i++) {
retry:		xioh_data->addr[i] = mac_between(from, to);

		for (j = 0; j < i; j++)
			if (xioh_data->addr[i].n == xioh_data->addr[j].n)
				goto retry;

		if (mac_used(db, &xioh_data->addr[i]))
			goto retry;
	}
}

void write_serial(struct serial *serial)
{
	serial->date = date();
	serial->version = XIOH_HARDWARE_VERSION;
	serial->number = get_next_serial();
}

void set_next_serial(unsigned serial)
{
	FILE *f	= fopen(next_serial, "w");
	if (!f) {
		printf("Can't open file \"%s\": %s\n", next_serial, strerror(errno));
		exit(errno);
	}
	fprintf(f, "%u", serial);
	fclose(f);
}

unsigned get_next_serial(void)
{
	unsigned cur = FIRST_SERIAL_NUMBER;
	FILE *f = fopen(next_serial, "r");
	if (f) {
		fscanf(f, "%u", &cur);
		fclose(f);
	}

	while (serial_used(db, cur))
		cur++;

	set_next_serial(cur+1);

	return cur;
}

#ifndef OLD_DB_FORMAT
# define TEST_IN_SQLITE3(cond, bind) \
	sqlite3 *db; \
	sqlite3_stmt *stmt; \
	int rc, res; \
\
	rc = sqlite3_open(fname, &db); \
	if (SQLITE_OK != rc) { \
		fprintf(stderr, "could not open sqlite database \"%s\" " \
				"(error %d)\n", fname, rc); \
		sqlite3_close(db); \
		res = -2; \
		goto opened; \
	} \
\
	rc = sqlite3_prepare_v2(db, "SELECT 1 FROM xioh_data " \
			"WHERE " cond, \
			-1, &stmt, NULL); \
	if (SQLITE_OK != rc) { \
		fprintf(stderr, "could not compile statement (error %d)\n", rc); \
		sqlite3_close(db); \
		res = -4; \
		goto prepared; \
	} \
\
	bind; \
\
	rc = sqlite3_step(stmt); \
\
	switch (rc) { \
	case SQLITE_DONE: \
		res = false; \
		break; \
	case SQLITE_ROW: \
		res = true; \
		break; \
	default: \
		fprintf(stderr, "could not execute statement (error %d)\n", rc); \
		res = -5; \
	} \
\
prepared: \
	sqlite3_finalize(stmt); \
opened: \
	sqlite3_close(db); \
\
	if (res < 0) \
		exit(-res); \
	else \
		return res;
#endif

bool mac_used(const char *fname, mac *addr)
{
#ifdef OLD_DB_FORMAT
	FILE *f = fopen(fname, "r");
	struct xioh_data xioh_data;
	unsigned i;

	if (!f) {
		printf("Can't open file \"%s\": %s\n", fname, strerror(errno));
		exit(errno);
	}

	while (0 < fread(&xioh_data, sizeof(xioh_data), 1, f)) {
		for (i = 0; i < MAC_NUM; i++)
			if (xioh_data.addr[i].n == addr->n) {
				print_mac_le(*addr);
				printf(" already used\n");
				fclose(f);
				return true;
			}
	}

	fclose(f);
	return false;
#else
	TEST_IN_SQLITE3("mac1 == ? OR mac2 == ? OR mac3 == ?",
			int i;
			for (i = 1; i <= 3; i++)
				sqlite3_bind_blob(stmt, i, addr, sizeof(*addr),
						SQLITE_STATIC))

#endif
}

bool serial_used(const char *fname, int serial)
{
#ifdef OLD_DB_FORMAT
	FILE *f = fopen(fname, "r");
	struct xioh_data xioh_data;

	if (!f) {
		printf("Can't open file \"%s\": %s\n", fname, strerror(errno));
		exit(errno);
	}

	while (0 < fread(&xioh_data, sizeof(xioh_data), 1, f)) {
		if (xioh_data.serial.number == serial) {
			printf("Serial number %d already used! Trying %d.\n", serial, serial+1);
			fclose(f);
			return true;
		}
	}

	fclose(f);
	return false;
#else
	TEST_IN_SQLITE3("serial_number == ?", sqlite3_bind_int(stmt, 1, serial))
#endif
}

void record_use(char *fname, struct xioh_data *xioh_data)
{
#ifdef OLD_DB_FORMAT
	FILE *f = fopen(fname, "a");
	if (!f) {
		printf("Can't open file \"%s\": %s\n", fname, strerror(errno));
		exit(errno);
	}

	fwrite(xioh_data, sizeof(*xioh_data), 1, f);
	fclose(f);
#else
	int rc, i;
	sqlite3 *db;
	sqlite3_stmt *stmt;

	rc = sqlite3_open(fname, &db);
	if (SQLITE_OK != rc) {
		fprintf(stderr, "could not open sqlite database \"%s\" (error %d)\n", fname, rc);
		sqlite3_close(db);
		exit(2);
	}

	rc = sqlite3_prepare_v2(db,
			"INSERT INTO xioh_data (xioh_data, version, date,"
			"hardware_version, serial_number, mac1, mac2, mac3 ) "
			"VALUES (?, ?, ?, ?, ?, ?, ?, ?)", -1, &stmt, NULL);

	if (SQLITE_OK != rc) {
		fprintf(stderr, "could not compile statement (error %d)\n", rc);
		sqlite3_close(db);
		exit(5);
	}

	sqlite3_bind_blob(stmt, 1, xioh_data, sizeof(*xioh_data),
			SQLITE_TRANSIENT);
	sqlite3_bind_int(stmt, 2, xioh_data->version);
	sqlite3_bind_int(stmt, 3, xioh_data->serial.date);
	sqlite3_bind_int(stmt, 4, xioh_data->serial.version);
	sqlite3_bind_int(stmt, 5, xioh_data->serial.number);
	for (i = 0; i < MAC_NUM; i++)
		sqlite3_bind_blob(stmt, 6, &xioh_data->addr[i],
				sizeof(*xioh_data->addr),
				SQLITE_TRANSIENT);

	rc = sqlite3_step(stmt);
	if (SQLITE_DONE != rc) {
		fprintf(stderr, "could not insert into database: (error %d)\n", rc);
		dump_xioh_data(xioh_data);
		sqlite3_finalize(stmt);
		sqlite3_close(db);
		exit(4);
	}

	sqlite3_finalize(stmt);
	rc = sqlite3_close(db);

	if (SQLITE_OK != rc) {
		fprintf(stderr, "Error on closing database (error %d)\n", rc);
		exit(2);
	}
#endif
}

mac mac_between(mac from, mac to)
{
	return (mac){.n = from.n + rand_range(to.n - from.n)};
}

bool parse_mac(char* src, union mac *dst)
{
	return 6 == sscanf(src, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
			dst->b+5, dst->b+4, dst->b+3, dst->b+2, dst->b+1, dst->b+0);
}

void print_mac_le(union mac mac)
{
	printf("%02x:%02x:%02x:%02x:%02x:%02x",
			mac.b[5], mac.b[4], mac.b[3],
			mac.b[2], mac.b[1], mac.b[0]);
}

void print_mac_be(union mac mac)
{
	printf("%02x:%02x:%02x:%02x:%02x:%02x",
			mac.b[0], mac.b[1], mac.b[2],
			mac.b[3], mac.b[4], mac.b[5]);
}

void reverse(uint8_t *t, int n)
{
	uint8_t s;
	int i;
	for (i = 0; i < n/2; i++) {
		s = t[i];
		t[i] = t[n-1-i];
		t[n-1-i] = s;
	}
}