Debian Codegraph @

src/prefix.c

raw
1 /*
2 * dfu-prefix
3 *
4 * Copyright 2011-2012 Stefan Schmidt <[email protected]>
5 * Copyright 2013 Hans Petter Selasky <[email protected]>
6 * Copyright 2014 Uwe Bonnes <[email protected]>
7 * Copyright 2014-2020 Tormod Volden <[email protected]>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <getopt.h>
32 #include <string.h>
33
34 #include "portable.h"
35 #include "dfu_file.h"
36
37 enum mode {
38 MODE_NONE,
39 MODE_ADD,
40 MODE_DEL,
41 MODE_CHECK
42 };
43
44 int verbose;
45
46 static void help(void)
47 {
48 fprintf(stderr, "Usage: dfu-prefix [options] ...\n"
49 " -h --help\t\t\tPrint this help message\n"
50 " -V --version\t\t\tPrint the version number\n"
51 " -c --check <file>\t\tCheck DFU prefix of <file>\n"
52 " -D --delete <file>\t\tDelete DFU prefix from <file>\n"
53 " -a --add <file>\t\tAdd DFU prefix to <file>\n"
54 "In combination with -a:\n"
55 );
56 fprintf(stderr, " -s --stellaris-address <address> Add TI Stellaris address prefix to <file>\n"
57 "In combination with -D or -c:\n"
58 " -T --stellaris\t\tAct on TI Stellaris address prefix of <file>\n"
59 "In combination with -a or -D or -c:\n"
60 " -L --lpc-prefix\t\tUse NXP LPC DFU prefix format\n"
61 );
62 }
63
64 static void print_version(void)
65 {
66 printf("dfu-prefix (%s) %s\n\n", PACKAGE, PACKAGE_VERSION);
67 printf("Copyright 2011-2012 Stefan Schmidt, 2014 Uwe Bonnes, 2014-2020 Tormod Volden\n"
68 "This program is Free Software and has ABSOLUTELY NO WARRANTY\n"
69 "Please report bugs to %s\n\n", PACKAGE_BUGREPORT);
70
71 }
72
73 static struct option opts[] = {
74 { "help", 0, 0, 'h' },
75 { "version", 0, 0, 'V' },
76 { "check", 1, 0, 'c' },
77 { "add", 1, 0, 'a' },
78 { "delete", 1, 0, 'D' },
79 { "stellaris-address", 1, 0, 's' },
80 { "stellaris", 0, 0, 'T' },
81 { "LPC", 0, 0, 'L' },
82 { 0, 0, 0, 0 }
83 };
84 int main(int argc, char **argv)
85 {
86 struct dfu_file file;
87 enum mode mode = MODE_NONE;
88 enum prefix_type type = ZERO_PREFIX;
89 uint32_t lmdfu_flash_address = 0;
90 char *end;
91
92 /* make sure all prints are flushed */
93 setvbuf(stdout, NULL, _IONBF, 0);
94
95 print_version();
96
97 memset(&file, 0, sizeof(file));
98
99 while (1) {
100 int c, option_index = 0;
101 c = getopt_long(argc, argv, "hVc:a:D:p:v:d:s:TL", opts,
102 &option_index);
103 if (c == -1)
104 break;
105
106 switch (c) {
107 case 'h':
108 help();
109 exit(EX_OK);
110 break;
111 case 'V':
112 exit(EX_OK);
113 break;
114 case 'D':
115 file.name = optarg;
116 mode = MODE_DEL;
117 break;
118 case 'c':
119 file.name = optarg;
120 mode = MODE_CHECK;
121 break;
122 case 'a':
123 file.name = optarg;
124 mode = MODE_ADD;
125 break;
126 case 's':
127 lmdfu_flash_address = strtoul(optarg, &end, 0);
128 if (*end) {
129 errx(EX_USAGE, "Invalid lmdfu "
130 "address: %s", optarg);
131 }
132 /* fall-through */
133 case 'T':
134 type = LMDFU_PREFIX;
135 break;
136 case 'L':
137 type = LPCDFU_UNENCRYPTED_PREFIX;
138 break;
139 default:
140 help();
141 exit(EX_USAGE);
142 break;
143 }
144 }
145
146 if (!file.name) {
147 fprintf(stderr, "You need to specify a filename\n");
148 help();
149 exit(EX_USAGE);
150 }
151
152 switch(mode) {
153 case MODE_ADD:
154 if (type == ZERO_PREFIX)
155 errx(EX_USAGE, "Prefix type must be specified");
156 dfu_load_file(&file, MAYBE_SUFFIX, NO_PREFIX);
157 file.lmdfu_address = lmdfu_flash_address;
158 file.prefix_type = type;
159 printf("Adding prefix to file\n");
160 dfu_store_file(&file, file.size.suffix != 0, 1);
161 break;
162
163 case MODE_CHECK:
164 dfu_load_file(&file, MAYBE_SUFFIX, MAYBE_PREFIX);
165 show_suffix_and_prefix(&file);
166 if (type > ZERO_PREFIX && file.prefix_type != type)
167 errx(EX_DATAERR, "No prefix of requested type");
168 break;
169
170 case MODE_DEL:
171 dfu_load_file(&file, MAYBE_SUFFIX, NEEDS_PREFIX);
172 if (type > ZERO_PREFIX && file.prefix_type != type)
173 errx(EX_DATAERR, "No prefix of requested type");
174 printf("Removing prefix from file\n");
175 /* if there was a suffix, rewrite it */
176 dfu_store_file(&file, file.size.suffix != 0, 0);
177 break;
178
179 default:
180 help();
181 exit(EX_USAGE);
182 break;
183 }
184 return EX_OK;
185 }