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 #include "scratch_super.h" 17 18 #include <android-base/logging.h> 19 #include <fs_mgr.h> 20 #include <fs_mgr_overlayfs.h> 21 #include <libfiemap/image_manager.h> 22 23 namespace android { 24 namespace snapshot { 25 26 #ifdef LIBSNAPSHOT_USE_HAL 27 using android::hal::BootControlClient; 28 using android::hal::BootControlVersion; 29 using android::hal::CommandResult; 30 #endif 31 32 using namespace std::chrono_literals; 33 using namespace std::string_literals; 34 35 #ifdef __ANDROID_RECOVERY__ 36 constexpr bool kIsRecovery = true; 37 #else 38 constexpr bool kIsRecovery = false; 39 #endif 40 DeviceInfo()41DeviceInfo::DeviceInfo() { 42 std::string scratch_device = android::snapshot::GetScratchOtaMetadataPartition(); 43 if (!scratch_device.empty()) { 44 std::string scratch_metadata = 45 android::snapshot::MapScratchOtaMetadataPartition(scratch_device); 46 if (!scratch_metadata.empty()) { 47 SetMetadataDir(scratch_metadata); 48 SetTempMetadata(); 49 } 50 } 51 } 52 GetMetadataDir() const53std::string DeviceInfo::GetMetadataDir() const { 54 return metadata_dir_; 55 } 56 SetMetadataDir(const std::string & value)57void DeviceInfo::SetMetadataDir(const std::string& value) { 58 metadata_dir_ = value; 59 } 60 GetSlotSuffix() const61std::string DeviceInfo::GetSlotSuffix() const { 62 return fs_mgr_get_slot_suffix(); 63 } 64 GetOtherSlotSuffix() const65std::string DeviceInfo::GetOtherSlotSuffix() const { 66 return fs_mgr_get_other_slot_suffix(); 67 } 68 GetPartitionOpener() const69const android::fs_mgr::IPartitionOpener& DeviceInfo::GetPartitionOpener() const { 70 return opener_; 71 } 72 GetSuperDevice(uint32_t slot) const73std::string DeviceInfo::GetSuperDevice(uint32_t slot) const { 74 return fs_mgr_get_super_partition_name(slot); 75 } 76 IsOverlayfsSetup() const77bool DeviceInfo::IsOverlayfsSetup() const { 78 return fs_mgr_overlayfs_is_setup(); 79 } 80 81 #ifdef LIBSNAPSHOT_USE_HAL EnsureBootHal()82bool DeviceInfo::EnsureBootHal() { 83 if (!boot_control_) { 84 auto hal = BootControlClient::WaitForService(); 85 if (!hal) { 86 LOG(ERROR) << "Could not find IBootControl HAL"; 87 return false; 88 } 89 if (hal->GetVersion() < BootControlVersion::BOOTCTL_V1_1) { 90 LOG(ERROR) << "Could not find IBootControl 1.1 HAL"; 91 return false; 92 } 93 boot_control_ = std::move(hal); 94 } 95 return true; 96 } 97 #endif 98 SetBootControlMergeStatus(MergeStatus status)99bool DeviceInfo::SetBootControlMergeStatus([[maybe_unused]] MergeStatus status) { 100 #ifdef LIBSNAPSHOT_USE_HAL 101 if (!EnsureBootHal()) { 102 return false; 103 } 104 const auto ret = boot_control_->SetSnapshotMergeStatus(status); 105 if (!ret.IsOk()) { 106 LOG(ERROR) << "Unable to set the snapshot merge status " << ret.errMsg; 107 return false; 108 } 109 return true; 110 #else 111 LOG(ERROR) << "HAL support not enabled."; 112 return false; 113 #endif 114 } 115 IsRecovery() const116bool DeviceInfo::IsRecovery() const { 117 return kIsRecovery; 118 } 119 IsFirstStageInit() const120bool DeviceInfo::IsFirstStageInit() const { 121 return first_stage_init_; 122 } 123 SetActiveBootSlot(unsigned int slot)124bool DeviceInfo::SetActiveBootSlot([[maybe_unused]] unsigned int slot) { 125 #ifdef LIBSNAPSHOT_USE_HAL 126 if (!EnsureBootHal()) { 127 return false; 128 } 129 130 CommandResult result = boot_control_->SetActiveBootSlot(slot); 131 if (!result.success) { 132 LOG(ERROR) << "Error setting slot " << slot << " active: " << result.errMsg; 133 return false; 134 } 135 return true; 136 #else 137 LOG(ERROR) << "HAL support not enabled."; 138 return false; 139 #endif 140 } 141 SetSlotAsUnbootable(unsigned int slot)142bool DeviceInfo::SetSlotAsUnbootable([[maybe_unused]] unsigned int slot) { 143 #ifdef LIBSNAPSHOT_USE_HAL 144 if (!EnsureBootHal()) { 145 return false; 146 } 147 148 CommandResult result = boot_control_->MarkSlotUnbootable(slot); 149 if (!result.success) { 150 LOG(ERROR) << "Error setting slot " << slot << " unbootable: " << result.errMsg; 151 return false; 152 } 153 return true; 154 #else 155 LOG(ERROR) << "HAL support not enabled."; 156 return false; 157 #endif 158 } 159 OpenImageManager() const160std::unique_ptr<android::fiemap::IImageManager> DeviceInfo::OpenImageManager() const { 161 return IDeviceInfo::OpenImageManager("ota"); 162 } 163 OpenImageManager(const std::string & gsid_dir) const164std::unique_ptr<android::fiemap::IImageManager> ISnapshotManager::IDeviceInfo::OpenImageManager( 165 const std::string& gsid_dir) const { 166 if (IsRecovery() || IsFirstStageInit()) { 167 android::fiemap::ImageManager::DeviceInfo device_info = { 168 .is_recovery = {IsRecovery()}, 169 }; 170 return android::fiemap::ImageManager::Open(gsid_dir, device_info); 171 } else { 172 // For now, use a preset timeout. 173 return android::fiemap::IImageManager::Open(gsid_dir, 15000ms); 174 } 175 } 176 GetDeviceMapper()177android::dm::IDeviceMapper& DeviceInfo::GetDeviceMapper() { 178 return android::dm::DeviceMapper::Instance(); 179 } 180 181 } // namespace snapshot 182 } // namespace android 183