openocd: fix command's usage string
[fw/openocd] / src / svf / svf.c
index ac7c2c68d01256cdedee8a412018bcd2bfc20795..608703434aa8618184aa33065dea0ed120539049 100644 (file)
  *   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.           *
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
  ***************************************************************************/
 
 /* 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
@@ -216,22 +214,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);
 
@@ -246,7 +246,7 @@ static int svf_last_printed_percentage = -1;
  * DEBUG, INFO, ERROR, USER
  */
 #define SVF_BUF_LOG(_lvl, _buf, _nbits, _desc)                                                 \
-       svf_hexbuf_print(LOG_LVL_##_lvl ,  __FILE__, __LINE__, __func__, _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,
@@ -257,7 +257,7 @@ static void svf_hexbuf_print(int dbg_lvl, const char *file, unsigned line,
        int msbits = bit_len % 8;
 
        /* allocate 2 bytes per hex digit */
-       char *prbuf = malloc((byte_len * 2) + 1);
+       char *prbuf = malloc((byte_len * 2) + 2 + 1);
        if (!prbuf)
                return;
 
@@ -276,6 +276,9 @@ static int svf_realloc_buffers(size_t len)
 {
        void *ptr;
 
+       if (svf_execute_tap() != ERROR_OK)
+               return ERROR_FAIL;
+
        ptr = realloc(svf_tdi_buffer, len);
        if (!ptr)
                return ERROR_FAIL;
@@ -299,22 +302,17 @@ static int svf_realloc_buffers(size_t len)
 static void svf_free_xxd_para(struct svf_xxr_para *para)
 {
        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;
-               }
+               free(para->tdi);
+               para->tdi = NULL;
+
+               free(para->tdo);
+               para->tdo = NULL;
+
+               free(para->mask);
+               para->mask = NULL;
+
+               free(para->smask);
+               para->smask = NULL;
        }
 }
 
@@ -358,7 +356,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
@@ -373,11 +371,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++;
@@ -389,11 +389,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) {
                                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
@@ -408,7 +411,7 @@ COMMAND_HANDLER(handle_svf_command)
        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;
@@ -526,8 +529,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);
@@ -538,28 +541,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;
 
@@ -571,12 +569,15 @@ free_all:
        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);
+               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;
 }
 
@@ -650,11 +651,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
@@ -728,6 +731,9 @@ parse_char:
                pos++;
        }
 
+       if (num == 0)
+               return ERROR_FAIL;
+
        *num_of_argu = num;
 
        return ERROR_OK;
@@ -755,16 +761,12 @@ 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 = malloc(new_byte_len);
+               free(*arr);
+               *arr = calloc(1, new_byte_len);
                if (NULL == *arr) {
                        LOG_ERROR("not enough memory");
                        return ERROR_FAIL;
                }
-               memset(*arr, 0, new_byte_len);
        }
        return ERROR_OK;
 }
@@ -838,7 +840,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;
        }
 
@@ -860,7 +862,11 @@ static int svf_check_tdo(void)
                        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");
-                       return ERROR_FAIL;
+
+                       if (svf_ignore_error == 0)
+                               return ERROR_FAIL;
+                       else
+                               svf_ignore_error++;
                }
        }
        svf_check_tdo_para_index = 0;
@@ -970,7 +976,7 @@ static int svf_run_command(struct command_context *cmd_ctx, char *cmd_str)
                                /* 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);
                                }
@@ -1018,6 +1024,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) {
@@ -1045,7 +1064,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 !=
@@ -1171,7 +1190,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,
@@ -1261,7 +1280,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,
@@ -1277,13 +1296,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;
                        }
@@ -1492,14 +1510,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) {
@@ -1509,8 +1526,8 @@ 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())
                                return ERROR_FAIL;
@@ -1524,8 +1541,8 @@ XXR_common:
                /* 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();
        }
@@ -1539,7 +1556,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
 };