- updated docs for ft2232_vid_pid command
[openocd.git] / src / openocd.c
blob449736e1615901a75508ccfa39b8fda31639e7d8
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 ***************************************************************************/
21 #define OPENOCD_VERSION "Open On-Chip Debugger " VERSION " (" PKGBLDDATE ") svn:" PKGBLDREV
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
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"
38 #include "command.h"
39 #include "server.h"
40 #include "telnet_server.h"
41 #include "gdb_server.h"
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>
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)
55 command_print(cmd_ctx, OPENOCD_VERSION);
57 return ERROR_OK;
60 static int daemon_startup = 0;
62 int handle_daemon_startup_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
64 if (argc==0)
65 return ERROR_OK;
66 if (argc > 1 )
67 return ERROR_COMMAND_SYNTAX_ERROR;
69 daemon_startup = strcmp("reset", args[0])==0;
71 command_print(cmd_ctx, OPENOCD_VERSION);
73 return ERROR_OK;
77 void exit_handler(void)
79 /* close JTAG interface */
80 if (jtag && jtag->quit)
81 jtag->quit();
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)
88 int retval;
89 static int initialized=0;
90 if (initialized)
91 return ERROR_OK;
93 initialized=1;
95 command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
97 atexit(exit_handler);
100 if (target_init(cmd_ctx) != ERROR_OK)
101 return ERROR_FAIL;
102 LOG_DEBUG("target init complete");
104 if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
106 /* we must be able to set up the jtag interface */
107 return retval;
109 LOG_DEBUG("jtag interface init complete");
111 /* Try to initialize & examine the JTAG chain at this point, but
112 * continue startup regardless
114 if (jtag_init(cmd_ctx) == ERROR_OK)
116 LOG_DEBUG("jtag init complete");
117 if (target_examine(cmd_ctx) == ERROR_OK)
119 LOG_DEBUG("jtag examine complete");
124 if (flash_init_drivers(cmd_ctx) != ERROR_OK)
125 return ERROR_FAIL;
126 LOG_DEBUG("flash init complete");
128 if (nand_init(cmd_ctx) != ERROR_OK)
129 return ERROR_FAIL;
130 LOG_DEBUG("NAND init complete");
132 if (pld_init(cmd_ctx) != ERROR_OK)
133 return ERROR_FAIL;
134 LOG_DEBUG("pld init complete");
136 /* initialize tcp server */
137 server_init();
139 /* initialize telnet subsystem */
140 telnet_init("Open On-Chip Debugger");
141 gdb_init();
143 return ERROR_OK;
147 /* implementations of OpenOCD that uses multithreading needs to lock OpenOCD while calling
148 * OpenOCD fn's. No-op in vanilla OpenOCD
150 void lockBigLock()
153 void unlockBigLock()
158 int main(int argc, char *argv[])
160 /* initialize commandline interface */
161 command_context_t *cmd_ctx, *cfg_cmd_ctx;
162 cmd_ctx = command_init();
164 register_command(cmd_ctx, NULL, "version", handle_version_command,
165 COMMAND_EXEC, "show OpenOCD version");
166 register_command(cmd_ctx, NULL, "daemon_startup", handle_daemon_startup_command, COMMAND_CONFIG,
167 "deprecated - use \"init\" and \"reset\" at end of startup script instead");
169 /* register subsystem commands */
170 server_register_commands(cmd_ctx);
171 telnet_register_commands(cmd_ctx);
172 gdb_register_commands(cmd_ctx);
173 log_register_commands(cmd_ctx);
174 jtag_register_commands(cmd_ctx);
175 interpreter_register_commands(cmd_ctx);
176 xsvf_register_commands(cmd_ctx);
177 target_register_commands(cmd_ctx);
178 flash_register_commands(cmd_ctx);
179 nand_register_commands(cmd_ctx);
180 pld_register_commands(cmd_ctx);
182 if (log_init(cmd_ctx) != ERROR_OK)
183 return EXIT_FAILURE;
184 LOG_DEBUG("log init complete");
186 LOG_OUTPUT( OPENOCD_VERSION "\n" );
189 /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
190 /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
191 /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
192 /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
193 /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
194 LOG_OUTPUT( "$URL$\n");
195 /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
196 /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
197 /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
198 /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
199 /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
201 register_command(cmd_ctx, NULL, "init", handle_init_command,
202 COMMAND_ANY, "initializes target and servers - nop on subsequent invocations");
204 cfg_cmd_ctx = copy_command_context(cmd_ctx);
205 cfg_cmd_ctx->mode = COMMAND_CONFIG;
206 command_set_output_handler(cfg_cmd_ctx, configuration_output_handler, NULL);
208 if (parse_cmdline_args(cfg_cmd_ctx, argc, argv) != ERROR_OK)
209 return EXIT_FAILURE;
211 if (parse_config_file(cfg_cmd_ctx) != ERROR_OK)
212 return EXIT_FAILURE;
214 command_done(cfg_cmd_ctx);
216 if (command_run_line(cmd_ctx, "init")!=ERROR_OK)
217 return EXIT_FAILURE;
219 if (daemon_startup)
220 command_run_line(cmd_ctx, "reset");
222 /* handle network connections */
223 server_loop(cmd_ctx);
225 /* shut server down */
226 server_quit();
228 /* free commandline interface */
229 command_done(cmd_ctx);
231 return EXIT_SUCCESS;