]> git.gag.com Git - fw/openocd/blob - src/openocd.c
c2f877648f2acdf2ebf880ab62d54000a15c2bcd
[fw/openocd] / src / openocd.c
1 /***************************************************************************\r
2  *   Copyright (C) 2005 by Dominic Rath                                    *\r
3  *   Dominic.Rath@gmx.de                                                   *\r
4  *                                                                         *\r
5  *   This program is free software; you can redistribute it and/or modify  *\r
6  *   it under the terms of the GNU General Public License as published by  *\r
7  *   the Free Software Foundation; either version 2 of the License, or     *\r
8  *   (at your option) any later version.                                   *\r
9  *                                                                         *\r
10  *   This program is distributed in the hope that it will be useful,       *\r
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *\r
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *\r
13  *   GNU General Public License for more details.                          *\r
14  *                                                                         *\r
15  *   You should have received a copy of the GNU General Public License     *\r
16  *   along with this program; if not, write to the                         *\r
17  *   Free Software Foundation, Inc.,                                       *\r
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *\r
19  ***************************************************************************/\r
20 \r
21 #define OPENOCD_VERSION "Open On-Chip Debugger " VERSION " (" PKGBLDDATE ") svn:" PKGBLDREV\r
22 \r
23 #ifdef HAVE_CONFIG_H\r
24 #include "config.h"\r
25 #endif\r
26 \r
27 #include "log.h"\r
28 #include "types.h"\r
29 #include "jtag.h"\r
30 #include "configuration.h"\r
31 #include "interpreter.h"\r
32 #include "xsvf.h"\r
33 #include "target.h"\r
34 #include "flash.h"\r
35 #include "nand.h"\r
36 #include "pld.h"\r
37 \r
38 #include "command.h"\r
39 #include "server.h"\r
40 #include "telnet_server.h"\r
41 #include "gdb_server.h"\r
42 \r
43 #include <sys/time.h>\r
44 #include <sys/types.h>\r
45 #include <strings.h>\r
46 #include <stdio.h>\r
47 #include <stdlib.h>\r
48 #include <string.h>\r
49 #include <unistd.h>\r
50 #include <errno.h>\r
51 \r
52 /* Give TELNET a way to find out what version this is */\r
53 int handle_version_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)\r
54 {\r
55         command_print(cmd_ctx, OPENOCD_VERSION);\r
56 \r
57         return ERROR_OK;\r
58 }\r
59 \r
60 void exit_handler(void)\r
61 {\r
62         /* close JTAG interface */\r
63         if (jtag && jtag->quit)\r
64                 jtag->quit();\r
65 }\r
66 \r
67 int main(int argc, char *argv[])\r
68 {\r
69         /* initialize commandline interface */\r
70         command_context_t *cmd_ctx, *cfg_cmd_ctx;\r
71         cmd_ctx = command_init();\r
72 \r
73         register_command(cmd_ctx, NULL, "version", handle_version_command,\r
74                                          COMMAND_EXEC, "show OpenOCD version");\r
75 \r
76         /* register subsystem commands */\r
77         server_register_commands(cmd_ctx);\r
78         telnet_register_commands(cmd_ctx);\r
79         gdb_register_commands(cmd_ctx);\r
80         log_register_commands(cmd_ctx);\r
81         jtag_register_commands(cmd_ctx);\r
82         interpreter_register_commands(cmd_ctx);\r
83         xsvf_register_commands(cmd_ctx);\r
84         target_register_commands(cmd_ctx);\r
85         flash_register_commands(cmd_ctx);\r
86         nand_register_commands(cmd_ctx);\r
87         pld_register_commands(cmd_ctx);\r
88         \r
89         if (log_init(cmd_ctx) != ERROR_OK)\r
90                 return EXIT_FAILURE;\r
91         DEBUG("log init complete");\r
92         \r
93         printf( OPENOCD_VERSION );\r
94         printf( "\n$URL$\n");\r
95   \r
96         DEBUG( OPENOCD_VERSION );\r
97         DEBUG( "$URL$");\r
98 \r
99         cfg_cmd_ctx = copy_command_context(cmd_ctx);\r
100         cfg_cmd_ctx->mode = COMMAND_CONFIG;\r
101         command_set_output_handler(cfg_cmd_ctx, configuration_output_handler, NULL);\r
102         \r
103         if (parse_cmdline_args(cfg_cmd_ctx, argc, argv) != ERROR_OK)\r
104                 return EXIT_FAILURE;\r
105 \r
106         if (parse_config_file(cfg_cmd_ctx) != ERROR_OK)\r
107                 return EXIT_FAILURE;\r
108         \r
109         command_done(cfg_cmd_ctx);\r
110 \r
111         command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);\r
112 \r
113         atexit(exit_handler);\r
114 \r
115         if (jtag_init(cmd_ctx) != ERROR_OK)\r
116                 return EXIT_FAILURE;\r
117         DEBUG("jtag init complete");\r
118 \r
119         if (target_init(cmd_ctx) != ERROR_OK)\r
120                 return EXIT_FAILURE;\r
121         DEBUG("target init complete");\r
122 \r
123         if (flash_init_drivers(cmd_ctx) != ERROR_OK)\r
124                 return EXIT_FAILURE;\r
125         DEBUG("flash init complete");\r
126 \r
127         if (nand_init(cmd_ctx) != ERROR_OK)\r
128                 return EXIT_FAILURE;\r
129         DEBUG("NAND init complete");\r
130 \r
131         if (pld_init(cmd_ctx) != ERROR_OK)\r
132                 return EXIT_FAILURE;\r
133         DEBUG("pld init complete");\r
134 \r
135         /* initialize tcp server */\r
136         server_init();\r
137 \r
138         /* initialize telnet subsystem */\r
139         telnet_init("Open On-Chip Debugger");\r
140         gdb_init();\r
141 \r
142         /* call any target resets */\r
143         if (target_init_reset(cmd_ctx) != ERROR_OK)\r
144                 return EXIT_FAILURE;\r
145         DEBUG("target init reset complete");\r
146 \r
147         /* handle network connections */\r
148         server_loop(cmd_ctx);\r
149 \r
150         /* shut server down */\r
151         server_quit();\r
152 \r
153         /* free commandline interface */\r
154         command_done(cmd_ctx);\r
155 \r
156         return EXIT_SUCCESS;\r
157 }\r