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 "gdb-remote.h"
17 #include "stlink-hw.h"
19 static const char hex[] = "0123456789abcdef";
21 int serve(struct stlink* sl, int port);
23 int main(int argc, char** argv) {
25 fprintf(stderr, "Usage: %s <port> /dev/sgX\n", argv[0]);
29 struct stlink *sl = stlink_quirk_open(argv[2], 0);
33 if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
34 stlink_enter_swd_mode(sl);
37 printf("Debugging ARM core %08x.\n", sl->core_id);
39 int port = atoi(argv[1]);
41 while(serve(sl, port) == 0);
48 int serve(struct stlink* sl, int port) {
49 int sock = socket(AF_INET, SOCK_STREAM, 0);
55 struct sockaddr_in serv_addr = {0};
56 serv_addr.sin_family = AF_INET;
57 serv_addr.sin_addr.s_addr = INADDR_ANY;
58 serv_addr.sin_port = htons(port);
60 if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
65 if(listen(sock, 5) < 0) {
70 stlink_force_debug(sl);
73 printf("Listening at *:%d...\n", port);
75 int client = accept(sock, NULL, NULL);
83 printf("GDB connected.\n");
88 int status = gdb_recv_packet(client, &packet);
90 fprintf(stderr, "cannot recv: %d\n", status);
94 //printf("recv: %s\n", packet);
102 printf("Core running, waiting for interrupt.\n");
104 int status = gdb_wait_for_interrupt(client);
106 fprintf(stderr, "cannot wait for int: %d\n", status);
110 stlink_force_debug(sl);
112 reply = strdup("S05"); // TRAP
118 reply = strdup("S05"); // TRAP
122 reply = strdup("S05"); // TRAP
126 stlink_read_all_regs(sl);
128 reply = calloc(8 * 16 + 1, 1);
129 for(int i = 0; i < 16; i++)
130 sprintf(&reply[i * 8], "%08x", htonl(sl->reg.r[i]));
135 unsigned id = strtoul(&packet[1], NULL, 16), reg = 0xDEADDEAD;
138 stlink_read_reg(sl, id);
139 reg = htonl(sl->reg.r[id]);
140 } else if(id == 0x19) {
141 stlink_read_reg(sl, 16);
142 reg = htonl(sl->reg.xpsr);
144 reply = strdup("E00");
147 reply = calloc(8 + 1, 1);
148 sprintf(reply, "%08x", reg);
154 char* s_reg = &packet[1];
155 char* s_value = strstr(&packet[1], "=") + 1;
157 unsigned reg = strtoul(s_reg, NULL, 16);
158 unsigned value = strtoul(s_value, NULL, 16);
161 stlink_write_reg(sl, ntohl(value), reg);
162 } else if(reg == 0x19) {
163 stlink_write_reg(sl, ntohl(value), 16);
165 reply = strdup("E00");
169 reply = strdup("OK");
176 for(int i = 0; i < 16; i++) {
178 strncpy(str, &packet[1 + i * 8], 8);
179 uint32_t reg = strtoul(str, NULL, 16);
180 stlink_write_reg(sl, ntohl(reg), i);
183 reply = strdup("OK");
187 char* s_start = &packet[1];
188 char* s_count = strstr(&packet[1], ",") + 1;
190 stm32_addr_t start = strtoul(s_start, NULL, 16);
191 unsigned count = strtoul(s_count, NULL, 16);
193 unsigned adj_start = start % 4;
195 stlink_read_mem32(sl, start - adj_start, (count % 4 == 0) ?
196 count : count + 4 - (count % 4));
198 reply = calloc(count * 2 + 1, 1);
199 for(int i = 0; i < count; i++) {
200 reply[i * 2 + 0] = hex[sl->q_buf[i + adj_start] >> 4];
201 reply[i * 2 + 1] = hex[sl->q_buf[i + adj_start] & 0xf];
208 char* s_start = &packet[1];
209 char* s_count = strstr(&packet[1], ",") + 1;
210 char* hexdata = strstr(packet, ":") + 1;
212 stm32_addr_t start = strtoul(s_start, NULL, 16);
213 unsigned count = strtoul(s_count, NULL, 16);
215 for(int i = 0; i < count; i ++) {
216 char hex[3] = { hexdata[i*2], hexdata[i*2+1], 0 };
217 uint8_t byte = strtoul(hex, NULL, 16);
221 if((count % 4) == 0 && (start % 4) == 0) {
222 stlink_write_mem32(sl, start, count);
224 stlink_write_mem8(sl, start, count);
227 reply = strdup("OK");
233 // After this function will be entered afterwards, the
234 // chip will be reset anyway. So this is a no-op.
245 //printf("send: %s\n", reply);
247 int result = gdb_send_packet(client, reply);
249 fprintf(stderr, "cannot send: %d\n", result);