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.
 
 
 
 
 
 

292 lines
8.3 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. #define OPENOCD_VERSION "Open On-Chip Debugger " VERSION " (" PKGBLDDATE ") "RELSTR PKGBLDREV
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "log.h"
  31. #include "types.h"
  32. #include "jtag.h"
  33. #include "configuration.h"
  34. #include "xsvf.h"
  35. #include "svf.h"
  36. #include "target.h"
  37. #include "flash.h"
  38. #include "nand.h"
  39. #include "pld.h"
  40. #include "mflash.h"
  41. #include "command.h"
  42. #include "server.h"
  43. #include "telnet_server.h"
  44. #include "gdb_server.h"
  45. #include "tcl_server.h"
  46. #include <sys/time.h>
  47. #include <sys/types.h>
  48. #include <strings.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <unistd.h>
  53. #include <errno.h>
  54. void print_version(void)
  55. {
  56. /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
  57. /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
  58. /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
  59. /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
  60. /* DANGER!!! make sure that the line below does not appear in a patch, do not remove */
  61. LOG_OUTPUT("$URL$\n");
  62. /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
  63. /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
  64. /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
  65. /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
  66. /* DANGER!!! make sure that the line above does not appear in a patch, do not remove */
  67. }
  68. /* Give TELNET a way to find out what version this is */
  69. int handle_version_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  70. {
  71. if (argc!=0)
  72. return ERROR_COMMAND_SYNTAX_ERROR;
  73. command_print(cmd_ctx, OPENOCD_VERSION);
  74. return ERROR_OK;
  75. }
  76. void exit_handler(void)
  77. {
  78. /* close JTAG interface */
  79. if (jtag && jtag->quit)
  80. jtag->quit();
  81. }
  82. static int log_target_callback_event_handler(struct target_s *target, enum target_event event, void *priv)
  83. {
  84. switch (event)
  85. {
  86. case TARGET_EVENT_GDB_START:
  87. target->display=0;
  88. break;
  89. case TARGET_EVENT_GDB_END:
  90. target->display=1;
  91. break;
  92. case TARGET_EVENT_HALTED:
  93. if (target->display)
  94. {
  95. /* do not display information when debugger caused the halt */
  96. target_arch_state(target);
  97. }
  98. break;
  99. default:
  100. break;
  101. }
  102. return ERROR_OK;
  103. }
  104. int ioutil_init(struct command_context_s *cmd_ctx);
  105. /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
  106. int handle_init_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  107. {
  108. if (argc!=0)
  109. return ERROR_COMMAND_SYNTAX_ERROR;
  110. int retval;
  111. static int initialized=0;
  112. if (initialized)
  113. return ERROR_OK;
  114. initialized=1;
  115. atexit(exit_handler);
  116. if (target_init(cmd_ctx) != ERROR_OK)
  117. return ERROR_FAIL;
  118. LOG_DEBUG("target init complete");
  119. if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
  120. {
  121. /* we must be able to set up the jtag interface */
  122. return retval;
  123. }
  124. LOG_DEBUG("jtag interface init complete");
  125. /* Try to initialize & examine the JTAG chain at this point, but
  126. * continue startup regardless */
  127. if (jtag_init(cmd_ctx) == ERROR_OK)
  128. {
  129. LOG_DEBUG("jtag init complete");
  130. if (target_examine() == ERROR_OK)
  131. {
  132. LOG_DEBUG("jtag examine complete");
  133. }
  134. }
  135. if (flash_init_drivers(cmd_ctx) != ERROR_OK)
  136. return ERROR_FAIL;
  137. LOG_DEBUG("flash init complete");
  138. if (mflash_init_drivers(cmd_ctx) != ERROR_OK)
  139. return ERROR_FAIL;
  140. LOG_DEBUG("mflash init complete");
  141. if (nand_init(cmd_ctx) != ERROR_OK)
  142. return ERROR_FAIL;
  143. LOG_DEBUG("NAND init complete");
  144. if (pld_init(cmd_ctx) != ERROR_OK)
  145. return ERROR_FAIL;
  146. LOG_DEBUG("pld init complete");
  147. /* initialize tcp server */
  148. server_init();
  149. /* initialize telnet subsystem */
  150. telnet_init("Open On-Chip Debugger");
  151. gdb_init();
  152. tcl_init(); /* allows tcl to just connect without going thru telnet */
  153. target_register_event_callback(log_target_callback_event_handler, cmd_ctx);
  154. return ERROR_OK;
  155. }
  156. command_context_t *global_cmd_ctx;
  157. command_context_t *setup_command_handler(void)
  158. {
  159. command_context_t *cmd_ctx;
  160. global_cmd_ctx = cmd_ctx = command_init();
  161. register_command(cmd_ctx, NULL, "version", handle_version_command,
  162. COMMAND_EXEC, "show OpenOCD version");
  163. /* register subsystem commands */
  164. server_register_commands(cmd_ctx);
  165. telnet_register_commands(cmd_ctx);
  166. gdb_register_commands(cmd_ctx);
  167. tcl_register_commands(cmd_ctx); /* tcl server commands */
  168. log_register_commands(cmd_ctx);
  169. jtag_register_commands(cmd_ctx);
  170. xsvf_register_commands(cmd_ctx);
  171. svf_register_commands(cmd_ctx);
  172. target_register_commands(cmd_ctx);
  173. flash_register_commands(cmd_ctx);
  174. nand_register_commands(cmd_ctx);
  175. pld_register_commands(cmd_ctx);
  176. mflash_register_commands(cmd_ctx);
  177. if (log_init(cmd_ctx) != ERROR_OK)
  178. {
  179. exit(-1);
  180. }
  181. LOG_DEBUG("log init complete");
  182. LOG_OUTPUT( OPENOCD_VERSION "\n" );
  183. register_command(cmd_ctx, NULL, "init", handle_init_command,
  184. COMMAND_ANY, "initializes target and servers - nop on subsequent invocations");
  185. return cmd_ctx;
  186. }
  187. int httpd_start(void);
  188. void httpd_stop(void);
  189. /* normally this is the main() function entry, but if OpenOCD is linked
  190. * into application, then this fn will not be invoked, but rather that
  191. * application will have it's own implementation of main(). */
  192. int openocd_main(int argc, char *argv[])
  193. {
  194. int ret;
  195. /* initialize commandline interface */
  196. command_context_t *cmd_ctx;
  197. cmd_ctx = setup_command_handler();
  198. #if BUILD_IOUTIL
  199. if (ioutil_init(cmd_ctx) != ERROR_OK)
  200. {
  201. return EXIT_FAILURE;
  202. }
  203. #endif
  204. LOG_OUTPUT("\n\nBUGS? Read http://svn.berlios.de/svnroot/repos/openocd/trunk/BUGS\n\n\n");
  205. print_version();
  206. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  207. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  208. if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
  209. return EXIT_FAILURE;
  210. ret = parse_config_file(cmd_ctx);
  211. if ( (ret != ERROR_OK) && (ret != ERROR_COMMAND_CLOSE_CONNECTION) )
  212. return EXIT_FAILURE;
  213. #if BUILD_HTTPD
  214. if (httpd_start()!=ERROR_OK)
  215. return EXIT_FAILURE;
  216. #endif
  217. if (ret != ERROR_COMMAND_CLOSE_CONNECTION)
  218. {
  219. command_context_mode(cmd_ctx, COMMAND_EXEC);
  220. if (command_run_line(cmd_ctx, "init")!=ERROR_OK)
  221. return EXIT_FAILURE;
  222. /* handle network connections */
  223. server_loop(cmd_ctx);
  224. }
  225. /* shut server down */
  226. server_quit();
  227. #if BUILD_HTTPD
  228. httpd_stop();
  229. #endif
  230. unregister_all_commands(cmd_ctx);
  231. /* free commandline interface */
  232. command_done(cmd_ctx);
  233. return EXIT_SUCCESS;
  234. }