jtag: hla: tcl: fix inconsistent expected_id handling
[fw/openocd] / src / jtag / hla / hla_tcl.c
1 /***************************************************************************
2  *   Copyright (C) 2011 by Mathias Kuester                                 *
3  *   Mathias Kuester <kesmtp@freenet.de>                                   *
4  *                                                                         *
5  *   Copyright (C) 2012 by Spencer Oliver                                  *
6  *   spen@spen-soft.co.uk                                                  *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
20  ***************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 /* project specific includes */
27 #include <jtag/interface.h>
28 #include <transport/transport.h>
29 #include <helper/time_support.h>
30
31 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
32                                   struct jtag_tap *pTap)
33 {
34         jim_wide w;
35         int e = Jim_GetOpt_Wide(goi, &w);
36         if (e != JIM_OK) {
37                 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter",
38                                        n->name);
39                 return e;
40         }
41
42         uint32_t *p = realloc(pTap->expected_ids,
43                               (pTap->expected_ids_cnt + 1) * sizeof(uint32_t));
44         if (!p) {
45                 Jim_SetResultFormatted(goi->interp, "no memory");
46                 return JIM_ERR;
47         }
48
49         pTap->expected_ids = p;
50         pTap->expected_ids[pTap->expected_ids_cnt++] = w;
51
52         return JIM_OK;
53 }
54
55 #define NTAP_OPT_IRLEN     0
56 #define NTAP_OPT_IRMASK    1
57 #define NTAP_OPT_IRCAPTURE 2
58 #define NTAP_OPT_ENABLED   3
59 #define NTAP_OPT_DISABLED  4
60 #define NTAP_OPT_EXPECTED_ID 5
61 #define NTAP_OPT_VERSION   6
62
63 static int jim_hl_newtap_cmd(Jim_GetOptInfo *goi)
64 {
65         struct jtag_tap *pTap;
66         int x;
67         int e;
68         Jim_Nvp *n;
69         char *cp;
70         const Jim_Nvp opts[] = {
71                 { .name = "-irlen",       .value = NTAP_OPT_IRLEN },
72                 { .name = "-irmask",       .value = NTAP_OPT_IRMASK },
73                 { .name = "-ircapture",       .value = NTAP_OPT_IRCAPTURE },
74                 { .name = "-enable",       .value = NTAP_OPT_ENABLED },
75                 { .name = "-disable",       .value = NTAP_OPT_DISABLED },
76                 { .name = "-expected-id",       .value = NTAP_OPT_EXPECTED_ID },
77                 { .name = "-ignore-version",       .value = NTAP_OPT_VERSION },
78                 { .name = NULL, .value = -1},
79         };
80
81         pTap = calloc(1, sizeof(struct jtag_tap));
82         if (!pTap) {
83                 Jim_SetResultFormatted(goi->interp, "no memory");
84                 return JIM_ERR;
85         }
86
87         /*
88          * we expect CHIP + TAP + OPTIONS
89          * */
90         if (goi->argc < 3) {
91                 Jim_SetResultFormatted(goi->interp,
92                                        "Missing CHIP TAP OPTIONS ....");
93                 free(pTap);
94                 return JIM_ERR;
95         }
96
97         const char *tmp;
98         Jim_GetOpt_String(goi, &tmp, NULL);
99         pTap->chip = strdup(tmp);
100
101         Jim_GetOpt_String(goi, &tmp, NULL);
102         pTap->tapname = strdup(tmp);
103
104         /* name + dot + name + null */
105         x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
106         cp = malloc(x);
107         sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
108         pTap->dotted_name = cp;
109
110         LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
111                   pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
112
113         while (goi->argc) {
114                 e = Jim_GetOpt_Nvp(goi, opts, &n);
115                 if (e != JIM_OK) {
116                         Jim_GetOpt_NvpUnknown(goi, opts, 0);
117                         free(cp);
118                         free(pTap);
119                         return e;
120                 }
121                 LOG_DEBUG("Processing option: %s", n->name);
122                 switch (n->value) {
123                 case NTAP_OPT_EXPECTED_ID:
124                         e = jim_newtap_expected_id(n, goi, pTap);
125                         if (JIM_OK != e) {
126                                 free(cp);
127                                 free(pTap);
128                                 return e;
129                         }
130                         break;
131                 case NTAP_OPT_IRLEN:
132                 case NTAP_OPT_IRMASK:
133                 case NTAP_OPT_IRCAPTURE:
134                         /* dummy read to ignore the next argument */
135                         Jim_GetOpt_Wide(goi, NULL);
136                         break;
137                 }               /* switch (n->value) */
138         }                       /* while (goi->argc) */
139
140         /* default is enabled-after-reset */
141         pTap->enabled = !pTap->disabled_after_reset;
142
143         jtag_tap_init(pTap);
144         return JIM_OK;
145 }
146
147 int jim_hl_newtap(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
148 {
149         Jim_GetOptInfo goi;
150         Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
151         return jim_hl_newtap_cmd(&goi);
152 }