Remove FSF address from GPL notices
[openocd.git] / src / openocd.c
blobb5bb44b33c0eebed2c6199f78bc531d82d5b5ad3
1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007-2010 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 Richard Missenden *
9 * richard.missenden@googlemail.com *
10 * *
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. *
15 * *
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. *
20 * *
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 ***************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include "openocd.h"
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>
38 #include <pld/pld.h>
39 #include <flash/mflash.h>
41 #include <server/server.h>
42 #include <server/gdb_server.h>
44 #ifdef HAVE_STRINGS_H
45 #include <strings.h>
46 #endif
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)
60 if (argc > 2)
61 return JIM_ERR;
62 const char *str = "";
63 char *version_str;
64 version_str = OPENOCD_VERSION;
66 if (argc == 2)
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));
74 return JIM_OK;
77 static int log_target_callback_event_handler(struct target *target,
78 enum target_event event,
79 void *priv)
81 switch (event) {
82 case TARGET_EVENT_GDB_START:
83 target->display = 0;
84 break;
85 case TARGET_EVENT_GDB_END:
86 target->display = 1;
87 break;
88 case TARGET_EVENT_HALTED:
89 if (target->display) {
90 /* do not display information when debugger caused the halt */
91 target_arch_state(target);
93 break;
94 default:
95 break;
98 return ERROR_OK;
101 static bool init_at_startup = true;
103 COMMAND_HANDLER(handle_noinit_command)
105 if (CMD_ARGC != 0)
106 return ERROR_COMMAND_SYNTAX_ERROR;
107 init_at_startup = false;
108 return ERROR_OK;
111 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
112 COMMAND_HANDLER(handle_init_command)
115 if (CMD_ARGC != 0)
116 return ERROR_COMMAND_SYNTAX_ERROR;
118 int retval;
119 static int initialized;
120 if (initialized)
121 return ERROR_OK;
123 initialized = 1;
125 retval = command_run_line(CMD_CTX, "target init");
126 if (ERROR_OK != retval)
127 return ERROR_FAIL;
129 retval = adapter_init(CMD_CTX);
130 if (retval != ERROR_OK) {
131 /* we must be able to set up the debug adapter */
132 return retval;
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)
146 return ERROR_FAIL;
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)
155 return ERROR_FAIL;
157 if (command_run_line(CMD_CTX, "mflash init") != ERROR_OK)
158 return ERROR_FAIL;
160 if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
161 return ERROR_FAIL;
163 if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
164 return ERROR_FAIL;
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);
172 return ERROR_OK;
175 COMMAND_HANDLER(handle_add_script_search_dir_command)
177 if (CMD_ARGC != 1)
178 return ERROR_COMMAND_SYNTAX_ERROR;
180 add_script_search_dir(CMD_ARGV[0]);
182 return ERROR_OK;
185 static const struct command_registration openocd_command_handlers[] = {
187 .name = "version",
188 .jim_handler = jim_version_command,
189 .mode = COMMAND_ANY,
190 .help = "show program version",
193 .name = "noinit",
194 .handler = &handle_noinit_command,
195 .mode = COMMAND_CONFIG,
196 .help = "Prevent 'init' from being called at startup.",
197 .usage = ""
200 .name = "init",
201 .handler = &handle_init_command,
202 .mode = COMMAND_ANY,
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.",
207 .usage = ""
210 .name = "add_script_search_dir",
211 .handler = &handle_add_script_search_dir_command,
212 .mode = COMMAND_ANY,
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)
231 log_init();
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,
250 NULL
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);
256 return NULL;
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;
266 return 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)
275 int ret;
277 if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
278 return ERROR_FAIL;
280 if (server_preinit() != ERROR_OK)
281 return ERROR_FAIL;
283 ret = parse_config_file(cmd_ctx);
284 if (ret == ERROR_COMMAND_CLOSE_CONNECTION)
285 return ERROR_OK;
286 else if (ret != ERROR_OK)
287 return ERROR_FAIL;
289 ret = server_init(cmd_ctx);
290 if (ERROR_OK != ret)
291 return ERROR_FAIL;
293 if (init_at_startup) {
294 ret = command_run_line(cmd_ctx, "init");
295 if (ERROR_OK != ret)
296 return ERROR_FAIL;
299 ret = server_loop(cmd_ctx);
301 int last_signal = server_quit();
302 if (last_signal != ERROR_OK)
303 return last_signal;
305 if (ret != ERROR_OK)
306 return ERROR_FAIL;
307 return 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[])
315 int ret;
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)
323 return EXIT_FAILURE;
325 if (ioutil_init(cmd_ctx) != ERROR_OK)
326 return EXIT_FAILURE;
328 LOG_OUTPUT("For bug reports, read\n\t"
329 "http://openocd.org/doc/doxygen/bugs.html"
330 "\n");
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);
343 adapter_quit();
345 if (ERROR_FAIL == ret)
346 return EXIT_FAILURE;
347 else if (ERROR_OK != ret)
348 exit_on_signal(ret);
350 return ret;