blob: d75a8fc7678d6776c99aa751d2a118fdb41346bb [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>
35#include <libdm/dm.h>
Yifan Hong420db9b2019-07-23 20:50:33 -070036#include <libsnapshot/snapshot.h>
Yifan Hong537802d2018-08-15 13:15:42 -070037
Yifan Hong90965502020-02-19 15:22:47 -080038#include "update_engine/cleanup_previous_update_action.h"
Yifan Hong537802d2018-08-15 13:15:42 -070039#include "update_engine/common/boot_control_interface.h"
40#include "update_engine/common/utils.h"
Yifan Hong012508e2019-07-22 18:30:40 -070041#include "update_engine/dynamic_partition_utils.h"
Yifan Hong537802d2018-08-15 13:15:42 -070042
43using android::base::GetBoolProperty;
44using android::base::Join;
45using android::dm::DeviceMapper;
46using android::dm::DmDeviceState;
47using android::fs_mgr::CreateLogicalPartition;
David Andersonbb90dfb2019-08-13 14:14:56 -070048using android::fs_mgr::CreateLogicalPartitionParams;
Yifan Hong537802d2018-08-15 13:15:42 -070049using android::fs_mgr::DestroyLogicalPartition;
50using android::fs_mgr::MetadataBuilder;
Yifan Hong012508e2019-07-22 18:30:40 -070051using android::fs_mgr::Partition;
Yifan Hong6e706b12018-11-09 16:50:51 -080052using android::fs_mgr::PartitionOpener;
Yifan Hong012508e2019-07-22 18:30:40 -070053using android::fs_mgr::SlotSuffixForSlotNumber;
Yifan Hongf5261562020-03-10 10:28:10 -070054using android::snapshot::OptimizeSourceCopyOperation;
Yifan Hong0850bca2020-01-16 15:14:07 -080055using android::snapshot::Return;
Yifan Hongf033ecb2020-01-07 18:13:56 -080056using android::snapshot::SnapshotManager;
Yifan Hong2257ee12020-01-13 18:33:00 -080057using android::snapshot::UpdateState;
Yifan Hong537802d2018-08-15 13:15:42 -070058
59namespace chromeos_update_engine {
60
Yifan Hong6e706b12018-11-09 16:50:51 -080061constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
62constexpr char kRetrfoitDynamicPartitions[] =
63 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong413d5722019-07-23 14:21:09 -070064constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled";
65constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit";
Yifan Hong420db9b2019-07-23 20:50:33 -070066// Map timeout for dynamic partitions.
67constexpr std::chrono::milliseconds kMapTimeout{1000};
68// Map timeout for dynamic partitions with snapshots. Since several devices
69// needs to be mapped, this timeout is longer than |kMapTimeout|.
70constexpr std::chrono::milliseconds kMapSnapshotTimeout{5000};
71
Yifan Hongbae27842019-10-24 16:56:12 -070072#ifdef __ANDROID_RECOVERY__
73constexpr bool kIsRecovery = true;
74#else
75constexpr bool kIsRecovery = false;
76#endif
77
Yifan Hong537802d2018-08-15 13:15:42 -070078DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
Yifan Hongbae27842019-10-24 16:56:12 -070079 Cleanup();
Yifan Hong537802d2018-08-15 13:15:42 -070080}
81
Yifan Hong186bb682019-07-23 14:04:39 -070082static FeatureFlag GetFeatureFlag(const char* enable_prop,
83 const char* retrofit_prop) {
84 bool retrofit = GetBoolProperty(retrofit_prop, false);
85 bool enabled = GetBoolProperty(enable_prop, false);
86 if (retrofit && !enabled) {
87 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
88 << " is not. These sysprops are inconsistent. Assume that "
89 << enable_prop << " is true from now on.";
90 }
91 if (retrofit) {
92 return FeatureFlag(FeatureFlag::Value::RETROFIT);
93 }
94 if (enabled) {
95 return FeatureFlag(FeatureFlag::Value::LAUNCH);
96 }
97 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070098}
99
Yifan Hongb38e1af2019-10-17 14:59:22 -0700100DynamicPartitionControlAndroid::DynamicPartitionControlAndroid()
101 : dynamic_partitions_(
102 GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions)),
103 virtual_ab_(GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit)) {
104 if (GetVirtualAbFeatureFlag().IsEnabled()) {
Yifan Hongf033ecb2020-01-07 18:13:56 -0800105 snapshot_ = SnapshotManager::New();
Yifan Hongb38e1af2019-10-17 14:59:22 -0700106 CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
107 }
108}
109
Yifan Hong186bb682019-07-23 14:04:39 -0700110FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700111 return dynamic_partitions_;
Yifan Hong6e706b12018-11-09 16:50:51 -0800112}
113
Yifan Hong413d5722019-07-23 14:21:09 -0700114FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700115 return virtual_ab_;
Yifan Hong413d5722019-07-23 14:21:09 -0700116}
117
Yifan Hongf5261562020-03-10 10:28:10 -0700118bool DynamicPartitionControlAndroid::OptimizeOperation(
119 const std::string& partition_name,
120 const InstallOperation& operation,
121 InstallOperation* optimized) {
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000122 switch (operation.type()) {
123 case InstallOperation::SOURCE_COPY:
124 return target_supports_snapshot_ &&
125 GetVirtualAbFeatureFlag().IsEnabled() &&
Yifan Hong6eec9952019-12-04 13:12:01 -0800126 mapped_devices_.count(partition_name +
127 SlotSuffixForSlotNumber(target_slot_)) > 0 &&
Yifan Hongf5261562020-03-10 10:28:10 -0700128 OptimizeSourceCopyOperation(operation, optimized);
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000129 break;
130 default:
131 break;
132 }
Alessio Balsini14980e22019-11-26 11:46:06 +0000133 return false;
134}
135
Yifan Hong8546a712019-03-28 14:42:53 -0700136bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700137 const std::string& super_device,
138 const std::string& target_partition_name,
139 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700140 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700141 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700142 CreateLogicalPartitionParams params = {
143 .block_device = super_device,
144 .metadata_slot = slot,
145 .partition_name = target_partition_name,
146 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700147 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700148 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700149 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
150 force_writable) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700151 // Only target partitions are mapped with force_writable. On Virtual
152 // A/B devices, target partitions may overlap with source partitions, so
153 // they must be mapped with snapshot.
154 params.timeout_ms = kMapSnapshotTimeout;
155 success = snapshot_->MapUpdateSnapshot(params, path);
156 } else {
157 params.timeout_ms = kMapTimeout;
158 success = CreateLogicalPartition(params, path);
159 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700160
Yifan Hong420db9b2019-07-23 20:50:33 -0700161 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700162 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
163 << super_device << " on device mapper.";
164 return false;
165 }
166 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700167 << " to device mapper (force_writable = " << force_writable
168 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700169 mapped_devices_.insert(target_partition_name);
170 return true;
171}
172
Yifan Hong8546a712019-03-28 14:42:53 -0700173bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
174 const std::string& super_device,
175 const std::string& target_partition_name,
176 uint32_t slot,
177 bool force_writable,
178 std::string* path) {
179 DmDeviceState state = GetState(target_partition_name);
180 if (state == DmDeviceState::ACTIVE) {
181 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
182 if (GetDmDevicePathByName(target_partition_name, path)) {
183 LOG(INFO) << target_partition_name
184 << " is mapped on device mapper: " << *path;
185 return true;
186 }
187 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
188 return false;
189 }
190 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
191 // the device might be mapped incorrectly before. Attempt to unmap it.
192 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
193 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
194 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700195 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700196 LOG(ERROR) << target_partition_name
197 << " is mapped before the update, and it cannot be unmapped.";
198 return false;
199 }
200 state = GetState(target_partition_name);
201 if (state != DmDeviceState::INVALID) {
202 LOG(ERROR) << target_partition_name << " is unmapped but state is "
203 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
204 return false;
205 }
206 }
207 if (state == DmDeviceState::INVALID) {
208 return MapPartitionInternal(
209 super_device, target_partition_name, slot, force_writable, path);
210 }
211
212 LOG(ERROR) << target_partition_name
213 << " is mapped on device mapper but state is unknown: "
214 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
215 return false;
216}
217
Yifan Hong537802d2018-08-15 13:15:42 -0700218bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700219 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700220 if (DeviceMapper::Instance().GetState(target_partition_name) !=
221 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700222 // Partitions at target slot on non-Virtual A/B devices are mapped as
223 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
224 // preopt apps as dm-linear.
225 // Call DestroyLogicalPartition to handle these cases.
226 bool success = DestroyLogicalPartition(target_partition_name);
227
228 // On a Virtual A/B device, |target_partition_name| may be a leftover from
229 // a paused update. Clean up any underlying devices.
230 if (GetVirtualAbFeatureFlag().IsEnabled()) {
231 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
232 }
233
234 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700235 LOG(ERROR) << "Cannot unmap " << target_partition_name
236 << " from device mapper.";
237 return false;
238 }
239 LOG(INFO) << "Successfully unmapped " << target_partition_name
240 << " from device mapper.";
241 }
242 mapped_devices_.erase(target_partition_name);
243 return true;
244}
245
Yifan Hongbae27842019-10-24 16:56:12 -0700246void DynamicPartitionControlAndroid::UnmapAllPartitions() {
Tao Bao8c4d0082019-08-08 08:56:16 -0700247 if (mapped_devices_.empty()) {
248 return;
249 }
Yifan Hong537802d2018-08-15 13:15:42 -0700250 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
251 // a copy is needed for the loop.
252 std::set<std::string> mapped = mapped_devices_;
253 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
254 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700255 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700256 }
257}
258
259void DynamicPartitionControlAndroid::Cleanup() {
Yifan Hongbae27842019-10-24 16:56:12 -0700260 UnmapAllPartitions();
261 metadata_device_.reset();
Yifan Hong537802d2018-08-15 13:15:42 -0700262}
263
264bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
265 return base::PathExists(base::FilePath(path));
266}
267
268android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
269 const std::string& name) {
270 return DeviceMapper::Instance().GetState(name);
271}
272
273bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
274 const std::string& name, std::string* path) {
275 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
276}
277
278std::unique_ptr<MetadataBuilder>
279DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700280 const std::string& super_device, uint32_t source_slot) {
281 return LoadMetadataBuilder(
282 super_device, source_slot, BootControlInterface::kInvalidSlot);
283}
284
285std::unique_ptr<MetadataBuilder>
286DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800287 const std::string& super_device,
288 uint32_t source_slot,
289 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700290 std::unique_ptr<MetadataBuilder> builder;
291 if (target_slot == BootControlInterface::kInvalidSlot) {
292 builder =
293 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
294 } else {
Yifan Hong50a56c62019-10-14 19:35:05 -0700295 bool always_keep_source_slot = !target_supports_snapshot_;
296 builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
297 super_device,
298 source_slot,
299 target_slot,
300 always_keep_source_slot);
Yifan Hong30fa5f52019-08-05 16:39:59 -0700301 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800302
Yifan Hong537802d2018-08-15 13:15:42 -0700303 if (builder == nullptr) {
304 LOG(WARNING) << "No metadata slot "
305 << BootControlInterface::SlotName(source_slot) << " in "
306 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700307 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700308 }
309 LOG(INFO) << "Loaded metadata from slot "
310 << BootControlInterface::SlotName(source_slot) << " in "
311 << super_device;
312 return builder;
313}
314
315bool DynamicPartitionControlAndroid::StoreMetadata(
316 const std::string& super_device,
317 MetadataBuilder* builder,
318 uint32_t target_slot) {
319 auto metadata = builder->Export();
320 if (metadata == nullptr) {
321 LOG(ERROR) << "Cannot export metadata to slot "
322 << BootControlInterface::SlotName(target_slot) << " in "
323 << super_device;
324 return false;
325 }
326
Yifan Hong186bb682019-07-23 14:04:39 -0700327 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800328 if (!FlashPartitionTable(super_device, *metadata)) {
329 LOG(ERROR) << "Cannot write metadata to " << super_device;
330 return false;
331 }
332 LOG(INFO) << "Written metadata to " << super_device;
333 } else {
334 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
335 LOG(ERROR) << "Cannot write metadata to slot "
336 << BootControlInterface::SlotName(target_slot) << " in "
337 << super_device;
338 return false;
339 }
340 LOG(INFO) << "Copied metadata to slot "
341 << BootControlInterface::SlotName(target_slot) << " in "
342 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700343 }
344
Yifan Hong537802d2018-08-15 13:15:42 -0700345 return true;
346}
347
348bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
349 // We can't use fs_mgr to look up |partition_name| because fstab
350 // doesn't list every slot partition (it uses the slotselect option
351 // to mask the suffix).
352 //
353 // We can however assume that there's an entry for the /misc mount
354 // point and use that to get the device file for the misc
355 // partition. This helps us locate the disk that |partition_name|
356 // resides on. From there we'll assume that a by-name scheme is used
357 // so we can just replace the trailing "misc" by the given
358 // |partition_name| and suffix corresponding to |slot|, e.g.
359 //
360 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
361 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
362 //
363 // If needed, it's possible to relax the by-name assumption in the
364 // future by trawling /sys/block looking for the appropriate sibling
365 // of misc and then finding an entry in /dev matching the sysfs
366 // entry.
367
368 std::string err, misc_device = get_bootloader_message_blk_device(&err);
369 if (misc_device.empty()) {
370 LOG(ERROR) << "Unable to get misc block device: " << err;
371 return false;
372 }
373
374 if (!utils::IsSymlink(misc_device.c_str())) {
375 LOG(ERROR) << "Device file " << misc_device << " for /misc "
376 << "is not a symlink.";
377 return false;
378 }
379 *out = base::FilePath(misc_device).DirName().value();
380 return true;
381}
Yifan Hong012508e2019-07-22 18:30:40 -0700382
383bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
384 uint32_t source_slot,
385 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700386 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800387 bool update,
388 uint64_t* required_size) {
Yifan Hong6eec9952019-12-04 13:12:01 -0800389 source_slot_ = source_slot;
390 target_slot_ = target_slot;
Yifan Hongf033ecb2020-01-07 18:13:56 -0800391 if (required_size != nullptr) {
392 *required_size = 0;
393 }
Yifan Hong6eec9952019-12-04 13:12:01 -0800394
Yifan Hong3a1a5612019-11-05 16:34:32 -0800395 if (fs_mgr_overlayfs_is_setup()) {
396 // Non DAP devices can use overlayfs as well.
397 LOG(WARNING)
398 << "overlayfs overrides are active and can interfere with our "
399 "resources.\n"
400 << "run adb enable-verity to deactivate if required and try again.";
401 }
402
403 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
404 return true;
405 }
406
407 if (target_slot == source_slot) {
408 LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot.";
409 return false;
410 }
411
412 // Although the current build supports dynamic partitions, the given payload
413 // doesn't use it for target partitions. This could happen when applying a
414 // retrofit update. Skip updating the partition metadata for the target slot.
415 is_target_dynamic_ = !manifest.dynamic_partition_metadata().groups().empty();
416 if (!is_target_dynamic_) {
417 return true;
418 }
419
Yifan Hongf0f4a912019-09-26 17:51:33 -0700420 target_supports_snapshot_ =
421 manifest.dynamic_partition_metadata().snapshot_enabled();
422
Yifan Hong2c62c132019-10-24 14:53:40 -0700423 if (GetVirtualAbFeatureFlag().IsEnabled()) {
424 metadata_device_ = snapshot_->EnsureMetadataMounted();
425 TEST_AND_RETURN_FALSE(metadata_device_ != nullptr);
426 }
427
Yifan Hongf0f4a912019-09-26 17:51:33 -0700428 if (!update)
429 return true;
430
Yifan Hongbae27842019-10-24 16:56:12 -0700431 bool delete_source = false;
432
Yifan Hong6d888562019-10-01 13:00:31 -0700433 if (GetVirtualAbFeatureFlag().IsEnabled()) {
434 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
435 // called before calling UnmapUpdateSnapshot.
436 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
437 // calls BeginUpdate() which resets update state
Yifan Hongbae27842019-10-24 16:56:12 -0700438 // - If !target_supports_snapshot_ or PrepareSnapshotPartitionsForUpdate
439 // failed in recovery, explicitly CancelUpdate().
Yifan Hong6d888562019-10-01 13:00:31 -0700440 if (target_supports_snapshot_) {
Yifan Hongbae27842019-10-24 16:56:12 -0700441 if (PrepareSnapshotPartitionsForUpdate(
442 source_slot, target_slot, manifest, required_size)) {
443 return true;
444 }
445
446 // Virtual A/B device doing Virtual A/B update in Android mode must use
447 // snapshots.
448 if (!IsRecovery()) {
449 LOG(ERROR) << "PrepareSnapshotPartitionsForUpdate failed in Android "
450 << "mode";
451 return false;
452 }
453
454 delete_source = true;
455 LOG(INFO) << "PrepareSnapshotPartitionsForUpdate failed in recovery. "
456 << "Attempt to overwrite existing partitions if possible";
457 } else {
458 // Downgrading to an non-Virtual A/B build or is secondary OTA.
459 LOG(INFO) << "Using regular A/B on Virtual A/B because package disabled "
460 << "snapshots.";
Yifan Hong6d888562019-10-01 13:00:31 -0700461 }
Yifan Hongbae27842019-10-24 16:56:12 -0700462
Yifan Hong6d888562019-10-01 13:00:31 -0700463 if (!snapshot_->CancelUpdate()) {
464 LOG(ERROR) << "Cannot cancel previous update.";
465 return false;
466 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700467 }
Yifan Hongbae27842019-10-24 16:56:12 -0700468
469 return PrepareDynamicPartitionsForUpdate(
470 source_slot, target_slot, manifest, delete_source);
Yifan Hong420db9b2019-07-23 20:50:33 -0700471}
472
473bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
474 uint32_t source_slot,
475 uint32_t target_slot,
Yifan Hongbae27842019-10-24 16:56:12 -0700476 const DeltaArchiveManifest& manifest,
477 bool delete_source) {
Yifan Hong012508e2019-07-22 18:30:40 -0700478 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
479
480 // Unmap all the target dynamic partitions because they would become
481 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700482 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
483 for (const auto& partition_name : group.partition_names()) {
484 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700485 return false;
486 }
487 }
488 }
489
490 std::string device_dir_str;
491 if (!GetDeviceDir(&device_dir_str)) {
492 return false;
493 }
494 base::FilePath device_dir(device_dir_str);
495 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700496 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700497
498 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
499 if (builder == nullptr) {
500 LOG(ERROR) << "No metadata at "
501 << BootControlInterface::SlotName(source_slot);
502 return false;
503 }
504
Yifan Hongbae27842019-10-24 16:56:12 -0700505 if (delete_source) {
506 TEST_AND_RETURN_FALSE(
507 DeleteSourcePartitions(builder.get(), source_slot, manifest));
508 }
509
Yifan Hong13d41cb2019-09-16 13:18:22 -0700510 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700511 return false;
512 }
513
514 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700515 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700516 return StoreMetadata(target_device, builder.get(), target_slot);
517}
518
Yifan Hong420db9b2019-07-23 20:50:33 -0700519bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
520 uint32_t source_slot,
521 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800522 const DeltaArchiveManifest& manifest,
523 uint64_t* required_size) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700524 if (!snapshot_->BeginUpdate()) {
525 LOG(ERROR) << "Cannot begin new update.";
526 return false;
527 }
Yifan Hongf033ecb2020-01-07 18:13:56 -0800528 auto ret = snapshot_->CreateUpdateSnapshots(manifest);
529 if (!ret) {
530 LOG(ERROR) << "Cannot create update snapshots: " << ret.string();
531 if (required_size != nullptr &&
Yifan Hong0850bca2020-01-16 15:14:07 -0800532 ret.error_code() == Return::ErrorCode::NO_SPACE) {
Yifan Hongf033ecb2020-01-07 18:13:56 -0800533 *required_size = ret.required_size();
534 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700535 return false;
536 }
537 return true;
538}
539
Yifan Hong700d7c12019-07-23 20:49:16 -0700540std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
541 uint32_t slot) {
542 return fs_mgr_get_super_partition_name(slot);
543}
544
Yifan Hong012508e2019-07-22 18:30:40 -0700545bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
546 MetadataBuilder* builder,
547 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700548 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700549 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
550 // COW group needs to be deleted to ensure there are enough space to create
551 // target partitions.
552 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
553
Yifan Hong012508e2019-07-22 18:30:40 -0700554 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
555 DeleteGroupsWithSuffix(builder, target_suffix);
556
557 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700558 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
559 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700560 }
561
562 std::string expr;
563 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700564 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700565 allocatable_space /= 2;
566 expr = "half of ";
567 }
568 if (total_size > allocatable_space) {
569 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
570 << " (" << total_size << ") has exceeded " << expr
571 << "allocatable space for dynamic partitions "
572 << allocatable_space << ".";
573 return false;
574 }
575
Yifan Hong13d41cb2019-09-16 13:18:22 -0700576 // name of partition(e.g. "system") -> size in bytes
577 std::map<std::string, uint64_t> partition_sizes;
578 for (const auto& partition : manifest.partitions()) {
579 partition_sizes.emplace(partition.partition_name(),
580 partition.new_partition_info().size());
581 }
582
583 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
584 auto group_name_suffix = group.name() + target_suffix;
585 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700586 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700587 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700588 return false;
589 }
590 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700591 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700592
Yifan Hong13d41cb2019-09-16 13:18:22 -0700593 for (const auto& partition_name : group.partition_names()) {
594 auto partition_sizes_it = partition_sizes.find(partition_name);
595 if (partition_sizes_it == partition_sizes.end()) {
596 // TODO(tbao): Support auto-filling partition info for framework-only
597 // OTA.
598 LOG(ERROR) << "dynamic_partition_metadata contains partition "
599 << partition_name << " but it is not part of the manifest. "
600 << "This is not supported.";
601 return false;
602 }
603 uint64_t partition_size = partition_sizes_it->second;
604
605 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700606 Partition* p = builder->AddPartition(
607 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
608 if (!p) {
609 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
610 << " to group " << group_name_suffix;
611 return false;
612 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700613 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700614 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700615 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700616 return false;
617 }
618 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700619 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700620 }
621 }
622
623 return true;
624}
625
Yifan Honga33bca42019-09-03 20:29:45 -0700626bool DynamicPartitionControlAndroid::FinishUpdate() {
Yifan Hongd66ecf12020-02-04 11:15:50 -0800627 if (GetVirtualAbFeatureFlag().IsEnabled() &&
628 snapshot_->GetUpdateState() == UpdateState::Initiated) {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700629 LOG(INFO) << "Snapshot writes are done.";
630 return snapshot_->FinishedSnapshotWrites();
631 }
632 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700633}
634
Yifan Hong3a1a5612019-11-05 16:34:32 -0800635bool DynamicPartitionControlAndroid::GetPartitionDevice(
636 const std::string& partition_name,
637 uint32_t slot,
638 uint32_t current_slot,
639 std::string* device) {
640 const auto& partition_name_suffix =
641 partition_name + SlotSuffixForSlotNumber(slot);
642 std::string device_dir_str;
643 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
644 base::FilePath device_dir(device_dir_str);
645
646 // When looking up target partition devices, treat them as static if the
647 // current payload doesn't encode them as dynamic partitions. This may happen
648 // when applying a retrofit update on top of a dynamic-partitions-enabled
649 // build.
650 if (GetDynamicPartitionsFeatureFlag().IsEnabled() &&
651 (slot == current_slot || is_target_dynamic_)) {
652 switch (GetDynamicPartitionDevice(
653 device_dir, partition_name_suffix, slot, current_slot, device)) {
654 case DynamicPartitionDeviceStatus::SUCCESS:
655 return true;
656 case DynamicPartitionDeviceStatus::TRY_STATIC:
657 break;
658 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
659 default:
660 return false;
661 }
662 }
663 base::FilePath path = device_dir.Append(partition_name_suffix);
664 if (!DeviceExists(path.value())) {
665 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
666 return false;
667 }
668
669 *device = path.value();
670 return true;
671}
672
673bool DynamicPartitionControlAndroid::IsSuperBlockDevice(
674 const base::FilePath& device_dir,
675 uint32_t current_slot,
676 const std::string& partition_name_suffix) {
677 std::string source_device =
678 device_dir.Append(GetSuperPartitionName(current_slot)).value();
679 auto source_metadata = LoadMetadataBuilder(source_device, current_slot);
680 return source_metadata->HasBlockDevice(partition_name_suffix);
681}
682
683DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus
684DynamicPartitionControlAndroid::GetDynamicPartitionDevice(
685 const base::FilePath& device_dir,
686 const std::string& partition_name_suffix,
687 uint32_t slot,
688 uint32_t current_slot,
689 std::string* device) {
690 std::string super_device =
691 device_dir.Append(GetSuperPartitionName(slot)).value();
692
693 auto builder = LoadMetadataBuilder(super_device, slot);
694 if (builder == nullptr) {
695 LOG(ERROR) << "No metadata in slot "
696 << BootControlInterface::SlotName(slot);
697 return DynamicPartitionDeviceStatus::ERROR;
698 }
699 if (builder->FindPartition(partition_name_suffix) == nullptr) {
700 LOG(INFO) << partition_name_suffix
701 << " is not in super partition metadata.";
702
703 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
704 LOG(ERROR) << "The static partition " << partition_name_suffix
705 << " is a block device for current metadata."
706 << "It cannot be used as a logical partition.";
707 return DynamicPartitionDeviceStatus::ERROR;
708 }
709
710 return DynamicPartitionDeviceStatus::TRY_STATIC;
711 }
712
713 if (slot == current_slot) {
714 if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) {
715 LOG(WARNING) << partition_name_suffix << " is at current slot but it is "
716 << "not mapped. Now try to map it.";
717 } else {
718 if (GetDmDevicePathByName(partition_name_suffix, device)) {
719 LOG(INFO) << partition_name_suffix
720 << " is mapped on device mapper: " << *device;
721 return DynamicPartitionDeviceStatus::SUCCESS;
722 }
723 LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown.";
724 return DynamicPartitionDeviceStatus::ERROR;
725 }
726 }
727
728 bool force_writable = slot != current_slot;
729 if (MapPartitionOnDeviceMapper(
730 super_device, partition_name_suffix, slot, force_writable, device)) {
731 return DynamicPartitionDeviceStatus::SUCCESS;
732 }
733 return DynamicPartitionDeviceStatus::ERROR;
734}
735
Yifan Hong6eec9952019-12-04 13:12:01 -0800736void DynamicPartitionControlAndroid::set_fake_mapped_devices(
737 const std::set<std::string>& fake) {
738 mapped_devices_ = fake;
739}
740
Yifan Hongbae27842019-10-24 16:56:12 -0700741bool DynamicPartitionControlAndroid::IsRecovery() {
742 return kIsRecovery;
743}
744
745static bool IsIncrementalUpdate(const DeltaArchiveManifest& manifest) {
746 const auto& partitions = manifest.partitions();
747 return std::any_of(partitions.begin(), partitions.end(), [](const auto& p) {
748 return p.has_old_partition_info();
749 });
750}
751
752bool DynamicPartitionControlAndroid::DeleteSourcePartitions(
753 MetadataBuilder* builder,
754 uint32_t source_slot,
755 const DeltaArchiveManifest& manifest) {
756 TEST_AND_RETURN_FALSE(IsRecovery());
757
758 if (IsIncrementalUpdate(manifest)) {
759 LOG(ERROR) << "Cannot sideload incremental OTA because snapshots cannot "
760 << "be created.";
761 if (GetVirtualAbFeatureFlag().IsLaunch()) {
762 LOG(ERROR) << "Sideloading incremental updates on devices launches "
763 << " Virtual A/B is not supported.";
764 }
765 return false;
766 }
767
768 LOG(INFO) << "Will overwrite existing partitions. Slot "
769 << BootControlInterface::SlotName(source_slot)
770 << "may be unbootable until update finishes!";
771 const std::string source_suffix = SlotSuffixForSlotNumber(source_slot);
772 DeleteGroupsWithSuffix(builder, source_suffix);
773
774 return true;
775}
776
Yifan Hong90965502020-02-19 15:22:47 -0800777std::unique_ptr<AbstractAction>
778DynamicPartitionControlAndroid::GetCleanupPreviousUpdateAction(
779 BootControlInterface* boot_control,
780 PrefsInterface* prefs,
781 CleanupPreviousUpdateActionDelegateInterface* delegate) {
782 if (!GetVirtualAbFeatureFlag().IsEnabled()) {
783 return std::make_unique<NoOpAction>();
784 }
785 return std::make_unique<CleanupPreviousUpdateAction>(
786 prefs, boot_control, snapshot_.get(), delegate);
787}
788
Yifan Hong537802d2018-08-15 13:15:42 -0700789} // namespace chromeos_update_engine