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