blob: 1e92f45b3af5eb71018440a509cb1361177ae846 [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
17#include "update_engine/dynamic_partition_control_android.h"
18
Yifan Hong420db9b2019-07-23 20:50:33 -070019#include <chrono> // NOLINT(build/c++11) - using libsnapshot / liblp API
Yifan Hong13d41cb2019-09-16 13:18:22 -070020#include <map>
Yifan Hong537802d2018-08-15 13:15:42 -070021#include <memory>
22#include <set>
23#include <string>
Yifan Hong012508e2019-07-22 18:30:40 -070024#include <vector>
Yifan Hong537802d2018-08-15 13:15:42 -070025
26#include <android-base/properties.h>
27#include <android-base/strings.h>
28#include <base/files/file_util.h>
29#include <base/logging.h>
Yifan Hong012508e2019-07-22 18:30:40 -070030#include <base/strings/string_util.h>
Yifan Hong537802d2018-08-15 13:15:42 -070031#include <bootloader_message/bootloader_message.h>
Yifan Hong012508e2019-07-22 18:30:40 -070032#include <fs_mgr.h>
Yifan Hong537802d2018-08-15 13:15:42 -070033#include <fs_mgr_dm_linear.h>
Yifan Hong3a1a5612019-11-05 16:34:32 -080034#include <fs_mgr_overlayfs.h>
Yifan Hong29692902020-03-26 12:47:05 -070035#include <libavb/libavb.h>
Yifan Hong3a1a5612019-11-05 16:34:32 -080036#include <libdm/dm.h>
Yifan Hong420db9b2019-07-23 20:50:33 -070037#include <libsnapshot/snapshot.h>
Yifan Hong537802d2018-08-15 13:15:42 -070038
Yifan Hong90965502020-02-19 15:22:47 -080039#include "update_engine/cleanup_previous_update_action.h"
Yifan Hong537802d2018-08-15 13:15:42 -070040#include "update_engine/common/boot_control_interface.h"
41#include "update_engine/common/utils.h"
Yifan Hong012508e2019-07-22 18:30:40 -070042#include "update_engine/dynamic_partition_utils.h"
Yifan Hong6a6d0f12020-03-11 13:20:52 -070043#include "update_engine/payload_consumer/delta_performer.h"
Yifan Hong537802d2018-08-15 13:15:42 -070044
45using android::base::GetBoolProperty;
Yifan Hong29692902020-03-26 12:47:05 -070046using android::base::GetProperty;
Yifan Hong537802d2018-08-15 13:15:42 -070047using android::base::Join;
48using android::dm::DeviceMapper;
49using android::dm::DmDeviceState;
50using android::fs_mgr::CreateLogicalPartition;
David Andersonbb90dfb2019-08-13 14:14:56 -070051using android::fs_mgr::CreateLogicalPartitionParams;
Yifan Hong537802d2018-08-15 13:15:42 -070052using android::fs_mgr::DestroyLogicalPartition;
Yifan Hong29692902020-03-26 12:47:05 -070053using android::fs_mgr::Fstab;
Yifan Hong537802d2018-08-15 13:15:42 -070054using android::fs_mgr::MetadataBuilder;
Yifan Hong012508e2019-07-22 18:30:40 -070055using android::fs_mgr::Partition;
Yifan Hong6e706b12018-11-09 16:50:51 -080056using android::fs_mgr::PartitionOpener;
Yifan Hong012508e2019-07-22 18:30:40 -070057using android::fs_mgr::SlotSuffixForSlotNumber;
Yifan Hongf5261562020-03-10 10:28:10 -070058using android::snapshot::OptimizeSourceCopyOperation;
Yifan Hong0850bca2020-01-16 15:14:07 -080059using android::snapshot::Return;
Yifan Hongf033ecb2020-01-07 18:13:56 -080060using android::snapshot::SnapshotManager;
Yifan Hong2257ee12020-01-13 18:33:00 -080061using android::snapshot::UpdateState;
Yifan Hong537802d2018-08-15 13:15:42 -070062
63namespace chromeos_update_engine {
64
Yifan Hong6e706b12018-11-09 16:50:51 -080065constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
66constexpr char kRetrfoitDynamicPartitions[] =
67 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong413d5722019-07-23 14:21:09 -070068constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled";
69constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit";
Yifan Hong29692902020-03-26 12:47:05 -070070constexpr char kPostinstallFstabPrefix[] = "ro.postinstall.fstab.prefix";
Yifan Hong420db9b2019-07-23 20:50:33 -070071// Map timeout for dynamic partitions.
72constexpr std::chrono::milliseconds kMapTimeout{1000};
73// Map timeout for dynamic partitions with snapshots. Since several devices
74// needs to be mapped, this timeout is longer than |kMapTimeout|.
75constexpr std::chrono::milliseconds kMapSnapshotTimeout{5000};
76
Yifan Hongbae27842019-10-24 16:56:12 -070077#ifdef __ANDROID_RECOVERY__
78constexpr bool kIsRecovery = true;
79#else
80constexpr bool kIsRecovery = false;
81#endif
82
Yifan Hong537802d2018-08-15 13:15:42 -070083DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
Yifan Hongbae27842019-10-24 16:56:12 -070084 Cleanup();
Yifan Hong537802d2018-08-15 13:15:42 -070085}
86
Yifan Hong186bb682019-07-23 14:04:39 -070087static FeatureFlag GetFeatureFlag(const char* enable_prop,
88 const char* retrofit_prop) {
89 bool retrofit = GetBoolProperty(retrofit_prop, false);
90 bool enabled = GetBoolProperty(enable_prop, false);
91 if (retrofit && !enabled) {
92 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
93 << " is not. These sysprops are inconsistent. Assume that "
94 << enable_prop << " is true from now on.";
95 }
96 if (retrofit) {
97 return FeatureFlag(FeatureFlag::Value::RETROFIT);
98 }
99 if (enabled) {
100 return FeatureFlag(FeatureFlag::Value::LAUNCH);
101 }
102 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -0700103}
104
Yifan Hongb38e1af2019-10-17 14:59:22 -0700105DynamicPartitionControlAndroid::DynamicPartitionControlAndroid()
106 : dynamic_partitions_(
107 GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions)),
108 virtual_ab_(GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit)) {
109 if (GetVirtualAbFeatureFlag().IsEnabled()) {
Yifan Hongf033ecb2020-01-07 18:13:56 -0800110 snapshot_ = SnapshotManager::New();
Yifan Hongb38e1af2019-10-17 14:59:22 -0700111 CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
112 }
113}
114
Yifan Hong186bb682019-07-23 14:04:39 -0700115FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700116 return dynamic_partitions_;
Yifan Hong6e706b12018-11-09 16:50:51 -0800117}
118
Yifan Hong413d5722019-07-23 14:21:09 -0700119FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700120 return virtual_ab_;
Yifan Hong413d5722019-07-23 14:21:09 -0700121}
122
Yifan Hongf5261562020-03-10 10:28:10 -0700123bool DynamicPartitionControlAndroid::OptimizeOperation(
124 const std::string& partition_name,
125 const InstallOperation& operation,
126 InstallOperation* optimized) {
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000127 switch (operation.type()) {
128 case InstallOperation::SOURCE_COPY:
129 return target_supports_snapshot_ &&
130 GetVirtualAbFeatureFlag().IsEnabled() &&
Yifan Hong6eec9952019-12-04 13:12:01 -0800131 mapped_devices_.count(partition_name +
132 SlotSuffixForSlotNumber(target_slot_)) > 0 &&
Yifan Hongf5261562020-03-10 10:28:10 -0700133 OptimizeSourceCopyOperation(operation, optimized);
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000134 break;
135 default:
136 break;
137 }
Alessio Balsini14980e22019-11-26 11:46:06 +0000138 return false;
139}
140
Yifan Hong8546a712019-03-28 14:42:53 -0700141bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700142 const std::string& super_device,
143 const std::string& target_partition_name,
144 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700145 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700146 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700147 CreateLogicalPartitionParams params = {
148 .block_device = super_device,
149 .metadata_slot = slot,
150 .partition_name = target_partition_name,
151 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700152 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700153 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700154 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
155 force_writable) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700156 // Only target partitions are mapped with force_writable. On Virtual
157 // A/B devices, target partitions may overlap with source partitions, so
158 // they must be mapped with snapshot.
159 params.timeout_ms = kMapSnapshotTimeout;
160 success = snapshot_->MapUpdateSnapshot(params, path);
161 } else {
162 params.timeout_ms = kMapTimeout;
163 success = CreateLogicalPartition(params, path);
164 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700165
Yifan Hong420db9b2019-07-23 20:50:33 -0700166 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700167 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
168 << super_device << " on device mapper.";
169 return false;
170 }
171 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700172 << " to device mapper (force_writable = " << force_writable
173 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700174 mapped_devices_.insert(target_partition_name);
175 return true;
176}
177
Yifan Hong8546a712019-03-28 14:42:53 -0700178bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
179 const std::string& super_device,
180 const std::string& target_partition_name,
181 uint32_t slot,
182 bool force_writable,
183 std::string* path) {
184 DmDeviceState state = GetState(target_partition_name);
185 if (state == DmDeviceState::ACTIVE) {
186 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
187 if (GetDmDevicePathByName(target_partition_name, path)) {
188 LOG(INFO) << target_partition_name
189 << " is mapped on device mapper: " << *path;
190 return true;
191 }
192 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
193 return false;
194 }
195 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
196 // the device might be mapped incorrectly before. Attempt to unmap it.
197 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
198 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
199 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700200 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700201 LOG(ERROR) << target_partition_name
202 << " is mapped before the update, and it cannot be unmapped.";
203 return false;
204 }
205 state = GetState(target_partition_name);
206 if (state != DmDeviceState::INVALID) {
207 LOG(ERROR) << target_partition_name << " is unmapped but state is "
208 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
209 return false;
210 }
211 }
212 if (state == DmDeviceState::INVALID) {
213 return MapPartitionInternal(
214 super_device, target_partition_name, slot, force_writable, path);
215 }
216
217 LOG(ERROR) << target_partition_name
218 << " is mapped on device mapper but state is unknown: "
219 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
220 return false;
221}
222
Yifan Hong537802d2018-08-15 13:15:42 -0700223bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700224 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700225 if (DeviceMapper::Instance().GetState(target_partition_name) !=
226 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700227 // Partitions at target slot on non-Virtual A/B devices are mapped as
228 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
229 // preopt apps as dm-linear.
230 // Call DestroyLogicalPartition to handle these cases.
231 bool success = DestroyLogicalPartition(target_partition_name);
232
233 // On a Virtual A/B device, |target_partition_name| may be a leftover from
234 // a paused update. Clean up any underlying devices.
235 if (GetVirtualAbFeatureFlag().IsEnabled()) {
236 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
237 }
238
239 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700240 LOG(ERROR) << "Cannot unmap " << target_partition_name
241 << " from device mapper.";
242 return false;
243 }
244 LOG(INFO) << "Successfully unmapped " << target_partition_name
245 << " from device mapper.";
246 }
247 mapped_devices_.erase(target_partition_name);
248 return true;
249}
250
Yifan Hongbae27842019-10-24 16:56:12 -0700251void DynamicPartitionControlAndroid::UnmapAllPartitions() {
Tao Bao8c4d0082019-08-08 08:56:16 -0700252 if (mapped_devices_.empty()) {
253 return;
254 }
Yifan Hong537802d2018-08-15 13:15:42 -0700255 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
256 // a copy is needed for the loop.
257 std::set<std::string> mapped = mapped_devices_;
258 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
259 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700260 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700261 }
262}
263
264void DynamicPartitionControlAndroid::Cleanup() {
Yifan Hongbae27842019-10-24 16:56:12 -0700265 UnmapAllPartitions();
266 metadata_device_.reset();
Yifan Hong537802d2018-08-15 13:15:42 -0700267}
268
269bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
270 return base::PathExists(base::FilePath(path));
271}
272
273android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
274 const std::string& name) {
275 return DeviceMapper::Instance().GetState(name);
276}
277
278bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
279 const std::string& name, std::string* path) {
280 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
281}
282
283std::unique_ptr<MetadataBuilder>
284DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700285 const std::string& super_device, uint32_t source_slot) {
286 return LoadMetadataBuilder(
287 super_device, source_slot, BootControlInterface::kInvalidSlot);
288}
289
290std::unique_ptr<MetadataBuilder>
291DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800292 const std::string& super_device,
293 uint32_t source_slot,
294 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700295 std::unique_ptr<MetadataBuilder> builder;
296 if (target_slot == BootControlInterface::kInvalidSlot) {
297 builder =
298 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
299 } else {
Yifan Hong50a56c62019-10-14 19:35:05 -0700300 bool always_keep_source_slot = !target_supports_snapshot_;
301 builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
302 super_device,
303 source_slot,
304 target_slot,
305 always_keep_source_slot);
Yifan Hong30fa5f52019-08-05 16:39:59 -0700306 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800307
Yifan Hong537802d2018-08-15 13:15:42 -0700308 if (builder == nullptr) {
309 LOG(WARNING) << "No metadata slot "
310 << BootControlInterface::SlotName(source_slot) << " in "
311 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700312 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700313 }
314 LOG(INFO) << "Loaded metadata from slot "
315 << BootControlInterface::SlotName(source_slot) << " in "
316 << super_device;
317 return builder;
318}
319
320bool DynamicPartitionControlAndroid::StoreMetadata(
321 const std::string& super_device,
322 MetadataBuilder* builder,
323 uint32_t target_slot) {
324 auto metadata = builder->Export();
325 if (metadata == nullptr) {
326 LOG(ERROR) << "Cannot export metadata to slot "
327 << BootControlInterface::SlotName(target_slot) << " in "
328 << super_device;
329 return false;
330 }
331
Yifan Hong186bb682019-07-23 14:04:39 -0700332 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800333 if (!FlashPartitionTable(super_device, *metadata)) {
334 LOG(ERROR) << "Cannot write metadata to " << super_device;
335 return false;
336 }
337 LOG(INFO) << "Written metadata to " << super_device;
338 } else {
339 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
340 LOG(ERROR) << "Cannot write metadata to slot "
341 << BootControlInterface::SlotName(target_slot) << " in "
342 << super_device;
343 return false;
344 }
345 LOG(INFO) << "Copied metadata to slot "
346 << BootControlInterface::SlotName(target_slot) << " in "
347 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700348 }
349
Yifan Hong537802d2018-08-15 13:15:42 -0700350 return true;
351}
352
353bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
354 // We can't use fs_mgr to look up |partition_name| because fstab
355 // doesn't list every slot partition (it uses the slotselect option
356 // to mask the suffix).
357 //
358 // We can however assume that there's an entry for the /misc mount
359 // point and use that to get the device file for the misc
360 // partition. This helps us locate the disk that |partition_name|
361 // resides on. From there we'll assume that a by-name scheme is used
362 // so we can just replace the trailing "misc" by the given
363 // |partition_name| and suffix corresponding to |slot|, e.g.
364 //
365 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
366 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
367 //
368 // If needed, it's possible to relax the by-name assumption in the
369 // future by trawling /sys/block looking for the appropriate sibling
370 // of misc and then finding an entry in /dev matching the sysfs
371 // entry.
372
373 std::string err, misc_device = get_bootloader_message_blk_device(&err);
374 if (misc_device.empty()) {
375 LOG(ERROR) << "Unable to get misc block device: " << err;
376 return false;
377 }
378
379 if (!utils::IsSymlink(misc_device.c_str())) {
380 LOG(ERROR) << "Device file " << misc_device << " for /misc "
381 << "is not a symlink.";
382 return false;
383 }
384 *out = base::FilePath(misc_device).DirName().value();
385 return true;
386}
Yifan Hong012508e2019-07-22 18:30:40 -0700387
388bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
389 uint32_t source_slot,
390 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700391 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800392 bool update,
393 uint64_t* required_size) {
Yifan Hong6eec9952019-12-04 13:12:01 -0800394 source_slot_ = source_slot;
395 target_slot_ = target_slot;
Yifan Hongf033ecb2020-01-07 18:13:56 -0800396 if (required_size != nullptr) {
397 *required_size = 0;
398 }
Yifan Hong6eec9952019-12-04 13:12:01 -0800399
Yifan Hong3a1a5612019-11-05 16:34:32 -0800400 if (fs_mgr_overlayfs_is_setup()) {
401 // Non DAP devices can use overlayfs as well.
402 LOG(WARNING)
403 << "overlayfs overrides are active and can interfere with our "
404 "resources.\n"
405 << "run adb enable-verity to deactivate if required and try again.";
406 }
407
Yifan Hong29692902020-03-26 12:47:05 -0700408 if (GetVirtualAbFeatureFlag().IsEnabled() && metadata_device_ == nullptr) {
409 metadata_device_ = snapshot_->EnsureMetadataMounted();
410 TEST_AND_RETURN_FALSE(metadata_device_ != nullptr);
411 }
412
413 if (update) {
414 TEST_AND_RETURN_FALSE(EraseSystemOtherAvbFooter(source_slot, target_slot));
415 }
416
Yifan Hong3a1a5612019-11-05 16:34:32 -0800417 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
418 return true;
419 }
420
421 if (target_slot == source_slot) {
422 LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot.";
423 return false;
424 }
425
426 // Although the current build supports dynamic partitions, the given payload
427 // doesn't use it for target partitions. This could happen when applying a
428 // retrofit update. Skip updating the partition metadata for the target slot.
429 is_target_dynamic_ = !manifest.dynamic_partition_metadata().groups().empty();
430 if (!is_target_dynamic_) {
431 return true;
432 }
433
Yifan Hongf0f4a912019-09-26 17:51:33 -0700434 target_supports_snapshot_ =
435 manifest.dynamic_partition_metadata().snapshot_enabled();
436
437 if (!update)
438 return true;
439
Yifan Hongbae27842019-10-24 16:56:12 -0700440 bool delete_source = false;
441
Yifan Hong6d888562019-10-01 13:00:31 -0700442 if (GetVirtualAbFeatureFlag().IsEnabled()) {
443 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
444 // called before calling UnmapUpdateSnapshot.
445 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
446 // calls BeginUpdate() which resets update state
Yifan Hongbae27842019-10-24 16:56:12 -0700447 // - If !target_supports_snapshot_ or PrepareSnapshotPartitionsForUpdate
448 // failed in recovery, explicitly CancelUpdate().
Yifan Hong6d888562019-10-01 13:00:31 -0700449 if (target_supports_snapshot_) {
Yifan Hongbae27842019-10-24 16:56:12 -0700450 if (PrepareSnapshotPartitionsForUpdate(
451 source_slot, target_slot, manifest, required_size)) {
452 return true;
453 }
454
455 // Virtual A/B device doing Virtual A/B update in Android mode must use
456 // snapshots.
457 if (!IsRecovery()) {
458 LOG(ERROR) << "PrepareSnapshotPartitionsForUpdate failed in Android "
459 << "mode";
460 return false;
461 }
462
463 delete_source = true;
464 LOG(INFO) << "PrepareSnapshotPartitionsForUpdate failed in recovery. "
465 << "Attempt to overwrite existing partitions if possible";
466 } else {
467 // Downgrading to an non-Virtual A/B build or is secondary OTA.
468 LOG(INFO) << "Using regular A/B on Virtual A/B because package disabled "
469 << "snapshots.";
Yifan Hong6d888562019-10-01 13:00:31 -0700470 }
Yifan Hongbae27842019-10-24 16:56:12 -0700471
Yifan Hong6d888562019-10-01 13:00:31 -0700472 if (!snapshot_->CancelUpdate()) {
473 LOG(ERROR) << "Cannot cancel previous update.";
474 return false;
475 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700476 }
Yifan Hongbae27842019-10-24 16:56:12 -0700477
478 return PrepareDynamicPartitionsForUpdate(
479 source_slot, target_slot, manifest, delete_source);
Yifan Hong420db9b2019-07-23 20:50:33 -0700480}
481
Yifan Hong29692902020-03-26 12:47:05 -0700482namespace {
483// Try our best to erase AVB footer.
484class AvbFooterEraser {
485 public:
486 explicit AvbFooterEraser(const std::string& path) : path_(path) {}
487 bool Erase() {
488 // Try to mark the block device read-only. Ignore any
489 // failure since this won't work when passing regular files.
490 ignore_result(utils::SetBlockDeviceReadOnly(path_, false /* readonly */));
491
492 fd_.reset(new EintrSafeFileDescriptor());
493 int flags = O_WRONLY | O_TRUNC | O_CLOEXEC | O_SYNC;
494 TEST_AND_RETURN_FALSE(fd_->Open(path_.c_str(), flags));
495
496 // Need to write end-AVB_FOOTER_SIZE to end.
497 static_assert(AVB_FOOTER_SIZE > 0);
498 off64_t offset = fd_->Seek(-AVB_FOOTER_SIZE, SEEK_END);
499 TEST_AND_RETURN_FALSE_ERRNO(offset >= 0);
500 uint64_t write_size = AVB_FOOTER_SIZE;
501 LOG(INFO) << "Zeroing " << path_ << " @ [" << offset << ", "
502 << (offset + write_size) << "] (" << write_size << " bytes)";
503 brillo::Blob zeros(write_size);
504 TEST_AND_RETURN_FALSE(utils::WriteAll(fd_, zeros.data(), zeros.size()));
505 return true;
506 }
507 ~AvbFooterEraser() {
508 TEST_AND_RETURN(fd_ != nullptr && fd_->IsOpen());
509 if (!fd_->Close()) {
510 LOG(WARNING) << "Failed to close fd for " << path_;
511 }
512 }
513
514 private:
515 std::string path_;
516 FileDescriptorPtr fd_;
517};
518
519} // namespace
520
521std::optional<bool>
522DynamicPartitionControlAndroid::IsAvbEnabledOnSystemOther() {
523 auto prefix = GetProperty(kPostinstallFstabPrefix, "");
524 if (prefix.empty()) {
525 LOG(WARNING) << "Cannot get " << kPostinstallFstabPrefix;
526 return std::nullopt;
527 }
528 auto path = base::FilePath(prefix).Append("etc/fstab.postinstall").value();
529 return IsAvbEnabledInFstab(path);
530}
531
532std::optional<bool> DynamicPartitionControlAndroid::IsAvbEnabledInFstab(
533 const std::string& path) {
534 Fstab fstab;
535 if (!ReadFstabFromFile(path, &fstab)) {
536 LOG(WARNING) << "Cannot read fstab from " << path;
537 return std::nullopt;
538 }
539 for (const auto& entry : fstab) {
540 if (!entry.avb_keys.empty()) {
541 return true;
542 }
543 }
544 return false;
545}
546
547bool DynamicPartitionControlAndroid::GetSystemOtherPath(
548 uint32_t source_slot,
549 uint32_t target_slot,
550 const std::string& partition_name_suffix,
551 std::string* path,
552 bool* should_unmap) {
553 path->clear();
554 *should_unmap = false;
555
556 // In recovery, just erase no matter what.
557 // - On devices with retrofit dynamic partitions, no logical partitions
558 // should be mounted at this point. Hence it should be safe to erase.
559 // Otherwise, do check that AVB is enabled on system_other before erasing.
560 if (!IsRecovery()) {
561 auto has_avb = IsAvbEnabledOnSystemOther();
562 TEST_AND_RETURN_FALSE(has_avb.has_value());
563 if (!has_avb.value()) {
564 LOG(INFO) << "AVB is not enabled on system_other. Skip erasing.";
565 return true;
566 }
567
568 // Found unexpected avb_keys for system_other on devices retrofitting
569 // dynamic partitions. Previous crash in update_engine may leave logical
570 // partitions mapped on physical system_other partition. It is difficult to
571 // handle these cases. Just fail.
572 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
573 LOG(ERROR) << "Cannot erase AVB footer on system_other on devices with "
574 << "retrofit dynamic partitions. They should not have AVB "
575 << "enabled on system_other.";
576 return false;
577 }
578 }
579
580 std::string device_dir_str;
581 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
582 base::FilePath device_dir(device_dir_str);
583
584 // On devices without dynamic partition, search for static partitions.
585 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
586 *path = device_dir.Append(partition_name_suffix).value();
587 TEST_AND_RETURN_FALSE(DeviceExists(*path));
588 return true;
589 }
590
591 auto source_super_device =
592 device_dir.Append(GetSuperPartitionName(source_slot)).value();
593
594 auto builder = LoadMetadataBuilder(source_super_device, source_slot);
595 if (builder == nullptr) {
596 if (IsRecovery()) {
597 // It might be corrupted for some reason. It should still be able to
598 // sideload.
599 LOG(WARNING) << "Super partition metadata cannot be read from the source "
600 << "slot, skip erasing.";
601 return true;
602 } else {
603 // Device has booted into Android mode, indicating that the super
604 // partition metadata should be there.
605 LOG(ERROR) << "Super partition metadata cannot be read from the source "
606 << "slot. This is unexpected on devices with dynamic "
607 << "partitions enabled.";
608 return false;
609 }
610 }
611 auto p = builder->FindPartition(partition_name_suffix);
612 if (p == nullptr) {
613 // If the source slot is flashed without system_other, it does not exist
614 // in super partition metadata at source slot. It is safe to skip it.
615 LOG(INFO) << "Can't find " << partition_name_suffix
616 << " in metadata source slot, skip erasing.";
617 return true;
618 }
619 // System_other created by flashing tools should be erased.
620 // If partition is created by update_engine (via NewForUpdate), it is a
621 // left-over partition from the previous update and does not contain
622 // system_other, hence there is no need to erase.
623 // Note the reverse is not necessary true. If the flag is not set, we don't
624 // know if the partition is created by update_engine or by flashing tools
625 // because older versions of super partition metadata does not contain this
626 // flag. It is okay to erase the AVB footer anyways.
627 if (p->attributes() & LP_PARTITION_ATTR_UPDATED) {
628 LOG(INFO) << partition_name_suffix
629 << " does not contain system_other, skip erasing.";
630 return true;
631 }
632
633 // Delete any pre-existing device with name |partition_name_suffix| and
634 // also remove it from |mapped_devices_|.
635 TEST_AND_RETURN_FALSE(UnmapPartitionOnDeviceMapper(partition_name_suffix));
636 // Use CreateLogicalPartition directly to avoid mapping with existing
637 // snapshots.
638 CreateLogicalPartitionParams params = {
639 .block_device = source_super_device,
640 .metadata_slot = source_slot,
641 .partition_name = partition_name_suffix,
642 .force_writable = true,
643 .timeout_ms = kMapTimeout,
644 };
645 TEST_AND_RETURN_FALSE(CreateLogicalPartition(params, path));
646 *should_unmap = true;
647 return true;
648}
649
650bool DynamicPartitionControlAndroid::EraseSystemOtherAvbFooter(
651 uint32_t source_slot, uint32_t target_slot) {
652 LOG(INFO) << "Erasing AVB footer of system_other partition before update.";
653
654 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
655 const std::string partition_name_suffix = "system" + target_suffix;
656
657 std::string path;
658 bool should_unmap = false;
659
660 TEST_AND_RETURN_FALSE(GetSystemOtherPath(
661 source_slot, target_slot, partition_name_suffix, &path, &should_unmap));
662
663 if (path.empty()) {
664 return true;
665 }
666
667 bool ret = AvbFooterEraser(path).Erase();
668
669 // Delete |partition_name_suffix| from device mapper and from
670 // |mapped_devices_| again so that it does not interfere with update process.
671 if (should_unmap) {
672 TEST_AND_RETURN_FALSE(UnmapPartitionOnDeviceMapper(partition_name_suffix));
673 }
674
675 return ret;
676}
677
Yifan Hong420db9b2019-07-23 20:50:33 -0700678bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
679 uint32_t source_slot,
680 uint32_t target_slot,
Yifan Hongbae27842019-10-24 16:56:12 -0700681 const DeltaArchiveManifest& manifest,
682 bool delete_source) {
Yifan Hong012508e2019-07-22 18:30:40 -0700683 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
684
685 // Unmap all the target dynamic partitions because they would become
686 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700687 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
688 for (const auto& partition_name : group.partition_names()) {
689 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700690 return false;
691 }
692 }
693 }
694
695 std::string device_dir_str;
696 if (!GetDeviceDir(&device_dir_str)) {
697 return false;
698 }
699 base::FilePath device_dir(device_dir_str);
700 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700701 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700702
703 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
704 if (builder == nullptr) {
705 LOG(ERROR) << "No metadata at "
706 << BootControlInterface::SlotName(source_slot);
707 return false;
708 }
709
Yifan Hongbae27842019-10-24 16:56:12 -0700710 if (delete_source) {
711 TEST_AND_RETURN_FALSE(
712 DeleteSourcePartitions(builder.get(), source_slot, manifest));
713 }
714
Yifan Hong13d41cb2019-09-16 13:18:22 -0700715 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700716 return false;
717 }
718
719 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700720 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700721 return StoreMetadata(target_device, builder.get(), target_slot);
722}
723
Yifan Hong420db9b2019-07-23 20:50:33 -0700724bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
725 uint32_t source_slot,
726 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800727 const DeltaArchiveManifest& manifest,
728 uint64_t* required_size) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700729 if (!snapshot_->BeginUpdate()) {
730 LOG(ERROR) << "Cannot begin new update.";
731 return false;
732 }
Yifan Hongf033ecb2020-01-07 18:13:56 -0800733 auto ret = snapshot_->CreateUpdateSnapshots(manifest);
734 if (!ret) {
735 LOG(ERROR) << "Cannot create update snapshots: " << ret.string();
736 if (required_size != nullptr &&
Yifan Hong0850bca2020-01-16 15:14:07 -0800737 ret.error_code() == Return::ErrorCode::NO_SPACE) {
Yifan Hongf033ecb2020-01-07 18:13:56 -0800738 *required_size = ret.required_size();
739 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700740 return false;
741 }
742 return true;
743}
744
Yifan Hong700d7c12019-07-23 20:49:16 -0700745std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
746 uint32_t slot) {
747 return fs_mgr_get_super_partition_name(slot);
748}
749
Yifan Hong012508e2019-07-22 18:30:40 -0700750bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
751 MetadataBuilder* builder,
752 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700753 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700754 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
755 // COW group needs to be deleted to ensure there are enough space to create
756 // target partitions.
757 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
758
Yifan Hong012508e2019-07-22 18:30:40 -0700759 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
760 DeleteGroupsWithSuffix(builder, target_suffix);
761
762 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700763 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
764 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700765 }
766
767 std::string expr;
768 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700769 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700770 allocatable_space /= 2;
771 expr = "half of ";
772 }
773 if (total_size > allocatable_space) {
774 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
775 << " (" << total_size << ") has exceeded " << expr
776 << "allocatable space for dynamic partitions "
777 << allocatable_space << ".";
778 return false;
779 }
780
Yifan Hong13d41cb2019-09-16 13:18:22 -0700781 // name of partition(e.g. "system") -> size in bytes
782 std::map<std::string, uint64_t> partition_sizes;
783 for (const auto& partition : manifest.partitions()) {
784 partition_sizes.emplace(partition.partition_name(),
785 partition.new_partition_info().size());
786 }
787
788 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
789 auto group_name_suffix = group.name() + target_suffix;
790 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700791 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700792 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700793 return false;
794 }
795 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700796 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700797
Yifan Hong13d41cb2019-09-16 13:18:22 -0700798 for (const auto& partition_name : group.partition_names()) {
799 auto partition_sizes_it = partition_sizes.find(partition_name);
800 if (partition_sizes_it == partition_sizes.end()) {
801 // TODO(tbao): Support auto-filling partition info for framework-only
802 // OTA.
803 LOG(ERROR) << "dynamic_partition_metadata contains partition "
804 << partition_name << " but it is not part of the manifest. "
805 << "This is not supported.";
806 return false;
807 }
808 uint64_t partition_size = partition_sizes_it->second;
809
810 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700811 Partition* p = builder->AddPartition(
812 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
813 if (!p) {
814 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
815 << " to group " << group_name_suffix;
816 return false;
817 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700818 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700819 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700820 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700821 return false;
822 }
823 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700824 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700825 }
826 }
827
828 return true;
829}
830
Yifan Hong7b3910a2020-03-24 17:47:32 -0700831bool DynamicPartitionControlAndroid::FinishUpdate(bool powerwash_required) {
Yifan Hongd66ecf12020-02-04 11:15:50 -0800832 if (GetVirtualAbFeatureFlag().IsEnabled() &&
833 snapshot_->GetUpdateState() == UpdateState::Initiated) {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700834 LOG(INFO) << "Snapshot writes are done.";
Yifan Hong7b3910a2020-03-24 17:47:32 -0700835 return snapshot_->FinishedSnapshotWrites(powerwash_required);
Yifan Hongf0f4a912019-09-26 17:51:33 -0700836 }
837 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700838}
839
Yifan Hong3a1a5612019-11-05 16:34:32 -0800840bool DynamicPartitionControlAndroid::GetPartitionDevice(
841 const std::string& partition_name,
842 uint32_t slot,
843 uint32_t current_slot,
844 std::string* device) {
845 const auto& partition_name_suffix =
846 partition_name + SlotSuffixForSlotNumber(slot);
847 std::string device_dir_str;
848 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
849 base::FilePath device_dir(device_dir_str);
850
851 // When looking up target partition devices, treat them as static if the
852 // current payload doesn't encode them as dynamic partitions. This may happen
853 // when applying a retrofit update on top of a dynamic-partitions-enabled
854 // build.
855 if (GetDynamicPartitionsFeatureFlag().IsEnabled() &&
856 (slot == current_slot || is_target_dynamic_)) {
857 switch (GetDynamicPartitionDevice(
858 device_dir, partition_name_suffix, slot, current_slot, device)) {
859 case DynamicPartitionDeviceStatus::SUCCESS:
860 return true;
861 case DynamicPartitionDeviceStatus::TRY_STATIC:
862 break;
863 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
864 default:
865 return false;
866 }
867 }
868 base::FilePath path = device_dir.Append(partition_name_suffix);
869 if (!DeviceExists(path.value())) {
870 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
871 return false;
872 }
873
874 *device = path.value();
875 return true;
876}
877
878bool DynamicPartitionControlAndroid::IsSuperBlockDevice(
879 const base::FilePath& device_dir,
880 uint32_t current_slot,
881 const std::string& partition_name_suffix) {
882 std::string source_device =
883 device_dir.Append(GetSuperPartitionName(current_slot)).value();
884 auto source_metadata = LoadMetadataBuilder(source_device, current_slot);
885 return source_metadata->HasBlockDevice(partition_name_suffix);
886}
887
888DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus
889DynamicPartitionControlAndroid::GetDynamicPartitionDevice(
890 const base::FilePath& device_dir,
891 const std::string& partition_name_suffix,
892 uint32_t slot,
893 uint32_t current_slot,
894 std::string* device) {
895 std::string super_device =
896 device_dir.Append(GetSuperPartitionName(slot)).value();
897
898 auto builder = LoadMetadataBuilder(super_device, slot);
899 if (builder == nullptr) {
900 LOG(ERROR) << "No metadata in slot "
901 << BootControlInterface::SlotName(slot);
902 return DynamicPartitionDeviceStatus::ERROR;
903 }
904 if (builder->FindPartition(partition_name_suffix) == nullptr) {
905 LOG(INFO) << partition_name_suffix
906 << " is not in super partition metadata.";
907
908 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
909 LOG(ERROR) << "The static partition " << partition_name_suffix
910 << " is a block device for current metadata."
911 << "It cannot be used as a logical partition.";
912 return DynamicPartitionDeviceStatus::ERROR;
913 }
914
915 return DynamicPartitionDeviceStatus::TRY_STATIC;
916 }
917
918 if (slot == current_slot) {
919 if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) {
920 LOG(WARNING) << partition_name_suffix << " is at current slot but it is "
921 << "not mapped. Now try to map it.";
922 } else {
923 if (GetDmDevicePathByName(partition_name_suffix, device)) {
924 LOG(INFO) << partition_name_suffix
925 << " is mapped on device mapper: " << *device;
926 return DynamicPartitionDeviceStatus::SUCCESS;
927 }
928 LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown.";
929 return DynamicPartitionDeviceStatus::ERROR;
930 }
931 }
932
933 bool force_writable = slot != current_slot;
934 if (MapPartitionOnDeviceMapper(
935 super_device, partition_name_suffix, slot, force_writable, device)) {
936 return DynamicPartitionDeviceStatus::SUCCESS;
937 }
938 return DynamicPartitionDeviceStatus::ERROR;
939}
940
Yifan Hong6eec9952019-12-04 13:12:01 -0800941void DynamicPartitionControlAndroid::set_fake_mapped_devices(
942 const std::set<std::string>& fake) {
943 mapped_devices_ = fake;
944}
945
Yifan Hongbae27842019-10-24 16:56:12 -0700946bool DynamicPartitionControlAndroid::IsRecovery() {
947 return kIsRecovery;
948}
949
950static bool IsIncrementalUpdate(const DeltaArchiveManifest& manifest) {
951 const auto& partitions = manifest.partitions();
952 return std::any_of(partitions.begin(), partitions.end(), [](const auto& p) {
953 return p.has_old_partition_info();
954 });
955}
956
957bool DynamicPartitionControlAndroid::DeleteSourcePartitions(
958 MetadataBuilder* builder,
959 uint32_t source_slot,
960 const DeltaArchiveManifest& manifest) {
961 TEST_AND_RETURN_FALSE(IsRecovery());
962
963 if (IsIncrementalUpdate(manifest)) {
964 LOG(ERROR) << "Cannot sideload incremental OTA because snapshots cannot "
965 << "be created.";
966 if (GetVirtualAbFeatureFlag().IsLaunch()) {
967 LOG(ERROR) << "Sideloading incremental updates on devices launches "
968 << " Virtual A/B is not supported.";
969 }
970 return false;
971 }
972
973 LOG(INFO) << "Will overwrite existing partitions. Slot "
974 << BootControlInterface::SlotName(source_slot)
975 << "may be unbootable until update finishes!";
976 const std::string source_suffix = SlotSuffixForSlotNumber(source_slot);
977 DeleteGroupsWithSuffix(builder, source_suffix);
978
979 return true;
980}
981
Yifan Hong90965502020-02-19 15:22:47 -0800982std::unique_ptr<AbstractAction>
983DynamicPartitionControlAndroid::GetCleanupPreviousUpdateAction(
984 BootControlInterface* boot_control,
985 PrefsInterface* prefs,
986 CleanupPreviousUpdateActionDelegateInterface* delegate) {
987 if (!GetVirtualAbFeatureFlag().IsEnabled()) {
988 return std::make_unique<NoOpAction>();
989 }
990 return std::make_unique<CleanupPreviousUpdateAction>(
991 prefs, boot_control, snapshot_.get(), delegate);
992}
993
Yifan Hong6a6d0f12020-03-11 13:20:52 -0700994bool DynamicPartitionControlAndroid::ResetUpdate(PrefsInterface* prefs) {
995 if (!GetVirtualAbFeatureFlag().IsEnabled()) {
996 return true;
997 }
998
999 LOG(INFO) << __func__ << " resetting update state and deleting snapshots.";
1000 TEST_AND_RETURN_FALSE(prefs != nullptr);
1001
1002 // If the device has already booted into the target slot,
1003 // ResetUpdateProgress may pass but CancelUpdate fails.
1004 // This is expected. A scheduled CleanupPreviousUpdateAction should free
1005 // space when it is done.
1006 TEST_AND_RETURN_FALSE(DeltaPerformer::ResetUpdateProgress(
1007 prefs, false /* quick */, false /* skip dynamic partitions metadata */));
1008
1009 TEST_AND_RETURN_FALSE(snapshot_->CancelUpdate());
1010
1011 return true;
1012}
1013
Yifan Hong537802d2018-08-15 13:15:42 -07001014} // namespace chromeos_update_engine