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.
 
 
 
 
 
 

267 lines
7.8 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_HALTED:
  90. target_arch_state(target);
  91. break;
  92. default:
  93. break;
  94. }
  95. return ERROR_OK;
  96. }
  97. /* OpenOCD can't really handle failure of this command. Patches welcome! :-) */
  98. int handle_init_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  99. {
  100. int retval;
  101. static int initialized=0;
  102. if (initialized)
  103. return ERROR_OK;
  104. initialized=1;
  105. atexit(exit_handler);
  106. if (target_init(cmd_ctx) != ERROR_OK)
  107. return ERROR_FAIL;
  108. LOG_DEBUG("target init complete");
  109. if ((retval=jtag_interface_init(cmd_ctx)) != ERROR_OK)
  110. {
  111. /* we must be able to set up the jtag interface */
  112. return retval;
  113. }
  114. LOG_DEBUG("jtag interface init complete");
  115. /* Try to initialize & examine the JTAG chain at this point, but
  116. * continue startup regardless */
  117. if (jtag_init(cmd_ctx) == ERROR_OK)
  118. {
  119. LOG_DEBUG("jtag init complete");
  120. if (target_examine() == ERROR_OK)
  121. {
  122. LOG_DEBUG("jtag examine complete");
  123. }
  124. }
  125. if (flash_init_drivers(cmd_ctx) != ERROR_OK)
  126. return ERROR_FAIL;
  127. LOG_DEBUG("flash init complete");
  128. if (mflash_init_drivers(cmd_ctx) != ERROR_OK)
  129. return ERROR_FAIL;
  130. LOG_DEBUG("mflash init complete");
  131. if (nand_init(cmd_ctx) != ERROR_OK)
  132. return ERROR_FAIL;
  133. LOG_DEBUG("NAND init complete");
  134. if (pld_init(cmd_ctx) != ERROR_OK)
  135. return ERROR_FAIL;
  136. LOG_DEBUG("pld init complete");
  137. /* initialize tcp server */
  138. server_init();
  139. /* initialize telnet subsystem */
  140. telnet_init("Open On-Chip Debugger");
  141. gdb_init();
  142. tcl_init(); /* allows tcl to just connect without going thru telnet */
  143. target_register_event_callback(log_target_callback_event_handler, cmd_ctx);
  144. return ERROR_OK;
  145. }
  146. command_context_t *global_cmd_ctx;
  147. command_context_t *setup_command_handler(void)
  148. {
  149. command_context_t *cmd_ctx;
  150. global_cmd_ctx = cmd_ctx = command_init();
  151. register_command(cmd_ctx, NULL, "version", handle_version_command,
  152. COMMAND_EXEC, "show OpenOCD version");
  153. /* register subsystem commands */
  154. server_register_commands(cmd_ctx);
  155. telnet_register_commands(cmd_ctx);
  156. gdb_register_commands(cmd_ctx);
  157. tcl_register_commands(cmd_ctx); /* tcl server commands */
  158. log_register_commands(cmd_ctx);
  159. jtag_register_commands(cmd_ctx);
  160. xsvf_register_commands(cmd_ctx);
  161. target_register_commands(cmd_ctx);
  162. flash_register_commands(cmd_ctx);
  163. nand_register_commands(cmd_ctx);
  164. pld_register_commands(cmd_ctx);
  165. mflash_register_commands(cmd_ctx);
  166. if (log_init(cmd_ctx) != ERROR_OK)
  167. {
  168. exit(-1);
  169. }
  170. LOG_DEBUG("log init complete");
  171. LOG_OUTPUT( OPENOCD_VERSION "\n" );
  172. register_command(cmd_ctx, NULL, "init", handle_init_command,
  173. COMMAND_ANY, "initializes target and servers - nop on subsequent invocations");
  174. return cmd_ctx;
  175. }
  176. /* normally this is the main() function entry, but if OpenOCD is linked
  177. * into application, then this fn will not be invoked, but rather that
  178. * application will have it's own implementation of main(). */
  179. int openocd_main(int argc, char *argv[])
  180. {
  181. int ret;
  182. /* initialize commandline interface */
  183. command_context_t *cmd_ctx;
  184. cmd_ctx = setup_command_handler();
  185. LOG_OUTPUT( "\n\nBUGS? Read http://svn.berlios.de/svnroot/repos/openocd/trunk/BUGS\n\n\n");
  186. print_version();
  187. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  188. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  189. if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
  190. return EXIT_FAILURE;
  191. ret = parse_config_file(cmd_ctx);
  192. if ( (ret != ERROR_OK) && (ret != ERROR_COMMAND_CLOSE_CONNECTION) )
  193. return EXIT_FAILURE;
  194. if (ret != ERROR_COMMAND_CLOSE_CONNECTION)
  195. {
  196. command_context_mode(cmd_ctx, COMMAND_EXEC);
  197. if (command_run_line(cmd_ctx, "init")!=ERROR_OK)
  198. return EXIT_FAILURE;
  199. /* handle network connections */
  200. server_loop(cmd_ctx);
  201. }
  202. /* shut server down */
  203. server_quit();
  204. unregister_all_commands(cmd_ctx);
  205. /* free commandline interface */
  206. command_done(cmd_ctx);
  207. return EXIT_SUCCESS;
  208. }