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