Audit and eliminate redundant #include directives in main src/ files.
[fw/openocd] / src / openocd.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜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 "jtag.h"
32 #include "configuration.h"
33 #include "xsvf.h"
34 #include "svf.h"
35 #include "target.h"
36 #include "flash.h"
37 #include "nand.h"
38 #include "pld.h"
39 #include "mflash.h"
40
41 #include "server.h"
42 #include "telnet_server.h"
43 #include "gdb_server.h"
44 #include "tcl_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 " (" PKGBLDDATE ") " RELSTR PKGBLDREV
53
54 static void print_version(void)
55 {
56         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
57         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
58         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
59         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
60         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
61         LOG_OUTPUT("$URL$\n");
62         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
63         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
64         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
65         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
66         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
67 }
68
69 /* Give TELNET a way to find out what version this is */
70 static int handle_version_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
71 {
72         if (argc!=0)
73                 return ERROR_COMMAND_SYNTAX_ERROR;
74
75         command_print(cmd_ctx, OPENOCD_VERSION);
76
77         return ERROR_OK;
78 }
79
80 static void exit_handler(void)
81 {
82         /* close JTAG interface */
83         if (jtag && jtag->quit)
84                 jtag->quit();
85 }
86
87 static int log_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
88 {
89         switch (event)
90         {
91                 case TARGET_EVENT_GDB_START:
92                         target->display=0;
93                         break;
94                 case TARGET_EVENT_GDB_END:
95                         target->display=1;
96                         break;
97                 case TARGET_EVENT_HALTED:
98                         if (target->display)
99                         {
100                                 /* do not display information when debugger caused the halt */
101                                 target_arch_state(target);
102                         }
103                         break;
104                 default:
105                         break;
106         }
107
108         return ERROR_OK;
109 }
110
111 int ioutil_init(struct command_context_s *cmd_ctx);
112
113 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
114 static int handle_init_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
115 {
116
117         if (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         atexit(exit_handler);
128
129         if (target_init(cmd_ctx) != ERROR_OK)
130                 return ERROR_FAIL;
131         LOG_DEBUG("target init complete");
132
133         if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
134         {
135                 /* we must be able to set up the jtag interface */
136                 return retval;
137         }
138         LOG_DEBUG("jtag interface init complete");
139
140         /* Try to initialize & examine the JTAG chain at this point, but
141          * continue startup regardless */
142         if (jtag_init(cmd_ctx) == ERROR_OK)
143         {
144                 LOG_DEBUG("jtag init complete");
145                 if (target_examine() == ERROR_OK)
146                 {
147                         LOG_DEBUG("jtag examine complete");
148                 }
149         }
150
151         if (flash_init_drivers(cmd_ctx) != ERROR_OK)
152                 return ERROR_FAIL;
153         LOG_DEBUG("flash init complete");
154
155         if (mflash_init_drivers(cmd_ctx) != ERROR_OK)
156                 return ERROR_FAIL;
157         LOG_DEBUG("mflash init complete");
158
159         if (nand_init(cmd_ctx) != ERROR_OK)
160                 return ERROR_FAIL;
161         LOG_DEBUG("NAND init complete");
162
163         if (pld_init(cmd_ctx) != ERROR_OK)
164                 return ERROR_FAIL;
165         LOG_DEBUG("pld init complete");
166
167         /* initialize tcp server */
168         server_init();
169
170         /* initialize telnet subsystem */
171         telnet_init("Open On-Chip Debugger");
172         gdb_init();
173         tcl_init(); /* allows tcl to just connect without going thru telnet */
174
175         target_register_event_callback(log_target_callback_event_handler, cmd_ctx);
176
177         return ERROR_OK;
178 }
179
180 command_context_t *global_cmd_ctx;
181
182 /* NB! this fn can be invoked outside this file for non PC hosted builds */
183 command_context_t *setup_command_handler(void)
184 {
185         command_context_t *cmd_ctx;
186
187         global_cmd_ctx = cmd_ctx = command_init();
188
189         register_command(cmd_ctx, NULL, "version", handle_version_command,
190                                          COMMAND_EXEC, "show OpenOCD version");
191
192         /* register subsystem commands */
193         server_register_commands(cmd_ctx);
194         telnet_register_commands(cmd_ctx);
195         gdb_register_commands(cmd_ctx);
196         tcl_register_commands(cmd_ctx); /* tcl server commands */
197         log_register_commands(cmd_ctx);
198         jtag_register_commands(cmd_ctx);
199         xsvf_register_commands(cmd_ctx);
200         svf_register_commands(cmd_ctx);
201         target_register_commands(cmd_ctx);
202         flash_register_commands(cmd_ctx);
203         nand_register_commands(cmd_ctx);
204         pld_register_commands(cmd_ctx);
205         mflash_register_commands(cmd_ctx);
206
207         if (log_init(cmd_ctx) != ERROR_OK)
208         {
209                 exit(-1);
210         }
211         LOG_DEBUG("log init complete");
212
213         LOG_OUTPUT( OPENOCD_VERSION "\n" );
214
215         register_command(cmd_ctx, NULL, "init", handle_init_command,
216                                          COMMAND_ANY, "initializes target and servers - nop on subsequent invocations");
217
218         return cmd_ctx;
219 }
220
221 int httpd_start(void);
222 void httpd_stop(void);
223
224
225 #if !BUILD_HTTPD && !BUILD_ECOSBOARD
226 /* implementations of OpenOCD that uses multithreading needs to know when
227  * OpenOCD is sleeping. No-op in vanilla OpenOCD
228  */
229 void openocd_sleep_prelude(void)
230 {
231 }
232
233 void openocd_sleep_postlude(void)
234 {
235 }
236 #endif
237
238
239 /* normally this is the main() function entry, but if OpenOCD is linked
240  * into application, then this fn will not be invoked, but rather that
241  * application will have it's own implementation of main(). */
242 int openocd_main(int argc, char *argv[])
243 {
244         int ret;
245
246         /* initialize commandline interface */
247         command_context_t *cmd_ctx;
248
249         cmd_ctx = setup_command_handler();
250
251 #if BUILD_IOUTIL
252         if (ioutil_init(cmd_ctx) != ERROR_OK)
253         {
254                 return EXIT_FAILURE;
255         }
256 #endif
257
258         LOG_OUTPUT("\n\nBUGS? Read http://svn.berlios.de/svnroot/repos/openocd/trunk/BUGS\n\n\n");
259
260         print_version();
261
262         command_context_mode(cmd_ctx, COMMAND_CONFIG);
263         command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
264
265         if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
266                 return EXIT_FAILURE;
267
268         ret = parse_config_file(cmd_ctx);
269         if ( (ret != ERROR_OK) && (ret != ERROR_COMMAND_CLOSE_CONNECTION) )
270                 return EXIT_FAILURE;
271
272 #if BUILD_HTTPD
273         if (httpd_start()!=ERROR_OK)
274                 return EXIT_FAILURE;
275 #endif
276
277         if (ret != ERROR_COMMAND_CLOSE_CONNECTION)
278         {
279                 command_context_mode(cmd_ctx, COMMAND_EXEC);
280                 if (command_run_line(cmd_ctx, "init")!=ERROR_OK)
281                         return EXIT_FAILURE;
282
283                 /* handle network connections */
284                 server_loop(cmd_ctx);
285         }
286
287         /* shut server down */
288         server_quit();
289
290 #if BUILD_HTTPD
291         httpd_stop();
292 #endif
293
294         unregister_all_commands(cmd_ctx);
295
296         /* free commandline interface */
297         command_done(cmd_ctx);
298
299
300         return EXIT_SUCCESS;
301 }