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.
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"
20 static const char hex[] = "0123456789abcdef";
22 static const char* current_memory_map = NULL;
27 uint32_t max_flash_size, flash_pagesize;
29 uint32_t bootrom_base, bootrom_size;
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 },
48 int serve(struct stlink* sl, int port);
49 static char* make_memory_map(const struct chip_params *params, uint32_t flash_size);
51 int main(int argc, char** argv) {
53 fprintf(stderr, "Usage: %s <port> /dev/sgX\n", argv[0]);
57 struct stlink *sl = stlink_quirk_open(argv[2], 0);
61 if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
62 stlink_enter_swd_mode(sl);
66 stlink_read_mem32(sl, 0xE0042000, 4);
67 chip_id = sl->q_buf[0] | (sl->q_buf[1] << 8) | (sl->q_buf[2] << 16) |
70 printf("Chip ID is %08x.\n", chip_id);
72 const struct chip_params* params = NULL;
74 for(int i = 0; i < sizeof(devices) / sizeof(devices[0]); i++) {
75 if(devices[i].chip_id == (chip_id & 0xFFF)) {
82 fprintf(stderr, "Cannot recognize the connected device!\n");
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);
92 stlink_read_mem32(sl, 0x1FFFF7E0, 4);
93 flash_size = sl->q_buf[0] | (sl->q_buf[1] << 8);
95 printf("Flash size is %d KiB.\n", flash_size);
97 current_memory_map = make_memory_map(params, flash_size * 0x400);
99 int port = atoi(argv[1]);
101 while(serve(sl, port) == 0);
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\">"
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>"
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
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);
130 snprintf(map, 4096, memory_map_template,
133 flash_size, params->flash_pagesize,
134 params->bootrom_base, params->bootrom_size);
139 #define CODE_BREAK_NUM 6
141 #define CODE_BREAK_LOW 0x01
142 #define CODE_BREAK_HIGH 0x02
144 struct code_hw_breakpoint {
149 struct code_hw_breakpoint code_breaks[CODE_BREAK_NUM];
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);
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);
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;
168 fprintf(stderr, "update_code_breakpoint: unaligned address %08x\n", addr);
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)) {
182 if(set) return -1; // Free slot not found
183 else return 0; // Breakpoint is already removed
186 struct code_hw_breakpoint* brk = &code_breaks[id];
188 brk->addr = fpb_addr;
190 if(set) brk->type |= type;
191 else brk->type &= ~type;
193 memset(sl->q_buf, 0, 4);
197 printf("clearing hw break %d\n", id);
200 stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
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);
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]);
214 stlink_write_mem32(sl, 0xe0002008 + id * 4, 4);
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)
230 struct flash_block* next;
233 static struct flash_block* flash_root;
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");
241 if(addr % FLASH_PAGE != 0 || length % FLASH_PAGE != 0) {
242 fprintf(stderr, "flash_add_block: unaligned block\n");
246 struct flash_block* new = malloc(sizeof(struct flash_block));
247 new->next = flash_root;
250 new->length = length;
251 new->data = calloc(length, 1);
258 static int flash_populate(stm32_addr_t addr, uint8_t* data, unsigned length) {
259 int fit_blocks = 0, fit_length = 0;
261 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
262 /* Block: ------X------Y--------
266 * Block intersects with data, if:
270 unsigned X = fb->addr, Y = fb->addr + fb->length;
271 unsigned a = addr, b = addr + length;
273 // from start of the block
274 unsigned start = (a > X ? a : X) - X;
275 unsigned end = (b > Y ? Y : b) - X;
277 memcpy(fb->data + start, data, end - start);
280 fit_length += end - start;
284 if(fit_blocks == 0) {
285 fprintf(stderr, "Unfit data block %08x -> %04x\n", addr, length);
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");
298 static int flash_go(struct stlink* sl) {
301 // Some kinds of clock settings do not allow writing to flash.
304 for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
306 printf("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
309 unsigned length = fb->length;
310 for(stm32_addr_t page = fb->addr; page < fb->addr + fb->length; page += 0x400) {
312 printf("flash_do: page %08x\n", page);
315 stlink_erase_flash_page(sl, page);
317 if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
318 length > 0x400 ? 0x400 : length) < 0)
329 for(struct flash_block* fb = flash_root, *next; fb; fb = next) {
340 int serve(struct stlink* sl, int port) {
341 int sock = socket(AF_INET, SOCK_STREAM, 0);
347 unsigned int val = 1;
348 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
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);
355 if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
360 if(listen(sock, 5) < 0) {
365 stlink_force_debug(sl);
367 init_code_breakpoints(sl);
369 printf("Listening at *:%d...\n", port);
371 int client = accept(sock, NULL, NULL);
379 printf("GDB connected.\n");
382 * To allow resetting the chip from GDB it is required to
383 * emulate attaching and detaching to target.
385 unsigned int attached = 1;
390 int status = gdb_recv_packet(client, &packet);
392 fprintf(stderr, "cannot recv: %d\n", status);
397 printf("recv: %s\n", packet);
404 if(packet[1] == 'P' || packet[1] == 'C' || packet[1] == 'L') {
409 char *separator = strstr(packet, ":"), *params = "";
410 if(separator == NULL) {
411 separator = packet + strlen(packet);
413 params = separator + 1;
416 unsigned queryNameLength = (separator - &packet[1]);
417 char* queryName = calloc(queryNameLength + 1, 1);
418 strncpy(queryName, &packet[1], queryNameLength);
421 printf("query: %s;%s\n", queryName, params);
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;
430 type = strsep(&tok, ":");
431 op = strsep(&tok, ":");
432 annex = strsep(&tok, ":");
433 s_addr = strsep(&tok, ",");
436 unsigned addr = strtoul(s_addr, NULL, 16),
437 length = strtoul(s_length, NULL, 16);
440 printf("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
441 type, op, annex, addr, length);
444 const char* data = NULL;
446 if(!strcmp(type, "memory-map") && !strcmp(op, "read"))
447 data = current_memory_map;
450 unsigned data_length = strlen(data);
451 if(addr + length > data_length)
452 length = data_length - addr;
457 reply = calloc(length + 2, 1);
459 strncpy(&reply[1], data, length);
474 char *cmdName = strtok_r(packet, ":;", ¶ms);
476 cmdName++; // vCommand -> Command
478 if(!strcmp(cmdName, "FlashErase")) {
479 char *s_addr, *s_length;
482 s_addr = strsep(&tok, ",");
485 unsigned addr = strtoul(s_addr, NULL, 16),
486 length = strtoul(s_length, NULL, 16);
489 printf("FlashErase: addr:%08x,len:%04x\n",
493 if(flash_add_block(addr, length) < 0) {
494 reply = strdup("E00");
496 reply = strdup("OK");
498 } else if(!strcmp(cmdName, "FlashWrite")) {
502 s_addr = strsep(&tok, ":");
505 unsigned addr = strtoul(s_addr, NULL, 16);
506 unsigned data_length = status - (data - packet);
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) {
516 decoded[dec_index++] = data[i] ^ 0x20;
518 decoded[dec_index++] = data[i];
523 if(dec_index % 2 != 0)
527 printf("binary packet %d -> %d\n", data_length, dec_index);
530 if(flash_populate(addr, decoded, dec_index) < 0) {
531 reply = strdup("E00");
533 reply = strdup("OK");
535 } else if(!strcmp(cmdName, "FlashDone")) {
536 if(flash_go(sl) < 0) {
537 reply = strdup("E00");
539 reply = strdup("OK");
541 } else if(!strcmp(cmdName, "Kill")) {
544 reply = strdup("OK");
557 int status = gdb_check_for_interrupt(client);
559 fprintf(stderr, "cannot check for int: %d\n", status);
564 stlink_force_debug(sl);
569 if(sl->core_stat == STLINK_CORE_HALTED) {
576 reply = strdup("S05"); // TRAP
582 reply = strdup("S05"); // TRAP
587 reply = strdup("S05"); // TRAP
589 /* Stub shall reply OK if not attached. */
590 reply = strdup("OK");
595 stlink_read_all_regs(sl);
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]));
604 unsigned id = strtoul(&packet[1], NULL, 16), reg = 0xDEADDEAD;
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);
613 reply = strdup("E00");
616 reply = calloc(8 + 1, 1);
617 sprintf(reply, "%08x", reg);
623 char* s_reg = &packet[1];
624 char* s_value = strstr(&packet[1], "=") + 1;
626 unsigned reg = strtoul(s_reg, NULL, 16);
627 unsigned value = strtoul(s_value, NULL, 16);
630 stlink_write_reg(sl, ntohl(value), reg);
631 } else if(reg == 0x19) {
632 stlink_write_reg(sl, ntohl(value), 16);
634 reply = strdup("E00");
638 reply = strdup("OK");
645 for(int i = 0; i < 16; i++) {
647 strncpy(str, &packet[1 + i * 8], 8);
648 uint32_t reg = strtoul(str, NULL, 16);
649 stlink_write_reg(sl, ntohl(reg), i);
652 reply = strdup("OK");
656 char* s_start = &packet[1];
657 char* s_count = strstr(&packet[1], ",") + 1;
659 stm32_addr_t start = strtoul(s_start, NULL, 16);
660 unsigned count = strtoul(s_count, NULL, 16);
662 unsigned adj_start = start % 4;
664 stlink_read_mem32(sl, start - adj_start, (count % 4 == 0) ?
665 count : count + 4 - (count % 4));
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];
677 char* s_start = &packet[1];
678 char* s_count = strstr(&packet[1], ",") + 1;
679 char* hexdata = strstr(packet, ":") + 1;
681 stm32_addr_t start = strtoul(s_start, NULL, 16);
682 unsigned count = strtoul(s_count, NULL, 16);
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);
690 if((count % 4) == 0 && (start % 4) == 0) {
691 stlink_write_mem32(sl, start, count);
693 stlink_write_mem8(sl, start, count);
696 reply = strdup("OK");
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");
707 reply = strdup("OK");
717 if(packet[1] == '1') {
718 stm32_addr_t addr = strtoul(&packet[3], NULL, 16);
719 update_code_breakpoint(sl, addr, 0);
721 reply = strdup("OK");
731 * Enter extended mode which allows restarting.
732 * We do support that always.
735 reply = strdup("OK");
741 /* Reset the core. */
744 init_code_breakpoints(sl);
748 reply = strdup("OK");
759 printf("send: %s\n", reply);
762 int result = gdb_send_packet(client, reply);
764 fprintf(stderr, "cannot send: %d\n", result);