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