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