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