blob: 27d1d54162d8c7e2593df11ef08d3b31bc80b028 [file] [log] [blame]
Yifan Hong537802d2018-08-15 13:15:42 -07001//
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 Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/aosp/dynamic_partition_control_android.h"
Yifan Hong537802d2018-08-15 13:15:42 -070018
Kelvin Zhang91d95fa2020-11-05 13:52:00 -050019#include <algorithm>
Yifan Hong420db9b2019-07-23 20:50:33 -070020#include <chrono> // NOLINT(build/c++11) - using libsnapshot / liblp API
Kelvin Zhang34618522020-09-28 09:21:02 -040021#include <cstdint>
Yifan Hong13d41cb2019-09-16 13:18:22 -070022#include <map>
Yifan Hong537802d2018-08-15 13:15:42 -070023#include <memory>
24#include <set>
25#include <string>
Tianjie99d570d2020-06-04 14:57:19 -070026#include <string_view>
27#include <utility>
Yifan Hong012508e2019-07-22 18:30:40 -070028#include <vector>
Yifan Hong537802d2018-08-15 13:15:42 -070029
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 Hong012508e2019-07-22 18:30:40 -070034#include <base/strings/string_util.h>
Yifan Hong3a1293a2021-04-16 13:21:20 -070035#include <base/strings/stringprintf.h>
Yifan Hong537802d2018-08-15 13:15:42 -070036#include <bootloader_message/bootloader_message.h>
Yifan Hong012508e2019-07-22 18:30:40 -070037#include <fs_mgr.h>
Yifan Hong537802d2018-08-15 13:15:42 -070038#include <fs_mgr_dm_linear.h>
Yifan Hong3a1a5612019-11-05 16:34:32 -080039#include <fs_mgr_overlayfs.h>
Yifan Hong29692902020-03-26 12:47:05 -070040#include <libavb/libavb.h>
Yifan Hong3a1a5612019-11-05 16:34:32 -080041#include <libdm/dm.h>
Kelvin Zhang34618522020-09-28 09:21:02 -040042#include <liblp/liblp.h>
43#include <libsnapshot/cow_writer.h>
Yifan Hong420db9b2019-07-23 20:50:33 -070044#include <libsnapshot/snapshot.h>
Yifan Hongf9cb4492020-04-15 13:00:20 -070045#include <libsnapshot/snapshot_stub.h>
Yifan Hong537802d2018-08-15 13:15:42 -070046
Amin Hassaniec7bc112020-10-29 16:47:58 -070047#include "update_engine/aosp/cleanup_previous_update_action.h"
48#include "update_engine/aosp/dynamic_partition_utils.h"
Yifan Hong537802d2018-08-15 13:15:42 -070049#include "update_engine/common/boot_control_interface.h"
Kelvin Zhangb9a5f612021-01-22 14:34:05 -050050#include "update_engine/common/dynamic_partition_control_interface.h"
51#include "update_engine/common/platform_constants.h"
Yifan Hong537802d2018-08-15 13:15:42 -070052#include "update_engine/common/utils.h"
Kelvin Zhang21a49912021-03-12 14:28:33 -050053#include "update_engine/payload_consumer/cow_writer_file_descriptor.h"
Yifan Hong6a6d0f12020-03-11 13:20:52 -070054#include "update_engine/payload_consumer/delta_performer.h"
Yifan Hong537802d2018-08-15 13:15:42 -070055
56using android::base::GetBoolProperty;
Yifan Hong29692902020-03-26 12:47:05 -070057using android::base::GetProperty;
Yifan Hong537802d2018-08-15 13:15:42 -070058using android::base::Join;
59using android::dm::DeviceMapper;
60using android::dm::DmDeviceState;
61using android::fs_mgr::CreateLogicalPartition;
David Andersonbb90dfb2019-08-13 14:14:56 -070062using android::fs_mgr::CreateLogicalPartitionParams;
Yifan Hong537802d2018-08-15 13:15:42 -070063using android::fs_mgr::DestroyLogicalPartition;
Yifan Hong29692902020-03-26 12:47:05 -070064using android::fs_mgr::Fstab;
Yifan Hong537802d2018-08-15 13:15:42 -070065using android::fs_mgr::MetadataBuilder;
Yifan Hong012508e2019-07-22 18:30:40 -070066using android::fs_mgr::Partition;
Yifan Hong6e706b12018-11-09 16:50:51 -080067using android::fs_mgr::PartitionOpener;
Yifan Hong012508e2019-07-22 18:30:40 -070068using android::fs_mgr::SlotSuffixForSlotNumber;
Yifan Hongf5261562020-03-10 10:28:10 -070069using android::snapshot::OptimizeSourceCopyOperation;
Yifan Hong0850bca2020-01-16 15:14:07 -080070using android::snapshot::Return;
Yifan Hongf033ecb2020-01-07 18:13:56 -080071using android::snapshot::SnapshotManager;
Yifan Hongf9cb4492020-04-15 13:00:20 -070072using android::snapshot::SnapshotManagerStub;
Yifan Hong2257ee12020-01-13 18:33:00 -080073using android::snapshot::UpdateState;
Yifan Hong3a1293a2021-04-16 13:21:20 -070074using base::StringPrintf;
Yifan Hong537802d2018-08-15 13:15:42 -070075
76namespace chromeos_update_engine {
77
Yifan Hong6e706b12018-11-09 16:50:51 -080078constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
79constexpr char kRetrfoitDynamicPartitions[] =
80 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong413d5722019-07-23 14:21:09 -070081constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled";
82constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit";
Kelvin Zhangda1b3142020-09-24 17:09:02 -040083constexpr char kVirtualAbCompressionEnabled[] =
84 "ro.virtual_ab.compression.enabled";
Kelvin Zhang1c4b9812022-04-06 17:29:00 -070085constexpr auto&& kVirtualAbCompressionXorEnabled =
86 "ro.virtual_ab.compression.xor.enabled";
Kelvin Zhangda1b3142020-09-24 17:09:02 -040087
88// Currently, android doesn't have a retrofit prop for VAB Compression. However,
89// struct FeatureFlag forces us to determine if a feature is 'retrofit'. So this
90// is here just to simplify code. Replace it with real retrofit prop name once
91// there is one.
92constexpr char kVirtualAbCompressionRetrofit[] = "";
Yifan Hong29692902020-03-26 12:47:05 -070093constexpr char kPostinstallFstabPrefix[] = "ro.postinstall.fstab.prefix";
Yifan Hong420db9b2019-07-23 20:50:33 -070094// Map timeout for dynamic partitions.
95constexpr std::chrono::milliseconds kMapTimeout{1000};
96// Map timeout for dynamic partitions with snapshots. Since several devices
97// needs to be mapped, this timeout is longer than |kMapTimeout|.
Kelvin Zhangcf3280b2021-09-01 19:36:01 -070098constexpr std::chrono::milliseconds kMapSnapshotTimeout{10000};
Yifan Hong420db9b2019-07-23 20:50:33 -070099
Yifan Hong537802d2018-08-15 13:15:42 -0700100DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
Yifan Hongbae27842019-10-24 16:56:12 -0700101 Cleanup();
Yifan Hong537802d2018-08-15 13:15:42 -0700102}
103
Yifan Hong186bb682019-07-23 14:04:39 -0700104static FeatureFlag GetFeatureFlag(const char* enable_prop,
105 const char* retrofit_prop) {
Kelvin Zhangda1b3142020-09-24 17:09:02 -0400106 // Default retrofit to false if retrofit_prop is empty.
107 bool retrofit = retrofit_prop && retrofit_prop[0] != '\0' &&
108 GetBoolProperty(retrofit_prop, false);
Yifan Hong186bb682019-07-23 14:04:39 -0700109 bool enabled = GetBoolProperty(enable_prop, false);
110 if (retrofit && !enabled) {
111 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
112 << " is not. These sysprops are inconsistent. Assume that "
113 << enable_prop << " is true from now on.";
114 }
115 if (retrofit) {
116 return FeatureFlag(FeatureFlag::Value::RETROFIT);
117 }
118 if (enabled) {
119 return FeatureFlag(FeatureFlag::Value::LAUNCH);
120 }
121 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -0700122}
123
Kelvin Zhangebd115e2021-03-08 16:10:25 -0500124DynamicPartitionControlAndroid::DynamicPartitionControlAndroid(
125 uint32_t source_slot)
Yifan Hongb38e1af2019-10-17 14:59:22 -0700126 : dynamic_partitions_(
127 GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions)),
Kelvin Zhangda1b3142020-09-24 17:09:02 -0400128 virtual_ab_(GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit)),
129 virtual_ab_compression_(GetFeatureFlag(kVirtualAbCompressionEnabled,
Kelvin Zhangebd115e2021-03-08 16:10:25 -0500130 kVirtualAbCompressionRetrofit)),
Kelvin Zhang1c4b9812022-04-06 17:29:00 -0700131 virtual_ab_compression_xor_(
132 GetFeatureFlag(kVirtualAbCompressionXorEnabled, "")),
Kelvin Zhangebd115e2021-03-08 16:10:25 -0500133 source_slot_(source_slot) {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700134 if (GetVirtualAbFeatureFlag().IsEnabled()) {
Yifan Hongf033ecb2020-01-07 18:13:56 -0800135 snapshot_ = SnapshotManager::New();
Yifan Hongf9cb4492020-04-15 13:00:20 -0700136 } else {
137 snapshot_ = SnapshotManagerStub::New();
Yifan Hongb38e1af2019-10-17 14:59:22 -0700138 }
Yifan Hongf9cb4492020-04-15 13:00:20 -0700139 CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
Yifan Hongb38e1af2019-10-17 14:59:22 -0700140}
141
Yifan Hong186bb682019-07-23 14:04:39 -0700142FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700143 return dynamic_partitions_;
Yifan Hong6e706b12018-11-09 16:50:51 -0800144}
145
Yifan Hong413d5722019-07-23 14:21:09 -0700146FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700147 return virtual_ab_;
Yifan Hong413d5722019-07-23 14:21:09 -0700148}
149
Kelvin Zhangda1b3142020-09-24 17:09:02 -0400150FeatureFlag
151DynamicPartitionControlAndroid::GetVirtualAbCompressionFeatureFlag() {
Kelvin Zhangb9a5f612021-01-22 14:34:05 -0500152 if constexpr (constants::kIsRecovery) {
153 // Don't attempt VABC in recovery
154 return FeatureFlag(FeatureFlag::Value::NONE);
155 }
Kelvin Zhangda1b3142020-09-24 17:09:02 -0400156 return virtual_ab_compression_;
157}
158
Kelvin Zhang1c4b9812022-04-06 17:29:00 -0700159FeatureFlag
160DynamicPartitionControlAndroid::GetVirtualAbCompressionXorFeatureFlag() {
161 return virtual_ab_compression_xor_;
162}
163
Yifan Hongf5261562020-03-10 10:28:10 -0700164bool DynamicPartitionControlAndroid::OptimizeOperation(
165 const std::string& partition_name,
166 const InstallOperation& operation,
167 InstallOperation* optimized) {
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000168 switch (operation.type()) {
169 case InstallOperation::SOURCE_COPY:
170 return target_supports_snapshot_ &&
171 GetVirtualAbFeatureFlag().IsEnabled() &&
Yifan Hong6eec9952019-12-04 13:12:01 -0800172 mapped_devices_.count(partition_name +
173 SlotSuffixForSlotNumber(target_slot_)) > 0 &&
Yifan Hongf5261562020-03-10 10:28:10 -0700174 OptimizeSourceCopyOperation(operation, optimized);
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000175 break;
176 default:
177 break;
178 }
Alessio Balsini14980e22019-11-26 11:46:06 +0000179 return false;
180}
181
Yifan Hong8546a712019-03-28 14:42:53 -0700182bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700183 const std::string& super_device,
184 const std::string& target_partition_name,
185 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700186 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700187 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700188 CreateLogicalPartitionParams params = {
189 .block_device = super_device,
190 .metadata_slot = slot,
191 .partition_name = target_partition_name,
192 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700193 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700194 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700195 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700196 force_writable && ExpectMetadataMounted()) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700197 // Only target partitions are mapped with force_writable. On Virtual
198 // A/B devices, target partitions may overlap with source partitions, so
199 // they must be mapped with snapshot.
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700200 // One exception is when /metadata is not mounted. Fallback to
201 // CreateLogicalPartition as snapshots are not created in the first place.
Yifan Hong420db9b2019-07-23 20:50:33 -0700202 params.timeout_ms = kMapSnapshotTimeout;
203 success = snapshot_->MapUpdateSnapshot(params, path);
204 } else {
205 params.timeout_ms = kMapTimeout;
206 success = CreateLogicalPartition(params, path);
207 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700208
Yifan Hong420db9b2019-07-23 20:50:33 -0700209 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700210 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
211 << super_device << " on device mapper.";
212 return false;
213 }
214 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700215 << " to device mapper (force_writable = " << force_writable
216 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700217 mapped_devices_.insert(target_partition_name);
218 return true;
219}
220
Yifan Hong8546a712019-03-28 14:42:53 -0700221bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
222 const std::string& super_device,
223 const std::string& target_partition_name,
224 uint32_t slot,
225 bool force_writable,
226 std::string* path) {
227 DmDeviceState state = GetState(target_partition_name);
228 if (state == DmDeviceState::ACTIVE) {
229 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
230 if (GetDmDevicePathByName(target_partition_name, path)) {
231 LOG(INFO) << target_partition_name
232 << " is mapped on device mapper: " << *path;
233 return true;
234 }
235 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
236 return false;
237 }
238 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
239 // the device might be mapped incorrectly before. Attempt to unmap it.
240 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
241 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
242 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700243 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700244 LOG(ERROR) << target_partition_name
245 << " is mapped before the update, and it cannot be unmapped.";
246 return false;
247 }
248 state = GetState(target_partition_name);
249 if (state != DmDeviceState::INVALID) {
250 LOG(ERROR) << target_partition_name << " is unmapped but state is "
251 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
252 return false;
253 }
254 }
255 if (state == DmDeviceState::INVALID) {
256 return MapPartitionInternal(
257 super_device, target_partition_name, slot, force_writable, path);
258 }
259
260 LOG(ERROR) << target_partition_name
261 << " is mapped on device mapper but state is unknown: "
262 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
263 return false;
264}
265
Yifan Hong537802d2018-08-15 13:15:42 -0700266bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700267 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700268 if (DeviceMapper::Instance().GetState(target_partition_name) !=
269 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700270 // Partitions at target slot on non-Virtual A/B devices are mapped as
271 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
272 // preopt apps as dm-linear.
273 // Call DestroyLogicalPartition to handle these cases.
274 bool success = DestroyLogicalPartition(target_partition_name);
275
276 // On a Virtual A/B device, |target_partition_name| may be a leftover from
277 // a paused update. Clean up any underlying devices.
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700278 if (ExpectMetadataMounted()) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700279 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700280 } else {
281 LOG(INFO) << "Skip UnmapUpdateSnapshot(" << target_partition_name
282 << ") because metadata is not mounted";
Yifan Hong420db9b2019-07-23 20:50:33 -0700283 }
284
285 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700286 LOG(ERROR) << "Cannot unmap " << target_partition_name
287 << " from device mapper.";
288 return false;
289 }
290 LOG(INFO) << "Successfully unmapped " << target_partition_name
291 << " from device mapper.";
292 }
293 mapped_devices_.erase(target_partition_name);
294 return true;
295}
296
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -0400297bool DynamicPartitionControlAndroid::UnmapAllPartitions() {
Kelvin Zhangf7ef12a2021-03-22 12:59:06 -0400298 snapshot_->UnmapAllSnapshots();
Tao Bao8c4d0082019-08-08 08:56:16 -0700299 if (mapped_devices_.empty()) {
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -0400300 return false;
Tao Bao8c4d0082019-08-08 08:56:16 -0700301 }
Yifan Hong537802d2018-08-15 13:15:42 -0700302 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
303 // a copy is needed for the loop.
304 std::set<std::string> mapped = mapped_devices_;
305 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
306 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700307 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700308 }
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -0400309 return true;
Yifan Hong537802d2018-08-15 13:15:42 -0700310}
311
312void DynamicPartitionControlAndroid::Cleanup() {
Yifan Hongbae27842019-10-24 16:56:12 -0700313 UnmapAllPartitions();
314 metadata_device_.reset();
Yifan Hong537802d2018-08-15 13:15:42 -0700315}
316
317bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
318 return base::PathExists(base::FilePath(path));
319}
320
321android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
322 const std::string& name) {
323 return DeviceMapper::Instance().GetState(name);
324}
325
326bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
327 const std::string& name, std::string* path) {
328 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
329}
330
331std::unique_ptr<MetadataBuilder>
332DynamicPartitionControlAndroid::LoadMetadataBuilder(
Tianjie24f96092020-06-30 12:26:25 -0700333 const std::string& super_device, uint32_t slot) {
334 auto builder = MetadataBuilder::New(PartitionOpener(), super_device, slot);
335 if (builder == nullptr) {
336 LOG(WARNING) << "No metadata slot " << BootControlInterface::SlotName(slot)
337 << " in " << super_device;
338 return nullptr;
339 }
340 LOG(INFO) << "Loaded metadata from slot "
341 << BootControlInterface::SlotName(slot) << " in " << super_device;
342 return builder;
Yifan Hong012508e2019-07-22 18:30:40 -0700343}
344
345std::unique_ptr<MetadataBuilder>
346DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800347 const std::string& super_device,
348 uint32_t source_slot,
349 uint32_t target_slot) {
Tianjie24f96092020-06-30 12:26:25 -0700350 bool always_keep_source_slot = !target_supports_snapshot_;
351 auto builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
352 super_device,
353 source_slot,
354 target_slot,
355 always_keep_source_slot);
Yifan Hong537802d2018-08-15 13:15:42 -0700356 if (builder == nullptr) {
357 LOG(WARNING) << "No metadata slot "
358 << BootControlInterface::SlotName(source_slot) << " in "
359 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700360 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700361 }
Tianjie24f96092020-06-30 12:26:25 -0700362 LOG(INFO) << "Created metadata for new update from slot "
Yifan Hong537802d2018-08-15 13:15:42 -0700363 << BootControlInterface::SlotName(source_slot) << " in "
364 << super_device;
365 return builder;
366}
367
368bool DynamicPartitionControlAndroid::StoreMetadata(
369 const std::string& super_device,
370 MetadataBuilder* builder,
371 uint32_t target_slot) {
372 auto metadata = builder->Export();
373 if (metadata == nullptr) {
374 LOG(ERROR) << "Cannot export metadata to slot "
375 << BootControlInterface::SlotName(target_slot) << " in "
376 << super_device;
377 return false;
378 }
379
Yifan Hong186bb682019-07-23 14:04:39 -0700380 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800381 if (!FlashPartitionTable(super_device, *metadata)) {
382 LOG(ERROR) << "Cannot write metadata to " << super_device;
383 return false;
384 }
385 LOG(INFO) << "Written metadata to " << super_device;
386 } else {
387 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
388 LOG(ERROR) << "Cannot write metadata to slot "
389 << BootControlInterface::SlotName(target_slot) << " in "
390 << super_device;
391 return false;
392 }
393 LOG(INFO) << "Copied metadata to slot "
394 << BootControlInterface::SlotName(target_slot) << " in "
395 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700396 }
397
Yifan Hong537802d2018-08-15 13:15:42 -0700398 return true;
399}
400
401bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
402 // We can't use fs_mgr to look up |partition_name| because fstab
403 // doesn't list every slot partition (it uses the slotselect option
404 // to mask the suffix).
405 //
406 // We can however assume that there's an entry for the /misc mount
407 // point and use that to get the device file for the misc
408 // partition. This helps us locate the disk that |partition_name|
409 // resides on. From there we'll assume that a by-name scheme is used
410 // so we can just replace the trailing "misc" by the given
411 // |partition_name| and suffix corresponding to |slot|, e.g.
412 //
413 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
414 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
415 //
416 // If needed, it's possible to relax the by-name assumption in the
417 // future by trawling /sys/block looking for the appropriate sibling
418 // of misc and then finding an entry in /dev matching the sysfs
419 // entry.
420
421 std::string err, misc_device = get_bootloader_message_blk_device(&err);
422 if (misc_device.empty()) {
423 LOG(ERROR) << "Unable to get misc block device: " << err;
424 return false;
425 }
426
427 if (!utils::IsSymlink(misc_device.c_str())) {
428 LOG(ERROR) << "Device file " << misc_device << " for /misc "
429 << "is not a symlink.";
430 return false;
431 }
432 *out = base::FilePath(misc_device).DirName().value();
433 return true;
434}
Yifan Hong012508e2019-07-22 18:30:40 -0700435
436bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
437 uint32_t source_slot,
438 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700439 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800440 bool update,
441 uint64_t* required_size) {
Yifan Hong6eec9952019-12-04 13:12:01 -0800442 source_slot_ = source_slot;
443 target_slot_ = target_slot;
Yifan Hongf033ecb2020-01-07 18:13:56 -0800444 if (required_size != nullptr) {
445 *required_size = 0;
446 }
Yifan Hong6eec9952019-12-04 13:12:01 -0800447
Yifan Hong3a1a5612019-11-05 16:34:32 -0800448 if (fs_mgr_overlayfs_is_setup()) {
449 // Non DAP devices can use overlayfs as well.
450 LOG(WARNING)
451 << "overlayfs overrides are active and can interfere with our "
452 "resources.\n"
453 << "run adb enable-verity to deactivate if required and try again.";
454 }
455
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700456 // If metadata is erased but not formatted, it is possible to not mount
457 // it in recovery. It is acceptable to skip mounting and choose fallback path
458 // (PrepareDynamicPartitionsForUpdate) when sideloading full OTAs.
459 TEST_AND_RETURN_FALSE(EnsureMetadataMounted() || IsRecovery());
Yifan Hong29692902020-03-26 12:47:05 -0700460
461 if (update) {
462 TEST_AND_RETURN_FALSE(EraseSystemOtherAvbFooter(source_slot, target_slot));
463 }
464
Yifan Hong3a1a5612019-11-05 16:34:32 -0800465 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
466 return true;
467 }
468
469 if (target_slot == source_slot) {
470 LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot.";
471 return false;
472 }
473
Yifan Hongf6f75c22020-07-31 15:20:25 -0700474 if (!SetTargetBuildVars(manifest)) {
475 return false;
476 }
Kelvin Zhangff5380b2022-03-16 13:35:04 -0700477 for (auto& list : dynamic_partition_list_) {
478 list.clear();
479 }
Yifan Hongf6f75c22020-07-31 15:20:25 -0700480
Yifan Hong3a1a5612019-11-05 16:34:32 -0800481 // Although the current build supports dynamic partitions, the given payload
482 // doesn't use it for target partitions. This could happen when applying a
483 // retrofit update. Skip updating the partition metadata for the target slot.
Yifan Hong3a1a5612019-11-05 16:34:32 -0800484 if (!is_target_dynamic_) {
485 return true;
486 }
487
Yifan Hongf0f4a912019-09-26 17:51:33 -0700488 if (!update)
489 return true;
490
Yifan Hongbae27842019-10-24 16:56:12 -0700491 bool delete_source = false;
492
Yifan Hong6d888562019-10-01 13:00:31 -0700493 if (GetVirtualAbFeatureFlag().IsEnabled()) {
494 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
495 // called before calling UnmapUpdateSnapshot.
496 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
497 // calls BeginUpdate() which resets update state
Yifan Hongbae27842019-10-24 16:56:12 -0700498 // - If !target_supports_snapshot_ or PrepareSnapshotPartitionsForUpdate
499 // failed in recovery, explicitly CancelUpdate().
Yifan Hong6d888562019-10-01 13:00:31 -0700500 if (target_supports_snapshot_) {
Yifan Hongbae27842019-10-24 16:56:12 -0700501 if (PrepareSnapshotPartitionsForUpdate(
502 source_slot, target_slot, manifest, required_size)) {
503 return true;
504 }
505
506 // Virtual A/B device doing Virtual A/B update in Android mode must use
507 // snapshots.
508 if (!IsRecovery()) {
509 LOG(ERROR) << "PrepareSnapshotPartitionsForUpdate failed in Android "
510 << "mode";
511 return false;
512 }
513
514 delete_source = true;
515 LOG(INFO) << "PrepareSnapshotPartitionsForUpdate failed in recovery. "
516 << "Attempt to overwrite existing partitions if possible";
517 } else {
518 // Downgrading to an non-Virtual A/B build or is secondary OTA.
519 LOG(INFO) << "Using regular A/B on Virtual A/B because package disabled "
520 << "snapshots.";
Yifan Hong6d888562019-10-01 13:00:31 -0700521 }
Yifan Hongbae27842019-10-24 16:56:12 -0700522
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700523 // In recovery, if /metadata is not mounted, it is likely that metadata
524 // partition is erased and not formatted yet. After sideloading, when
525 // rebooting into the new version, init will erase metadata partition,
526 // hence the failure of CancelUpdate() can be ignored here.
527 // However, if metadata is mounted and CancelUpdate fails, sideloading
528 // should not proceed because during next boot, snapshots will overlay on
529 // the devices incorrectly.
530 if (ExpectMetadataMounted()) {
531 TEST_AND_RETURN_FALSE(snapshot_->CancelUpdate());
532 } else {
533 LOG(INFO) << "Skip canceling previous update because metadata is not "
534 << "mounted";
Yifan Hong6d888562019-10-01 13:00:31 -0700535 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700536 }
Yifan Hongbae27842019-10-24 16:56:12 -0700537
Tianjie24f96092020-06-30 12:26:25 -0700538 // TODO(xunchang) support partial update on non VAB enabled devices.
Yifan Hong5c5743f2020-04-16 12:59:07 -0700539 TEST_AND_RETURN_FALSE(PrepareDynamicPartitionsForUpdate(
540 source_slot, target_slot, manifest, delete_source));
541
542 if (required_size != nullptr) {
543 *required_size = 0;
544 }
545 return true;
Yifan Hong420db9b2019-07-23 20:50:33 -0700546}
547
Yifan Hongf6f75c22020-07-31 15:20:25 -0700548bool DynamicPartitionControlAndroid::SetTargetBuildVars(
549 const DeltaArchiveManifest& manifest) {
550 // Precondition: current build supports dynamic partition.
551 CHECK(GetDynamicPartitionsFeatureFlag().IsEnabled());
552
553 bool is_target_dynamic =
554 !manifest.dynamic_partition_metadata().groups().empty();
555 bool target_supports_snapshot =
556 manifest.dynamic_partition_metadata().snapshot_enabled();
557
558 if (manifest.partial_update()) {
559 // Partial updates requires DAP. On partial updates that does not involve
560 // dynamic partitions, groups() can be empty, so also assume
561 // is_target_dynamic in this case. This assumption should be safe because we
562 // also check target_supports_snapshot below, which presumably also implies
563 // target build supports dynamic partition.
564 if (!is_target_dynamic) {
565 LOG(INFO) << "Assuming target build supports dynamic partitions for "
566 "partial updates.";
567 is_target_dynamic = true;
568 }
569
570 // Partial updates requires Virtual A/B. Double check that both current
571 // build and target build supports Virtual A/B.
572 if (!GetVirtualAbFeatureFlag().IsEnabled()) {
573 LOG(ERROR) << "Partial update cannot be applied on a device that does "
574 "not support snapshots.";
575 return false;
576 }
577 if (!target_supports_snapshot) {
578 LOG(ERROR) << "Cannot apply partial update to a build that does not "
579 "support snapshots.";
580 return false;
581 }
582 }
583
584 // Store the flags.
585 is_target_dynamic_ = is_target_dynamic;
586 // If !is_target_dynamic_, leave target_supports_snapshot_ unset because
587 // snapshots would not work without dynamic partition.
588 if (is_target_dynamic_) {
589 target_supports_snapshot_ = target_supports_snapshot;
590 }
591 return true;
592}
593
Yifan Hong29692902020-03-26 12:47:05 -0700594namespace {
595// Try our best to erase AVB footer.
596class AvbFooterEraser {
597 public:
598 explicit AvbFooterEraser(const std::string& path) : path_(path) {}
599 bool Erase() {
600 // Try to mark the block device read-only. Ignore any
601 // failure since this won't work when passing regular files.
602 ignore_result(utils::SetBlockDeviceReadOnly(path_, false /* readonly */));
603
604 fd_.reset(new EintrSafeFileDescriptor());
605 int flags = O_WRONLY | O_TRUNC | O_CLOEXEC | O_SYNC;
606 TEST_AND_RETURN_FALSE(fd_->Open(path_.c_str(), flags));
607
608 // Need to write end-AVB_FOOTER_SIZE to end.
609 static_assert(AVB_FOOTER_SIZE > 0);
610 off64_t offset = fd_->Seek(-AVB_FOOTER_SIZE, SEEK_END);
611 TEST_AND_RETURN_FALSE_ERRNO(offset >= 0);
612 uint64_t write_size = AVB_FOOTER_SIZE;
613 LOG(INFO) << "Zeroing " << path_ << " @ [" << offset << ", "
614 << (offset + write_size) << "] (" << write_size << " bytes)";
615 brillo::Blob zeros(write_size);
616 TEST_AND_RETURN_FALSE(utils::WriteAll(fd_, zeros.data(), zeros.size()));
617 return true;
618 }
619 ~AvbFooterEraser() {
620 TEST_AND_RETURN(fd_ != nullptr && fd_->IsOpen());
621 if (!fd_->Close()) {
622 LOG(WARNING) << "Failed to close fd for " << path_;
623 }
624 }
625
626 private:
627 std::string path_;
628 FileDescriptorPtr fd_;
629};
630
631} // namespace
632
633std::optional<bool>
634DynamicPartitionControlAndroid::IsAvbEnabledOnSystemOther() {
635 auto prefix = GetProperty(kPostinstallFstabPrefix, "");
636 if (prefix.empty()) {
637 LOG(WARNING) << "Cannot get " << kPostinstallFstabPrefix;
638 return std::nullopt;
639 }
640 auto path = base::FilePath(prefix).Append("etc/fstab.postinstall").value();
641 return IsAvbEnabledInFstab(path);
642}
643
644std::optional<bool> DynamicPartitionControlAndroid::IsAvbEnabledInFstab(
645 const std::string& path) {
646 Fstab fstab;
647 if (!ReadFstabFromFile(path, &fstab)) {
Yifan Hong93cde302020-04-27 12:59:29 -0700648 PLOG(WARNING) << "Cannot read fstab from " << path;
649 if (errno == ENOENT) {
650 return false;
651 }
Yifan Hong29692902020-03-26 12:47:05 -0700652 return std::nullopt;
653 }
654 for (const auto& entry : fstab) {
655 if (!entry.avb_keys.empty()) {
656 return true;
657 }
658 }
659 return false;
660}
661
662bool DynamicPartitionControlAndroid::GetSystemOtherPath(
663 uint32_t source_slot,
664 uint32_t target_slot,
665 const std::string& partition_name_suffix,
666 std::string* path,
667 bool* should_unmap) {
668 path->clear();
669 *should_unmap = false;
670
P.Adarsh Reddy13e4195d2020-06-08 23:17:36 +0530671 // Check that AVB is enabled on system_other before erasing.
672 auto has_avb = IsAvbEnabledOnSystemOther();
673 TEST_AND_RETURN_FALSE(has_avb.has_value());
674 if (!has_avb.value()) {
675 LOG(INFO) << "AVB is not enabled on system_other. Skip erasing.";
676 return true;
677 }
Yifan Hong29692902020-03-26 12:47:05 -0700678
P.Adarsh Reddy13e4195d2020-06-08 23:17:36 +0530679 if (!IsRecovery()) {
Yifan Hong29692902020-03-26 12:47:05 -0700680 // Found unexpected avb_keys for system_other on devices retrofitting
681 // dynamic partitions. Previous crash in update_engine may leave logical
682 // partitions mapped on physical system_other partition. It is difficult to
683 // handle these cases. Just fail.
684 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
685 LOG(ERROR) << "Cannot erase AVB footer on system_other on devices with "
686 << "retrofit dynamic partitions. They should not have AVB "
687 << "enabled on system_other.";
688 return false;
689 }
690 }
691
692 std::string device_dir_str;
693 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
694 base::FilePath device_dir(device_dir_str);
695
696 // On devices without dynamic partition, search for static partitions.
697 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
698 *path = device_dir.Append(partition_name_suffix).value();
699 TEST_AND_RETURN_FALSE(DeviceExists(*path));
700 return true;
701 }
702
703 auto source_super_device =
704 device_dir.Append(GetSuperPartitionName(source_slot)).value();
705
706 auto builder = LoadMetadataBuilder(source_super_device, source_slot);
707 if (builder == nullptr) {
708 if (IsRecovery()) {
709 // It might be corrupted for some reason. It should still be able to
710 // sideload.
711 LOG(WARNING) << "Super partition metadata cannot be read from the source "
712 << "slot, skip erasing.";
713 return true;
714 } else {
715 // Device has booted into Android mode, indicating that the super
716 // partition metadata should be there.
717 LOG(ERROR) << "Super partition metadata cannot be read from the source "
718 << "slot. This is unexpected on devices with dynamic "
719 << "partitions enabled.";
720 return false;
721 }
722 }
723 auto p = builder->FindPartition(partition_name_suffix);
724 if (p == nullptr) {
725 // If the source slot is flashed without system_other, it does not exist
726 // in super partition metadata at source slot. It is safe to skip it.
727 LOG(INFO) << "Can't find " << partition_name_suffix
728 << " in metadata source slot, skip erasing.";
729 return true;
730 }
731 // System_other created by flashing tools should be erased.
732 // If partition is created by update_engine (via NewForUpdate), it is a
733 // left-over partition from the previous update and does not contain
734 // system_other, hence there is no need to erase.
735 // Note the reverse is not necessary true. If the flag is not set, we don't
736 // know if the partition is created by update_engine or by flashing tools
737 // because older versions of super partition metadata does not contain this
738 // flag. It is okay to erase the AVB footer anyways.
739 if (p->attributes() & LP_PARTITION_ATTR_UPDATED) {
740 LOG(INFO) << partition_name_suffix
741 << " does not contain system_other, skip erasing.";
742 return true;
743 }
744
Yifan Hong64331b32020-05-13 16:50:40 -0700745 if (p->size() < AVB_FOOTER_SIZE) {
746 LOG(INFO) << partition_name_suffix << " has length " << p->size()
747 << "( < AVB_FOOTER_SIZE " << AVB_FOOTER_SIZE
748 << "), skip erasing.";
749 return true;
750 }
751
Yifan Hong29692902020-03-26 12:47:05 -0700752 // Delete any pre-existing device with name |partition_name_suffix| and
753 // also remove it from |mapped_devices_|.
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700754 // In recovery, metadata might not be mounted, and
755 // UnmapPartitionOnDeviceMapper might fail. However,
756 // it is unusual that system_other has already been mapped. Hence, just skip.
Yifan Hong29692902020-03-26 12:47:05 -0700757 TEST_AND_RETURN_FALSE(UnmapPartitionOnDeviceMapper(partition_name_suffix));
758 // Use CreateLogicalPartition directly to avoid mapping with existing
759 // snapshots.
760 CreateLogicalPartitionParams params = {
761 .block_device = source_super_device,
762 .metadata_slot = source_slot,
763 .partition_name = partition_name_suffix,
764 .force_writable = true,
765 .timeout_ms = kMapTimeout,
766 };
767 TEST_AND_RETURN_FALSE(CreateLogicalPartition(params, path));
768 *should_unmap = true;
769 return true;
770}
771
772bool DynamicPartitionControlAndroid::EraseSystemOtherAvbFooter(
773 uint32_t source_slot, uint32_t target_slot) {
774 LOG(INFO) << "Erasing AVB footer of system_other partition before update.";
775
776 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
777 const std::string partition_name_suffix = "system" + target_suffix;
778
779 std::string path;
780 bool should_unmap = false;
781
782 TEST_AND_RETURN_FALSE(GetSystemOtherPath(
783 source_slot, target_slot, partition_name_suffix, &path, &should_unmap));
784
785 if (path.empty()) {
786 return true;
787 }
788
789 bool ret = AvbFooterEraser(path).Erase();
790
791 // Delete |partition_name_suffix| from device mapper and from
792 // |mapped_devices_| again so that it does not interfere with update process.
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700793 // In recovery, metadata might not be mounted, and
794 // UnmapPartitionOnDeviceMapper might fail. However, DestroyLogicalPartition
795 // should be called. If DestroyLogicalPartition does fail, it is still okay
796 // to skip the error here and let Prepare*() fail later.
Yifan Hong29692902020-03-26 12:47:05 -0700797 if (should_unmap) {
798 TEST_AND_RETURN_FALSE(UnmapPartitionOnDeviceMapper(partition_name_suffix));
799 }
800
801 return ret;
802}
803
Yifan Hong420db9b2019-07-23 20:50:33 -0700804bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
805 uint32_t source_slot,
806 uint32_t target_slot,
Yifan Hongbae27842019-10-24 16:56:12 -0700807 const DeltaArchiveManifest& manifest,
808 bool delete_source) {
Yifan Hong012508e2019-07-22 18:30:40 -0700809 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
810
811 // Unmap all the target dynamic partitions because they would become
812 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700813 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
814 for (const auto& partition_name : group.partition_names()) {
815 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700816 return false;
817 }
818 }
819 }
820
821 std::string device_dir_str;
Tianjie9f4dc7f2021-03-15 16:00:50 -0700822 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
Yifan Hong012508e2019-07-22 18:30:40 -0700823 base::FilePath device_dir(device_dir_str);
824 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700825 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700826
827 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
828 if (builder == nullptr) {
829 LOG(ERROR) << "No metadata at "
830 << BootControlInterface::SlotName(source_slot);
831 return false;
832 }
833
Yifan Hongbae27842019-10-24 16:56:12 -0700834 if (delete_source) {
835 TEST_AND_RETURN_FALSE(
836 DeleteSourcePartitions(builder.get(), source_slot, manifest));
837 }
838
Tianjie9f4dc7f2021-03-15 16:00:50 -0700839 TEST_AND_RETURN_FALSE(
840 UpdatePartitionMetadata(builder.get(), target_slot, manifest));
Yifan Hong012508e2019-07-22 18:30:40 -0700841
842 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700843 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700844 return StoreMetadata(target_device, builder.get(), target_slot);
845}
846
Yifan Hong3a1293a2021-04-16 13:21:20 -0700847DynamicPartitionControlAndroid::SpaceLimit
848DynamicPartitionControlAndroid::GetSpaceLimit(bool use_snapshot) {
849 // On device retrofitting dynamic partitions, allocatable_space = "super",
850 // where "super" is the sum of all block devices for that slot. Since block
851 // devices are dedicated for the corresponding slot, there's no need to halve
852 // the allocatable space.
853 if (GetDynamicPartitionsFeatureFlag().IsRetrofit())
854 return SpaceLimit::ERROR_IF_EXCEEDED_SUPER;
855
856 // On device launching dynamic partitions w/o VAB, regardless of recovery
857 // sideload, super partition must be big enough to hold both A and B slots of
858 // groups. Hence,
859 // allocatable_space = super / 2
860 if (!GetVirtualAbFeatureFlag().IsEnabled())
861 return SpaceLimit::ERROR_IF_EXCEEDED_HALF_OF_SUPER;
862
863 // Source build supports VAB. Super partition must be big enough to hold
864 // one slot of groups (ERROR_IF_EXCEEDED_SUPER). However, there are cases
865 // where additional warning messages needs to be written.
866
867 // If using snapshot updates, implying that target build also uses VAB,
868 // allocatable_space = super
869 if (use_snapshot)
870 return SpaceLimit::ERROR_IF_EXCEEDED_SUPER;
871
872 // Source build supports VAB but not using snapshot updates. There are
873 // several cases, as listed below.
874 // Sideloading: allocatable_space = super.
875 if (IsRecovery())
876 return SpaceLimit::ERROR_IF_EXCEEDED_SUPER;
877
878 // On launch VAB device, this implies secondary payload.
879 // Technically, we don't have to check anything, but sum(groups) < super
880 // still applies.
881 if (!GetVirtualAbFeatureFlag().IsRetrofit())
882 return SpaceLimit::ERROR_IF_EXCEEDED_SUPER;
883
884 // On retrofit VAB device, either of the following:
885 // - downgrading: allocatable_space = super / 2
886 // - secondary payload: don't check anything
887 // These two cases are indistinguishable,
888 // hence emit warning if sum(groups) > super / 2
889 return SpaceLimit::WARN_IF_EXCEEDED_HALF_OF_SUPER;
890}
891
Tianjie9f4dc7f2021-03-15 16:00:50 -0700892bool DynamicPartitionControlAndroid::CheckSuperPartitionAllocatableSpace(
893 android::fs_mgr::MetadataBuilder* builder,
894 const DeltaArchiveManifest& manifest,
895 bool use_snapshot) {
Yifan Hong3a1293a2021-04-16 13:21:20 -0700896 uint64_t sum_groups = 0;
Tianjie9f4dc7f2021-03-15 16:00:50 -0700897 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
Yifan Hong3a1293a2021-04-16 13:21:20 -0700898 sum_groups += group.size();
Tianjie9f4dc7f2021-03-15 16:00:50 -0700899 }
900
Yifan Hong3a1293a2021-04-16 13:21:20 -0700901 uint64_t full_space = builder->AllocatableSpace();
902 uint64_t half_space = full_space / 2;
903 constexpr const char* fmt =
904 "The maximum size of all groups for the target slot (%" PRIu64
905 ") has exceeded %sallocatable space for dynamic partitions %" PRIu64 ".";
906 switch (GetSpaceLimit(use_snapshot)) {
907 case SpaceLimit::ERROR_IF_EXCEEDED_HALF_OF_SUPER: {
908 if (sum_groups > half_space) {
909 LOG(ERROR) << StringPrintf(fmt, sum_groups, "HALF OF ", half_space);
910 return false;
911 }
912 // If test passes, it implies that the following two conditions also pass.
913 break;
914 }
915 case SpaceLimit::WARN_IF_EXCEEDED_HALF_OF_SUPER: {
916 if (sum_groups > half_space) {
917 LOG(WARNING) << StringPrintf(fmt, sum_groups, "HALF OF ", half_space)
918 << " This is allowed for downgrade or secondary OTA on "
919 "retrofit VAB device.";
920 }
921 // still check sum(groups) < super
922 [[fallthrough]];
923 }
924 case SpaceLimit::ERROR_IF_EXCEEDED_SUPER: {
925 if (sum_groups > full_space) {
926 LOG(ERROR) << base::StringPrintf(fmt, sum_groups, "", full_space);
927 return false;
928 }
929 break;
930 }
Tianjie9f4dc7f2021-03-15 16:00:50 -0700931 }
932
933 return true;
934}
935
Yifan Hong420db9b2019-07-23 20:50:33 -0700936bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
937 uint32_t source_slot,
938 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800939 const DeltaArchiveManifest& manifest,
940 uint64_t* required_size) {
Yifan Hong4d7c5eb2020-04-03 11:31:50 -0700941 TEST_AND_RETURN_FALSE(ExpectMetadataMounted());
Tianjie9f4dc7f2021-03-15 16:00:50 -0700942
943 std::string device_dir_str;
944 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
945 base::FilePath device_dir(device_dir_str);
946 auto super_device =
947 device_dir.Append(GetSuperPartitionName(source_slot)).value();
948 auto builder = LoadMetadataBuilder(super_device, source_slot);
949 if (builder == nullptr) {
950 LOG(ERROR) << "No metadata at "
951 << BootControlInterface::SlotName(source_slot);
952 return false;
953 }
954
955 TEST_AND_RETURN_FALSE(
956 CheckSuperPartitionAllocatableSpace(builder.get(), manifest, true));
957
Yifan Hong420db9b2019-07-23 20:50:33 -0700958 if (!snapshot_->BeginUpdate()) {
959 LOG(ERROR) << "Cannot begin new update.";
960 return false;
961 }
Yifan Hongf033ecb2020-01-07 18:13:56 -0800962 auto ret = snapshot_->CreateUpdateSnapshots(manifest);
963 if (!ret) {
964 LOG(ERROR) << "Cannot create update snapshots: " << ret.string();
965 if (required_size != nullptr &&
Yifan Hong0850bca2020-01-16 15:14:07 -0800966 ret.error_code() == Return::ErrorCode::NO_SPACE) {
Yifan Hongf033ecb2020-01-07 18:13:56 -0800967 *required_size = ret.required_size();
968 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700969 return false;
970 }
971 return true;
972}
973
Yifan Hong700d7c12019-07-23 20:49:16 -0700974std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
975 uint32_t slot) {
976 return fs_mgr_get_super_partition_name(slot);
977}
978
Yifan Hong012508e2019-07-22 18:30:40 -0700979bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
980 MetadataBuilder* builder,
981 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700982 const DeltaArchiveManifest& manifest) {
Yifan Hong8d6df9a2020-08-13 13:59:54 -0700983 // Check preconditions.
Yifan Hong265a8e32021-04-16 13:30:02 -0700984 if (GetVirtualAbFeatureFlag().IsEnabled()) {
985 CHECK(!target_supports_snapshot_ || IsRecovery())
986 << "Must use snapshot on VAB device when target build supports VAB and "
987 "not sideloading.";
988 LOG_IF(INFO, !target_supports_snapshot_)
989 << "Not using snapshot on VAB device because target build does not "
990 "support snapshot. Secondary or downgrade OTA?";
991 LOG_IF(INFO, IsRecovery())
992 << "Not using snapshot on VAB device because sideloading.";
993 }
Yifan Hong8d6df9a2020-08-13 13:59:54 -0700994
Yifan Honga4e7da32019-09-30 18:25:03 -0700995 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
996 // COW group needs to be deleted to ensure there are enough space to create
997 // target partitions.
998 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
999
Yifan Hong012508e2019-07-22 18:30:40 -07001000 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
1001 DeleteGroupsWithSuffix(builder, target_suffix);
1002
Tianjie9f4dc7f2021-03-15 16:00:50 -07001003 TEST_AND_RETURN_FALSE(
1004 CheckSuperPartitionAllocatableSpace(builder, manifest, false));
Yifan Hong012508e2019-07-22 18:30:40 -07001005
Yifan Hong13d41cb2019-09-16 13:18:22 -07001006 // name of partition(e.g. "system") -> size in bytes
1007 std::map<std::string, uint64_t> partition_sizes;
1008 for (const auto& partition : manifest.partitions()) {
1009 partition_sizes.emplace(partition.partition_name(),
1010 partition.new_partition_info().size());
1011 }
1012
1013 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
1014 auto group_name_suffix = group.name() + target_suffix;
1015 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -07001016 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -07001017 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -07001018 return false;
1019 }
1020 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -07001021 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -07001022
Yifan Hong13d41cb2019-09-16 13:18:22 -07001023 for (const auto& partition_name : group.partition_names()) {
1024 auto partition_sizes_it = partition_sizes.find(partition_name);
1025 if (partition_sizes_it == partition_sizes.end()) {
1026 // TODO(tbao): Support auto-filling partition info for framework-only
1027 // OTA.
1028 LOG(ERROR) << "dynamic_partition_metadata contains partition "
1029 << partition_name << " but it is not part of the manifest. "
1030 << "This is not supported.";
1031 return false;
1032 }
1033 uint64_t partition_size = partition_sizes_it->second;
1034
1035 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -07001036 Partition* p = builder->AddPartition(
1037 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
1038 if (!p) {
1039 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
1040 << " to group " << group_name_suffix;
1041 return false;
1042 }
Yifan Hong13d41cb2019-09-16 13:18:22 -07001043 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -07001044 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -07001045 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -07001046 return false;
1047 }
David Anderson0e1c7fd2021-03-08 19:01:59 -08001048 if (p->size() < partition_size) {
1049 LOG(ERROR) << "Partition " << partition_name_suffix
1050 << " was expected to have size " << partition_size
1051 << ", but instead has size " << p->size();
1052 return false;
1053 }
Yifan Hong012508e2019-07-22 18:30:40 -07001054 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -07001055 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -07001056 }
1057 }
1058
1059 return true;
1060}
1061
Yifan Hong7b3910a2020-03-24 17:47:32 -07001062bool DynamicPartitionControlAndroid::FinishUpdate(bool powerwash_required) {
Yifan Hong4d7c5eb2020-04-03 11:31:50 -07001063 if (ExpectMetadataMounted()) {
1064 if (snapshot_->GetUpdateState() == UpdateState::Initiated) {
1065 LOG(INFO) << "Snapshot writes are done.";
1066 return snapshot_->FinishedSnapshotWrites(powerwash_required);
1067 }
1068 } else {
1069 LOG(INFO) << "Skip FinishedSnapshotWrites() because /metadata is not "
1070 << "mounted";
Yifan Hongf0f4a912019-09-26 17:51:33 -07001071 }
1072 return true;
Yifan Honga33bca42019-09-03 20:29:45 -07001073}
1074
Yifan Hong3a1a5612019-11-05 16:34:32 -08001075bool DynamicPartitionControlAndroid::GetPartitionDevice(
1076 const std::string& partition_name,
1077 uint32_t slot,
1078 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -07001079 bool not_in_payload,
1080 std::string* device,
1081 bool* is_dynamic) {
Kelvin Zhang66a9ebb2021-01-25 13:35:10 -05001082 auto partition_dev =
1083 GetPartitionDevice(partition_name, slot, current_slot, not_in_payload);
Kelvin Zhang91d95fa2020-11-05 13:52:00 -05001084 if (!partition_dev.has_value()) {
Yifan Hong3a1a5612019-11-05 16:34:32 -08001085 return false;
1086 }
Kelvin Zhang91d95fa2020-11-05 13:52:00 -05001087 if (device) {
1088 *device = std::move(partition_dev->rw_device_path);
1089 }
1090 if (is_dynamic) {
1091 *is_dynamic = partition_dev->is_dynamic;
1092 }
Yifan Hong3a1a5612019-11-05 16:34:32 -08001093 return true;
1094}
1095
Tianjie51a5a392020-06-03 14:39:32 -07001096bool DynamicPartitionControlAndroid::GetPartitionDevice(
1097 const std::string& partition_name,
1098 uint32_t slot,
1099 uint32_t current_slot,
1100 std::string* device) {
1101 return GetPartitionDevice(
1102 partition_name, slot, current_slot, false, device, nullptr);
1103}
1104
Kelvin Zhang91d95fa2020-11-05 13:52:00 -05001105static std::string GetStaticDevicePath(
1106 const base::FilePath& device_dir,
1107 const std::string& partition_name_suffixed) {
1108 base::FilePath path = device_dir.Append(partition_name_suffixed);
1109 return path.value();
1110}
1111
1112std::optional<PartitionDevice>
1113DynamicPartitionControlAndroid::GetPartitionDevice(
1114 const std::string& partition_name,
1115 uint32_t slot,
1116 uint32_t current_slot,
1117 bool not_in_payload) {
1118 std::string device_dir_str;
1119 if (!GetDeviceDir(&device_dir_str)) {
1120 LOG(ERROR) << "Failed to GetDeviceDir()";
1121 return {};
1122 }
1123 const base::FilePath device_dir(device_dir_str);
1124 // When VABC is enabled, we can't get device path for dynamic partitions in
1125 // target slot.
1126 const auto& partition_name_suffix =
1127 partition_name + SlotSuffixForSlotNumber(slot);
Kelvin Zhangebd115e2021-03-08 16:10:25 -05001128 if (UpdateUsesSnapshotCompression() && slot != current_slot &&
1129 IsDynamicPartition(partition_name, slot)) {
Kelvin Zhang91ad6622021-03-01 13:46:17 -05001130 return {
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -04001131 {.readonly_device_path = base::FilePath{std::string{VABC_DEVICE_DIR}}
1132 .Append(partition_name_suffix)
1133 .value(),
Kelvin Zhang91ad6622021-03-01 13:46:17 -05001134 .is_dynamic = true}};
Kelvin Zhang91d95fa2020-11-05 13:52:00 -05001135 }
1136
1137 // When looking up target partition devices, treat them as static if the
1138 // current payload doesn't encode them as dynamic partitions. This may happen
1139 // when applying a retrofit update on top of a dynamic-partitions-enabled
1140 // build.
1141 std::string device;
1142 if (GetDynamicPartitionsFeatureFlag().IsEnabled() &&
1143 (slot == current_slot || is_target_dynamic_)) {
1144 switch (GetDynamicPartitionDevice(device_dir,
1145 partition_name_suffix,
1146 slot,
1147 current_slot,
1148 not_in_payload,
1149 &device)) {
1150 case DynamicPartitionDeviceStatus::SUCCESS:
1151 return {{.rw_device_path = device,
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -04001152 .readonly_device_path = device,
Kelvin Zhang91d95fa2020-11-05 13:52:00 -05001153 .is_dynamic = true}};
1154
1155 case DynamicPartitionDeviceStatus::TRY_STATIC:
1156 break;
1157 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
1158 default:
1159 return {};
1160 }
1161 }
1162 // Try static partitions.
1163 auto static_path = GetStaticDevicePath(device_dir, partition_name_suffix);
1164 if (!DeviceExists(static_path)) {
1165 LOG(ERROR) << "Device file " << static_path << " does not exist.";
1166 return {};
1167 }
1168
1169 return {{.rw_device_path = static_path,
Kelvin Zhanga9b5d8c2021-05-05 09:17:46 -04001170 .readonly_device_path = static_path,
Kelvin Zhang91d95fa2020-11-05 13:52:00 -05001171 .is_dynamic = false}};
1172}
1173
Yifan Hong3a1a5612019-11-05 16:34:32 -08001174bool DynamicPartitionControlAndroid::IsSuperBlockDevice(
1175 const base::FilePath& device_dir,
1176 uint32_t current_slot,
1177 const std::string& partition_name_suffix) {
1178 std::string source_device =
1179 device_dir.Append(GetSuperPartitionName(current_slot)).value();
1180 auto source_metadata = LoadMetadataBuilder(source_device, current_slot);
1181 return source_metadata->HasBlockDevice(partition_name_suffix);
1182}
1183
1184DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus
1185DynamicPartitionControlAndroid::GetDynamicPartitionDevice(
1186 const base::FilePath& device_dir,
1187 const std::string& partition_name_suffix,
1188 uint32_t slot,
1189 uint32_t current_slot,
Tianjie51a5a392020-06-03 14:39:32 -07001190 bool not_in_payload,
Yifan Hong3a1a5612019-11-05 16:34:32 -08001191 std::string* device) {
1192 std::string super_device =
1193 device_dir.Append(GetSuperPartitionName(slot)).value();
1194
1195 auto builder = LoadMetadataBuilder(super_device, slot);
1196 if (builder == nullptr) {
1197 LOG(ERROR) << "No metadata in slot "
1198 << BootControlInterface::SlotName(slot);
1199 return DynamicPartitionDeviceStatus::ERROR;
1200 }
1201 if (builder->FindPartition(partition_name_suffix) == nullptr) {
1202 LOG(INFO) << partition_name_suffix
1203 << " is not in super partition metadata.";
1204
1205 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
1206 LOG(ERROR) << "The static partition " << partition_name_suffix
1207 << " is a block device for current metadata."
1208 << "It cannot be used as a logical partition.";
1209 return DynamicPartitionDeviceStatus::ERROR;
1210 }
1211
1212 return DynamicPartitionDeviceStatus::TRY_STATIC;
1213 }
1214
1215 if (slot == current_slot) {
1216 if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) {
1217 LOG(WARNING) << partition_name_suffix << " is at current slot but it is "
1218 << "not mapped. Now try to map it.";
1219 } else {
1220 if (GetDmDevicePathByName(partition_name_suffix, device)) {
1221 LOG(INFO) << partition_name_suffix
1222 << " is mapped on device mapper: " << *device;
1223 return DynamicPartitionDeviceStatus::SUCCESS;
1224 }
1225 LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown.";
1226 return DynamicPartitionDeviceStatus::ERROR;
1227 }
1228 }
1229
Tianjie51a5a392020-06-03 14:39:32 -07001230 bool force_writable = (slot != current_slot) && !not_in_payload;
Yifan Hong3a1a5612019-11-05 16:34:32 -08001231 if (MapPartitionOnDeviceMapper(
1232 super_device, partition_name_suffix, slot, force_writable, device)) {
1233 return DynamicPartitionDeviceStatus::SUCCESS;
1234 }
1235 return DynamicPartitionDeviceStatus::ERROR;
1236}
1237
Yifan Hong6eec9952019-12-04 13:12:01 -08001238void DynamicPartitionControlAndroid::set_fake_mapped_devices(
1239 const std::set<std::string>& fake) {
1240 mapped_devices_ = fake;
1241}
1242
Yifan Hongbae27842019-10-24 16:56:12 -07001243bool DynamicPartitionControlAndroid::IsRecovery() {
Kelvin Zhanga22ef552020-10-12 19:03:52 -04001244 return constants::kIsRecovery;
Yifan Hongbae27842019-10-24 16:56:12 -07001245}
1246
1247static bool IsIncrementalUpdate(const DeltaArchiveManifest& manifest) {
1248 const auto& partitions = manifest.partitions();
1249 return std::any_of(partitions.begin(), partitions.end(), [](const auto& p) {
1250 return p.has_old_partition_info();
1251 });
1252}
1253
1254bool DynamicPartitionControlAndroid::DeleteSourcePartitions(
1255 MetadataBuilder* builder,
1256 uint32_t source_slot,
1257 const DeltaArchiveManifest& manifest) {
1258 TEST_AND_RETURN_FALSE(IsRecovery());
1259
1260 if (IsIncrementalUpdate(manifest)) {
1261 LOG(ERROR) << "Cannot sideload incremental OTA because snapshots cannot "
1262 << "be created.";
1263 if (GetVirtualAbFeatureFlag().IsLaunch()) {
1264 LOG(ERROR) << "Sideloading incremental updates on devices launches "
1265 << " Virtual A/B is not supported.";
1266 }
1267 return false;
1268 }
1269
1270 LOG(INFO) << "Will overwrite existing partitions. Slot "
1271 << BootControlInterface::SlotName(source_slot)
Tianjie9f4dc7f2021-03-15 16:00:50 -07001272 << " may be unbootable until update finishes!";
Yifan Hongbae27842019-10-24 16:56:12 -07001273 const std::string source_suffix = SlotSuffixForSlotNumber(source_slot);
1274 DeleteGroupsWithSuffix(builder, source_suffix);
1275
1276 return true;
1277}
1278
Yifan Hong90965502020-02-19 15:22:47 -08001279std::unique_ptr<AbstractAction>
1280DynamicPartitionControlAndroid::GetCleanupPreviousUpdateAction(
1281 BootControlInterface* boot_control,
1282 PrefsInterface* prefs,
1283 CleanupPreviousUpdateActionDelegateInterface* delegate) {
1284 if (!GetVirtualAbFeatureFlag().IsEnabled()) {
1285 return std::make_unique<NoOpAction>();
1286 }
1287 return std::make_unique<CleanupPreviousUpdateAction>(
1288 prefs, boot_control, snapshot_.get(), delegate);
1289}
1290
Yifan Hong6a6d0f12020-03-11 13:20:52 -07001291bool DynamicPartitionControlAndroid::ResetUpdate(PrefsInterface* prefs) {
1292 if (!GetVirtualAbFeatureFlag().IsEnabled()) {
1293 return true;
1294 }
Kelvin Zhangff5380b2022-03-16 13:35:04 -07001295 for (auto& list : dynamic_partition_list_) {
1296 list.clear();
1297 }
Yifan Hong6a6d0f12020-03-11 13:20:52 -07001298
1299 LOG(INFO) << __func__ << " resetting update state and deleting snapshots.";
1300 TEST_AND_RETURN_FALSE(prefs != nullptr);
1301
1302 // If the device has already booted into the target slot,
1303 // ResetUpdateProgress may pass but CancelUpdate fails.
1304 // This is expected. A scheduled CleanupPreviousUpdateAction should free
1305 // space when it is done.
1306 TEST_AND_RETURN_FALSE(DeltaPerformer::ResetUpdateProgress(
1307 prefs, false /* quick */, false /* skip dynamic partitions metadata */));
1308
Yifan Hong4d7c5eb2020-04-03 11:31:50 -07001309 if (ExpectMetadataMounted()) {
1310 TEST_AND_RETURN_FALSE(snapshot_->CancelUpdate());
1311 } else {
1312 LOG(INFO) << "Skip cancelling update in ResetUpdate because /metadata is "
1313 << "not mounted";
1314 }
Yifan Hong6a6d0f12020-03-11 13:20:52 -07001315
1316 return true;
1317}
1318
Tianjie99d570d2020-06-04 14:57:19 -07001319bool DynamicPartitionControlAndroid::ListDynamicPartitionsForSlot(
Tianjie3a55fc22021-02-13 16:02:22 -08001320 uint32_t slot,
1321 uint32_t current_slot,
1322 std::vector<std::string>* partitions) {
Kelvin Zhangebd115e2021-03-08 16:10:25 -05001323 CHECK(slot == source_slot_ || target_slot_ != UINT32_MAX)
1324 << " source slot: " << source_slot_ << " target slot: " << target_slot_
1325 << " slot: " << slot
1326 << " attempting to query dynamic partition metadata for target slot "
1327 "before PreparePartitionForUpdate() is called. The "
1328 "metadata in target slot isn't valid until "
1329 "PreparePartitionForUpdate() is called, contining execution would "
1330 "likely cause problems.";
Tianjie3a55fc22021-02-13 16:02:22 -08001331 bool slot_enables_dynamic_partitions =
1332 GetDynamicPartitionsFeatureFlag().IsEnabled();
1333 // Check if the target slot has dynamic partitions, this may happen when
1334 // applying a retrofit package.
1335 if (slot != current_slot) {
1336 slot_enables_dynamic_partitions =
1337 slot_enables_dynamic_partitions && is_target_dynamic_;
1338 }
1339
1340 if (!slot_enables_dynamic_partitions) {
1341 LOG(INFO) << "Dynamic partition is not enabled for slot " << slot;
1342 return true;
Tianjie99d570d2020-06-04 14:57:19 -07001343 }
1344
1345 std::string device_dir_str;
1346 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
1347 base::FilePath device_dir(device_dir_str);
Tianjie3a55fc22021-02-13 16:02:22 -08001348 auto super_device = device_dir.Append(GetSuperPartitionName(slot)).value();
1349 auto builder = LoadMetadataBuilder(super_device, slot);
Tianjie99d570d2020-06-04 14:57:19 -07001350 TEST_AND_RETURN_FALSE(builder != nullptr);
1351
1352 std::vector<std::string> result;
Tianjie3a55fc22021-02-13 16:02:22 -08001353 auto suffix = SlotSuffixForSlotNumber(slot);
Tianjie99d570d2020-06-04 14:57:19 -07001354 for (const auto& group : builder->ListGroups()) {
1355 for (const auto& partition : builder->ListPartitionsInGroup(group)) {
1356 std::string_view partition_name = partition->name();
1357 if (!android::base::ConsumeSuffix(&partition_name, suffix)) {
1358 continue;
1359 }
1360 result.emplace_back(partition_name);
1361 }
1362 }
1363 *partitions = std::move(result);
1364 return true;
1365}
1366
Tianjie24f96092020-06-30 12:26:25 -07001367bool DynamicPartitionControlAndroid::VerifyExtentsForUntouchedPartitions(
1368 uint32_t source_slot,
1369 uint32_t target_slot,
1370 const std::vector<std::string>& partitions) {
1371 std::string device_dir_str;
1372 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
1373 base::FilePath device_dir(device_dir_str);
1374
1375 auto source_super_device =
1376 device_dir.Append(GetSuperPartitionName(source_slot)).value();
1377 auto source_builder = LoadMetadataBuilder(source_super_device, source_slot);
1378 TEST_AND_RETURN_FALSE(source_builder != nullptr);
1379
1380 auto target_super_device =
1381 device_dir.Append(GetSuperPartitionName(target_slot)).value();
1382 auto target_builder = LoadMetadataBuilder(target_super_device, target_slot);
1383 TEST_AND_RETURN_FALSE(target_builder != nullptr);
1384
1385 return MetadataBuilder::VerifyExtentsAgainstSourceMetadata(
1386 *source_builder, source_slot, *target_builder, target_slot, partitions);
1387}
1388
Yifan Hong4d7c5eb2020-04-03 11:31:50 -07001389bool DynamicPartitionControlAndroid::ExpectMetadataMounted() {
1390 // No need to mount metadata for non-Virtual A/B devices.
1391 if (!GetVirtualAbFeatureFlag().IsEnabled()) {
1392 return false;
1393 }
1394 // Intentionally not checking |metadata_device_| in Android mode.
1395 // /metadata should always be mounted in Android mode. If it isn't, let caller
1396 // fails when calling into SnapshotManager.
1397 if (!IsRecovery()) {
1398 return true;
1399 }
1400 // In recovery mode, explicitly check |metadata_device_|.
1401 return metadata_device_ != nullptr;
1402}
1403
1404bool DynamicPartitionControlAndroid::EnsureMetadataMounted() {
1405 // No need to mount metadata for non-Virtual A/B devices.
1406 if (!GetVirtualAbFeatureFlag().IsEnabled()) {
1407 return true;
1408 }
1409
1410 if (metadata_device_ == nullptr) {
1411 metadata_device_ = snapshot_->EnsureMetadataMounted();
1412 }
1413 return metadata_device_ != nullptr;
1414}
1415
Kelvin Zhang34618522020-09-28 09:21:02 -04001416std::unique_ptr<android::snapshot::ISnapshotWriter>
1417DynamicPartitionControlAndroid::OpenCowWriter(
1418 const std::string& partition_name,
1419 const std::optional<std::string>& source_path,
1420 bool is_append) {
1421 auto suffix = SlotSuffixForSlotNumber(target_slot_);
1422
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -04001423 auto super_device = GetSuperDevice();
1424 if (!super_device.has_value()) {
Kelvin Zhang34618522020-09-28 09:21:02 -04001425 return nullptr;
1426 }
Kelvin Zhang34618522020-09-28 09:21:02 -04001427 CreateLogicalPartitionParams params = {
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -04001428 .block_device = super_device->value(),
Kelvin Zhang34618522020-09-28 09:21:02 -04001429 .metadata_slot = target_slot_,
1430 .partition_name = partition_name + suffix,
1431 .force_writable = true,
Kelvin Zhang877ddbe2020-11-09 13:37:56 -05001432 .timeout_ms = kMapSnapshotTimeout};
Kelvin Zhang34618522020-09-28 09:21:02 -04001433 // TODO(zhangkelvin) Open an APPEND mode CowWriter once there's an API to do
1434 // it.
1435 return snapshot_->OpenSnapshotWriter(params, std::move(source_path));
Kelvin Zhang877ddbe2020-11-09 13:37:56 -05001436} // namespace chromeos_update_engine
Kelvin Zhang34618522020-09-28 09:21:02 -04001437
Kelvin Zhang1a0ed712022-01-26 16:09:05 -08001438std::unique_ptr<FileDescriptor> DynamicPartitionControlAndroid::OpenCowFd(
Kelvin Zhangc82511c2020-11-06 16:01:24 -05001439 const std::string& unsuffixed_partition_name,
1440 const std::optional<std::string>& source_path,
1441 bool is_append) {
1442 auto cow_writer =
1443 OpenCowWriter(unsuffixed_partition_name, source_path, is_append);
1444 if (cow_writer == nullptr) {
1445 return nullptr;
1446 }
Kelvin Zhang21a49912021-03-12 14:28:33 -05001447 if (!cow_writer->InitializeAppend(kEndOfInstallLabel)) {
Kelvin Zhang77804b42021-12-30 09:04:54 -08001448 LOG(ERROR) << "Failed to InitializeAppend(" << kEndOfInstallLabel << ")";
Kelvin Zhang21a49912021-03-12 14:28:33 -05001449 return nullptr;
1450 }
Kelvin Zhang77804b42021-12-30 09:04:54 -08001451 auto reader = cow_writer->OpenReader();
1452 if (reader == nullptr) {
1453 LOG(ERROR) << "ICowWriter::OpenReader() failed.";
1454 return nullptr;
1455 }
Kelvin Zhang1a0ed712022-01-26 16:09:05 -08001456 return std::make_unique<CowWriterFileDescriptor>(std::move(cow_writer),
Kelvin Zhang77804b42021-12-30 09:04:54 -08001457 std::move(reader));
Kelvin Zhangc82511c2020-11-06 16:01:24 -05001458}
1459
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -04001460std::optional<base::FilePath> DynamicPartitionControlAndroid::GetSuperDevice() {
1461 std::string device_dir_str;
1462 if (!GetDeviceDir(&device_dir_str)) {
1463 LOG(ERROR) << "Failed to get device dir!";
1464 return {};
1465 }
1466 base::FilePath device_dir(device_dir_str);
1467 auto super_device = device_dir.Append(GetSuperPartitionName(target_slot_));
1468 return super_device;
1469}
1470
1471bool DynamicPartitionControlAndroid::MapAllPartitions() {
Kelvin Zhang877ddbe2020-11-09 13:37:56 -05001472 return snapshot_->MapAllSnapshots(kMapSnapshotTimeout);
Kelvin Zhang9d87d6d2020-10-23 17:03:59 -04001473}
1474
Kelvin Zhangeb9de162020-11-16 15:47:28 -05001475bool DynamicPartitionControlAndroid::IsDynamicPartition(
Kelvin Zhangebd115e2021-03-08 16:10:25 -05001476 const std::string& partition_name, uint32_t slot) {
1477 if (slot >= dynamic_partition_list_.size()) {
1478 LOG(ERROR) << "Seeing unexpected slot # " << slot << " currently assuming "
1479 << dynamic_partition_list_.size() << " slots";
1480 return false;
1481 }
1482 auto& dynamic_partition_list = dynamic_partition_list_[slot];
1483 if (dynamic_partition_list.empty() &&
Kelvin Zhangeb9de162020-11-16 15:47:28 -05001484 GetDynamicPartitionsFeatureFlag().IsEnabled()) {
Tianjie3a55fc22021-02-13 16:02:22 -08001485 // Use the DAP config of the target slot.
1486 CHECK(ListDynamicPartitionsForSlot(
Kelvin Zhangebd115e2021-03-08 16:10:25 -05001487 slot, source_slot_, &dynamic_partition_list));
Kelvin Zhangeb9de162020-11-16 15:47:28 -05001488 }
Kelvin Zhangebd115e2021-03-08 16:10:25 -05001489 return std::find(dynamic_partition_list.begin(),
1490 dynamic_partition_list.end(),
1491 partition_name) != dynamic_partition_list.end();
Kelvin Zhangeb9de162020-11-16 15:47:28 -05001492}
Kelvin Zhang91d95fa2020-11-05 13:52:00 -05001493
Yifan Hongb0cbd392021-02-04 11:11:45 -08001494bool DynamicPartitionControlAndroid::UpdateUsesSnapshotCompression() {
Kelvin Zhang741c2d42021-04-13 12:40:38 -04001495 return GetVirtualAbFeatureFlag().IsEnabled() &&
1496 snapshot_->UpdateUsesCompression();
Yifan Hongb0cbd392021-02-04 11:11:45 -08001497}
1498
Yifan Hong537802d2018-08-15 13:15:42 -07001499} // namespace chromeos_update_engine