]> git.sur5r.net Git - openocd/blob - src/jtag/hla/hla_tcl.c
jim-nvp: Make Jim_GetOpt_String const-correct
[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, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
22  ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 /* project specific includes */
29 #include <jtag/interface.h>
30 #include <transport/transport.h>
31 #include <helper/time_support.h>
32
33 static int jim_newtap_expected_id(Jim_Nvp *n, Jim_GetOptInfo *goi,
34                                   struct jtag_tap *pTap)
35 {
36         jim_wide w;
37         int e = Jim_GetOpt_Wide(goi, &w);
38         if (e != JIM_OK) {
39                 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter",
40                                        n->name);
41                 return e;
42         }
43
44         unsigned expected_len = sizeof(uint32_t) * pTap->expected_ids_cnt;
45         uint32_t *new_expected_ids = malloc(expected_len + sizeof(uint32_t));
46         if (new_expected_ids == NULL) {
47                 Jim_SetResultFormatted(goi->interp, "no memory");
48                 return JIM_ERR;
49         }
50
51         memcpy(new_expected_ids, pTap->expected_ids, expected_len);
52
53         new_expected_ids[pTap->expected_ids_cnt] = w;
54
55         free(pTap->expected_ids);
56         pTap->expected_ids = new_expected_ids;
57         pTap->expected_ids_cnt++;
58
59         return JIM_OK;
60 }
61
62 #define NTAP_OPT_IRLEN     0
63 #define NTAP_OPT_IRMASK    1
64 #define NTAP_OPT_IRCAPTURE 2
65 #define NTAP_OPT_ENABLED   3
66 #define NTAP_OPT_DISABLED  4
67 #define NTAP_OPT_EXPECTED_ID 5
68 #define NTAP_OPT_VERSION   6
69
70 static int jim_hl_newtap_cmd(Jim_GetOptInfo *goi)
71 {
72         struct jtag_tap *pTap;
73         int x;
74         int e;
75         Jim_Nvp *n;
76         char *cp;
77         const Jim_Nvp opts[] = {
78                 { .name = "-irlen",       .value = NTAP_OPT_IRLEN },
79                 { .name = "-irmask",       .value = NTAP_OPT_IRMASK },
80                 { .name = "-ircapture",       .value = NTAP_OPT_IRCAPTURE },
81                 { .name = "-enable",       .value = NTAP_OPT_ENABLED },
82                 { .name = "-disable",       .value = NTAP_OPT_DISABLED },
83                 { .name = "-expected-id",       .value = NTAP_OPT_EXPECTED_ID },
84                 { .name = "-ignore-version",       .value = NTAP_OPT_VERSION },
85                 { .name = NULL, .value = -1},
86         };
87
88         pTap = calloc(1, sizeof(struct jtag_tap));
89         if (!pTap) {
90                 Jim_SetResultFormatted(goi->interp, "no memory");
91                 return JIM_ERR;
92         }
93
94         /*
95          * we expect CHIP + TAP + OPTIONS
96          * */
97         if (goi->argc < 3) {
98                 Jim_SetResultFormatted(goi->interp,
99                                        "Missing CHIP TAP OPTIONS ....");
100                 free(pTap);
101                 return JIM_ERR;
102         }
103
104         const char *tmp;
105         Jim_GetOpt_String(goi, &tmp, NULL);
106         pTap->chip = strdup(tmp);
107
108         Jim_GetOpt_String(goi, &tmp, NULL);
109         pTap->tapname = strdup(tmp);
110
111         /* name + dot + name + null */
112         x = strlen(pTap->chip) + 1 + strlen(pTap->tapname) + 1;
113         cp = malloc(x);
114         sprintf(cp, "%s.%s", pTap->chip, pTap->tapname);
115         pTap->dotted_name = cp;
116
117         LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
118                   pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
119
120         while (goi->argc) {
121                 e = Jim_GetOpt_Nvp(goi, opts, &n);
122                 if (e != JIM_OK) {
123                         Jim_GetOpt_NvpUnknown(goi, opts, 0);
124                         free(cp);
125                         free(pTap);
126                         return e;
127                 }
128                 LOG_DEBUG("Processing option: %s", n->name);
129                 switch (n->value) {
130                 case NTAP_OPT_EXPECTED_ID:
131                         e = jim_newtap_expected_id(n, goi, pTap);
132                         if (JIM_OK != e) {
133                                 free(cp);
134                                 free(pTap);
135                                 return e;
136                         }
137                         break;
138                 case NTAP_OPT_IRLEN:
139                 case NTAP_OPT_IRMASK:
140                 case NTAP_OPT_IRCAPTURE:
141                         /* dummy read to ignore the next argument */
142                         Jim_GetOpt_Wide(goi, NULL);
143                         break;
144                 }               /* switch (n->value) */
145         }                       /* while (goi->argc) */
146
147         /* default is enabled-after-reset */
148         pTap->enabled = !pTap->disabled_after_reset;
149
150         jtag_tap_init(pTap);
151         return JIM_OK;
152 }
153
154 int jim_hl_newtap(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
155 {
156         Jim_GetOptInfo goi;
157         Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
158         return jim_hl_newtap_cmd(&goi);
159 }