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 <flash/mflash.h>
41 #include <server/server.h>
42 #include <server/gdb_server.h>
49 #define OPENOCD_VERSION \
50 "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
52 #define OPENOCD_VERSION \
53 "Open On-Chip Debugger " VERSION RELSTR
56 static const char openocd_startup_tcl[] = {
57 #include "startup_tcl.inc"
58 0 /* Terminate with zero */
61 /* Give scripts and TELNET a way to find out what version this is */
62 static int jim_version_command(Jim_Interp *interp, int argc,
63 Jim_Obj * const *argv)
69 version_str = OPENOCD_VERSION;
72 str = Jim_GetString(argv[1], NULL);
74 if (strcmp("git", str) == 0)
75 version_str = GITVERSION;
77 Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
82 static int log_target_callback_event_handler(struct target *target,
83 enum target_event event,
87 case TARGET_EVENT_GDB_START:
90 case TARGET_EVENT_GDB_END:
93 case TARGET_EVENT_HALTED:
94 if (target->display) {
95 /* do not display information when debugger caused the halt */
96 target_arch_state(target);
106 static bool init_at_startup = true;
108 COMMAND_HANDLER(handle_noinit_command)
111 return ERROR_COMMAND_SYNTAX_ERROR;
112 init_at_startup = false;
116 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
117 COMMAND_HANDLER(handle_init_command)
121 return ERROR_COMMAND_SYNTAX_ERROR;
124 static int initialized;
130 retval = command_run_line(CMD_CTX, "target init");
131 if (ERROR_OK != retval)
134 retval = adapter_init(CMD_CTX);
135 if (retval != ERROR_OK) {
136 /* we must be able to set up the debug adapter */
140 LOG_DEBUG("Debug Adapter init complete");
142 /* "transport init" verifies the expected devices are present;
143 * for JTAG, it checks the list of configured TAPs against
144 * what's discoverable, possibly with help from the platform's
145 * JTAG event handlers. (which require COMMAND_EXEC)
147 command_context_mode(CMD_CTX, COMMAND_EXEC);
149 retval = command_run_line(CMD_CTX, "transport init");
150 if (ERROR_OK != retval)
153 LOG_DEBUG("Examining targets...");
154 if (target_examine() != ERROR_OK)
155 LOG_DEBUG("target examination failed");
157 command_context_mode(CMD_CTX, COMMAND_CONFIG);
159 if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
162 if (command_run_line(CMD_CTX, "mflash init") != ERROR_OK)
165 if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
168 if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
170 command_context_mode(CMD_CTX, COMMAND_EXEC);
172 /* initialize telnet subsystem */
173 gdb_target_add_all(all_targets);
175 target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
180 COMMAND_HANDLER(handle_add_script_search_dir_command)
183 return ERROR_COMMAND_SYNTAX_ERROR;
185 add_script_search_dir(CMD_ARGV[0]);
190 static const struct command_registration openocd_command_handlers[] = {
193 .jim_handler = jim_version_command,
195 .help = "show program version",
199 .handler = &handle_noinit_command,
200 .mode = COMMAND_CONFIG,
201 .help = "Prevent 'init' from being called at startup.",
206 .handler = &handle_init_command,
208 .help = "Initializes configured targets and servers. "
209 "Changes command mode from CONFIG to EXEC. "
210 "Unless 'noinit' is called, this command is "
211 "called automatically at the end of startup.",
215 .name = "add_script_search_dir",
216 .handler = &handle_add_script_search_dir_command,
218 .help = "dir to search for config files and scripts",
219 .usage = "<directory>"
221 COMMAND_REGISTRATION_DONE
224 static int openocd_register_commands(struct command_context *cmd_ctx)
226 return register_commands(cmd_ctx, NULL, openocd_command_handlers);
229 struct command_context *global_cmd_ctx;
231 /* NB! this fn can be invoked outside this file for non PC hosted builds
232 * NB! do not change to 'static'!!!!
234 struct command_context *setup_command_handler(Jim_Interp *interp)
237 LOG_DEBUG("log_init: complete");
239 struct command_context *cmd_ctx = command_init(openocd_startup_tcl, interp);
241 /* register subsystem commands */
242 typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
243 static const command_registrant_t command_registrants[] = {
244 &openocd_register_commands,
245 &server_register_commands,
246 &gdb_register_commands,
247 &log_register_commands,
248 &transport_register_commands,
249 &interface_register_commands,
250 &target_register_commands,
251 &flash_register_commands,
252 &nand_register_commands,
253 &pld_register_commands,
254 &mflash_register_commands,
257 for (unsigned i = 0; NULL != command_registrants[i]; i++) {
258 int retval = (*command_registrants[i])(cmd_ctx);
259 if (ERROR_OK != retval) {
260 command_done(cmd_ctx);
264 LOG_DEBUG("command registration: complete");
266 LOG_OUTPUT(OPENOCD_VERSION "\n"
267 "Licensed under GNU GPL v2\n");
269 global_cmd_ctx = cmd_ctx;
274 /** OpenOCD runtime meat that can become single-thread in future. It parse
275 * commandline, reads configuration, sets up the target and starts server loop.
276 * Commandline arguments are passed into this function from openocd_main().
278 static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
282 if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
285 if (server_preinit() != ERROR_OK)
288 ret = parse_config_file(cmd_ctx);
289 if (ret == ERROR_COMMAND_CLOSE_CONNECTION)
291 else if (ret != ERROR_OK)
294 ret = server_init(cmd_ctx);
298 if (init_at_startup) {
299 ret = command_run_line(cmd_ctx, "init");
300 if (ERROR_OK != ret) {
306 ret = server_loop(cmd_ctx);
308 int last_signal = server_quit();
309 if (last_signal != ERROR_OK)
317 /* normally this is the main() function entry, but if OpenOCD is linked
318 * into application, then this fn will not be invoked, but rather that
319 * application will have it's own implementation of main(). */
320 int openocd_main(int argc, char *argv[])
324 /* initialize commandline interface */
325 struct command_context *cmd_ctx;
327 cmd_ctx = setup_command_handler(NULL);
329 if (util_init(cmd_ctx) != ERROR_OK)
332 if (ioutil_init(cmd_ctx) != ERROR_OK)
335 LOG_OUTPUT("For bug reports, read\n\t"
336 "http://openocd.org/doc/doxygen/bugs.html"
339 command_context_mode(cmd_ctx, COMMAND_CONFIG);
340 command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
342 /* Start the executable meat that can evolve into thread in future. */
343 ret = openocd_thread(argc, argv, cmd_ctx);
345 unregister_all_commands(cmd_ctx, NULL);
347 /* free commandline interface */
348 command_done(cmd_ctx);
352 if (ERROR_FAIL == ret)
354 else if (ERROR_OK != ret)