Merge commit 'c4869a54733ae92099a7316954e34d1ab7b6097c' into sam-update

This commit is contained in:
Jason Kotzin 2022-08-10 22:31:16 -07:00
commit 3ae3095499
5 changed files with 171 additions and 116 deletions

View File

@ -35,10 +35,11 @@ class stm32_boot:
def _sync(self):
# Send sync byte
#print("sending sync byte")
self.serial.write(bytes((0x7F,)))
self.serial.write(b"\x7f")
self._checkack()
def _sendcmd(self, cmd):
cmd = ord(cmd)
cmd = bytes((cmd, cmd ^ 0xff))
#print("sendcmd:", repr(cmd))
self.serial.write(cmd)
@ -51,7 +52,7 @@ class stm32_boot:
self.serial.write(data)
def _checkack(self):
ACK = bytes((0x79,))
ACK = b"\x79"
b = self.serial.read(1)
if b != ACK: raise Exception("Invalid ack: %r" % b)
#print("got ack!")
@ -59,7 +60,7 @@ class stm32_boot:
def get(self):
self._sendcmd(0x00)
self._sendcmd(b"\x00")
self._checkack()
num = self.serial.read(1)[0]
data = self.serial.read(num+1)
@ -68,27 +69,27 @@ class stm32_boot:
def eraseall(self):
# Send erase cmd
self._sendcmd(0x43)
self._sendcmd(b"\x43")
self._checkack()
# Global erase
self._sendcmd(0xff)
self._sendcmd(b"\xff")
self._checkack()
def read(self, addr, len):
# Send read cmd
self._sendcmd(0x11)
self._sendcmd(b"\x11")
self._checkack()
# Send address
self._send(struct.pack(">L", addr))
self._checkack()
# Send length
self._sendcmd(len-1)
self._sendcmd(bytes((len-1,)))
self._checkack()
return self.serial.read(len)
def write(self, addr, data):
# Send write cmd
self._sendcmd(0x31)
self._sendcmd(b"\x31")
self._checkack()
# Send address
self._send(struct.pack(">L", addr))
@ -99,7 +100,7 @@ class stm32_boot:
def write_protect(self, sectors):
# Send WP cmd
self._sendcmd(0x63)
self._sendcmd(b"\x63")
self._checkack()
# Send sector list
self._send(bytes((len(sectors)-1,)) + bytes(sectors))
@ -108,19 +109,19 @@ class stm32_boot:
self._sync()
def write_unprotect(self):
self._sendcmd(0x73)
self._sendcmd(b"\x73")
self._checkack()
self._checkack()
self._sync()
def read_protect(self):
self._sendcmd(0x82)
self._sendcmd(b"\x82")
self._checkack()
self._checkack()
self._sync()
def read_unprotect(self):
self._sendcmd(0x92)
self._sendcmd(b"\x92")
self._checkack()
self._checkack()
self._sync()

View File

@ -114,7 +114,7 @@ static bmp_type_t find_cmsis_dap_interface(libusb_device *dev,bmp_info_t *info)
return type;
}
int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info)
int find_debuggers(BMP_CL_OPTIONS_t *cl_opts, bmp_info_t *info)
{
libusb_device **devs;
int res = libusb_init(&info->libusb_ctx);
@ -124,11 +124,11 @@ int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info)
exit(-1);
}
if (cl_opts->opt_cable) {
if ((!strcmp(cl_opts->opt_cable, "list")) ||
(!strcmp(cl_opts->opt_cable, "l"))) {
cable_desc_t *cable = &cable_desc[0];
if (!strcmp(cl_opts->opt_cable, "list") ||
!strcmp(cl_opts->opt_cable, "l")) {
cable_desc_t *cable = cable_desc;
DEBUG_WARN("Available cables:\n");
for (; cable->name; cable++) {
for (; cable->name; ++cable) {
DEBUG_WARN("\t%s\n", cable->name);
}
exit(0);
@ -149,7 +149,7 @@ int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info)
bool access_problems = false;
char *active_cable = NULL;
bool ftdi_unknown = false;
rescan:
rescan:
found_debuggers = 0;
serial[0] = 0;
manufacturer[0] = 0;
@ -157,9 +157,9 @@ int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info)
access_problems = false;
active_cable = NULL;
ftdi_unknown = false;
for (int i = 0; devs[i]; i++) {
for (size_t i = 0; devs[i]; ++i) {
bmp_type_t type = BMP_TYPE_NONE;
libusb_device *dev = devs[i];
libusb_device *dev = devs[i];
int res = libusb_get_device_descriptor(dev, &desc);
if (res < 0) {
DEBUG_WARN( "WARN: libusb_get_device_descriptor() failed: %s",
@ -173,7 +173,7 @@ int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info)
case LIBUSB_CLASS_WIRELESS:
continue;
}
libusb_device_handle *handle;
libusb_device_handle *handle = NULL;
res = libusb_open(dev, &handle);
if (res != LIBUSB_SUCCESS) {
if (!access_problems) {
@ -183,70 +183,104 @@ int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info)
}
continue;
}
res = libusb_get_string_descriptor_ascii(
handle, desc.iSerialNumber, (uint8_t*)serial,
sizeof(serial));
if (cl_opts->opt_serial && ((res <= 0) ||
!strstr(serial, cl_opts->opt_serial))) {
/* If the device even has a serial number string, fetch it */
if (desc.iSerialNumber) {
res = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber,
(uint8_t *)serial, sizeof(serial));
/* If the call fails and it's not because the device gave us STALL, continue to the next one */
if (res < 0 && res != LIBUSB_ERROR_PIPE) {
libusb_close(handle);
continue;
}
/* Device has no serial and that's ok. */
else if (res <= 0)
serial[0] = '\0';
}
else
serial[0] = '\0';
if (cl_opts->opt_serial && !strstr(serial, cl_opts->opt_serial)) {
libusb_close(handle);
continue;
}
if (res < 0)
serial[0] = 0;
manufacturer[0] = 0;
res = libusb_get_string_descriptor_ascii(
handle, desc.iManufacturer, (uint8_t*)manufacturer,
sizeof(manufacturer));
product[0] = 0;
res = libusb_get_string_descriptor_ascii(
handle, desc.iProduct, (uint8_t*)product,
sizeof(product));
/* Attempt to get the manufacturer string */
if (desc.iManufacturer) {
res = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer,
(uint8_t *)manufacturer, sizeof(manufacturer));
/* If the call fails and it's not because the device gave us STALL, continue to the next one */
if (res < 0 && res != LIBUSB_ERROR_PIPE) {
DEBUG_WARN("WARN: libusb_get_string_descriptor_ascii() call to fetch manufacturer string failed: %s\n",
libusb_strerror(res));
libusb_close(handle);
continue;
}
/* Device has no manufacturer string and that's ok. */
else if (res <= 0)
manufacturer[0] = '\0';
}
else
manufacturer[0] = '\0';
/* Attempt to get the product string */
if (desc.iProduct) {
res = libusb_get_string_descriptor_ascii(handle, desc.iProduct,
(uint8_t *)product, sizeof(product));
/* If the call fails and it's not because the device gave us STALL, continue to the next one */
if (res < 0 && res != LIBUSB_ERROR_PIPE) {
DEBUG_WARN("WARN: libusb_get_string_descriptor_ascii() call to fetch product string failed: %s\n",
libusb_strerror(res));
libusb_close(handle);
continue;
}
/* Device has no product string and that's ok. */
else if (res <= 0)
product[0] = '\0';
}
else
product[0] = '\0';
libusb_close(handle);
if (cl_opts->opt_ident_string) {
char *match_manu = NULL;
char *match_product = NULL;
match_manu = strstr(manufacturer, cl_opts->opt_ident_string);
match_manu = strstr(manufacturer, cl_opts->opt_ident_string);
match_product = strstr(product, cl_opts->opt_ident_string);
if (!match_manu && !match_product) {
if (!match_manu && !match_product)
continue;
}
}
/* Either serial and/or ident_string match or are not given.
* Check type.*/
if (desc.idVendor == VENDOR_ID_BMP) {
if (desc.idProduct == PRODUCT_ID_BMP) {
if (desc.idProduct == PRODUCT_ID_BMP)
type = BMP_TYPE_BMP;
} else {
else {
if (desc.idProduct == PRODUCT_ID_BMP_BL)
DEBUG_WARN("BMP in bootloader mode found. Restart or reflash!\n");
continue;
}
} else if ((type == BMP_TYPE_NONE) &&
((type = find_cmsis_dap_interface(dev, info)) != BMP_TYPE_NONE)) {
} else if (type == BMP_TYPE_NONE &&
(type = find_cmsis_dap_interface(dev, info)) != BMP_TYPE_NONE) {
/* find_cmsis_dap_interface has set valid type*/
} else if ((strstr(manufacturer, "CMSIS")) || (strstr(product, "CMSIS"))) {
} else if (strstr(manufacturer, "CMSIS") || strstr(product, "CMSIS"))
type = BMP_TYPE_CMSIS_DAP;
} else if (desc.idVendor == VENDOR_ID_STLINK) {
if ((desc.idProduct == PRODUCT_ID_STLINKV2) ||
(desc.idProduct == PRODUCT_ID_STLINKV21) ||
(desc.idProduct == PRODUCT_ID_STLINKV21_MSD) ||
(desc.idProduct == PRODUCT_ID_STLINKV3_NO_MSD) ||
(desc.idProduct == PRODUCT_ID_STLINKV3_BL) ||
(desc.idProduct == PRODUCT_ID_STLINKV3) ||
(desc.idProduct == PRODUCT_ID_STLINKV3E)) {
else if (desc.idVendor == VENDOR_ID_STLINK) {
if (desc.idProduct == PRODUCT_ID_STLINKV2 ||
desc.idProduct == PRODUCT_ID_STLINKV21 ||
desc.idProduct == PRODUCT_ID_STLINKV21_MSD ||
desc.idProduct == PRODUCT_ID_STLINKV3_NO_MSD ||
desc.idProduct == PRODUCT_ID_STLINKV3_BL ||
desc.idProduct == PRODUCT_ID_STLINKV3 ||
desc.idProduct == PRODUCT_ID_STLINKV3E)
type = BMP_TYPE_STLINKV2;
} else {
else {
if (desc.idProduct == PRODUCT_ID_STLINKV1)
DEBUG_WARN( "INFO: STLINKV1 not supported\n");
continue;
}
} else if (desc.idVendor == VENDOR_ID_SEGGER) {
} else if (desc.idVendor == VENDOR_ID_SEGGER)
type = BMP_TYPE_JLINK;
} else {
cable_desc_t *cable = &cable_desc[0];
for (; cable->name; cable++) {
else {
cable_desc_t *cable = cable_desc;
for (; cable->name; ++cable) {
bool found = false;
if ((cable->vendor != desc.idVendor) || (cable->product != desc.idProduct))
if (cable->vendor != desc.idVendor || cable->product != desc.idProduct)
continue; /* VID/PID do not match*/
if (cl_opts->opt_cable) {
if (strncmp(cable->name, cl_opts->opt_cable, strlen(cable->name)))
@ -260,10 +294,10 @@ int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info)
else
found = true;
} else { /* VID/PID fits, but no cl_opts->opt_cable and no description*/
if ((cable->vendor == 0x0403) && /* FTDI*/
((cable->product == 0x6010) || /* FT2232C/D/H*/
(cable->product == 0x6011) || /* FT4232H Quad HS USB-UART/FIFO IC */
(cable->product == 0x6014))) { /* FT232H Single HS USB-UART/FIFO IC */
if (cable->vendor == 0x0403 && /* FTDI*/
(cable->product == 0x6010 || /* FT2232C/D/H*/
cable->product == 0x6011 || /* FT4232H Quad HS USB-UART/FIFO IC */
cable->product == 0x6014)) { /* FT232H Single HS USB-UART/FIFO IC */
ftdi_unknown = true;
continue; /* Cable name is needed */
}
@ -279,8 +313,8 @@ int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info)
}
if (report) {
DEBUG_WARN("%2d: %s, %s, %s\n", found_debuggers + 1,
(serial[0]) ? serial : NO_SERIAL_NUMBER,
manufacturer,product);
serial[0] ? serial : NO_SERIAL_NUMBER,
manufacturer,product);
}
info->vid = desc.idVendor;
info->pid = desc.idProduct;
@ -289,21 +323,20 @@ int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info)
strncpy(info->product, product, sizeof(info->product));
strncpy(info->manufacturer, manufacturer, sizeof(info->manufacturer));
if (cl_opts->opt_position &&
(cl_opts->opt_position == (found_debuggers + 1))) {
cl_opts->opt_position == found_debuggers + 1) {
found_debuggers = 1;
break;
} else {
found_debuggers++;
}
} else
++found_debuggers;
}
if ((found_debuggers == 0) && ftdi_unknown)
if (found_debuggers == 0 && ftdi_unknown)
DEBUG_WARN("Generic FTDI MPSSE VID/PID found. Please specify exact type with \"-c <cable>\" !\n");
if ((found_debuggers == 1) && !cl_opts->opt_cable && (info->bmp_type == BMP_TYPE_LIBFTDI))
if (found_debuggers == 1 && !cl_opts->opt_cable && info->bmp_type == BMP_TYPE_LIBFTDI)
cl_opts->opt_cable = active_cable;
if (!found_debuggers && cl_opts->opt_list_only)
DEBUG_WARN("No usable debugger found\n");
if ((found_debuggers > 1) ||
((found_debuggers == 1) && (cl_opts->opt_list_only))) {
if (found_debuggers > 1 ||
(found_debuggers == 1 && cl_opts->opt_list_only)) {
if (!report) {
if (found_debuggers > 1)
DEBUG_WARN("%d debuggers found!\nSelect with -P <pos> "
@ -321,8 +354,9 @@ int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info)
DEBUG_WARN(
"No debugger found. Please check access rights to USB devices!\n");
libusb_free_device_list(devs, 1);
return (found_debuggers == 1) ? 0 : -1;
return found_debuggers == 1 ? 0 : -1;
}
static void LIBUSB_CALL on_trans_done(struct libusb_transfer *trans)
{
struct trans_ctx * const ctx = trans->user_data;
@ -330,15 +364,14 @@ static void LIBUSB_CALL on_trans_done(struct libusb_transfer *trans)
if (trans->status != LIBUSB_TRANSFER_COMPLETED)
{
DEBUG_WARN("on_trans_done: ");
if(trans->status == LIBUSB_TRANSFER_TIMED_OUT) {
if (trans->status == LIBUSB_TRANSFER_TIMED_OUT)
DEBUG_WARN(" Timeout\n");
} else if (trans->status == LIBUSB_TRANSFER_CANCELLED) {
else if (trans->status == LIBUSB_TRANSFER_CANCELLED)
DEBUG_WARN(" cancelled\n");
} else if (trans->status == LIBUSB_TRANSFER_NO_DEVICE) {
else if (trans->status == LIBUSB_TRANSFER_NO_DEVICE)
DEBUG_WARN(" no device\n");
} else {
else
DEBUG_WARN(" unknown\n");
}
ctx->flags |= TRANS_FLAGS_HAS_ERROR;
}
ctx->flags |= TRANS_FLAGS_IS_DONE;
@ -390,23 +423,22 @@ int send_recv(usb_link_t *link,
uint8_t *rxbuf, size_t rxsize)
{
int res = 0;
if( txsize) {
int txlen = txsize;
if (txsize) {
libusb_fill_bulk_transfer(link->req_trans,
link->ul_libusb_device_handle,
link->ep_tx | LIBUSB_ENDPOINT_OUT,
txbuf, txlen,
txbuf, txsize,
NULL, NULL, 0);
int i = 0;
DEBUG_WIRE(" Send (%3d): ", txlen);
for (; i < txlen; i++) {
size_t i = 0;
DEBUG_WIRE(" Send (%3zu): ", txsize);
for (; i < txsize; ++i) {
DEBUG_WIRE("%02x", txbuf[i]);
if ((i & 7) == 7)
if ((i & 7U) == 7U)
DEBUG_WIRE(".");
if ((i & 31) == 31)
if ((i & 31U) == 31U)
DEBUG_WIRE("\n ");
}
if (!(i & 31))
if (!(i & 31U))
DEBUG_WIRE("\n");
if (submit_wait(link, link->req_trans)) {
libusb_clear_halt(link->ul_libusb_device_handle, link->ep_tx);
@ -426,14 +458,13 @@ int send_recv(usb_link_t *link,
return -1;
}
res = link->rep_trans->actual_length;
if (res >0) {
int i;
uint8_t *p = rxbuf;
DEBUG_WIRE(" Rec (%zu/%d)", rxsize, res);
for (i = 0; i < res && i < 32 ; i++) {
if ( i && ((i & 7) == 0))
if (res > 0) {
const size_t rxlen = (size_t)res;
DEBUG_WIRE(" Rec (%zu/%zu)", rxsize, rxlen);
for (size_t i = 0; i < rxlen && i < 32 ; ++i) {
if (i && ((i & 7U) == 0U))
DEBUG_WIRE(".");
DEBUG_WIRE("%02x", p[i]);
DEBUG_WIRE("%02x", rxbuf[i]);
}
}
}

View File

@ -76,11 +76,10 @@ void platform_init(int argc, char **argv)
atexit(exit_function);
signal(SIGTERM, sigterm_handler);
signal(SIGINT, sigterm_handler);
if (cl_opts.opt_device) {
if (cl_opts.opt_device)
info.bmp_type = BMP_TYPE_BMP;
} else if (find_debuggers(&cl_opts, &info)) {
else if (find_debuggers(&cl_opts, &info))
exit(-1);
}
bmp_ident(&info);
switch (info.bmp_type) {
case BMP_TYPE_BMP:
@ -89,11 +88,11 @@ void platform_init(int argc, char **argv)
remote_init();
break;
case BMP_TYPE_STLINKV2:
if (stlink_init( &info))
if (stlink_init(&info))
exit(-1);
break;
case BMP_TYPE_CMSIS_DAP:
if (dap_init( &info))
if (dap_init(&info))
exit(-1);
break;
case BMP_TYPE_LIBFTDI:
@ -107,14 +106,12 @@ void platform_init(int argc, char **argv)
default:
exit(-1);
}
int ret = -1;
if (cl_opts.opt_mode != BMP_MODE_DEBUG) {
ret = cl_execute(&cl_opts);
} else {
if (cl_opts.opt_mode != BMP_MODE_DEBUG)
exit(cl_execute(&cl_opts));
else {
gdb_if_init();
return;
}
exit(ret);
}
int platform_adiv5_swdp_scan(uint32_t targetid)

View File

@ -510,23 +510,39 @@ int stlink_init(bmp_info_t *info)
bool found = false;
while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
int result = libusb_get_device_descriptor(dev, &desc);
if (result != LIBUSB_SUCCESS) {
DEBUG_WARN("libusb_get_device_descriptor failed %s",
libusb_strerror(r));
libusb_strerror(result));
return -1;
}
if ((desc.idVendor != info->vid) ||
(desc.idProduct != info->pid) ||
(libusb_open(dev, &sl->ul_libusb_device_handle)
!= LIBUSB_SUCCESS)) {
if (desc.idVendor != info->vid ||
desc.idProduct != info->pid) {
continue;
}
if ((result = libusb_open(dev, &sl->ul_libusb_device_handle)) != LIBUSB_SUCCESS)
{
DEBUG_WARN("Failed to open STLink device %04x:%04x - %s\n",
desc.idVendor, desc.idProduct, libusb_strerror(result));
DEBUG_WARN("Are you sure the permissions on the device are set correctly?\n");
continue;
}
char serial[64];
r = libusb_get_string_descriptor_ascii(
sl->ul_libusb_device_handle, desc.iSerialNumber,
(uint8_t*)serial,sizeof(serial));
if (r <= 0 || !strstr(serial, info->serial)) {
if (desc.iSerialNumber) {
int result = libusb_get_string_descriptor_ascii(sl->ul_libusb_device_handle,
desc.iSerialNumber, (uint8_t *)serial, sizeof(serial));
/* If the call fails and it's not because the device gave us STALL, continue to the next one */
if (result < 0 && result != LIBUSB_ERROR_PIPE) {
libusb_close(sl->ul_libusb_device_handle);
continue;
}
else if (result <= 0)
serial[0] = '\0';
}
else
serial[0] = '\0';
/* Likewise, if the serial number returned doesn't match the one in info, go to next */
if (!strstr(serial, info->serial)) {
libusb_close(sl->ul_libusb_device_handle);
continue;
}
@ -535,7 +551,7 @@ int stlink_init(bmp_info_t *info)
}
libusb_free_device_list(devs, 1);
if (!found)
return 0;
return 1;
if (info->vid != VENDOR_ID_STLINK)
return 0;
switch (info->pid) {

View File

@ -295,6 +295,16 @@ static void stm32g0_detach(target *t)
{
struct stm32g0_priv_s *ps = (struct stm32g0_priv_s*)t->target_storage;
/*
* First re-enable DBGEN clock, in case it got disabled in the meantime
* (happens during flash), so that writes to DBG_* registers below succeed.
*/
target_mem_write32(t, RCC_APBENR1, ps->saved_regs.rcc_apbenr1 |
RCC_APBENR1_DBGEN);
/*
* Then restore the DBG_* registers and clock settings.
*/
target_mem_write32(t, DBG_APB_FZ1, ps->saved_regs.dbg_apb_fz1);
target_mem_write32(t, DBG_CR, ps->saved_regs.dbg_cr);
target_mem_write32(t, RCC_APBENR1, ps->saved_regs.rcc_apbenr1);