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.
 
 
 
 
 
 

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