summaryrefslogtreecommitdiff
path: root/xpp/astribank_allow.c
blob: b87f6fae4dac673e4f38876e22e8bbeca7cb7c0e (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
/*
 * Written by Oron Peled <oron@actcom.co.il> and
 *            Alex Landau <alex.landau@xorcom.com>
 * Copyright (C) 2008, Xorcom
 *
 * All rights reserved.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <getopt.h>
#include <time.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <ctype.h>
#include "mpp.h"
#include "mpp_funcs.h"
#include "debug.h"

static const char rcsid[] = "$Id$";

#define	DBG_MASK	0x80

static char	*progname;

static void usage()
{
	fprintf(stderr, "Usage: %s [options...] -D {/proc/bus/usb|/dev/bus/usb}/<bus>/<dev> options\n", progname);
	fprintf(stderr, "\tOptions:\n");
	fprintf(stderr, "\t\t[-v]               # Increase verbosity\n");
	fprintf(stderr, "\t\t[-d mask]          # Debug mask (0xFF for everything)\n");
	fprintf(stderr, "\t\t[-w]               # Write capabilities to EEPROM, otherwise read capabilities\n");
	fprintf(stderr, "\t\t[-f filename]      # License filename (stdin/stdout if not specified)\n\n");
	exit(1);
}

static int capabilities_burn(
		struct astribank_device *astribank,
		struct eeprom_table *eeprom_table,
		struct capabilities *capabilities,
		struct capkey *key)
{
	int	ret;

	INFO("Burning capabilities\n");
	ret = mpp_caps_set(astribank, eeprom_table, capabilities, key);
	if(ret < 0) {
		ERR("Capabilities burning failed: %d\n", ret);
		return ret;
	}
	INFO("Done\n");
	return 0;
}

static int bin_to_file(void *buf, int len, FILE *f)
{
	static int bytes_on_line;
	unsigned char *p = buf;
	if (buf == NULL) {
		if (bytes_on_line != 0) {
			if (fprintf(f, "\n") != 1)
				return -1;
			bytes_on_line = 0;
		}
		return 0;
	}
	int i;
	for (i = 0; i < len; i++) {
		if (fprintf(f, "%02x", *p++) != 2)
			return -1;
		bytes_on_line++;
		if (bytes_on_line >= 16) {
			if (fprintf(f, "\n") != 1)
				return -1;
			bytes_on_line = 0;
		}
	}
	return 0;
}

static int write_to_file(struct eeprom_table *eeprom_table, struct capabilities *caps, struct capkey *key, FILE *f)
{
	fprintf(f, "-----BEGIN XORCOM LICENSE BLOCK-----\n");
	fprintf(f, "Version: 1.0\n");
	fprintf(f, "Timestamp: %u\n", caps->timestamp);
	fprintf(f, "Serial: %.*s\n", LABEL_SIZE, eeprom_table->label);
	fprintf(f, "Capabilities.Port.FXS: %d\n", caps->ports_fxs);
	fprintf(f, "Capabilities.Port.FXO: %d\n", caps->ports_fxo);
	fprintf(f, "Capabilities.Port.BRI: %d\n", caps->ports_bri);
	fprintf(f, "Capabilities.Port.PRI: %d\n", caps->ports_pri);
	fprintf(f, "Capabilities.Twinstar: %d\n", CAP_EXTRA_TWINSTAR(caps));
	fprintf(f, "Data:\n");
	bin_to_file(eeprom_table, sizeof(*eeprom_table), f);
	bin_to_file(caps, sizeof(*caps), f);
	bin_to_file(key, sizeof(*key), f);
	bin_to_file(NULL, 0, f);
	fprintf(f, "-----END XORCOM LICENSE BLOCK-----\n");
	return 0;
}

/*
 * Removes whitespace on both sizes of the string.
 * Returns a pointer to the first non-space char. The string
 * is modified in place to trim trailing whitespace.
 * If the whole string is whitespace, returns NULL.
 */
char *trim(char *s)
{
	int len = strlen(s);
	while (len > 0 && isspace(s[len-1])) {
		len--;
	}
	if (len == 0)
		return NULL;
	s[len] = '\0';
	while (isspace(*s))
		s++;
	/* *s is not a space, since in this case we'd return NULL above */
	return s;
}

int get_key_value(char *line, char **key, char **value)
{
	char *p = strchr(line, ':');
	if (p == NULL)
		return -1;
	*p = '\0';
	*key = trim(line);
	*value = trim(p + 1);
	return 0;
}

static int hex_digit_to_int(char c)
{
	if (c >= '0' && c <= '9')
		return c - '0';
	else if (c >= 'a' && c <= 'f')
		return c - 'a' + 10;
	else
		return -1;
}

static int str_to_bin(char *line, void *buf, int maxlen)
{
	static int offset;
	unsigned char *p = buf;
	if (strlen(line) % 2 != 0)
		return -1;
	while (offset < maxlen && *line) {
		uint8_t value;
		char c = hex_digit_to_int(*line++);
		if (c < 0 || *line == '\0')
			return -1;
		value = c << 4;
		c = hex_digit_to_int(*line++);
		if (c < 0)
			return -1;
		value |= c;
		p[offset++] = value;
	}
	if (offset == maxlen && *line)
		return -1;
	return offset;
}

static int read_from_file(struct eeprom_table *eeprom_table, struct capabilities *caps, struct capkey *capkey, FILE *f)
{
	char buf[256];
	char *line, *key, *value;
	int state = 0;
	int lineno = 0;
	struct table {
		struct eeprom_table eeprom_table;
		struct capabilities capabilities;
		struct capkey capkey;
	} PACKED table;

	memset(&table, 0, sizeof(struct table));
	/*
	 * states:
	 * 0: start - before BEGIN_LICENSE_BLOCK line. on BEGIN_LICENSE_BLOCK line goto 1.
	 * 1: read Version, goto 2. if not version line then error.
	 * 2: after BEGIN line. split line into key:value. if line is Data:, goto 3.
	 * 3: read binary data. if line is END_LICENSE_BLOCK goto 4.
	 * 4: END_LICENSE_BLOCK - ignore lines.
	 */
	while (fgets(buf, 256, f) != NULL) {
		lineno++;
		int len = strlen(buf);
		if (len > 0 && buf[len-1] != '\n') {
			ERR("Line %d: Line too long\n", lineno);
			return -1;
		}
		line = trim(buf);
		if (line == NULL) {
 			if (state > 0 && state < 4) {
				ERR("Line %d: Empty line\n", lineno);
				return -1;
			}
			else
				continue;
		}
		switch (state) {
			case 0:
				if (strcmp(line, "-----BEGIN XORCOM LICENSE BLOCK-----") == 0)
					state = 1;
				else {
					ERR("Line %d: Invalid license begin block\n", lineno);
					return -1;
				}
				break;
			case 1:
				if (get_key_value(line, &key, &value) < 0) {
					ERR("Line %d: Can't parse line\n", lineno);
					return -1;
				}
				if (strcmp(key, "Version") == 0) {
					if (strcmp(value, "1.0") == 0) {
						state = 2;
					} else {
						ERR("Line %d: Unknown license file version '%s', need version '1.0'\n", lineno, value);
						return -1;
					}
				} else {
					ERR("Line %d: No license file version\n", lineno);
					return -1;
				}
				break;
			case 2:
				if (get_key_value(line, &key, &value) < 0) {
					ERR("Line %d: Can't parse line\n", lineno);
					return -1;
				}
				if (strcmp(key, "Data") == 0) {
					state = 3;
					break;
				}
				break;
			case 3:
				if (strcmp(line, "-----END XORCOM LICENSE BLOCK-----") == 0) {
					state = 4;
					break;
				}
				if (str_to_bin(line, &table, sizeof(table)) < 0) {
					ERR("Line %d: Error in data block\n", lineno);
					return -1;
				}
				break;
			case 4:
				break;

		}
	}
	if (state != 4) {
		ERR("Invalid license file\n");
		return -1;
	}
	memcpy(eeprom_table, &table.eeprom_table, sizeof(*eeprom_table));
	memcpy(caps, &table.capabilities, sizeof(*caps));
	memcpy(capkey, &table.capkey, sizeof(*capkey));
	return 0;
}

int main(int argc, char *argv[])
{
	char			*devpath = NULL;
	struct astribank_device *astribank;
	struct eeprom_table	eeprom_table;
	struct capabilities	caps;
	struct capkey		key;
	const char		options[] = "vd:D:wf:";
	int			do_write = 0;
	FILE			*file;
	char			*filename = NULL;
	int			ret;

	progname = argv[0];
	while (1) {
		int	c;

		c = getopt (argc, argv, options);
		if (c == -1)
			break;

		switch (c) {
			case 'D':
				devpath = optarg;
				break;
			case 'v':
				verbose++;
				break;
			case 'd':
				debug_mask = strtoul(optarg, NULL, 0);
				break;
			case 'w':
				do_write = 1;
				break;
			case 'f':
				filename = optarg;
				break;
			case 'h':
			default:
				ERR("Unknown option '%c'\n", c);
				usage();
		}
	}
	if(!devpath) {
		ERR("Missing device path\n");
		usage();
	}
	DBG("Startup %s\n", devpath);
	if((astribank = mpp_init(devpath)) == NULL) {
		ERR("Failed initializing MPP\n");
		return 1;
	}
	if(astribank->eeprom_type != EEPROM_TYPE_LARGE) {
		ERR("Cannot use this program with astribank EEPROM type %d (need %d)\n",
			astribank->eeprom_type, EEPROM_TYPE_LARGE);
		return 1;
	}
	ret = mpp_caps_get(astribank, &eeprom_table, &caps, &key);
	if(ret < 0) {
		ERR("Failed to get original capabilities: %d\n", ret);
		return 1;
	}
	if (do_write) {
		/* update capabilities based on input file */
		file = stdin;
		if (filename) {
			file = fopen(filename, "r");
			if (file == NULL) {
				ERR("Can't open file '%s'\n", filename);
				return 1;
			}
		}
		ret = read_from_file(&eeprom_table, &caps, &key, file);
		if (ret < 0) {
			ERR("Failed to read capabilities from file: %d\n", ret);
			return 1;
		}
		show_capabilities(&caps, stderr);
		if (capabilities_burn(astribank, &eeprom_table, &caps, &key) < 0)
			return 1;
		if (file != stdin)
			fclose(file);
	} else {
		/* print capabilities to stdout */
		file = stdout;
		if (filename) {
			file = fopen(filename, "w");
			if (file == NULL) {
				ERR("Can't create file '%s'\n", filename);
				return 1;
			}
		}
		ret = write_to_file(&eeprom_table, &caps, &key, file);
		if (ret < 0) {
			ERR("Failed to write capabilities to file: %d\n", ret);
			return 1;
		}
		if (file != stdout)
			fclose(file);
	}
	mpp_exit(astribank);
	return 0;
}