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 ***************************************************************************/
30 #include "replacements.h"
33 #include "target_request.h"
34 #include "binarybuffer.h"
41 command_t *target_request_cmd = NULL;
42 static int charmsg_mode = 0;
44 int target_asciimsg(target_t *target, u32 length)
46 char *msg = malloc(CEIL(length + 1, 4) * 4);
47 debug_msg_receiver_t *c = target->dbgmsg;
49 target->type->target_request_data(target, CEIL(length, 4), (u8*)msg);
56 command_print(c->cmd_ctx, "%s", msg);
63 int target_charmsg(target_t *target, u8 msg)
65 LOG_USER_N("%c", msg);
70 int target_hexmsg(target_t *target, int size, u32 length)
72 u8 *data = malloc(CEIL(length * size, 4) * 4);
75 debug_msg_receiver_t *c = target->dbgmsg;
78 LOG_DEBUG("size: %i, length: %i", size, length);
80 target->type->target_request_data(target, CEIL(length * size, 4), (u8*)data);
83 for (i = 0; i < length; i++)
88 line_len += snprintf(line + line_len, 128 - line_len, "%8.8x ", le_to_h_u32(data + (4*i)));
91 line_len += snprintf(line + line_len, 128 - line_len, "%4.4x ", le_to_h_u16(data + (2*i)));
94 line_len += snprintf(line + line_len, 128 - line_len, "%2.2x ", data[i]);
98 if ((i%8 == 7) || (i == length - 1))
100 LOG_DEBUG("%s", line);
104 command_print(c->cmd_ctx, "%s", line);
117 /* handle requests from the target received by a target specific
118 * side-band channel (e.g. ARM7/9 DCC)
120 int target_request(target_t *target, u32 request)
122 target_req_cmd_t target_req_cmd = request & 0xff;
124 if ( charmsg_mode ) {
125 target_charmsg(target, target_req_cmd );
128 switch (target_req_cmd)
130 case TARGET_REQ_TRACEMSG:
131 trace_point(target, (request & 0xffffff00) >> 8);
133 case TARGET_REQ_DEBUGMSG:
134 if (((request & 0xff00) >> 8) == 0)
136 target_asciimsg(target, (request & 0xffff0000) >> 16);
140 target_hexmsg(target, (request & 0xff00) >> 8, (request & 0xffff0000) >> 16);
143 case TARGET_REQ_DEBUGCHAR:
144 target_charmsg(target, (request & 0x00ff0000) >> 16);
146 /* case TARGET_REQ_SEMIHOSTING:
150 LOG_ERROR("unknown target request: %2.2x", target_req_cmd);
157 int add_debug_msg_receiver(struct command_context_s *cmd_ctx, target_t *target)
159 debug_msg_receiver_t **p = &target->dbgmsg;
162 return ERROR_INVALID_ARGUMENTS;
164 /* see if there's already a list */
167 /* find end of linked list */
174 /* add new debug message receiver */
175 (*p) = malloc(sizeof(debug_msg_receiver_t));
176 (*p)->cmd_ctx = cmd_ctx;
179 /* enable callback */
180 target->dbg_msg_enabled = 1;
185 debug_msg_receiver_t* find_debug_msg_receiver(struct command_context_s *cmd_ctx, target_t *target)
187 int do_all_targets = 0;
188 debug_msg_receiver_t **p = &target->dbgmsg;
190 /* if no target has been specified search all of them */
193 /* if no targets haven been specified */
194 if (all_targets == NULL)
197 target = all_targets;
205 if ((*p)->cmd_ctx == cmd_ctx)
212 target = target->next;
213 } while (target && do_all_targets);
218 int delete_debug_msg_receiver(struct command_context_s *cmd_ctx, target_t *target)
220 debug_msg_receiver_t **p;
221 debug_msg_receiver_t *c;
222 int do_all_targets = 0;
224 /* if no target has been specified search all of them */
227 /* if no targets haven been specified */
228 if (all_targets == NULL)
231 target = all_targets;
241 debug_msg_receiver_t *next = c->next;
242 if (c->cmd_ctx == cmd_ctx)
248 /* disable callback */
249 target->dbg_msg_enabled = 0;
258 target = target->next;
259 } while (target && do_all_targets);
264 int handle_target_request_debugmsgs_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
266 target_t *target = get_current_target(cmd_ctx);
270 /* see if reciever is already registered */
271 if (find_debug_msg_receiver(cmd_ctx, target) != NULL)
276 if (!strcmp(args[0], "enable") || !strcmp(args[0], "charmsg"))
278 /* don't register if this command context is already receiving */
282 add_debug_msg_receiver(cmd_ctx, target);
284 charmsg_mode = !strcmp(args[0], "charmsg");
286 else if (!strcmp(args[0], "disable"))
288 /* no need to delete a receiver if none is registered */
292 delete_debug_msg_receiver(cmd_ctx, target);
297 command_print(cmd_ctx, "usage: target_request debugmsgs ['enable'|'disable'|'charmsg']");
301 command_print(cmd_ctx, "receiving debug messages from current target %s",
302 (receiving) ? (charmsg_mode?"charmsg":"enabled") : "disabled" );
306 int target_request_register_commands(struct command_context_s *cmd_ctx)
309 register_command(cmd_ctx, NULL, "target_request", NULL, COMMAND_ANY, "target_request commands");
311 register_command(cmd_ctx, target_request_cmd, "debugmsgs", handle_target_request_debugmsgs_command,
312 COMMAND_EXEC, "enable/disable reception of debug messages from target");