Call set_active after flashall and update

If the device has previously failed to boot, and the current slot is
marked as unbootable, we must call set_active to reenable the slot.

Bug: 29827625
Change-Id: I8b723dda80e246b48e5967aff4503c3d120bfb9b
diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp
index 636092e..706063a 100644
--- a/fastboot/fastboot.cpp
+++ b/fastboot/fastboot.cpp
@@ -314,8 +314,11 @@
             "\n"
             "commands:\n"
             "  update <filename>                        Reflash device from update.zip.\n"
+            "                                           Sets the flashed slot as active.\n"
             "  flashall                                 Flash boot, system, vendor, and --\n"
-            "                                           if found -- recovery.\n"
+            "                                           if found -- recovery. If the device\n"
+            "                                           supports slots, the slot that has\n"
+            "                                           been flashed to is set as active.\n"
             "  flash <partition> [ <filename> ]         Write a file to a flash partition.\n"
             "  flashing lock                            Locks the device. Prevents flashing.\n"
             "  flashing unlock                          Unlocks the device. Allows flashing\n"
@@ -973,6 +976,20 @@
     fb_queue_command("signature", "installing signature");
 }
 
+// Sets slot_override as the active slot. If slot_override is blank,
+// set current slot as active instead. This clears slot-unbootable.
+static void set_active(Transport* transport, const char* slot_override) {
+    if (slot_override && slot_override[0]) {
+        fb_set_active(slot_override);
+    } else {
+        std::string current_slot;
+        if (fb_getvar(transport, "current-slot", &current_slot)) {
+            current_slot = verify_slot(transport, current_slot.c_str(), false);
+            fb_set_active(current_slot.c_str());
+        }
+    }
+}
+
 static void do_update(Transport* transport, const char* filename, const char* slot_override, bool erase_first) {
     queue_info_dump();
 
@@ -1022,6 +1039,7 @@
     }
 
     CloseArchive(zip);
+    set_active(transport, slot_override);
 }
 
 static void do_send_signature(char* fn) {
@@ -1072,6 +1090,8 @@
         };
         do_for_partitions(transport, images[i].part_name, slot_override, flashall, false);
     }
+
+    set_active(transport, slot_override);
 }
 
 #define skip(n) do { argc -= (n); argv += (n); } while (0)
@@ -1406,7 +1426,12 @@
     if (wants_set_active) {
         if (next_active == "") {
             if (slot_override == "") {
-                wants_set_active = false;
+                std::string current_slot;
+                if (fb_getvar(transport, "current-slot", &current_slot)) {
+                    next_active = verify_slot(transport, current_slot.c_str(), false);
+                } else {
+                    wants_set_active = false;
+                }
             } else {
                 next_active = verify_slot(transport, slot_override.c_str(), false);
             }