]> git.sur5r.net Git - openocd/blob - src/openocd.c
hooks to enable experimentation with scripting language support. Reduces patch size...
[openocd] / src / openocd.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #define OPENOCD_VERSION "Open On-Chip Debugger " VERSION " (" PKGBLDDATE ") svn:" PKGBLDREV
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "log.h"
28 #include "types.h"
29 #include "jtag.h"
30 #include "configuration.h"
31 #include "interpreter.h"
32 #include "xsvf.h"
33 #include "target.h"
34 #include "flash.h"
35 #include "nand.h"
36 #include "pld.h"
37
38 #include "command.h"
39 #include "server.h"
40 #include "telnet_server.h"
41 #include "gdb_server.h"
42
43 #include <sys/time.h>
44 #include <sys/types.h>
45 #include <strings.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <errno.h>
51
52 /* Give TELNET a way to find out what version this is */
53 int handle_version_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
54 {
55         command_print(cmd_ctx, OPENOCD_VERSION);
56
57         return ERROR_OK;
58 }
59
60 static int daemon_startup = 0;
61
62 int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
63 {
64         if (argc==0)
65                 return ERROR_OK;
66         if (argc > 1 )
67                 return ERROR_COMMAND_SYNTAX_ERROR;
68         
69         daemon_startup = strcmp("reset", args[0])==0;
70         
71         command_print(cmd_ctx, OPENOCD_VERSION);
72
73         return ERROR_OK;
74 }
75
76
77 void exit_handler(void)
78 {
79         /* close JTAG interface */
80         if (jtag && jtag->quit)
81                 jtag->quit();
82 }
83
84
85 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
86 int handle_init_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
87 {
88         int retval;
89         static int initialized=0;
90         if (initialized)
91                 return ERROR_OK;
92         
93         initialized=1;
94         
95         command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
96
97         atexit(exit_handler);
98
99         
100         if (target_init(cmd_ctx) != ERROR_OK)
101                 return ERROR_FAIL;
102         LOG_DEBUG("target init complete");
103
104         if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
105         {
106                 /* we must be able to set up the jtag interface */
107                 return retval;
108         }
109         LOG_DEBUG("jtag interface init complete");
110
111         /* Try to initialize & examine the JTAG chain at this point, but
112          * continue startup regardless
113          */
114         if (jtag_init(cmd_ctx) == ERROR_OK)
115         {
116                 LOG_DEBUG("jtag init complete");
117                 if (target_examine(cmd_ctx) == ERROR_OK)
118                 {
119                         LOG_DEBUG("jtag examine complete");
120                 }
121         }
122
123         
124         if (flash_init_drivers(cmd_ctx) != ERROR_OK)
125                 return ERROR_FAIL;
126         LOG_DEBUG("flash init complete");
127
128         if (nand_init(cmd_ctx) != ERROR_OK)
129                 return ERROR_FAIL;
130         LOG_DEBUG("NAND init complete");
131
132         if (pld_init(cmd_ctx) != ERROR_OK)
133                 return ERROR_FAIL;
134         LOG_DEBUG("pld init complete");
135
136         /* initialize tcp server */
137         server_init();
138
139         /* initialize telnet subsystem */
140         telnet_init("Open On-Chip Debugger");
141         gdb_init();
142
143         return ERROR_OK;
144 }
145
146
147 /* implementations of OpenOCD that uses multithreading needs to lock OpenOCD while calling
148  * OpenOCD fn's. No-op in vanilla OpenOCD
149  */
150 void lockBigLock()
151 {
152 }
153 void unlockBigLock()
154 {
155 }
156
157 /* Hook to add scripting language */
158 int jim_command(command_context_t *context, char *line)
159 {
160         LOG_ERROR("Syntax error");
161         return ERROR_COMMAND_SYNTAX_ERROR;
162 }
163
164 int main(int argc, char *argv[])
165 {
166         /* initialize commandline interface */
167         command_context_t *cmd_ctx, *cfg_cmd_ctx;
168         cmd_ctx = command_init();
169
170         register_command(cmd_ctx, NULL, "version", handle_version_command,
171                                          COMMAND_EXEC, "show OpenOCD version");
172         register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG, 
173                         "deprecated - use \"init\" and \"reset\" at end of startup script instead");
174         
175         /* register subsystem commands */
176         server_register_commands(cmd_ctx);
177         telnet_register_commands(cmd_ctx);
178         gdb_register_commands(cmd_ctx);
179         log_register_commands(cmd_ctx);
180         jtag_register_commands(cmd_ctx);
181         interpreter_register_commands(cmd_ctx);
182         xsvf_register_commands(cmd_ctx);
183         target_register_commands(cmd_ctx);
184         flash_register_commands(cmd_ctx);
185         nand_register_commands(cmd_ctx);
186         pld_register_commands(cmd_ctx);
187         
188         if (log_init(cmd_ctx) != ERROR_OK)
189                 return EXIT_FAILURE;
190         LOG_DEBUG("log init complete");
191         
192         LOG_OUTPUT( OPENOCD_VERSION "\n" );
193         
194         
195         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
196         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
197         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
198         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
199         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
200         LOG_OUTPUT( "$URL$\n");
201         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
202         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
203         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
204         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
205         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
206
207         register_command(cmd_ctx, NULL, "init", handle_init_command,
208                                          COMMAND_ANY, "initializes target and servers - nop on subsequent invocations");
209
210         cfg_cmd_ctx = copy_command_context(cmd_ctx);
211         cfg_cmd_ctx->mode = COMMAND_CONFIG;
212         command_set_output_handler(cfg_cmd_ctx, configuration_output_handler, NULL);
213         
214         if (parse_cmdline_args(cfg_cmd_ctx, argc, argv) != ERROR_OK)
215                 return EXIT_FAILURE;
216
217         if (parse_config_file(cfg_cmd_ctx) != ERROR_OK)
218                 return EXIT_FAILURE;
219         
220         command_done(cfg_cmd_ctx);
221
222         if (command_run_line(cmd_ctx, "init")!=ERROR_OK)
223                 return EXIT_FAILURE;
224         
225         if (daemon_startup)
226                 command_run_line(cmd_ctx, "reset");
227
228         /* handle network connections */
229         server_loop(cmd_ctx);
230
231         /* shut server down */
232         server_quit();
233
234         unregister_all_commands(cmd_ctx);
235         
236         /* free commandline interface */
237         command_done(cmd_ctx);
238
239         return EXIT_SUCCESS;
240 }
241