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>
48 #define OPENOCD_VERSION \
49 "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
51 static const char openocd_startup_tcl
[] = {
52 #include "startup_tcl.inc"
53 0 /* Terminate with zero */
56 /* Give scripts and TELNET a way to find out what version this is */
57 static int jim_version_command(Jim_Interp
*interp
, int argc
,
58 Jim_Obj
* const *argv
)
64 version_str
= OPENOCD_VERSION
;
67 str
= Jim_GetString(argv
[1], NULL
);
69 if (strcmp("git", str
) == 0)
70 version_str
= GITVERSION
;
72 Jim_SetResult(interp
, Jim_NewStringObj(interp
, version_str
, -1));
77 static int log_target_callback_event_handler(struct target
*target
,
78 enum target_event event
,
82 case TARGET_EVENT_GDB_START
:
85 case TARGET_EVENT_GDB_END
:
88 case TARGET_EVENT_HALTED
:
89 if (target
->display
) {
90 /* do not display information when debugger caused the halt */
91 target_arch_state(target
);
101 static bool init_at_startup
= true;
103 COMMAND_HANDLER(handle_noinit_command
)
106 return ERROR_COMMAND_SYNTAX_ERROR
;
107 init_at_startup
= false;
111 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
112 COMMAND_HANDLER(handle_init_command
)
116 return ERROR_COMMAND_SYNTAX_ERROR
;
119 static int initialized
;
125 retval
= command_run_line(CMD_CTX
, "target init");
126 if (ERROR_OK
!= retval
)
129 retval
= adapter_init(CMD_CTX
);
130 if (retval
!= ERROR_OK
) {
131 /* we must be able to set up the debug adapter */
135 LOG_DEBUG("Debug Adapter init complete");
137 /* "transport init" verifies the expected devices are present;
138 * for JTAG, it checks the list of configured TAPs against
139 * what's discoverable, possibly with help from the platform's
140 * JTAG event handlers. (which require COMMAND_EXEC)
142 command_context_mode(CMD_CTX
, COMMAND_EXEC
);
144 retval
= command_run_line(CMD_CTX
, "transport init");
145 if (ERROR_OK
!= retval
)
148 LOG_DEBUG("Examining targets...");
149 if (target_examine() != ERROR_OK
)
150 LOG_DEBUG("target examination failed");
152 command_context_mode(CMD_CTX
, COMMAND_CONFIG
);
154 if (command_run_line(CMD_CTX
, "flash init") != ERROR_OK
)
157 if (command_run_line(CMD_CTX
, "mflash init") != ERROR_OK
)
160 if (command_run_line(CMD_CTX
, "nand init") != ERROR_OK
)
163 if (command_run_line(CMD_CTX
, "pld init") != ERROR_OK
)
165 command_context_mode(CMD_CTX
, COMMAND_EXEC
);
167 /* initialize telnet subsystem */
168 gdb_target_add_all(all_targets
);
170 target_register_event_callback(log_target_callback_event_handler
, CMD_CTX
);
175 COMMAND_HANDLER(handle_add_script_search_dir_command
)
178 return ERROR_COMMAND_SYNTAX_ERROR
;
180 add_script_search_dir(CMD_ARGV
[0]);
185 static const struct command_registration openocd_command_handlers
[] = {
188 .jim_handler
= jim_version_command
,
190 .help
= "show program version",
194 .handler
= &handle_noinit_command
,
195 .mode
= COMMAND_CONFIG
,
196 .help
= "Prevent 'init' from being called at startup.",
201 .handler
= &handle_init_command
,
203 .help
= "Initializes configured targets and servers. "
204 "Changes command mode from CONFIG to EXEC. "
205 "Unless 'noinit' is called, this command is "
206 "called automatically at the end of startup.",
210 .name
= "add_script_search_dir",
211 .handler
= &handle_add_script_search_dir_command
,
213 .help
= "dir to search for config files and scripts",
214 .usage
= "<directory>"
216 COMMAND_REGISTRATION_DONE
219 static int openocd_register_commands(struct command_context
*cmd_ctx
)
221 return register_commands(cmd_ctx
, NULL
, openocd_command_handlers
);
224 struct command_context
*global_cmd_ctx
;
226 /* NB! this fn can be invoked outside this file for non PC hosted builds
227 * NB! do not change to 'static'!!!!
229 struct command_context
*setup_command_handler(Jim_Interp
*interp
)
232 LOG_DEBUG("log_init: complete");
234 struct command_context
*cmd_ctx
= command_init(openocd_startup_tcl
, interp
);
236 /* register subsystem commands */
237 typedef int (*command_registrant_t
)(struct command_context
*cmd_ctx_value
);
238 static const command_registrant_t command_registrants
[] = {
239 &openocd_register_commands
,
240 &server_register_commands
,
241 &gdb_register_commands
,
242 &log_register_commands
,
243 &transport_register_commands
,
244 &interface_register_commands
,
245 &target_register_commands
,
246 &flash_register_commands
,
247 &nand_register_commands
,
248 &pld_register_commands
,
249 &mflash_register_commands
,
252 for (unsigned i
= 0; NULL
!= command_registrants
[i
]; i
++) {
253 int retval
= (*command_registrants
[i
])(cmd_ctx
);
254 if (ERROR_OK
!= retval
) {
255 command_done(cmd_ctx
);
259 LOG_DEBUG("command registration: complete");
261 LOG_OUTPUT(OPENOCD_VERSION
"\n"
262 "Licensed under GNU GPL v2\n");
264 global_cmd_ctx
= cmd_ctx
;
269 /** OpenOCD runtime meat that can become single-thread in future. It parse
270 * commandline, reads configuration, sets up the target and starts server loop.
271 * Commandline arguments are passed into this function from openocd_main().
273 static int openocd_thread(int argc
, char *argv
[], struct command_context
*cmd_ctx
)
277 if (parse_cmdline_args(cmd_ctx
, argc
, argv
) != ERROR_OK
)
280 if (server_preinit() != ERROR_OK
)
283 ret
= parse_config_file(cmd_ctx
);
284 if (ret
== ERROR_COMMAND_CLOSE_CONNECTION
)
286 else if (ret
!= ERROR_OK
)
289 ret
= server_init(cmd_ctx
);
293 if (init_at_startup
) {
294 ret
= command_run_line(cmd_ctx
, "init");
299 ret
= server_loop(cmd_ctx
);
301 int last_signal
= server_quit();
302 if (last_signal
!= ERROR_OK
)
310 /* normally this is the main() function entry, but if OpenOCD is linked
311 * into application, then this fn will not be invoked, but rather that
312 * application will have it's own implementation of main(). */
313 int openocd_main(int argc
, char *argv
[])
317 /* initialize commandline interface */
318 struct command_context
*cmd_ctx
;
320 cmd_ctx
= setup_command_handler(NULL
);
322 if (util_init(cmd_ctx
) != ERROR_OK
)
325 if (ioutil_init(cmd_ctx
) != ERROR_OK
)
328 LOG_OUTPUT("For bug reports, read\n\t"
329 "http://openocd.org/doc/doxygen/bugs.html"
332 command_context_mode(cmd_ctx
, COMMAND_CONFIG
);
333 command_set_output_handler(cmd_ctx
, configuration_output_handler
, NULL
);
335 /* Start the executable meat that can evolve into thread in future. */
336 ret
= openocd_thread(argc
, argv
, cmd_ctx
);
338 unregister_all_commands(cmd_ctx
, NULL
);
340 /* free commandline interface */
341 command_done(cmd_ctx
);
345 if (ERROR_FAIL
== ret
)
347 else if (ERROR_OK
!= ret
)