flash: jtagspi: fix clang build warning

Clang is unable to fully track the content of the array
write_buffer[] and incorrectly complains that it could contain
some uninitialized value.

To help clang to track the execution flow, rewrite the handling of
the buffer by using simpler indexing and by moving away cmd_byte
from the first buffer's element to the variable cmd_byte.

While there:
- fix the error codes returned while parsing the command line and
- use directly command_print_sameline() instead of passing through
  intermediate buffers.

Change-Id: I1969e896887ea3a4abebee057cc04c03005fa57c
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/7718
Tested-by: jenkins
This commit is contained in:
Antonio Borneo 2023-05-27 17:01:58 +02:00
parent 00603bf156
commit 0e526314a1
1 changed files with 27 additions and 35 deletions

View File

@ -301,24 +301,18 @@ COMMAND_HANDLER(jtagspi_handle_set)
COMMAND_HANDLER(jtagspi_handle_cmd)
{
struct flash_bank *bank;
unsigned int index = 1;
const int max = 21;
uint8_t num_write, num_read, write_buffer[max], read_buffer[1 << CHAR_BIT];
uint8_t data, *ptr;
char temp[4], output[(2 + max + (1 << CHAR_BIT)) * 3 + 8];
int retval;
const unsigned int max = 20;
uint8_t cmd_byte, num_read, write_buffer[max], read_buffer[1 << CHAR_BIT];
LOG_DEBUG("%s", __func__);
if (CMD_ARGC < 3) {
command_print(CMD, "jtagspi: not enough arguments");
if (CMD_ARGC < 3)
return ERROR_COMMAND_SYNTAX_ERROR;
}
num_write = CMD_ARGC - 2;
uint8_t num_write = CMD_ARGC - 3;
if (num_write > max) {
LOG_ERROR("at most %d bytes may be send", max);
return ERROR_COMMAND_SYNTAX_ERROR;
command_print(CMD, "at most %d bytes may be send", max);
return ERROR_COMMAND_ARGUMENT_INVALID;
}
/* calling flash_command_get_bank without probing because we like to be
@ -326,33 +320,31 @@ COMMAND_HANDLER(jtagspi_handle_cmd)
"release from power down" is needed before probing when flash is in
power down mode.
*/
retval = CALL_COMMAND_HANDLER(flash_command_get_bank_probe_optional, 0,
int retval = CALL_COMMAND_HANDLER(flash_command_get_bank_probe_optional, 0,
&bank, false);
if (ERROR_OK != retval)
return retval;
COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index++], num_read);
snprintf(output, sizeof(output), "spi: ");
for (ptr = &write_buffer[0] ; index < CMD_ARGC; index++) {
COMMAND_PARSE_NUMBER(u8, CMD_ARGV[index], data);
*ptr++ = data;
snprintf(temp, sizeof(temp), "%02" PRIx8 " ", data);
strncat(output, temp, sizeof(output) - strlen(output) - 1);
}
strncat(output, "-> ", sizeof(output) - strlen(output) - 1);
/* process command */
ptr = &read_buffer[0];
retval = jtagspi_cmd(bank, write_buffer[0], &write_buffer[1], num_write - 1, ptr, -num_read);
if (retval != ERROR_OK)
return retval;
for ( ; num_read > 0; num_read--) {
snprintf(temp, sizeof(temp), "%02" PRIx8 " ", *ptr++);
strncat(output, temp, sizeof(output) - strlen(output) - 1);
}
command_print(CMD, "%s", output);
COMMAND_PARSE_NUMBER(u8, CMD_ARGV[1], num_read);
COMMAND_PARSE_NUMBER(u8, CMD_ARGV[2], cmd_byte);
for (unsigned int i = 0; i < num_write; i++)
COMMAND_PARSE_NUMBER(u8, CMD_ARGV[i + 3], write_buffer[i]);
/* process command */
retval = jtagspi_cmd(bank, cmd_byte, write_buffer, num_write, read_buffer, -num_read);
if (retval != ERROR_OK)
return retval;
command_print_sameline(CMD, "spi: %02" PRIx8, cmd_byte);
for (unsigned int i = 0; i < num_write; i++)
command_print_sameline(CMD, " %02" PRIx8, write_buffer[i]);
command_print_sameline(CMD, " ->");
for (unsigned int i = 0; i < num_read; i++)
command_print_sameline(CMD, " %02" PRIx8, read_buffer[i]);
return ERROR_OK;
}