From 4e058cac0d1db29824000a24d2dd602885880e50 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Thu, 28 May 2020 04:39:37 +0000 Subject: [PATCH] fastboot: Allow fastboot to asynchronously differentiate between fastboot and fastbootd. It's not possible to programmatically determine which fastboot mode a device is in, without sending a getvar:is-userspace query. Unfortunately this is not possible asynchronously, and may interrupt other queries being processed. This patch changes fastbootd's USB interface name to "fastbootd". Note that tools use the protocol number/class and not this string, so it should be safe to extend. When using "fastboot devices", the interface name is now listed if set. Note that currently only the Linux version of the fastboot tool is capable of reading the interface name. Bug: 156966319 Test: fastboot devices on Linux Change-Id: I57ccf2bec1dda573fe3ac628a646624b76f45905 --- fastboot/device/usb_client.cpp | 2 +- fastboot/fastboot.cpp | 8 ++++++-- fastboot/usb.h | 2 ++ fastboot/usb_linux.cpp | 9 +++++++++ fastboot/usb_osx.cpp | 1 + fastboot/usb_windows.cpp | 1 + 6 files changed, 20 insertions(+), 3 deletions(-) diff --git a/fastboot/device/usb_client.cpp b/fastboot/device/usb_client.cpp index 9c8076528..c65316707 100644 --- a/fastboot/device/usb_client.cpp +++ b/fastboot/device/usb_client.cpp @@ -146,7 +146,7 @@ static struct SsFuncDesc ss_descriptors = { }, }; -#define STR_INTERFACE_ "fastboot" +#define STR_INTERFACE_ "fastbootd" static const struct { struct usb_functionfs_strings_head header; diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp index 7f6e7230f..7abc936b4 100644 --- a/fastboot/fastboot.cpp +++ b/fastboot/fastboot.cpp @@ -258,6 +258,10 @@ static int match_fastboot(usb_ifc_info* info) { static int list_devices_callback(usb_ifc_info* info) { if (match_fastboot_with_serial(info, nullptr) == 0) { std::string serial = info->serial_number; + std::string interface = info->interface; + if (interface.empty()) { + interface = "fastboot"; + } if (!info->writable) { serial = UsbNoPermissionsShortHelpText(); } @@ -266,9 +270,9 @@ static int list_devices_callback(usb_ifc_info* info) { } // output compatible with "adb devices" if (!g_long_listing) { - printf("%s\tfastboot", serial.c_str()); + printf("%s\t%s", serial.c_str(), interface.c_str()); } else { - printf("%-22s fastboot", serial.c_str()); + printf("%-22s %s", serial.c_str(), interface.c_str()); if (strlen(info->device_path) > 0) printf(" %s", info->device_path); } putchar('\n'); diff --git a/fastboot/usb.h b/fastboot/usb.h index 7ca44c414..e5f56e20a 100644 --- a/fastboot/usb.h +++ b/fastboot/usb.h @@ -50,6 +50,8 @@ struct usb_ifc_info { char serial_number[256]; char device_path[256]; + + char interface[256]; }; class UsbTransport : public Transport { diff --git a/fastboot/usb_linux.cpp b/fastboot/usb_linux.cpp index 6363aa547..964488cc8 100644 --- a/fastboot/usb_linux.cpp +++ b/fastboot/usb_linux.cpp @@ -43,6 +43,8 @@ #include #include +#include +#include #include #include #include @@ -263,6 +265,13 @@ static int filter_usb_device(char* sysfs_name, info.has_bulk_in = (in != -1); info.has_bulk_out = (out != -1); + std::string interface; + auto path = android::base::StringPrintf("/sys/bus/usb/devices/%s/%s:1.%d/interface", + sysfs_name, sysfs_name, ifc->bInterfaceNumber); + if (android::base::ReadFileToString(path, &interface)) { + snprintf(info.interface, sizeof(info.interface), "%s", interface.c_str()); + } + if(callback(&info) == 0) { *ept_in_id = in; *ept_out_id = out; diff --git a/fastboot/usb_osx.cpp b/fastboot/usb_osx.cpp index 8a3c21331..610eebfb2 100644 --- a/fastboot/usb_osx.cpp +++ b/fastboot/usb_osx.cpp @@ -368,6 +368,7 @@ static int try_device(io_service_t device, usb_handle *handle) { // device has no serial number handle->info.serial_number[0] = 0; } + handle->info.interface[0] = 0; handle->info.writable = 1; if (try_interfaces(dev, handle)) { diff --git a/fastboot/usb_windows.cpp b/fastboot/usb_windows.cpp index bf840f888..67bf8a30b 100644 --- a/fastboot/usb_windows.cpp +++ b/fastboot/usb_windows.cpp @@ -319,6 +319,7 @@ int recognized_device(usb_handle* handle, ifc_match_func callback) { &serial_number_len, true)) { info.serial_number[0] = 0; } + info.interface[0] = 0; info.device_path[0] = 0;