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