[go: up one dir, main page]

Menu

[873111]: / src / suffix.c  Maximize  Restore  History

Download this file

218 lines (191 with data), 4.7 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
/*
* (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 "dfu_file.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
};
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"
);
}
static void print_version(void)
{
printf("dfu-suffix %s\n\n", VERSION);
printf("(C) 2011-2012 Stefan Schmidt\n"
"This program is Free Software and has ABSOLUTELY NO WARRANTY\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' },
};
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 void remove_suffix(struct dfu_file *file)
{
int ret;
ret = parse_dfu_suffix(file);
if (ret <= 0)
exit(1);
#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 */
}
static void add_suffix(struct dfu_file *file, int pid, int vid, int did) {
int ret;
ret = check_suffix(file);
if (ret > 0) {
printf("Please remove existing DFU suffix before adding a new one.\n");
exit(1);
}
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;
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:", 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;
default:
help();
exit(2);
}
}
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:
add_suffix(&file, pid, vid, did);
break;
case MODE_CHECK:
/* FIXME: could open read-only here */
check_suffix(&file);
break;
case MODE_DEL:
remove_suffix(&file);
break;
default:
help();
exit(2);
}
fclose(file.filep);
exit(0);
}