forked from ataradov/edbg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedbg.c
420 lines (341 loc) · 11.1 KB
/
edbg.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* Copyright (c) 2013-2015, Alex Taradov <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*- Includes ----------------------------------------------------------------*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "target.h"
#include "edbg.h"
#include "dap.h"
#include "dbg.h"
/*- Definitions -------------------------------------------------------------*/
#define VERSION "v0.5"
#define MAX_DEBUGGERS 20
#define DAP_FREQ 16000000 // Hz
#ifndef O_BINARY
#define O_BINARY 0
#endif
/*- Types -------------------------------------------------------------------*/
/*- Variables ---------------------------------------------------------------*/
static const struct option long_options[] =
{
{ "help", no_argument, 0, 'h' },
{ "verbose", no_argument, 0, 'b' },
{ "erase", no_argument, 0, 'e' },
{ "program", no_argument, 0, 'p' },
{ "verify", no_argument, 0, 'v' },
{ "lock", no_argument, 0, 'k' },
{ "read", no_argument, 0, 'r' },
{ "file", required_argument, 0, 'f' },
{ "target", required_argument, 0, 't' },
{ "list", no_argument, 0, 'l' },
{ "serial", required_argument, 0, 's' },
{ "offset", required_argument, 0, 'o' },
{ "size", required_argument, 0, 'z' },
{ 0, 0, 0, 0 }
};
static const char *short_options = "hbepvkrf:t:ls:o:z:";
static char *g_serial = NULL;
static bool g_list = false;
static char *g_target = NULL;
static bool g_verbose = false;
static target_options_t g_target_options =
{
.erase = false,
.program = false,
.verify = false,
.lock = false,
.read = false,
.name = NULL,
.offset = -1,
.size = -1,
};
/*- Implementations ---------------------------------------------------------*/
//-----------------------------------------------------------------------------
void verbose(char *fmt, ...)
{
va_list args;
if (g_verbose)
{
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
fflush(stdout);
}
}
//-----------------------------------------------------------------------------
void warning(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "Warning: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
//-----------------------------------------------------------------------------
void check(bool cond, char *fmt, ...)
{
if (!cond)
{
va_list args;
dbg_close();
va_start(args, fmt);
fprintf(stderr, "Error: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
exit(1);
}
}
//-----------------------------------------------------------------------------
void error_exit(char *fmt, ...)
{
va_list args;
dbg_close();
va_start(args, fmt);
fprintf(stderr, "Error: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
exit(1);
}
//-----------------------------------------------------------------------------
void perror_exit(char *text)
{
dbg_close();
perror(text);
exit(1);
}
//-----------------------------------------------------------------------------
void sleep_ms(int ms)
{
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, NULL);
}
//-----------------------------------------------------------------------------
void *buf_alloc(int size)
{
void *buf;
if (NULL == (buf = malloc(size)))
error_exit("out of memory");
return buf;
}
//-----------------------------------------------------------------------------
void buf_free(void *buf)
{
free(buf);
}
//-----------------------------------------------------------------------------
int load_file(char *name, uint8_t *data, int size)
{
struct stat stat;
int fd, rsize;
check(NULL != name, "input file name is not specified");
fd = open(name, O_RDONLY | O_BINARY);
if (fd < 0)
perror_exit("open()");
fstat(fd, &stat);
if (stat.st_size < size)
size = stat.st_size;
rsize = read(fd, data, size);
if (rsize < 0)
perror_exit("read()");
check(rsize == size, "cannot fully read file");
close(fd);
return rsize;
}
//-----------------------------------------------------------------------------
void save_file(char *name, uint8_t *data, int size)
{
int fd, rsize;
check(NULL != name, "output file name is not specified");
fd = open(name, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0644);
if (fd < 0)
perror_exit("open()");
rsize = write(fd, data, size);
if (rsize < 0)
perror_exit("write()");
check(rsize == size, "error writing the file");
close(fd);
}
//-----------------------------------------------------------------------------
static void print_help(char *name)
{
printf("CMSIS-DAP SWD programmer " VERSION ", built " __DATE__ " " __TIME__ " \n");
printf("Usage: %s [options]\n", name);
printf("Options:\n");
printf(" -h, --help print this help message and exit\n");
printf(" -b, --verbose print verbose messages\n");
printf(" -e, --erase perform a chip erase before programming\n");
printf(" -p, --program program the chip\n");
printf(" -v, --verify verify memory\n");
printf(" -k, --lock lock the chip (set security bit)\n");
printf(" -r, --read read the whole content of the chip flash\n");
printf(" -f, --file <file> binary file to be programmed or verified\n");
printf(" -t, --target <name> specify a target type (use '-t list' for a list of supported target types)\n");
printf(" -l, --list list all available debuggers\n");
printf(" -s, --serial <number> use a debugger with a specified serial number\n");
printf(" -o, --offset <number> offset for the operation\n");
printf(" -z, --size <number> size for the operation\n");
exit(0);
}
//-----------------------------------------------------------------------------
static void parse_command_line(int argc, char **argv)
{
int option_index = 0;
int c;
while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1)
{
switch (c)
{
case 'h': print_help(argv[0]); break;
case 'e': g_target_options.erase = true; break;
case 'p': g_target_options.program = true; break;
case 'v': g_target_options.verify = true; break;
case 'k': g_target_options.lock = true; break;
case 'r': g_target_options.read = true; break;
case 'f': g_target_options.name = optarg; break;
case 't': g_target = optarg; break;
case 'l': g_list = true; break;
case 's': g_serial = optarg; break;
case 'b': g_verbose = true; break;
case 'o': g_target_options.offset = (uint32_t)strtoul(optarg, NULL, 0); break;
case 'z': g_target_options.size = (uint32_t)strtoul(optarg, NULL, 0); break;
default: exit(1); break;
}
}
check(optind >= argc, "malformed command line, use '-h' for more information");
}
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
debugger_t debuggers[MAX_DEBUGGERS];
int n_debuggers = 0;
int debugger = -1;
target_t *target;
parse_command_line(argc, argv);
if (!(g_target_options.erase || g_target_options.program || g_target_options.verify ||
g_target_options.lock || g_target_options.read || g_list || g_target))
error_exit("no actions specified");
if (g_target_options.read && (g_target_options.erase || g_target_options.program ||
g_target_options.verify || g_target_options.lock))
error_exit("mutually exclusive actions specified");
n_debuggers = dbg_enumerate(debuggers, MAX_DEBUGGERS);
if (g_list)
{
printf("Attached debuggers:\n");
for (int i = 0; i < n_debuggers; i++)
printf(" %s - %s %s\n", debuggers[i].serial, debuggers[i].manufacturer, debuggers[i].product);
return 0;
}
if (NULL == g_target)
error_exit("no target type specified (use '-t' option)");
if (0 == strcmp("list", g_target))
{
target_list();
return 0;
}
target = target_get_ops(g_target);
if (g_serial)
{
for (int i = 0; i < n_debuggers; i++)
{
if (0 == strcmp(debuggers[i].serial, g_serial))
{
debugger = i;
break;
}
}
if (-1 == debugger)
error_exit("unable to find a debugger with a specified serial number");
}
if (0 == n_debuggers)
error_exit("no debuggers found");
else if (1 == n_debuggers)
debugger = 0;
else if (n_debuggers > 1 && -1 == debugger)
error_exit("more than one debugger found, please specify a serial number");
dbg_open(&debuggers[debugger]);
dap_disconnect();
dap_get_debugger_info();
dap_connect();
dap_transfer_configure(0, 128, 128);
dap_swd_configure(0);
dap_led(0, 1);
dap_reset_link();
dap_swj_clock(DAP_FREQ);
dap_target_prepare();
target->ops->select(&g_target_options);
if (g_target_options.erase)
{
verbose("Erasing... ");
target->ops->erase();
verbose(" done.\n");
}
if (g_target_options.program)
{
verbose("Programming...");
target->ops->program();
verbose(" done.\n");
}
if (g_target_options.verify)
{
verbose("Verification...");
target->ops->verify();
verbose(" done.\n");
}
if (g_target_options.lock)
{
verbose("Locking... ");
target->ops->lock();
verbose(" done.\n");
}
if (g_target_options.read)
{
verbose("Reading...");
target->ops->read();
verbose(" done.\n");
}
target->ops->deselect();
dap_disconnect();
dap_led(0, 0);
dbg_close();
return 0;
}