summaryrefslogtreecommitdiff
path: root/xpp/astribank_hexload.c
blob: 0fa9010a28b75b35f211b01d82aaf7125c877efe (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
/*
 * Written by Oron Peled <oron@actcom.co.il>
 * 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 <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
#include <arpa/inet.h>
#include <debug.h>
#include "hexfile.h"
#include "mpptalk.h"
#include "pic_loader.h"
#include "echo_loader.h"
#include "astribank_usb.h"
#include "../autoconfig.h"

#define	DBG_MASK	0x80
#define	MAX_HEX_LINES	64000
#define HAVE_OCTASIC	1

static char	*progname;

static void usage()
{
	fprintf(stderr, "Usage: %s [options...] -D {/proc/bus/usb|/dev/bus/usb}/<bus>/<dev> hexfile...\n", progname);
	fprintf(stderr, "\tOptions: {-F|-p}\n");
	fprintf(stderr, "\t\t[-E]               # Burn to EEPROM\n");
#if HAVE_OCTASIC
	fprintf(stderr, "\t\t[-O]               # Load Octasic firmware\n");
	fprintf(stderr, "\t\t[-o]               # Show Octasic version\n");
#endif
	fprintf(stderr, "\t\t[-F]               # Load FPGA firmware\n");
	fprintf(stderr, "\t\t[-p]               # Load PIC firmware\n");
	fprintf(stderr, "\t\t[-v]               # Increase verbosity\n");
	fprintf(stderr, "\t\t[-A]               # Set A-Law for 1st module\n");
	fprintf(stderr, "\t\t[-d mask]          # Debug mask (0xFF for everything)\n");
	exit(1);
}

int handle_hexline(struct astribank_device *astribank, struct hexline *hexline)
{
	uint16_t	len;
	uint16_t	offset_dummy;
	uint8_t		*data;
	int		ret;

	assert(hexline);
	assert(astribank);
	if(hexline->d.content.header.tt != TT_DATA) {
		DBG("Non data record type = %d\n", hexline->d.content.header.tt);
		return 0;
	}
	len = hexline->d.content.header.ll;
	offset_dummy = hexline->d.content.header.offset;
	data = hexline->d.content.tt_data.data;
	if((ret = mpp_send_seg(astribank, data, offset_dummy, len)) < 0) {
		ERR("Failed hexfile send line: %d\n", ret);
		return -EINVAL;
	}
	return 0;
}

void print_parse_errors(int level, const char *msg, ...)
{
	va_list ap;

	if (verbose > level) {
		va_start (ap, msg);
		vfprintf (stderr, msg, ap);
		va_end (ap);
	}
}

static int load_hexfile(struct astribank_device *astribank, const char *hexfile, enum dev_dest dest)
{
	struct hexdata		*hexdata = NULL;
	int			finished = 0;
	int			ret;
	unsigned		i;
	char			star[] = "+\\+|+/+-";
	const char		*devstr;

	parse_hexfile_set_reporting(print_parse_errors);
	if((hexdata  = parse_hexfile(hexfile, MAX_HEX_LINES)) == NULL) {
		perror(hexfile);
		return -errno;
	}
	devstr = xusb_devpath(astribank->xusb);
	INFO("%s [%s]: Loading %s Firmware: %s (version %s)\n",
		devstr,
		xusb_serial(astribank->xusb),
		dev_dest2str(dest),
		hexdata->fname, hexdata->version_info);
	if((ret = mpp_send_start(astribank, dest, hexdata->version_info)) < 0) {
		ERR("%s: Failed hexfile send start: %d\n", devstr, ret);
		return ret;
	}
	for(i = 0; i < hexdata->maxlines; i++) {
		struct hexline	*hexline = hexdata->lines[i];

		if(!hexline)
			break;
		if(verbose > LOG_INFO) {
			printf("Sending: %4d%%    %c\r", (100 * i) / hexdata->last_line, star[i % sizeof(star)]);
			fflush(stdout);
		}
		if(finished) {
			ERR("%s: Extra data after End Of Data Record (line %d)\n", devstr, i);
			return 0;
		}
		if(hexline->d.content.header.tt == TT_EOF) {
			DBG("End of data\n");
			finished = 1;
			continue;
		}
		if((ret = handle_hexline(astribank, hexline)) < 0) {
			ERR("%s: Failed hexfile sending in lineno %d (ret=%d)\n", devstr, i, ret);;
			return ret;
		}
	}
	if(verbose > LOG_INFO) {
		putchar('\n');
		fflush(stdout);
	}
	if((ret = mpp_send_end(astribank)) < 0) {
		ERR("%s: Failed hexfile send end: %d\n", devstr, ret);
		return ret;
	}
#if 0
	fclose(fp);
#endif
	free_hexdata(hexdata);
	DBG("hexfile loaded successfully\n");
	return 0;
}

int main(int argc, char *argv[])
{
	char			*devpath = NULL;
	int			opt_pic = 0;
	int			opt_echo = 0;
	int			opt_ecver = 0;
#if HAVE_OCTASIC
	int			opt_alaw = 0;
#endif
	int			opt_dest = 0;
	int			opt_sum = 0;
	enum dev_dest		dest = DEST_NONE;
	const char		options[] = "vd:D:EFOopA";
	int			iface_num;
	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 'E':
				if(dest != DEST_NONE) {
					ERR("The -F and -E options are mutually exclusive.\n");
					usage();
				}
				opt_dest++;
				dest = DEST_EEPROM;
				break;
			case 'F':
				if(dest != DEST_NONE) {
					ERR("The -F and -E options are mutually exclusive.\n");
					usage();
				}
				opt_dest++;
				dest = DEST_FPGA;
				break;
#if HAVE_OCTASIC
			case 'O':
				opt_echo = 1;
				break;
			case 'o':
				opt_ecver = 1;
				break;
			case 'A':
				opt_alaw = 1;
				break;
#endif
			case 'p':
				opt_pic = 1;
				break;
			case 'v':
				verbose++;
				break;
			case 'd':
				debug_mask = strtoul(optarg, NULL, 0);
				break;
			case 'h':
			default:
				ERR("Unknown option '%c'\n", c);
				usage();
		}
	}
	opt_sum = opt_dest + opt_pic + opt_echo;
	if(opt_sum > 1 || (opt_sum == 0 && opt_ecver == 0)) {
		ERR("The -F, -E"
#if HAVE_OCTASIC
			", -O"
#endif
			" and -p options are mutually exclusive, if neither is used then -o should present\n");
		usage();
	}
	iface_num = (opt_dest) ? 1 : 0;
	if(!opt_pic && !opt_ecver) {
		if(optind != argc - 1) {
			ERR("Got %d hexfile names (Need exactly one hexfile)\n",
				argc - 1 - optind);
			usage();
		}
	}
	if(!devpath) {
		ERR("Missing device path.\n");
		usage();
	}
	if(opt_dest) {
		/*
		 * MPP Interface
		 */
		struct astribank_device *astribank;

		if((astribank = mpp_init(devpath, iface_num)) == NULL) {
			ERR("%s: Opening astribank failed\n", devpath);
			return 1;
		}
		//show_astribank_info(astribank);
		if(load_hexfile(astribank, argv[optind], dest) < 0) {
			ERR("%s: Loading firmware to %s failed\n", devpath, dev_dest2str(dest));
			return 1;
		}
		astribank_close(astribank, 0);
	} else if(opt_pic || opt_echo || opt_ecver) {
		/*
		 * XPP Interface
		 */
		struct astribank_device *astribank;

		if((astribank = astribank_open(devpath, iface_num)) == NULL) {
			ERR("%s: Opening astribank failed\n", devpath);
			return 1;
		}
		//show_astribank_info(astribank);
#if HAVE_OCTASIC
		if (opt_ecver) {
			if((ret = echo_ver(astribank)) < 0) {
				ERR("%s: Get Octasic version failed (Is Echo canceller card connected?)\n", devpath);
				return 1;
			} else 
				INFO("Octasic version: 0x%0X\n", ret);
		}
#endif
		if (opt_pic) {
			if ((ret = load_pic(astribank, argc - optind, argv + optind)) < 0) {
				ERR("%s: Loading PIC's failed\n", devpath);
				return 1;
			}
#if HAVE_OCTASIC
		} else if (opt_echo) {
			if((ret = load_echo(astribank, argv[optind], opt_alaw)) < 0) {
				ERR("%s: Loading ECHO's failed\n", devpath);
				return 1;
			}
#endif
		}
		astribank_close(astribank, 0);
	}
	return 0;
}