blob: 2bd7d5104cf4b1fcc39173bca9714560a59f91f0 [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 Hong420db9b2019-07-23 20:50:33 -070034#include <libsnapshot/snapshot.h>
Yifan Hong537802d2018-08-15 13:15:42 -070035
36#include "update_engine/common/boot_control_interface.h"
37#include "update_engine/common/utils.h"
Yifan Hong012508e2019-07-22 18:30:40 -070038#include "update_engine/dynamic_partition_utils.h"
Yifan Hong537802d2018-08-15 13:15:42 -070039
40using android::base::GetBoolProperty;
41using android::base::Join;
42using android::dm::DeviceMapper;
43using android::dm::DmDeviceState;
44using android::fs_mgr::CreateLogicalPartition;
David Andersonbb90dfb2019-08-13 14:14:56 -070045using android::fs_mgr::CreateLogicalPartitionParams;
Yifan Hong537802d2018-08-15 13:15:42 -070046using android::fs_mgr::DestroyLogicalPartition;
47using android::fs_mgr::MetadataBuilder;
Yifan Hong012508e2019-07-22 18:30:40 -070048using android::fs_mgr::Partition;
Yifan Hong6e706b12018-11-09 16:50:51 -080049using android::fs_mgr::PartitionOpener;
Yifan Hong012508e2019-07-22 18:30:40 -070050using android::fs_mgr::SlotSuffixForSlotNumber;
Yifan Hong537802d2018-08-15 13:15:42 -070051
52namespace chromeos_update_engine {
53
Yifan Hong6e706b12018-11-09 16:50:51 -080054constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
55constexpr char kRetrfoitDynamicPartitions[] =
56 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong413d5722019-07-23 14:21:09 -070057constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled";
58constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit";
Yifan Hong420db9b2019-07-23 20:50:33 -070059// Map timeout for dynamic partitions.
60constexpr std::chrono::milliseconds kMapTimeout{1000};
61// Map timeout for dynamic partitions with snapshots. Since several devices
62// needs to be mapped, this timeout is longer than |kMapTimeout|.
63constexpr std::chrono::milliseconds kMapSnapshotTimeout{5000};
64
65DynamicPartitionControlAndroid::DynamicPartitionControlAndroid() {
66 if (GetVirtualAbFeatureFlag().IsEnabled()) {
67 snapshot_ = android::snapshot::SnapshotManager::New();
68 CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
69 }
70}
Yifan Hong537802d2018-08-15 13:15:42 -070071
72DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
73 CleanupInternal(false /* wait */);
74}
75
Yifan Hong186bb682019-07-23 14:04:39 -070076static FeatureFlag GetFeatureFlag(const char* enable_prop,
77 const char* retrofit_prop) {
78 bool retrofit = GetBoolProperty(retrofit_prop, false);
79 bool enabled = GetBoolProperty(enable_prop, false);
80 if (retrofit && !enabled) {
81 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
82 << " is not. These sysprops are inconsistent. Assume that "
83 << enable_prop << " is true from now on.";
84 }
85 if (retrofit) {
86 return FeatureFlag(FeatureFlag::Value::RETROFIT);
87 }
88 if (enabled) {
89 return FeatureFlag(FeatureFlag::Value::LAUNCH);
90 }
91 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070092}
93
Yifan Hong186bb682019-07-23 14:04:39 -070094FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
95 return GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions);
Yifan Hong6e706b12018-11-09 16:50:51 -080096}
97
Yifan Hong413d5722019-07-23 14:21:09 -070098FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
99 return GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit);
100}
101
Yifan Hong8546a712019-03-28 14:42:53 -0700102bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700103 const std::string& super_device,
104 const std::string& target_partition_name,
105 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700106 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700107 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700108 CreateLogicalPartitionParams params = {
109 .block_device = super_device,
110 .metadata_slot = slot,
111 .partition_name = target_partition_name,
112 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700113 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700114 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700115 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
116 force_writable) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700117 // Only target partitions are mapped with force_writable. On Virtual
118 // A/B devices, target partitions may overlap with source partitions, so
119 // they must be mapped with snapshot.
120 params.timeout_ms = kMapSnapshotTimeout;
121 success = snapshot_->MapUpdateSnapshot(params, path);
122 } else {
123 params.timeout_ms = kMapTimeout;
124 success = CreateLogicalPartition(params, path);
125 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700126
Yifan Hong420db9b2019-07-23 20:50:33 -0700127 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700128 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
129 << super_device << " on device mapper.";
130 return false;
131 }
132 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700133 << " to device mapper (force_writable = " << force_writable
134 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700135 mapped_devices_.insert(target_partition_name);
136 return true;
137}
138
Yifan Hong8546a712019-03-28 14:42:53 -0700139bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
140 const std::string& super_device,
141 const std::string& target_partition_name,
142 uint32_t slot,
143 bool force_writable,
144 std::string* path) {
145 DmDeviceState state = GetState(target_partition_name);
146 if (state == DmDeviceState::ACTIVE) {
147 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
148 if (GetDmDevicePathByName(target_partition_name, path)) {
149 LOG(INFO) << target_partition_name
150 << " is mapped on device mapper: " << *path;
151 return true;
152 }
153 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
154 return false;
155 }
156 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
157 // the device might be mapped incorrectly before. Attempt to unmap it.
158 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
159 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
160 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700161 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700162 LOG(ERROR) << target_partition_name
163 << " is mapped before the update, and it cannot be unmapped.";
164 return false;
165 }
166 state = GetState(target_partition_name);
167 if (state != DmDeviceState::INVALID) {
168 LOG(ERROR) << target_partition_name << " is unmapped but state is "
169 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
170 return false;
171 }
172 }
173 if (state == DmDeviceState::INVALID) {
174 return MapPartitionInternal(
175 super_device, target_partition_name, slot, force_writable, path);
176 }
177
178 LOG(ERROR) << target_partition_name
179 << " is mapped on device mapper but state is unknown: "
180 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
181 return false;
182}
183
Yifan Hong537802d2018-08-15 13:15:42 -0700184bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700185 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700186 if (DeviceMapper::Instance().GetState(target_partition_name) !=
187 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700188 // Partitions at target slot on non-Virtual A/B devices are mapped as
189 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
190 // preopt apps as dm-linear.
191 // Call DestroyLogicalPartition to handle these cases.
192 bool success = DestroyLogicalPartition(target_partition_name);
193
194 // On a Virtual A/B device, |target_partition_name| may be a leftover from
195 // a paused update. Clean up any underlying devices.
196 if (GetVirtualAbFeatureFlag().IsEnabled()) {
197 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
198 }
199
200 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700201 LOG(ERROR) << "Cannot unmap " << target_partition_name
202 << " from device mapper.";
203 return false;
204 }
205 LOG(INFO) << "Successfully unmapped " << target_partition_name
206 << " from device mapper.";
207 }
208 mapped_devices_.erase(target_partition_name);
209 return true;
210}
211
212void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
Tao Bao8c4d0082019-08-08 08:56:16 -0700213 if (mapped_devices_.empty()) {
214 return;
215 }
Yifan Hong537802d2018-08-15 13:15:42 -0700216 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
217 // a copy is needed for the loop.
218 std::set<std::string> mapped = mapped_devices_;
219 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
220 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700221 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700222 }
223}
224
225void DynamicPartitionControlAndroid::Cleanup() {
226 CleanupInternal(true /* wait */);
227}
228
229bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
230 return base::PathExists(base::FilePath(path));
231}
232
233android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
234 const std::string& name) {
235 return DeviceMapper::Instance().GetState(name);
236}
237
238bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
239 const std::string& name, std::string* path) {
240 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
241}
242
243std::unique_ptr<MetadataBuilder>
244DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700245 const std::string& super_device, uint32_t source_slot) {
246 return LoadMetadataBuilder(
247 super_device, source_slot, BootControlInterface::kInvalidSlot);
248}
249
250std::unique_ptr<MetadataBuilder>
251DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800252 const std::string& super_device,
253 uint32_t source_slot,
254 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700255 std::unique_ptr<MetadataBuilder> builder;
256 if (target_slot == BootControlInterface::kInvalidSlot) {
257 builder =
258 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
259 } else {
260 builder = MetadataBuilder::NewForUpdate(
261 PartitionOpener(), super_device, source_slot, target_slot);
262 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800263
Yifan Hong537802d2018-08-15 13:15:42 -0700264 if (builder == nullptr) {
265 LOG(WARNING) << "No metadata slot "
266 << BootControlInterface::SlotName(source_slot) << " in "
267 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700268 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700269 }
270 LOG(INFO) << "Loaded metadata from slot "
271 << BootControlInterface::SlotName(source_slot) << " in "
272 << super_device;
273 return builder;
274}
275
276bool DynamicPartitionControlAndroid::StoreMetadata(
277 const std::string& super_device,
278 MetadataBuilder* builder,
279 uint32_t target_slot) {
280 auto metadata = builder->Export();
281 if (metadata == nullptr) {
282 LOG(ERROR) << "Cannot export metadata to slot "
283 << BootControlInterface::SlotName(target_slot) << " in "
284 << super_device;
285 return false;
286 }
287
Yifan Hong186bb682019-07-23 14:04:39 -0700288 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800289 if (!FlashPartitionTable(super_device, *metadata)) {
290 LOG(ERROR) << "Cannot write metadata to " << super_device;
291 return false;
292 }
293 LOG(INFO) << "Written metadata to " << super_device;
294 } else {
295 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
296 LOG(ERROR) << "Cannot write metadata to slot "
297 << BootControlInterface::SlotName(target_slot) << " in "
298 << super_device;
299 return false;
300 }
301 LOG(INFO) << "Copied metadata to slot "
302 << BootControlInterface::SlotName(target_slot) << " in "
303 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700304 }
305
Yifan Hong537802d2018-08-15 13:15:42 -0700306 return true;
307}
308
309bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
310 // We can't use fs_mgr to look up |partition_name| because fstab
311 // doesn't list every slot partition (it uses the slotselect option
312 // to mask the suffix).
313 //
314 // We can however assume that there's an entry for the /misc mount
315 // point and use that to get the device file for the misc
316 // partition. This helps us locate the disk that |partition_name|
317 // resides on. From there we'll assume that a by-name scheme is used
318 // so we can just replace the trailing "misc" by the given
319 // |partition_name| and suffix corresponding to |slot|, e.g.
320 //
321 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
322 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
323 //
324 // If needed, it's possible to relax the by-name assumption in the
325 // future by trawling /sys/block looking for the appropriate sibling
326 // of misc and then finding an entry in /dev matching the sysfs
327 // entry.
328
329 std::string err, misc_device = get_bootloader_message_blk_device(&err);
330 if (misc_device.empty()) {
331 LOG(ERROR) << "Unable to get misc block device: " << err;
332 return false;
333 }
334
335 if (!utils::IsSymlink(misc_device.c_str())) {
336 LOG(ERROR) << "Device file " << misc_device << " for /misc "
337 << "is not a symlink.";
338 return false;
339 }
340 *out = base::FilePath(misc_device).DirName().value();
341 return true;
342}
Yifan Hong012508e2019-07-22 18:30:40 -0700343
344bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
345 uint32_t source_slot,
346 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700347 const DeltaArchiveManifest& manifest,
348 bool update) {
349 target_supports_snapshot_ =
350 manifest.dynamic_partition_metadata().snapshot_enabled();
351
352 if (!update)
353 return true;
354
355 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700356 return PrepareSnapshotPartitionsForUpdate(
357 source_slot, target_slot, manifest);
358 }
359 return PrepareDynamicPartitionsForUpdate(source_slot, target_slot, manifest);
360}
361
362bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
363 uint32_t source_slot,
364 uint32_t target_slot,
365 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700366 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
367
368 // Unmap all the target dynamic partitions because they would become
369 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700370 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
371 for (const auto& partition_name : group.partition_names()) {
372 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700373 return false;
374 }
375 }
376 }
377
378 std::string device_dir_str;
379 if (!GetDeviceDir(&device_dir_str)) {
380 return false;
381 }
382 base::FilePath device_dir(device_dir_str);
383 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700384 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700385
386 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
387 if (builder == nullptr) {
388 LOG(ERROR) << "No metadata at "
389 << BootControlInterface::SlotName(source_slot);
390 return false;
391 }
392
Yifan Hong13d41cb2019-09-16 13:18:22 -0700393 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700394 return false;
395 }
396
397 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700398 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700399 return StoreMetadata(target_device, builder.get(), target_slot);
400}
401
Yifan Hong420db9b2019-07-23 20:50:33 -0700402bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
403 uint32_t source_slot,
404 uint32_t target_slot,
405 const DeltaArchiveManifest& manifest) {
406 if (!snapshot_->BeginUpdate()) {
407 LOG(ERROR) << "Cannot begin new update.";
408 return false;
409 }
410 if (!snapshot_->CreateUpdateSnapshots(manifest)) {
411 LOG(ERROR) << "Cannot create update snapshots.";
412 return false;
413 }
414 return true;
415}
416
Yifan Hong700d7c12019-07-23 20:49:16 -0700417std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
418 uint32_t slot) {
419 return fs_mgr_get_super_partition_name(slot);
420}
421
Yifan Hong012508e2019-07-22 18:30:40 -0700422bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
423 MetadataBuilder* builder,
424 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700425 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700426 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
427 // COW group needs to be deleted to ensure there are enough space to create
428 // target partitions.
429 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
430
Yifan Hong012508e2019-07-22 18:30:40 -0700431 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
432 DeleteGroupsWithSuffix(builder, target_suffix);
433
434 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700435 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
436 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700437 }
438
439 std::string expr;
440 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700441 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700442 allocatable_space /= 2;
443 expr = "half of ";
444 }
445 if (total_size > allocatable_space) {
446 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
447 << " (" << total_size << ") has exceeded " << expr
448 << "allocatable space for dynamic partitions "
449 << allocatable_space << ".";
450 return false;
451 }
452
Yifan Hong13d41cb2019-09-16 13:18:22 -0700453 // name of partition(e.g. "system") -> size in bytes
454 std::map<std::string, uint64_t> partition_sizes;
455 for (const auto& partition : manifest.partitions()) {
456 partition_sizes.emplace(partition.partition_name(),
457 partition.new_partition_info().size());
458 }
459
460 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
461 auto group_name_suffix = group.name() + target_suffix;
462 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700463 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700464 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700465 return false;
466 }
467 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700468 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700469
Yifan Hong13d41cb2019-09-16 13:18:22 -0700470 for (const auto& partition_name : group.partition_names()) {
471 auto partition_sizes_it = partition_sizes.find(partition_name);
472 if (partition_sizes_it == partition_sizes.end()) {
473 // TODO(tbao): Support auto-filling partition info for framework-only
474 // OTA.
475 LOG(ERROR) << "dynamic_partition_metadata contains partition "
476 << partition_name << " but it is not part of the manifest. "
477 << "This is not supported.";
478 return false;
479 }
480 uint64_t partition_size = partition_sizes_it->second;
481
482 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700483 Partition* p = builder->AddPartition(
484 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
485 if (!p) {
486 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
487 << " to group " << group_name_suffix;
488 return false;
489 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700490 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700491 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700492 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700493 return false;
494 }
495 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700496 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700497 }
498 }
499
500 return true;
501}
502
Yifan Honga33bca42019-09-03 20:29:45 -0700503bool DynamicPartitionControlAndroid::FinishUpdate() {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700504 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) {
505 LOG(INFO) << "Snapshot writes are done.";
506 return snapshot_->FinishedSnapshotWrites();
507 }
508 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700509}
510
Yifan Hong537802d2018-08-15 13:15:42 -0700511} // namespace chromeos_update_engine