blob: 0c1f0d305390a2b8531d0ff4b8a214d9d6cafcc0 [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
Yifan Hong537802d2018-08-15 13:15:42 -070065DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
66 CleanupInternal(false /* wait */);
67}
68
Yifan Hong186bb682019-07-23 14:04:39 -070069static FeatureFlag GetFeatureFlag(const char* enable_prop,
70 const char* retrofit_prop) {
71 bool retrofit = GetBoolProperty(retrofit_prop, false);
72 bool enabled = GetBoolProperty(enable_prop, false);
73 if (retrofit && !enabled) {
74 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
75 << " is not. These sysprops are inconsistent. Assume that "
76 << enable_prop << " is true from now on.";
77 }
78 if (retrofit) {
79 return FeatureFlag(FeatureFlag::Value::RETROFIT);
80 }
81 if (enabled) {
82 return FeatureFlag(FeatureFlag::Value::LAUNCH);
83 }
84 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070085}
86
Yifan Hongb38e1af2019-10-17 14:59:22 -070087DynamicPartitionControlAndroid::DynamicPartitionControlAndroid()
88 : dynamic_partitions_(
89 GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions)),
90 virtual_ab_(GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit)) {
91 if (GetVirtualAbFeatureFlag().IsEnabled()) {
92 snapshot_ = android::snapshot::SnapshotManager::New();
93 CHECK(snapshot_ != nullptr) << "Cannot initialize SnapshotManager.";
94 }
95}
96
Yifan Hong186bb682019-07-23 14:04:39 -070097FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -070098 return dynamic_partitions_;
Yifan Hong6e706b12018-11-09 16:50:51 -080099}
100
Yifan Hong413d5722019-07-23 14:21:09 -0700101FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
Yifan Hongb38e1af2019-10-17 14:59:22 -0700102 return virtual_ab_;
Yifan Hong413d5722019-07-23 14:21:09 -0700103}
104
Yifan Hong8546a712019-03-28 14:42:53 -0700105bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -0700106 const std::string& super_device,
107 const std::string& target_partition_name,
108 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -0700109 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -0700110 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -0700111 CreateLogicalPartitionParams params = {
112 .block_device = super_device,
113 .metadata_slot = slot,
114 .partition_name = target_partition_name,
115 .force_writable = force_writable,
David Andersonbb90dfb2019-08-13 14:14:56 -0700116 };
Yifan Hong420db9b2019-07-23 20:50:33 -0700117 bool success = false;
Yifan Hongf0f4a912019-09-26 17:51:33 -0700118 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_ &&
119 force_writable) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700120 // Only target partitions are mapped with force_writable. On Virtual
121 // A/B devices, target partitions may overlap with source partitions, so
122 // they must be mapped with snapshot.
123 params.timeout_ms = kMapSnapshotTimeout;
124 success = snapshot_->MapUpdateSnapshot(params, path);
125 } else {
126 params.timeout_ms = kMapTimeout;
127 success = CreateLogicalPartition(params, path);
128 }
David Andersonbb90dfb2019-08-13 14:14:56 -0700129
Yifan Hong420db9b2019-07-23 20:50:33 -0700130 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700131 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
132 << super_device << " on device mapper.";
133 return false;
134 }
135 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700136 << " to device mapper (force_writable = " << force_writable
137 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700138 mapped_devices_.insert(target_partition_name);
139 return true;
140}
141
Yifan Hong8546a712019-03-28 14:42:53 -0700142bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
143 const std::string& super_device,
144 const std::string& target_partition_name,
145 uint32_t slot,
146 bool force_writable,
147 std::string* path) {
148 DmDeviceState state = GetState(target_partition_name);
149 if (state == DmDeviceState::ACTIVE) {
150 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
151 if (GetDmDevicePathByName(target_partition_name, path)) {
152 LOG(INFO) << target_partition_name
153 << " is mapped on device mapper: " << *path;
154 return true;
155 }
156 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
157 return false;
158 }
159 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
160 // the device might be mapped incorrectly before. Attempt to unmap it.
161 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
162 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
163 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700164 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700165 LOG(ERROR) << target_partition_name
166 << " is mapped before the update, and it cannot be unmapped.";
167 return false;
168 }
169 state = GetState(target_partition_name);
170 if (state != DmDeviceState::INVALID) {
171 LOG(ERROR) << target_partition_name << " is unmapped but state is "
172 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
173 return false;
174 }
175 }
176 if (state == DmDeviceState::INVALID) {
177 return MapPartitionInternal(
178 super_device, target_partition_name, slot, force_writable, path);
179 }
180
181 LOG(ERROR) << target_partition_name
182 << " is mapped on device mapper but state is unknown: "
183 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
184 return false;
185}
186
Yifan Hong537802d2018-08-15 13:15:42 -0700187bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700188 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700189 if (DeviceMapper::Instance().GetState(target_partition_name) !=
190 DmDeviceState::INVALID) {
Yifan Hong420db9b2019-07-23 20:50:33 -0700191 // Partitions at target slot on non-Virtual A/B devices are mapped as
192 // dm-linear. Also, on Virtual A/B devices, system_other may be mapped for
193 // preopt apps as dm-linear.
194 // Call DestroyLogicalPartition to handle these cases.
195 bool success = DestroyLogicalPartition(target_partition_name);
196
197 // On a Virtual A/B device, |target_partition_name| may be a leftover from
198 // a paused update. Clean up any underlying devices.
199 if (GetVirtualAbFeatureFlag().IsEnabled()) {
200 success &= snapshot_->UnmapUpdateSnapshot(target_partition_name);
201 }
202
203 if (!success) {
Yifan Hong537802d2018-08-15 13:15:42 -0700204 LOG(ERROR) << "Cannot unmap " << target_partition_name
205 << " from device mapper.";
206 return false;
207 }
208 LOG(INFO) << "Successfully unmapped " << target_partition_name
209 << " from device mapper.";
210 }
211 mapped_devices_.erase(target_partition_name);
212 return true;
213}
214
215void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
Tao Bao8c4d0082019-08-08 08:56:16 -0700216 if (mapped_devices_.empty()) {
217 return;
218 }
Yifan Hong537802d2018-08-15 13:15:42 -0700219 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
220 // a copy is needed for the loop.
221 std::set<std::string> mapped = mapped_devices_;
222 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
223 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700224 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700225 }
226}
227
228void DynamicPartitionControlAndroid::Cleanup() {
229 CleanupInternal(true /* wait */);
230}
231
232bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
233 return base::PathExists(base::FilePath(path));
234}
235
236android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
237 const std::string& name) {
238 return DeviceMapper::Instance().GetState(name);
239}
240
241bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
242 const std::string& name, std::string* path) {
243 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
244}
245
246std::unique_ptr<MetadataBuilder>
247DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700248 const std::string& super_device, uint32_t source_slot) {
249 return LoadMetadataBuilder(
250 super_device, source_slot, BootControlInterface::kInvalidSlot);
251}
252
253std::unique_ptr<MetadataBuilder>
254DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800255 const std::string& super_device,
256 uint32_t source_slot,
257 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700258 std::unique_ptr<MetadataBuilder> builder;
259 if (target_slot == BootControlInterface::kInvalidSlot) {
260 builder =
261 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
262 } else {
Yifan Hong50a56c62019-10-14 19:35:05 -0700263 bool always_keep_source_slot = !target_supports_snapshot_;
264 builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
265 super_device,
266 source_slot,
267 target_slot,
268 always_keep_source_slot);
Yifan Hong30fa5f52019-08-05 16:39:59 -0700269 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800270
Yifan Hong537802d2018-08-15 13:15:42 -0700271 if (builder == nullptr) {
272 LOG(WARNING) << "No metadata slot "
273 << BootControlInterface::SlotName(source_slot) << " in "
274 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700275 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700276 }
277 LOG(INFO) << "Loaded metadata from slot "
278 << BootControlInterface::SlotName(source_slot) << " in "
279 << super_device;
280 return builder;
281}
282
283bool DynamicPartitionControlAndroid::StoreMetadata(
284 const std::string& super_device,
285 MetadataBuilder* builder,
286 uint32_t target_slot) {
287 auto metadata = builder->Export();
288 if (metadata == nullptr) {
289 LOG(ERROR) << "Cannot export metadata to slot "
290 << BootControlInterface::SlotName(target_slot) << " in "
291 << super_device;
292 return false;
293 }
294
Yifan Hong186bb682019-07-23 14:04:39 -0700295 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800296 if (!FlashPartitionTable(super_device, *metadata)) {
297 LOG(ERROR) << "Cannot write metadata to " << super_device;
298 return false;
299 }
300 LOG(INFO) << "Written metadata to " << super_device;
301 } else {
302 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
303 LOG(ERROR) << "Cannot write metadata to slot "
304 << BootControlInterface::SlotName(target_slot) << " in "
305 << super_device;
306 return false;
307 }
308 LOG(INFO) << "Copied metadata to slot "
309 << BootControlInterface::SlotName(target_slot) << " in "
310 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700311 }
312
Yifan Hong537802d2018-08-15 13:15:42 -0700313 return true;
314}
315
316bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
317 // We can't use fs_mgr to look up |partition_name| because fstab
318 // doesn't list every slot partition (it uses the slotselect option
319 // to mask the suffix).
320 //
321 // We can however assume that there's an entry for the /misc mount
322 // point and use that to get the device file for the misc
323 // partition. This helps us locate the disk that |partition_name|
324 // resides on. From there we'll assume that a by-name scheme is used
325 // so we can just replace the trailing "misc" by the given
326 // |partition_name| and suffix corresponding to |slot|, e.g.
327 //
328 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
329 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
330 //
331 // If needed, it's possible to relax the by-name assumption in the
332 // future by trawling /sys/block looking for the appropriate sibling
333 // of misc and then finding an entry in /dev matching the sysfs
334 // entry.
335
336 std::string err, misc_device = get_bootloader_message_blk_device(&err);
337 if (misc_device.empty()) {
338 LOG(ERROR) << "Unable to get misc block device: " << err;
339 return false;
340 }
341
342 if (!utils::IsSymlink(misc_device.c_str())) {
343 LOG(ERROR) << "Device file " << misc_device << " for /misc "
344 << "is not a symlink.";
345 return false;
346 }
347 *out = base::FilePath(misc_device).DirName().value();
348 return true;
349}
Yifan Hong012508e2019-07-22 18:30:40 -0700350
351bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
352 uint32_t source_slot,
353 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700354 const DeltaArchiveManifest& manifest,
355 bool update) {
356 target_supports_snapshot_ =
357 manifest.dynamic_partition_metadata().snapshot_enabled();
358
359 if (!update)
360 return true;
361
Yifan Hong6d888562019-10-01 13:00:31 -0700362 if (GetVirtualAbFeatureFlag().IsEnabled()) {
363 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
364 // called before calling UnmapUpdateSnapshot.
365 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
366 // calls BeginUpdate() which resets update state
367 // - If !target_supports_snapshot_, explicitly CancelUpdate().
368 if (target_supports_snapshot_) {
369 return PrepareSnapshotPartitionsForUpdate(
370 source_slot, target_slot, manifest);
371 }
372 if (!snapshot_->CancelUpdate()) {
373 LOG(ERROR) << "Cannot cancel previous update.";
374 return false;
375 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700376 }
377 return PrepareDynamicPartitionsForUpdate(source_slot, target_slot, manifest);
378}
379
380bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
381 uint32_t source_slot,
382 uint32_t target_slot,
383 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700384 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
385
386 // Unmap all the target dynamic partitions because they would become
387 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700388 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
389 for (const auto& partition_name : group.partition_names()) {
390 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700391 return false;
392 }
393 }
394 }
395
396 std::string device_dir_str;
397 if (!GetDeviceDir(&device_dir_str)) {
398 return false;
399 }
400 base::FilePath device_dir(device_dir_str);
401 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700402 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700403
404 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
405 if (builder == nullptr) {
406 LOG(ERROR) << "No metadata at "
407 << BootControlInterface::SlotName(source_slot);
408 return false;
409 }
410
Yifan Hong13d41cb2019-09-16 13:18:22 -0700411 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700412 return false;
413 }
414
415 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700416 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700417 return StoreMetadata(target_device, builder.get(), target_slot);
418}
419
Yifan Hong420db9b2019-07-23 20:50:33 -0700420bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
421 uint32_t source_slot,
422 uint32_t target_slot,
423 const DeltaArchiveManifest& manifest) {
424 if (!snapshot_->BeginUpdate()) {
425 LOG(ERROR) << "Cannot begin new update.";
426 return false;
427 }
428 if (!snapshot_->CreateUpdateSnapshots(manifest)) {
429 LOG(ERROR) << "Cannot create update snapshots.";
430 return false;
431 }
432 return true;
433}
434
Yifan Hong700d7c12019-07-23 20:49:16 -0700435std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
436 uint32_t slot) {
437 return fs_mgr_get_super_partition_name(slot);
438}
439
Yifan Hong012508e2019-07-22 18:30:40 -0700440bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
441 MetadataBuilder* builder,
442 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700443 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700444 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
445 // COW group needs to be deleted to ensure there are enough space to create
446 // target partitions.
447 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
448
Yifan Hong012508e2019-07-22 18:30:40 -0700449 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
450 DeleteGroupsWithSuffix(builder, target_suffix);
451
452 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700453 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
454 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700455 }
456
457 std::string expr;
458 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700459 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700460 allocatable_space /= 2;
461 expr = "half of ";
462 }
463 if (total_size > allocatable_space) {
464 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
465 << " (" << total_size << ") has exceeded " << expr
466 << "allocatable space for dynamic partitions "
467 << allocatable_space << ".";
468 return false;
469 }
470
Yifan Hong13d41cb2019-09-16 13:18:22 -0700471 // name of partition(e.g. "system") -> size in bytes
472 std::map<std::string, uint64_t> partition_sizes;
473 for (const auto& partition : manifest.partitions()) {
474 partition_sizes.emplace(partition.partition_name(),
475 partition.new_partition_info().size());
476 }
477
478 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
479 auto group_name_suffix = group.name() + target_suffix;
480 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700481 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700482 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700483 return false;
484 }
485 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700486 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700487
Yifan Hong13d41cb2019-09-16 13:18:22 -0700488 for (const auto& partition_name : group.partition_names()) {
489 auto partition_sizes_it = partition_sizes.find(partition_name);
490 if (partition_sizes_it == partition_sizes.end()) {
491 // TODO(tbao): Support auto-filling partition info for framework-only
492 // OTA.
493 LOG(ERROR) << "dynamic_partition_metadata contains partition "
494 << partition_name << " but it is not part of the manifest. "
495 << "This is not supported.";
496 return false;
497 }
498 uint64_t partition_size = partition_sizes_it->second;
499
500 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700501 Partition* p = builder->AddPartition(
502 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
503 if (!p) {
504 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
505 << " to group " << group_name_suffix;
506 return false;
507 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700508 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700509 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700510 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700511 return false;
512 }
513 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700514 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700515 }
516 }
517
518 return true;
519}
520
Yifan Honga33bca42019-09-03 20:29:45 -0700521bool DynamicPartitionControlAndroid::FinishUpdate() {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700522 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) {
523 LOG(INFO) << "Snapshot writes are done.";
524 return snapshot_->FinishedSnapshotWrites();
525 }
526 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700527}
528
Yifan Hong537802d2018-08-15 13:15:42 -0700529} // namespace chromeos_update_engine