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