1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "esp_gdbstub_common.h"
16
17 // GDB command input buffer
18 static unsigned char s_cmd[GDBSTUB_CMD_BUFLEN];
19
20 // Running checksum of the output packet
21 static char s_chsum;
22
23 // Send the start of a packet; reset checksum calculation.
esp_gdbstub_send_start(void)24 void esp_gdbstub_send_start(void)
25 {
26 s_chsum = 0;
27 esp_gdbstub_putchar('$');
28 }
29
30 // Send a char as part of a packet
esp_gdbstub_send_char(char c)31 void esp_gdbstub_send_char(char c)
32 {
33 if (c == '#' || c == '$' || c == '}' || c == '*') {
34 esp_gdbstub_putchar('}');
35 esp_gdbstub_putchar(c ^ 0x20);
36 s_chsum += (c ^ 0x20) + '}';
37 } else {
38 esp_gdbstub_putchar(c);
39 s_chsum += c;
40 }
41 }
42
43 // Send a string as part of a packet
esp_gdbstub_send_str(const char * c)44 void esp_gdbstub_send_str(const char *c)
45 {
46 while (*c != 0) {
47 esp_gdbstub_send_char(*c);
48 c++;
49 }
50 }
51
52 // Send a hex val as part of a packet.
53 // 'bits'/4 dictates the number of hex chars sent.
esp_gdbstub_send_hex(int val,int bits)54 void esp_gdbstub_send_hex(int val, int bits)
55 {
56 const char *hex_chars = "0123456789abcdef";
57 for (int i = bits; i > 0; i -= 4) {
58 esp_gdbstub_send_char(hex_chars[(val >> (i - 4)) & 0xf]);
59 }
60 }
61
62 // Finish sending a packet.
esp_gdbstub_send_end(void)63 void esp_gdbstub_send_end(void)
64 {
65 esp_gdbstub_putchar('#');
66 esp_gdbstub_send_hex(s_chsum, 8);
67 esp_gdbstub_flush();
68 }
69
70 // Send a packet with a string as content
esp_gdbstub_send_str_packet(const char * str)71 void esp_gdbstub_send_str_packet(const char *str)
72 {
73 esp_gdbstub_send_start();
74 if (str != NULL) {
75 esp_gdbstub_send_str(str);
76 }
77 esp_gdbstub_send_end();
78 }
79
80 // Grab a hex value from the gdb packet. Ptr will get positioned on the end
81 // of the hex string, as far as the routine has read into it. Bits/4 indicates
82 // the max amount of hex chars it gobbles up. Bits can be -1 to eat up as much
83 // hex chars as possible.
esp_gdbstub_gethex(const unsigned char ** ptr,int bits)84 uint32_t esp_gdbstub_gethex(const unsigned char **ptr, int bits)
85 {
86 int i;
87 int no;
88 uint32_t v = 0;
89 char c;
90 no = bits / 4;
91 if (bits == -1) {
92 no = 64;
93 }
94 for (i = 0; i < no; i++) {
95 c = **ptr;
96 (*ptr)++;
97 if (c >= '0' && c <= '9') {
98 v <<= 4;
99 v |= (c - '0');
100 } else if (c >= 'A' && c <= 'F') {
101 v <<= 4;
102 v |= (c - 'A') + 10;
103 } else if (c >= 'a' && c <= 'f') {
104 v <<= 4;
105 v |= (c - 'a') + 10;
106 } else if (c == '#') {
107 if (bits == -1) {
108 (*ptr)--;
109 return v;
110 }
111 return GDBSTUB_ST_ENDPACKET;
112 } else {
113 if (bits == -1) {
114 (*ptr)--;
115 return v;
116 }
117 return GDBSTUB_ST_ERR;
118 }
119 }
120 return v;
121 }
122
123
124 // Lower layer: grab a command packet and check the checksum
125 // Calls gdbHandleCommand on the packet if the checksum is OK
126 // Returns GDBSTUB_ST_OK on success, GDBSTUB_ST_ERR when checksum fails, a
127 // character if it is received instead of the GDB packet
128 // start char.
esp_gdbstub_read_command(unsigned char ** out_cmd,size_t * out_size)129 int esp_gdbstub_read_command(unsigned char **out_cmd, size_t *out_size)
130 {
131 unsigned char c;
132 unsigned char chsum = 0;
133 unsigned char sentchs[2];
134 int p = 0;
135 c = esp_gdbstub_getchar();
136 if (c != '$') {
137 return c;
138 }
139 while (1) {
140 c = esp_gdbstub_getchar();
141 if (c == '#') {
142 // end of packet, checksum follows
143 s_cmd[p] = 0;
144 break;
145 }
146 chsum += c;
147 if (c == '$') {
148 // restart packet?
149 chsum = 0;
150 p = 0;
151 continue;
152 }
153 if (c == '}') {
154 //escape the next char
155 c = esp_gdbstub_getchar();
156 chsum += c;
157 c ^= 0x20;
158 }
159 s_cmd[p++] = c;
160 if (p >= GDBSTUB_CMD_BUFLEN) {
161 return GDBSTUB_ST_ERR;
162 }
163 }
164 // A # has been received. Get and check the received chsum.
165 sentchs[0] = esp_gdbstub_getchar();
166 sentchs[1] = esp_gdbstub_getchar();
167 const unsigned char *c_ptr = &sentchs[0];
168 unsigned char rchsum = esp_gdbstub_gethex(&c_ptr, 8);
169 if (rchsum != chsum) {
170 esp_gdbstub_putchar('-');
171 return GDBSTUB_ST_ERR;
172 } else {
173 esp_gdbstub_putchar('+');
174 *out_cmd = s_cmd;
175 *out_size = p;
176 return GDBSTUB_ST_OK;
177 }
178 }
179