jtag: move prototype of adapter init/quit and speed to adapter.h
[fw/openocd] / 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/adapter.h>
31 #include <jtag/driver.h>
32 #include <jtag/jtag.h>
33 #include <transport/transport.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 (retval != ERROR_OK)
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 (retval != ERROR_OK)
155                 return ERROR_FAIL;
156
157         retval = command_run_line(CMD_CTX, "dap init");
158         if (retval != ERROR_OK)
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 /*
239  * TODO: to be removed after v0.12.0
240  * workaround for syntax change of "expr" in jimtcl 0.81
241  * replace "expr" with openocd version that prints the deprecated msg
242  */
243 struct jim_scriptobj {
244         void *token;
245         Jim_Obj *filename_obj;
246         int len;
247         int subst_flags;
248         int in_use;
249         int firstline;
250         int linenr;
251         int missing;
252 };
253
254 static int jim_expr_command(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
255 {
256         if (argc == 2)
257                 return Jim_EvalExpression(interp, argv[1]);
258
259         if (argc > 2) {
260                 Jim_Obj *obj = Jim_ConcatObj(interp, argc - 1, argv + 1);
261                 Jim_IncrRefCount(obj);
262                 const char *s = Jim_String(obj);
263                 struct jim_scriptobj *script = Jim_GetIntRepPtr(interp->currentScriptObj);
264                 if (interp->currentScriptObj == interp->emptyObj ||
265                                 strcmp(interp->currentScriptObj->typePtr->name, "script") ||
266                                 script->subst_flags ||
267                                 script->filename_obj == interp->emptyObj)
268                         LOG_WARNING("DEPRECATED! use 'expr { %s }' not 'expr %s'", s, s);
269                 else
270                         LOG_WARNING("DEPRECATED! (%s:%d) use 'expr { %s }' not 'expr %s'",
271                                                 Jim_String(script->filename_obj), script->linenr, s, s);
272                 int retcode = Jim_EvalExpression(interp, obj);
273                 Jim_DecrRefCount(interp, obj);
274                 return retcode;
275         }
276
277         Jim_WrongNumArgs(interp, 1, argv, "expression ?...?");
278         return JIM_ERR;
279 }
280
281 static const struct command_registration expr_handler[] = {
282         {
283                 .name = "expr",
284                 .jim_handler = jim_expr_command,
285                 .mode = COMMAND_ANY,
286                 .help = "",
287                 .usage = "",
288         },
289         COMMAND_REGISTRATION_DONE
290 };
291
292 static int workaround_for_jimtcl_expr(struct command_context *cmd_ctx)
293 {
294         return register_commands(cmd_ctx, NULL, expr_handler);
295 }
296
297 struct command_context *global_cmd_ctx;
298
299 static struct command_context *setup_command_handler(Jim_Interp *interp)
300 {
301         log_init();
302         LOG_DEBUG("log_init: complete");
303
304         struct command_context *cmd_ctx = command_init(openocd_startup_tcl, interp);
305
306         /* register subsystem commands */
307         typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
308         static const command_registrant_t command_registrants[] = {
309                 &workaround_for_jimtcl_expr,
310                 &openocd_register_commands,
311                 &server_register_commands,
312                 &gdb_register_commands,
313                 &log_register_commands,
314                 &rtt_server_register_commands,
315                 &transport_register_commands,
316                 &interface_register_commands,
317                 &target_register_commands,
318                 &flash_register_commands,
319                 &nand_register_commands,
320                 &pld_register_commands,
321                 &cti_register_commands,
322                 &dap_register_commands,
323                 &arm_tpiu_swo_register_commands,
324                 NULL
325         };
326         for (unsigned i = 0; command_registrants[i]; i++) {
327                 int retval = (*command_registrants[i])(cmd_ctx);
328                 if (retval != ERROR_OK) {
329                         command_done(cmd_ctx);
330                         return NULL;
331                 }
332         }
333         LOG_DEBUG("command registration: complete");
334
335         LOG_OUTPUT(OPENOCD_VERSION "\n"
336                 "Licensed under GNU GPL v2\n");
337
338         global_cmd_ctx = cmd_ctx;
339
340         return cmd_ctx;
341 }
342
343 /** OpenOCD runtime meat that can become single-thread in future. It parse
344  * commandline, reads configuration, sets up the target and starts server loop.
345  * Commandline arguments are passed into this function from openocd_main().
346  */
347 static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
348 {
349         int ret;
350
351         if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
352                 return ERROR_FAIL;
353
354         if (server_preinit() != ERROR_OK)
355                 return ERROR_FAIL;
356
357         ret = parse_config_file(cmd_ctx);
358         if (ret == ERROR_COMMAND_CLOSE_CONNECTION) {
359                 server_quit(); /* gdb server may be initialized by -c init */
360                 return ERROR_OK;
361         } else if (ret != ERROR_OK) {
362                 server_quit(); /* gdb server may be initialized by -c init */
363                 return ERROR_FAIL;
364         }
365
366         ret = server_init(cmd_ctx);
367         if (ret != ERROR_OK)
368                 return ERROR_FAIL;
369
370         if (init_at_startup) {
371                 ret = command_run_line(cmd_ctx, "init");
372                 if (ret != ERROR_OK) {
373                         server_quit();
374                         return ERROR_FAIL;
375                 }
376         }
377
378         ret = server_loop(cmd_ctx);
379
380         int last_signal = server_quit();
381         if (last_signal != ERROR_OK)
382                 return last_signal;
383
384         if (ret != ERROR_OK)
385                 return ERROR_FAIL;
386         return ERROR_OK;
387 }
388
389 /* normally this is the main() function entry, but if OpenOCD is linked
390  * into application, then this fn will not be invoked, but rather that
391  * application will have it's own implementation of main(). */
392 int openocd_main(int argc, char *argv[])
393 {
394         int ret;
395
396         /* initialize commandline interface */
397         struct command_context *cmd_ctx;
398
399         cmd_ctx = setup_command_handler(NULL);
400
401         if (util_init(cmd_ctx) != ERROR_OK)
402                 return EXIT_FAILURE;
403
404         if (rtt_init() != ERROR_OK)
405                 return EXIT_FAILURE;
406
407         LOG_OUTPUT("For bug reports, read\n\t"
408                 "http://openocd.org/doc/doxygen/bugs.html"
409                 "\n");
410
411         command_context_mode(cmd_ctx, COMMAND_CONFIG);
412         command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
413
414         server_host_os_entry();
415
416         /* Start the executable meat that can evolve into thread in future. */
417         ret = openocd_thread(argc, argv, cmd_ctx);
418
419         flash_free_all_banks();
420         gdb_service_free();
421         arm_tpiu_swo_cleanup_all();
422         server_free();
423
424         unregister_all_commands(cmd_ctx, NULL);
425         help_del_all_commands(cmd_ctx);
426
427         /* free all DAP and CTI objects */
428         arm_cti_cleanup_all();
429         dap_cleanup_all();
430
431         adapter_quit();
432
433         server_host_os_close();
434
435         /* Shutdown commandline interface */
436         command_exit(cmd_ctx);
437
438         rtt_exit();
439         free_config();
440
441         if (ret == ERROR_FAIL)
442                 return EXIT_FAILURE;
443         else if (ret != ERROR_OK)
444                 exit_on_signal(ret);
445
446         return ret;
447 }