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 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/aosp/dynamic_partition_control_android.h" |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 18 | |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 19 | #include <algorithm> |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 20 | #include <chrono> // NOLINT(build/c++11) - using libsnapshot / liblp API |
Kelvin Zhang | 3461852 | 2020-09-28 09:21:02 -0400 | [diff] [blame] | 21 | #include <cstdint> |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 22 | #include <map> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 23 | #include <memory> |
| 24 | #include <set> |
| 25 | #include <string> |
Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 26 | #include <string_view> |
| 27 | #include <utility> |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 28 | #include <vector> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 29 | |
| 30 | #include <android-base/properties.h> |
| 31 | #include <android-base/strings.h> |
| 32 | #include <base/files/file_util.h> |
| 33 | #include <base/logging.h> |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 34 | #include <base/strings/string_util.h> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 35 | #include <bootloader_message/bootloader_message.h> |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 36 | #include <fs_mgr.h> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 37 | #include <fs_mgr_dm_linear.h> |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 38 | #include <fs_mgr_overlayfs.h> |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 39 | #include <libavb/libavb.h> |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 40 | #include <libdm/dm.h> |
Kelvin Zhang | 3461852 | 2020-09-28 09:21:02 -0400 | [diff] [blame] | 41 | #include <liblp/liblp.h> |
| 42 | #include <libsnapshot/cow_writer.h> |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 43 | #include <libsnapshot/snapshot.h> |
Yifan Hong | f9cb449 | 2020-04-15 13:00:20 -0700 | [diff] [blame] | 44 | #include <libsnapshot/snapshot_stub.h> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 45 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 46 | #include "update_engine/aosp/cleanup_previous_update_action.h" |
| 47 | #include "update_engine/aosp/dynamic_partition_utils.h" |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 48 | #include "update_engine/common/boot_control_interface.h" |
Kelvin Zhang | b9a5f61 | 2021-01-22 14:34:05 -0500 | [diff] [blame] | 49 | #include "update_engine/common/dynamic_partition_control_interface.h" |
| 50 | #include "update_engine/common/platform_constants.h" |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 51 | #include "update_engine/common/utils.h" |
Yifan Hong | 6a6d0f1 | 2020-03-11 13:20:52 -0700 | [diff] [blame] | 52 | #include "update_engine/payload_consumer/delta_performer.h" |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 53 | |
| 54 | using android::base::GetBoolProperty; |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 55 | using android::base::GetProperty; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 56 | using android::base::Join; |
| 57 | using android::dm::DeviceMapper; |
| 58 | using android::dm::DmDeviceState; |
| 59 | using android::fs_mgr::CreateLogicalPartition; |
David Anderson | bb90dfb | 2019-08-13 14:14:56 -0700 | [diff] [blame] | 60 | using android::fs_mgr::CreateLogicalPartitionParams; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 61 | using android::fs_mgr::DestroyLogicalPartition; |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 62 | using android::fs_mgr::Fstab; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 63 | using android::fs_mgr::MetadataBuilder; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 64 | using android::fs_mgr::Partition; |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 65 | using android::fs_mgr::PartitionOpener; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 66 | using android::fs_mgr::SlotSuffixForSlotNumber; |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 67 | using android::snapshot::OptimizeSourceCopyOperation; |
Yifan Hong | 0850bca | 2020-01-16 15:14:07 -0800 | [diff] [blame] | 68 | using android::snapshot::Return; |
Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 69 | using android::snapshot::SnapshotManager; |
Yifan Hong | f9cb449 | 2020-04-15 13:00:20 -0700 | [diff] [blame] | 70 | using android::snapshot::SnapshotManagerStub; |
Yifan Hong | 2257ee1 | 2020-01-13 18:33:00 -0800 | [diff] [blame] | 71 | using android::snapshot::UpdateState; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 72 | |
| 73 | namespace chromeos_update_engine { |
| 74 | |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 75 | constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions"; |
| 76 | constexpr char kRetrfoitDynamicPartitions[] = |
| 77 | "ro.boot.dynamic_partitions_retrofit"; |
Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 78 | constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled"; |
| 79 | constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit"; |
Kelvin Zhang | da1b314 | 2020-09-24 17:09:02 -0400 | [diff] [blame] | 80 | constexpr char kVirtualAbCompressionEnabled[] = |
| 81 | "ro.virtual_ab.compression.enabled"; |
| 82 | |
| 83 | // Currently, android doesn't have a retrofit prop for VAB Compression. However, |
| 84 | // struct FeatureFlag forces us to determine if a feature is 'retrofit'. So this |
| 85 | // is here just to simplify code. Replace it with real retrofit prop name once |
| 86 | // there is one. |
| 87 | constexpr char kVirtualAbCompressionRetrofit[] = ""; |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 88 | constexpr char kPostinstallFstabPrefix[] = "ro.postinstall.fstab.prefix"; |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 89 | // Map timeout for dynamic partitions. |
| 90 | constexpr std::chrono::milliseconds kMapTimeout{1000}; |
| 91 | // Map timeout for dynamic partitions with snapshots. Since several devices |
| 92 | // needs to be mapped, this timeout is longer than |kMapTimeout|. |
| 93 | constexpr std::chrono::milliseconds kMapSnapshotTimeout{5000}; |
| 94 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 95 | DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() { |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 96 | Cleanup(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 99 | static FeatureFlag GetFeatureFlag(const char* enable_prop, |
| 100 | const char* retrofit_prop) { |
Kelvin Zhang | da1b314 | 2020-09-24 17:09:02 -0400 | [diff] [blame] | 101 | // Default retrofit to false if retrofit_prop is empty. |
| 102 | bool retrofit = retrofit_prop && retrofit_prop[0] != '\0' && |
| 103 | GetBoolProperty(retrofit_prop, false); |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 104 | bool enabled = GetBoolProperty(enable_prop, false); |
| 105 | if (retrofit && !enabled) { |
| 106 | LOG(ERROR) << retrofit_prop << " is true but " << enable_prop |
| 107 | << " is not. These sysprops are inconsistent. Assume that " |
| 108 | << enable_prop << " is true from now on."; |
| 109 | } |
| 110 | if (retrofit) { |
| 111 | return FeatureFlag(FeatureFlag::Value::RETROFIT); |
| 112 | } |
| 113 | if (enabled) { |
| 114 | return FeatureFlag(FeatureFlag::Value::LAUNCH); |
| 115 | } |
| 116 | return FeatureFlag(FeatureFlag::Value::NONE); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 119 | DynamicPartitionControlAndroid::DynamicPartitionControlAndroid() |
| 120 | : dynamic_partitions_( |
| 121 | GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions)), |
Kelvin Zhang | da1b314 | 2020-09-24 17:09:02 -0400 | [diff] [blame] | 122 | virtual_ab_(GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit)), |
| 123 | virtual_ab_compression_(GetFeatureFlag(kVirtualAbCompressionEnabled, |
| 124 | kVirtualAbCompressionRetrofit)) { |
Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 125 | if (GetVirtualAbFeatureFlag().IsEnabled()) { |
Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 126 | snapshot_ = SnapshotManager::New(); |
Yifan Hong | f9cb449 | 2020-04-15 13:00:20 -0700 | [diff] [blame] | 127 | } else { |
| 128 | snapshot_ = SnapshotManagerStub::New(); |
Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 129 | } |
Yifan Hong | f9cb449 | 2020-04-15 13:00:20 -0700 | [diff] [blame] | 130 | CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager."; |
Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 133 | FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() { |
Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 134 | return dynamic_partitions_; |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 137 | FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() { |
Yifan Hong | b38e1af | 2019-10-17 14:59:22 -0700 | [diff] [blame] | 138 | return virtual_ab_; |
Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Kelvin Zhang | da1b314 | 2020-09-24 17:09:02 -0400 | [diff] [blame] | 141 | FeatureFlag |
| 142 | DynamicPartitionControlAndroid::GetVirtualAbCompressionFeatureFlag() { |
Kelvin Zhang | b9a5f61 | 2021-01-22 14:34:05 -0500 | [diff] [blame] | 143 | if constexpr (constants::kIsRecovery) { |
| 144 | // Don't attempt VABC in recovery |
| 145 | return FeatureFlag(FeatureFlag::Value::NONE); |
| 146 | } |
Kelvin Zhang | da1b314 | 2020-09-24 17:09:02 -0400 | [diff] [blame] | 147 | return virtual_ab_compression_; |
| 148 | } |
| 149 | |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 150 | bool DynamicPartitionControlAndroid::OptimizeOperation( |
| 151 | const std::string& partition_name, |
| 152 | const InstallOperation& operation, |
| 153 | InstallOperation* optimized) { |
Alessio Balsini | 2a3b4a2 | 2019-11-25 16:46:51 +0000 | [diff] [blame] | 154 | switch (operation.type()) { |
| 155 | case InstallOperation::SOURCE_COPY: |
| 156 | return target_supports_snapshot_ && |
| 157 | GetVirtualAbFeatureFlag().IsEnabled() && |
Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 158 | mapped_devices_.count(partition_name + |
| 159 | SlotSuffixForSlotNumber(target_slot_)) > 0 && |
Yifan Hong | f526156 | 2020-03-10 10:28:10 -0700 | [diff] [blame] | 160 | OptimizeSourceCopyOperation(operation, optimized); |
Alessio Balsini | 2a3b4a2 | 2019-11-25 16:46:51 +0000 | [diff] [blame] | 161 | break; |
| 162 | default: |
| 163 | break; |
| 164 | } |
Alessio Balsini | 14980e2 | 2019-11-26 11:46:06 +0000 | [diff] [blame] | 165 | return false; |
| 166 | } |
| 167 | |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 168 | bool DynamicPartitionControlAndroid::MapPartitionInternal( |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 169 | const std::string& super_device, |
| 170 | const std::string& target_partition_name, |
| 171 | uint32_t slot, |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 172 | bool force_writable, |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 173 | std::string* path) { |
David Anderson | bb90dfb | 2019-08-13 14:14:56 -0700 | [diff] [blame] | 174 | CreateLogicalPartitionParams params = { |
| 175 | .block_device = super_device, |
| 176 | .metadata_slot = slot, |
| 177 | .partition_name = target_partition_name, |
| 178 | .force_writable = force_writable, |
David Anderson | bb90dfb | 2019-08-13 14:14:56 -0700 | [diff] [blame] | 179 | }; |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 180 | bool success = false; |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 181 | if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ && |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 182 | force_writable && ExpectMetadataMounted()) { |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 183 | // Only target partitions are mapped with force_writable. On Virtual |
| 184 | // A/B devices, target partitions may overlap with source partitions, so |
| 185 | // they must be mapped with snapshot. |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 186 | // One exception is when /metadata is not mounted. Fallback to |
| 187 | // CreateLogicalPartition as snapshots are not created in the first place. |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 188 | params.timeout_ms = kMapSnapshotTimeout; |
| 189 | success = snapshot_->MapUpdateSnapshot(params, path); |
| 190 | } else { |
| 191 | params.timeout_ms = kMapTimeout; |
| 192 | success = CreateLogicalPartition(params, path); |
| 193 | } |
David Anderson | bb90dfb | 2019-08-13 14:14:56 -0700 | [diff] [blame] | 194 | |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 195 | if (!success) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 196 | LOG(ERROR) << "Cannot map " << target_partition_name << " in " |
| 197 | << super_device << " on device mapper."; |
| 198 | return false; |
| 199 | } |
| 200 | LOG(INFO) << "Succesfully mapped " << target_partition_name |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 201 | << " to device mapper (force_writable = " << force_writable |
| 202 | << "); device path at " << *path; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 203 | mapped_devices_.insert(target_partition_name); |
| 204 | return true; |
| 205 | } |
| 206 | |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 207 | bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper( |
| 208 | const std::string& super_device, |
| 209 | const std::string& target_partition_name, |
| 210 | uint32_t slot, |
| 211 | bool force_writable, |
| 212 | std::string* path) { |
| 213 | DmDeviceState state = GetState(target_partition_name); |
| 214 | if (state == DmDeviceState::ACTIVE) { |
| 215 | if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) { |
| 216 | if (GetDmDevicePathByName(target_partition_name, path)) { |
| 217 | LOG(INFO) << target_partition_name |
| 218 | << " is mapped on device mapper: " << *path; |
| 219 | return true; |
| 220 | } |
| 221 | LOG(ERROR) << target_partition_name << " is mapped but path is unknown."; |
| 222 | return false; |
| 223 | } |
| 224 | // If target_partition_name is not in mapped_devices_ but state is ACTIVE, |
| 225 | // the device might be mapped incorrectly before. Attempt to unmap it. |
| 226 | // Note that for source partitions, if GetState() == ACTIVE, callers (e.g. |
| 227 | // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but |
| 228 | // should directly call GetDmDevicePathByName. |
David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 229 | if (!UnmapPartitionOnDeviceMapper(target_partition_name)) { |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 230 | LOG(ERROR) << target_partition_name |
| 231 | << " is mapped before the update, and it cannot be unmapped."; |
| 232 | return false; |
| 233 | } |
| 234 | state = GetState(target_partition_name); |
| 235 | if (state != DmDeviceState::INVALID) { |
| 236 | LOG(ERROR) << target_partition_name << " is unmapped but state is " |
| 237 | << static_cast<std::underlying_type_t<DmDeviceState>>(state); |
| 238 | return false; |
| 239 | } |
| 240 | } |
| 241 | if (state == DmDeviceState::INVALID) { |
| 242 | return MapPartitionInternal( |
| 243 | super_device, target_partition_name, slot, force_writable, path); |
| 244 | } |
| 245 | |
| 246 | LOG(ERROR) << target_partition_name |
| 247 | << " is mapped on device mapper but state is unknown: " |
| 248 | << static_cast<std::underlying_type_t<DmDeviceState>>(state); |
| 249 | return false; |
| 250 | } |
| 251 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 252 | bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper( |
David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 253 | const std::string& target_partition_name) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 254 | if (DeviceMapper::Instance().GetState(target_partition_name) != |
| 255 | DmDeviceState::INVALID) { |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 256 | // Partitions at target slot on non-Virtual A/B devices are mapped as |
| 257 | // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for |
| 258 | // preopt apps as dm-linear. |
| 259 | // Call DestroyLogicalPartition to handle these cases. |
| 260 | bool success = DestroyLogicalPartition(target_partition_name); |
| 261 | |
| 262 | // On a Virtual A/B device, |target_partition_name| may be a leftover from |
| 263 | // a paused update. Clean up any underlying devices. |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 264 | if (ExpectMetadataMounted()) { |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 265 | success &= snapshot_->UnmapUpdateSnapshot(target_partition_name); |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 266 | } else { |
| 267 | LOG(INFO) << "Skip UnmapUpdateSnapshot(" << target_partition_name |
| 268 | << ") because metadata is not mounted"; |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if (!success) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 272 | LOG(ERROR) << "Cannot unmap " << target_partition_name |
| 273 | << " from device mapper."; |
| 274 | return false; |
| 275 | } |
| 276 | LOG(INFO) << "Successfully unmapped " << target_partition_name |
| 277 | << " from device mapper."; |
| 278 | } |
| 279 | mapped_devices_.erase(target_partition_name); |
| 280 | return true; |
| 281 | } |
| 282 | |
Kelvin Zhang | 9d87d6d | 2020-10-23 17:03:59 -0400 | [diff] [blame] | 283 | bool DynamicPartitionControlAndroid::UnmapAllPartitions() { |
Tao Bao | 8c4d008 | 2019-08-08 08:56:16 -0700 | [diff] [blame] | 284 | if (mapped_devices_.empty()) { |
Kelvin Zhang | 9d87d6d | 2020-10-23 17:03:59 -0400 | [diff] [blame] | 285 | return false; |
Tao Bao | 8c4d008 | 2019-08-08 08:56:16 -0700 | [diff] [blame] | 286 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 287 | // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence |
| 288 | // a copy is needed for the loop. |
| 289 | std::set<std::string> mapped = mapped_devices_; |
| 290 | LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper"; |
| 291 | for (const auto& partition_name : mapped) { |
David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 292 | ignore_result(UnmapPartitionOnDeviceMapper(partition_name)); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 293 | } |
Kelvin Zhang | 9d87d6d | 2020-10-23 17:03:59 -0400 | [diff] [blame] | 294 | return true; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | void DynamicPartitionControlAndroid::Cleanup() { |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 298 | UnmapAllPartitions(); |
| 299 | metadata_device_.reset(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) { |
| 303 | return base::PathExists(base::FilePath(path)); |
| 304 | } |
| 305 | |
| 306 | android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState( |
| 307 | const std::string& name) { |
| 308 | return DeviceMapper::Instance().GetState(name); |
| 309 | } |
| 310 | |
| 311 | bool DynamicPartitionControlAndroid::GetDmDevicePathByName( |
| 312 | const std::string& name, std::string* path) { |
| 313 | return DeviceMapper::Instance().GetDmDevicePathByName(name, path); |
| 314 | } |
| 315 | |
| 316 | std::unique_ptr<MetadataBuilder> |
| 317 | DynamicPartitionControlAndroid::LoadMetadataBuilder( |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 318 | const std::string& super_device, uint32_t slot) { |
| 319 | auto builder = MetadataBuilder::New(PartitionOpener(), super_device, slot); |
| 320 | if (builder == nullptr) { |
| 321 | LOG(WARNING) << "No metadata slot " << BootControlInterface::SlotName(slot) |
| 322 | << " in " << super_device; |
| 323 | return nullptr; |
| 324 | } |
| 325 | LOG(INFO) << "Loaded metadata from slot " |
| 326 | << BootControlInterface::SlotName(slot) << " in " << super_device; |
| 327 | return builder; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | std::unique_ptr<MetadataBuilder> |
| 331 | DynamicPartitionControlAndroid::LoadMetadataBuilder( |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 332 | const std::string& super_device, |
| 333 | uint32_t source_slot, |
| 334 | uint32_t target_slot) { |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 335 | bool always_keep_source_slot = !target_supports_snapshot_; |
| 336 | auto builder = MetadataBuilder::NewForUpdate(PartitionOpener(), |
| 337 | super_device, |
| 338 | source_slot, |
| 339 | target_slot, |
| 340 | always_keep_source_slot); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 341 | if (builder == nullptr) { |
| 342 | LOG(WARNING) << "No metadata slot " |
| 343 | << BootControlInterface::SlotName(source_slot) << " in " |
| 344 | << super_device; |
Yifan Hong | f48a005 | 2018-10-29 16:30:43 -0700 | [diff] [blame] | 345 | return nullptr; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 346 | } |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 347 | LOG(INFO) << "Created metadata for new update from slot " |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 348 | << BootControlInterface::SlotName(source_slot) << " in " |
| 349 | << super_device; |
| 350 | return builder; |
| 351 | } |
| 352 | |
| 353 | bool DynamicPartitionControlAndroid::StoreMetadata( |
| 354 | const std::string& super_device, |
| 355 | MetadataBuilder* builder, |
| 356 | uint32_t target_slot) { |
| 357 | auto metadata = builder->Export(); |
| 358 | if (metadata == nullptr) { |
| 359 | LOG(ERROR) << "Cannot export metadata to slot " |
| 360 | << BootControlInterface::SlotName(target_slot) << " in " |
| 361 | << super_device; |
| 362 | return false; |
| 363 | } |
| 364 | |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 365 | if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) { |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 366 | if (!FlashPartitionTable(super_device, *metadata)) { |
| 367 | LOG(ERROR) << "Cannot write metadata to " << super_device; |
| 368 | return false; |
| 369 | } |
| 370 | LOG(INFO) << "Written metadata to " << super_device; |
| 371 | } else { |
| 372 | if (!UpdatePartitionTable(super_device, *metadata, target_slot)) { |
| 373 | LOG(ERROR) << "Cannot write metadata to slot " |
| 374 | << BootControlInterface::SlotName(target_slot) << " in " |
| 375 | << super_device; |
| 376 | return false; |
| 377 | } |
| 378 | LOG(INFO) << "Copied metadata to slot " |
| 379 | << BootControlInterface::SlotName(target_slot) << " in " |
| 380 | << super_device; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 383 | return true; |
| 384 | } |
| 385 | |
| 386 | bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) { |
| 387 | // We can't use fs_mgr to look up |partition_name| because fstab |
| 388 | // doesn't list every slot partition (it uses the slotselect option |
| 389 | // to mask the suffix). |
| 390 | // |
| 391 | // We can however assume that there's an entry for the /misc mount |
| 392 | // point and use that to get the device file for the misc |
| 393 | // partition. This helps us locate the disk that |partition_name| |
| 394 | // resides on. From there we'll assume that a by-name scheme is used |
| 395 | // so we can just replace the trailing "misc" by the given |
| 396 | // |partition_name| and suffix corresponding to |slot|, e.g. |
| 397 | // |
| 398 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc -> |
| 399 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a |
| 400 | // |
| 401 | // If needed, it's possible to relax the by-name assumption in the |
| 402 | // future by trawling /sys/block looking for the appropriate sibling |
| 403 | // of misc and then finding an entry in /dev matching the sysfs |
| 404 | // entry. |
| 405 | |
| 406 | std::string err, misc_device = get_bootloader_message_blk_device(&err); |
| 407 | if (misc_device.empty()) { |
| 408 | LOG(ERROR) << "Unable to get misc block device: " << err; |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | if (!utils::IsSymlink(misc_device.c_str())) { |
| 413 | LOG(ERROR) << "Device file " << misc_device << " for /misc " |
| 414 | << "is not a symlink."; |
| 415 | return false; |
| 416 | } |
| 417 | *out = base::FilePath(misc_device).DirName().value(); |
| 418 | return true; |
| 419 | } |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 420 | |
| 421 | bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate( |
| 422 | uint32_t source_slot, |
| 423 | uint32_t target_slot, |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 424 | const DeltaArchiveManifest& manifest, |
Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 425 | bool update, |
| 426 | uint64_t* required_size) { |
Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 427 | source_slot_ = source_slot; |
| 428 | target_slot_ = target_slot; |
Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 429 | if (required_size != nullptr) { |
| 430 | *required_size = 0; |
| 431 | } |
Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 432 | |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 433 | if (fs_mgr_overlayfs_is_setup()) { |
| 434 | // Non DAP devices can use overlayfs as well. |
| 435 | LOG(WARNING) |
| 436 | << "overlayfs overrides are active and can interfere with our " |
| 437 | "resources.\n" |
| 438 | << "run adb enable-verity to deactivate if required and try again."; |
| 439 | } |
| 440 | |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 441 | // If metadata is erased but not formatted, it is possible to not mount |
| 442 | // it in recovery. It is acceptable to skip mounting and choose fallback path |
| 443 | // (PrepareDynamicPartitionsForUpdate) when sideloading full OTAs. |
| 444 | TEST_AND_RETURN_FALSE(EnsureMetadataMounted() || IsRecovery()); |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 445 | |
| 446 | if (update) { |
| 447 | TEST_AND_RETURN_FALSE(EraseSystemOtherAvbFooter(source_slot, target_slot)); |
| 448 | } |
| 449 | |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 450 | if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) { |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | if (target_slot == source_slot) { |
| 455 | LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot."; |
| 456 | return false; |
| 457 | } |
| 458 | |
Yifan Hong | f6f75c2 | 2020-07-31 15:20:25 -0700 | [diff] [blame] | 459 | if (!SetTargetBuildVars(manifest)) { |
| 460 | return false; |
| 461 | } |
| 462 | |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 463 | // Although the current build supports dynamic partitions, the given payload |
| 464 | // doesn't use it for target partitions. This could happen when applying a |
| 465 | // retrofit update. Skip updating the partition metadata for the target slot. |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 466 | if (!is_target_dynamic_) { |
| 467 | return true; |
| 468 | } |
| 469 | |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 470 | if (!update) |
| 471 | return true; |
| 472 | |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 473 | bool delete_source = false; |
| 474 | |
Yifan Hong | 6d88856 | 2019-10-01 13:00:31 -0700 | [diff] [blame] | 475 | if (GetVirtualAbFeatureFlag().IsEnabled()) { |
| 476 | // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be |
| 477 | // called before calling UnmapUpdateSnapshot. |
| 478 | // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate() |
| 479 | // calls BeginUpdate() which resets update state |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 480 | // - If !target_supports_snapshot_ or PrepareSnapshotPartitionsForUpdate |
| 481 | // failed in recovery, explicitly CancelUpdate(). |
Yifan Hong | 6d88856 | 2019-10-01 13:00:31 -0700 | [diff] [blame] | 482 | if (target_supports_snapshot_) { |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 483 | if (PrepareSnapshotPartitionsForUpdate( |
| 484 | source_slot, target_slot, manifest, required_size)) { |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | // Virtual A/B device doing Virtual A/B update in Android mode must use |
| 489 | // snapshots. |
| 490 | if (!IsRecovery()) { |
| 491 | LOG(ERROR) << "PrepareSnapshotPartitionsForUpdate failed in Android " |
| 492 | << "mode"; |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | delete_source = true; |
| 497 | LOG(INFO) << "PrepareSnapshotPartitionsForUpdate failed in recovery. " |
| 498 | << "Attempt to overwrite existing partitions if possible"; |
| 499 | } else { |
| 500 | // Downgrading to an non-Virtual A/B build or is secondary OTA. |
| 501 | LOG(INFO) << "Using regular A/B on Virtual A/B because package disabled " |
| 502 | << "snapshots."; |
Yifan Hong | 6d88856 | 2019-10-01 13:00:31 -0700 | [diff] [blame] | 503 | } |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 504 | |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 505 | // In recovery, if /metadata is not mounted, it is likely that metadata |
| 506 | // partition is erased and not formatted yet. After sideloading, when |
| 507 | // rebooting into the new version, init will erase metadata partition, |
| 508 | // hence the failure of CancelUpdate() can be ignored here. |
| 509 | // However, if metadata is mounted and CancelUpdate fails, sideloading |
| 510 | // should not proceed because during next boot, snapshots will overlay on |
| 511 | // the devices incorrectly. |
| 512 | if (ExpectMetadataMounted()) { |
| 513 | TEST_AND_RETURN_FALSE(snapshot_->CancelUpdate()); |
| 514 | } else { |
| 515 | LOG(INFO) << "Skip canceling previous update because metadata is not " |
| 516 | << "mounted"; |
Yifan Hong | 6d88856 | 2019-10-01 13:00:31 -0700 | [diff] [blame] | 517 | } |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 518 | } |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 519 | |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 520 | // TODO(xunchang) support partial update on non VAB enabled devices. |
Yifan Hong | 5c5743f | 2020-04-16 12:59:07 -0700 | [diff] [blame] | 521 | TEST_AND_RETURN_FALSE(PrepareDynamicPartitionsForUpdate( |
| 522 | source_slot, target_slot, manifest, delete_source)); |
| 523 | |
| 524 | if (required_size != nullptr) { |
| 525 | *required_size = 0; |
| 526 | } |
| 527 | return true; |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Yifan Hong | f6f75c2 | 2020-07-31 15:20:25 -0700 | [diff] [blame] | 530 | bool DynamicPartitionControlAndroid::SetTargetBuildVars( |
| 531 | const DeltaArchiveManifest& manifest) { |
| 532 | // Precondition: current build supports dynamic partition. |
| 533 | CHECK(GetDynamicPartitionsFeatureFlag().IsEnabled()); |
| 534 | |
| 535 | bool is_target_dynamic = |
| 536 | !manifest.dynamic_partition_metadata().groups().empty(); |
| 537 | bool target_supports_snapshot = |
| 538 | manifest.dynamic_partition_metadata().snapshot_enabled(); |
| 539 | |
| 540 | if (manifest.partial_update()) { |
| 541 | // Partial updates requires DAP. On partial updates that does not involve |
| 542 | // dynamic partitions, groups() can be empty, so also assume |
| 543 | // is_target_dynamic in this case. This assumption should be safe because we |
| 544 | // also check target_supports_snapshot below, which presumably also implies |
| 545 | // target build supports dynamic partition. |
| 546 | if (!is_target_dynamic) { |
| 547 | LOG(INFO) << "Assuming target build supports dynamic partitions for " |
| 548 | "partial updates."; |
| 549 | is_target_dynamic = true; |
| 550 | } |
| 551 | |
| 552 | // Partial updates requires Virtual A/B. Double check that both current |
| 553 | // build and target build supports Virtual A/B. |
| 554 | if (!GetVirtualAbFeatureFlag().IsEnabled()) { |
| 555 | LOG(ERROR) << "Partial update cannot be applied on a device that does " |
| 556 | "not support snapshots."; |
| 557 | return false; |
| 558 | } |
| 559 | if (!target_supports_snapshot) { |
| 560 | LOG(ERROR) << "Cannot apply partial update to a build that does not " |
| 561 | "support snapshots."; |
| 562 | return false; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | // Store the flags. |
| 567 | is_target_dynamic_ = is_target_dynamic; |
| 568 | // If !is_target_dynamic_, leave target_supports_snapshot_ unset because |
| 569 | // snapshots would not work without dynamic partition. |
| 570 | if (is_target_dynamic_) { |
| 571 | target_supports_snapshot_ = target_supports_snapshot; |
| 572 | } |
| 573 | return true; |
| 574 | } |
| 575 | |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 576 | namespace { |
| 577 | // Try our best to erase AVB footer. |
| 578 | class AvbFooterEraser { |
| 579 | public: |
| 580 | explicit AvbFooterEraser(const std::string& path) : path_(path) {} |
| 581 | bool Erase() { |
| 582 | // Try to mark the block device read-only. Ignore any |
| 583 | // failure since this won't work when passing regular files. |
| 584 | ignore_result(utils::SetBlockDeviceReadOnly(path_, false /* readonly */)); |
| 585 | |
| 586 | fd_.reset(new EintrSafeFileDescriptor()); |
| 587 | int flags = O_WRONLY | O_TRUNC | O_CLOEXEC | O_SYNC; |
| 588 | TEST_AND_RETURN_FALSE(fd_->Open(path_.c_str(), flags)); |
| 589 | |
| 590 | // Need to write end-AVB_FOOTER_SIZE to end. |
| 591 | static_assert(AVB_FOOTER_SIZE > 0); |
| 592 | off64_t offset = fd_->Seek(-AVB_FOOTER_SIZE, SEEK_END); |
| 593 | TEST_AND_RETURN_FALSE_ERRNO(offset >= 0); |
| 594 | uint64_t write_size = AVB_FOOTER_SIZE; |
| 595 | LOG(INFO) << "Zeroing " << path_ << " @ [" << offset << ", " |
| 596 | << (offset + write_size) << "] (" << write_size << " bytes)"; |
| 597 | brillo::Blob zeros(write_size); |
| 598 | TEST_AND_RETURN_FALSE(utils::WriteAll(fd_, zeros.data(), zeros.size())); |
| 599 | return true; |
| 600 | } |
| 601 | ~AvbFooterEraser() { |
| 602 | TEST_AND_RETURN(fd_ != nullptr && fd_->IsOpen()); |
| 603 | if (!fd_->Close()) { |
| 604 | LOG(WARNING) << "Failed to close fd for " << path_; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | private: |
| 609 | std::string path_; |
| 610 | FileDescriptorPtr fd_; |
| 611 | }; |
| 612 | |
| 613 | } // namespace |
| 614 | |
| 615 | std::optional<bool> |
| 616 | DynamicPartitionControlAndroid::IsAvbEnabledOnSystemOther() { |
| 617 | auto prefix = GetProperty(kPostinstallFstabPrefix, ""); |
| 618 | if (prefix.empty()) { |
| 619 | LOG(WARNING) << "Cannot get " << kPostinstallFstabPrefix; |
| 620 | return std::nullopt; |
| 621 | } |
| 622 | auto path = base::FilePath(prefix).Append("etc/fstab.postinstall").value(); |
| 623 | return IsAvbEnabledInFstab(path); |
| 624 | } |
| 625 | |
| 626 | std::optional<bool> DynamicPartitionControlAndroid::IsAvbEnabledInFstab( |
| 627 | const std::string& path) { |
| 628 | Fstab fstab; |
| 629 | if (!ReadFstabFromFile(path, &fstab)) { |
Yifan Hong | 93cde30 | 2020-04-27 12:59:29 -0700 | [diff] [blame] | 630 | PLOG(WARNING) << "Cannot read fstab from " << path; |
| 631 | if (errno == ENOENT) { |
| 632 | return false; |
| 633 | } |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 634 | return std::nullopt; |
| 635 | } |
| 636 | for (const auto& entry : fstab) { |
| 637 | if (!entry.avb_keys.empty()) { |
| 638 | return true; |
| 639 | } |
| 640 | } |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | bool DynamicPartitionControlAndroid::GetSystemOtherPath( |
| 645 | uint32_t source_slot, |
| 646 | uint32_t target_slot, |
| 647 | const std::string& partition_name_suffix, |
| 648 | std::string* path, |
| 649 | bool* should_unmap) { |
| 650 | path->clear(); |
| 651 | *should_unmap = false; |
| 652 | |
P.Adarsh Reddy | 13e4195d | 2020-06-08 23:17:36 +0530 | [diff] [blame] | 653 | // Check that AVB is enabled on system_other before erasing. |
| 654 | auto has_avb = IsAvbEnabledOnSystemOther(); |
| 655 | TEST_AND_RETURN_FALSE(has_avb.has_value()); |
| 656 | if (!has_avb.value()) { |
| 657 | LOG(INFO) << "AVB is not enabled on system_other. Skip erasing."; |
| 658 | return true; |
| 659 | } |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 660 | |
P.Adarsh Reddy | 13e4195d | 2020-06-08 23:17:36 +0530 | [diff] [blame] | 661 | if (!IsRecovery()) { |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 662 | // Found unexpected avb_keys for system_other on devices retrofitting |
| 663 | // dynamic partitions. Previous crash in update_engine may leave logical |
| 664 | // partitions mapped on physical system_other partition. It is difficult to |
| 665 | // handle these cases. Just fail. |
| 666 | if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) { |
| 667 | LOG(ERROR) << "Cannot erase AVB footer on system_other on devices with " |
| 668 | << "retrofit dynamic partitions. They should not have AVB " |
| 669 | << "enabled on system_other."; |
| 670 | return false; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | std::string device_dir_str; |
| 675 | TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str)); |
| 676 | base::FilePath device_dir(device_dir_str); |
| 677 | |
| 678 | // On devices without dynamic partition, search for static partitions. |
| 679 | if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) { |
| 680 | *path = device_dir.Append(partition_name_suffix).value(); |
| 681 | TEST_AND_RETURN_FALSE(DeviceExists(*path)); |
| 682 | return true; |
| 683 | } |
| 684 | |
| 685 | auto source_super_device = |
| 686 | device_dir.Append(GetSuperPartitionName(source_slot)).value(); |
| 687 | |
| 688 | auto builder = LoadMetadataBuilder(source_super_device, source_slot); |
| 689 | if (builder == nullptr) { |
| 690 | if (IsRecovery()) { |
| 691 | // It might be corrupted for some reason. It should still be able to |
| 692 | // sideload. |
| 693 | LOG(WARNING) << "Super partition metadata cannot be read from the source " |
| 694 | << "slot, skip erasing."; |
| 695 | return true; |
| 696 | } else { |
| 697 | // Device has booted into Android mode, indicating that the super |
| 698 | // partition metadata should be there. |
| 699 | LOG(ERROR) << "Super partition metadata cannot be read from the source " |
| 700 | << "slot. This is unexpected on devices with dynamic " |
| 701 | << "partitions enabled."; |
| 702 | return false; |
| 703 | } |
| 704 | } |
| 705 | auto p = builder->FindPartition(partition_name_suffix); |
| 706 | if (p == nullptr) { |
| 707 | // If the source slot is flashed without system_other, it does not exist |
| 708 | // in super partition metadata at source slot. It is safe to skip it. |
| 709 | LOG(INFO) << "Can't find " << partition_name_suffix |
| 710 | << " in metadata source slot, skip erasing."; |
| 711 | return true; |
| 712 | } |
| 713 | // System_other created by flashing tools should be erased. |
| 714 | // If partition is created by update_engine (via NewForUpdate), it is a |
| 715 | // left-over partition from the previous update and does not contain |
| 716 | // system_other, hence there is no need to erase. |
| 717 | // Note the reverse is not necessary true. If the flag is not set, we don't |
| 718 | // know if the partition is created by update_engine or by flashing tools |
| 719 | // because older versions of super partition metadata does not contain this |
| 720 | // flag. It is okay to erase the AVB footer anyways. |
| 721 | if (p->attributes() & LP_PARTITION_ATTR_UPDATED) { |
| 722 | LOG(INFO) << partition_name_suffix |
| 723 | << " does not contain system_other, skip erasing."; |
| 724 | return true; |
| 725 | } |
| 726 | |
Yifan Hong | 64331b3 | 2020-05-13 16:50:40 -0700 | [diff] [blame] | 727 | if (p->size() < AVB_FOOTER_SIZE) { |
| 728 | LOG(INFO) << partition_name_suffix << " has length " << p->size() |
| 729 | << "( < AVB_FOOTER_SIZE " << AVB_FOOTER_SIZE |
| 730 | << "), skip erasing."; |
| 731 | return true; |
| 732 | } |
| 733 | |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 734 | // Delete any pre-existing device with name |partition_name_suffix| and |
| 735 | // also remove it from |mapped_devices_|. |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 736 | // In recovery, metadata might not be mounted, and |
| 737 | // UnmapPartitionOnDeviceMapper might fail. However, |
| 738 | // it is unusual that system_other has already been mapped. Hence, just skip. |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 739 | TEST_AND_RETURN_FALSE(UnmapPartitionOnDeviceMapper(partition_name_suffix)); |
| 740 | // Use CreateLogicalPartition directly to avoid mapping with existing |
| 741 | // snapshots. |
| 742 | CreateLogicalPartitionParams params = { |
| 743 | .block_device = source_super_device, |
| 744 | .metadata_slot = source_slot, |
| 745 | .partition_name = partition_name_suffix, |
| 746 | .force_writable = true, |
| 747 | .timeout_ms = kMapTimeout, |
| 748 | }; |
| 749 | TEST_AND_RETURN_FALSE(CreateLogicalPartition(params, path)); |
| 750 | *should_unmap = true; |
| 751 | return true; |
| 752 | } |
| 753 | |
| 754 | bool DynamicPartitionControlAndroid::EraseSystemOtherAvbFooter( |
| 755 | uint32_t source_slot, uint32_t target_slot) { |
| 756 | LOG(INFO) << "Erasing AVB footer of system_other partition before update."; |
| 757 | |
| 758 | const std::string target_suffix = SlotSuffixForSlotNumber(target_slot); |
| 759 | const std::string partition_name_suffix = "system" + target_suffix; |
| 760 | |
| 761 | std::string path; |
| 762 | bool should_unmap = false; |
| 763 | |
| 764 | TEST_AND_RETURN_FALSE(GetSystemOtherPath( |
| 765 | source_slot, target_slot, partition_name_suffix, &path, &should_unmap)); |
| 766 | |
| 767 | if (path.empty()) { |
| 768 | return true; |
| 769 | } |
| 770 | |
| 771 | bool ret = AvbFooterEraser(path).Erase(); |
| 772 | |
| 773 | // Delete |partition_name_suffix| from device mapper and from |
| 774 | // |mapped_devices_| again so that it does not interfere with update process. |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 775 | // In recovery, metadata might not be mounted, and |
| 776 | // UnmapPartitionOnDeviceMapper might fail. However, DestroyLogicalPartition |
| 777 | // should be called. If DestroyLogicalPartition does fail, it is still okay |
| 778 | // to skip the error here and let Prepare*() fail later. |
Yifan Hong | 2969290 | 2020-03-26 12:47:05 -0700 | [diff] [blame] | 779 | if (should_unmap) { |
| 780 | TEST_AND_RETURN_FALSE(UnmapPartitionOnDeviceMapper(partition_name_suffix)); |
| 781 | } |
| 782 | |
| 783 | return ret; |
| 784 | } |
| 785 | |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 786 | bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate( |
| 787 | uint32_t source_slot, |
| 788 | uint32_t target_slot, |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 789 | const DeltaArchiveManifest& manifest, |
| 790 | bool delete_source) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 791 | const std::string target_suffix = SlotSuffixForSlotNumber(target_slot); |
| 792 | |
| 793 | // Unmap all the target dynamic partitions because they would become |
| 794 | // inconsistent with the new metadata. |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 795 | for (const auto& group : manifest.dynamic_partition_metadata().groups()) { |
| 796 | for (const auto& partition_name : group.partition_names()) { |
| 797 | if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 798 | return false; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | std::string device_dir_str; |
| 804 | if (!GetDeviceDir(&device_dir_str)) { |
| 805 | return false; |
| 806 | } |
| 807 | base::FilePath device_dir(device_dir_str); |
| 808 | auto source_device = |
Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 809 | device_dir.Append(GetSuperPartitionName(source_slot)).value(); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 810 | |
| 811 | auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot); |
| 812 | if (builder == nullptr) { |
| 813 | LOG(ERROR) << "No metadata at " |
| 814 | << BootControlInterface::SlotName(source_slot); |
| 815 | return false; |
| 816 | } |
| 817 | |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 818 | if (delete_source) { |
| 819 | TEST_AND_RETURN_FALSE( |
| 820 | DeleteSourcePartitions(builder.get(), source_slot, manifest)); |
| 821 | } |
| 822 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 823 | if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 824 | return false; |
| 825 | } |
| 826 | |
| 827 | auto target_device = |
Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 828 | device_dir.Append(GetSuperPartitionName(target_slot)).value(); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 829 | return StoreMetadata(target_device, builder.get(), target_slot); |
| 830 | } |
| 831 | |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 832 | bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate( |
| 833 | uint32_t source_slot, |
| 834 | uint32_t target_slot, |
Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 835 | const DeltaArchiveManifest& manifest, |
| 836 | uint64_t* required_size) { |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 837 | TEST_AND_RETURN_FALSE(ExpectMetadataMounted()); |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 838 | if (!snapshot_->BeginUpdate()) { |
| 839 | LOG(ERROR) << "Cannot begin new update."; |
| 840 | return false; |
| 841 | } |
Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 842 | auto ret = snapshot_->CreateUpdateSnapshots(manifest); |
| 843 | if (!ret) { |
| 844 | LOG(ERROR) << "Cannot create update snapshots: " << ret.string(); |
| 845 | if (required_size != nullptr && |
Yifan Hong | 0850bca | 2020-01-16 15:14:07 -0800 | [diff] [blame] | 846 | ret.error_code() == Return::ErrorCode::NO_SPACE) { |
Yifan Hong | f033ecb | 2020-01-07 18:13:56 -0800 | [diff] [blame] | 847 | *required_size = ret.required_size(); |
| 848 | } |
Yifan Hong | 420db9b | 2019-07-23 20:50:33 -0700 | [diff] [blame] | 849 | return false; |
| 850 | } |
| 851 | return true; |
| 852 | } |
| 853 | |
Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 854 | std::string DynamicPartitionControlAndroid::GetSuperPartitionName( |
| 855 | uint32_t slot) { |
| 856 | return fs_mgr_get_super_partition_name(slot); |
| 857 | } |
| 858 | |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 859 | bool DynamicPartitionControlAndroid::UpdatePartitionMetadata( |
| 860 | MetadataBuilder* builder, |
| 861 | uint32_t target_slot, |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 862 | const DeltaArchiveManifest& manifest) { |
Yifan Hong | 8d6df9a | 2020-08-13 13:59:54 -0700 | [diff] [blame] | 863 | // Check preconditions. |
| 864 | CHECK(!GetVirtualAbFeatureFlag().IsEnabled() || IsRecovery()) |
| 865 | << "UpdatePartitionMetadata is called on a Virtual A/B device " |
| 866 | "but source partitions is not deleted. This is not allowed."; |
| 867 | |
Yifan Hong | a4e7da3 | 2019-09-30 18:25:03 -0700 | [diff] [blame] | 868 | // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over |
| 869 | // COW group needs to be deleted to ensure there are enough space to create |
| 870 | // target partitions. |
| 871 | builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName); |
| 872 | |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 873 | const std::string target_suffix = SlotSuffixForSlotNumber(target_slot); |
| 874 | DeleteGroupsWithSuffix(builder, target_suffix); |
| 875 | |
| 876 | uint64_t total_size = 0; |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 877 | for (const auto& group : manifest.dynamic_partition_metadata().groups()) { |
| 878 | total_size += group.size(); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | std::string expr; |
| 882 | uint64_t allocatable_space = builder->AllocatableSpace(); |
Yifan Hong | 8d6df9a | 2020-08-13 13:59:54 -0700 | [diff] [blame] | 883 | // On device retrofitting dynamic partitions, allocatable_space = super. |
| 884 | // On device launching dynamic partitions w/o VAB, |
| 885 | // allocatable_space = super / 2. |
| 886 | // On device launching dynamic partitions with VAB, allocatable_space = super. |
| 887 | if (!GetDynamicPartitionsFeatureFlag().IsRetrofit() && |
| 888 | !GetVirtualAbFeatureFlag().IsEnabled()) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 889 | allocatable_space /= 2; |
| 890 | expr = "half of "; |
| 891 | } |
| 892 | if (total_size > allocatable_space) { |
| 893 | LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix |
| 894 | << " (" << total_size << ") has exceeded " << expr |
| 895 | << "allocatable space for dynamic partitions " |
| 896 | << allocatable_space << "."; |
| 897 | return false; |
| 898 | } |
| 899 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 900 | // name of partition(e.g. "system") -> size in bytes |
| 901 | std::map<std::string, uint64_t> partition_sizes; |
| 902 | for (const auto& partition : manifest.partitions()) { |
| 903 | partition_sizes.emplace(partition.partition_name(), |
| 904 | partition.new_partition_info().size()); |
| 905 | } |
| 906 | |
| 907 | for (const auto& group : manifest.dynamic_partition_metadata().groups()) { |
| 908 | auto group_name_suffix = group.name() + target_suffix; |
| 909 | if (!builder->AddGroup(group_name_suffix, group.size())) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 910 | LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size " |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 911 | << group.size(); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 912 | return false; |
| 913 | } |
| 914 | LOG(INFO) << "Added group " << group_name_suffix << " with size " |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 915 | << group.size(); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 916 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 917 | for (const auto& partition_name : group.partition_names()) { |
| 918 | auto partition_sizes_it = partition_sizes.find(partition_name); |
| 919 | if (partition_sizes_it == partition_sizes.end()) { |
| 920 | // TODO(tbao): Support auto-filling partition info for framework-only |
| 921 | // OTA. |
| 922 | LOG(ERROR) << "dynamic_partition_metadata contains partition " |
| 923 | << partition_name << " but it is not part of the manifest. " |
| 924 | << "This is not supported."; |
| 925 | return false; |
| 926 | } |
| 927 | uint64_t partition_size = partition_sizes_it->second; |
| 928 | |
| 929 | auto partition_name_suffix = partition_name + target_suffix; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 930 | Partition* p = builder->AddPartition( |
| 931 | partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY); |
| 932 | if (!p) { |
| 933 | LOG(ERROR) << "Cannot add partition " << partition_name_suffix |
| 934 | << " to group " << group_name_suffix; |
| 935 | return false; |
| 936 | } |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 937 | if (!builder->ResizePartition(p, partition_size)) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 938 | LOG(ERROR) << "Cannot resize partition " << partition_name_suffix |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 939 | << " to size " << partition_size << ". Not enough space?"; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 940 | return false; |
| 941 | } |
| 942 | LOG(INFO) << "Added partition " << partition_name_suffix << " to group " |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame] | 943 | << group_name_suffix << " with size " << partition_size; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | |
| 947 | return true; |
| 948 | } |
| 949 | |
Yifan Hong | 7b3910a | 2020-03-24 17:47:32 -0700 | [diff] [blame] | 950 | bool DynamicPartitionControlAndroid::FinishUpdate(bool powerwash_required) { |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 951 | if (ExpectMetadataMounted()) { |
| 952 | if (snapshot_->GetUpdateState() == UpdateState::Initiated) { |
| 953 | LOG(INFO) << "Snapshot writes are done."; |
| 954 | return snapshot_->FinishedSnapshotWrites(powerwash_required); |
| 955 | } |
| 956 | } else { |
| 957 | LOG(INFO) << "Skip FinishedSnapshotWrites() because /metadata is not " |
| 958 | << "mounted"; |
Yifan Hong | f0f4a91 | 2019-09-26 17:51:33 -0700 | [diff] [blame] | 959 | } |
| 960 | return true; |
Yifan Hong | a33bca4 | 2019-09-03 20:29:45 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 963 | bool DynamicPartitionControlAndroid::GetPartitionDevice( |
| 964 | const std::string& partition_name, |
| 965 | uint32_t slot, |
| 966 | uint32_t current_slot, |
Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 967 | bool not_in_payload, |
| 968 | std::string* device, |
| 969 | bool* is_dynamic) { |
Kelvin Zhang | 66a9ebb | 2021-01-25 13:35:10 -0500 | [diff] [blame] | 970 | auto partition_dev = |
| 971 | GetPartitionDevice(partition_name, slot, current_slot, not_in_payload); |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 972 | if (!partition_dev.has_value()) { |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 973 | return false; |
| 974 | } |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 975 | if (device) { |
| 976 | *device = std::move(partition_dev->rw_device_path); |
| 977 | } |
| 978 | if (is_dynamic) { |
| 979 | *is_dynamic = partition_dev->is_dynamic; |
| 980 | } |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 981 | return true; |
| 982 | } |
| 983 | |
Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 984 | bool DynamicPartitionControlAndroid::GetPartitionDevice( |
| 985 | const std::string& partition_name, |
| 986 | uint32_t slot, |
| 987 | uint32_t current_slot, |
| 988 | std::string* device) { |
| 989 | return GetPartitionDevice( |
| 990 | partition_name, slot, current_slot, false, device, nullptr); |
| 991 | } |
| 992 | |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 993 | static std::string GetStaticDevicePath( |
| 994 | const base::FilePath& device_dir, |
| 995 | const std::string& partition_name_suffixed) { |
| 996 | base::FilePath path = device_dir.Append(partition_name_suffixed); |
| 997 | return path.value(); |
| 998 | } |
| 999 | |
| 1000 | std::optional<PartitionDevice> |
| 1001 | DynamicPartitionControlAndroid::GetPartitionDevice( |
| 1002 | const std::string& partition_name, |
| 1003 | uint32_t slot, |
| 1004 | uint32_t current_slot, |
| 1005 | bool not_in_payload) { |
| 1006 | std::string device_dir_str; |
| 1007 | if (!GetDeviceDir(&device_dir_str)) { |
| 1008 | LOG(ERROR) << "Failed to GetDeviceDir()"; |
| 1009 | return {}; |
| 1010 | } |
| 1011 | const base::FilePath device_dir(device_dir_str); |
| 1012 | // When VABC is enabled, we can't get device path for dynamic partitions in |
| 1013 | // target slot. |
| 1014 | const auto& partition_name_suffix = |
| 1015 | partition_name + SlotSuffixForSlotNumber(slot); |
Kelvin Zhang | 0618835 | 2021-02-10 13:21:47 -0500 | [diff] [blame] | 1016 | if (UpdateUsesSnapshotCompression() && IsDynamicPartition(partition_name) && |
| 1017 | slot != current_slot) { |
Kelvin Zhang | 91ad662 | 2021-03-01 13:46:17 -0500 | [diff] [blame] | 1018 | return { |
| 1019 | {.mountable_device_path = base::FilePath{std::string{VABC_DEVICE_DIR}} |
| 1020 | .Append(partition_name_suffix) |
| 1021 | .value(), |
| 1022 | .is_dynamic = true}}; |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | // When looking up target partition devices, treat them as static if the |
| 1026 | // current payload doesn't encode them as dynamic partitions. This may happen |
| 1027 | // when applying a retrofit update on top of a dynamic-partitions-enabled |
| 1028 | // build. |
| 1029 | std::string device; |
| 1030 | if (GetDynamicPartitionsFeatureFlag().IsEnabled() && |
| 1031 | (slot == current_slot || is_target_dynamic_)) { |
| 1032 | switch (GetDynamicPartitionDevice(device_dir, |
| 1033 | partition_name_suffix, |
| 1034 | slot, |
| 1035 | current_slot, |
| 1036 | not_in_payload, |
| 1037 | &device)) { |
| 1038 | case DynamicPartitionDeviceStatus::SUCCESS: |
| 1039 | return {{.rw_device_path = device, |
| 1040 | .mountable_device_path = device, |
| 1041 | .is_dynamic = true}}; |
| 1042 | |
| 1043 | case DynamicPartitionDeviceStatus::TRY_STATIC: |
| 1044 | break; |
| 1045 | case DynamicPartitionDeviceStatus::ERROR: // fallthrough |
| 1046 | default: |
| 1047 | return {}; |
| 1048 | } |
| 1049 | } |
| 1050 | // Try static partitions. |
| 1051 | auto static_path = GetStaticDevicePath(device_dir, partition_name_suffix); |
| 1052 | if (!DeviceExists(static_path)) { |
| 1053 | LOG(ERROR) << "Device file " << static_path << " does not exist."; |
| 1054 | return {}; |
| 1055 | } |
| 1056 | |
| 1057 | return {{.rw_device_path = static_path, |
| 1058 | .mountable_device_path = static_path, |
| 1059 | .is_dynamic = false}}; |
| 1060 | } |
| 1061 | |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 1062 | bool DynamicPartitionControlAndroid::IsSuperBlockDevice( |
| 1063 | const base::FilePath& device_dir, |
| 1064 | uint32_t current_slot, |
| 1065 | const std::string& partition_name_suffix) { |
| 1066 | std::string source_device = |
| 1067 | device_dir.Append(GetSuperPartitionName(current_slot)).value(); |
| 1068 | auto source_metadata = LoadMetadataBuilder(source_device, current_slot); |
| 1069 | return source_metadata->HasBlockDevice(partition_name_suffix); |
| 1070 | } |
| 1071 | |
| 1072 | DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus |
| 1073 | DynamicPartitionControlAndroid::GetDynamicPartitionDevice( |
| 1074 | const base::FilePath& device_dir, |
| 1075 | const std::string& partition_name_suffix, |
| 1076 | uint32_t slot, |
| 1077 | uint32_t current_slot, |
Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 1078 | bool not_in_payload, |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 1079 | std::string* device) { |
| 1080 | std::string super_device = |
| 1081 | device_dir.Append(GetSuperPartitionName(slot)).value(); |
| 1082 | |
| 1083 | auto builder = LoadMetadataBuilder(super_device, slot); |
| 1084 | if (builder == nullptr) { |
| 1085 | LOG(ERROR) << "No metadata in slot " |
| 1086 | << BootControlInterface::SlotName(slot); |
| 1087 | return DynamicPartitionDeviceStatus::ERROR; |
| 1088 | } |
| 1089 | if (builder->FindPartition(partition_name_suffix) == nullptr) { |
| 1090 | LOG(INFO) << partition_name_suffix |
| 1091 | << " is not in super partition metadata."; |
| 1092 | |
| 1093 | if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) { |
| 1094 | LOG(ERROR) << "The static partition " << partition_name_suffix |
| 1095 | << " is a block device for current metadata." |
| 1096 | << "It cannot be used as a logical partition."; |
| 1097 | return DynamicPartitionDeviceStatus::ERROR; |
| 1098 | } |
| 1099 | |
| 1100 | return DynamicPartitionDeviceStatus::TRY_STATIC; |
| 1101 | } |
| 1102 | |
| 1103 | if (slot == current_slot) { |
| 1104 | if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) { |
| 1105 | LOG(WARNING) << partition_name_suffix << " is at current slot but it is " |
| 1106 | << "not mapped. Now try to map it."; |
| 1107 | } else { |
| 1108 | if (GetDmDevicePathByName(partition_name_suffix, device)) { |
| 1109 | LOG(INFO) << partition_name_suffix |
| 1110 | << " is mapped on device mapper: " << *device; |
| 1111 | return DynamicPartitionDeviceStatus::SUCCESS; |
| 1112 | } |
| 1113 | LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown."; |
| 1114 | return DynamicPartitionDeviceStatus::ERROR; |
| 1115 | } |
| 1116 | } |
| 1117 | |
Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 1118 | bool force_writable = (slot != current_slot) && !not_in_payload; |
Yifan Hong | 3a1a561 | 2019-11-05 16:34:32 -0800 | [diff] [blame] | 1119 | if (MapPartitionOnDeviceMapper( |
| 1120 | super_device, partition_name_suffix, slot, force_writable, device)) { |
| 1121 | return DynamicPartitionDeviceStatus::SUCCESS; |
| 1122 | } |
| 1123 | return DynamicPartitionDeviceStatus::ERROR; |
| 1124 | } |
| 1125 | |
Yifan Hong | 6eec995 | 2019-12-04 13:12:01 -0800 | [diff] [blame] | 1126 | void DynamicPartitionControlAndroid::set_fake_mapped_devices( |
| 1127 | const std::set<std::string>& fake) { |
| 1128 | mapped_devices_ = fake; |
| 1129 | } |
| 1130 | |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 1131 | bool DynamicPartitionControlAndroid::IsRecovery() { |
Kelvin Zhang | a22ef55 | 2020-10-12 19:03:52 -0400 | [diff] [blame] | 1132 | return constants::kIsRecovery; |
Yifan Hong | bae2784 | 2019-10-24 16:56:12 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static bool IsIncrementalUpdate(const DeltaArchiveManifest& manifest) { |
| 1136 | const auto& partitions = manifest.partitions(); |
| 1137 | return std::any_of(partitions.begin(), partitions.end(), [](const auto& p) { |
| 1138 | return p.has_old_partition_info(); |
| 1139 | }); |
| 1140 | } |
| 1141 | |
| 1142 | bool DynamicPartitionControlAndroid::DeleteSourcePartitions( |
| 1143 | MetadataBuilder* builder, |
| 1144 | uint32_t source_slot, |
| 1145 | const DeltaArchiveManifest& manifest) { |
| 1146 | TEST_AND_RETURN_FALSE(IsRecovery()); |
| 1147 | |
| 1148 | if (IsIncrementalUpdate(manifest)) { |
| 1149 | LOG(ERROR) << "Cannot sideload incremental OTA because snapshots cannot " |
| 1150 | << "be created."; |
| 1151 | if (GetVirtualAbFeatureFlag().IsLaunch()) { |
| 1152 | LOG(ERROR) << "Sideloading incremental updates on devices launches " |
| 1153 | << " Virtual A/B is not supported."; |
| 1154 | } |
| 1155 | return false; |
| 1156 | } |
| 1157 | |
| 1158 | LOG(INFO) << "Will overwrite existing partitions. Slot " |
| 1159 | << BootControlInterface::SlotName(source_slot) |
| 1160 | << "may be unbootable until update finishes!"; |
| 1161 | const std::string source_suffix = SlotSuffixForSlotNumber(source_slot); |
| 1162 | DeleteGroupsWithSuffix(builder, source_suffix); |
| 1163 | |
| 1164 | return true; |
| 1165 | } |
| 1166 | |
Yifan Hong | 9096550 | 2020-02-19 15:22:47 -0800 | [diff] [blame] | 1167 | std::unique_ptr<AbstractAction> |
| 1168 | DynamicPartitionControlAndroid::GetCleanupPreviousUpdateAction( |
| 1169 | BootControlInterface* boot_control, |
| 1170 | PrefsInterface* prefs, |
| 1171 | CleanupPreviousUpdateActionDelegateInterface* delegate) { |
| 1172 | if (!GetVirtualAbFeatureFlag().IsEnabled()) { |
| 1173 | return std::make_unique<NoOpAction>(); |
| 1174 | } |
| 1175 | return std::make_unique<CleanupPreviousUpdateAction>( |
| 1176 | prefs, boot_control, snapshot_.get(), delegate); |
| 1177 | } |
| 1178 | |
Yifan Hong | 6a6d0f1 | 2020-03-11 13:20:52 -0700 | [diff] [blame] | 1179 | bool DynamicPartitionControlAndroid::ResetUpdate(PrefsInterface* prefs) { |
| 1180 | if (!GetVirtualAbFeatureFlag().IsEnabled()) { |
| 1181 | return true; |
| 1182 | } |
| 1183 | |
| 1184 | LOG(INFO) << __func__ << " resetting update state and deleting snapshots."; |
| 1185 | TEST_AND_RETURN_FALSE(prefs != nullptr); |
| 1186 | |
| 1187 | // If the device has already booted into the target slot, |
| 1188 | // ResetUpdateProgress may pass but CancelUpdate fails. |
| 1189 | // This is expected. A scheduled CleanupPreviousUpdateAction should free |
| 1190 | // space when it is done. |
| 1191 | TEST_AND_RETURN_FALSE(DeltaPerformer::ResetUpdateProgress( |
| 1192 | prefs, false /* quick */, false /* skip dynamic partitions metadata */)); |
| 1193 | |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 1194 | if (ExpectMetadataMounted()) { |
| 1195 | TEST_AND_RETURN_FALSE(snapshot_->CancelUpdate()); |
| 1196 | } else { |
| 1197 | LOG(INFO) << "Skip cancelling update in ResetUpdate because /metadata is " |
| 1198 | << "not mounted"; |
| 1199 | } |
Yifan Hong | 6a6d0f1 | 2020-03-11 13:20:52 -0700 | [diff] [blame] | 1200 | |
| 1201 | return true; |
| 1202 | } |
| 1203 | |
Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 1204 | bool DynamicPartitionControlAndroid::ListDynamicPartitionsForSlot( |
Tianjie | 3a55fc2 | 2021-02-13 16:02:22 -0800 | [diff] [blame] | 1205 | uint32_t slot, |
| 1206 | uint32_t current_slot, |
| 1207 | std::vector<std::string>* partitions) { |
| 1208 | bool slot_enables_dynamic_partitions = |
| 1209 | GetDynamicPartitionsFeatureFlag().IsEnabled(); |
| 1210 | // Check if the target slot has dynamic partitions, this may happen when |
| 1211 | // applying a retrofit package. |
| 1212 | if (slot != current_slot) { |
| 1213 | slot_enables_dynamic_partitions = |
| 1214 | slot_enables_dynamic_partitions && is_target_dynamic_; |
| 1215 | } |
| 1216 | |
| 1217 | if (!slot_enables_dynamic_partitions) { |
| 1218 | LOG(INFO) << "Dynamic partition is not enabled for slot " << slot; |
| 1219 | return true; |
Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | std::string device_dir_str; |
| 1223 | TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str)); |
| 1224 | base::FilePath device_dir(device_dir_str); |
Tianjie | 3a55fc2 | 2021-02-13 16:02:22 -0800 | [diff] [blame] | 1225 | auto super_device = device_dir.Append(GetSuperPartitionName(slot)).value(); |
| 1226 | auto builder = LoadMetadataBuilder(super_device, slot); |
Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 1227 | TEST_AND_RETURN_FALSE(builder != nullptr); |
| 1228 | |
| 1229 | std::vector<std::string> result; |
Tianjie | 3a55fc2 | 2021-02-13 16:02:22 -0800 | [diff] [blame] | 1230 | auto suffix = SlotSuffixForSlotNumber(slot); |
Tianjie | 99d570d | 2020-06-04 14:57:19 -0700 | [diff] [blame] | 1231 | for (const auto& group : builder->ListGroups()) { |
| 1232 | for (const auto& partition : builder->ListPartitionsInGroup(group)) { |
| 1233 | std::string_view partition_name = partition->name(); |
| 1234 | if (!android::base::ConsumeSuffix(&partition_name, suffix)) { |
| 1235 | continue; |
| 1236 | } |
| 1237 | result.emplace_back(partition_name); |
| 1238 | } |
| 1239 | } |
| 1240 | *partitions = std::move(result); |
| 1241 | return true; |
| 1242 | } |
| 1243 | |
Tianjie | 24f9609 | 2020-06-30 12:26:25 -0700 | [diff] [blame] | 1244 | bool DynamicPartitionControlAndroid::VerifyExtentsForUntouchedPartitions( |
| 1245 | uint32_t source_slot, |
| 1246 | uint32_t target_slot, |
| 1247 | const std::vector<std::string>& partitions) { |
| 1248 | std::string device_dir_str; |
| 1249 | TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str)); |
| 1250 | base::FilePath device_dir(device_dir_str); |
| 1251 | |
| 1252 | auto source_super_device = |
| 1253 | device_dir.Append(GetSuperPartitionName(source_slot)).value(); |
| 1254 | auto source_builder = LoadMetadataBuilder(source_super_device, source_slot); |
| 1255 | TEST_AND_RETURN_FALSE(source_builder != nullptr); |
| 1256 | |
| 1257 | auto target_super_device = |
| 1258 | device_dir.Append(GetSuperPartitionName(target_slot)).value(); |
| 1259 | auto target_builder = LoadMetadataBuilder(target_super_device, target_slot); |
| 1260 | TEST_AND_RETURN_FALSE(target_builder != nullptr); |
| 1261 | |
| 1262 | return MetadataBuilder::VerifyExtentsAgainstSourceMetadata( |
| 1263 | *source_builder, source_slot, *target_builder, target_slot, partitions); |
| 1264 | } |
| 1265 | |
Yifan Hong | 4d7c5eb | 2020-04-03 11:31:50 -0700 | [diff] [blame] | 1266 | bool DynamicPartitionControlAndroid::ExpectMetadataMounted() { |
| 1267 | // No need to mount metadata for non-Virtual A/B devices. |
| 1268 | if (!GetVirtualAbFeatureFlag().IsEnabled()) { |
| 1269 | return false; |
| 1270 | } |
| 1271 | // Intentionally not checking |metadata_device_| in Android mode. |
| 1272 | // /metadata should always be mounted in Android mode. If it isn't, let caller |
| 1273 | // fails when calling into SnapshotManager. |
| 1274 | if (!IsRecovery()) { |
| 1275 | return true; |
| 1276 | } |
| 1277 | // In recovery mode, explicitly check |metadata_device_|. |
| 1278 | return metadata_device_ != nullptr; |
| 1279 | } |
| 1280 | |
| 1281 | bool DynamicPartitionControlAndroid::EnsureMetadataMounted() { |
| 1282 | // No need to mount metadata for non-Virtual A/B devices. |
| 1283 | if (!GetVirtualAbFeatureFlag().IsEnabled()) { |
| 1284 | return true; |
| 1285 | } |
| 1286 | |
| 1287 | if (metadata_device_ == nullptr) { |
| 1288 | metadata_device_ = snapshot_->EnsureMetadataMounted(); |
| 1289 | } |
| 1290 | return metadata_device_ != nullptr; |
| 1291 | } |
| 1292 | |
Kelvin Zhang | 3461852 | 2020-09-28 09:21:02 -0400 | [diff] [blame] | 1293 | std::unique_ptr<android::snapshot::ISnapshotWriter> |
| 1294 | DynamicPartitionControlAndroid::OpenCowWriter( |
| 1295 | const std::string& partition_name, |
| 1296 | const std::optional<std::string>& source_path, |
| 1297 | bool is_append) { |
| 1298 | auto suffix = SlotSuffixForSlotNumber(target_slot_); |
| 1299 | |
Kelvin Zhang | 9d87d6d | 2020-10-23 17:03:59 -0400 | [diff] [blame] | 1300 | auto super_device = GetSuperDevice(); |
| 1301 | if (!super_device.has_value()) { |
Kelvin Zhang | 3461852 | 2020-09-28 09:21:02 -0400 | [diff] [blame] | 1302 | return nullptr; |
| 1303 | } |
Kelvin Zhang | 3461852 | 2020-09-28 09:21:02 -0400 | [diff] [blame] | 1304 | CreateLogicalPartitionParams params = { |
Kelvin Zhang | 9d87d6d | 2020-10-23 17:03:59 -0400 | [diff] [blame] | 1305 | .block_device = super_device->value(), |
Kelvin Zhang | 3461852 | 2020-09-28 09:21:02 -0400 | [diff] [blame] | 1306 | .metadata_slot = target_slot_, |
| 1307 | .partition_name = partition_name + suffix, |
| 1308 | .force_writable = true, |
Kelvin Zhang | 877ddbe | 2020-11-09 13:37:56 -0500 | [diff] [blame] | 1309 | .timeout_ms = kMapSnapshotTimeout}; |
Kelvin Zhang | 3461852 | 2020-09-28 09:21:02 -0400 | [diff] [blame] | 1310 | // TODO(zhangkelvin) Open an APPEND mode CowWriter once there's an API to do |
| 1311 | // it. |
| 1312 | return snapshot_->OpenSnapshotWriter(params, std::move(source_path)); |
Kelvin Zhang | 877ddbe | 2020-11-09 13:37:56 -0500 | [diff] [blame] | 1313 | } // namespace chromeos_update_engine |
Kelvin Zhang | 3461852 | 2020-09-28 09:21:02 -0400 | [diff] [blame] | 1314 | |
Kelvin Zhang | c82511c | 2020-11-06 16:01:24 -0500 | [diff] [blame] | 1315 | FileDescriptorPtr DynamicPartitionControlAndroid::OpenCowReader( |
| 1316 | const std::string& unsuffixed_partition_name, |
| 1317 | const std::optional<std::string>& source_path, |
| 1318 | bool is_append) { |
| 1319 | auto cow_writer = |
| 1320 | OpenCowWriter(unsuffixed_partition_name, source_path, is_append); |
| 1321 | if (cow_writer == nullptr) { |
| 1322 | return nullptr; |
| 1323 | } |
Kelvin Zhang | ec205cf | 2020-09-28 13:23:40 -0400 | [diff] [blame] | 1324 | cow_writer->InitializeAppend(kEndOfInstallLabel); |
Kelvin Zhang | c82511c | 2020-11-06 16:01:24 -0500 | [diff] [blame] | 1325 | return cow_writer->OpenReader(); |
| 1326 | } |
| 1327 | |
Kelvin Zhang | 9d87d6d | 2020-10-23 17:03:59 -0400 | [diff] [blame] | 1328 | std::optional<base::FilePath> DynamicPartitionControlAndroid::GetSuperDevice() { |
| 1329 | std::string device_dir_str; |
| 1330 | if (!GetDeviceDir(&device_dir_str)) { |
| 1331 | LOG(ERROR) << "Failed to get device dir!"; |
| 1332 | return {}; |
| 1333 | } |
| 1334 | base::FilePath device_dir(device_dir_str); |
| 1335 | auto super_device = device_dir.Append(GetSuperPartitionName(target_slot_)); |
| 1336 | return super_device; |
| 1337 | } |
| 1338 | |
| 1339 | bool DynamicPartitionControlAndroid::MapAllPartitions() { |
Kelvin Zhang | 877ddbe | 2020-11-09 13:37:56 -0500 | [diff] [blame] | 1340 | return snapshot_->MapAllSnapshots(kMapSnapshotTimeout); |
Kelvin Zhang | 9d87d6d | 2020-10-23 17:03:59 -0400 | [diff] [blame] | 1341 | } |
| 1342 | |
Kelvin Zhang | eb9de16 | 2020-11-16 15:47:28 -0500 | [diff] [blame] | 1343 | bool DynamicPartitionControlAndroid::IsDynamicPartition( |
| 1344 | const std::string& partition_name) { |
| 1345 | if (dynamic_partition_list_.empty() && |
| 1346 | GetDynamicPartitionsFeatureFlag().IsEnabled()) { |
Tianjie | 3a55fc2 | 2021-02-13 16:02:22 -0800 | [diff] [blame] | 1347 | // Use the DAP config of the target slot. |
| 1348 | CHECK(ListDynamicPartitionsForSlot( |
| 1349 | target_slot_, source_slot_, &dynamic_partition_list_)); |
Kelvin Zhang | eb9de16 | 2020-11-16 15:47:28 -0500 | [diff] [blame] | 1350 | } |
| 1351 | return std::find(dynamic_partition_list_.begin(), |
| 1352 | dynamic_partition_list_.end(), |
| 1353 | partition_name) != dynamic_partition_list_.end(); |
| 1354 | } |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 1355 | |
Yifan Hong | b0cbd39 | 2021-02-04 11:11:45 -0800 | [diff] [blame] | 1356 | bool DynamicPartitionControlAndroid::UpdateUsesSnapshotCompression() { |
| 1357 | return snapshot_->UpdateUsesCompression(); |
| 1358 | } |
| 1359 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 1360 | } // namespace chromeos_update_engine |