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