Have fun!
+Running programs from SRAM
+==========================
+
+You can run your firmware directly from SRAM if you want to.
+Just link it at 0x20000000 and do
+(gdb) load firmware.elf
+
+It will be loaded, and pc will be adjusted to point to start of the
+code, if it is linked correctly (i.e. ELF has correct entry point).
+
+Writing to flash
+================
+
+The GDB stub ships with a correct memory map, including the flash area.
+If you would link your executable to 0x08000000 and then do
+(gdb) load firmware.elf
+then it would be written to the memory.
+
Caveats
=======
`continue' GDB command does not work: target does not step at
all or steps with a turtle speed. Looks like there's something
wrong with SCSI requests.
+
+GDB sends requests for a multi-sectioned ELF files (most ones;
+having both .text and .rodata is enough) in a quite strange way which
+absolutely does not conform to flash page boundaries. Which is even more
+weird when you think about FlashErase requests which it sends correctly.
+And I couldn't think of a way which will resolve this correctly now.
+
+Hardware breakpoints are not supported yet. You can still run your code from
+RAM, and then GDB will insert bkpt opcodes automagically.
reply = strdup("OK");
} else if(!strcmp(cmdName, "FlashWrite")) {
+ char *s_addr, *data;
+ char *tok = params;
+
+ s_addr = strsep(&tok, ":");
+ data = tok;
+
+ unsigned addr = strtoul(s_addr, NULL, 16);
+ unsigned data_length = status - (data - packet);
+
+ // length of decoded data cannot be more than
+ // encoded, as escapes are removed
+ uint8_t *decoded = calloc(data_length, 1);
+ unsigned dec_index = 0;
+ for(int i = 0; i < data_length; i++) {
+ if(data[i] == 0x7d) {
+ i++;
+ decoded[dec_index++] = data[i] ^ 0x20;
+ } else {
+ decoded[dec_index++] = data[i];
+ }
+ }
+
+ #ifdef DEBUG
+ printf("binary packet %d -> %d\n", data_length, dec_index);
+ #endif
+
+ if(!stlink_write_flash(sl, addr, decoded, dec_index) < 0) {
+ fprintf(stderr, "Flash write or verification failed.\n");
+ reply = strdup("E00");
+ } else {
+ reply = strdup("OK");
+ }
} else if(!strcmp(cmdName, "FlashDone")) {
+ stlink_reset(sl);
+
reply = strdup("OK");
}
return res;
}
+// The stlink_fwrite_flash should not muck with mmapped files inside itself,
+// and should use this function instead. (Hell, what's the reason behind mmap
+// there?!) But, as it is not actually used anywhere, nobody cares.
+
+#define WRITE_BLOCK_SIZE 0x40
+int stlink_write_flash(struct stlink* sl, stm32_addr_t addr, uint8_t* base, unsigned len) {
+ int error = -1;
+ size_t off;
+ flash_loader_t fl;
+
+ /* check addr range is inside the flash */
+ if (addr < sl->flash_base) {
+ fprintf(stderr, "addr too low\n");
+ return -1;
+ } else if ((addr + len) < addr) {
+ fprintf(stderr, "addr overruns\n");
+ return -1;
+ } else if ((addr + len) > (sl->flash_base + sl->flash_size)) {
+ fprintf(stderr, "addr too high\n");
+ return -1;
+ } else if ((addr & 1) || (len & 1)) {
+ fprintf(stderr, "unaligned addr or size\n");
+ return -1;
+ }
+
+ /* flash loader initialization */
+ if (init_flash_loader(sl, &fl) == -1) {
+ fprintf(stderr, "init_flash_loader() == -1\n");
+ return -1;
+ }
+
+ /* write each page. above WRITE_BLOCK_SIZE fails? */
+ for (off = 0; off < len; off += WRITE_BLOCK_SIZE) {
+ /* adjust last write size */
+ size_t size = WRITE_BLOCK_SIZE;
+ if((off + WRITE_BLOCK_SIZE) > len)
+ size = len - off;
+ printf("writing %d\n", size);
+
+ // By some weird reason it fails with an error like
+ // write error, count == 31
+ // but it still writes all the data correctly
+ // so, just ignore it, we are checking the data anyway
+ if(run_flash_loader(sl, &fl, addr + off, base + off, size) == -1) {
+ //fprintf(stderr, "run_flash_loader(0x%x) == -1\n", addr + off);
+ //return -1;
+ }
+ }
+
+ for(off = 0; off < len; off += sl->flash_pgsz) {
+ size_t aligned_size;
+
+ /* adjust last page size */
+ size_t cmp_size = sl->flash_pgsz;
+ if ((off + sl->flash_pgsz) > len)
+ cmp_size = len - off;
+
+ aligned_size = cmp_size;
+ if (aligned_size & (4 - 1))
+ aligned_size = (cmp_size + 4) & ~(4 - 1);
+
+ stlink_read_mem32(sl, addr + off, aligned_size);
+
+ if (memcmp(sl->q_buf, base + off, cmp_size))
+ return -1;
+ }
+
+ return 0;
+}
+
static int stlink_fwrite_flash
(struct stlink* sl, const char* path, stm32_addr_t addr)
{