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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 #include "stlink-common.h"
33 #include "ao-stmload.h"
34 #include "ao-selfload.h"
35 #include "ao-verbose.h"
36 #include "ao-editaltos.h"
40 * Read a 16-bit value from the target device with arbitrary
44 get_uint16_sl(stlink_t *sl, uint32_t addr)
46 const uint8_t *data = sl->q_buf;
54 actual_addr = addr & ~3;
56 stlink_read_mem32(sl, actual_addr, 8);
62 result = data[off] | (data[off + 1] << 8);
67 get_uint16(stlink_t *sl, uint32_t addr)
70 result = get_uint16_sl(sl, addr);
71 printf ("read 0x%08x = 0x%04x\n", addr, result);
76 * Read a 32-bit value from the target device with arbitrary
80 get_uint32_sl(stlink_t *sl, uint32_t addr)
82 const uint8_t *data = sl->q_buf;
89 printf ("read 0x%x\n", addr);
91 actual_addr = addr & ~3;
93 stlink_read_mem32(sl, actual_addr, 8);
99 result = data[off] | (data[off + 1] << 8) | (data[off+2] << 16) | (data[off+3] << 24);
104 * Read a 32-bit value from the target device with arbitrary
108 get_uint32(stlink_t *sl, uint32_t addr)
112 result = get_uint32_sl(sl, addr);
113 printf ("read 0x%08x = 0x%08x\n", addr, result);
118 * Check to see if the target device has been
119 * flashed with a similar firmware image before
121 * This is done by looking for the same romconfig version,
122 * which should be at the same location as the linker script
123 * places this at 0x100 from the start of the rom section
126 check_flashed(stlink_t *sl)
128 uint16_t romconfig_version = get_uint16(sl, AO_ROMCONFIG_VERSION);
129 uint16_t romconfig_check = get_uint16(sl, AO_ROMCONFIG_CHECK);
131 if (romconfig_version != (uint16_t) ~romconfig_check) {
132 fprintf (stderr, "Device has not been flashed before\n");
138 static const struct option options[] = {
139 { .name = "v1", .has_arg = 0, .val = '1' },
140 { .name = "raw", .has_arg = 0, .val = 'r' },
141 { .name = "cal", .has_arg = 1, .val = 'c' },
142 { .name = "serial", .has_arg = 1, .val = 's' },
143 { .name = "verbose", .has_arg = 1, .val = 'v' },
147 static void usage(char *program)
149 fprintf(stderr, "usage: %s [--v1] [--raw] [--verbose=<verbose>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
154 done(stlink_t *sl, int code)
158 stlink_exit_debug_mode(sl);
164 ends_with(char *whole, char *suffix)
166 int whole_len = strlen(whole);
167 int suffix_len = strlen(suffix);
169 if (suffix_len > whole_len)
171 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
175 main (int argc, char **argv)
182 unsigned int serial = 0;
195 struct ao_hex_image *load;
199 struct ao_sym *file_symbols;
200 int num_file_symbols;
202 while ((c = getopt_long(argc, argv, "1rc:s:v:", options, NULL)) != -1) {
211 cal = strtoul(optarg, &cal_end, 10);
212 if (cal_end == optarg || *cal_end != '\0')
216 serial = strtoul(optarg, &serial_end, 10);
217 if (serial_end == optarg || *serial_end != '\0')
229 ao_verbose = verbose;
232 ccdbg_add_debug(CC_DEBUG_BITBANG);
234 filename = argv[optind];
235 if (filename == NULL)
238 if (ends_with (filename, ".elf")) {
239 load = ao_load_elf(filename, &file_symbols, &num_file_symbols);
240 } else if (ends_with (filename, ".ihx")) {
241 load = ao_hex_load(filename, &file_symbols, &num_file_symbols);
246 if (!ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols)) {
247 fprintf(stderr, "Cannot find required symbols\n");
252 /* Connect to the programming dongle
255 for (tries = 0; tries < 3; tries++) {
257 sl = stlink_v1_open(50);
259 sl = stlink_open_usb(50);
263 fprintf (stderr, "No STLink devices present\n");
267 if (sl->chip_id != 0)
274 fprintf (stderr, "Debugger connection failed\n");
278 /* Verify that the loaded image fits entirely within device flash
280 if (load->address < sl->flash_base ||
281 sl->flash_base + sl->flash_size < load->address + load->length) {
282 fprintf (stderr, "\%s\": Invalid memory range 0x%08x - 0x%08x\n", filename,
283 load->address, load->address + load->length);
287 /* Enter debugging mode
289 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
290 stlink_exit_dfu_mode(sl);
292 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
293 stlink_enter_swd_mode(sl);
297 /* Go fetch existing config values
300 was_flashed = check_flashed(sl);
304 fprintf (stderr, "Must provide serial number\n");
307 serial = get_uint16(sl, AO_SERIAL_NUMBER);
308 if (!serial || serial == 0xffff) {
309 fprintf (stderr, "Invalid existing serial %d\n", serial);
314 if (!cal && AO_RADIO_CAL && was_flashed) {
315 cal = get_uint32(sl, AO_RADIO_CAL);
316 if (!cal || cal == 0xffffffff) {
317 fprintf (stderr, "Invalid existing rf cal %d\n", cal);
322 if (!ao_editaltos(load, serial, cal))
326 /* And flash the resulting image to the device
329 success = (stlink_write_flash(sl, load->address, load->data, load->length) >= 0);
332 fprintf (stderr, "\"%s\": Write failed\n", filename);