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>
15 #include <sys/socket.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
20 #include <stlink-common.h>
22 #include "gdb-remote.h"
24 #define DEFAULT_LOGGING_LEVEL 10
25 #define DEFAULT_GDB_LISTEN_PORT 4242
27 #define STRINGIFY_inner(name) #name
28 #define STRINGIFY(name) STRINGIFY_inner(name)
30 #define FLASH_BASE 0x08000000
31 #define FLASH_PAGE (sl->flash_pgsz)
32 #define FLASH_PAGE_MASK (~((1 << 10) - 1))
33 #define FLASH_SIZE (FLASH_PAGE * 128)
35 volatile int do_exit = 0;
41 static const char hex[] = "0123456789abcdef";
43 static const char* current_memory_map = NULL;
46 * Chip IDs are explained in the appropriate programming manual for the
47 * DBGMCU_IDCODE register (0xE0042000)
50 #define CORE_M3_R1 0x1BA00477
51 #define CORE_M3_R2 0x4BA00477
52 #define CORE_M4_R0 0x2BA01477
57 uint32_t flash_size_reg;
58 uint32_t max_flash_size, flash_pagesize;
60 uint32_t bootrom_base, bootrom_size;
62 { 0x410, "F1 Medium-density device", 0x1ffff7e0,
63 0x20000, 0x400, 0x5000, 0x1ffff000, 0x800 }, // table 2, pm0063
64 { 0x411, "F2 device", 0, /* No flash size register found in the docs*/
65 0x100000, 0x20000, 0x20000, 0x1fff0000, 0x7800 }, // table 1, pm0059
66 { 0x412, "F1 Low-density device", 0x1ffff7e0,
67 0x8000, 0x400, 0x2800, 0x1ffff000, 0x800 }, // table 1, pm0063
68 { 0x413, "F4 device", 0x1FFF7A10,
69 0x100000, 0x20000, 0x30000, 0x1fff0000, 0x7800 }, // table 1, pm0081
70 { 0x414, "F1 High-density device", 0x1ffff7e0,
71 0x80000, 0x800, 0x10000, 0x1ffff000, 0x800 }, // table 3 pm0063
72 // This ignores the EEPROM! (and uses the page erase size,
73 // not the sector write protection...)
74 { 0x416, "L1 Med-density device", 0x1FF8004C, // table 1, pm0062
75 0x20000, 0x100, 0x4000, 0x1ff00000, 0x1000 },
76 { 0x418, "F1 Connectivity line device", 0x1ffff7e0,
77 0x40000, 0x800, 0x10000, 0x1fffb000, 0x4800 },
78 { 0x420, "F1 Medium-density value line device", 0x1ffff7e0,
79 0x20000, 0x400, 0x2000, 0x1ffff000, 0x800 },
80 { 0x428, "F1 High-density value line device", 0x1ffff7e0,
81 0x80000, 0x800, 0x8000, 0x1ffff000, 0x800 },
82 { 0x430, "F1 XL-density device", 0x1ffff7e0, // pm0068
83 0x100000, 0x800, 0x18000, 0x1fffe000, 0x1800 },
87 typedef struct _st_state_t {
88 // things from command line, bleh
90 // "/dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTE531X6-if00-port0" is only 58 chars
97 int serve(stlink_t *sl, int port);
98 char* make_memory_map(const struct chip_params *params, uint32_t flash_size);
101 int parse_options(int argc, char** argv, st_state_t *st) {
102 static struct option long_options[] = {
103 {"help", no_argument, NULL, 'h'},
104 {"verbose", optional_argument, NULL, 'v'},
105 {"device", required_argument, NULL, 'd'},
106 {"stlink_version", required_argument, NULL, 's'},
107 {"stlinkv1", no_argument, NULL, '1'},
108 {"listen_port", required_argument, NULL, 'p'},
111 const char * help_str = "%s - usage:\n\n"
112 " -h, --help\t\tPrint this help\n"
113 " -vXX, --verbose=XX\tspecify a specific verbosity level (0..99)\n"
114 " -v, --verbose\tspecify generally verbose logging\n"
115 " -d <device>, --device=/dev/stlink2_1\n"
116 "\t\t\tWhere is your stlink device connected?\n"
117 " -s X, --stlink_version=X\n"
118 "\t\t\tChoose what version of stlink to use, (defaults to 2)\n"
119 " -1, --stlinkv1\tForce stlink version 1\n"
120 " -p 4242, --listen_port=1234\n"
121 "\t\t\tSet the gdb server listen port. "
122 "(default port: " STRINGIFY(DEFAULT_GDB_LISTEN_PORT) ")\n"
126 int option_index = 0;
129 while ((c = getopt_long(argc, argv, "hv::d:s:1p:", long_options, &option_index)) != -1) {
132 printf("XXXXX Shouldn't really normally come here, only if there's no corresponding option\n");
133 printf("option %s", long_options[option_index].name);
135 printf(" with arg %s", optarg);
140 printf(help_str, argv[0]);
145 st->logging_level = atoi(optarg);
147 st->logging_level = DEFAULT_LOGGING_LEVEL;
151 if (strlen(optarg) > sizeof (st->devicename)) {
152 fprintf(stderr, "device name too long: %ld\n", strlen(optarg));
154 strcpy(st->devicename, optarg);
158 st->stlink_version = 1;
161 sscanf(optarg, "%i", &q);
162 if (q < 0 || q > 2) {
163 fprintf(stderr, "stlink version %d unknown!\n", q);
166 st->stlink_version = q;
169 sscanf(optarg, "%i", &q);
171 fprintf(stderr, "Can't use a negative port to listen on: %d\n", q);
180 printf("non-option ARGV-elements: ");
181 while (optind < argc)
182 printf("%s ", argv[optind++]);
189 int main(int argc, char** argv) {
194 memset(&state, 0, sizeof(state));
196 state.stlink_version = 2;
197 state.logging_level = 10;
198 state.listen_port = DEFAULT_GDB_LISTEN_PORT;
199 parse_options(argc, argv, &state);
200 switch (state.stlink_version) {
202 sl = stlink_open_usb(state.logging_level);
203 if(sl == NULL) return 1;
206 #if (CONFIG_USE_LIBSG == 1)
207 if (strlen(state.devicename) == 0) {
208 const int DevNumMax = 99;
209 int ExistDevCount = 0;
211 for (int DevNum = 0; DevNum <= DevNumMax; DevNum++) {
213 char DevName[] = "/dev/sgX";
214 const int X_index = 7;
215 DevName[X_index] = DevNum + '0';
216 if (!access(DevName, F_OK)) {
217 sl = stlink_quirk_open(DevName, 0);
220 } else if (DevNum < 100) {
221 char DevName[] = "/dev/sgXY";
222 const int X_index = 7;
223 const int Y_index = 8;
224 DevName[X_index] = DevNum / 10 + '0';
225 DevName[Y_index] = DevNum % 10 + '0';
226 if (!access(DevName, F_OK)) {
227 sl = stlink_quirk_open(DevName, 0);
231 if (sl != NULL) break;
235 fprintf(stdout, "\nNumber of /dev/sgX devices found: %i \n",
237 fprintf(stderr, "ST-LINK not found\n");
241 sl = stlink_quirk_open(state.devicename, state.logging_level);
245 fprintf(stderr, "Support for stlink v1 disabled at build time...\n");
246 fprintf(stderr, "Perhaps you're on OSX, and we haven't finished removing the libsg deps?\n");
251 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
252 stlink_exit_dfu_mode(sl);
255 if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) {
256 stlink_enter_swd_mode(sl);
259 uint32_t chip_id = stlink_chip_id(sl);
260 uint32_t core_id = stlink_core_id(sl);
262 /* Fix chip_id for F4 */
263 if (((chip_id & 0xFFF) == 0x411) && (core_id == CORE_M4_R0)) {
264 printf("Fixing wrong chip_id for STM32F4 Rev A errata\n");
268 printf("Chip ID is %08x, Core ID is %08x.\n", chip_id, core_id);
270 const struct chip_params* params = NULL;
272 for(int i = 0; i < sizeof(devices) / sizeof(devices[0]); i++) {
273 if(devices[i].chip_id == (chip_id & 0xFFF)) {
274 params = &devices[i];
280 fprintf(stderr, "Cannot recognize the connected device!\n");
284 printf("Device connected: %s\n", params->description);
285 printf("Device parameters: SRAM: 0x%x bytes, Flash: up to 0x%x bytes in pages of 0x%x bytes\n",
286 params->sram_size, params->max_flash_size, params->flash_pagesize);
288 FLASH_PAGE = params->flash_pagesize;
292 stlink_read_mem32(sl, params->flash_size_reg, 4);
293 flash_size = sl->q_buf[0] | (sl->q_buf[1] << 8);
295 printf("Flash size is %d KiB.\n", flash_size);
296 // memory map is in 1k blocks.
297 current_memory_map = make_memory_map(params, flash_size * 0x400);
299 while(serve(sl, state.listen_port) == 0);
301 /* Switch back to mass storage mode before closing. */
303 stlink_exit_debug_mode(sl);
309 static const char* const memory_map_template =
310 "<?xml version=\"1.0\"?>"
311 "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
312 " \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
314 " <memory type=\"rom\" start=\"0x00000000\" length=\"0x%x\"/>" // code = sram, bootrom or flash; flash is bigger
315 " <memory type=\"ram\" start=\"0x20000000\" length=\"0x%x\"/>" // sram 8k
316 " <memory type=\"flash\" start=\"0x08000000\" length=\"0x%x\">"
317 " <property name=\"blocksize\">0x%x</property>"
319 " <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>" // peripheral regs
320 " <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>" // cortex regs
321 " <memory type=\"rom\" start=\"0x%08x\" length=\"0x%x\"/>" // bootrom
322 " <memory type=\"rom\" start=\"0x1ffff800\" length=\"0x8\"/>" // option byte area
325 char* make_memory_map(const struct chip_params *params, uint32_t flash_size) {
326 /* This will be freed in serve() */
327 char* map = malloc(4096);
330 snprintf(map, 4096, memory_map_template,
333 flash_size, params->flash_pagesize,
334 params->bootrom_base, params->bootrom_size);
341 * DWT_COMP0 0xE0001020
342 * DWT_MASK0 0xE0001024
343 * DWT_FUNCTION0 0xE0001028
344 * DWT_COMP1 0xE0001030
345 * DWT_MASK1 0xE0001034
346 * DWT_FUNCTION1 0xE0001038
347 * DWT_COMP2 0xE0001040
348 * DWT_MASK2 0xE0001044
349 * DWT_FUNCTION2 0xE0001048
350 * DWT_COMP3 0xE0001050
351 * DWT_MASK3 0xE0001054
352 * DWT_FUNCTION3 0xE0001058
355 #define DATA_WATCH_NUM 4
357 enum watchfun { WATCHDISABLED = 0, WATCHREAD = 5, WATCHWRITE = 6, WATCHACCESS = 7 };
359 struct code_hw_watchpoint {
365 struct code_hw_watchpoint data_watches[DATA_WATCH_NUM];
367 static void init_data_watchpoints(stlink_t *sl) {
369 printf("init watchpoints\n");
372 // set trcena in debug command to turn on dwt unit
373 stlink_read_mem32(sl, 0xE000EDFC, 4);
375 stlink_write_mem32(sl, 0xE000EDFC, 4);
377 // make sure all watchpoints are cleared
378 memset(sl->q_buf, 0, 4);
379 for(int i = 0; i < DATA_WATCH_NUM; i++) {
380 data_watches[i].fun = WATCHDISABLED;
381 stlink_write_mem32(sl, 0xe0001028 + i * 16, 4);
385 static int add_data_watchpoint(stlink_t *sl, enum watchfun wf, stm32_addr_t addr, unsigned int len)
391 // find a free watchpoint
401 if((mask != -1) && (mask < 16)) {
402 for(i = 0; i < DATA_WATCH_NUM; i++) {
403 // is this an empty slot ?
404 if(data_watches[i].fun == WATCHDISABLED) {
406 printf("insert watchpoint %d addr %x wf %u mask %u len %d\n", i, addr, wf, mask, len);
409 data_watches[i].fun = wf;
410 data_watches[i].addr = addr;
411 data_watches[i].mask = mask;
413 // insert comparator address
414 sl->q_buf[0] = (addr & 0xff);
415 sl->q_buf[1] = ((addr >> 8) & 0xff);
416 sl->q_buf[2] = ((addr >> 16) & 0xff);
417 sl->q_buf[3] = ((addr >> 24) & 0xff);
419 stlink_write_mem32(sl, 0xE0001020 + i * 16, 4);
422 memset(sl->q_buf, 0, 4);
424 stlink_write_mem32(sl, 0xE0001024 + i * 16, 4);
427 memset(sl->q_buf, 0, 4);
429 stlink_write_mem32(sl, 0xE0001028 + i * 16, 4);
431 // just to make sure the matched bit is clear !
432 stlink_read_mem32(sl, 0xE0001028 + i * 16, 4);
439 printf("failure: add watchpoints addr %x wf %u len %u\n", addr, wf, len);
444 static int delete_data_watchpoint(stlink_t *sl, stm32_addr_t addr)
448 for(i = 0 ; i < DATA_WATCH_NUM; i++) {
449 if((data_watches[i].addr == addr) && (data_watches[i].fun != WATCHDISABLED)) {
451 printf("delete watchpoint %d addr %x\n", i, addr);
454 memset(sl->q_buf, 0, 4);
455 data_watches[i].fun = WATCHDISABLED;
456 stlink_write_mem32(sl, 0xe0001028 + i * 16, 4);
463 printf("failure: delete watchpoint addr %x\n", addr);
469 #define CODE_BREAK_NUM 6
470 #define CODE_BREAK_LOW 0x01
471 #define CODE_BREAK_HIGH 0x02
473 struct code_hw_breakpoint {
478 struct code_hw_breakpoint code_breaks[CODE_BREAK_NUM];
480 static void init_code_breakpoints(stlink_t *sl) {
481 memset(sl->q_buf, 0, 4);
482 sl->q_buf[0] = 0x03; // KEY | ENABLE
483 stlink_write_mem32(sl, CM3_REG_FP_CTRL, 4);
484 printf("KARL - should read back as 0x03, not 60 02 00 00\n");
485 stlink_read_mem32(sl, CM3_REG_FP_CTRL, 4);
487 memset(sl->q_buf, 0, 4);
488 for(int i = 0; i < CODE_BREAK_NUM; i++) {
489 code_breaks[i].type = 0;
490 stlink_write_mem32(sl, CM3_REG_FP_COMP0 + i * 4, 4);
494 static int update_code_breakpoint(stlink_t *sl, stm32_addr_t addr, int set) {
495 stm32_addr_t fpb_addr = addr & ~0x3;
496 int type = addr & 0x2 ? CODE_BREAK_HIGH : CODE_BREAK_LOW;
499 fprintf(stderr, "update_code_breakpoint: unaligned address %08x\n", addr);
504 for(int i = 0; i < CODE_BREAK_NUM; i++) {
505 if(fpb_addr == code_breaks[i].addr ||
506 (set && code_breaks[i].type == 0)) {
513 if(set) return -1; // Free slot not found
514 else return 0; // Breakpoint is already removed
517 struct code_hw_breakpoint* brk = &code_breaks[id];
519 brk->addr = fpb_addr;
521 if(set) brk->type |= type;
522 else brk->type &= ~type;
524 memset(sl->q_buf, 0, 4);
528 printf("clearing hw break %d\n", id);
531 stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
533 sl->q_buf[0] = ( brk->addr & 0xff) | 1;
534 sl->q_buf[1] = ((brk->addr >> 8) & 0xff);
535 sl->q_buf[2] = ((brk->addr >> 16) & 0xff);
536 sl->q_buf[3] = ((brk->addr >> 24) & 0xff) | (brk->type << 6);
539 printf("setting hw break %d at %08x (%d)\n",
540 id, brk->addr, brk->type);
541 printf("reg %02x %02x %02x %02x\n",
542 sl->q_buf[3], sl->q_buf[2], sl->q_buf[1], sl->q_buf[0]);
545 stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
557 struct flash_block* next;
560 static struct flash_block* flash_root;
562 static int flash_add_block(stm32_addr_t addr, unsigned length,
564 if(addr < FLASH_BASE || addr + length > FLASH_BASE + FLASH_SIZE) {
565 fprintf(stderr, "flash_add_block: incorrect bounds\n");
569 if(addr % FLASH_PAGE != 0 || length % FLASH_PAGE != 0) {
570 fprintf(stderr, "flash_add_block: unaligned block\n");
574 struct flash_block* new = malloc(sizeof(struct flash_block));
575 new->next = flash_root;
578 new->length = length;
579 new->data = calloc(length, 1);
586 static int flash_populate(stm32_addr_t addr, uint8_t* data, unsigned length) {
587 int fit_blocks = 0, fit_length = 0;
589 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
590 /* Block: ------X------Y--------
594 * Block intersects with data, if:
598 unsigned X = fb->addr, Y = fb->addr + fb->length;
599 unsigned a = addr, b = addr + length;
601 // from start of the block
602 unsigned start = (a > X ? a : X) - X;
603 unsigned end = (b > Y ? Y : b) - X;
605 memcpy(fb->data + start, data, end - start);
608 fit_length += end - start;
612 if(fit_blocks == 0) {
613 fprintf(stderr, "Unfit data block %08x -> %04x\n", addr, length);
617 if(fit_length != length) {
618 fprintf(stderr, "warning: data block %08x -> %04x truncated to %04x\n",
619 addr, length, fit_length);
620 fprintf(stderr, "(this is not an error, just a GDB glitch)\n");
626 static int flash_go(stlink_t *sl) {
629 // Some kinds of clock settings do not allow writing to flash.
632 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
634 printf("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
637 unsigned length = fb->length;
638 for(stm32_addr_t page = fb->addr; page < fb->addr + fb->length; page += FLASH_PAGE) {
640 printf("flash_do: page %08x\n", page);
643 stlink_erase_flash_page(sl, page);
645 if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
646 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, int port) {
669 int sock = socket(AF_INET, SOCK_STREAM, 0);
675 unsigned int val = 1;
676 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
678 struct sockaddr_in serv_addr = {0};
679 serv_addr.sin_family = AF_INET;
680 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
681 serv_addr.sin_port = htons(port);
683 if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
688 if(listen(sock, 5) < 0) {
693 stlink_force_debug(sl);
695 init_code_breakpoints(sl);
696 init_data_watchpoints(sl);
698 printf("Listening at *:%d...\n", port);
700 (void) signal (SIGINT, ctrl_c);
701 int client = accept(sock, NULL, NULL);
702 signal (SIGINT, SIG_DFL);
710 printf("GDB connected.\n");
713 * To allow resetting the chip from GDB it is required to
714 * emulate attaching and detaching to target.
716 unsigned int attached = 1;
721 int status = gdb_recv_packet(client, &packet);
723 fprintf(stderr, "cannot recv: %d\n", status);
728 printf("recv: %s\n", packet);
736 if(packet[1] == 'P' || packet[1] == 'C' || packet[1] == 'L') {
741 char *separator = strstr(packet, ":"), *params = "";
742 if(separator == NULL) {
743 separator = packet + strlen(packet);
745 params = separator + 1;
748 unsigned queryNameLength = (separator - &packet[1]);
749 char* queryName = calloc(queryNameLength + 1, 1);
750 strncpy(queryName, &packet[1], queryNameLength);
753 printf("query: %s;%s\n", queryName, params);
756 if(!strcmp(queryName, "Supported")) {
757 reply = strdup("PacketSize=3fff;qXfer:memory-map:read+");
758 } else if(!strcmp(queryName, "Xfer")) {
759 char *type, *op, *s_addr, *s_length;
761 char *annex __attribute__((unused));
763 type = strsep(&tok, ":");
764 op = strsep(&tok, ":");
765 annex = strsep(&tok, ":");
766 s_addr = strsep(&tok, ",");
769 unsigned addr = strtoul(s_addr, NULL, 16),
770 length = strtoul(s_length, NULL, 16);
773 printf("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
774 type, op, annex, addr, length);
777 const char* data = NULL;
779 if(!strcmp(type, "memory-map") && !strcmp(op, "read"))
780 data = current_memory_map;
783 unsigned data_length = strlen(data);
784 if(addr + length > data_length)
785 length = data_length - addr;
790 reply = calloc(length + 2, 1);
792 strncpy(&reply[1], data, length);
807 char *cmdName = strtok_r(packet, ":;", ¶ms);
809 cmdName++; // vCommand -> Command
811 if(!strcmp(cmdName, "FlashErase")) {
812 char *s_addr, *s_length;
815 s_addr = strsep(&tok, ",");
818 unsigned addr = strtoul(s_addr, NULL, 16),
819 length = strtoul(s_length, NULL, 16);
822 printf("FlashErase: addr:%08x,len:%04x\n",
826 if(flash_add_block(addr, length, sl) < 0) {
827 reply = strdup("E00");
829 reply = strdup("OK");
831 } else if(!strcmp(cmdName, "FlashWrite")) {
835 s_addr = strsep(&tok, ":");
838 unsigned addr = strtoul(s_addr, NULL, 16);
839 unsigned data_length = status - (data - packet);
841 // Length of decoded data cannot be more than
842 // encoded, as escapes are removed.
843 // Additional byte is reserved for alignment fix.
844 uint8_t *decoded = calloc(data_length + 1, 1);
845 unsigned dec_index = 0;
846 for(int i = 0; i < data_length; i++) {
847 if(data[i] == 0x7d) {
849 decoded[dec_index++] = data[i] ^ 0x20;
851 decoded[dec_index++] = data[i];
856 if(dec_index % 2 != 0)
860 printf("binary packet %d -> %d\n", data_length, dec_index);
863 if(flash_populate(addr, decoded, dec_index) < 0) {
864 reply = strdup("E00");
866 reply = strdup("OK");
868 } else if(!strcmp(cmdName, "FlashDone")) {
869 if(flash_go(sl) < 0) {
870 reply = strdup("E00");
872 reply = strdup("OK");
874 } else if(!strcmp(cmdName, "Kill")) {
877 reply = strdup("OK");
890 int status = gdb_check_for_interrupt(client);
892 fprintf(stderr, "cannot check for int: %d\n", status);
897 stlink_force_debug(sl);
902 if(sl->core_stat == STLINK_CORE_HALTED) {
909 reply = strdup("S05"); // TRAP
915 reply = strdup("S05"); // TRAP
920 reply = strdup("S05"); // TRAP
922 /* Stub shall reply OK if not attached. */
923 reply = strdup("OK");
928 stlink_read_all_regs(sl, ®p);
930 reply = calloc(8 * 16 + 1, 1);
931 for(int i = 0; i < 16; i++)
932 sprintf(&reply[i * 8], "%08x", htonl(regp.r[i]));
937 unsigned id = strtoul(&packet[1], NULL, 16);
938 unsigned myreg = 0xDEADDEAD;
941 stlink_read_reg(sl, id, ®p);
942 myreg = htonl(regp.r[id]);
943 } else if(id == 0x19) {
944 stlink_read_reg(sl, 16, ®p);
945 myreg = htonl(regp.xpsr);
947 reply = strdup("E00");
950 reply = calloc(8 + 1, 1);
951 sprintf(reply, "%08x", myreg);
957 char* s_reg = &packet[1];
958 char* s_value = strstr(&packet[1], "=") + 1;
960 unsigned reg = strtoul(s_reg, NULL, 16);
961 unsigned value = strtoul(s_value, NULL, 16);
964 stlink_write_reg(sl, ntohl(value), reg);
965 } else if(reg == 0x19) {
966 stlink_write_reg(sl, ntohl(value), 16);
968 reply = strdup("E00");
972 reply = strdup("OK");
979 for(int i = 0; i < 16; i++) {
981 strncpy(str, &packet[1 + i * 8], 8);
982 uint32_t reg = strtoul(str, NULL, 16);
983 stlink_write_reg(sl, ntohl(reg), i);
986 reply = strdup("OK");
990 char* s_start = &packet[1];
991 char* s_count = strstr(&packet[1], ",") + 1;
993 stm32_addr_t start = strtoul(s_start, NULL, 16);
994 unsigned count = strtoul(s_count, NULL, 16);
996 unsigned adj_start = start % 4;
998 stlink_read_mem32(sl, start - adj_start, (count % 4 == 0) ?
999 count : count + 4 - (count % 4));
1001 reply = calloc(count * 2 + 1, 1);
1002 for(int i = 0; i < count; i++) {
1003 reply[i * 2 + 0] = hex[sl->q_buf[i + adj_start] >> 4];
1004 reply[i * 2 + 1] = hex[sl->q_buf[i + adj_start] & 0xf];
1011 char* s_start = &packet[1];
1012 char* s_count = strstr(&packet[1], ",") + 1;
1013 char* hexdata = strstr(packet, ":") + 1;
1015 stm32_addr_t start = strtoul(s_start, NULL, 16);
1016 unsigned count = strtoul(s_count, NULL, 16);
1018 for(int i = 0; i < count; i ++) {
1019 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
1020 uint8_t byte = strtoul(hex, NULL, 16);
1021 sl->q_buf[i] = byte;
1024 if((count % 4) == 0 && (start % 4) == 0) {
1025 stlink_write_mem32(sl, start, count);
1027 stlink_write_mem8(sl, start, count);
1030 reply = strdup("OK");
1037 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
1038 stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
1040 switch (packet[1]) {
1042 if(update_code_breakpoint(sl, addr, 1) < 0) {
1043 reply = strdup("E00");
1045 reply = strdup("OK");
1049 case '2': // insert write watchpoint
1050 case '3': // insert read watchpoint
1051 case '4': // insert access watchpoint
1054 if(packet[1] == '2') {
1056 } else if(packet[1] == '3') {
1060 if(add_data_watchpoint(sl, wf, addr, len) < 0) {
1061 reply = strdup("E00");
1063 reply = strdup("OK");
1076 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
1077 //stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
1079 switch (packet[1]) {
1080 case '1': // remove breakpoint
1081 update_code_breakpoint(sl, addr, 0);
1082 reply = strdup("OK");
1085 case '2' : // remove write watchpoint
1086 case '3' : // remove read watchpoint
1087 case '4' : // remove access watchpoint
1088 if(delete_data_watchpoint(sl, addr) < 0) {
1089 reply = strdup("E00");
1091 reply = strdup("OK");
1103 * Enter extended mode which allows restarting.
1104 * We do support that always.
1107 reply = strdup("OK");
1113 /* Reset the core. */
1116 init_code_breakpoints(sl);
1117 init_data_watchpoints(sl);
1121 reply = strdup("OK");
1132 printf("send: %s\n", reply);
1135 int result = gdb_send_packet(client, reply);
1137 fprintf(stderr, "cannot send: %d\n", result);