configure.ac: add libusb/libusbx support (for xpp)
[dahdi/tools.git] / xpp / astribank_allow.c
1 /*
2  * Written by Oron Peled <oron@actcom.co.il> and
3  *            Alex Landau <alex.landau@xorcom.com>
4  * Copyright (C) 2008, Xorcom
5  *
6  * All rights reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <getopt.h>
30 #include <time.h>
31 #include <sys/types.h>
32 #include <arpa/inet.h>
33 #include <ctype.h>
34 #include "mpp.h"
35 #include "mpptalk.h"
36 #include <debug.h>
37 #include "astribank_license.h"
38
39 static const char rcsid[] = "$Id$";
40
41 #define DBG_MASK        0x80
42
43 static char     *progname;
44
45 static void usage()
46 {
47         fprintf(stderr, "Usage: %s [options...] -D {/proc/bus/usb|/dev/bus/usb}/<bus>/<dev> options\n", progname);
48         fprintf(stderr, "\tOptions:\n");
49         fprintf(stderr, "\t\t[-v]               # Increase verbosity\n");
50         fprintf(stderr, "\t\t[-d mask]          # Debug mask (0xFF for everything)\n");
51         fprintf(stderr, "\t\t[-w]               # Write capabilities to EEPROM, otherwise read capabilities\n");
52         fprintf(stderr, "\t\t[-f filename]      # License filename (stdin/stdout if not specified)\n\n");
53         fprintf(stderr, "\t\t[-m num]           # Numeric code of License markers to generate\n");
54         license_markers_help("\t", stderr);
55         exit(1);
56 }
57
58 static int capabilities_burn(
59                 struct astribank_device *astribank,
60                 struct eeprom_table *eeprom_table,
61                 struct capabilities *capabilities,
62                 struct capkey *key)
63 {
64         int     ret;
65
66         INFO("Burning capabilities\n");
67         ret = mpp_caps_set(astribank, eeprom_table, capabilities, key);
68         if(ret < 0) {
69                 ERR("Capabilities burning failed: %d\n", ret);
70                 return ret;
71         }
72         INFO("Done\n");
73         return 0;
74 }
75
76 int main(int argc, char *argv[])
77 {
78         char                    *devpath = NULL;
79         struct astribank_device *astribank;
80         struct eeprom_table     eeprom_table;
81         struct capabilities     caps;
82         struct capkey           key;
83         const char              options[] = "vd:D:wf:m:";
84         int                     do_write = 0;
85         unsigned int            marker = LICENSE_MARKER_GENERIC;
86         FILE                    *file;
87         char                    *filename = NULL;
88         int                     ret;
89
90         progname = argv[0];
91         while (1) {
92                 int     c;
93
94                 c = getopt (argc, argv, options);
95                 if (c == -1)
96                         break;
97
98                 switch (c) {
99                         case 'D':
100                                 devpath = optarg;
101                                 break;
102                         case 'v':
103                                 verbose++;
104                                 break;
105                         case 'd':
106                                 debug_mask = strtoul(optarg, NULL, 0);
107                                 break;
108                         case 'w':
109                                 do_write = 1;
110                                 break;
111                         case 'f':
112                                 filename = optarg;
113                                 break;
114                         case 'm':
115                                 marker = strtoul(optarg, NULL, 0);
116                                 if (!license_marker_valid(marker))
117                                         usage();
118                                 break;
119                         case 'h':
120                         default:
121                                 ERR("Unknown option '%c'\n", c);
122                                 usage();
123                 }
124         }
125         if(!devpath) {
126                 ERR("Missing device path\n");
127                 usage();
128         }
129         DBG("Startup %s\n", devpath);
130         if((astribank = mpp_init(devpath, 1)) == NULL) {
131                 ERR("Failed initializing MPP\n");
132                 return 1;
133         }
134         if(astribank->eeprom_type != EEPROM_TYPE_LARGE) {
135                 ERR("Cannot use this program with astribank EEPROM type %d (need %d)\n",
136                         astribank->eeprom_type, EEPROM_TYPE_LARGE);
137                 return 1;
138         }
139         ret = mpp_caps_get(astribank, &eeprom_table, &caps, &key);
140         if(ret < 0) {
141                 ERR("Failed to get original capabilities: %d\n", ret);
142                 return 1;
143         }
144         if (do_write) {
145                 unsigned int used_marker;
146                 /* update capabilities based on input file */
147                 file = stdin;
148                 if (filename) {
149                         file = fopen(filename, "r");
150                         if (file == NULL) {
151                                 ERR("Can't open file '%s'\n", filename);
152                                 return 1;
153                         }
154                 }
155                 ret = read_from_file(&eeprom_table, &caps, &key, &used_marker, file);
156                 if (ret < 0) {
157                         ERR("Failed to read capabilities from file: %d\n", ret);
158                         return 1;
159                 }
160                 show_capabilities(&caps, stderr);
161                 if (capabilities_burn(astribank, &eeprom_table, &caps, &key) < 0)
162                         return 1;
163                 if (file != stdin)
164                         fclose(file);
165         } else {
166                 /* print capabilities to stdout */
167                 file = stdout;
168                 if (filename) {
169                         file = fopen(filename, "w");
170                         if (file == NULL) {
171                                 ERR("Can't create file '%s'\n", filename);
172                                 return 1;
173                         }
174                 }
175                 ret = write_to_file(&eeprom_table, &caps, &key, marker, file);
176                 if (ret < 0) {
177                         ERR("Failed to write capabilities to file: %d\n", ret);
178                         return 1;
179                 }
180                 if (file != stdout)
181                         fclose(file);
182         }
183         mpp_exit(astribank);
184         return 0;
185 }