aboutsummaryrefslogtreecommitdiffstats
path: root/mpm/tools/tlv_eeprom/eeprom-init.c
blob: 6cca434fc8c261961c76f48c8d4d8dde3267ab27 (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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright 2019 Ettus Research, a National Instruments Brand
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "tlv_eeprom.h"
#include "tlv_eeprom_io.h"
#include "usrp_eeprom.h"

#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((*x)))

int parse_board_info(int argc, char **argv, void *data)
{
	struct usrp_eeprom_board_info *info = data;

	if (!argc) {
		sprintf(data, "<pid> <rev> <compat_rev> <serial>");
		return 0;
	}

	assert(argc >= 4);

	info->pid = strtoul(argv[0], NULL, 0);
	info->rev = strtoul(argv[1], NULL, 0);
	info->compat_rev = strtoul(argv[2], NULL, 0);
	strncpy(info->serial, argv[3], 8);
	info->serial[7] = '\0';

	return 4;
}
static struct usrp_eeprom_board_info board_info;

int parse_module_info(int argc, char **argv, void *data)
{
	struct usrp_eeprom_module_info *info = data;

	if (!argc) {
		sprintf(data, "<pid> <rev> <serial>");
		return 0;
	}

	assert(argc >= 3);

	info->pid = strtoul(argv[0], NULL, 0);
	info->rev = strtoul(argv[1], NULL, 0);
	strncpy(info->serial, argv[2], 8);
	info->serial[7] = '\0';

	return 3;
}
static struct usrp_eeprom_module_info module_info;

int parse_mac(int argc, char **argv, void *data)
{
	struct usrp_eeprom_mac_addr *mac = data;
	int ret;

	if (!argc) {
		sprintf(data, "<mac_addr>");
		return 0;
	}

	assert(argc >= 1);

	ret = sscanf(*argv, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
	             mac->addr + 0, mac->addr + 1, mac->addr + 2,
	             mac->addr + 3, mac->addr + 4, mac->addr + 5);
	assert(ret == 6);

	return 1;
}
static struct usrp_eeprom_mac_addr mac_addrs[USRP_EEPROM_MAX_MAC_ADDRS];

int parse_db_pwr_seq(int argc, char **argv, void *data)
{
	struct usrp_eeprom_db_pwr_seq *seq = data;
	uint8_t i;

	if (!argc) {
		sprintf(data, "<nsteps> [delay supply_mask]...");
		return 0;
	}

	assert(argc >= 1);
	seq->nsteps = strtoul(argv[0], NULL, 0);
	assert(argc >= 1 + (2 * seq->nsteps));

	argv++;
	for (i = 0; i < seq->nsteps; i++) {
		seq->steps[i].delay = strtoul(*argv++, NULL, 0);
		seq->steps[i].supply_mask = strtoul(*argv++, NULL, 0);
	}

	return 1 + 2 * i;
}
static struct usrp_eeprom_db_pwr_seq db_pwr_seq;

int parse_mcu_flags(int argc, char **argv, void *data)
{
	struct usrp_eeprom_mcu_flags *flags = data;
	unsigned long tmp;
	int i;

	if (!argc) {
		sprintf(data, "<flags[0]> <flags[1]> <flags[2]> "
		              "<flags[3]> <flags[4]> <flags[5]>");
		return 0;
	}

	assert(argc >= ARRAY_SIZE(flags->flags));
	for (i = 0; i < ARRAY_SIZE(flags->flags); i++) {
		tmp = strtoul(argv[i], NULL, 0);
		assert(tmp <= 0xFF);

		flags->flags[i] = tmp;
	}

	return i;
}
static struct usrp_eeprom_mcu_flags mcu_flags;

int parse_fan_limits(int argc, char **argv, void *data)
{
	struct usrp_eeprom_fan_limits *fan_limits = data;
	int i;

	if (!argc) {
		sprintf(data, "<fan min rpm> <fan start rpm> <fan max rpm>");
		return 0;
	}

	assert(argc >= 3);
	fan_limits->min = strtoul(argv[0], NULL, 0);
	fan_limits->start = strtoul(argv[1], NULL, 0);
	fan_limits->max = strtoul(argv[2], NULL, 0);

	return 3;
}
static struct usrp_eeprom_fan_limits fan_limits;

int parse_fan_fixed_capacity(int argc, char **argv, void *data)
{
	struct usrp_eeprom_fan_fixed_capacity *fan_fixed_capacity = data;

	if (!argc) {
		sprintf(data, "<fan fixed capacity>");
		return 0;
	}

	fan_fixed_capacity->capacity = strtoul(argv[0], NULL, 0);

	return 1;
}
static struct usrp_eeprom_fan_fixed_capacity fan_fixed_capacity;

int parse_clkaux_tuning_word(int argc, char **argv, void *data)
{
	struct usrp_eeprom_clkaux_tuning_word *clkaux_tuning_word = data;

	if (!argc) {
		sprintf(data, "<dac tuning word>");
		return 0;
	}

	clkaux_tuning_word->tuning_word = strtoul(argv[0], NULL, 0);

	return 1;
}
static struct usrp_eeprom_clkaux_tuning_word clkaux_tuning_word;

struct arg_parser {
	const char *arg;
	const char *alias;
	void *store;
	size_t size;
	uint8_t tag;
	int (*parse)(int, char **, void *);
	int dirty;
};

#define PARSER_(name_, alias_, data_, tag_, parser_) { \
	.arg = #name_, \
	.alias = alias_, \
	.store = &(data_), \
	.size = sizeof((data_)), \
	.tag = (tag_), \
	.parse = (parser_), \
}

#define PARSER(param_, tag_) PARSER_(param_, NULL, param_, tag_, parse_ ##param_)
#define MAC_PARSER_ALIAS(idx_, alias_) PARSER_( mac_addr_ ## idx_, (alias_), mac_addrs[(idx_)], USRP_EEPROM_MAC_ADDR_TAG((idx_)), parse_mac)
#define MAC_PARSER(idx_) MAC_PARSER_ALIAS(idx_, NULL)

static struct arg_parser parsers[] = {
	PARSER(board_info, USRP_EEPROM_BOARD_INFO_TAG),
	PARSER(module_info, USRP_EEPROM_MODULE_INFO_TAG),
	MAC_PARSER_ALIAS(0, "eth0_mac"),
	MAC_PARSER_ALIAS(1, "qsfp0_mac"),
	MAC_PARSER_ALIAS(2, "qsfp1_mac"),
	MAC_PARSER(3),
	MAC_PARSER(4),
	MAC_PARSER(5),
	MAC_PARSER(6),
	MAC_PARSER(7),
	MAC_PARSER(8),
	MAC_PARSER(9),
	MAC_PARSER(10),
	MAC_PARSER(11),
	MAC_PARSER(12),
	PARSER(db_pwr_seq, USRP_EEPROM_DB_PWR_SEQ_TAG),
	PARSER(mcu_flags, USRP_EEPROM_MCU_FLAGS),
	PARSER(fan_limits, USRP_EEPROM_FAN_LIMITS),
	PARSER(fan_fixed_capacity, USRP_EEPROM_FAN_FIXED_CAPACITY),
	PARSER(clkaux_tuning_word, USRP_EEPROM_CLKAUX_TUNING_WORD),
};

static struct arg_parser *parser_lookup(const char *arg)
{
	for (size_t i = 0; i < ARRAY_SIZE(parsers); i++) {
		if (!strcmp(parsers[i].arg, arg))
			return parsers + i;
		if (parsers[i].alias && !strcmp(parsers[i].alias, arg))
			return parsers + i;
	}

	return NULL;
}

#ifdef TLV_EEPROM_UPDATE
static void tlv_update(uint8_t tag, uint8_t len, const void *val)
{
	struct arg_parser *parser = NULL;

	for (size_t i = 0; i < ARRAY_SIZE(parsers); i++) {
		if (parsers[i].tag == tag)
			parser = parsers + i;
	}

	if (!parser) {
		fprintf(stderr, "found unknown tag %02x len %u", tag, len);
		fprintf(stderr, "cannot use this utility to update\n");
		exit(EXIT_FAILURE);
	}

	assert(len == parser->size);
	memcpy(parser->store, val, len);
	parser->dirty = 1;
}

static void handle_update(const char *filename)
{
	struct tlv_eeprom *eeprom;

	eeprom = tlv_eeprom_read_from_file(filename);
	if (!eeprom) {
		perror("failed to read");
		exit(EXIT_FAILURE);
	}

	if (tlv_eeprom_validate(eeprom, USRP_EEPROM_MAGIC)) {
		fprintf(stderr, "contents invalid, cannot update\n");
		exit(EXIT_FAILURE);
	}

	tlv_for_each(eeprom->tlv, eeprom->size, tlv_update);
	free(eeprom);
}
#else
static void handle_update(const char *filename)
{
}
#endif

static void validate_file_matches(const struct tlv_eeprom *eeprom,
				  const char *filename)
{
	struct tlv_eeprom *file_eeprom;

	file_eeprom = tlv_eeprom_read_from_file(filename);
	if (!file_eeprom) {
		perror("failed to read for validate");
		exit(EXIT_FAILURE);
	}

	if (memcmp(file_eeprom, eeprom, sizeof(*eeprom))) {
		fprintf(stderr, "eeprom validation failed! "
				"values read do not match what was written. "
				"perhaps the eeprom was write-protected?\n");
		exit(EXIT_FAILURE);
	}

	free(file_eeprom);
}

static void usage(const char *argv0)
{
	const struct arg_parser *p;
	char buffer[4096];

	fprintf(stderr, "usage: %s <output_file>\n", argv0);
	for (size_t i = 0; i < ARRAY_SIZE(parsers); i++) {
		p = parsers + i;
		p->parse(0, NULL, buffer);
		fprintf(stderr, "\t--%s %s\n", p->arg, buffer);
	}
}

int main(int argc, char **argv)
{
	struct arg_parser *parser;
	const char *output_file;
	struct tlv_eeprom eeprom;
	uint8_t *ptr;
	int ret;

	if (argc < 2) {
		usage(argv[0]);
		return 1;
	}

	output_file = argv[1];
	handle_update(output_file);

	argc -= 2;
	argv += 2;

	while (argc > 0) {
		if (strncmp("--", *argv, 2)) {
			fprintf(stderr, "unexpected arg\n");
			return 1;
		}

		*argv += 2;
		parser = parser_lookup(*argv);
		if (!parser) {
			fprintf(stderr, "unknown type: %s\n", *argv);
			return 1;
		}

		argc--;
		argv++;

		ret = parser->parse(argc, argv, parser->store);
		if (ret < 0) {
			fprintf(stderr, "parsing failed\n");
			return 1;
		}
		parser->dirty = 1;

		argc -= ret;
		argv += ret;
	}

	/*
	 * Now that the data's been parsed, write back any changed fields
	 */
	memset(&eeprom, 0, sizeof(eeprom));
	ptr = eeprom.tlv;
	for (size_t i = 0; i < ARRAY_SIZE(parsers); i++) {
		parser = parsers + i;
		if (!parser->dirty)
			continue;
		ptr += tlv_write(ptr, parser->tag, parser->size, parser->store);
	}

	tlv_eeprom_seal(&eeprom, USRP_EEPROM_MAGIC, ptr - eeprom.tlv);
	tlv_eeprom_write_to_file(&eeprom, output_file);
	validate_file_matches(&eeprom, output_file);

	return 0;
}