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/hardware.cc b/hardware.cc
new file mode 100644
index 0000000..07901f5
--- /dev/null
+++ b/hardware.cc
@@ -0,0 +1,47 @@
+// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "update_engine/hardware.h"
+
+#include <base/logging.h>
+#include <rootdev/rootdev.h>
+
+using std::string;
+
+namespace chromeos_update_engine {
+
+const string Hardware::BootDevice() {
+  char boot_path[PATH_MAX];
+  // Resolve the boot device path fully, including dereferencing
+  // through dm-verity.
+  int ret = rootdev(boot_path, sizeof(boot_path), true, false);
+
+  if (ret < 0) {
+    LOG(ERROR) << "rootdev failed to find the root device";
+    return "";
+  }
+  LOG_IF(WARNING, ret > 0) << "rootdev found a device name with no device node";
+
+  // This local variable is used to construct the return string and is not
+  // passed around after use.
+  return boot_path;
+}
+
+const string Hardware::KernelDeviceOfBootDevice(
+    const std::string& boot_device) {
+  // Currently this assumes the last digit of the boot device is
+  // 3, 5, or 7, and changes it to 2, 4, or 6, respectively, to
+  // get the kernel device.
+  string ret = boot_device;
+  if (ret.empty())
+    return ret;
+  char last_char = ret[ret.size() - 1];
+  if (last_char == '3' || last_char == '5' || last_char == '7') {
+    ret[ret.size() - 1] = last_char - 1;
+    return ret;
+  }
+  return "";
+}
+
+}  // namespace chromeos_update_engine