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.
 
 
 
 
 
 

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