From 2223a1c92575442fb7bf4344701f79efcc01c691 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 8 Mar 2015 11:50:37 -0700 Subject: Add a Makefile to build flash stubs. --- flashstub/Makefile | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 flashstub/Makefile diff --git a/flashstub/Makefile b/flashstub/Makefile new file mode 100644 index 0000000..cb6f693 --- /dev/null +++ b/flashstub/Makefile @@ -0,0 +1,33 @@ +CROSS_COMPILE ?= arm-none-eabi- +AS = $(CROSS_COMPILE)as +CC = $(CROSS_COMPILE)gcc +OBJCOPY = $(CROSS_COMPILE)objcopy +HEXDUMP = hexdump + +ifneq ($(V), 1) +Q = @ +endif + +CFLAGS=-mcpu=cortex-m3 -mthumb -I../libopencm3/include +ASFLAGS=-mcpu=cortex-m3 -mthumb + +all: lmi.stub stm32.stub stm32f4.stub nrf51.stub + +%.o: %.s + $(Q)echo " AS $<" + $(Q)$(AS) $(ASFLAGS) -o $@ $< + +%.bin: %.o + $(Q)echo " OBJCOPY $@" + $(Q)$(OBJCOPY) -O binary $< $@ + +%.stub: %.bin + $(Q)echo " HEXDUMP $@" + $(Q)$(HEXDUMP) -v -e '/2 "0x%04X, "' $< > $@ + +.PHONY: clean + +clean: + $(Q)echo " CLEAN" + -$(Q)rm -f *.o *.bin *.stub + -- cgit v1.2.3 From e380ced5171dc3c9098005924f61051a43b79428 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 8 Mar 2015 11:51:59 -0700 Subject: Change STM32F1 driver to use generated stub. --- flashstub/stm32.stub | 1 + src/stm32f1.c | 35 +---------------------------------- 2 files changed, 2 insertions(+), 34 deletions(-) create mode 100644 flashstub/stm32.stub diff --git a/flashstub/stm32.stub b/flashstub/stm32.stub new file mode 100644 index 0000000..755f445 --- /dev/null +++ b/flashstub/stm32.stub @@ -0,0 +1 @@ +0x4809, 0x490A, 0x467A, 0x322C, 0x4B09, 0x2501, 0x2B00, 0xD00A, 0x6105, 0x8814, 0x800C, 0x68C4, 0x2601, 0x4234, 0xD1FB, 0x3B02, 0x3102, 0x3202, 0xE7F2, 0xBE00, 0x2000, 0x4002, 0x0000, 0x0000, 0x000C, 0x0000, 0xAAAA, 0xAAAA, 0xBBBB, 0xBBBB, 0xCCCC, 0xCCCC, \ No newline at end of file diff --git a/src/stm32f1.c b/src/stm32f1.c index a746d5d..0e87c21 100644 --- a/src/stm32f1.c +++ b/src/stm32f1.c @@ -120,40 +120,7 @@ static const char stm32hd_xml_memory_map[] = "" #define DBGMCU_IDCODE_F0 0x40015800 static const uint16_t stm32f1_flash_write_stub[] = { -// _start: - 0x4809, // ldr r0, [pc, #36] // _flashbase - 0x490a, // ldr r1, [pc, #40] // _addr - 0x467a, // mov r2, pc - 0x322c, // adds r2, #44 - 0x4b09, // ldr r3, [pc, #36] // _size - 0x2501, // movs r5, #1 -// _next: - 0x2b00, // cmp r3, #0 - 0xd00a, // beq _done - 0x6105, // str r5, [r0, #16] - 0x8814, // ldrh r4, [r2] - 0x800c, // strh r4, [r1] -// _wait: - 0x68c4, // ldr r4, [r0, #12] - 0x2601, // movs r6, #1 - 0x4234, // tst r4, r6 - 0xd1fb, // bne _wait - - 0x3b02, // subs r3, #2 - 0x3102, // adds r1, #2 - 0x3202, // adds r2, #2 - 0xe7f2, // b _next -// _done: - 0xbe00, // bkpt -// .org 0x28 -// _flashbase: - 0x2000, 0x4002, // .word 0x40022000 (FPEC_BASE) -// _addr: -// 0x0000, 0x0000, -// _size: -// 0x0000, 0x0000, -// _data: -// ... +#include "../flashstub/stm32.stub" }; bool stm32f1_probe(struct target_s *target) -- cgit v1.2.3 From c2462a6788c119edd9995fa46f1316a9c0431e0b Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 8 Mar 2015 12:55:59 -0700 Subject: Add cortexm generic stub call, and use in stm32f1 driver. --- src/cortexm.c | 30 +++++++++++++++++++++++++++++- src/include/cortexm.h | 3 +++ src/stm32f1.c | 13 +++++-------- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/cortexm.c b/src/cortexm.c index 4bfb4d9..0327edc 100644 --- a/src/cortexm.c +++ b/src/cortexm.c @@ -603,6 +603,35 @@ static int cortexm_fault_unwind(struct target_s *target) return 0; } +int cortexm_run_stub(struct target_s *target, uint32_t loadaddr, + const uint16_t *stub, uint32_t stublen, + uint32_t r0, uint32_t r1, uint32_t r2, uint32_t r3) +{ + uint32_t regs[target->regs_size / 4]; + + memset(regs, 0, sizeof(regs)); + regs[0] = r0; + regs[1] = r1; + regs[2] = r2; + regs[3] = r3; + regs[15] = loadaddr; + regs[16] = 0x1000000; + regs[19] = 0; + + target_mem_write(target, loadaddr, stub, stublen); + cortexm_regs_write(target, regs); + + if (target_check_error(target)) + return -1; + + /* Execute the stub */ + cortexm_halt_resume(target, 0); + while (!cortexm_halt_wait(target)) + ; + + return 0; +} + /* The following routines implement hardware breakpoints. * The Flash Patch and Breakpoint (FPB) system is used. */ @@ -648,7 +677,6 @@ cortexm_clear_hw_bp(struct target_s *target, uint32_t addr) return 0; } - /* The following routines implement hardware watchpoints. * The Data Watch and Trace (DWT) system is used. */ diff --git a/src/include/cortexm.h b/src/include/cortexm.h index 1fd77e6..8f4d45d 100644 --- a/src/include/cortexm.h +++ b/src/include/cortexm.h @@ -146,6 +146,9 @@ bool cortexm_attach(struct target_s *target); void cortexm_detach(struct target_s *target); void cortexm_halt_resume(struct target_s *target, bool step); +int cortexm_run_stub(struct target_s *target, uint32_t loadaddr, + const uint16_t *stub, uint32_t stublen, + uint32_t r0, uint32_t r1, uint32_t r2, uint32_t r3); #endif diff --git a/src/stm32f1.c b/src/stm32f1.c index 0e87c21..daba1b0 100644 --- a/src/stm32f1.c +++ b/src/stm32f1.c @@ -32,9 +32,12 @@ #include "general.h" #include "adiv5.h" #include "target.h" +#include "cortexm.h" #include "command.h" #include "gdb_packet.h" +#define SRAM_BASE 0x20000000 + static bool stm32f1_cmd_erase_mass(target *t); static bool stm32f1_cmd_option(target *t, int argc, char *argv[]); @@ -257,15 +260,9 @@ static int stm32f1_flash_write(struct target_s *target, uint32_t dest, memcpy((uint8_t *)&data[2] + offset, src, len); /* Write stub and data to target ram and set PC */ - target_mem_write(target, 0x20000000, stm32f1_flash_write_stub, 0x2C); target_mem_write(target, 0x2000002C, data, sizeof(data)); - target_pc_write(target, 0x20000000); - if(target_check_error(target)) - return -1; - - /* Execute the stub */ - target_halt_resume(target, 0); - while(!target_halt_wait(target)); + cortexm_run_stub(target, SRAM_BASE, stm32f1_flash_write_stub, 0x2C, + 0, 0, 0, 0); /* Check for error */ if (target_mem_read32(target, FLASH_SR) & SR_ERROR_MASK) -- cgit v1.2.3 From 437aedda11a112407af7b3f7884090023944faef Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 8 Mar 2015 13:35:07 -0700 Subject: Rewrote stm32f1 stub in C and call with parameters in registers. --- flashstub/Makefile | 6 +++++- flashstub/stm32.s | 43 ------------------------------------------- flashstub/stm32.stub | 1 - flashstub/stm32f1.c | 34 ++++++++++++++++++++++++++++++++++ flashstub/stm32f1.stub | 1 + src/include/general.h | 2 ++ src/stm32f1.c | 27 ++++++++++++--------------- 7 files changed, 54 insertions(+), 60 deletions(-) delete mode 100644 flashstub/stm32.s delete mode 100644 flashstub/stm32.stub create mode 100644 flashstub/stm32f1.c create mode 100644 flashstub/stm32f1.stub diff --git a/flashstub/Makefile b/flashstub/Makefile index cb6f693..eeb1803 100644 --- a/flashstub/Makefile +++ b/flashstub/Makefile @@ -11,7 +11,11 @@ endif CFLAGS=-mcpu=cortex-m3 -mthumb -I../libopencm3/include ASFLAGS=-mcpu=cortex-m3 -mthumb -all: lmi.stub stm32.stub stm32f4.stub nrf51.stub +all: lmi.stub stm32f4.stub nrf51.stub stm32f1.stub + +stm32f1.o: stm32f1.c + $(Q)echo " CC $<" + $(Q)$(CC) $(CFLAGS) -DSTM32F1 -o $@ -c $< %.o: %.s $(Q)echo " AS $<" diff --git a/flashstub/stm32.s b/flashstub/stm32.s deleted file mode 100644 index 8a9cb54..0000000 --- a/flashstub/stm32.s +++ /dev/null @@ -1,43 +0,0 @@ -.global _start - -_start: - ldr r0, _flashbase - ldr r1, _addr - mov r2, pc - add r2, #(_data - . - 2) - ldr r3, _size - mov r5, #1 -_next: - cmp r3, #0 - beq _done - @ Write PG command to FLASH_CR - str r5, [r0, #0x10] - @ Write data to flash (half-word) - ldrh r4, [r2] - strh r4, [r1] - -_wait: @ Wait for BSY bit to clear - ldr r4, [r0, #0x0C] - mov r6, #1 - tst r4, r6 - bne _wait - - sub r3, #2 - add r1, #2 - add r2, #2 - b _next -_done: - bkpt - -@.align 4 -.org 0x28 -_flashbase: - .word 0x40022000 -_addr: - .word 0 -_size: - .word 12 -_data: - .word 0xAAAAAAAA - .word 0xBBBBBBBB - .word 0xCCCCCCCC diff --git a/flashstub/stm32.stub b/flashstub/stm32.stub deleted file mode 100644 index 755f445..0000000 --- a/flashstub/stm32.stub +++ /dev/null @@ -1 +0,0 @@ -0x4809, 0x490A, 0x467A, 0x322C, 0x4B09, 0x2501, 0x2B00, 0xD00A, 0x6105, 0x8814, 0x800C, 0x68C4, 0x2601, 0x4234, 0xD1FB, 0x3B02, 0x3102, 0x3202, 0xE7F2, 0xBE00, 0x2000, 0x4002, 0x0000, 0x0000, 0x000C, 0x0000, 0xAAAA, 0xAAAA, 0xBBBB, 0xBBBB, 0xCCCC, 0xCCCC, \ No newline at end of file diff --git a/flashstub/stm32f1.c b/flashstub/stm32f1.c new file mode 100644 index 0000000..8370114 --- /dev/null +++ b/flashstub/stm32f1.c @@ -0,0 +1,34 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2015 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "libopencm3/stm32/flash.h" + +void __attribute__((naked)) +stm32f1_flash_write_stub(uint16_t *dest, uint16_t *src, uint32_t size) +{ + while (size) { + FLASH_CR = FLASH_CR_PG; + *dest++ = *src++; + size -= 2; + while (FLASH_SR & FLASH_SR_BSY) + ; + } + asm("bkpt"); +} + diff --git a/flashstub/stm32f1.stub b/flashstub/stm32f1.stub new file mode 100644 index 0000000..97d2a5e --- /dev/null +++ b/flashstub/stm32f1.stub @@ -0,0 +1 @@ +0x4613, 0xE010, 0x4A09, 0x2401, 0x6014, 0x4602, 0x1C90, 0x460C, 0x1CA1, 0x8824, 0x8014, 0x3B02, 0xBF00, 0x4A05, 0x6812, 0xF002, 0x0201, 0x2A00, 0xD1F9, 0x2B00, 0xD1EC, 0xBE00, 0x2010, 0x4002, 0x200C, 0x4002, \ No newline at end of file diff --git a/src/include/general.h b/src/include/general.h index 2cb4bd9..cf8c1e6 100644 --- a/src/include/general.h +++ b/src/include/general.h @@ -38,5 +38,7 @@ #define DEBUG printf #endif +#define ALIGN(x, n) (((x) + (n) - 1) & ~((n) - 1)) + #endif diff --git a/src/stm32f1.c b/src/stm32f1.c index daba1b0..96faf1f 100644 --- a/src/stm32f1.c +++ b/src/stm32f1.c @@ -36,8 +36,6 @@ #include "command.h" #include "gdb_packet.h" -#define SRAM_BASE 0x20000000 - static bool stm32f1_cmd_erase_mass(target *t); static bool stm32f1_cmd_option(target *t, int argc, char *argv[]); @@ -123,9 +121,12 @@ static const char stm32hd_xml_memory_map[] = "" #define DBGMCU_IDCODE_F0 0x40015800 static const uint16_t stm32f1_flash_write_stub[] = { -#include "../flashstub/stm32.stub" +#include "../flashstub/stm32f1.stub" }; +#define SRAM_BASE 0x20000000 +#define STUB_BUFFER_BASE ALIGN(SRAM_BASE + sizeof(stm32f1_flash_write_stub), 4) + bool stm32f1_probe(struct target_s *target) { target->idcode = target_mem_read32(target, DBGMCU_IDCODE) & 0xfff; @@ -247,22 +248,18 @@ static int stm32f1_flash_write(struct target_s *target, uint32_t dest, const uint8_t *src, size_t len) { uint32_t offset = dest % 4; - uint32_t words = (offset + len + 3) / 4; - if (words > 256) - return -1; - uint32_t data[2 + words]; + uint8_t data[ALIGN(offset + len, 4)]; /* Construct data buffer used by stub */ - data[0] = dest - offset; - data[1] = words * 4; /* length must always be a multiple of 4 */ - data[2] = 0xFFFFFFFF; /* pad partial words with all 1s to avoid */ - data[words + 1] = 0xFFFFFFFF; /* damaging overlapping areas */ - memcpy((uint8_t *)&data[2] + offset, src, len); + /* pad partial words with all 1s to avoid damaging overlapping areas */ + memset(data, 0xff, sizeof(data)); + memcpy((uint8_t *)data + offset, src, len); /* Write stub and data to target ram and set PC */ - target_mem_write(target, 0x2000002C, data, sizeof(data)); - cortexm_run_stub(target, SRAM_BASE, stm32f1_flash_write_stub, 0x2C, - 0, 0, 0, 0); + target_mem_write(target, STUB_BUFFER_BASE, data, sizeof(data)); + cortexm_run_stub(target, SRAM_BASE, stm32f1_flash_write_stub, + sizeof(stm32f1_flash_write_stub), + dest - offset, STUB_BUFFER_BASE, sizeof(data), 0); /* Check for error */ if (target_mem_read32(target, FLASH_SR) & SR_ERROR_MASK) -- cgit v1.2.3 From 8ddb186b35267e90832deb2db4b0c58f77de4b87 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 8 Mar 2015 15:02:38 -0700 Subject: Allow stub to return an error code. --- flashstub/stm32f1.c | 9 ++++++++- flashstub/stm32f1.stub | 2 +- flashstub/stub.h | 30 ++++++++++++++++++++++++++++++ src/cortexm.c | 9 ++++++++- src/stm32f1.c | 15 +++++---------- 5 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 flashstub/stub.h diff --git a/flashstub/stm32f1.c b/flashstub/stm32f1.c index 8370114..2268eee 100644 --- a/flashstub/stm32f1.c +++ b/flashstub/stm32f1.c @@ -18,6 +18,9 @@ * along with this program. If not, see . */ #include "libopencm3/stm32/flash.h" +#include "stub.h" + +#define SR_ERROR_MASK 0x14 void __attribute__((naked)) stm32f1_flash_write_stub(uint16_t *dest, uint16_t *src, uint32_t size) @@ -29,6 +32,10 @@ stm32f1_flash_write_stub(uint16_t *dest, uint16_t *src, uint32_t size) while (FLASH_SR & FLASH_SR_BSY) ; } - asm("bkpt"); + + if (FLASH_SR & SR_ERROR_MASK) + stub_exit(1); + + stub_exit(0); } diff --git a/flashstub/stm32f1.stub b/flashstub/stm32f1.stub index 97d2a5e..428dbb0 100644 --- a/flashstub/stm32f1.stub +++ b/flashstub/stm32f1.stub @@ -1 +1 @@ -0x4613, 0xE010, 0x4A09, 0x2401, 0x6014, 0x4602, 0x1C90, 0x460C, 0x1CA1, 0x8824, 0x8014, 0x3B02, 0xBF00, 0x4A05, 0x6812, 0xF002, 0x0201, 0x2A00, 0xD1F9, 0x2B00, 0xD1EC, 0xBE00, 0x2010, 0x4002, 0x200C, 0x4002, \ No newline at end of file +0x4613, 0xE010, 0x4A0D, 0x2401, 0x6014, 0x4602, 0x1C90, 0x460C, 0x1CA1, 0x8824, 0x8014, 0x3B02, 0xBF00, 0x4A09, 0x6812, 0xF002, 0x0201, 0x2A00, 0xD1F9, 0x2B00, 0xD1EC, 0x4B05, 0x681B, 0xF003, 0x0314, 0x2B00, 0xD000, 0xBE01, 0xBE00, 0xBF00, 0x2010, 0x4002, 0x200C, 0x4002, \ No newline at end of file diff --git a/flashstub/stub.h b/flashstub/stub.h new file mode 100644 index 0000000..d279cf3 --- /dev/null +++ b/flashstub/stub.h @@ -0,0 +1,30 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2015 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef __STUB_H +#define __STUB_H + +static inline __attribute__((always_inline)) +stub_exit(const int code) +{ + asm("bkpt %0"::"i"(code)); +} + +#endif + diff --git a/src/cortexm.c b/src/cortexm.c index 0327edc..a129a3c 100644 --- a/src/cortexm.c +++ b/src/cortexm.c @@ -60,6 +60,7 @@ const struct command_s cortexm_cmd_list[] = { static int cortexm_regs_read(struct target_s *target, void *data); static int cortexm_regs_write(struct target_s *target, const void *data); static int cortexm_pc_write(struct target_s *target, const uint32_t val); +static uint32_t cortexm_pc_read(struct target_s *target); static void cortexm_reset(struct target_s *target); static int cortexm_halt_wait(struct target_s *target); @@ -216,6 +217,7 @@ cortexm_probe(struct target_s *target) target->regs_read = cortexm_regs_read; target->regs_write = cortexm_regs_write; target->pc_write = cortexm_pc_write; + target->pc_read = cortexm_pc_read; target->reset = cortexm_reset; target->halt_request = cortexm_halt_request; @@ -629,7 +631,12 @@ int cortexm_run_stub(struct target_s *target, uint32_t loadaddr, while (!cortexm_halt_wait(target)) ; - return 0; + uint32_t pc = cortexm_pc_read(target); + uint16_t bkpt_instr = target_mem_read16(target, pc); + if (bkpt_instr >> 8 != 0xbe) + return -2; + + return bkpt_instr & 0xff; } /* The following routines implement hardware breakpoints. diff --git a/src/stm32f1.c b/src/stm32f1.c index 96faf1f..8f7a7d7 100644 --- a/src/stm32f1.c +++ b/src/stm32f1.c @@ -256,16 +256,11 @@ static int stm32f1_flash_write(struct target_s *target, uint32_t dest, memcpy((uint8_t *)data + offset, src, len); /* Write stub and data to target ram and set PC */ - target_mem_write(target, STUB_BUFFER_BASE, data, sizeof(data)); - cortexm_run_stub(target, SRAM_BASE, stm32f1_flash_write_stub, - sizeof(stm32f1_flash_write_stub), - dest - offset, STUB_BUFFER_BASE, sizeof(data), 0); - - /* Check for error */ - if (target_mem_read32(target, FLASH_SR) & SR_ERROR_MASK) - return -1; - - return 0; + target_mem_write(target, STUB_BUFFER_BASE, (void*)data, sizeof(data)); + return cortexm_run_stub(target, SRAM_BASE, stm32f1_flash_write_stub, + sizeof(stm32f1_flash_write_stub), + dest - offset, STUB_BUFFER_BASE, sizeof(data), + 0); } static bool stm32f1_cmd_erase_mass(target *t) -- cgit v1.2.3 From acfd258aa7b02ea23a6d26c29c3432bf3e857039 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 8 Mar 2015 22:43:12 -0700 Subject: Rewrite STM32F4 stub in C and use new interface. --- flashstub/Makefile | 6 +++- flashstub/stm32f4.c | 40 ++++++++++++++++++++++++++ flashstub/stm32f4.s | 44 ---------------------------- flashstub/stm32f4.stub | 1 + src/stm32f4.c | 78 +++++++++++--------------------------------------- 5 files changed, 62 insertions(+), 107 deletions(-) create mode 100644 flashstub/stm32f4.c delete mode 100644 flashstub/stm32f4.s create mode 100644 flashstub/stm32f4.stub diff --git a/flashstub/Makefile b/flashstub/Makefile index eeb1803..c955ca8 100644 --- a/flashstub/Makefile +++ b/flashstub/Makefile @@ -8,7 +8,7 @@ ifneq ($(V), 1) Q = @ endif -CFLAGS=-mcpu=cortex-m3 -mthumb -I../libopencm3/include +CFLAGS=-Os -std=gnu99 -mcpu=cortex-m3 -mthumb -I../libopencm3/include ASFLAGS=-mcpu=cortex-m3 -mthumb all: lmi.stub stm32f4.stub nrf51.stub stm32f1.stub @@ -17,6 +17,10 @@ stm32f1.o: stm32f1.c $(Q)echo " CC $<" $(Q)$(CC) $(CFLAGS) -DSTM32F1 -o $@ -c $< +stm32f4.o: stm32f4.c + $(Q)echo " CC $<" + $(Q)$(CC) $(CFLAGS) -DSTM32F4 -o $@ -c $< + %.o: %.s $(Q)echo " AS $<" $(Q)$(AS) $(ASFLAGS) -o $@ $< diff --git a/flashstub/stm32f4.c b/flashstub/stm32f4.c new file mode 100644 index 0000000..6732897 --- /dev/null +++ b/flashstub/stm32f4.c @@ -0,0 +1,40 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2015 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "libopencm3/stm32/flash.h" +#include "stub.h" + +#define SR_ERROR_MASK 0xF2 + +void __attribute__((naked)) +stm32f4_flash_write_stub(uint32_t *dest, uint32_t *src, uint32_t size) +{ + for (int i = 0; i < size; i += 4) { + FLASH_CR = FLASH_CR_PROGRAM_X32 | FLASH_CR_PG; + *dest++ = *src++; + while (FLASH_SR & FLASH_SR_BSY) + ; + } + + if (FLASH_SR & SR_ERROR_MASK) + stub_exit(1); + + stub_exit(0); +} + diff --git a/flashstub/stm32f4.s b/flashstub/stm32f4.s deleted file mode 100644 index fa3fda0..0000000 --- a/flashstub/stm32f4.s +++ /dev/null @@ -1,44 +0,0 @@ -.global _start - -_start: - ldr r0, _flashbase - ldr r1, _addr - mov r2, pc - add r2, #(_data - . - 2) - ldr r3, _size - ldr r5, _cr -_next: - cbz r3, _done - @ Write PG command to FLASH_CR - str r5, [r0, #0x10] - @ Write data to flash (word) - ldr r4, [r2] - str r4, [r1] - -_wait: @ Wait for BSY bit to clear - ldrh r4, [r0, #0x0E] - mov r6, #1 - tst r4, r6 - bne _wait - - sub r3, #4 - add r1, #4 - add r2, #4 - b _next -_done: - bkpt - -@.align 4 -.org 0x28 -_cr: - .word 0x00000201 -_flashbase: - .word 0x40023C00 -_addr: - .word 0x0800bf78 -_size: - .word 8 -_data: - .word 0xAAAAAAAA - .word 0xBBBBBBBB - .word 0xCCCCCCCC diff --git a/flashstub/stm32f4.stub b/flashstub/stm32f4.stub new file mode 100644 index 0000000..25b5d9a --- /dev/null +++ b/flashstub/stm32f4.stub @@ -0,0 +1 @@ +0x2300, 0x4293, 0x4C09, 0xD20B, 0x4D09, 0xF240, 0x2601, 0x602E, 0x58CD, 0x50C5, 0x6825, 0xF415, 0x3F80, 0xD1FB, 0x3304, 0xE7F0, 0x6823, 0xF013, 0x0FF2, 0xD000, 0xBE01, 0xBE00, 0x3C0C, 0x4002, 0x3C10, 0x4002, \ No newline at end of file diff --git a/src/stm32f4.c b/src/stm32f4.c index 2a8bc21..fe844ca 100644 --- a/src/stm32f4.c +++ b/src/stm32f4.c @@ -33,6 +33,7 @@ #include "general.h" #include "adiv5.h" #include "target.h" +#include "cortexm.h" #include "command.h" #include "gdb_packet.h" @@ -120,44 +121,12 @@ static const char stm32f4_xml_memory_map[] = "" /* This routine is uses word access. Only usable on target voltage >2.7V */ static const uint16_t stm32f4_flash_write_stub[] = { -// _start: - 0x480a, // ldr r0, [pc, #40] // _flashbase - 0x490b, // ldr r1, [pc, #44] // _addr - 0x467a, // mov r2, pc - 0x3230, // adds r2, #48 - 0x4b0a, // ldr r3, [pc, #36] // _size - 0x4d07, // ldr r5, [pc, #28] // _cr -// _next: - 0xb153, // cbz r3, _done - 0x6105, // str r5, [r0, #16] - 0x6814, // ldr r4, [r2] - 0x600c, // str r4, [r1] -// _wait: - 0x89c4, // ldrb r4, [r0, #14] - 0x2601, // movs r6, #1 - 0x4234, // tst r4, r6 - 0xd1fb, // bne _wait - - 0x3b04, // subs r3, #4 - 0x3104, // adds r1, #4 - 0x3204, // adds r2, #4 - 0xe7f3, // b _next -// _done: - 0xbe00, // bkpt - 0x0000, -// .org 0x28 -//_cr: - 0x0201, 0x0000, //.word 0x00000201 (Value to write to FLASH_CR) */ -// _flashbase: - 0x3c00, 0x4002, // .word 0x40023c00 (FPEC_BASE) -// _addr: -// 0x0000, 0x0000, -// _size: -// 0x0000, 0x0000, -// _data: -// ... +#include "../flashstub/stm32f4.stub" }; +#define SRAM_BASE 0x20000000 +#define STUB_BUFFER_BASE ALIGN(SRAM_BASE + sizeof(stm32f4_flash_write_stub), 4) + bool stm32f4_probe(struct target_s *target) { uint32_t idcode; @@ -239,34 +208,19 @@ static int stm32f4_flash_write(struct target_s *target, uint32_t dest, const uint8_t *src, size_t len) { uint32_t offset = dest % 4; - uint32_t words = (offset + len + 3) / 4; - uint32_t data[2 + words]; - uint16_t sr; + uint8_t data[ALIGN(offset + len, 4)]; /* Construct data buffer used by stub */ - data[0] = dest - offset; - data[1] = words * 4; /* length must always be a multiple of 4 */ - data[2] = 0xFFFFFFFF; /* pad partial words with all 1s to avoid */ - data[words + 1] = 0xFFFFFFFF; /* damaging overlapping areas */ - memcpy((uint8_t *)&data[2] + offset, src, len); - - /* Write stub and data to target ram and set PC */ - target_mem_write(target, 0x20000000, stm32f4_flash_write_stub, 0x30); - target_mem_write(target, 0x20000030, data, sizeof(data)); - target_pc_write(target, 0x20000000); - if(target_check_error(target)) - return -1; - - /* Execute the stub */ - target_halt_resume(target, 0); - while(!target_halt_wait(target)); - - /* Check for error */ - sr = target_mem_read32(target, FLASH_SR); - if(sr & SR_ERROR_MASK) - return -1; - - return 0; + /* pad partial words with all 1s to avoid damaging overlapping areas */ + memset(data, 0xff, sizeof(data)); + memcpy((uint8_t *)data + offset, src, len); + + /* Write buffer to target ram call stub */ + target_mem_write(target, STUB_BUFFER_BASE, data, sizeof(data)); + return cortexm_run_stub(target, SRAM_BASE, stm32f4_flash_write_stub, + sizeof(stm32f4_flash_write_stub), + dest - offset, STUB_BUFFER_BASE, sizeof(data), + 0); } static bool stm32f4_cmd_erase_mass(target *t) -- cgit v1.2.3 From 41256e3c2ac211757ef4a3639075b1b5cab1a4f5 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 8 Mar 2015 22:45:53 -0700 Subject: Add missing return type to stub_exit. --- flashstub/stub.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flashstub/stub.h b/flashstub/stub.h index d279cf3..b837bae 100644 --- a/flashstub/stub.h +++ b/flashstub/stub.h @@ -20,7 +20,7 @@ #ifndef __STUB_H #define __STUB_H -static inline __attribute__((always_inline)) +static inline void __attribute__((always_inline)) stub_exit(const int code) { asm("bkpt %0"::"i"(code)); -- cgit v1.2.3 From 12e313dab9658ed41668a951b2a25160cafc208d Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 8 Mar 2015 22:53:51 -0700 Subject: Use for loop in STM32F1 stub and compile with -Os. --- flashstub/stm32f1.c | 3 +-- flashstub/stm32f1.stub | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/flashstub/stm32f1.c b/flashstub/stm32f1.c index 2268eee..f9ba0a1 100644 --- a/flashstub/stm32f1.c +++ b/flashstub/stm32f1.c @@ -25,10 +25,9 @@ void __attribute__((naked)) stm32f1_flash_write_stub(uint16_t *dest, uint16_t *src, uint32_t size) { - while (size) { + for (int i; i < size; i += 2) { FLASH_CR = FLASH_CR_PG; *dest++ = *src++; - size -= 2; while (FLASH_SR & FLASH_SR_BSY) ; } diff --git a/flashstub/stm32f1.stub b/flashstub/stm32f1.stub index 428dbb0..7f2914c 100644 --- a/flashstub/stm32f1.stub +++ b/flashstub/stm32f1.stub @@ -1 +1 @@ -0x4613, 0xE010, 0x4A0D, 0x2401, 0x6014, 0x4602, 0x1C90, 0x460C, 0x1CA1, 0x8824, 0x8014, 0x3B02, 0xBF00, 0x4A09, 0x6812, 0xF002, 0x0201, 0x2A00, 0xD1F9, 0x2B00, 0xD1EC, 0x4B05, 0x681B, 0xF003, 0x0314, 0x2B00, 0xD000, 0xBE01, 0xBE00, 0xBF00, 0x2010, 0x4002, 0x200C, 0x4002, \ No newline at end of file +0x2300, 0x4293, 0x4C09, 0xD20A, 0x4D09, 0x2601, 0x602E, 0x5ACD, 0x52C5, 0x6825, 0xF015, 0x0F01, 0xD1FB, 0x3302, 0xE7F1, 0x6823, 0xF013, 0x0F14, 0xD000, 0xBE01, 0xBE00, 0xBF00, 0x200C, 0x4002, 0x2010, 0x4002, \ No newline at end of file -- cgit v1.2.3