[go: up one dir, main page]

Menu

[4e312c]: / src / suffix.c  Maximize  Restore  History

Download this file

277 lines (248 with data), 6.3 kB

  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
/*
* (C) 2011 - 2012 Stefan Schmidt <stefan@datenfreihafen.org>
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <getopt.h>
#include <string.h>
#include "dfu_file.h"
#include "lmdfu.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_FTRUNCATE
# include <unistd.h>
#endif
enum mode {
MODE_NONE,
MODE_ADD,
MODE_DEL,
MODE_CHECK
};
enum lmdfu_mode {
LMDFU_NONE,
LMDFU_ADD,
LMDFU_DEL,
LMDFU_CHECK
};
static void help(void)
{
printf("Usage: dfu-suffix [options] <file>\n"
" -h --help\tPrint this help message\n"
" -V --version\tPrint the version number\n"
" -D --delete\tDelete DFU suffix from <file>\n"
" -p --pid\tAdd product ID into DFU suffix in <file>\n"
" -v --vid\tAdd vendor ID into DFU suffix in <file>\n"
" -d --did\tAdd device ID into DFU suffix in <file>\n"
" -c --check\tCheck DFU suffix of <file>\n"
" -a --add\tAdd DFU suffix to <file>\n"
);
printf( " -s --stellaris-address <address> Add TI Stellaris address "
"prefix to <file>,\n\t\tto be used together with -a\n"
" -T --stellaris Act on TI Stellaris extension prefix of "
"<file>, to be used\n\t\tin combination with -D or -c\n"
);
}
static void print_version(void)
{
printf("dfu-suffix (" PACKAGE ") " PACKAGE_VERSION "\n\n");
printf("(C) 2011-2012 Stefan Schmidt\n"
"This program is Free Software and has ABSOLUTELY NO WARRANTY\n"
"Please report bugs to " PACKAGE_BUGREPORT "\n\n");
}
static struct option opts[] = {
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'V' },
{ "delete", 1, 0, 'D' },
{ "pid", 1, 0, 'p' },
{ "vid", 1, 0, 'v' },
{ "did", 1, 0, 'd' },
{ "check", 1, 0, 'c' },
{ "add", 1, 0, 'a' },
{ "stellaris-address", 1, 0, 's' },
{ "stellaris", 0, 0, 'T' },
};
static int check_suffix(struct dfu_file *file) {
int ret;
ret = parse_dfu_suffix(file);
if (ret > 0) {
printf("The file %s contains a DFU suffix with the following properties:\n", file->name);
printf("BCD device:\t0x%04X\n", file->bcdDevice);
printf("Product ID:\t0x%04X\n",file->idProduct);
printf("Vendor ID:\t0x%04X\n", file->idVendor);
printf("BCD DFU:\t0x%04X\n", file->bcdDFU);
printf("Length:\t\t%i\n", file->suffixlen);
printf("CRC:\t\t0x%08X\n", file->dwCRC);
}
return ret;
}
static int remove_suffix(struct dfu_file *file)
{
int ret;
ret = parse_dfu_suffix(file);
if (ret <= 0)
return 0;
#ifdef HAVE_FTRUNCATE
/* There is no easy way to truncate to a size with stdio */
ret = ftruncate(fileno(file->filep),
(long) file->size - file->suffixlen);
if (ret < 0) {
perror("ftruncate");
exit(1);
}
printf("DFU suffix removed\n");
#else
printf("Suffix removal not implemented on this platform\n");
#endif /* HAVE_FTRUNCATE */
return 1;
}
static void add_suffix(struct dfu_file *file, int pid, int vid, int did) {
int ret;
file->idProduct = pid;
file->idVendor = vid;
file->bcdDevice = did;
ret = generate_dfu_suffix(file);
if (ret < 0) {
perror("generate");
exit(1);
}
printf("New DFU suffix added.\n");
}
int main(int argc, char **argv)
{
struct dfu_file file;
int pid, vid, did;
enum mode mode = MODE_NONE;
enum lmdfu_mode lmdfu_mode = LMDFU_NONE;
unsigned int lmdfu_flash_address=0;
int lmdfu_prefix=0;
char *end;
print_version();
pid = vid = did = 0xffff;
file.name = NULL;
while (1) {
int c, option_index = 0;
c = getopt_long(argc, argv, "hVD:p:v:d:c:a:s:T", opts,
&option_index);
if (c == -1)
break;
switch (c) {
case 'h':
help();
exit(0);
break;
case 'V':
exit(0);
break;
case 'D':
file.name = optarg;
mode = MODE_DEL;
break;
case 'p':
pid = strtol(optarg, NULL, 16);
break;
case 'v':
vid = strtol(optarg, NULL, 16);
break;
case 'd':
did = strtol(optarg, NULL, 16);
break;
case 'c':
file.name = optarg;
mode = MODE_CHECK;
break;
case 'a':
file.name = optarg;
mode = MODE_ADD;
break;
case 's':
lmdfu_mode = LMDFU_ADD;
lmdfu_flash_address = strtoul(optarg, &end, 0);
if (*end) {
fprintf(stderr, "Error: Invalid lmdfu "
"address: %s\n", optarg);
exit(2);
}
break;
case 'T':
lmdfu_mode = LMDFU_CHECK; /* or LMDFU_DEL */
break;
default:
help();
exit(2);
}
}
if(mode == MODE_DEL && lmdfu_mode == LMDFU_CHECK)
lmdfu_mode = LMDFU_DEL;
if (!file.name) {
fprintf(stderr, "You need to specify a filename\n");
help();
exit(2);
}
if (mode != MODE_NONE) {
file.filep = fopen(file.name, "r+b");
if (file.filep == NULL) {
perror(file.name);
exit(1);
}
}
switch(mode) {
case MODE_ADD:
if (check_suffix(&file)) {
if(lmdfu_prefix) lmdfu_check_prefix(&file);
printf("Please remove existing DFU suffix before adding a new one.\n");
exit(1);
}
if(lmdfu_mode == LMDFU_ADD) {
if(lmdfu_check_prefix(&file)) {
fprintf(stderr, "Adding new anyway\n");
}
lmdfu_add_prefix(file, lmdfu_flash_address);
}
add_suffix(&file, pid, vid, did);
break;
case MODE_CHECK:
/* FIXME: could open read-only here */
check_suffix(&file);
if(lmdfu_mode == LMDFU_CHECK)
lmdfu_check_prefix(&file);
break;
case MODE_DEL:
if(!remove_suffix(&file)) {
if(lmdfu_mode == LMDFU_DEL)
if (lmdfu_check_prefix(&file))
lmdfu_remove_prefix(&file);
exit(1);
}
break;
default:
help();
exit(2);
}
if(lmdfu_mode == LMDFU_DEL) {
if (check_suffix(&file)) {
fprintf(stderr, "DFU suffix exist. Remove suffix before using -T or use it with -D to delete suffix\n");
exit(1);
} else {
if(lmdfu_check_prefix(&file))
lmdfu_remove_prefix(&file);
}
}
fclose(file.filep);
exit(0);
}