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.
15 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
24 #include <stlink-common.h>
26 #include "gdb-remote.h"
28 #define DEFAULT_LOGGING_LEVEL 50
29 #define DEFAULT_GDB_LISTEN_PORT 4242
31 #define STRINGIFY_inner(name) #name
32 #define STRINGIFY(name) STRINGIFY_inner(name)
34 #define FLASH_BASE 0x08000000
36 //Allways update the FLASH_PAGE before each use, by calling stlink_calculate_pagesize
37 #define FLASH_PAGE (sl->flash_pgsz)
39 stlink_t *connected_stlink = NULL;
41 static const char hex[] = "0123456789abcdef";
43 static const char* current_memory_map = NULL;
45 typedef struct _st_state_t {
46 // things from command line, bleh
55 int serve(stlink_t *sl, st_state_t *st);
56 char* make_memory_map(stlink_t *sl);
58 static void cleanup(int signal __attribute__((unused))) {
59 if (connected_stlink) {
60 /* Switch back to mass storage mode before closing. */
61 stlink_run(connected_stlink);
62 stlink_exit_debug_mode(connected_stlink);
63 stlink_close(connected_stlink);
71 int parse_options(int argc, char** argv, st_state_t *st) {
72 static struct option long_options[] = {
73 {"help", no_argument, NULL, 'h'},
74 {"verbose", optional_argument, NULL, 'v'},
75 {"stlink_version", required_argument, NULL, 's'},
76 {"stlinkv1", no_argument, NULL, '1'},
77 {"listen_port", required_argument, NULL, 'p'},
78 {"multi", optional_argument, NULL, 'm'},
79 {"no-reset", optional_argument, NULL, 'n'},
82 const char * help_str = "%s - usage:\n\n"
83 " -h, --help\t\tPrint this help\n"
84 " -vXX, --verbose=XX\tSpecify a specific verbosity level (0..99)\n"
85 " -v, --verbose\t\tSpecify generally verbose logging\n"
86 " -s X, --stlink_version=X\n"
87 "\t\t\tChoose what version of stlink to use, (defaults to 2)\n"
88 " -1, --stlinkv1\tForce stlink version 1\n"
89 " -p 4242, --listen_port=1234\n"
90 "\t\t\tSet the gdb server listen port. "
91 "(default port: " STRINGIFY(DEFAULT_GDB_LISTEN_PORT) ")\n"
93 "\t\t\tSet gdb server to extended mode.\n"
94 "\t\t\tst-util will continue listening for connections after disconnect.\n"
96 "\t\t\tDo not reset board on connection.\n"
98 "The STLINKv2 device to use can be specified in the environment\n"
99 "variable STLINK_DEVICE on the format <USB_BUS>:<USB_ADDR>.\n"
104 int option_index = 0;
107 while ((c = getopt_long(argc, argv, "hv::s:1p:mn", long_options, &option_index)) != -1) {
110 printf("XXXXX Shouldn't really normally come here, only if there's no corresponding option\n");
111 printf("option %s", long_options[option_index].name);
113 printf(" with arg %s", optarg);
118 printf(help_str, argv[0]);
123 st->logging_level = atoi(optarg);
125 st->logging_level = DEFAULT_LOGGING_LEVEL;
129 st->stlink_version = 1;
132 sscanf(optarg, "%i", &q);
133 if (q < 0 || q > 2) {
134 fprintf(stderr, "stlink version %d unknown!\n", q);
137 st->stlink_version = q;
140 sscanf(optarg, "%i", &q);
142 fprintf(stderr, "Can't use a negative port to listen on: %d\n", q);
157 printf("non-option ARGV-elements: ");
158 while (optind < argc)
159 printf("%s ", argv[optind++]);
166 int main(int argc, char** argv) {
172 memset(&state, 0, sizeof(state));
174 state.stlink_version = 2;
175 state.logging_level = DEFAULT_LOGGING_LEVEL;
176 state.listen_port = DEFAULT_GDB_LISTEN_PORT;
177 state.reset = 1; /* By default, reset board */
178 parse_options(argc, argv, &state);
179 switch (state.stlink_version) {
181 sl = stlink_open_usb(state.logging_level, 0);
182 if(sl == NULL) return 1;
185 sl = stlink_v1_open(state.logging_level, 0);
186 if(sl == NULL) return 1;
190 connected_stlink = sl;
191 signal(SIGINT, &cleanup);
192 signal(SIGTERM, &cleanup);
198 printf("Chip ID is %08x, Core ID is %08x.\n", sl->chip_id, sl->core_id);
200 voltage = stlink_target_voltage(sl);
202 printf("Target voltage is %d mV.\n", voltage);
207 current_memory_map = make_memory_map(sl);
211 if (WSAStartup(MAKEWORD(2,2),&wsadata) !=0 ) {
221 } while (state.persistent);
228 /* Switch back to mass storage mode before closing. */
229 stlink_exit_debug_mode(sl);
235 static const char* const target_description_F4 =
236 "<?xml version=\"1.0\"?>"
237 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
238 "<target version=\"1.0\">"
239 " <architecture>arm</architecture>"
240 " <feature name=\"org.gnu.gdb.arm.m-profile\">"
241 " <reg name=\"r0\" bitsize=\"32\"/>"
242 " <reg name=\"r1\" bitsize=\"32\"/>"
243 " <reg name=\"r2\" bitsize=\"32\"/>"
244 " <reg name=\"r3\" bitsize=\"32\"/>"
245 " <reg name=\"r4\" bitsize=\"32\"/>"
246 " <reg name=\"r5\" bitsize=\"32\"/>"
247 " <reg name=\"r6\" bitsize=\"32\"/>"
248 " <reg name=\"r7\" bitsize=\"32\"/>"
249 " <reg name=\"r8\" bitsize=\"32\"/>"
250 " <reg name=\"r9\" bitsize=\"32\"/>"
251 " <reg name=\"r10\" bitsize=\"32\"/>"
252 " <reg name=\"r11\" bitsize=\"32\"/>"
253 " <reg name=\"r12\" bitsize=\"32\"/>"
254 " <reg name=\"sp\" bitsize=\"32\" type=\"data_ptr\"/>"
255 " <reg name=\"lr\" bitsize=\"32\"/>"
256 " <reg name=\"pc\" bitsize=\"32\" type=\"code_ptr\"/>"
257 " <reg name=\"xpsr\" bitsize=\"32\" regnum=\"25\"/>"
258 " <reg name=\"msp\" bitsize=\"32\" regnum=\"26\" type=\"data_ptr\" group=\"general\" />"
259 " <reg name=\"psp\" bitsize=\"32\" regnum=\"27\" type=\"data_ptr\" group=\"general\" />"
260 " <reg name=\"control\" bitsize=\"8\" regnum=\"28\" type=\"int\" group=\"general\" />"
261 " <reg name=\"faultmask\" bitsize=\"8\" regnum=\"29\" type=\"int\" group=\"general\" />"
262 " <reg name=\"basepri\" bitsize=\"8\" regnum=\"30\" type=\"int\" group=\"general\" />"
263 " <reg name=\"primask\" bitsize=\"8\" regnum=\"31\" type=\"int\" group=\"general\" />"
264 " <reg name=\"s0\" bitsize=\"32\" regnum=\"32\" type=\"float\" group=\"float\" />"
265 " <reg name=\"s1\" bitsize=\"32\" type=\"float\" group=\"float\" />"
266 " <reg name=\"s2\" bitsize=\"32\" type=\"float\" group=\"float\" />"
267 " <reg name=\"s3\" bitsize=\"32\" type=\"float\" group=\"float\" />"
268 " <reg name=\"s4\" bitsize=\"32\" type=\"float\" group=\"float\" />"
269 " <reg name=\"s5\" bitsize=\"32\" type=\"float\" group=\"float\" />"
270 " <reg name=\"s6\" bitsize=\"32\" type=\"float\" group=\"float\" />"
271 " <reg name=\"s7\" bitsize=\"32\" type=\"float\" group=\"float\" />"
272 " <reg name=\"s8\" bitsize=\"32\" type=\"float\" group=\"float\" />"
273 " <reg name=\"s9\" bitsize=\"32\" type=\"float\" group=\"float\" />"
274 " <reg name=\"s10\" bitsize=\"32\" type=\"float\" group=\"float\" />"
275 " <reg name=\"s11\" bitsize=\"32\" type=\"float\" group=\"float\" />"
276 " <reg name=\"s12\" bitsize=\"32\" type=\"float\" group=\"float\" />"
277 " <reg name=\"s13\" bitsize=\"32\" type=\"float\" group=\"float\" />"
278 " <reg name=\"s14\" bitsize=\"32\" type=\"float\" group=\"float\" />"
279 " <reg name=\"s15\" bitsize=\"32\" type=\"float\" group=\"float\" />"
280 " <reg name=\"s16\" bitsize=\"32\" type=\"float\" group=\"float\" />"
281 " <reg name=\"s17\" bitsize=\"32\" type=\"float\" group=\"float\" />"
282 " <reg name=\"s18\" bitsize=\"32\" type=\"float\" group=\"float\" />"
283 " <reg name=\"s19\" bitsize=\"32\" type=\"float\" group=\"float\" />"
284 " <reg name=\"s20\" bitsize=\"32\" type=\"float\" group=\"float\" />"
285 " <reg name=\"s21\" bitsize=\"32\" type=\"float\" group=\"float\" />"
286 " <reg name=\"s22\" bitsize=\"32\" type=\"float\" group=\"float\" />"
287 " <reg name=\"s23\" bitsize=\"32\" type=\"float\" group=\"float\" />"
288 " <reg name=\"s24\" bitsize=\"32\" type=\"float\" group=\"float\" />"
289 " <reg name=\"s25\" bitsize=\"32\" type=\"float\" group=\"float\" />"
290 " <reg name=\"s26\" bitsize=\"32\" type=\"float\" group=\"float\" />"
291 " <reg name=\"s27\" bitsize=\"32\" type=\"float\" group=\"float\" />"
292 " <reg name=\"s28\" bitsize=\"32\" type=\"float\" group=\"float\" />"
293 " <reg name=\"s29\" bitsize=\"32\" type=\"float\" group=\"float\" />"
294 " <reg name=\"s30\" bitsize=\"32\" type=\"float\" group=\"float\" />"
295 " <reg name=\"s31\" bitsize=\"32\" type=\"float\" group=\"float\" />"
296 " <reg name=\"fpscr\" bitsize=\"32\" type=\"int\" group=\"float\" />"
300 static const char* const memory_map_template_F4 =
301 "<?xml version=\"1.0\"?>"
302 "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
303 " \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
305 " <memory type=\"rom\" start=\"0x00000000\" length=\"0x100000\"/>" // code = sram, bootrom or flash; flash is bigger
306 " <memory type=\"ram\" start=\"0x10000000\" length=\"0x10000\"/>" // ccm ram
307 " <memory type=\"ram\" start=\"0x20000000\" length=\"0x20000\"/>" // sram
308 " <memory type=\"flash\" start=\"0x08000000\" length=\"0x10000\">" //Sectors 0..3
309 " <property name=\"blocksize\">0x4000</property>" //16kB
311 " <memory type=\"flash\" start=\"0x08010000\" length=\"0x10000\">" //Sector 4
312 " <property name=\"blocksize\">0x10000</property>" //64kB
314 " <memory type=\"flash\" start=\"0x08020000\" length=\"0x70000\">" //Sectors 5..11
315 " <property name=\"blocksize\">0x20000</property>" //128kB
317 " <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>" // peripheral regs
318 " <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>" // cortex regs
319 " <memory type=\"rom\" start=\"0x1fff0000\" length=\"0x7800\"/>" // bootrom
320 " <memory type=\"rom\" start=\"0x1fffc000\" length=\"0x10\"/>" // option byte area
323 static const char* const memory_map_template =
324 "<?xml version=\"1.0\"?>"
325 "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
326 " \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
328 " <memory type=\"rom\" start=\"0x00000000\" length=\"0x%zx\"/>" // code = sram, bootrom or flash; flash is bigger
329 " <memory type=\"ram\" start=\"0x20000000\" length=\"0x%zx\"/>" // sram 8k
330 " <memory type=\"flash\" start=\"0x08000000\" length=\"0x%zx\">"
331 " <property name=\"blocksize\">0x%zx</property>"
333 " <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>" // peripheral regs
334 " <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>" // cortex regs
335 " <memory type=\"rom\" start=\"0x%08x\" length=\"0x%zx\"/>" // bootrom
336 " <memory type=\"rom\" start=\"0x1ffff800\" length=\"0x10\"/>" // option byte area
339 char* make_memory_map(stlink_t *sl) {
340 /* This will be freed in serve() */
341 char* map = malloc(4096);
344 if(sl->chip_id==STM32_CHIPID_F4) {
345 strcpy(map, memory_map_template_F4);
347 snprintf(map, 4096, memory_map_template,
350 sl->flash_size, sl->flash_pgsz,
351 sl->sys_base, sl->sys_size);
358 * DWT_COMP0 0xE0001020
359 * DWT_MASK0 0xE0001024
360 * DWT_FUNCTION0 0xE0001028
361 * DWT_COMP1 0xE0001030
362 * DWT_MASK1 0xE0001034
363 * DWT_FUNCTION1 0xE0001038
364 * DWT_COMP2 0xE0001040
365 * DWT_MASK2 0xE0001044
366 * DWT_FUNCTION2 0xE0001048
367 * DWT_COMP3 0xE0001050
368 * DWT_MASK3 0xE0001054
369 * DWT_FUNCTION3 0xE0001058
372 #define DATA_WATCH_NUM 4
374 enum watchfun { WATCHDISABLED = 0, WATCHREAD = 5, WATCHWRITE = 6, WATCHACCESS = 7 };
376 struct code_hw_watchpoint {
382 struct code_hw_watchpoint data_watches[DATA_WATCH_NUM];
384 static void init_data_watchpoints(stlink_t *sl) {
386 printf("init watchpoints\n");
389 // set trcena in debug command to turn on dwt unit
390 stlink_write_debug32(sl, 0xE000EDFC,
391 stlink_read_debug32(sl, 0xE000EDFC) | (1<<24));
393 // make sure all watchpoints are cleared
394 for(int i = 0; i < DATA_WATCH_NUM; i++) {
395 data_watches[i].fun = WATCHDISABLED;
396 stlink_write_debug32(sl, 0xe0001028 + i * 16, 0);
400 static int add_data_watchpoint(stlink_t *sl, enum watchfun wf, stm32_addr_t addr, unsigned int len)
406 // find a free watchpoint
416 if((mask != (uint32_t)-1) && (mask < 16)) {
417 for(i = 0; i < DATA_WATCH_NUM; i++) {
418 // is this an empty slot ?
419 if(data_watches[i].fun == WATCHDISABLED) {
421 printf("insert watchpoint %d addr %x wf %u mask %u len %d\n", i, addr, wf, mask, len);
424 data_watches[i].fun = wf;
425 data_watches[i].addr = addr;
426 data_watches[i].mask = mask;
428 // insert comparator address
429 stlink_write_debug32(sl, 0xE0001020 + i * 16, addr);
432 stlink_write_debug32(sl, 0xE0001024 + i * 16, mask);
435 stlink_write_debug32(sl, 0xE0001028 + i * 16, wf);
437 // just to make sure the matched bit is clear !
438 stlink_read_debug32(sl, 0xE0001028 + i * 16);
445 printf("failure: add watchpoints addr %x wf %u len %u\n", addr, wf, len);
450 static int delete_data_watchpoint(stlink_t *sl, stm32_addr_t addr)
454 for(i = 0 ; i < DATA_WATCH_NUM; i++) {
455 if((data_watches[i].addr == addr) && (data_watches[i].fun != WATCHDISABLED)) {
457 printf("delete watchpoint %d addr %x\n", i, addr);
460 data_watches[i].fun = WATCHDISABLED;
461 stlink_write_debug32(sl, 0xe0001028 + i * 16, 0);
468 printf("failure: delete watchpoint addr %x\n", addr);
474 #define CODE_BREAK_NUM 6
475 #define CODE_BREAK_LOW 0x01
476 #define CODE_BREAK_HIGH 0x02
478 struct code_hw_breakpoint {
483 struct code_hw_breakpoint code_breaks[CODE_BREAK_NUM];
485 static void init_code_breakpoints(stlink_t *sl) {
486 memset(sl->q_buf, 0, 4);
487 stlink_write_debug32(sl, CM3_REG_FP_CTRL, 0x03 /*KEY | ENABLE4*/);
488 printf("KARL - should read back as 0x03, not 60 02 00 00\n");
489 stlink_read_debug32(sl, CM3_REG_FP_CTRL);
491 for(int i = 0; i < CODE_BREAK_NUM; i++) {
492 code_breaks[i].type = 0;
493 stlink_write_debug32(sl, CM3_REG_FP_COMP0 + i * 4, 0);
497 static int update_code_breakpoint(stlink_t *sl, stm32_addr_t addr, int set) {
498 stm32_addr_t fpb_addr = addr & ~0x3;
499 int type = addr & 0x2 ? CODE_BREAK_HIGH : CODE_BREAK_LOW;
502 fprintf(stderr, "update_code_breakpoint: unaligned address %08x\n", addr);
507 for(int i = 0; i < CODE_BREAK_NUM; i++) {
508 if(fpb_addr == code_breaks[i].addr ||
509 (set && code_breaks[i].type == 0)) {
516 if(set) return -1; // Free slot not found
517 else return 0; // Breakpoint is already removed
520 struct code_hw_breakpoint* brk = &code_breaks[id];
522 brk->addr = fpb_addr;
524 if(set) brk->type |= type;
525 else brk->type &= ~type;
529 printf("clearing hw break %d\n", id);
532 stlink_write_debug32(sl, 0xe0002008 + id * 4, 0);
534 uint32_t mask = (brk->addr) | 1 | (brk->type << 30);
537 printf("setting hw break %d at %08x (%d)\n",
538 id, brk->addr, brk->type);
539 printf("reg %08x \n",
543 stlink_write_debug32(sl, 0xe0002008 + id * 4, mask);
555 struct flash_block* next;
558 static struct flash_block* flash_root;
560 static int flash_add_block(stm32_addr_t addr, unsigned length, stlink_t *sl) {
562 if(addr < FLASH_BASE || addr + length > FLASH_BASE + sl->flash_size) {
563 fprintf(stderr, "flash_add_block: incorrect bounds\n");
567 stlink_calculate_pagesize(sl, addr);
568 if(addr % FLASH_PAGE != 0 || length % FLASH_PAGE != 0) {
569 fprintf(stderr, "flash_add_block: unaligned block\n");
573 struct flash_block* new = malloc(sizeof(struct flash_block));
574 new->next = flash_root;
577 new->length = length;
578 new->data = calloc(length, 1);
585 static int flash_populate(stm32_addr_t addr, uint8_t* data, unsigned length) {
586 unsigned int fit_blocks = 0, fit_length = 0;
588 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
589 /* Block: ------X------Y--------
593 * Block intersects with data, if:
597 unsigned X = fb->addr, Y = fb->addr + fb->length;
598 unsigned a = addr, b = addr + length;
600 // from start of the block
601 unsigned start = (a > X ? a : X) - X;
602 unsigned end = (b > Y ? Y : b) - X;
604 memcpy(fb->data + start, data, end - start);
607 fit_length += end - start;
611 if(fit_blocks == 0) {
612 fprintf(stderr, "Unfit data block %08x -> %04x\n", addr, length);
616 if(fit_length != length) {
617 fprintf(stderr, "warning: data block %08x -> %04x truncated to %04x\n",
618 addr, length, fit_length);
619 fprintf(stderr, "(this is not an error, just a GDB glitch)\n");
625 static int flash_go(stlink_t *sl) {
628 // Some kinds of clock settings do not allow writing to flash.
631 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
633 printf("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
636 unsigned length = fb->length;
637 for(stm32_addr_t page = fb->addr; page < fb->addr + fb->length; page += FLASH_PAGE) {
640 stlink_calculate_pagesize(sl, page);
643 printf("flash_do: page %08x\n", page);
646 if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
647 length > FLASH_PAGE ? FLASH_PAGE : length) < 0)
657 for(struct flash_block* fb = flash_root, *next; fb; fb = next) {
668 int serve(stlink_t *sl, st_state_t *st) {
669 int sock = socket(AF_INET, SOCK_STREAM, 0);
675 unsigned int val = 1;
676 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
678 struct sockaddr_in serv_addr;
679 memset(&serv_addr,0,sizeof(struct sockaddr_in));
680 serv_addr.sin_family = AF_INET;
681 serv_addr.sin_addr.s_addr = INADDR_ANY;
682 serv_addr.sin_port = htons(st->listen_port);
684 if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
689 if(listen(sock, 5) < 0) {
694 printf("Listening at *:%d...\n", st->listen_port);
696 int client = accept(sock, NULL, NULL);
697 //signal (SIGINT, SIG_DFL);
705 stlink_force_debug(sl);
709 init_code_breakpoints(sl);
710 init_data_watchpoints(sl);
712 printf("GDB connected.\n");
715 * To allow resetting the chip from GDB it is required to
716 * emulate attaching and detaching to target.
718 unsigned int attached = 1;
723 int status = gdb_recv_packet(client, &packet);
725 fprintf(stderr, "cannot recv: %d\n", status);
730 printf("recv: %s\n", packet);
738 if(packet[1] == 'P' || packet[1] == 'C' || packet[1] == 'L') {
743 char *separator = strstr(packet, ":"), *params = "";
744 if(separator == NULL) {
745 separator = packet + strlen(packet);
747 params = separator + 1;
750 unsigned queryNameLength = (separator - &packet[1]);
751 char* queryName = calloc(queryNameLength + 1, 1);
752 strncpy(queryName, &packet[1], queryNameLength);
755 printf("query: %s;%s\n", queryName, params);
758 if(!strcmp(queryName, "Supported")) {
759 if(sl->chip_id==STM32_CHIPID_F4) {
760 reply = strdup("PacketSize=3fff;qXfer:memory-map:read+;qXfer:features:read+");
763 reply = strdup("PacketSize=3fff;qXfer:memory-map:read+");
765 } else if(!strcmp(queryName, "Xfer")) {
766 char *type, *op, *__s_addr, *s_length;
768 char *annex __attribute__((unused));
770 type = strsep(&tok, ":");
771 op = strsep(&tok, ":");
772 annex = strsep(&tok, ":");
773 __s_addr = strsep(&tok, ",");
776 unsigned addr = strtoul(__s_addr, NULL, 16),
777 length = strtoul(s_length, NULL, 16);
780 printf("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
781 type, op, annex, addr, length);
784 const char* data = NULL;
786 if(!strcmp(type, "memory-map") && !strcmp(op, "read"))
787 data = current_memory_map;
789 if(!strcmp(type, "features") && !strcmp(op, "read"))
790 data = target_description_F4;
793 unsigned data_length = strlen(data);
794 if(addr + length > data_length)
795 length = data_length - addr;
800 reply = calloc(length + 2, 1);
802 strncpy(&reply[1], data, length);
805 } else if(!strncmp(queryName, "Rcmd,",4)) {
806 // Rcmd uses the wrong separator
807 char *separator = strstr(packet, ","), *params = "";
808 if(separator == NULL) {
809 separator = packet + strlen(packet);
811 params = separator + 1;
815 if (!strncmp(params,"726573756d65",12)) {// resume
817 printf("Rcmd: resume\n");
821 reply = strdup("OK");
822 } else if (!strncmp(params,"68616c74",8)) { //halt
823 reply = strdup("OK");
825 stlink_force_debug(sl);
828 printf("Rcmd: halt\n");
830 } else if (!strncmp(params,"6a7461675f7265736574",20)) { //jtag_reset
831 reply = strdup("OK");
833 stlink_jtag_reset(sl, 1);
834 stlink_jtag_reset(sl, 0);
835 stlink_force_debug(sl);
838 printf("Rcmd: jtag_reset\n");
840 } else if (!strncmp(params,"7265736574",10)) { //reset
841 reply = strdup("OK");
843 stlink_force_debug(sl);
845 init_code_breakpoints(sl);
846 init_data_watchpoints(sl);
849 printf("Rcmd: reset\n");
853 printf("Rcmd: %s\n", params);
870 char *cmdName = strtok_r(packet, ":;", ¶ms);
872 cmdName++; // vCommand -> Command
874 if(!strcmp(cmdName, "FlashErase")) {
875 char *__s_addr, *s_length;
878 __s_addr = strsep(&tok, ",");
881 unsigned addr = strtoul(__s_addr, NULL, 16),
882 length = strtoul(s_length, NULL, 16);
885 printf("FlashErase: addr:%08x,len:%04x\n",
889 if(flash_add_block(addr, length, sl) < 0) {
890 reply = strdup("E00");
892 reply = strdup("OK");
894 } else if(!strcmp(cmdName, "FlashWrite")) {
895 char *__s_addr, *data;
898 __s_addr = strsep(&tok, ":");
901 unsigned addr = strtoul(__s_addr, NULL, 16);
902 unsigned data_length = status - (data - packet);
904 // Length of decoded data cannot be more than
905 // encoded, as escapes are removed.
906 // Additional byte is reserved for alignment fix.
907 uint8_t *decoded = calloc(data_length + 1, 1);
908 unsigned dec_index = 0;
909 for(unsigned int i = 0; i < data_length; i++) {
910 if(data[i] == 0x7d) {
912 decoded[dec_index++] = data[i] ^ 0x20;
914 decoded[dec_index++] = data[i];
919 if(dec_index % 2 != 0)
923 printf("binary packet %d -> %d\n", data_length, dec_index);
926 if(flash_populate(addr, decoded, dec_index) < 0) {
927 reply = strdup("E00");
929 reply = strdup("OK");
931 } else if(!strcmp(cmdName, "FlashDone")) {
932 if(flash_go(sl) < 0) {
933 reply = strdup("E00");
935 reply = strdup("OK");
937 } else if(!strcmp(cmdName, "Kill")) {
940 reply = strdup("OK");
953 int status = gdb_check_for_interrupt(client);
955 fprintf(stderr, "cannot check for int: %d\n", status);
960 stlink_force_debug(sl);
965 if(sl->core_stat == STLINK_CORE_HALTED) {
972 reply = strdup("S05"); // TRAP
978 reply = strdup("S05"); // TRAP
983 reply = strdup("S05"); // TRAP
985 /* Stub shall reply OK if not attached. */
986 reply = strdup("OK");
991 stlink_read_all_regs(sl, ®p);
993 reply = calloc(8 * 16 + 1, 1);
994 for(int i = 0; i < 16; i++)
995 sprintf(&reply[i * 8], "%08x", htonl(regp.r[i]));
1000 unsigned id = strtoul(&packet[1], NULL, 16);
1001 unsigned myreg = 0xDEADDEAD;
1004 stlink_read_reg(sl, id, ®p);
1005 myreg = htonl(regp.r[id]);
1006 } else if(id == 0x19) {
1007 stlink_read_reg(sl, 16, ®p);
1008 myreg = htonl(regp.xpsr);
1009 } else if(id == 0x1A) {
1010 stlink_read_reg(sl, 17, ®p);
1011 myreg = htonl(regp.main_sp);
1012 } else if(id == 0x1B) {
1013 stlink_read_reg(sl, 18, ®p);
1014 myreg = htonl(regp.process_sp);
1015 } else if(id == 0x1C) {
1016 stlink_read_unsupported_reg(sl, id, ®p);
1017 myreg = htonl(regp.control);
1018 } else if(id == 0x1D) {
1019 stlink_read_unsupported_reg(sl, id, ®p);
1020 myreg = htonl(regp.faultmask);
1021 } else if(id == 0x1E) {
1022 stlink_read_unsupported_reg(sl, id, ®p);
1023 myreg = htonl(regp.basepri);
1024 } else if(id == 0x1F) {
1025 stlink_read_unsupported_reg(sl, id, ®p);
1026 myreg = htonl(regp.primask);
1027 } else if(id >= 0x20 && id < 0x40) {
1028 stlink_read_unsupported_reg(sl, id, ®p);
1029 myreg = htonl(regp.s[id-0x20]);
1030 } else if(id == 0x40) {
1031 stlink_read_unsupported_reg(sl, id, ®p);
1032 myreg = htonl(regp.fpscr);
1034 reply = strdup("E00");
1037 reply = calloc(8 + 1, 1);
1038 sprintf(reply, "%08x", myreg);
1044 char* s_reg = &packet[1];
1045 char* s_value = strstr(&packet[1], "=") + 1;
1047 unsigned reg = strtoul(s_reg, NULL, 16);
1048 unsigned value = strtoul(s_value, NULL, 16);
1051 stlink_write_reg(sl, ntohl(value), reg);
1052 } else if(reg == 0x19) {
1053 stlink_write_reg(sl, ntohl(value), 16);
1054 } else if(reg == 0x1A) {
1055 stlink_write_reg(sl, ntohl(value), 17);
1056 } else if(reg == 0x1B) {
1057 stlink_write_reg(sl, ntohl(value), 18);
1058 } else if(reg == 0x1C) {
1059 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1060 } else if(reg == 0x1D) {
1061 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1062 } else if(reg == 0x1E) {
1063 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1064 } else if(reg == 0x1F) {
1065 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1066 } else if(reg >= 0x20 && reg < 0x40) {
1067 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1068 } else if(reg == 0x40) {
1069 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1071 reply = strdup("E00");
1075 reply = strdup("OK");
1082 for(int i = 0; i < 16; i++) {
1084 strncpy(str, &packet[1 + i * 8], 8);
1085 uint32_t reg = strtoul(str, NULL, 16);
1086 stlink_write_reg(sl, ntohl(reg), i);
1089 reply = strdup("OK");
1093 char* s_start = &packet[1];
1094 char* s_count = strstr(&packet[1], ",") + 1;
1096 stm32_addr_t start = strtoul(s_start, NULL, 16);
1097 unsigned count = strtoul(s_count, NULL, 16);
1099 unsigned adj_start = start % 4;
1100 unsigned count_rnd = (count + adj_start + 4 - 1) / 4 * 4;
1102 stlink_read_mem32(sl, start - adj_start, count_rnd);
1104 reply = calloc(count * 2 + 1, 1);
1105 for(unsigned int i = 0; i < count; i++) {
1106 reply[i * 2 + 0] = hex[sl->q_buf[i + adj_start] >> 4];
1107 reply[i * 2 + 1] = hex[sl->q_buf[i + adj_start] & 0xf];
1114 char* s_start = &packet[1];
1115 char* s_count = strstr(&packet[1], ",") + 1;
1116 char* hexdata = strstr(packet, ":") + 1;
1118 stm32_addr_t start = strtoul(s_start, NULL, 16);
1119 unsigned count = strtoul(s_count, NULL, 16);
1122 unsigned align_count = 4 - start % 4;
1123 if (align_count > count) align_count = count;
1124 for(unsigned int i = 0; i < align_count; i ++) {
1125 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
1126 uint8_t byte = strtoul(hex, NULL, 16);
1127 sl->q_buf[i] = byte;
1129 stlink_write_mem8(sl, start, align_count);
1130 start += align_count;
1131 count -= align_count;
1132 hexdata += 2*align_count;
1135 if(count - count % 4) {
1136 unsigned aligned_count = count - count % 4;
1138 for(unsigned int i = 0; i < aligned_count; i ++) {
1139 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
1140 uint8_t byte = strtoul(hex, NULL, 16);
1141 sl->q_buf[i] = byte;
1143 stlink_write_mem32(sl, start, aligned_count);
1144 count -= aligned_count;
1145 start += aligned_count;
1146 hexdata += 2*aligned_count;
1150 for(unsigned int i = 0; i < count; i ++) {
1151 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
1152 uint8_t byte = strtoul(hex, NULL, 16);
1153 sl->q_buf[i] = byte;
1155 stlink_write_mem8(sl, start, count);
1157 reply = strdup("OK");
1163 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
1164 stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
1166 switch (packet[1]) {
1168 if(update_code_breakpoint(sl, addr, 1) < 0) {
1169 reply = strdup("E00");
1171 reply = strdup("OK");
1175 case '2': // insert write watchpoint
1176 case '3': // insert read watchpoint
1177 case '4': // insert access watchpoint
1180 if(packet[1] == '2') {
1182 } else if(packet[1] == '3') {
1188 if(add_data_watchpoint(sl, wf, addr, len) < 0) {
1189 reply = strdup("E00");
1191 reply = strdup("OK");
1203 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
1204 //stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
1206 switch (packet[1]) {
1207 case '1': // remove breakpoint
1208 update_code_breakpoint(sl, addr, 0);
1209 reply = strdup("OK");
1212 case '2' : // remove write watchpoint
1213 case '3' : // remove read watchpoint
1214 case '4' : // remove access watchpoint
1215 if(delete_data_watchpoint(sl, addr) < 0) {
1216 reply = strdup("E00");
1218 reply = strdup("OK");
1230 * Enter extended mode which allows restarting.
1231 * We do support that always.
1235 * Also, set to persistent mode
1236 * to allow GDB disconnect.
1240 reply = strdup("OK");
1246 /* Reset the core. */
1249 init_code_breakpoints(sl);
1250 init_data_watchpoints(sl);
1254 reply = strdup("OK");
1265 printf("send: %s\n", reply);
1268 int result = gdb_send_packet(client, reply);
1270 fprintf(stderr, "cannot send: %d\n", result);