xpp: astribank_hexload: allow empty span specs
[dahdi/tools.git] / xpp / astribank_hexload.c
1 /*
2  * Written by Oron Peled <oron@actcom.co.il>
3  * Copyright (C) 2008, Xorcom
4  *
5  * All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <arpa/inet.h>
31 #include <debug.h>
32 #include "hexfile.h"
33 #include "mpptalk.h"
34 #include "pic_loader.h"
35 #include "echo_loader.h"
36 #include "astribank_usb.h"
37 #include "../autoconfig.h"
38
39 #define DBG_MASK        0x80
40 #define MAX_HEX_LINES   64000
41 #define HAVE_OCTASIC    1
42 #define DEF_SPAN_SPEC_FORMAT    "*:%c1" /* %c: 'E' or 'T' */
43
44 static char     *progname;
45
46 static void usage()
47 {
48         fprintf(stderr, "Usage: %s [options...] -D {/proc/bus/usb|/dev/bus/usb}/<bus>/<dev> hexfile...\n", progname);
49         fprintf(stderr, "\tOptions: {-F|-p}\n");
50         fprintf(stderr, "\t\t[-E]               # Burn to EEPROM\n");
51 #if HAVE_OCTASIC
52         fprintf(stderr, "\t\t[-O]               # Load Octasic firmware\n");
53         fprintf(stderr, "\t\t[-o]               # Show Octasic version\n");
54         fprintf(stderr, "\t\t[-S <pri-spec>]    # Set PRI type specification string\n");
55 #endif
56         fprintf(stderr, "\t\t[-F]               # Load FPGA firmware\n");
57         fprintf(stderr, "\t\t[-p]               # Load PIC firmware\n");
58         fprintf(stderr, "\t\t[-v]               # Increase verbosity\n");
59         fprintf(stderr, "\t\t[-A]               # Set A-Law for 1st module\n");
60         fprintf(stderr, "\t\t[-d mask]          # Debug mask (0xFF for everything)\n");
61         exit(1);
62 }
63
64 int handle_hexline(struct astribank_device *astribank, struct hexline *hexline)
65 {
66         uint16_t        len;
67         uint16_t        offset_dummy;
68         uint8_t         *data;
69         int             ret;
70
71         assert(hexline);
72         assert(astribank);
73         if(hexline->d.content.header.tt != TT_DATA) {
74                 DBG("Non data record type = %d\n", hexline->d.content.header.tt);
75                 return 0;
76         }
77         len = hexline->d.content.header.ll;
78         offset_dummy = hexline->d.content.header.offset;
79         data = hexline->d.content.tt_data.data;
80         if((ret = mpp_send_seg(astribank, data, offset_dummy, len)) < 0) {
81                 ERR("Failed hexfile send line: %d\n", ret);
82                 return -EINVAL;
83         }
84         return 0;
85 }
86
87 void print_parse_errors(int level, const char *msg, ...)
88 {
89         va_list ap;
90
91         if (verbose > level) {
92                 va_start (ap, msg);
93                 vfprintf (stderr, msg, ap);
94                 va_end (ap);
95         }
96 }
97
98 static int load_hexfile(struct astribank_device *astribank, const char *hexfile, enum dev_dest dest)
99 {
100         struct hexdata          *hexdata = NULL;
101         int                     finished = 0;
102         int                     ret;
103         unsigned                i;
104         char                    star[] = "+\\+|+/+-";
105         const char              *devstr;
106
107         parse_hexfile_set_reporting(print_parse_errors);
108         if((hexdata  = parse_hexfile(hexfile, MAX_HEX_LINES)) == NULL) {
109                 perror(hexfile);
110                 return -errno;
111         }
112         devstr = xusb_devpath(astribank->xusb);
113         INFO("%s [%s]: Loading %s Firmware: %s (version %s)\n",
114                 devstr,
115                 xusb_serial(astribank->xusb),
116                 dev_dest2str(dest),
117                 hexdata->fname, hexdata->version_info);
118         if((ret = mpp_send_start(astribank, dest, hexdata->version_info)) < 0) {
119                 ERR("%s: Failed hexfile send start: %d\n", devstr, ret);
120                 return ret;
121         }
122         for(i = 0; i < hexdata->maxlines; i++) {
123                 struct hexline  *hexline = hexdata->lines[i];
124
125                 if(!hexline)
126                         break;
127                 if(verbose > LOG_INFO) {
128                         printf("Sending: %4d%%    %c\r", (100 * i) / hexdata->last_line, star[i % sizeof(star)]);
129                         fflush(stdout);
130                 }
131                 if(finished) {
132                         ERR("%s: Extra data after End Of Data Record (line %d)\n", devstr, i);
133                         return 0;
134                 }
135                 if(hexline->d.content.header.tt == TT_EOF) {
136                         DBG("End of data\n");
137                         finished = 1;
138                         continue;
139                 }
140                 if((ret = handle_hexline(astribank, hexline)) < 0) {
141                         ERR("%s: Failed hexfile sending in lineno %d (ret=%d)\n", devstr, i, ret);;
142                         return ret;
143                 }
144         }
145         if(verbose > LOG_INFO) {
146                 putchar('\n');
147                 fflush(stdout);
148         }
149         if((ret = mpp_send_end(astribank)) < 0) {
150                 ERR("%s: Failed hexfile send end: %d\n", devstr, ret);
151                 return ret;
152         }
153 #if 0
154         fclose(fp);
155 #endif
156         free_hexdata(hexdata);
157         DBG("hexfile loaded successfully\n");
158         return 0;
159 }
160
161 int main(int argc, char *argv[])
162 {
163         char                    *devpath = NULL;
164         int                     opt_pic = 0;
165         int                     opt_echo = 0;
166         int                     opt_ecver = 0;
167 #if HAVE_OCTASIC
168         int                     opt_alaw = 0;
169         const char              *span_spec = NULL;
170         char                    def_span_spec[sizeof(DEF_SPAN_SPEC_FORMAT)];
171 #endif
172         int                     opt_dest = 0;
173         int                     opt_sum = 0;
174         enum dev_dest           dest = DEST_NONE;
175         const char              options[] = "vd:D:EFOopAS:";
176         int                     iface_num;
177         int                     ret;
178
179         progname = argv[0];
180         while (1) {
181                 int     c;
182
183                 c = getopt (argc, argv, options);
184                 if (c == -1)
185                         break;
186
187                 switch (c) {
188                         case 'D':
189                                 devpath = optarg;
190                                 break;
191                         case 'E':
192                                 if(dest != DEST_NONE) {
193                                         ERR("The -F and -E options are mutually exclusive.\n");
194                                         usage();
195                                 }
196                                 opt_dest++;
197                                 dest = DEST_EEPROM;
198                                 break;
199                         case 'F':
200                                 if(dest != DEST_NONE) {
201                                         ERR("The -F and -E options are mutually exclusive.\n");
202                                         usage();
203                                 }
204                                 opt_dest++;
205                                 dest = DEST_FPGA;
206                                 break;
207 #if HAVE_OCTASIC
208                         case 'O':
209                                 opt_echo = 1;
210                                 break;
211                         case 'o':
212                                 opt_ecver = 1;
213                                 break;
214                         case 'A':
215                                 opt_alaw = 1;
216                                 break;
217                         case 'S':
218                                 span_spec = optarg;
219                                 break;
220 #endif
221                         case 'p':
222                                 opt_pic = 1;
223                                 break;
224                         case 'v':
225                                 verbose++;
226                                 break;
227                         case 'd':
228                                 debug_mask = strtoul(optarg, NULL, 0);
229                                 break;
230                         case 'h':
231                         default:
232                                 ERR("Unknown option '%c'\n", c);
233                                 usage();
234                 }
235         }
236         opt_sum = opt_dest + opt_pic + opt_echo;
237         if(opt_sum > 1 || (opt_sum == 0 && opt_ecver == 0)) {
238                 ERR("The -F, -E"
239 #if HAVE_OCTASIC
240                         ", -O"
241 #endif
242                         " and -p options are mutually exclusive, if neither is used then -o should present\n");
243                 usage();
244         }
245         iface_num = (opt_dest) ? 1 : 0;
246         if(!opt_pic && !opt_ecver) {
247                 if(optind != argc - 1) {
248                         ERR("Got %d hexfile names (Need exactly one hexfile)\n",
249                                 argc - 1 - optind);
250                         usage();
251                 }
252         }
253         if(!devpath) {
254                 ERR("Missing device path.\n");
255                 usage();
256         }
257 # ifdef HAVE_OCTASIC
258         if (!span_spec) {
259                 snprintf(def_span_spec, sizeof(def_span_spec),
260                                 DEF_SPAN_SPEC_FORMAT, opt_alaw? 'E' : 'T');
261                 span_spec = def_span_spec;
262         }
263 #endif
264         if(opt_dest) {
265                 /*
266                  * MPP Interface
267                  */
268                 struct astribank_device *astribank;
269
270                 if((astribank = mpp_init(devpath, iface_num)) == NULL) {
271                         ERR("%s: Opening astribank failed\n", devpath);
272                         return 1;
273                 }
274                 //show_astribank_info(astribank);
275                 if(load_hexfile(astribank, argv[optind], dest) < 0) {
276                         ERR("%s: Loading firmware to %s failed\n", devpath, dev_dest2str(dest));
277                         return 1;
278                 }
279                 astribank_close(astribank, 0);
280         } else if(opt_pic || opt_echo || opt_ecver) {
281                 /*
282                  * XPP Interface
283                  */
284                 struct astribank_device *astribank;
285
286                 if((astribank = astribank_open(devpath, iface_num)) == NULL) {
287                         ERR("%s: Opening astribank failed\n", devpath);
288                         return 1;
289                 }
290                 //show_astribank_info(astribank);
291 #if HAVE_OCTASIC
292                 if (opt_ecver) {
293                         if((ret = echo_ver(astribank)) < 0) {
294                                 ERR("%s: Get Octasic version failed (Is Echo canceller card connected?)\n", devpath);
295                                 return 1;
296                         } else 
297                                 INFO("Octasic version: 0x%0X\n", ret);
298                 }
299 #endif
300                 if (opt_pic) {
301                         if ((ret = load_pic(astribank, argc - optind, argv + optind)) < 0) {
302                                 ERR("%s: Loading PIC's failed\n", devpath);
303                                 return 1;
304                         }
305 #if HAVE_OCTASIC
306                 } else if (opt_echo) {
307                         if((ret = load_echo(astribank, argv[optind], opt_alaw, span_spec)) < 0) {
308                                 ERR("%s: Loading ECHO's failed\n", devpath);
309                                 return 1;
310                         }
311 #endif
312                 }
313                 astribank_close(astribank, 0);
314         }
315         return 0;
316 }