From 488f40918be3c19996c5111afa041fb94d952163 Mon Sep 17 00:00:00 2001 From: Sandeep Dhavale Date: Tue, 8 Nov 2022 22:30:30 +0000 Subject: [PATCH] Fastboot AIDL additional changes * Fastbootshim library which wraps HIDL implementation as AIDL * Change default implementation from library to binary Bug: 205760652 Test: Build Change-Id: If92d28d68f1d454b1a65f85e731a78a49b108635 Signed-off-by: Sandeep Dhavale --- fastboot/aidl/default/Android.bp | 10 +- ...roid.hardware.fastboot-service.example.xml | 8 ++ ...dware.fastboot-service.example_recovery.rc | 6 + fastboot/aidl/default/main.cpp | 38 ++++++ fastboot/aidl/fastbootshim/Android.bp | 61 +++++++++ fastboot/aidl/fastbootshim/fastbootshim.cpp | 123 ++++++++++++++++++ .../aidl/fastbootshim/include/fastbootshim.h | 49 +++++++ 7 files changed, 292 insertions(+), 3 deletions(-) create mode 100644 fastboot/aidl/default/android.hardware.fastboot-service.example.xml create mode 100644 fastboot/aidl/default/android.hardware.fastboot-service.example_recovery.rc create mode 100644 fastboot/aidl/default/main.cpp create mode 100644 fastboot/aidl/fastbootshim/Android.bp create mode 100644 fastboot/aidl/fastbootshim/fastbootshim.cpp create mode 100644 fastboot/aidl/fastbootshim/include/fastbootshim.h diff --git a/fastboot/aidl/default/Android.bp b/fastboot/aidl/default/Android.bp index 5cd4542952..0c96b335c8 100644 --- a/fastboot/aidl/default/Android.bp +++ b/fastboot/aidl/default/Android.bp @@ -22,16 +22,20 @@ package { default_applicable_licenses: ["hardware_interfaces_license"], } -cc_library { - name: "android.hardware.fastboot-impl-mock", - recovery: true, +cc_binary { + name: "android.hardware.fastboot-service.example_recovery", + init_rc: ["android.hardware.fastboot-service.example_recovery.rc"], + vintf_fragments: ["android.hardware.fastboot-service.example.xml"], + recovery_available: true, srcs: [ "Fastboot.cpp", + "main.cpp", ], relative_install_path: "hw", shared_libs: [ "libbase", "libbinder_ndk", + "liblog", "libutils", "libcutils", "android.hardware.fastboot-V1-ndk", diff --git a/fastboot/aidl/default/android.hardware.fastboot-service.example.xml b/fastboot/aidl/default/android.hardware.fastboot-service.example.xml new file mode 100644 index 0000000000..9490f98ef2 --- /dev/null +++ b/fastboot/aidl/default/android.hardware.fastboot-service.example.xml @@ -0,0 +1,8 @@ + + + android.hardware.fastboot + 1 + IFastboot/default + + + diff --git a/fastboot/aidl/default/android.hardware.fastboot-service.example_recovery.rc b/fastboot/aidl/default/android.hardware.fastboot-service.example_recovery.rc new file mode 100644 index 0000000000..5d4ee13925 --- /dev/null +++ b/fastboot/aidl/default/android.hardware.fastboot-service.example_recovery.rc @@ -0,0 +1,6 @@ +service vendor.fastboot-default /system/bin/hw/android.hardware.fastboot-service.example_recovery + class hal + seclabel u:r:hal_fastboot_default:s0 + user system + group system + interface aidl android.hardware.fastboot.IFastboot/default diff --git a/fastboot/aidl/default/main.cpp b/fastboot/aidl/default/main.cpp new file mode 100644 index 0000000000..1b1b41d638 --- /dev/null +++ b/fastboot/aidl/default/main.cpp @@ -0,0 +1,38 @@ + +/* + * Copyright (C) 2022 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 +#include +#include +#include "Fastboot.h" + +using aidl::android::hardware::fastboot::Fastboot; +using aidl::android::hardware::fastboot::IFastboot; + +int main(int, char* argv[]) { + android::base::InitLogging(argv, android::base::KernelLogger); + ABinderProcess_setThreadPoolMaxThreadCount(0); + std::shared_ptr service = ndk::SharedRefBase::make(); + + const std::string instance = std::string(IFastboot::descriptor) + "/default"; + auto status = AServiceManager_addService(service->asBinder().get(), instance.c_str()); + CHECK_EQ(status, STATUS_OK) << "Failed to add service " << instance << " " << status; + LOG(INFO) << "IFastboot AIDL service running..."; + + ABinderProcess_joinThreadPool(); + return EXIT_FAILURE; // should not reach +} diff --git a/fastboot/aidl/fastbootshim/Android.bp b/fastboot/aidl/fastbootshim/Android.bp new file mode 100644 index 0000000000..c843c12aef --- /dev/null +++ b/fastboot/aidl/fastbootshim/Android.bp @@ -0,0 +1,61 @@ +// Copyright (C) 2022 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. + +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "hardware_interfaces_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["hardware_interfaces_license"], +} + +cc_defaults { + name: "libfastbootshim_defaults", + target: { + darwin: { + enabled: false, + }, + }, + cflags: [ + "-Wall", + "-Werror", + ], + static_libs: [ + "android.hardware.fastboot-V1-ndk", + "android.hardware.fastboot@1.0", + "android.hardware.fastboot@1.1", + ], + shared_libs: [ + "libbase", + "libbinder_ndk", + "libcutils", + "libhidlbase", + "liblog", + "libutils", + ], +} + +// Shim library that wraps a HIDL Fastboot object into an AIDL Fastboot object. +cc_library_static { + name: "libfastbootshim", + defaults: ["libfastbootshim_defaults"], + recovery_available: true, + srcs: [ + "fastbootshim.cpp", + ], + export_include_dirs: [ + "include", + ], +} diff --git a/fastboot/aidl/fastbootshim/fastbootshim.cpp b/fastboot/aidl/fastbootshim/fastbootshim.cpp new file mode 100644 index 0000000000..4ab67f3088 --- /dev/null +++ b/fastboot/aidl/fastbootshim/fastbootshim.cpp @@ -0,0 +1,123 @@ +/* + * Copyright (C) 2022 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 + +using ::android::sp; +using ::android::hardware::hidl_string; +using ::android::hardware::Void; +using ::android::hardware::fastboot::V1_0::FileSystemType; +using ::android::hardware::fastboot::V1_0::Result; +using ::android::hardware::fastboot::V1_0::Status; + +using ndk::ScopedAStatus; + +namespace aidl { +namespace android { +namespace hardware { +namespace fastboot { +ScopedAStatus ResultToAStatus(Result result) { + switch (result.status) { + case Status::SUCCESS: + return ScopedAStatus::ok(); + case Status::NOT_SUPPORTED: + return ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION); + case Status::INVALID_ARGUMENT: + return ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); + case Status::FAILURE_UNKNOWN: + return ScopedAStatus::fromServiceSpecificErrorWithMessage( + BnFastboot::FAILURE_UNKNOWN, ("Error " + std::string(result.message)).c_str()); + } + return ScopedAStatus::fromServiceSpecificErrorWithMessage( + BnFastboot::FAILURE_UNKNOWN, + ("Unrecognized status value " + toString(result.status)).c_str()); +} +FastbootShim::FastbootShim(const sp& service) : service_(service) {} + +ScopedAStatus FastbootShim::getPartitionType(const std::string& in_partitionName, + FileSystemType* _aidl_return) { + Result out_result = {Status::FAILURE_UNKNOWN, ""}; + if (in_partitionName.empty()) { + return ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, + "Invalid partition name"); + } + const hidl_string partition = in_partitionName; + auto ret = service_->getPartitionType(partition, [&](auto type, auto& result) { + out_result = result; + if (out_result.status != Status::SUCCESS) return; + *_aidl_return = static_cast(type); + }); + return ResultToAStatus(out_result); +} + +ScopedAStatus FastbootShim::doOemCommand(const std::string& in_oemCmd, std::string* _aidl_return) { + Result out_result = {Status::FAILURE_UNKNOWN, ""}; + *_aidl_return = ""; + if (in_oemCmd.empty()) { + return ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, "Invalid command"); + } + const hidl_string oemCmdArgs = in_oemCmd; + auto ret = service_->doOemCommand(oemCmdArgs, [&](auto& result) { + out_result = result; + if (out_result.status != Status::SUCCESS) return; + *_aidl_return = std::string(result.message.c_str()); + }); + return ResultToAStatus(out_result); +} + +ScopedAStatus FastbootShim::getVariant(std::string* _aidl_return) { + Result out_result = {Status::FAILURE_UNKNOWN, ""}; + *_aidl_return = ""; + auto ret = service_->getVariant([&](auto& variant, auto& result) { + out_result = result; + if (out_result.status != Status::SUCCESS) return; + *_aidl_return = std::string(variant.c_str()); + }); + return ResultToAStatus(out_result); +} + +ScopedAStatus FastbootShim::getOffModeChargeState(bool* _aidl_return) { + Result out_result = {Status::FAILURE_UNKNOWN, ""}; + *_aidl_return = false; + auto ret = service_->getOffModeChargeState([&](auto state, auto& result) { + out_result = result; + if (out_result.status != Status::SUCCESS) return; + *_aidl_return = state; + }); + return ResultToAStatus(out_result); +} + +ScopedAStatus FastbootShim::getBatteryVoltageFlashingThreshold(int32_t* _aidl_return) { + Result out_result = {Status::FAILURE_UNKNOWN, ""}; + *_aidl_return = 0; + auto ret = service_->getBatteryVoltageFlashingThreshold([&](auto batteryVoltage, auto& result) { + out_result = result; + if (out_result.status != Status::SUCCESS) return; + *_aidl_return = batteryVoltage; + }); + return ResultToAStatus(out_result); +} + +ScopedAStatus FastbootShim::doOemSpecificErase() { + Result out_result = {Status::FAILURE_UNKNOWN, ""}; + auto ret = service_->doOemSpecificErase([&](auto& result) { out_result = result; }); + return ResultToAStatus(out_result); +} + +} // namespace fastboot +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/fastboot/aidl/fastbootshim/include/fastbootshim.h b/fastboot/aidl/fastbootshim/include/fastbootshim.h new file mode 100644 index 0000000000..410a03e039 --- /dev/null +++ b/fastboot/aidl/fastbootshim/include/fastbootshim.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2022 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. + */ + +#pragma once + +#include +#include + +namespace aidl { +namespace android { +namespace hardware { +namespace fastboot { +// Shim that wraps HIDL IFastboot with AIDL BnFastboot +class FastbootShim : public BnFastboot { + using HidlFastboot = ::android::hardware::fastboot::V1_1::IFastboot; + + public: + explicit FastbootShim(const ::android::sp& service); + ::ndk::ScopedAStatus doOemCommand(const std::string& in_oemCmd, + std::string* _aidl_return) override; + ::ndk::ScopedAStatus doOemSpecificErase() override; + ::ndk::ScopedAStatus getBatteryVoltageFlashingThreshold(int32_t* _aidl_return) override; + ::ndk::ScopedAStatus getOffModeChargeState(bool* _aidl_return) override; + ::ndk::ScopedAStatus getPartitionType( + const std::string& in_partitionName, + ::aidl::android::hardware::fastboot::FileSystemType* _aidl_return) override; + ::ndk::ScopedAStatus getVariant(std::string* _aidl_return) override; + + private: + ::android::sp service_; +}; + +} // namespace fastboot +} // namespace hardware +} // namespace android +} // namespace aidl