Revert "Convert BootControl HAL to binder."

This reverts commit 29dcbf32a88f1638bb911c86b8e0ea12f84d9364.

Bug: 32973182
Change-Id: I284737db77ff1c140a0abf0c364551d4139c8914
diff --git a/boot_control_android.cc b/boot_control_android.cc
index 71ccd73..a7d7456 100644
--- a/boot_control_android.cc
+++ b/boot_control_android.cc
@@ -28,17 +28,13 @@
 
 using std::string;
 
-using android::hardware::Return;
-using android::hardware::boot::V1_0::BoolResult;
-using android::hardware::boot::V1_0::CommandResult;
-using android::hardware::boot::V1_0::IBootControl;
-using android::hardware::hidl_string;
-
-namespace {
-auto StoreResultCallback(CommandResult* dest) {
-  return [dest](const CommandResult& result) { *dest = result; };
-}
-}  // namespace
+#ifdef _UE_SIDELOAD
+// When called from update_engine_sideload, we don't attempt to dynamically load
+// the right boot_control HAL, instead we use the only HAL statically linked in
+// via the PRODUCT_STATIC_BOOT_CONTROL_HAL make variable and access the module
+// struct directly.
+extern const hw_module_t HAL_MODULE_INFO_SYM;
+#endif  // _UE_SIDELOAD
 
 namespace chromeos_update_engine {
 
@@ -56,25 +52,43 @@
 }  // namespace boot_control
 
 bool BootControlAndroid::Init() {
-  module_ = IBootControl::getService("bootctrl");
-  if (module_ == nullptr) {
-    LOG(ERROR) << "Error getting bootctrl HIDL module.\n";
+  const hw_module_t* hw_module;
+  int ret;
+
+#ifdef _UE_SIDELOAD
+  // For update_engine_sideload, we simulate the hw_get_module() by accessing it
+  // from the current process directly.
+  hw_module = &HAL_MODULE_INFO_SYM;
+  ret = 0;
+  if (!hw_module ||
+      strcmp(BOOT_CONTROL_HARDWARE_MODULE_ID, hw_module->id) != 0) {
+    ret = -EINVAL;
+  }
+#else  // !_UE_SIDELOAD
+  ret = hw_get_module(BOOT_CONTROL_HARDWARE_MODULE_ID, &hw_module);
+#endif  // _UE_SIDELOAD
+  if (ret != 0) {
+    LOG(ERROR) << "Error loading boot_control HAL implementation.";
     return false;
   }
 
-  LOG(INFO) << "Loaded boot HIDL HAL version "
-            << module_->getInterfaceVersion().get_major() << "."
-            << module_->getInterfaceVersion().get_minor();
+  module_ = reinterpret_cast<boot_control_module_t*>(const_cast<hw_module_t*>(hw_module));
+  module_->init(module_);
 
+  LOG(INFO) << "Loaded boot_control HAL "
+            << "'" << hw_module->name << "' "
+            << "version " << (hw_module->module_api_version>>8) << "."
+            << (hw_module->module_api_version&0xff) << " "
+            << "authored by '" << hw_module->author << "'.";
   return true;
 }
 
 unsigned int BootControlAndroid::GetNumSlots() const {
-  return module_->getNumberSlots();
+  return module_->getNumberSlots(module_);
 }
 
 BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
-  return module_->getCurrentSlot();
+  return module_->getCurrentSlot(module_);
 }
 
 bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
@@ -109,13 +123,8 @@
     return false;
   }
 
-  string suffix;
-  auto store_suffix_cb = [&suffix](hidl_string cb_suffix) {
-    suffix = cb_suffix.c_str();
-  };
-  Return<void> ret = module_->getSuffix(slot, store_suffix_cb);
-
-  if (!ret.getStatus().isOk()) {
+  const char* suffix = module_->getSuffix(module_, slot);
+  if (suffix == nullptr) {
     LOG(ERROR) << "boot_control impl returned no suffix for slot "
                << SlotName(slot);
     return false;
@@ -132,65 +141,42 @@
 }
 
 bool BootControlAndroid::IsSlotBootable(Slot slot) const {
-  Return<BoolResult> ret = module_->isSlotBootable(slot);
-  if (!ret.getStatus().isOk()) {
+  int ret = module_->isSlotBootable(module_, slot);
+  if (ret < 0) {
     LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
-               << " is bootable: "
-               << ret.getStatus().exceptionMessage().string();
+               << " is bootable: " << strerror(-ret);
     return false;
   }
-  if (ret == BoolResult::INVALID_SLOT) {
-    LOG(ERROR) << "Invalid slot: " << SlotName(slot);
-    return false;
-  }
-  return ret == BoolResult::TRUE;
+  return ret == 1;
 }
 
 bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
-  CommandResult result;
-  auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
-  if (!ret.getStatus().isOk()) {
-    LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
-               << SlotName(slot) << ": "
-               << ret.getStatus().exceptionMessage().string();
+  int ret = module_->setSlotAsUnbootable(module_, slot);
+  if (ret < 0) {
+    LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
+               << " as bootable: " << strerror(-ret);
     return false;
   }
-  if (!result.success) {
-    LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
-               << " as unbootable: " << result.errMsg.c_str();
-  }
-  return result.success;
+  return ret == 0;
 }
 
 bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
-  CommandResult result;
-  auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
-  if (!ret.getStatus().isOk()) {
-    LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
-               << ": " << ret.getStatus().exceptionMessage().string();
-    return false;
-  }
-  if (!result.success) {
+  int ret = module_->setActiveBootSlot(module_, slot);
+  if (ret < 0) {
     LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
-               << ": " << result.errMsg.c_str();
+               << ": " << strerror(-ret);
   }
-  return result.success;
+  return ret == 0;
 }
 
 bool BootControlAndroid::MarkBootSuccessfulAsync(
     base::Callback<void(bool)> callback) {
-  CommandResult result;
-  auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
-  if (!ret.getStatus().isOk()) {
-    LOG(ERROR) << "Unable to call MarkBootSuccessful: "
-               << ret.getStatus().exceptionMessage().string();
-    return false;
-  }
-  if (!result.success) {
-    LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str();
+  int ret = module_->markBootSuccessful(module_);
+  if (ret < 0) {
+    LOG(ERROR) << "Unable to mark boot successful: " << strerror(-ret);
   }
   return brillo::MessageLoop::current()->PostTask(
-             FROM_HERE, base::Bind(callback, result.success)) !=
+             FROM_HERE, base::Bind(callback, ret == 0)) !=
          brillo::MessageLoop::kTaskIdNull;
 }