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

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)