From: Peter Zotov Date: Tue, 15 Feb 2011 21:01:12 +0000 (+0300) Subject: Implemented flash writing. X-Git-Url: https://git.gag.com/?a=commitdiff_plain;ds=sidebyside;h=47bb36079b0dbfafc4fc54d3734f95de92587080;p=fw%2Fstlink Implemented flash writing. --- diff --git a/README b/README index 77697c8..47c7c2f 100644 --- a/README +++ b/README @@ -10,9 +10,36 @@ Then, in gdb: 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. diff --git a/src/gdb-server.c b/src/gdb-server.c index c616215..615bf06 100644 --- a/src/gdb-server.c +++ b/src/gdb-server.c @@ -222,7 +222,41 @@ int serve(struct stlink* sl, int port) { 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"); } diff --git a/src/stlink-hw.c b/src/stlink-hw.c index 92cd021..52f3971 100644 --- a/src/stlink-hw.c +++ b/src/stlink-hw.c @@ -1378,6 +1378,76 @@ static int stlink_fcheck_flash 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) { diff --git a/src/stlink-hw.h b/src/stlink-hw.h index b902c17..83146f3 100644 --- a/src/stlink-hw.h +++ b/src/stlink-hw.h @@ -157,5 +157,6 @@ void stlink_close(struct stlink *sl); int stlink_erase_flash_page(struct stlink* sl, stm32_addr_t page); int stlink_erase_flash_mass(struct stlink* sl); +int stlink_write_flash(struct stlink* sl, stm32_addr_t address, uint8_t* data, unsigned length); #endif