From 6b465d6a7751715b905e7d249c25fd26fbf88a92 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Tue, 29 Mar 2022 14:53:31 -0700 Subject: [PATCH 1/9] Temporarily enable DBG clock in stm32g0_detach(); fixes #1003 --- src/target/stm32g0.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/target/stm32g0.c b/src/target/stm32g0.c index 43c58c30..83ab821c 100644 --- a/src/target/stm32g0.c +++ b/src/target/stm32g0.c @@ -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); From 6c700f7b6cd2d289723951a6e6bf31f61ece578d Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Fri, 15 Apr 2022 20:39:14 +0200 Subject: [PATCH 2/9] scripts: bootprog.py: use bytes literals --- scripts/bootprog.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/scripts/bootprog.py b/scripts/bootprog.py index 968fcd68..18a3ad64 100755 --- a/scripts/bootprog.py +++ b/scripts/bootprog.py @@ -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() From c5dbf851f6621af6d0af1bdacad376bf8d274196 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Fri, 1 Apr 2022 00:43:29 -0400 Subject: [PATCH 3/9] hosted/bmp_libusb: Formatting cleanup --- src/platforms/hosted/bmp_libusb.c | 125 +++++++++++++++--------------- 1 file changed, 61 insertions(+), 64 deletions(-) diff --git a/src/platforms/hosted/bmp_libusb.c b/src/platforms/hosted/bmp_libusb.c index 79d4a85b..05104990 100644 --- a/src/platforms/hosted/bmp_libusb.c +++ b/src/platforms/hosted/bmp_libusb.c @@ -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) { @@ -186,7 +186,7 @@ int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info) res = libusb_get_string_descriptor_ascii( handle, desc.iSerialNumber, (uint8_t*)serial, sizeof(serial)); - if (cl_opts->opt_serial && ((res <= 0) || + if (cl_opts->opt_serial && (res <= 0 || !strstr(serial, cl_opts->opt_serial))) { libusb_close(handle); continue; @@ -195,58 +195,57 @@ int find_debuggers(BMP_CL_OPTIONS_t *cl_opts,bmp_info_t *info) serial[0] = 0; manufacturer[0] = 0; res = libusb_get_string_descriptor_ascii( - handle, desc.iManufacturer, (uint8_t*)manufacturer, + handle, desc.iManufacturer, (uint8_t *)manufacturer, sizeof(manufacturer)); product[0] = 0; res = libusb_get_string_descriptor_ascii( - handle, desc.iProduct, (uint8_t*)product, + handle, desc.iProduct, (uint8_t *)product, sizeof(product)); 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 +259,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 +278,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 +288,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 \" !\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 " @@ -321,8 +319,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 +329,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 +388,23 @@ int send_recv(usb_link_t *link, uint8_t *rxbuf, size_t rxsize) { int res = 0; - if( txsize) { + if (txsize) { int txlen = txsize; libusb_fill_bulk_transfer(link->req_trans, link->ul_libusb_device_handle, link->ep_tx | LIBUSB_ENDPOINT_OUT, txbuf, txlen, NULL, NULL, 0); - int i = 0; + size_t i = 0; DEBUG_WIRE(" Send (%3d): ", txlen); - for (; i < txlen; i++) { + for (; i < txlen; ++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,12 +424,11 @@ int send_recv(usb_link_t *link, return -1; } res = link->rep_trans->actual_length; - if (res >0) { - int i; + if (res > 0) { uint8_t *p = rxbuf; DEBUG_WIRE(" Rec (%zu/%d)", rxsize, res); - for (i = 0; i < res && i < 32 ; i++) { - if ( i && ((i & 7) == 0)) + for (size_t i = 0; i < res && i < 32 ; ++i) { + if (i && ((i & 7U) == 0U)) DEBUG_WIRE("."); DEBUG_WIRE("%02x", p[i]); } From c7eba0a439fbcb2e6bb7f694a2dc9eaf82486350 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Fri, 1 Apr 2022 00:43:57 -0400 Subject: [PATCH 4/9] hosted/bmp_libusb: Fixed some signed/unsigned issues and UB in send_recv --- src/platforms/hosted/bmp_libusb.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/platforms/hosted/bmp_libusb.c b/src/platforms/hosted/bmp_libusb.c index 05104990..c48b21af 100644 --- a/src/platforms/hosted/bmp_libusb.c +++ b/src/platforms/hosted/bmp_libusb.c @@ -389,15 +389,14 @@ int send_recv(usb_link_t *link, { int res = 0; if (txsize) { - int txlen = 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); size_t i = 0; - DEBUG_WIRE(" Send (%3d): ", txlen); - for (; i < txlen; ++i) { + DEBUG_WIRE(" Send (%3zu): ", txsize); + for (; i < txsize; ++i) { DEBUG_WIRE("%02x", txbuf[i]); if ((i & 7U) == 7U) DEBUG_WIRE("."); @@ -425,12 +424,12 @@ int send_recv(usb_link_t *link, } res = link->rep_trans->actual_length; if (res > 0) { - uint8_t *p = rxbuf; - DEBUG_WIRE(" Rec (%zu/%d)", rxsize, res); - for (size_t i = 0; i < res && i < 32 ; ++i) { + 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]); } } } From 94e92814044af054f2868db7c69714b03774cb1f Mon Sep 17 00:00:00 2001 From: dragonmux Date: Fri, 1 Apr 2022 12:46:13 -0400 Subject: [PATCH 5/9] hosted/bmp_libusb: Rewrote the string reader logic to not violate the USB spec and properly handle libusb errors --- src/platforms/hosted/bmp_libusb.c | 65 ++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/src/platforms/hosted/bmp_libusb.c b/src/platforms/hosted/bmp_libusb.c index c48b21af..248fcd3e 100644 --- a/src/platforms/hosted/bmp_libusb.c +++ b/src/platforms/hosted/bmp_libusb.c @@ -183,24 +183,59 @@ rescan: } 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; From 4287f1ba0e88d719130623e5d74f2b0293bf4cac Mon Sep 17 00:00:00 2001 From: dragonmux Date: Thu, 14 Apr 2022 13:43:11 -0400 Subject: [PATCH 6/9] hosted/stlinkv2: Rewrote the serial number readering logic to not violate the USB spec and properly handle libusb errors --- src/platforms/hosted/stlinkv2.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/platforms/hosted/stlinkv2.c b/src/platforms/hosted/stlinkv2.c index f87ffc8c..622b7b64 100644 --- a/src/platforms/hosted/stlinkv2.c +++ b/src/platforms/hosted/stlinkv2.c @@ -523,10 +523,21 @@ int stlink_init(bmp_info_t *info) 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) { + const 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; } From ba8ed132a8b94fb4a003cc3b740791133e1c1894 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Thu, 14 Apr 2022 14:19:44 -0400 Subject: [PATCH 7/9] hosted/stlinkv2: Fix !found causing hosted to continue on anyway, and improved the error reporting from the device finder loop --- src/platforms/hosted/stlinkv2.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/platforms/hosted/stlinkv2.c b/src/platforms/hosted/stlinkv2.c index 622b7b64..0198b721 100644 --- a/src/platforms/hosted/stlinkv2.c +++ b/src/platforms/hosted/stlinkv2.c @@ -510,21 +510,26 @@ 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 %#06x:%#06x - %s", + desc.idVendor, desc.idProduct, libusb_strerror(result)); + DEBUG_WARN("Are you sure the permissions on the device are set correctly?"); continue; } char serial[64]; if (desc.iSerialNumber) { - const int result = libusb_get_string_descriptor_ascii(sl->ul_libusb_device_handle, + 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) { @@ -546,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) { From a27661cd0bc73d0725e8d7b6f595817947fd8ca8 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Thu, 14 Apr 2022 14:20:59 -0400 Subject: [PATCH 8/9] hosted/platform: Formatting consistency improvements --- src/platforms/hosted/platform.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/platforms/hosted/platform.c b/src/platforms/hosted/platform.c index a90294d4..84a76c66 100644 --- a/src/platforms/hosted/platform.c +++ b/src/platforms/hosted/platform.c @@ -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) From c4869a54733ae92099a7316954e34d1ab7b6097c Mon Sep 17 00:00:00 2001 From: dragonmux Date: Thu, 14 Apr 2022 14:46:44 -0400 Subject: [PATCH 9/9] hosted/stlinkv2: Cleaned up the new error messages as they weren't outputting nicely --- src/platforms/hosted/stlinkv2.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/platforms/hosted/stlinkv2.c b/src/platforms/hosted/stlinkv2.c index 0198b721..1a814de1 100644 --- a/src/platforms/hosted/stlinkv2.c +++ b/src/platforms/hosted/stlinkv2.c @@ -522,9 +522,9 @@ int stlink_init(bmp_info_t *info) } if ((result = libusb_open(dev, &sl->ul_libusb_device_handle)) != LIBUSB_SUCCESS) { - DEBUG_WARN("Failed to open STLink device %#06x:%#06x - %s", + 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?"); + DEBUG_WARN("Are you sure the permissions on the device are set correctly?\n"); continue; } char serial[64];