drivers/stlink_usb: check error code returned by st-link

Most of ST-Link commands return an error code, but this is almost
never checked.

Check for the error code for every command that returns it.

Change-Id: Ifc4e32f8c73196de23700ba0989bfdfe0f5b46e3
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: http://openocd.zylin.com/4825
Tested-by: jenkins
Reviewed-by: Tomas Vanek <vanekt@fbl.cz>
This commit is contained in:
Antonio Borneo 2018-12-07 12:37:20 +01:00 committed by Tomas Vanek
parent d46b28983d
commit 05c6c871e3
1 changed files with 46 additions and 36 deletions

View File

@ -644,8 +644,9 @@ static int stlink_usb_xfer_v1_get_sense(void *handle)
transfers block in cmdbuf transfers block in cmdbuf
<size> indicates number of bytes in the following <size> indicates number of bytes in the following
data phase. data phase.
Ignore the (eventual) error code in the received packet.
*/ */
static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size) static int stlink_usb_xfer_noerrcheck(void *handle, const uint8_t *buf, int size)
{ {
int err, cmdsize = STLINK_CMD_SIZE_V2; int err, cmdsize = STLINK_CMD_SIZE_V2;
struct stlink_usb_handle_s *h = handle; struct stlink_usb_handle_s *h = handle;
@ -766,6 +767,22 @@ static int stlink_usb_error_check(void *handle)
} }
} }
/*
* Wrapper around stlink_usb_xfer_noerrcheck()
* to check the error code in the received packet
*/
static int stlink_usb_xfer_errcheck(void *handle, const uint8_t *buf, int size)
{
int retval;
assert(size > 0);
retval = stlink_usb_xfer_noerrcheck(handle, buf, size);
if (retval != ERROR_OK)
return retval;
return stlink_usb_error_check(handle);
}
/** Issue an STLINK command via USB transfer, with retries on any wait status responses. /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
@ -782,7 +799,7 @@ static int stlink_cmd_allow_retry(void *handle, const uint8_t *buf, int size)
while (1) { while (1) {
if ((h->transport != HL_TRANSPORT_SWIM) || !retries) { if ((h->transport != HL_TRANSPORT_SWIM) || !retries) {
res = stlink_usb_xfer(handle, buf, size); res = stlink_usb_xfer_noerrcheck(handle, buf, size);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
} }
@ -886,7 +903,7 @@ static int stlink_usb_version(void *handle)
h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION; h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
res = stlink_usb_xfer(handle, h->databuf, 6); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 6);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -927,7 +944,7 @@ static int stlink_usb_version(void *handle)
h->cmdbuf[h->cmdidx++] = STLINK_APIV3_GET_VERSION_EX; h->cmdbuf[h->cmdidx++] = STLINK_APIV3_GET_VERSION_EX;
res = stlink_usb_xfer(handle, h->databuf, 12); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 12);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -1035,7 +1052,7 @@ static int stlink_usb_check_voltage(void *handle, float *target_voltage)
h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE; h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE;
int result = stlink_usb_xfer(handle, h->databuf, 8); int result = stlink_usb_xfer_noerrcheck(handle, h->databuf, 8);
if (result != ERROR_OK) if (result != ERROR_OK)
return result; return result;
@ -1114,7 +1131,7 @@ static int stlink_usb_current_mode(void *handle, uint8_t *mode)
h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE; h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
res = stlink_usb_xfer(handle, h->databuf, 2); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 2);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -1202,7 +1219,7 @@ static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
return ERROR_FAIL; return ERROR_FAIL;
} }
res = stlink_usb_xfer(handle, 0, 0); res = stlink_usb_xfer_noerrcheck(handle, 0, 0);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -1348,7 +1365,8 @@ static int stlink_swim_status(void *handle)
stlink_usb_init_buffer(handle, h->rx_ep, 4); stlink_usb_init_buffer(handle, h->rx_ep, 4);
h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND; h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READSTATUS; h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READSTATUS;
res = stlink_usb_xfer(handle, h->databuf, 4); /* error is checked by the caller */
res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 4);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
return ERROR_OK; return ERROR_OK;
@ -1368,7 +1386,7 @@ static int stlink_swim_cap(void *handle, uint8_t *cap)
h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND; h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READ_CAP; h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READ_CAP;
h->cmdbuf[h->cmdidx++] = 0x01; h->cmdbuf[h->cmdidx++] = 0x01;
res = stlink_usb_xfer(handle, h->databuf, 8); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 8);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
memcpy(cap, h->databuf, 8); memcpy(cap, h->databuf, 8);
@ -1525,7 +1543,7 @@ static int stlink_swim_readbytes(void *handle, uint32_t addr, uint32_t len, uint
stlink_usb_init_buffer(handle, h->rx_ep, len); stlink_usb_init_buffer(handle, h->rx_ep, len);
h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND; h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READBUF; h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READBUF;
res = stlink_usb_xfer(handle, data, len); res = stlink_usb_xfer_noerrcheck(handle, data, len);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -1551,7 +1569,7 @@ static int stlink_usb_idcode(void *handle, uint32_t *idcode)
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
res = stlink_usb_xfer(handle, h->databuf, 4); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 4);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -1621,7 +1639,7 @@ static int stlink_usb_trace_read(void *handle, uint8_t *buf, size_t *size)
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GET_TRACE_NB; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GET_TRACE_NB;
res = stlink_usb_xfer(handle, h->databuf, 2); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 2);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -1698,7 +1716,7 @@ static enum target_state stlink_usb_state(void *handle)
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
res = stlink_usb_xfer(handle, h->databuf, 2); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 2);
if (res != ERROR_OK) if (res != ERROR_OK)
return TARGET_UNKNOWN; return TARGET_UNKNOWN;
@ -1749,7 +1767,7 @@ static void stlink_usb_trace_disable(void *handle)
stlink_usb_init_buffer(handle, h->rx_ep, 2); stlink_usb_init_buffer(handle, h->rx_ep, 2);
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX;
res = stlink_usb_xfer(handle, h->databuf, 2); res = stlink_usb_xfer_errcheck(handle, h->databuf, 2);
if (res == ERROR_OK) if (res == ERROR_OK)
h->trace.enabled = false; h->trace.enabled = false;
@ -1774,7 +1792,7 @@ static int stlink_usb_trace_enable(void *handle)
h_u32_to_le(h->cmdbuf+h->cmdidx, h->trace.source_hz); h_u32_to_le(h->cmdbuf+h->cmdidx, h->trace.source_hz);
h->cmdidx += 4; h->cmdidx += 4;
res = stlink_usb_xfer(handle, h->databuf, 2); res = stlink_usb_xfer_errcheck(handle, h->databuf, 2);
if (res == ERROR_OK) { if (res == ERROR_OK) {
h->trace.enabled = true; h->trace.enabled = true;
@ -1901,11 +1919,11 @@ static int stlink_usb_read_regs(void *handle)
if (h->version.jtag_api == STLINK_JTAG_API_V1) { if (h->version.jtag_api == STLINK_JTAG_API_V1) {
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
res = stlink_usb_xfer(handle, h->databuf, 84); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 84);
/* regs data from offset 0 */ /* regs data from offset 0 */
} else { } else {
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
res = stlink_usb_xfer(handle, h->databuf, 88); res = stlink_usb_xfer_errcheck(handle, h->databuf, 88);
/* status at offset 0, regs data from offset 4 */ /* status at offset 0, regs data from offset 4 */
} }
@ -1930,7 +1948,7 @@ static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
h->cmdbuf[h->cmdidx++] = num; h->cmdbuf[h->cmdidx++] = num;
if (h->version.jtag_api == STLINK_JTAG_API_V1) { if (h->version.jtag_api == STLINK_JTAG_API_V1) {
res = stlink_usb_xfer(handle, h->databuf, 4); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 4);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
*val = le_to_h_u32(h->databuf); *val = le_to_h_u32(h->databuf);
@ -1967,7 +1985,6 @@ static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
static int stlink_usb_get_rw_status(void *handle) static int stlink_usb_get_rw_status(void *handle)
{ {
int res;
struct stlink_usb_handle_s *h = handle; struct stlink_usb_handle_s *h = handle;
assert(handle != NULL); assert(handle != NULL);
@ -1980,18 +1997,11 @@ static int stlink_usb_get_rw_status(void *handle)
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
if (h->version.flags & STLINK_F_HAS_GETLASTRWSTATUS2) { if (h->version.flags & STLINK_F_HAS_GETLASTRWSTATUS2) {
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2;
return stlink_usb_xfer_errcheck(handle, h->databuf, 12);
res = stlink_usb_xfer(handle, h->databuf, 12);
} else { } else {
h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS; h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
res = stlink_usb_xfer(handle, h->databuf, 2);
} }
if (res != ERROR_OK)
return res;
return stlink_usb_error_check(h);
} }
/** */ /** */
@ -2023,7 +2033,7 @@ static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
if (read_len == 1) if (read_len == 1)
read_len++; read_len++;
res = stlink_usb_xfer(handle, h->databuf, read_len); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, read_len);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -2057,7 +2067,7 @@ static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
h_u16_to_le(h->cmdbuf+h->cmdidx, len); h_u16_to_le(h->cmdbuf+h->cmdidx, len);
h->cmdidx += 2; h->cmdidx += 2;
res = stlink_usb_xfer(handle, buffer, len); res = stlink_usb_xfer_noerrcheck(handle, buffer, len);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -2092,7 +2102,7 @@ static int stlink_usb_read_mem16(void *handle, uint32_t addr, uint16_t len,
h_u16_to_le(h->cmdbuf+h->cmdidx, len); h_u16_to_le(h->cmdbuf+h->cmdidx, len);
h->cmdidx += 2; h->cmdidx += 2;
res = stlink_usb_xfer(handle, h->databuf, len); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, len);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -2129,7 +2139,7 @@ static int stlink_usb_write_mem16(void *handle, uint32_t addr, uint16_t len,
h_u16_to_le(h->cmdbuf+h->cmdidx, len); h_u16_to_le(h->cmdbuf+h->cmdidx, len);
h->cmdidx += 2; h->cmdidx += 2;
res = stlink_usb_xfer(handle, buffer, len); res = stlink_usb_xfer_noerrcheck(handle, buffer, len);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -2161,7 +2171,7 @@ static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
h_u16_to_le(h->cmdbuf+h->cmdidx, len); h_u16_to_le(h->cmdbuf+h->cmdidx, len);
h->cmdidx += 2; h->cmdidx += 2;
res = stlink_usb_xfer(handle, h->databuf, len); res = stlink_usb_xfer_noerrcheck(handle, h->databuf, len);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -2195,7 +2205,7 @@ static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
h_u16_to_le(h->cmdbuf+h->cmdidx, len); h_u16_to_le(h->cmdbuf+h->cmdidx, len);
h->cmdidx += 2; h->cmdidx += 2;
res = stlink_usb_xfer(handle, buffer, len); res = stlink_usb_xfer_noerrcheck(handle, buffer, len);
if (res != ERROR_OK) if (res != ERROR_OK)
return res; return res;
@ -2516,7 +2526,7 @@ static int stlink_get_com_freq(void *handle, bool is_jtag, struct speed_map *map
h->cmdbuf[h->cmdidx++] = STLINK_APIV3_GET_COM_FREQ; h->cmdbuf[h->cmdidx++] = STLINK_APIV3_GET_COM_FREQ;
h->cmdbuf[h->cmdidx++] = is_jtag ? 1 : 0; h->cmdbuf[h->cmdidx++] = is_jtag ? 1 : 0;
int res = stlink_usb_xfer(handle, h->databuf, 52); int res = stlink_usb_xfer_errcheck(handle, h->databuf, 52);
int size = h->databuf[8]; int size = h->databuf[8];
@ -2553,7 +2563,7 @@ static int stlink_set_com_freq(void *handle, bool is_jtag, unsigned int frequenc
h_u32_to_le(&h->cmdbuf[4], frequency); h_u32_to_le(&h->cmdbuf[4], frequency);
return stlink_usb_xfer(handle, h->databuf, 8); return stlink_usb_xfer_errcheck(handle, h->databuf, 8);
} }
static int stlink_speed_v3(void *handle, bool is_jtag, int khz, bool query) static int stlink_speed_v3(void *handle, bool is_jtag, int khz, bool query)