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;
75 if (elf_getshdrstrndx(e, &shstrndx) < 0)
83 while ((scn = elf_nextscn(e, scn)) != NULL) {
85 if (gelf_getshdr(scn, &shdr) != &shdr)
89 section_name = elf_strptr(e, shstrndx, shdr.sh_name);
91 printf ("name %s\n", section_name);
93 if (shdr.sh_type == SHT_PROGBITS)
95 printf ("\ttype %lx\n", shdr.sh_type);
96 printf ("\tflags %lx\n", shdr.sh_flags);
97 printf ("\taddr %lx\n", shdr.sh_addr);
98 printf ("\toffset %lx\n", shdr.sh_offset);
99 printf ("\tsize %lx\n", shdr.sh_size);
100 printf ("\tlink %lx\n", shdr.sh_link);
101 printf ("\tinfo %lx\n", shdr.sh_info);
102 printf ("\taddralign %lx\n", shdr.sh_addralign);
103 printf ("\tentsize %lx\n", shdr.sh_entsize);
107 if (shdr.sh_type == SHT_SYMTAB) {
108 symbol_data = elf_getdata(scn, NULL);
109 symbol_count = shdr.sh_size / shdr.sh_entsize;
117 for (i = 0; i < symbol_count; i++) {
118 gelf_getsym(symbol_data, i, &sym);
120 symbol_name = elf_strptr(e, shdr.sh_link, sym.st_name);
122 for (s = 0; s < NUM_SYMBOLS; s++)
123 if (!strcmp (ao_symbols[s].name, symbol_name)) {
125 ao_symbols[s].addr = sym.st_value;
126 if (ao_symbols[s].required)
131 return required >= NUM_REQUIRED_SYMBOLS;
140 uint32_t round4(uint32_t a) {
145 new_load (uint32_t addr, uint32_t len)
150 new = calloc (1, sizeof (struct load) + len);
160 load_paste(struct load *into, struct load *from)
162 if (from->addr < into->addr || into->addr + into->len < from->addr + from->len)
165 memcpy(into->buf + from->addr - into->addr, from->buf, from->len);
169 * Make a new load structure large enough to hold the old one and
173 expand_load(struct load *from, uint32_t addr, uint32_t len)
178 uint32_t from_last = from->addr + from->len;
179 uint32_t last = addr + len;
181 if (addr > from->addr)
183 if (last < from_last)
188 if (addr == from->addr && len == from->len)
191 new = new_load(addr, len);
193 load_paste(new, from);
200 * Create a new load structure with data from the existing one
204 load_write(struct load *from, uint32_t addr, uint32_t len, void *data)
208 new = expand_load(from, addr, len);
209 memcpy(new->buf + addr - new->addr, data, len);
214 * Construct a large in-memory block for all
215 * of the loaded sections of the program
232 struct load *load = NULL;
237 if (elf_getshdrstrndx(e, &shstrndx) < 0)
240 if (elf_getphdrnum(e, &nphdr) < 0)
243 if (elf_getshdrnum(e, &nshdr) < 0)
247 * As far as I can tell, all of the phdr sections should
248 * be flashed to memory
250 for (p = 0; p < nphdr; p++) {
253 gelf_getphdr(e, p, &phdr);
255 if (phdr.p_type != PT_LOAD)
258 p_offset = phdr.p_offset;
259 /* Get the associated file section */
262 printf ("offset %08x vaddr %08x paddr %08x filesz %08x memsz %08x\n",
263 (uint32_t) phdr.p_offset,
264 (uint32_t) phdr.p_vaddr,
265 (uint32_t) phdr.p_paddr,
266 (uint32_t) phdr.p_filesz,
267 (uint32_t) phdr.p_memsz);
270 for (s = 0; s < nshdr; s++) {
271 scn = elf_getscn(e, s);
274 printf ("getscn failed\n");
277 if (gelf_getshdr(scn, &shdr) != &shdr) {
278 printf ("gelf_getshdr failed\n");
282 section_name = elf_strptr(e, shstrndx, shdr.sh_name);
284 if (phdr.p_offset <= shdr.sh_offset && shdr.sh_offset < phdr.p_offset + phdr.p_filesz) {
286 if (shdr.sh_size == 0)
289 sh_paddr = phdr.p_paddr + shdr.sh_offset - phdr.p_offset;
291 printf ("\tsize %08x rom %08x exec %08x %s\n",
292 (uint32_t) shdr.sh_size,
294 (uint32_t) shdr.sh_addr,
297 data = elf_getdata(scn, NULL);
299 /* Write the section data into the memory block */
300 load = load_write(load, sh_paddr, shdr.sh_size, data->d_buf);
308 * Edit the to-be-written memory block
311 rewrite(struct load *load, unsigned addr, uint8_t *data, int len)
315 if (addr < load->addr || load->addr + load->len < addr + len)
318 printf("rewrite %04x:", addr);
319 for (i = 0; i < len; i++)
320 printf (" %02x", load->buf[addr - load->addr + i]);
322 for (i = 0; i < len; i++)
323 printf (" %02x", data[i]);
325 memcpy(load->buf + addr - load->addr, data, len);
329 * Open the specified ELF file and
330 * check for the symbols we need
334 ao_open_elf(char *name)
339 Elf_Data *symbol_data = NULL;
342 size_t n, shstrndx, sz;
343 int i, symbol_count, s;
346 if (elf_version(EV_CURRENT) == EV_NONE)
349 fd = open(name, O_RDONLY, 0);
354 e = elf_begin(fd, ELF_C_READ, NULL);
359 if (elf_kind(e) != ELF_K_ELF)
362 if (elf_getshdrstrndx(e, &shstrndx) != 0)
365 if (!find_symbols(e)) {
366 fprintf (stderr, "Cannot find required symbols\n");
374 * Read a 32-bit value from the target device with arbitrary
378 get_uint32(stlink_t *sl, uint32_t addr)
380 const uint8_t *data = sl->q_buf;
381 uint32_t actual_addr;
387 printf ("read 0x%x\n", addr);
389 actual_addr = addr & ~3;
391 stlink_read_mem32(sl, actual_addr, 8);
397 result = data[off] | (data[off + 1] << 8) | (data[off+2] << 16) | (data[off+3] << 24);
398 printf ("read 0x%08x = 0x%08x\n", addr, result);
403 * Read a 16-bit value from the target device with arbitrary
407 get_uint16(stlink_t *sl, uint32_t addr)
409 const uint8_t *data = sl->q_buf;
410 uint32_t actual_addr;
417 actual_addr = addr & ~3;
419 stlink_read_mem32(sl, actual_addr, 8);
425 result = data[off] | (data[off + 1] << 8);
426 printf ("read 0x%08x = 0x%04x\n", addr, result);
431 * Check to see if the target device has been
432 * flashed with a similar firmware image before
434 * This is done by looking for the same romconfig version,
435 * which should be at the same location as the linker script
436 * places this at 0x100 from the start of the rom section
439 check_flashed(stlink_t *sl)
441 uint16_t romconfig_version = get_uint16(sl, AO_ROMCONFIG_VERSION);
442 uint16_t romconfig_check = get_uint16(sl, AO_ROMCONFIG_CHECK);
444 if (romconfig_version != (uint16_t) ~romconfig_check) {
445 fprintf (stderr, "Device has not been flashed before\n");
451 static const struct option options[] = {
452 { .name = "device", .has_arg = 1, .val = 'D' },
453 { .name = "cal", .has_arg = 1, .val = 'c' },
454 { .name = "serial", .has_arg = 1, .val = 's' },
458 static void usage(char *program)
460 fprintf(stderr, "usage: %s [--cal=<radio-cal>] [--serial=<serial>] file.elf\n", program);
465 done(stlink_t *sl, int code)
470 stlink_exit_debug_mode(sl);
477 main (int argc, char **argv)
483 unsigned int serial = 0;
499 while ((c = getopt_long(argc, argv, "D:c:s:", options, NULL)) != -1) {
505 cal = strtoul(optarg, &cal_end, 10);
506 if (cal_end == optarg || *cal_end != '\0')
510 serial = strtoul(optarg, &serial_end, 10);
511 if (serial_end == optarg || *serial_end != '\0')
520 filename = argv[optind];
521 if (filename == NULL)
525 * Open the source file and load the symbols and
529 e = ao_open_elf(filename);
531 fprintf(stderr, "Cannot open file \"%s\"\n", filename);
535 if (!find_symbols(e)) {
536 fprintf(stderr, "Cannot find symbols in \"%s\"\n", filename);
540 if (!(load = get_load(e))) {
541 fprintf(stderr, "Cannot find program data in \"%s\"\n", filename);
545 /* Connect to the programming dongle
548 for (tries = 0; tries < 3; tries++) {
550 sl = stlink_v1_open(50);
552 sl = stlink_open_usb(50);
556 fprintf (stderr, "No STLink devices present\n");
560 if (sl->chip_id != 0)
565 if (sl->chip_id == 0) {
566 fprintf (stderr, "Debugger connection failed\n");
570 /* Verify that the loaded image fits entirely within device flash
572 if (load->addr < sl->flash_base ||
573 sl->flash_base + sl->flash_size < load->addr + load->len) {
574 fprintf (stderr, "\%s\": Invalid memory range 0x%08x - 0x%08x\n", filename,
575 load->addr, load->addr + load->len);
579 /* Enter debugging mode
581 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
582 stlink_exit_dfu_mode(sl);
584 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
585 stlink_enter_swd_mode(sl);
587 /* Go fetch existing config values
590 was_flashed = check_flashed(sl);
594 fprintf (stderr, "Must provide serial number\n");
597 serial = get_uint16(sl, AO_SERIAL_NUMBER);
598 if (!serial || serial == 0xffff) {
599 fprintf (stderr, "Invalid existing serial %d\n", serial);
604 if (!cal && AO_RADIO_CAL && was_flashed) {
605 cal = get_uint32(sl, AO_RADIO_CAL);
606 if (!cal || cal == 0xffffffff) {
607 fprintf (stderr, "Invalid existing rf cal %d\n", cal);
612 /* Write the config values into the flash image
615 serial_int[0] = serial & 0xff;
616 serial_int[1] = (serial >> 8) & 0xff;
618 if (!rewrite(load, AO_SERIAL_NUMBER, serial_int, sizeof (serial_int))) {
619 fprintf(stderr, "Cannot rewrite serial integer at %08x\n",
624 if (AO_USB_DESCRIPTORS) {
625 unsigned usb_descriptors;
626 usb_descriptors = AO_USB_DESCRIPTORS - load->addr;
629 while (load->buf[usb_descriptors] != 0 && usb_descriptors < load->len) {
630 if (load->buf[usb_descriptors+1] == AO_USB_DESC_STRING) {
635 usb_descriptors += load->buf[usb_descriptors];
637 if (usb_descriptors >= load->len || load->buf[usb_descriptors] == 0 ) {
638 fprintf(stderr, "Cannot rewrite serial string at %08x\n", AO_USB_DESCRIPTORS);
642 serial_ucs2_len = load->buf[usb_descriptors] - 2;
643 serial_ucs2 = malloc(serial_ucs2_len);
645 fprintf(stderr, "Malloc(%d) failed\n", serial_ucs2_len);
649 for (i = serial_ucs2_len / 2; i; i--) {
650 serial_ucs2[i * 2 - 1] = 0;
651 serial_ucs2[i * 2 - 2] = (s % 10) + '0';
654 if (!rewrite(load, usb_descriptors + 2 + load->addr, serial_ucs2, serial_ucs2_len)) {
655 fprintf (stderr, "Cannot rewrite USB descriptor at %08x\n", AO_USB_DESCRIPTORS);
660 if (cal && AO_RADIO_CAL) {
661 cal_int[0] = cal & 0xff;
662 cal_int[1] = (cal >> 8) & 0xff;
663 cal_int[2] = (cal >> 16) & 0xff;
664 cal_int[3] = (cal >> 24) & 0xff;
666 if (!rewrite(load, AO_RADIO_CAL, cal_int, sizeof (cal_int))) {
667 fprintf(stderr, "Cannot rewrite radio calibration at %08x\n", AO_RADIO_CAL);
672 /* And flash the resulting image to the device
674 if (stlink_write_flash(sl, load->addr, load->buf, load->len) < 0) {
675 fprintf (stderr, "\"%s\": Write failed\n", filename);