1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
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. *
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. *
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 ***************************************************************************/
31 #include "target_request.h"
32 #include "target_type.h"
33 #include <helper/binarybuffer.h>
35 #include <helper/log.h>
38 static int charmsg_mode
= 0;
40 static int target_asciimsg(struct target
*target
, uint32_t length
)
42 char *msg
= malloc(DIV_ROUND_UP(length
+ 1, 4) * 4);
43 struct debug_msg_receiver
*c
= target
->dbgmsg
;
45 target
->type
->target_request_data(target
, DIV_ROUND_UP(length
, 4), (uint8_t*)msg
);
52 command_print(c
->cmd_ctx
, "%s", msg
);
59 static int target_charmsg(struct target
*target
, uint8_t msg
)
61 LOG_USER_N("%c", msg
);
66 static int target_hexmsg(struct target
*target
, int size
, uint32_t length
)
68 uint8_t *data
= malloc(DIV_ROUND_UP(length
* size
, 4) * 4);
71 struct debug_msg_receiver
*c
= target
->dbgmsg
;
74 LOG_DEBUG("size: %i, length: %i", (int)size
, (int)length
);
76 target
->type
->target_request_data(target
, DIV_ROUND_UP(length
* size
, 4), (uint8_t*)data
);
79 for (i
= 0; i
< length
; i
++)
84 line_len
+= snprintf(line
+ line_len
, 128 - line_len
, "%8.8" PRIx32
" ", le_to_h_u32(data
+ (4*i
)));
87 line_len
+= snprintf(line
+ line_len
, 128 - line_len
, "%4.4x ", le_to_h_u16(data
+ (2*i
)));
90 line_len
+= snprintf(line
+ line_len
, 128 - line_len
, "%2.2x ", data
[i
]);
94 if ((i
%8 == 7) || (i
== length
- 1))
96 LOG_DEBUG("%s", line
);
100 command_print(c
->cmd_ctx
, "%s", line
);
113 /* handle requests from the target received by a target specific
114 * side-band channel (e.g. ARM7/9 DCC)
116 int target_request(struct target
*target
, uint32_t request
)
118 target_req_cmd_t target_req_cmd
= request
& 0xff;
121 target_charmsg(target
, target_req_cmd
);
124 switch (target_req_cmd
)
126 case TARGET_REQ_TRACEMSG
:
127 trace_point(target
, (request
& 0xffffff00) >> 8);
129 case TARGET_REQ_DEBUGMSG
:
130 if (((request
& 0xff00) >> 8) == 0)
132 target_asciimsg(target
, (request
& 0xffff0000) >> 16);
136 target_hexmsg(target
, (request
& 0xff00) >> 8, (request
& 0xffff0000) >> 16);
139 case TARGET_REQ_DEBUGCHAR
:
140 target_charmsg(target
, (request
& 0x00ff0000) >> 16);
142 /* case TARGET_REQ_SEMIHOSTING:
146 LOG_ERROR("unknown target request: %2.2x", target_req_cmd
);
153 static int add_debug_msg_receiver(struct command_context
*cmd_ctx
, struct target
*target
)
155 struct debug_msg_receiver
**p
= &target
->dbgmsg
;
158 return ERROR_INVALID_ARGUMENTS
;
160 /* see if there's already a list */
163 /* find end of linked list */
170 /* add new debug message receiver */
171 (*p
) = malloc(sizeof(struct debug_msg_receiver
));
172 (*p
)->cmd_ctx
= cmd_ctx
;
175 /* enable callback */
176 target
->dbg_msg_enabled
= 1;
181 static struct debug_msg_receiver
* find_debug_msg_receiver(struct command_context
*cmd_ctx
, struct target
*target
)
183 int do_all_targets
= 0;
184 struct debug_msg_receiver
**p
= &target
->dbgmsg
;
186 /* if no target has been specified search all of them */
189 /* if no targets haven been specified */
190 if (all_targets
== NULL
)
193 target
= all_targets
;
201 if ((*p
)->cmd_ctx
== cmd_ctx
)
208 target
= target
->next
;
209 } while (target
&& do_all_targets
);
214 int delete_debug_msg_receiver(struct command_context
*cmd_ctx
, struct target
*target
)
216 struct debug_msg_receiver
**p
;
217 struct debug_msg_receiver
*c
;
218 int do_all_targets
= 0;
220 /* if no target has been specified search all of them */
223 /* if no targets haven been specified */
224 if (all_targets
== NULL
)
227 target
= all_targets
;
237 struct debug_msg_receiver
*next
= c
->next
;
238 if (c
->cmd_ctx
== cmd_ctx
)
244 /* disable callback */
245 target
->dbg_msg_enabled
= 0;
254 target
= target
->next
;
255 } while (target
&& do_all_targets
);
260 COMMAND_HANDLER(handle_target_request_debugmsgs_command
)
262 struct target
*target
= get_current_target(CMD_CTX
);
266 /* see if reciever is already registered */
267 if (find_debug_msg_receiver(CMD_CTX
, target
) != NULL
)
272 if (!strcmp(CMD_ARGV
[0], "enable") || !strcmp(CMD_ARGV
[0], "charmsg"))
274 /* don't register if this command context is already receiving */
278 add_debug_msg_receiver(CMD_CTX
, target
);
280 charmsg_mode
= !strcmp(CMD_ARGV
[0], "charmsg");
282 else if (!strcmp(CMD_ARGV
[0], "disable"))
284 /* no need to delete a receiver if none is registered */
288 delete_debug_msg_receiver(CMD_CTX
, target
);
293 command_print(CMD_CTX
, "usage: target_request debugmsgs ['enable'|'disable'|'charmsg']");
297 command_print(CMD_CTX
, "receiving debug messages from current target %s",
298 (receiving
) ? (charmsg_mode
?"charmsg":"enabled") : "disabled");
302 static const struct command_registration target_req_exec_command_handlers
[] = {
305 .handler
= &handle_target_request_debugmsgs_command
,
306 .mode
= COMMAND_EXEC
,
307 .help
= "set reception of debug messages from target",
308 .usage
= "(enable|disable)",
310 COMMAND_REGISTRATION_DONE
312 static const struct command_registration target_req_command_handlers
[] = {
314 .name
= "target_request",
316 .help
= "target request command group",
317 .chain
= target_req_exec_command_handlers
,
319 COMMAND_REGISTRATION_DONE
322 int target_request_register_commands(struct command_context
*cmd_ctx
)
324 return register_commands(cmd_ctx
, NULL
, target_req_command_handlers
);