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;
116 new_load (uint32_t addr, uint32_t len)
118 struct load *new = calloc (1, sizeof (struct load) + len);
128 load_paste(struct load *into, struct load *from)
130 if (from->addr < into->addr || into->addr + into->len < from->addr + from->len)
133 memcpy(into->buf + from->addr - into->addr, from->buf, from->len);
137 * Make a new load structure large enough to hold the old one and
141 expand_load(struct load *from, uint32_t addr, uint32_t len)
146 uint32_t from_last = from->addr + from->len;
147 uint32_t last = addr + len;
149 if (addr > from->addr)
151 if (last < from_last)
156 if (addr == from->addr && len == from->len)
159 new = new_load(addr, len);
161 load_paste(new, from);
168 * Create a new load structure with data from the existing one
172 load_write(struct load *from, uint32_t addr, uint32_t len, void *data)
176 new = expand_load(from, addr, len);
177 memcpy(new->buf + addr - new->addr, data, len);
182 * Construct a large in-memory block for all
183 * of the loaded sections of the program
197 struct load *load = NULL;
199 if (elf_getshdrstrndx(e, &shstrndx) < 0)
202 if (elf_getphdrnum(e, &nphdr) < 0)
206 * As far as I can tell, all of the phdr sections should
207 * be flashed to memory
209 for (p = 0; p < nphdr; p++) {
212 gelf_getphdr(e, p, &phdr);
214 /* Get the associated file section */
215 scn = gelf_offscn(e, phdr.p_offset);
217 if (gelf_getshdr(scn, &shdr) != &shdr)
220 data = elf_getdata(scn, NULL);
222 /* Write the section data into the memory block */
223 load = load_write(load, phdr.p_paddr, phdr.p_filesz, data->d_buf);
229 * Edit the to-be-written memory block
232 rewrite(struct load *load, unsigned addr, uint8_t *data, int len)
236 if (addr < load->addr || load->addr + load->len < addr + len)
239 printf("rewrite %04x:", addr);
240 for (i = 0; i < len; i++)
241 printf (" %02x", load->buf[addr - load->addr + i]);
243 for (i = 0; i < len; i++)
244 printf (" %02x", data[i]);
246 memcpy(load->buf + addr - load->addr, data, len);
250 * Open the specified ELF file and
251 * check for the symbols we need
255 ao_open_elf(char *name)
260 Elf_Data *symbol_data = NULL;
263 size_t n, shstrndx, sz;
264 int i, symbol_count, s;
267 if (elf_version(EV_CURRENT) == EV_NONE)
270 fd = open(name, O_RDONLY, 0);
275 e = elf_begin(fd, ELF_C_READ, NULL);
280 if (elf_kind(e) != ELF_K_ELF)
283 if (elf_getshdrstrndx(e, &shstrndx) != 0)
286 if (!find_symbols(e)) {
287 fprintf (stderr, "Cannot find required symbols\n");
295 * Read a 32-bit value from the target device with arbitrary
299 get_uint32(stlink_t *sl, uint32_t addr)
301 const uint8_t *data = sl->q_buf;
302 uint32_t actual_addr;
308 printf ("read 0x%x\n", addr);
310 actual_addr = addr & ~3;
312 stlink_read_mem32(sl, actual_addr, 8);
318 result = data[off] | (data[off + 1] << 8) | (data[off+2] << 16) | (data[off+3] << 24);
319 printf ("read 0x%08x = 0x%08x\n", addr, result);
324 * Read a 16-bit value from the target device with arbitrary
328 get_uint16(stlink_t *sl, uint32_t addr)
330 const uint8_t *data = sl->q_buf;
331 uint32_t actual_addr;
338 actual_addr = addr & ~3;
340 stlink_read_mem32(sl, actual_addr, 8);
346 result = data[off] | (data[off + 1] << 8);
347 printf ("read 0x%08x = 0x%04x\n", addr, result);
352 * Check to see if the target device has been
353 * flashed with a similar firmware image before
355 * This is done by looking for the same romconfig version,
356 * which should be at the same location as the linker script
357 * places this at 0x100 from the start of the rom section
360 check_flashed(stlink_t *sl)
362 uint16_t romconfig_version = get_uint16(sl, AO_ROMCONFIG_VERSION);
363 uint16_t romconfig_check = get_uint16(sl, AO_ROMCONFIG_CHECK);
365 if (romconfig_version != (uint16_t) ~romconfig_check) {
366 fprintf (stderr, "Device has not been flashed before\n");
372 static const struct option options[] = {
373 { .name = "device", .has_arg = 1, .val = 'D' },
374 { .name = "cal", .has_arg = 1, .val = 'c' },
375 { .name = "serial", .has_arg = 1, .val = 's' },
379 static void usage(char *program)
381 fprintf(stderr, "usage: %s [--cal=<radio-cal>] [--serial=<serial>] file.elf\n", program);
386 done(stlink_t *sl, int code)
391 stlink_exit_debug_mode(sl);
398 main (int argc, char **argv)
404 unsigned int serial = 0;
419 while ((c = getopt_long(argc, argv, "D:c:s:", options, NULL)) != -1) {
425 cal = strtoul(optarg, &cal_end, 10);
426 if (cal_end == optarg || *cal_end != '\0')
430 serial = strtoul(optarg, &serial_end, 10);
431 if (serial_end == optarg || *serial_end != '\0')
440 filename = argv[optind];
441 if (filename == NULL)
445 * Open the source file and load the symbols and
449 e = ao_open_elf(filename);
451 fprintf(stderr, "Cannot open file \"%s\"\n", filename);
455 if (!find_symbols(e)) {
456 fprintf(stderr, "Cannot find symbols in \"%s\"\n", filename);
460 if (!(load = get_load(e))) {
461 fprintf(stderr, "Cannot find program data in \"%s\"\n", filename);
465 /* Connect to the programming dongle
469 sl = stlink_v1_open(50);
471 sl = stlink_open_usb(50);
475 fprintf (stderr, "No STLink devices present\n");
481 /* Verify that the loaded image fits entirely within device flash
483 if (load->addr < sl->flash_base ||
484 sl->flash_base + sl->flash_size < load->addr + load->len) {
485 fprintf (stderr, "\%s\": Invalid memory range 0x%08x - 0x%08x\n", filename,
486 load->addr, load->addr + load->len);
490 /* Enter debugging mode
492 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
493 stlink_exit_dfu_mode(sl);
495 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
496 stlink_enter_swd_mode(sl);
498 /* Go fetch existing config values
501 was_flashed = check_flashed(sl);
505 fprintf (stderr, "Must provide serial number\n");
508 serial = get_uint16(sl, AO_SERIAL_NUMBER);
509 if (!serial || serial == 0xffff) {
510 fprintf (stderr, "Invalid existing serial %d\n", serial);
515 if (!cal && AO_RADIO_CAL && was_flashed) {
516 cal = get_uint32(sl, AO_RADIO_CAL);
517 if (!cal || cal == 0xffffffff) {
518 fprintf (stderr, "Invalid existing rf cal %d\n", cal);
523 /* Write the config values into the flash image
526 serial_int[0] = serial & 0xff;
527 serial_int[1] = (serial >> 8) & 0xff;
529 if (!rewrite(load, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
530 fprintf(stderr, "Cannot rewrite serial integer at %08x\n",
535 if (AO_USB_DESCRIPTORS) {
536 unsigned usb_descriptors;
537 usb_descriptors = AO_USB_DESCRIPTORS - load->addr;
540 while (load->buf[usb_descriptors] != 0 && usb_descriptors < load->len) {
541 if (load->buf[usb_descriptors+1] == AO_USB_DESC_STRING) {
546 usb_descriptors += load->buf[usb_descriptors];
548 if (usb_descriptors >= load->len || load->buf[usb_descriptors] == 0 ) {
549 fprintf(stderr, "Cannot rewrite serial string at %08x\n", AO_USB_DESCRIPTORS);
553 serial_ucs2_len = load->buf[usb_descriptors] - 2;
554 serial_ucs2 = malloc(serial_ucs2_len);
556 fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
560 for (i = serial_ucs2_len / 2; i; i--) {
561 serial_ucs2[i * 2 - 1] = 0;
562 serial_ucs2[i * 2 - 2] = (s % 10) + '0';
565 if (!rewrite(load, usb_descriptors + 2 + load->addr, serial_ucs2, serial_ucs2_len)) {
566 fprintf (stderr, "Cannot rewrite USB descriptor at %08x\n", AO_USB_DESCRIPTORS);
571 if (cal && AO_RADIO_CAL) {
572 cal_int[0] = cal & 0xff;
573 cal_int[1] = (cal >> 8) & 0xff;
574 cal_int[2] = (cal >> 16) & 0xff;
575 cal_int[3] = (cal >> 24) & 0xff;
577 if (!rewrite(load, AO_RADIO_CAL, cal_int, sizeof (cal_int))) {
578 fprintf(stderr, "Cannot rewrite radio calibration at %08x\n", AO_RADIO_CAL);
583 /* And flash the resulting image to the device
585 if (stlink_write_flash(sl, load->addr, load->buf, load->len) < 0) {
586 fprintf (stderr, "\"%s\": Write failed\n", filename);