blob: bb854800f0c412baf731ebfb6d2a130be80956a8 [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 Hongf033ecb2020-01-07 18:13:56 -080053using android::snapshot::SnapshotManager;
Alessio Balsini2a3b4a22019-11-25 16:46:51 +000054using android::snapshot::SourceCopyOperationIsClone;
Yifan Hong537802d2018-08-15 13:15:42 -070055
56namespace chromeos_update_engine {
57
Yifan Hong6e706b12018-11-09 16:50:51 -080058constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
59constexpr char kRetrfoitDynamicPartitions[] =
60 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong413d5722019-07-23 14:21:09 -070061constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled";
62constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit";
Yifan Hong420db9b2019-07-23 20:50:33 -070063// Map timeout for dynamic partitions.
64constexpr std::chrono::milliseconds kMapTimeout{1000};
65// Map timeout for dynamic partitions with snapshots. Since several devices
66// needs to be mapped, this timeout is longer than |kMapTimeout|.
67constexpr std::chrono::milliseconds kMapSnapshotTimeout{5000};
68
Yifan Hong537802d2018-08-15 13:15:42 -070069DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
Yifan Hong02513dc2019-10-30 11:23:04 -070070 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -070071}
72
Yifan Hong186bb682019-07-23 14:04:39 -070073static FeatureFlag GetFeatureFlag(const char* enable_prop,
74 const char* retrofit_prop) {
75 bool retrofit = GetBoolProperty(retrofit_prop, false);
76 bool enabled = GetBoolProperty(enable_prop, false);
77 if (retrofit && !enabled) {
78 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
79 << " is not. These sysprops are inconsistent. Assume that "
80 << enable_prop << " is true from now on.";
81 }
82 if (retrofit) {
83 return FeatureFlag(FeatureFlag::Value::RETROFIT);
84 }
85 if (enabled) {
86 return FeatureFlag(FeatureFlag::Value::LAUNCH);
87 }
88 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070089}
90
Yifan Hongb38e1af2019-10-17 14:59:22 -070091DynamicPartitionControlAndroid::DynamicPartitionControlAndroid()
92 : dynamic_partitions_(
93 GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions)),
94 virtual_ab_(GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit)) {
95 if (GetVirtualAbFeatureFlag().IsEnabled()) {
Yifan Hongf033ecb2020-01-07 18:13:56 -080096 snapshot_ = SnapshotManager::New();
Yifan Hongb38e1af2019-10-17 14:59:22 -070097 CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
98 }
99}
100
Yifan Hong186bb682019-07-23 14:04:39 -0700101FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700102 return dynamic_partitions_;
Yifan Hong6e706b12018-11-09 16:50:51 -0800103}
104
Yifan Hong413d5722019-07-23 14:21:09 -0700105FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700106 return virtual_ab_;
Yifan Hong413d5722019-07-23 14:21:09 -0700107}
108
Alessio Balsini14980e22019-11-26 11:46:06 +0000109bool DynamicPartitionControlAndroid::ShouldSkipOperation(
Yifan Hong6eec9952019-12-04 13:12:01 -0800110 const std::string& partition_name, const InstallOperation& operation) {
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000111 switch (operation.type()) {
112 case InstallOperation::SOURCE_COPY:
113 return target_supports_snapshot_ &&
114 GetVirtualAbFeatureFlag().IsEnabled() &&
Yifan Hong6eec9952019-12-04 13:12:01 -0800115 mapped_devices_.count(partition_name +
116 SlotSuffixForSlotNumber(target_slot_)) > 0 &&
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000117 SourceCopyOperationIsClone(operation);
118 break;
119 default:
120 break;
121 }
Alessio Balsini14980e22019-11-26 11:46:06 +0000122 return false;
123}
124
Yifan Hong8546a712019-03-28 14:42:53 -0700125bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700126 const std::string& super_device,
127 const std::string& target_partition_name,
128 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700129 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700130 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700131 CreateLogicalPartitionParams params = {
132 .block_device = super_device,
133 .metadata_slot = slot,
134 .partition_name = target_partition_name,
135 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700136 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700137 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700138 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
139 force_writable) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700140 // Only target partitions are mapped with force_writable. On Virtual
141 // A/B devices, target partitions may overlap with source partitions, so
142 // they must be mapped with snapshot.
143 params.timeout_ms = kMapSnapshotTimeout;
144 success = snapshot_->MapUpdateSnapshot(params, path);
145 } else {
146 params.timeout_ms = kMapTimeout;
147 success = CreateLogicalPartition(params, path);
148 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700149
Yifan Hong420db9b2019-07-23 20:50:33 -0700150 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700151 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
152 << super_device << " on device mapper.";
153 return false;
154 }
155 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700156 << " to device mapper (force_writable = " << force_writable
157 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700158 mapped_devices_.insert(target_partition_name);
159 return true;
160}
161
Yifan Hong8546a712019-03-28 14:42:53 -0700162bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
163 const std::string& super_device,
164 const std::string& target_partition_name,
165 uint32_t slot,
166 bool force_writable,
167 std::string* path) {
168 DmDeviceState state = GetState(target_partition_name);
169 if (state == DmDeviceState::ACTIVE) {
170 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
171 if (GetDmDevicePathByName(target_partition_name, path)) {
172 LOG(INFO) << target_partition_name
173 << " is mapped on device mapper: " << *path;
174 return true;
175 }
176 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
177 return false;
178 }
179 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
180 // the device might be mapped incorrectly before. Attempt to unmap it.
181 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
182 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
183 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700184 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700185 LOG(ERROR) << target_partition_name
186 << " is mapped before the update, and it cannot be unmapped.";
187 return false;
188 }
189 state = GetState(target_partition_name);
190 if (state != DmDeviceState::INVALID) {
191 LOG(ERROR) << target_partition_name << " is unmapped but state is "
192 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
193 return false;
194 }
195 }
196 if (state == DmDeviceState::INVALID) {
197 return MapPartitionInternal(
198 super_device, target_partition_name, slot, force_writable, path);
199 }
200
201 LOG(ERROR) << target_partition_name
202 << " is mapped on device mapper but state is unknown: "
203 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
204 return false;
205}
206
Yifan Hong537802d2018-08-15 13:15:42 -0700207bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700208 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700209 if (DeviceMapper::Instance().GetState(target_partition_name) !=
210 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700211 // Partitions at target slot on non-Virtual A/B devices are mapped as
212 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
213 // preopt apps as dm-linear.
214 // Call DestroyLogicalPartition to handle these cases.
215 bool success = DestroyLogicalPartition(target_partition_name);
216
217 // On a Virtual A/B device, |target_partition_name| may be a leftover from
218 // a paused update. Clean up any underlying devices.
219 if (GetVirtualAbFeatureFlag().IsEnabled()) {
220 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
221 }
222
223 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700224 LOG(ERROR) << "Cannot unmap " << target_partition_name
225 << " from device mapper.";
226 return false;
227 }
228 LOG(INFO) << "Successfully unmapped " << target_partition_name
229 << " from device mapper.";
230 }
231 mapped_devices_.erase(target_partition_name);
232 return true;
233}
234
Yifan Hong02513dc2019-10-30 11:23:04 -0700235void DynamicPartitionControlAndroid::CleanupInternal() {
Yifan Hong2c62c132019-10-24 14:53:40 -0700236 metadata_device_.reset();
Tao Bao8c4d0082019-08-08 08:56:16 -0700237 if (mapped_devices_.empty()) {
238 return;
239 }
Yifan Hong537802d2018-08-15 13:15:42 -0700240 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
241 // a copy is needed for the loop.
242 std::set<std::string> mapped = mapped_devices_;
243 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
244 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700245 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700246 }
247}
248
249void DynamicPartitionControlAndroid::Cleanup() {
Yifan Hong02513dc2019-10-30 11:23:04 -0700250 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -0700251}
252
253bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
254 return base::PathExists(base::FilePath(path));
255}
256
257android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
258 const std::string& name) {
259 return DeviceMapper::Instance().GetState(name);
260}
261
262bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
263 const std::string& name, std::string* path) {
264 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
265}
266
267std::unique_ptr<MetadataBuilder>
268DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700269 const std::string& super_device, uint32_t source_slot) {
270 return LoadMetadataBuilder(
271 super_device, source_slot, BootControlInterface::kInvalidSlot);
272}
273
274std::unique_ptr<MetadataBuilder>
275DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800276 const std::string& super_device,
277 uint32_t source_slot,
278 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700279 std::unique_ptr<MetadataBuilder> builder;
280 if (target_slot == BootControlInterface::kInvalidSlot) {
281 builder =
282 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
283 } else {
Yifan Hong50a56c62019-10-14 19:35:05 -0700284 bool always_keep_source_slot = !target_supports_snapshot_;
285 builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
286 super_device,
287 source_slot,
288 target_slot,
289 always_keep_source_slot);
Yifan Hong30fa5f52019-08-05 16:39:59 -0700290 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800291
Yifan Hong537802d2018-08-15 13:15:42 -0700292 if (builder == nullptr) {
293 LOG(WARNING) << "No metadata slot "
294 << BootControlInterface::SlotName(source_slot) << " in "
295 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700296 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700297 }
298 LOG(INFO) << "Loaded metadata from slot "
299 << BootControlInterface::SlotName(source_slot) << " in "
300 << super_device;
301 return builder;
302}
303
304bool DynamicPartitionControlAndroid::StoreMetadata(
305 const std::string& super_device,
306 MetadataBuilder* builder,
307 uint32_t target_slot) {
308 auto metadata = builder->Export();
309 if (metadata == nullptr) {
310 LOG(ERROR) << "Cannot export metadata to slot "
311 << BootControlInterface::SlotName(target_slot) << " in "
312 << super_device;
313 return false;
314 }
315
Yifan Hong186bb682019-07-23 14:04:39 -0700316 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800317 if (!FlashPartitionTable(super_device, *metadata)) {
318 LOG(ERROR) << "Cannot write metadata to " << super_device;
319 return false;
320 }
321 LOG(INFO) << "Written metadata to " << super_device;
322 } else {
323 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
324 LOG(ERROR) << "Cannot write metadata to slot "
325 << BootControlInterface::SlotName(target_slot) << " in "
326 << super_device;
327 return false;
328 }
329 LOG(INFO) << "Copied metadata to slot "
330 << BootControlInterface::SlotName(target_slot) << " in "
331 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700332 }
333
Yifan Hong537802d2018-08-15 13:15:42 -0700334 return true;
335}
336
337bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
338 // We can't use fs_mgr to look up |partition_name| because fstab
339 // doesn't list every slot partition (it uses the slotselect option
340 // to mask the suffix).
341 //
342 // We can however assume that there's an entry for the /misc mount
343 // point and use that to get the device file for the misc
344 // partition. This helps us locate the disk that |partition_name|
345 // resides on. From there we'll assume that a by-name scheme is used
346 // so we can just replace the trailing "misc" by the given
347 // |partition_name| and suffix corresponding to |slot|, e.g.
348 //
349 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
350 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
351 //
352 // If needed, it's possible to relax the by-name assumption in the
353 // future by trawling /sys/block looking for the appropriate sibling
354 // of misc and then finding an entry in /dev matching the sysfs
355 // entry.
356
357 std::string err, misc_device = get_bootloader_message_blk_device(&err);
358 if (misc_device.empty()) {
359 LOG(ERROR) << "Unable to get misc block device: " << err;
360 return false;
361 }
362
363 if (!utils::IsSymlink(misc_device.c_str())) {
364 LOG(ERROR) << "Device file " << misc_device << " for /misc "
365 << "is not a symlink.";
366 return false;
367 }
368 *out = base::FilePath(misc_device).DirName().value();
369 return true;
370}
Yifan Hong012508e2019-07-22 18:30:40 -0700371
372bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
373 uint32_t source_slot,
374 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700375 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800376 bool update,
377 uint64_t* required_size) {
Yifan Hong6eec9952019-12-04 13:12:01 -0800378 source_slot_ = source_slot;
379 target_slot_ = target_slot;
Yifan Hongf033ecb2020-01-07 18:13:56 -0800380 if (required_size != nullptr) {
381 *required_size = 0;
382 }
Yifan Hong6eec9952019-12-04 13:12:01 -0800383
Yifan Hong3a1a5612019-11-05 16:34:32 -0800384 if (fs_mgr_overlayfs_is_setup()) {
385 // Non DAP devices can use overlayfs as well.
386 LOG(WARNING)
387 << "overlayfs overrides are active and can interfere with our "
388 "resources.\n"
389 << "run adb enable-verity to deactivate if required and try again.";
390 }
391
392 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
393 return true;
394 }
395
396 if (target_slot == source_slot) {
397 LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot.";
398 return false;
399 }
400
401 // Although the current build supports dynamic partitions, the given payload
402 // doesn't use it for target partitions. This could happen when applying a
403 // retrofit update. Skip updating the partition metadata for the target slot.
404 is_target_dynamic_ = !manifest.dynamic_partition_metadata().groups().empty();
405 if (!is_target_dynamic_) {
406 return true;
407 }
408
Yifan Hongf0f4a912019-09-26 17:51:33 -0700409 target_supports_snapshot_ =
410 manifest.dynamic_partition_metadata().snapshot_enabled();
411
Yifan Hong2c62c132019-10-24 14:53:40 -0700412 if (GetVirtualAbFeatureFlag().IsEnabled()) {
413 metadata_device_ = snapshot_->EnsureMetadataMounted();
414 TEST_AND_RETURN_FALSE(metadata_device_ != nullptr);
415 }
416
Yifan Hongf0f4a912019-09-26 17:51:33 -0700417 if (!update)
418 return true;
419
Yifan Hong6d888562019-10-01 13:00:31 -0700420 if (GetVirtualAbFeatureFlag().IsEnabled()) {
421 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
422 // called before calling UnmapUpdateSnapshot.
423 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
424 // calls BeginUpdate() which resets update state
425 // - If !target_supports_snapshot_, explicitly CancelUpdate().
426 if (target_supports_snapshot_) {
427 return PrepareSnapshotPartitionsForUpdate(
Yifan Hongf033ecb2020-01-07 18:13:56 -0800428 source_slot, target_slot, manifest, required_size);
Yifan Hong6d888562019-10-01 13:00:31 -0700429 }
430 if (!snapshot_->CancelUpdate()) {
431 LOG(ERROR) << "Cannot cancel previous update.";
432 return false;
433 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700434 }
435 return PrepareDynamicPartitionsForUpdate(source_slot, target_slot, manifest);
436}
437
438bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
439 uint32_t source_slot,
440 uint32_t target_slot,
441 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700442 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
443
444 // Unmap all the target dynamic partitions because they would become
445 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700446 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
447 for (const auto& partition_name : group.partition_names()) {
448 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700449 return false;
450 }
451 }
452 }
453
454 std::string device_dir_str;
455 if (!GetDeviceDir(&device_dir_str)) {
456 return false;
457 }
458 base::FilePath device_dir(device_dir_str);
459 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700460 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700461
462 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
463 if (builder == nullptr) {
464 LOG(ERROR) << "No metadata at "
465 << BootControlInterface::SlotName(source_slot);
466 return false;
467 }
468
Yifan Hong13d41cb2019-09-16 13:18:22 -0700469 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700470 return false;
471 }
472
473 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700474 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700475 return StoreMetadata(target_device, builder.get(), target_slot);
476}
477
Yifan Hong420db9b2019-07-23 20:50:33 -0700478bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
479 uint32_t source_slot,
480 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800481 const DeltaArchiveManifest& manifest,
482 uint64_t* required_size) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700483 if (!snapshot_->BeginUpdate()) {
484 LOG(ERROR) << "Cannot begin new update.";
485 return false;
486 }
Yifan Hongf033ecb2020-01-07 18:13:56 -0800487 auto ret = snapshot_->CreateUpdateSnapshots(manifest);
488 if (!ret) {
489 LOG(ERROR) << "Cannot create update snapshots: " << ret.string();
490 if (required_size != nullptr &&
491 ret.error_code() == SnapshotManager::Return::ErrorCode::NO_SPACE) {
492 *required_size = ret.required_size();
493 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700494 return false;
495 }
496 return true;
497}
498
Yifan Hong700d7c12019-07-23 20:49:16 -0700499std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
500 uint32_t slot) {
501 return fs_mgr_get_super_partition_name(slot);
502}
503
Yifan Hong012508e2019-07-22 18:30:40 -0700504bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
505 MetadataBuilder* builder,
506 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700507 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700508 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
509 // COW group needs to be deleted to ensure there are enough space to create
510 // target partitions.
511 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
512
Yifan Hong012508e2019-07-22 18:30:40 -0700513 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
514 DeleteGroupsWithSuffix(builder, target_suffix);
515
516 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700517 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
518 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700519 }
520
521 std::string expr;
522 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700523 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700524 allocatable_space /= 2;
525 expr = "half of ";
526 }
527 if (total_size > allocatable_space) {
528 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
529 << " (" << total_size << ") has exceeded " << expr
530 << "allocatable space for dynamic partitions "
531 << allocatable_space << ".";
532 return false;
533 }
534
Yifan Hong13d41cb2019-09-16 13:18:22 -0700535 // name of partition(e.g. "system") -> size in bytes
536 std::map<std::string, uint64_t> partition_sizes;
537 for (const auto& partition : manifest.partitions()) {
538 partition_sizes.emplace(partition.partition_name(),
539 partition.new_partition_info().size());
540 }
541
542 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
543 auto group_name_suffix = group.name() + target_suffix;
544 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700545 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700546 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700547 return false;
548 }
549 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700550 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700551
Yifan Hong13d41cb2019-09-16 13:18:22 -0700552 for (const auto& partition_name : group.partition_names()) {
553 auto partition_sizes_it = partition_sizes.find(partition_name);
554 if (partition_sizes_it == partition_sizes.end()) {
555 // TODO(tbao): Support auto-filling partition info for framework-only
556 // OTA.
557 LOG(ERROR) << "dynamic_partition_metadata contains partition "
558 << partition_name << " but it is not part of the manifest. "
559 << "This is not supported.";
560 return false;
561 }
562 uint64_t partition_size = partition_sizes_it->second;
563
564 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700565 Partition* p = builder->AddPartition(
566 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
567 if (!p) {
568 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
569 << " to group " << group_name_suffix;
570 return false;
571 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700572 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700573 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700574 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700575 return false;
576 }
577 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700578 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700579 }
580 }
581
582 return true;
583}
584
Yifan Honga33bca42019-09-03 20:29:45 -0700585bool DynamicPartitionControlAndroid::FinishUpdate() {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700586 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) {
587 LOG(INFO) << "Snapshot writes are done.";
588 return snapshot_->FinishedSnapshotWrites();
589 }
590 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700591}
592
Yifan Hong3a1a5612019-11-05 16:34:32 -0800593bool DynamicPartitionControlAndroid::GetPartitionDevice(
594 const std::string& partition_name,
595 uint32_t slot,
596 uint32_t current_slot,
597 std::string* device) {
598 const auto& partition_name_suffix =
599 partition_name + SlotSuffixForSlotNumber(slot);
600 std::string device_dir_str;
601 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
602 base::FilePath device_dir(device_dir_str);
603
604 // When looking up target partition devices, treat them as static if the
605 // current payload doesn't encode them as dynamic partitions. This may happen
606 // when applying a retrofit update on top of a dynamic-partitions-enabled
607 // build.
608 if (GetDynamicPartitionsFeatureFlag().IsEnabled() &&
609 (slot == current_slot || is_target_dynamic_)) {
610 switch (GetDynamicPartitionDevice(
611 device_dir, partition_name_suffix, slot, current_slot, device)) {
612 case DynamicPartitionDeviceStatus::SUCCESS:
613 return true;
614 case DynamicPartitionDeviceStatus::TRY_STATIC:
615 break;
616 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
617 default:
618 return false;
619 }
620 }
621 base::FilePath path = device_dir.Append(partition_name_suffix);
622 if (!DeviceExists(path.value())) {
623 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
624 return false;
625 }
626
627 *device = path.value();
628 return true;
629}
630
631bool DynamicPartitionControlAndroid::IsSuperBlockDevice(
632 const base::FilePath& device_dir,
633 uint32_t current_slot,
634 const std::string& partition_name_suffix) {
635 std::string source_device =
636 device_dir.Append(GetSuperPartitionName(current_slot)).value();
637 auto source_metadata = LoadMetadataBuilder(source_device, current_slot);
638 return source_metadata->HasBlockDevice(partition_name_suffix);
639}
640
641DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus
642DynamicPartitionControlAndroid::GetDynamicPartitionDevice(
643 const base::FilePath& device_dir,
644 const std::string& partition_name_suffix,
645 uint32_t slot,
646 uint32_t current_slot,
647 std::string* device) {
648 std::string super_device =
649 device_dir.Append(GetSuperPartitionName(slot)).value();
650
651 auto builder = LoadMetadataBuilder(super_device, slot);
652 if (builder == nullptr) {
653 LOG(ERROR) << "No metadata in slot "
654 << BootControlInterface::SlotName(slot);
655 return DynamicPartitionDeviceStatus::ERROR;
656 }
657 if (builder->FindPartition(partition_name_suffix) == nullptr) {
658 LOG(INFO) << partition_name_suffix
659 << " is not in super partition metadata.";
660
661 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
662 LOG(ERROR) << "The static partition " << partition_name_suffix
663 << " is a block device for current metadata."
664 << "It cannot be used as a logical partition.";
665 return DynamicPartitionDeviceStatus::ERROR;
666 }
667
668 return DynamicPartitionDeviceStatus::TRY_STATIC;
669 }
670
671 if (slot == current_slot) {
672 if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) {
673 LOG(WARNING) << partition_name_suffix << " is at current slot but it is "
674 << "not mapped. Now try to map it.";
675 } else {
676 if (GetDmDevicePathByName(partition_name_suffix, device)) {
677 LOG(INFO) << partition_name_suffix
678 << " is mapped on device mapper: " << *device;
679 return DynamicPartitionDeviceStatus::SUCCESS;
680 }
681 LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown.";
682 return DynamicPartitionDeviceStatus::ERROR;
683 }
684 }
685
686 bool force_writable = slot != current_slot;
687 if (MapPartitionOnDeviceMapper(
688 super_device, partition_name_suffix, slot, force_writable, device)) {
689 return DynamicPartitionDeviceStatus::SUCCESS;
690 }
691 return DynamicPartitionDeviceStatus::ERROR;
692}
693
Yifan Hong6eec9952019-12-04 13:12:01 -0800694void DynamicPartitionControlAndroid::set_fake_mapped_devices(
695 const std::set<std::string>& fake) {
696 mapped_devices_ = fake;
697}
698
Yifan Hong537802d2018-08-15 13:15:42 -0700699} // namespace chromeos_update_engine