2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
30 #include "stlink-common.h"
34 #include "ao-stmload.h"
35 #include "ao-selfload.h"
36 #include "ao-verbose.h"
37 #include "ao-editaltos.h"
41 * Read a 16-bit value from the target device with arbitrary
45 get_uint16_sl(stlink_t *sl, uint32_t addr)
47 const uint8_t *data = sl->q_buf;
55 actual_addr = addr & ~3;
57 stlink_read_mem32(sl, actual_addr, 8);
63 result = data[off] | (data[off + 1] << 8);
68 get_uint16(stlink_t *sl, uint32_t addr)
71 result = get_uint16_sl(sl, addr);
72 printf ("read 0x%08x = 0x%04x\n", addr, result);
77 * Read a 32-bit value from the target device with arbitrary
81 get_uint32_sl(stlink_t *sl, uint32_t addr)
83 const uint8_t *data = sl->q_buf;
90 printf ("read 0x%x\n", addr);
92 actual_addr = addr & ~3;
94 stlink_read_mem32(sl, actual_addr, 8);
100 result = data[off] | (data[off + 1] << 8) | (data[off+2] << 16) | (data[off+3] << 24);
105 * Read a 32-bit value from the target device with arbitrary
109 get_uint32(stlink_t *sl, uint32_t addr)
113 result = get_uint32_sl(sl, addr);
114 printf ("read 0x%08x = 0x%08x\n", addr, result);
119 * Check to see if the target device has been
120 * flashed with a similar firmware image before
122 * This is done by looking for the same romconfig version,
123 * which should be at the same location as the linker script
124 * places this at 0x100 from the start of the rom section
127 check_flashed(stlink_t *sl)
129 uint16_t romconfig_version = get_uint16(sl, AO_ROMCONFIG_VERSION);
130 uint16_t romconfig_check = get_uint16(sl, AO_ROMCONFIG_CHECK);
132 if (romconfig_version != (uint16_t) ~romconfig_check) {
133 fprintf (stderr, "Device has not been flashed before\n");
139 static const struct option options[] = {
140 { .name = "v1", .has_arg = 0, .val = '1' },
141 { .name = "raw", .has_arg = 0, .val = 'r' },
142 { .name = "cal", .has_arg = 1, .val = 'c' },
143 { .name = "serial", .has_arg = 1, .val = 's' },
144 { .name = "verbose", .has_arg = 1, .val = 'v' },
148 static void usage(char *program)
150 fprintf(stderr, "usage: %s [--v1] [--raw] [--verbose=<verbose>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
155 done(stlink_t *sl, int code)
159 stlink_exit_debug_mode(sl);
165 ends_with(char *whole, char *suffix)
167 int whole_len = strlen(whole);
168 int suffix_len = strlen(suffix);
170 if (suffix_len > whole_len)
172 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
176 main (int argc, char **argv)
183 unsigned int serial = 0;
196 struct ao_hex_image *load;
200 struct ao_sym *file_symbols;
201 int num_file_symbols;
203 while ((c = getopt_long(argc, argv, "1rc:s:v:", options, NULL)) != -1) {
212 cal = strtoul(optarg, &cal_end, 10);
213 if (cal_end == optarg || *cal_end != '\0')
217 serial = strtoul(optarg, &serial_end, 10);
218 if (serial_end == optarg || *serial_end != '\0')
230 ao_verbose = verbose;
233 ccdbg_add_debug(CC_DEBUG_BITBANG);
235 filename = argv[optind];
236 if (filename == NULL)
239 if (ends_with (filename, ".elf")) {
240 load = ao_load_elf(filename, &file_symbols, &num_file_symbols);
241 } else if (ends_with (filename, ".ihx")) {
242 load = ao_hex_load(filename, &file_symbols, &num_file_symbols);
247 if (!ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols)) {
248 fprintf(stderr, "Cannot find required symbols\n");
253 /* Connect to the programming dongle
256 for (tries = 0; tries < 3; tries++) {
258 sl = stlink_v1_open(50);
260 sl = stlink_open_usb(50);
264 fprintf (stderr, "No STLink devices present\n");
268 if (sl->chip_id != 0)
275 fprintf (stderr, "Debugger connection failed\n");
279 /* Verify that the loaded image fits entirely within device flash
281 if (load->address < sl->flash_base ||
282 sl->flash_base + sl->flash_size < load->address + load->length) {
283 fprintf (stderr, "\%s\": Invalid memory range 0x%08x - 0x%08x\n", filename,
284 load->address, load->address + load->length);
288 /* Enter debugging mode
290 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
291 stlink_exit_dfu_mode(sl);
293 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
294 stlink_enter_swd_mode(sl);
298 /* Go fetch existing config values
301 was_flashed = check_flashed(sl);
305 fprintf (stderr, "Must provide serial number\n");
308 serial = get_uint16(sl, AO_SERIAL_NUMBER);
309 if (!serial || serial == 0xffff) {
310 fprintf (stderr, "Invalid existing serial %d\n", serial);
315 if (!cal && AO_RADIO_CAL && was_flashed) {
316 cal = get_uint32(sl, AO_RADIO_CAL);
317 if (!cal || cal == 0xffffffff) {
318 fprintf (stderr, "Invalid existing rf cal %d\n", cal);
323 if (!ao_editaltos(load, serial, cal))
327 /* And flash the resulting image to the device
330 success = (stlink_write_flash(sl, load->address, load->data, load->length) >= 0);
333 fprintf (stderr, "\"%s\": Write failed\n", filename);