jtag: linuxgpiod: drop extra parenthesis
[openocd.git] / src / openocd.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
6 * *
7 * Copyright (C) 2007-2010 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
9 * *
10 * Copyright (C) 2008 Richard Missenden *
11 * richard.missenden@googlemail.com *
12 ***************************************************************************/
13
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17
18 #include "openocd.h"
19 #include <jtag/adapter.h>
20 #include <jtag/jtag.h>
21 #include <transport/transport.h>
22 #include <helper/util.h>
23 #include <helper/configuration.h>
24 #include <flash/nor/core.h>
25 #include <flash/nand/core.h>
26 #include <pld/pld.h>
27 #include <target/arm_cti.h>
28 #include <target/arm_adi_v5.h>
29 #include <target/arm_tpiu_swo.h>
30 #include <rtt/rtt.h>
31
32 #include <server/server.h>
33 #include <server/gdb_server.h>
34 #include <server/rtt_server.h>
35
36 #ifdef HAVE_STRINGS_H
37 #include <strings.h>
38 #endif
39
40 #ifdef PKGBLDDATE
41 #define OPENOCD_VERSION \
42 "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
43 #else
44 #define OPENOCD_VERSION \
45 "Open On-Chip Debugger " VERSION RELSTR
46 #endif
47
48 static const char openocd_startup_tcl[] = {
49 #include "startup_tcl.inc"
50 0 /* Terminate with zero */
51 };
52
53 /* Give scripts and TELNET a way to find out what version this is */
54 static int jim_version_command(Jim_Interp *interp, int argc,
55 Jim_Obj * const *argv)
56 {
57 if (argc > 2)
58 return JIM_ERR;
59 const char *str = "";
60 char *version_str;
61 version_str = OPENOCD_VERSION;
62
63 if (argc == 2)
64 str = Jim_GetString(argv[1], NULL);
65
66 if (strcmp("git", str) == 0)
67 version_str = GITVERSION;
68
69 Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
70
71 return JIM_OK;
72 }
73
74 static int log_target_callback_event_handler(struct target *target,
75 enum target_event event,
76 void *priv)
77 {
78 switch (event) {
79 case TARGET_EVENT_GDB_START:
80 target->verbose_halt_msg = false;
81 break;
82 case TARGET_EVENT_GDB_END:
83 target->verbose_halt_msg = true;
84 break;
85 case TARGET_EVENT_HALTED:
86 if (target->verbose_halt_msg) {
87 /* do not display information when debugger caused the halt */
88 target_arch_state(target);
89 }
90 break;
91 default:
92 break;
93 }
94
95 return ERROR_OK;
96 }
97
98 static bool init_at_startup = true;
99
100 COMMAND_HANDLER(handle_noinit_command)
101 {
102 if (CMD_ARGC != 0)
103 return ERROR_COMMAND_SYNTAX_ERROR;
104 init_at_startup = false;
105 return ERROR_OK;
106 }
107
108 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
109 COMMAND_HANDLER(handle_init_command)
110 {
111
112 if (CMD_ARGC != 0)
113 return ERROR_COMMAND_SYNTAX_ERROR;
114
115 int retval;
116 static int initialized;
117 if (initialized)
118 return ERROR_OK;
119
120 initialized = 1;
121
122 retval = command_run_line(CMD_CTX, "target init");
123 if (retval != ERROR_OK)
124 return ERROR_FAIL;
125
126 retval = adapter_init(CMD_CTX);
127 if (retval != ERROR_OK) {
128 /* we must be able to set up the debug adapter */
129 return retval;
130 }
131
132 LOG_DEBUG("Debug Adapter init complete");
133
134 /* "transport init" verifies the expected devices are present;
135 * for JTAG, it checks the list of configured TAPs against
136 * what's discoverable, possibly with help from the platform's
137 * JTAG event handlers. (which require COMMAND_EXEC)
138 */
139 command_context_mode(CMD_CTX, COMMAND_EXEC);
140
141 retval = command_run_line(CMD_CTX, "transport init");
142 if (retval != ERROR_OK)
143 return ERROR_FAIL;
144
145 retval = command_run_line(CMD_CTX, "dap init");
146 if (retval != ERROR_OK)
147 return ERROR_FAIL;
148
149 LOG_DEBUG("Examining targets...");
150 if (target_examine() != ERROR_OK)
151 LOG_DEBUG("target examination failed");
152
153 command_context_mode(CMD_CTX, COMMAND_CONFIG);
154
155 if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
156 return ERROR_FAIL;
157
158 if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
159 return ERROR_FAIL;
160
161 if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
162 return ERROR_FAIL;
163 command_context_mode(CMD_CTX, COMMAND_EXEC);
164
165 /* in COMMAND_EXEC, after target_examine(), only tpiu or only swo */
166 if (command_run_line(CMD_CTX, "tpiu init") != ERROR_OK)
167 return ERROR_FAIL;
168
169 /* initialize telnet subsystem */
170 gdb_target_add_all(all_targets);
171
172 target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
173
174 if (command_run_line(CMD_CTX, "_run_post_init_commands") != ERROR_OK)
175 return ERROR_FAIL;
176
177 return ERROR_OK;
178 }
179
180 COMMAND_HANDLER(handle_add_script_search_dir_command)
181 {
182 if (CMD_ARGC != 1)
183 return ERROR_COMMAND_SYNTAX_ERROR;
184
185 add_script_search_dir(CMD_ARGV[0]);
186
187 return ERROR_OK;
188 }
189
190 static const struct command_registration openocd_command_handlers[] = {
191 {
192 .name = "version",
193 .jim_handler = jim_version_command,
194 .mode = COMMAND_ANY,
195 .help = "show program version",
196 },
197 {
198 .name = "noinit",
199 .handler = &handle_noinit_command,
200 .mode = COMMAND_CONFIG,
201 .help = "Prevent 'init' from being called at startup.",
202 .usage = ""
203 },
204 {
205 .name = "init",
206 .handler = &handle_init_command,
207 .mode = COMMAND_ANY,
208 .help = "Initializes configured targets and servers. "
209 "Changes command mode from CONFIG to EXEC. "
210 "Unless 'noinit' is called, this command is "
211 "called automatically at the end of startup.",
212 .usage = ""
213 },
214 {
215 .name = "add_script_search_dir",
216 .handler = &handle_add_script_search_dir_command,
217 .mode = COMMAND_ANY,
218 .help = "dir to search for config files and scripts",
219 .usage = "<directory>"
220 },
221 COMMAND_REGISTRATION_DONE
222 };
223
224 static int openocd_register_commands(struct command_context *cmd_ctx)
225 {
226 return register_commands(cmd_ctx, NULL, openocd_command_handlers);
227 }
228
229 /*
230 * TODO: to be removed after v0.12.0
231 * workaround for syntax change of "expr" in jimtcl 0.81
232 * replace "expr" with openocd version that prints the deprecated msg
233 */
234 struct jim_scriptobj {
235 void *token;
236 Jim_Obj *filename_obj;
237 int len;
238 int subst_flags;
239 int in_use;
240 int firstline;
241 int linenr;
242 int missing;
243 };
244
245 static int jim_expr_command(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
246 {
247 if (argc == 2)
248 return Jim_EvalExpression(interp, argv[1]);
249
250 if (argc > 2) {
251 Jim_Obj *obj = Jim_ConcatObj(interp, argc - 1, argv + 1);
252 Jim_IncrRefCount(obj);
253 const char *s = Jim_String(obj);
254 struct jim_scriptobj *script = Jim_GetIntRepPtr(interp->currentScriptObj);
255 if (interp->currentScriptObj == interp->emptyObj ||
256 strcmp(interp->currentScriptObj->typePtr->name, "script") ||
257 script->subst_flags ||
258 script->filename_obj == interp->emptyObj)
259 LOG_WARNING("DEPRECATED! use 'expr { %s }' not 'expr %s'", s, s);
260 else
261 LOG_WARNING("DEPRECATED! (%s:%d) use 'expr { %s }' not 'expr %s'",
262 Jim_String(script->filename_obj), script->linenr, s, s);
263 int retcode = Jim_EvalExpression(interp, obj);
264 Jim_DecrRefCount(interp, obj);
265 return retcode;
266 }
267
268 Jim_WrongNumArgs(interp, 1, argv, "expression ?...?");
269 return JIM_ERR;
270 }
271
272 static const struct command_registration expr_handler[] = {
273 {
274 .name = "expr",
275 .jim_handler = jim_expr_command,
276 .mode = COMMAND_ANY,
277 .help = "",
278 .usage = "",
279 },
280 COMMAND_REGISTRATION_DONE
281 };
282
283 static int workaround_for_jimtcl_expr(struct command_context *cmd_ctx)
284 {
285 return register_commands(cmd_ctx, NULL, expr_handler);
286 }
287
288 struct command_context *global_cmd_ctx;
289
290 static struct command_context *setup_command_handler(Jim_Interp *interp)
291 {
292 log_init();
293 LOG_DEBUG("log_init: complete");
294
295 struct command_context *cmd_ctx = command_init(openocd_startup_tcl, interp);
296
297 /* register subsystem commands */
298 typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
299 static const command_registrant_t command_registrants[] = {
300 &workaround_for_jimtcl_expr,
301 &openocd_register_commands,
302 &server_register_commands,
303 &gdb_register_commands,
304 &log_register_commands,
305 &rtt_server_register_commands,
306 &transport_register_commands,
307 &adapter_register_commands,
308 &target_register_commands,
309 &flash_register_commands,
310 &nand_register_commands,
311 &pld_register_commands,
312 &cti_register_commands,
313 &dap_register_commands,
314 &arm_tpiu_swo_register_commands,
315 NULL
316 };
317 for (unsigned i = 0; command_registrants[i]; i++) {
318 int retval = (*command_registrants[i])(cmd_ctx);
319 if (retval != ERROR_OK) {
320 command_done(cmd_ctx);
321 return NULL;
322 }
323 }
324 LOG_DEBUG("command registration: complete");
325
326 LOG_OUTPUT(OPENOCD_VERSION "\n"
327 "Licensed under GNU GPL v2\n");
328
329 global_cmd_ctx = cmd_ctx;
330
331 return cmd_ctx;
332 }
333
334 /** OpenOCD runtime meat that can become single-thread in future. It parse
335 * commandline, reads configuration, sets up the target and starts server loop.
336 * Commandline arguments are passed into this function from openocd_main().
337 */
338 static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
339 {
340 int ret;
341
342 if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
343 return ERROR_FAIL;
344
345 if (server_preinit() != ERROR_OK)
346 return ERROR_FAIL;
347
348 ret = parse_config_file(cmd_ctx);
349 if (ret == ERROR_COMMAND_CLOSE_CONNECTION) {
350 server_quit(); /* gdb server may be initialized by -c init */
351 return ERROR_OK;
352 } else if (ret != ERROR_OK) {
353 server_quit(); /* gdb server may be initialized by -c init */
354 return ERROR_FAIL;
355 }
356
357 ret = server_init(cmd_ctx);
358 if (ret != ERROR_OK)
359 return ERROR_FAIL;
360
361 if (init_at_startup) {
362 ret = command_run_line(cmd_ctx, "init");
363 if (ret != ERROR_OK) {
364 server_quit();
365 return ERROR_FAIL;
366 }
367 }
368
369 ret = server_loop(cmd_ctx);
370
371 int last_signal = server_quit();
372 if (last_signal != ERROR_OK)
373 return last_signal;
374
375 if (ret != ERROR_OK)
376 return ERROR_FAIL;
377 return ERROR_OK;
378 }
379
380 /* normally this is the main() function entry, but if OpenOCD is linked
381 * into application, then this fn will not be invoked, but rather that
382 * application will have it's own implementation of main(). */
383 int openocd_main(int argc, char *argv[])
384 {
385 int ret;
386
387 /* initialize commandline interface */
388 struct command_context *cmd_ctx;
389
390 cmd_ctx = setup_command_handler(NULL);
391
392 if (util_init(cmd_ctx) != ERROR_OK)
393 return EXIT_FAILURE;
394
395 if (rtt_init() != ERROR_OK)
396 return EXIT_FAILURE;
397
398 LOG_OUTPUT("For bug reports, read\n\t"
399 "http://openocd.org/doc/doxygen/bugs.html"
400 "\n");
401
402 command_context_mode(cmd_ctx, COMMAND_CONFIG);
403 command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
404
405 server_host_os_entry();
406
407 /* Start the executable meat that can evolve into thread in future. */
408 ret = openocd_thread(argc, argv, cmd_ctx);
409
410 flash_free_all_banks();
411 gdb_service_free();
412 arm_tpiu_swo_cleanup_all();
413 server_free();
414
415 unregister_all_commands(cmd_ctx, NULL);
416 help_del_all_commands(cmd_ctx);
417
418 /* free all DAP and CTI objects */
419 arm_cti_cleanup_all();
420 dap_cleanup_all();
421
422 adapter_quit();
423
424 server_host_os_close();
425
426 /* Shutdown commandline interface */
427 command_exit(cmd_ctx);
428
429 rtt_exit();
430 free_config();
431
432 log_exit();
433
434 if (ret == ERROR_FAIL)
435 return EXIT_FAILURE;
436 else if (ret != ERROR_OK)
437 exit_on_signal(ret);
438
439 return ret;
440 }

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)