nrf5: add nrf5 device definition for HWID 0x00E3
[openocd.git] / src / openocd.c
blobf084dd4522d6294e027578982f803d9c9b86927b
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>
40 #include <target/arm_cti.h>
41 #include <target/arm_adi_v5.h>
43 #include <server/server.h>
44 #include <server/gdb_server.h>
46 #ifdef HAVE_STRINGS_H
47 #include <strings.h>
48 #endif
50 #ifdef PKGBLDDATE
51 #define OPENOCD_VERSION \
52 "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
53 #else
54 #define OPENOCD_VERSION \
55 "Open On-Chip Debugger " VERSION RELSTR
56 #endif
58 static const char openocd_startup_tcl[] = {
59 #include "startup_tcl.inc"
60 0 /* Terminate with zero */
63 /* Give scripts and TELNET a way to find out what version this is */
64 static int jim_version_command(Jim_Interp *interp, int argc,
65 Jim_Obj * const *argv)
67 if (argc > 2)
68 return JIM_ERR;
69 const char *str = "";
70 char *version_str;
71 version_str = OPENOCD_VERSION;
73 if (argc == 2)
74 str = Jim_GetString(argv[1], NULL);
76 if (strcmp("git", str) == 0)
77 version_str = GITVERSION;
79 Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
81 return JIM_OK;
84 static int log_target_callback_event_handler(struct target *target,
85 enum target_event event,
86 void *priv)
88 switch (event) {
89 case TARGET_EVENT_GDB_START:
90 target->verbose_halt_msg = false;
91 break;
92 case TARGET_EVENT_GDB_END:
93 target->verbose_halt_msg = true;
94 break;
95 case TARGET_EVENT_HALTED:
96 if (target->verbose_halt_msg) {
97 /* do not display information when debugger caused the halt */
98 target_arch_state(target);
100 break;
101 default:
102 break;
105 return ERROR_OK;
108 static bool init_at_startup = true;
110 COMMAND_HANDLER(handle_noinit_command)
112 if (CMD_ARGC != 0)
113 return ERROR_COMMAND_SYNTAX_ERROR;
114 init_at_startup = false;
115 return ERROR_OK;
118 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
119 COMMAND_HANDLER(handle_init_command)
122 if (CMD_ARGC != 0)
123 return ERROR_COMMAND_SYNTAX_ERROR;
125 int retval;
126 static int initialized;
127 if (initialized)
128 return ERROR_OK;
130 initialized = 1;
132 retval = command_run_line(CMD_CTX, "target init");
133 if (ERROR_OK != retval)
134 return ERROR_FAIL;
136 retval = adapter_init(CMD_CTX);
137 if (retval != ERROR_OK) {
138 /* we must be able to set up the debug adapter */
139 return retval;
142 LOG_DEBUG("Debug Adapter init complete");
144 /* "transport init" verifies the expected devices are present;
145 * for JTAG, it checks the list of configured TAPs against
146 * what's discoverable, possibly with help from the platform's
147 * JTAG event handlers. (which require COMMAND_EXEC)
149 command_context_mode(CMD_CTX, COMMAND_EXEC);
151 retval = command_run_line(CMD_CTX, "transport init");
152 if (ERROR_OK != retval)
153 return ERROR_FAIL;
155 retval = command_run_line(CMD_CTX, "dap init");
156 if (ERROR_OK != retval)
157 return ERROR_FAIL;
159 LOG_DEBUG("Examining targets...");
160 if (target_examine() != ERROR_OK)
161 LOG_DEBUG("target examination failed");
163 command_context_mode(CMD_CTX, COMMAND_CONFIG);
165 if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
166 return ERROR_FAIL;
168 if (command_run_line(CMD_CTX, "mflash init") != ERROR_OK)
169 return ERROR_FAIL;
171 if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
172 return ERROR_FAIL;
174 if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
175 return ERROR_FAIL;
176 command_context_mode(CMD_CTX, COMMAND_EXEC);
178 /* initialize telnet subsystem */
179 gdb_target_add_all(all_targets);
181 target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
183 return ERROR_OK;
186 COMMAND_HANDLER(handle_add_script_search_dir_command)
188 if (CMD_ARGC != 1)
189 return ERROR_COMMAND_SYNTAX_ERROR;
191 add_script_search_dir(CMD_ARGV[0]);
193 return ERROR_OK;
196 static const struct command_registration openocd_command_handlers[] = {
198 .name = "version",
199 .jim_handler = jim_version_command,
200 .mode = COMMAND_ANY,
201 .help = "show program version",
204 .name = "noinit",
205 .handler = &handle_noinit_command,
206 .mode = COMMAND_CONFIG,
207 .help = "Prevent 'init' from being called at startup.",
208 .usage = ""
211 .name = "init",
212 .handler = &handle_init_command,
213 .mode = COMMAND_ANY,
214 .help = "Initializes configured targets and servers. "
215 "Changes command mode from CONFIG to EXEC. "
216 "Unless 'noinit' is called, this command is "
217 "called automatically at the end of startup.",
218 .usage = ""
221 .name = "add_script_search_dir",
222 .handler = &handle_add_script_search_dir_command,
223 .mode = COMMAND_ANY,
224 .help = "dir to search for config files and scripts",
225 .usage = "<directory>"
227 COMMAND_REGISTRATION_DONE
230 static int openocd_register_commands(struct command_context *cmd_ctx)
232 return register_commands(cmd_ctx, NULL, openocd_command_handlers);
235 struct command_context *global_cmd_ctx;
237 /* NB! this fn can be invoked outside this file for non PC hosted builds
238 * NB! do not change to 'static'!!!!
240 struct command_context *setup_command_handler(Jim_Interp *interp)
242 log_init();
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 &transport_register_commands,
255 &interface_register_commands,
256 &target_register_commands,
257 &flash_register_commands,
258 &nand_register_commands,
259 &pld_register_commands,
260 &mflash_register_commands,
261 &cti_register_commands,
262 &dap_register_commands,
263 NULL
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);
269 return NULL;
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;
279 return 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)
288 int ret;
290 if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
291 return ERROR_FAIL;
293 if (server_preinit() != ERROR_OK)
294 return ERROR_FAIL;
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 */
299 return ERROR_OK;
300 } else if (ret != ERROR_OK) {
301 server_quit(); /* gdb server may be initialized by -c init */
302 return ERROR_FAIL;
305 ret = server_init(cmd_ctx);
306 if (ERROR_OK != ret)
307 return ERROR_FAIL;
309 if (init_at_startup) {
310 ret = command_run_line(cmd_ctx, "init");
311 if (ERROR_OK != ret) {
312 server_quit();
313 return ERROR_FAIL;
317 ret = server_loop(cmd_ctx);
319 int last_signal = server_quit();
320 if (last_signal != ERROR_OK)
321 return last_signal;
323 if (ret != ERROR_OK)
324 return ERROR_FAIL;
325 return 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[])
333 int ret;
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)
341 return EXIT_FAILURE;
343 if (ioutil_init(cmd_ctx) != ERROR_OK)
344 return EXIT_FAILURE;
346 LOG_OUTPUT("For bug reports, read\n\t"
347 "http://openocd.org/doc/doxygen/bugs.html"
348 "\n");
350 command_context_mode(cmd_ctx, COMMAND_CONFIG);
351 command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
353 /* Start the executable meat that can evolve into thread in future. */
354 ret = openocd_thread(argc, argv, cmd_ctx);
356 flash_free_all_banks();
357 gdb_service_free();
358 server_free();
360 unregister_all_commands(cmd_ctx, NULL);
362 /* free all DAP and CTI objects */
363 dap_cleanup_all();
364 arm_cti_cleanup_all();
366 adapter_quit();
368 /* Shutdown commandline interface */
369 command_exit(cmd_ctx);
371 free_config();
373 if (ERROR_FAIL == ret)
374 return EXIT_FAILURE;
375 else if (ERROR_OK != ret)
376 exit_on_signal(ret);
378 return ret;