]> git.sur5r.net Git - openocd/blob - src/openocd.c
jtag/drivers/openjtag: Add support for Cypress CY7C65215
[openocd] / src / openocd.c
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007-2010 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   Copyright (C) 2008 Richard Missenden                                  *
9  *   richard.missenden@googlemail.com                                      *
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  *   This program is distributed in the hope that it will be useful,       *
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19  *   GNU General Public License for more details.                          *
20  *                                                                         *
21  *   You should have received a copy of the GNU General Public License     *
22  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
23  ***************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "openocd.h"
30 #include <jtag/driver.h>
31 #include <jtag/jtag.h>
32 #include <transport/transport.h>
33 #include <helper/ioutil.h>
34 #include <helper/util.h>
35 #include <helper/configuration.h>
36 #include <flash/nor/core.h>
37 #include <flash/nand/core.h>
38 #include <pld/pld.h>
39 #include <flash/mflash.h>
40
41 #include <server/server.h>
42 #include <server/gdb_server.h>
43
44 #ifdef HAVE_STRINGS_H
45 #include <strings.h>
46 #endif
47
48 #define OPENOCD_VERSION \
49         "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
50
51 static const char openocd_startup_tcl[] = {
52 #include "startup_tcl.inc"
53 0 /* Terminate with zero */
54 };
55
56 /* Give scripts and TELNET a way to find out what version this is */
57 static int jim_version_command(Jim_Interp *interp, int argc,
58         Jim_Obj * const *argv)
59 {
60         if (argc > 2)
61                 return JIM_ERR;
62         const char *str = "";
63         char *version_str;
64         version_str = OPENOCD_VERSION;
65
66         if (argc == 2)
67                 str = Jim_GetString(argv[1], NULL);
68
69         if (strcmp("git", str) == 0)
70                 version_str = GITVERSION;
71
72         Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
73
74         return JIM_OK;
75 }
76
77 static int log_target_callback_event_handler(struct target *target,
78         enum target_event event,
79         void *priv)
80 {
81         switch (event) {
82                 case TARGET_EVENT_GDB_START:
83                         target->display = 0;
84                         break;
85                 case TARGET_EVENT_GDB_END:
86                         target->display = 1;
87                         break;
88                 case TARGET_EVENT_HALTED:
89                         if (target->display) {
90                                 /* do not display information when debugger caused the halt */
91                                 target_arch_state(target);
92                         }
93                         break;
94                 default:
95                         break;
96         }
97
98         return ERROR_OK;
99 }
100
101 static bool init_at_startup = true;
102
103 COMMAND_HANDLER(handle_noinit_command)
104 {
105         if (CMD_ARGC != 0)
106                 return ERROR_COMMAND_SYNTAX_ERROR;
107         init_at_startup = false;
108         return ERROR_OK;
109 }
110
111 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
112 COMMAND_HANDLER(handle_init_command)
113 {
114
115         if (CMD_ARGC != 0)
116                 return ERROR_COMMAND_SYNTAX_ERROR;
117
118         int retval;
119         static int initialized;
120         if (initialized)
121                 return ERROR_OK;
122
123         initialized = 1;
124
125         retval = command_run_line(CMD_CTX, "target init");
126         if (ERROR_OK != retval)
127                 return ERROR_FAIL;
128
129         retval = adapter_init(CMD_CTX);
130         if (retval != ERROR_OK) {
131                 /* we must be able to set up the debug adapter */
132                 return retval;
133         }
134
135         LOG_DEBUG("Debug Adapter init complete");
136
137         /* "transport init" verifies the expected devices are present;
138          * for JTAG, it checks the list of configured TAPs against
139          * what's discoverable, possibly with help from the platform's
140          * JTAG event handlers.  (which require COMMAND_EXEC)
141          */
142         command_context_mode(CMD_CTX, COMMAND_EXEC);
143
144         retval = command_run_line(CMD_CTX, "transport init");
145         if (ERROR_OK != retval)
146                 return ERROR_FAIL;
147
148         LOG_DEBUG("Examining targets...");
149         if (target_examine() != ERROR_OK)
150                 LOG_DEBUG("target examination failed");
151
152         command_context_mode(CMD_CTX, COMMAND_CONFIG);
153
154         if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
155                 return ERROR_FAIL;
156
157         if (command_run_line(CMD_CTX, "mflash init") != ERROR_OK)
158                 return ERROR_FAIL;
159
160         if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
161                 return ERROR_FAIL;
162
163         if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
164                 return ERROR_FAIL;
165         command_context_mode(CMD_CTX, COMMAND_EXEC);
166
167         /* initialize telnet subsystem */
168         gdb_target_add_all(all_targets);
169
170         target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
171
172         return ERROR_OK;
173 }
174
175 COMMAND_HANDLER(handle_add_script_search_dir_command)
176 {
177         if (CMD_ARGC != 1)
178                 return ERROR_COMMAND_SYNTAX_ERROR;
179
180         add_script_search_dir(CMD_ARGV[0]);
181
182         return ERROR_OK;
183 }
184
185 static const struct command_registration openocd_command_handlers[] = {
186         {
187                 .name = "version",
188                 .jim_handler = jim_version_command,
189                 .mode = COMMAND_ANY,
190                 .help = "show program version",
191         },
192         {
193                 .name = "noinit",
194                 .handler = &handle_noinit_command,
195                 .mode = COMMAND_CONFIG,
196                 .help = "Prevent 'init' from being called at startup.",
197                 .usage = ""
198         },
199         {
200                 .name = "init",
201                 .handler = &handle_init_command,
202                 .mode = COMMAND_ANY,
203                 .help = "Initializes configured targets and servers.  "
204                         "Changes command mode from CONFIG to EXEC.  "
205                         "Unless 'noinit' is called, this command is "
206                         "called automatically at the end of startup.",
207                 .usage = ""
208         },
209         {
210                 .name = "add_script_search_dir",
211                 .handler = &handle_add_script_search_dir_command,
212                 .mode = COMMAND_ANY,
213                 .help = "dir to search for config files and scripts",
214                 .usage = "<directory>"
215         },
216         COMMAND_REGISTRATION_DONE
217 };
218
219 static int openocd_register_commands(struct command_context *cmd_ctx)
220 {
221         return register_commands(cmd_ctx, NULL, openocd_command_handlers);
222 }
223
224 struct command_context *global_cmd_ctx;
225
226 /* NB! this fn can be invoked outside this file for non PC hosted builds
227  * NB! do not change to 'static'!!!!
228  */
229 struct command_context *setup_command_handler(Jim_Interp *interp)
230 {
231         log_init();
232         LOG_DEBUG("log_init: complete");
233
234         struct command_context *cmd_ctx = command_init(openocd_startup_tcl, interp);
235
236         /* register subsystem commands */
237         typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
238         static const command_registrant_t command_registrants[] = {
239                 &openocd_register_commands,
240                 &server_register_commands,
241                 &gdb_register_commands,
242                 &log_register_commands,
243                 &transport_register_commands,
244                 &interface_register_commands,
245                 &target_register_commands,
246                 &flash_register_commands,
247                 &nand_register_commands,
248                 &pld_register_commands,
249                 &mflash_register_commands,
250                 NULL
251         };
252         for (unsigned i = 0; NULL != command_registrants[i]; i++) {
253                 int retval = (*command_registrants[i])(cmd_ctx);
254                 if (ERROR_OK != retval) {
255                         command_done(cmd_ctx);
256                         return NULL;
257                 }
258         }
259         LOG_DEBUG("command registration: complete");
260
261         LOG_OUTPUT(OPENOCD_VERSION "\n"
262                 "Licensed under GNU GPL v2\n");
263
264         global_cmd_ctx = cmd_ctx;
265
266         return cmd_ctx;
267 }
268
269 /** OpenOCD runtime meat that can become single-thread in future. It parse
270  * commandline, reads configuration, sets up the target and starts server loop.
271  * Commandline arguments are passed into this function from openocd_main().
272  */
273 static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
274 {
275         int ret;
276
277         if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
278                 return ERROR_FAIL;
279
280         if (server_preinit() != ERROR_OK)
281                 return ERROR_FAIL;
282
283         ret = parse_config_file(cmd_ctx);
284         if (ret == ERROR_COMMAND_CLOSE_CONNECTION)
285                 return ERROR_OK;
286         else if (ret != ERROR_OK)
287                 return ERROR_FAIL;
288
289         ret = server_init(cmd_ctx);
290         if (ERROR_OK != ret)
291                 return ERROR_FAIL;
292
293         if (init_at_startup) {
294                 ret = command_run_line(cmd_ctx, "init");
295                 if (ERROR_OK != ret)
296                         return ERROR_FAIL;
297         }
298
299         ret = server_loop(cmd_ctx);
300
301         int last_signal = server_quit();
302         if (last_signal != ERROR_OK)
303                 return last_signal;
304
305         if (ret != ERROR_OK)
306                 return ERROR_FAIL;
307         return ERROR_OK;
308 }
309
310 /* normally this is the main() function entry, but if OpenOCD is linked
311  * into application, then this fn will not be invoked, but rather that
312  * application will have it's own implementation of main(). */
313 int openocd_main(int argc, char *argv[])
314 {
315         int ret;
316
317         /* initialize commandline interface */
318         struct command_context *cmd_ctx;
319
320         cmd_ctx = setup_command_handler(NULL);
321
322         if (util_init(cmd_ctx) != ERROR_OK)
323                 return EXIT_FAILURE;
324
325         if (ioutil_init(cmd_ctx) != ERROR_OK)
326                 return EXIT_FAILURE;
327
328         LOG_OUTPUT("For bug reports, read\n\t"
329                 "http://openocd.org/doc/doxygen/bugs.html"
330                 "\n");
331
332         command_context_mode(cmd_ctx, COMMAND_CONFIG);
333         command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
334
335         /* Start the executable meat that can evolve into thread in future. */
336         ret = openocd_thread(argc, argv, cmd_ctx);
337
338         unregister_all_commands(cmd_ctx, NULL);
339
340         /* free commandline interface */
341         command_done(cmd_ctx);
342
343         adapter_quit();
344
345         if (ERROR_FAIL == ret)
346                 return EXIT_FAILURE;
347         else if (ERROR_OK != ret)
348                 exit_on_signal(ret);
349
350         return ret;
351 }