blob: c641a6b3c98f8eda91020129c7a692d3a187b8d1 [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() {
Yifan Hong02513dc2019-10-30 11:23:04 -070066 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -070067}
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
Yifan Hong02513dc2019-10-30 11:23:04 -0700215void DynamicPartitionControlAndroid::CleanupInternal() {
Yifan Hong2c62c132019-10-24 14:53:40 -0700216 metadata_device_.reset();
Tao Bao8c4d0082019-08-08 08:56:16 -0700217 if (mapped_devices_.empty()) {
218 return;
219 }
Yifan Hong537802d2018-08-15 13:15:42 -0700220 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
221 // a copy is needed for the loop.
222 std::set<std::string> mapped = mapped_devices_;
223 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
224 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700225 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700226 }
227}
228
229void DynamicPartitionControlAndroid::Cleanup() {
Yifan Hong02513dc2019-10-30 11:23:04 -0700230 CleanupInternal();
Yifan Hong537802d2018-08-15 13:15:42 -0700231}
232
233bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
234 return base::PathExists(base::FilePath(path));
235}
236
237android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
238 const std::string& name) {
239 return DeviceMapper::Instance().GetState(name);
240}
241
242bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
243 const std::string& name, std::string* path) {
244 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
245}
246
247std::unique_ptr<MetadataBuilder>
248DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700249 const std::string& super_device, uint32_t source_slot) {
250 return LoadMetadataBuilder(
251 super_device, source_slot, BootControlInterface::kInvalidSlot);
252}
253
254std::unique_ptr<MetadataBuilder>
255DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800256 const std::string& super_device,
257 uint32_t source_slot,
258 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700259 std::unique_ptr<MetadataBuilder> builder;
260 if (target_slot == BootControlInterface::kInvalidSlot) {
261 builder =
262 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
263 } else {
Yifan Hong50a56c62019-10-14 19:35:05 -0700264 bool always_keep_source_slot = !target_supports_snapshot_;
265 builder = MetadataBuilder::NewForUpdate(PartitionOpener(),
266 super_device,
267 source_slot,
268 target_slot,
269 always_keep_source_slot);
Yifan Hong30fa5f52019-08-05 16:39:59 -0700270 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800271
Yifan Hong537802d2018-08-15 13:15:42 -0700272 if (builder == nullptr) {
273 LOG(WARNING) << "No metadata slot "
274 << BootControlInterface::SlotName(source_slot) << " in "
275 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700276 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700277 }
278 LOG(INFO) << "Loaded metadata from slot "
279 << BootControlInterface::SlotName(source_slot) << " in "
280 << super_device;
281 return builder;
282}
283
284bool DynamicPartitionControlAndroid::StoreMetadata(
285 const std::string& super_device,
286 MetadataBuilder* builder,
287 uint32_t target_slot) {
288 auto metadata = builder->Export();
289 if (metadata == nullptr) {
290 LOG(ERROR) << "Cannot export metadata to slot "
291 << BootControlInterface::SlotName(target_slot) << " in "
292 << super_device;
293 return false;
294 }
295
Yifan Hong186bb682019-07-23 14:04:39 -0700296 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800297 if (!FlashPartitionTable(super_device, *metadata)) {
298 LOG(ERROR) << "Cannot write metadata to " << super_device;
299 return false;
300 }
301 LOG(INFO) << "Written metadata to " << super_device;
302 } else {
303 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
304 LOG(ERROR) << "Cannot write metadata to slot "
305 << BootControlInterface::SlotName(target_slot) << " in "
306 << super_device;
307 return false;
308 }
309 LOG(INFO) << "Copied metadata to slot "
310 << BootControlInterface::SlotName(target_slot) << " in "
311 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700312 }
313
Yifan Hong537802d2018-08-15 13:15:42 -0700314 return true;
315}
316
317bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
318 // We can't use fs_mgr to look up |partition_name| because fstab
319 // doesn't list every slot partition (it uses the slotselect option
320 // to mask the suffix).
321 //
322 // We can however assume that there's an entry for the /misc mount
323 // point and use that to get the device file for the misc
324 // partition. This helps us locate the disk that |partition_name|
325 // resides on. From there we'll assume that a by-name scheme is used
326 // so we can just replace the trailing "misc" by the given
327 // |partition_name| and suffix corresponding to |slot|, e.g.
328 //
329 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
330 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
331 //
332 // If needed, it's possible to relax the by-name assumption in the
333 // future by trawling /sys/block looking for the appropriate sibling
334 // of misc and then finding an entry in /dev matching the sysfs
335 // entry.
336
337 std::string err, misc_device = get_bootloader_message_blk_device(&err);
338 if (misc_device.empty()) {
339 LOG(ERROR) << "Unable to get misc block device: " << err;
340 return false;
341 }
342
343 if (!utils::IsSymlink(misc_device.c_str())) {
344 LOG(ERROR) << "Device file " << misc_device << " for /misc "
345 << "is not a symlink.";
346 return false;
347 }
348 *out = base::FilePath(misc_device).DirName().value();
349 return true;
350}
Yifan Hong012508e2019-07-22 18:30:40 -0700351
352bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
353 uint32_t source_slot,
354 uint32_t target_slot,
Yifan Hongf0f4a912019-09-26 17:51:33 -0700355 const DeltaArchiveManifest& manifest,
356 bool update) {
357 target_supports_snapshot_ =
358 manifest.dynamic_partition_metadata().snapshot_enabled();
359
Yifan Hong2c62c132019-10-24 14:53:40 -0700360 if (GetVirtualAbFeatureFlag().IsEnabled()) {
361 metadata_device_ = snapshot_->EnsureMetadataMounted();
362 TEST_AND_RETURN_FALSE(metadata_device_ != nullptr);
363 }
364
Yifan Hongf0f4a912019-09-26 17:51:33 -0700365 if (!update)
366 return true;
367
Yifan Hong6d888562019-10-01 13:00:31 -0700368 if (GetVirtualAbFeatureFlag().IsEnabled()) {
369 // On Virtual A/B device, either CancelUpdate() or BeginUpdate() must be
370 // called before calling UnmapUpdateSnapshot.
371 // - If target_supports_snapshot_, PrepareSnapshotPartitionsForUpdate()
372 // calls BeginUpdate() which resets update state
373 // - If !target_supports_snapshot_, explicitly CancelUpdate().
374 if (target_supports_snapshot_) {
375 return PrepareSnapshotPartitionsForUpdate(
376 source_slot, target_slot, manifest);
377 }
378 if (!snapshot_->CancelUpdate()) {
379 LOG(ERROR) << "Cannot cancel previous update.";
380 return false;
381 }
Yifan Hong420db9b2019-07-23 20:50:33 -0700382 }
383 return PrepareDynamicPartitionsForUpdate(source_slot, target_slot, manifest);
384}
385
386bool DynamicPartitionControlAndroid::PrepareDynamicPartitionsForUpdate(
387 uint32_t source_slot,
388 uint32_t target_slot,
389 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700390 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
391
392 // Unmap all the target dynamic partitions because they would become
393 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700394 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
395 for (const auto& partition_name : group.partition_names()) {
396 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700397 return false;
398 }
399 }
400 }
401
402 std::string device_dir_str;
403 if (!GetDeviceDir(&device_dir_str)) {
404 return false;
405 }
406 base::FilePath device_dir(device_dir_str);
407 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700408 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700409
410 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
411 if (builder == nullptr) {
412 LOG(ERROR) << "No metadata at "
413 << BootControlInterface::SlotName(source_slot);
414 return false;
415 }
416
Yifan Hong13d41cb2019-09-16 13:18:22 -0700417 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700418 return false;
419 }
420
421 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700422 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700423 return StoreMetadata(target_device, builder.get(), target_slot);
424}
425
Yifan Hong420db9b2019-07-23 20:50:33 -0700426bool DynamicPartitionControlAndroid::PrepareSnapshotPartitionsForUpdate(
427 uint32_t source_slot,
428 uint32_t target_slot,
429 const DeltaArchiveManifest& manifest) {
430 if (!snapshot_->BeginUpdate()) {
431 LOG(ERROR) << "Cannot begin new update.";
432 return false;
433 }
434 if (!snapshot_->CreateUpdateSnapshots(manifest)) {
435 LOG(ERROR) << "Cannot create update snapshots.";
436 return false;
437 }
438 return true;
439}
440
Yifan Hong700d7c12019-07-23 20:49:16 -0700441std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
442 uint32_t slot) {
443 return fs_mgr_get_super_partition_name(slot);
444}
445
Yifan Hong012508e2019-07-22 18:30:40 -0700446bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
447 MetadataBuilder* builder,
448 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700449 const DeltaArchiveManifest& manifest) {
Yifan Honga4e7da32019-09-30 18:25:03 -0700450 // If applying downgrade from Virtual A/B to non-Virtual A/B, the left-over
451 // COW group needs to be deleted to ensure there are enough space to create
452 // target partitions.
453 builder->RemoveGroupAndPartitions(android::snapshot::kCowGroupName);
454
Yifan Hong012508e2019-07-22 18:30:40 -0700455 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
456 DeleteGroupsWithSuffix(builder, target_suffix);
457
458 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700459 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
460 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700461 }
462
463 std::string expr;
464 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700465 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700466 allocatable_space /= 2;
467 expr = "half of ";
468 }
469 if (total_size > allocatable_space) {
470 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
471 << " (" << total_size << ") has exceeded " << expr
472 << "allocatable space for dynamic partitions "
473 << allocatable_space << ".";
474 return false;
475 }
476
Yifan Hong13d41cb2019-09-16 13:18:22 -0700477 // name of partition(e.g. "system") -> size in bytes
478 std::map<std::string, uint64_t> partition_sizes;
479 for (const auto& partition : manifest.partitions()) {
480 partition_sizes.emplace(partition.partition_name(),
481 partition.new_partition_info().size());
482 }
483
484 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
485 auto group_name_suffix = group.name() + target_suffix;
486 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700487 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700488 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700489 return false;
490 }
491 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700492 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700493
Yifan Hong13d41cb2019-09-16 13:18:22 -0700494 for (const auto& partition_name : group.partition_names()) {
495 auto partition_sizes_it = partition_sizes.find(partition_name);
496 if (partition_sizes_it == partition_sizes.end()) {
497 // TODO(tbao): Support auto-filling partition info for framework-only
498 // OTA.
499 LOG(ERROR) << "dynamic_partition_metadata contains partition "
500 << partition_name << " but it is not part of the manifest. "
501 << "This is not supported.";
502 return false;
503 }
504 uint64_t partition_size = partition_sizes_it->second;
505
506 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700507 Partition* p = builder->AddPartition(
508 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
509 if (!p) {
510 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
511 << " to group " << group_name_suffix;
512 return false;
513 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700514 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700515 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700516 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700517 return false;
518 }
519 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700520 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700521 }
522 }
523
524 return true;
525}
526
Yifan Honga33bca42019-09-03 20:29:45 -0700527bool DynamicPartitionControlAndroid::FinishUpdate() {
Yifan Hongf0f4a912019-09-26 17:51:33 -0700528 if (GetVirtualAbFeatureFlag().IsEnabled() && target_supports_snapshot_) {
529 LOG(INFO) << "Snapshot writes are done.";
530 return snapshot_->FinishedSnapshotWrites();
531 }
532 return true;
Yifan Honga33bca42019-09-03 20:29:45 -0700533}
534
Yifan Hong537802d2018-08-15 13:15:42 -0700535} // namespace chromeos_update_engine