Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for libusb_kernel_driver_active & error handling for st.st_size #1210

Merged
merged 8 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions doc/man/st-flash.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ You can use this instead of st-util(1) if you prefer, but remember to use the

Use hexadecimal format for the *ADDR* and *SIZE*.

The STLink device to use can be specified using the --serial parameter, or via
the environment variable STLINK_DEVICE on the format <USB_BUS>:<USB_ADDR>.
The STLink device to use can be specified using the --serial parameter.

# COMMANDS

Expand Down Expand Up @@ -52,7 +51,7 @@ reset
: Enable ignore ending empty bytes optimization

\--serial *iSerial*
: TODO
: Serial number of ST-LINK device to use

\--flash=fsize
: Where fsize is the size in decimal, octal, or hex followed by an optional multiplier
Expand Down
3 changes: 0 additions & 3 deletions doc/man/st-info.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ st-info - Provides information about connected STLink and STM32 devices
Provides information about connected STLink programmers and STM32 devices:
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>.

# OPTIONS

\--version
Expand Down
3 changes: 0 additions & 3 deletions doc/man/st-util.1
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ option, the default \f[B]4242\f[R] port will be used.
.PP
Stlink version 2 is used by default unless the option
\f[B]\[en]stlinkv1\f[R] is given.
.PP
The STLinkV2 device to use can be specified in the environment variable
STLINK_DEVICE on the format :.
.SH OPTIONS
.TP
.B \-h, \-\-help
Expand Down
3 changes: 1 addition & 2 deletions doc/man/st-util.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ Run the main binary of the local package (src/main.rs).
If a port number is not specified using the **--listen_port** option, the
default **4242** port will be used.

The STLink device to use can be specified using the --serial parameter, or via
the environment variable STLINK_DEVICE on the format <USB_BUS>:<USB_ADDR>.
The STLink device to use can be specified using the --serial parameter.

# OPTIONS

Expand Down
3 changes: 1 addition & 2 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,7 @@ There are a few options:
Do not reset board on connection.
```

The STLink device to use can be specified using the --serial parameter, or via
the environment variable STLINK_DEVICE on the format <USB_BUS>:<USB_ADDR>.
The STLink device to use can be specified using the --serial parameter.

Then, in your project directory, someting like this...
(remember, you need to run an _ARM_ gdb, not an x86 gdb)
Expand Down
10 changes: 4 additions & 6 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2198,14 +2198,12 @@ static int map_file(mapped_file_t *mf, const char *path) {
goto on_error;
}

if (sizeof(st.st_size) != sizeof(size_t)) {
// on 32 bit systems, check if there is an overflow
if (st.st_size > (off_t)SSIZE_MAX) {
fprintf(stderr, "mmap() size_t overflow for file %s\n", path);
goto on_error;
}
if (st.st_size > (intmax_t) SIZE_MAX ) {
fprintf(stderr, "mmap() file %s too big\n", path);
goto on_error;
}


mf->base =
(uint8_t *)mmap(NULL, (size_t)(st.st_size), PROT_READ, MAP_SHARED, fd, 0);

Expand Down
41 changes: 6 additions & 35 deletions src/stlink-lib/usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1134,33 +1134,8 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect,
#endif

libusb_device **list = NULL;
// TODO: We should use ssize_t and use it as a counter if > 0.
Nightwalker-87 marked this conversation as resolved.
Show resolved Hide resolved
// As per libusb API: ssize_t libusb_get_device_list (libusb_context *ctx, libusb_device ***list)
int cnt = (int)libusb_get_device_list(slu->libusb_ctx, &list);
ssize_t cnt = libusb_get_device_list(slu->libusb_ctx, &list);
struct libusb_device_descriptor desc;
int devBus = 0;
int devAddr = 0;

// TODO: Reading a environment variable in a usb open function is not very nice, this should
// be refactored and moved into the CLI tools, and instead of giving USB_BUS:USB_ADDR a real
// stlink serial string should be passed to this function. Probably people are using this
// but this is very odd because as programmer can change to multiple busses and it is better
// to detect them based on serial.
char *device = getenv("STLINK_DEVICE");

if (device) {
char *c = strchr(device, ':');

if (c == NULL) {
WLOG("STLINK_DEVICE must be <USB_BUS>:<USB_ADDR> format\n");
goto on_error;
}

devBus = atoi(device);
*c++ = 0;
devAddr = atoi(c);
ILOG("bus %03d dev %03d\n", devBus, devAddr);
}

while (cnt-- > 0) {
struct libusb_device_handle *handle;
Expand All @@ -1169,13 +1144,6 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect,

if (desc.idVendor != STLINK_USB_VID_ST) { continue; }

if (devBus && devAddr) {
if ((libusb_get_bus_number(list[cnt]) != devBus) ||
(libusb_get_device_address(list[cnt]) != devAddr)) {
continue;
}
}

ret = libusb_open(list[cnt], &handle);

if (ret) { continue; } // could not open device
Expand All @@ -1202,7 +1170,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect,
}

if (cnt < 0) {
WLOG ("Couldn't find %s ST-Link devices\n", (devBus && devAddr) ? "matched" : "any");
WLOG ("Couldn't find any ST-Link devices\n");
libusb_free_device_list(list, 1);
goto on_error;
} else {
Expand All @@ -1221,6 +1189,8 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect,

libusb_free_device_list(list, 1);

// libusb_kernel_driver_active is not available on Windows.
#if !defined(_WIN32)
Nightwalker-87 marked this conversation as resolved.
Show resolved Hide resolved
if (libusb_kernel_driver_active(slu->usb_handle, 0) == 1) {
ret = libusb_detach_kernel_driver(slu->usb_handle, 0);

Expand All @@ -1229,6 +1199,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect,
goto on_libusb_error;
}
}
#endif

if (libusb_get_configuration(slu->usb_handle, &config)) {
// this may fail for a previous configured device
Expand Down Expand Up @@ -1287,7 +1258,7 @@ stlink_t *stlink_open_usb(enum ugly_loglevel verbose, enum connect_type connect,
// the NRST pin must be pull down before selecting the SWD/JTAG mode
if (mode == STLINK_DEV_DEBUG_MODE) {
DLOG("-- exit_debug_mode\n");
_stlink_usb_exit_dfu_mode(sl);
_stlink_usb_exit_debug_mode(sl);
}

_stlink_usb_jtag_reset(sl, STLINK_DEBUG_APIV2_DRIVE_NRST_LOW);
Expand Down