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.
 
 
 
 
 
 

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