blob: 4ad02c74c0ec15dd261445dcfb8dfeeeab05fdbd [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(
Yifan Hong6eec9952019-12-04 13:12:01 -0800109 const std::string& partition_name, 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() &&
Yifan Hong6eec9952019-12-04 13:12:01 -0800114 mapped_devices_.count(partition_name +
115 SlotSuffixForSlotNumber(target_slot_)) > 0 &&
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000116 SourceCopyOperationIsClone(operation);
117 break;
118 default:
119 break;
120 }
Alessio Balsini14980e22019-11-26 11:46:06 +0000121 return false;
122}
123
Yifan Hong8546a712019-03-28 14:42:53 -0700124bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700125 const std::string& super_device,
126 const std::string& target_partition_name,
127 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700128 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700129 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700130 CreateLogicalPartitionParams params = {
131 .block_device = super_device,
132 .metadata_slot = slot,
133 .partition_name = target_partition_name,
134 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700135 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700136 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700137 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
138 force_writable) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700139 // Only target partitions are mapped with force_writable. On Virtual
140 // A/B devices, target partitions may overlap with source partitions, so
141 // they must be mapped with snapshot.
142 params.timeout_ms = kMapSnapshotTimeout;
143 success = snapshot_->MapUpdateSnapshot(params, path);
144 } else {
145 params.timeout_ms = kMapTimeout;
146 success = CreateLogicalPartition(params, path);
147 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700148
Yifan Hong420db9b2019-07-23 20:50:33 -0700149 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700150 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
151 << super_device << " on device mapper.";
152 return false;
153 }
154 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700155 << " to device mapper (force_writable = " << force_writable
156 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700157 mapped_devices_.insert(target_partition_name);
158 return true;
159}
160
Yifan Hong8546a712019-03-28 14:42:53 -0700161bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
162 const std::string& super_device,
163 const std::string& target_partition_name,
164 uint32_t slot,
165 bool force_writable,
166 std::string* path) {
167 DmDeviceState state = GetState(target_partition_name);
168 if (state == DmDeviceState::ACTIVE) {
169 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
170 if (GetDmDevicePathByName(target_partition_name, path)) {
171 LOG(INFO) << target_partition_name
172 << " is mapped on device mapper: " << *path;
173 return true;
174 }
175 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
176 return false;
177 }
178 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
179 // the device might be mapped incorrectly before. Attempt to unmap it.
180 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
181 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
182 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700183 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700184 LOG(ERROR) << target_partition_name
185 << " is mapped before the update, and it cannot be unmapped.";
186 return false;
187 }
188 state = GetState(target_partition_name);
189 if (state != DmDeviceState::INVALID) {
190 LOG(ERROR) << target_partition_name << " is unmapped but state is "
191 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
192 return false;
193 }
194 }
195 if (state == DmDeviceState::INVALID) {
196 return MapPartitionInternal(
197 super_device, target_partition_name, slot, force_writable, path);
198 }
199
200 LOG(ERROR) << target_partition_name
201 << " is mapped on device mapper but state is unknown: "
202 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
203 return false;
204}
205
Yifan Hong537802d2018-08-15 13:15:42 -0700206bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700207 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700208 if (DeviceMapper::Instance().GetState(target_partition_name) !=
209 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700210 // Partitions at target slot on non-Virtual A/B devices are mapped as
211 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
212 // preopt apps as dm-linear.
213 // Call DestroyLogicalPartition to handle these cases.
214 bool success = DestroyLogicalPartition(target_partition_name);
215
216 // On a Virtual A/B device, |target_partition_name| may be a leftover from
217 // a paused update. Clean up any underlying devices.
218 if (GetVirtualAbFeatureFlag().IsEnabled()) {
219 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
220 }
221
222 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700223 LOG(ERROR) << "Cannot unmap " << target_partition_name
224 << " from device mapper.";
225 return false;
226 }
227 LOG(INFO) << "Successfully unmapped " << target_partition_name
228 << " from device mapper.";
229 }
230 mapped_devices_.erase(target_partition_name);
231 return true;
232}
233
Yifan Hong02513dc2019-10-30 11:23:04 -0700234void DynamicPartitionControlAndroid::CleanupInternal() {
Yifan Hong2c62c132019-10-24 14:53:40 -0700235 metadata_device_.reset();
Tao Bao8c4d0082019-08-08 08:56:16 -0700236 if (mapped_devices_.empty()) {
237 return;
238 }
Yifan Hong537802d2018-08-15 13:15:42 -0700239 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
240 // a copy is needed for the loop.
241 std::set<std::string> mapped = mapped_devices_;
242 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
243 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700244 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700245 }
246}
247
248void DynamicPartitionControlAndroid::Cleanup() {
Yifan Hong02513dc2019-10-30 11:23:04 -0700249 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -0700250}
251
252bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
253 return base::PathExists(base::FilePath(path));
254}
255
256android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
257 const std::string& name) {
258 return DeviceMapper::Instance().GetState(name);
259}
260
261bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
262 const std::string& name, std::string* path) {
263 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
264}
265
266std::unique_ptr<MetadataBuilder>
267DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700268 const std::string& super_device, uint32_t source_slot) {
269 return LoadMetadataBuilder(
270 super_device, source_slot, BootControlInterface::kInvalidSlot);
271}
272
273std::unique_ptr<MetadataBuilder>
274DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800275 const std::string& super_device,
276 uint32_t source_slot,
277 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700278 std::unique_ptr<MetadataBuilder> builder;
279 if (target_slot == BootControlInterface::kInvalidSlot) {
280 builder =
281 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
282 } else {
Yifan Hong50a56c62019-10-14 19:35:05 -0700283 bool always_keep_source_slot = !target_supports_snapshot_;
284 builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
285 super_device,
286 source_slot,
287 target_slot,
288 always_keep_source_slot);
Yifan Hong30fa5f52019-08-05 16:39:59 -0700289 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800290
Yifan Hong537802d2018-08-15 13:15:42 -0700291 if (builder == nullptr) {
292 LOG(WARNING) << "No metadata slot "
293 << BootControlInterface::SlotName(source_slot) << " in "
294 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700295 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700296 }
297 LOG(INFO) << "Loaded metadata from slot "
298 << BootControlInterface::SlotName(source_slot) << " in "
299 << super_device;
300 return builder;
301}
302
303bool DynamicPartitionControlAndroid::StoreMetadata(
304 const std::string& super_device,
305 MetadataBuilder* builder,
306 uint32_t target_slot) {
307 auto metadata = builder->Export();
308 if (metadata == nullptr) {
309 LOG(ERROR) << "Cannot export metadata to slot "
310 << BootControlInterface::SlotName(target_slot) << " in "
311 << super_device;
312 return false;
313 }
314
Yifan Hong186bb682019-07-23 14:04:39 -0700315 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800316 if (!FlashPartitionTable(super_device, *metadata)) {
317 LOG(ERROR) << "Cannot write metadata to " << super_device;
318 return false;
319 }
320 LOG(INFO) << "Written metadata to " << super_device;
321 } else {
322 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
323 LOG(ERROR) << "Cannot write metadata to slot "
324 << BootControlInterface::SlotName(target_slot) << " in "
325 << super_device;
326 return false;
327 }
328 LOG(INFO) << "Copied metadata to slot "
329 << BootControlInterface::SlotName(target_slot) << " in "
330 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700331 }
332
Yifan Hong537802d2018-08-15 13:15:42 -0700333 return true;
334}
335
336bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
337 // We can't use fs_mgr to look up |partition_name| because fstab
338 // doesn't list every slot partition (it uses the slotselect option
339 // to mask the suffix).
340 //
341 // We can however assume that there's an entry for the /misc mount
342 // point and use that to get the device file for the misc
343 // partition. This helps us locate the disk that |partition_name|
344 // resides on. From there we'll assume that a by-name scheme is used
345 // so we can just replace the trailing "misc" by the given
346 // |partition_name| and suffix corresponding to |slot|, e.g.
347 //
348 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
349 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
350 //
351 // If needed, it's possible to relax the by-name assumption in the
352 // future by trawling /sys/block looking for the appropriate sibling
353 // of misc and then finding an entry in /dev matching the sysfs
354 // entry.
355
356 std::string err, misc_device = get_bootloader_message_blk_device(&err);
357 if (misc_device.empty()) {
358 LOG(ERROR) << "Unable to get misc block device: " << err;
359 return false;
360 }
361
362 if (!utils::IsSymlink(misc_device.c_str())) {
363 LOG(ERROR) << "Device file " << misc_device << " for /misc "
364 << "is not a symlink.";
365 return false;
366 }
367 *out = base::FilePath(misc_device).DirName().value();
368 return true;
369}
Yifan Hong012508e2019-07-22 18:30:40 -0700370
371bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
372 uint32_t source_slot,
373 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700374 const DeltaArchiveManifest& manifest,
375 bool update) {
Yifan Hong6eec9952019-12-04 13:12:01 -0800376 source_slot_ = source_slot;
377 target_slot_ = target_slot;
378
Yifan Hong3a1a5612019-11-05 16:34:32 -0800379 if (fs_mgr_overlayfs_is_setup()) {
380 // Non DAP devices can use overlayfs as well.
381 LOG(WARNING)
382 << "overlayfs overrides are active and can interfere with our "
383 "resources.\n"
384 << "run adb enable-verity to deactivate if required and try again.";
385 }
386
387 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
388 return true;
389 }
390
391 if (target_slot == source_slot) {
392 LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot.";
393 return false;
394 }
395
396 // Although the current build supports dynamic partitions, the given payload
397 // doesn't use it for target partitions. This could happen when applying a
398 // retrofit update. Skip updating the partition metadata for the target slot.
399 is_target_dynamic_ = !manifest.dynamic_partition_metadata().groups().empty();
400 if (!is_target_dynamic_) {
401 return true;
402 }
403
Yifan Hongf0f4a912019-09-26 17:51:33 -0700404 target_supports_snapshot_ =
405 manifest.dynamic_partition_metadata().snapshot_enabled();
406
Yifan Hong2c62c132019-10-24 14:53:40 -0700407 if (GetVirtualAbFeatureFlag().IsEnabled()) {
408 metadata_device_ = snapshot_->EnsureMetadataMounted();
409 TEST_AND_RETURN_FALSE(metadata_device_ != nullptr);
410 }
411
Yifan Hongf0f4a912019-09-26 17:51:33 -0700412 if (!update)
413 return true;
414
Yifan Hong6d888562019-10-01 13:00:31 -0700415 if (GetVirtualAbFeatureFlag().IsEnabled()) {
416 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
417 // called before calling UnmapUpdateSnapshot.
418 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
419 // calls BeginUpdate() which resets update state
420 // - If !target_supports_snapshot_, explicitly CancelUpdate().
421 if (target_supports_snapshot_) {
422 return PrepareSnapshotPartitionsForUpdate(
423 source_slot, target_slot, manifest);
424 }
425 if (!snapshot_->CancelUpdate()) {
426 LOG(ERROR) << "Cannot cancel previous update.";
427 return false;
428 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700429 }
430 return PrepareDynamicPartitionsForUpdate(source_slot, target_slot, manifest);
431}
432
433bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
434 uint32_t source_slot,
435 uint32_t target_slot,
436 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700437 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
438
439 // Unmap all the target dynamic partitions because they would become
440 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700441 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
442 for (const auto& partition_name : group.partition_names()) {
443 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700444 return false;
445 }
446 }
447 }
448
449 std::string device_dir_str;
450 if (!GetDeviceDir(&device_dir_str)) {
451 return false;
452 }
453 base::FilePath device_dir(device_dir_str);
454 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700455 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700456
457 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
458 if (builder == nullptr) {
459 LOG(ERROR) << "No metadata at "
460 << BootControlInterface::SlotName(source_slot);
461 return false;
462 }
463
Yifan Hong13d41cb2019-09-16 13:18:22 -0700464 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700465 return false;
466 }
467
468 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700469 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700470 return StoreMetadata(target_device, builder.get(), target_slot);
471}
472
Yifan Hong420db9b2019-07-23 20:50:33 -0700473bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
474 uint32_t source_slot,
475 uint32_t target_slot,
476 const DeltaArchiveManifest& manifest) {
477 if (!snapshot_->BeginUpdate()) {
478 LOG(ERROR) << "Cannot begin new update.";
479 return false;
480 }
481 if (!snapshot_->CreateUpdateSnapshots(manifest)) {
482 LOG(ERROR) << "Cannot create update snapshots.";
483 return false;
484 }
485 return true;
486}
487
Yifan Hong700d7c12019-07-23 20:49:16 -0700488std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
489 uint32_t slot) {
490 return fs_mgr_get_super_partition_name(slot);
491}
492
Yifan Hong012508e2019-07-22 18:30:40 -0700493bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
494 MetadataBuilder* builder,
495 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700496 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700497 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
498 // COW group needs to be deleted to ensure there are enough space to create
499 // target partitions.
500 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
501
Yifan Hong012508e2019-07-22 18:30:40 -0700502 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
503 DeleteGroupsWithSuffix(builder, target_suffix);
504
505 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700506 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
507 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700508 }
509
510 std::string expr;
511 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700512 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700513 allocatable_space /= 2;
514 expr = "half of ";
515 }
516 if (total_size > allocatable_space) {
517 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
518 << " (" << total_size << ") has exceeded " << expr
519 << "allocatable space for dynamic partitions "
520 << allocatable_space << ".";
521 return false;
522 }
523
Yifan Hong13d41cb2019-09-16 13:18:22 -0700524 // name of partition(e.g. "system") -> size in bytes
525 std::map<std::string, uint64_t> partition_sizes;
526 for (const auto& partition : manifest.partitions()) {
527 partition_sizes.emplace(partition.partition_name(),
528 partition.new_partition_info().size());
529 }
530
531 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
532 auto group_name_suffix = group.name() + target_suffix;
533 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700534 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700535 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700536 return false;
537 }
538 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700539 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700540
Yifan Hong13d41cb2019-09-16 13:18:22 -0700541 for (const auto& partition_name : group.partition_names()) {
542 auto partition_sizes_it = partition_sizes.find(partition_name);
543 if (partition_sizes_it == partition_sizes.end()) {
544 // TODO(tbao): Support auto-filling partition info for framework-only
545 // OTA.
546 LOG(ERROR) << "dynamic_partition_metadata contains partition "
547 << partition_name << " but it is not part of the manifest. "
548 << "This is not supported.";
549 return false;
550 }
551 uint64_t partition_size = partition_sizes_it->second;
552
553 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700554 Partition* p = builder->AddPartition(
555 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
556 if (!p) {
557 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
558 << " to group " << group_name_suffix;
559 return false;
560 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700561 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700562 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700563 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700564 return false;
565 }
566 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700567 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700568 }
569 }
570
571 return true;
572}
573
Yifan Honga33bca42019-09-03 20:29:45 -0700574bool DynamicPartitionControlAndroid::FinishUpdate() {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700575 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) {
576 LOG(INFO) << "Snapshot writes are done.";
577 return snapshot_->FinishedSnapshotWrites();
578 }
579 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700580}
581
Yifan Hong3a1a5612019-11-05 16:34:32 -0800582bool DynamicPartitionControlAndroid::GetPartitionDevice(
583 const std::string& partition_name,
584 uint32_t slot,
585 uint32_t current_slot,
586 std::string* device) {
587 const auto& partition_name_suffix =
588 partition_name + SlotSuffixForSlotNumber(slot);
589 std::string device_dir_str;
590 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
591 base::FilePath device_dir(device_dir_str);
592
593 // When looking up target partition devices, treat them as static if the
594 // current payload doesn't encode them as dynamic partitions. This may happen
595 // when applying a retrofit update on top of a dynamic-partitions-enabled
596 // build.
597 if (GetDynamicPartitionsFeatureFlag().IsEnabled() &&
598 (slot == current_slot || is_target_dynamic_)) {
599 switch (GetDynamicPartitionDevice(
600 device_dir, partition_name_suffix, slot, current_slot, device)) {
601 case DynamicPartitionDeviceStatus::SUCCESS:
602 return true;
603 case DynamicPartitionDeviceStatus::TRY_STATIC:
604 break;
605 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
606 default:
607 return false;
608 }
609 }
610 base::FilePath path = device_dir.Append(partition_name_suffix);
611 if (!DeviceExists(path.value())) {
612 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
613 return false;
614 }
615
616 *device = path.value();
617 return true;
618}
619
620bool DynamicPartitionControlAndroid::IsSuperBlockDevice(
621 const base::FilePath& device_dir,
622 uint32_t current_slot,
623 const std::string& partition_name_suffix) {
624 std::string source_device =
625 device_dir.Append(GetSuperPartitionName(current_slot)).value();
626 auto source_metadata = LoadMetadataBuilder(source_device, current_slot);
627 return source_metadata->HasBlockDevice(partition_name_suffix);
628}
629
630DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus
631DynamicPartitionControlAndroid::GetDynamicPartitionDevice(
632 const base::FilePath& device_dir,
633 const std::string& partition_name_suffix,
634 uint32_t slot,
635 uint32_t current_slot,
636 std::string* device) {
637 std::string super_device =
638 device_dir.Append(GetSuperPartitionName(slot)).value();
639
640 auto builder = LoadMetadataBuilder(super_device, slot);
641 if (builder == nullptr) {
642 LOG(ERROR) << "No metadata in slot "
643 << BootControlInterface::SlotName(slot);
644 return DynamicPartitionDeviceStatus::ERROR;
645 }
646 if (builder->FindPartition(partition_name_suffix) == nullptr) {
647 LOG(INFO) << partition_name_suffix
648 << " is not in super partition metadata.";
649
650 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
651 LOG(ERROR) << "The static partition " << partition_name_suffix
652 << " is a block device for current metadata."
653 << "It cannot be used as a logical partition.";
654 return DynamicPartitionDeviceStatus::ERROR;
655 }
656
657 return DynamicPartitionDeviceStatus::TRY_STATIC;
658 }
659
660 if (slot == current_slot) {
661 if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) {
662 LOG(WARNING) << partition_name_suffix << " is at current slot but it is "
663 << "not mapped. Now try to map it.";
664 } else {
665 if (GetDmDevicePathByName(partition_name_suffix, device)) {
666 LOG(INFO) << partition_name_suffix
667 << " is mapped on device mapper: " << *device;
668 return DynamicPartitionDeviceStatus::SUCCESS;
669 }
670 LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown.";
671 return DynamicPartitionDeviceStatus::ERROR;
672 }
673 }
674
675 bool force_writable = slot != current_slot;
676 if (MapPartitionOnDeviceMapper(
677 super_device, partition_name_suffix, slot, force_writable, device)) {
678 return DynamicPartitionDeviceStatus::SUCCESS;
679 }
680 return DynamicPartitionDeviceStatus::ERROR;
681}
682
Yifan Hong6eec9952019-12-04 13:12:01 -0800683void DynamicPartitionControlAndroid::set_fake_mapped_devices(
684 const std::set<std::string>& fake) {
685 mapped_devices_ = fake;
686}
687
Yifan Hong537802d2018-08-15 13:15:42 -0700688} // namespace chromeos_update_engine