blob: 4414e4b4552ee53d87f8da11c145d9a98156cfa6 [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;
Alessio Balsini2a3b4a22019-11-25 16:46:51 +000053using android::snapshot::SourceCopyOperationIsClone;
Yifan Hong537802d2018-08-15 13:15:42 -070054
55namespace chromeos_update_engine {
56
Yifan Hong6e706b12018-11-09 16:50:51 -080057constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
58constexpr char kRetrfoitDynamicPartitions[] =
59 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong413d5722019-07-23 14:21:09 -070060constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled";
61constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit";
Yifan Hong420db9b2019-07-23 20:50:33 -070062// Map timeout for dynamic partitions.
63constexpr std::chrono::milliseconds kMapTimeout{1000};
64// Map timeout for dynamic partitions with snapshots. Since several devices
65// needs to be mapped, this timeout is longer than |kMapTimeout|.
66constexpr std::chrono::milliseconds kMapSnapshotTimeout{5000};
67
Yifan Hong537802d2018-08-15 13:15:42 -070068DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
Yifan Hong02513dc2019-10-30 11:23:04 -070069 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -070070}
71
Yifan Hong186bb682019-07-23 14:04:39 -070072static FeatureFlag GetFeatureFlag(const char* enable_prop,
73 const char* retrofit_prop) {
74 bool retrofit = GetBoolProperty(retrofit_prop, false);
75 bool enabled = GetBoolProperty(enable_prop, false);
76 if (retrofit && !enabled) {
77 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
78 << " is not. These sysprops are inconsistent. Assume that "
79 << enable_prop << " is true from now on.";
80 }
81 if (retrofit) {
82 return FeatureFlag(FeatureFlag::Value::RETROFIT);
83 }
84 if (enabled) {
85 return FeatureFlag(FeatureFlag::Value::LAUNCH);
86 }
87 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070088}
89
Yifan Hongb38e1af2019-10-17 14:59:22 -070090DynamicPartitionControlAndroid::DynamicPartitionControlAndroid()
91 : dynamic_partitions_(
92 GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions)),
93 virtual_ab_(GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit)) {
94 if (GetVirtualAbFeatureFlag().IsEnabled()) {
95 snapshot_ = android::snapshot::SnapshotManager::New();
96 CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
97 }
98}
99
Yifan Hong186bb682019-07-23 14:04:39 -0700100FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700101 return dynamic_partitions_;
Yifan Hong6e706b12018-11-09 16:50:51 -0800102}
103
Yifan Hong413d5722019-07-23 14:21:09 -0700104FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700105 return virtual_ab_;
Yifan Hong413d5722019-07-23 14:21:09 -0700106}
107
Alessio Balsini14980e22019-11-26 11:46:06 +0000108bool DynamicPartitionControlAndroid::ShouldSkipOperation(
109 const InstallOperation& operation) {
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000110 switch (operation.type()) {
111 case InstallOperation::SOURCE_COPY:
112 return target_supports_snapshot_ &&
113 GetVirtualAbFeatureFlag().IsEnabled() &&
114 SourceCopyOperationIsClone(operation);
115 break;
116 default:
117 break;
118 }
Alessio Balsini14980e22019-11-26 11:46:06 +0000119 return false;
120}
121
Yifan Hong8546a712019-03-28 14:42:53 -0700122bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700123 const std::string& super_device,
124 const std::string& target_partition_name,
125 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700126 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700127 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700128 CreateLogicalPartitionParams params = {
129 .block_device = super_device,
130 .metadata_slot = slot,
131 .partition_name = target_partition_name,
132 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700133 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700134 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700135 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
136 force_writable) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700137 // Only target partitions are mapped with force_writable. On Virtual
138 // A/B devices, target partitions may overlap with source partitions, so
139 // they must be mapped with snapshot.
140 params.timeout_ms = kMapSnapshotTimeout;
141 success = snapshot_->MapUpdateSnapshot(params, path);
142 } else {
143 params.timeout_ms = kMapTimeout;
144 success = CreateLogicalPartition(params, path);
145 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700146
Yifan Hong420db9b2019-07-23 20:50:33 -0700147 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700148 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
149 << super_device << " on device mapper.";
150 return false;
151 }
152 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700153 << " to device mapper (force_writable = " << force_writable
154 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700155 mapped_devices_.insert(target_partition_name);
156 return true;
157}
158
Yifan Hong8546a712019-03-28 14:42:53 -0700159bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
160 const std::string& super_device,
161 const std::string& target_partition_name,
162 uint32_t slot,
163 bool force_writable,
164 std::string* path) {
165 DmDeviceState state = GetState(target_partition_name);
166 if (state == DmDeviceState::ACTIVE) {
167 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
168 if (GetDmDevicePathByName(target_partition_name, path)) {
169 LOG(INFO) << target_partition_name
170 << " is mapped on device mapper: " << *path;
171 return true;
172 }
173 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
174 return false;
175 }
176 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
177 // the device might be mapped incorrectly before. Attempt to unmap it.
178 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
179 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
180 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700181 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700182 LOG(ERROR) << target_partition_name
183 << " is mapped before the update, and it cannot be unmapped.";
184 return false;
185 }
186 state = GetState(target_partition_name);
187 if (state != DmDeviceState::INVALID) {
188 LOG(ERROR) << target_partition_name << " is unmapped but state is "
189 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
190 return false;
191 }
192 }
193 if (state == DmDeviceState::INVALID) {
194 return MapPartitionInternal(
195 super_device, target_partition_name, slot, force_writable, path);
196 }
197
198 LOG(ERROR) << target_partition_name
199 << " is mapped on device mapper but state is unknown: "
200 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
201 return false;
202}
203
Yifan Hong537802d2018-08-15 13:15:42 -0700204bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700205 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700206 if (DeviceMapper::Instance().GetState(target_partition_name) !=
207 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700208 // Partitions at target slot on non-Virtual A/B devices are mapped as
209 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
210 // preopt apps as dm-linear.
211 // Call DestroyLogicalPartition to handle these cases.
212 bool success = DestroyLogicalPartition(target_partition_name);
213
214 // On a Virtual A/B device, |target_partition_name| may be a leftover from
215 // a paused update. Clean up any underlying devices.
216 if (GetVirtualAbFeatureFlag().IsEnabled()) {
217 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
218 }
219
220 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700221 LOG(ERROR) << "Cannot unmap " << target_partition_name
222 << " from device mapper.";
223 return false;
224 }
225 LOG(INFO) << "Successfully unmapped " << target_partition_name
226 << " from device mapper.";
227 }
228 mapped_devices_.erase(target_partition_name);
229 return true;
230}
231
Yifan Hong02513dc2019-10-30 11:23:04 -0700232void DynamicPartitionControlAndroid::CleanupInternal() {
Yifan Hong2c62c132019-10-24 14:53:40 -0700233 metadata_device_.reset();
Tao Bao8c4d0082019-08-08 08:56:16 -0700234 if (mapped_devices_.empty()) {
235 return;
236 }
Yifan Hong537802d2018-08-15 13:15:42 -0700237 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
238 // a copy is needed for the loop.
239 std::set<std::string> mapped = mapped_devices_;
240 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
241 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700242 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700243 }
244}
245
246void DynamicPartitionControlAndroid::Cleanup() {
Yifan Hong02513dc2019-10-30 11:23:04 -0700247 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -0700248}
249
250bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
251 return base::PathExists(base::FilePath(path));
252}
253
254android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
255 const std::string& name) {
256 return DeviceMapper::Instance().GetState(name);
257}
258
259bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
260 const std::string& name, std::string* path) {
261 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
262}
263
264std::unique_ptr<MetadataBuilder>
265DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700266 const std::string& super_device, uint32_t source_slot) {
267 return LoadMetadataBuilder(
268 super_device, source_slot, BootControlInterface::kInvalidSlot);
269}
270
271std::unique_ptr<MetadataBuilder>
272DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800273 const std::string& super_device,
274 uint32_t source_slot,
275 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700276 std::unique_ptr<MetadataBuilder> builder;
277 if (target_slot == BootControlInterface::kInvalidSlot) {
278 builder =
279 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
280 } else {
Yifan Hong50a56c62019-10-14 19:35:05 -0700281 bool always_keep_source_slot = !target_supports_snapshot_;
282 builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
283 super_device,
284 source_slot,
285 target_slot,
286 always_keep_source_slot);
Yifan Hong30fa5f52019-08-05 16:39:59 -0700287 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800288
Yifan Hong537802d2018-08-15 13:15:42 -0700289 if (builder == nullptr) {
290 LOG(WARNING) << "No metadata slot "
291 << BootControlInterface::SlotName(source_slot) << " in "
292 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700293 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700294 }
295 LOG(INFO) << "Loaded metadata from slot "
296 << BootControlInterface::SlotName(source_slot) << " in "
297 << super_device;
298 return builder;
299}
300
301bool DynamicPartitionControlAndroid::StoreMetadata(
302 const std::string& super_device,
303 MetadataBuilder* builder,
304 uint32_t target_slot) {
305 auto metadata = builder->Export();
306 if (metadata == nullptr) {
307 LOG(ERROR) << "Cannot export metadata to slot "
308 << BootControlInterface::SlotName(target_slot) << " in "
309 << super_device;
310 return false;
311 }
312
Yifan Hong186bb682019-07-23 14:04:39 -0700313 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800314 if (!FlashPartitionTable(super_device, *metadata)) {
315 LOG(ERROR) << "Cannot write metadata to " << super_device;
316 return false;
317 }
318 LOG(INFO) << "Written metadata to " << super_device;
319 } else {
320 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
321 LOG(ERROR) << "Cannot write metadata to slot "
322 << BootControlInterface::SlotName(target_slot) << " in "
323 << super_device;
324 return false;
325 }
326 LOG(INFO) << "Copied metadata to slot "
327 << BootControlInterface::SlotName(target_slot) << " in "
328 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700329 }
330
Yifan Hong537802d2018-08-15 13:15:42 -0700331 return true;
332}
333
334bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
335 // We can't use fs_mgr to look up |partition_name| because fstab
336 // doesn't list every slot partition (it uses the slotselect option
337 // to mask the suffix).
338 //
339 // We can however assume that there's an entry for the /misc mount
340 // point and use that to get the device file for the misc
341 // partition. This helps us locate the disk that |partition_name|
342 // resides on. From there we'll assume that a by-name scheme is used
343 // so we can just replace the trailing "misc" by the given
344 // |partition_name| and suffix corresponding to |slot|, e.g.
345 //
346 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
347 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
348 //
349 // If needed, it's possible to relax the by-name assumption in the
350 // future by trawling /sys/block looking for the appropriate sibling
351 // of misc and then finding an entry in /dev matching the sysfs
352 // entry.
353
354 std::string err, misc_device = get_bootloader_message_blk_device(&err);
355 if (misc_device.empty()) {
356 LOG(ERROR) << "Unable to get misc block device: " << err;
357 return false;
358 }
359
360 if (!utils::IsSymlink(misc_device.c_str())) {
361 LOG(ERROR) << "Device file " << misc_device << " for /misc "
362 << "is not a symlink.";
363 return false;
364 }
365 *out = base::FilePath(misc_device).DirName().value();
366 return true;
367}
Yifan Hong012508e2019-07-22 18:30:40 -0700368
369bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
370 uint32_t source_slot,
371 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700372 const DeltaArchiveManifest& manifest,
373 bool update) {
Yifan Hong3a1a5612019-11-05 16:34:32 -0800374 if (fs_mgr_overlayfs_is_setup()) {
375 // Non DAP devices can use overlayfs as well.
376 LOG(WARNING)
377 << "overlayfs overrides are active and can interfere with our "
378 "resources.\n"
379 << "run adb enable-verity to deactivate if required and try again.";
380 }
381
382 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
383 return true;
384 }
385
386 if (target_slot == source_slot) {
387 LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot.";
388 return false;
389 }
390
391 // Although the current build supports dynamic partitions, the given payload
392 // doesn't use it for target partitions. This could happen when applying a
393 // retrofit update. Skip updating the partition metadata for the target slot.
394 is_target_dynamic_ = !manifest.dynamic_partition_metadata().groups().empty();
395 if (!is_target_dynamic_) {
396 return true;
397 }
398
Yifan Hongf0f4a912019-09-26 17:51:33 -0700399 target_supports_snapshot_ =
400 manifest.dynamic_partition_metadata().snapshot_enabled();
401
Yifan Hong2c62c132019-10-24 14:53:40 -0700402 if (GetVirtualAbFeatureFlag().IsEnabled()) {
403 metadata_device_ = snapshot_->EnsureMetadataMounted();
404 TEST_AND_RETURN_FALSE(metadata_device_ != nullptr);
405 }
406
Yifan Hongf0f4a912019-09-26 17:51:33 -0700407 if (!update)
408 return true;
409
Yifan Hong6d888562019-10-01 13:00:31 -0700410 if (GetVirtualAbFeatureFlag().IsEnabled()) {
411 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
412 // called before calling UnmapUpdateSnapshot.
413 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
414 // calls BeginUpdate() which resets update state
415 // - If !target_supports_snapshot_, explicitly CancelUpdate().
416 if (target_supports_snapshot_) {
417 return PrepareSnapshotPartitionsForUpdate(
418 source_slot, target_slot, manifest);
419 }
420 if (!snapshot_->CancelUpdate()) {
421 LOG(ERROR) << "Cannot cancel previous update.";
422 return false;
423 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700424 }
425 return PrepareDynamicPartitionsForUpdate(source_slot, target_slot, manifest);
426}
427
428bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
429 uint32_t source_slot,
430 uint32_t target_slot,
431 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700432 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
433
434 // Unmap all the target dynamic partitions because they would become
435 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700436 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
437 for (const auto& partition_name : group.partition_names()) {
438 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700439 return false;
440 }
441 }
442 }
443
444 std::string device_dir_str;
445 if (!GetDeviceDir(&device_dir_str)) {
446 return false;
447 }
448 base::FilePath device_dir(device_dir_str);
449 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700450 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700451
452 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
453 if (builder == nullptr) {
454 LOG(ERROR) << "No metadata at "
455 << BootControlInterface::SlotName(source_slot);
456 return false;
457 }
458
Yifan Hong13d41cb2019-09-16 13:18:22 -0700459 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700460 return false;
461 }
462
463 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700464 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700465 return StoreMetadata(target_device, builder.get(), target_slot);
466}
467
Yifan Hong420db9b2019-07-23 20:50:33 -0700468bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
469 uint32_t source_slot,
470 uint32_t target_slot,
471 const DeltaArchiveManifest& manifest) {
472 if (!snapshot_->BeginUpdate()) {
473 LOG(ERROR) << "Cannot begin new update.";
474 return false;
475 }
476 if (!snapshot_->CreateUpdateSnapshots(manifest)) {
477 LOG(ERROR) << "Cannot create update snapshots.";
478 return false;
479 }
480 return true;
481}
482
Yifan Hong700d7c12019-07-23 20:49:16 -0700483std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
484 uint32_t slot) {
485 return fs_mgr_get_super_partition_name(slot);
486}
487
Yifan Hong012508e2019-07-22 18:30:40 -0700488bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
489 MetadataBuilder* builder,
490 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700491 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700492 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
493 // COW group needs to be deleted to ensure there are enough space to create
494 // target partitions.
495 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
496
Yifan Hong012508e2019-07-22 18:30:40 -0700497 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
498 DeleteGroupsWithSuffix(builder, target_suffix);
499
500 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700501 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
502 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700503 }
504
505 std::string expr;
506 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700507 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700508 allocatable_space /= 2;
509 expr = "half of ";
510 }
511 if (total_size > allocatable_space) {
512 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
513 << " (" << total_size << ") has exceeded " << expr
514 << "allocatable space for dynamic partitions "
515 << allocatable_space << ".";
516 return false;
517 }
518
Yifan Hong13d41cb2019-09-16 13:18:22 -0700519 // name of partition(e.g. "system") -> size in bytes
520 std::map<std::string, uint64_t> partition_sizes;
521 for (const auto& partition : manifest.partitions()) {
522 partition_sizes.emplace(partition.partition_name(),
523 partition.new_partition_info().size());
524 }
525
526 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
527 auto group_name_suffix = group.name() + target_suffix;
528 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700529 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700530 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700531 return false;
532 }
533 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700534 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700535
Yifan Hong13d41cb2019-09-16 13:18:22 -0700536 for (const auto& partition_name : group.partition_names()) {
537 auto partition_sizes_it = partition_sizes.find(partition_name);
538 if (partition_sizes_it == partition_sizes.end()) {
539 // TODO(tbao): Support auto-filling partition info for framework-only
540 // OTA.
541 LOG(ERROR) << "dynamic_partition_metadata contains partition "
542 << partition_name << " but it is not part of the manifest. "
543 << "This is not supported.";
544 return false;
545 }
546 uint64_t partition_size = partition_sizes_it->second;
547
548 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700549 Partition* p = builder->AddPartition(
550 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
551 if (!p) {
552 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
553 << " to group " << group_name_suffix;
554 return false;
555 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700556 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700557 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700558 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700559 return false;
560 }
561 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700562 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700563 }
564 }
565
566 return true;
567}
568
Yifan Honga33bca42019-09-03 20:29:45 -0700569bool DynamicPartitionControlAndroid::FinishUpdate() {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700570 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) {
571 LOG(INFO) << "Snapshot writes are done.";
572 return snapshot_->FinishedSnapshotWrites();
573 }
574 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700575}
576
Yifan Hong3a1a5612019-11-05 16:34:32 -0800577bool DynamicPartitionControlAndroid::GetPartitionDevice(
578 const std::string& partition_name,
579 uint32_t slot,
580 uint32_t current_slot,
581 std::string* device) {
582 const auto& partition_name_suffix =
583 partition_name + SlotSuffixForSlotNumber(slot);
584 std::string device_dir_str;
585 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
586 base::FilePath device_dir(device_dir_str);
587
588 // When looking up target partition devices, treat them as static if the
589 // current payload doesn't encode them as dynamic partitions. This may happen
590 // when applying a retrofit update on top of a dynamic-partitions-enabled
591 // build.
592 if (GetDynamicPartitionsFeatureFlag().IsEnabled() &&
593 (slot == current_slot || is_target_dynamic_)) {
594 switch (GetDynamicPartitionDevice(
595 device_dir, partition_name_suffix, slot, current_slot, device)) {
596 case DynamicPartitionDeviceStatus::SUCCESS:
597 return true;
598 case DynamicPartitionDeviceStatus::TRY_STATIC:
599 break;
600 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
601 default:
602 return false;
603 }
604 }
605 base::FilePath path = device_dir.Append(partition_name_suffix);
606 if (!DeviceExists(path.value())) {
607 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
608 return false;
609 }
610
611 *device = path.value();
612 return true;
613}
614
615bool DynamicPartitionControlAndroid::IsSuperBlockDevice(
616 const base::FilePath& device_dir,
617 uint32_t current_slot,
618 const std::string& partition_name_suffix) {
619 std::string source_device =
620 device_dir.Append(GetSuperPartitionName(current_slot)).value();
621 auto source_metadata = LoadMetadataBuilder(source_device, current_slot);
622 return source_metadata->HasBlockDevice(partition_name_suffix);
623}
624
625DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus
626DynamicPartitionControlAndroid::GetDynamicPartitionDevice(
627 const base::FilePath& device_dir,
628 const std::string& partition_name_suffix,
629 uint32_t slot,
630 uint32_t current_slot,
631 std::string* device) {
632 std::string super_device =
633 device_dir.Append(GetSuperPartitionName(slot)).value();
634
635 auto builder = LoadMetadataBuilder(super_device, slot);
636 if (builder == nullptr) {
637 LOG(ERROR) << "No metadata in slot "
638 << BootControlInterface::SlotName(slot);
639 return DynamicPartitionDeviceStatus::ERROR;
640 }
641 if (builder->FindPartition(partition_name_suffix) == nullptr) {
642 LOG(INFO) << partition_name_suffix
643 << " is not in super partition metadata.";
644
645 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
646 LOG(ERROR) << "The static partition " << partition_name_suffix
647 << " is a block device for current metadata."
648 << "It cannot be used as a logical partition.";
649 return DynamicPartitionDeviceStatus::ERROR;
650 }
651
652 return DynamicPartitionDeviceStatus::TRY_STATIC;
653 }
654
655 if (slot == current_slot) {
656 if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) {
657 LOG(WARNING) << partition_name_suffix << " is at current slot but it is "
658 << "not mapped. Now try to map it.";
659 } else {
660 if (GetDmDevicePathByName(partition_name_suffix, device)) {
661 LOG(INFO) << partition_name_suffix
662 << " is mapped on device mapper: " << *device;
663 return DynamicPartitionDeviceStatus::SUCCESS;
664 }
665 LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown.";
666 return DynamicPartitionDeviceStatus::ERROR;
667 }
668 }
669
670 bool force_writable = slot != current_slot;
671 if (MapPartitionOnDeviceMapper(
672 super_device, partition_name_suffix, slot, force_writable, device)) {
673 return DynamicPartitionDeviceStatus::SUCCESS;
674 }
675 return DynamicPartitionDeviceStatus::ERROR;
676}
677
Yifan Hong537802d2018-08-15 13:15:42 -0700678} // namespace chromeos_update_engine