transport selection tweaks
[fw/openocd] / src / jtag / 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
47 #include "transport.h"
48
49 /*-----------------------------------------------------------------------*/
50
51 /*
52  * Infrastructure internals
53  */
54
55 /** List of transports known to OpenOCD. */
56 static struct transport *transport_list;
57
58 /**
59  * NULL-terminated Vector of names of transports which the
60  * currently selected debug adapter supports.  This is declared
61  * by the time that adapter is fully set up.
62  */
63 static const char **allowed_transports;
64
65 /** * The transport being used for the current OpenOCD session.  */
66 static struct transport *session;
67
68 static  int transport_select(struct command_context *ctx, const char *name)
69 {
70         /* name may only identify a known transport;
71          * caller guarantees session's transport isn't yet set.*/
72         for (struct transport *t = transport_list; t; t = t->next) {
73                         if (strcmp(t->name, name) == 0) {
74                                 int retval = t->select(ctx);
75                         /* select() registers commands specific to this
76                          * transport, and may also reset the link, e.g.
77                          * forcing it to JTAG or SWD mode.
78                          */
79                         if (retval == ERROR_OK)
80                                 session = t;
81                         else
82                                 LOG_ERROR("Error %d selecting '%s' as "
83                                         "transport", retval, 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
115         allowed_transports = vector;
116
117         /* autoselect if there's no choice ... */
118         if (!vector[1]) {
119                 LOG_INFO("only one transport option; autoselect '%s'",
120                                 vector[0]);
121                 return transport_select(ctx, vector [0]);
122         } else {
123                 /* guard against user config errors */
124                 LOG_WARNING("must select a transport.");
125                 while (*vector)
126                         LOG_DEBUG("allow transport '%s'", *vector++);
127                 return ERROR_OK;
128         }
129 }
130
131
132 /**
133  * Used to verify corrrect adapter driver initialization.
134  *
135  * @returns true iff the adapter declared one or more transports.
136  */
137 bool transports_are_declared(void)
138 {
139         return allowed_transports != NULL;
140 }
141
142 /**
143  * Registers a transport.  There are general purpose transports
144  * (such as JTAG), as well as relatively proprietary ones which are
145  * specific to a given chip (or chip family).
146  *
147  * Code implementing a transport needs to register it before it can
148  * be selected and then activated.  This is a dynamic process, so
149  * that chips (and families) can define transports as needed (without
150  * nneeding error-prone static tables).
151  *
152  * @param new_transport the transport being registered.  On a
153  * successful return, this memory is owned by the transport framework.
154  *
155  * @returns ERROR_OK on success, else a fault code.
156  */
157 int transport_register(struct transport *new_transport)
158 {
159         struct transport *t;
160
161         for (t = transport_list; t; t = t->next) {
162                 if (strcmp(t->name, new_transport->name) == 0) {
163                         LOG_ERROR("transport name already used");
164                         return ERROR_FAIL;
165                 }
166         }
167
168         if (!new_transport->select || !new_transport->init) {
169                 LOG_ERROR("invalid transport %s", new_transport->name);
170         }
171
172         /* splice this into the list */
173         new_transport->next = transport_list;
174         transport_list = new_transport;
175         LOG_DEBUG("register '%s'", new_transport->name);
176
177         return ERROR_OK;
178 }
179
180 /**
181  * Returns the transport currently being used by this debug or
182  * programming session.
183  *
184  * @returns handle to the read-only transport entity.
185  */
186 struct transport *get_current_transport(void)
187 {
188
189         /* REVISIT -- constify */
190         return session;
191 }
192
193
194 /*-----------------------------------------------------------------------*/
195
196 /*
197  * Infrastructure for Tcl interface to transports.
198  */
199
200 /**
201  * Makes and stores a copy of a set of transports passed as
202  * parameters to a command.
203  *
204  * @param vector where the resulting copy is stored, as an argv-style
205  *      NULL-terminated vector.
206  */
207 COMMAND_HELPER(transport_list_parse, char ***vector)
208 {
209         char **argv;
210         unsigned n = CMD_ARGC;
211         unsigned j = 0;
212
213         *vector = NULL;
214
215         if (n < 1)
216                 return ERROR_COMMAND_SYNTAX_ERROR;
217
218         /* our return vector must be NULL terminated */
219         argv = (char **) calloc(n + 1, sizeof(char *));
220         if (argv == NULL)
221                 return ERROR_FAIL;
222
223         for (unsigned i = 0; i < n; i++) {
224                 struct transport *t;
225
226                 for (t = transport_list; t; t = t->next) {
227                         if (strcmp(t->name, CMD_ARGV[i]) != 0)
228                                 continue;
229                         argv[j++] = strdup(CMD_ARGV[i]);
230                         break;
231                 }
232                 if (!t) {
233                         LOG_ERROR("no such transport '%s'", CMD_ARGV[i]);
234                         goto fail;
235                 }
236         }
237
238         *vector = argv;
239         return ERROR_OK;
240
241 fail:
242         for (unsigned i = 0; i < n; i++)
243                 free(argv[i]);
244         free(argv);
245         return ERROR_FAIL;
246 }
247
248 COMMAND_HANDLER(handle_transport_init)
249 {
250         LOG_DEBUG("%s", __func__);
251         if (!session) {
252                 LOG_ERROR("session's transport is not selected.");
253                 return ERROR_FAIL;
254         }
255
256         return session->init(CMD_CTX);
257 }
258
259 COMMAND_HANDLER(handle_transport_list)
260 {
261         if (CMD_ARGC != 0)
262                 return ERROR_COMMAND_SYNTAX_ERROR;
263
264         command_print(CMD_CTX, "The following transports are available:");
265
266         for (struct transport *t = transport_list; t; t = t->next)
267                 command_print(CMD_CTX, "\t%s", t->name);
268
269         return ERROR_OK;
270 }
271
272 /**
273  * Implements the Tcl "transport select" command, choosing the
274  * transport to be used in this debug session from among the
275  * set supported by the debug adapter being used.
276  */
277 COMMAND_HANDLER(handle_transport_select)
278 {
279         int retval = ERROR_OK;;
280
281         switch (CMD_ARGC) {
282         case 0:                 /* "select" */
283                 if (session) {
284                         goto show;
285                 }
286                 LOG_ERROR("session's transport is not selected.");
287                 return ERROR_FAIL;
288
289         case 1:                 /* "select FOO" */
290                 if ((session!= NULL) && strcmp(session->name, CMD_ARGV[0]) == 0) {
291                         /* NOP */
292                         LOG_DEBUG("transport '%s' is already selected",
293                                         CMD_ARGV[0]);
294                         return ERROR_OK;
295                 } else {
296                         /* we can't change this session's transport after-the-fact */
297                         if (session) {
298                                 LOG_ERROR("session's transport is already selected.");
299                                 return ERROR_FAIL;
300                         }
301                 }
302                 break;
303
304         default:                /* select FOO BAR */
305                 /* we only select *one* transport per session */
306                 LOG_ERROR("may only select one transport!");
307                 return ERROR_COMMAND_SYNTAX_ERROR;
308         }
309
310         /* Is this transport supported by our debug adapter?
311          * Example, "JTAG-only" means SWD is not supported.
312          *
313          * NOTE:  requires adapter to have been set up, with
314          * transports declared via C.
315          */
316         if (!allowed_transports) {
317                 LOG_ERROR("Debug adapter doesn't support any transports?");
318                 return ERROR_FAIL;
319         }
320
321         for (unsigned i = 0; allowed_transports[i]; i++) {
322
323                 if (strcmp(allowed_transports[i], CMD_ARGV[0]) == 0)
324                         return transport_select(CMD_CTX, CMD_ARGV[0]);
325         }
326
327         LOG_ERROR("Debug adapter doesn't support '%s' "
328                         "transport?", CMD_ARGV[0]);
329         return ERROR_FAIL;
330
331
332 show:
333         /* report the current transport selection */
334         command_print(CMD_CTX, "%s", session->name);
335         return retval;
336 }
337
338 static const struct command_registration transport_commands[] = {
339         {
340                 .name = "init",
341                 .handler = handle_transport_init,
342                 /* this would be COMMAND_CONFIG ... except that
343                  * it needs to trigger event handlers that may
344                  * require COMMAND_EXEC ...
345                  */
346                 .mode = COMMAND_ANY,
347                 .help = "Initialize this session's transport",
348         },
349         {
350                 .name = "list",
351                 .handler = handle_transport_list,
352                 .mode = COMMAND_ANY,
353                 .help = "list all built-in transports",
354         },
355         {
356                 .name = "select",
357                 .handler = handle_transport_select,
358                 .mode = COMMAND_ANY,
359                 .help = "Select this session's transport",
360                 .usage = "[transport_name]",
361         },
362         COMMAND_REGISTRATION_DONE
363 };
364
365 static const struct command_registration transport_group[] = {
366         {
367                 .name = "transport",
368                 .mode = COMMAND_ANY,
369                 .help = "Transport command group",
370                 .chain = transport_commands,
371         },
372         COMMAND_REGISTRATION_DONE
373 };
374
375
376 int transport_register_commands(struct command_context *ctx)
377 {
378         return register_commands(ctx, NULL, transport_group);
379 }