openocd: fix SPDX tag format for files .c
[fw/openocd] / src / svf / svf.c
index 69f75ac79a7a0ed1d784949de3d24f04aaaba75b..a5374316eafa5f8b6a9d2e026feaca295b9a7a73 100644 (file)
@@ -1,30 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /***************************************************************************
  *    Copyright (C) 2009 by Simon Qian                                     *
  *    SimonQian@SimonQian.com                                              *
- *                                                                         *
- *   This program is free software; you can redistribute it and/or modify  *
- *   it under the terms of the GNU General Public License as published by  *
- *   the Free Software Foundation; either version 2 of the License, or     *
- *   (at your option) any later version.                                   *
- *                                                                         *
- *   This program is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
- *   GNU General Public License for more details.                          *
- *                                                                         *
- *   You should have received a copy of the GNU General Public License     *
- *   along with this program; if not, write to the                         *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
  ***************************************************************************/
 
 /* The specification for SVF is available here:
  * http://www.asset-intertech.com/support/svf.pdf
- * Below, this document is refered to as the "SVF spec".
+ * Below, this document is referred to as the "SVF spec".
  *
  * The specification for XSVF is available here:
  * http://www.xilinx.com/support/documentation/application_notes/xapp503.pdf
- * Below, this document is refered to as the "XSVF spec".
+ * Below, this document is referred to as the "XSVF spec".
  */
 
 #ifdef HAVE_CONFIG_H
@@ -33,6 +20,7 @@
 
 #include <jtag/jtag.h>
 #include "svf.h"
+#include "helper/system.h"
 #include <helper/time_support.h>
 
 /* SVF command */
@@ -216,22 +204,24 @@ static int svf_read_command_from_file(FILE *fd);
 static int svf_check_tdo(void);
 static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len);
 static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str);
+static int svf_execute_tap(void);
 
 static FILE *svf_fd;
 static char *svf_read_line;
 static size_t svf_read_line_size;
 static char *svf_command_buffer;
 static size_t svf_command_buffer_size;
-static int svf_line_number = 1;
+static int svf_line_number;
 static int svf_getline(char **lineptr, size_t *n, FILE *stream);
 
 #define SVF_MAX_BUFFER_SIZE_TO_COMMIT   (1024 * 1024)
 static uint8_t *svf_tdi_buffer, *svf_tdo_buffer, *svf_mask_buffer;
-static int svf_buffer_index, svf_buffer_size ;
+static int svf_buffer_index, svf_buffer_size;
 static int svf_quiet;
 static int svf_nil;
+static int svf_ignore_error;
 
-/* Targetting particular tap */
+/* Targeting particular tap */
 static int svf_tap_is_specified;
 static int svf_set_padding(struct svf_xxr_para *para, int len, unsigned char tdi);
 
@@ -241,40 +231,79 @@ static long svf_total_lines;
 static int svf_percentage;
 static int svf_last_printed_percentage = -1;
 
-static void svf_free_xxd_para(struct svf_xxr_para *para)
+/*
+ * macro is used to print the svf hex buffer at desired debug level
+ * DEBUG, INFO, ERROR, USER
+ */
+#define SVF_BUF_LOG(_lvl, _buf, _nbits, _desc)                                                 \
+       svf_hexbuf_print(LOG_LVL_##_lvl,  __FILE__, __LINE__, __func__, _buf, _nbits, _desc)
+
+static void svf_hexbuf_print(int dbg_lvl, const char *file, unsigned line,
+                                                        const char *function, const uint8_t *buf,
+                                                        int bit_len, const char *desc)
 {
-       if (NULL != para) {
-               if (para->tdi != NULL) {
-                       free(para->tdi);
-                       para->tdi = NULL;
-               }
-               if (para->tdo != NULL) {
-                       free(para->tdo);
-                       para->tdo = NULL;
-               }
-               if (para->mask != NULL) {
-                       free(para->mask);
-                       para->mask = NULL;
-               }
-               if (para->smask != NULL) {
-                       free(para->smask);
-                       para->smask = NULL;
-               }
-       }
+       int j, len = 0;
+       int byte_len = DIV_ROUND_UP(bit_len, 8);
+       int msbits = bit_len % 8;
+
+       /* allocate 2 bytes per hex digit */
+       char *prbuf = malloc((byte_len * 2) + 2 + 1);
+       if (!prbuf)
+               return;
+
+       /* print correct number of bytes, mask excess bits where applicable */
+       uint8_t msb = buf[byte_len - 1] & (msbits ? (1 << msbits) - 1 : 0xff);
+       len = sprintf(prbuf, msbits <= 4 ? "0x%01"PRIx8 : "0x%02"PRIx8, msb);
+       for (j = byte_len - 2; j >= 0; j--)
+               len += sprintf(prbuf + len, "%02"PRIx8, buf[j]);
+
+       log_printf_lf(dbg_lvl, file, line, function, "%8s = %s", desc ? desc : " ", prbuf);
+
+       free(prbuf);
 }
 
-static unsigned svf_get_mask_u32(int bitlen)
+static int svf_realloc_buffers(size_t len)
 {
-       uint32_t bitmask;
+       void *ptr;
 
-       if (bitlen < 0)
-               bitmask = 0;
-       else if (bitlen >= 32)
-               bitmask = 0xFFFFFFFF;
-       else
-               bitmask = (1 << bitlen) - 1;
+       if (svf_execute_tap() != ERROR_OK)
+               return ERROR_FAIL;
+
+       ptr = realloc(svf_tdi_buffer, len);
+       if (!ptr)
+               return ERROR_FAIL;
+       svf_tdi_buffer = ptr;
+
+       ptr = realloc(svf_tdo_buffer, len);
+       if (!ptr)
+               return ERROR_FAIL;
+       svf_tdo_buffer = ptr;
+
+       ptr = realloc(svf_mask_buffer, len);
+       if (!ptr)
+               return ERROR_FAIL;
+       svf_mask_buffer = ptr;
+
+       svf_buffer_size = len;
+
+       return ERROR_OK;
+}
+
+static void svf_free_xxd_para(struct svf_xxr_para *para)
+{
+       if (para) {
+               free(para->tdi);
+               para->tdi = NULL;
+
+               free(para->tdo);
+               para->tdo = NULL;
 
-       return bitmask;
+               free(para->mask);
+               para->mask = NULL;
+
+               free(para->smask);
+               para->smask = NULL;
+       }
 }
 
 int svf_add_statemove(tap_state_t state_to)
@@ -317,7 +346,7 @@ COMMAND_HANDLER(handle_svf_command)
 #define SVF_MAX_NUM_OF_OPTIONS 5
        int command_num = 0;
        int ret = ERROR_OK;
-       long long time_measure_ms;
+       int64_t time_measure_ms;
        int time_measure_s, time_measure_m;
 
        /* use NULL to indicate a "plain" svf file which accounts for
@@ -332,11 +361,13 @@ COMMAND_HANDLER(handle_svf_command)
        /* parse command line */
        svf_quiet = 0;
        svf_nil = 0;
+       svf_progress_enabled = 0;
+       svf_ignore_error = 0;
        for (unsigned int i = 0; i < CMD_ARGC; i++) {
                if (strcmp(CMD_ARGV[i], "-tap") == 0) {
                        tap = jtag_tap_by_string(CMD_ARGV[i+1]);
                        if (!tap) {
-                               command_print(CMD_CTX, "Tap: %s unknown", CMD_ARGV[i+1]);
+                               command_print(CMD, "Tap: %s unknown", CMD_ARGV[i+1]);
                                return ERROR_FAIL;
                        }
                        i++;
@@ -348,11 +379,14 @@ COMMAND_HANDLER(handle_svf_command)
                else if ((strcmp(CMD_ARGV[i],
                                  "progress") == 0) || (strcmp(CMD_ARGV[i], "-progress") == 0))
                        svf_progress_enabled = 1;
+               else if ((strcmp(CMD_ARGV[i],
+                                 "ignore_error") == 0) || (strcmp(CMD_ARGV[i], "-ignore_error") == 0))
+                       svf_ignore_error = 1;
                else {
                        svf_fd = fopen(CMD_ARGV[i], "r");
-                       if (svf_fd == NULL) {
+                       if (!svf_fd) {
                                int err = errno;
-                               command_print(CMD_CTX, "open(\"%s\"): %s", CMD_ARGV[i], strerror(err));
+                               command_print(CMD, "open(\"%s\"): %s", CMD_ARGV[i], strerror(err));
                                /* no need to free anything now */
                                return ERROR_COMMAND_SYNTAX_ERROR;
                        } else
@@ -360,19 +394,19 @@ COMMAND_HANDLER(handle_svf_command)
                }
        }
 
-       if (svf_fd == NULL)
+       if (!svf_fd)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
        /* get time */
        time_measure_ms = timeval_ms();
 
        /* init */
-       svf_line_number = 1;
+       svf_line_number = 0;
        svf_command_buffer_size = 0;
 
        svf_check_tdo_para_index = 0;
        svf_check_tdo_para = malloc(sizeof(struct svf_check_tdo_para) * SVF_CHECK_TDO_PARA_SIZE);
-       if (NULL == svf_check_tdo_para) {
+       if (!svf_check_tdo_para) {
                LOG_ERROR("not enough memory");
                ret = ERROR_FAIL;
                goto free_all;
@@ -383,25 +417,10 @@ COMMAND_HANDLER(handle_svf_command)
        /* in case current command cannot be committed, and next command is a bit scan command */
        /* here is 32K bits for this big scan command, it should be enough */
        /* buffer will be reallocated if buffer size is not enough */
-       svf_tdi_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
-       if (NULL == svf_tdi_buffer) {
-               LOG_ERROR("not enough memory");
-               ret = ERROR_FAIL;
-               goto free_all;
-       }
-       svf_tdo_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
-       if (NULL == svf_tdo_buffer) {
-               LOG_ERROR("not enough memory");
-               ret = ERROR_FAIL;
-               goto free_all;
-       }
-       svf_mask_buffer = (uint8_t *)malloc(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT);
-       if (NULL == svf_mask_buffer) {
-               LOG_ERROR("not enough memory");
+       if (svf_realloc_buffers(2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT) != ERROR_OK) {
                ret = ERROR_FAIL;
                goto free_all;
        }
-       svf_buffer_size = 2 * SVF_MAX_BUFFER_SIZE_TO_COMMIT;
 
        memcpy(&svf_para, &svf_para_init, sizeof(svf_para));
 
@@ -430,25 +449,25 @@ COMMAND_HANDLER(handle_svf_command)
                }
 
                /* HDR %d TDI (0) */
-               if (ERROR_OK != svf_set_padding(&svf_para.hdr_para, header_dr_len, 0)) {
+               if (svf_set_padding(&svf_para.hdr_para, header_dr_len, 0) != ERROR_OK) {
                        LOG_ERROR("failed to set data header");
                        return ERROR_FAIL;
                }
 
                /* HIR %d TDI (0xFF) */
-               if (ERROR_OK != svf_set_padding(&svf_para.hir_para, header_ir_len, 0xFF)) {
+               if (svf_set_padding(&svf_para.hir_para, header_ir_len, 0xFF) != ERROR_OK) {
                        LOG_ERROR("failed to set instruction header");
                        return ERROR_FAIL;
                }
 
                /* TDR %d TDI (0) */
-               if (ERROR_OK != svf_set_padding(&svf_para.tdr_para, trailer_dr_len, 0)) {
+               if (svf_set_padding(&svf_para.tdr_para, trailer_dr_len, 0) != ERROR_OK) {
                        LOG_ERROR("failed to set data trailer");
                        return ERROR_FAIL;
                }
 
                /* TIR %d TDI (0xFF) */
-               if (ERROR_OK != svf_set_padding(&svf_para.tir_para, trailer_ir_len, 0xFF)) {
+               if (svf_set_padding(&svf_para.tir_para, trailer_ir_len, 0xFF) != ERROR_OK) {
                        LOG_ERROR("failed to set instruction trailer");
                        return ERROR_FAIL;
                }
@@ -462,7 +481,7 @@ COMMAND_HANDLER(handle_svf_command)
                }
                rewind(svf_fd);
        }
-       while (ERROR_OK == svf_read_command_from_file(svf_fd)) {
+       while (svf_read_command_from_file(svf_fd) == ERROR_OK) {
                /* Log Output */
                if (svf_quiet) {
                        if (svf_progress_enabled) {
@@ -480,7 +499,7 @@ COMMAND_HANDLER(handle_svf_command)
                                LOG_USER_N("%s", svf_read_line);
                }
                /* Run Command */
-               if (ERROR_OK != svf_run_command(CMD_CTX, svf_command_buffer)) {
+               if (svf_run_command(CMD_CTX, svf_command_buffer) != ERROR_OK) {
                        LOG_ERROR("fail to run command at line %d", svf_line_number);
                        ret = ERROR_FAIL;
                        break;
@@ -488,9 +507,9 @@ COMMAND_HANDLER(handle_svf_command)
                command_num++;
        }
 
-       if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
+       if ((!svf_nil) && (jtag_execute_queue() != ERROR_OK))
                ret = ERROR_FAIL;
-       else if (ERROR_OK != svf_check_tdo())
+       else if (svf_check_tdo() != ERROR_OK)
                ret = ERROR_FAIL;
 
        /* print time */
@@ -500,8 +519,8 @@ COMMAND_HANDLER(handle_svf_command)
        time_measure_m = time_measure_s / 60;
        time_measure_s %= 60;
        if (time_measure_ms < 1000)
-               command_print(CMD_CTX,
-                       "\r\nTime used: %dm%ds%lldms ",
+               command_print(CMD,
+                       "\r\nTime used: %dm%ds%" PRId64 "ms ",
                        time_measure_m,
                        time_measure_s,
                        time_measure_ms);
@@ -512,28 +531,23 @@ free_all:
        svf_fd = 0;
 
        /* free buffers */
-       if (svf_command_buffer) {
-               free(svf_command_buffer);
-               svf_command_buffer = NULL;
-               svf_command_buffer_size = 0;
-       }
-       if (svf_check_tdo_para) {
-               free(svf_check_tdo_para);
-               svf_check_tdo_para = NULL;
-               svf_check_tdo_para_index = 0;
-       }
-       if (svf_tdi_buffer) {
-               free(svf_tdi_buffer);
-               svf_tdi_buffer = NULL;
-       }
-       if (svf_tdo_buffer) {
-               free(svf_tdo_buffer);
-               svf_tdo_buffer = NULL;
-       }
-       if (svf_mask_buffer) {
-               free(svf_mask_buffer);
-               svf_mask_buffer = NULL;
-       }
+       free(svf_command_buffer);
+       svf_command_buffer = NULL;
+       svf_command_buffer_size = 0;
+
+       free(svf_check_tdo_para);
+       svf_check_tdo_para = NULL;
+       svf_check_tdo_para_index = 0;
+
+       free(svf_tdi_buffer);
+       svf_tdi_buffer = NULL;
+
+       free(svf_tdo_buffer);
+       svf_tdo_buffer = NULL;
+
+       free(svf_mask_buffer);
+       svf_mask_buffer = NULL;
+
        svf_buffer_index = 0;
        svf_buffer_size = 0;
 
@@ -544,13 +558,16 @@ free_all:
        svf_free_xxd_para(&svf_para.sdr_para);
        svf_free_xxd_para(&svf_para.sir_para);
 
-       if (ERROR_OK == ret)
-               command_print(CMD_CTX,
-                       "svf file programmed successfully for %d commands",
-                       command_num);
+       if (ret == ERROR_OK)
+               command_print(CMD,
+                             "svf file programmed %s for %d commands with %d errors",
+                             (svf_ignore_error > 1) ? "unsuccessfully" : "successfully",
+                             command_num,
+                             (svf_ignore_error > 1) ? (svf_ignore_error - 1) : 0);
        else
-               command_print(CMD_CTX, "svf file programmed failed");
+               command_print(CMD, "svf file programmed failed");
 
+       svf_ignore_error = 0;
        return ret;
 }
 
@@ -559,9 +576,9 @@ static int svf_getline(char **lineptr, size_t *n, FILE *stream)
 #define MIN_CHUNK 16   /* Buffer is increased by this size each time as required */
        size_t i = 0;
 
-       if (*lineptr == NULL) {
+       if (!*lineptr) {
                *n = MIN_CHUNK;
-               *lineptr = (char *)malloc(*n);
+               *lineptr = malloc(*n);
                if (!*lineptr)
                        return -1;
        }
@@ -624,11 +641,13 @@ static int svf_read_command_from_file(FILE *fd)
                                if (svf_getline(&svf_read_line, &svf_read_line_size, svf_fd) <= 0)
                                        return ERROR_FAIL;
                                i = -1;
+                               /* fallthrough */
                        case '\r':
                                slash = 0;
                                /* Don't save '\r' and '\n' if no data is parsed */
                                if (!cmd_pos)
                                        break;
+                               /* fallthrough */
                        default:
                                /* The parsing code currently expects a space
                                 * before parentheses -- "TDI (123)".  Also a
@@ -642,9 +661,10 @@ static int svf_read_command_from_file(FILE *fd)
                                 *  - added space.
                                 *  - terminating NUL ('\0')
                                 */
-                               if ((cmd_pos + 2) >= svf_command_buffer_size) {
-                                       svf_command_buffer = realloc(svf_command_buffer, (cmd_pos + 2));
-                                       if (svf_command_buffer == NULL) {
+                               if (cmd_pos + 3 > svf_command_buffer_size) {
+                                       svf_command_buffer = realloc(svf_command_buffer, cmd_pos + 3);
+                                       svf_command_buffer_size = cmd_pos + 3;
+                                       if (!svf_command_buffer) {
                                                LOG_ERROR("not enough memory");
                                                return ERROR_FAIL;
                                        }
@@ -701,6 +721,9 @@ parse_char:
                pos++;
        }
 
+       if (num == 0)
+               return ERROR_FAIL;
+
        *num_of_argu = num;
 
        return ERROR_OK;
@@ -708,8 +731,8 @@ parse_char:
 
 bool svf_tap_state_is_stable(tap_state_t state)
 {
-       return (TAP_RESET == state) || (TAP_IDLE == state)
-                       || (TAP_DRPAUSE == state) || (TAP_IRPAUSE == state);
+       return (state == TAP_RESET) || (state == TAP_IDLE)
+                       || (state == TAP_DRPAUSE) || (state == TAP_IRPAUSE);
 }
 
 static int svf_find_string_in_array(char *str, char **strs, int num_of_element)
@@ -727,17 +750,13 @@ static int svf_adjust_array_length(uint8_t **arr, int orig_bit_len, int new_bit_
 {
        int new_byte_len = (new_bit_len + 7) >> 3;
 
-       if ((NULL == *arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3))) {
-               if (*arr != NULL) {
-                       free(*arr);
-                       *arr = NULL;
-               }
-               *arr = (uint8_t *)malloc(new_byte_len);
-               if (NULL == *arr) {
+       if ((!*arr) || (((orig_bit_len + 7) >> 3) < ((new_bit_len + 7) >> 3))) {
+               free(*arr);
+               *arr = calloc(1, new_byte_len);
+               if (!*arr) {
                        LOG_ERROR("not enough memory");
                        return ERROR_FAIL;
                }
-               memset(*arr, 0, new_byte_len);
        }
        return ERROR_OK;
 }
@@ -760,7 +779,7 @@ static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_l
        int i, str_len = strlen(str), str_hbyte_len = (bit_len + 3) >> 2;
        uint8_t ch = 0;
 
-       if (ERROR_OK != svf_adjust_array_length(bin, orig_bit_len, bit_len)) {
+       if (svf_adjust_array_length(bin, orig_bit_len, bit_len) != ERROR_OK) {
                LOG_ERROR("fail to adjust length of array");
                return ERROR_FAIL;
        }
@@ -811,7 +830,7 @@ static int svf_copy_hexstring_to_binary(char *str, uint8_t **bin, int orig_bit_l
 
        /* check validity: we must have consumed everything */
        if (str_len > 0 || (ch & ~((2 << ((bit_len - 1) % 4)) - 1)) != 0) {
-               LOG_ERROR("value execeeds length");
+               LOG_ERROR("value exceeds length");
                return ERROR_FAIL;
        }
 
@@ -828,20 +847,16 @@ static int svf_check_tdo(void)
                if ((svf_check_tdo_para[i].enabled)
                                && buf_cmp_mask(&svf_tdi_buffer[index_var], &svf_tdo_buffer[index_var],
                                &svf_mask_buffer[index_var], len)) {
-                       unsigned bitmask;
-                       unsigned received, expected, tapmask;
-                       bitmask = svf_get_mask_u32(svf_check_tdo_para[i].bit_len);
-
-                       memcpy(&received, svf_tdi_buffer + index_var, sizeof(unsigned));
-                       memcpy(&expected, svf_tdo_buffer + index_var, sizeof(unsigned));
-                       memcpy(&tapmask, svf_mask_buffer + index_var, sizeof(unsigned));
                        LOG_ERROR("tdo check error at line %d",
                                svf_check_tdo_para[i].line_num);
-                       LOG_ERROR("read = 0x%X, want = 0x%X, mask = 0x%X",
-                               received & bitmask,
-                               expected & bitmask,
-                               tapmask & bitmask);
-                       return ERROR_FAIL;
+                       SVF_BUF_LOG(ERROR, &svf_tdi_buffer[index_var], len, "READ");
+                       SVF_BUF_LOG(ERROR, &svf_tdo_buffer[index_var], len, "WANT");
+                       SVF_BUF_LOG(ERROR, &svf_mask_buffer[index_var], len, "MASK");
+
+                       if (svf_ignore_error == 0)
+                               return ERROR_FAIL;
+                       else
+                               svf_ignore_error++;
                }
        }
        svf_check_tdo_para_index = 0;
@@ -867,9 +882,9 @@ static int svf_add_check_para(uint8_t enabled, int buffer_offset, int bit_len)
 
 static int svf_execute_tap(void)
 {
-       if ((!svf_nil) && (ERROR_OK != jtag_execute_queue()))
+       if ((!svf_nil) && (jtag_execute_queue() != ERROR_OK))
                return ERROR_FAIL;
-       else if (ERROR_OK != svf_check_tdo())
+       else if (svf_check_tdo() != ERROR_OK)
                return ERROR_FAIL;
 
        svf_buffer_index = 0;
@@ -897,7 +912,7 @@ static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
        /* flag padding commands skipped due to -tap command */
        int padding_command_skipped = 0;
 
-       if (ERROR_OK != svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu))
+       if (svf_parse_cmd_string(cmd_str, strlen(cmd_str), argus, &num_of_argu) != ERROR_OK)
                return ERROR_FAIL;
 
        /* NOTE: we're a bit loose here, because we ignore case in
@@ -937,7 +952,7 @@ static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
                                LOG_ERROR("invalid parameter of %s", argus[0]);
                                return ERROR_FAIL;
                        }
-                       if (1 == num_of_argu) {
+                       if (num_of_argu == 1) {
                                /* TODO: set jtag speed to full speed */
                                svf_para.frequency = 0;
                        } else {
@@ -945,13 +960,13 @@ static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
                                        LOG_ERROR("HZ not found in FREQUENCY command");
                                        return ERROR_FAIL;
                                }
-                               if (ERROR_OK != svf_execute_tap())
+                               if (svf_execute_tap() != ERROR_OK)
                                        return ERROR_FAIL;
                                svf_para.frequency = atof(argus[1]);
                                /* TODO: set jtag speed to */
                                if (svf_para.frequency > 0) {
                                        command_run_linef(cmd_ctx,
-                                                       "adapter_khz %d",
+                                                       "adapter speed %d",
                                                        (int)svf_para.frequency / 1000);
                                        LOG_DEBUG("\tfrequency = %f", svf_para.frequency);
                                }
@@ -963,35 +978,35 @@ static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
                                break;
                        }
                        xxr_para_tmp = &svf_para.hdr_para;
-                       goto XXR_common;
+                       goto xxr_common;
                case HIR:
                        if (svf_tap_is_specified) {
                                padding_command_skipped = 1;
                                break;
                        }
                        xxr_para_tmp = &svf_para.hir_para;
-                       goto XXR_common;
+                       goto xxr_common;
                case TDR:
                        if (svf_tap_is_specified) {
                                padding_command_skipped = 1;
                                break;
                        }
                        xxr_para_tmp = &svf_para.tdr_para;
-                       goto XXR_common;
+                       goto xxr_common;
                case TIR:
                        if (svf_tap_is_specified) {
                                padding_command_skipped = 1;
                                break;
                        }
                        xxr_para_tmp = &svf_para.tir_para;
-                       goto XXR_common;
+                       goto xxr_common;
                case SDR:
                        xxr_para_tmp = &svf_para.sdr_para;
-                       goto XXR_common;
+                       goto xxr_common;
                case SIR:
                        xxr_para_tmp = &svf_para.sir_para;
-                       goto XXR_common;
-XXR_common:
+                       goto xxr_common;
+xxr_common:
                        /* XXR length [TDI (tdi)] [TDO (tdo)][MASK (mask)] [SMASK (smask)] */
                        if ((num_of_argu > 10) || (num_of_argu % 2)) {
                                LOG_ERROR("invalid parameter of %s", argus[0]);
@@ -999,6 +1014,19 @@ XXR_common:
                        }
                        i_tmp = xxr_para_tmp->len;
                        xxr_para_tmp->len = atoi(argus[1]);
+                       /* If we are to enlarge the buffers, all parts of xxr_para_tmp
+                        * need to be freed */
+                       if (i_tmp < xxr_para_tmp->len) {
+                               free(xxr_para_tmp->tdi);
+                               xxr_para_tmp->tdi = NULL;
+                               free(xxr_para_tmp->tdo);
+                               xxr_para_tmp->tdo = NULL;
+                               free(xxr_para_tmp->mask);
+                               xxr_para_tmp->mask = NULL;
+                               free(xxr_para_tmp->smask);
+                               xxr_para_tmp->smask = NULL;
+                       }
+
                        LOG_DEBUG("\tlength = %d", xxr_para_tmp->len);
                        xxr_para_tmp->data_mask = 0;
                        for (i = 2; i < num_of_argu; i += 2) {
@@ -1026,7 +1054,7 @@ XXR_common:
                                        pbuffer_tmp = &xxr_para_tmp->smask;
                                        xxr_para_tmp->data_mask |= XXR_SMASK;
                                } else {
-                                       LOG_ERROR("unknow parameter: %s", argus[i]);
+                                       LOG_ERROR("unknown parameter: %s", argus[i]);
                                        return ERROR_FAIL;
                                }
                                if (ERROR_OK !=
@@ -1035,8 +1063,7 @@ XXR_common:
                                        LOG_ERROR("fail to parse hex value");
                                        return ERROR_FAIL;
                                }
-                               LOG_DEBUG("\t%s = 0x%X", argus[i],
-                                               (**(int **)pbuffer_tmp) & svf_get_mask_u32(xxr_para_tmp->len));
+                               SVF_BUF_LOG(DEBUG, *pbuffer_tmp, xxr_para_tmp->len, argus[i]);
                        }
                        /* If a command changes the length of the last scan of the same type and the
                         * MASK parameter is absent, */
@@ -1053,7 +1080,7 @@ XXR_common:
                        }
                        /* If TDO is absent, no comparison is needed, set the mask to 0 */
                        if (!(xxr_para_tmp->data_mask & XXR_TDO)) {
-                               if (NULL == xxr_para_tmp->tdo) {
+                               if (!xxr_para_tmp->tdo) {
                                        if (ERROR_OK !=
                                        svf_adjust_array_length(&xxr_para_tmp->tdo, i_tmp,
                                                xxr_para_tmp->len)) {
@@ -1061,7 +1088,7 @@ XXR_common:
                                                return ERROR_FAIL;
                                        }
                                }
-                               if (NULL == xxr_para_tmp->mask) {
+                               if (!xxr_para_tmp->mask) {
                                        if (ERROR_OK !=
                                        svf_adjust_array_length(&xxr_para_tmp->mask, i_tmp,
                                                xxr_para_tmp->len)) {
@@ -1072,52 +1099,16 @@ XXR_common:
                                memset(xxr_para_tmp->mask, 0, (xxr_para_tmp->len + 7) >> 3);
                        }
                        /* do scan if necessary */
-                       if (SDR == command) {
+                       if (command == SDR) {
                                /* check buffer size first, reallocate if necessary */
                                i = svf_para.hdr_para.len + svf_para.sdr_para.len +
                                                svf_para.tdr_para.len;
                                if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
-#if 1
-                                       /* simply print error message */
-                                       LOG_ERROR("buffer is not enough, report to author");
-                                       return ERROR_FAIL;
-#else
-                                       uint8_t *buffer_tmp;
-
                                        /* reallocate buffer */
-                                       buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
-                                               LOG_ERROR("not enough memory");
-                                               return ERROR_FAIL;
-                                       }
-                                       memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
-                                       /* svf_tdi_buffer isn't NULL here */
-                                       free(svf_tdi_buffer);
-                                       svf_tdi_buffer = buffer_tmp;
-
-                                       buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
+                                       if (svf_realloc_buffers(svf_buffer_index + ((i + 7) >> 3)) != ERROR_OK) {
                                                LOG_ERROR("not enough memory");
                                                return ERROR_FAIL;
                                        }
-                                       memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
-                                       /* svf_tdo_buffer isn't NULL here */
-                                       free(svf_tdo_buffer);
-                                       svf_tdo_buffer = buffer_tmp;
-
-                                       buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
-                                               LOG_ERROR("not enough memory");
-                                               return ERROR_FAIL;
-                                       }
-                                       memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
-                                       /* svf_mask_buffer isn't NULL here */
-                                       free(svf_mask_buffer);
-                                       svf_mask_buffer = buffer_tmp;
-
-                                       buffer_tmp = NULL;
-                                       svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
-#endif
                                }
 
                                /* assemble dr data */
@@ -1189,7 +1180,7 @@ XXR_common:
                                        svf_add_check_para(0, svf_buffer_index, i);
                                field.num_bits = i;
                                field.out_value = &svf_tdi_buffer[svf_buffer_index];
-                               field.in_value = &svf_tdi_buffer[svf_buffer_index];
+                               field.in_value = (xxr_para_tmp->data_mask & XXR_TDO) ? &svf_tdi_buffer[svf_buffer_index] : NULL;
                                if (!svf_nil) {
                                        /* NOTE:  doesn't use SVF-specified state paths */
                                        jtag_add_plain_dr_scan(field.num_bits,
@@ -1199,52 +1190,15 @@ XXR_common:
                                }
 
                                svf_buffer_index += (i + 7) >> 3;
-                       } else if (SIR == command) {
+                       } else if (command == SIR) {
                                /* check buffer size first, reallocate if necessary */
                                i = svf_para.hir_para.len + svf_para.sir_para.len +
                                                svf_para.tir_para.len;
                                if ((svf_buffer_size - svf_buffer_index) < ((i + 7) >> 3)) {
-#if 1
-                                       /* simply print error message */
-                                       LOG_ERROR("buffer is not enough, report to author");
-                                       return ERROR_FAIL;
-#else
-                                       uint8_t *buffer_tmp;
-
-                                       /* reallocate buffer */
-                                       buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
-                                               LOG_ERROR("not enough memory");
-                                               return ERROR_FAIL;
-                                       }
-                                       memcpy(buffer_tmp, svf_tdi_buffer, svf_buffer_index);
-                                       /* svf_tdi_buffer isn't NULL here */
-                                       free(svf_tdi_buffer);
-                                       svf_tdi_buffer = buffer_tmp;
-
-                                       buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
-                                               LOG_ERROR("not enough memory");
-                                               return ERROR_FAIL;
-                                       }
-                                       memcpy(buffer_tmp, svf_tdo_buffer, svf_buffer_index);
-                                       /* svf_tdo_buffer isn't NULL here */
-                                       free(svf_tdo_buffer);
-                                       svf_tdo_buffer = buffer_tmp;
-
-                                       buffer_tmp = (uint8_t *)malloc(svf_buffer_index + ((i + 7) >> 3));
-                                       if (NULL == buffer_tmp) {
+                                       if (svf_realloc_buffers(svf_buffer_index + ((i + 7) >> 3)) != ERROR_OK) {
                                                LOG_ERROR("not enough memory");
                                                return ERROR_FAIL;
                                        }
-                                       memcpy(buffer_tmp, svf_mask_buffer, svf_buffer_index);
-                                       /* svf_mask_buffer isn't NULL here */
-                                       free(svf_mask_buffer);
-                                       svf_mask_buffer = buffer_tmp;
-
-                                       buffer_tmp = NULL;
-                                       svf_buffer_size = svf_buffer_index + ((i + 7) >> 3);
-#endif
                                }
 
                                /* assemble ir data */
@@ -1316,7 +1270,7 @@ XXR_common:
                                        svf_add_check_para(0, svf_buffer_index, i);
                                field.num_bits = i;
                                field.out_value = &svf_tdi_buffer[svf_buffer_index];
-                               field.in_value = &svf_tdi_buffer[svf_buffer_index];
+                               field.in_value = (xxr_para_tmp->data_mask & XXR_TDO) ? &svf_tdi_buffer[svf_buffer_index] : NULL;
                                if (!svf_nil) {
                                        /* NOTE:  doesn't use SVF-specified state paths */
                                        jtag_add_plain_ir_scan(field.num_bits,
@@ -1332,13 +1286,12 @@ XXR_common:
                case PIOMAP:
                        LOG_ERROR("PIO and PIOMAP are not supported");
                        return ERROR_FAIL;
-                       break;
                case RUNTEST:
                        /* RUNTEST [run_state] run_count run_clk [min_time SEC [MAXIMUM max_time
                         * SEC]] [ENDSTATE end_state] */
                        /* RUNTEST [run_state] min_time SEC [MAXIMUM max_time SEC] [ENDSTATE
                         * end_state] */
-                       if ((num_of_argu < 3) && (num_of_argu > 11)) {
+                       if ((num_of_argu < 3) || (num_of_argu > 11)) {
                                LOG_ERROR("invalid parameter of %s", argus[0]);
                                return ERROR_FAIL;
                        }
@@ -1455,8 +1408,8 @@ XXR_common:
                        }
                        if (num_of_argu > 2) {
                                /* STATE pathstate1 ... stable_state */
-                               path = (tap_state_t *)malloc((num_of_argu - 1) * sizeof(tap_state_t));
-                               if (NULL == path) {
+                               path = malloc((num_of_argu - 1) * sizeof(tap_state_t));
+                               if (!path) {
                                        LOG_ERROR("not enough memory");
                                        return ERROR_FAIL;
                                }
@@ -1470,7 +1423,7 @@ XXR_common:
                                                return ERROR_FAIL;
                                        }
                                        /* OpenOCD refuses paths containing TAP_RESET */
-                                       if (TAP_RESET == path[i]) {
+                                       if (path[i] == TAP_RESET) {
                                                /* FIXME last state MUST be stable! */
                                                if (i > 0) {
                                                        if (!svf_nil)
@@ -1523,7 +1476,7 @@ XXR_common:
                                return ERROR_FAIL;
                        }
                        if (svf_para.trst_mode != TRST_ABSENT) {
-                               if (ERROR_OK != svf_execute_tap())
+                               if (svf_execute_tap() != ERROR_OK)
                                        return ERROR_FAIL;
                                i_tmp = svf_find_string_in_array(argus[1],
                                                (char **)svf_trst_mode_name,
@@ -1547,14 +1500,13 @@ XXR_common:
                                svf_para.trst_mode = i_tmp;
                                LOG_DEBUG("\ttrst_mode = %s", svf_trst_mode_name[svf_para.trst_mode]);
                        } else {
-                               LOG_ERROR("can not accpet TRST command if trst_mode is ABSENT");
+                               LOG_ERROR("can not accept TRST command if trst_mode is ABSENT");
                                return ERROR_FAIL;
                        }
                        break;
                default:
                        LOG_ERROR("invalid svf command: %s", argus[0]);
                        return ERROR_FAIL;
-                       break;
        }
 
        if (!svf_quiet) {
@@ -1564,27 +1516,22 @@ XXR_common:
 
        if (debug_level >= LOG_LVL_DEBUG) {
                /* for convenient debugging, execute tap if possible */
-               if ((svf_buffer_index > 0) && \
-                               (((command != STATE) && (command != RUNTEST)) || \
+               if ((svf_buffer_index > 0) &&
+                               (((command != STATE) && (command != RUNTEST)) ||
                                                ((command == STATE) && (num_of_argu == 2)))) {
-                       if (ERROR_OK != svf_execute_tap())
+                       if (svf_execute_tap() != ERROR_OK)
                                return ERROR_FAIL;
 
                        /* output debug info */
-                       if ((SIR == command) || (SDR == command)) {
-                               int read_value;
-                               memcpy(&read_value, svf_tdi_buffer, sizeof(int));
-                               /* in debug mode, data is from index 0 */
-                               int read_mask = svf_get_mask_u32(svf_check_tdo_para[0].bit_len);
-                               LOG_DEBUG("\tTDO read = 0x%X", read_value & read_mask);
-                       }
+                       if ((command == SIR) || (command == SDR))
+                               SVF_BUF_LOG(DEBUG, svf_tdi_buffer, svf_check_tdo_para[0].bit_len, "TDO read");
                }
        } else {
                /* for fast executing, execute tap if necessary */
                /* half of the buffer is for the next command */
                if (((svf_buffer_index >= SVF_MAX_BUFFER_SIZE_TO_COMMIT) ||
-                               (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) && \
-                               (((command != STATE) && (command != RUNTEST)) || \
+                               (svf_check_tdo_para_index >= SVF_CHECK_TDO_PARA_SIZE / 2)) &&
+                               (((command != STATE) && (command != RUNTEST)) ||
                                                ((command == STATE) && (num_of_argu == 2))))
                        return svf_execute_tap();
        }
@@ -1598,7 +1545,7 @@ static const struct command_registration svf_command_handlers[] = {
                .handler = handle_svf_command,
                .mode = COMMAND_EXEC,
                .help = "Runs a SVF file.",
-               .usage = "svf [-tap device.tap] <file> [quiet] [nil] [progress]",
+               .usage = "[-tap device.tap] <file> [quiet] [nil] [progress] [ignore_error]",
        },
        COMMAND_REGISTRATION_DONE
 };