Richard Missenden exit now works during startup script
[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 #define OPENOCD_VERSION "Open On-Chip Debugger " VERSION " (" PKGBLDDATE ") svn:" PKGBLDREV
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "log.h"
34 #include "types.h"
35 #include "jtag.h"
36 #include "configuration.h"
37 #include "xsvf.h"
38 #include "target.h"
39 #include "flash.h"
40 #include "nand.h"
41 #include "pld.h"
42
43 #include "command.h"
44 #include "server.h"
45 #include "telnet_server.h"
46 #include "gdb_server.h"
47 #include "tcl_server.h"
48
49 #include <sys/time.h>
50 #include <sys/types.h>
51 #include <strings.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <errno.h>
57
58 #ifdef _WIN32
59 #include <malloc.h>
60 #else
61 #include <alloca.h>
62 #endif
63
64 #include "replacements.h"
65
66
67
68
69
70
71 void print_version()
72 {
73         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
74         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
75         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
76         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
77         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
78         LOG_OUTPUT( "$URL$\n");
79         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
80         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
81         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
82         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
83         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
84 }
85
86
87
88
89
90
91
92
93
94 /* Give TELNET a way to find out what version this is */
95 int handle_version_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
96 {
97         command_print(cmd_ctx, OPENOCD_VERSION);
98
99         return ERROR_OK;
100 }
101
102 void exit_handler(void)
103 {
104         /* close JTAG interface */
105         if (jtag && jtag->quit)
106                 jtag->quit();
107 }
108
109 static int log_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
110 {
111         switch (event)
112         {
113                 case TARGET_EVENT_HALTED:
114                         target_arch_state(target);
115                         break;
116                 default:
117                         break;
118         }
119
120         return ERROR_OK;
121 }
122
123 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
124 int handle_init_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
125 {
126         int retval;
127         static int initialized=0;
128         if (initialized)
129                 return ERROR_OK;
130         
131         initialized=1;
132         
133         atexit(exit_handler);
134         
135         if (target_init(cmd_ctx) != ERROR_OK)
136                 return ERROR_FAIL;
137         LOG_DEBUG("target init complete");
138
139         if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
140         {
141                 /* we must be able to set up the jtag interface */
142                 return retval;
143         }
144         LOG_DEBUG("jtag interface init complete");
145
146         /* Try to initialize & examine the JTAG chain at this point, but
147          * continue startup regardless */
148         if (jtag_init(cmd_ctx) == ERROR_OK)
149         {
150                 LOG_DEBUG("jtag init complete");
151                 if (target_examine() == ERROR_OK)
152                 {
153                         LOG_DEBUG("jtag examine complete");
154                 }
155         }
156         
157         if (flash_init_drivers(cmd_ctx) != ERROR_OK)
158                 return ERROR_FAIL;
159         LOG_DEBUG("flash init complete");
160
161         if (nand_init(cmd_ctx) != ERROR_OK)
162                 return ERROR_FAIL;
163         LOG_DEBUG("NAND init complete");
164
165         if (pld_init(cmd_ctx) != ERROR_OK)
166                 return ERROR_FAIL;
167         LOG_DEBUG("pld init complete");
168
169         /* initialize tcp server */
170         server_init();
171
172         /* initialize telnet subsystem */
173         telnet_init("Open On-Chip Debugger");
174         gdb_init();
175         tcl_init(); /* allows tcl to just connect without going thru telnet */
176
177         target_register_event_callback(log_target_callback_event_handler, cmd_ctx);
178         
179         return ERROR_OK;
180 }
181
182 command_context_t *global_cmd_ctx;
183
184 command_context_t *setup_command_handler(void)
185 {
186         command_context_t *cmd_ctx;
187         
188         global_cmd_ctx = cmd_ctx = command_init();
189         
190         register_command(cmd_ctx, NULL, "version", handle_version_command,
191                                          COMMAND_EXEC, "show OpenOCD version");
192         
193         /* register subsystem commands */
194         server_register_commands(cmd_ctx);
195         telnet_register_commands(cmd_ctx);
196         gdb_register_commands(cmd_ctx);
197         tcl_register_commands(cmd_ctx); /* tcl server commands */
198         log_register_commands(cmd_ctx);
199         jtag_register_commands(cmd_ctx);
200         xsvf_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         
206         if (log_init(cmd_ctx) != ERROR_OK)
207         {
208                 exit(-1);
209         }
210         LOG_DEBUG("log init complete");
211
212         LOG_OUTPUT( OPENOCD_VERSION "\n" );
213         
214         register_command(cmd_ctx, NULL, "init", handle_init_command,
215                                          COMMAND_ANY, "initializes target and servers - nop on subsequent invocations");
216
217         return cmd_ctx;
218 }
219
220 /* normally this is the main() function entry, but if OpenOCD is linked
221  * into application, then this fn will not be invoked, but rather that
222  * application will have it's own implementation of main(). */
223 int openocd_main(int argc, char *argv[])
224 {
225         int ret;
226
227         /* initialize commandline interface */
228         command_context_t *cmd_ctx;
229
230         cmd_ctx = setup_command_handler();
231         
232         LOG_OUTPUT( "\n\nBUGS? Read http://svn.berlios.de/svnroot/repos/openocd/trunk/BUGS\n\n\n");
233
234         print_version();
235         
236         command_context_mode(cmd_ctx, COMMAND_CONFIG);
237         command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
238
239         if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
240                 return EXIT_FAILURE;
241         
242         ret = parse_config_file(cmd_ctx);
243         if ( (ret != ERROR_OK) && (ret != ERROR_COMMAND_CLOSE_CONNECTION) )
244                 return EXIT_FAILURE;
245
246         if (ret != ERROR_COMMAND_CLOSE_CONNECTION) 
247         {
248                 command_context_mode(cmd_ctx, COMMAND_EXEC);
249                 if (command_run_line(cmd_ctx, "init")!=ERROR_OK)
250                         return EXIT_FAILURE;
251         
252                 /* handle network connections */
253                 server_loop(cmd_ctx);
254         }
255
256         /* shut server down */
257         server_quit();
258
259         unregister_all_commands(cmd_ctx);
260         
261         /* free commandline interface */
262         command_done(cmd_ctx);
263
264         return EXIT_SUCCESS;
265 }