2 * Copyright (C) 2011 Peter Zotov <whitequark@whitequark.org>
3 * Use of this source code is governed by a BSD-style
4 * license that can be found in the LICENSE file.
14 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
23 #include <stlink-common.h>
24 #include <uglylogging.h>
26 #include "gdb-remote.h"
27 #include "gdb-server.h"
29 #define FLASH_BASE 0x08000000
31 //Allways update the FLASH_PAGE before each use, by calling stlink_calculate_pagesize
32 #define FLASH_PAGE (sl->flash_pgsz)
34 stlink_t *connected_stlink = NULL;
36 static const char hex[] = "0123456789abcdef";
38 static const char* current_memory_map = NULL;
40 typedef struct _st_state_t {
41 // things from command line, bleh
50 int serve(stlink_t *sl, st_state_t *st);
51 char* make_memory_map(stlink_t *sl);
53 static void cleanup(int signal __attribute__((unused))) {
54 if (connected_stlink) {
55 /* Switch back to mass storage mode before closing. */
56 stlink_run(connected_stlink);
57 stlink_exit_debug_mode(connected_stlink);
58 stlink_close(connected_stlink);
66 int parse_options(int argc, char** argv, st_state_t *st) {
67 static struct option long_options[] = {
68 {"help", no_argument, NULL, 'h'},
69 {"verbose", optional_argument, NULL, 'v'},
70 {"stlink_version", required_argument, NULL, 's'},
71 {"stlinkv1", no_argument, NULL, '1'},
72 {"listen_port", required_argument, NULL, 'p'},
73 {"multi", optional_argument, NULL, 'm'},
74 {"no-reset", optional_argument, NULL, 'n'},
77 const char * help_str = "%s - usage:\n\n"
78 " -h, --help\t\tPrint this help\n"
79 " -vXX, --verbose=XX\tSpecify a specific verbosity level (0..99)\n"
80 " -v, --verbose\t\tSpecify generally verbose logging\n"
81 " -s X, --stlink_version=X\n"
82 "\t\t\tChoose what version of stlink to use, (defaults to 2)\n"
83 " -1, --stlinkv1\tForce stlink version 1\n"
84 " -p 4242, --listen_port=1234\n"
85 "\t\t\tSet the gdb server listen port. "
86 "(default port: " STRINGIFY(DEFAULT_GDB_LISTEN_PORT) ")\n"
88 "\t\t\tSet gdb server to extended mode.\n"
89 "\t\t\tst-util will continue listening for connections after disconnect.\n"
91 "\t\t\tDo not reset board on connection.\n"
93 "The STLINKv2 device to use can be specified in the environment\n"
94 "variable STLINK_DEVICE on the format <USB_BUS>:<USB_ADDR>.\n"
102 while ((c = getopt_long(argc, argv, "hv::s:1p:mn", long_options, &option_index)) != -1) {
105 printf("XXXXX Shouldn't really normally come here, only if there's no corresponding option\n");
106 printf("option %s", long_options[option_index].name);
108 printf(" with arg %s", optarg);
113 printf(help_str, argv[0]);
118 st->logging_level = atoi(optarg);
120 st->logging_level = DEFAULT_LOGGING_LEVEL;
124 st->stlink_version = 1;
127 sscanf(optarg, "%i", &q);
128 if (q < 0 || q > 2) {
129 fprintf(stderr, "stlink version %d unknown!\n", q);
132 st->stlink_version = q;
135 sscanf(optarg, "%i", &q);
137 fprintf(stderr, "Can't use a negative port to listen on: %d\n", q);
152 printf("non-option ARGV-elements: ");
153 while (optind < argc)
154 printf("%s ", argv[optind++]);
161 int main(int argc, char** argv) {
167 memset(&state, 0, sizeof(state));
169 state.stlink_version = 2;
170 state.logging_level = DEFAULT_LOGGING_LEVEL;
171 state.listen_port = DEFAULT_GDB_LISTEN_PORT;
172 state.reset = 1; /* By default, reset board */
173 parse_options(argc, argv, &state);
174 switch (state.stlink_version) {
176 sl = stlink_open_usb(state.logging_level, 0);
177 if(sl == NULL) return 1;
180 sl = stlink_v1_open(state.logging_level, 0);
181 if(sl == NULL) return 1;
185 connected_stlink = sl;
186 signal(SIGINT, &cleanup);
187 signal(SIGTERM, &cleanup);
193 ILOG("Chip ID is %08x, Core ID is %08x.\n", sl->chip_id, sl->core_id);
195 voltage = stlink_target_voltage(sl);
197 ILOG("Target voltage is %d mV.\n", voltage);
202 current_memory_map = make_memory_map(sl);
206 if (WSAStartup(MAKEWORD(2,2),&wsadata) !=0 ) {
216 } while (state.persistent);
223 /* Switch back to mass storage mode before closing. */
224 stlink_exit_debug_mode(sl);
230 static const char* const target_description_F4 =
231 "<?xml version=\"1.0\"?>"
232 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
233 "<target version=\"1.0\">"
234 " <architecture>arm</architecture>"
235 " <feature name=\"org.gnu.gdb.arm.m-profile\">"
236 " <reg name=\"r0\" bitsize=\"32\"/>"
237 " <reg name=\"r1\" bitsize=\"32\"/>"
238 " <reg name=\"r2\" bitsize=\"32\"/>"
239 " <reg name=\"r3\" bitsize=\"32\"/>"
240 " <reg name=\"r4\" bitsize=\"32\"/>"
241 " <reg name=\"r5\" bitsize=\"32\"/>"
242 " <reg name=\"r6\" bitsize=\"32\"/>"
243 " <reg name=\"r7\" bitsize=\"32\"/>"
244 " <reg name=\"r8\" bitsize=\"32\"/>"
245 " <reg name=\"r9\" bitsize=\"32\"/>"
246 " <reg name=\"r10\" bitsize=\"32\"/>"
247 " <reg name=\"r11\" bitsize=\"32\"/>"
248 " <reg name=\"r12\" bitsize=\"32\"/>"
249 " <reg name=\"sp\" bitsize=\"32\" type=\"data_ptr\"/>"
250 " <reg name=\"lr\" bitsize=\"32\"/>"
251 " <reg name=\"pc\" bitsize=\"32\" type=\"code_ptr\"/>"
252 " <reg name=\"xpsr\" bitsize=\"32\" regnum=\"25\"/>"
253 " <reg name=\"msp\" bitsize=\"32\" regnum=\"26\" type=\"data_ptr\" group=\"general\" />"
254 " <reg name=\"psp\" bitsize=\"32\" regnum=\"27\" type=\"data_ptr\" group=\"general\" />"
255 " <reg name=\"control\" bitsize=\"8\" regnum=\"28\" type=\"int\" group=\"general\" />"
256 " <reg name=\"faultmask\" bitsize=\"8\" regnum=\"29\" type=\"int\" group=\"general\" />"
257 " <reg name=\"basepri\" bitsize=\"8\" regnum=\"30\" type=\"int\" group=\"general\" />"
258 " <reg name=\"primask\" bitsize=\"8\" regnum=\"31\" type=\"int\" group=\"general\" />"
259 " <reg name=\"s0\" bitsize=\"32\" regnum=\"32\" type=\"float\" group=\"float\" />"
260 " <reg name=\"s1\" bitsize=\"32\" type=\"float\" group=\"float\" />"
261 " <reg name=\"s2\" bitsize=\"32\" type=\"float\" group=\"float\" />"
262 " <reg name=\"s3\" bitsize=\"32\" type=\"float\" group=\"float\" />"
263 " <reg name=\"s4\" bitsize=\"32\" type=\"float\" group=\"float\" />"
264 " <reg name=\"s5\" bitsize=\"32\" type=\"float\" group=\"float\" />"
265 " <reg name=\"s6\" bitsize=\"32\" type=\"float\" group=\"float\" />"
266 " <reg name=\"s7\" bitsize=\"32\" type=\"float\" group=\"float\" />"
267 " <reg name=\"s8\" bitsize=\"32\" type=\"float\" group=\"float\" />"
268 " <reg name=\"s9\" bitsize=\"32\" type=\"float\" group=\"float\" />"
269 " <reg name=\"s10\" bitsize=\"32\" type=\"float\" group=\"float\" />"
270 " <reg name=\"s11\" bitsize=\"32\" type=\"float\" group=\"float\" />"
271 " <reg name=\"s12\" bitsize=\"32\" type=\"float\" group=\"float\" />"
272 " <reg name=\"s13\" bitsize=\"32\" type=\"float\" group=\"float\" />"
273 " <reg name=\"s14\" bitsize=\"32\" type=\"float\" group=\"float\" />"
274 " <reg name=\"s15\" bitsize=\"32\" type=\"float\" group=\"float\" />"
275 " <reg name=\"s16\" bitsize=\"32\" type=\"float\" group=\"float\" />"
276 " <reg name=\"s17\" bitsize=\"32\" type=\"float\" group=\"float\" />"
277 " <reg name=\"s18\" bitsize=\"32\" type=\"float\" group=\"float\" />"
278 " <reg name=\"s19\" bitsize=\"32\" type=\"float\" group=\"float\" />"
279 " <reg name=\"s20\" bitsize=\"32\" type=\"float\" group=\"float\" />"
280 " <reg name=\"s21\" bitsize=\"32\" type=\"float\" group=\"float\" />"
281 " <reg name=\"s22\" bitsize=\"32\" type=\"float\" group=\"float\" />"
282 " <reg name=\"s23\" bitsize=\"32\" type=\"float\" group=\"float\" />"
283 " <reg name=\"s24\" bitsize=\"32\" type=\"float\" group=\"float\" />"
284 " <reg name=\"s25\" bitsize=\"32\" type=\"float\" group=\"float\" />"
285 " <reg name=\"s26\" bitsize=\"32\" type=\"float\" group=\"float\" />"
286 " <reg name=\"s27\" bitsize=\"32\" type=\"float\" group=\"float\" />"
287 " <reg name=\"s28\" bitsize=\"32\" type=\"float\" group=\"float\" />"
288 " <reg name=\"s29\" bitsize=\"32\" type=\"float\" group=\"float\" />"
289 " <reg name=\"s30\" bitsize=\"32\" type=\"float\" group=\"float\" />"
290 " <reg name=\"s31\" bitsize=\"32\" type=\"float\" group=\"float\" />"
291 " <reg name=\"fpscr\" bitsize=\"32\" type=\"int\" group=\"float\" />"
295 static const char* const memory_map_template_F4 =
296 "<?xml version=\"1.0\"?>"
297 "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
298 " \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
300 " <memory type=\"rom\" start=\"0x00000000\" length=\"0x100000\"/>" // code = sram, bootrom or flash; flash is bigger
301 " <memory type=\"ram\" start=\"0x10000000\" length=\"0x10000\"/>" // ccm ram
302 " <memory type=\"ram\" start=\"0x20000000\" length=\"0x20000\"/>" // sram
303 " <memory type=\"flash\" start=\"0x08000000\" length=\"0x10000\">" //Sectors 0..3
304 " <property name=\"blocksize\">0x4000</property>" //16kB
306 " <memory type=\"flash\" start=\"0x08010000\" length=\"0x10000\">" //Sector 4
307 " <property name=\"blocksize\">0x10000</property>" //64kB
309 " <memory type=\"flash\" start=\"0x08020000\" length=\"0x70000\">" //Sectors 5..11
310 " <property name=\"blocksize\">0x20000</property>" //128kB
312 " <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>" // peripheral regs
313 " <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>" // cortex regs
314 " <memory type=\"rom\" start=\"0x1fff0000\" length=\"0x7800\"/>" // bootrom
315 " <memory type=\"rom\" start=\"0x1fffc000\" length=\"0x10\"/>" // option byte area
318 static const char* const memory_map_template =
319 "<?xml version=\"1.0\"?>"
320 "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
321 " \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
323 " <memory type=\"rom\" start=\"0x00000000\" length=\"0x%zx\"/>" // code = sram, bootrom or flash; flash is bigger
324 " <memory type=\"ram\" start=\"0x20000000\" length=\"0x%zx\"/>" // sram 8k
325 " <memory type=\"flash\" start=\"0x08000000\" length=\"0x%zx\">"
326 " <property name=\"blocksize\">0x%zx</property>"
328 " <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>" // peripheral regs
329 " <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>" // cortex regs
330 " <memory type=\"rom\" start=\"0x%08x\" length=\"0x%zx\"/>" // bootrom
331 " <memory type=\"rom\" start=\"0x1ffff800\" length=\"0x10\"/>" // option byte area
334 char* make_memory_map(stlink_t *sl) {
335 /* This will be freed in serve() */
336 char* map = malloc(4096);
339 if(sl->chip_id==STM32_CHIPID_F4) {
340 strcpy(map, memory_map_template_F4);
342 snprintf(map, 4096, memory_map_template,
345 sl->flash_size, sl->flash_pgsz,
346 sl->sys_base, sl->sys_size);
353 * DWT_COMP0 0xE0001020
354 * DWT_MASK0 0xE0001024
355 * DWT_FUNCTION0 0xE0001028
356 * DWT_COMP1 0xE0001030
357 * DWT_MASK1 0xE0001034
358 * DWT_FUNCTION1 0xE0001038
359 * DWT_COMP2 0xE0001040
360 * DWT_MASK2 0xE0001044
361 * DWT_FUNCTION2 0xE0001048
362 * DWT_COMP3 0xE0001050
363 * DWT_MASK3 0xE0001054
364 * DWT_FUNCTION3 0xE0001058
367 #define DATA_WATCH_NUM 4
369 enum watchfun { WATCHDISABLED = 0, WATCHREAD = 5, WATCHWRITE = 6, WATCHACCESS = 7 };
371 struct code_hw_watchpoint {
377 struct code_hw_watchpoint data_watches[DATA_WATCH_NUM];
379 static void init_data_watchpoints(stlink_t *sl) {
380 DLOG("init watchpoints\n");
382 // set trcena in debug command to turn on dwt unit
383 stlink_write_debug32(sl, 0xE000EDFC,
384 stlink_read_debug32(sl, 0xE000EDFC) | (1<<24));
386 // make sure all watchpoints are cleared
387 for(int i = 0; i < DATA_WATCH_NUM; i++) {
388 data_watches[i].fun = WATCHDISABLED;
389 stlink_write_debug32(sl, 0xe0001028 + i * 16, 0);
393 static int add_data_watchpoint(stlink_t *sl, enum watchfun wf,
394 stm32_addr_t addr, unsigned int len) {
399 // find a free watchpoint
409 if((mask != (uint32_t)-1) && (mask < 16)) {
410 for(i = 0; i < DATA_WATCH_NUM; i++) {
411 // is this an empty slot ?
412 if(data_watches[i].fun == WATCHDISABLED) {
413 DLOG("insert watchpoint %d addr %x wf %u mask %u len %d\n", i, addr, wf, mask, len);
415 data_watches[i].fun = wf;
416 data_watches[i].addr = addr;
417 data_watches[i].mask = mask;
419 // insert comparator address
420 stlink_write_debug32(sl, 0xE0001020 + i * 16, addr);
423 stlink_write_debug32(sl, 0xE0001024 + i * 16, mask);
426 stlink_write_debug32(sl, 0xE0001028 + i * 16, wf);
428 // just to make sure the matched bit is clear !
429 stlink_read_debug32(sl, 0xE0001028 + i * 16);
435 DLOG("failure: add watchpoints addr %x wf %u len %u\n", addr, wf, len);
439 static int delete_data_watchpoint(stlink_t *sl, stm32_addr_t addr)
443 for(i = 0 ; i < DATA_WATCH_NUM; i++) {
444 if((data_watches[i].addr == addr) && (data_watches[i].fun != WATCHDISABLED)) {
445 DLOG("delete watchpoint %d addr %x\n", i, addr);
447 data_watches[i].fun = WATCHDISABLED;
448 stlink_write_debug32(sl, 0xe0001028 + i * 16, 0);
454 DLOG("failure: delete watchpoint addr %x\n", addr);
459 #define CODE_BREAK_NUM 6
460 #define CODE_LIT_NUM 2
461 #define CODE_BREAK_LOW 0x01
462 #define CODE_BREAK_HIGH 0x02
464 struct code_hw_breakpoint {
469 struct code_hw_breakpoint code_breaks[CODE_BREAK_NUM];
471 static void init_code_breakpoints(stlink_t *sl) {
472 memset(sl->q_buf, 0, 4);
473 stlink_write_debug32(sl, CM3_REG_FP_CTRL, 0x03 /*KEY | ENABLE4*/);
474 unsigned int val = stlink_read_debug32(sl, CM3_REG_FP_CTRL);
475 if (((val & 3) != 1) ||
476 ((((val >> 8) & 0x70) | ((val >> 4) & 0xf)) != CODE_BREAK_NUM) ||
477 (((val >> 8) & 0xf) != CODE_LIT_NUM)){
478 ELOG("[FP_CTRL] = 0x%08x expecting 0x%08x\n", val,
479 ((CODE_BREAK_NUM & 0x70) << 8) | (CODE_LIT_NUM << 8) | ((CODE_BREAK_NUM & 0xf) << 4) | 1);
483 for(int i = 0; i < CODE_BREAK_NUM; i++) {
484 code_breaks[i].type = 0;
485 stlink_write_debug32(sl, CM3_REG_FP_COMP0 + i * 4, 0);
489 static int update_code_breakpoint(stlink_t *sl, stm32_addr_t addr, int set) {
490 stm32_addr_t fpb_addr = addr & ~0x3;
491 int type = addr & 0x2 ? CODE_BREAK_HIGH : CODE_BREAK_LOW;
494 ELOG("update_code_breakpoint: unaligned address %08x\n", addr);
499 for(int i = 0; i < CODE_BREAK_NUM; i++) {
500 if(fpb_addr == code_breaks[i].addr ||
501 (set && code_breaks[i].type == 0)) {
508 if(set) return -1; // Free slot not found
509 else return 0; // Breakpoint is already removed
512 struct code_hw_breakpoint* brk = &code_breaks[id];
514 brk->addr = fpb_addr;
516 if(set) brk->type |= type;
517 else brk->type &= ~type;
520 DLOG("clearing hw break %d\n", id);
522 stlink_write_debug32(sl, 0xe0002008 + id * 4, 0);
524 uint32_t mask = (brk->addr) | 1 | (brk->type << 30);
526 DLOG("setting hw break %d at %08x (%d)\n",
527 id, brk->addr, brk->type);
531 stlink_write_debug32(sl, 0xe0002008 + id * 4, mask);
543 struct flash_block* next;
546 static struct flash_block* flash_root;
548 static int flash_add_block(stm32_addr_t addr, unsigned length, stlink_t *sl) {
550 if(addr < FLASH_BASE || addr + length > FLASH_BASE + sl->flash_size) {
551 ELOG("flash_add_block: incorrect bounds\n");
555 stlink_calculate_pagesize(sl, addr);
556 if(addr % FLASH_PAGE != 0 || length % FLASH_PAGE != 0) {
557 ELOG("flash_add_block: unaligned block\n");
561 struct flash_block* new = malloc(sizeof(struct flash_block));
562 new->next = flash_root;
565 new->length = length;
566 new->data = calloc(length, 1);
573 static int flash_populate(stm32_addr_t addr, uint8_t* data, unsigned length) {
574 unsigned int fit_blocks = 0, fit_length = 0;
576 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
577 /* Block: ------X------Y--------
581 * Block intersects with data, if:
585 unsigned X = fb->addr, Y = fb->addr + fb->length;
586 unsigned a = addr, b = addr + length;
588 // from start of the block
589 unsigned start = (a > X ? a : X) - X;
590 unsigned end = (b > Y ? Y : b) - X;
592 memcpy(fb->data + start, data, end - start);
595 fit_length += end - start;
599 if(fit_blocks == 0) {
600 ELOG("Unfit data block %08x -> %04x\n", addr, length);
604 if(fit_length != length) {
605 WLOG("data block %08x -> %04x truncated to %04x\n",
606 addr, length, fit_length);
607 WLOG("(this is not an error, just a GDB glitch)\n");
613 static int flash_go(stlink_t *sl) {
616 // Some kinds of clock settings do not allow writing to flash.
619 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
620 DLOG("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
622 unsigned length = fb->length;
623 for(stm32_addr_t page = fb->addr; page < fb->addr + fb->length; page += FLASH_PAGE) {
626 stlink_calculate_pagesize(sl, page);
628 DLOG("flash_do: page %08x\n", page);
630 if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
631 length > FLASH_PAGE ? FLASH_PAGE : length) < 0)
641 for(struct flash_block* fb = flash_root, *next; fb; fb = next) {
652 int serve(stlink_t *sl, st_state_t *st) {
653 int sock = socket(AF_INET, SOCK_STREAM, 0);
659 unsigned int val = 1;
660 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
662 struct sockaddr_in serv_addr;
663 memset(&serv_addr,0,sizeof(struct sockaddr_in));
664 serv_addr.sin_family = AF_INET;
665 serv_addr.sin_addr.s_addr = INADDR_ANY;
666 serv_addr.sin_port = htons(st->listen_port);
668 if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
673 if(listen(sock, 5) < 0) {
678 ILOG("Listening at *:%d...\n", st->listen_port);
680 int client = accept(sock, NULL, NULL);
681 //signal (SIGINT, SIG_DFL);
689 stlink_force_debug(sl);
693 init_code_breakpoints(sl);
694 init_data_watchpoints(sl);
696 ILOG("GDB connected.\n");
699 * To allow resetting the chip from GDB it is required to
700 * emulate attaching and detaching to target.
702 unsigned int attached = 1;
707 int status = gdb_recv_packet(client, &packet);
709 ELOG("cannot recv: %d\n", status);
713 DLOG("recv: %s\n", packet);
720 if(packet[1] == 'P' || packet[1] == 'C' || packet[1] == 'L') {
725 char *separator = strstr(packet, ":"), *params = "";
726 if(separator == NULL) {
727 separator = packet + strlen(packet);
729 params = separator + 1;
732 unsigned queryNameLength = (separator - &packet[1]);
733 char* queryName = calloc(queryNameLength + 1, 1);
734 strncpy(queryName, &packet[1], queryNameLength);
736 DLOG("query: %s;%s\n", queryName, params);
738 if(!strcmp(queryName, "Supported")) {
739 if(sl->chip_id==STM32_CHIPID_F4) {
740 reply = strdup("PacketSize=3fff;qXfer:memory-map:read+;qXfer:features:read+");
743 reply = strdup("PacketSize=3fff;qXfer:memory-map:read+");
745 } else if(!strcmp(queryName, "Xfer")) {
746 char *type, *op, *__s_addr, *s_length;
748 char *annex __attribute__((unused));
750 type = strsep(&tok, ":");
751 op = strsep(&tok, ":");
752 annex = strsep(&tok, ":");
753 __s_addr = strsep(&tok, ",");
756 unsigned addr = strtoul(__s_addr, NULL, 16),
757 length = strtoul(s_length, NULL, 16);
759 DLOG("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
760 type, op, annex, addr, length);
762 const char* data = NULL;
764 if(!strcmp(type, "memory-map") && !strcmp(op, "read"))
765 data = current_memory_map;
767 if(!strcmp(type, "features") && !strcmp(op, "read"))
768 data = target_description_F4;
771 unsigned data_length = strlen(data);
772 if(addr + length > data_length)
773 length = data_length - addr;
778 reply = calloc(length + 2, 1);
780 strncpy(&reply[1], data, length);
783 } else if(!strncmp(queryName, "Rcmd,",4)) {
784 // Rcmd uses the wrong separator
785 char *separator = strstr(packet, ","), *params = "";
786 if(separator == NULL) {
787 separator = packet + strlen(packet);
789 params = separator + 1;
793 if (!strncmp(params,"726573756d65",12)) {// resume
794 DLOG("Rcmd: resume\n");
797 reply = strdup("OK");
798 } else if (!strncmp(params,"68616c74",8)) { //halt
799 reply = strdup("OK");
801 stlink_force_debug(sl);
803 DLOG("Rcmd: halt\n");
804 } else if (!strncmp(params,"6a7461675f7265736574",20)) { //jtag_reset
805 reply = strdup("OK");
807 stlink_jtag_reset(sl, 1);
808 stlink_jtag_reset(sl, 0);
809 stlink_force_debug(sl);
811 DLOG("Rcmd: jtag_reset\n");
812 } else if (!strncmp(params,"7265736574",10)) { //reset
813 reply = strdup("OK");
815 stlink_force_debug(sl);
817 init_code_breakpoints(sl);
818 init_data_watchpoints(sl);
820 DLOG("Rcmd: reset\n");
822 DLOG("Rcmd: %s\n", params);
837 char *cmdName = strtok_r(packet, ":;", ¶ms);
839 cmdName++; // vCommand -> Command
841 if(!strcmp(cmdName, "FlashErase")) {
842 char *__s_addr, *s_length;
845 __s_addr = strsep(&tok, ",");
848 unsigned addr = strtoul(__s_addr, NULL, 16),
849 length = strtoul(s_length, NULL, 16);
851 DLOG("FlashErase: addr:%08x,len:%04x\n",
854 if(flash_add_block(addr, length, sl) < 0) {
855 reply = strdup("E00");
857 reply = strdup("OK");
859 } else if(!strcmp(cmdName, "FlashWrite")) {
860 char *__s_addr, *data;
863 __s_addr = strsep(&tok, ":");
866 unsigned addr = strtoul(__s_addr, NULL, 16);
867 unsigned data_length = status - (data - packet);
869 // Length of decoded data cannot be more than
870 // encoded, as escapes are removed.
871 // Additional byte is reserved for alignment fix.
872 uint8_t *decoded = calloc(data_length + 1, 1);
873 unsigned dec_index = 0;
874 for(unsigned int i = 0; i < data_length; i++) {
875 if(data[i] == 0x7d) {
877 decoded[dec_index++] = data[i] ^ 0x20;
879 decoded[dec_index++] = data[i];
884 if(dec_index % 2 != 0)
887 DLOG("binary packet %d -> %d\n", data_length, dec_index);
889 if(flash_populate(addr, decoded, dec_index) < 0) {
890 reply = strdup("E00");
892 reply = strdup("OK");
894 } else if(!strcmp(cmdName, "FlashDone")) {
895 if(flash_go(sl) < 0) {
896 reply = strdup("E00");
898 reply = strdup("OK");
900 } else if(!strcmp(cmdName, "Kill")) {
903 reply = strdup("OK");
916 int status = gdb_check_for_interrupt(client);
918 ELOG("cannot check for int: %d\n", status);
923 stlink_force_debug(sl);
928 if(sl->core_stat == STLINK_CORE_HALTED) {
935 reply = strdup("S05"); // TRAP
941 reply = strdup("S05"); // TRAP
946 reply = strdup("S05"); // TRAP
948 /* Stub shall reply OK if not attached. */
949 reply = strdup("OK");
954 stlink_read_all_regs(sl, ®p);
956 reply = calloc(8 * 16 + 1, 1);
957 for(int i = 0; i < 16; i++)
958 sprintf(&reply[i * 8], "%08x", htonl(regp.r[i]));
963 unsigned id = strtoul(&packet[1], NULL, 16);
964 unsigned myreg = 0xDEADDEAD;
967 stlink_read_reg(sl, id, ®p);
968 myreg = htonl(regp.r[id]);
969 } else if(id == 0x19) {
970 stlink_read_reg(sl, 16, ®p);
971 myreg = htonl(regp.xpsr);
972 } else if(id == 0x1A) {
973 stlink_read_reg(sl, 17, ®p);
974 myreg = htonl(regp.main_sp);
975 } else if(id == 0x1B) {
976 stlink_read_reg(sl, 18, ®p);
977 myreg = htonl(regp.process_sp);
978 } else if(id == 0x1C) {
979 stlink_read_unsupported_reg(sl, id, ®p);
980 myreg = htonl(regp.control);
981 } else if(id == 0x1D) {
982 stlink_read_unsupported_reg(sl, id, ®p);
983 myreg = htonl(regp.faultmask);
984 } else if(id == 0x1E) {
985 stlink_read_unsupported_reg(sl, id, ®p);
986 myreg = htonl(regp.basepri);
987 } else if(id == 0x1F) {
988 stlink_read_unsupported_reg(sl, id, ®p);
989 myreg = htonl(regp.primask);
990 } else if(id >= 0x20 && id < 0x40) {
991 stlink_read_unsupported_reg(sl, id, ®p);
992 myreg = htonl(regp.s[id-0x20]);
993 } else if(id == 0x40) {
994 stlink_read_unsupported_reg(sl, id, ®p);
995 myreg = htonl(regp.fpscr);
997 reply = strdup("E00");
1000 reply = calloc(8 + 1, 1);
1001 sprintf(reply, "%08x", myreg);
1007 char* s_reg = &packet[1];
1008 char* s_value = strstr(&packet[1], "=") + 1;
1010 unsigned reg = strtoul(s_reg, NULL, 16);
1011 unsigned value = strtoul(s_value, NULL, 16);
1014 stlink_write_reg(sl, ntohl(value), reg);
1015 } else if(reg == 0x19) {
1016 stlink_write_reg(sl, ntohl(value), 16);
1017 } else if(reg == 0x1A) {
1018 stlink_write_reg(sl, ntohl(value), 17);
1019 } else if(reg == 0x1B) {
1020 stlink_write_reg(sl, ntohl(value), 18);
1021 } else if(reg == 0x1C) {
1022 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1023 } else if(reg == 0x1D) {
1024 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1025 } else if(reg == 0x1E) {
1026 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1027 } else if(reg == 0x1F) {
1028 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1029 } else if(reg >= 0x20 && reg < 0x40) {
1030 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1031 } else if(reg == 0x40) {
1032 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1034 reply = strdup("E00");
1038 reply = strdup("OK");
1045 for(int i = 0; i < 16; i++) {
1047 strncpy(str, &packet[1 + i * 8], 8);
1048 uint32_t reg = strtoul(str, NULL, 16);
1049 stlink_write_reg(sl, ntohl(reg), i);
1052 reply = strdup("OK");
1056 char* s_start = &packet[1];
1057 char* s_count = strstr(&packet[1], ",") + 1;
1059 stm32_addr_t start = strtoul(s_start, NULL, 16);
1060 unsigned count = strtoul(s_count, NULL, 16);
1062 unsigned adj_start = start % 4;
1063 unsigned count_rnd = (count + adj_start + 4 - 1) / 4 * 4;
1065 stlink_read_mem32(sl, start - adj_start, count_rnd);
1067 reply = calloc(count * 2 + 1, 1);
1068 for(unsigned int i = 0; i < count; i++) {
1069 reply[i * 2 + 0] = hex[sl->q_buf[i + adj_start] >> 4];
1070 reply[i * 2 + 1] = hex[sl->q_buf[i + adj_start] & 0xf];
1077 char* s_start = &packet[1];
1078 char* s_count = strstr(&packet[1], ",") + 1;
1079 char* hexdata = strstr(packet, ":") + 1;
1081 stm32_addr_t start = strtoul(s_start, NULL, 16);
1082 unsigned count = strtoul(s_count, NULL, 16);
1085 unsigned align_count = 4 - start % 4;
1086 if (align_count > count) align_count = count;
1087 for(unsigned int i = 0; i < align_count; i ++) {
1088 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
1089 uint8_t byte = strtoul(hex, NULL, 16);
1090 sl->q_buf[i] = byte;
1092 stlink_write_mem8(sl, start, align_count);
1093 start += align_count;
1094 count -= align_count;
1095 hexdata += 2*align_count;
1098 if(count - count % 4) {
1099 unsigned aligned_count = count - count % 4;
1101 for(unsigned int i = 0; i < aligned_count; i ++) {
1102 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
1103 uint8_t byte = strtoul(hex, NULL, 16);
1104 sl->q_buf[i] = byte;
1106 stlink_write_mem32(sl, start, aligned_count);
1107 count -= aligned_count;
1108 start += aligned_count;
1109 hexdata += 2*aligned_count;
1113 for(unsigned int i = 0; i < count; i ++) {
1114 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
1115 uint8_t byte = strtoul(hex, NULL, 16);
1116 sl->q_buf[i] = byte;
1118 stlink_write_mem8(sl, start, count);
1120 reply = strdup("OK");
1126 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
1127 stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
1129 switch (packet[1]) {
1131 if(update_code_breakpoint(sl, addr, 1) < 0) {
1132 reply = strdup("E00");
1134 reply = strdup("OK");
1138 case '2': // insert write watchpoint
1139 case '3': // insert read watchpoint
1140 case '4': { // insert access watchpoint
1142 if(packet[1] == '2') {
1144 } else if(packet[1] == '3') {
1150 if(add_data_watchpoint(sl, wf, addr, len) < 0) {
1151 reply = strdup("E00");
1153 reply = strdup("OK");
1165 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
1166 //stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
1168 switch (packet[1]) {
1169 case '1': // remove breakpoint
1170 update_code_breakpoint(sl, addr, 0);
1171 reply = strdup("OK");
1174 case '2' : // remove write watchpoint
1175 case '3' : // remove read watchpoint
1176 case '4' : // remove access watchpoint
1177 if(delete_data_watchpoint(sl, addr) < 0) {
1178 reply = strdup("E00");
1180 reply = strdup("OK");
1192 * Enter extended mode which allows restarting.
1193 * We do support that always.
1197 * Also, set to persistent mode
1198 * to allow GDB disconnect.
1202 reply = strdup("OK");
1208 /* Reset the core. */
1211 init_code_breakpoints(sl);
1212 init_data_watchpoints(sl);
1216 reply = strdup("OK");
1226 DLOG("send: %s\n", reply);
1228 int result = gdb_send_packet(client, reply);
1230 ELOG("cannot send: %d\n", result);