blob: bd07165c240a46a2015574d1945492e4d3d311e8 [file] [log] [blame]
Yifan Hong537802d2018-08-15 13:15:42 -07001//
2// Copyright (C) 2018 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "update_engine/dynamic_partition_control_android.h"
18
Yifan Hong420db9b2019-07-23 20:50:33 -070019#include <chrono> // NOLINT(build/c++11) - using libsnapshot / liblp API
Yifan Hong13d41cb2019-09-16 13:18:22 -070020#include <map>
Yifan Hong537802d2018-08-15 13:15:42 -070021#include <memory>
22#include <set>
23#include <string>
Yifan Hong012508e2019-07-22 18:30:40 -070024#include <vector>
Yifan Hong537802d2018-08-15 13:15:42 -070025
26#include <android-base/properties.h>
27#include <android-base/strings.h>
28#include <base/files/file_util.h>
29#include <base/logging.h>
Yifan Hong012508e2019-07-22 18:30:40 -070030#include <base/strings/string_util.h>
Yifan Hong537802d2018-08-15 13:15:42 -070031#include <bootloader_message/bootloader_message.h>
Yifan Hong012508e2019-07-22 18:30:40 -070032#include <fs_mgr.h>
Yifan Hong537802d2018-08-15 13:15:42 -070033#include <fs_mgr_dm_linear.h>
Yifan Hong3a1a5612019-11-05 16:34:32 -080034#include <fs_mgr_overlayfs.h>
35#include <libdm/dm.h>
Yifan Hong420db9b2019-07-23 20:50:33 -070036#include <libsnapshot/snapshot.h>
Yifan Hong537802d2018-08-15 13:15:42 -070037
38#include "update_engine/common/boot_control_interface.h"
39#include "update_engine/common/utils.h"
Yifan Hong012508e2019-07-22 18:30:40 -070040#include "update_engine/dynamic_partition_utils.h"
Yifan Hong537802d2018-08-15 13:15:42 -070041
42using android::base::GetBoolProperty;
43using android::base::Join;
44using android::dm::DeviceMapper;
45using android::dm::DmDeviceState;
46using android::fs_mgr::CreateLogicalPartition;
David Andersonbb90dfb2019-08-13 14:14:56 -070047using android::fs_mgr::CreateLogicalPartitionParams;
Yifan Hong537802d2018-08-15 13:15:42 -070048using android::fs_mgr::DestroyLogicalPartition;
49using android::fs_mgr::MetadataBuilder;
Yifan Hong012508e2019-07-22 18:30:40 -070050using android::fs_mgr::Partition;
Yifan Hong6e706b12018-11-09 16:50:51 -080051using android::fs_mgr::PartitionOpener;
Yifan Hong012508e2019-07-22 18:30:40 -070052using android::fs_mgr::SlotSuffixForSlotNumber;
Yifan Hong0850bca2020-01-16 15:14:07 -080053using android::snapshot::Return;
Yifan Hongf033ecb2020-01-07 18:13:56 -080054using android::snapshot::SnapshotManager;
Alessio Balsini2a3b4a22019-11-25 16:46:51 +000055using android::snapshot::SourceCopyOperationIsClone;
Yifan Hong537802d2018-08-15 13:15:42 -070056
57namespace chromeos_update_engine {
58
Yifan Hong6e706b12018-11-09 16:50:51 -080059constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
60constexpr char kRetrfoitDynamicPartitions[] =
61 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong413d5722019-07-23 14:21:09 -070062constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled";
63constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit";
Yifan Hong420db9b2019-07-23 20:50:33 -070064// Map timeout for dynamic partitions.
65constexpr std::chrono::milliseconds kMapTimeout{1000};
66// Map timeout for dynamic partitions with snapshots. Since several devices
67// needs to be mapped, this timeout is longer than |kMapTimeout|.
68constexpr std::chrono::milliseconds kMapSnapshotTimeout{5000};
69
Yifan Hong537802d2018-08-15 13:15:42 -070070DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
Yifan Hong02513dc2019-10-30 11:23:04 -070071 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -070072}
73
Yifan Hong186bb682019-07-23 14:04:39 -070074static FeatureFlag GetFeatureFlag(const char* enable_prop,
75 const char* retrofit_prop) {
76 bool retrofit = GetBoolProperty(retrofit_prop, false);
77 bool enabled = GetBoolProperty(enable_prop, false);
78 if (retrofit && !enabled) {
79 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
80 << " is not. These sysprops are inconsistent. Assume that "
81 << enable_prop << " is true from now on.";
82 }
83 if (retrofit) {
84 return FeatureFlag(FeatureFlag::Value::RETROFIT);
85 }
86 if (enabled) {
87 return FeatureFlag(FeatureFlag::Value::LAUNCH);
88 }
89 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070090}
91
Yifan Hongb38e1af2019-10-17 14:59:22 -070092DynamicPartitionControlAndroid::DynamicPartitionControlAndroid()
93 : dynamic_partitions_(
94 GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions)),
95 virtual_ab_(GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit)) {
96 if (GetVirtualAbFeatureFlag().IsEnabled()) {
Yifan Hongf033ecb2020-01-07 18:13:56 -080097 snapshot_ = SnapshotManager::New();
Yifan Hongb38e1af2019-10-17 14:59:22 -070098 CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
99 }
100}
101
Yifan Hong186bb682019-07-23 14:04:39 -0700102FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700103 return dynamic_partitions_;
Yifan Hong6e706b12018-11-09 16:50:51 -0800104}
105
Yifan Hong413d5722019-07-23 14:21:09 -0700106FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700107 return virtual_ab_;
Yifan Hong413d5722019-07-23 14:21:09 -0700108}
109
Alessio Balsini14980e22019-11-26 11:46:06 +0000110bool DynamicPartitionControlAndroid::ShouldSkipOperation(
Yifan Hong6eec9952019-12-04 13:12:01 -0800111 const std::string& partition_name, const InstallOperation& operation) {
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000112 switch (operation.type()) {
113 case InstallOperation::SOURCE_COPY:
114 return target_supports_snapshot_ &&
115 GetVirtualAbFeatureFlag().IsEnabled() &&
Yifan Hong6eec9952019-12-04 13:12:01 -0800116 mapped_devices_.count(partition_name +
117 SlotSuffixForSlotNumber(target_slot_)) > 0 &&
Alessio Balsini2a3b4a22019-11-25 16:46:51 +0000118 SourceCopyOperationIsClone(operation);
119 break;
120 default:
121 break;
122 }
Alessio Balsini14980e22019-11-26 11:46:06 +0000123 return false;
124}
125
Yifan Hong8546a712019-03-28 14:42:53 -0700126bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700127 const std::string& super_device,
128 const std::string& target_partition_name,
129 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700130 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700131 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700132 CreateLogicalPartitionParams params = {
133 .block_device = super_device,
134 .metadata_slot = slot,
135 .partition_name = target_partition_name,
136 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700137 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700138 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700139 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
140 force_writable) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700141 // Only target partitions are mapped with force_writable. On Virtual
142 // A/B devices, target partitions may overlap with source partitions, so
143 // they must be mapped with snapshot.
144 params.timeout_ms = kMapSnapshotTimeout;
145 success = snapshot_->MapUpdateSnapshot(params, path);
146 } else {
147 params.timeout_ms = kMapTimeout;
148 success = CreateLogicalPartition(params, path);
149 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700150
Yifan Hong420db9b2019-07-23 20:50:33 -0700151 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700152 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
153 << super_device << " on device mapper.";
154 return false;
155 }
156 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700157 << " to device mapper (force_writable = " << force_writable
158 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700159 mapped_devices_.insert(target_partition_name);
160 return true;
161}
162
Yifan Hong8546a712019-03-28 14:42:53 -0700163bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
164 const std::string& super_device,
165 const std::string& target_partition_name,
166 uint32_t slot,
167 bool force_writable,
168 std::string* path) {
169 DmDeviceState state = GetState(target_partition_name);
170 if (state == DmDeviceState::ACTIVE) {
171 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
172 if (GetDmDevicePathByName(target_partition_name, path)) {
173 LOG(INFO) << target_partition_name
174 << " is mapped on device mapper: " << *path;
175 return true;
176 }
177 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
178 return false;
179 }
180 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
181 // the device might be mapped incorrectly before. Attempt to unmap it.
182 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
183 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
184 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700185 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700186 LOG(ERROR) << target_partition_name
187 << " is mapped before the update, and it cannot be unmapped.";
188 return false;
189 }
190 state = GetState(target_partition_name);
191 if (state != DmDeviceState::INVALID) {
192 LOG(ERROR) << target_partition_name << " is unmapped but state is "
193 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
194 return false;
195 }
196 }
197 if (state == DmDeviceState::INVALID) {
198 return MapPartitionInternal(
199 super_device, target_partition_name, slot, force_writable, path);
200 }
201
202 LOG(ERROR) << target_partition_name
203 << " is mapped on device mapper but state is unknown: "
204 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
205 return false;
206}
207
Yifan Hong537802d2018-08-15 13:15:42 -0700208bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700209 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700210 if (DeviceMapper::Instance().GetState(target_partition_name) !=
211 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700212 // Partitions at target slot on non-Virtual A/B devices are mapped as
213 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
214 // preopt apps as dm-linear.
215 // Call DestroyLogicalPartition to handle these cases.
216 bool success = DestroyLogicalPartition(target_partition_name);
217
218 // On a Virtual A/B device, |target_partition_name| may be a leftover from
219 // a paused update. Clean up any underlying devices.
220 if (GetVirtualAbFeatureFlag().IsEnabled()) {
221 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
222 }
223
224 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700225 LOG(ERROR) << "Cannot unmap " << target_partition_name
226 << " from device mapper.";
227 return false;
228 }
229 LOG(INFO) << "Successfully unmapped " << target_partition_name
230 << " from device mapper.";
231 }
232 mapped_devices_.erase(target_partition_name);
233 return true;
234}
235
Yifan Hong02513dc2019-10-30 11:23:04 -0700236void DynamicPartitionControlAndroid::CleanupInternal() {
Yifan Hong2c62c132019-10-24 14:53:40 -0700237 metadata_device_.reset();
Tao Bao8c4d0082019-08-08 08:56:16 -0700238 if (mapped_devices_.empty()) {
239 return;
240 }
Yifan Hong537802d2018-08-15 13:15:42 -0700241 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
242 // a copy is needed for the loop.
243 std::set<std::string> mapped = mapped_devices_;
244 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
245 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700246 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700247 }
248}
249
250void DynamicPartitionControlAndroid::Cleanup() {
Yifan Hong02513dc2019-10-30 11:23:04 -0700251 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -0700252}
253
254bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
255 return base::PathExists(base::FilePath(path));
256}
257
258android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
259 const std::string& name) {
260 return DeviceMapper::Instance().GetState(name);
261}
262
263bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
264 const std::string& name, std::string* path) {
265 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
266}
267
268std::unique_ptr<MetadataBuilder>
269DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700270 const std::string& super_device, uint32_t source_slot) {
271 return LoadMetadataBuilder(
272 super_device, source_slot, BootControlInterface::kInvalidSlot);
273}
274
275std::unique_ptr<MetadataBuilder>
276DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800277 const std::string& super_device,
278 uint32_t source_slot,
279 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700280 std::unique_ptr<MetadataBuilder> builder;
281 if (target_slot == BootControlInterface::kInvalidSlot) {
282 builder =
283 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
284 } else {
Yifan Hong50a56c62019-10-14 19:35:05 -0700285 bool always_keep_source_slot = !target_supports_snapshot_;
286 builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
287 super_device,
288 source_slot,
289 target_slot,
290 always_keep_source_slot);
Yifan Hong30fa5f52019-08-05 16:39:59 -0700291 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800292
Yifan Hong537802d2018-08-15 13:15:42 -0700293 if (builder == nullptr) {
294 LOG(WARNING) << "No metadata slot "
295 << BootControlInterface::SlotName(source_slot) << " in "
296 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700297 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700298 }
299 LOG(INFO) << "Loaded metadata from slot "
300 << BootControlInterface::SlotName(source_slot) << " in "
301 << super_device;
302 return builder;
303}
304
305bool DynamicPartitionControlAndroid::StoreMetadata(
306 const std::string& super_device,
307 MetadataBuilder* builder,
308 uint32_t target_slot) {
309 auto metadata = builder->Export();
310 if (metadata == nullptr) {
311 LOG(ERROR) << "Cannot export metadata to slot "
312 << BootControlInterface::SlotName(target_slot) << " in "
313 << super_device;
314 return false;
315 }
316
Yifan Hong186bb682019-07-23 14:04:39 -0700317 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800318 if (!FlashPartitionTable(super_device, *metadata)) {
319 LOG(ERROR) << "Cannot write metadata to " << super_device;
320 return false;
321 }
322 LOG(INFO) << "Written metadata to " << super_device;
323 } else {
324 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
325 LOG(ERROR) << "Cannot write metadata to slot "
326 << BootControlInterface::SlotName(target_slot) << " in "
327 << super_device;
328 return false;
329 }
330 LOG(INFO) << "Copied metadata to slot "
331 << BootControlInterface::SlotName(target_slot) << " in "
332 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700333 }
334
Yifan Hong537802d2018-08-15 13:15:42 -0700335 return true;
336}
337
338bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
339 // We can't use fs_mgr to look up |partition_name| because fstab
340 // doesn't list every slot partition (it uses the slotselect option
341 // to mask the suffix).
342 //
343 // We can however assume that there's an entry for the /misc mount
344 // point and use that to get the device file for the misc
345 // partition. This helps us locate the disk that |partition_name|
346 // resides on. From there we'll assume that a by-name scheme is used
347 // so we can just replace the trailing "misc" by the given
348 // |partition_name| and suffix corresponding to |slot|, e.g.
349 //
350 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
351 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
352 //
353 // If needed, it's possible to relax the by-name assumption in the
354 // future by trawling /sys/block looking for the appropriate sibling
355 // of misc and then finding an entry in /dev matching the sysfs
356 // entry.
357
358 std::string err, misc_device = get_bootloader_message_blk_device(&err);
359 if (misc_device.empty()) {
360 LOG(ERROR) << "Unable to get misc block device: " << err;
361 return false;
362 }
363
364 if (!utils::IsSymlink(misc_device.c_str())) {
365 LOG(ERROR) << "Device file " << misc_device << " for /misc "
366 << "is not a symlink.";
367 return false;
368 }
369 *out = base::FilePath(misc_device).DirName().value();
370 return true;
371}
Yifan Hong012508e2019-07-22 18:30:40 -0700372
373bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
374 uint32_t source_slot,
375 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700376 const DeltaArchiveManifest& manifest,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800377 bool update,
378 uint64_t* required_size) {
Yifan Hong6eec9952019-12-04 13:12:01 -0800379 source_slot_ = source_slot;
380 target_slot_ = target_slot;
Yifan Hongf033ecb2020-01-07 18:13:56 -0800381 if (required_size != nullptr) {
382 *required_size = 0;
383 }
Yifan Hong6eec9952019-12-04 13:12:01 -0800384
Yifan Hong3a1a5612019-11-05 16:34:32 -0800385 if (fs_mgr_overlayfs_is_setup()) {
386 // Non DAP devices can use overlayfs as well.
387 LOG(WARNING)
388 << "overlayfs overrides are active and can interfere with our "
389 "resources.\n"
390 << "run adb enable-verity to deactivate if required and try again.";
391 }
392
393 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
394 return true;
395 }
396
397 if (target_slot == source_slot) {
398 LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot.";
399 return false;
400 }
401
402 // Although the current build supports dynamic partitions, the given payload
403 // doesn't use it for target partitions. This could happen when applying a
404 // retrofit update. Skip updating the partition metadata for the target slot.
405 is_target_dynamic_ = !manifest.dynamic_partition_metadata().groups().empty();
406 if (!is_target_dynamic_) {
407 return true;
408 }
409
Yifan Hongf0f4a912019-09-26 17:51:33 -0700410 target_supports_snapshot_ =
411 manifest.dynamic_partition_metadata().snapshot_enabled();
412
Yifan Hong2c62c132019-10-24 14:53:40 -0700413 if (GetVirtualAbFeatureFlag().IsEnabled()) {
414 metadata_device_ = snapshot_->EnsureMetadataMounted();
415 TEST_AND_RETURN_FALSE(metadata_device_ != nullptr);
416 }
417
Yifan Hongf0f4a912019-09-26 17:51:33 -0700418 if (!update)
419 return true;
420
Yifan Hong6d888562019-10-01 13:00:31 -0700421 if (GetVirtualAbFeatureFlag().IsEnabled()) {
422 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
423 // called before calling UnmapUpdateSnapshot.
424 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
425 // calls BeginUpdate() which resets update state
426 // - If !target_supports_snapshot_, explicitly CancelUpdate().
427 if (target_supports_snapshot_) {
428 return PrepareSnapshotPartitionsForUpdate(
Yifan Hongf033ecb2020-01-07 18:13:56 -0800429 source_slot, target_slot, manifest, required_size);
Yifan Hong6d888562019-10-01 13:00:31 -0700430 }
431 if (!snapshot_->CancelUpdate()) {
432 LOG(ERROR) << "Cannot cancel previous update.";
433 return false;
434 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700435 }
436 return PrepareDynamicPartitionsForUpdate(source_slot, target_slot, manifest);
437}
438
439bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
440 uint32_t source_slot,
441 uint32_t target_slot,
442 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700443 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
444
445 // Unmap all the target dynamic partitions because they would become
446 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700447 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
448 for (const auto& partition_name : group.partition_names()) {
449 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700450 return false;
451 }
452 }
453 }
454
455 std::string device_dir_str;
456 if (!GetDeviceDir(&device_dir_str)) {
457 return false;
458 }
459 base::FilePath device_dir(device_dir_str);
460 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700461 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700462
463 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
464 if (builder == nullptr) {
465 LOG(ERROR) << "No metadata at "
466 << BootControlInterface::SlotName(source_slot);
467 return false;
468 }
469
Yifan Hong13d41cb2019-09-16 13:18:22 -0700470 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700471 return false;
472 }
473
474 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700475 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700476 return StoreMetadata(target_device, builder.get(), target_slot);
477}
478
Yifan Hong420db9b2019-07-23 20:50:33 -0700479bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
480 uint32_t source_slot,
481 uint32_t target_slot,
Yifan Hongf033ecb2020-01-07 18:13:56 -0800482 const DeltaArchiveManifest& manifest,
483 uint64_t* required_size) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700484 if (!snapshot_->BeginUpdate()) {
485 LOG(ERROR) << "Cannot begin new update.";
486 return false;
487 }
Yifan Hongf033ecb2020-01-07 18:13:56 -0800488 auto ret = snapshot_->CreateUpdateSnapshots(manifest);
489 if (!ret) {
490 LOG(ERROR) << "Cannot create update snapshots: " << ret.string();
491 if (required_size != nullptr &&
Yifan Hong0850bca2020-01-16 15:14:07 -0800492 ret.error_code() == Return::ErrorCode::NO_SPACE) {
Yifan Hongf033ecb2020-01-07 18:13:56 -0800493 *required_size = ret.required_size();
494 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700495 return false;
496 }
497 return true;
498}
499
Yifan Hong700d7c12019-07-23 20:49:16 -0700500std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
501 uint32_t slot) {
502 return fs_mgr_get_super_partition_name(slot);
503}
504
Yifan Hong012508e2019-07-22 18:30:40 -0700505bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
506 MetadataBuilder* builder,
507 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700508 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700509 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
510 // COW group needs to be deleted to ensure there are enough space to create
511 // target partitions.
512 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
513
Yifan Hong012508e2019-07-22 18:30:40 -0700514 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
515 DeleteGroupsWithSuffix(builder, target_suffix);
516
517 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700518 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
519 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700520 }
521
522 std::string expr;
523 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700524 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700525 allocatable_space /= 2;
526 expr = "half of ";
527 }
528 if (total_size > allocatable_space) {
529 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
530 << " (" << total_size << ") has exceeded " << expr
531 << "allocatable space for dynamic partitions "
532 << allocatable_space << ".";
533 return false;
534 }
535
Yifan Hong13d41cb2019-09-16 13:18:22 -0700536 // name of partition(e.g. "system") -> size in bytes
537 std::map<std::string, uint64_t> partition_sizes;
538 for (const auto& partition : manifest.partitions()) {
539 partition_sizes.emplace(partition.partition_name(),
540 partition.new_partition_info().size());
541 }
542
543 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
544 auto group_name_suffix = group.name() + target_suffix;
545 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700546 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700547 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700548 return false;
549 }
550 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700551 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700552
Yifan Hong13d41cb2019-09-16 13:18:22 -0700553 for (const auto& partition_name : group.partition_names()) {
554 auto partition_sizes_it = partition_sizes.find(partition_name);
555 if (partition_sizes_it == partition_sizes.end()) {
556 // TODO(tbao): Support auto-filling partition info for framework-only
557 // OTA.
558 LOG(ERROR) << "dynamic_partition_metadata contains partition "
559 << partition_name << " but it is not part of the manifest. "
560 << "This is not supported.";
561 return false;
562 }
563 uint64_t partition_size = partition_sizes_it->second;
564
565 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700566 Partition* p = builder->AddPartition(
567 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
568 if (!p) {
569 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
570 << " to group " << group_name_suffix;
571 return false;
572 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700573 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700574 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700575 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700576 return false;
577 }
578 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700579 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700580 }
581 }
582
583 return true;
584}
585
Yifan Honga33bca42019-09-03 20:29:45 -0700586bool DynamicPartitionControlAndroid::FinishUpdate() {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700587 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) {
588 LOG(INFO) << "Snapshot writes are done.";
589 return snapshot_->FinishedSnapshotWrites();
590 }
591 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700592}
593
Yifan Hong3a1a5612019-11-05 16:34:32 -0800594bool DynamicPartitionControlAndroid::GetPartitionDevice(
595 const std::string& partition_name,
596 uint32_t slot,
597 uint32_t current_slot,
598 std::string* device) {
599 const auto& partition_name_suffix =
600 partition_name + SlotSuffixForSlotNumber(slot);
601 std::string device_dir_str;
602 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
603 base::FilePath device_dir(device_dir_str);
604
605 // When looking up target partition devices, treat them as static if the
606 // current payload doesn't encode them as dynamic partitions. This may happen
607 // when applying a retrofit update on top of a dynamic-partitions-enabled
608 // build.
609 if (GetDynamicPartitionsFeatureFlag().IsEnabled() &&
610 (slot == current_slot || is_target_dynamic_)) {
611 switch (GetDynamicPartitionDevice(
612 device_dir, partition_name_suffix, slot, current_slot, device)) {
613 case DynamicPartitionDeviceStatus::SUCCESS:
614 return true;
615 case DynamicPartitionDeviceStatus::TRY_STATIC:
616 break;
617 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
618 default:
619 return false;
620 }
621 }
622 base::FilePath path = device_dir.Append(partition_name_suffix);
623 if (!DeviceExists(path.value())) {
624 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
625 return false;
626 }
627
628 *device = path.value();
629 return true;
630}
631
632bool DynamicPartitionControlAndroid::IsSuperBlockDevice(
633 const base::FilePath& device_dir,
634 uint32_t current_slot,
635 const std::string& partition_name_suffix) {
636 std::string source_device =
637 device_dir.Append(GetSuperPartitionName(current_slot)).value();
638 auto source_metadata = LoadMetadataBuilder(source_device, current_slot);
639 return source_metadata->HasBlockDevice(partition_name_suffix);
640}
641
642DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus
643DynamicPartitionControlAndroid::GetDynamicPartitionDevice(
644 const base::FilePath& device_dir,
645 const std::string& partition_name_suffix,
646 uint32_t slot,
647 uint32_t current_slot,
648 std::string* device) {
649 std::string super_device =
650 device_dir.Append(GetSuperPartitionName(slot)).value();
651
652 auto builder = LoadMetadataBuilder(super_device, slot);
653 if (builder == nullptr) {
654 LOG(ERROR) << "No metadata in slot "
655 << BootControlInterface::SlotName(slot);
656 return DynamicPartitionDeviceStatus::ERROR;
657 }
658 if (builder->FindPartition(partition_name_suffix) == nullptr) {
659 LOG(INFO) << partition_name_suffix
660 << " is not in super partition metadata.";
661
662 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
663 LOG(ERROR) << "The static partition " << partition_name_suffix
664 << " is a block device for current metadata."
665 << "It cannot be used as a logical partition.";
666 return DynamicPartitionDeviceStatus::ERROR;
667 }
668
669 return DynamicPartitionDeviceStatus::TRY_STATIC;
670 }
671
672 if (slot == current_slot) {
673 if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) {
674 LOG(WARNING) << partition_name_suffix << " is at current slot but it is "
675 << "not mapped. Now try to map it.";
676 } else {
677 if (GetDmDevicePathByName(partition_name_suffix, device)) {
678 LOG(INFO) << partition_name_suffix
679 << " is mapped on device mapper: " << *device;
680 return DynamicPartitionDeviceStatus::SUCCESS;
681 }
682 LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown.";
683 return DynamicPartitionDeviceStatus::ERROR;
684 }
685 }
686
687 bool force_writable = slot != current_slot;
688 if (MapPartitionOnDeviceMapper(
689 super_device, partition_name_suffix, slot, force_writable, device)) {
690 return DynamicPartitionDeviceStatus::SUCCESS;
691 }
692 return DynamicPartitionDeviceStatus::ERROR;
693}
694
Yifan Hong6eec9952019-12-04 13:12:01 -0800695void DynamicPartitionControlAndroid::set_fake_mapped_devices(
696 const std::set<std::string>& fake) {
697 mapped_devices_ = fake;
698}
699
Yifan Hong537802d2018-08-15 13:15:42 -0700700} // namespace chromeos_update_engine