4fec563383a3c581a66e2b8b97e6e9ecd4287410
[openocd.git] / src / openocd.c
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 ***************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
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 <target/arm_cti.h>
40 #include <target/arm_adi_v5.h>
41 #include <target/arm_tpiu_swo.h>
42 #include <rtt/rtt.h>
43
44 #include <server/server.h>
45 #include <server/gdb_server.h>
46 #include <server/rtt_server.h>
47
48 #ifdef HAVE_STRINGS_H
49 #include <strings.h>
50 #endif
51
52 #ifdef PKGBLDDATE
53 #define OPENOCD_VERSION \
54 "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
55 #else
56 #define OPENOCD_VERSION \
57 "Open On-Chip Debugger " VERSION RELSTR
58 #endif
59
60 static const char openocd_startup_tcl[] = {
61 #include "startup_tcl.inc"
62 0 /* Terminate with zero */
63 };
64
65 /* Give scripts and TELNET a way to find out what version this is */
66 static int jim_version_command(Jim_Interp *interp, int argc,
67 Jim_Obj * const *argv)
68 {
69 if (argc > 2)
70 return JIM_ERR;
71 const char *str = "";
72 char *version_str;
73 version_str = OPENOCD_VERSION;
74
75 if (argc == 2)
76 str = Jim_GetString(argv[1], NULL);
77
78 if (strcmp("git", str) == 0)
79 version_str = GITVERSION;
80
81 Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
82
83 return JIM_OK;
84 }
85
86 static int log_target_callback_event_handler(struct target *target,
87 enum target_event event,
88 void *priv)
89 {
90 switch (event) {
91 case TARGET_EVENT_GDB_START:
92 target->verbose_halt_msg = false;
93 break;
94 case TARGET_EVENT_GDB_END:
95 target->verbose_halt_msg = true;
96 break;
97 case TARGET_EVENT_HALTED:
98 if (target->verbose_halt_msg) {
99 /* do not display information when debugger caused the halt */
100 target_arch_state(target);
101 }
102 break;
103 default:
104 break;
105 }
106
107 return ERROR_OK;
108 }
109
110 static bool init_at_startup = true;
111
112 COMMAND_HANDLER(handle_noinit_command)
113 {
114 if (CMD_ARGC != 0)
115 return ERROR_COMMAND_SYNTAX_ERROR;
116 init_at_startup = false;
117 return ERROR_OK;
118 }
119
120 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
121 COMMAND_HANDLER(handle_init_command)
122 {
123
124 if (CMD_ARGC != 0)
125 return ERROR_COMMAND_SYNTAX_ERROR;
126
127 int retval;
128 static int initialized;
129 if (initialized)
130 return ERROR_OK;
131
132 initialized = 1;
133
134 retval = command_run_line(CMD_CTX, "target init");
135 if (ERROR_OK != retval)
136 return ERROR_FAIL;
137
138 retval = adapter_init(CMD_CTX);
139 if (retval != ERROR_OK) {
140 /* we must be able to set up the debug adapter */
141 return retval;
142 }
143
144 LOG_DEBUG("Debug Adapter init complete");
145
146 /* "transport init" verifies the expected devices are present;
147 * for JTAG, it checks the list of configured TAPs against
148 * what's discoverable, possibly with help from the platform's
149 * JTAG event handlers. (which require COMMAND_EXEC)
150 */
151 command_context_mode(CMD_CTX, COMMAND_EXEC);
152
153 retval = command_run_line(CMD_CTX, "transport init");
154 if (ERROR_OK != retval)
155 return ERROR_FAIL;
156
157 retval = command_run_line(CMD_CTX, "dap init");
158 if (ERROR_OK != retval)
159 return ERROR_FAIL;
160
161 LOG_DEBUG("Examining targets...");
162 if (target_examine() != ERROR_OK)
163 LOG_DEBUG("target examination failed");
164
165 command_context_mode(CMD_CTX, COMMAND_CONFIG);
166
167 if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
168 return ERROR_FAIL;
169
170 if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
171 return ERROR_FAIL;
172
173 if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
174 return ERROR_FAIL;
175 command_context_mode(CMD_CTX, COMMAND_EXEC);
176
177 /* in COMMAND_EXEC, after target_examine(), only tpiu or only swo */
178 if (command_run_line(CMD_CTX, "tpiu init") != ERROR_OK)
179 return ERROR_FAIL;
180
181 /* initialize telnet subsystem */
182 gdb_target_add_all(all_targets);
183
184 target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
185
186 return ERROR_OK;
187 }
188
189 COMMAND_HANDLER(handle_add_script_search_dir_command)
190 {
191 if (CMD_ARGC != 1)
192 return ERROR_COMMAND_SYNTAX_ERROR;
193
194 add_script_search_dir(CMD_ARGV[0]);
195
196 return ERROR_OK;
197 }
198
199 static const struct command_registration openocd_command_handlers[] = {
200 {
201 .name = "version",
202 .jim_handler = jim_version_command,
203 .mode = COMMAND_ANY,
204 .help = "show program version",
205 },
206 {
207 .name = "noinit",
208 .handler = &handle_noinit_command,
209 .mode = COMMAND_CONFIG,
210 .help = "Prevent 'init' from being called at startup.",
211 .usage = ""
212 },
213 {
214 .name = "init",
215 .handler = &handle_init_command,
216 .mode = COMMAND_ANY,
217 .help = "Initializes configured targets and servers. "
218 "Changes command mode from CONFIG to EXEC. "
219 "Unless 'noinit' is called, this command is "
220 "called automatically at the end of startup.",
221 .usage = ""
222 },
223 {
224 .name = "add_script_search_dir",
225 .handler = &handle_add_script_search_dir_command,
226 .mode = COMMAND_ANY,
227 .help = "dir to search for config files and scripts",
228 .usage = "<directory>"
229 },
230 COMMAND_REGISTRATION_DONE
231 };
232
233 static int openocd_register_commands(struct command_context *cmd_ctx)
234 {
235 return register_commands(cmd_ctx, NULL, openocd_command_handlers);
236 }
237
238 struct command_context *global_cmd_ctx;
239
240 static struct command_context *setup_command_handler(Jim_Interp *interp)
241 {
242 log_init();
243 LOG_DEBUG("log_init: complete");
244
245 struct command_context *cmd_ctx = command_init(openocd_startup_tcl, interp);
246
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 &rtt_server_register_commands,
255 &transport_register_commands,
256 &interface_register_commands,
257 &target_register_commands,
258 &flash_register_commands,
259 &nand_register_commands,
260 &pld_register_commands,
261 &cti_register_commands,
262 &dap_register_commands,
263 &arm_tpiu_swo_register_commands,
264 NULL
265 };
266 for (unsigned i = 0; NULL != command_registrants[i]; i++) {
267 int retval = (*command_registrants[i])(cmd_ctx);
268 if (ERROR_OK != retval) {
269 command_done(cmd_ctx);
270 return NULL;
271 }
272 }
273 LOG_DEBUG("command registration: complete");
274
275 LOG_OUTPUT(OPENOCD_VERSION "\n"
276 "Licensed under GNU GPL v2\n");
277
278 global_cmd_ctx = cmd_ctx;
279
280 return cmd_ctx;
281 }
282
283 /** OpenOCD runtime meat that can become single-thread in future. It parse
284 * commandline, reads configuration, sets up the target and starts server loop.
285 * Commandline arguments are passed into this function from openocd_main().
286 */
287 static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
288 {
289 int ret;
290
291 if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
292 return ERROR_FAIL;
293
294 if (server_preinit() != ERROR_OK)
295 return ERROR_FAIL;
296
297 ret = parse_config_file(cmd_ctx);
298 if (ret == ERROR_COMMAND_CLOSE_CONNECTION) {
299 server_quit(); /* gdb server may be initialized by -c init */
300 return ERROR_OK;
301 } else if (ret != ERROR_OK) {
302 server_quit(); /* gdb server may be initialized by -c init */
303 return ERROR_FAIL;
304 }
305
306 ret = server_init(cmd_ctx);
307 if (ERROR_OK != ret)
308 return ERROR_FAIL;
309
310 if (init_at_startup) {
311 ret = command_run_line(cmd_ctx, "init");
312 if (ERROR_OK != ret) {
313 server_quit();
314 return ERROR_FAIL;
315 }
316 }
317
318 ret = server_loop(cmd_ctx);
319
320 int last_signal = server_quit();
321 if (last_signal != ERROR_OK)
322 return last_signal;
323
324 if (ret != ERROR_OK)
325 return ERROR_FAIL;
326 return ERROR_OK;
327 }
328
329 /* normally this is the main() function entry, but if OpenOCD is linked
330 * into application, then this fn will not be invoked, but rather that
331 * application will have it's own implementation of main(). */
332 int openocd_main(int argc, char *argv[])
333 {
334 int ret;
335
336 /* initialize commandline interface */
337 struct command_context *cmd_ctx;
338
339 cmd_ctx = setup_command_handler(NULL);
340
341 if (util_init(cmd_ctx) != ERROR_OK)
342 return EXIT_FAILURE;
343
344 if (ioutil_init(cmd_ctx) != ERROR_OK)
345 return EXIT_FAILURE;
346
347 if (rtt_init() != ERROR_OK)
348 return EXIT_FAILURE;
349
350 LOG_OUTPUT("For bug reports, read\n\t"
351 "http://openocd.org/doc/doxygen/bugs.html"
352 "\n");
353
354 command_context_mode(cmd_ctx, COMMAND_CONFIG);
355 command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
356
357 server_host_os_entry();
358
359 /* Start the executable meat that can evolve into thread in future. */
360 ret = openocd_thread(argc, argv, cmd_ctx);
361
362 flash_free_all_banks();
363 gdb_service_free();
364 arm_tpiu_swo_cleanup_all();
365 server_free();
366
367 unregister_all_commands(cmd_ctx, NULL);
368
369 /* free all DAP and CTI objects */
370 dap_cleanup_all();
371 arm_cti_cleanup_all();
372
373 adapter_quit();
374
375 server_host_os_close();
376
377 /* Shutdown commandline interface */
378 command_exit(cmd_ctx);
379
380 rtt_exit();
381 free_config();
382
383 if (ERROR_FAIL == ret)
384 return EXIT_FAILURE;
385 else if (ERROR_OK != ret)
386 exit_on_signal(ret);
387
388 return ret;
389 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)