Skip to content

Commit

Permalink
Add VID and PID device target filters
Browse files Browse the repository at this point in the history
  • Loading branch information
tomas-pecserke committed Jun 3, 2023
1 parent 02f83f3 commit 4313928
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ PICOTOOL:
Tool for interacting with a RP2040 device in BOOTSEL mode, or with a RP2040 binary
SYNOPSYS:
picotool info [-b] [-p] [-d] [-l] [-a] [--bus <bus>] [--address <addr>] [--serial <serial>] [-f] [-F]
picotool info [-b] [-p] [-d] [-l] [-a] [--bus <bus>] [--address <addr>] [--vid <vid>] [--pid <pid>] [--serial <serial>] [-f] [-F]
picotool info [-b] [-p] [-d] [-l] [-a] <filename> [-t <type>]
picotool load [-n] [-N] [-u] [-v] [-x] <filename> [-t <type>] [-o <offset>] [--bus <bus>] [--address <addr>] [--serial <serial>] [-f] [-F]
picotool save [-p] [--bus <bus>] [--address <addr>] [--serial <serial>] [-f] [-F] <filename> [-t <type>]
picotool save -a [--bus <bus>] [--address <addr>] [--serial <serial>] [-f] [-F] <filename> [-t <type>]
picotool save -r <from> <to> [--bus <bus>] [--address <addr>] [--serial <serial>] [-f] [-F] <filename> [-t <type>]
picotool verify [--bus <bus>] [--address <addr>] [--serial <serial>] [-f] [-F] <filename> [-t <type>] [-r <from> <to>] [-o <offset>]
picotool reboot [-a] [-u] [--bus <bus>] [--address <addr>] [--serial <serial>] [-f] [-F]
picotool load [-n] [-N] [-u] [-v] [-x] <filename> [-t <type>] [-o <offset>] [--bus <bus>] [--address <addr>] [--vid <vid>] [--pid <pid>] [--serial <serial>] [-f] [-F]
picotool save [-p] [--bus <bus>] [--address <addr>] [--vid <vid>] [--pid <pid>] [--serial <serial>] [-f] [-F] <filename> [-t <type>]
picotool save -a [--bus <bus>] [--address <addr>] [--vid <vid>] [--pid <pid>] [--serial <serial>] [-f] [-F] <filename> [-t <type>]
picotool save -r <from> <to> [--bus <bus>] [--address <addr>] [--vid <vid>] [--pid <pid>] [--serial <serial>] [-f] [-F] <filename> [-t <type>]
picotool verify [--bus <bus>] [--address <addr>] [--vid <vid>] [--pid <pid>] [--serial <serial>] [-f] [-F] <filename> [-t <type>] [-r <from> <to>] [-o <offset>]
picotool reboot [-a] [-u] [--bus <bus>] [--address <addr>] [--vid <vid>] [--pid <pid>] [--serial <serial>] [-f] [-F]
picotool version [-s]
picotool help [<cmd>]
Expand Down Expand Up @@ -111,7 +111,7 @@ INFO:
Without any arguments, this will display basic information for all connected RP2040 devices in BOOTSEL mode
SYNOPSYS:
picotool info [-b] [-p] [-d] [-l] [-a] [--bus <bus>] [--address <addr>] [-f] [-F]
picotool info [-b] [-p] [-d] [-l] [-a] [--bus <bus>] [--address <addr>] [--vid <vid>] [--pid <pid>] [--serial <serial>] [-f] [-F]
picotool info [-b] [-p] [-d] [-l] [-a] <filename> [-t <type>]
OPTIONS:
Expand All @@ -133,6 +133,10 @@ TARGET SELECTION:
Filter devices by USB bus number
--address <addr>
Filter devices by USB device address
--vid <vid>
Filter devices by vendor id
--pid <pid>
Filter devices by product id
--serial <serial>
Filter devices by serial id
-f, --force
Expand Down Expand Up @@ -220,9 +224,9 @@ SAVE:
Save the program / memory stored in flash on the device to a file.
SYNOPSYS:
picotool save [-p] [--bus <bus>] [--address <addr>] [-f] [-F] <filename> [-t <type>]
picotool save -a [--bus <bus>] [--address <addr>] [-f] [-F] <filename> [-t <type>]
picotool save -r <from> <to> [--bus <bus>] [--address <addr>] [-f] [-F] <filename> [-t <type>]
picotool save [-p] [--bus <bus>] [--address <addr>] [--vid <vid>] [--pid <pid>] [--serial <serial>] [-f] [-F] <filename> [-t <type>]
picotool save -a [--bus <bus>] [--address <addr>] [--vid <vid>] [--pid <pid>] [--serial <serial>] [-f] [-F] <filename> [-t <type>]
picotool save -r <from> <to> [--bus <bus>] [--address <addr>] [--vid <vid>] [--pid <pid>] [--serial <serial>] [-f] [-F] <filename> [-t <type>]
OPTIONS:
Selection of data to save
Expand All @@ -242,6 +246,10 @@ OPTIONS:
Filter devices by USB bus number
--address <addr>
Filter devices by USB device address
--vid <vid>
Filter devices by vendor id
--pid <pid>
Filter devices by product id
--serial <serial>
Filter devices by serial id
-f, --force
Expand Down
42 changes: 38 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ struct _settings {
uint32_t binary_start = FLASH_START;
int bus=-1;
int address=-1;
int vid = -1;
int pid = -1;
string serial;
uint32_t offset = 0;
uint32_t from = 0;
Expand Down Expand Up @@ -302,13 +304,17 @@ auto device_selection =
+ (option("--address") & integer("addr").min_value(1).max_value(127).set(settings.address)
.if_missing([] { return "missing address"; }))
% "Filter devices by USB device address"
).min(0).doc_non_optional(true) +
(
(option("--serial") & value("serial").set(settings.serial)
+ (option("--vid") & hex("vid").min_value(0).max_value(0xffff).set(settings.vid)
.if_missing([] { return "missing vendor id"; }))
% "Filter devices by vendor id"
+ (option("--pid") & hex("pid").min_value(0).max_value(0xffff).set(settings.pid)
.if_missing([] { return "missing product id"; }))
% "Filter devices by product id"
+ (option("--serial") & value("serial").set(settings.serial)
.if_missing([] { return "missing serial id"; }))
% "Filter devices by serial id"
#if !defined(_WIN32)
+ option('f', "--force").set(settings.force)
+ option('f', "--force").set(settings.force)
% "Force a device not in BOOTSEL mode but running compatible code to reset so the command can be executed. "
"After executing the command (unless the command itself is a 'reboot') "
"the device will be rebooted back to application mode"
Expand Down Expand Up @@ -1602,16 +1608,28 @@ string missing_device_string(bool wasRetry) {
oss << 's';
}
oss << " in BOOTSEL mode";

if (settings.vid != -1 && settings.pid != -1) {
oss << " with ID '" << std::hex << settings.vid << ':' << std::hex << settings.pid << '\'';
} else if (settings.vid != -1) {
oss << " with Vendor ID '" << std::hex << settings.vid << '\'';
} else if (settings.pid != -1) {
oss << " with Product ID '" << std::hex << settings.pid << '\'';
}

if (!settings.serial.empty()) {
oss << " with serial ID '" << settings.serial << '\'';
}

oss << " was found";

if (settings.bus != -1) {
oss << " at bus " << settings.bus;
}
if (settings.address != -1) {
oss << (settings.bus == -1 ? " with" : ",") << " address " << settings.address;
}

oss << '.';

return oss.str();
Expand Down Expand Up @@ -2205,6 +2223,16 @@ string get_usb_device_serial(libusb_device *device, libusb_device_handle *handle
return "";
}

int get_usb_device_vid_pid(libusb_device *device, uint16_t &vid, uint16_t &pid) {
struct libusb_device_descriptor desc;
int ret = libusb_get_device_descriptor(device, &desc);
if (ret) {
return -1;
}

return 0;
}

int main(int argc, char **argv) {
libusb_context *ctx = nullptr;

Expand Down Expand Up @@ -2250,6 +2278,12 @@ int main(int argc, char **argv) {
for (libusb_device **dev = devs; *dev; dev++) {
if (settings.bus != -1 && settings.bus != libusb_get_bus_number(*dev)) continue;
if (settings.address != -1 && settings.address != libusb_get_device_address(*dev)) continue;
if (settings.vid != -1 || settings.pid != -1) {
uint16_t vid, pid;
int ret = get_usb_device_vid_pid(*dev, vid, pid);
if (settings.vid != -1 && (ret || settings.vid != vid)) continue;
if (settings.pid != -1 && (ret || settings.pid != vid)) continue;
}
libusb_device_handle *handle = nullptr;
auto result = picoboot_open_device(*dev, &handle);
if (handle) {
Expand Down

0 comments on commit 4313928

Please sign in to comment.