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/util.h>
34 #include <helper/configuration.h>
35 #include <flash/nor/core.h>
36 #include <flash/nand/core.h>
38 #include <target/arm_cti.h>
39 #include <target/arm_adi_v5.h>
40 #include <target/arm_tpiu_swo.h>
43 #include <server/server.h>
44 #include <server/gdb_server.h>
45 #include <server/rtt_server.h>
52 #define OPENOCD_VERSION \
53 "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
55 #define OPENOCD_VERSION \
56 "Open On-Chip Debugger " VERSION RELSTR
59 static const char openocd_startup_tcl
[] = {
60 #include "startup_tcl.inc"
61 0 /* Terminate with zero */
64 /* Give scripts and TELNET a way to find out what version this is */
65 static int jim_version_command(Jim_Interp
*interp
, int argc
,
66 Jim_Obj
* const *argv
)
72 version_str
= OPENOCD_VERSION
;
75 str
= Jim_GetString(argv
[1], NULL
);
77 if (strcmp("git", str
) == 0)
78 version_str
= GITVERSION
;
80 Jim_SetResult(interp
, Jim_NewStringObj(interp
, version_str
, -1));
85 static int log_target_callback_event_handler(struct target
*target
,
86 enum target_event event
,
90 case TARGET_EVENT_GDB_START
:
91 target
->verbose_halt_msg
= false;
93 case TARGET_EVENT_GDB_END
:
94 target
->verbose_halt_msg
= true;
96 case TARGET_EVENT_HALTED
:
97 if (target
->verbose_halt_msg
) {
98 /* do not display information when debugger caused the halt */
99 target_arch_state(target
);
109 static bool init_at_startup
= true;
111 COMMAND_HANDLER(handle_noinit_command
)
114 return ERROR_COMMAND_SYNTAX_ERROR
;
115 init_at_startup
= false;
119 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
120 COMMAND_HANDLER(handle_init_command
)
124 return ERROR_COMMAND_SYNTAX_ERROR
;
127 static int initialized
;
133 retval
= command_run_line(CMD_CTX
, "target init");
134 if (ERROR_OK
!= retval
)
137 retval
= adapter_init(CMD_CTX
);
138 if (retval
!= ERROR_OK
) {
139 /* we must be able to set up the debug adapter */
143 LOG_DEBUG("Debug Adapter init complete");
145 /* "transport init" verifies the expected devices are present;
146 * for JTAG, it checks the list of configured TAPs against
147 * what's discoverable, possibly with help from the platform's
148 * JTAG event handlers. (which require COMMAND_EXEC)
150 command_context_mode(CMD_CTX
, COMMAND_EXEC
);
152 retval
= command_run_line(CMD_CTX
, "transport init");
153 if (ERROR_OK
!= retval
)
156 retval
= command_run_line(CMD_CTX
, "dap init");
157 if (ERROR_OK
!= retval
)
160 LOG_DEBUG("Examining targets...");
161 if (target_examine() != ERROR_OK
)
162 LOG_DEBUG("target examination failed");
164 command_context_mode(CMD_CTX
, COMMAND_CONFIG
);
166 if (command_run_line(CMD_CTX
, "flash init") != ERROR_OK
)
169 if (command_run_line(CMD_CTX
, "nand init") != ERROR_OK
)
172 if (command_run_line(CMD_CTX
, "pld init") != ERROR_OK
)
174 command_context_mode(CMD_CTX
, COMMAND_EXEC
);
176 /* in COMMAND_EXEC, after target_examine(), only tpiu or only swo */
177 if (command_run_line(CMD_CTX
, "tpiu init") != ERROR_OK
)
180 /* initialize telnet subsystem */
181 gdb_target_add_all(all_targets
);
183 target_register_event_callback(log_target_callback_event_handler
, CMD_CTX
);
188 COMMAND_HANDLER(handle_add_script_search_dir_command
)
191 return ERROR_COMMAND_SYNTAX_ERROR
;
193 add_script_search_dir(CMD_ARGV
[0]);
198 static const struct command_registration openocd_command_handlers
[] = {
201 .jim_handler
= jim_version_command
,
203 .help
= "show program version",
207 .handler
= &handle_noinit_command
,
208 .mode
= COMMAND_CONFIG
,
209 .help
= "Prevent 'init' from being called at startup.",
214 .handler
= &handle_init_command
,
216 .help
= "Initializes configured targets and servers. "
217 "Changes command mode from CONFIG to EXEC. "
218 "Unless 'noinit' is called, this command is "
219 "called automatically at the end of startup.",
223 .name
= "add_script_search_dir",
224 .handler
= &handle_add_script_search_dir_command
,
226 .help
= "dir to search for config files and scripts",
227 .usage
= "<directory>"
229 COMMAND_REGISTRATION_DONE
232 static int openocd_register_commands(struct command_context
*cmd_ctx
)
234 return register_commands(cmd_ctx
, NULL
, openocd_command_handlers
);
237 struct command_context
*global_cmd_ctx
;
239 static struct command_context
*setup_command_handler(Jim_Interp
*interp
)
242 LOG_DEBUG("log_init: complete");
244 struct command_context
*cmd_ctx
= command_init(openocd_startup_tcl
, interp
);
246 /* register subsystem commands */
247 typedef int (*command_registrant_t
)(struct command_context
*cmd_ctx_value
);
248 static const command_registrant_t command_registrants
[] = {
249 &openocd_register_commands
,
250 &server_register_commands
,
251 &gdb_register_commands
,
252 &log_register_commands
,
253 &rtt_server_register_commands
,
254 &transport_register_commands
,
255 &interface_register_commands
,
256 &target_register_commands
,
257 &flash_register_commands
,
258 &nand_register_commands
,
259 &pld_register_commands
,
260 &cti_register_commands
,
261 &dap_register_commands
,
262 &arm_tpiu_swo_register_commands
,
265 for (unsigned i
= 0; NULL
!= command_registrants
[i
]; i
++) {
266 int retval
= (*command_registrants
[i
])(cmd_ctx
);
267 if (ERROR_OK
!= retval
) {
268 command_done(cmd_ctx
);
272 LOG_DEBUG("command registration: complete");
274 LOG_OUTPUT(OPENOCD_VERSION
"\n"
275 "Licensed under GNU GPL v2\n");
277 global_cmd_ctx
= cmd_ctx
;
282 /** OpenOCD runtime meat that can become single-thread in future. It parse
283 * commandline, reads configuration, sets up the target and starts server loop.
284 * Commandline arguments are passed into this function from openocd_main().
286 static int openocd_thread(int argc
, char *argv
[], struct command_context
*cmd_ctx
)
290 if (parse_cmdline_args(cmd_ctx
, argc
, argv
) != ERROR_OK
)
293 if (server_preinit() != ERROR_OK
)
296 ret
= parse_config_file(cmd_ctx
);
297 if (ret
== ERROR_COMMAND_CLOSE_CONNECTION
) {
298 server_quit(); /* gdb server may be initialized by -c init */
300 } else if (ret
!= ERROR_OK
) {
301 server_quit(); /* gdb server may be initialized by -c init */
305 ret
= server_init(cmd_ctx
);
309 if (init_at_startup
) {
310 ret
= command_run_line(cmd_ctx
, "init");
311 if (ERROR_OK
!= ret
) {
317 ret
= server_loop(cmd_ctx
);
319 int last_signal
= server_quit();
320 if (last_signal
!= ERROR_OK
)
328 /* normally this is the main() function entry, but if OpenOCD is linked
329 * into application, then this fn will not be invoked, but rather that
330 * application will have it's own implementation of main(). */
331 int openocd_main(int argc
, char *argv
[])
335 /* initialize commandline interface */
336 struct command_context
*cmd_ctx
;
338 cmd_ctx
= setup_command_handler(NULL
);
340 if (util_init(cmd_ctx
) != ERROR_OK
)
343 if (rtt_init() != ERROR_OK
)
346 LOG_OUTPUT("For bug reports, read\n\t"
347 "http://openocd.org/doc/doxygen/bugs.html"
350 command_context_mode(cmd_ctx
, COMMAND_CONFIG
);
351 command_set_output_handler(cmd_ctx
, configuration_output_handler
, NULL
);
353 server_host_os_entry();
355 /* Start the executable meat that can evolve into thread in future. */
356 ret
= openocd_thread(argc
, argv
, cmd_ctx
);
358 flash_free_all_banks();
360 arm_tpiu_swo_cleanup_all();
363 unregister_all_commands(cmd_ctx
, NULL
);
364 help_del_all_commands(cmd_ctx
);
366 /* free all DAP and CTI objects */
368 arm_cti_cleanup_all();
372 server_host_os_close();
374 /* Shutdown commandline interface */
375 command_exit(cmd_ctx
);
380 if (ERROR_FAIL
== ret
)
382 else if (ERROR_OK
!= ret
)