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