blob: 464cdf131dbf35dabf9393d70f3bba3fd7b4dbb2 [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
Yifan Hong8546a712019-03-28 14:42:53 -0700107bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700108 const std::string& super_device,
109 const std::string& target_partition_name,
110 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700111 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700112 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700113 CreateLogicalPartitionParams params = {
114 .block_device = super_device,
115 .metadata_slot = slot,
116 .partition_name = target_partition_name,
117 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700118 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700119 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700120 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
121 force_writable) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700122 // Only target partitions are mapped with force_writable. On Virtual
123 // A/B devices, target partitions may overlap with source partitions, so
124 // they must be mapped with snapshot.
125 params.timeout_ms = kMapSnapshotTimeout;
126 success = snapshot_->MapUpdateSnapshot(params, path);
127 } else {
128 params.timeout_ms = kMapTimeout;
129 success = CreateLogicalPartition(params, path);
130 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700131
Yifan Hong420db9b2019-07-23 20:50:33 -0700132 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700133 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
134 << super_device << " on device mapper.";
135 return false;
136 }
137 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700138 << " to device mapper (force_writable = " << force_writable
139 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700140 mapped_devices_.insert(target_partition_name);
141 return true;
142}
143
Yifan Hong8546a712019-03-28 14:42:53 -0700144bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
145 const std::string& super_device,
146 const std::string& target_partition_name,
147 uint32_t slot,
148 bool force_writable,
149 std::string* path) {
150 DmDeviceState state = GetState(target_partition_name);
151 if (state == DmDeviceState::ACTIVE) {
152 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
153 if (GetDmDevicePathByName(target_partition_name, path)) {
154 LOG(INFO) << target_partition_name
155 << " is mapped on device mapper: " << *path;
156 return true;
157 }
158 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
159 return false;
160 }
161 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
162 // the device might be mapped incorrectly before. Attempt to unmap it.
163 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
164 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
165 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700166 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700167 LOG(ERROR) << target_partition_name
168 << " is mapped before the update, and it cannot be unmapped.";
169 return false;
170 }
171 state = GetState(target_partition_name);
172 if (state != DmDeviceState::INVALID) {
173 LOG(ERROR) << target_partition_name << " is unmapped but state is "
174 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
175 return false;
176 }
177 }
178 if (state == DmDeviceState::INVALID) {
179 return MapPartitionInternal(
180 super_device, target_partition_name, slot, force_writable, path);
181 }
182
183 LOG(ERROR) << target_partition_name
184 << " is mapped on device mapper but state is unknown: "
185 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
186 return false;
187}
188
Yifan Hong537802d2018-08-15 13:15:42 -0700189bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700190 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700191 if (DeviceMapper::Instance().GetState(target_partition_name) !=
192 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700193 // Partitions at target slot on non-Virtual A/B devices are mapped as
194 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
195 // preopt apps as dm-linear.
196 // Call DestroyLogicalPartition to handle these cases.
197 bool success = DestroyLogicalPartition(target_partition_name);
198
199 // On a Virtual A/B device, |target_partition_name| may be a leftover from
200 // a paused update. Clean up any underlying devices.
201 if (GetVirtualAbFeatureFlag().IsEnabled()) {
202 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
203 }
204
205 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700206 LOG(ERROR) << "Cannot unmap " << target_partition_name
207 << " from device mapper.";
208 return false;
209 }
210 LOG(INFO) << "Successfully unmapped " << target_partition_name
211 << " from device mapper.";
212 }
213 mapped_devices_.erase(target_partition_name);
214 return true;
215}
216
Yifan Hong02513dc2019-10-30 11:23:04 -0700217void DynamicPartitionControlAndroid::CleanupInternal() {
Yifan Hong2c62c132019-10-24 14:53:40 -0700218 metadata_device_.reset();
Tao Bao8c4d0082019-08-08 08:56:16 -0700219 if (mapped_devices_.empty()) {
220 return;
221 }
Yifan Hong537802d2018-08-15 13:15:42 -0700222 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
223 // a copy is needed for the loop.
224 std::set<std::string> mapped = mapped_devices_;
225 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
226 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700227 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700228 }
229}
230
231void DynamicPartitionControlAndroid::Cleanup() {
Yifan Hong02513dc2019-10-30 11:23:04 -0700232 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -0700233}
234
235bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
236 return base::PathExists(base::FilePath(path));
237}
238
239android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
240 const std::string& name) {
241 return DeviceMapper::Instance().GetState(name);
242}
243
244bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
245 const std::string& name, std::string* path) {
246 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
247}
248
249std::unique_ptr<MetadataBuilder>
250DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700251 const std::string& super_device, uint32_t source_slot) {
252 return LoadMetadataBuilder(
253 super_device, source_slot, BootControlInterface::kInvalidSlot);
254}
255
256std::unique_ptr<MetadataBuilder>
257DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800258 const std::string& super_device,
259 uint32_t source_slot,
260 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700261 std::unique_ptr<MetadataBuilder> builder;
262 if (target_slot == BootControlInterface::kInvalidSlot) {
263 builder =
264 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
265 } else {
Yifan Hong50a56c62019-10-14 19:35:05 -0700266 bool always_keep_source_slot = !target_supports_snapshot_;
267 builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
268 super_device,
269 source_slot,
270 target_slot,
271 always_keep_source_slot);
Yifan Hong30fa5f52019-08-05 16:39:59 -0700272 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800273
Yifan Hong537802d2018-08-15 13:15:42 -0700274 if (builder == nullptr) {
275 LOG(WARNING) << "No metadata slot "
276 << BootControlInterface::SlotName(source_slot) << " in "
277 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700278 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700279 }
280 LOG(INFO) << "Loaded metadata from slot "
281 << BootControlInterface::SlotName(source_slot) << " in "
282 << super_device;
283 return builder;
284}
285
286bool DynamicPartitionControlAndroid::StoreMetadata(
287 const std::string& super_device,
288 MetadataBuilder* builder,
289 uint32_t target_slot) {
290 auto metadata = builder->Export();
291 if (metadata == nullptr) {
292 LOG(ERROR) << "Cannot export metadata to slot "
293 << BootControlInterface::SlotName(target_slot) << " in "
294 << super_device;
295 return false;
296 }
297
Yifan Hong186bb682019-07-23 14:04:39 -0700298 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800299 if (!FlashPartitionTable(super_device, *metadata)) {
300 LOG(ERROR) << "Cannot write metadata to " << super_device;
301 return false;
302 }
303 LOG(INFO) << "Written metadata to " << super_device;
304 } else {
305 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
306 LOG(ERROR) << "Cannot write metadata to slot "
307 << BootControlInterface::SlotName(target_slot) << " in "
308 << super_device;
309 return false;
310 }
311 LOG(INFO) << "Copied metadata to slot "
312 << BootControlInterface::SlotName(target_slot) << " in "
313 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700314 }
315
Yifan Hong537802d2018-08-15 13:15:42 -0700316 return true;
317}
318
319bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
320 // We can't use fs_mgr to look up |partition_name| because fstab
321 // doesn't list every slot partition (it uses the slotselect option
322 // to mask the suffix).
323 //
324 // We can however assume that there's an entry for the /misc mount
325 // point and use that to get the device file for the misc
326 // partition. This helps us locate the disk that |partition_name|
327 // resides on. From there we'll assume that a by-name scheme is used
328 // so we can just replace the trailing "misc" by the given
329 // |partition_name| and suffix corresponding to |slot|, e.g.
330 //
331 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
332 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
333 //
334 // If needed, it's possible to relax the by-name assumption in the
335 // future by trawling /sys/block looking for the appropriate sibling
336 // of misc and then finding an entry in /dev matching the sysfs
337 // entry.
338
339 std::string err, misc_device = get_bootloader_message_blk_device(&err);
340 if (misc_device.empty()) {
341 LOG(ERROR) << "Unable to get misc block device: " << err;
342 return false;
343 }
344
345 if (!utils::IsSymlink(misc_device.c_str())) {
346 LOG(ERROR) << "Device file " << misc_device << " for /misc "
347 << "is not a symlink.";
348 return false;
349 }
350 *out = base::FilePath(misc_device).DirName().value();
351 return true;
352}
Yifan Hong012508e2019-07-22 18:30:40 -0700353
354bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
355 uint32_t source_slot,
356 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700357 const DeltaArchiveManifest& manifest,
358 bool update) {
Yifan Hong3a1a5612019-11-05 16:34:32 -0800359 if (fs_mgr_overlayfs_is_setup()) {
360 // Non DAP devices can use overlayfs as well.
361 LOG(WARNING)
362 << "overlayfs overrides are active and can interfere with our "
363 "resources.\n"
364 << "run adb enable-verity to deactivate if required and try again.";
365 }
366
367 if (!GetDynamicPartitionsFeatureFlag().IsEnabled()) {
368 return true;
369 }
370
371 if (target_slot == source_slot) {
372 LOG(ERROR) << "Cannot call PreparePartitionsForUpdate on current slot.";
373 return false;
374 }
375
376 // Although the current build supports dynamic partitions, the given payload
377 // doesn't use it for target partitions. This could happen when applying a
378 // retrofit update. Skip updating the partition metadata for the target slot.
379 is_target_dynamic_ = !manifest.dynamic_partition_metadata().groups().empty();
380 if (!is_target_dynamic_) {
381 return true;
382 }
383
Yifan Hongf0f4a912019-09-26 17:51:33 -0700384 target_supports_snapshot_ =
385 manifest.dynamic_partition_metadata().snapshot_enabled();
386
Yifan Hong2c62c132019-10-24 14:53:40 -0700387 if (GetVirtualAbFeatureFlag().IsEnabled()) {
388 metadata_device_ = snapshot_->EnsureMetadataMounted();
389 TEST_AND_RETURN_FALSE(metadata_device_ != nullptr);
390 }
391
Yifan Hongf0f4a912019-09-26 17:51:33 -0700392 if (!update)
393 return true;
394
Yifan Hong6d888562019-10-01 13:00:31 -0700395 if (GetVirtualAbFeatureFlag().IsEnabled()) {
396 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
397 // called before calling UnmapUpdateSnapshot.
398 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
399 // calls BeginUpdate() which resets update state
400 // - If !target_supports_snapshot_, explicitly CancelUpdate().
401 if (target_supports_snapshot_) {
402 return PrepareSnapshotPartitionsForUpdate(
403 source_slot, target_slot, manifest);
404 }
405 if (!snapshot_->CancelUpdate()) {
406 LOG(ERROR) << "Cannot cancel previous update.";
407 return false;
408 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700409 }
410 return PrepareDynamicPartitionsForUpdate(source_slot, target_slot, manifest);
411}
412
413bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
414 uint32_t source_slot,
415 uint32_t target_slot,
416 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700417 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
418
419 // Unmap all the target dynamic partitions because they would become
420 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700421 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
422 for (const auto& partition_name : group.partition_names()) {
423 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700424 return false;
425 }
426 }
427 }
428
429 std::string device_dir_str;
430 if (!GetDeviceDir(&device_dir_str)) {
431 return false;
432 }
433 base::FilePath device_dir(device_dir_str);
434 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700435 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700436
437 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
438 if (builder == nullptr) {
439 LOG(ERROR) << "No metadata at "
440 << BootControlInterface::SlotName(source_slot);
441 return false;
442 }
443
Yifan Hong13d41cb2019-09-16 13:18:22 -0700444 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700445 return false;
446 }
447
448 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700449 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700450 return StoreMetadata(target_device, builder.get(), target_slot);
451}
452
Yifan Hong420db9b2019-07-23 20:50:33 -0700453bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
454 uint32_t source_slot,
455 uint32_t target_slot,
456 const DeltaArchiveManifest& manifest) {
457 if (!snapshot_->BeginUpdate()) {
458 LOG(ERROR) << "Cannot begin new update.";
459 return false;
460 }
461 if (!snapshot_->CreateUpdateSnapshots(manifest)) {
462 LOG(ERROR) << "Cannot create update snapshots.";
463 return false;
464 }
465 return true;
466}
467
Yifan Hong700d7c12019-07-23 20:49:16 -0700468std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
469 uint32_t slot) {
470 return fs_mgr_get_super_partition_name(slot);
471}
472
Yifan Hong012508e2019-07-22 18:30:40 -0700473bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
474 MetadataBuilder* builder,
475 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700476 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700477 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
478 // COW group needs to be deleted to ensure there are enough space to create
479 // target partitions.
480 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
481
Yifan Hong012508e2019-07-22 18:30:40 -0700482 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
483 DeleteGroupsWithSuffix(builder, target_suffix);
484
485 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700486 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
487 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700488 }
489
490 std::string expr;
491 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700492 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700493 allocatable_space /= 2;
494 expr = "half of ";
495 }
496 if (total_size > allocatable_space) {
497 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
498 << " (" << total_size << ") has exceeded " << expr
499 << "allocatable space for dynamic partitions "
500 << allocatable_space << ".";
501 return false;
502 }
503
Yifan Hong13d41cb2019-09-16 13:18:22 -0700504 // name of partition(e.g. "system") -> size in bytes
505 std::map<std::string, uint64_t> partition_sizes;
506 for (const auto& partition : manifest.partitions()) {
507 partition_sizes.emplace(partition.partition_name(),
508 partition.new_partition_info().size());
509 }
510
511 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
512 auto group_name_suffix = group.name() + target_suffix;
513 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700514 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700515 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700516 return false;
517 }
518 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700519 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700520
Yifan Hong13d41cb2019-09-16 13:18:22 -0700521 for (const auto& partition_name : group.partition_names()) {
522 auto partition_sizes_it = partition_sizes.find(partition_name);
523 if (partition_sizes_it == partition_sizes.end()) {
524 // TODO(tbao): Support auto-filling partition info for framework-only
525 // OTA.
526 LOG(ERROR) << "dynamic_partition_metadata contains partition "
527 << partition_name << " but it is not part of the manifest. "
528 << "This is not supported.";
529 return false;
530 }
531 uint64_t partition_size = partition_sizes_it->second;
532
533 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700534 Partition* p = builder->AddPartition(
535 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
536 if (!p) {
537 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
538 << " to group " << group_name_suffix;
539 return false;
540 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700541 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700542 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700543 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700544 return false;
545 }
546 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700547 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700548 }
549 }
550
551 return true;
552}
553
Yifan Honga33bca42019-09-03 20:29:45 -0700554bool DynamicPartitionControlAndroid::FinishUpdate() {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700555 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) {
556 LOG(INFO) << "Snapshot writes are done.";
557 return snapshot_->FinishedSnapshotWrites();
558 }
559 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700560}
561
Yifan Hong3a1a5612019-11-05 16:34:32 -0800562bool DynamicPartitionControlAndroid::GetPartitionDevice(
563 const std::string& partition_name,
564 uint32_t slot,
565 uint32_t current_slot,
566 std::string* device) {
567 const auto& partition_name_suffix =
568 partition_name + SlotSuffixForSlotNumber(slot);
569 std::string device_dir_str;
570 TEST_AND_RETURN_FALSE(GetDeviceDir(&device_dir_str));
571 base::FilePath device_dir(device_dir_str);
572
573 // When looking up target partition devices, treat them as static if the
574 // current payload doesn't encode them as dynamic partitions. This may happen
575 // when applying a retrofit update on top of a dynamic-partitions-enabled
576 // build.
577 if (GetDynamicPartitionsFeatureFlag().IsEnabled() &&
578 (slot == current_slot || is_target_dynamic_)) {
579 switch (GetDynamicPartitionDevice(
580 device_dir, partition_name_suffix, slot, current_slot, device)) {
581 case DynamicPartitionDeviceStatus::SUCCESS:
582 return true;
583 case DynamicPartitionDeviceStatus::TRY_STATIC:
584 break;
585 case DynamicPartitionDeviceStatus::ERROR: // fallthrough
586 default:
587 return false;
588 }
589 }
590 base::FilePath path = device_dir.Append(partition_name_suffix);
591 if (!DeviceExists(path.value())) {
592 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
593 return false;
594 }
595
596 *device = path.value();
597 return true;
598}
599
600bool DynamicPartitionControlAndroid::IsSuperBlockDevice(
601 const base::FilePath& device_dir,
602 uint32_t current_slot,
603 const std::string& partition_name_suffix) {
604 std::string source_device =
605 device_dir.Append(GetSuperPartitionName(current_slot)).value();
606 auto source_metadata = LoadMetadataBuilder(source_device, current_slot);
607 return source_metadata->HasBlockDevice(partition_name_suffix);
608}
609
610DynamicPartitionControlAndroid::DynamicPartitionDeviceStatus
611DynamicPartitionControlAndroid::GetDynamicPartitionDevice(
612 const base::FilePath& device_dir,
613 const std::string& partition_name_suffix,
614 uint32_t slot,
615 uint32_t current_slot,
616 std::string* device) {
617 std::string super_device =
618 device_dir.Append(GetSuperPartitionName(slot)).value();
619
620 auto builder = LoadMetadataBuilder(super_device, slot);
621 if (builder == nullptr) {
622 LOG(ERROR) << "No metadata in slot "
623 << BootControlInterface::SlotName(slot);
624 return DynamicPartitionDeviceStatus::ERROR;
625 }
626 if (builder->FindPartition(partition_name_suffix) == nullptr) {
627 LOG(INFO) << partition_name_suffix
628 << " is not in super partition metadata.";
629
630 if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) {
631 LOG(ERROR) << "The static partition " << partition_name_suffix
632 << " is a block device for current metadata."
633 << "It cannot be used as a logical partition.";
634 return DynamicPartitionDeviceStatus::ERROR;
635 }
636
637 return DynamicPartitionDeviceStatus::TRY_STATIC;
638 }
639
640 if (slot == current_slot) {
641 if (GetState(partition_name_suffix) != DmDeviceState::ACTIVE) {
642 LOG(WARNING) << partition_name_suffix << " is at current slot but it is "
643 << "not mapped. Now try to map it.";
644 } else {
645 if (GetDmDevicePathByName(partition_name_suffix, device)) {
646 LOG(INFO) << partition_name_suffix
647 << " is mapped on device mapper: " << *device;
648 return DynamicPartitionDeviceStatus::SUCCESS;
649 }
650 LOG(ERROR) << partition_name_suffix << "is mapped but path is unknown.";
651 return DynamicPartitionDeviceStatus::ERROR;
652 }
653 }
654
655 bool force_writable = slot != current_slot;
656 if (MapPartitionOnDeviceMapper(
657 super_device, partition_name_suffix, slot, force_writable, device)) {
658 return DynamicPartitionDeviceStatus::SUCCESS;
659 }
660 return DynamicPartitionDeviceStatus::ERROR;
661}
662
Yifan Hong537802d2018-08-15 13:15:42 -0700663} // namespace chromeos_update_engine