Implemented flash writing.
authorPeter Zotov <whitequark@whitequark.org>
Tue, 15 Feb 2011 21:01:12 +0000 (00:01 +0300)
committerPeter Zotov <whitequark@whitequark.org>
Tue, 15 Feb 2011 21:01:12 +0000 (00:01 +0300)
README
src/gdb-server.c
src/stlink-hw.c
src/stlink-hw.h

diff --git a/README b/README
index 77697c89b50864c19b9a2669f1ef42215c1567d9..47c7c2f05e81bb47128f7fc15731cadb4d4de903 100644 (file)
--- 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.
index c6162153adc13ae6f931860dd96ea84e9afadf96..615bf06c04e5d872911c88be734047df9f63b339 100644 (file)
@@ -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");
                        }
 
index 92cd021e868c68cf5237e9c2a6f8a3ecd435737e..52f3971eb554b8df5c79a9427383a8ba0229fe2a 100644 (file)
@@ -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)
 {
index b902c1762073810c69d6bc634555ff281d07d9d9..83146f3c6f2b83f14c4198ee2d791f60ee3330fb 100644 (file)
@@ -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