blob: c24aee9009d564ce647e411da6657f4de4cc03ad [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 Hong537802d2018-08-15 13:15:42 -070053
54namespace chromeos_update_engine {
55
Yifan Hong6e706b12018-11-09 16:50:51 -080056constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
57constexpr char kRetrfoitDynamicPartitions[] =
58 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong413d5722019-07-23 14:21:09 -070059constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled";
60constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit";
Yifan Hong420db9b2019-07-23 20:50:33 -070061// Map timeout for dynamic partitions.
62constexpr std::chrono::milliseconds kMapTimeout{1000};
63// Map timeout for dynamic partitions with snapshots. Since several devices
64// needs to be mapped, this timeout is longer than |kMapTimeout|.
65constexpr std::chrono::milliseconds kMapSnapshotTimeout{5000};
66
Yifan Hong537802d2018-08-15 13:15:42 -070067DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
Yifan Hong02513dc2019-10-30 11:23:04 -070068 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -070069}
70
Yifan Hong186bb682019-07-23 14:04:39 -070071static FeatureFlag GetFeatureFlag(const char* enable_prop,
72 const char* retrofit_prop) {
73 bool retrofit = GetBoolProperty(retrofit_prop, false);
74 bool enabled = GetBoolProperty(enable_prop, false);
75 if (retrofit && !enabled) {
76 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
77 << " is not. These sysprops are inconsistent. Assume that "
78 << enable_prop << " is true from now on.";
79 }
80 if (retrofit) {
81 return FeatureFlag(FeatureFlag::Value::RETROFIT);
82 }
83 if (enabled) {
84 return FeatureFlag(FeatureFlag::Value::LAUNCH);
85 }
86 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070087}
88
Yifan Hongb38e1af2019-10-17 14:59:22 -070089DynamicPartitionControlAndroid::DynamicPartitionControlAndroid()
90 : dynamic_partitions_(
91 GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions)),
92 virtual_ab_(GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit)) {
93 if (GetVirtualAbFeatureFlag().IsEnabled()) {
94 snapshot_ = android::snapshot::SnapshotManager::New();
95 CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
96 }
97}
98
Yifan Hong186bb682019-07-23 14:04:39 -070099FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700100 return dynamic_partitions_;
Yifan Hong6e706b12018-11-09 16:50:51 -0800101}
102
Yifan Hong413d5722019-07-23 14:21:09 -0700103FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700104 return virtual_ab_;
Yifan Hong413d5722019-07-23 14:21:09 -0700105}
106
Alessio Balsini14980e22019-11-26 11:46:06 +0000107bool DynamicPartitionControlAndroid::ShouldSkipOperation(
108 const InstallOperation& operation) {
109 return false;
110}
111
Yifan Hong8546a712019-03-28 14:42:53 -0700112bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700113 const std::string& super_device,
114 const std::string& target_partition_name,
115 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700116 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700117 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700118 CreateLogicalPartitionParams params = {
119 .block_device = super_device,
120 .metadata_slot = slot,
121 .partition_name = target_partition_name,
122 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700123 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700124 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700125 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
126 force_writable) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700127 // Only target partitions are mapped with force_writable. On Virtual
128 // A/B devices, target partitions may overlap with source partitions, so
129 // they must be mapped with snapshot.
130 params.timeout_ms = kMapSnapshotTimeout;
131 success = snapshot_->MapUpdateSnapshot(params, path);
132 } else {
133 params.timeout_ms = kMapTimeout;
134 success = CreateLogicalPartition(params, path);
135 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700136
Yifan Hong420db9b2019-07-23 20:50:33 -0700137 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700138 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
139 << super_device << " on device mapper.";
140 return false;
141 }
142 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700143 << " to device mapper (force_writable = " << force_writable
144 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700145 mapped_devices_.insert(target_partition_name);
146 return true;
147}
148
Yifan Hong8546a712019-03-28 14:42:53 -0700149bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
150 const std::string& super_device,
151 const std::string& target_partition_name,
152 uint32_t slot,
153 bool force_writable,
154 std::string* path) {
155 DmDeviceState state = GetState(target_partition_name);
156 if (state == DmDeviceState::ACTIVE) {
157 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
158 if (GetDmDevicePathByName(target_partition_name, path)) {
159 LOG(INFO) << target_partition_name
160 << " is mapped on device mapper: " << *path;
161 return true;
162 }
163 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
164 return false;
165 }
166 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
167 // the device might be mapped incorrectly before. Attempt to unmap it.
168 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
169 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
170 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700171 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700172 LOG(ERROR) << target_partition_name
173 << " is mapped before the update, and it cannot be unmapped.";
174 return false;
175 }
176 state = GetState(target_partition_name);
177 if (state != DmDeviceState::INVALID) {
178 LOG(ERROR) << target_partition_name << " is unmapped but state is "
179 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
180 return false;
181 }
182 }
183 if (state == DmDeviceState::INVALID) {
184 return MapPartitionInternal(
185 super_device, target_partition_name, slot, force_writable, path);
186 }
187
188 LOG(ERROR) << target_partition_name
189 << " is mapped on device mapper but state is unknown: "
190 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
191 return false;
192}
193
Yifan Hong537802d2018-08-15 13:15:42 -0700194bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700195 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700196 if (DeviceMapper::Instance().GetState(target_partition_name) !=
197 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700198 // Partitions at target slot on non-Virtual A/B devices are mapped as
199 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
200 // preopt apps as dm-linear.
201 // Call DestroyLogicalPartition to handle these cases.
202 bool success = DestroyLogicalPartition(target_partition_name);
203
204 // On a Virtual A/B device, |target_partition_name| may be a leftover from
205 // a paused update. Clean up any underlying devices.
206 if (GetVirtualAbFeatureFlag().IsEnabled()) {
207 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
208 }
209
210 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700211 LOG(ERROR) << "Cannot unmap " << target_partition_name
212 << " from device mapper.";
213 return false;
214 }
215 LOG(INFO) << "Successfully unmapped " << target_partition_name
216 << " from device mapper.";
217 }
218 mapped_devices_.erase(target_partition_name);
219 return true;
220}
221
Yifan Hong02513dc2019-10-30 11:23:04 -0700222void DynamicPartitionControlAndroid::CleanupInternal() {
Yifan Hong2c62c132019-10-24 14:53:40 -0700223 metadata_device_.reset();
Tao Bao8c4d0082019-08-08 08:56:16 -0700224 if (mapped_devices_.empty()) {
225 return;
226 }
Yifan Hong537802d2018-08-15 13:15:42 -0700227 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
228 // a copy is needed for the loop.
229 std::set<std::string> mapped = mapped_devices_;
230 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
231 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700232 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700233 }
234}
235
236void DynamicPartitionControlAndroid::Cleanup() {
Yifan Hong02513dc2019-10-30 11:23:04 -0700237 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -0700238}
239
240bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
241 return base::PathExists(base::FilePath(path));
242}
243
244android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
245 const std::string& name) {
246 return DeviceMapper::Instance().GetState(name);
247}
248
249bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
250 const std::string& name, std::string* path) {
251 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
252}
253
254std::unique_ptr<MetadataBuilder>
255DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700256 const std::string& super_device, uint32_t source_slot) {
257 return LoadMetadataBuilder(
258 super_device, source_slot, BootControlInterface::kInvalidSlot);
259}
260
261std::unique_ptr<MetadataBuilder>
262DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800263 const std::string& super_device,
264 uint32_t source_slot,
265 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700266 std::unique_ptr<MetadataBuilder> builder;
267 if (target_slot == BootControlInterface::kInvalidSlot) {
268 builder =
269 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
270 } else {
Yifan Hong50a56c62019-10-14 19:35:05 -0700271 bool always_keep_source_slot = !target_supports_snapshot_;
272 builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
273 super_device,
274 source_slot,
275 target_slot,
276 always_keep_source_slot);
Yifan Hong30fa5f52019-08-05 16:39:59 -0700277 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800278
Yifan Hong537802d2018-08-15 13:15:42 -0700279 if (builder == nullptr) {
280 LOG(WARNING) << "No metadata slot "
281 << BootControlInterface::SlotName(source_slot) << " in "
282 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700283 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700284 }
285 LOG(INFO) << "Loaded metadata from slot "
286 << BootControlInterface::SlotName(source_slot) << " in "
287 << super_device;
288 return builder;
289}
290
291bool DynamicPartitionControlAndroid::StoreMetadata(
292 const std::string& super_device,
293 MetadataBuilder* builder,
294 uint32_t target_slot) {
295 auto metadata = builder->Export();
296 if (metadata == nullptr) {
297 LOG(ERROR) << "Cannot export metadata to slot "
298 << BootControlInterface::SlotName(target_slot) << " in "
299 << super_device;
300 return false;
301 }
302
Yifan Hong186bb682019-07-23 14:04:39 -0700303 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800304 if (!FlashPartitionTable(super_device, *metadata)) {
305 LOG(ERROR) << "Cannot write metadata to " << super_device;
306 return false;
307 }
308 LOG(INFO) << "Written metadata to " << super_device;
309 } else {
310 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
311 LOG(ERROR) << "Cannot write metadata to slot "
312 << BootControlInterface::SlotName(target_slot) << " in "
313 << super_device;
314 return false;
315 }
316 LOG(INFO) << "Copied metadata to slot "
317 << BootControlInterface::SlotName(target_slot) << " in "
318 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700319 }
320
Yifan Hong537802d2018-08-15 13:15:42 -0700321 return true;
322}
323
324bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
325 // We can't use fs_mgr to look up |partition_name| because fstab
326 // doesn't list every slot partition (it uses the slotselect option
327 // to mask the suffix).
328 //
329 // We can however assume that there's an entry for the /misc mount
330 // point and use that to get the device file for the misc
331 // partition. This helps us locate the disk that |partition_name|
332 // resides on. From there we'll assume that a by-name scheme is used
333 // so we can just replace the trailing "misc" by the given
334 // |partition_name| and suffix corresponding to |slot|, e.g.
335 //
336 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
337 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
338 //
339 // If needed, it's possible to relax the by-name assumption in the
340 // future by trawling /sys/block looking for the appropriate sibling
341 // of misc and then finding an entry in /dev matching the sysfs
342 // entry.
343
344 std::string err, misc_device = get_bootloader_message_blk_device(&err);
345 if (misc_device.empty()) {
346 LOG(ERROR) << "Unable to get misc block device: " << err;
347 return false;
348 }
349
350 if (!utils::IsSymlink(misc_device.c_str())) {
351 LOG(ERROR) << "Device file " << misc_device << " for /misc "
352 << "is not a symlink.";
353 return false;
354 }
355 *out = base::FilePath(misc_device).DirName().value();
356 return true;
357}
Yifan Hong012508e2019-07-22 18:30:40 -0700358
359bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
360 uint32_t source_slot,
361 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700362 const DeltaArchiveManifest& manifest,
363 bool update) {
Yifan Hong3a1a5612019-11-05 16:34:32 -0800364 if (fs_mgr_overlayfs_is_setup()) {
365 // Non DAP devices can use overlayfs as well.
366 LOG(WARNING)
367 << "overlayfs overrides are active and can interfere with our "
368 "resources.\n"
369 << "run adb enable-verity to deactivate if required and try again.";
370 }
371
372 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
373 return true;
374 }
375
376 if (target_slot == source_slot) {
377 LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot.";
378 return false;
379 }
380
381 // Although the current build supports dynamic partitions, the given payload
382 // doesn't use it for target partitions. This could happen when applying a
383 // retrofit update. Skip updating the partition metadata for the target slot.
384 is_target_dynamic_ = !manifest.dynamic_partition_metadata().groups().empty();
385 if (!is_target_dynamic_) {
386 return true;
387 }
388
Yifan Hongf0f4a912019-09-26 17:51:33 -0700389 target_supports_snapshot_ =
390 manifest.dynamic_partition_metadata().snapshot_enabled();
391
Yifan Hong2c62c132019-10-24 14:53:40 -0700392 if (GetVirtualAbFeatureFlag().IsEnabled()) {
393 metadata_device_ = snapshot_->EnsureMetadataMounted();
394 TEST_AND_RETURN_FALSE(metadata_device_ != nullptr);
395 }
396
Yifan Hongf0f4a912019-09-26 17:51:33 -0700397 if (!update)
398 return true;
399
Yifan Hong6d888562019-10-01 13:00:31 -0700400 if (GetVirtualAbFeatureFlag().IsEnabled()) {
401 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
402 // called before calling UnmapUpdateSnapshot.
403 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
404 // calls BeginUpdate() which resets update state
405 // - If !target_supports_snapshot_, explicitly CancelUpdate().
406 if (target_supports_snapshot_) {
407 return PrepareSnapshotPartitionsForUpdate(
408 source_slot, target_slot, manifest);
409 }
410 if (!snapshot_->CancelUpdate()) {
411 LOG(ERROR) << "Cannot cancel previous update.";
412 return false;
413 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700414 }
415 return PrepareDynamicPartitionsForUpdate(source_slot, target_slot, manifest);
416}
417
418bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
419 uint32_t source_slot,
420 uint32_t target_slot,
421 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700422 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
423
424 // Unmap all the target dynamic partitions because they would become
425 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700426 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
427 for (const auto& partition_name : group.partition_names()) {
428 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700429 return false;
430 }
431 }
432 }
433
434 std::string device_dir_str;
435 if (!GetDeviceDir(&device_dir_str)) {
436 return false;
437 }
438 base::FilePath device_dir(device_dir_str);
439 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700440 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700441
442 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
443 if (builder == nullptr) {
444 LOG(ERROR) << "No metadata at "
445 << BootControlInterface::SlotName(source_slot);
446 return false;
447 }
448
Yifan Hong13d41cb2019-09-16 13:18:22 -0700449 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700450 return false;
451 }
452
453 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700454 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700455 return StoreMetadata(target_device, builder.get(), target_slot);
456}
457
Yifan Hong420db9b2019-07-23 20:50:33 -0700458bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
459 uint32_t source_slot,
460 uint32_t target_slot,
461 const DeltaArchiveManifest& manifest) {
462 if (!snapshot_->BeginUpdate()) {
463 LOG(ERROR) << "Cannot begin new update.";
464 return false;
465 }
466 if (!snapshot_->CreateUpdateSnapshots(manifest)) {
467 LOG(ERROR) << "Cannot create update snapshots.";
468 return false;
469 }
470 return true;
471}
472
Yifan Hong700d7c12019-07-23 20:49:16 -0700473std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
474 uint32_t slot) {
475 return fs_mgr_get_super_partition_name(slot);
476}
477
Yifan Hong012508e2019-07-22 18:30:40 -0700478bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
479 MetadataBuilder* builder,
480 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700481 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700482 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
483 // COW group needs to be deleted to ensure there are enough space to create
484 // target partitions.
485 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
486
Yifan Hong012508e2019-07-22 18:30:40 -0700487 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
488 DeleteGroupsWithSuffix(builder, target_suffix);
489
490 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700491 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
492 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700493 }
494
495 std::string expr;
496 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700497 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700498 allocatable_space /= 2;
499 expr = "half of ";
500 }
501 if (total_size > allocatable_space) {
502 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
503 << " (" << total_size << ") has exceeded " << expr
504 << "allocatable space for dynamic partitions "
505 << allocatable_space << ".";
506 return false;
507 }
508
Yifan Hong13d41cb2019-09-16 13:18:22 -0700509 // name of partition(e.g. "system") -> size in bytes
510 std::map<std::string, uint64_t> partition_sizes;
511 for (const auto& partition : manifest.partitions()) {
512 partition_sizes.emplace(partition.partition_name(),
513 partition.new_partition_info().size());
514 }
515
516 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
517 auto group_name_suffix = group.name() + target_suffix;
518 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700519 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700520 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700521 return false;
522 }
523 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700524 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700525
Yifan Hong13d41cb2019-09-16 13:18:22 -0700526 for (const auto& partition_name : group.partition_names()) {
527 auto partition_sizes_it = partition_sizes.find(partition_name);
528 if (partition_sizes_it == partition_sizes.end()) {
529 // TODO(tbao): Support auto-filling partition info for framework-only
530 // OTA.
531 LOG(ERROR) << "dynamic_partition_metadata contains partition "
532 << partition_name << " but it is not part of the manifest. "
533 << "This is not supported.";
534 return false;
535 }
536 uint64_t partition_size = partition_sizes_it->second;
537
538 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700539 Partition* p = builder->AddPartition(
540 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
541 if (!p) {
542 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
543 << " to group " << group_name_suffix;
544 return false;
545 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700546 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700547 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700548 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700549 return false;
550 }
551 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700552 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700553 }
554 }
555
556 return true;
557}
558
Yifan Honga33bca42019-09-03 20:29:45 -0700559bool DynamicPartitionControlAndroid::FinishUpdate() {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700560 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) {
561 LOG(INFO) << "Snapshot writes are done.";
562 return snapshot_->FinishedSnapshotWrites();
563 }
564 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700565}
566
Yifan Hong3a1a5612019-11-05 16:34:32 -0800567bool DynamicPartitionControlAndroid::GetPartitionDevice(
568 const std::string& partition_name,
569 uint32_t slot,
570 uint32_t current_slot,
571 std::string* device) {
572 const auto& partition_name_suffix =
573 partition_name + SlotSuffixForSlotNumber(slot);
574 std::string device_dir_str;
575 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
576 base::FilePath device_dir(device_dir_str);
577
578 // When looking up target partition devices, treat them as static if the
579 // current payload doesn't encode them as dynamic partitions. This may happen
580 // when applying a retrofit update on top of a dynamic-partitions-enabled
581 // build.
582 if (GetDynamicPartitionsFeatureFlag().IsEnabled() &&
583 (slot == current_slot || is_target_dynamic_)) {
584 switch (GetDynamicPartitionDevice(
585 device_dir, partition_name_suffix, slot, current_slot, device)) {
586 case DynamicPartitionDeviceStatus::SUCCESS:
587 return true;
588 case DynamicPartitionDeviceStatus::TRY_STATIC:
589 break;
590 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
591 default:
592 return false;
593 }
594 }
595 base::FilePath path = device_dir.Append(partition_name_suffix);
596 if (!DeviceExists(path.value())) {
597 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
598 return false;
599 }
600
601 *device = path.value();
602 return true;
603}
604
605bool DynamicPartitionControlAndroid::IsSuperBlockDevice(
606 const base::FilePath& device_dir,
607 uint32_t current_slot,
608 const std::string& partition_name_suffix) {
609 std::string source_device =
610 device_dir.Append(GetSuperPartitionName(current_slot)).value();
611 auto source_metadata = LoadMetadataBuilder(source_device, current_slot);
612 return source_metadata->HasBlockDevice(partition_name_suffix);
613}
614
615DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus
616DynamicPartitionControlAndroid::GetDynamicPartitionDevice(
617 const base::FilePath& device_dir,
618 const std::string& partition_name_suffix,
619 uint32_t slot,
620 uint32_t current_slot,
621 std::string* device) {
622 std::string super_device =
623 device_dir.Append(GetSuperPartitionName(slot)).value();
624
625 auto builder = LoadMetadataBuilder(super_device, slot);
626 if (builder == nullptr) {
627 LOG(ERROR) << "No metadata in slot "
628 << BootControlInterface::SlotName(slot);
629 return DynamicPartitionDeviceStatus::ERROR;
630 }
631 if (builder->FindPartition(partition_name_suffix) == nullptr) {
632 LOG(INFO) << partition_name_suffix
633 << " is not in super partition metadata.";
634
635 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
636 LOG(ERROR) << "The static partition " << partition_name_suffix
637 << " is a block device for current metadata."
638 << "It cannot be used as a logical partition.";
639 return DynamicPartitionDeviceStatus::ERROR;
640 }
641
642 return DynamicPartitionDeviceStatus::TRY_STATIC;
643 }
644
645 if (slot == current_slot) {
646 if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) {
647 LOG(WARNING) << partition_name_suffix << " is at current slot but it is "
648 << "not mapped. Now try to map it.";
649 } else {
650 if (GetDmDevicePathByName(partition_name_suffix, device)) {
651 LOG(INFO) << partition_name_suffix
652 << " is mapped on device mapper: " << *device;
653 return DynamicPartitionDeviceStatus::SUCCESS;
654 }
655 LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown.";
656 return DynamicPartitionDeviceStatus::ERROR;
657 }
658 }
659
660 bool force_writable = slot != current_slot;
661 if (MapPartitionOnDeviceMapper(
662 super_device, partition_name_suffix, slot, force_writable, device)) {
663 return DynamicPartitionDeviceStatus::SUCCESS;
664 }
665 return DynamicPartitionDeviceStatus::ERROR;
666}
667
Yifan Hong537802d2018-08-15 13:15:42 -0700668} // namespace chromeos_update_engine