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.
28 #include "stlink-common.h"
30 #define AO_USB_DESC_STRING 3
38 { 0, "ao_romconfig_version", 1 },
39 #define AO_ROMCONFIG_VERSION (ao_symbols[0].addr)
41 { 0, "ao_romconfig_check", 1 },
42 #define AO_ROMCONFIG_CHECK (ao_symbols[1].addr)
44 { 0, "ao_serial_number", 1 },
45 #define AO_SERIAL_NUMBER (ao_symbols[2].addr)
47 { 0, "ao_usb_descriptors", 0 },
48 #define AO_USB_DESCRIPTORS (ao_symbols[3].addr)
50 { 0, "ao_radio_cal", 0 },
51 #define AO_RADIO_CAL (ao_symbols[4].addr)
55 #define NUM_REQUIRED_SYMBOLS 3
58 * Look through the Elf file for the AltOS symbols
59 * that can be adjusted before the image is written
66 Elf_Data *symbol_data = NULL;
69 int i, symbol_count, s;
78 while ((scn = elf_nextscn(e, scn)) != NULL) {
79 if (gelf_getshdr(scn, &shdr) != &shdr)
82 if (shdr.sh_type == SHT_SYMTAB) {
83 symbol_data = elf_getdata(scn, NULL);
84 symbol_count = shdr.sh_size / shdr.sh_entsize;
92 for (i = 0; i < symbol_count; i++) {
93 gelf_getsym(symbol_data, i, &sym);
95 symbol_name = elf_strptr(e, shdr.sh_link, sym.st_name);
97 for (s = 0; s < NUM_SYMBOLS; s++)
98 if (!strcmp (ao_symbols[s].name, symbol_name)) {
100 ao_symbols[s].addr = sym.st_value;
101 if (ao_symbols[s].required)
106 return required >= NUM_REQUIRED_SYMBOLS;
115 uint32_t round4(uint32_t a) {
120 new_load (uint32_t addr, uint32_t len)
125 new = calloc (1, sizeof (struct load) + len);
135 load_paste(struct load *into, struct load *from)
137 if (from->addr < into->addr || into->addr + into->len < from->addr + from->len)
140 memcpy(into->buf + from->addr - into->addr, from->buf, from->len);
144 * Make a new load structure large enough to hold the old one and
148 expand_load(struct load *from, uint32_t addr, uint32_t len)
153 uint32_t from_last = from->addr + from->len;
154 uint32_t last = addr + len;
156 if (addr > from->addr)
158 if (last < from_last)
163 if (addr == from->addr && len == from->len)
166 new = new_load(addr, len);
168 load_paste(new, from);
175 * Create a new load structure with data from the existing one
179 load_write(struct load *from, uint32_t addr, uint32_t len, void *data)
183 new = expand_load(from, addr, len);
184 memcpy(new->buf + addr - new->addr, data, len);
189 * Construct a large in-memory block for all
190 * of the loaded sections of the program
204 struct load *load = NULL;
206 if (elf_getshdrstrndx(e, &shstrndx) < 0)
209 if (elf_getphdrnum(e, &nphdr) < 0)
213 * As far as I can tell, all of the phdr sections should
214 * be flashed to memory
216 for (p = 0; p < nphdr; p++) {
219 gelf_getphdr(e, p, &phdr);
221 /* Get the associated file section */
222 scn = gelf_offscn(e, phdr.p_offset);
224 if (gelf_getshdr(scn, &shdr) != &shdr)
227 data = elf_getdata(scn, NULL);
229 /* Write the section data into the memory block */
230 load = load_write(load, phdr.p_paddr, phdr.p_filesz, data->d_buf);
236 * Edit the to-be-written memory block
239 rewrite(struct load *load, unsigned addr, uint8_t *data, int len)
243 if (addr < load->addr || load->addr + load->len < addr + len)
246 printf("rewrite %04x:", addr);
247 for (i = 0; i < len; i++)
248 printf (" %02x", load->buf[addr - load->addr + i]);
250 for (i = 0; i < len; i++)
251 printf (" %02x", data[i]);
253 memcpy(load->buf + addr - load->addr, data, len);
257 * Open the specified ELF file and
258 * check for the symbols we need
262 ao_open_elf(char *name)
267 Elf_Data *symbol_data = NULL;
270 size_t n, shstrndx, sz;
271 int i, symbol_count, s;
274 if (elf_version(EV_CURRENT) == EV_NONE)
277 fd = open(name, O_RDONLY, 0);
282 e = elf_begin(fd, ELF_C_READ, NULL);
287 if (elf_kind(e) != ELF_K_ELF)
290 if (elf_getshdrstrndx(e, &shstrndx) != 0)
293 if (!find_symbols(e)) {
294 fprintf (stderr, "Cannot find required symbols\n");
302 * Read a 32-bit value from the target device with arbitrary
306 get_uint32(stlink_t *sl, uint32_t addr)
308 const uint8_t *data = sl->q_buf;
309 uint32_t actual_addr;
315 printf ("read 0x%x\n", addr);
317 actual_addr = addr & ~3;
319 stlink_read_mem32(sl, actual_addr, 8);
325 result = data[off] | (data[off + 1] << 8) | (data[off+2] << 16) | (data[off+3] << 24);
326 printf ("read 0x%08x = 0x%08x\n", addr, result);
331 * Read a 16-bit value from the target device with arbitrary
335 get_uint16(stlink_t *sl, uint32_t addr)
337 const uint8_t *data = sl->q_buf;
338 uint32_t actual_addr;
345 actual_addr = addr & ~3;
347 stlink_read_mem32(sl, actual_addr, 8);
353 result = data[off] | (data[off + 1] << 8);
354 printf ("read 0x%08x = 0x%04x\n", addr, result);
359 * Check to see if the target device has been
360 * flashed with a similar firmware image before
362 * This is done by looking for the same romconfig version,
363 * which should be at the same location as the linker script
364 * places this at 0x100 from the start of the rom section
367 check_flashed(stlink_t *sl)
369 uint16_t romconfig_version = get_uint16(sl, AO_ROMCONFIG_VERSION);
370 uint16_t romconfig_check = get_uint16(sl, AO_ROMCONFIG_CHECK);
372 if (romconfig_version != (uint16_t) ~romconfig_check) {
373 fprintf (stderr, "Device has not been flashed before\n");
379 static const struct option options[] = {
380 { .name = "device", .has_arg = 1, .val = 'D' },
381 { .name = "cal", .has_arg = 1, .val = 'c' },
382 { .name = "serial", .has_arg = 1, .val = 's' },
386 static void usage(char *program)
388 fprintf(stderr, "usage: %s [--cal=<radio-cal>] [--serial=<serial>] file.elf\n", program);
393 done(stlink_t *sl, int code)
398 stlink_exit_debug_mode(sl);
405 main (int argc, char **argv)
411 unsigned int serial = 0;
426 while ((c = getopt_long(argc, argv, "D:c:s:", options, NULL)) != -1) {
432 cal = strtoul(optarg, &cal_end, 10);
433 if (cal_end == optarg || *cal_end != '\0')
437 serial = strtoul(optarg, &serial_end, 10);
438 if (serial_end == optarg || *serial_end != '\0')
447 filename = argv[optind];
448 if (filename == NULL)
452 * Open the source file and load the symbols and
456 e = ao_open_elf(filename);
458 fprintf(stderr, "Cannot open file \"%s\"\n", filename);
462 if (!find_symbols(e)) {
463 fprintf(stderr, "Cannot find symbols in \"%s\"\n", filename);
467 if (!(load = get_load(e))) {
468 fprintf(stderr, "Cannot find program data in \"%s\"\n", filename);
472 /* Connect to the programming dongle
476 sl = stlink_v1_open(50);
478 sl = stlink_open_usb(50);
482 fprintf (stderr, "No STLink devices present\n");
488 /* Verify that the loaded image fits entirely within device flash
490 if (load->addr < sl->flash_base ||
491 sl->flash_base + sl->flash_size < load->addr + load->len) {
492 fprintf (stderr, "\%s\": Invalid memory range 0x%08x - 0x%08x\n", filename,
493 load->addr, load->addr + load->len);
497 /* Enter debugging mode
499 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
500 stlink_exit_dfu_mode(sl);
502 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
503 stlink_enter_swd_mode(sl);
505 /* Go fetch existing config values
508 was_flashed = check_flashed(sl);
512 fprintf (stderr, "Must provide serial number\n");
515 serial = get_uint16(sl, AO_SERIAL_NUMBER);
516 if (!serial || serial == 0xffff) {
517 fprintf (stderr, "Invalid existing serial %d\n", serial);
522 if (!cal && AO_RADIO_CAL && was_flashed) {
523 cal = get_uint32(sl, AO_RADIO_CAL);
524 if (!cal || cal == 0xffffffff) {
525 fprintf (stderr, "Invalid existing rf cal %d\n", cal);
530 /* Write the config values into the flash image
533 serial_int[0] = serial & 0xff;
534 serial_int[1] = (serial >> 8) & 0xff;
536 if (!rewrite(load, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
537 fprintf(stderr, "Cannot rewrite serial integer at %08x\n",
542 if (AO_USB_DESCRIPTORS) {
543 unsigned usb_descriptors;
544 usb_descriptors = AO_USB_DESCRIPTORS - load->addr;
547 while (load->buf[usb_descriptors] != 0 && usb_descriptors < load->len) {
548 if (load->buf[usb_descriptors+1] == AO_USB_DESC_STRING) {
553 usb_descriptors += load->buf[usb_descriptors];
555 if (usb_descriptors >= load->len || load->buf[usb_descriptors] == 0 ) {
556 fprintf(stderr, "Cannot rewrite serial string at %08x\n", AO_USB_DESCRIPTORS);
560 serial_ucs2_len = load->buf[usb_descriptors] - 2;
561 serial_ucs2 = malloc(serial_ucs2_len);
563 fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
567 for (i = serial_ucs2_len / 2; i; i--) {
568 serial_ucs2[i * 2 - 1] = 0;
569 serial_ucs2[i * 2 - 2] = (s % 10) + '0';
572 if (!rewrite(load, usb_descriptors + 2 + load->addr, serial_ucs2, serial_ucs2_len)) {
573 fprintf (stderr, "Cannot rewrite USB descriptor at %08x\n", AO_USB_DESCRIPTORS);
578 if (cal && AO_RADIO_CAL) {
579 cal_int[0] = cal & 0xff;
580 cal_int[1] = (cal >> 8) & 0xff;
581 cal_int[2] = (cal >> 16) & 0xff;
582 cal_int[3] = (cal >> 24) & 0xff;
584 if (!rewrite(load, AO_RADIO_CAL, cal_int, sizeof (cal_int))) {
585 fprintf(stderr, "Cannot rewrite radio calibration at %08x\n", AO_RADIO_CAL);
590 /* And flash the resulting image to the device
592 if (stlink_write_flash(sl, load->addr, load->buf, load->len) < 0) {
593 fprintf (stderr, "\"%s\": Write failed\n", filename);