Send an UMA metric when failed to boot into the new partition.

When a payload is successfully applied, the /other/ partition
is marked as valid and a reboot is needed, the reboot into this
new partition can fail due to several reasons. If than happens,
the firmware can rollback to the previous partition.

When this happens, this fix sends a new UMA metric with the
attempt number of this failing payload.

In order to test this functionality we need to fake the
utils::BootDevice() to emulate a reboot into the same or
a different partition. To achieve this, this function is
moved to a new "HardwareInterface" that can be faked
using the FakeHardware class that can hold similar hardware
related functions. Implementations and unittest were
refactored as needed.

BUG=chromium:243572
TEST=unittests

Change-Id: I1a4242df0bd61e2718ab881ead603b1d3705b877
Reviewed-on: https://gerrit.chromium.org/gerrit/61815
Commit-Queue: Alex Deymo <deymo@chromium.org>
Reviewed-by: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/update_attempter.cc b/update_attempter.cc
index 6809108..0f8abfb 100644
--- a/update_attempter.cc
+++ b/update_attempter.cc
@@ -21,6 +21,7 @@
 #include "update_engine/download_action.h"
 #include "update_engine/filesystem_copier_action.h"
 #include "update_engine/gpio_handler.h"
+#include "update_engine/hardware_interface.h"
 #include "update_engine/libcurl_http_fetcher.h"
 #include "update_engine/multi_range_http_fetcher.h"
 #include "update_engine/omaha_request_action.h"
@@ -463,9 +464,9 @@
   shared_ptr<OmahaResponseHandlerAction> response_handler_action(
       new OmahaResponseHandlerAction(system_state_));
   shared_ptr<FilesystemCopierAction> filesystem_copier_action(
-      new FilesystemCopierAction(false, false));
+      new FilesystemCopierAction(system_state_, false, false));
   shared_ptr<FilesystemCopierAction> kernel_filesystem_copier_action(
-      new FilesystemCopierAction(true, false));
+      new FilesystemCopierAction(system_state_, true, false));
   shared_ptr<OmahaRequestAction> download_started_action(
       new OmahaRequestAction(system_state_,
                              new OmahaEvent(
@@ -491,9 +492,9 @@
                                                     is_test_mode_),
                              false));
   shared_ptr<FilesystemCopierAction> filesystem_verifier_action(
-      new FilesystemCopierAction(false, true));
+      new FilesystemCopierAction(system_state_, false, true));
   shared_ptr<FilesystemCopierAction> kernel_filesystem_verifier_action(
-      new FilesystemCopierAction(true, true));
+      new FilesystemCopierAction(system_state_, true, true));
   shared_ptr<OmahaRequestAction> update_complete_action(
       new OmahaRequestAction(system_state_,
                              new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
@@ -566,15 +567,17 @@
   LOG(INFO) << "Setting rollback options.";
   InstallPlan install_plan;
   if (install_path == NULL) {
-    TEST_AND_RETURN_FALSE(utils::GetInstallDev(utils::BootDevice(),
-                                               &install_plan.install_path));
+    TEST_AND_RETURN_FALSE(utils::GetInstallDev(
+        system_state_->hardware()->BootDevice(),
+        &install_plan.install_path));
   }
   else {
     install_plan.install_path = *install_path;
   }
 
-  install_plan.kernel_install_path = utils::BootKernelDevice(
-      install_plan.install_path);
+  install_plan.kernel_install_path =
+      system_state_->hardware()->KernelDeviceOfBootDevice(
+          install_plan.install_path);
   install_plan.powerwash_required = powerwash;
   if (powerwash) {
     // Enterprise-enrolled devices have an empty owner in their device policy.
@@ -703,6 +706,17 @@
                        kUpdateNoticeUnspecified);
     LOG(INFO) << "Update successfully applied, waiting to reboot.";
 
+    const InstallPlan& install_plan = response_handler_action_->install_plan();
+
+    // Generate an unique payload identifier.
+    const string target_version_uid =
+        install_plan.payload_hash + ":" + install_plan.metadata_signature;
+
+    // Expect to reboot into the new version to send the proper metric during
+    // next boot.
+    system_state_->payload_state()->ExpectRebootInNewVersion(
+        target_version_uid);
+
     // Also report the success code so that the percentiles can be
     // interpreted properly for the remaining error codes in UMA.
     utils::SendErrorCodeToUma(system_state_, code);
@@ -844,6 +858,9 @@
       if (!file_util::Delete(kUpdateCompletedMarkerPath, false))
         ret_value = false;
 
+      // Notify the PayloadState that the successful payload was canceled.
+      system_state_->payload_state()->ResetUpdateStatus();
+
       return ret_value;
     }