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

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)