target: add Espressif ESP32-S2 basic support
[fw/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/adapter.h>
31 #include <jtag/jtag.h>
32 #include <transport/transport.h>
33 #include <helper/util.h>
34 #include <helper/configuration.h>
35 #include <flash/nor/core.h>
36 #include <flash/nand/core.h>
37 #include <pld/pld.h>
38 #include <target/arm_cti.h>
39 #include <target/arm_adi_v5.h>
40 #include <target/arm_tpiu_swo.h>
41 #include <rtt/rtt.h>
42
43 #include <server/server.h>
44 #include <server/gdb_server.h>
45 #include <server/rtt_server.h>
46
47 #ifdef HAVE_STRINGS_H
48 #include <strings.h>
49 #endif
50
51 #ifdef PKGBLDDATE
52 #define OPENOCD_VERSION \
53         "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
54 #else
55 #define OPENOCD_VERSION \
56         "Open On-Chip Debugger " VERSION RELSTR
57 #endif
58
59 static const char openocd_startup_tcl[] = {
60 #include "startup_tcl.inc"
61 0 /* Terminate with zero */
62 };
63
64 /* Give scripts and TELNET a way to find out what version this is */
65 static int jim_version_command(Jim_Interp *interp, int argc,
66         Jim_Obj * const *argv)
67 {
68         if (argc > 2)
69                 return JIM_ERR;
70         const char *str = "";
71         char *version_str;
72         version_str = OPENOCD_VERSION;
73
74         if (argc == 2)
75                 str = Jim_GetString(argv[1], NULL);
76
77         if (strcmp("git", str) == 0)
78                 version_str = GITVERSION;
79
80         Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
81
82         return JIM_OK;
83 }
84
85 static int log_target_callback_event_handler(struct target *target,
86         enum target_event event,
87         void *priv)
88 {
89         switch (event) {
90                 case TARGET_EVENT_GDB_START:
91                         target->verbose_halt_msg = false;
92                         break;
93                 case TARGET_EVENT_GDB_END:
94                         target->verbose_halt_msg = true;
95                         break;
96                 case TARGET_EVENT_HALTED:
97                         if (target->verbose_halt_msg) {
98                                 /* do not display information when debugger caused the halt */
99                                 target_arch_state(target);
100                         }
101                         break;
102                 default:
103                         break;
104         }
105
106         return ERROR_OK;
107 }
108
109 static bool init_at_startup = true;
110
111 COMMAND_HANDLER(handle_noinit_command)
112 {
113         if (CMD_ARGC != 0)
114                 return ERROR_COMMAND_SYNTAX_ERROR;
115         init_at_startup = false;
116         return ERROR_OK;
117 }
118
119 /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
120 COMMAND_HANDLER(handle_init_command)
121 {
122
123         if (CMD_ARGC != 0)
124                 return ERROR_COMMAND_SYNTAX_ERROR;
125
126         int retval;
127         static int initialized;
128         if (initialized)
129                 return ERROR_OK;
130
131         initialized = 1;
132
133         retval = command_run_line(CMD_CTX, "target init");
134         if (retval != ERROR_OK)
135                 return ERROR_FAIL;
136
137         retval = adapter_init(CMD_CTX);
138         if (retval != ERROR_OK) {
139                 /* we must be able to set up the debug adapter */
140                 return retval;
141         }
142
143         LOG_DEBUG("Debug Adapter init complete");
144
145         /* "transport init" verifies the expected devices are present;
146          * for JTAG, it checks the list of configured TAPs against
147          * what's discoverable, possibly with help from the platform's
148          * JTAG event handlers.  (which require COMMAND_EXEC)
149          */
150         command_context_mode(CMD_CTX, COMMAND_EXEC);
151
152         retval = command_run_line(CMD_CTX, "transport init");
153         if (retval != ERROR_OK)
154                 return ERROR_FAIL;
155
156         retval = command_run_line(CMD_CTX, "dap init");
157         if (retval != ERROR_OK)
158                 return ERROR_FAIL;
159
160         LOG_DEBUG("Examining targets...");
161         if (target_examine() != ERROR_OK)
162                 LOG_DEBUG("target examination failed");
163
164         command_context_mode(CMD_CTX, COMMAND_CONFIG);
165
166         if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
167                 return ERROR_FAIL;
168
169         if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
170                 return ERROR_FAIL;
171
172         if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
173                 return ERROR_FAIL;
174         command_context_mode(CMD_CTX, COMMAND_EXEC);
175
176         /* in COMMAND_EXEC, after target_examine(), only tpiu or only swo */
177         if (command_run_line(CMD_CTX, "tpiu init") != ERROR_OK)
178                 return ERROR_FAIL;
179
180         /* initialize telnet subsystem */
181         gdb_target_add_all(all_targets);
182
183         target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
184
185         if (command_run_line(CMD_CTX, "_run_post_init_commands") != ERROR_OK)
186                 return ERROR_FAIL;
187
188         return ERROR_OK;
189 }
190
191 COMMAND_HANDLER(handle_add_script_search_dir_command)
192 {
193         if (CMD_ARGC != 1)
194                 return ERROR_COMMAND_SYNTAX_ERROR;
195
196         add_script_search_dir(CMD_ARGV[0]);
197
198         return ERROR_OK;
199 }
200
201 static const struct command_registration openocd_command_handlers[] = {
202         {
203                 .name = "version",
204                 .jim_handler = jim_version_command,
205                 .mode = COMMAND_ANY,
206                 .help = "show program version",
207         },
208         {
209                 .name = "noinit",
210                 .handler = &handle_noinit_command,
211                 .mode = COMMAND_CONFIG,
212                 .help = "Prevent 'init' from being called at startup.",
213                 .usage = ""
214         },
215         {
216                 .name = "init",
217                 .handler = &handle_init_command,
218                 .mode = COMMAND_ANY,
219                 .help = "Initializes configured targets and servers.  "
220                         "Changes command mode from CONFIG to EXEC.  "
221                         "Unless 'noinit' is called, this command is "
222                         "called automatically at the end of startup.",
223                 .usage = ""
224         },
225         {
226                 .name = "add_script_search_dir",
227                 .handler = &handle_add_script_search_dir_command,
228                 .mode = COMMAND_ANY,
229                 .help = "dir to search for config files and scripts",
230                 .usage = "<directory>"
231         },
232         COMMAND_REGISTRATION_DONE
233 };
234
235 static int openocd_register_commands(struct command_context *cmd_ctx)
236 {
237         return register_commands(cmd_ctx, NULL, openocd_command_handlers);
238 }
239
240 /*
241  * TODO: to be removed after v0.12.0
242  * workaround for syntax change of "expr" in jimtcl 0.81
243  * replace "expr" with openocd version that prints the deprecated msg
244  */
245 struct jim_scriptobj {
246         void *token;
247         Jim_Obj *filename_obj;
248         int len;
249         int subst_flags;
250         int in_use;
251         int firstline;
252         int linenr;
253         int missing;
254 };
255
256 static int jim_expr_command(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
257 {
258         if (argc == 2)
259                 return Jim_EvalExpression(interp, argv[1]);
260
261         if (argc > 2) {
262                 Jim_Obj *obj = Jim_ConcatObj(interp, argc - 1, argv + 1);
263                 Jim_IncrRefCount(obj);
264                 const char *s = Jim_String(obj);
265                 struct jim_scriptobj *script = Jim_GetIntRepPtr(interp->currentScriptObj);
266                 if (interp->currentScriptObj == interp->emptyObj ||
267                                 strcmp(interp->currentScriptObj->typePtr->name, "script") ||
268                                 script->subst_flags ||
269                                 script->filename_obj == interp->emptyObj)
270                         LOG_WARNING("DEPRECATED! use 'expr { %s }' not 'expr %s'", s, s);
271                 else
272                         LOG_WARNING("DEPRECATED! (%s:%d) use 'expr { %s }' not 'expr %s'",
273                                                 Jim_String(script->filename_obj), script->linenr, s, s);
274                 int retcode = Jim_EvalExpression(interp, obj);
275                 Jim_DecrRefCount(interp, obj);
276                 return retcode;
277         }
278
279         Jim_WrongNumArgs(interp, 1, argv, "expression ?...?");
280         return JIM_ERR;
281 }
282
283 static const struct command_registration expr_handler[] = {
284         {
285                 .name = "expr",
286                 .jim_handler = jim_expr_command,
287                 .mode = COMMAND_ANY,
288                 .help = "",
289                 .usage = "",
290         },
291         COMMAND_REGISTRATION_DONE
292 };
293
294 static int workaround_for_jimtcl_expr(struct command_context *cmd_ctx)
295 {
296         return register_commands(cmd_ctx, NULL, expr_handler);
297 }
298
299 struct command_context *global_cmd_ctx;
300
301 static struct command_context *setup_command_handler(Jim_Interp *interp)
302 {
303         log_init();
304         LOG_DEBUG("log_init: complete");
305
306         struct command_context *cmd_ctx = command_init(openocd_startup_tcl, interp);
307
308         /* register subsystem commands */
309         typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
310         static const command_registrant_t command_registrants[] = {
311                 &workaround_for_jimtcl_expr,
312                 &openocd_register_commands,
313                 &server_register_commands,
314                 &gdb_register_commands,
315                 &log_register_commands,
316                 &rtt_server_register_commands,
317                 &transport_register_commands,
318                 &adapter_register_commands,
319                 &target_register_commands,
320                 &flash_register_commands,
321                 &nand_register_commands,
322                 &pld_register_commands,
323                 &cti_register_commands,
324                 &dap_register_commands,
325                 &arm_tpiu_swo_register_commands,
326                 NULL
327         };
328         for (unsigned i = 0; command_registrants[i]; i++) {
329                 int retval = (*command_registrants[i])(cmd_ctx);
330                 if (retval != ERROR_OK) {
331                         command_done(cmd_ctx);
332                         return NULL;
333                 }
334         }
335         LOG_DEBUG("command registration: complete");
336
337         LOG_OUTPUT(OPENOCD_VERSION "\n"
338                 "Licensed under GNU GPL v2\n");
339
340         global_cmd_ctx = cmd_ctx;
341
342         return cmd_ctx;
343 }
344
345 /** OpenOCD runtime meat that can become single-thread in future. It parse
346  * commandline, reads configuration, sets up the target and starts server loop.
347  * Commandline arguments are passed into this function from openocd_main().
348  */
349 static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
350 {
351         int ret;
352
353         if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
354                 return ERROR_FAIL;
355
356         if (server_preinit() != ERROR_OK)
357                 return ERROR_FAIL;
358
359         ret = parse_config_file(cmd_ctx);
360         if (ret == ERROR_COMMAND_CLOSE_CONNECTION) {
361                 server_quit(); /* gdb server may be initialized by -c init */
362                 return ERROR_OK;
363         } else if (ret != ERROR_OK) {
364                 server_quit(); /* gdb server may be initialized by -c init */
365                 return ERROR_FAIL;
366         }
367
368         ret = server_init(cmd_ctx);
369         if (ret != ERROR_OK)
370                 return ERROR_FAIL;
371
372         if (init_at_startup) {
373                 ret = command_run_line(cmd_ctx, "init");
374                 if (ret != ERROR_OK) {
375                         server_quit();
376                         return ERROR_FAIL;
377                 }
378         }
379
380         ret = server_loop(cmd_ctx);
381
382         int last_signal = server_quit();
383         if (last_signal != ERROR_OK)
384                 return last_signal;
385
386         if (ret != ERROR_OK)
387                 return ERROR_FAIL;
388         return ERROR_OK;
389 }
390
391 /* normally this is the main() function entry, but if OpenOCD is linked
392  * into application, then this fn will not be invoked, but rather that
393  * application will have it's own implementation of main(). */
394 int openocd_main(int argc, char *argv[])
395 {
396         int ret;
397
398         /* initialize commandline interface */
399         struct command_context *cmd_ctx;
400
401         cmd_ctx = setup_command_handler(NULL);
402
403         if (util_init(cmd_ctx) != ERROR_OK)
404                 return EXIT_FAILURE;
405
406         if (rtt_init() != ERROR_OK)
407                 return EXIT_FAILURE;
408
409         LOG_OUTPUT("For bug reports, read\n\t"
410                 "http://openocd.org/doc/doxygen/bugs.html"
411                 "\n");
412
413         command_context_mode(cmd_ctx, COMMAND_CONFIG);
414         command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
415
416         server_host_os_entry();
417
418         /* Start the executable meat that can evolve into thread in future. */
419         ret = openocd_thread(argc, argv, cmd_ctx);
420
421         flash_free_all_banks();
422         gdb_service_free();
423         arm_tpiu_swo_cleanup_all();
424         server_free();
425
426         unregister_all_commands(cmd_ctx, NULL);
427         help_del_all_commands(cmd_ctx);
428
429         /* free all DAP and CTI objects */
430         arm_cti_cleanup_all();
431         dap_cleanup_all();
432
433         adapter_quit();
434
435         server_host_os_close();
436
437         /* Shutdown commandline interface */
438         command_exit(cmd_ctx);
439
440         rtt_exit();
441         free_config();
442
443         log_exit();
444
445         if (ret == ERROR_FAIL)
446                 return EXIT_FAILURE;
447         else if (ret != ERROR_OK)
448                 exit_on_signal(ret);
449
450         return ret;
451 }