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