Remove utils::DeviceForMountPoint().

We only use it to get misc block device path, it can be replaced
with get_bootloader_message_blk_device(), and for writing bootloader
message, we can use update_bootloader_message() instead.

Also removed dependency on libfs_mgr since we are now using
libbootloader_message instead of using libfs_mgr directly.

Bug: None
Test: update_engine_unittests
Test: perform an OTA with powerwash
Change-Id: I80d0eea8e3ef6084820298cc0079da26fe1bfc63
diff --git a/Android.mk b/Android.mk
index 364fb39..e141ae1 100644
--- a/Android.mk
+++ b/Android.mk
@@ -211,6 +211,7 @@
     $(ue_update_metadata_protos_exported_static_libraries)
 
 ue_libupdate_engine_boot_control_exported_shared_libraries := \
+    libbootloader_message \
     libhwbinder \
     libhidlbase \
     libutils \
@@ -258,7 +259,6 @@
     libcurl \
     libcutils \
     libexpat \
-    libfs_mgr \
     liblog \
     libmetrics \
     libssl \
@@ -339,8 +339,7 @@
     update_manager/update_manager.cc \
     update_manager/update_time_restrictions_policy_impl.cc \
     update_manager/weekly_time.cc \
-    update_status_utils.cc \
-    utils_android.cc
+    update_status_utils.cc
 ifeq ($(local_use_binder),1)
 LOCAL_AIDL_INCLUDES += $(LOCAL_PATH)/binder_bindings
 LOCAL_SRC_FILES += \
@@ -381,7 +380,6 @@
     libbrillo-binder \
     libcurl \
     libcutils \
-    libfs_mgr \
     liblog \
     libmetricslogger \
     libssl \
@@ -422,8 +420,7 @@
     proxy_resolver.cc \
     update_attempter_android.cc \
     update_boot_flags_action.cc \
-    update_status_utils.cc \
-    utils_android.cc
+    update_status_utils.cc
 include $(BUILD_STATIC_LIBRARY)
 
 endif  # local_use_omaha == 1
@@ -499,8 +496,7 @@
     sideload_main.cc \
     update_attempter_android.cc \
     update_boot_flags_action.cc \
-    update_status_utils.cc \
-    utils_android.cc
+    update_status_utils.cc
 # Use commonly used shared libraries. libprotobuf-cpp-lite.so is filtered out,
 # as it doesn't look beneficial to be installed separately due to its size. Note
 # that we explicitly request their recovery variants, so that the expected files
@@ -508,7 +504,6 @@
 LOCAL_SHARED_LIBRARIES := \
     libbase.recovery \
     libbootloader_message.recovery \
-    libfs_mgr.recovery \
     liblog.recovery \
     $(filter-out libprotobuf-cpp-lite.recovery,$(ue_libpayload_consumer_exported_shared_libraries:=.recovery))
 LOCAL_STATIC_LIBRARIES := \
diff --git a/boot_control_android.cc b/boot_control_android.cc
index 8c1603b..12a3a10 100644
--- a/boot_control_android.cc
+++ b/boot_control_android.cc
@@ -16,14 +16,16 @@
 
 #include "update_engine/boot_control_android.h"
 
+#include <memory>
+#include <utility>
+
 #include <base/bind.h>
 #include <base/files/file_util.h>
 #include <base/logging.h>
-#include <base/strings/string_util.h>
+#include <bootloader_message/bootloader_message.h>
 #include <brillo/message_loops/message_loop.h>
 
 #include "update_engine/common/utils.h"
-#include "update_engine/utils_android.h"
 
 using std::string;
 
@@ -96,12 +98,14 @@
   // of misc and then finding an entry in /dev matching the sysfs
   // entry.
 
-  base::FilePath misc_device;
-  if (!utils::DeviceForMountPoint("/misc", &misc_device))
+  string err, misc_device = get_bootloader_message_blk_device(&err);
+  if (misc_device.empty()) {
+    LOG(ERROR) << "Unable to get misc block device: " << err;
     return false;
+  }
 
-  if (!utils::IsSymlink(misc_device.value().c_str())) {
-    LOG(ERROR) << "Device file " << misc_device.value() << " for /misc "
+  if (!utils::IsSymlink(misc_device.c_str())) {
+    LOG(ERROR) << "Device file " << misc_device << " for /misc "
                << "is not a symlink.";
     return false;
   }
@@ -118,7 +122,8 @@
     return false;
   }
 
-  base::FilePath path = misc_device.DirName().Append(partition_name + suffix);
+  base::FilePath path =
+      base::FilePath(misc_device).DirName().Append(partition_name + suffix);
   if (!base::PathExists(path)) {
     LOG(ERROR) << "Device file " << path.value() << " does not exist.";
     return false;
diff --git a/boot_control_recovery.cc b/boot_control_recovery.cc
index b74f4aa..722d32a 100644
--- a/boot_control_recovery.cc
+++ b/boot_control_recovery.cc
@@ -16,14 +16,16 @@
 
 #include "update_engine/boot_control_recovery.h"
 
+#include <memory>
+#include <utility>
+
 #include <base/bind.h>
 #include <base/files/file_util.h>
 #include <base/logging.h>
-#include <base/strings/string_util.h>
+#include <bootloader_message/bootloader_message.h>
 #include <brillo/message_loops/message_loop.h>
 
 #include "update_engine/common/utils.h"
-#include "update_engine/utils_android.h"
 
 using std::string;
 
@@ -111,12 +113,14 @@
   // of misc and then finding an entry in /dev matching the sysfs
   // entry.
 
-  base::FilePath misc_device;
-  if (!utils::DeviceForMountPoint("/misc", &misc_device))
+  string err, misc_device = get_bootloader_message_blk_device(&err);
+  if (misc_device.empty()) {
+    LOG(ERROR) << "Unable to get misc block device: " << err;
     return false;
+  }
 
-  if (!utils::IsSymlink(misc_device.value().c_str())) {
-    LOG(ERROR) << "Device file " << misc_device.value() << " for /misc "
+  if (!utils::IsSymlink(misc_device.c_str())) {
+    LOG(ERROR) << "Device file " << misc_device << " for /misc "
                << "is not a symlink.";
     return false;
   }
@@ -128,7 +132,8 @@
     return false;
   }
 
-  base::FilePath path = misc_device.DirName().Append(partition_name + suffix);
+  base::FilePath path =
+      base::FilePath(misc_device).DirName().Append(partition_name + suffix);
   if (!base::PathExists(path)) {
     LOG(ERROR) << "Device file " << path.value() << " does not exist.";
     return false;
diff --git a/hardware_android.cc b/hardware_android.cc
index 9dd8bb6..a8a479d 100644
--- a/hardware_android.cc
+++ b/hardware_android.cc
@@ -16,22 +16,16 @@
 
 #include "update_engine/hardware_android.h"
 
-#include <fcntl.h>
-#include <sys/stat.h>
 #include <sys/types.h>
 
-#include <algorithm>
 #include <memory>
 
 #include <android-base/properties.h>
 #include <base/files/file_util.h>
-#include <base/strings/stringprintf.h>
 #include <bootloader_message/bootloader_message.h>
 
 #include "update_engine/common/hardware.h"
 #include "update_engine/common/platform_constants.h"
-#include "update_engine/common/utils.h"
-#include "update_engine/utils_android.h"
 
 using android::base::GetBoolProperty;
 using android::base::GetIntProperty;
@@ -42,12 +36,6 @@
 
 namespace {
 
-// The powerwash arguments passed to recovery. Arguments are separated by \n.
-const char kAndroidRecoveryPowerwashCommand[] =
-    "recovery\n"
-    "--wipe_data\n"
-    "--reason=wipe_data_from_ota\n";
-
 // Android properties that identify the hardware and potentially non-updatable
 // parts of the bootloader (such as the bootloader version and the baseband
 // version).
@@ -58,39 +46,6 @@
 const char kPropBootRevision[] = "ro.boot.revision";
 const char kPropBuildDateUTC[] = "ro.build.date.utc";
 
-// Write a recovery command line |message| to the BCB. The arguments to recovery
-// must be separated by '\n'. An empty string will erase the BCB.
-bool WriteBootloaderRecoveryMessage(const string& message) {
-  base::FilePath misc_device;
-  if (!utils::DeviceForMountPoint("/misc", &misc_device))
-    return false;
-
-  // Setup a bootloader_message with just the command and recovery fields set.
-  bootloader_message boot = {};
-  if (!message.empty()) {
-    strncpy(boot.command, "boot-recovery", sizeof(boot.command) - 1);
-    memcpy(boot.recovery,
-           message.data(),
-           std::min(message.size(), sizeof(boot.recovery) - 1));
-  }
-
-  int fd = HANDLE_EINTR(open(misc_device.value().c_str(), O_WRONLY | O_SYNC));
-  if (fd < 0) {
-    PLOG(ERROR) << "Opening misc";
-    return false;
-  }
-  ScopedFdCloser fd_closer(&fd);
-  // We only re-write the first part of the bootloader_message, up to and
-  // including the recovery message.
-  size_t boot_size =
-      offsetof(bootloader_message, recovery) + sizeof(boot.recovery);
-  if (!utils::WriteAll(fd, &boot, boot_size)) {
-    PLOG(ERROR) << "Writing recovery command to misc";
-    return false;
-  }
-  return true;
-}
-
 }  // namespace
 
 namespace hardware {
@@ -199,11 +154,22 @@
 
 bool HardwareAndroid::SchedulePowerwash() {
   LOG(INFO) << "Scheduling a powerwash to BCB.";
-  return WriteBootloaderRecoveryMessage(kAndroidRecoveryPowerwashCommand);
+  string err;
+  if (!update_bootloader_message({"--wipe_data", "--reason=wipe_data_from_ota"},
+                                 &err)) {
+    LOG(ERROR) << "Failed to update bootloader message: " << err;
+    return false;
+  }
+  return true;
 }
 
 bool HardwareAndroid::CancelPowerwash() {
-  return WriteBootloaderRecoveryMessage("");
+  string err;
+  if (!clear_bootloader_message(&err)) {
+    LOG(ERROR) << "Failed to clear bootloader message: " << err;
+    return false;
+  }
+  return true;
 }
 
 bool HardwareAndroid::GetNonVolatileDirectory(base::FilePath* path) const {
diff --git a/image_properties_android.cc b/image_properties_android.cc
index 1d82feb..2d418b3 100644
--- a/image_properties_android.cc
+++ b/image_properties_android.cc
@@ -33,7 +33,6 @@
 #include "update_engine/common/prefs_interface.h"
 #include "update_engine/common/utils.h"
 #include "update_engine/system_state.h"
-#include "update_engine/utils_android.h"
 
 using android::base::GetProperty;
 using std::string;
@@ -79,18 +78,23 @@
 
 // Open misc partition for read or write and output the fd in |out_fd|.
 bool OpenMisc(bool write, int* out_fd) {
-  base::FilePath misc_device;
+  string misc_device;
   int flags = write ? O_WRONLY | O_SYNC : O_RDONLY;
   if (root_prefix) {
     // Use a file for unittest and create one if doesn't exist.
-    misc_device = base::FilePath(root_prefix).Append("misc");
+    misc_device = base::FilePath(root_prefix).Append("misc").value();
     if (write)
       flags |= O_CREAT;
-  } else if (!utils::DeviceForMountPoint("/misc", &misc_device)) {
-    return false;
+  } else {
+    string err;
+    misc_device = get_bootloader_message_blk_device(&err);
+    if (misc_device.empty()) {
+      LOG(ERROR) << "Unable to get misc block device: " << err;
+      return false;
+    }
   }
 
-  int fd = HANDLE_EINTR(open(misc_device.value().c_str(), flags, 0600));
+  int fd = HANDLE_EINTR(open(misc_device.c_str(), flags, 0600));
   if (fd < 0) {
     PLOG(ERROR) << "Opening misc failed";
     return false;
diff --git a/utils_android.cc b/utils_android.cc
deleted file mode 100644
index 393e65a..0000000
--- a/utils_android.cc
+++ /dev/null
@@ -1,65 +0,0 @@
-//
-// Copyright (C) 2016 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-#include "update_engine/utils_android.h"
-
-#include <fs_mgr.h>
-
-using std::string;
-
-namespace chromeos_update_engine {
-
-namespace {
-
-// Open the appropriate fstab file and fallback to /fstab.device if
-// that's what's being used.
-static struct fstab* OpenFSTab() {
-  struct fstab* fstab = fs_mgr_read_fstab_default();
-  if (fstab != nullptr)
-    return fstab;
-
-  fstab = fs_mgr_read_fstab("/fstab.device");
-  return fstab;
-}
-
-}  // namespace
-
-namespace utils {
-
-bool DeviceForMountPoint(const string& mount_point, base::FilePath* device) {
-  struct fstab* fstab;
-  struct fstab_rec* record;
-
-  fstab = OpenFSTab();
-  if (fstab == nullptr) {
-    LOG(ERROR) << "Error opening fstab file.";
-    return false;
-  }
-  record = fs_mgr_get_entry_for_mount_point(fstab, mount_point.c_str());
-  if (record == nullptr) {
-    LOG(ERROR) << "Error finding " << mount_point << " entry in fstab file.";
-    fs_mgr_free_fstab(fstab);
-    return false;
-  }
-
-  *device = base::FilePath(record->blk_device);
-  fs_mgr_free_fstab(fstab);
-  return true;
-}
-
-}  // namespace utils
-
-}  // namespace chromeos_update_engine
diff --git a/utils_android.h b/utils_android.h
deleted file mode 100644
index 18dd8ab..0000000
--- a/utils_android.h
+++ /dev/null
@@ -1,37 +0,0 @@
-//
-// Copyright (C) 2016 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-//
-
-#ifndef UPDATE_ENGINE_UTILS_ANDROID_H_
-#define UPDATE_ENGINE_UTILS_ANDROID_H_
-
-#include <string>
-
-#include <base/files/file_util.h>
-
-namespace chromeos_update_engine {
-
-namespace utils {
-
-// Find the block device that should be mounted in the |mount_point| path and
-// store it in |device|. Returns whether a device was found on the fstab.
-bool DeviceForMountPoint(const std::string& mount_point,
-                         base::FilePath* device);
-
-}  // namespace utils
-
-}  // namespace chromeos_update_engine
-
-#endif  // UPDATE_ENGINE_UTILS_ANDROID_H_