"<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
" \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
"<memory-map>"
- " <memory type=\"rom\" start=\"0x00000000\" length=\"0x100000\"/>" // code
- " <memory type=\"ram\" start=\"0x20000000\" length=\"0x100000\"/>" // sram
- " <memory type=\"flash\" start=\"0x08000000\" length=\"0x20000\">" // flash 128k
- " <property name=\"blocksize\">0x400</property>" // 1k pages
+ " <memory type=\"rom\" start=\"0x00000000\" length=\"0x20000\"/>" // code = sram or flash
+ " <memory type=\"ram\" start=\"0x20000000\" length=\"0x2000\"/>" // sram 8k
+ " <memory type=\"flash\" start=\"0x08000000\" length=\"0x20000\">" // flash 128k
+ " <property name=\"blocksize\">0x400</property>" // 1k pages
" </memory>"
+ " <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>" // peripheral regs
+ " <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>" // cortex regs
"</memory-map>";
int serve(struct stlink* sl, int port);
return 0;
}
+#define CODE_BREAK_NUM 6
+
+#define CODE_BREAK_LOW 0x01
+#define CODE_BREAK_HIGH 0x02
+
+struct code_hw_breakpoint {
+ stm32_addr_t addr;
+ int type;
+};
+
+struct code_hw_breakpoint code_breaks[CODE_BREAK_NUM];
+
+static void init_code_breakpoints(struct stlink* sl) {
+ memset(sl->q_buf, 0, 4);
+ sl->q_buf[0] = 0x03; // KEY | ENABLE
+ stlink_write_mem32(sl, 0xe0002000, 4);
+
+ memset(sl->q_buf, 0, 4);
+ for(int i = 0; i < CODE_BREAK_NUM; i++) {
+ code_breaks[i].type = 0;
+ stlink_write_mem32(sl, 0xe0002008 + i * 4, 4);
+ }
+}
+
+static int update_code_breakpoint(struct stlink* sl, stm32_addr_t addr, int set) {
+ stm32_addr_t fpb_addr = addr & ~0x3;
+ int type = addr & 0x2 ? CODE_BREAK_HIGH : CODE_BREAK_LOW;
+
+ if(addr & 1) {
+ fprintf(stderr, "update_code_breakpoint: unaligned address %08x\n", addr);
+ return -1;
+ }
+
+ int id = -1;
+ for(int i = 0; i < CODE_BREAK_NUM; i++) {
+ if(fpb_addr == code_breaks[i].addr ||
+ (set && code_breaks[i].type == 0)) {
+ id = i;
+ break;
+ }
+ }
+
+ if(id == -1) {
+ if(set) return -1; // Free slot not found
+ else return 0; // Breakpoint is already removed
+ }
+
+ struct code_hw_breakpoint* brk = &code_breaks[id];
+
+ brk->addr = fpb_addr;
+
+ if(set) brk->type |= type;
+ else brk->type &= ~type;
+
+ memset(sl->q_buf, 0, 4);
+
+ if(brk->type == 0) {
+ #ifdef DEBUG
+ printf("clearing hw break %d\n", id);
+ #endif
+
+ stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
+ } else {
+ sl->q_buf[0] = ( brk->addr & 0xff) | 1;
+ sl->q_buf[1] = ((brk->addr >> 8) & 0xff);
+ sl->q_buf[2] = ((brk->addr >> 16) & 0xff);
+ sl->q_buf[3] = ((brk->addr >> 24) & 0xff) | (brk->type << 6);
+
+ #ifdef DEBUG
+ printf("setting hw break %d at %08x (%d)\n",
+ id, brk->addr, brk->type);
+ printf("reg %02x %02x %02x %02x\n",
+ sl->q_buf[3], sl->q_buf[2], sl->q_buf[1], sl->q_buf[0]);
+ #endif
+
+ stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
+ }
+
+ return 0;
+}
+
+#define FLASH_BASE 0x08000000
+#define FLASH_PAGE 0x400
+#define FLASH_PAGE_MASK (~((1 << 10) - 1))
+#define FLASH_SIZE (FLASH_PAGE * 128)
+
+struct flash_block {
+ stm32_addr_t addr;
+ unsigned length;
+ uint8_t* data;
+
+ struct flash_block* next;
+};
+
+static struct flash_block* flash_root;
+
+static int flash_add_block(stm32_addr_t addr, unsigned length) {
+ if(addr < FLASH_BASE || addr + length > FLASH_BASE + FLASH_SIZE) {
+ fprintf(stderr, "flash_add_block: incorrect bounds\n");
+ return -1;
+ }
+
+ if(addr % FLASH_PAGE != 0 || length % FLASH_PAGE != 0) {
+ fprintf(stderr, "flash_add_block: unaligned block\n");
+ return -1;
+ }
+
+ struct flash_block* new = malloc(sizeof(struct flash_block));
+ new->next = flash_root;
+
+ new->addr = addr;
+ new->length = length;
+ new->data = calloc(length, 1);
+
+ flash_root = new;
+
+ return 0;
+}
+
+static int flash_populate(stm32_addr_t addr, uint8_t* data, unsigned length) {
+ int fit_blocks = 0, fit_length = 0;
+
+ for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
+ /* Block: ------X------Y--------
+ * Data: a-----b
+ * a--b
+ * a-----------b
+ * Block intersects with data, if:
+ * a < Y && b > x
+ */
+
+ unsigned X = fb->addr, Y = fb->addr + fb->length;
+ unsigned a = addr, b = addr + length;
+ if(a < Y && b > X) {
+ // from start of the block
+ unsigned start = (a > X ? a : X) - X;
+ unsigned end = (b > Y ? Y : b) - X;
+
+ memcpy(fb->data + start, data, end - start);
+
+ fit_blocks++;
+ fit_length += end - start;
+ }
+ }
+
+ if(fit_blocks == 0) {
+ fprintf(stderr, "Unfit data block %08x -> %04x\n", addr, length);
+ return -1;
+ }
+
+ if(fit_length != length) {
+ fprintf(stderr, "warning: data block %08x -> %04x truncated to %04x\n",
+ addr, length, fit_length);
+ fprintf(stderr, "(this is not an error, just a GDB glitch)\n");
+ }
+
+ return 0;
+}
+
+static int flash_go(struct stlink* sl) {
+ int error = -1;
+
+ // Some kinds of clock settings do not allow writing to flash.
+ stlink_reset(sl);
+
+ for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
+ #ifdef DEBUG
+ printf("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
+ #endif
+
+ unsigned length = fb->length;
+ for(stm32_addr_t page = fb->addr; page < fb->addr + fb->length; page += 0x400) {
+ #ifdef DEBUG
+ printf("flash_do: page %08x\n", page);
+ #endif
+
+ stlink_erase_flash_page(sl, page);
+
+ if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
+ length > 0x400 ? 0x400 : length) < 0)
+ goto error;
+ }
+
+ }
+
+ stlink_reset(sl);
+
+ error = 0;
+
+error:
+ for(struct flash_block* fb = flash_root, *next; fb; fb = next) {
+ next = fb->next;
+ free(fb->data);
+ free(fb);
+ }
+
+ flash_root = NULL;
+
+ return error;
+}
+
int serve(struct stlink* sl, int port) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0) {
stlink_force_debug(sl);
stlink_reset(sl);
+ init_code_breakpoints(sl);
printf("Listening at *:%d...\n", port);
printf("GDB connected.\n");
+ /*
+ * To allow resetting the chip from GDB it is required to
+ * emulate attaching and detaching to target.
+ */
+ unsigned int attached = 1;
+
while(1) {
char* packet;
}
case 'v': {
- char *separator = strstr(packet, ":"), *params = "";
- if(separator == NULL) {
- separator = packet + strlen(packet);
- } else {
- params = separator + 1;
- }
+ char *params = NULL;
+ char *cmdName = strtok_r(packet, ":;", ¶ms);
- unsigned cmdNameLength = (separator - &packet[1]);
- char* cmdName = calloc(cmdNameLength + 1, 1);
- strncpy(cmdName, &packet[1], cmdNameLength);
+ cmdName++; // vCommand -> Command
if(!strcmp(cmdName, "FlashErase")) {
char *s_addr, *s_length;
addr, length);
#endif
- for(stm32_addr_t cur = addr;
- cur < addr + length; cur += 0x400) {
- #ifdef DEBUG
- printf("do_erase: %08x\n", cur);
- #endif
+ if(flash_add_block(addr, length) < 0) {
+ reply = strdup("E00");
+ } else {
+ reply = strdup("OK");
+ }
+ } else if(!strcmp(cmdName, "FlashWrite")) {
+ char *s_addr, *data;
+ char *tok = params;
- stlink_erase_flash_page(sl, cur);
+ 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.
+ // Additional byte is reserved for alignment fix.
+ uint8_t *decoded = calloc(data_length + 1, 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];
+ }
}
- reply = strdup("OK");
- } else if(!strcmp(cmdName, "FlashWrite")) {
+ // Fix alignment
+ if(dec_index % 2 != 0)
+ dec_index++;
+
+ #ifdef DEBUG
+ printf("binary packet %d -> %d\n", data_length, dec_index);
+ #endif
+
+ if(flash_populate(addr, decoded, dec_index) < 0) {
+ reply = strdup("E00");
+ } else {
+ reply = strdup("OK");
+ }
} else if(!strcmp(cmdName, "FlashDone")) {
+ if(flash_go(sl) < 0) {
+ reply = strdup("E00");
+ } else {
+ reply = strdup("OK");
+ }
+ } else if(!strcmp(cmdName, "Kill")) {
+ attached = 0;
+
reply = strdup("OK");
}
if(reply == NULL)
reply = strdup("");
- free(cmdName);
-
break;
}
case 'c':
stlink_run(sl);
- printf("Core running, waiting for interrupt.\n");
+ while(1) {
+ int status = gdb_check_for_interrupt(client);
+ if(status < 0) {
+ fprintf(stderr, "cannot check for int: %d\n", status);
+ return 1;
+ }
- int status = gdb_wait_for_interrupt(client);
- if(status < 0) {
- fprintf(stderr, "cannot wait for int: %d\n", status);
- return 1;
- }
+ if(status == 1) {
+ stlink_force_debug(sl);
+ break;
+ }
- stlink_force_debug(sl);
+ stlink_status(sl);
+ if(sl->core_stat == STLINK_CORE_HALTED) {
+ break;
+ }
+
+ usleep(100000);
+ }
reply = strdup("S05"); // TRAP
break;
break;
case '?':
- reply = strdup("S05"); // TRAP
+ if(attached) {
+ reply = strdup("S05"); // TRAP
+ } else {
+ /* Stub shall reply OK if not attached. */
+ reply = strdup("OK");
+ }
break;
case 'g':
break;
}
- case 'k': {
- // After this function will be entered afterwards, the
- // chip will be reset anyway. So this is a no-op.
+ case 'Z': {
+ if(packet[1] == '1') {
+ stm32_addr_t addr = strtoul(&packet[3], NULL, 16);
+ if(update_code_breakpoint(sl, addr, 1) < 0) {
+ reply = strdup("E00");
+ } else {
+ reply = strdup("OK");
+ }
+ } else {
+ reply = strdup("");
+ }
+
+ break;
+ }
+
+ case 'z': {
+ if(packet[1] == '1') {
+ stm32_addr_t addr = strtoul(&packet[3], NULL, 16);
+ update_code_breakpoint(sl, addr, 0);
- close(client);
- return 0;
+ reply = strdup("OK");
+ } else {
+ reply = strdup("");
+ }
+
+ break;
+ }
+
+ case '!': {
+ /*
+ * Enter extended mode which allows restarting.
+ * We do support that always.
+ */
+
+ reply = strdup("OK");
+
+ break;
+ }
+
+ case 'R': {
+ /* Reset the core. */
+
+ stlink_reset(sl);
+ init_code_breakpoints(sl);
+
+ attached = 1;
+
+ reply = strdup("OK");
+
+ break;
}
default: