1 /* -*- tab-width:8 -*- */
4 Copyright (C) 2011 Peter Zotov <whitequark@whitequark.org>
5 Use of this source code is governed by a BSD-style
6 license that can be found in the LICENSE file.
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include "gdb-remote.h"
18 #include "stlink-hw.h"
20 static const char hex[] = "0123456789abcdef";
22 static const char* current_memory_map = NULL;
27 uint32_t max_flash_size, flash_pagesize;
29 uint32_t bootrom_base, bootrom_size;
31 { 0x412, "Low-density device",
32 0x8000, 0x400, 0x2800, 0x1ffff000, 0x800 },
33 { 0x410, "Medium-density device",
34 0x20000, 0x400, 0x5000, 0x1ffff000, 0x800 },
35 { 0x414, "High-density device",
36 0x80000, 0x800, 0x10000, 0x1ffff000, 0x800 },
37 { 0x418, "Connectivity line device",
38 0x40000, 0x800, 0x10000, 0x1fffb000, 0x4800 },
39 { 0x420, "Medium-density value line device",
40 0x20000, 0x400, 0x2000, 0x1ffff000, 0x800 },
41 { 0x428, "High-density value line device",
42 0x80000, 0x800, 0x8000, 0x1ffff000, 0x800 },
43 { 0x430, "XL-density device",
44 0x100000, 0x800, 0x18000, 0x1fffe000, 0x1800 },
48 int serve(struct stlink* sl, int port);
49 char* make_memory_map(const struct chip_params *params, uint32_t flash_size);
51 int main(int argc, char** argv) {
53 fprintf(stderr, "Usage: %s <port> /dev/sgX\n", argv[0]);
57 struct stlink *sl = stlink_quirk_open(argv[2], 0);
61 if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
62 stlink_enter_swd_mode(sl);
66 stlink_read_mem32(sl, 0xE0042000, 4);
67 chip_id = sl->q_buf[0] | (sl->q_buf[1] << 8) | (sl->q_buf[2] << 16) |
70 printf("Chip ID is %08x.\n", chip_id);
72 const struct chip_params* params = NULL;
74 for(int i = 0; i < sizeof(devices) / sizeof(devices[0]); i++) {
75 if(devices[i].chip_id == (chip_id & 0xFFF)) {
82 fprintf(stderr, "Cannot recognize the connected device!\n");
86 printf("Device connected: %s\n", params->description);
87 printf("Device parameters: SRAM: 0x%x bytes, Flash: up to 0x%x bytes in pages of 0x%x bytes\n",
88 params->sram_size, params->max_flash_size, params->flash_pagesize);
92 stlink_read_mem32(sl, 0x1FFFF7E0, 4);
93 flash_size = sl->q_buf[0] | (sl->q_buf[1] << 8);
95 printf("Flash size is %d KiB.\n", flash_size);
97 current_memory_map = make_memory_map(params, flash_size * 0x400);
99 int port = atoi(argv[1]);
101 while(serve(sl, port) == 0);
108 static const char* const memory_map_template =
109 "<?xml version=\"1.0\"?>"
110 "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
111 " \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
113 " <memory type=\"rom\" start=\"0x00000000\" length=\"0x%x\"/>" // code = sram, bootrom or flash; flash is bigger
114 " <memory type=\"ram\" start=\"0x20000000\" length=\"0x%x\"/>" // sram 8k
115 " <memory type=\"flash\" start=\"0x08000000\" length=\"0x%x\">"
116 " <property name=\"blocksize\">0x%x</property>"
118 " <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>" // peripheral regs
119 " <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>" // cortex regs
120 " <memory type=\"rom\" start=\"0x%08x\" length=\"0x%x\"/>" // bootrom
121 " <memory type=\"rom\" start=\"0x1ffff800\" length=\"0x8\"/>" // option byte area
124 char* make_memory_map(const struct chip_params *params, uint32_t flash_size) {
125 /* This will be freed in serve() */
126 char* map = malloc(4096);
129 snprintf(map, 4096, memory_map_template,
132 flash_size, params->flash_pagesize,
133 params->bootrom_base, params->bootrom_size);
140 * DWT_COMP0 0xE0001020
141 * DWT_MASK0 0xE0001024
142 * DWT_FUNCTION0 0xE0001028
143 * DWT_COMP1 0xE0001030
144 * DWT_MASK1 0xE0001034
145 * DWT_FUNCTION1 0xE0001038
146 * DWT_COMP2 0xE0001040
147 * DWT_MASK2 0xE0001044
148 * DWT_FUNCTION2 0xE0001048
149 * DWT_COMP3 0xE0001050
150 * DWT_MASK3 0xE0001054
151 * DWT_FUNCTION3 0xE0001058
154 #define DATA_WATCH_NUM 4
156 enum watchfun { WATCHDISABLED = 0, WATCHREAD = 5, WATCHWRITE = 6, WATCHACCESS = 7 };
158 struct code_hw_watchpoint {
164 struct code_hw_watchpoint data_watches[DATA_WATCH_NUM];
166 static void init_data_watchpoints(struct stlink *sl) {
170 printf("init watchpoints\n");
173 // set trcena in debug command to turn on dwt unit
174 stlink_read_mem32(sl, 0xE000EDFC, 4);
176 stlink_write_mem32(sl, 0xE000EDFC, 4);
178 // make sure all watchpoints are cleared
179 memset(sl->q_buf, 0, 4);
180 for(int i = 0; i < DATA_WATCH_NUM; i++) {
181 data_watches[i].fun = WATCHDISABLED;
182 stlink_write_mem32(sl, 0xe0001028 + i * 16, 4);
186 static int add_data_watchpoint(struct stlink* sl, enum watchfun wf, stm32_addr_t addr, unsigned int len)
192 // find a free watchpoint
202 if((mask != -1) && (mask < 16)) {
203 for(i = 0; i < DATA_WATCH_NUM; i++) {
204 // is this an empty slot ?
205 if(data_watches[i].fun == WATCHDISABLED) {
207 printf("insert watchpoint %d addr %x wf %u mask %u len %d\n", i, addr, wf, mask, len);
210 data_watches[i].fun = wf;
211 data_watches[i].addr = addr;
212 data_watches[i].mask = mask;
214 // insert comparator address
215 sl->q_buf[0] = (addr & 0xff);
216 sl->q_buf[1] = ((addr >> 8) & 0xff);
217 sl->q_buf[2] = ((addr >> 16) & 0xff);
218 sl->q_buf[3] = ((addr >> 24) & 0xff);
220 stlink_write_mem32(sl, 0xE0001020 + i * 16, 4);
223 memset(sl->q_buf, 0, 4);
225 stlink_write_mem32(sl, 0xE0001024 + i * 16, 4);
228 memset(sl->q_buf, 0, 4);
230 stlink_write_mem32(sl, 0xE0001028 + i * 16, 4);
232 // just to make sure the matched bit is clear !
233 stlink_read_mem32(sl, 0xE0001028 + i * 16, 4);
240 printf("failure: add watchpoints addr %x wf %u len %u\n", addr, wf, len);
245 static int delete_data_watchpoint(struct stlink* sl, stm32_addr_t addr)
249 for(i = 0 ; i < DATA_WATCH_NUM; i++) {
250 if((data_watches[i].addr == addr) && (data_watches[i].fun != WATCHDISABLED)) {
252 printf("delete watchpoint %d addr %x\n", i, addr);
255 memset(sl->q_buf, 0, 4);
256 data_watches[i].fun = WATCHDISABLED;
257 stlink_write_mem32(sl, 0xe0001028 + i * 16, 4);
264 printf("failure: delete watchpoint addr %x\n", addr);
270 #define CODE_BREAK_NUM 6
271 #define CODE_BREAK_LOW 0x01
272 #define CODE_BREAK_HIGH 0x02
274 struct code_hw_breakpoint {
279 struct code_hw_breakpoint code_breaks[CODE_BREAK_NUM];
281 static void init_code_breakpoints(struct stlink* sl) {
282 memset(sl->q_buf, 0, 4);
283 sl->q_buf[0] = 0x03; // KEY | ENABLE
284 stlink_write_mem32(sl, 0xe0002000, 4);
286 memset(sl->q_buf, 0, 4);
287 for(int i = 0; i < CODE_BREAK_NUM; i++) {
288 code_breaks[i].type = 0;
289 stlink_write_mem32(sl, 0xe0002008 + i * 4, 4);
293 static int update_code_breakpoint(struct stlink* sl, stm32_addr_t addr, int set) {
294 stm32_addr_t fpb_addr = addr & ~0x3;
295 int type = addr & 0x2 ? CODE_BREAK_HIGH : CODE_BREAK_LOW;
298 fprintf(stderr, "update_code_breakpoint: unaligned address %08x\n", addr);
303 for(int i = 0; i < CODE_BREAK_NUM; i++) {
304 if(fpb_addr == code_breaks[i].addr ||
305 (set && code_breaks[i].type == 0)) {
312 if(set) return -1; // Free slot not found
313 else return 0; // Breakpoint is already removed
316 struct code_hw_breakpoint* brk = &code_breaks[id];
318 brk->addr = fpb_addr;
320 if(set) brk->type |= type;
321 else brk->type &= ~type;
323 memset(sl->q_buf, 0, 4);
327 printf("clearing hw break %d\n", id);
330 stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
332 sl->q_buf[0] = ( brk->addr & 0xff) | 1;
333 sl->q_buf[1] = ((brk->addr >> 8) & 0xff);
334 sl->q_buf[2] = ((brk->addr >> 16) & 0xff);
335 sl->q_buf[3] = ((brk->addr >> 24) & 0xff) | (brk->type << 6);
338 printf("setting hw break %d at %08x (%d)\n",
339 id, brk->addr, brk->type);
340 printf("reg %02x %02x %02x %02x\n",
341 sl->q_buf[3], sl->q_buf[2], sl->q_buf[1], sl->q_buf[0]);
344 stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
350 #define FLASH_BASE 0x08000000
351 #define FLASH_PAGE 0x400
352 #define FLASH_PAGE_MASK (~((1 << 10) - 1))
353 #define FLASH_SIZE (FLASH_PAGE * 128)
360 struct flash_block* next;
363 static struct flash_block* flash_root;
365 static int flash_add_block(stm32_addr_t addr, unsigned length) {
366 if(addr < FLASH_BASE || addr + length > FLASH_BASE + FLASH_SIZE) {
367 fprintf(stderr, "flash_add_block: incorrect bounds\n");
371 if(addr % FLASH_PAGE != 0 || length % FLASH_PAGE != 0) {
372 fprintf(stderr, "flash_add_block: unaligned block\n");
376 struct flash_block* new = malloc(sizeof(struct flash_block));
377 new->next = flash_root;
380 new->length = length;
381 new->data = calloc(length, 1);
388 static int flash_populate(stm32_addr_t addr, uint8_t* data, unsigned length) {
389 int fit_blocks = 0, fit_length = 0;
391 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
392 /* Block: ------X------Y--------
396 * Block intersects with data, if:
400 unsigned X = fb->addr, Y = fb->addr + fb->length;
401 unsigned a = addr, b = addr + length;
403 // from start of the block
404 unsigned start = (a > X ? a : X) - X;
405 unsigned end = (b > Y ? Y : b) - X;
407 memcpy(fb->data + start, data, end - start);
410 fit_length += end - start;
414 if(fit_blocks == 0) {
415 fprintf(stderr, "Unfit data block %08x -> %04x\n", addr, length);
419 if(fit_length != length) {
420 fprintf(stderr, "warning: data block %08x -> %04x truncated to %04x\n",
421 addr, length, fit_length);
422 fprintf(stderr, "(this is not an error, just a GDB glitch)\n");
428 static int flash_go(struct stlink* sl) {
431 // Some kinds of clock settings do not allow writing to flash.
434 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
436 printf("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
439 unsigned length = fb->length;
440 for(stm32_addr_t page = fb->addr; page < fb->addr + fb->length; page += 0x400) {
442 printf("flash_do: page %08x\n", page);
445 stlink_erase_flash_page(sl, page);
447 if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
448 length > 0x400 ? 0x400 : length) < 0)
459 for(struct flash_block* fb = flash_root, *next; fb; fb = next) {
470 int serve(struct stlink* sl, int port) {
471 int sock = socket(AF_INET, SOCK_STREAM, 0);
477 unsigned int val = 1;
478 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
480 struct sockaddr_in serv_addr = {0};
481 serv_addr.sin_family = AF_INET;
482 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
483 serv_addr.sin_port = htons(port);
485 if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
490 if(listen(sock, 5) < 0) {
495 stlink_force_debug(sl);
497 init_code_breakpoints(sl);
498 init_data_watchpoints(sl);
500 printf("Listening at *:%d...\n", port);
502 int client = accept(sock, NULL, NULL);
510 printf("GDB connected.\n");
513 * To allow resetting the chip from GDB it is required to
514 * emulate attaching and detaching to target.
516 unsigned int attached = 1;
521 int status = gdb_recv_packet(client, &packet);
523 fprintf(stderr, "cannot recv: %d\n", status);
528 printf("recv: %s\n", packet);
535 if(packet[1] == 'P' || packet[1] == 'C' || packet[1] == 'L') {
540 char *separator = strstr(packet, ":"), *params = "";
541 if(separator == NULL) {
542 separator = packet + strlen(packet);
544 params = separator + 1;
547 unsigned queryNameLength = (separator - &packet[1]);
548 char* queryName = calloc(queryNameLength + 1, 1);
549 strncpy(queryName, &packet[1], queryNameLength);
552 printf("query: %s;%s\n", queryName, params);
555 if(!strcmp(queryName, "Supported")) {
556 reply = strdup("PacketSize=3fff;qXfer:memory-map:read+");
557 } else if(!strcmp(queryName, "Xfer")) {
558 char *type, *op, *annex, *s_addr, *s_length;
561 type = strsep(&tok, ":");
562 op = strsep(&tok, ":");
563 annex = strsep(&tok, ":");
564 s_addr = strsep(&tok, ",");
567 unsigned addr = strtoul(s_addr, NULL, 16),
568 length = strtoul(s_length, NULL, 16);
571 printf("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
572 type, op, annex, addr, length);
575 const char* data = NULL;
577 if(!strcmp(type, "memory-map") && !strcmp(op, "read"))
578 data = current_memory_map;
581 unsigned data_length = strlen(data);
582 if(addr + length > data_length)
583 length = data_length - addr;
588 reply = calloc(length + 2, 1);
590 strncpy(&reply[1], data, length);
605 char *cmdName = strtok_r(packet, ":;", ¶ms);
607 cmdName++; // vCommand -> Command
609 if(!strcmp(cmdName, "FlashErase")) {
610 char *s_addr, *s_length;
613 s_addr = strsep(&tok, ",");
616 unsigned addr = strtoul(s_addr, NULL, 16),
617 length = strtoul(s_length, NULL, 16);
620 printf("FlashErase: addr:%08x,len:%04x\n",
624 if(flash_add_block(addr, length) < 0) {
625 reply = strdup("E00");
627 reply = strdup("OK");
629 } else if(!strcmp(cmdName, "FlashWrite")) {
633 s_addr = strsep(&tok, ":");
636 unsigned addr = strtoul(s_addr, NULL, 16);
637 unsigned data_length = status - (data - packet);
639 // Length of decoded data cannot be more than
640 // encoded, as escapes are removed.
641 // Additional byte is reserved for alignment fix.
642 uint8_t *decoded = calloc(data_length + 1, 1);
643 unsigned dec_index = 0;
644 for(int i = 0; i < data_length; i++) {
645 if(data[i] == 0x7d) {
647 decoded[dec_index++] = data[i] ^ 0x20;
649 decoded[dec_index++] = data[i];
654 if(dec_index % 2 != 0)
658 printf("binary packet %d -> %d\n", data_length, dec_index);
661 if(flash_populate(addr, decoded, dec_index) < 0) {
662 reply = strdup("E00");
664 reply = strdup("OK");
666 } else if(!strcmp(cmdName, "FlashDone")) {
667 if(flash_go(sl) < 0) {
668 reply = strdup("E00");
670 reply = strdup("OK");
672 } else if(!strcmp(cmdName, "Kill")) {
675 reply = strdup("OK");
688 int status = gdb_check_for_interrupt(client);
690 fprintf(stderr, "cannot check for int: %d\n", status);
695 stlink_force_debug(sl);
700 if(sl->core_stat == STLINK_CORE_HALTED) {
707 reply = strdup("S05"); // TRAP
713 reply = strdup("S05"); // TRAP
718 reply = strdup("S05"); // TRAP
720 /* Stub shall reply OK if not attached. */
721 reply = strdup("OK");
726 stlink_read_all_regs(sl);
728 reply = calloc(8 * 16 + 1, 1);
729 for(int i = 0; i < 16; i++)
730 sprintf(&reply[i * 8], "%08x", htonl(sl->reg.r[i]));
735 unsigned id = strtoul(&packet[1], NULL, 16), reg = 0xDEADDEAD;
738 stlink_read_reg(sl, id);
739 reg = htonl(sl->reg.r[id]);
740 } else if(id == 0x19) {
741 stlink_read_reg(sl, 16);
742 reg = htonl(sl->reg.xpsr);
744 reply = strdup("E00");
747 reply = calloc(8 + 1, 1);
748 sprintf(reply, "%08x", reg);
754 char* s_reg = &packet[1];
755 char* s_value = strstr(&packet[1], "=") + 1;
757 unsigned reg = strtoul(s_reg, NULL, 16);
758 unsigned value = strtoul(s_value, NULL, 16);
761 stlink_write_reg(sl, ntohl(value), reg);
762 } else if(reg == 0x19) {
763 stlink_write_reg(sl, ntohl(value), 16);
765 reply = strdup("E00");
769 reply = strdup("OK");
776 for(int i = 0; i < 16; i++) {
778 strncpy(str, &packet[1 + i * 8], 8);
779 uint32_t reg = strtoul(str, NULL, 16);
780 stlink_write_reg(sl, ntohl(reg), i);
783 reply = strdup("OK");
787 char* s_start = &packet[1];
788 char* s_count = strstr(&packet[1], ",") + 1;
790 stm32_addr_t start = strtoul(s_start, NULL, 16);
791 unsigned count = strtoul(s_count, NULL, 16);
793 unsigned adj_start = start % 4;
795 stlink_read_mem32(sl, start - adj_start, (count % 4 == 0) ?
796 count : count + 4 - (count % 4));
798 reply = calloc(count * 2 + 1, 1);
799 for(int i = 0; i < count; i++) {
800 reply[i * 2 + 0] = hex[sl->q_buf[i + adj_start] >> 4];
801 reply[i * 2 + 1] = hex[sl->q_buf[i + adj_start] & 0xf];
808 char* s_start = &packet[1];
809 char* s_count = strstr(&packet[1], ",") + 1;
810 char* hexdata = strstr(packet, ":") + 1;
812 stm32_addr_t start = strtoul(s_start, NULL, 16);
813 unsigned count = strtoul(s_count, NULL, 16);
815 for(int i = 0; i < count; i ++) {
816 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
817 uint8_t byte = strtoul(hex, NULL, 16);
821 if((count % 4) == 0 && (start % 4) == 0) {
822 stlink_write_mem32(sl, start, count);
824 stlink_write_mem8(sl, start, count);
827 reply = strdup("OK");
834 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
835 stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
839 if(update_code_breakpoint(sl, addr, 1) < 0) {
840 reply = strdup("E00");
842 reply = strdup("OK");
846 case '2': // insert write watchpoint
847 case '3': // insert read watchpoint
848 case '4': // insert access watchpoint
851 if(packet[1] == '2') {
853 } else if(packet[1] == '3') {
857 if(add_data_watchpoint(sl, wf, addr, len) < 0) {
858 reply = strdup("E00");
860 reply = strdup("OK");
872 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
873 stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
875 case '1': // remove breakpoint
876 update_code_breakpoint(sl, addr, 0);
877 reply = strdup("OK");
880 case '2' : // remove write watchpoint
881 case '3' : // remove read watchpoint
882 case '4' : // remove access watchpoint
883 if(delete_data_watchpoint(sl, addr) < 0) {
884 reply = strdup("E00");
886 reply = strdup("OK");
898 * Enter extended mode which allows restarting.
899 * We do support that always.
902 reply = strdup("OK");
908 /* Reset the core. */
911 init_code_breakpoints(sl);
912 init_data_watchpoints(sl);
916 reply = strdup("OK");
927 printf("send: %s\n", reply);
930 int result = gdb_send_packet(client, reply);
932 fprintf(stderr, "cannot send: %d\n", result);