From 2534d485d8f0585b3ea4c09091aa29c8bf88474a Mon Sep 17 00:00:00 2001 From: Sandeep Dhavale Date: Tue, 8 Nov 2022 22:30:05 +0000 Subject: [PATCH] Fastbootd: Use Fastboot AIDL with the help of fastbootshim for legacy fastboot HAL Bug: 205760652 Test: build & flash & reboot fastboot Change-Id: I79617a396f536258655bdc28006ac2d0a7ab1912 Signed-off-by: Sandeep Dhavale --- fastboot/Android.bp | 2 ++ fastboot/device/commands.cpp | 45 ++++++++++++------------ fastboot/device/fastboot_device.cpp | 24 ++++++++++++- fastboot/device/fastboot_device.h | 6 ++-- fastboot/device/variables.cpp | 53 +++++++++++------------------ 5 files changed, 69 insertions(+), 61 deletions(-) diff --git a/fastboot/Android.bp b/fastboot/Android.bp index eed49fa7c..765174bb9 100644 --- a/fastboot/Android.bp +++ b/fastboot/Android.bp @@ -168,6 +168,7 @@ cc_binary { "android.hardware.boot-V1-ndk", "libboot_control_client", "android.hardware.fastboot@1.1", + "android.hardware.fastboot-V1-ndk", "android.hardware.health@2.0", "android.hardware.health-V1-ndk", "libasyncio", @@ -192,6 +193,7 @@ cc_binary { "libc++fs", "libhealthhalutils", "libhealthshim", + "libfastbootshim", "libsnapshot_cow", "liblz4", "libsnapshot_nobinder", diff --git a/fastboot/device/commands.cpp b/fastboot/device/commands.cpp index 3799d1fe1..f8befd3b8 100644 --- a/fastboot/device/commands.cpp +++ b/fastboot/device/commands.cpp @@ -57,8 +57,6 @@ static constexpr bool kEnableFetch = false; using android::fs_mgr::MetadataBuilder; using android::hal::CommandResult; using ::android::hardware::hidl_string; -using ::android::hardware::fastboot::V1_0::Result; -using ::android::hardware::fastboot::V1_0::Status; using android::snapshot::SnapshotManager; using MergeStatus = android::hal::BootControlClient::MergeStatus; @@ -203,20 +201,21 @@ bool OemPostWipeData(FastbootDevice* device) { return false; } - Result ret; - auto ret_val = fastboot_hal->doOemSpecificErase([&](Result result) { ret = result; }); - if (!ret_val.isOk()) { - return false; - } - if (ret.status == Status::NOT_SUPPORTED) { - return false; - } else if (ret.status != Status::SUCCESS) { - device->WriteStatus(FastbootResult::FAIL, ret.message); - } else { + auto status = fastboot_hal->doOemSpecificErase(); + if (status.isOk()) { device->WriteStatus(FastbootResult::OKAY, "Erasing succeeded"); + return true; + } + switch (status.getExceptionCode()) { + case EX_UNSUPPORTED_OPERATION: + return false; + case EX_SERVICE_SPECIFIC: + device->WriteStatus(FastbootResult::FAIL, status.getDescription()); + return false; + default: + LOG(ERROR) << "Erase operation failed" << status.getDescription(); + return false; } - - return true; } bool EraseHandler(FastbootDevice* device, const std::vector& args) { @@ -266,18 +265,16 @@ bool OemCmdHandler(FastbootDevice* device, const std::vector& args) if (args[0] == "oem postwipedata userdata") { return device->WriteStatus(FastbootResult::FAIL, "Unable to do oem postwipedata userdata"); } - - Result ret; - auto ret_val = fastboot_hal->doOemCommand(args[0], [&](Result result) { ret = result; }); - if (!ret_val.isOk()) { - return device->WriteStatus(FastbootResult::FAIL, "Unable to do OEM command"); - } - if (ret.status != Status::SUCCESS) { - return device->WriteStatus(FastbootResult::FAIL, ret.message); + std::string message; + auto status = fastboot_hal->doOemCommand(args[0], &message); + if (!status.isOk()) { + LOG(ERROR) << "Unable to do OEM command " << args[0].c_str() << status.getDescription(); + return device->WriteStatus(FastbootResult::FAIL, + "Unable to do OEM command " + status.getDescription()); } - device->WriteInfo(ret.message); - return device->WriteStatus(FastbootResult::OKAY, ret.message); + device->WriteInfo(message); + return device->WriteStatus(FastbootResult::OKAY, message); } bool DownloadHandler(FastbootDevice* device, const std::vector& args) { diff --git a/fastboot/device/fastboot_device.cpp b/fastboot/device/fastboot_device.cpp index 4932e5ceb..5afeb4f29 100644 --- a/fastboot/device/fastboot_device.cpp +++ b/fastboot/device/fastboot_device.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -64,6 +65,27 @@ std::shared_ptr get_health_service() { return nullptr; } +std::shared_ptr get_fastboot_service() { + using aidl::android::hardware::fastboot::IFastboot; + using HidlFastboot = android::hardware::fastboot::V1_1::IFastboot; + using aidl::android::hardware::fastboot::FastbootShim; + auto service_name = IFastboot::descriptor + "/default"s; + ndk::SpAIBinder binder(AServiceManager_getService(service_name.c_str())); + std::shared_ptr fastboot = IFastboot::fromBinder(binder); + if (fastboot != nullptr) { + LOG(INFO) << "Using AIDL fastboot service"; + return fastboot; + } + LOG(INFO) << "Unable to get AIDL fastboot service, trying HIDL..."; + android::sp hidl_fastboot = HidlFastboot::getService(); + if (hidl_fastboot != nullptr) { + LOG(INFO) << "Found and now using fastboot HIDL implementation"; + return ndk::SharedRefBase::make(hidl_fastboot); + } + LOG(WARNING) << "No fastboot implementation is found."; + return nullptr; +} + FastbootDevice::FastbootDevice() : kCommandMap({ {FB_CMD_SET_ACTIVE, SetActiveHandler}, @@ -87,7 +109,7 @@ FastbootDevice::FastbootDevice() }), boot_control_hal_(BootControlClient::WaitForService()), health_hal_(get_health_service()), - fastboot_hal_(IFastboot::getService()), + fastboot_hal_(get_fastboot_service()), active_slot_("") { if (android::base::GetProperty("fastbootd.protocol", "usb") == "tcp") { transport_ = std::make_unique(); diff --git a/fastboot/device/fastboot_device.h b/fastboot/device/fastboot_device.h index 9df8fa58f..fcaf249d3 100644 --- a/fastboot/device/fastboot_device.h +++ b/fastboot/device/fastboot_device.h @@ -23,8 +23,8 @@ #include #include +#include #include -#include #include "commands.h" #include "transport.h" @@ -52,7 +52,7 @@ class FastbootDevice { Transport* get_transport() { return transport_.get(); } BootControlClient* boot_control_hal() const { return boot_control_hal_.get(); } BootControlClient* boot1_1() const; - android::sp fastboot_hal() { + std::shared_ptr fastboot_hal() { return fastboot_hal_; } std::shared_ptr health_hal() { return health_hal_; } @@ -65,7 +65,7 @@ class FastbootDevice { std::unique_ptr transport_; std::unique_ptr boot_control_hal_; std::shared_ptr health_hal_; - android::sp fastboot_hal_; + std::shared_ptr fastboot_hal_; std::vector download_data_; std::string active_slot_; }; diff --git a/fastboot/device/variables.cpp b/fastboot/device/variables.cpp index b6eb2cdd5..5f99656a1 100644 --- a/fastboot/device/variables.cpp +++ b/fastboot/device/variables.cpp @@ -41,9 +41,7 @@ static constexpr bool kEnableFetch = false; #endif using MergeStatus = android::hal::BootControlClient::MergeStatus; -using ::android::hardware::fastboot::V1_0::FileSystemType; -using ::android::hardware::fastboot::V1_0::Result; -using ::android::hardware::fastboot::V1_0::Status; +using aidl::android::hardware::fastboot::FileSystemType; using namespace android::fs_mgr; using namespace std::string_literals; @@ -104,17 +102,16 @@ bool GetVariant(FastbootDevice* device, const std::vector& /* args *message = "Fastboot HAL not found"; return false; } + std::string device_variant = ""; + auto status = fastboot_hal->getVariant(&device_variant); - Result ret; - auto ret_val = fastboot_hal->getVariant([&](std::string device_variant, Result result) { - *message = device_variant; - ret = result; - }); - if (!ret_val.isOk() || ret.status != Status::SUCCESS) { + if (!status.isOk()) { *message = "Unable to get device variant"; + LOG(ERROR) << message->c_str() << status.getDescription(); return false; } + *message = device_variant; return true; } @@ -147,17 +144,14 @@ bool GetBatterySoCOk(FastbootDevice* device, const std::vector& /* return false; } - Result ret; - auto ret_val = fastboot_hal->getBatteryVoltageFlashingThreshold( - [&](int32_t voltage_threshold, Result result) { - *message = battery_voltage >= voltage_threshold ? "yes" : "no"; - ret = result; - }); - - if (!ret_val.isOk() || ret.status != Status::SUCCESS) { + auto voltage_threshold = 0; + auto status = fastboot_hal->getBatteryVoltageFlashingThreshold(&voltage_threshold); + if (!status.isOk()) { *message = "Unable to get battery voltage flashing threshold"; + LOG(ERROR) << message->c_str() << status.getDescription(); return false; } + *message = battery_voltage >= voltage_threshold ? "yes" : "no"; return true; } @@ -169,18 +163,14 @@ bool GetOffModeChargeState(FastbootDevice* device, const std::vectorgetOffModeChargeState([&](bool off_mode_charging_state, Result result) { - *message = off_mode_charging_state ? "1" : "0"; - ret = result; - }); - if (!ret_val.isOk() || (ret.status != Status::SUCCESS)) { + bool off_mode_charging_state = false; + auto status = fastboot_hal->getOffModeChargeState(&off_mode_charging_state); + if (!status.isOk()) { *message = "Unable to get off mode charge state"; + LOG(ERROR) << message->c_str() << status.getDescription(); return false; } - + *message = off_mode_charging_state ? "1" : "0"; return true; } @@ -337,14 +327,11 @@ bool GetPartitionType(FastbootDevice* device, const std::vector& ar } FileSystemType type; - Result ret; - auto ret_val = - fastboot_hal->getPartitionType(args[0], [&](FileSystemType fs_type, Result result) { - type = fs_type; - ret = result; - }); - if (!ret_val.isOk() || (ret.status != Status::SUCCESS)) { + auto status = fastboot_hal->getPartitionType(args[0], &type); + + if (!status.isOk()) { *message = "Unable to retrieve partition type"; + LOG(ERROR) << message->c_str() << status.getDescription(); } else { switch (type) { case FileSystemType::RAW: