- correct line endings from previous commit
[openocd.git] / src / openocd.c
blobd362c6490ab4c2e8d46cd6768d83f005834df0c3
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 void exit_handler(void)
62 /* close JTAG interface */
63 if (jtag && jtag->quit)
64 jtag->quit();
67 int main(int argc, char *argv[])
69 /* initialize commandline interface */
70 command_context_t *cmd_ctx, *cfg_cmd_ctx;
71 cmd_ctx = command_init();
73 register_command(cmd_ctx, NULL, "version", handle_version_command,
74 COMMAND_EXEC, "show OpenOCD version");
76 /* register subsystem commands */
77 server_register_commands(cmd_ctx);
78 telnet_register_commands(cmd_ctx);
79 gdb_register_commands(cmd_ctx);
80 log_register_commands(cmd_ctx);
81 jtag_register_commands(cmd_ctx);
82 interpreter_register_commands(cmd_ctx);
83 xsvf_register_commands(cmd_ctx);
84 target_register_commands(cmd_ctx);
85 flash_register_commands(cmd_ctx);
86 nand_register_commands(cmd_ctx);
87 pld_register_commands(cmd_ctx);
89 if (log_init(cmd_ctx) != ERROR_OK)
90 return EXIT_FAILURE;
91 DEBUG("log init complete");
93 printf( OPENOCD_VERSION );
94 printf( "\n$URL$\n");
96 DEBUG( OPENOCD_VERSION );
97 DEBUG( "$URL$");
99 cfg_cmd_ctx = copy_command_context(cmd_ctx);
100 cfg_cmd_ctx->mode = COMMAND_CONFIG;
101 command_set_output_handler(cfg_cmd_ctx, configuration_output_handler, NULL);
103 if (parse_cmdline_args(cfg_cmd_ctx, argc, argv) != ERROR_OK)
104 return EXIT_FAILURE;
106 if (parse_config_file(cfg_cmd_ctx) != ERROR_OK)
107 return EXIT_FAILURE;
109 command_done(cfg_cmd_ctx);
111 command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
113 atexit(exit_handler);
115 if (jtag_init(cmd_ctx) != ERROR_OK)
116 return EXIT_FAILURE;
117 DEBUG("jtag init complete");
119 if (target_init(cmd_ctx) != ERROR_OK)
120 return EXIT_FAILURE;
121 DEBUG("target init complete");
123 if (flash_init_drivers(cmd_ctx) != ERROR_OK)
124 return EXIT_FAILURE;
125 DEBUG("flash init complete");
127 if (nand_init(cmd_ctx) != ERROR_OK)
128 return EXIT_FAILURE;
129 DEBUG("NAND init complete");
131 if (pld_init(cmd_ctx) != ERROR_OK)
132 return EXIT_FAILURE;
133 DEBUG("pld init complete");
135 /* initialize tcp server */
136 server_init();
138 /* initialize telnet subsystem */
139 telnet_init("Open On-Chip Debugger");
140 gdb_init();
142 /* call any target resets */
143 if (target_init_reset(cmd_ctx) != ERROR_OK)
144 return EXIT_FAILURE;
145 DEBUG("target init reset complete");
147 /* handle network connections */
148 server_loop(cmd_ctx);
150 /* shut server down */
151 server_quit();
153 /* free commandline interface */
154 command_done(cmd_ctx);
156 return EXIT_SUCCESS;