fastboot: add flashing bootloader commands

add the following commands:
    flashing unlock_bootloader
    flashing lock_bootloader
    flashing get_unlock_bootloader_nonce

Change-Id: Ia4f56ebdcb6785a5196cc669d68da1553ed53c58
Signed-off-by: Patrick Tjin <pattjin@google.com>
diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp
index be80cce..de4c0ea 100644
--- a/fastboot/fastboot.cpp
+++ b/fastboot/fastboot.cpp
@@ -279,17 +279,22 @@
             "  flashall                                 flash boot, system, vendor and if found,\n"
             "                                           recovery\n"
             "  flash <partition> [ <filename> ]         write a file to a flash partition\n"
-            "  flashing lock                            locks the device. Prevents flashing"
+            "  flashing lock                            locks the device. Prevents flashing\n"
             "                                           partitions\n"
-            "  flashing unlock                          unlocks the device. Allows user to"
-            "                                           flash any partition except the ones"
+            "  flashing unlock                          unlocks the device. Allows user to\n"
+            "                                           flash any partition except the ones\n"
             "                                           that are related to bootloader\n"
-            "  flashing lock_critical                   Prevents flashing bootloader related"
+            "  flashing lock_critical                   Prevents flashing bootloader related\n"
             "                                           partitions\n"
-            "  flashing unlock_critical                 Enables flashing bootloader related"
+            "  flashing unlock_critical                 Enables flashing bootloader related\n"
             "                                           partitions\n"
-            "  flashing get_unlock_ability              Queries bootloader to see if the"
+            "  flashing get_unlock_ability              Queries bootloader to see if the\n"
             "                                           device is unlocked\n"
+            "  flashing get_unlock_bootloader_nonce     Queries the bootloader to get the\n"
+            "                                           unlock nonce\n"
+            "  flashing unlock_bootloader <request>     Issue unlock bootloader using request\n"
+            "  flashing lock_bootloader                 Locks the bootloader to prevent\n"
+            "                                           bootloader version rollback\n"
             "  erase <partition>                        erase a flash partition\n"
             "  format[:[<fs type>][:[<size>]] <partition> format a flash partition.\n"
             "                                           Can override the fs type and/or\n"
@@ -831,6 +836,27 @@
 #define skip(n) do { argc -= (n); argv += (n); } while (0)
 #define require(n) do { if (argc < (n)) {usage(); exit(1);}} while (0)
 
+int do_bypass_unlock_command(int argc, char **argv)
+{
+    unsigned sz;
+    void *data;
+
+    if (argc <= 2) return 0;
+    skip(2);
+
+    /*
+     * Process unlock_bootloader, we have to load the message file
+     * and send that to the remote device.
+     */
+    require(1);
+    data = load_file(*argv, &sz);
+    if (data == 0) die("could not load '%s': %s", *argv, strerror(errno));
+    fb_queue_download("unlock_message", data, sz);
+    fb_queue_command("flashing unlock_bootloader", "unlocking bootloader");
+    skip(1);
+    return 0;
+}
+
 int do_oem_command(int argc, char **argv)
 {
     char command[256];
@@ -1230,12 +1256,18 @@
             wants_reboot = 1;
         } else if(!strcmp(*argv, "oem")) {
             argc = do_oem_command(argc, argv);
-        } else if(!strcmp(*argv, "flashing") && argc == 2) {
-            if(!strcmp(*(argv+1), "unlock") || !strcmp(*(argv+1), "lock")
-               || !strcmp(*(argv+1), "unlock_critical")
-               || !strcmp(*(argv+1), "lock_critical")
-               || !strcmp(*(argv+1), "get_unlock_ability")) {
-              argc = do_oem_command(argc, argv);
+        } else if(!strcmp(*argv, "flashing")) {
+            if (argc == 2 && (!strcmp(*(argv+1), "unlock") ||
+                              !strcmp(*(argv+1), "lock") ||
+                              !strcmp(*(argv+1), "unlock_critical") ||
+                              !strcmp(*(argv+1), "lock_critical") ||
+                              !strcmp(*(argv+1), "get_unlock_ability") ||
+                              !strcmp(*(argv+1), "get_unlock_bootloader_nonce") ||
+                              !strcmp(*(argv+1), "lock_bootloader"))) {
+                argc = do_oem_command(argc, argv);
+            } else
+            if (argc == 3 && !strcmp(*(argv+1), "unlock_bootloader")) {
+                argc = do_bypass_unlock_command(argc, argv);
             } else {
               usage();
               return 1;