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