- Replace 'while(' with 'while ('.
[openocd.git] / src / jtag / tcl.c
index 5fc1bce21c264acc22c1dd80705ca30ece854ca5..75153636331de5b1fc12c970851a286765a3bf63 100644 (file)
@@ -49,7 +49,7 @@ static const Jim_Nvp nvp_jtag_tap_event[] = {
 /* jtag interfaces (parport, FTDI-USB, TI-USB, ...)
  */
 
-#if BUILD_ECOSBOARD == 1
+#if BUILD_ZY1000 == 1
        extern jtag_interface_t zy1000_interface;
 #elif defined(BUILD_MINIDRIVER_DUMMY)
        extern jtag_interface_t minidummy_interface;
@@ -119,7 +119,7 @@ static const Jim_Nvp nvp_jtag_tap_event[] = {
  * or some number of standard driver interfaces, never both.
  */
 jtag_interface_t *jtag_interfaces[] = {
-#if BUILD_ECOSBOARD == 1
+#if BUILD_ZY1000 == 1
        &zy1000_interface,
 #elif defined(BUILD_MINIDRIVER_DUMMY)
        &minidummy_interface,
@@ -256,7 +256,7 @@ static int jtag_tap_configure_cmd( Jim_GetOptInfo *goi, jtag_tap_t * tap)
                                        jteap = tap->event_action;
                                        /* replace existing? */
                                        while (jteap) {
-                                               if (jteap->event == (enum jtag_tap_event)n->value) {
+                                               if (jteap->event == (enum jtag_event)n->value) {
                                                        break;
                                                }
                                                jteap = jteap->next;
@@ -296,9 +296,15 @@ static int jtag_tap_configure_cmd( Jim_GetOptInfo *goi, jtag_tap_t * tap)
        return JIM_OK;
 }
 
+static int is_bad_irval(int ir_length, jim_wide w)
+{
+       jim_wide v = 1;
 
-extern void jtag_tap_init(jtag_tap_t *tap);
-extern void jtag_tap_free(jtag_tap_t *tap);
+       v <<= ir_length;
+       v -= 1;
+       v = ~v;
+       return (w & v) != 0;
+}
 
 static int jim_newtap_cmd( Jim_GetOptInfo *goi )
 {
@@ -325,16 +331,16 @@ static int jim_newtap_cmd( Jim_GetOptInfo *goi )
                { .name = NULL                          ,       .value = -1 },
        };
 
-       pTap = malloc( sizeof(jtag_tap_t) );
-       memset( pTap, 0, sizeof(*pTap) );
-       if( !pTap ){
-               Jim_SetResult_sprintf( goi->interp, "no memory");
+       pTap = calloc(1, sizeof(jtag_tap_t));
+       if (!pTap) {
+               Jim_SetResult_sprintf(goi->interp, "no memory");
                return JIM_ERR;
        }
+
        /*
         * we expect CHIP + TAP + OPTIONS
         * */
-       if( goi->argc < 3 ){
+       if ( goi->argc < 3 ){
                Jim_SetResult_sprintf(goi->interp, "Missing CHIP TAP OPTIONS ....");
                return JIM_ERR;
        }
@@ -353,9 +359,6 @@ static int jim_newtap_cmd( Jim_GetOptInfo *goi )
        LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
                          pTap->chip, pTap->tapname, pTap->dotted_name, goi->argc);
 
-       /* default is enabled */
-       pTap->enabled = 1;
-
        /* deal with options */
 #define NTREQ_IRLEN      1
 #define NTREQ_IRCAPTURE  2
@@ -364,37 +367,37 @@ static int jim_newtap_cmd( Jim_GetOptInfo *goi )
        /* clear them as we find them */
        reqbits = (NTREQ_IRLEN | NTREQ_IRCAPTURE | NTREQ_IRMASK);
 
-       while( goi->argc ){
+       while ( goi->argc ){
                e = Jim_GetOpt_Nvp( goi, opts, &n );
-               if( e != JIM_OK ){
+               if ( e != JIM_OK ){
                        Jim_GetOpt_NvpUnknown( goi, opts, 0 );
                        return e;
                }
                LOG_DEBUG("Processing option: %s", n->name );
                switch( n->value ){
                case NTAP_OPT_ENABLED:
-                       pTap->enabled = 1;
+                       pTap->disabled_after_reset = false;
                        break;
                case NTAP_OPT_DISABLED:
-                       pTap->enabled = 0;
+                       pTap->disabled_after_reset = true;
                        break;
                case NTAP_OPT_EXPECTED_ID:
                {
-                       u32 *new_expected_ids;
+                       uint32_t *new_expected_ids;
 
                        e = Jim_GetOpt_Wide( goi, &w );
-                       if( e != JIM_OK) {
+                       if ( e != JIM_OK) {
                                Jim_SetResult_sprintf(goi->interp, "option: %s bad parameter", n->name);
                                return e;
                        }
 
-                       new_expected_ids = malloc(sizeof(u32) * (pTap->expected_ids_cnt + 1));
+                       new_expected_ids = malloc(sizeof(uint32_t) * (pTap->expected_ids_cnt + 1));
                        if (new_expected_ids == NULL) {
                                Jim_SetResult_sprintf( goi->interp, "no memory");
                                return JIM_ERR;
                        }
 
-                       memcpy(new_expected_ids, pTap->expected_ids, sizeof(u32) * pTap->expected_ids_cnt);
+                       memcpy(new_expected_ids, pTap->expected_ids, sizeof(uint32_t) * pTap->expected_ids_cnt);
 
                        new_expected_ids[pTap->expected_ids_cnt] = w;
 
@@ -407,32 +410,41 @@ static int jim_newtap_cmd( Jim_GetOptInfo *goi )
                case NTAP_OPT_IRMASK:
                case NTAP_OPT_IRCAPTURE:
                        e = Jim_GetOpt_Wide( goi, &w );
-                       if( e != JIM_OK ){
+                       if ( e != JIM_OK ){
                                Jim_SetResult_sprintf( goi->interp, "option: %s bad parameter", n->name );
                                return e;
                        }
-                       if( (w < 0) || (w > 0xffff) ){
-                               /* wacky value */
-                               Jim_SetResult_sprintf( goi->interp, "option: %s - wacky value: %d (0x%x)",
-                                                                          n->name, (int)(w), (int)(w));
-                               return JIM_ERR;
-                       }
                        switch(n->value){
                        case NTAP_OPT_IRLEN:
+                               if (w > (jim_wide) (8 * sizeof(pTap->ir_capture_value)))
+                                       LOG_WARNING("huge IR length %d", (int) w);
                                pTap->ir_length = w;
                                reqbits &= (~(NTREQ_IRLEN));
                                break;
                        case NTAP_OPT_IRMASK:
+                               if (is_bad_irval(pTap->ir_length, w)) {
+                                       LOG_ERROR("IR mask %x too big",
+                                                       (int) w);
+                                       return ERROR_FAIL;
+                               }
                                pTap->ir_capture_mask = w;
                                reqbits &= (~(NTREQ_IRMASK));
                                break;
                        case NTAP_OPT_IRCAPTURE:
+                               if (is_bad_irval(pTap->ir_length, w)) {
+                                       LOG_ERROR("IR capture %x too big",
+                                                       (int) w);
+                                       return ERROR_FAIL;
+                               }
                                pTap->ir_capture_value = w;
                                reqbits &= (~(NTREQ_IRCAPTURE));
                                break;
                        }
                } /* switch(n->value) */
-       } /* while( goi->argc ) */
+       } /* while ( goi->argc ) */
+
+       /* default is enabled-after-reset */
+       pTap->enabled = !pTap->disabled_after_reset;
 
        /* Did all the required option bits get cleared? */
        if (0 == reqbits)
@@ -448,7 +460,7 @@ static int jim_newtap_cmd( Jim_GetOptInfo *goi )
        return JIM_ERR;
 }
 
-static void jtag_tap_handle_event( jtag_tap_t * tap, enum jtag_tap_event e)
+static void jtag_tap_handle_event(jtag_tap_t *tap, enum jtag_event e)
 {
        jtag_tap_event_action_t * jteap;
        int done;
@@ -466,6 +478,12 @@ static void jtag_tap_handle_event( jtag_tap_t * tap, enum jtag_tap_event e)
                                        Jim_GetString(jteap->body, NULL) );
                        if (Jim_EvalObj(interp, jteap->body) != JIM_OK) {
                                Jim_PrintErrorMessage(interp);
+                       } else {
+                               /* NOTE:  we currently assume the handlers
+                                * can't fail.  That presumes later code
+                                * will be verifying the scan chains ...
+                                */
+                               tap->enabled = (e == JTAG_TAP_EVENT_ENABLE);
                        }
                }
 
@@ -517,7 +535,7 @@ static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv
        Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );
 
        e = Jim_GetOpt_Nvp( &goi, jtag_cmds, &n );
-       if( e != JIM_OK ){
+       if ( e != JIM_OK ){
                Jim_GetOpt_NvpUnknown( &goi, jtag_cmds, 0 );
                return e;
        }
@@ -527,19 +545,19 @@ static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv
                /* return the name of the interface */
                /* TCL code might need to know the exact type... */
                /* FUTURE: we allow this as a means to "set" the interface. */
-               if( goi.argc != 0 ){
+               if ( goi.argc != 0 ){
                        Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
                        return JIM_ERR;
                }
                Jim_SetResultString( goi.interp, jtag_interface->name, -1 );
                return JIM_OK;
        case JTAG_CMD_INIT_RESET:
-               if( goi.argc != 0 ){
+               if ( goi.argc != 0 ){
                        Jim_WrongNumArgs( goi.interp, 1, goi.argv-1, "(no params)");
                        return JIM_ERR;
                }
                e = jtag_init_reset(context);
-               if( e != ERROR_OK ){
+               if ( e != ERROR_OK ){
                        Jim_SetResult_sprintf( goi.interp, "error: %d", e);
                        return JIM_ERR;
                }
@@ -550,39 +568,58 @@ static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv
        case JTAG_CMD_TAPISENABLED:
        case JTAG_CMD_TAPENABLE:
        case JTAG_CMD_TAPDISABLE:
-               if( goi.argc != 1 ){
+               if ( goi.argc != 1 ){
                        Jim_SetResultString( goi.interp, "Too many parameters",-1 );
                        return JIM_ERR;
                }
 
                {
                        jtag_tap_t *t;
-                       t = jtag_tap_by_jim_obj( goi.interp, goi.argv[0] );
-                       if( t == NULL ){
+
+                       t = jtag_tap_by_jim_obj(goi.interp, goi.argv[0]);
+                       if (t == NULL)
                                return JIM_ERR;
-                       }
-                       switch( n->value ){
+
+                       switch (n->value) {
                        case JTAG_CMD_TAPISENABLED:
-                               e = t->enabled;
                                break;
                        case JTAG_CMD_TAPENABLE:
-                               jtag_tap_handle_event( t, JTAG_TAP_EVENT_ENABLE);
-                               e = 1;
-                               t->enabled = e;
+                               if (t->enabled)
+                                       break;
+                               jtag_tap_handle_event(t, JTAG_TAP_EVENT_ENABLE);
+                               if (!t->enabled)
+                                       break;
+
+                               /* FIXME add JTAG sanity checks, w/o TLR
+                                *  - scan chain length grew by one (this)
+                                *  - IDs and IR lengths are as expected
+                                */
+
+                               jtag_call_event_callbacks(JTAG_TAP_EVENT_ENABLE);
                                break;
                        case JTAG_CMD_TAPDISABLE:
-                               jtag_tap_handle_event( t, JTAG_TAP_EVENT_DISABLE);
-                               e = 0;
-                               t->enabled = e;
+                               if (!t->enabled)
+                                       break;
+                               jtag_tap_handle_event(t, JTAG_TAP_EVENT_DISABLE);
+                               if (t->enabled)
+                                       break;
+
+                               /* FIXME add JTAG sanity checks, w/o TLR
+                                *  - scan chain length shrank by one (this)
+                                *  - IDs and IR lengths are as expected
+                                */
+
+                               jtag_call_event_callbacks(JTAG_TAP_EVENT_DISABLE);
                                break;
                        }
-                       Jim_SetResult( goi.interp, Jim_NewIntObj( goi.interp, e ) );
+                       e = t->enabled;
+                       Jim_SetResult(goi.interp, Jim_NewIntObj(goi.interp, e));
                        return JIM_OK;
                }
                break;
 
        case JTAG_CMD_CGET:
-               if( goi.argc < 2 ){
+               if ( goi.argc < 2 ){
                        Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ...");
                        return JIM_ERR;
                }
@@ -592,7 +629,7 @@ static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv
 
                        Jim_GetOpt_Obj(&goi, &o);
                        t = jtag_tap_by_jim_obj( goi.interp, o );
-                       if( t == NULL ){
+                       if ( t == NULL ){
                                return JIM_ERR;
                        }
 
@@ -602,7 +639,7 @@ static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv
                break;
 
        case JTAG_CMD_CONFIGURE:
-               if( goi.argc < 3 ){
+               if ( goi.argc < 3 ){
                        Jim_WrongNumArgs( goi.interp, 0, NULL, "?tap-name? -option ?VALUE? ...");
                        return JIM_ERR;
                }
@@ -612,7 +649,7 @@ static int jim_jtag_command( Jim_Interp *interp, int argc, Jim_Obj *const *argv
 
                        Jim_GetOpt_Obj(&goi, &o);
                        t = jtag_tap_by_jim_obj( goi.interp, o );
-                       if( t == NULL ){
+                       if ( t == NULL ){
                                return JIM_ERR;
                        }
 
@@ -769,7 +806,7 @@ static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *c
         * argv[ 3] = not actually used by anything but in the docs
         */
 
-       if( argc < 4 ){
+       if ( argc < 4 ){
                command_print( cmd_ctx, "OLD DEPRECATED SYNTAX: Please use the NEW syntax");
                return ERROR_OK;
        }
@@ -810,7 +847,7 @@ static int handle_jtag_device_command(struct command_context_s *cmd_ctx, char *c
                         Jim_GetString( newargs[9], NULL ) );
 
        e = jim_jtag_command( interp, 10, newargs );
-       if( e != JIM_OK ){
+       if ( e != JIM_OK ){
                command_print( cmd_ctx, "%s", Jim_GetString( Jim_GetResult(interp), NULL ) );
        }
        return e;
@@ -824,8 +861,8 @@ static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cm
        command_print(cmd_ctx, "     TapName            | Enabled |   IdCode      Expected    IrLen IrCap  IrMask Instr     ");
        command_print(cmd_ctx, "---|--------------------|---------|------------|------------|------|------|------|---------");
 
-       while( tap ){
-               u32 expected, expected_mask, cur_instr, ii;
+       while ( tap ){
+               uint32_t expected, expected_mask, cur_instr, ii;
                expected = buf_get_u32(tap->expected, 0, tap->ir_length);
                expected_mask = buf_get_u32(tap->expected_mask, 0, tap->ir_length);
                cur_instr = buf_get_u32(tap->cur_instr, 0, tap->ir_length);
@@ -835,16 +872,16 @@ static int handle_scan_chain_command(struct command_context_s *cmd_ctx, char *cm
                                          tap->abs_chain_position,
                                          tap->dotted_name,
                                          tap->enabled ? 'Y' : 'n',
-                                         tap->idcode,
-                                         (tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
-                                         tap->ir_length,
-                                         expected,
-                                         expected_mask,
-                                         cur_instr);
+                                         (unsigned int)(tap->idcode),
+                                         (unsigned int)(tap->expected_ids_cnt > 0 ? tap->expected_ids[0] : 0),
+                                         (unsigned int)(tap->ir_length),
+                                         (unsigned int)(expected),
+                                         (unsigned int)(expected_mask),
+                                         (unsigned int)(cur_instr));
 
                for (ii = 1; ii < tap->expected_ids_cnt; ii++) {
                        command_print(cmd_ctx, "   |                    |         |            | 0x%08x |      |      |      |         ",
-                                                 tap->expected_ids[ii]);
+                                                 (unsigned int)(tap->expected_ids[ii]));
                }
 
                tap = tap->next_tap;
@@ -1038,7 +1075,7 @@ static int handle_jtag_khz_command(struct command_context_s *cmd_ctx, char *cmd,
                        return retval;
        }
 
-       int cur_speed;
+       int cur_speed = jtag_get_speed_khz();
        retval = jtag_get_speed_readable(&cur_speed);
        if (ERROR_OK != retval)
                return retval;
@@ -1093,7 +1130,7 @@ static int handle_runtest_command(struct command_context_s *cmd_ctx,
        if (ERROR_OK != retval)
                return retval;
 
-       jtag_add_runtest(num_clocks, jtag_get_end_state());
+       jtag_add_runtest(num_clocks, TAP_IDLE);
        jtag_execute_queue();
 
        return ERROR_OK;
@@ -1137,20 +1174,20 @@ static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, c
         */
        endstate = TAP_IDLE;
 
-       if( argc >= 4 ){
+       if ( argc >= 4 ){
                /* have at least one pair of numbers. */
                /* is last pair the magic text? */
-               if( 0 == strcmp( "-endstate", args[ argc - 2 ] ) ){
+               if ( 0 == strcmp( "-endstate", args[ argc - 2 ] ) ){
                        const char *cpA;
                        const char *cpS;
                        cpA = args[ argc-1 ];
                        for( endstate = 0 ; endstate < TAP_NUM_STATES ; endstate++ ){
                                cpS = tap_state_name( endstate );
-                               if( 0 == strcmp( cpA, cpS ) ){
+                               if ( 0 == strcmp( cpA, cpS ) ){
                                        break;
                                }
                        }
-                       if( endstate >= TAP_NUM_STATES ){
+                       if ( endstate >= TAP_NUM_STATES ){
                                return ERROR_COMMAND_SYNTAX_ERROR;
                        } else {
                                if (!scan_is_safe(endstate))
@@ -1181,7 +1218,7 @@ static int handle_irscan_command(struct command_context_s *cmd_ctx, char *cmd, c
                fields[i].num_bits = field_size;
                fields[i].out_value = malloc(CEIL(field_size, 8));
 
-               u32 value;
+               uint32_t value;
                retval = parse_u32(args[i * 2 + 1], &value);
                if (ERROR_OK != retval)
                        goto error_return;
@@ -1233,6 +1270,8 @@ static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args
 
        endstate = TAP_IDLE;
 
+       script_debug(interp, "drscan", argc, args);
+
        /* validate arguments as numbers */
        e = JIM_OK;
        for (i = 2; i < argc; i+=2)
@@ -1242,7 +1281,7 @@ static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args
 
                e = Jim_GetLong(interp, args[i], &bits);
                /* If valid - try next arg */
-               if( e == JIM_OK ){
+               if ( e == JIM_OK ){
                        continue;
                }
 
@@ -1260,13 +1299,13 @@ static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args
                /* get arg as a string. */
                cp = Jim_GetString( args[i], NULL );
                /* is it the magic? */
-               if( 0 == strcmp( "-endstate", cp ) ){
+               if ( 0 == strcmp( "-endstate", cp ) ){
                        /* is the statename valid? */
                        cp = Jim_GetString( args[i+1], NULL );
 
                        /* see if it is a valid state name */
                        endstate = tap_state_by_name(cp);
-                       if( endstate < 0 ){
+                       if ( endstate < 0 ){
                                /* update the error message */
                                Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp );
                        } else {
@@ -1282,13 +1321,13 @@ static int Jim_Command_drscan(Jim_Interp *interp, int argc, Jim_Obj *const *args
                }
 
                /* Still an error? */
-               if( e != JIM_OK ){
+               if ( e != JIM_OK ){
                        return e; /* too bad */
                }
        } /* validate args */
 
        tap = jtag_tap_by_jim_obj( interp, args[1] );
-       if( tap == NULL ){
+       if ( tap == NULL ){
                return JIM_ERR;
        }
 
@@ -1354,13 +1393,15 @@ static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *ar
                return JIM_ERR;
        }
 
+       script_debug(interp, "pathmove", argc, args);
+
        int i;
        for (i=0; i<argc-1; i++)
        {
                const char *cp;
                cp = Jim_GetString( args[i+1], NULL );
                states[i] = tap_state_by_name(cp);
-               if( states[i] < 0 )
+               if ( states[i] < 0 )
                {
                        /* update the error message */
                        Jim_SetResult_sprintf(interp,"endstate: %s invalid", cp );
@@ -1388,6 +1429,8 @@ static int Jim_Command_pathmove(Jim_Interp *interp, int argc, Jim_Obj *const *ar
 
 static int Jim_Command_flush_count(Jim_Interp *interp, int argc, Jim_Obj *const *args)
 {
+       script_debug(interp, "flush_count", argc, args);
+
        Jim_SetResult(interp, Jim_NewIntObj(interp, jtag_get_flush_queue_count()));
 
        return JIM_OK;
@@ -1459,4 +1502,3 @@ static int handle_tms_sequence_command(struct command_context_s *cmd_ctx, char *
 
        return ERROR_OK;
 }
-

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)