-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.c
293 lines (250 loc) · 7.71 KB
/
main.c
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
/*
* Copyright (c) 2001 Stephen Williams ([email protected])
* Copyright (c) 2001-2002 David Brownell ([email protected])
* Copyright (c) 2008 Roger Williams ([email protected])
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
/*
* This program supports loading firmware into a target USB device
* that is discovered and referenced by the hotplug usb agent. It can
* also do other useful things, like set the permissions of the device
* and create a symbolic link for the benefit of applications that are
* looking for the device.
*
* -I <path> -- Download this firmware (intel hex)
* -t <type> -- uController type: an21, fx, fx2, fx2lp
* -s <path> -- use this second stage loader
* -c <byte> -- Download to EEPROM, with this config byte
*
* -L <path> -- Create a symbolic link to the device.
* -m <mode> -- Set the permissions on the device after download.
* -D <path> -- Use this device, instead of $DEVICE
*
* -V -- Print version ID for program
*
* This program is intended to be started by hotplug scripts in
* response to a device appearing on the bus. It therefore also
* expects these environment variables which are passed by hotplug to
* its sub-scripts:
*
* DEVICE=<path>
* This is the path to the device is /proc/bus/usb. It is the
* complete path to the device, that I can pass to open and
* manipulate as a USB device.
*/
# include <stdlib.h>
# include <stdio.h>
# include <getopt.h>
# include <string.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include "ezusb.h"
#ifndef FXLOAD_VERSION
# define FXLOAD_VERSION (__DATE__ " (development)")
#endif
#include <errno.h>
#include <syslog.h>
#include <stdarg.h>
static int dosyslog=0;
void logerror(const char *format, ...)
__attribute__ ((format (__printf__, 1, 2)));
void logerror(const char *format, ...)
{
va_list ap;
va_start(ap, format);
if(dosyslog)
vsyslog(LOG_ERR, format, ap);
else
vfprintf(stderr, format, ap);
va_end(ap);
}
int main(int argc, char*argv[])
{
const char *link_path = 0;
const char *ihex_path = 0;
const char *device_path = getenv("DEVICE");
const char *type = 0;
const char *stage1 = 0;
mode_t mode = 0;
int opt;
int config = -1;
int do_erase = 0;
int ww_config_vid=-1,ww_config_pid=-1;
while ((opt = getopt (argc, argv, "2vVE?D:I:L:c:lm:s:t:d:")) != EOF)
switch (opt) {
case '2': // original version of "-t fx2"
type = "fx2";
break;
case 'D':
device_path = optarg;
break;
case 'I':
ihex_path = optarg;
break;
case 'L':
link_path = optarg;
break;
case 'V':
puts (FXLOAD_VERSION);
return 0;
case 'c':
config = strtoul (optarg, 0, 0);
if (config < 0 || config > 255) {
logerror("illegal config byte: %s\n", optarg);
goto usage;
}
break;
case 'l':
openlog(argv[0], LOG_CONS|LOG_NOWAIT|LOG_PERROR, LOG_USER);
dosyslog=1;
break;
case 'm':
mode = strtoul(optarg,0,0);
mode &= 0777;
break;
case 's':
stage1 = optarg;
break;
case 't':
if (strcmp (optarg, "an21") // original AnchorChips parts
&& strcmp (optarg, "fx") // updated Cypress versions
&& strcmp (optarg, "fx2") // Cypress USB 2.0 versions
&& strcmp (optarg, "fx2lp") // updated FX2
) {
logerror("illegal microcontroller type: %s\n", optarg);
goto usage;
}
type = optarg;
break;
case 'v':
verbose++;
break;
case 'E':
do_erase=1;
break;
case 'd':
sscanf(optarg,"%x%*c%x",&ww_config_vid,&ww_config_pid);
break;
case '?':
default:
goto usage;
}
if (config >= 0) {
if (type == 0) {
logerror("must specify microcontroller type %s",
"to write EEPROM!\n");
goto usage;
}
if (!stage1){
logerror("need 2nd stage loader %s",
"to write EEPROM!\n");
goto usage;
}
if (!ihex_path && (!ww_config_vid || !ww_config_pid)) {
logerror("firmware or VID:PID %s",
"to write EEPROM!\n");
goto usage;
}
if (link_path || mode) {
logerror("links and modes not set up when writing EEPROM\n");
goto usage;
}
}
if (!device_path) {
logerror("no device specified!\n");
usage:
fputs ("usage: ", stderr);
fputs (argv [0], stderr);
fputs (" [-vVE] [-l] [-t type] [-D devpath]\n", stderr);
fputs ("\t\t[-I firmware_hexfile] ", stderr);
fputs ("[-s loader] [-c config_byte] [-d VID:PID]\n", stderr);
fputs ("\t\t[-L link] [-m mode]\n", stderr);
fputs ("... [-D devpath] overrides DEVICE= in env\n", stderr);
fputs ("... device types: one of an21, fx, fx2, fx2lp\n", stderr);
fputs ("... at least one of -I, -L, -m, -E is required\n", stderr);
fputs ("options -c and -d affect only EEPROM content\n", stderr);
return -1;
}
if (ihex_path || do_erase || (ww_config_vid && ww_config_pid)) {
int fd = open(device_path, O_RDWR);
int status;
int fx2;
if (fd == -1) {
logerror("%s : %s\n", strerror(errno), device_path);
return -1;
}
if (type == 0) {
type = "fx"; /* an21-compatible for most purposes */
fx2 = 0;
} else if (strcmp (type, "fx2lp") == 0)
fx2 = 2;
else
fx2 = (strcmp (type, "fx2") == 0);
if (verbose)
logerror("microcontroller type: %s\n", type);
if (stage1) {
/* first stage: put loader into internal memory */
if (verbose)
logerror("1st stage: load 2nd stage loader\n");
status = ezusb_load_ram (fd, stage1, fx2, 0);
if (status != 0)
return status;
/* second stage ... write either EEPROM, or RAM. */
if(do_erase)
status = ezusb_erase_eeprom(fd);
else if (config >= 0)
status = ezusb_load_eeprom (fd, ihex_path, type, config,
ww_config_vid,ww_config_pid);
else
status = ezusb_load_ram (fd, ihex_path, fx2, 1);
if (status != 0)
return status;
} else {
/* single stage, put into internal memory */
if (verbose)
logerror("single stage: load on-chip memory\n");
status = ezusb_load_ram (fd, ihex_path, fx2, 0);
if (status != 0)
return status;
}
/* some firmware won't renumerate, but typically it will.
* link and chmod only make sense without renumeration...
*/
}
if (link_path) {
int rc = unlink(link_path);
rc = symlink(device_path, link_path);
if (rc == -1) {
logerror("%s : %s\n", strerror(errno), link_path);
return -1;
}
}
if (mode != 0) {
int rc = chmod(device_path, mode);
if (rc == -1) {
logerror("%s : %s\n", strerror(errno), link_path);
return -1;
}
}
if (!ihex_path && !link_path && !mode && !do_erase && (!ww_config_vid || !ww_config_pid)) {
logerror("missing request! (firmware, link, mode, erase or device id)\n");
return -1;
}
return 0;
}