1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2007 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007,2008 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
10 * Copyright (C) 2008 by Spencer Oliver *
11 * spen@spen-soft.co.uk *
12 ***************************************************************************/
18 #include <helper/log.h>
19 #include <helper/binarybuffer.h>
22 #include "target_request.h"
23 #include "target_type.h"
26 static bool got_message;
28 bool target_got_message(void)
35 static int charmsg_mode;
37 static int target_asciimsg(struct target *target, uint32_t length)
39 char *msg = malloc(DIV_ROUND_UP(length + 1, 4) * 4);
40 struct debug_msg_receiver *c = target->dbgmsg;
42 target->type->target_request_data(target, DIV_ROUND_UP(length, 4), (uint8_t *)msg);
48 command_output_text(c->cmd_ctx, msg);
55 static int target_charmsg(struct target *target, uint8_t msg)
57 LOG_USER_N("%c", msg);
62 static int target_hexmsg(struct target *target, int size, uint32_t length)
64 uint8_t *data = malloc(DIV_ROUND_UP(length * size, 4) * 4);
67 struct debug_msg_receiver *c = target->dbgmsg;
70 LOG_DEBUG("size: %i, length: %i", (int)size, (int)length);
72 target->type->target_request_data(target, DIV_ROUND_UP(length * size, 4), (uint8_t *)data);
75 for (i = 0; i < length; i++) {
78 line_len += snprintf(line + line_len, 128 - line_len, "%8.8" PRIx32 " ", le_to_h_u32(data + (4*i)));
81 line_len += snprintf(line + line_len, 128 - line_len, "%4.4x ", le_to_h_u16(data + (2*i)));
84 line_len += snprintf(line + line_len, 128 - line_len, "%2.2x ", data[i]);
88 if ((i%8 == 7) || (i == length - 1)) {
89 LOG_DEBUG("%s", line);
92 command_output_text(c->cmd_ctx, line);
105 /* handle requests from the target received by a target specific
106 * side-band channel (e.g. ARM7/9 DCC)
108 int target_request(struct target *target, uint32_t request)
110 target_req_cmd_t target_req_cmd = request & 0xff;
112 assert(target->type->target_request_data);
114 /* Record that we got a target message for back-off algorithm */
118 target_charmsg(target, target_req_cmd);
122 switch (target_req_cmd) {
123 case TARGET_REQ_TRACEMSG:
124 trace_point(target, (request & 0xffffff00) >> 8);
126 case TARGET_REQ_DEBUGMSG:
127 if (((request & 0xff00) >> 8) == 0)
128 target_asciimsg(target, (request & 0xffff0000) >> 16);
130 target_hexmsg(target, (request & 0xff00) >> 8, (request & 0xffff0000) >> 16);
132 case TARGET_REQ_DEBUGCHAR:
133 target_charmsg(target, (request & 0x00ff0000) >> 16);
135 /* case TARGET_REQ_SEMIHOSTING:
139 LOG_ERROR("unknown target request: %2.2x", target_req_cmd);
146 static int add_debug_msg_receiver(struct command_context *cmd_ctx, struct target *target)
148 struct debug_msg_receiver **p = &target->dbgmsg;
151 return ERROR_COMMAND_SYNTAX_ERROR;
153 /* see if there's already a list */
155 /* find end of linked list */
161 /* add new debug message receiver */
162 (*p) = malloc(sizeof(struct debug_msg_receiver));
163 (*p)->cmd_ctx = cmd_ctx;
166 /* enable callback */
167 target->dbg_msg_enabled = 1;
172 static struct debug_msg_receiver *find_debug_msg_receiver(struct command_context *cmd_ctx,
173 struct target *target)
175 int do_all_targets = 0;
177 /* if no target has been specified search all of them */
179 /* if no targets haven been specified */
183 target = all_targets;
187 /* so we target != null */
188 struct debug_msg_receiver **p = &target->dbgmsg;
191 if ((*p)->cmd_ctx == cmd_ctx)
196 target = target->next;
197 } while (target && do_all_targets);
202 int delete_debug_msg_receiver(struct command_context *cmd_ctx, struct target *target)
204 struct debug_msg_receiver **p;
205 struct debug_msg_receiver *c;
206 int do_all_targets = 0;
208 /* if no target has been specified search all of them */
210 /* if no targets haven been specified */
214 target = all_targets;
222 struct debug_msg_receiver *next = c->next;
223 if (c->cmd_ctx == cmd_ctx) {
227 /* disable callback */
228 target->dbg_msg_enabled = 0;
236 target = target->next;
237 } while (target && do_all_targets);
242 COMMAND_HANDLER(handle_target_request_debugmsgs_command)
244 struct target *target = get_current_target(CMD_CTX);
248 if (!target->type->target_request_data) {
249 LOG_ERROR("Target %s does not support target requests", target_name(target));
253 /* see if receiver is already registered */
254 if (find_debug_msg_receiver(CMD_CTX, target))
258 if (!strcmp(CMD_ARGV[0], "enable") || !strcmp(CMD_ARGV[0], "charmsg")) {
259 /* don't register if this command context is already receiving */
262 add_debug_msg_receiver(CMD_CTX, target);
264 charmsg_mode = !strcmp(CMD_ARGV[0], "charmsg");
265 } else if (!strcmp(CMD_ARGV[0], "disable")) {
266 /* no need to delete a receiver if none is registered */
269 delete_debug_msg_receiver(CMD_CTX, target);
272 return ERROR_COMMAND_SYNTAX_ERROR;
275 command_print(CMD, "receiving debug messages from current target %s",
276 (receiving) ? (charmsg_mode ? "charmsg" : "enabled") : "disabled");
280 static const struct command_registration target_req_exec_command_handlers[] = {
283 .handler = handle_target_request_debugmsgs_command,
284 .mode = COMMAND_EXEC,
285 .help = "display and/or modify reception of debug messages from target",
286 .usage = "['enable'|'charmsg'|'disable']",
288 COMMAND_REGISTRATION_DONE
290 static const struct command_registration target_req_command_handlers[] = {
292 .name = "target_request",
294 .help = "target request command group",
296 .chain = target_req_exec_command_handlers,
298 COMMAND_REGISTRATION_DONE
301 int target_request_register_commands(struct command_context *cmd_ctx)
303 return register_commands(cmd_ctx, NULL, target_req_command_handlers);