Skip to content

Commit

Permalink
fix for stlink old DFU serial number
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek BOCHKATI <tarek.bouchkati@gmail.com>
  • Loading branch information
tarek-bochkati committed Mar 16, 2021
1 parent 9956bde commit 8b370a8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 45 deletions.
3 changes: 0 additions & 3 deletions doc/man/st-info.1
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ Display the chip ID of the device
.B \-\-serial
Display the serial code of the device
.TP
.B \-\-hla\-serial
Display the hex escaped serial code of the device
.TP
.B \-\-probe
Display the summarized information of the connected programmers and
devices
Expand Down
8 changes: 2 additions & 6 deletions doc/man/st-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ st-info - Provides information about connected STLink and STM32 devices
# DESCRIPTION

Provides information about connected STLink programmers and STM32 devices:
Serial code, OpenOCD hla-serial, flash, page size, sram, chipid, description.
Serial code, flash, page size, sram, chipid, description.

The STLink device to probe can be specified via the environment variable
STLINK_DEVICE on the format <USB_BUS>:<USB_ADDR>.
Expand All @@ -29,9 +29,6 @@ STLINK_DEVICE on the format <USB_BUS>:<USB_ADDR>.
\--serial
: Display the serial code of the device

\--hla-serial
: Display the hex escaped serial code of the device

\--flash
: Display amount of flash memory available in the device

Expand All @@ -53,8 +50,7 @@ Display information about connected programmers and devices

$ st-info --probe
Found 1 stlink programmers
serial: 303033413030323233343338353130323334333133393339
hla-serial: "\x30\x30\x33\x41\x30\x30\x32\x32\x33\x34\x33\x38\x35\x31\x30\x32\x33\x34\x33\x31\x33\x39\x33\x39"
serial: 57FF72067265575742132067
flash: 131072 (pagesize: 128)
sram: 20480
chipid: 0x0447
Expand Down
5 changes: 2 additions & 3 deletions inc/stlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ enum target_state {
#define STLINK_SWDCLK_15KHZ_DIVISOR 265
#define STLINK_SWDCLK_5KHZ_DIVISOR 798

#define STLINK_SERIAL_MAX_SIZE 64
#define STLINK_SERIAL_MAX_SIZE 24

#define STLINK_V3_MAX_FREQ_NB 10

Expand Down Expand Up @@ -191,8 +191,7 @@ struct _stlink {
uint32_t chip_id; // set by stlink_load_device_params(), used to identify flash and sram
enum target_state core_stat; // set by stlink_status()

char serial[STLINK_SERIAL_MAX_SIZE];
int serial_size;
char serial[STLINK_SERIAL_MAX_SIZE + 1];
int freq; // set by stlink_open_usb(), values: STLINK_SWDCLK_xxx_DIVISOR

enum stlink_flash_type flash_type;
Expand Down
28 changes: 2 additions & 26 deletions src/st-info/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,13 @@ static void usage(void) {
puts("st-info --version");
puts("st-info --probe");
puts("st-info --serial");
puts("st-info --hla-serial");
puts("st-info --flash [--connect-under-reset]");
puts("st-info --pagesize [--connect-under-reset]");
puts("st-info --sram [--connect-under-reset]");
puts("st-info --chipid [--connect-under-reset]");
puts("st-info --descr [--connect-under-reset]");
}

/* Print normal or OpenOCD hla_serial with newline */
static void stlink_print_serial(stlink_t *sl, bool openocd) {
const char *fmt;

if (openocd) {
printf("\"");
fmt = "\\x%02x";
} else {
fmt = "%02x";
}

for (int n = 0; n < sl->serial_size; n++) { printf(fmt, sl->serial[n]); }

if (openocd) { printf("\""); }

printf("\n");
}

static void stlink_print_version(stlink_t *sl) {
// Implementation of version printing is minimalistic
// but contains all available information from sl->version
Expand All @@ -53,10 +34,7 @@ static void stlink_print_info(stlink_t *sl) {

printf(" version: ");
stlink_print_version(sl);
printf(" serial: ");
stlink_print_serial(sl, false);
printf(" hla-serial: ");
stlink_print_serial(sl, true);
printf(" serial: %s\n", sl->serial);
printf(" flash: %u (pagesize: %u)\n",
(uint32_t)sl->flash_size, (uint32_t)sl->flash_pgsz);
printf(" sram: %u\n", (uint32_t)sl->sram_size);
Expand Down Expand Up @@ -131,9 +109,7 @@ static int print_data(int ac, char **av) {
if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) { stlink_enter_swd_mode(sl); }

if (strcmp(av[1], "--serial") == 0) {
stlink_print_serial(sl, false);
} else if (strcmp(av[1], "--hla-serial") == 0) {
stlink_print_serial(sl, true);
printf("%s\n", sl->serial);
} else if (strcmp(av[1], "--flash") == 0) {
printf("0x%x\n", (uint32_t)sl->flash_size);
} else if (strcmp(av[1], "--pagesize") == 0) {
Expand Down
45 changes: 38 additions & 7 deletions src/stlink-lib/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,40 @@ static stlink_backend_t _stlink_usb_backend = {
_stlink_usb_read_trace
};

int stlink_serial(struct libusb_device_handle *handle, struct libusb_device_descriptor *desc, char *serial) {
unsigned char desc_serial[(STLINK_SERIAL_MAX_SIZE + 1) * 2];

/* get the LANGID from String Descriptor Zero */
int ret = libusb_get_string_descriptor(handle, 0, 0, desc_serial, sizeof(desc_serial));

if (ret < 4) return ret;

uint32_t langid = desc_serial[2] | (desc_serial[3] << 8);

/* get the serial */
ret = libusb_get_string_descriptor(handle, desc->iSerialNumber, langid, desc_serial,
sizeof(desc_serial));

if (ret < 0) return ret; // could not read serial

unsigned char len = desc_serial[0];

if (len == ((STLINK_SERIAL_MAX_SIZE + 1) * 2)) { /* len == 50 */
/* good ST-Link adapter */
ret = libusb_get_string_descriptor_ascii(
handle, desc->iSerialNumber, (unsigned char *)serial, STLINK_SERIAL_MAX_SIZE + 1);
} else if (len == ((STLINK_SERIAL_MAX_SIZE / 2 + 1) * 2)) { /* len == 26 */
/* fix-up the buggy serial */
for (unsigned int i = 0; i < STLINK_SERIAL_MAX_SIZE; i += 2)
sprintf(serial + i, "%02X", desc_serial[i + 2]);
serial[STLINK_SERIAL_MAX_SIZE] = '\0';
} else {
return -1;
}

return 0;
}

stlink_t *stlink_open_usb(enum ugly_loglevel verbose, int reset, char serial[STLINK_SERIAL_MAX_SIZE], int freq) {
stlink_t* sl = NULL;
struct stlink_libusb* slu = NULL;
Expand Down Expand Up @@ -1236,15 +1270,12 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, int reset, char serial[STL

if (ret) { continue; } // could not open device

sl->serial_size = libusb_get_string_descriptor_ascii(
handle, desc.iSerialNumber, (unsigned char *)sl->serial, sizeof(sl->serial));
ret = stlink_serial(handle, &desc, sl->serial);

libusb_close(handle);

if (sl->serial_size < 0) { continue; } // could not read serial

// if no serial provided, or if serial match device, fixup version and protocol
if (((serial == NULL) || (*serial == 0)) || (memcmp(serial, &sl->serial, sl->serial_size) == 0)) {
if (((serial == NULL) || (*serial == 0)) || (memcmp(serial, &sl->serial, STLINK_SERIAL_MAX_SIZE) == 0)) {
if (STLINK_V1_USB_PID(desc.idProduct)) {
slu->protocoll = 1;
sl->version.stlink_v = 1;
Expand Down Expand Up @@ -1433,7 +1464,7 @@ static size_t stlink_probe_usb_devs(libusb_device **devs, stlink_t **sldevs[]) {
if (!STLINK_SUPPORTED_USB_PID(desc.idProduct)) { continue; }

struct libusb_device_handle* handle;
char serial[STLINK_SERIAL_MAX_SIZE] = {0, };
char serial[STLINK_SERIAL_MAX_SIZE + 1] = {0, };

ret = libusb_open(dev, &handle);

Expand All @@ -1447,7 +1478,7 @@ static size_t stlink_probe_usb_devs(libusb_device **devs, stlink_t **sldevs[]) {
break;
}

ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, (unsigned char *)&serial, sizeof(serial));
ret = stlink_serial(handle, &desc, serial);

libusb_close(handle);

Expand Down

0 comments on commit 8b370a8

Please sign in to comment.