]> git.gag.com Git - fw/stlink/blob - src/gdb-server.c
cc2b527d0149904639d47fd038f93487c1f261d7
[fw/stlink] / src / gdb-server.c
1 /* -*- tab-width:8 -*- */
2
3 /*
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.
7 */
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17 #include "gdb-remote.h"
18 #include "stlink-hw.h"
19
20 static const char hex[] = "0123456789abcdef";
21
22 static const char* current_memory_map = NULL;
23
24 struct chip_params {
25         uint32_t chip_id;
26         char* description;
27         uint32_t max_flash_size, flash_pagesize;
28         uint32_t sram_size;
29         uint32_t bootrom_base, bootrom_size;
30 } const devices[] = {
31         { 0x412, "Low-density device",
32           0x8000,   0x400, 0x2800,  0x1ffff000, 0x800  },
33         { 0x410, "Medium-density device",
34           0x20000,  0x400, 0x5000,  0x1ffff000, 0x800  },
35         { 0x414, "High-density device",
36           0x80000,  0x800, 0x10000, 0x1ffff000, 0x800  },
37         { 0x418, "Connectivity line device",
38           0x40000,  0x800, 0x10000, 0x1fffb000, 0x4800 },
39         { 0x420, "Medium-density value line device",
40           0x20000,  0x400, 0x2000,  0x1ffff000, 0x800  },
41         { 0x428, "High-density value line device",
42           0x80000,  0x800, 0x8000,  0x1ffff000, 0x800  },
43         { 0x430, "XL-density device",
44           0x100000, 0x800, 0x18000, 0x1fffe000, 0x1800 },
45         { 0 }
46 };
47
48 int serve(struct stlink* sl, int port);
49 static char* make_memory_map(const struct chip_params *params, uint32_t flash_size);
50
51 int main(int argc, char** argv) {
52         if(argc != 3) {
53                 fprintf(stderr, "Usage: %s <port> /dev/sgX\n", argv[0]);
54                 return 1;
55         }
56
57         struct stlink *sl = stlink_quirk_open(argv[2], 0);
58         if (sl == NULL)
59                 return 1;
60
61         if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
62                 stlink_enter_swd_mode(sl);
63
64         uint32_t chip_id;
65
66         stlink_read_mem32(sl, 0xE0042000, 4);
67         chip_id = sl->q_buf[0] | (sl->q_buf[1] << 8) | (sl->q_buf[2] << 16) |
68                 (sl->q_buf[3] << 24);
69
70         printf("Chip ID is %08x.\n", chip_id);
71
72         const struct chip_params* params = NULL;
73
74         for(int i = 0; i < sizeof(devices) / sizeof(devices[0]); i++) {
75                 if(devices[i].chip_id == (chip_id & 0xFFF)) {
76                         params = &devices[i];
77                         break;
78                 }
79         }
80
81         if(params == NULL) {
82                 fprintf(stderr, "Cannot recognize the connected device!\n");
83                 return 0;
84         }
85
86         printf("Device connected: %s\n", params->description);
87         printf("Device parameters: SRAM: 0x%x bytes, Flash: up to 0x%x bytes in pages of 0x%x bytes\n",
88                 params->sram_size, params->max_flash_size, params->flash_pagesize);
89
90         uint32_t flash_size;
91
92         stlink_read_mem32(sl, 0x1FFFF7E0, 4);
93         flash_size = sl->q_buf[0] | (sl->q_buf[1] << 8);
94
95         printf("Flash size is %d KiB.\n", flash_size);
96
97         current_memory_map = make_memory_map(params, flash_size * 0x400);
98
99         int port = atoi(argv[1]);
100
101         while(serve(sl, port) == 0);
102
103         stlink_close(sl);
104
105         return 0;
106 }
107
108 static const char* const memory_map_template =
109   "<?xml version=\"1.0\"?>"
110   "<!DOCTYPE memory-map PUBLIC \"+//IDN gnu.org//DTD GDB Memory Map V1.0//EN\""
111   "     \"http://sourceware.org/gdb/gdb-memory-map.dtd\">"
112   "<memory-map>"
113   "  <memory type=\"rom\" start=\"0x00000000\" length=\"0x%x\"/>"       // code = sram, bootrom or flash; flash is bigger
114   "  <memory type=\"ram\" start=\"0x20000000\" length=\"0x%x\"/>"       // sram 8k
115   "  <memory type=\"flash\" start=\"0x08000000\" length=\"0x%x\">"
116   "    <property name=\"blocksize\">0x%x</property>"
117   "  </memory>"
118   "  <memory type=\"ram\" start=\"0x40000000\" length=\"0x1fffffff\"/>" // peripheral regs
119   "  <memory type=\"ram\" start=\"0xe0000000\" length=\"0x1fffffff\"/>" // cortex regs
120   "  <memory type=\"rom\" start=\"0x%08x\" length=\"0x%x\"/>"           // bootrom
121   "  <memory type=\"rom\" start=\"0x1ffff800\" length=\"0x8\"/>"        // option byte area
122   "</memory-map>";
123
124 static char*
125 make_memory_map(const struct chip_params *params, uint32_t flash_size) {
126         /* This will be freed in serve() */
127         char* map = malloc(4096);
128         map[0] = '\0';
129
130         snprintf(map, 4096, memory_map_template,
131                         flash_size,
132                         params->sram_size,
133                         flash_size, params->flash_pagesize,
134                         params->bootrom_base, params->bootrom_size);
135
136         return map;
137 }
138
139 #define CODE_BREAK_NUM  6
140
141 #define CODE_BREAK_LOW  0x01
142 #define CODE_BREAK_HIGH 0x02
143
144 struct code_hw_breakpoint {
145         stm32_addr_t addr;
146         int          type;
147 };
148
149 struct code_hw_breakpoint code_breaks[CODE_BREAK_NUM];
150
151 static void init_code_breakpoints(struct stlink* sl) {
152         memset(sl->q_buf, 0, 4);
153         sl->q_buf[0] = 0x03; // KEY | ENABLE
154         stlink_write_mem32(sl, 0xe0002000, 4);
155
156         memset(sl->q_buf, 0, 4);
157         for(int i = 0; i < CODE_BREAK_NUM; i++) {
158                 code_breaks[i].type = 0;
159                 stlink_write_mem32(sl, 0xe0002008 + i * 4, 4);
160         }
161 }
162
163 static int update_code_breakpoint(struct stlink* sl, stm32_addr_t addr, int set) {
164         stm32_addr_t fpb_addr = addr & ~0x3;
165         int type = addr & 0x2 ? CODE_BREAK_HIGH : CODE_BREAK_LOW;
166
167         if(addr & 1) {
168                 fprintf(stderr, "update_code_breakpoint: unaligned address %08x\n", addr);
169                 return -1;
170         }
171
172         int id = -1;
173         for(int i = 0; i < CODE_BREAK_NUM; i++) {
174                 if(fpb_addr == code_breaks[i].addr ||
175                         (set && code_breaks[i].type == 0)) {
176                         id = i;
177                         break;
178                 }
179         }
180
181         if(id == -1) {
182                 if(set) return -1; // Free slot not found
183                 else    return 0;  // Breakpoint is already removed
184         }
185
186         struct code_hw_breakpoint* brk = &code_breaks[id];
187
188         brk->addr = fpb_addr;
189
190         if(set) brk->type |= type;
191         else    brk->type &= ~type;
192
193         memset(sl->q_buf, 0, 4);
194
195         if(brk->type == 0) {
196                 #ifdef DEBUG
197                 printf("clearing hw break %d\n", id);
198                 #endif
199
200                 stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
201         } else {
202                 sl->q_buf[0] = ( brk->addr        & 0xff) | 1;
203                 sl->q_buf[1] = ((brk->addr >> 8)  & 0xff);
204                 sl->q_buf[2] = ((brk->addr >> 16) & 0xff);
205                 sl->q_buf[3] = ((brk->addr >> 24) & 0xff) | (brk->type << 6);
206
207                 #ifdef DEBUG
208                 printf("setting hw break %d at %08x (%d)\n",
209                         id, brk->addr, brk->type);
210                 printf("reg %02x %02x %02x %02x\n",
211                         sl->q_buf[3], sl->q_buf[2], sl->q_buf[1], sl->q_buf[0]);
212                 #endif
213
214                 stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
215         }
216
217         return 0;
218 }
219
220 #define FLASH_BASE 0x08000000
221 #define FLASH_PAGE 0x400
222 #define FLASH_PAGE_MASK (~((1 << 10) - 1))
223 #define FLASH_SIZE (FLASH_PAGE * 128)
224
225 struct flash_block {
226         stm32_addr_t addr;
227         unsigned     length;
228         uint8_t*     data;
229
230         struct flash_block* next;
231 };
232
233 static struct flash_block* flash_root;
234
235 static int flash_add_block(stm32_addr_t addr, unsigned length) {
236         if(addr < FLASH_BASE || addr + length > FLASH_BASE + FLASH_SIZE) {
237                 fprintf(stderr, "flash_add_block: incorrect bounds\n");
238                 return -1;
239         }
240
241         if(addr % FLASH_PAGE != 0 || length % FLASH_PAGE != 0) {
242                 fprintf(stderr, "flash_add_block: unaligned block\n");
243                 return -1;
244         }
245
246         struct flash_block* new = malloc(sizeof(struct flash_block));
247         new->next = flash_root;
248
249         new->addr   = addr;
250         new->length = length;
251         new->data   = calloc(length, 1);
252
253         flash_root = new;
254
255         return 0;
256 }
257
258 static int flash_populate(stm32_addr_t addr, uint8_t* data, unsigned length) {
259         int fit_blocks = 0, fit_length = 0;
260
261         for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
262                 /* Block: ------X------Y--------
263                  * Data:            a-----b
264                  *                a--b
265                  *            a-----------b
266                  * Block intersects with data, if:
267                  *  a < Y && b > x
268                  */
269
270                 unsigned X = fb->addr, Y = fb->addr + fb->length;
271                 unsigned a = addr, b = addr + length;
272                 if(a < Y && b > X) {
273                         // from start of the block
274                         unsigned start = (a > X ? a : X) - X;
275                         unsigned end   = (b > Y ? Y : b) - X;
276
277                         memcpy(fb->data + start, data, end - start);
278
279                         fit_blocks++;
280                         fit_length += end - start;
281                 }
282         }
283
284         if(fit_blocks == 0) {
285                 fprintf(stderr, "Unfit data block %08x -> %04x\n", addr, length);
286                 return -1;
287         }
288
289         if(fit_length != length) {
290                 fprintf(stderr, "warning: data block %08x -> %04x truncated to %04x\n",
291                         addr, length, fit_length);
292                 fprintf(stderr, "(this is not an error, just a GDB glitch)\n");
293         }
294
295         return 0;
296 }
297
298 static int flash_go(struct stlink* sl) {
299         int error = -1;
300
301         // Some kinds of clock settings do not allow writing to flash.
302         stlink_reset(sl);
303
304         for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
305                 #ifdef DEBUG
306                 printf("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
307                 #endif
308
309                 unsigned length = fb->length;
310                 for(stm32_addr_t page = fb->addr; page < fb->addr + fb->length; page += 0x400) {
311                         #ifdef DEBUG
312                         printf("flash_do: page %08x\n", page);
313                         #endif
314
315                         stlink_erase_flash_page(sl, page);
316
317                         if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
318                                         length > 0x400 ? 0x400 : length) < 0)
319                                 goto error;
320                 }
321
322         }
323
324         stlink_reset(sl);
325
326         error = 0;
327
328 error:
329         for(struct flash_block* fb = flash_root, *next; fb; fb = next) {
330                 next = fb->next;
331                 free(fb->data);
332                 free(fb);
333         }
334
335         flash_root = NULL;
336
337         return error;
338 }
339
340 int serve(struct stlink* sl, int port) {
341         int sock = socket(AF_INET, SOCK_STREAM, 0);
342         if(sock < 0) {
343                 perror("socket");
344                 return 1;
345         }
346
347         unsigned int val = 1;
348         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
349
350         struct sockaddr_in serv_addr = {0};
351         serv_addr.sin_family = AF_INET;
352         serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
353         serv_addr.sin_port = htons(port);
354
355         if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
356                 perror("bind");
357                 return 1;
358         }
359
360         if(listen(sock, 5) < 0) {
361                 perror("listen");
362                 return 1;
363         }
364
365         stlink_force_debug(sl);
366         stlink_reset(sl);
367         init_code_breakpoints(sl);
368
369         printf("Listening at *:%d...\n", port);
370
371         int client = accept(sock, NULL, NULL);
372         if(client < 0) {
373                 perror("accept");
374                 return 1;
375         }
376
377         close(sock);
378
379         printf("GDB connected.\n");
380
381         /*
382          * To allow resetting the chip from GDB it is required to
383          * emulate attaching and detaching to target.
384          */
385         unsigned int attached = 1;
386
387         while(1) {
388                 char* packet;
389
390                 int status = gdb_recv_packet(client, &packet);
391                 if(status < 0) {
392                         fprintf(stderr, "cannot recv: %d\n", status);
393                         return 1;
394                 }
395
396                 #ifdef DEBUG
397                 printf("recv: %s\n", packet);
398                 #endif
399
400                 char* reply = NULL;
401
402                 switch(packet[0]) {
403                 case 'q': {
404                         if(packet[1] == 'P' || packet[1] == 'C' || packet[1] == 'L') {
405                                 reply = strdup("");
406                                 break;
407                         }
408
409                         char *separator = strstr(packet, ":"), *params = "";
410                         if(separator == NULL) {
411                                 separator = packet + strlen(packet);
412                         } else {
413                                 params = separator + 1;
414                         }
415
416                         unsigned queryNameLength = (separator - &packet[1]);
417                         char* queryName = calloc(queryNameLength + 1, 1);
418                         strncpy(queryName, &packet[1], queryNameLength);
419
420                         #ifdef DEBUG
421                         printf("query: %s;%s\n", queryName, params);
422                         #endif
423
424                         if(!strcmp(queryName, "Supported")) {
425                                 reply = strdup("PacketSize=3fff;qXfer:memory-map:read+");
426                         } else if(!strcmp(queryName, "Xfer")) {
427                                 char *type, *op, *annex, *s_addr, *s_length;
428                                 char *tok = params;
429
430                                 type     = strsep(&tok, ":");
431                                 op       = strsep(&tok, ":");
432                                 annex    = strsep(&tok, ":");
433                                 s_addr   = strsep(&tok, ",");
434                                 s_length = tok;
435
436                                 unsigned addr = strtoul(s_addr, NULL, 16),
437                                        length = strtoul(s_length, NULL, 16);
438
439                                 #ifdef DEBUG
440                                 printf("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
441                                         type, op, annex, addr, length);
442                                 #endif
443
444                                 const char* data = NULL;
445
446                                 if(!strcmp(type, "memory-map") && !strcmp(op, "read"))
447                                         data = current_memory_map;
448
449                                 if(data) {
450                                         unsigned data_length = strlen(data);
451                                         if(addr + length > data_length)
452                                                 length = data_length - addr;
453
454                                         if(length == 0) {
455                                                 reply = strdup("l");
456                                         } else {
457                                                 reply = calloc(length + 2, 1);
458                                                 reply[0] = 'm';
459                                                 strncpy(&reply[1], data, length);
460                                         }
461                                 }
462                         }
463
464                         if(reply == NULL)
465                                 reply = strdup("");
466
467                         free(queryName);
468
469                         break;
470                 }
471
472                 case 'v': {
473                         char *params = NULL;
474                         char *cmdName = strtok_r(packet, ":;", &params);
475
476                         cmdName++; // vCommand -> Command
477
478                         if(!strcmp(cmdName, "FlashErase")) {
479                                 char *s_addr, *s_length;
480                                 char *tok = params;
481
482                                 s_addr   = strsep(&tok, ",");
483                                 s_length = tok;
484
485                                 unsigned addr = strtoul(s_addr, NULL, 16),
486                                        length = strtoul(s_length, NULL, 16);
487
488                                 #ifdef DEBUG
489                                 printf("FlashErase: addr:%08x,len:%04x\n",
490                                         addr, length);
491                                 #endif
492
493                                 if(flash_add_block(addr, length) < 0) {
494                                         reply = strdup("E00");
495                                 } else {
496                                         reply = strdup("OK");
497                                 }
498                         } else if(!strcmp(cmdName, "FlashWrite")) {
499                                 char *s_addr, *data;
500                                 char *tok = params;
501
502                                 s_addr = strsep(&tok, ":");
503                                 data   = tok;
504
505                                 unsigned addr = strtoul(s_addr, NULL, 16);
506                                 unsigned data_length = status - (data - packet);
507
508                                 // Length of decoded data cannot be more than
509                                 // encoded, as escapes are removed.
510                                 // Additional byte is reserved for alignment fix.
511                                 uint8_t *decoded = calloc(data_length + 1, 1);
512                                 unsigned dec_index = 0;
513                                 for(int i = 0; i < data_length; i++) {
514                                         if(data[i] == 0x7d) {
515                                                 i++;
516                                                 decoded[dec_index++] = data[i] ^ 0x20;
517                                         } else {
518                                                 decoded[dec_index++] = data[i];
519                                         }
520                                 }
521
522                                 // Fix alignment
523                                 if(dec_index % 2 != 0)
524                                         dec_index++;
525
526                                 #ifdef DEBUG
527                                 printf("binary packet %d -> %d\n", data_length, dec_index);
528                                 #endif
529
530                                 if(flash_populate(addr, decoded, dec_index) < 0) {
531                                         reply = strdup("E00");
532                                 } else {
533                                         reply = strdup("OK");
534                                 }
535                         } else if(!strcmp(cmdName, "FlashDone")) {
536                                 if(flash_go(sl) < 0) {
537                                         reply = strdup("E00");
538                                 } else {
539                                         reply = strdup("OK");
540                                 }
541                         } else if(!strcmp(cmdName, "Kill")) {
542                                 attached = 0;
543
544                                 reply = strdup("OK");
545                         }
546
547                         if(reply == NULL)
548                                 reply = strdup("");
549
550                         break;
551                 }
552
553                 case 'c':
554                         stlink_run(sl);
555
556                         while(1) {
557                                 int status = gdb_check_for_interrupt(client);
558                                 if(status < 0) {
559                                         fprintf(stderr, "cannot check for int: %d\n", status);
560                                         return 1;
561                                 }
562
563                                 if(status == 1) {
564                                         stlink_force_debug(sl);
565                                         break;
566                                 }
567
568                                 stlink_status(sl);
569                                 if(sl->core_stat == STLINK_CORE_HALTED) {
570                                         break;
571                                 }
572
573                                 usleep(100000);
574                         }
575
576                         reply = strdup("S05"); // TRAP
577                         break;
578
579                 case 's':
580                         stlink_step(sl);
581
582                         reply = strdup("S05"); // TRAP
583                         break;
584
585                 case '?':
586                         if(attached) {
587                                 reply = strdup("S05"); // TRAP
588                         } else {
589                                 /* Stub shall reply OK if not attached. */
590                                 reply = strdup("OK");
591                         }
592                         break;
593
594                 case 'g':
595                         stlink_read_all_regs(sl);
596
597                         reply = calloc(8 * 16 + 1, 1);
598                         for(int i = 0; i < 16; i++)
599                                 sprintf(&reply[i * 8], "%08x", htonl(sl->reg.r[i]));
600
601                         break;
602
603                 case 'p': {
604                         unsigned id = strtoul(&packet[1], NULL, 16), reg = 0xDEADDEAD;
605
606                         if(id < 16) {
607                                 stlink_read_reg(sl, id);
608                                 reg = htonl(sl->reg.r[id]);
609                         } else if(id == 0x19) {
610                                 stlink_read_reg(sl, 16);
611                                 reg = htonl(sl->reg.xpsr);
612                         } else {
613                                 reply = strdup("E00");
614                         }
615
616                         reply = calloc(8 + 1, 1);
617                         sprintf(reply, "%08x", reg);
618
619                         break;
620                 }
621
622                 case 'P': {
623                         char* s_reg = &packet[1];
624                         char* s_value = strstr(&packet[1], "=") + 1;
625
626                         unsigned reg   = strtoul(s_reg,   NULL, 16);
627                         unsigned value = strtoul(s_value, NULL, 16);
628
629                         if(reg < 16) {
630                                 stlink_write_reg(sl, ntohl(value), reg);
631                         } else if(reg == 0x19) {
632                                 stlink_write_reg(sl, ntohl(value), 16);
633                         } else {
634                                 reply = strdup("E00");
635                         }
636
637                         if(!reply) {
638                                 reply = strdup("OK");
639                         }
640
641                         break;
642                 }
643
644                 case 'G':
645                         for(int i = 0; i < 16; i++) {
646                                 char str[9] = {0};
647                                 strncpy(str, &packet[1 + i * 8], 8);
648                                 uint32_t reg = strtoul(str, NULL, 16);
649                                 stlink_write_reg(sl, ntohl(reg), i);
650                         }
651
652                         reply = strdup("OK");
653                         break;
654
655                 case 'm': {
656                         char* s_start = &packet[1];
657                         char* s_count = strstr(&packet[1], ",") + 1;
658
659                         stm32_addr_t start = strtoul(s_start, NULL, 16);
660                         unsigned     count = strtoul(s_count, NULL, 16);
661
662                         unsigned adj_start = start % 4;
663
664                         stlink_read_mem32(sl, start - adj_start, (count % 4 == 0) ?
665                                                 count : count + 4 - (count % 4));
666
667                         reply = calloc(count * 2 + 1, 1);
668                         for(int i = 0; i < count; i++) {
669                                 reply[i * 2 + 0] = hex[sl->q_buf[i + adj_start] >> 4];
670                                 reply[i * 2 + 1] = hex[sl->q_buf[i + adj_start] & 0xf];
671                         }
672
673                         break;
674                 }
675
676                 case 'M': {
677                         char* s_start = &packet[1];
678                         char* s_count = strstr(&packet[1], ",") + 1;
679                         char* hexdata = strstr(packet, ":") + 1;
680
681                         stm32_addr_t start = strtoul(s_start, NULL, 16);
682                         unsigned     count = strtoul(s_count, NULL, 16);
683
684                         for(int i = 0; i < count; i ++) {
685                                 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
686                                 uint8_t byte = strtoul(hex, NULL, 16);
687                                 sl->q_buf[i] = byte;
688                         }
689
690                         if((count % 4) == 0 && (start % 4) == 0) {
691                                 stlink_write_mem32(sl, start, count);
692                         } else {
693                                 stlink_write_mem8(sl, start, count);
694                         }
695
696                         reply = strdup("OK");
697
698                         break;
699                 }
700
701                 case 'Z': {
702                         if(packet[1] == '1') {
703                                 stm32_addr_t addr = strtoul(&packet[3], NULL, 16);
704                                 if(update_code_breakpoint(sl, addr, 1) < 0) {
705                                         reply = strdup("E00");
706                                 } else {
707                                         reply = strdup("OK");
708                                 }
709                         } else {
710                                 reply = strdup("");
711                         }
712
713                         break;
714                 }
715
716                 case 'z': {
717                         if(packet[1] == '1') {
718                                 stm32_addr_t addr = strtoul(&packet[3], NULL, 16);
719                                 update_code_breakpoint(sl, addr, 0);
720
721                                 reply = strdup("OK");
722                         } else {
723                                 reply = strdup("");
724                         }
725
726                         break;
727                 }
728
729                 case '!': {
730                         /*
731                          * Enter extended mode which allows restarting.
732                          * We do support that always.
733                          */
734
735                         reply = strdup("OK");
736
737                         break;
738                 }
739
740                 case 'R': {
741                         /* Reset the core. */
742
743                         stlink_reset(sl);
744                         init_code_breakpoints(sl);
745
746                         attached = 1;
747
748                         reply = strdup("OK");
749
750                         break;
751                 }
752
753                 default:
754                         reply = strdup("");
755                 }
756
757                 if(reply) {
758                         #ifdef DEBUG
759                         printf("send: %s\n", reply);
760                         #endif
761
762                         int result = gdb_send_packet(client, reply);
763                         if(result != 0) {
764                                 fprintf(stderr, "cannot send: %d\n", result);
765                                 return 1;
766                         }
767
768                         free(reply);
769                 }
770
771                 free(packet);
772         }
773
774         return 0;
775 }