1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 Richard Missenden *
9 * richard.missenden@googlemail.com *
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, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
30 #include <jtag/adapter.h>
31 #include <jtag/driver.h>
32 #include <jtag/jtag.h>
33 #include <transport/transport.h>
34 #include <helper/util.h>
35 #include <helper/configuration.h>
36 #include <flash/nor/core.h>
37 #include <flash/nand/core.h>
39 #include <target/arm_cti.h>
40 #include <target/arm_adi_v5.h>
41 #include <target/arm_tpiu_swo.h>
44 #include <server/server.h>
45 #include <server/gdb_server.h>
46 #include <server/rtt_server.h>
53 #define OPENOCD_VERSION \
54 "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
56 #define OPENOCD_VERSION \
57 "Open On-Chip Debugger " VERSION RELSTR
60 static const char openocd_startup_tcl[] = {
61 #include "startup_tcl.inc"
62 0 /* Terminate with zero */
65 /* Give scripts and TELNET a way to find out what version this is */
66 static int jim_version_command(Jim_Interp *interp, int argc,
67 Jim_Obj * const *argv)
73 version_str = OPENOCD_VERSION;
76 str = Jim_GetString(argv[1], NULL);
78 if (strcmp("git", str) == 0)
79 version_str = GITVERSION;
81 Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
86 static int log_target_callback_event_handler(struct target *target,
87 enum target_event event,
91 case TARGET_EVENT_GDB_START:
92 target->verbose_halt_msg = false;
94 case TARGET_EVENT_GDB_END:
95 target->verbose_halt_msg = true;
97 case TARGET_EVENT_HALTED:
98 if (target->verbose_halt_msg) {
99 /* do not display information when debugger caused the halt */
100 target_arch_state(target);
110 static bool init_at_startup = true;
112 COMMAND_HANDLER(handle_noinit_command)
115 return ERROR_COMMAND_SYNTAX_ERROR;
116 init_at_startup = false;
120 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
121 COMMAND_HANDLER(handle_init_command)
125 return ERROR_COMMAND_SYNTAX_ERROR;
128 static int initialized;
134 retval = command_run_line(CMD_CTX, "target init");
135 if (retval != ERROR_OK)
138 retval = adapter_init(CMD_CTX);
139 if (retval != ERROR_OK) {
140 /* we must be able to set up the debug adapter */
144 LOG_DEBUG("Debug Adapter init complete");
146 /* "transport init" verifies the expected devices are present;
147 * for JTAG, it checks the list of configured TAPs against
148 * what's discoverable, possibly with help from the platform's
149 * JTAG event handlers. (which require COMMAND_EXEC)
151 command_context_mode(CMD_CTX, COMMAND_EXEC);
153 retval = command_run_line(CMD_CTX, "transport init");
154 if (retval != ERROR_OK)
157 retval = command_run_line(CMD_CTX, "dap init");
158 if (retval != ERROR_OK)
161 LOG_DEBUG("Examining targets...");
162 if (target_examine() != ERROR_OK)
163 LOG_DEBUG("target examination failed");
165 command_context_mode(CMD_CTX, COMMAND_CONFIG);
167 if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
170 if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
173 if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
175 command_context_mode(CMD_CTX, COMMAND_EXEC);
177 /* in COMMAND_EXEC, after target_examine(), only tpiu or only swo */
178 if (command_run_line(CMD_CTX, "tpiu init") != ERROR_OK)
181 /* initialize telnet subsystem */
182 gdb_target_add_all(all_targets);
184 target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
189 COMMAND_HANDLER(handle_add_script_search_dir_command)
192 return ERROR_COMMAND_SYNTAX_ERROR;
194 add_script_search_dir(CMD_ARGV[0]);
199 static const struct command_registration openocd_command_handlers[] = {
202 .jim_handler = jim_version_command,
204 .help = "show program version",
208 .handler = &handle_noinit_command,
209 .mode = COMMAND_CONFIG,
210 .help = "Prevent 'init' from being called at startup.",
215 .handler = &handle_init_command,
217 .help = "Initializes configured targets and servers. "
218 "Changes command mode from CONFIG to EXEC. "
219 "Unless 'noinit' is called, this command is "
220 "called automatically at the end of startup.",
224 .name = "add_script_search_dir",
225 .handler = &handle_add_script_search_dir_command,
227 .help = "dir to search for config files and scripts",
228 .usage = "<directory>"
230 COMMAND_REGISTRATION_DONE
233 static int openocd_register_commands(struct command_context *cmd_ctx)
235 return register_commands(cmd_ctx, NULL, openocd_command_handlers);
239 * TODO: to be removed after v0.12.0
240 * workaround for syntax change of "expr" in jimtcl 0.81
241 * replace "expr" with openocd version that prints the deprecated msg
243 struct jim_scriptobj {
245 Jim_Obj *filename_obj;
254 static int jim_expr_command(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
257 return Jim_EvalExpression(interp, argv[1]);
260 Jim_Obj *obj = Jim_ConcatObj(interp, argc - 1, argv + 1);
261 Jim_IncrRefCount(obj);
262 const char *s = Jim_String(obj);
263 struct jim_scriptobj *script = Jim_GetIntRepPtr(interp->currentScriptObj);
264 if (interp->currentScriptObj == interp->emptyObj ||
265 strcmp(interp->currentScriptObj->typePtr->name, "script") ||
266 script->subst_flags ||
267 script->filename_obj == interp->emptyObj)
268 LOG_WARNING("DEPRECATED! use 'expr { %s }' not 'expr %s'", s, s);
270 LOG_WARNING("DEPRECATED! (%s:%d) use 'expr { %s }' not 'expr %s'",
271 Jim_String(script->filename_obj), script->linenr, s, s);
272 int retcode = Jim_EvalExpression(interp, obj);
273 Jim_DecrRefCount(interp, obj);
277 Jim_WrongNumArgs(interp, 1, argv, "expression ?...?");
281 static const struct command_registration expr_handler[] = {
284 .jim_handler = jim_expr_command,
289 COMMAND_REGISTRATION_DONE
292 static int workaround_for_jimtcl_expr(struct command_context *cmd_ctx)
294 return register_commands(cmd_ctx, NULL, expr_handler);
297 struct command_context *global_cmd_ctx;
299 static struct command_context *setup_command_handler(Jim_Interp *interp)
302 LOG_DEBUG("log_init: complete");
304 struct command_context *cmd_ctx = command_init(openocd_startup_tcl, interp);
306 /* register subsystem commands */
307 typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
308 static const command_registrant_t command_registrants[] = {
309 &workaround_for_jimtcl_expr,
310 &openocd_register_commands,
311 &server_register_commands,
312 &gdb_register_commands,
313 &log_register_commands,
314 &rtt_server_register_commands,
315 &transport_register_commands,
316 &interface_register_commands,
317 &target_register_commands,
318 &flash_register_commands,
319 &nand_register_commands,
320 &pld_register_commands,
321 &cti_register_commands,
322 &dap_register_commands,
323 &arm_tpiu_swo_register_commands,
326 for (unsigned i = 0; command_registrants[i]; i++) {
327 int retval = (*command_registrants[i])(cmd_ctx);
328 if (retval != ERROR_OK) {
329 command_done(cmd_ctx);
333 LOG_DEBUG("command registration: complete");
335 LOG_OUTPUT(OPENOCD_VERSION "\n"
336 "Licensed under GNU GPL v2\n");
338 global_cmd_ctx = cmd_ctx;
343 /** OpenOCD runtime meat that can become single-thread in future. It parse
344 * commandline, reads configuration, sets up the target and starts server loop.
345 * Commandline arguments are passed into this function from openocd_main().
347 static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
351 if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
354 if (server_preinit() != ERROR_OK)
357 ret = parse_config_file(cmd_ctx);
358 if (ret == ERROR_COMMAND_CLOSE_CONNECTION) {
359 server_quit(); /* gdb server may be initialized by -c init */
361 } else if (ret != ERROR_OK) {
362 server_quit(); /* gdb server may be initialized by -c init */
366 ret = server_init(cmd_ctx);
370 if (init_at_startup) {
371 ret = command_run_line(cmd_ctx, "init");
372 if (ret != ERROR_OK) {
378 ret = server_loop(cmd_ctx);
380 int last_signal = server_quit();
381 if (last_signal != ERROR_OK)
389 /* normally this is the main() function entry, but if OpenOCD is linked
390 * into application, then this fn will not be invoked, but rather that
391 * application will have it's own implementation of main(). */
392 int openocd_main(int argc, char *argv[])
396 /* initialize commandline interface */
397 struct command_context *cmd_ctx;
399 cmd_ctx = setup_command_handler(NULL);
401 if (util_init(cmd_ctx) != ERROR_OK)
404 if (rtt_init() != ERROR_OK)
407 LOG_OUTPUT("For bug reports, read\n\t"
408 "http://openocd.org/doc/doxygen/bugs.html"
411 command_context_mode(cmd_ctx, COMMAND_CONFIG);
412 command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
414 server_host_os_entry();
416 /* Start the executable meat that can evolve into thread in future. */
417 ret = openocd_thread(argc, argv, cmd_ctx);
419 flash_free_all_banks();
421 arm_tpiu_swo_cleanup_all();
424 unregister_all_commands(cmd_ctx, NULL);
425 help_del_all_commands(cmd_ctx);
427 /* free all DAP and CTI objects */
428 arm_cti_cleanup_all();
433 server_host_os_close();
435 /* Shutdown commandline interface */
436 command_exit(cmd_ctx);
441 if (ret == ERROR_FAIL)
443 else if (ret != ERROR_OK)