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.
14 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #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 static const char hex[] = "0123456789abcdef";
41 static const char* current_memory_map = NULL;
43 typedef struct _st_state_t {
44 // things from command line, bleh
46 // "/dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTE531X6-if00-port0" is only 58 chars
53 int serve(stlink_t *sl, int port);
54 char* make_memory_map(stlink_t *sl);
57 int parse_options(int argc, char** argv, st_state_t *st) {
58 static struct option long_options[] = {
59 {"help", no_argument, NULL, 'h'},
60 {"verbose", optional_argument, NULL, 'v'},
61 {"device", required_argument, NULL, 'd'},
62 {"stlink_version", required_argument, NULL, 's'},
63 {"stlinkv1", no_argument, NULL, '1'},
64 {"listen_port", required_argument, NULL, 'p'},
67 const char * help_str = "%s - usage:\n\n"
68 " -h, --help\t\tPrint this help\n"
69 " -vXX, --verbose=XX\tspecify a specific verbosity level (0..99)\n"
70 " -v, --verbose\tspecify generally verbose logging\n"
71 " -d <device>, --device=/dev/stlink2_1\n"
72 "\t\t\tWhere is your stlink device connected?\n"
73 " -s X, --stlink_version=X\n"
74 "\t\t\tChoose what version of stlink to use, (defaults to 2)\n"
75 " -1, --stlinkv1\tForce stlink version 1\n"
76 " -p 4242, --listen_port=1234\n"
77 "\t\t\tSet the gdb server listen port. "
78 "(default port: " STRINGIFY(DEFAULT_GDB_LISTEN_PORT) ")\n"
85 while ((c = getopt_long(argc, argv, "hv::d:s:1p:", long_options, &option_index)) != -1) {
88 printf("XXXXX Shouldn't really normally come here, only if there's no corresponding option\n");
89 printf("option %s", long_options[option_index].name);
91 printf(" with arg %s", optarg);
96 printf(help_str, argv[0]);
101 st->logging_level = atoi(optarg);
103 st->logging_level = DEFAULT_LOGGING_LEVEL;
107 if (strlen(optarg) > sizeof (st->devicename)) {
108 fprintf(stderr, "device name too long: %zd\n", strlen(optarg));
110 strcpy(st->devicename, optarg);
114 st->stlink_version = 1;
117 sscanf(optarg, "%i", &q);
118 if (q < 0 || q > 2) {
119 fprintf(stderr, "stlink version %d unknown!\n", q);
122 st->stlink_version = q;
125 sscanf(optarg, "%i", &q);
127 fprintf(stderr, "Can't use a negative port to listen on: %d\n", q);
136 printf("non-option ARGV-elements: ");
137 while (optind < argc)
138 printf("%s ", argv[optind++]);
145 int main(int argc, char** argv) {
150 memset(&state, 0, sizeof(state));
152 state.stlink_version = 2;
153 state.logging_level = DEFAULT_LOGGING_LEVEL;
154 state.listen_port = DEFAULT_GDB_LISTEN_PORT;
155 parse_options(argc, argv, &state);
156 switch (state.stlink_version) {
158 sl = stlink_open_usb(state.logging_level);
159 if(sl == NULL) return 1;
162 sl = stlink_v1_open(state.logging_level);
163 if(sl == NULL) return 1;
167 printf("Chip ID is %08x, Core ID is %08x.\n", sl->chip_id, sl->core_id);
171 current_memory_map = make_memory_map(sl);
175 if (WSAStartup(MAKEWORD(2,2),&wsadata) !=0 ) {
180 while(serve(sl, state.listen_port) == 0);
187 /* Switch back to mass storage mode before closing. */
189 stlink_exit_debug_mode(sl);
195 static const char* const target_description_F4 =
196 "<?xml version=\"1.0\"?>"
197 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
198 "<target version=\"1.0\">"
199 " <architecture>arm</architecture>"
200 " <feature name=\"org.gnu.gdb.arm.m-profile\">"
201 " <reg name=\"r0\" bitsize=\"32\"/>"
202 " <reg name=\"r1\" bitsize=\"32\"/>"
203 " <reg name=\"r2\" bitsize=\"32\"/>"
204 " <reg name=\"r3\" bitsize=\"32\"/>"
205 " <reg name=\"r4\" bitsize=\"32\"/>"
206 " <reg name=\"r5\" bitsize=\"32\"/>"
207 " <reg name=\"r6\" bitsize=\"32\"/>"
208 " <reg name=\"r7\" bitsize=\"32\"/>"
209 " <reg name=\"r8\" bitsize=\"32\"/>"
210 " <reg name=\"r9\" bitsize=\"32\"/>"
211 " <reg name=\"r10\" bitsize=\"32\"/>"
212 " <reg name=\"r11\" bitsize=\"32\"/>"
213 " <reg name=\"r12\" bitsize=\"32\"/>"
214 " <reg name=\"sp\" bitsize=\"32\" type=\"data_ptr\"/>"
215 " <reg name=\"lr\" bitsize=\"32\"/>"
216 " <reg name=\"pc\" bitsize=\"32\" type=\"code_ptr\"/>"
217 " <reg name=\"xpsr\" bitsize=\"32\" regnum=\"25\"/>"
218 " <reg name=\"msp\" bitsize=\"32\" regnum=\"26\" type=\"data_ptr\" group=\"general\" />"
219 " <reg name=\"psp\" bitsize=\"32\" regnum=\"27\" type=\"data_ptr\" group=\"general\" />"
220 " <reg name=\"control\" bitsize=\"8\" regnum=\"28\" type=\"int\" group=\"general\" />"
221 " <reg name=\"faultmask\" bitsize=\"8\" regnum=\"29\" type=\"int\" group=\"general\" />"
222 " <reg name=\"basepri\" bitsize=\"8\" regnum=\"30\" type=\"int\" group=\"general\" />"
223 " <reg name=\"primask\" bitsize=\"8\" regnum=\"31\" type=\"int\" group=\"general\" />"
224 " <reg name=\"s0\" bitsize=\"32\" regnum=\"32\" type=\"float\" group=\"float\" />"
225 " <reg name=\"s1\" bitsize=\"32\" type=\"float\" group=\"float\" />"
226 " <reg name=\"s2\" bitsize=\"32\" type=\"float\" group=\"float\" />"
227 " <reg name=\"s3\" bitsize=\"32\" type=\"float\" group=\"float\" />"
228 " <reg name=\"s4\" bitsize=\"32\" type=\"float\" group=\"float\" />"
229 " <reg name=\"s5\" bitsize=\"32\" type=\"float\" group=\"float\" />"
230 " <reg name=\"s6\" bitsize=\"32\" type=\"float\" group=\"float\" />"
231 " <reg name=\"s7\" bitsize=\"32\" type=\"float\" group=\"float\" />"
232 " <reg name=\"s8\" bitsize=\"32\" type=\"float\" group=\"float\" />"
233 " <reg name=\"s9\" bitsize=\"32\" type=\"float\" group=\"float\" />"
234 " <reg name=\"s10\" bitsize=\"32\" type=\"float\" group=\"float\" />"
235 " <reg name=\"s11\" bitsize=\"32\" type=\"float\" group=\"float\" />"
236 " <reg name=\"s12\" bitsize=\"32\" type=\"float\" group=\"float\" />"
237 " <reg name=\"s13\" bitsize=\"32\" type=\"float\" group=\"float\" />"
238 " <reg name=\"s14\" bitsize=\"32\" type=\"float\" group=\"float\" />"
239 " <reg name=\"s15\" bitsize=\"32\" type=\"float\" group=\"float\" />"
240 " <reg name=\"s16\" bitsize=\"32\" type=\"float\" group=\"float\" />"
241 " <reg name=\"s17\" bitsize=\"32\" type=\"float\" group=\"float\" />"
242 " <reg name=\"s18\" bitsize=\"32\" type=\"float\" group=\"float\" />"
243 " <reg name=\"s19\" bitsize=\"32\" type=\"float\" group=\"float\" />"
244 " <reg name=\"s20\" bitsize=\"32\" type=\"float\" group=\"float\" />"
245 " <reg name=\"s21\" bitsize=\"32\" type=\"float\" group=\"float\" />"
246 " <reg name=\"s22\" bitsize=\"32\" type=\"float\" group=\"float\" />"
247 " <reg name=\"s23\" bitsize=\"32\" type=\"float\" group=\"float\" />"
248 " <reg name=\"s24\" bitsize=\"32\" type=\"float\" group=\"float\" />"
249 " <reg name=\"s25\" bitsize=\"32\" type=\"float\" group=\"float\" />"
250 " <reg name=\"s26\" bitsize=\"32\" type=\"float\" group=\"float\" />"
251 " <reg name=\"s27\" bitsize=\"32\" type=\"float\" group=\"float\" />"
252 " <reg name=\"s28\" bitsize=\"32\" type=\"float\" group=\"float\" />"
253 " <reg name=\"s29\" bitsize=\"32\" type=\"float\" group=\"float\" />"
254 " <reg name=\"s30\" bitsize=\"32\" type=\"float\" group=\"float\" />"
255 " <reg name=\"s31\" bitsize=\"32\" type=\"float\" group=\"float\" />"
256 " <reg name=\"fpscr\" bitsize=\"32\" type=\"int\" group=\"float\" />"
260 static const char* const memory_map_template_F4 =
261 "<?xml version=\"1.0\"?>"
262 "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
263 " \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
265 " <memory type=\"rom\" start=\"0x00000000\" length=\"0x100000\"/>" // code = sram, bootrom or flash; flash is bigger
266 " <memory type=\"ram\" start=\"0x10000000\" length=\"0x10000\"/>" // ccm ram
267 " <memory type=\"ram\" start=\"0x20000000\" length=\"0x20000\"/>" // sram
268 " <memory type=\"flash\" start=\"0x08000000\" length=\"0x10000\">" //Sectors 0..3
269 " <property name=\"blocksize\">0x4000</property>" //16kB
271 " <memory type=\"flash\" start=\"0x08010000\" length=\"0x10000\">" //Sector 4
272 " <property name=\"blocksize\">0x10000</property>" //64kB
274 " <memory type=\"flash\" start=\"0x08020000\" length=\"0x70000\">" //Sectors 5..11
275 " <property name=\"blocksize\">0x20000</property>" //128kB
277 " <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>" // peripheral regs
278 " <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>" // cortex regs
279 " <memory type=\"rom\" start=\"0x1fff0000\" length=\"0x7800\"/>" // bootrom
280 " <memory type=\"rom\" start=\"0x1fffc000\" length=\"0x10\"/>" // option byte area
283 static const char* const memory_map_template =
284 "<?xml version=\"1.0\"?>"
285 "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
286 " \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
288 " <memory type=\"rom\" start=\"0x00000000\" length=\"0x%zx\"/>" // code = sram, bootrom or flash; flash is bigger
289 " <memory type=\"ram\" start=\"0x20000000\" length=\"0x%zx\"/>" // sram 8k
290 " <memory type=\"flash\" start=\"0x08000000\" length=\"0x%zx\">"
291 " <property name=\"blocksize\">0x%zx</property>"
293 " <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>" // peripheral regs
294 " <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>" // cortex regs
295 " <memory type=\"rom\" start=\"0x%08x\" length=\"0x%zx\"/>" // bootrom
296 " <memory type=\"rom\" start=\"0x1ffff800\" length=\"0x10\"/>" // option byte area
299 char* make_memory_map(stlink_t *sl) {
300 /* This will be freed in serve() */
301 char* map = malloc(4096);
304 if(sl->chip_id==STM32_CHIPID_F4) {
305 strcpy(map, memory_map_template_F4);
307 snprintf(map, 4096, memory_map_template,
310 sl->flash_size, sl->flash_pgsz,
311 sl->sys_base, sl->sys_size);
318 * DWT_COMP0 0xE0001020
319 * DWT_MASK0 0xE0001024
320 * DWT_FUNCTION0 0xE0001028
321 * DWT_COMP1 0xE0001030
322 * DWT_MASK1 0xE0001034
323 * DWT_FUNCTION1 0xE0001038
324 * DWT_COMP2 0xE0001040
325 * DWT_MASK2 0xE0001044
326 * DWT_FUNCTION2 0xE0001048
327 * DWT_COMP3 0xE0001050
328 * DWT_MASK3 0xE0001054
329 * DWT_FUNCTION3 0xE0001058
332 #define DATA_WATCH_NUM 4
334 enum watchfun { WATCHDISABLED = 0, WATCHREAD = 5, WATCHWRITE = 6, WATCHACCESS = 7 };
336 struct code_hw_watchpoint {
342 struct code_hw_watchpoint data_watches[DATA_WATCH_NUM];
344 static void init_data_watchpoints(stlink_t *sl) {
346 printf("init watchpoints\n");
349 // set trcena in debug command to turn on dwt unit
350 stlink_write_debug32(sl, 0xE000EDFC,
351 stlink_read_debug32(sl, 0xE000EDFC) | (1<<24));
353 // make sure all watchpoints are cleared
354 for(int i = 0; i < DATA_WATCH_NUM; i++) {
355 data_watches[i].fun = WATCHDISABLED;
356 stlink_write_debug32(sl, 0xe0001028 + i * 16, 0);
360 static int add_data_watchpoint(stlink_t *sl, enum watchfun wf, stm32_addr_t addr, unsigned int len)
366 // find a free watchpoint
376 if((mask != (uint32_t)-1) && (mask < 16)) {
377 for(i = 0; i < DATA_WATCH_NUM; i++) {
378 // is this an empty slot ?
379 if(data_watches[i].fun == WATCHDISABLED) {
381 printf("insert watchpoint %d addr %x wf %u mask %u len %d\n", i, addr, wf, mask, len);
384 data_watches[i].fun = wf;
385 data_watches[i].addr = addr;
386 data_watches[i].mask = mask;
388 // insert comparator address
389 stlink_write_debug32(sl, 0xE0001020 + i * 16, addr);
392 stlink_write_debug32(sl, 0xE0001024 + i * 16, mask);
395 stlink_write_debug32(sl, 0xE0001028 + i * 16, wf);
397 // just to make sure the matched bit is clear !
398 stlink_read_debug32(sl, 0xE0001028 + i * 16);
405 printf("failure: add watchpoints addr %x wf %u len %u\n", addr, wf, len);
410 static int delete_data_watchpoint(stlink_t *sl, stm32_addr_t addr)
414 for(i = 0 ; i < DATA_WATCH_NUM; i++) {
415 if((data_watches[i].addr == addr) && (data_watches[i].fun != WATCHDISABLED)) {
417 printf("delete watchpoint %d addr %x\n", i, addr);
420 data_watches[i].fun = WATCHDISABLED;
421 stlink_write_debug32(sl, 0xe0001028 + i * 16, 0);
428 printf("failure: delete watchpoint addr %x\n", addr);
434 #define CODE_BREAK_NUM 6
435 #define CODE_BREAK_LOW 0x01
436 #define CODE_BREAK_HIGH 0x02
438 struct code_hw_breakpoint {
443 struct code_hw_breakpoint code_breaks[CODE_BREAK_NUM];
445 static void init_code_breakpoints(stlink_t *sl) {
446 memset(sl->q_buf, 0, 4);
447 stlink_write_debug32(sl, CM3_REG_FP_CTRL, 0x03 /*KEY | ENABLE4*/);
448 printf("KARL - should read back as 0x03, not 60 02 00 00\n");
449 stlink_read_debug32(sl, CM3_REG_FP_CTRL);
451 for(int i = 0; i < CODE_BREAK_NUM; i++) {
452 code_breaks[i].type = 0;
453 stlink_write_debug32(sl, CM3_REG_FP_COMP0 + i * 4, 0);
457 static int update_code_breakpoint(stlink_t *sl, stm32_addr_t addr, int set) {
458 stm32_addr_t fpb_addr = addr & ~0x3;
459 int type = addr & 0x2 ? CODE_BREAK_HIGH : CODE_BREAK_LOW;
462 fprintf(stderr, "update_code_breakpoint: unaligned address %08x\n", addr);
467 for(int i = 0; i < CODE_BREAK_NUM; i++) {
468 if(fpb_addr == code_breaks[i].addr ||
469 (set && code_breaks[i].type == 0)) {
476 if(set) return -1; // Free slot not found
477 else return 0; // Breakpoint is already removed
480 struct code_hw_breakpoint* brk = &code_breaks[id];
482 brk->addr = fpb_addr;
484 if(set) brk->type |= type;
485 else brk->type &= ~type;
489 printf("clearing hw break %d\n", id);
492 stlink_write_debug32(sl, 0xe0002008 + id * 4, 0);
494 uint32_t mask = (brk->addr) | 1 | (brk->type << 30);
497 printf("setting hw break %d at %08x (%d)\n",
498 id, brk->addr, brk->type);
499 printf("reg %08x \n",
503 stlink_write_debug32(sl, 0xe0002008 + id * 4, mask);
515 struct flash_block* next;
518 static struct flash_block* flash_root;
520 static int flash_add_block(stm32_addr_t addr, unsigned length, stlink_t *sl) {
522 if(addr < FLASH_BASE || addr + length > FLASH_BASE + sl->flash_size) {
523 fprintf(stderr, "flash_add_block: incorrect bounds\n");
527 stlink_calculate_pagesize(sl, addr);
528 if(addr % FLASH_PAGE != 0 || length % FLASH_PAGE != 0) {
529 fprintf(stderr, "flash_add_block: unaligned block\n");
533 struct flash_block* new = malloc(sizeof(struct flash_block));
534 new->next = flash_root;
537 new->length = length;
538 new->data = calloc(length, 1);
545 static int flash_populate(stm32_addr_t addr, uint8_t* data, unsigned length) {
546 unsigned int fit_blocks = 0, fit_length = 0;
548 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
549 /* Block: ------X------Y--------
553 * Block intersects with data, if:
557 unsigned X = fb->addr, Y = fb->addr + fb->length;
558 unsigned a = addr, b = addr + length;
560 // from start of the block
561 unsigned start = (a > X ? a : X) - X;
562 unsigned end = (b > Y ? Y : b) - X;
564 memcpy(fb->data + start, data, end - start);
567 fit_length += end - start;
571 if(fit_blocks == 0) {
572 fprintf(stderr, "Unfit data block %08x -> %04x\n", addr, length);
576 if(fit_length != length) {
577 fprintf(stderr, "warning: data block %08x -> %04x truncated to %04x\n",
578 addr, length, fit_length);
579 fprintf(stderr, "(this is not an error, just a GDB glitch)\n");
585 static int flash_go(stlink_t *sl) {
588 // Some kinds of clock settings do not allow writing to flash.
591 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
593 printf("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
596 unsigned length = fb->length;
597 for(stm32_addr_t page = fb->addr; page < fb->addr + fb->length; page += FLASH_PAGE) {
600 stlink_calculate_pagesize(sl, page);
603 printf("flash_do: page %08x\n", page);
606 if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
607 length > FLASH_PAGE ? FLASH_PAGE : length) < 0)
617 for(struct flash_block* fb = flash_root, *next; fb; fb = next) {
628 int serve(stlink_t *sl, int port) {
629 int sock = socket(AF_INET, SOCK_STREAM, 0);
635 unsigned int val = 1;
636 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
638 struct sockaddr_in serv_addr;
639 memset(&serv_addr,0,sizeof(struct sockaddr_in));
640 serv_addr.sin_family = AF_INET;
641 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
642 serv_addr.sin_port = htons(port);
644 if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
649 if(listen(sock, 5) < 0) {
655 stlink_force_debug(sl);
657 init_code_breakpoints(sl);
658 init_data_watchpoints(sl);
660 printf("Listening at *:%d...\n", port);
662 int client = accept(sock, NULL, NULL);
663 //signal (SIGINT, SIG_DFL);
671 printf("GDB connected.\n");
674 * To allow resetting the chip from GDB it is required to
675 * emulate attaching and detaching to target.
677 unsigned int attached = 1;
682 int status = gdb_recv_packet(client, &packet);
684 fprintf(stderr, "cannot recv: %d\n", status);
689 printf("recv: %s\n", packet);
697 if(packet[1] == 'P' || packet[1] == 'C' || packet[1] == 'L') {
702 char *separator = strstr(packet, ":"), *params = "";
703 if(separator == NULL) {
704 separator = packet + strlen(packet);
706 params = separator + 1;
709 unsigned queryNameLength = (separator - &packet[1]);
710 char* queryName = calloc(queryNameLength + 1, 1);
711 strncpy(queryName, &packet[1], queryNameLength);
714 printf("query: %s;%s\n", queryName, params);
717 if(!strcmp(queryName, "Supported")) {
718 if(sl->chip_id==STM32_CHIPID_F4) {
719 reply = strdup("PacketSize=3fff;qXfer:memory-map:read+;qXfer:features:read+");
722 reply = strdup("PacketSize=3fff;qXfer:memory-map:read+");
724 } else if(!strcmp(queryName, "Xfer")) {
725 char *type, *op, *__s_addr, *s_length;
727 char *annex __attribute__((unused));
729 type = strsep(&tok, ":");
730 op = strsep(&tok, ":");
731 annex = strsep(&tok, ":");
732 __s_addr = strsep(&tok, ",");
735 unsigned addr = strtoul(__s_addr, NULL, 16),
736 length = strtoul(s_length, NULL, 16);
739 printf("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
740 type, op, annex, addr, length);
743 const char* data = NULL;
745 if(!strcmp(type, "memory-map") && !strcmp(op, "read"))
746 data = current_memory_map;
748 if(!strcmp(type, "features") && !strcmp(op, "read"))
749 data = target_description_F4;
752 unsigned data_length = strlen(data);
753 if(addr + length > data_length)
754 length = data_length - addr;
759 reply = calloc(length + 2, 1);
761 strncpy(&reply[1], data, length);
764 } else if(!strncmp(queryName, "Rcmd,",4)) {
765 // Rcmd uses the wrong separator
766 char *separator = strstr(packet, ","), *params = "";
767 if(separator == NULL) {
768 separator = packet + strlen(packet);
770 params = separator + 1;
774 if (!strncmp(params,"726573756d65",12)) {// resume
776 printf("Rcmd: resume\n");
780 reply = strdup("OK");
781 } else if (!strncmp(params,"68616c74",8)) { //halt
782 reply = strdup("OK");
784 stlink_force_debug(sl);
787 printf("Rcmd: halt\n");
789 } else if (!strncmp(params,"6a7461675f7265736574",20)) { //jtag_reset
790 reply = strdup("OK");
792 stlink_jtag_reset(sl, 1);
793 stlink_jtag_reset(sl, 0);
794 stlink_force_debug(sl);
797 printf("Rcmd: jtag_reset\n");
799 } else if (!strncmp(params,"7265736574",10)) { //reset
800 reply = strdup("OK");
802 stlink_force_debug(sl);
804 init_code_breakpoints(sl);
805 init_data_watchpoints(sl);
808 printf("Rcmd: reset\n");
812 printf("Rcmd: %s\n", params);
829 char *cmdName = strtok_r(packet, ":;", ¶ms);
831 cmdName++; // vCommand -> Command
833 if(!strcmp(cmdName, "FlashErase")) {
834 char *__s_addr, *s_length;
837 __s_addr = strsep(&tok, ",");
840 unsigned addr = strtoul(__s_addr, NULL, 16),
841 length = strtoul(s_length, NULL, 16);
844 printf("FlashErase: addr:%08x,len:%04x\n",
848 if(flash_add_block(addr, length, sl) < 0) {
849 reply = strdup("E00");
851 reply = strdup("OK");
853 } else if(!strcmp(cmdName, "FlashWrite")) {
854 char *__s_addr, *data;
857 __s_addr = strsep(&tok, ":");
860 unsigned addr = strtoul(__s_addr, NULL, 16);
861 unsigned data_length = status - (data - packet);
863 // Length of decoded data cannot be more than
864 // encoded, as escapes are removed.
865 // Additional byte is reserved for alignment fix.
866 uint8_t *decoded = calloc(data_length + 1, 1);
867 unsigned dec_index = 0;
868 for(unsigned int i = 0; i < data_length; i++) {
869 if(data[i] == 0x7d) {
871 decoded[dec_index++] = data[i] ^ 0x20;
873 decoded[dec_index++] = data[i];
878 if(dec_index % 2 != 0)
882 printf("binary packet %d -> %d\n", data_length, dec_index);
885 if(flash_populate(addr, decoded, dec_index) < 0) {
886 reply = strdup("E00");
888 reply = strdup("OK");
890 } else if(!strcmp(cmdName, "FlashDone")) {
891 if(flash_go(sl) < 0) {
892 reply = strdup("E00");
894 reply = strdup("OK");
896 } else if(!strcmp(cmdName, "Kill")) {
899 reply = strdup("OK");
912 int status = gdb_check_for_interrupt(client);
914 fprintf(stderr, "cannot check for int: %d\n", status);
919 stlink_force_debug(sl);
924 if(sl->core_stat == STLINK_CORE_HALTED) {
931 reply = strdup("S05"); // TRAP
937 reply = strdup("S05"); // TRAP
942 reply = strdup("S05"); // TRAP
944 /* Stub shall reply OK if not attached. */
945 reply = strdup("OK");
950 stlink_read_all_regs(sl, ®p);
952 reply = calloc(8 * 16 + 1, 1);
953 for(int i = 0; i < 16; i++)
954 sprintf(&reply[i * 8], "%08x", htonl(regp.r[i]));
959 unsigned id = strtoul(&packet[1], NULL, 16);
960 unsigned myreg = 0xDEADDEAD;
963 stlink_read_reg(sl, id, ®p);
964 myreg = htonl(regp.r[id]);
965 } else if(id == 0x19) {
966 stlink_read_reg(sl, 16, ®p);
967 myreg = htonl(regp.xpsr);
968 } else if(id == 0x1A) {
969 stlink_read_reg(sl, 17, ®p);
970 myreg = htonl(regp.main_sp);
971 } else if(id == 0x1B) {
972 stlink_read_reg(sl, 18, ®p);
973 myreg = htonl(regp.process_sp);
974 } else if(id == 0x1C) {
975 stlink_read_unsupported_reg(sl, id, ®p);
976 myreg = htonl(regp.control);
977 } else if(id == 0x1D) {
978 stlink_read_unsupported_reg(sl, id, ®p);
979 myreg = htonl(regp.faultmask);
980 } else if(id == 0x1E) {
981 stlink_read_unsupported_reg(sl, id, ®p);
982 myreg = htonl(regp.basepri);
983 } else if(id == 0x1F) {
984 stlink_read_unsupported_reg(sl, id, ®p);
985 myreg = htonl(regp.primask);
986 } else if(id >= 0x20 && id < 0x40) {
987 stlink_read_unsupported_reg(sl, id, ®p);
988 myreg = htonl(regp.s[id-0x20]);
989 } else if(id == 0x40) {
990 stlink_read_unsupported_reg(sl, id, ®p);
991 myreg = htonl(regp.fpscr);
993 reply = strdup("E00");
996 reply = calloc(8 + 1, 1);
997 sprintf(reply, "%08x", myreg);
1003 char* s_reg = &packet[1];
1004 char* s_value = strstr(&packet[1], "=") + 1;
1006 unsigned reg = strtoul(s_reg, NULL, 16);
1007 unsigned value = strtoul(s_value, NULL, 16);
1010 stlink_write_reg(sl, ntohl(value), reg);
1011 } else if(reg == 0x19) {
1012 stlink_write_reg(sl, ntohl(value), 16);
1013 } else if(reg == 0x1A) {
1014 stlink_write_reg(sl, ntohl(value), 17);
1015 } else if(reg == 0x1B) {
1016 stlink_write_reg(sl, ntohl(value), 18);
1017 } else if(reg == 0x1C) {
1018 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1019 } else if(reg == 0x1D) {
1020 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1021 } else if(reg == 0x1E) {
1022 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1023 } else if(reg == 0x1F) {
1024 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1025 } else if(reg >= 0x20 && reg < 0x40) {
1026 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1027 } else if(reg == 0x40) {
1028 stlink_write_unsupported_reg(sl, ntohl(value), reg, ®p);
1030 reply = strdup("E00");
1034 reply = strdup("OK");
1041 for(int i = 0; i < 16; i++) {
1043 strncpy(str, &packet[1 + i * 8], 8);
1044 uint32_t reg = strtoul(str, NULL, 16);
1045 stlink_write_reg(sl, ntohl(reg), i);
1048 reply = strdup("OK");
1052 char* s_start = &packet[1];
1053 char* s_count = strstr(&packet[1], ",") + 1;
1055 stm32_addr_t start = strtoul(s_start, NULL, 16);
1056 unsigned count = strtoul(s_count, NULL, 16);
1058 unsigned adj_start = start % 4;
1060 stlink_read_mem32(sl, start - adj_start, (count % 4 == 0) ?
1061 count : count + 4 - (count % 4));
1063 reply = calloc(count * 2 + 1, 1);
1064 for(unsigned int i = 0; i < count; i++) {
1065 reply[i * 2 + 0] = hex[sl->q_buf[i + adj_start] >> 4];
1066 reply[i * 2 + 1] = hex[sl->q_buf[i + adj_start] & 0xf];
1073 char* s_start = &packet[1];
1074 char* s_count = strstr(&packet[1], ",") + 1;
1075 char* hexdata = strstr(packet, ":") + 1;
1077 stm32_addr_t start = strtoul(s_start, NULL, 16);
1078 unsigned count = strtoul(s_count, NULL, 16);
1081 unsigned align_count = 4 - start % 4;
1082 if (align_count > count) align_count = count;
1083 for(unsigned int i = 0; i < align_count; i ++) {
1084 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
1085 uint8_t byte = strtoul(hex, NULL, 16);
1086 sl->q_buf[i] = byte;
1088 stlink_write_mem8(sl, start, align_count);
1089 start += align_count;
1090 count -= align_count;
1091 hexdata += 2*align_count;
1094 if(count - count % 4) {
1095 unsigned aligned_count = count - count % 4;
1097 for(unsigned int i = 0; i < aligned_count; i ++) {
1098 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
1099 uint8_t byte = strtoul(hex, NULL, 16);
1100 sl->q_buf[i] = byte;
1102 stlink_write_mem32(sl, start, aligned_count);
1103 count -= aligned_count;
1104 start += aligned_count;
1105 hexdata += 2*aligned_count;
1109 for(unsigned int i = 0; i < count; i ++) {
1110 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
1111 uint8_t byte = strtoul(hex, NULL, 16);
1112 sl->q_buf[i] = byte;
1114 stlink_write_mem8(sl, start, count);
1116 reply = strdup("OK");
1122 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
1123 stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
1125 switch (packet[1]) {
1127 if(update_code_breakpoint(sl, addr, 1) < 0) {
1128 reply = strdup("E00");
1130 reply = strdup("OK");
1134 case '2': // insert write watchpoint
1135 case '3': // insert read watchpoint
1136 case '4': // insert access watchpoint
1139 if(packet[1] == '2') {
1141 } else if(packet[1] == '3') {
1147 if(add_data_watchpoint(sl, wf, addr, len) < 0) {
1148 reply = strdup("E00");
1150 reply = strdup("OK");
1162 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
1163 //stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
1165 switch (packet[1]) {
1166 case '1': // remove breakpoint
1167 update_code_breakpoint(sl, addr, 0);
1168 reply = strdup("OK");
1171 case '2' : // remove write watchpoint
1172 case '3' : // remove read watchpoint
1173 case '4' : // remove access watchpoint
1174 if(delete_data_watchpoint(sl, addr) < 0) {
1175 reply = strdup("E00");
1177 reply = strdup("OK");
1189 * Enter extended mode which allows restarting.
1190 * We do support that always.
1193 reply = strdup("OK");
1199 /* Reset the core. */
1202 init_code_breakpoints(sl);
1203 init_data_watchpoints(sl);
1207 reply = strdup("OK");
1218 printf("send: %s\n", reply);
1221 int result = gdb_send_packet(client, reply);
1223 fprintf(stderr, "cannot send: %d\n", result);