- added "init" command. "init" and "reset" at end of startup script is equivalent
[fw/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         static int initialized=0;
89         if (initialized)
90                 return ERROR_OK;
91         
92         initialized=1;
93         
94         command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
95
96         atexit(exit_handler);
97
98         if (jtag_init(cmd_ctx) != ERROR_OK)
99                 return ERROR_FAIL;
100         LOG_DEBUG("jtag init complete");
101
102         if (target_init(cmd_ctx) != ERROR_OK)
103                 return ERROR_FAIL;
104         LOG_DEBUG("target init complete");
105
106         if (flash_init_drivers(cmd_ctx) != ERROR_OK)
107                 return ERROR_FAIL;
108         LOG_DEBUG("flash init complete");
109
110         if (nand_init(cmd_ctx) != ERROR_OK)
111                 return ERROR_FAIL;
112         LOG_DEBUG("NAND init complete");
113
114         if (pld_init(cmd_ctx) != ERROR_OK)
115                 return ERROR_FAIL;
116         LOG_DEBUG("pld init complete");
117
118         /* initialize tcp server */
119         server_init();
120
121         /* initialize telnet subsystem */
122         telnet_init("Open On-Chip Debugger");
123         gdb_init();
124
125         return ERROR_OK;
126 }
127
128
129 /* implementations of OpenOCD that uses multithreading needs to lock OpenOCD while calling
130  * OpenOCD fn's. No-op in vanilla OpenOCD
131  */
132 void lockBigLock()
133 {
134 }
135 void unlockBigLock()
136 {
137 }
138
139
140 int main(int argc, char *argv[])
141 {
142         /* initialize commandline interface */
143         command_context_t *cmd_ctx, *cfg_cmd_ctx;
144         cmd_ctx = command_init();
145
146         register_command(cmd_ctx, NULL, "version", handle_version_command,
147                                          COMMAND_EXEC, "show OpenOCD version");
148         register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG, 
149                         "deprecated - use \"init\" and \"reset\" at end of startup script instead");
150         
151         /* register subsystem commands */
152         server_register_commands(cmd_ctx);
153         telnet_register_commands(cmd_ctx);
154         gdb_register_commands(cmd_ctx);
155         log_register_commands(cmd_ctx);
156         jtag_register_commands(cmd_ctx);
157         interpreter_register_commands(cmd_ctx);
158         xsvf_register_commands(cmd_ctx);
159         target_register_commands(cmd_ctx);
160         flash_register_commands(cmd_ctx);
161         nand_register_commands(cmd_ctx);
162         pld_register_commands(cmd_ctx);
163         
164         if (log_init(cmd_ctx) != ERROR_OK)
165                 return EXIT_FAILURE;
166         LOG_DEBUG("log init complete");
167         
168         LOG_OUTPUT( OPENOCD_VERSION "\n" );
169         
170         
171         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
172         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
173         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
174         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
175         /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
176         LOG_OUTPUT( "$URL$\n");
177         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
178         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
179         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
180         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
181         /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
182
183         register_command(cmd_ctx, NULL, "init", handle_init_command,
184                                          COMMAND_ANY, "initializes target and servers - nop on subsequent invocations");
185
186         cfg_cmd_ctx = copy_command_context(cmd_ctx);
187         cfg_cmd_ctx->mode = COMMAND_CONFIG;
188         command_set_output_handler(cfg_cmd_ctx, configuration_output_handler, NULL);
189         
190         if (parse_cmdline_args(cfg_cmd_ctx, argc, argv) != ERROR_OK)
191                 return EXIT_FAILURE;
192
193         if (parse_config_file(cfg_cmd_ctx) != ERROR_OK)
194                 return EXIT_FAILURE;
195         
196         command_done(cfg_cmd_ctx);
197
198         if (command_run_line(cmd_ctx, "init")!=ERROR_OK)
199                 return EXIT_FAILURE;
200         
201         if (daemon_startup)
202                 command_run_line(cmd_ctx, "reset");
203
204         /* handle network connections */
205         server_loop(cmd_ctx);
206
207         /* shut server down */
208         server_quit();
209
210         /* free commandline interface */
211         command_done(cmd_ctx);
212
213         return EXIT_SUCCESS;
214 }
215