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.
 
 
 
 
 
 

301 lines
8.2 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Ø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, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "openocd.h"
  30. #include <jtag/jtag.h>
  31. #include <helper/ioutil.h>
  32. #include <helper/configuration.h>
  33. #include <xsvf/xsvf.h>
  34. #include <svf/svf.h>
  35. #include <flash/nor/core.h>
  36. #include <flash/nand/core.h>
  37. #include <pld/pld.h>
  38. #include <flash/mflash.h>
  39. #include <server/server.h>
  40. #include <server/gdb_server.h>
  41. #include <server/httpd.h>
  42. #ifdef HAVE_STRINGS_H
  43. #include <strings.h>
  44. #endif
  45. #define OPENOCD_VERSION \
  46. "Open On-Chip Debugger " VERSION RELSTR " (" PKGBLDDATE ")"
  47. /* Give TELNET a way to find out what version this is */
  48. COMMAND_HANDLER(handle_version_command)
  49. {
  50. if (CMD_ARGC != 0)
  51. return ERROR_COMMAND_SYNTAX_ERROR;
  52. command_print(CMD_CTX, OPENOCD_VERSION);
  53. return ERROR_OK;
  54. }
  55. static int log_target_callback_event_handler(struct target *target, enum target_event event, void *priv)
  56. {
  57. switch (event)
  58. {
  59. case TARGET_EVENT_GDB_START:
  60. target->display = 0;
  61. break;
  62. case TARGET_EVENT_GDB_END:
  63. target->display = 1;
  64. break;
  65. case TARGET_EVENT_HALTED:
  66. if (target->display)
  67. {
  68. /* do not display information when debugger caused the halt */
  69. target_arch_state(target);
  70. }
  71. break;
  72. default:
  73. break;
  74. }
  75. return ERROR_OK;
  76. }
  77. static bool init_at_startup = true;
  78. COMMAND_HANDLER(handle_noinit_command)
  79. {
  80. if (CMD_ARGC != 0)
  81. return ERROR_COMMAND_SYNTAX_ERROR;
  82. init_at_startup = false;
  83. return ERROR_OK;
  84. }
  85. /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
  86. COMMAND_HANDLER(handle_init_command)
  87. {
  88. if (CMD_ARGC != 0)
  89. return ERROR_COMMAND_SYNTAX_ERROR;
  90. int retval;
  91. static int initialized = 0;
  92. if (initialized)
  93. return ERROR_OK;
  94. initialized = 1;
  95. retval = command_run_line(CMD_CTX, "target init");
  96. if (ERROR_OK != retval)
  97. return ERROR_FAIL;
  98. if ((retval = jtag_interface_init(CMD_CTX)) != ERROR_OK)
  99. {
  100. /* we must be able to set up the jtag interface */
  101. return retval;
  102. }
  103. LOG_DEBUG("jtag interface init complete");
  104. /* Try to initialize & examine the JTAG chain at this point,
  105. * but continue startup regardless. Note that platforms
  106. * need to be able to provide JTAG event handlers that use
  107. * a variety of JTAG operations in order to do that...
  108. */
  109. command_context_mode(CMD_CTX, COMMAND_EXEC);
  110. if (command_run_line(CMD_CTX, "jtag init") == ERROR_OK)
  111. {
  112. LOG_DEBUG("Examining targets...");
  113. if (target_examine() != ERROR_OK)
  114. LOG_DEBUG("target examination failed");
  115. }
  116. else
  117. LOG_WARNING("jtag initialization failed; try 'jtag init' again.");
  118. command_context_mode(CMD_CTX, COMMAND_CONFIG);
  119. if (command_run_line(CMD_CTX, "flash init") != ERROR_OK)
  120. return ERROR_FAIL;
  121. if (command_run_line(CMD_CTX, "mflash init") != ERROR_OK)
  122. return ERROR_FAIL;
  123. if (command_run_line(CMD_CTX, "nand init") != ERROR_OK)
  124. return ERROR_FAIL;
  125. if (command_run_line(CMD_CTX, "pld init") != ERROR_OK)
  126. return ERROR_FAIL;
  127. command_context_mode(CMD_CTX, COMMAND_EXEC);
  128. /* initialize telnet subsystem */
  129. gdb_target_add_all(all_targets);
  130. target_register_event_callback(log_target_callback_event_handler, CMD_CTX);
  131. return ERROR_OK;
  132. }
  133. static const struct command_registration openocd_command_handlers[] = {
  134. {
  135. .name = "version",
  136. .handler = &handle_version_command,
  137. .mode = COMMAND_ANY,
  138. .help = "show program version",
  139. },
  140. {
  141. .name = "noinit",
  142. .handler = &handle_noinit_command,
  143. .mode = COMMAND_CONFIG,
  144. .help = "Prevent 'init' from being called at startup.",
  145. },
  146. {
  147. .name = "init",
  148. .handler = &handle_init_command,
  149. .mode = COMMAND_ANY,
  150. .help = "Initializes configured targets and servers. "
  151. "Changes command mode from CONFIG to EXEC. "
  152. "Unless 'noinit' is called, this command is "
  153. "called automatically at the end of startup.",
  154. },
  155. COMMAND_REGISTRATION_DONE
  156. };
  157. int openocd_register_commands(struct command_context *cmd_ctx)
  158. {
  159. return register_commands(cmd_ctx, NULL, openocd_command_handlers);
  160. }
  161. struct command_context *global_cmd_ctx;
  162. /* NB! this fn can be invoked outside this file for non PC hosted builds */
  163. struct command_context *setup_command_handler(Jim_Interp *interp)
  164. {
  165. log_init();
  166. LOG_DEBUG("log_init: complete");
  167. const char *startup = openocd_startup_tcl;
  168. struct command_context *cmd_ctx = command_init(startup, interp);
  169. /* register subsystem commands */
  170. typedef int (*command_registrant_t)(struct command_context *cmd_ctx);
  171. command_registrant_t command_registrants[] = {
  172. &openocd_register_commands,
  173. &server_register_commands,
  174. &gdb_register_commands,
  175. &log_register_commands,
  176. &jtag_register_commands,
  177. &xsvf_register_commands,
  178. &svf_register_commands,
  179. &target_register_commands,
  180. &flash_register_commands,
  181. &nand_register_commands,
  182. &pld_register_commands,
  183. &mflash_register_commands,
  184. NULL
  185. };
  186. for (unsigned i = 0; NULL != command_registrants[i]; i++)
  187. {
  188. int retval = (*command_registrants[i])(cmd_ctx);
  189. if (ERROR_OK != retval)
  190. {
  191. command_done(cmd_ctx);
  192. return NULL;
  193. }
  194. }
  195. LOG_DEBUG("command registration: complete");
  196. LOG_OUTPUT(OPENOCD_VERSION "\n");
  197. global_cmd_ctx = cmd_ctx;
  198. return cmd_ctx;
  199. }
  200. /* normally this is the main() function entry, but if OpenOCD is linked
  201. * into application, then this fn will not be invoked, but rather that
  202. * application will have it's own implementation of main(). */
  203. int openocd_main(int argc, char *argv[])
  204. {
  205. int ret;
  206. /* initialize commandline interface */
  207. struct command_context *cmd_ctx;
  208. cmd_ctx = setup_command_handler(NULL);
  209. if (ioutil_init(cmd_ctx) != ERROR_OK)
  210. return EXIT_FAILURE;
  211. LOG_OUTPUT("For bug reports, read\n\t"
  212. "http://openocd.berlios.de/doc/doxygen/bugs.html"
  213. "\n");
  214. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  215. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  216. if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
  217. return EXIT_FAILURE;
  218. if (server_preinit() != ERROR_OK)
  219. return EXIT_FAILURE;
  220. ret = parse_config_file(cmd_ctx);
  221. if (ret != ERROR_OK)
  222. return EXIT_FAILURE;
  223. if (httpd_start(cmd_ctx) != ERROR_OK)
  224. return EXIT_FAILURE;
  225. ret = server_init(cmd_ctx);
  226. if (ERROR_OK != ret)
  227. return EXIT_FAILURE;
  228. if (init_at_startup)
  229. {
  230. ret = command_run_line(cmd_ctx, "init");
  231. if (ERROR_OK != ret)
  232. ret = EXIT_FAILURE;
  233. }
  234. /* handle network connections */
  235. if (ERROR_OK == ret)
  236. server_loop(cmd_ctx);
  237. server_quit();
  238. httpd_stop();
  239. unregister_all_commands(cmd_ctx, NULL);
  240. /* free commandline interface */
  241. command_done(cmd_ctx);
  242. jtag_interface_quit();
  243. return ret;
  244. }