1 // Copyright (C) 2019 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "device_info.h" 16 17 #include <android-base/logging.h> 18 #include <fs_mgr.h> 19 #include <fs_mgr_overlayfs.h> 20 #include <libfiemap/image_manager.h> 21 22 namespace android { 23 namespace snapshot { 24 25 #ifdef LIBSNAPSHOT_USE_HAL 26 using android::hal::BootControlClient; 27 using android::hal::BootControlVersion; 28 using android::hal::CommandResult; 29 #endif 30 31 using namespace std::chrono_literals; 32 using namespace std::string_literals; 33 34 #ifdef __ANDROID_RECOVERY__ 35 constexpr bool kIsRecovery = true; 36 #else 37 constexpr bool kIsRecovery = false; 38 #endif 39 GetMetadataDir() const40std::string DeviceInfo::GetMetadataDir() const { 41 return "/metadata/ota"s; 42 } 43 GetSlotSuffix() const44std::string DeviceInfo::GetSlotSuffix() const { 45 return fs_mgr_get_slot_suffix(); 46 } 47 GetOtherSlotSuffix() const48std::string DeviceInfo::GetOtherSlotSuffix() const { 49 return fs_mgr_get_other_slot_suffix(); 50 } 51 GetPartitionOpener() const52const android::fs_mgr::IPartitionOpener& DeviceInfo::GetPartitionOpener() const { 53 return opener_; 54 } 55 GetSuperDevice(uint32_t slot) const56std::string DeviceInfo::GetSuperDevice(uint32_t slot) const { 57 return fs_mgr_get_super_partition_name(slot); 58 } 59 IsOverlayfsSetup() const60bool DeviceInfo::IsOverlayfsSetup() const { 61 return fs_mgr_overlayfs_is_setup(); 62 } 63 64 #ifdef LIBSNAPSHOT_USE_HAL EnsureBootHal()65bool DeviceInfo::EnsureBootHal() { 66 if (!boot_control_) { 67 auto hal = BootControlClient::WaitForService(); 68 if (!hal) { 69 LOG(ERROR) << "Could not find IBootControl HAL"; 70 return false; 71 } 72 if (hal->GetVersion() < BootControlVersion::BOOTCTL_V1_1) { 73 LOG(ERROR) << "Could not find IBootControl 1.1 HAL"; 74 return false; 75 } 76 boot_control_ = std::move(hal); 77 } 78 return true; 79 } 80 #endif 81 SetBootControlMergeStatus(MergeStatus status)82bool DeviceInfo::SetBootControlMergeStatus([[maybe_unused]] MergeStatus status) { 83 #ifdef LIBSNAPSHOT_USE_HAL 84 if (!EnsureBootHal()) { 85 return false; 86 } 87 const auto ret = boot_control_->SetSnapshotMergeStatus(status); 88 if (!ret.IsOk()) { 89 LOG(ERROR) << "Unable to set the snapshot merge status " << ret.errMsg; 90 return false; 91 } 92 return true; 93 #else 94 LOG(ERROR) << "HAL support not enabled."; 95 return false; 96 #endif 97 } 98 IsRecovery() const99bool DeviceInfo::IsRecovery() const { 100 return kIsRecovery; 101 } 102 IsFirstStageInit() const103bool DeviceInfo::IsFirstStageInit() const { 104 return first_stage_init_; 105 } 106 SetSlotAsUnbootable(unsigned int slot)107bool DeviceInfo::SetSlotAsUnbootable([[maybe_unused]] unsigned int slot) { 108 #ifdef LIBSNAPSHOT_USE_HAL 109 if (!EnsureBootHal()) { 110 return false; 111 } 112 113 CommandResult result = boot_control_->MarkSlotUnbootable(slot); 114 if (!result.success) { 115 LOG(ERROR) << "Error setting slot " << slot << " unbootable: " << result.errMsg; 116 return false; 117 } 118 return true; 119 #else 120 LOG(ERROR) << "HAL support not enabled."; 121 return false; 122 #endif 123 } 124 OpenImageManager() const125std::unique_ptr<android::fiemap::IImageManager> DeviceInfo::OpenImageManager() const { 126 return IDeviceInfo::OpenImageManager("ota"); 127 } 128 OpenImageManager(const std::string & gsid_dir) const129std::unique_ptr<android::fiemap::IImageManager> ISnapshotManager::IDeviceInfo::OpenImageManager( 130 const std::string& gsid_dir) const { 131 if (IsRecovery() || IsFirstStageInit()) { 132 android::fiemap::ImageManager::DeviceInfo device_info = { 133 .is_recovery = {IsRecovery()}, 134 }; 135 return android::fiemap::ImageManager::Open(gsid_dir, device_info); 136 } else { 137 // For now, use a preset timeout. 138 return android::fiemap::IImageManager::Open(gsid_dir, 15000ms); 139 } 140 } 141 GetDeviceMapper()142android::dm::IDeviceMapper& DeviceInfo::GetDeviceMapper() { 143 return android::dm::DeviceMapper::Instance(); 144 } 145 146 } // namespace snapshot 147 } // namespace android 148