Remove FSF address from GPL notices
[openocd.git] / src / target / target_request.c
blob6ca204b508dd71e04a1be7abcb545d12597dbb5c
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, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include <helper/log.h>
30 #include <helper/binarybuffer.h>
32 #include "target.h"
33 #include "target_request.h"
34 #include "target_type.h"
35 #include "trace.h"
37 static bool got_message;
39 bool target_got_message(void)
41 bool t = got_message;
42 got_message = false;
43 return t;
46 static int charmsg_mode;
48 static int target_asciimsg(struct target *target, uint32_t length)
50 char *msg = malloc(DIV_ROUND_UP(length + 1, 4) * 4);
51 struct debug_msg_receiver *c = target->dbgmsg;
53 target->type->target_request_data(target, DIV_ROUND_UP(length, 4), (uint8_t *)msg);
54 msg[length] = 0;
56 LOG_DEBUG("%s", msg);
58 while (c) {
59 command_print(c->cmd_ctx, "%s", msg);
60 c = c->next;
63 return ERROR_OK;
66 static int target_charmsg(struct target *target, uint8_t msg)
68 LOG_USER_N("%c", msg);
70 return ERROR_OK;
73 static int target_hexmsg(struct target *target, int size, uint32_t length)
75 uint8_t *data = malloc(DIV_ROUND_UP(length * size, 4) * 4);
76 char line[128];
77 int line_len;
78 struct debug_msg_receiver *c = target->dbgmsg;
79 uint32_t i;
81 LOG_DEBUG("size: %i, length: %i", (int)size, (int)length);
83 target->type->target_request_data(target, DIV_ROUND_UP(length * size, 4), (uint8_t *)data);
85 line_len = 0;
86 for (i = 0; i < length; i++) {
87 switch (size) {
88 case 4:
89 line_len += snprintf(line + line_len, 128 - line_len, "%8.8" PRIx32 " ", le_to_h_u32(data + (4*i)));
90 break;
91 case 2:
92 line_len += snprintf(line + line_len, 128 - line_len, "%4.4x ", le_to_h_u16(data + (2*i)));
93 break;
94 case 1:
95 line_len += snprintf(line + line_len, 128 - line_len, "%2.2x ", data[i]);
96 break;
99 if ((i%8 == 7) || (i == length - 1)) {
100 LOG_DEBUG("%s", line);
102 while (c) {
103 command_print(c->cmd_ctx, "%s", line);
104 c = c->next;
106 c = target->dbgmsg;
107 line_len = 0;
111 free(data);
113 return ERROR_OK;
116 /* handle requests from the target received by a target specific
117 * side-band channel (e.g. ARM7/9 DCC)
119 int target_request(struct target *target, uint32_t request)
121 target_req_cmd_t target_req_cmd = request & 0xff;
123 assert(target->type->target_request_data);
125 /* Record that we got a target message for back-off algorithm */
126 got_message = true;
128 if (charmsg_mode) {
129 target_charmsg(target, target_req_cmd);
130 return ERROR_OK;
133 switch (target_req_cmd) {
134 case TARGET_REQ_TRACEMSG:
135 trace_point(target, (request & 0xffffff00) >> 8);
136 break;
137 case TARGET_REQ_DEBUGMSG:
138 if (((request & 0xff00) >> 8) == 0)
139 target_asciimsg(target, (request & 0xffff0000) >> 16);
140 else
141 target_hexmsg(target, (request & 0xff00) >> 8, (request & 0xffff0000) >> 16);
142 break;
143 case TARGET_REQ_DEBUGCHAR:
144 target_charmsg(target, (request & 0x00ff0000) >> 16);
145 break;
146 /* case TARGET_REQ_SEMIHOSTING:
147 * break;
149 default:
150 LOG_ERROR("unknown target request: %2.2x", target_req_cmd);
151 break;
154 return ERROR_OK;
157 static int add_debug_msg_receiver(struct command_context *cmd_ctx, struct target *target)
159 struct debug_msg_receiver **p = &target->dbgmsg;
161 if (target == NULL)
162 return ERROR_COMMAND_SYNTAX_ERROR;
164 /* see if there's already a list */
165 if (*p) {
166 /* find end of linked list */
167 while ((*p)->next)
168 p = &((*p)->next);
169 p = &((*p)->next);
172 /* add new debug message receiver */
173 (*p) = malloc(sizeof(struct debug_msg_receiver));
174 (*p)->cmd_ctx = cmd_ctx;
175 (*p)->next = NULL;
177 /* enable callback */
178 target->dbg_msg_enabled = 1;
180 return ERROR_OK;
183 static struct debug_msg_receiver *find_debug_msg_receiver(struct command_context *cmd_ctx,
184 struct target *target)
186 int do_all_targets = 0;
188 /* if no target has been specified search all of them */
189 if (target == NULL) {
190 /* if no targets haven been specified */
191 if (all_targets == NULL)
192 return NULL;
194 target = all_targets;
195 do_all_targets = 1;
198 /* so we target != null */
199 struct debug_msg_receiver **p = &target->dbgmsg;
200 do {
201 while (*p) {
202 if ((*p)->cmd_ctx == cmd_ctx)
203 return *p;
204 p = &((*p)->next);
207 target = target->next;
208 } while (target && do_all_targets);
210 return NULL;
213 int delete_debug_msg_receiver(struct command_context *cmd_ctx, struct target *target)
215 struct debug_msg_receiver **p;
216 struct debug_msg_receiver *c;
217 int do_all_targets = 0;
219 /* if no target has been specified search all of them */
220 if (target == NULL) {
221 /* if no targets haven been specified */
222 if (all_targets == NULL)
223 return ERROR_OK;
225 target = all_targets;
226 do_all_targets = 1;
229 do {
230 p = &target->dbgmsg;
231 c = *p;
232 while (c) {
233 struct debug_msg_receiver *next = c->next;
234 if (c->cmd_ctx == cmd_ctx) {
235 *p = next;
236 free(c);
237 if (*p == NULL) {
238 /* disable callback */
239 target->dbg_msg_enabled = 0;
241 return ERROR_OK;
242 } else
243 p = &(c->next);
244 c = next;
247 target = target->next;
248 } while (target && do_all_targets);
250 return ERROR_OK;
253 COMMAND_HANDLER(handle_target_request_debugmsgs_command)
255 struct target *target = get_current_target(CMD_CTX);
257 int receiving = 0;
259 if (target->type->target_request_data == NULL) {
260 LOG_ERROR("Target %s does not support target requests", target_name(target));
261 return ERROR_OK;
264 /* see if reciever is already registered */
265 if (find_debug_msg_receiver(CMD_CTX, target) != NULL)
266 receiving = 1;
268 if (CMD_ARGC > 0) {
269 if (!strcmp(CMD_ARGV[0], "enable") || !strcmp(CMD_ARGV[0], "charmsg")) {
270 /* don't register if this command context is already receiving */
271 if (!receiving) {
272 receiving = 1;
273 add_debug_msg_receiver(CMD_CTX, target);
275 charmsg_mode = !strcmp(CMD_ARGV[0], "charmsg");
276 } else if (!strcmp(CMD_ARGV[0], "disable")) {
277 /* no need to delete a receiver if none is registered */
278 if (receiving) {
279 receiving = 0;
280 delete_debug_msg_receiver(CMD_CTX, target);
282 } else
283 return ERROR_COMMAND_SYNTAX_ERROR;
286 command_print(CMD_CTX, "receiving debug messages from current target %s",
287 (receiving) ? (charmsg_mode ? "charmsg" : "enabled") : "disabled");
288 return ERROR_OK;
291 static const struct command_registration target_req_exec_command_handlers[] = {
293 .name = "debugmsgs",
294 .handler = handle_target_request_debugmsgs_command,
295 .mode = COMMAND_EXEC,
296 .help = "display and/or modify reception of debug messages from target",
297 .usage = "['enable'|'charmsg'|'disable']",
299 COMMAND_REGISTRATION_DONE
301 static const struct command_registration target_req_command_handlers[] = {
303 .name = "target_request",
304 .mode = COMMAND_ANY,
305 .help = "target request command group",
306 .usage = "",
307 .chain = target_req_exec_command_handlers,
309 COMMAND_REGISTRATION_DONE
312 int target_request_register_commands(struct command_context *cmd_ctx)
314 return register_commands(cmd_ctx, NULL, target_req_command_handlers);