fastboot: Add ability to specify device path

For manufacturing and testing, there is a need to talk to
whatever device is connected to a given port on the host.  This
change modifies fastboot's "-s" option to take either a serial
number or a device path.  The device paths of the connected
devices can be listed using "fastboot -l devices" whose output
will resemble:

    016B75D60A00600D	usb:2-5	fastboot
    AD3C12020173	usb:1-4.3	fastboot

The second column lists the device paths.  If the -l option is
not given, the output from "fastboot devices" will be the same as
it used to be (i.e. the paths will not be printed).

Finally, note that the format of the device paths are platform
dependent.  The example above is from Linux.  On OS-X, the paths
will be "usb:" followed by hex digits.  For Windows, the device
paths will be printed as "????????????" and the -s option will
not be able to select a device until someone implements the
underlying functionality in usb_windows.c.

Change-Id: I1f01b8f47acd32edb0ac18db107316a2c923bbde
Signed-off-by: Scott Anderson <saa@android.com>
diff --git a/fastboot/fastboot.c b/fastboot/fastboot.c
index 805014f..f1b675f 100644
--- a/fastboot/fastboot.c
+++ b/fastboot/fastboot.c
@@ -58,6 +58,7 @@
 static const char *cmdline = 0;
 static int wipe_data = 0;
 static unsigned short vendor_id = 0;
+static int long_listing = 0;
 
 static unsigned base_addr = 0x10000000;
 
@@ -163,9 +164,10 @@
     if(info->ifc_class != 0xff) return -1;
     if(info->ifc_subclass != 0x42) return -1;
     if(info->ifc_protocol != 0x03) return -1;
-    // require matching serial number if a serial number is specified
+    // require matching serial number or device path if requested
     // at the command line with the -s option.
-    if (serial && strcmp(serial, info->serial_number) != 0) return -1;
+    if (serial && (strcmp(serial, info->serial_number) != 0 &&
+                   strcmp(serial, info->device_path) != 0)) return -1;
     return 0;
 }
 
@@ -179,8 +181,16 @@
         if (!serial[0]) {
             serial = "????????????";
         }
-        // output compatible with "adb devices"
-        printf("%s\tfastboot\n", serial);
+        if (!long_listing) {
+            // output compatible with "adb devices"
+            printf("%s\tfastboot\n", serial);
+        } else {
+            char* device_path = info->device_path;
+            if (!device_path[0]) {
+                device_path = "????????????";
+            }
+            printf("%s\t%s\tfastboot\n", serial, device_path);
+        }
     }
 
     return -1;
@@ -234,7 +244,9 @@
             "\n"
             "options:\n"
             "  -w                                       erase userdata and cache\n"
-            "  -s <serial number>                       specify device serial number\n"
+            "  -s <specific device>                     specify device serial number\n"
+            "                                           or path to device port\n"
+            "  -l                                       with \"devices\", lists device paths\n"
             "  -p <product>                             specify product name\n"
             "  -c <cmdline>                             override kernel commandline\n"
             "  -i <vendor id>                           specify a custom USB vendor id\n"
@@ -567,6 +579,7 @@
     int wants_wipe = 0;
     int wants_reboot = 0;
     int wants_reboot_bootloader = 0;
+    int wants_device_list = 0;
     void *data;
     unsigned sz;
     unsigned page_size = 2048;
@@ -578,11 +591,6 @@
         return 1;
     }
 
-    if (!strcmp(*argv, "devices")) {
-        list_devices();
-        return 0;
-    }
-
     if (!strcmp(*argv, "help")) {
         usage();
         return 0;
@@ -608,6 +616,9 @@
             require(2);
             serial = argv[1];
             skip(2);
+        } else if(!strcmp(*argv, "-l")) {
+            long_listing = 1;
+            skip(1);
         } else if(!strcmp(*argv, "-p")) {
             require(2);
             product = argv[1];
@@ -626,6 +637,9 @@
                 die("invalid vendor id '%s'", argv[1]);
             vendor_id = (unsigned short)val;
             skip(2);
+        } else if (!strcmp(*argv, "devices")) {
+            skip(1);
+            wants_device_list = 1;
         } else if(!strcmp(*argv, "getvar")) {
             require(2);
             fb_queue_display(argv[1], argv[1]);
@@ -721,6 +735,9 @@
         }
     }
 
+    if (wants_device_list)
+        list_devices();
+
     if (wants_wipe) {
         fb_queue_erase("userdata");
         fb_queue_format("userdata", 1);
@@ -733,6 +750,9 @@
         fb_queue_command("reboot-bootloader", "rebooting into bootloader");
     }
 
+    if (fb_queue_is_empty())
+        return 0;
+
     usb = open_device();
 
     status = fb_execute_queue(usb);