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.
 
 
 
 
 
 

381 lines
10 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 int jim_stacktrace_command(Jim_Interp *interp, int argc,
  151. Jim_Obj * const *argv)
  152. {
  153. if (argc != 1)
  154. {
  155. return JIM_ERR;
  156. }
  157. Jim_Obj * stacktrace = Jim_DuplicateObj(interp, interp->stackTrace);
  158. /* insert actual error site at beginning of list*/
  159. Jim_Obj *procname = Jim_NewStringObj(interp, "", -1); /* Uhhh... don't know this one. */
  160. Jim_ListInsertElements(interp, stacktrace, 0, 1, &procname);
  161. Jim_Obj *filename = Jim_NewStringObj(interp, interp->errorFileName, -1);
  162. Jim_ListInsertElements(interp, stacktrace, 1, 1, &filename);
  163. Jim_Obj *line = Jim_NewIntObj(interp, interp->errorLine);
  164. Jim_ListInsertElements(interp, stacktrace, 2, 1, &line);
  165. Jim_SetResult(interp, stacktrace);
  166. return JIM_OK;
  167. }
  168. static const struct command_registration openocd_command_handlers[] = {
  169. {
  170. .name = "version",
  171. .jim_handler = jim_version_command,
  172. .mode = COMMAND_ANY,
  173. .help = "show program version",
  174. },
  175. {
  176. .name = "noinit",
  177. .handler = &handle_noinit_command,
  178. .mode = COMMAND_CONFIG,
  179. .help = "Prevent 'init' from being called at startup.",
  180. },
  181. {
  182. .name = "init",
  183. .handler = &handle_init_command,
  184. .mode = COMMAND_ANY,
  185. .help = "Initializes configured targets and servers. "
  186. "Changes command mode from CONFIG to EXEC. "
  187. "Unless 'noinit' is called, this command is "
  188. "called automatically at the end of startup.",
  189. },
  190. {
  191. .name = "add_script_search_dir",
  192. .handler = &handle_add_script_search_dir_command,
  193. .mode = COMMAND_ANY,
  194. .help = "dir to search for config files and scripts",
  195. },
  196. {
  197. .name = "stacktrace",
  198. .jim_handler = jim_stacktrace_command,
  199. .mode = COMMAND_ANY,
  200. .help = "returns the stacktrace as a list of triples: proc, file, line."
  201. "The stack trace is reset when a new stack trace is being built after "
  202. "a new failure has occurred.",
  203. },
  204. COMMAND_REGISTRATION_DONE
  205. };
  206. static int openocd_register_commands(struct command_context *cmd_ctx)
  207. {
  208. return register_commands(cmd_ctx, NULL, openocd_command_handlers);
  209. }
  210. struct command_context *global_cmd_ctx;
  211. /* NB! this fn can be invoked outside this file for non PC hosted builds
  212. * NB! do not change to 'static'!!!!
  213. */
  214. struct command_context *setup_command_handler(Jim_Interp *interp)
  215. {
  216. log_init();
  217. LOG_DEBUG("log_init: complete");
  218. const char *startup = openocd_startup_tcl;
  219. struct command_context *cmd_ctx = command_init(startup, interp);
  220. /* register subsystem commands */
  221. typedef int (*command_registrant_t)(struct command_context *cmd_ctx_value);
  222. static const command_registrant_t command_registrants[] = {
  223. &openocd_register_commands,
  224. &server_register_commands,
  225. &gdb_register_commands,
  226. &log_register_commands,
  227. &transport_register_commands,
  228. &interface_register_commands,
  229. &target_register_commands,
  230. &flash_register_commands,
  231. &nand_register_commands,
  232. &pld_register_commands,
  233. &mflash_register_commands,
  234. NULL
  235. };
  236. for (unsigned i = 0; NULL != command_registrants[i]; i++)
  237. {
  238. int retval = (*command_registrants[i])(cmd_ctx);
  239. if (ERROR_OK != retval)
  240. {
  241. command_done(cmd_ctx);
  242. return NULL;
  243. }
  244. }
  245. LOG_DEBUG("command registration: complete");
  246. LOG_OUTPUT(OPENOCD_VERSION "\n"
  247. "Licensed under GNU GPL v2\n");
  248. global_cmd_ctx = cmd_ctx;
  249. return cmd_ctx;
  250. }
  251. /** OpenOCD runtime meat that can become single-thread in future. It parse
  252. * commandline, reads configuration, sets up the target and starts server loop.
  253. * Commandline arguments are passed into this function from openocd_main().
  254. */
  255. static int openocd_thread(int argc, char *argv[], struct command_context *cmd_ctx)
  256. {
  257. int ret;
  258. if (parse_cmdline_args(cmd_ctx, argc, argv) != ERROR_OK)
  259. return EXIT_FAILURE;
  260. if (server_preinit() != ERROR_OK)
  261. return EXIT_FAILURE;
  262. ret = parse_config_file(cmd_ctx);
  263. if (ret != ERROR_OK)
  264. return EXIT_FAILURE;
  265. ret = server_init(cmd_ctx);
  266. if (ERROR_OK != ret)
  267. return EXIT_FAILURE;
  268. ret = command_run_line(cmd_ctx, "init_targets");
  269. if (ERROR_OK != ret)
  270. ret = EXIT_FAILURE;
  271. if (init_at_startup)
  272. {
  273. ret = command_run_line(cmd_ctx, "init");
  274. if (ERROR_OK != ret)
  275. return EXIT_FAILURE;
  276. }
  277. server_loop(cmd_ctx);
  278. server_quit();
  279. return ret;
  280. }
  281. /* normally this is the main() function entry, but if OpenOCD is linked
  282. * into application, then this fn will not be invoked, but rather that
  283. * application will have it's own implementation of main(). */
  284. int openocd_main(int argc, char *argv[])
  285. {
  286. int ret;
  287. /* initialize commandline interface */
  288. struct command_context *cmd_ctx;
  289. cmd_ctx = setup_command_handler(NULL);
  290. if (util_init(cmd_ctx) != ERROR_OK)
  291. return EXIT_FAILURE;
  292. if (ioutil_init(cmd_ctx) != ERROR_OK)
  293. return EXIT_FAILURE;
  294. LOG_OUTPUT("For bug reports, read\n\t"
  295. "http://openocd.berlios.de/doc/doxygen/bugs.html"
  296. "\n");
  297. command_context_mode(cmd_ctx, COMMAND_CONFIG);
  298. command_set_output_handler(cmd_ctx, configuration_output_handler, NULL);
  299. /* Start the executable meat that can evolve into thread in future. */
  300. ret = openocd_thread(argc, argv, cmd_ctx);
  301. unregister_all_commands(cmd_ctx, NULL);
  302. /* free commandline interface */
  303. command_done(cmd_ctx);
  304. adapter_quit();
  305. return ret;
  306. }