summaryrefslogtreecommitdiff
path: root/util/cbi-util.c
blob: 108b82da7f39172448854c9633702c846ba848c9 (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
/* Copyright 2018 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Cros Board Info utility
 */

#include <compile_time_macros.h>
#include <errno.h>
#include <dirent.h>
#include <getopt.h>
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "cros_board_info.h"
#include "crc8.h"

#define ARGS_MASK_BOARD_VERSION		(1 << 0)
#define ARGS_MASK_FILENAME		(1 << 1)
#define ARGS_MASK_OEM_ID		(1 << 2)
#define ARGS_MASK_SIZE			(1 << 3)
#define ARGS_MASK_SKU_ID		(1 << 4)

/* TODO: Set it by macro */
const char cmd_name[] = "cbi-util";

/* Command line options */
enum {
	OPT_FILENAME,
	OPT_BOARD_VERSION,
	OPT_OEM_ID,
	OPT_SKU_ID,
	OPT_DRAM_PART_NUM,
	OPT_OEM_NAME,
	OPT_MODEL_ID,
	OPT_FW_CONFIG,
	OPT_PCB_SUPPLIER,
	OPT_SSFC,
	OPT_SIZE,
	OPT_ERASE_BYTE,
	OPT_SHOW_ALL,
	OPT_HELP,
};

static const struct option opts_create[] = {
	{"file", 1, 0, OPT_FILENAME},
	{"board_version", 1, 0, OPT_BOARD_VERSION},
	{"oem_id", 1, 0, OPT_OEM_ID},
	{"sku_id", 1, 0, OPT_SKU_ID},
	{"dram_part_num", 1, 0, OPT_DRAM_PART_NUM},
	{"oem_name", 1, 0, OPT_OEM_NAME},
	{"model_id", 1, 0, OPT_MODEL_ID},
	{"fw_config", 1, 0, OPT_FW_CONFIG},
	{"pcb_supplier", 1, 0, OPT_PCB_SUPPLIER},
	{"ssfc", 1, 0, OPT_SSFC},
	{"size", 1, 0, OPT_SIZE},
	{"erase_byte", 1, 0, OPT_ERASE_BYTE},
	{NULL, 0, 0, 0}
};

static const struct option opts_show[] = {
	{"file", 1, 0, OPT_FILENAME},
	{"all", 0, 0, OPT_SHOW_ALL},
	{NULL, 0, 0, 0}
};

static const char *field_name[] = {
	/* Same order as enum cbi_data_tag */
	"BOARD_VERSION",
	"OEM_ID",
	"SKU_ID",
	"DRAM_PART_NUM",
	"OEM_NAME",
	"MODEL_ID",
	"FW_CONFIG",
	"PCB_SUPPLIER",
	"SSFC",
};
BUILD_ASSERT(ARRAY_SIZE(field_name) == CBI_TAG_COUNT);

const char help_create[] =
	"\n"
	"'%s create [ARGS]' creates an EEPROM image file.\n"
	"Required ARGS are:\n"
	"  --file <file>               Path to output file\n"
	"  --board_version <value>     Board version\n"
	"  --oem_id <value>            OEM ID\n"
	"  --sku_id <value>            SKU ID\n"
	"  --size <size>               Size of output file in bytes\n"
	"<value> must be a positive integer <= 0XFFFFFFFF and field size can\n"
	"be optionally specified by <value:size> notation: e.g. 0xabcd:4.\n"
	"<value> can be a string for DRAM PART NUM/OEM NAME.\n"
	"<size> must be a positive integer <= 0XFFFF.\n"
	"Optional ARGS are:\n"
	"  --dram_part_num <value>     DRAM PART NUM\n"
	"  --oem_name <value>	OEM NAME\n"
	"  --erase_byte <uint8>       Byte used for empty space. Default:0xff\n"
	"  --format_version <uint16>  Data format version\n"
	"  --model_id <value>         Model ID\n"
	"  --fw_config <value>        Firmware configuration bit-field\n"
	"  --pcb_supplier <value>     PCB supplier\n"
	"  --ssfc <value>             Second Source Factory Cache bit-field\n"
	"\n"
	"<value> must be a positive integer <= 0XFFFFFFFF and field size can\n"
	"    be optionally specified by <value:size> notation: e.g. 0xabcd:4.\n"
	"<size> must be a positive integer <= 0XFFFF.\n"
	"<string> is a string\n"
	"\n";

const char help_show[] =
	"\n"
	"'%s show [ARGS]' shows data in an EEPROM image file.\n"
	"Required ARGS are:\n"
	"  --file <file>               Path to input file\n"
	"Optional ARGS are:\n"
	"  --all                       Dump all information\n"
	"It also validates the contents against the checksum and\n"
	"returns non-zero if validation fails.\n"
	"\n";

struct integer_field {
	uint32_t val;
	int size;
};

static void print_help_create(void)
{
	printf(help_create, cmd_name);
}

static void print_help_show(void)
{
	printf(help_show, cmd_name);
}

static void print_help(void)
{
	printf("\nUsage: %s <create|show> [ARGS]\n"
		"\n"
		"Utility for CBI:Cros Board Info images.\n", cmd_name);
	print_help_create();
	print_help_show();
}

static int write_file(const char *filename, const char *buf, int size)
{
	FILE *f;
	int i;

	/* Write to file */
	f = fopen(filename, "wb");
	if (!f) {
		perror("Error opening output file");
		return -1;
	}
	i = fwrite(buf, 1, size, f);
	fclose(f);
	if (i != size) {
		perror("Error writing to file");
		return -1;
	}

	return 0;
}

static uint8_t *read_file(const char *filename, uint32_t *size_ptr)
{
	FILE *f;
	uint8_t *buf;
	long size;

	*size_ptr = 0;

	f = fopen(filename, "rb");
	if (!f) {
		fprintf(stderr, "Unable to open file %s\n", filename);
		return NULL;
	}

	fseek(f, 0, SEEK_END);
	size = ftell(f);
	rewind(f);

	if (size < 0 || size > UINT32_MAX) {
		fclose(f);
		return NULL;
	}

	buf = malloc(size);
	if (!buf) {
		fclose(f);
		return NULL;
	}

	if (1 != fread(buf, size, 1, f)) {
		fprintf(stderr, "Unable to read from %s\n", filename);
		fclose(f);
		free(buf);
		return NULL;
	}

	fclose(f);

	*size_ptr = size;
	return buf;
}

static int estimate_field_size(uint32_t value)
{
	if (value <= UINT8_MAX)
		return 1;
	if (value <= UINT16_MAX)
		return 2;
	return 4;
}

static int parse_integer_field(const char *arg, struct integer_field *f)
{
	uint64_t val;
	char *e;
	char *ch;

	val = strtoul(arg, &e, 0);
	if (val > UINT32_MAX || !*arg || (e && *e && *e != ':')) {
		fprintf(stderr, "Invalid integer value\n");
		return -1;
	}
	f->val = val;

	ch = strchr(arg, ':');
	if (ch) {
		ch++;
		val = strtoul(ch, &e , 0);
		if (val < 1 || 4 < val || !*ch || (e && *e)) {
			fprintf(stderr, "Invalid size suffix\n");
			return -1;
		}
		f->size = val;
	} else {
		f->size = estimate_field_size(f->val);
	}

	if (f->val > (1ull << f->size * 8)) {
		fprintf(stderr, "Value (0x%x) exceeds field size (%d)\n",
			f->val, f->size);
		return -1;
	}

	return 0;
}

static int cmd_create(int argc, char **argv)
{
	uint8_t *cbi;
	struct board_info {
		struct integer_field ver;
		struct integer_field oem;
		struct integer_field sku;
		struct integer_field model;
		struct integer_field fw_config;
		struct integer_field pcb_supplier;
		struct integer_field ssfc;
		const char *dram_part_num;
		const char *oem_name;
	} bi;
	struct cbi_header *h;
	int rv;
	uint8_t *p;
	const char *filename;
	uint32_t set_mask = 0;
	uint16_t size;
	uint8_t erase = 0xff;
	int i;

	memset(&bi, 0, sizeof(bi));

	while ((i = getopt_long(argc, argv, "", opts_create, NULL)) != -1) {
		uint64_t val;
		char *e;
		switch (i) {
		case '?': /* Unhandled option */
			print_help_create();
			return -1;
		case OPT_HELP:
			print_help_create();
			return 0;
		case OPT_BOARD_VERSION:
			if (parse_integer_field(optarg, &bi.ver))
				return -1;
			set_mask |= ARGS_MASK_BOARD_VERSION;
			break;
		case OPT_ERASE_BYTE:
			erase = strtoul(optarg, &e, 0);
			if (!*optarg || (e && *e)) {
				fprintf(stderr, "Invalid --erase_byte\n");
				return -1;
			}
			break;
		case OPT_FILENAME:
			filename = optarg;
			set_mask |= ARGS_MASK_FILENAME;
			break;
		case OPT_OEM_ID:
			if (parse_integer_field(optarg, &bi.oem))
				return -1;
			set_mask |= ARGS_MASK_OEM_ID;
			break;
		case OPT_SIZE:
			val = strtoul(optarg, &e, 0);
			if (val > UINT16_MAX || !*optarg || (e && *e)) {
				fprintf(stderr, "Invalid --size\n");
				return -1;
			}
			size = val;
			set_mask |= ARGS_MASK_SIZE;
			break;
		case OPT_SKU_ID:
			if (parse_integer_field(optarg, &bi.sku))
				return -1;
			set_mask |= ARGS_MASK_SKU_ID;
			break;
		case OPT_DRAM_PART_NUM:
			bi.dram_part_num = optarg;
			break;
		case OPT_OEM_NAME:
			bi.oem_name = optarg;
			break;
		case OPT_MODEL_ID:
			if (parse_integer_field(optarg, &bi.model))
				return -1;
			break;
		case OPT_FW_CONFIG:
			if (parse_integer_field(optarg, &bi.fw_config))
				return -1;
			break;
		case OPT_PCB_SUPPLIER:
			if (parse_integer_field(optarg, &bi.pcb_supplier))
				return -1;
			break;
		case OPT_SSFC:
			if (parse_integer_field(optarg, &bi.ssfc))
				return -1;
			break;
		}
	}

	if (set_mask != (ARGS_MASK_BOARD_VERSION | ARGS_MASK_FILENAME |
			ARGS_MASK_OEM_ID | ARGS_MASK_SIZE | ARGS_MASK_SKU_ID)) {
		fprintf(stderr, "Missing required arguments\n");
		print_help_create();
		return -1;
	}

	cbi = malloc(size);
	if (!cbi) {
		fprintf(stderr, "Failed to allocate memory\n");
		return -1;
	}
	memset(cbi, erase, size);

	h = (struct cbi_header *)cbi;
	memcpy(h->magic, cbi_magic, sizeof(cbi_magic));
	h->major_version = CBI_VERSION_MAJOR;
	h->minor_version = CBI_VERSION_MINOR;
	p = h->data;
	p = cbi_set_data(p, CBI_TAG_BOARD_VERSION, &bi.ver.val, bi.ver.size);
	p = cbi_set_data(p, CBI_TAG_OEM_ID, &bi.oem.val, bi.oem.size);
	p = cbi_set_data(p, CBI_TAG_SKU_ID, &bi.sku.val, bi.sku.size);
	if (bi.dram_part_num != NULL) {
		p = cbi_set_data(p, CBI_TAG_DRAM_PART_NUM, bi.dram_part_num,
				strlen(bi.dram_part_num) + 1);
	}
	if (bi.oem_name != NULL) {
		p = cbi_set_data(p, CBI_TAG_OEM_NAME, bi.oem_name,
				strlen(bi.oem_name) + 1);
	}
	p = cbi_set_data(p, CBI_TAG_MODEL_ID, &bi.model.val, bi.model.size);
	p = cbi_set_data(p, CBI_TAG_FW_CONFIG, &bi.fw_config.val,
			 bi.fw_config.size);
	p = cbi_set_data(p, CBI_TAG_PCB_SUPPLIER, &bi.pcb_supplier.val,
			bi.pcb_supplier.size);
	p = cbi_set_data(p, CBI_TAG_SSFC, &bi.ssfc.val, bi.ssfc.size);

	h->total_size = p - cbi;
	h->crc = cbi_crc8(h);

	/* Output image */
	rv = write_file(filename, cbi, size);
	if (rv) {
		fprintf(stderr, "Unable to write CBI image to %s\n", filename);
		return rv;
	}
	free(cbi);

	fprintf(stderr, "CBI image is created successfully\n");

	return 0;
}

static void print_string(const uint8_t *buf, enum cbi_data_tag tag)
{
	struct cbi_data *d = cbi_find_tag(buf, tag);
	const char *name;

	if (!d)
		return;

	name = d->tag < CBI_TAG_COUNT ? field_name[d->tag] : "???";

	printf("    %s: %.*s (%u, %u)\n", name, d->size, (const char *)d->value,
		d->tag, d->size);
}

static void print_integer(const uint8_t *buf, enum cbi_data_tag tag)
{
	uint32_t v;
	struct cbi_data *d = cbi_find_tag(buf, tag);
	const char *name;

	if (!d)
		return;

	name = d->tag < CBI_TAG_COUNT ? field_name[d->tag] : "???";

	switch (d->size) {
	case 1:
		v = *(uint8_t *)d->value;
		break;
	case 2:
		v = *(uint16_t *)d->value;
		break;
	case 4:
		v = *(uint32_t *)d->value;
		break;
	default:
		printf("    %s: Integer of size %d not supported\n",
		       name, d->size);
		return;
	}
	printf("    %s: %u (0x%x, %u, %u)\n", name, v, v, d->tag, d->size);
}

static int cmd_show(int argc, char **argv)
{
	uint8_t *buf;
	uint32_t size;
	struct cbi_header *h;
	uint32_t set_mask = 0;
	const char *filename;
	int show_all = 0;
	int i;

	while ((i = getopt_long(argc, argv, "", opts_show, NULL)) != -1) {
		switch (i) {
		case '?': /* Unhandled option */
			print_help_show();
			return -1;
		case OPT_HELP:
			print_help_show();
			return 0;
		case OPT_FILENAME:
			filename = optarg;
			set_mask |= ARGS_MASK_FILENAME;
			break;
		case OPT_SHOW_ALL:
			show_all = 1;
			break;
		}
	}

	if (set_mask != ARGS_MASK_FILENAME) {
		fprintf(stderr, "Missing required arguments\n");
		print_help_show();
		return -1;
	}

	buf = read_file(filename, &size);
	if (!buf) {
		fprintf(stderr, "Unable to read CBI image\n");
		return -1;
	}

	h = (struct cbi_header *)buf;
	printf("CBI image: %s\n", filename);

	if (memcmp(h->magic, cbi_magic, sizeof(cbi_magic))) {
		fprintf(stderr, "Invalid Magic\n");
		return -1;
	}

	if (cbi_crc8(h) != h->crc) {
		fprintf(stderr, "Invalid CRC\n");
		return -1;
	}

	printf("  TOTAL_SIZE: %u\n", h->total_size);
	if (show_all)
		printf("  CBI_VERSION: %u\n", h->version);
	printf("  Data Field: name: value (hex, tag, size)\n");
	print_integer(buf, CBI_TAG_BOARD_VERSION);
	print_integer(buf, CBI_TAG_OEM_ID);
	print_integer(buf, CBI_TAG_SKU_ID);
	print_integer(buf, CBI_TAG_MODEL_ID);
	print_integer(buf, CBI_TAG_FW_CONFIG);
	print_integer(buf, CBI_TAG_PCB_SUPPLIER);
	print_integer(buf, CBI_TAG_SSFC);
	print_string(buf, CBI_TAG_DRAM_PART_NUM);
	print_string(buf, CBI_TAG_OEM_NAME);

	free(buf);

	printf("Data validated successfully\n");
	return 0;
}

int main(int argc, char **argv)
{
	if (argc < 2) {
		fprintf(stderr, "Unknown option or missing value\n");
		print_help();
		return -1;
	}

	if (!strncmp(argv[1], "create", sizeof("create")))
		return cmd_create(--argc, ++argv);
	else if (!strncmp(argv[1], "show", sizeof("show")))
		return cmd_show(--argc, ++argv);

	fprintf(stderr, "Unknown option or missing value\n");
	print_help();

	return -1;
}