- fix broken JTAG error handling
[openocd.git] / src / jtag / jtag.c
index bc5e88fb29d645dcccf45cdf2f3dff1622eeb189..47031b7eb8aaf0f5390b9efa45d3e7c28a4cc5c6 100644 (file)
@@ -331,8 +331,9 @@ jtag_device_t* jtag_get_device(int num)
                device = device->next;
                i++;
        }
-
-       return NULL;
+       
+       ERROR("jtag device number %d not defined", num);
+       exit(-1);
 }
 
 void* cmd_queue_alloc(size_t size)
@@ -359,7 +360,8 @@ void* cmd_queue_alloc(size_t size)
        offset = (*p_page)->used;
        (*p_page)->used += size;
        
-       return ((*p_page)->address) + offset;
+       u8 *t=(u8 *)((*p_page)->address);
+       return t + offset;
 }
 
 void cmd_queue_free()
@@ -416,7 +418,7 @@ int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state,
        
        cmd_queue_cur_state = cmd_queue_end_state;
                
-       for (i=0; i < jtag_num_devices; i++)
+       for (i = 0; i < jtag_num_devices; i++)
        {
                int found = 0;
                device = jtag_get_device(i);
@@ -424,21 +426,30 @@ int jtag_add_ir_scan(int num_fields, scan_field_t *fields, enum tap_state state,
                (*last_cmd)->cmd.scan->fields[i].device = i;
                (*last_cmd)->cmd.scan->fields[i].num_bits = scan_size;
                (*last_cmd)->cmd.scan->fields[i].in_value = NULL;
-               (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;
-               (*last_cmd)->cmd.scan->fields[i].in_handler_priv = NULL;
-               if (jtag_verify_capture_ir)
-               {
-                       jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
-               }
+               (*last_cmd)->cmd.scan->fields[i].in_handler = NULL;     /* disable verification by default */
 
                /* search the list */
-               for (j=0; j < num_fields; j++)
+               for (j = 0; j < num_fields; j++)
                {
                        if (i == fields[j].device)
                        {
                                found = 1;
                                (*last_cmd)->cmd.scan->fields[i].out_value = buf_cpy(fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
                                (*last_cmd)->cmd.scan->fields[i].out_mask = buf_cpy(fields[j].out_mask, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
+                       
+                               if (jtag_verify_capture_ir)
+                               {
+                                       if (fields[j].in_handler==NULL)
+                                       {
+                                               jtag_set_check_value((*last_cmd)->cmd.scan->fields+i, device->expected, device->expected_mask, NULL);
+                                       } else
+                                       {
+                                               (*last_cmd)->cmd.scan->fields[i].in_handler = fields[j].in_handler;
+                                               (*last_cmd)->cmd.scan->fields[i].in_handler_priv = fields[j].in_handler_priv;
+                                               (*last_cmd)->cmd.scan->fields[i].in_check_value = device->expected; 
+                                               (*last_cmd)->cmd.scan->fields[i].in_check_mask = device->expected_mask;
+                                       }
+                               }
                                
                                device->bypass = 0;
                                break;
@@ -562,12 +573,12 @@ int jtag_add_dr_scan(int num_fields, scan_field_t *fields, enum tap_state state,
                        
        cmd_queue_cur_state = cmd_queue_end_state;
        
-       for (i=0; i < jtag_num_devices; i++)
+       for (i = 0; i < jtag_num_devices; i++)
        {
                int found = 0;
                (*last_cmd)->cmd.scan->fields[field_count].device = i;
        
-               for (j=0; j < num_fields; j++)
+               for (j = 0; j < num_fields; j++)
                {
                        if (i == fields[j].device)
                        {
@@ -717,52 +728,18 @@ int jtag_add_pathmove(int num_states, enum tap_state *path)
                return ERROR_JTAG_NOT_IMPLEMENTED;
        }
        
-       if (jtag->support_pathmove)
-       {
-               /* allocate memory for a new list member */
-               *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
-               last_comand_pointer = &((*last_cmd)->next);
-               (*last_cmd)->next = NULL;
-               (*last_cmd)->type = JTAG_PATHMOVE;
+       /* allocate memory for a new list member */
+       *last_cmd = cmd_queue_alloc(sizeof(jtag_command_t));
+       last_comand_pointer = &((*last_cmd)->next);
+       (*last_cmd)->next = NULL;
+       (*last_cmd)->type = JTAG_PATHMOVE;
+
+       (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
+       (*last_cmd)->cmd.pathmove->num_states = num_states;
+       (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
        
-               (*last_cmd)->cmd.pathmove = cmd_queue_alloc(sizeof(pathmove_command_t));
-               (*last_cmd)->cmd.pathmove->num_states = num_states;
-               (*last_cmd)->cmd.pathmove->path = cmd_queue_alloc(sizeof(enum tap_state) * num_states);
-               
-               for (i = 0; i < num_states; i++)
-                       (*last_cmd)->cmd.pathmove->path[i] = path[i];
-       }
-       else
-       {
-               /* validate the desired path, and see if it fits a default path */
-               int begin = 0;
-               int end = 0;
-               int j;
-               
-               for (i = 0; i < num_states; i++)
-               {
-                       for (j = i; j < num_states; j++)
-                       {
-                               if (tap_move_map[path[j]] != -1)
-                               {       
-                                       end = j;
-                                       break;
-                               }
-                       }
-                       
-                       if (begin - end <= 7)   /* a default path spans no more than 7 states */
-                       {
-                               jtag_add_statemove(path[end]);
-                       }
-                       else
-                       {
-                               ERROR("encountered a TAP path that can't be fulfilled by default paths");       
-                               return ERROR_JTAG_NOT_IMPLEMENTED;
-                       }
-                       
-                       i = end;
-               }
-       }
+       for (i = 0; i < num_states; i++)
+               (*last_cmd)->cmd.pathmove->path[i] = path[i];
 
        if (cmd_queue_cur_state == TAP_TLR && cmd_queue_end_state != TAP_TLR)
                jtag_call_event_callbacks(JTAG_TRST_RELEASED);
@@ -952,7 +929,7 @@ int jtag_scan_size(scan_command_t *cmd)
        int i;
 
        /* count bits in scan command */
-       for (i=0; i<cmd->num_fields; i++)
+       for (i = 0; i < cmd->num_fields; i++)
        {
                bit_count += cmd->fields[i].num_bits;
        }
@@ -991,8 +968,6 @@ int jtag_build_buffer(scan_command_t *cmd, u8 **buffer)
 
 }
 
-extern int jtag_check_value(u8 *captured, void *priv);
-
 int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
 {
        int i;
@@ -1002,7 +977,7 @@ int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
        /* we return ERROR_OK, unless a check fails, or a handler reports a problem */
        retval = ERROR_OK;
        
-       for (i=0; i < cmd->num_fields; i++)
+       for (i = 0; i < cmd->num_fields; i++)
        {
                /* if neither in_value nor in_handler
                 * are specified we don't have to examine this field
@@ -1012,38 +987,21 @@ int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
                        int num_bits = cmd->fields[i].num_bits;
                        u8 *captured = buf_set_buf(buffer, bit_count, malloc(CEIL(num_bits, 8)), 0, num_bits);
                        
-                       #ifdef _DEBUG_JTAG_IO_
-                               char *char_buf;
+#ifdef _DEBUG_JTAG_IO_
+                       char *char_buf;
 
-                               char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
-                               DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
-                               free(char_buf);
-                       #endif
+                       char_buf = buf_to_str(captured, (num_bits > 64) ? 64 : num_bits, 16);
+                       DEBUG("fields[%i].in_value: 0x%s", i, char_buf);
+                       free(char_buf);
+#endif
                        
-                       void *priv=cmd->fields[i].in_handler_priv;
-                       if (cmd->fields[i].in_handler==&jtag_check_value)
-                       {
-                               /* Yuk! we want to pass in the pointer to cmd->fields[i] which is not known
-                                * when jtag_check_value is invoked
-                                * 
-                                * Not pretty, but this is part of synthesizing check_mask via in_handler
-                                * with a minimum of code impact.
-                                * 
-                                * A cleaner change would be to modify the in_handler to always take field 
-                                * as an argument. Perhaps later...
-                                * 
-                                * Change in_handler to be varargs and have fields+i as the first vararg?
-                                * 
-                                */
-                               priv=cmd->fields+i;
-                       }
                        if (cmd->fields[i].in_value)
                        {
                                buf_cpy(captured, cmd->fields[i].in_value, num_bits);
                                
                                if (cmd->fields[i].in_handler)
                                {
-                                       if (cmd->fields[i].in_handler(cmd->fields[i].in_value, priv) != ERROR_OK)
+                                       if (cmd->fields[i].in_handler(cmd->fields[i].in_value, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
                                        {
                                                WARNING("in_handler reported a failed check");
                                                retval = ERROR_JTAG_QUEUE_FAILED;
@@ -1054,7 +1012,7 @@ int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
                        /* no in_value specified, but a handler takes care of the scanned data */
                        if (cmd->fields[i].in_handler && (!cmd->fields[i].in_value))
                        {
-                               if (cmd->fields[i].in_handler(captured, priv) != ERROR_OK)
+                               if (cmd->fields[i].in_handler(captured, cmd->fields[i].in_handler_priv, cmd->fields+i) != ERROR_OK)
                                {
                                        /* We're going to call the error:handler later, but if the in_handler
                                         * reported an error we report this failure upstream
@@ -1072,10 +1030,9 @@ int jtag_read_buffer(u8 *buffer, scan_command_t *cmd)
        return retval;
 }
 
-int jtag_check_value(u8 *captured, void *priv)
+int jtag_check_value(u8 *captured, void *priv, scan_field_t *field)
 {
-       int retval=ERROR_OK;
-       scan_field_t *field=(scan_field_t *)priv;
+       int retval = ERROR_OK;
        int num_bits = field->num_bits;
        
        int compare_failed = 0;
@@ -1086,21 +1043,7 @@ int jtag_check_value(u8 *captured, void *priv)
                compare_failed = buf_cmp(captured, field->in_check_value, num_bits);
        
        if (compare_failed)
-       {
-               if (field->in_handler_error_handler.error_handler)
-               {
-                       /* ask the error handler if once has been specified if this is a real problem */ 
-                       if (field->in_handler_error_handler.error_handler(captured, field->in_handler_error_handler.error_handler_priv) != ERROR_OK)
-                               retval = ERROR_JTAG_QUEUE_FAILED;
-                       else
-                               compare_failed = 0;
-               }
-               else
                {
-                       /* if there wasn't a handler specified, we report a failure */
-                       retval = ERROR_JTAG_QUEUE_FAILED;
-               }
-               
                /* An error handler could have caught the failing check
                 * only report a problem when there wasn't a handler, or if the handler
                 * acknowledged the error
@@ -1124,6 +1067,8 @@ int jtag_check_value(u8 *captured, void *priv)
 
                        free(captured_char);
                        free(in_check_value_char);
+                       
+                       retval = ERROR_JTAG_QUEUE_FAILED;
                }
                
        }
@@ -1137,16 +1082,12 @@ int jtag_check_value(u8 *captured, void *priv)
 void jtag_set_check_value(scan_field_t *field, u8 *value, u8 *mask, error_handler_t *in_error_handler)
 {
        if (value)
-               field->in_handler=jtag_check_value;
+               field->in_handler = jtag_check_value;
        else
-               field->in_handler=NULL; /* No check, e.g. embeddedice uses value==NULL to indicate no check */
-       field->in_handler_priv=NULL; /* this will be filled in at the invocation site to point to the field duplicate */ 
-       field->in_check_value=value;
-       field->in_check_mask=mask;
-       if (in_error_handler)
-               field->in_handler_error_handler=*in_error_handler;
-       else
-               field->in_handler_error_handler.error_handler=NULL;
+               field->in_handler = NULL;       /* No check, e.g. embeddedice uses value==NULL to indicate no check */
+       field->in_handler_priv = NULL;  /* this will be filled in at the invocation site to point to the field duplicate */ 
+       field->in_check_value = value;
+       field->in_check_mask = mask;
 }
 
 enum scan_type jtag_scan_type(scan_command_t *cmd)
@@ -1154,7 +1095,7 @@ enum scan_type jtag_scan_type(scan_command_t *cmd)
        int i;
        int type = 0;
        
-       for (i=0; i < cmd->num_fields; i++)
+       for (i = 0; i < cmd->num_fields; i++)
        {
                if (cmd->fields[i].in_value || cmd->fields[i].in_handler)
                        type |= SCAN_IN;
@@ -1371,7 +1312,7 @@ int jtag_register_commands(struct command_context_s *cmd_ctx)
        register_command(cmd_ctx, NULL, "jtag_speed", handle_jtag_speed_command,
                COMMAND_ANY, "set jtag speed (if supported) <speed>");
        register_command(cmd_ctx, NULL, "jtag_device", handle_jtag_device_command,
-               COMMAND_CONFIG, NULL);
+               COMMAND_CONFIG, "jtag_device <ir_length> <ir_expected> <ir_mask>");
        register_command(cmd_ctx, NULL, "reset_config", handle_reset_config_command,
                COMMAND_CONFIG, NULL);
        register_command(cmd_ctx, NULL, "jtag_nsrst_delay", handle_jtag_nsrst_delay_command,

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)