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.
 
 
 
 
 
 

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