You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

387 lines
10 KiB

  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. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "openocd.h"
  28. #include <jtag/driver.h>
  29. #include <jtag/jtag.h>
  30. #include <transport/transport.h>
  31. #include <helper/util.h>
  32. #include <helper/configuration.h>
  33. #include <flash/nor/core.h>
  34. #include <flash/nand/core.h>
  35. #include <pld/pld.h>
  36. #include <target/arm_cti.h>
  37. #include <target/arm_adi_v5.h>
  38. #include <target/arm_tpiu_swo.h>
  39. #include <rtt/rtt.h>
  40. #include <server/server.h>
  41. #include <server/gdb_server.h>
  42. #include <server/rtt_server.h>
  43. #ifdef HAVE_STRINGS_H
  44. #include <strings.h>
  45. #endif
  46. #ifdef PKGBLDDATE
  47. #define OPENOCD_VERSION \
  48. "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
  49. #else
  50. #define OPENOCD_VERSION \
  51. "Open On-Chip Debugger " VERSION RELSTR
  52. #endif
  53. static const char openocd_startup_tcl[] = {
  54. #include "startup_tcl.inc"
  55. 0 /* Terminate with zero */
  56. };
  57. /* Give scripts and TELNET a way to find out what version this is */
  58. static int jim_version_command(Jim_Interp *interp, int argc,
  59. Jim_Obj * const *argv)
  60. {
  61. if (argc > 2)
  62. return JIM_ERR;
  63. const char *str = "";
  64. char *version_str;
  65. version_str = OPENOCD_VERSION;
  66. if (argc == 2)
  67. str = Jim_GetString(argv[1], NULL);
  68. if (strcmp("git", str) == 0)
  69. version_str = GITVERSION;
  70. Jim_SetResult(interp, Jim_NewStringObj(interp, version_str, -1));
  71. return JIM_OK;
  72. }
  73. static int log_target_callback_event_handler(struct target *target,
  74. enum target_event event,
  75. void *priv)
  76. {
  77. switch (event) {
  78. case TARGET_EVENT_GDB_START:
  79. target->verbose_halt_msg = false;
  80. break;
  81. case TARGET_EVENT_GDB_END:
  82. target->verbose_halt_msg = true;
  83. break;
  84. case TARGET_EVENT_HALTED:
  85. if (target->verbose_halt_msg) {
  86. /* do not display information when debugger caused the halt */
  87. target_arch_state(target);
  88. }
  89. break;
  90. default:
  91. break;
  92. }
  93. return ERROR_OK;
  94. }
  95. static bool init_at_startup = true;
  96. COMMAND_HANDLER(handle_noinit_command)
  97. {
  98. if (CMD_ARGC != 0)
  99. return ERROR_COMMAND_SYNTAX_ERROR;
  100. init_at_startup = false;
  101. return ERROR_OK;
  102. }
  103. /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
  104. COMMAND_HANDLER(handle_init_command)
  105. {
  106. if (CMD_ARGC != 0)
  107. return ERROR_COMMAND_SYNTAX_ERROR;
  108. int retval;
  109. static int initialized;
  110. if (initialized)
  111. return ERROR_OK;
  112. initialized = 1;
  113. retval = command_run_line(CMD_CTX, "target init");
  114. if (retval != ERROR_OK)
  115. return ERROR_FAIL;
  116. retval = adapter_init(CMD_CTX);
  117. if (retval != ERROR_OK) {
  118. /* we must be able to set up the debug adapter */
  119. return retval;
  120. }
  121. LOG_DEBUG("Debug Adapter init complete");
  122. /* "transport init" verifies the expected devices are present;
  123. * for JTAG, it checks the list of configured TAPs against
  124. * what's discoverable, possibly with help from the platform's
  125. * JTAG event handlers. (which require COMMAND_EXEC)
  126. */
  127. command_context_mode(CMD_CTX, COMMAND_EXEC);
  128. retval = command_run_line(CMD_CTX, "transport init");
  129. if (retval != ERROR_OK)
  130. return ERROR_FAIL;
  131. retval = command_run_line(CMD_CTX, "dap init");
  132. if (retval != ERROR_OK)
  133. return ERROR_FAIL;
  134. LOG_DEBUG("Examining targets...");
  135. if (target_examine() != ERROR_OK)
  136. LOG_DEBUG("target examination failed");
  137. command_context_mode(CMD_CTX, COMMAND_CONFIG);
  138. if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
  139. return ERROR_FAIL;
  140. if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
  141. return ERROR_FAIL;
  142. if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
  143. return ERROR_FAIL;
  144. command_context_mode(CMD_CTX, COMMAND_EXEC);
  145. /* in COMMAND_EXEC, after target_examine(), only tpiu or only swo */
  146. if (command_run_line(CMD_CTX, "tpiu init") != ERROR_OK)
  147. return ERROR_FAIL;
  148. /* initialize telnet subsystem */
  149. gdb_target_add_all(all_targets);
  150. target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
  151. return ERROR_OK;
  152. }
  153. COMMAND_HANDLER(handle_add_script_search_dir_command)
  154. {
  155. if (CMD_ARGC != 1)
  156. return ERROR_COMMAND_SYNTAX_ERROR;
  157. add_script_search_dir(CMD_ARGV[0]);
  158. return ERROR_OK;
  159. }
  160. static const struct command_registration openocd_command_handlers[] = {
  161. {
  162. .name = "version",
  163. .jim_handler = jim_version_command,
  164. .mode = COMMAND_ANY,
  165. .help = "show program version",
  166. },
  167. {
  168. .name = "noinit",
  169. .handler = &handle_noinit_command,
  170. .mode = COMMAND_CONFIG,
  171. .help = "Prevent 'init' from being called at startup.",
  172. .usage = ""
  173. },
  174. {
  175. .name = "init",
  176. .handler = &handle_init_command,
  177. .mode = COMMAND_ANY,
  178. .help = "Initializes configured targets and servers. "
  179. "Changes command mode from CONFIG to EXEC. "
  180. "Unless 'noinit' is called, this command is "
  181. "called automatically at the end of startup.",
  182. .usage = ""
  183. },
  184. {
  185. .name = "add_script_search_dir",
  186. .handler = &handle_add_script_search_dir_command,
  187. .mode = COMMAND_ANY,
  188. .help = "dir to search for config files and scripts",
  189. .usage = "<directory>"
  190. },
  191. COMMAND_REGISTRATION_DONE
  192. };
  193. static int openocd_register_commands(struct command_context *cmd_ctx)
  194. {
  195. return register_commands(cmd_ctx, NULL, openocd_command_handlers);
  196. }
  197. struct command_context *global_cmd_ctx;
  198. static struct command_context *setup_command_handler(Jim_Interp *interp)
  199. {
  200. log_init();
  201. LOG_DEBUG("log_init: complete");
  202. struct command_context *cmd_ctx = command_init(openocd_startup_tcl, interp);
  203. /* register subsystem commands */
  204. typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
  205. static const command_registrant_t command_registrants[] = {
  206. &openocd_register_commands,
  207. &server_register_commands,
  208. &gdb_register_commands,
  209. &log_register_commands,
  210. &rtt_server_register_commands,
  211. &transport_register_commands,
  212. &interface_register_commands,
  213. &target_register_commands,
  214. &flash_register_commands,
  215. &nand_register_commands,
  216. &pld_register_commands,
  217. &cti_register_commands,
  218. &dap_register_commands,
  219. &arm_tpiu_swo_register_commands,
  220. NULL
  221. };
  222. for (unsigned i = 0; command_registrants[i]; i++) {
  223. int retval = (*command_registrants[i])(cmd_ctx);
  224. if (retval != ERROR_OK) {
  225. command_done(cmd_ctx);
  226. return NULL;
  227. }
  228. }
  229. LOG_DEBUG("command registration: complete");
  230. LOG_OUTPUT(OPENOCD_VERSION "\n"
  231. "Licensed under GNU GPL v2\n");
  232. global_cmd_ctx = cmd_ctx;
  233. return cmd_ctx;
  234. }
  235. /** OpenOCD runtime meat that can become single-thread in future. It parse
  236. * commandline, reads configuration, sets up the target and starts server loop.
  237. * Commandline arguments are passed into this function from openocd_main().
  238. */
  239. static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
  240. {
  241. int ret;
  242. if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
  243. return ERROR_FAIL;
  244. if (server_preinit() != ERROR_OK)
  245. return ERROR_FAIL;
  246. ret = parse_config_file(cmd_ctx);
  247. if (ret == ERROR_COMMAND_CLOSE_CONNECTION) {
  248. server_quit(); /* gdb server may be initialized by -c init */
  249. return ERROR_OK;
  250. } else if (ret != ERROR_OK) {
  251. server_quit(); /* gdb server may be initialized by -c init */
  252. return ERROR_FAIL;
  253. }
  254. ret = server_init(cmd_ctx);
  255. if (ret != ERROR_OK)
  256. return ERROR_FAIL;
  257. if (init_at_startup) {
  258. ret = command_run_line(cmd_ctx, "init");
  259. if (ret != ERROR_OK) {
  260. server_quit();
  261. return ERROR_FAIL;
  262. }
  263. }
  264. ret = server_loop(cmd_ctx);
  265. int last_signal = server_quit();
  266. if (last_signal != ERROR_OK)
  267. return last_signal;
  268. if (ret != ERROR_OK)
  269. return ERROR_FAIL;
  270. return ERROR_OK;
  271. }
  272. /* normally this is the main() function entry, but if OpenOCD is linked
  273. * into application, then this fn will not be invoked, but rather that
  274. * application will have it's own implementation of main(). */
  275. int openocd_main(int argc, char *argv[])
  276. {
  277. int ret;
  278. /* initialize commandline interface */
  279. struct command_context *cmd_ctx;
  280. cmd_ctx = setup_command_handler(NULL);
  281. if (util_init(cmd_ctx) != ERROR_OK)
  282. return EXIT_FAILURE;
  283. if (rtt_init() != ERROR_OK)
  284. return EXIT_FAILURE;
  285. LOG_OUTPUT("For bug reports, read\n\t"
  286. "http://openocd.org/doc/doxygen/bugs.html"
  287. "\n");
  288. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  289. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  290. server_host_os_entry();
  291. /* Start the executable meat that can evolve into thread in future. */
  292. ret = openocd_thread(argc, argv, cmd_ctx);
  293. flash_free_all_banks();
  294. gdb_service_free();
  295. arm_tpiu_swo_cleanup_all();
  296. server_free();
  297. unregister_all_commands(cmd_ctx, NULL);
  298. help_del_all_commands(cmd_ctx);
  299. /* free all DAP and CTI objects */
  300. arm_cti_cleanup_all();
  301. dap_cleanup_all();
  302. adapter_quit();
  303. server_host_os_close();
  304. /* Shutdown commandline interface */
  305. command_exit(cmd_ctx);
  306. rtt_exit();
  307. free_config();
  308. if (ret == ERROR_FAIL)
  309. return EXIT_FAILURE;
  310. else if (ret != ERROR_OK)
  311. exit_on_signal(ret);
  312. return ret;
  313. }