File: novena-eeprom.c

package info (click to toggle)
novena-eeprom 1.0-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 92 kB
  • ctags: 38
  • sloc: ansic: 352; makefile: 15
file content (361 lines) | stat: -rw-r--r-- 7,405 bytes parent folder | download
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <ctype.h>

#include "novena-eeprom.h"

#define EEPROM_ADDRESS (0xac>>1)
#define I2C_BUS "/dev/i2c-2"

struct eeprom_dev {
	/* File handle to I2C bus */
	int				fd;

	/* I2C address of the EEPROM */
	int				addr;

	/* True, if we've read the contents of eeprom */
	int				cached;

	/* Contents of the EEPROM */
	struct novena_eeprom_data 	data;
};

int parse_features(char *str) {
	char *ctx;
	char *sep = ",";
	char *word;
	uint16_t flags = 0;

	for (word = strtok_r(str, sep, &ctx);
	     word;
	     word = strtok_r(NULL, sep, &ctx)) {

		struct feature *feature = features;
		while (feature->name) {
			if (!strcmp(feature->name, word)) {
				flags |= feature->flags;
				break;
			}
			feature++;
		}
		if (!feature->name) {
			fprintf(stderr, "Unrecognized feature \"%s\"\n", word);
			return -1;
		}
	}
	return flags;
}

int parse_mac(char *str, void *out) {
	int i;
	char *mac = out;
	for (i=0; i<6; i++) {
		if (!isdigit(str[0]) &&
			(tolower(str[0]) < 'a' || tolower(str[0]) > 'f')) {
			printf("Unable to parse MAC address\n");
			return 1;
		}
		if (!isdigit(str[1]) &&
			(tolower(str[1]) < 'a' || tolower(str[1]) > 'f')) {
			printf("Unable to parse MAC address\n");
			return 1;
		}

		*mac = strtoul(str, NULL, 16);
		mac++;
		str+=2;
		if (*str == '-' || *str == ':' || *str == '.')
			str++;
	}
	if (*str) {
		printf("Unable to parse MAC address\n");
		return 1;
	}
	return 0;
}

int eeprom_read_i2c(struct eeprom_dev *dev, int addr, void *data, int count) {
	struct i2c_rdwr_ioctl_data session;
	struct i2c_msg messages[2];
	uint8_t set_addr_buf[2];

	memset(set_addr_buf, 0, sizeof(set_addr_buf));
	memset(data, 0, count);

	set_addr_buf[0] = addr>>8;
	set_addr_buf[1] = addr;

	messages[0].addr = dev->addr;
	messages[0].flags = 0;
	messages[0].len = sizeof(set_addr_buf);
	messages[0].buf = set_addr_buf;

	messages[1].addr = dev->addr;
	messages[1].flags = I2C_M_RD;
	messages[1].len = count;
	messages[1].buf = data;

	session.msgs = messages;
	session.nmsgs = 2;

	if(ioctl(dev->fd, I2C_RDWR, &session) < 0) {
		perror("Unable to communicate with i2c device");
		return 1;
	}

	return 0;
}

int eeprom_write_i2c(struct eeprom_dev *dev, int addr, void *data, int count) {
	struct i2c_rdwr_ioctl_data session;
	struct i2c_msg messages[1];
	uint8_t data_buf[2+count];

	data_buf[0] = addr>>8;
	data_buf[1] = addr;
	memcpy(&data_buf[2], data, count);

	messages[0].addr = dev->addr;
	messages[0].flags = 0;
	messages[0].len = sizeof(data_buf);
	messages[0].buf = data_buf;

	session.msgs = messages;
	session.nmsgs = 1;

	if(ioctl(dev->fd, I2C_RDWR, &session) < 0) {
		perror("Unable to communicate with i2c device");
		return 1;
	}

	return 0;
}

int eeprom_read(struct eeprom_dev *dev) {
	int ret;

	if (dev->cached)
		return 0;

	ret = eeprom_read_i2c(dev, 0, &dev->data, sizeof(dev->data));
	if (ret)
		return ret;

	dev->cached = 1;
	return 0;
}

int eeprom_write(struct eeprom_dev *dev) {
	dev->cached = 1;
	return eeprom_write_i2c(dev, 0, &dev->data, sizeof(dev->data));
}

struct eeprom_dev *eeprom_open(char *path, int addr) {
	struct eeprom_dev *dev;

	dev = malloc(sizeof(*dev));
	if (!dev) {
		perror("Unable to alloc data");
		goto malloc_err;
	}

	memset(dev, 0, sizeof(*dev));

	dev->fd = open(path, O_RDWR);
	if (dev->fd == -1) {
		perror("Unable to open i2c device");
		goto open_err;
	}

	dev->addr = addr;

	return dev;

open_err:
	free(dev);
malloc_err:
	return NULL;
}

int eeprom_close(struct eeprom_dev **dev) {
	if (!dev || !*dev)
		return 0;
	close((*dev)->fd);
	free(*dev);
	*dev = NULL;
	return 0;
}

int print_usage(char *name) {
	printf("Usage:\n"
	"  %s [-m 'xx:xx:xx:xx:xx:xx'] [-s 'serial'] [-f features] [-w]\n"
	"\n"
	"If no arguments are specified, then the current EEPROM contents\n"
	"will be read and printed.\n"
	"\n"
	"Specify -w to write a new EEPROM value.  Any unspecified fields\n"
	"will be set to 0.\n"
	"\n"
	"    -m    Specify a MAC address for the Gigabit Ethernet\n"
	"    -s    Specify the device's serial number\n"
	"    -f    A comma-delimited list of features present\n"
	"    -w    Actually write the value to the EEPROM\n"
	"\n", name);

	printf("Valid features:\n");
	struct feature *feature = features;
	while (feature->name) {
		printf("    %-12s%s\n", feature->name, feature->descr);
		feature++;
	}
	printf("\n");

	printf("Example:\n"
		"  %s -f es8328,retina -s 12345 -w\n"
		"", name);
	return 0;
}

int print_eeprom_data(struct eeprom_dev *dev) {
	int ret;
	ret = eeprom_read(dev);
	if (ret)
		return ret;

	printf("\tSignature:   %c%c%c%c%c%c\n",
			dev->data.signature[0],
			dev->data.signature[1],
			dev->data.signature[2],
			dev->data.signature[3],
			dev->data.signature[4],
			dev->data.signature[5]);
	printf("\tVersion:     %d\n", dev->data.version);
	printf("\tSerial:      %d\n", dev->data.serial);
	printf("\tMAC:         %02x:%02x:%02x:%02x:%02x:%02x\n",
			dev->data.mac[0], dev->data.mac[1],
			dev->data.mac[2], dev->data.mac[3],
			dev->data.mac[4], dev->data.mac[5]);
	printf("\tFeatures:    0x%x", dev->data.features);
	if (dev->data.features) {
		int matched = 0;
		int flags = dev->data.features;
		struct feature *feature = features;
		while (feature->name) {
			if (feature->flags & flags) {
				if (!matched)
					printf(" (%s", feature->name);
				else
					printf(",%s", feature->name);
				matched++;
				flags &= ~feature->flags;
			}
			feature++;
		}
		if (matched)
			printf(")");
		if (flags)
			printf(" Unrecognized flags: 0x%02x", flags);
		printf("\n");
	}
	return 0;
}

int main(int argc, char **argv) {
	struct eeprom_dev *dev;
	int ch;
	int writing = 0;
	int features;
	int32_t serial;
	uint8_t new_mac[6];

	int update_mac = 0;
	int update_features = 0;
	int update_serial = 0;

	dev = eeprom_open(I2C_BUS, EEPROM_ADDRESS);
	if (!dev)
		return 1;

	while ((ch = getopt(argc, argv, "m:s:f:w")) != -1) {
		switch(ch) {

		/* MAC address */
		case 'm':
			if (parse_mac(optarg, new_mac))
				return 1;
			update_mac = 1;
			break;

		/* Serial number */
		case 's':
			serial = strtoul(optarg, NULL, 0);
			update_serial = 1;
			break;

		/* Featuresset */
		case 'f':
			features = parse_features(optarg);
			if (features == -1)
				return 1;
			update_features = 1;
			break;

		/* Write data */
		case 'w':
			writing = 1;
			break;

		default:
			printf("Unrecognized option: %c\n", ch);
			print_usage(argv[0]);
			return 1;
		}
	}

	argc -= optind;
	argv += optind;

	if (argc)
		print_usage(argv[0]);
	else if (!writing) {
		if (update_mac || update_serial || update_features)
			printf("Not writing data, as -w was not specified\n");
		printf("Current EEPROM settings:\n");
		print_eeprom_data(dev);
	}
	else {
		int ret;
		ret = eeprom_read(dev);
		if (ret)
			return 1;
		if (update_mac)
			memcpy(&dev->data.mac, new_mac, sizeof(new_mac));
		if (update_serial)
			dev->data.serial = serial;
		if (update_features)
			dev->data.features = features;
		memcpy(&dev->data.signature, NOVENA_SIGNATURE, sizeof(dev->data.signature));
		dev->data.version = NOVENA_VERSION;

		ret = eeprom_write(dev);
		if (ret) {
			printf("EEPROM write failed\n");
			return 1;
		}

		printf("Updated EEPROM.  New values:\n");
		print_eeprom_data(dev);
	}

	eeprom_close(&dev);

	return 0;
}