387db2d6eeebba12ed5bd08029187af7a91dfed6
[openocd.git] / src / target / target_request.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 √ėyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <helper/log.h>
31 #include <helper/binarybuffer.h>
32
33 #include "target.h"
34 #include "target_request.h"
35 #include "target_type.h"
36 #include "trace.h"
37
38
39 static bool got_message = false;
40
41 bool target_got_message(void)
42 {
43 bool t = got_message;
44 got_message = false;
45 return t;
46 }
47
48 static int charmsg_mode = 0;
49
50 static int target_asciimsg(struct target *target, uint32_t length)
51 {
52 char *msg = malloc(DIV_ROUND_UP(length + 1, 4) * 4);
53 struct debug_msg_receiver *c = target->dbgmsg;
54
55 target->type->target_request_data(target, DIV_ROUND_UP(length, 4), (uint8_t*)msg);
56 msg[length] = 0;
57
58 LOG_DEBUG("%s", msg);
59
60 while (c)
61 {
62 command_print(c->cmd_ctx, "%s", msg);
63 c = c->next;
64 }
65
66 return ERROR_OK;
67 }
68
69 static int target_charmsg(struct target *target, uint8_t msg)
70 {
71 LOG_USER_N("%c", msg);
72
73 return ERROR_OK;
74 }
75
76 static int target_hexmsg(struct target *target, int size, uint32_t length)
77 {
78 uint8_t *data = malloc(DIV_ROUND_UP(length * size, 4) * 4);
79 char line[128];
80 int line_len;
81 struct debug_msg_receiver *c = target->dbgmsg;
82 uint32_t i;
83
84 LOG_DEBUG("size: %i, length: %i", (int)size, (int)length);
85
86 target->type->target_request_data(target, DIV_ROUND_UP(length * size, 4), (uint8_t*)data);
87
88 line_len = 0;
89 for (i = 0; i < length; i++)
90 {
91 switch (size)
92 {
93 case 4:
94 line_len += snprintf(line + line_len, 128 - line_len, "%8.8" PRIx32 " ", le_to_h_u32(data + (4*i)));
95 break;
96 case 2:
97 line_len += snprintf(line + line_len, 128 - line_len, "%4.4x ", le_to_h_u16(data + (2*i)));
98 break;
99 case 1:
100 line_len += snprintf(line + line_len, 128 - line_len, "%2.2x ", data[i]);
101 break;
102 }
103
104 if ((i%8 == 7) || (i == length - 1))
105 {
106 LOG_DEBUG("%s", line);
107
108 while (c)
109 {
110 command_print(c->cmd_ctx, "%s", line);
111 c = c->next;
112 }
113 c = target->dbgmsg;
114 line_len = 0;
115 }
116 }
117
118 free(data);
119
120 return ERROR_OK;
121 }
122
123 /* handle requests from the target received by a target specific
124 * side-band channel (e.g. ARM7/9 DCC)
125 */
126 int target_request(struct target *target, uint32_t request)
127 {
128 target_req_cmd_t target_req_cmd = request & 0xff;
129
130 /* Record that we got a target message for back-off algorithm */
131 got_message = true;
132
133 if (charmsg_mode) {
134 target_charmsg(target, target_req_cmd);
135 return ERROR_OK;
136 }
137 switch (target_req_cmd)
138 {
139 case TARGET_REQ_TRACEMSG:
140 trace_point(target, (request & 0xffffff00) >> 8);
141 break;
142 case TARGET_REQ_DEBUGMSG:
143 if (((request & 0xff00) >> 8) == 0)
144 {
145 target_asciimsg(target, (request & 0xffff0000) >> 16);
146 }
147 else
148 {
149 target_hexmsg(target, (request & 0xff00) >> 8, (request & 0xffff0000) >> 16);
150 }
151 break;
152 case TARGET_REQ_DEBUGCHAR:
153 target_charmsg(target, (request & 0x00ff0000) >> 16);
154 break;
155 /* case TARGET_REQ_SEMIHOSTING:
156 * break;
157 */
158 default:
159 LOG_ERROR("unknown target request: %2.2x", target_req_cmd);
160 break;
161 }
162
163 return ERROR_OK;
164 }
165
166 static int add_debug_msg_receiver(struct command_context *cmd_ctx, struct target *target)
167 {
168 struct debug_msg_receiver **p = &target->dbgmsg;
169
170 if (target == NULL)
171 return ERROR_COMMAND_SYNTAX_ERROR;
172
173 /* see if there's already a list */
174 if (*p)
175 {
176 /* find end of linked list */
177 p = &target->dbgmsg;
178 while ((*p)->next)
179 p = &((*p)->next);
180 p = &((*p)->next);
181 }
182
183 /* add new debug message receiver */
184 (*p) = malloc(sizeof(struct debug_msg_receiver));
185 (*p)->cmd_ctx = cmd_ctx;
186 (*p)->next = NULL;
187
188 /* enable callback */
189 target->dbg_msg_enabled = 1;
190
191 return ERROR_OK;
192 }
193
194 static struct debug_msg_receiver* find_debug_msg_receiver(struct command_context *cmd_ctx, struct target *target)
195 {
196 int do_all_targets = 0;
197
198 /* if no target has been specified search all of them */
199 if (target == NULL) {
200 /* if no targets haven been specified */
201 if (all_targets == NULL)
202 return NULL;
203
204 target = all_targets;
205 do_all_targets = 1;
206 }
207
208 /* so we target != null */
209 struct debug_msg_receiver **p = &target->dbgmsg;
210 do {
211 while (*p)
212 {
213 if ((*p)->cmd_ctx == cmd_ctx)
214 {
215 return *p;
216 }
217 p = &((*p)->next);
218 }
219
220 target = target->next;
221 } while (target && do_all_targets);
222
223 return NULL;
224 }
225
226 int delete_debug_msg_receiver(struct command_context *cmd_ctx, struct target *target)
227 {
228 struct debug_msg_receiver **p;
229 struct debug_msg_receiver *c;
230 int do_all_targets = 0;
231
232 /* if no target has been specified search all of them */
233 if (target == NULL)
234 {
235 /* if no targets haven been specified */
236 if (all_targets == NULL)
237 return ERROR_OK;
238
239 target = all_targets;
240 do_all_targets = 1;
241 }
242
243 do
244 {
245 p = &target->dbgmsg;
246 c = *p;
247 while (c)
248 {
249 struct debug_msg_receiver *next = c->next;
250 if (c->cmd_ctx == cmd_ctx)
251 {
252 *p = next;
253 free(c);
254 if (*p == NULL)
255 {
256 /* disable callback */
257 target->dbg_msg_enabled = 0;
258 }
259 return ERROR_OK;
260 }
261 else
262 p = &(c->next);
263 c = next;
264 }
265
266 target = target->next;
267 } while (target && do_all_targets);
268
269 return ERROR_OK;
270 }
271
272 COMMAND_HANDLER(handle_target_request_debugmsgs_command)
273 {
274 struct target *target = get_current_target(CMD_CTX);
275
276 int receiving = 0;
277
278 /* see if reciever is already registered */
279 if (find_debug_msg_receiver(CMD_CTX, target) != NULL)
280 receiving = 1;
281
282 if (CMD_ARGC > 0)
283 {
284 if (!strcmp(CMD_ARGV[0], "enable") || !strcmp(CMD_ARGV[0], "charmsg"))
285 {
286 /* don't register if this command context is already receiving */
287 if (!receiving)
288 {
289 receiving = 1;
290 add_debug_msg_receiver(CMD_CTX, target);
291 }
292 charmsg_mode = !strcmp(CMD_ARGV[0], "charmsg");
293 }
294 else if (!strcmp(CMD_ARGV[0], "disable"))
295 {
296 /* no need to delete a receiver if none is registered */
297 if (receiving)
298 {
299 receiving = 0;
300 delete_debug_msg_receiver(CMD_CTX, target);
301 }
302 }
303 else
304 {
305 command_print(CMD_CTX, "usage: target_request debugmsgs ['enable'|'disable'|'charmsg']");
306 }
307 }
308
309 command_print(CMD_CTX, "receiving debug messages from current target %s",
310 (receiving) ? (charmsg_mode?"charmsg":"enabled") : "disabled");
311 return ERROR_OK;
312 }
313
314 static const struct command_registration target_req_exec_command_handlers[] = {
315 {
316 .name = "debugmsgs",
317 .handler = handle_target_request_debugmsgs_command,
318 .mode = COMMAND_EXEC,
319 .help = "display and/or modify reception of debug messages from target",
320 .usage = "['enable'|'charmsg'|'disable']",
321 },
322 COMMAND_REGISTRATION_DONE
323 };
324 static const struct command_registration target_req_command_handlers[] = {
325 {
326 .name = "target_request",
327 .mode = COMMAND_ANY,
328 .help = "target request command group",
329 .chain = target_req_exec_command_handlers,
330 },
331 COMMAND_REGISTRATION_DONE
332 };
333
334 int target_request_register_commands(struct command_context *cmd_ctx)
335 {
336 return register_commands(cmd_ctx, NULL, target_req_command_handlers);
337 }