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 100
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 static const char hex[] = "0123456789abcdef";
37 static const char* current_memory_map = NULL;
40 * Chip IDs are explained in the appropriate programming manual for the
41 * DBGMCU_IDCODE register (0xE0042000)
44 #define CORE_M3_R1 0x1BA00477
45 #define CORE_M3_R2 0x4BA00477
46 #define CORE_M4_R0 0x2BA01477
51 uint32_t flash_size_reg;
52 uint32_t max_flash_size, flash_pagesize;
54 uint32_t bootrom_base, bootrom_size;
56 { 0x410, "F1 Medium-density device", 0x1ffff7e0,
57 0x20000, 0x400, 0x5000, 0x1ffff000, 0x800 }, // table 2, pm0063
58 { 0x411, "F2 device", 0, /* No flash size register found in the docs*/
59 0x100000, 0x20000, 0x20000, 0x1fff0000, 0x7800 }, // table 1, pm0059
60 { 0x412, "F1 Low-density device", 0x1ffff7e0,
61 0x8000, 0x400, 0x2800, 0x1ffff000, 0x800 }, // table 1, pm0063
62 { 0x413, "F4 device", 0x1FFF7A10,
63 0x100000, 0x20000, 0x30000, 0x1fff0000, 0x7800 }, // table 1, pm0081
64 { 0x414, "F1 High-density device", 0x1ffff7e0,
65 0x80000, 0x800, 0x10000, 0x1ffff000, 0x800 }, // table 3 pm0063
66 // This ignores the EEPROM! (and uses the page erase size,
67 // not the sector write protection...)
68 { 0x416, "L1 Med-density device", 0x1FF8004C, // table 1, pm0062
69 0x20000, 0x100, 0x4000, 0x1ff00000, 0x1000 },
70 { 0x418, "F1 Connectivity line device", 0x1ffff7e0,
71 0x40000, 0x800, 0x10000, 0x1fffb000, 0x4800 },
72 { 0x420, "F1 Medium-density value line device", 0x1ffff7e0,
73 0x20000, 0x400, 0x2000, 0x1ffff000, 0x800 },
74 { 0x428, "F1 High-density value line device", 0x1ffff7e0,
75 0x80000, 0x800, 0x8000, 0x1ffff000, 0x800 },
76 { 0x430, "F1 XL-density device", 0x1ffff7e0, // pm0068
77 0x100000, 0x800, 0x18000, 0x1fffe000, 0x1800 },
81 typedef struct _st_state_t {
82 // things from command line, bleh
84 // "/dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTE531X6-if00-port0" is only 58 chars
91 int serve(stlink_t *sl, int port);
92 char* make_memory_map(const struct chip_params *params, uint32_t flash_size);
95 int parse_options(int argc, char** argv, st_state_t *st) {
96 static struct option long_options[] = {
97 {"help", no_argument, NULL, 'h'},
98 {"verbose", optional_argument, NULL, 'v'},
99 {"device", required_argument, NULL, 'd'},
100 {"stlink_version", required_argument, NULL, 's'},
101 {"stlinkv1", no_argument, NULL, '1'},
102 {"listen_port", required_argument, NULL, 'p'},
105 const char * help_str = "%s - usage:\n\n"
106 " -h, --help\t\tPrint this help\n"
107 " -vXX, --verbose=XX\tspecify a specific verbosity level (0..99)\n"
108 " -v, --verbose\tspecify generally verbose logging\n"
109 " -d <device>, --device=/dev/stlink2_1\n"
110 "\t\t\tWhere is your stlink device connected?\n"
111 " -s X, --stlink_version=X\n"
112 "\t\t\tChoose what version of stlink to use, (defaults to 2)\n"
113 " -1, --stlinkv1\tForce stlink version 1\n"
114 " -p 4242, --listen_port=1234\n"
115 "\t\t\tSet the gdb server listen port. "
116 "(default port: " STRINGIFY(DEFAULT_GDB_LISTEN_PORT) ")\n"
120 int option_index = 0;
123 while ((c = getopt_long(argc, argv, "hv::d:s:1p:", long_options, &option_index)) != -1) {
126 printf("XXXXX Shouldn't really normally come here, only if there's no corresponding option\n");
127 printf("option %s", long_options[option_index].name);
129 printf(" with arg %s", optarg);
134 printf(help_str, argv[0]);
139 st->logging_level = atoi(optarg);
141 st->logging_level = DEFAULT_LOGGING_LEVEL;
145 if (strlen(optarg) > sizeof (st->devicename)) {
146 fprintf(stderr, "device name too long: %ld\n", strlen(optarg));
148 strcpy(st->devicename, optarg);
152 st->stlink_version = 1;
155 sscanf(optarg, "%i", &q);
156 if (q < 0 || q > 2) {
157 fprintf(stderr, "stlink version %d unknown!\n", q);
160 st->stlink_version = q;
163 sscanf(optarg, "%i", &q);
165 fprintf(stderr, "Can't use a negative port to listen on: %d\n", q);
174 printf("non-option ARGV-elements: ");
175 while (optind < argc)
176 printf("%s ", argv[optind++]);
183 int main(int argc, char** argv) {
188 memset(&state, 0, sizeof(state));
190 state.stlink_version = 2;
191 state.logging_level = DEFAULT_LOGGING_LEVEL;
192 state.listen_port = DEFAULT_GDB_LISTEN_PORT;
193 parse_options(argc, argv, &state);
194 switch (state.stlink_version) {
196 sl = stlink_open_usb(state.logging_level);
197 if(sl == NULL) return 1;
200 if (strlen(state.devicename) == 0) {
201 const int DevNumMax = 99;
202 int ExistDevCount = 0;
204 for (int DevNum = 0; DevNum <= DevNumMax; DevNum++) {
206 char DevName[] = "/dev/sgX";
207 const int X_index = 7;
208 DevName[X_index] = DevNum + '0';
209 if (!access(DevName, F_OK)) {
210 sl = stlink_v1_open(DevName, 0);
213 } else if (DevNum < 100) {
214 char DevName[] = "/dev/sgXY";
215 const int X_index = 7;
216 const int Y_index = 8;
217 DevName[X_index] = DevNum / 10 + '0';
218 DevName[Y_index] = DevNum % 10 + '0';
219 if (!access(DevName, F_OK)) {
220 sl = stlink_v1_open(DevName, 0);
224 if (sl != NULL) break;
228 fprintf(stdout, "\nNumber of /dev/sgX devices found: %i \n",
230 fprintf(stderr, "ST-LINK not found\n");
234 sl = stlink_v1_open(state.devicename, state.logging_level);
239 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) {
240 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
241 stlink_exit_dfu_mode(sl);
243 stlink_enter_swd_mode(sl);
246 uint32_t chip_id = stlink_chip_id(sl);
247 uint32_t core_id = stlink_core_id(sl);
249 /* Fix chip_id for F4 */
250 if (((chip_id & 0xFFF) == 0x411) && (core_id == CORE_M4_R0)) {
251 printf("Fixing wrong chip_id for STM32F4 Rev A errata\n");
255 printf("Chip ID is %08x, Core ID is %08x.\n", chip_id, core_id);
257 const struct chip_params* params = NULL;
259 for(int i = 0; i < sizeof(devices) / sizeof(devices[0]); i++) {
260 if(devices[i].chip_id == (chip_id & 0xFFF)) {
261 params = &devices[i];
267 fprintf(stderr, "Cannot recognize the connected device!\n");
271 printf("Device connected: %s\n", params->description);
272 printf("Device parameters: SRAM: 0x%x bytes, Flash: up to 0x%x bytes in pages of 0x%x bytes\n",
273 params->sram_size, params->max_flash_size, params->flash_pagesize);
275 FLASH_PAGE = params->flash_pagesize;
279 stlink_read_mem32(sl, params->flash_size_reg, 4);
280 flash_size = sl->q_buf[0] | (sl->q_buf[1] << 8);
282 printf("Flash size is %d KiB.\n", flash_size);
283 // memory map is in 1k blocks.
284 current_memory_map = make_memory_map(params, flash_size * 0x400);
286 while(serve(sl, state.listen_port) == 0);
288 /* Switch back to mass storage mode before closing. */
290 stlink_exit_debug_mode(sl);
296 static const char* const memory_map_template =
297 "<?xml version=\"1.0\"?>"
298 "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
299 " \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
301 " <memory type=\"rom\" start=\"0x00000000\" length=\"0x%x\"/>" // code = sram, bootrom or flash; flash is bigger
302 " <memory type=\"ram\" start=\"0x20000000\" length=\"0x%x\"/>" // sram 8k
303 " <memory type=\"flash\" start=\"0x08000000\" length=\"0x%x\">"
304 " <property name=\"blocksize\">0x%x</property>"
306 " <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>" // peripheral regs
307 " <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>" // cortex regs
308 " <memory type=\"rom\" start=\"0x%08x\" length=\"0x%x\"/>" // bootrom
309 " <memory type=\"rom\" start=\"0x1ffff800\" length=\"0x8\"/>" // option byte area
312 char* make_memory_map(const struct chip_params *params, uint32_t flash_size) {
313 /* This will be freed in serve() */
314 char* map = malloc(4096);
317 snprintf(map, 4096, memory_map_template,
320 flash_size, params->flash_pagesize,
321 params->bootrom_base, params->bootrom_size);
328 * DWT_COMP0 0xE0001020
329 * DWT_MASK0 0xE0001024
330 * DWT_FUNCTION0 0xE0001028
331 * DWT_COMP1 0xE0001030
332 * DWT_MASK1 0xE0001034
333 * DWT_FUNCTION1 0xE0001038
334 * DWT_COMP2 0xE0001040
335 * DWT_MASK2 0xE0001044
336 * DWT_FUNCTION2 0xE0001048
337 * DWT_COMP3 0xE0001050
338 * DWT_MASK3 0xE0001054
339 * DWT_FUNCTION3 0xE0001058
342 #define DATA_WATCH_NUM 4
344 enum watchfun { WATCHDISABLED = 0, WATCHREAD = 5, WATCHWRITE = 6, WATCHACCESS = 7 };
346 struct code_hw_watchpoint {
352 struct code_hw_watchpoint data_watches[DATA_WATCH_NUM];
354 static void init_data_watchpoints(stlink_t *sl) {
356 printf("init watchpoints\n");
359 // set trcena in debug command to turn on dwt unit
360 stlink_read_mem32(sl, 0xE000EDFC, 4);
362 stlink_write_mem32(sl, 0xE000EDFC, 4);
364 // make sure all watchpoints are cleared
365 memset(sl->q_buf, 0, 4);
366 for(int i = 0; i < DATA_WATCH_NUM; i++) {
367 data_watches[i].fun = WATCHDISABLED;
368 stlink_write_mem32(sl, 0xe0001028 + i * 16, 4);
372 static int add_data_watchpoint(stlink_t *sl, enum watchfun wf, stm32_addr_t addr, unsigned int len)
378 // find a free watchpoint
388 if((mask != -1) && (mask < 16)) {
389 for(i = 0; i < DATA_WATCH_NUM; i++) {
390 // is this an empty slot ?
391 if(data_watches[i].fun == WATCHDISABLED) {
393 printf("insert watchpoint %d addr %x wf %u mask %u len %d\n", i, addr, wf, mask, len);
396 data_watches[i].fun = wf;
397 data_watches[i].addr = addr;
398 data_watches[i].mask = mask;
400 // insert comparator address
401 sl->q_buf[0] = (addr & 0xff);
402 sl->q_buf[1] = ((addr >> 8) & 0xff);
403 sl->q_buf[2] = ((addr >> 16) & 0xff);
404 sl->q_buf[3] = ((addr >> 24) & 0xff);
406 stlink_write_mem32(sl, 0xE0001020 + i * 16, 4);
409 memset(sl->q_buf, 0, 4);
411 stlink_write_mem32(sl, 0xE0001024 + i * 16, 4);
414 memset(sl->q_buf, 0, 4);
416 stlink_write_mem32(sl, 0xE0001028 + i * 16, 4);
418 // just to make sure the matched bit is clear !
419 stlink_read_mem32(sl, 0xE0001028 + i * 16, 4);
426 printf("failure: add watchpoints addr %x wf %u len %u\n", addr, wf, len);
431 static int delete_data_watchpoint(stlink_t *sl, stm32_addr_t addr)
435 for(i = 0 ; i < DATA_WATCH_NUM; i++) {
436 if((data_watches[i].addr == addr) && (data_watches[i].fun != WATCHDISABLED)) {
438 printf("delete watchpoint %d addr %x\n", i, addr);
441 memset(sl->q_buf, 0, 4);
442 data_watches[i].fun = WATCHDISABLED;
443 stlink_write_mem32(sl, 0xe0001028 + i * 16, 4);
450 printf("failure: delete watchpoint addr %x\n", addr);
456 #define CODE_BREAK_NUM 6
457 #define CODE_BREAK_LOW 0x01
458 #define CODE_BREAK_HIGH 0x02
460 struct code_hw_breakpoint {
465 struct code_hw_breakpoint code_breaks[CODE_BREAK_NUM];
467 static void init_code_breakpoints(stlink_t *sl) {
468 memset(sl->q_buf, 0, 4);
469 sl->q_buf[0] = 0x03; // KEY | ENABLE
470 stlink_write_mem32(sl, CM3_REG_FP_CTRL, 4);
471 printf("KARL - should read back as 0x03, not 60 02 00 00\n");
472 stlink_read_mem32(sl, CM3_REG_FP_CTRL, 4);
474 memset(sl->q_buf, 0, 4);
475 for(int i = 0; i < CODE_BREAK_NUM; i++) {
476 code_breaks[i].type = 0;
477 stlink_write_mem32(sl, CM3_REG_FP_COMP0 + i * 4, 4);
481 static int update_code_breakpoint(stlink_t *sl, stm32_addr_t addr, int set) {
482 stm32_addr_t fpb_addr = addr & ~0x3;
483 int type = addr & 0x2 ? CODE_BREAK_HIGH : CODE_BREAK_LOW;
486 fprintf(stderr, "update_code_breakpoint: unaligned address %08x\n", addr);
491 for(int i = 0; i < CODE_BREAK_NUM; i++) {
492 if(fpb_addr == code_breaks[i].addr ||
493 (set && code_breaks[i].type == 0)) {
500 if(set) return -1; // Free slot not found
501 else return 0; // Breakpoint is already removed
504 struct code_hw_breakpoint* brk = &code_breaks[id];
506 brk->addr = fpb_addr;
508 if(set) brk->type |= type;
509 else brk->type &= ~type;
511 memset(sl->q_buf, 0, 4);
515 printf("clearing hw break %d\n", id);
518 stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
520 sl->q_buf[0] = ( brk->addr & 0xff) | 1;
521 sl->q_buf[1] = ((brk->addr >> 8) & 0xff);
522 sl->q_buf[2] = ((brk->addr >> 16) & 0xff);
523 sl->q_buf[3] = ((brk->addr >> 24) & 0xff) | (brk->type << 6);
526 printf("setting hw break %d at %08x (%d)\n",
527 id, brk->addr, brk->type);
528 printf("reg %02x %02x %02x %02x\n",
529 sl->q_buf[3], sl->q_buf[2], sl->q_buf[1], sl->q_buf[0]);
532 stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
544 struct flash_block* next;
547 static struct flash_block* flash_root;
549 static int flash_add_block(stm32_addr_t addr, unsigned length,
551 if(addr < FLASH_BASE || addr + length > FLASH_BASE + FLASH_SIZE) {
552 fprintf(stderr, "flash_add_block: incorrect bounds\n");
556 if(addr % FLASH_PAGE != 0 || length % FLASH_PAGE != 0) {
557 fprintf(stderr, "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 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 fprintf(stderr, "Unfit data block %08x -> %04x\n", addr, length);
604 if(fit_length != length) {
605 fprintf(stderr, "warning: data block %08x -> %04x truncated to %04x\n",
606 addr, length, fit_length);
607 fprintf(stderr, "(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) {
621 printf("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
624 unsigned length = fb->length;
625 for(stm32_addr_t page = fb->addr; page < fb->addr + fb->length; page += FLASH_PAGE) {
627 printf("flash_do: page %08x\n", page);
630 stlink_erase_flash_page(sl, page);
632 if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
633 length > FLASH_PAGE ? FLASH_PAGE : length) < 0)
644 for(struct flash_block* fb = flash_root, *next; fb; fb = next) {
655 int serve(stlink_t *sl, int port) {
656 int sock = socket(AF_INET, SOCK_STREAM, 0);
662 unsigned int val = 1;
663 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
665 struct sockaddr_in serv_addr = {0};
666 serv_addr.sin_family = AF_INET;
667 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
668 serv_addr.sin_port = htons(port);
670 if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
675 if(listen(sock, 5) < 0) {
680 stlink_force_debug(sl);
682 init_code_breakpoints(sl);
683 init_data_watchpoints(sl);
685 printf("Listening at *:%d...\n", port);
687 int client = accept(sock, NULL, NULL);
688 signal (SIGINT, SIG_DFL);
696 printf("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 fprintf(stderr, "cannot recv: %d\n", status);
714 printf("recv: %s\n", packet);
722 if(packet[1] == 'P' || packet[1] == 'C' || packet[1] == 'L') {
727 char *separator = strstr(packet, ":"), *params = "";
728 if(separator == NULL) {
729 separator = packet + strlen(packet);
731 params = separator + 1;
734 unsigned queryNameLength = (separator - &packet[1]);
735 char* queryName = calloc(queryNameLength + 1, 1);
736 strncpy(queryName, &packet[1], queryNameLength);
739 printf("query: %s;%s\n", queryName, params);
742 if(!strcmp(queryName, "Supported")) {
743 reply = strdup("PacketSize=3fff;qXfer:memory-map:read+");
744 } else if(!strcmp(queryName, "Xfer")) {
745 char *type, *op, *s_addr, *s_length;
747 char *annex __attribute__((unused));
749 type = strsep(&tok, ":");
750 op = strsep(&tok, ":");
751 annex = strsep(&tok, ":");
752 s_addr = strsep(&tok, ",");
755 unsigned addr = strtoul(s_addr, NULL, 16),
756 length = strtoul(s_length, NULL, 16);
759 printf("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
760 type, op, annex, addr, length);
763 const char* data = NULL;
765 if(!strcmp(type, "memory-map") && !strcmp(op, "read"))
766 data = current_memory_map;
769 unsigned data_length = strlen(data);
770 if(addr + length > data_length)
771 length = data_length - addr;
776 reply = calloc(length + 2, 1);
778 strncpy(&reply[1], data, length);
793 char *cmdName = strtok_r(packet, ":;", ¶ms);
795 cmdName++; // vCommand -> Command
797 if(!strcmp(cmdName, "FlashErase")) {
798 char *s_addr, *s_length;
801 s_addr = strsep(&tok, ",");
804 unsigned addr = strtoul(s_addr, NULL, 16),
805 length = strtoul(s_length, NULL, 16);
808 printf("FlashErase: addr:%08x,len:%04x\n",
812 if(flash_add_block(addr, length, sl) < 0) {
813 reply = strdup("E00");
815 reply = strdup("OK");
817 } else if(!strcmp(cmdName, "FlashWrite")) {
821 s_addr = strsep(&tok, ":");
824 unsigned addr = strtoul(s_addr, NULL, 16);
825 unsigned data_length = status - (data - packet);
827 // Length of decoded data cannot be more than
828 // encoded, as escapes are removed.
829 // Additional byte is reserved for alignment fix.
830 uint8_t *decoded = calloc(data_length + 1, 1);
831 unsigned dec_index = 0;
832 for(int i = 0; i < data_length; i++) {
833 if(data[i] == 0x7d) {
835 decoded[dec_index++] = data[i] ^ 0x20;
837 decoded[dec_index++] = data[i];
842 if(dec_index % 2 != 0)
846 printf("binary packet %d -> %d\n", data_length, dec_index);
849 if(flash_populate(addr, decoded, dec_index) < 0) {
850 reply = strdup("E00");
852 reply = strdup("OK");
854 } else if(!strcmp(cmdName, "FlashDone")) {
855 if(flash_go(sl) < 0) {
856 reply = strdup("E00");
858 reply = strdup("OK");
860 } else if(!strcmp(cmdName, "Kill")) {
863 reply = strdup("OK");
876 int status = gdb_check_for_interrupt(client);
878 fprintf(stderr, "cannot check for int: %d\n", status);
883 stlink_force_debug(sl);
888 if(sl->core_stat == STLINK_CORE_HALTED) {
895 reply = strdup("S05"); // TRAP
901 reply = strdup("S05"); // TRAP
906 reply = strdup("S05"); // TRAP
908 /* Stub shall reply OK if not attached. */
909 reply = strdup("OK");
914 stlink_read_all_regs(sl, ®p);
916 reply = calloc(8 * 16 + 1, 1);
917 for(int i = 0; i < 16; i++)
918 sprintf(&reply[i * 8], "%08x", htonl(regp.r[i]));
923 unsigned id = strtoul(&packet[1], NULL, 16);
924 unsigned myreg = 0xDEADDEAD;
927 stlink_read_reg(sl, id, ®p);
928 myreg = htonl(regp.r[id]);
929 } else if(id == 0x19) {
930 stlink_read_reg(sl, 16, ®p);
931 myreg = htonl(regp.xpsr);
933 reply = strdup("E00");
936 reply = calloc(8 + 1, 1);
937 sprintf(reply, "%08x", myreg);
943 char* s_reg = &packet[1];
944 char* s_value = strstr(&packet[1], "=") + 1;
946 unsigned reg = strtoul(s_reg, NULL, 16);
947 unsigned value = strtoul(s_value, NULL, 16);
950 stlink_write_reg(sl, ntohl(value), reg);
951 } else if(reg == 0x19) {
952 stlink_write_reg(sl, ntohl(value), 16);
954 reply = strdup("E00");
958 reply = strdup("OK");
965 for(int i = 0; i < 16; i++) {
967 strncpy(str, &packet[1 + i * 8], 8);
968 uint32_t reg = strtoul(str, NULL, 16);
969 stlink_write_reg(sl, ntohl(reg), i);
972 reply = strdup("OK");
976 char* s_start = &packet[1];
977 char* s_count = strstr(&packet[1], ",") + 1;
979 stm32_addr_t start = strtoul(s_start, NULL, 16);
980 unsigned count = strtoul(s_count, NULL, 16);
982 unsigned adj_start = start % 4;
984 stlink_read_mem32(sl, start - adj_start, (count % 4 == 0) ?
985 count : count + 4 - (count % 4));
987 reply = calloc(count * 2 + 1, 1);
988 for(int i = 0; i < count; i++) {
989 reply[i * 2 + 0] = hex[sl->q_buf[i + adj_start] >> 4];
990 reply[i * 2 + 1] = hex[sl->q_buf[i + adj_start] & 0xf];
997 char* s_start = &packet[1];
998 char* s_count = strstr(&packet[1], ",") + 1;
999 char* hexdata = strstr(packet, ":") + 1;
1001 stm32_addr_t start = strtoul(s_start, NULL, 16);
1002 unsigned count = strtoul(s_count, NULL, 16);
1004 for(int i = 0; i < count; i ++) {
1005 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
1006 uint8_t byte = strtoul(hex, NULL, 16);
1007 sl->q_buf[i] = byte;
1010 if((count % 4) == 0 && (start % 4) == 0) {
1011 stlink_write_mem32(sl, start, count);
1013 stlink_write_mem8(sl, start, count);
1016 reply = strdup("OK");
1023 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
1024 stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
1026 switch (packet[1]) {
1028 if(update_code_breakpoint(sl, addr, 1) < 0) {
1029 reply = strdup("E00");
1031 reply = strdup("OK");
1035 case '2': // insert write watchpoint
1036 case '3': // insert read watchpoint
1037 case '4': // insert access watchpoint
1040 if(packet[1] == '2') {
1042 } else if(packet[1] == '3') {
1046 if(add_data_watchpoint(sl, wf, addr, len) < 0) {
1047 reply = strdup("E00");
1049 reply = strdup("OK");
1062 stm32_addr_t addr = strtoul(&packet[3], &endptr, 16);
1063 //stm32_addr_t len = strtoul(&endptr[1], NULL, 16);
1065 switch (packet[1]) {
1066 case '1': // remove breakpoint
1067 update_code_breakpoint(sl, addr, 0);
1068 reply = strdup("OK");
1071 case '2' : // remove write watchpoint
1072 case '3' : // remove read watchpoint
1073 case '4' : // remove access watchpoint
1074 if(delete_data_watchpoint(sl, addr) < 0) {
1075 reply = strdup("E00");
1077 reply = strdup("OK");
1089 * Enter extended mode which allows restarting.
1090 * We do support that always.
1093 reply = strdup("OK");
1099 /* Reset the core. */
1102 init_code_breakpoints(sl);
1103 init_data_watchpoints(sl);
1107 reply = strdup("OK");
1118 printf("send: %s\n", reply);
1121 int result = gdb_send_packet(client, reply);
1123 fprintf(stderr, "cannot send: %d\n", result);