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/driver.h>
31 #include <jtag/jtag.h>
32 #include <transport/transport.h>
33 #include <helper/ioutil.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 (ERROR_OK != retval)
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 (ERROR_OK != retval)
157 retval = command_run_line(CMD_CTX, "dap init");
158 if (ERROR_OK != retval)
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);
238 struct command_context *global_cmd_ctx;
240 static struct command_context *setup_command_handler(Jim_Interp *interp)
243 LOG_DEBUG("log_init: complete");
245 struct command_context *cmd_ctx = command_init(openocd_startup_tcl, interp);
247 /* register subsystem commands */
248 typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
249 static const command_registrant_t command_registrants[] = {
250 &openocd_register_commands,
251 &server_register_commands,
252 &gdb_register_commands,
253 &log_register_commands,
254 &rtt_server_register_commands,
255 &transport_register_commands,
256 &interface_register_commands,
257 &target_register_commands,
258 &flash_register_commands,
259 &nand_register_commands,
260 &pld_register_commands,
261 &cti_register_commands,
262 &dap_register_commands,
263 &arm_tpiu_swo_register_commands,
266 for (unsigned i = 0; NULL != command_registrants[i]; i++) {
267 int retval = (*command_registrants[i])(cmd_ctx);
268 if (ERROR_OK != retval) {
269 command_done(cmd_ctx);
273 LOG_DEBUG("command registration: complete");
275 LOG_OUTPUT(OPENOCD_VERSION "\n"
276 "Licensed under GNU GPL v2\n");
278 global_cmd_ctx = cmd_ctx;
283 /** OpenOCD runtime meat that can become single-thread in future. It parse
284 * commandline, reads configuration, sets up the target and starts server loop.
285 * Commandline arguments are passed into this function from openocd_main().
287 static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
291 if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
294 if (server_preinit() != ERROR_OK)
297 ret = parse_config_file(cmd_ctx);
298 if (ret == ERROR_COMMAND_CLOSE_CONNECTION) {
299 server_quit(); /* gdb server may be initialized by -c init */
301 } else if (ret != ERROR_OK) {
302 server_quit(); /* gdb server may be initialized by -c init */
306 ret = server_init(cmd_ctx);
310 if (init_at_startup) {
311 ret = command_run_line(cmd_ctx, "init");
312 if (ERROR_OK != ret) {
318 ret = server_loop(cmd_ctx);
320 int last_signal = server_quit();
321 if (last_signal != ERROR_OK)
329 /* normally this is the main() function entry, but if OpenOCD is linked
330 * into application, then this fn will not be invoked, but rather that
331 * application will have it's own implementation of main(). */
332 int openocd_main(int argc, char *argv[])
336 /* initialize commandline interface */
337 struct command_context *cmd_ctx;
339 cmd_ctx = setup_command_handler(NULL);
341 if (util_init(cmd_ctx) != ERROR_OK)
344 if (ioutil_init(cmd_ctx) != ERROR_OK)
347 if (rtt_init() != ERROR_OK)
350 LOG_OUTPUT("For bug reports, read\n\t"
351 "http://openocd.org/doc/doxygen/bugs.html"
354 command_context_mode(cmd_ctx, COMMAND_CONFIG);
355 command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
357 server_host_os_entry();
359 /* Start the executable meat that can evolve into thread in future. */
360 ret = openocd_thread(argc, argv, cmd_ctx);
362 flash_free_all_banks();
364 arm_tpiu_swo_cleanup_all();
367 unregister_all_commands(cmd_ctx, NULL);
369 /* free all DAP and CTI objects */
371 arm_cti_cleanup_all();
375 server_host_os_close();
377 /* Shutdown commandline interface */
378 command_exit(cmd_ctx);
383 if (ERROR_FAIL == ret)
385 else if (ERROR_OK != ret)