]> git.sur5r.net Git - openocd/blob - src/target/target_request.c
df2fc34ee1d66b90b3e7f3c5a856244d2068b9da
[openocd] / 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  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
25  ***************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <helper/log.h>
32 #include <helper/binarybuffer.h>
33
34 #include "target.h"
35 #include "target_request.h"
36 #include "target_type.h"
37 #include "trace.h"
38
39 static bool got_message;
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;
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                 command_print(c->cmd_ctx, "%s", msg);
62                 c = c->next;
63         }
64
65         return ERROR_OK;
66 }
67
68 static int target_charmsg(struct target *target, uint8_t msg)
69 {
70         LOG_USER_N("%c", msg);
71
72         return ERROR_OK;
73 }
74
75 static int target_hexmsg(struct target *target, int size, uint32_t length)
76 {
77         uint8_t *data = malloc(DIV_ROUND_UP(length * size, 4) * 4);
78         char line[128];
79         int line_len;
80         struct debug_msg_receiver *c = target->dbgmsg;
81         uint32_t i;
82
83         LOG_DEBUG("size: %i, length: %i", (int)size, (int)length);
84
85         target->type->target_request_data(target, DIV_ROUND_UP(length * size, 4), (uint8_t *)data);
86
87         line_len = 0;
88         for (i = 0; i < length; i++) {
89                 switch (size) {
90                         case 4:
91                                 line_len += snprintf(line + line_len, 128 - line_len, "%8.8" PRIx32 " ", le_to_h_u32(data + (4*i)));
92                                 break;
93                         case 2:
94                                 line_len += snprintf(line + line_len, 128 - line_len, "%4.4x ", le_to_h_u16(data + (2*i)));
95                                 break;
96                         case 1:
97                                 line_len += snprintf(line + line_len, 128 - line_len, "%2.2x ", data[i]);
98                                 break;
99                 }
100
101                 if ((i%8 == 7) || (i == length - 1)) {
102                         LOG_DEBUG("%s", line);
103
104                         while (c) {
105                                 command_print(c->cmd_ctx, "%s", line);
106                                 c = c->next;
107                         }
108                         c = target->dbgmsg;
109                         line_len = 0;
110                 }
111         }
112
113         free(data);
114
115         return ERROR_OK;
116 }
117
118 /* handle requests from the target received by a target specific
119  * side-band channel (e.g. ARM7/9 DCC)
120  */
121 int target_request(struct target *target, uint32_t request)
122 {
123         target_req_cmd_t target_req_cmd = request & 0xff;
124
125         assert(target->type->target_request_data);
126
127         /* Record that we got a target message for back-off algorithm */
128         got_message = true;
129
130         if (charmsg_mode) {
131                 target_charmsg(target, target_req_cmd);
132                 return ERROR_OK;
133         }
134
135         switch (target_req_cmd) {
136                 case TARGET_REQ_TRACEMSG:
137                         trace_point(target, (request & 0xffffff00) >> 8);
138                         break;
139                 case TARGET_REQ_DEBUGMSG:
140                         if (((request & 0xff00) >> 8) == 0)
141                                 target_asciimsg(target, (request & 0xffff0000) >> 16);
142                         else
143                                 target_hexmsg(target, (request & 0xff00) >> 8, (request & 0xffff0000) >> 16);
144                         break;
145                 case TARGET_REQ_DEBUGCHAR:
146                         target_charmsg(target, (request & 0x00ff0000) >> 16);
147                         break;
148 /*              case TARGET_REQ_SEMIHOSTING:
149  *                      break;
150  */
151                 default:
152                         LOG_ERROR("unknown target request: %2.2x", target_req_cmd);
153                         break;
154         }
155
156         return ERROR_OK;
157 }
158
159 static int add_debug_msg_receiver(struct command_context *cmd_ctx, struct target *target)
160 {
161         struct debug_msg_receiver **p = &target->dbgmsg;
162
163         if (target == NULL)
164                 return ERROR_COMMAND_SYNTAX_ERROR;
165
166         /* see if there's already a list */
167         if (*p) {
168                 /* find end of linked list */
169                 while ((*p)->next)
170                         p = &((*p)->next);
171                 p = &((*p)->next);
172         }
173
174         /* add new debug message receiver */
175         (*p) = malloc(sizeof(struct debug_msg_receiver));
176         (*p)->cmd_ctx = cmd_ctx;
177         (*p)->next = NULL;
178
179         /* enable callback */
180         target->dbg_msg_enabled = 1;
181
182         return ERROR_OK;
183 }
184
185 static struct debug_msg_receiver *find_debug_msg_receiver(struct command_context *cmd_ctx,
186                 struct target *target)
187 {
188         int do_all_targets = 0;
189
190         /* if no target has been specified search all of them */
191         if (target == NULL) {
192                 /* if no targets haven been specified */
193                 if (all_targets == NULL)
194                         return NULL;
195
196                 target = all_targets;
197                 do_all_targets = 1;
198         }
199
200         /* so we target != null */
201         struct debug_msg_receiver **p = &target->dbgmsg;
202         do {
203                 while (*p) {
204                         if ((*p)->cmd_ctx == cmd_ctx)
205                                 return *p;
206                         p = &((*p)->next);
207                 }
208
209                 target = target->next;
210         } while (target && do_all_targets);
211
212         return NULL;
213 }
214
215 int delete_debug_msg_receiver(struct command_context *cmd_ctx, struct target *target)
216 {
217         struct debug_msg_receiver **p;
218         struct debug_msg_receiver *c;
219         int do_all_targets = 0;
220
221         /* if no target has been specified search all of them */
222         if (target == NULL) {
223                 /* if no targets haven been specified */
224                 if (all_targets == NULL)
225                         return ERROR_OK;
226
227                 target = all_targets;
228                 do_all_targets = 1;
229         }
230
231         do {
232                 p = &target->dbgmsg;
233                 c = *p;
234                 while (c) {
235                         struct debug_msg_receiver *next = c->next;
236                         if (c->cmd_ctx == cmd_ctx) {
237                                 *p = next;
238                                 free(c);
239                                 if (*p == NULL) {
240                                         /* disable callback */
241                                         target->dbg_msg_enabled = 0;
242                                 }
243                                 return ERROR_OK;
244                         } else
245                                 p = &(c->next);
246                         c = next;
247                 }
248
249                 target = target->next;
250         } while (target && do_all_targets);
251
252         return ERROR_OK;
253 }
254
255 COMMAND_HANDLER(handle_target_request_debugmsgs_command)
256 {
257         struct target *target = get_current_target(CMD_CTX);
258
259         int receiving = 0;
260
261         if (target->type->target_request_data == NULL) {
262                 LOG_ERROR("Target %s does not support target requests", target_name(target));
263                 return ERROR_OK;
264         }
265
266         /* see if reciever is already registered */
267         if (find_debug_msg_receiver(CMD_CTX, target) != NULL)
268                 receiving = 1;
269
270         if (CMD_ARGC > 0) {
271                 if (!strcmp(CMD_ARGV[0], "enable") || !strcmp(CMD_ARGV[0], "charmsg")) {
272                         /* don't register if this command context is already receiving */
273                         if (!receiving) {
274                                 receiving = 1;
275                                 add_debug_msg_receiver(CMD_CTX, target);
276                         }
277                         charmsg_mode = !strcmp(CMD_ARGV[0], "charmsg");
278                 } else if (!strcmp(CMD_ARGV[0], "disable")) {
279                         /* no need to delete a receiver if none is registered */
280                         if (receiving) {
281                                 receiving = 0;
282                                 delete_debug_msg_receiver(CMD_CTX, target);
283                         }
284                 } else
285                         return ERROR_COMMAND_SYNTAX_ERROR;
286         }
287
288         command_print(CMD_CTX, "receiving debug messages from current target %s",
289                         (receiving) ? (charmsg_mode ? "charmsg" : "enabled") : "disabled");
290         return ERROR_OK;
291 }
292
293 static const struct command_registration target_req_exec_command_handlers[] = {
294         {
295                 .name = "debugmsgs",
296                 .handler = handle_target_request_debugmsgs_command,
297                 .mode = COMMAND_EXEC,
298                 .help = "display and/or modify reception of debug messages from target",
299                 .usage = "['enable'|'charmsg'|'disable']",
300         },
301         COMMAND_REGISTRATION_DONE
302 };
303 static const struct command_registration target_req_command_handlers[] = {
304         {
305                 .name = "target_request",
306                 .mode = COMMAND_ANY,
307                 .help = "target request command group",
308                 .usage = "",
309                 .chain = target_req_exec_command_handlers,
310         },
311         COMMAND_REGISTRATION_DONE
312 };
313
314 int target_request_register_commands(struct command_context *cmd_ctx)
315 {
316         return register_commands(cmd_ctx, NULL, target_req_command_handlers);
317 }