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.
 
 
 
 
 
 

371 lines
9.7 KiB

  1. /*
  2. * Copyright (c) 2010 by David Brownell
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software Foundation,
  14. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. /** @file
  20. * Infrastructure for specifying and managing the transport protocol
  21. * used in a given debug or programming session.
  22. *
  23. * Examples of "debug-capable" transports are JTAG or SWD.
  24. * Additionally, JTAG supports boundary scan testing.
  25. *
  26. * Examples of "programming-capable" transports include SPI or UART;
  27. * those are used (often mediated by a ROM bootloader) for ISP style
  28. * programming, to perform an initial load of code into flash, or
  29. * sometimes into SRAM. Target code could use "variant" options to
  30. * decide how to use such protocols. For example, Cortex-M3 cores
  31. * from TI/Luminary and from NXP use different protocols for for
  32. * UART or SPI based firmware loading.
  33. *
  34. * As a rule, there are protocols layered on top of the transport.
  35. * For example, different chip families use JTAG in different ways
  36. * for debugging. Also, each family that supports programming over
  37. * a UART link for initial firmware loading tends to define its own
  38. * messaging and error handling.
  39. */
  40. #include <helper/log.h>
  41. #include <transport/transport.h>
  42. extern struct command_context *global_cmd_ctx;
  43. /*-----------------------------------------------------------------------*/
  44. /*
  45. * Infrastructure internals
  46. */
  47. /** List of transports known to OpenOCD. */
  48. static struct transport *transport_list;
  49. /**
  50. * NULL-terminated Vector of names of transports which the
  51. * currently selected debug adapter supports. This is declared
  52. * by the time that adapter is fully set up.
  53. */
  54. static const char **allowed_transports;
  55. /** * The transport being used for the current OpenOCD session. */
  56. static struct transport *session;
  57. static int transport_select(struct command_context *ctx, const char *name)
  58. {
  59. /* name may only identify a known transport;
  60. * caller guarantees session's transport isn't yet set.*/
  61. for (struct transport *t = transport_list; t; t = t->next) {
  62. if (strcmp(t->name, name) == 0) {
  63. int retval = t->select(ctx);
  64. /* select() registers commands specific to this
  65. * transport, and may also reset the link, e.g.
  66. * forcing it to JTAG or SWD mode.
  67. */
  68. if (retval == ERROR_OK)
  69. session = t;
  70. else
  71. LOG_ERROR("Error selecting '%s' as "
  72. "transport", t->name);
  73. return retval;
  74. }
  75. }
  76. LOG_ERROR("No transport named '%s' is available.", name);
  77. return ERROR_FAIL;
  78. }
  79. /**
  80. * Called by debug adapter drivers, or affiliated Tcl config scripts,
  81. * to declare the set of transports supported by an adapter. When
  82. * there is only one member of that set, it is automatically selected.
  83. */
  84. int allow_transports(struct command_context *ctx, const char **vector)
  85. {
  86. /* NOTE: caller is required to provide only a list
  87. * of *valid* transport names
  88. *
  89. * REVISIT should we validate that? and insist there's
  90. * at least one non-NULL element in that list?
  91. *
  92. * ... allow removals, e.g. external strapping prevents use
  93. * of one transport; C code should be definitive about what
  94. * can be used when all goes well.
  95. */
  96. if (allowed_transports != NULL || session) {
  97. LOG_ERROR("Can't modify the set of allowed transports.");
  98. return ERROR_FAIL;
  99. }
  100. allowed_transports = vector;
  101. /* autoselect if there's no choice ... */
  102. if (!vector[1]) {
  103. LOG_INFO("only one transport option; autoselect '%s'",
  104. vector[0]);
  105. return transport_select(ctx, vector [0]);
  106. } else {
  107. /* guard against user config errors */
  108. LOG_WARNING("must select a transport.");
  109. while (*vector) {
  110. LOG_DEBUG("allow transport '%s'", *vector);
  111. vector++;
  112. }
  113. return ERROR_OK;
  114. }
  115. }
  116. /**
  117. * Used to verify corrrect adapter driver initialization.
  118. *
  119. * @returns true iff the adapter declared one or more transports.
  120. */
  121. bool transports_are_declared(void)
  122. {
  123. return allowed_transports != NULL;
  124. }
  125. /**
  126. * Registers a transport. There are general purpose transports
  127. * (such as JTAG), as well as relatively proprietary ones which are
  128. * specific to a given chip (or chip family).
  129. *
  130. * Code implementing a transport needs to register it before it can
  131. * be selected and then activated. This is a dynamic process, so
  132. * that chips (and families) can define transports as needed (without
  133. * nneeding error-prone static tables).
  134. *
  135. * @param new_transport the transport being registered. On a
  136. * successful return, this memory is owned by the transport framework.
  137. *
  138. * @returns ERROR_OK on success, else a fault code.
  139. */
  140. int transport_register(struct transport *new_transport)
  141. {
  142. struct transport *t;
  143. for (t = transport_list; t; t = t->next) {
  144. if (strcmp(t->name, new_transport->name) == 0) {
  145. LOG_ERROR("transport name already used");
  146. return ERROR_FAIL;
  147. }
  148. }
  149. if (!new_transport->select || !new_transport->init) {
  150. LOG_ERROR("invalid transport %s", new_transport->name);
  151. }
  152. /* splice this into the list */
  153. new_transport->next = transport_list;
  154. transport_list = new_transport;
  155. LOG_DEBUG("register '%s'", new_transport->name);
  156. return ERROR_OK;
  157. }
  158. /**
  159. * Returns the transport currently being used by this debug or
  160. * programming session.
  161. *
  162. * @returns handle to the read-only transport entity.
  163. */
  164. struct transport *get_current_transport(void)
  165. {
  166. /* REVISIT -- constify */
  167. return session;
  168. }
  169. /*-----------------------------------------------------------------------*/
  170. /*
  171. * Infrastructure for Tcl interface to transports.
  172. */
  173. /**
  174. * Makes and stores a copy of a set of transports passed as
  175. * parameters to a command.
  176. *
  177. * @param vector where the resulting copy is stored, as an argv-style
  178. * NULL-terminated vector.
  179. */
  180. COMMAND_HELPER(transport_list_parse, char ***vector)
  181. {
  182. char **argv;
  183. unsigned n = CMD_ARGC;
  184. unsigned j = 0;
  185. *vector = NULL;
  186. if (n < 1)
  187. return ERROR_COMMAND_SYNTAX_ERROR;
  188. /* our return vector must be NULL terminated */
  189. argv = (char **) calloc(n + 1, sizeof(char *));
  190. if (argv == NULL)
  191. return ERROR_FAIL;
  192. for (unsigned i = 0; i < n; i++) {
  193. struct transport *t;
  194. for (t = transport_list; t; t = t->next) {
  195. if (strcmp(t->name, CMD_ARGV[i]) != 0)
  196. continue;
  197. argv[j++] = strdup(CMD_ARGV[i]);
  198. break;
  199. }
  200. if (!t) {
  201. LOG_ERROR("no such transport '%s'", CMD_ARGV[i]);
  202. goto fail;
  203. }
  204. }
  205. *vector = argv;
  206. return ERROR_OK;
  207. fail:
  208. for (unsigned i = 0; i < n; i++)
  209. free(argv[i]);
  210. free(argv);
  211. return ERROR_FAIL;
  212. }
  213. COMMAND_HANDLER(handle_transport_init)
  214. {
  215. LOG_DEBUG("%s", __func__);
  216. if (!session) {
  217. LOG_ERROR("session's transport is not selected.");
  218. return ERROR_FAIL;
  219. }
  220. return session->init(CMD_CTX);
  221. }
  222. COMMAND_HANDLER(handle_transport_list)
  223. {
  224. if (CMD_ARGC != 0)
  225. return ERROR_COMMAND_SYNTAX_ERROR;
  226. command_print(CMD_CTX, "The following transports are available:");
  227. for (struct transport *t = transport_list; t; t = t->next)
  228. command_print(CMD_CTX, "\t%s", t->name);
  229. return ERROR_OK;
  230. }
  231. /**
  232. * Implements the Tcl "transport select" command, choosing the
  233. * transport to be used in this debug session from among the
  234. * set supported by the debug adapter being used. Return value
  235. * is scriptable (allowing "if swd then..." etc).
  236. */
  237. static int jim_transport_select(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
  238. {
  239. switch (argc) {
  240. case 1: /* return/display */
  241. if (!session) {
  242. LOG_ERROR("session's transport is not selected.");
  243. return JIM_ERR;
  244. } else {
  245. Jim_SetResultString(interp, session->name, -1);
  246. return JIM_OK;
  247. }
  248. break;
  249. case 2: /* assign */
  250. if (session) {
  251. /* can't change session's transport after-the-fact */
  252. LOG_ERROR("session's transport is already selected.");
  253. return JIM_ERR;
  254. }
  255. /* Is this transport supported by our debug adapter?
  256. * Example, "JTAG-only" means SWD is not supported.
  257. *
  258. * NOTE: requires adapter to have been set up, with
  259. * transports declared via C.
  260. */
  261. if (!allowed_transports) {
  262. LOG_ERROR("Debug adapter doesn't support any transports?");
  263. return JIM_ERR;
  264. }
  265. for (unsigned i = 0; allowed_transports[i]; i++) {
  266. if (strcmp(allowed_transports[i], argv[1]->bytes) == 0)
  267. return transport_select(global_cmd_ctx, argv[1]->bytes);
  268. }
  269. LOG_ERROR("Debug adapter doesn't support '%s' "
  270. "transport", argv[1]->bytes);
  271. return JIM_ERR;
  272. break;
  273. default:
  274. Jim_WrongNumArgs(interp, 1, argv, "[too many parameters]");
  275. return JIM_ERR;
  276. }
  277. }
  278. static const struct command_registration transport_commands[] = {
  279. {
  280. .name = "init",
  281. .handler = handle_transport_init,
  282. /* this would be COMMAND_CONFIG ... except that
  283. * it needs to trigger event handlers that may
  284. * require COMMAND_EXEC ...
  285. */
  286. .mode = COMMAND_ANY,
  287. .help = "Initialize this session's transport",
  288. },
  289. {
  290. .name = "list",
  291. .handler = handle_transport_list,
  292. .mode = COMMAND_ANY,
  293. .help = "list all built-in transports",
  294. },
  295. {
  296. .name = "select",
  297. .jim_handler = jim_transport_select,
  298. .mode = COMMAND_ANY,
  299. .help = "Select this session's transport",
  300. .usage = "[transport_name]",
  301. },
  302. COMMAND_REGISTRATION_DONE
  303. };
  304. static const struct command_registration transport_group[] = {
  305. {
  306. .name = "transport",
  307. .mode = COMMAND_ANY,
  308. .help = "Transport command group",
  309. .chain = transport_commands,
  310. },
  311. COMMAND_REGISTRATION_DONE
  312. };
  313. int transport_register_commands(struct command_context *ctx)
  314. {
  315. return register_commands(ctx, NULL, transport_group);
  316. }