| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2018 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
|  | 16 |  | 
|  | 17 | #include "update_engine/dynamic_partition_control_android.h" | 
|  | 18 |  | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 19 | #include <chrono>  // NOLINT(build/c++11) - using libsnapshot / liblp API | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 20 | #include <map> | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 21 | #include <memory> | 
|  | 22 | #include <set> | 
|  | 23 | #include <string> | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 24 | #include <vector> | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | #include <android-base/properties.h> | 
|  | 27 | #include <android-base/strings.h> | 
|  | 28 | #include <base/files/file_util.h> | 
|  | 29 | #include <base/logging.h> | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 30 | #include <base/strings/string_util.h> | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 31 | #include <bootloader_message/bootloader_message.h> | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 32 | #include <fs_mgr.h> | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 33 | #include <fs_mgr_dm_linear.h> | 
| Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 34 | #include <fs_mgr_overlayfs.h> | 
|  | 35 | #include <libdm/dm.h> | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 36 | #include <libsnapshot/snapshot.h> | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 37 |  | 
|  | 38 | #include "update_engine/common/boot_control_interface.h" | 
|  | 39 | #include "update_engine/common/utils.h" | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 40 | #include "update_engine/dynamic_partition_utils.h" | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | using android::base::GetBoolProperty; | 
|  | 43 | using android::base::Join; | 
|  | 44 | using android::dm::DeviceMapper; | 
|  | 45 | using android::dm::DmDeviceState; | 
|  | 46 | using android::fs_mgr::CreateLogicalPartition; | 
| David Anderson | bb90dfb | 2019-08-13 14:14:56 -0700 | [diff] [blame] | 47 | using android::fs_mgr::CreateLogicalPartitionParams; | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 48 | using android::fs_mgr::DestroyLogicalPartition; | 
|  | 49 | using android::fs_mgr::MetadataBuilder; | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 50 | using android::fs_mgr::Partition; | 
| Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 51 | using android::fs_mgr::PartitionOpener; | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 52 | using android::fs_mgr::SlotSuffixForSlotNumber; | 
| Yifan Hong | 0850bca | 2020-01-16 15:14:07 -0800 | [diff] [blame^] | 53 | using android::snapshot::Return; | 
| Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 54 | using android::snapshot::SnapshotManager; | 
| Alessio Balsini | 2a3b4a2 | 2019-11-25 16:46:51 +0000 | [diff] [blame] | 55 | using android::snapshot::SourceCopyOperationIsClone; | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 56 |  | 
|  | 57 | namespace chromeos_update_engine { | 
|  | 58 |  | 
| Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 59 | constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions"; | 
|  | 60 | constexpr char kRetrfoitDynamicPartitions[] = | 
|  | 61 | "ro.boot.dynamic_partitions_retrofit"; | 
| Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 62 | constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled"; | 
|  | 63 | constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit"; | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 64 | // Map timeout for dynamic partitions. | 
|  | 65 | constexpr std::chrono::milliseconds kMapTimeout{1000}; | 
|  | 66 | // Map timeout for dynamic partitions with snapshots. Since several devices | 
|  | 67 | // needs to be mapped, this timeout is longer than |kMapTimeout|. | 
|  | 68 | constexpr std::chrono::milliseconds kMapSnapshotTimeout{5000}; | 
|  | 69 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 70 | DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() { | 
| Yifan Hong | 02513dc | 2019-10-30 11:23:04 -0700 | [diff] [blame] | 71 | CleanupInternal(); | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
| Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 74 | static FeatureFlag GetFeatureFlag(const char* enable_prop, | 
|  | 75 | const char* retrofit_prop) { | 
|  | 76 | bool retrofit = GetBoolProperty(retrofit_prop, false); | 
|  | 77 | bool enabled = GetBoolProperty(enable_prop, false); | 
|  | 78 | if (retrofit && !enabled) { | 
|  | 79 | LOG(ERROR) << retrofit_prop << " is true but " << enable_prop | 
|  | 80 | << " is not. These sysprops are inconsistent. Assume that " | 
|  | 81 | << enable_prop << " is true from now on."; | 
|  | 82 | } | 
|  | 83 | if (retrofit) { | 
|  | 84 | return FeatureFlag(FeatureFlag::Value::RETROFIT); | 
|  | 85 | } | 
|  | 86 | if (enabled) { | 
|  | 87 | return FeatureFlag(FeatureFlag::Value::LAUNCH); | 
|  | 88 | } | 
|  | 89 | return FeatureFlag(FeatureFlag::Value::NONE); | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 92 | DynamicPartitionControlAndroid::DynamicPartitionControlAndroid() | 
|  | 93 | : dynamic_partitions_( | 
|  | 94 | GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions)), | 
|  | 95 | virtual_ab_(GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit)) { | 
|  | 96 | if (GetVirtualAbFeatureFlag().IsEnabled()) { | 
| Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 97 | snapshot_ = SnapshotManager::New(); | 
| Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 98 | CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager."; | 
|  | 99 | } | 
|  | 100 | } | 
|  | 101 |  | 
| Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 102 | FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() { | 
| Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 103 | return dynamic_partitions_; | 
| Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 106 | FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() { | 
| Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 107 | return virtual_ab_; | 
| Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
| Alessio Balsini | 14980e2 | 2019-11-26 11:46:06 +0000 | [diff] [blame] | 110 | bool DynamicPartitionControlAndroid::ShouldSkipOperation( | 
| Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 111 | const std::string& partition_name, const InstallOperation& operation) { | 
| Alessio Balsini | 2a3b4a2 | 2019-11-25 16:46:51 +0000 | [diff] [blame] | 112 | switch (operation.type()) { | 
|  | 113 | case InstallOperation::SOURCE_COPY: | 
|  | 114 | return target_supports_snapshot_ && | 
|  | 115 | GetVirtualAbFeatureFlag().IsEnabled() && | 
| Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 116 | mapped_devices_.count(partition_name + | 
|  | 117 | SlotSuffixForSlotNumber(target_slot_)) > 0 && | 
| Alessio Balsini | 2a3b4a2 | 2019-11-25 16:46:51 +0000 | [diff] [blame] | 118 | SourceCopyOperationIsClone(operation); | 
|  | 119 | break; | 
|  | 120 | default: | 
|  | 121 | break; | 
|  | 122 | } | 
| Alessio Balsini | 14980e2 | 2019-11-26 11:46:06 +0000 | [diff] [blame] | 123 | return false; | 
|  | 124 | } | 
|  | 125 |  | 
| Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 126 | bool DynamicPartitionControlAndroid::MapPartitionInternal( | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 127 | const std::string& super_device, | 
|  | 128 | const std::string& target_partition_name, | 
|  | 129 | uint32_t slot, | 
| Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 130 | bool force_writable, | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 131 | std::string* path) { | 
| David Anderson | bb90dfb | 2019-08-13 14:14:56 -0700 | [diff] [blame] | 132 | CreateLogicalPartitionParams params = { | 
|  | 133 | .block_device = super_device, | 
|  | 134 | .metadata_slot = slot, | 
|  | 135 | .partition_name = target_partition_name, | 
|  | 136 | .force_writable = force_writable, | 
| David Anderson | bb90dfb | 2019-08-13 14:14:56 -0700 | [diff] [blame] | 137 | }; | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 138 | bool success = false; | 
| Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 139 | if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ && | 
|  | 140 | force_writable) { | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 141 | // Only target partitions are mapped with force_writable. On Virtual | 
|  | 142 | // A/B devices, target partitions may overlap with source partitions, so | 
|  | 143 | // they must be mapped with snapshot. | 
|  | 144 | params.timeout_ms = kMapSnapshotTimeout; | 
|  | 145 | success = snapshot_->MapUpdateSnapshot(params, path); | 
|  | 146 | } else { | 
|  | 147 | params.timeout_ms = kMapTimeout; | 
|  | 148 | success = CreateLogicalPartition(params, path); | 
|  | 149 | } | 
| David Anderson | bb90dfb | 2019-08-13 14:14:56 -0700 | [diff] [blame] | 150 |  | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 151 | if (!success) { | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 152 | LOG(ERROR) << "Cannot map " << target_partition_name << " in " | 
|  | 153 | << super_device << " on device mapper."; | 
|  | 154 | return false; | 
|  | 155 | } | 
|  | 156 | LOG(INFO) << "Succesfully mapped " << target_partition_name | 
| Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 157 | << " to device mapper (force_writable = " << force_writable | 
|  | 158 | << "); device path at " << *path; | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 159 | mapped_devices_.insert(target_partition_name); | 
|  | 160 | return true; | 
|  | 161 | } | 
|  | 162 |  | 
| Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 163 | bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper( | 
|  | 164 | const std::string& super_device, | 
|  | 165 | const std::string& target_partition_name, | 
|  | 166 | uint32_t slot, | 
|  | 167 | bool force_writable, | 
|  | 168 | std::string* path) { | 
|  | 169 | DmDeviceState state = GetState(target_partition_name); | 
|  | 170 | if (state == DmDeviceState::ACTIVE) { | 
|  | 171 | if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) { | 
|  | 172 | if (GetDmDevicePathByName(target_partition_name, path)) { | 
|  | 173 | LOG(INFO) << target_partition_name | 
|  | 174 | << " is mapped on device mapper: " << *path; | 
|  | 175 | return true; | 
|  | 176 | } | 
|  | 177 | LOG(ERROR) << target_partition_name << " is mapped but path is unknown."; | 
|  | 178 | return false; | 
|  | 179 | } | 
|  | 180 | // If target_partition_name is not in mapped_devices_ but state is ACTIVE, | 
|  | 181 | // the device might be mapped incorrectly before. Attempt to unmap it. | 
|  | 182 | // Note that for source partitions, if GetState() == ACTIVE, callers (e.g. | 
|  | 183 | // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but | 
|  | 184 | // should directly call GetDmDevicePathByName. | 
| David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 185 | if (!UnmapPartitionOnDeviceMapper(target_partition_name)) { | 
| Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 186 | LOG(ERROR) << target_partition_name | 
|  | 187 | << " is mapped before the update, and it cannot be unmapped."; | 
|  | 188 | return false; | 
|  | 189 | } | 
|  | 190 | state = GetState(target_partition_name); | 
|  | 191 | if (state != DmDeviceState::INVALID) { | 
|  | 192 | LOG(ERROR) << target_partition_name << " is unmapped but state is " | 
|  | 193 | << static_cast<std::underlying_type_t<DmDeviceState>>(state); | 
|  | 194 | return false; | 
|  | 195 | } | 
|  | 196 | } | 
|  | 197 | if (state == DmDeviceState::INVALID) { | 
|  | 198 | return MapPartitionInternal( | 
|  | 199 | super_device, target_partition_name, slot, force_writable, path); | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | LOG(ERROR) << target_partition_name | 
|  | 203 | << " is mapped on device mapper but state is unknown: " | 
|  | 204 | << static_cast<std::underlying_type_t<DmDeviceState>>(state); | 
|  | 205 | return false; | 
|  | 206 | } | 
|  | 207 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 208 | bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper( | 
| David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 209 | const std::string& target_partition_name) { | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 210 | if (DeviceMapper::Instance().GetState(target_partition_name) != | 
|  | 211 | DmDeviceState::INVALID) { | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 212 | // Partitions at target slot on non-Virtual A/B devices are mapped as | 
|  | 213 | // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for | 
|  | 214 | // preopt apps as dm-linear. | 
|  | 215 | // Call DestroyLogicalPartition to handle these cases. | 
|  | 216 | bool success = DestroyLogicalPartition(target_partition_name); | 
|  | 217 |  | 
|  | 218 | // On a Virtual A/B device, |target_partition_name| may be a leftover from | 
|  | 219 | // a paused update. Clean up any underlying devices. | 
|  | 220 | if (GetVirtualAbFeatureFlag().IsEnabled()) { | 
|  | 221 | success &= snapshot_->UnmapUpdateSnapshot(target_partition_name); | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | if (!success) { | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 225 | LOG(ERROR) << "Cannot unmap " << target_partition_name | 
|  | 226 | << " from device mapper."; | 
|  | 227 | return false; | 
|  | 228 | } | 
|  | 229 | LOG(INFO) << "Successfully unmapped " << target_partition_name | 
|  | 230 | << " from device mapper."; | 
|  | 231 | } | 
|  | 232 | mapped_devices_.erase(target_partition_name); | 
|  | 233 | return true; | 
|  | 234 | } | 
|  | 235 |  | 
| Yifan Hong | 02513dc | 2019-10-30 11:23:04 -0700 | [diff] [blame] | 236 | void DynamicPartitionControlAndroid::CleanupInternal() { | 
| Yifan Hong | 2c62c13 | 2019-10-24 14:53:40 -0700 | [diff] [blame] | 237 | metadata_device_.reset(); | 
| Tao Bao | 8c4d008 | 2019-08-08 08:56:16 -0700 | [diff] [blame] | 238 | if (mapped_devices_.empty()) { | 
|  | 239 | return; | 
|  | 240 | } | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 241 | // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence | 
|  | 242 | // a copy is needed for the loop. | 
|  | 243 | std::set<std::string> mapped = mapped_devices_; | 
|  | 244 | LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper"; | 
|  | 245 | for (const auto& partition_name : mapped) { | 
| David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 246 | ignore_result(UnmapPartitionOnDeviceMapper(partition_name)); | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 247 | } | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | void DynamicPartitionControlAndroid::Cleanup() { | 
| Yifan Hong | 02513dc | 2019-10-30 11:23:04 -0700 | [diff] [blame] | 251 | CleanupInternal(); | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 252 | } | 
|  | 253 |  | 
|  | 254 | bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) { | 
|  | 255 | return base::PathExists(base::FilePath(path)); | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState( | 
|  | 259 | const std::string& name) { | 
|  | 260 | return DeviceMapper::Instance().GetState(name); | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | bool DynamicPartitionControlAndroid::GetDmDevicePathByName( | 
|  | 264 | const std::string& name, std::string* path) { | 
|  | 265 | return DeviceMapper::Instance().GetDmDevicePathByName(name, path); | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | std::unique_ptr<MetadataBuilder> | 
|  | 269 | DynamicPartitionControlAndroid::LoadMetadataBuilder( | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 270 | const std::string& super_device, uint32_t source_slot) { | 
|  | 271 | return LoadMetadataBuilder( | 
|  | 272 | super_device, source_slot, BootControlInterface::kInvalidSlot); | 
|  | 273 | } | 
|  | 274 |  | 
|  | 275 | std::unique_ptr<MetadataBuilder> | 
|  | 276 | DynamicPartitionControlAndroid::LoadMetadataBuilder( | 
| Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 277 | const std::string& super_device, | 
|  | 278 | uint32_t source_slot, | 
|  | 279 | uint32_t target_slot) { | 
| Yifan Hong | 30fa5f5 | 2019-08-05 16:39:59 -0700 | [diff] [blame] | 280 | std::unique_ptr<MetadataBuilder> builder; | 
|  | 281 | if (target_slot == BootControlInterface::kInvalidSlot) { | 
|  | 282 | builder = | 
|  | 283 | MetadataBuilder::New(PartitionOpener(), super_device, source_slot); | 
|  | 284 | } else { | 
| Yifan Hong | 50a56c6 | 2019-10-14 19:35:05 -0700 | [diff] [blame] | 285 | bool always_keep_source_slot = !target_supports_snapshot_; | 
|  | 286 | builder = MetadataBuilder::NewForUpdate(PartitionOpener(), | 
|  | 287 | super_device, | 
|  | 288 | source_slot, | 
|  | 289 | target_slot, | 
|  | 290 | always_keep_source_slot); | 
| Yifan Hong | 30fa5f5 | 2019-08-05 16:39:59 -0700 | [diff] [blame] | 291 | } | 
| Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 292 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 293 | if (builder == nullptr) { | 
|  | 294 | LOG(WARNING) << "No metadata slot " | 
|  | 295 | << BootControlInterface::SlotName(source_slot) << " in " | 
|  | 296 | << super_device; | 
| Yifan Hong | f48a005 | 2018-10-29 16:30:43 -0700 | [diff] [blame] | 297 | return nullptr; | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 298 | } | 
|  | 299 | LOG(INFO) << "Loaded metadata from slot " | 
|  | 300 | << BootControlInterface::SlotName(source_slot) << " in " | 
|  | 301 | << super_device; | 
|  | 302 | return builder; | 
|  | 303 | } | 
|  | 304 |  | 
|  | 305 | bool DynamicPartitionControlAndroid::StoreMetadata( | 
|  | 306 | const std::string& super_device, | 
|  | 307 | MetadataBuilder* builder, | 
|  | 308 | uint32_t target_slot) { | 
|  | 309 | auto metadata = builder->Export(); | 
|  | 310 | if (metadata == nullptr) { | 
|  | 311 | LOG(ERROR) << "Cannot export metadata to slot " | 
|  | 312 | << BootControlInterface::SlotName(target_slot) << " in " | 
|  | 313 | << super_device; | 
|  | 314 | return false; | 
|  | 315 | } | 
|  | 316 |  | 
| Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 317 | if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) { | 
| Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 318 | if (!FlashPartitionTable(super_device, *metadata)) { | 
|  | 319 | LOG(ERROR) << "Cannot write metadata to " << super_device; | 
|  | 320 | return false; | 
|  | 321 | } | 
|  | 322 | LOG(INFO) << "Written metadata to " << super_device; | 
|  | 323 | } else { | 
|  | 324 | if (!UpdatePartitionTable(super_device, *metadata, target_slot)) { | 
|  | 325 | LOG(ERROR) << "Cannot write metadata to slot " | 
|  | 326 | << BootControlInterface::SlotName(target_slot) << " in " | 
|  | 327 | << super_device; | 
|  | 328 | return false; | 
|  | 329 | } | 
|  | 330 | LOG(INFO) << "Copied metadata to slot " | 
|  | 331 | << BootControlInterface::SlotName(target_slot) << " in " | 
|  | 332 | << super_device; | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 333 | } | 
|  | 334 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 335 | return true; | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) { | 
|  | 339 | // We can't use fs_mgr to look up |partition_name| because fstab | 
|  | 340 | // doesn't list every slot partition (it uses the slotselect option | 
|  | 341 | // to mask the suffix). | 
|  | 342 | // | 
|  | 343 | // We can however assume that there's an entry for the /misc mount | 
|  | 344 | // point and use that to get the device file for the misc | 
|  | 345 | // partition. This helps us locate the disk that |partition_name| | 
|  | 346 | // resides on. From there we'll assume that a by-name scheme is used | 
|  | 347 | // so we can just replace the trailing "misc" by the given | 
|  | 348 | // |partition_name| and suffix corresponding to |slot|, e.g. | 
|  | 349 | // | 
|  | 350 | //   /dev/block/platform/soc.0/7824900.sdhci/by-name/misc -> | 
|  | 351 | //   /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a | 
|  | 352 | // | 
|  | 353 | // If needed, it's possible to relax the by-name assumption in the | 
|  | 354 | // future by trawling /sys/block looking for the appropriate sibling | 
|  | 355 | // of misc and then finding an entry in /dev matching the sysfs | 
|  | 356 | // entry. | 
|  | 357 |  | 
|  | 358 | std::string err, misc_device = get_bootloader_message_blk_device(&err); | 
|  | 359 | if (misc_device.empty()) { | 
|  | 360 | LOG(ERROR) << "Unable to get misc block device: " << err; | 
|  | 361 | return false; | 
|  | 362 | } | 
|  | 363 |  | 
|  | 364 | if (!utils::IsSymlink(misc_device.c_str())) { | 
|  | 365 | LOG(ERROR) << "Device file " << misc_device << " for /misc " | 
|  | 366 | << "is not a symlink."; | 
|  | 367 | return false; | 
|  | 368 | } | 
|  | 369 | *out = base::FilePath(misc_device).DirName().value(); | 
|  | 370 | return true; | 
|  | 371 | } | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 372 |  | 
|  | 373 | bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate( | 
|  | 374 | uint32_t source_slot, | 
|  | 375 | uint32_t target_slot, | 
| Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 376 | const DeltaArchiveManifest& manifest, | 
| Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 377 | bool update, | 
|  | 378 | uint64_t* required_size) { | 
| Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 379 | source_slot_ = source_slot; | 
|  | 380 | target_slot_ = target_slot; | 
| Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 381 | if (required_size != nullptr) { | 
|  | 382 | *required_size = 0; | 
|  | 383 | } | 
| Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 384 |  | 
| Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 385 | if (fs_mgr_overlayfs_is_setup()) { | 
|  | 386 | // Non DAP devices can use overlayfs as well. | 
|  | 387 | LOG(WARNING) | 
|  | 388 | << "overlayfs overrides are active and can interfere with our " | 
|  | 389 | "resources.\n" | 
|  | 390 | << "run adb enable-verity to deactivate if required and try again."; | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) { | 
|  | 394 | return true; | 
|  | 395 | } | 
|  | 396 |  | 
|  | 397 | if (target_slot == source_slot) { | 
|  | 398 | LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot."; | 
|  | 399 | return false; | 
|  | 400 | } | 
|  | 401 |  | 
|  | 402 | // Although the current build supports dynamic partitions, the given payload | 
|  | 403 | // doesn't use it for target partitions. This could happen when applying a | 
|  | 404 | // retrofit update. Skip updating the partition metadata for the target slot. | 
|  | 405 | is_target_dynamic_ = !manifest.dynamic_partition_metadata().groups().empty(); | 
|  | 406 | if (!is_target_dynamic_) { | 
|  | 407 | return true; | 
|  | 408 | } | 
|  | 409 |  | 
| Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 410 | target_supports_snapshot_ = | 
|  | 411 | manifest.dynamic_partition_metadata().snapshot_enabled(); | 
|  | 412 |  | 
| Yifan Hong | 2c62c13 | 2019-10-24 14:53:40 -0700 | [diff] [blame] | 413 | if (GetVirtualAbFeatureFlag().IsEnabled()) { | 
|  | 414 | metadata_device_ = snapshot_->EnsureMetadataMounted(); | 
|  | 415 | TEST_AND_RETURN_FALSE(metadata_device_ != nullptr); | 
|  | 416 | } | 
|  | 417 |  | 
| Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 418 | if (!update) | 
|  | 419 | return true; | 
|  | 420 |  | 
| Yifan Hong | 6d88856 | 2019-10-01 13:00:31 -0700 | [diff] [blame] | 421 | if (GetVirtualAbFeatureFlag().IsEnabled()) { | 
|  | 422 | // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be | 
|  | 423 | // called before calling UnmapUpdateSnapshot. | 
|  | 424 | // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate() | 
|  | 425 | //   calls BeginUpdate() which resets update state | 
|  | 426 | // - If !target_supports_snapshot_, explicitly CancelUpdate(). | 
|  | 427 | if (target_supports_snapshot_) { | 
|  | 428 | return PrepareSnapshotPartitionsForUpdate( | 
| Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 429 | source_slot, target_slot, manifest, required_size); | 
| Yifan Hong | 6d88856 | 2019-10-01 13:00:31 -0700 | [diff] [blame] | 430 | } | 
|  | 431 | if (!snapshot_->CancelUpdate()) { | 
|  | 432 | LOG(ERROR) << "Cannot cancel previous update."; | 
|  | 433 | return false; | 
|  | 434 | } | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 435 | } | 
|  | 436 | return PrepareDynamicPartitionsForUpdate(source_slot, target_slot, manifest); | 
|  | 437 | } | 
|  | 438 |  | 
|  | 439 | bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate( | 
|  | 440 | uint32_t source_slot, | 
|  | 441 | uint32_t target_slot, | 
|  | 442 | const DeltaArchiveManifest& manifest) { | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 443 | const std::string target_suffix = SlotSuffixForSlotNumber(target_slot); | 
|  | 444 |  | 
|  | 445 | // Unmap all the target dynamic partitions because they would become | 
|  | 446 | // inconsistent with the new metadata. | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 447 | for (const auto& group : manifest.dynamic_partition_metadata().groups()) { | 
|  | 448 | for (const auto& partition_name : group.partition_names()) { | 
|  | 449 | if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) { | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 450 | return false; | 
|  | 451 | } | 
|  | 452 | } | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | std::string device_dir_str; | 
|  | 456 | if (!GetDeviceDir(&device_dir_str)) { | 
|  | 457 | return false; | 
|  | 458 | } | 
|  | 459 | base::FilePath device_dir(device_dir_str); | 
|  | 460 | auto source_device = | 
| Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 461 | device_dir.Append(GetSuperPartitionName(source_slot)).value(); | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 462 |  | 
|  | 463 | auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot); | 
|  | 464 | if (builder == nullptr) { | 
|  | 465 | LOG(ERROR) << "No metadata at " | 
|  | 466 | << BootControlInterface::SlotName(source_slot); | 
|  | 467 | return false; | 
|  | 468 | } | 
|  | 469 |  | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 470 | if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) { | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 471 | return false; | 
|  | 472 | } | 
|  | 473 |  | 
|  | 474 | auto target_device = | 
| Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 475 | device_dir.Append(GetSuperPartitionName(target_slot)).value(); | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 476 | return StoreMetadata(target_device, builder.get(), target_slot); | 
|  | 477 | } | 
|  | 478 |  | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 479 | bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate( | 
|  | 480 | uint32_t source_slot, | 
|  | 481 | uint32_t target_slot, | 
| Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 482 | const DeltaArchiveManifest& manifest, | 
|  | 483 | uint64_t* required_size) { | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 484 | if (!snapshot_->BeginUpdate()) { | 
|  | 485 | LOG(ERROR) << "Cannot begin new update."; | 
|  | 486 | return false; | 
|  | 487 | } | 
| Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 488 | auto ret = snapshot_->CreateUpdateSnapshots(manifest); | 
|  | 489 | if (!ret) { | 
|  | 490 | LOG(ERROR) << "Cannot create update snapshots: " << ret.string(); | 
|  | 491 | if (required_size != nullptr && | 
| Yifan Hong | 0850bca | 2020-01-16 15:14:07 -0800 | [diff] [blame^] | 492 | ret.error_code() == Return::ErrorCode::NO_SPACE) { | 
| Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 493 | *required_size = ret.required_size(); | 
|  | 494 | } | 
| Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 495 | return false; | 
|  | 496 | } | 
|  | 497 | return true; | 
|  | 498 | } | 
|  | 499 |  | 
| Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 500 | std::string DynamicPartitionControlAndroid::GetSuperPartitionName( | 
|  | 501 | uint32_t slot) { | 
|  | 502 | return fs_mgr_get_super_partition_name(slot); | 
|  | 503 | } | 
|  | 504 |  | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 505 | bool DynamicPartitionControlAndroid::UpdatePartitionMetadata( | 
|  | 506 | MetadataBuilder* builder, | 
|  | 507 | uint32_t target_slot, | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 508 | const DeltaArchiveManifest& manifest) { | 
| Yifan Hong | a4e7da3 | 2019-09-30 18:25:03 -0700 | [diff] [blame] | 509 | // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over | 
|  | 510 | // COW group needs to be deleted to ensure there are enough space to create | 
|  | 511 | // target partitions. | 
|  | 512 | builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName); | 
|  | 513 |  | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 514 | const std::string target_suffix = SlotSuffixForSlotNumber(target_slot); | 
|  | 515 | DeleteGroupsWithSuffix(builder, target_suffix); | 
|  | 516 |  | 
|  | 517 | uint64_t total_size = 0; | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 518 | for (const auto& group : manifest.dynamic_partition_metadata().groups()) { | 
|  | 519 | total_size += group.size(); | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 520 | } | 
|  | 521 |  | 
|  | 522 | std::string expr; | 
|  | 523 | uint64_t allocatable_space = builder->AllocatableSpace(); | 
| Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 524 | if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) { | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 525 | allocatable_space /= 2; | 
|  | 526 | expr = "half of "; | 
|  | 527 | } | 
|  | 528 | if (total_size > allocatable_space) { | 
|  | 529 | LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix | 
|  | 530 | << " (" << total_size << ") has exceeded " << expr | 
|  | 531 | << "allocatable space for dynamic partitions " | 
|  | 532 | << allocatable_space << "."; | 
|  | 533 | return false; | 
|  | 534 | } | 
|  | 535 |  | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 536 | // name of partition(e.g. "system") -> size in bytes | 
|  | 537 | std::map<std::string, uint64_t> partition_sizes; | 
|  | 538 | for (const auto& partition : manifest.partitions()) { | 
|  | 539 | partition_sizes.emplace(partition.partition_name(), | 
|  | 540 | partition.new_partition_info().size()); | 
|  | 541 | } | 
|  | 542 |  | 
|  | 543 | for (const auto& group : manifest.dynamic_partition_metadata().groups()) { | 
|  | 544 | auto group_name_suffix = group.name() + target_suffix; | 
|  | 545 | if (!builder->AddGroup(group_name_suffix, group.size())) { | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 546 | LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size " | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 547 | << group.size(); | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 548 | return false; | 
|  | 549 | } | 
|  | 550 | LOG(INFO) << "Added group " << group_name_suffix << " with size " | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 551 | << group.size(); | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 552 |  | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 553 | for (const auto& partition_name : group.partition_names()) { | 
|  | 554 | auto partition_sizes_it = partition_sizes.find(partition_name); | 
|  | 555 | if (partition_sizes_it == partition_sizes.end()) { | 
|  | 556 | // TODO(tbao): Support auto-filling partition info for framework-only | 
|  | 557 | // OTA. | 
|  | 558 | LOG(ERROR) << "dynamic_partition_metadata contains partition " | 
|  | 559 | << partition_name << " but it is not part of the manifest. " | 
|  | 560 | << "This is not supported."; | 
|  | 561 | return false; | 
|  | 562 | } | 
|  | 563 | uint64_t partition_size = partition_sizes_it->second; | 
|  | 564 |  | 
|  | 565 | auto partition_name_suffix = partition_name + target_suffix; | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 566 | Partition* p = builder->AddPartition( | 
|  | 567 | partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY); | 
|  | 568 | if (!p) { | 
|  | 569 | LOG(ERROR) << "Cannot add partition " << partition_name_suffix | 
|  | 570 | << " to group " << group_name_suffix; | 
|  | 571 | return false; | 
|  | 572 | } | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 573 | if (!builder->ResizePartition(p, partition_size)) { | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 574 | LOG(ERROR) << "Cannot resize partition " << partition_name_suffix | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 575 | << " to size " << partition_size << ". Not enough space?"; | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 576 | return false; | 
|  | 577 | } | 
|  | 578 | LOG(INFO) << "Added partition " << partition_name_suffix << " to group " | 
| Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 579 | << group_name_suffix << " with size " << partition_size; | 
| Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 580 | } | 
|  | 581 | } | 
|  | 582 |  | 
|  | 583 | return true; | 
|  | 584 | } | 
|  | 585 |  | 
| Yifan Hong | a33bca4 | 2019-09-03 20:29:45 -0700 | [diff] [blame] | 586 | bool DynamicPartitionControlAndroid::FinishUpdate() { | 
| Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 587 | if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) { | 
|  | 588 | LOG(INFO) << "Snapshot writes are done."; | 
|  | 589 | return snapshot_->FinishedSnapshotWrites(); | 
|  | 590 | } | 
|  | 591 | return true; | 
| Yifan Hong | a33bca4 | 2019-09-03 20:29:45 -0700 | [diff] [blame] | 592 | } | 
|  | 593 |  | 
| Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 594 | bool DynamicPartitionControlAndroid::GetPartitionDevice( | 
|  | 595 | const std::string& partition_name, | 
|  | 596 | uint32_t slot, | 
|  | 597 | uint32_t current_slot, | 
|  | 598 | std::string* device) { | 
|  | 599 | const auto& partition_name_suffix = | 
|  | 600 | partition_name + SlotSuffixForSlotNumber(slot); | 
|  | 601 | std::string device_dir_str; | 
|  | 602 | TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str)); | 
|  | 603 | base::FilePath device_dir(device_dir_str); | 
|  | 604 |  | 
|  | 605 | // When looking up target partition devices, treat them as static if the | 
|  | 606 | // current payload doesn't encode them as dynamic partitions. This may happen | 
|  | 607 | // when applying a retrofit update on top of a dynamic-partitions-enabled | 
|  | 608 | // build. | 
|  | 609 | if (GetDynamicPartitionsFeatureFlag().IsEnabled() && | 
|  | 610 | (slot == current_slot || is_target_dynamic_)) { | 
|  | 611 | switch (GetDynamicPartitionDevice( | 
|  | 612 | device_dir, partition_name_suffix, slot, current_slot, device)) { | 
|  | 613 | case DynamicPartitionDeviceStatus::SUCCESS: | 
|  | 614 | return true; | 
|  | 615 | case DynamicPartitionDeviceStatus::TRY_STATIC: | 
|  | 616 | break; | 
|  | 617 | case DynamicPartitionDeviceStatus::ERROR:  // fallthrough | 
|  | 618 | default: | 
|  | 619 | return false; | 
|  | 620 | } | 
|  | 621 | } | 
|  | 622 | base::FilePath path = device_dir.Append(partition_name_suffix); | 
|  | 623 | if (!DeviceExists(path.value())) { | 
|  | 624 | LOG(ERROR) << "Device file " << path.value() << " does not exist."; | 
|  | 625 | return false; | 
|  | 626 | } | 
|  | 627 |  | 
|  | 628 | *device = path.value(); | 
|  | 629 | return true; | 
|  | 630 | } | 
|  | 631 |  | 
|  | 632 | bool DynamicPartitionControlAndroid::IsSuperBlockDevice( | 
|  | 633 | const base::FilePath& device_dir, | 
|  | 634 | uint32_t current_slot, | 
|  | 635 | const std::string& partition_name_suffix) { | 
|  | 636 | std::string source_device = | 
|  | 637 | device_dir.Append(GetSuperPartitionName(current_slot)).value(); | 
|  | 638 | auto source_metadata = LoadMetadataBuilder(source_device, current_slot); | 
|  | 639 | return source_metadata->HasBlockDevice(partition_name_suffix); | 
|  | 640 | } | 
|  | 641 |  | 
|  | 642 | DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus | 
|  | 643 | DynamicPartitionControlAndroid::GetDynamicPartitionDevice( | 
|  | 644 | const base::FilePath& device_dir, | 
|  | 645 | const std::string& partition_name_suffix, | 
|  | 646 | uint32_t slot, | 
|  | 647 | uint32_t current_slot, | 
|  | 648 | std::string* device) { | 
|  | 649 | std::string super_device = | 
|  | 650 | device_dir.Append(GetSuperPartitionName(slot)).value(); | 
|  | 651 |  | 
|  | 652 | auto builder = LoadMetadataBuilder(super_device, slot); | 
|  | 653 | if (builder == nullptr) { | 
|  | 654 | LOG(ERROR) << "No metadata in slot " | 
|  | 655 | << BootControlInterface::SlotName(slot); | 
|  | 656 | return DynamicPartitionDeviceStatus::ERROR; | 
|  | 657 | } | 
|  | 658 | if (builder->FindPartition(partition_name_suffix) == nullptr) { | 
|  | 659 | LOG(INFO) << partition_name_suffix | 
|  | 660 | << " is not in super partition metadata."; | 
|  | 661 |  | 
|  | 662 | if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) { | 
|  | 663 | LOG(ERROR) << "The static partition " << partition_name_suffix | 
|  | 664 | << " is a block device for current metadata." | 
|  | 665 | << "It cannot be used as a logical partition."; | 
|  | 666 | return DynamicPartitionDeviceStatus::ERROR; | 
|  | 667 | } | 
|  | 668 |  | 
|  | 669 | return DynamicPartitionDeviceStatus::TRY_STATIC; | 
|  | 670 | } | 
|  | 671 |  | 
|  | 672 | if (slot == current_slot) { | 
|  | 673 | if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) { | 
|  | 674 | LOG(WARNING) << partition_name_suffix << " is at current slot but it is " | 
|  | 675 | << "not mapped. Now try to map it."; | 
|  | 676 | } else { | 
|  | 677 | if (GetDmDevicePathByName(partition_name_suffix, device)) { | 
|  | 678 | LOG(INFO) << partition_name_suffix | 
|  | 679 | << " is mapped on device mapper: " << *device; | 
|  | 680 | return DynamicPartitionDeviceStatus::SUCCESS; | 
|  | 681 | } | 
|  | 682 | LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown."; | 
|  | 683 | return DynamicPartitionDeviceStatus::ERROR; | 
|  | 684 | } | 
|  | 685 | } | 
|  | 686 |  | 
|  | 687 | bool force_writable = slot != current_slot; | 
|  | 688 | if (MapPartitionOnDeviceMapper( | 
|  | 689 | super_device, partition_name_suffix, slot, force_writable, device)) { | 
|  | 690 | return DynamicPartitionDeviceStatus::SUCCESS; | 
|  | 691 | } | 
|  | 692 | return DynamicPartitionDeviceStatus::ERROR; | 
|  | 693 | } | 
|  | 694 |  | 
| Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 695 | void DynamicPartitionControlAndroid::set_fake_mapped_devices( | 
|  | 696 | const std::set<std::string>& fake) { | 
|  | 697 | mapped_devices_ = fake; | 
|  | 698 | } | 
|  | 699 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 700 | }  // namespace chromeos_update_engine |