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.
 
 
 
 
 
 

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