| 1 |
/* |
| 2 |
* dfu-suffix |
| 3 |
* |
| 4 |
* Copyright 2011-2012 Stefan Schmidt <[email protected]> |
| 5 |
* Copyright 2013 Hans Petter Selasky <[email protected]> |
| 6 |
* Copyright 2014-2020 Tormod Volden <[email protected]> |
| 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
# include "config.h" |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stdlib.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdint.h> |
| 30 |
#include <getopt.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#include "portable.h" |
| 34 |
#include "dfu_file.h" |
| 35 |
|
| 36 |
enum mode { |
| 37 |
MODE_NONE, |
| 38 |
MODE_ADD, |
| 39 |
MODE_DEL, |
| 40 |
MODE_CHECK |
| 41 |
}; |
| 42 |
|
| 43 |
int verbose; |
| 44 |
|
| 45 |
static void help(void) |
| 46 |
{ |
| 47 |
fprintf(stderr, "Usage: dfu-suffix [options] ...\n" |
| 48 |
" -h --help\t\t\tPrint this help message\n" |
| 49 |
" -V --version\t\t\tPrint the version number\n" |
| 50 |
" -c --check <file>\t\tCheck DFU suffix of <file>\n" |
| 51 |
" -a --add <file>\t\tAdd DFU suffix to <file>\n" |
| 52 |
" -D --delete <file>\t\tDelete DFU suffix from <file>\n" |
| 53 |
" -p --pid <productID>\t\tAdd product ID into DFU suffix in <file>\n" |
| 54 |
" -v --vid <vendorID>\t\tAdd vendor ID into DFU suffix in <file>\n" |
| 55 |
" -d --did <deviceID>\t\tAdd device ID into DFU suffix in <file>\n" |
| 56 |
" -S --spec <specID>\t\tAdd DFU specification ID into DFU suffix in <file>\n" |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
static void print_version(void) |
| 61 |
{ |
| 62 |
printf("dfu-suffix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION); |
| 63 |
printf("Copyright 2011-2012 Stefan Schmidt, 2013-2020 Tormod Volden\n" |
| 64 |
"This program is Free Software and has ABSOLUTELY NO WARRANTY\n" |
| 65 |
"Please report bugs to %s\n\n", PACKAGE_BUGREPORT); |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
static struct option opts[] = { |
| 70 |
{ "help", 0, 0, 'h' }, |
| 71 |
{ "version", 0, 0, 'V' }, |
| 72 |
{ "check", 1, 0, 'c' }, |
| 73 |
{ "add", 1, 0, 'a' }, |
| 74 |
{ "delete", 1, 0, 'D' }, |
| 75 |
{ "pid", 1, 0, 'p' }, |
| 76 |
{ "vid", 1, 0, 'v' }, |
| 77 |
{ "did", 1, 0, 'd' }, |
| 78 |
{ "spec", 1, 0, 'S' }, |
| 79 |
{ 0, 0, 0, 0 } |
| 80 |
}; |
| 81 |
|
| 82 |
int main(int argc, char **argv) |
| 83 |
{ |
| 84 |
struct dfu_file file; |
| 85 |
int pid, vid, did, spec; |
| 86 |
enum mode mode = MODE_NONE; |
| 87 |
|
| 88 |
/* make sure all prints are flushed */ |
| 89 |
setvbuf(stdout, NULL, _IONBF, 0); |
| 90 |
|
| 91 |
print_version(); |
| 92 |
|
| 93 |
pid = vid = did = 0xffff; |
| 94 |
spec = 0x0100; /* Default to bcdDFU version 1.0 */ |
| 95 |
memset(&file, 0, sizeof(file)); |
| 96 |
|
| 97 |
while (1) { |
| 98 |
int c, option_index = 0; |
| 99 |
c = getopt_long(argc, argv, "hVc:a:D:p:v:d:S:s:T", opts, |
| 100 |
&option_index); |
| 101 |
if (c == -1) |
| 102 |
break; |
| 103 |
|
| 104 |
switch (c) { |
| 105 |
case 'h': |
| 106 |
help(); |
| 107 |
exit(EX_OK); |
| 108 |
break; |
| 109 |
case 'V': |
| 110 |
exit(EX_OK); |
| 111 |
break; |
| 112 |
case 'D': |
| 113 |
file.name = optarg; |
| 114 |
mode = MODE_DEL; |
| 115 |
break; |
| 116 |
case 'p': |
| 117 |
pid = strtol(optarg, NULL, 16); |
| 118 |
break; |
| 119 |
case 'v': |
| 120 |
vid = strtol(optarg, NULL, 16); |
| 121 |
break; |
| 122 |
case 'd': |
| 123 |
did = strtol(optarg, NULL, 16); |
| 124 |
break; |
| 125 |
case 'S': |
| 126 |
spec = strtol(optarg, NULL, 16); |
| 127 |
break; |
| 128 |
case 'c': |
| 129 |
file.name = optarg; |
| 130 |
mode = MODE_CHECK; |
| 131 |
break; |
| 132 |
case 'a': |
| 133 |
file.name = optarg; |
| 134 |
mode = MODE_ADD; |
| 135 |
break; |
| 136 |
default: |
| 137 |
help(); |
| 138 |
exit(EX_USAGE); |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if (!file.name) { |
| 144 |
fprintf(stderr, "You need to specify a filename\n"); |
| 145 |
help(); |
| 146 |
exit(EX_USAGE); |
| 147 |
} |
| 148 |
|
| 149 |
if (spec != 0x0100 && spec != 0x011a) { |
| 150 |
fprintf(stderr, "Only DFU specification 0x0100 and 0x011a supported\n"); |
| 151 |
help(); |
| 152 |
exit(EX_USAGE); |
| 153 |
} |
| 154 |
|
| 155 |
switch(mode) { |
| 156 |
case MODE_ADD: |
| 157 |
dfu_load_file(&file, NO_SUFFIX, MAYBE_PREFIX); |
| 158 |
file.idVendor = vid; |
| 159 |
file.idProduct = pid; |
| 160 |
file.bcdDevice = did; |
| 161 |
file.bcdDFU = spec; |
| 162 |
/* always write suffix, rewrite prefix if there was one */ |
| 163 |
dfu_store_file(&file, 1, file.size.prefix != 0); |
| 164 |
printf("Suffix successfully added to file\n"); |
| 165 |
break; |
| 166 |
|
| 167 |
case MODE_CHECK: |
| 168 |
dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); |
| 169 |
show_suffix_and_prefix(&file); |
| 170 |
break; |
| 171 |
|
| 172 |
case MODE_DEL: |
| 173 |
dfu_load_file(&file, NEEDS_SUFFIX, MAYBE_PREFIX); |
| 174 |
dfu_store_file(&file, 0, file.size.prefix != 0); |
| 175 |
if (file.size.suffix) /* had a suffix */ |
| 176 |
printf("Suffix successfully removed from file\n"); |
| 177 |
break; |
| 178 |
|
| 179 |
default: |
| 180 |
help(); |
| 181 |
exit(EX_USAGE); |
| 182 |
break; |
| 183 |
} |
| 184 |
return EX_OK; |
| 185 |
} |