blob: 881ff1117e6300cfa8e7093386581318e06359a4 [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
38#include "update_engine/common/boot_control_interface.h"
39#include "update_engine/common/utils.h"
Yifan Hong012508e2019-07-22 18:30:40 -070040#include "update_engine/dynamic_partition_utils.h"
Yifan Hong537802d2018-08-15 13:15:42 -070041
42using android::base::GetBoolProperty;
43using android::base::Join;
44using android::dm::DeviceMapper;
45using android::dm::DmDeviceState;
46using android::fs_mgr::CreateLogicalPartition;
David Andersonbb90dfb2019-08-13 14:14:56 -070047using android::fs_mgr::CreateLogicalPartitionParams;
Yifan Hong537802d2018-08-15 13:15:42 -070048using android::fs_mgr::DestroyLogicalPartition;
49using android::fs_mgr::MetadataBuilder;
Yifan Hong012508e2019-07-22 18:30:40 -070050using android::fs_mgr::Partition;
Yifan Hong6e706b12018-11-09 16:50:51 -080051using android::fs_mgr::PartitionOpener;
Yifan Hong012508e2019-07-22 18:30:40 -070052using android::fs_mgr::SlotSuffixForSlotNumber;
Yifan Hong0850bca2020-01-16 15:14:07 -080053using android::snapshot::Return;
Yifan Hongf033ecb2020-01-07 18:13:56 -080054using android::snapshot::SnapshotManager;
Alessio Balsini2a3b4a22019-11-25 16:46:51 +000055using android::snapshot::SourceCopyOperationIsClone;
Yifan Hong2257ee12020-01-13 18:33:00 -080056using android::snapshot::UpdateState;
Yifan Hong537802d2018-08-15 13:15:42 -070057
58namespace chromeos_update_engine {
59
Yifan Hong6e706b12018-11-09 16:50:51 -080060constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
61constexpr char kRetrfoitDynamicPartitions[] =
62 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong413d5722019-07-23 14:21:09 -070063constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled";
64constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit";
Yifan Hong420db9b2019-07-23 20:50:33 -070065// Map timeout for dynamic partitions.
66constexpr std::chrono::milliseconds kMapTimeout{1000};
67// Map timeout for dynamic partitions with snapshots. Since several devices
68// needs to be mapped, this timeout is longer than |kMapTimeout|.
69constexpr std::chrono::milliseconds kMapSnapshotTimeout{5000};
70
Yifan Hong537802d2018-08-15 13:15:42 -070071DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
Yifan Hong02513dc2019-10-30 11:23:04 -070072 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -070073}
74
Yifan Hong186bb682019-07-23 14:04:39 -070075static FeatureFlag GetFeatureFlag(const char* enable_prop,
76 const char* retrofit_prop) {
77 bool retrofit = GetBoolProperty(retrofit_prop, false);
78 bool enabled = GetBoolProperty(enable_prop, false);
79 if (retrofit && !enabled) {
80 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
81 << " is not. These sysprops are inconsistent. Assume that "
82 << enable_prop << " is true from now on.";
83 }
84 if (retrofit) {
85 return FeatureFlag(FeatureFlag::Value::RETROFIT);
86 }
87 if (enabled) {
88 return FeatureFlag(FeatureFlag::Value::LAUNCH);
89 }
90 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070091}
92
Yifan Hongb38e1af2019-10-17 14:59:22 -070093DynamicPartitionControlAndroid::DynamicPartitionControlAndroid()
94 : dynamic_partitions_(
95 GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions)),
96 virtual_ab_(GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit)) {
97 if (GetVirtualAbFeatureFlag().IsEnabled()) {
Yifan Hongf033ecb2020-01-07 18:13:56 -080098 snapshot_ = SnapshotManager::New();
Yifan Hongb38e1af2019-10-17 14:59:22 -070099 CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
100 }
101}
102
Yifan Hong186bb682019-07-23 14:04:39 -0700103FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700104 return dynamic_partitions_;
Yifan Hong6e706b12018-11-09 16:50:51 -0800105}
106
Yifan Hong413d5722019-07-23 14:21:09 -0700107FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700108 return virtual_ab_;
Yifan Hong413d5722019-07-23 14:21:09 -0700109}
110
Alessio Balsini14980e22019-11-26 11:46:06 +0000111bool DynamicPartitionControlAndroid::ShouldSkipOperation(
Yifan Hong6eec9952019-12-04 13:12:01 -0800112 const std::string& partition_name, const InstallOperation& operation) {
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000113 switch (operation.type()) {
114 case InstallOperation::SOURCE_COPY:
115 return target_supports_snapshot_ &&
116 GetVirtualAbFeatureFlag().IsEnabled() &&
Yifan Hong6eec9952019-12-04 13:12:01 -0800117 mapped_devices_.count(partition_name +
118 SlotSuffixForSlotNumber(target_slot_)) > 0 &&
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000119 SourceCopyOperationIsClone(operation);
120 break;
121 default:
122 break;
123 }
Alessio Balsini14980e22019-11-26 11:46:06 +0000124 return false;
125}
126
Yifan Hong8546a712019-03-28 14:42:53 -0700127bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700128 const std::string& super_device,
129 const std::string& target_partition_name,
130 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700131 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700132 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700133 CreateLogicalPartitionParams params = {
134 .block_device = super_device,
135 .metadata_slot = slot,
136 .partition_name = target_partition_name,
137 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700138 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700139 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700140 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
141 force_writable) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700142 // Only target partitions are mapped with force_writable. On Virtual
143 // A/B devices, target partitions may overlap with source partitions, so
144 // they must be mapped with snapshot.
145 params.timeout_ms = kMapSnapshotTimeout;
146 success = snapshot_->MapUpdateSnapshot(params, path);
147 } else {
148 params.timeout_ms = kMapTimeout;
149 success = CreateLogicalPartition(params, path);
150 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700151
Yifan Hong420db9b2019-07-23 20:50:33 -0700152 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700153 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
154 << super_device << " on device mapper.";
155 return false;
156 }
157 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700158 << " to device mapper (force_writable = " << force_writable
159 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700160 mapped_devices_.insert(target_partition_name);
161 return true;
162}
163
Yifan Hong8546a712019-03-28 14:42:53 -0700164bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
165 const std::string& super_device,
166 const std::string& target_partition_name,
167 uint32_t slot,
168 bool force_writable,
169 std::string* path) {
170 DmDeviceState state = GetState(target_partition_name);
171 if (state == DmDeviceState::ACTIVE) {
172 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
173 if (GetDmDevicePathByName(target_partition_name, path)) {
174 LOG(INFO) << target_partition_name
175 << " is mapped on device mapper: " << *path;
176 return true;
177 }
178 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
179 return false;
180 }
181 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
182 // the device might be mapped incorrectly before. Attempt to unmap it.
183 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
184 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
185 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700186 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700187 LOG(ERROR) << target_partition_name
188 << " is mapped before the update, and it cannot be unmapped.";
189 return false;
190 }
191 state = GetState(target_partition_name);
192 if (state != DmDeviceState::INVALID) {
193 LOG(ERROR) << target_partition_name << " is unmapped but state is "
194 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
195 return false;
196 }
197 }
198 if (state == DmDeviceState::INVALID) {
199 return MapPartitionInternal(
200 super_device, target_partition_name, slot, force_writable, path);
201 }
202
203 LOG(ERROR) << target_partition_name
204 << " is mapped on device mapper but state is unknown: "
205 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
206 return false;
207}
208
Yifan Hong537802d2018-08-15 13:15:42 -0700209bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700210 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700211 if (DeviceMapper::Instance().GetState(target_partition_name) !=
212 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700213 // Partitions at target slot on non-Virtual A/B devices are mapped as
214 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
215 // preopt apps as dm-linear.
216 // Call DestroyLogicalPartition to handle these cases.
217 bool success = DestroyLogicalPartition(target_partition_name);
218
219 // On a Virtual A/B device, |target_partition_name| may be a leftover from
220 // a paused update. Clean up any underlying devices.
221 if (GetVirtualAbFeatureFlag().IsEnabled()) {
222 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
223 }
224
225 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700226 LOG(ERROR) << "Cannot unmap " << target_partition_name
227 << " from device mapper.";
228 return false;
229 }
230 LOG(INFO) << "Successfully unmapped " << target_partition_name
231 << " from device mapper.";
232 }
233 mapped_devices_.erase(target_partition_name);
234 return true;
235}
236
Yifan Hong02513dc2019-10-30 11:23:04 -0700237void DynamicPartitionControlAndroid::CleanupInternal() {
Yifan Hong2c62c132019-10-24 14:53:40 -0700238 metadata_device_.reset();
Tao Bao8c4d0082019-08-08 08:56:16 -0700239 if (mapped_devices_.empty()) {
240 return;
241 }
Yifan Hong537802d2018-08-15 13:15:42 -0700242 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
243 // a copy is needed for the loop.
244 std::set<std::string> mapped = mapped_devices_;
245 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
246 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700247 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700248 }
249}
250
251void DynamicPartitionControlAndroid::Cleanup() {
Yifan Hong02513dc2019-10-30 11:23:04 -0700252 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -0700253}
254
255bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
256 return base::PathExists(base::FilePath(path));
257}
258
259android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
260 const std::string& name) {
261 return DeviceMapper::Instance().GetState(name);
262}
263
264bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
265 const std::string& name, std::string* path) {
266 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
267}
268
269std::unique_ptr<MetadataBuilder>
270DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700271 const std::string& super_device, uint32_t source_slot) {
272 return LoadMetadataBuilder(
273 super_device, source_slot, BootControlInterface::kInvalidSlot);
274}
275
276std::unique_ptr<MetadataBuilder>
277DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800278 const std::string& super_device,
279 uint32_t source_slot,
280 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700281 std::unique_ptr<MetadataBuilder> builder;
282 if (target_slot == BootControlInterface::kInvalidSlot) {
283 builder =
284 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
285 } else {
Yifan Hong50a56c62019-10-14 19:35:05 -0700286 bool always_keep_source_slot = !target_supports_snapshot_;
287 builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
288 super_device,
289 source_slot,
290 target_slot,
291 always_keep_source_slot);
Yifan Hong30fa5f52019-08-05 16:39:59 -0700292 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800293
Yifan Hong537802d2018-08-15 13:15:42 -0700294 if (builder == nullptr) {
295 LOG(WARNING) << "No metadata slot "
296 << BootControlInterface::SlotName(source_slot) << " in "
297 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700298 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700299 }
300 LOG(INFO) << "Loaded metadata from slot "
301 << BootControlInterface::SlotName(source_slot) << " in "
302 << super_device;
303 return builder;
304}
305
306bool DynamicPartitionControlAndroid::StoreMetadata(
307 const std::string& super_device,
308 MetadataBuilder* builder,
309 uint32_t target_slot) {
310 auto metadata = builder->Export();
311 if (metadata == nullptr) {
312 LOG(ERROR) << "Cannot export metadata to slot "
313 << BootControlInterface::SlotName(target_slot) << " in "
314 << super_device;
315 return false;
316 }
317
Yifan Hong186bb682019-07-23 14:04:39 -0700318 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800319 if (!FlashPartitionTable(super_device, *metadata)) {
320 LOG(ERROR) << "Cannot write metadata to " << super_device;
321 return false;
322 }
323 LOG(INFO) << "Written metadata to " << super_device;
324 } else {
325 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
326 LOG(ERROR) << "Cannot write metadata to slot "
327 << BootControlInterface::SlotName(target_slot) << " in "
328 << super_device;
329 return false;
330 }
331 LOG(INFO) << "Copied metadata to slot "
332 << BootControlInterface::SlotName(target_slot) << " in "
333 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700334 }
335
Yifan Hong537802d2018-08-15 13:15:42 -0700336 return true;
337}
338
339bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
340 // We can't use fs_mgr to look up |partition_name| because fstab
341 // doesn't list every slot partition (it uses the slotselect option
342 // to mask the suffix).
343 //
344 // We can however assume that there's an entry for the /misc mount
345 // point and use that to get the device file for the misc
346 // partition. This helps us locate the disk that |partition_name|
347 // resides on. From there we'll assume that a by-name scheme is used
348 // so we can just replace the trailing "misc" by the given
349 // |partition_name| and suffix corresponding to |slot|, e.g.
350 //
351 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
352 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
353 //
354 // If needed, it's possible to relax the by-name assumption in the
355 // future by trawling /sys/block looking for the appropriate sibling
356 // of misc and then finding an entry in /dev matching the sysfs
357 // entry.
358
359 std::string err, misc_device = get_bootloader_message_blk_device(&err);
360 if (misc_device.empty()) {
361 LOG(ERROR) << "Unable to get misc block device: " << err;
362 return false;
363 }
364
365 if (!utils::IsSymlink(misc_device.c_str())) {
366 LOG(ERROR) << "Device file " << misc_device << " for /misc "
367 << "is not a symlink.";
368 return false;
369 }
370 *out = base::FilePath(misc_device).DirName().value();
371 return true;
372}
Yifan Hong012508e2019-07-22 18:30:40 -0700373
374bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
375 uint32_t source_slot,
376 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700377 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800378 bool update,
379 uint64_t* required_size) {
Yifan Hong6eec9952019-12-04 13:12:01 -0800380 source_slot_ = source_slot;
381 target_slot_ = target_slot;
Yifan Hongf033ecb2020-01-07 18:13:56 -0800382 if (required_size != nullptr) {
383 *required_size = 0;
384 }
Yifan Hong6eec9952019-12-04 13:12:01 -0800385
Yifan Hong3a1a5612019-11-05 16:34:32 -0800386 if (fs_mgr_overlayfs_is_setup()) {
387 // Non DAP devices can use overlayfs as well.
388 LOG(WARNING)
389 << "overlayfs overrides are active and can interfere with our "
390 "resources.\n"
391 << "run adb enable-verity to deactivate if required and try again.";
392 }
393
394 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
395 return true;
396 }
397
398 if (target_slot == source_slot) {
399 LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot.";
400 return false;
401 }
402
403 // Although the current build supports dynamic partitions, the given payload
404 // doesn't use it for target partitions. This could happen when applying a
405 // retrofit update. Skip updating the partition metadata for the target slot.
406 is_target_dynamic_ = !manifest.dynamic_partition_metadata().groups().empty();
407 if (!is_target_dynamic_) {
408 return true;
409 }
410
Yifan Hongf0f4a912019-09-26 17:51:33 -0700411 target_supports_snapshot_ =
412 manifest.dynamic_partition_metadata().snapshot_enabled();
413
Yifan Hong2c62c132019-10-24 14:53:40 -0700414 if (GetVirtualAbFeatureFlag().IsEnabled()) {
415 metadata_device_ = snapshot_->EnsureMetadataMounted();
416 TEST_AND_RETURN_FALSE(metadata_device_ != nullptr);
417 }
418
Yifan Hongf0f4a912019-09-26 17:51:33 -0700419 if (!update)
420 return true;
421
Yifan Hong6d888562019-10-01 13:00:31 -0700422 if (GetVirtualAbFeatureFlag().IsEnabled()) {
423 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
424 // called before calling UnmapUpdateSnapshot.
425 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
426 // calls BeginUpdate() which resets update state
427 // - If !target_supports_snapshot_, explicitly CancelUpdate().
428 if (target_supports_snapshot_) {
429 return PrepareSnapshotPartitionsForUpdate(
Yifan Hongf033ecb2020-01-07 18:13:56 -0800430 source_slot, target_slot, manifest, required_size);
Yifan Hong6d888562019-10-01 13:00:31 -0700431 }
432 if (!snapshot_->CancelUpdate()) {
433 LOG(ERROR) << "Cannot cancel previous update.";
434 return false;
435 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700436 }
437 return PrepareDynamicPartitionsForUpdate(source_slot, target_slot, manifest);
438}
439
440bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
441 uint32_t source_slot,
442 uint32_t target_slot,
443 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700444 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
445
446 // Unmap all the target dynamic partitions because they would become
447 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700448 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
449 for (const auto& partition_name : group.partition_names()) {
450 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700451 return false;
452 }
453 }
454 }
455
456 std::string device_dir_str;
457 if (!GetDeviceDir(&device_dir_str)) {
458 return false;
459 }
460 base::FilePath device_dir(device_dir_str);
461 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700462 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700463
464 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
465 if (builder == nullptr) {
466 LOG(ERROR) << "No metadata at "
467 << BootControlInterface::SlotName(source_slot);
468 return false;
469 }
470
Yifan Hong13d41cb2019-09-16 13:18:22 -0700471 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700472 return false;
473 }
474
475 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700476 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700477 return StoreMetadata(target_device, builder.get(), target_slot);
478}
479
Yifan Hong420db9b2019-07-23 20:50:33 -0700480bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
481 uint32_t source_slot,
482 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800483 const DeltaArchiveManifest& manifest,
484 uint64_t* required_size) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700485 if (!snapshot_->BeginUpdate()) {
486 LOG(ERROR) << "Cannot begin new update.";
487 return false;
488 }
Yifan Hongf033ecb2020-01-07 18:13:56 -0800489 auto ret = snapshot_->CreateUpdateSnapshots(manifest);
490 if (!ret) {
491 LOG(ERROR) << "Cannot create update snapshots: " << ret.string();
492 if (required_size != nullptr &&
Yifan Hong0850bca2020-01-16 15:14:07 -0800493 ret.error_code() == Return::ErrorCode::NO_SPACE) {
Yifan Hongf033ecb2020-01-07 18:13:56 -0800494 *required_size = ret.required_size();
495 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700496 return false;
497 }
498 return true;
499}
500
Yifan Hong700d7c12019-07-23 20:49:16 -0700501std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
502 uint32_t slot) {
503 return fs_mgr_get_super_partition_name(slot);
504}
505
Yifan Hong012508e2019-07-22 18:30:40 -0700506bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
507 MetadataBuilder* builder,
508 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700509 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700510 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
511 // COW group needs to be deleted to ensure there are enough space to create
512 // target partitions.
513 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
514
Yifan Hong012508e2019-07-22 18:30:40 -0700515 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
516 DeleteGroupsWithSuffix(builder, target_suffix);
517
518 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700519 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
520 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700521 }
522
523 std::string expr;
524 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700525 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700526 allocatable_space /= 2;
527 expr = "half of ";
528 }
529 if (total_size > allocatable_space) {
530 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
531 << " (" << total_size << ") has exceeded " << expr
532 << "allocatable space for dynamic partitions "
533 << allocatable_space << ".";
534 return false;
535 }
536
Yifan Hong13d41cb2019-09-16 13:18:22 -0700537 // name of partition(e.g. "system") -> size in bytes
538 std::map<std::string, uint64_t> partition_sizes;
539 for (const auto& partition : manifest.partitions()) {
540 partition_sizes.emplace(partition.partition_name(),
541 partition.new_partition_info().size());
542 }
543
544 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
545 auto group_name_suffix = group.name() + target_suffix;
546 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700547 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700548 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700549 return false;
550 }
551 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700552 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700553
Yifan Hong13d41cb2019-09-16 13:18:22 -0700554 for (const auto& partition_name : group.partition_names()) {
555 auto partition_sizes_it = partition_sizes.find(partition_name);
556 if (partition_sizes_it == partition_sizes.end()) {
557 // TODO(tbao): Support auto-filling partition info for framework-only
558 // OTA.
559 LOG(ERROR) << "dynamic_partition_metadata contains partition "
560 << partition_name << " but it is not part of the manifest. "
561 << "This is not supported.";
562 return false;
563 }
564 uint64_t partition_size = partition_sizes_it->second;
565
566 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700567 Partition* p = builder->AddPartition(
568 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
569 if (!p) {
570 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
571 << " to group " << group_name_suffix;
572 return false;
573 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700574 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700575 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700576 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700577 return false;
578 }
579 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700580 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700581 }
582 }
583
584 return true;
585}
586
Yifan Honga33bca42019-09-03 20:29:45 -0700587bool DynamicPartitionControlAndroid::FinishUpdate() {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700588 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) {
589 LOG(INFO) << "Snapshot writes are done.";
590 return snapshot_->FinishedSnapshotWrites();
591 }
592 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700593}
594
Yifan Hong3a1a5612019-11-05 16:34:32 -0800595bool DynamicPartitionControlAndroid::GetPartitionDevice(
596 const std::string& partition_name,
597 uint32_t slot,
598 uint32_t current_slot,
599 std::string* device) {
600 const auto& partition_name_suffix =
601 partition_name + SlotSuffixForSlotNumber(slot);
602 std::string device_dir_str;
603 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
604 base::FilePath device_dir(device_dir_str);
605
606 // When looking up target partition devices, treat them as static if the
607 // current payload doesn't encode them as dynamic partitions. This may happen
608 // when applying a retrofit update on top of a dynamic-partitions-enabled
609 // build.
610 if (GetDynamicPartitionsFeatureFlag().IsEnabled() &&
611 (slot == current_slot || is_target_dynamic_)) {
612 switch (GetDynamicPartitionDevice(
613 device_dir, partition_name_suffix, slot, current_slot, device)) {
614 case DynamicPartitionDeviceStatus::SUCCESS:
615 return true;
616 case DynamicPartitionDeviceStatus::TRY_STATIC:
617 break;
618 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
619 default:
620 return false;
621 }
622 }
623 base::FilePath path = device_dir.Append(partition_name_suffix);
624 if (!DeviceExists(path.value())) {
625 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
626 return false;
627 }
628
629 *device = path.value();
630 return true;
631}
632
633bool DynamicPartitionControlAndroid::IsSuperBlockDevice(
634 const base::FilePath& device_dir,
635 uint32_t current_slot,
636 const std::string& partition_name_suffix) {
637 std::string source_device =
638 device_dir.Append(GetSuperPartitionName(current_slot)).value();
639 auto source_metadata = LoadMetadataBuilder(source_device, current_slot);
640 return source_metadata->HasBlockDevice(partition_name_suffix);
641}
642
643DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus
644DynamicPartitionControlAndroid::GetDynamicPartitionDevice(
645 const base::FilePath& device_dir,
646 const std::string& partition_name_suffix,
647 uint32_t slot,
648 uint32_t current_slot,
649 std::string* device) {
650 std::string super_device =
651 device_dir.Append(GetSuperPartitionName(slot)).value();
652
653 auto builder = LoadMetadataBuilder(super_device, slot);
654 if (builder == nullptr) {
655 LOG(ERROR) << "No metadata in slot "
656 << BootControlInterface::SlotName(slot);
657 return DynamicPartitionDeviceStatus::ERROR;
658 }
659 if (builder->FindPartition(partition_name_suffix) == nullptr) {
660 LOG(INFO) << partition_name_suffix
661 << " is not in super partition metadata.";
662
663 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
664 LOG(ERROR) << "The static partition " << partition_name_suffix
665 << " is a block device for current metadata."
666 << "It cannot be used as a logical partition.";
667 return DynamicPartitionDeviceStatus::ERROR;
668 }
669
670 return DynamicPartitionDeviceStatus::TRY_STATIC;
671 }
672
673 if (slot == current_slot) {
674 if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) {
675 LOG(WARNING) << partition_name_suffix << " is at current slot but it is "
676 << "not mapped. Now try to map it.";
677 } else {
678 if (GetDmDevicePathByName(partition_name_suffix, device)) {
679 LOG(INFO) << partition_name_suffix
680 << " is mapped on device mapper: " << *device;
681 return DynamicPartitionDeviceStatus::SUCCESS;
682 }
683 LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown.";
684 return DynamicPartitionDeviceStatus::ERROR;
685 }
686 }
687
688 bool force_writable = slot != current_slot;
689 if (MapPartitionOnDeviceMapper(
690 super_device, partition_name_suffix, slot, force_writable, device)) {
691 return DynamicPartitionDeviceStatus::SUCCESS;
692 }
693 return DynamicPartitionDeviceStatus::ERROR;
694}
695
Yifan Hong6eec9952019-12-04 13:12:01 -0800696void DynamicPartitionControlAndroid::set_fake_mapped_devices(
697 const std::set<std::string>& fake) {
698 mapped_devices_ = fake;
699}
700
Yifan Hong2257ee12020-01-13 18:33:00 -0800701ErrorCode DynamicPartitionControlAndroid::CleanupSuccessfulUpdate() {
702 // Already reboot into new boot. Clean up.
703 if (!GetVirtualAbFeatureFlag().IsEnabled()) {
704 return ErrorCode::kSuccess;
705 }
706 auto ret = snapshot_->WaitForMerge();
707 if (ret.is_ok()) {
708 return ErrorCode::kSuccess;
709 }
710 if (ret.error_code() == Return::ErrorCode::NEEDS_REBOOT) {
711 return ErrorCode::kError;
712 }
713 return ErrorCode::kDeviceCorrupted;
714}
715
Yifan Hong537802d2018-08-15 13:15:42 -0700716} // namespace chromeos_update_engine