blob: e351dbdfa9138d8ec6561f01890c4e31ede8d414 [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 Hong13d41cb2019-09-16 13:18:22 -070019#include <map>
Yifan Hong537802d2018-08-15 13:15:42 -070020#include <memory>
21#include <set>
22#include <string>
Yifan Hong012508e2019-07-22 18:30:40 -070023#include <vector>
Yifan Hong537802d2018-08-15 13:15:42 -070024
25#include <android-base/properties.h>
26#include <android-base/strings.h>
27#include <base/files/file_util.h>
28#include <base/logging.h>
Yifan Hong012508e2019-07-22 18:30:40 -070029#include <base/strings/string_util.h>
Yifan Hong537802d2018-08-15 13:15:42 -070030#include <bootloader_message/bootloader_message.h>
Yifan Hong012508e2019-07-22 18:30:40 -070031#include <fs_mgr.h>
Yifan Hong537802d2018-08-15 13:15:42 -070032#include <fs_mgr_dm_linear.h>
33
34#include "update_engine/common/boot_control_interface.h"
35#include "update_engine/common/utils.h"
Yifan Hong012508e2019-07-22 18:30:40 -070036#include "update_engine/dynamic_partition_utils.h"
Yifan Hong537802d2018-08-15 13:15:42 -070037
38using android::base::GetBoolProperty;
39using android::base::Join;
40using android::dm::DeviceMapper;
41using android::dm::DmDeviceState;
42using android::fs_mgr::CreateLogicalPartition;
David Andersonbb90dfb2019-08-13 14:14:56 -070043using android::fs_mgr::CreateLogicalPartitionParams;
Yifan Hong537802d2018-08-15 13:15:42 -070044using android::fs_mgr::DestroyLogicalPartition;
45using android::fs_mgr::MetadataBuilder;
Yifan Hong012508e2019-07-22 18:30:40 -070046using android::fs_mgr::Partition;
Yifan Hong6e706b12018-11-09 16:50:51 -080047using android::fs_mgr::PartitionOpener;
Yifan Hong012508e2019-07-22 18:30:40 -070048using android::fs_mgr::SlotSuffixForSlotNumber;
Yifan Hong537802d2018-08-15 13:15:42 -070049
50namespace chromeos_update_engine {
51
Yifan Hong6e706b12018-11-09 16:50:51 -080052constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
53constexpr char kRetrfoitDynamicPartitions[] =
54 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong413d5722019-07-23 14:21:09 -070055constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled";
56constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit";
Yifan Hong537802d2018-08-15 13:15:42 -070057constexpr uint64_t kMapTimeoutMillis = 1000;
58
59DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
60 CleanupInternal(false /* wait */);
61}
62
Yifan Hong186bb682019-07-23 14:04:39 -070063static FeatureFlag GetFeatureFlag(const char* enable_prop,
64 const char* retrofit_prop) {
65 bool retrofit = GetBoolProperty(retrofit_prop, false);
66 bool enabled = GetBoolProperty(enable_prop, false);
67 if (retrofit && !enabled) {
68 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
69 << " is not. These sysprops are inconsistent. Assume that "
70 << enable_prop << " is true from now on.";
71 }
72 if (retrofit) {
73 return FeatureFlag(FeatureFlag::Value::RETROFIT);
74 }
75 if (enabled) {
76 return FeatureFlag(FeatureFlag::Value::LAUNCH);
77 }
78 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070079}
80
Yifan Hong186bb682019-07-23 14:04:39 -070081FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
82 return GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions);
Yifan Hong6e706b12018-11-09 16:50:51 -080083}
84
Yifan Hong413d5722019-07-23 14:21:09 -070085FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
86 return GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit);
87}
88
Yifan Hong8546a712019-03-28 14:42:53 -070089bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -070090 const std::string& super_device,
91 const std::string& target_partition_name,
92 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -070093 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070094 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -070095 CreateLogicalPartitionParams params = {
96 .block_device = super_device,
97 .metadata_slot = slot,
98 .partition_name = target_partition_name,
99 .force_writable = force_writable,
100 .timeout_ms = std::chrono::milliseconds(kMapTimeoutMillis),
101 };
102
103 if (!CreateLogicalPartition(params, path)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700104 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
105 << super_device << " on device mapper.";
106 return false;
107 }
108 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700109 << " to device mapper (force_writable = " << force_writable
110 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700111 mapped_devices_.insert(target_partition_name);
112 return true;
113}
114
Yifan Hong8546a712019-03-28 14:42:53 -0700115bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
116 const std::string& super_device,
117 const std::string& target_partition_name,
118 uint32_t slot,
119 bool force_writable,
120 std::string* path) {
121 DmDeviceState state = GetState(target_partition_name);
122 if (state == DmDeviceState::ACTIVE) {
123 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
124 if (GetDmDevicePathByName(target_partition_name, path)) {
125 LOG(INFO) << target_partition_name
126 << " is mapped on device mapper: " << *path;
127 return true;
128 }
129 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
130 return false;
131 }
132 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
133 // the device might be mapped incorrectly before. Attempt to unmap it.
134 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
135 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
136 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700137 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700138 LOG(ERROR) << target_partition_name
139 << " is mapped before the update, and it cannot be unmapped.";
140 return false;
141 }
142 state = GetState(target_partition_name);
143 if (state != DmDeviceState::INVALID) {
144 LOG(ERROR) << target_partition_name << " is unmapped but state is "
145 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
146 return false;
147 }
148 }
149 if (state == DmDeviceState::INVALID) {
150 return MapPartitionInternal(
151 super_device, target_partition_name, slot, force_writable, path);
152 }
153
154 LOG(ERROR) << target_partition_name
155 << " is mapped on device mapper but state is unknown: "
156 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
157 return false;
158}
159
Yifan Hong537802d2018-08-15 13:15:42 -0700160bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700161 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700162 if (DeviceMapper::Instance().GetState(target_partition_name) !=
163 DmDeviceState::INVALID) {
David Anderson4c891c92019-06-21 17:45:23 -0700164 if (!DestroyLogicalPartition(target_partition_name)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700165 LOG(ERROR) << "Cannot unmap " << target_partition_name
166 << " from device mapper.";
167 return false;
168 }
169 LOG(INFO) << "Successfully unmapped " << target_partition_name
170 << " from device mapper.";
171 }
172 mapped_devices_.erase(target_partition_name);
173 return true;
174}
175
176void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
Tao Bao8c4d0082019-08-08 08:56:16 -0700177 if (mapped_devices_.empty()) {
178 return;
179 }
Yifan Hong537802d2018-08-15 13:15:42 -0700180 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
181 // a copy is needed for the loop.
182 std::set<std::string> mapped = mapped_devices_;
183 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
184 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700185 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700186 }
187}
188
189void DynamicPartitionControlAndroid::Cleanup() {
190 CleanupInternal(true /* wait */);
191}
192
193bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
194 return base::PathExists(base::FilePath(path));
195}
196
197android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
198 const std::string& name) {
199 return DeviceMapper::Instance().GetState(name);
200}
201
202bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
203 const std::string& name, std::string* path) {
204 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
205}
206
207std::unique_ptr<MetadataBuilder>
208DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700209 const std::string& super_device, uint32_t source_slot) {
210 return LoadMetadataBuilder(
211 super_device, source_slot, BootControlInterface::kInvalidSlot);
212}
213
214std::unique_ptr<MetadataBuilder>
215DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800216 const std::string& super_device,
217 uint32_t source_slot,
218 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700219 std::unique_ptr<MetadataBuilder> builder;
220 if (target_slot == BootControlInterface::kInvalidSlot) {
221 builder =
222 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
223 } else {
224 builder = MetadataBuilder::NewForUpdate(
225 PartitionOpener(), super_device, source_slot, target_slot);
226 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800227
Yifan Hong537802d2018-08-15 13:15:42 -0700228 if (builder == nullptr) {
229 LOG(WARNING) << "No metadata slot "
230 << BootControlInterface::SlotName(source_slot) << " in "
231 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700232 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700233 }
234 LOG(INFO) << "Loaded metadata from slot "
235 << BootControlInterface::SlotName(source_slot) << " in "
236 << super_device;
237 return builder;
238}
239
240bool DynamicPartitionControlAndroid::StoreMetadata(
241 const std::string& super_device,
242 MetadataBuilder* builder,
243 uint32_t target_slot) {
244 auto metadata = builder->Export();
245 if (metadata == nullptr) {
246 LOG(ERROR) << "Cannot export metadata to slot "
247 << BootControlInterface::SlotName(target_slot) << " in "
248 << super_device;
249 return false;
250 }
251
Yifan Hong186bb682019-07-23 14:04:39 -0700252 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800253 if (!FlashPartitionTable(super_device, *metadata)) {
254 LOG(ERROR) << "Cannot write metadata to " << super_device;
255 return false;
256 }
257 LOG(INFO) << "Written metadata to " << super_device;
258 } else {
259 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
260 LOG(ERROR) << "Cannot write metadata to slot "
261 << BootControlInterface::SlotName(target_slot) << " in "
262 << super_device;
263 return false;
264 }
265 LOG(INFO) << "Copied metadata to slot "
266 << BootControlInterface::SlotName(target_slot) << " in "
267 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700268 }
269
Yifan Hong537802d2018-08-15 13:15:42 -0700270 return true;
271}
272
273bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
274 // We can't use fs_mgr to look up |partition_name| because fstab
275 // doesn't list every slot partition (it uses the slotselect option
276 // to mask the suffix).
277 //
278 // We can however assume that there's an entry for the /misc mount
279 // point and use that to get the device file for the misc
280 // partition. This helps us locate the disk that |partition_name|
281 // resides on. From there we'll assume that a by-name scheme is used
282 // so we can just replace the trailing "misc" by the given
283 // |partition_name| and suffix corresponding to |slot|, e.g.
284 //
285 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
286 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
287 //
288 // If needed, it's possible to relax the by-name assumption in the
289 // future by trawling /sys/block looking for the appropriate sibling
290 // of misc and then finding an entry in /dev matching the sysfs
291 // entry.
292
293 std::string err, misc_device = get_bootloader_message_blk_device(&err);
294 if (misc_device.empty()) {
295 LOG(ERROR) << "Unable to get misc block device: " << err;
296 return false;
297 }
298
299 if (!utils::IsSymlink(misc_device.c_str())) {
300 LOG(ERROR) << "Device file " << misc_device << " for /misc "
301 << "is not a symlink.";
302 return false;
303 }
304 *out = base::FilePath(misc_device).DirName().value();
305 return true;
306}
Yifan Hong012508e2019-07-22 18:30:40 -0700307
308bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
309 uint32_t source_slot,
310 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700311 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700312 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
313
314 // Unmap all the target dynamic partitions because they would become
315 // inconsistent with the new metadata.
Yifan Hong13d41cb2019-09-16 13:18:22 -0700316 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
317 for (const auto& partition_name : group.partition_names()) {
318 if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700319 return false;
320 }
321 }
322 }
323
324 std::string device_dir_str;
325 if (!GetDeviceDir(&device_dir_str)) {
326 return false;
327 }
328 base::FilePath device_dir(device_dir_str);
329 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700330 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700331
332 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
333 if (builder == nullptr) {
334 LOG(ERROR) << "No metadata at "
335 << BootControlInterface::SlotName(source_slot);
336 return false;
337 }
338
Yifan Hong13d41cb2019-09-16 13:18:22 -0700339 if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700340 return false;
341 }
342
343 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700344 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700345 return StoreMetadata(target_device, builder.get(), target_slot);
346}
347
Yifan Hong700d7c12019-07-23 20:49:16 -0700348std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
349 uint32_t slot) {
350 return fs_mgr_get_super_partition_name(slot);
351}
352
Yifan Hong012508e2019-07-22 18:30:40 -0700353bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
354 MetadataBuilder* builder,
355 uint32_t target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700356 const DeltaArchiveManifest& manifest) {
Yifan Hong012508e2019-07-22 18:30:40 -0700357 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
358 DeleteGroupsWithSuffix(builder, target_suffix);
359
360 uint64_t total_size = 0;
Yifan Hong13d41cb2019-09-16 13:18:22 -0700361 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
362 total_size += group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700363 }
364
365 std::string expr;
366 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700367 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700368 allocatable_space /= 2;
369 expr = "half of ";
370 }
371 if (total_size > allocatable_space) {
372 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
373 << " (" << total_size << ") has exceeded " << expr
374 << "allocatable space for dynamic partitions "
375 << allocatable_space << ".";
376 return false;
377 }
378
Yifan Hong13d41cb2019-09-16 13:18:22 -0700379 // name of partition(e.g. "system") -> size in bytes
380 std::map<std::string, uint64_t> partition_sizes;
381 for (const auto& partition : manifest.partitions()) {
382 partition_sizes.emplace(partition.partition_name(),
383 partition.new_partition_info().size());
384 }
385
386 for (const auto& group : manifest.dynamic_partition_metadata().groups()) {
387 auto group_name_suffix = group.name() + target_suffix;
388 if (!builder->AddGroup(group_name_suffix, group.size())) {
Yifan Hong012508e2019-07-22 18:30:40 -0700389 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700390 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700391 return false;
392 }
393 LOG(INFO) << "Added group " << group_name_suffix << " with size "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700394 << group.size();
Yifan Hong012508e2019-07-22 18:30:40 -0700395
Yifan Hong13d41cb2019-09-16 13:18:22 -0700396 for (const auto& partition_name : group.partition_names()) {
397 auto partition_sizes_it = partition_sizes.find(partition_name);
398 if (partition_sizes_it == partition_sizes.end()) {
399 // TODO(tbao): Support auto-filling partition info for framework-only
400 // OTA.
401 LOG(ERROR) << "dynamic_partition_metadata contains partition "
402 << partition_name << " but it is not part of the manifest. "
403 << "This is not supported.";
404 return false;
405 }
406 uint64_t partition_size = partition_sizes_it->second;
407
408 auto partition_name_suffix = partition_name + target_suffix;
Yifan Hong012508e2019-07-22 18:30:40 -0700409 Partition* p = builder->AddPartition(
410 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
411 if (!p) {
412 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
413 << " to group " << group_name_suffix;
414 return false;
415 }
Yifan Hong13d41cb2019-09-16 13:18:22 -0700416 if (!builder->ResizePartition(p, partition_size)) {
Yifan Hong012508e2019-07-22 18:30:40 -0700417 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
Yifan Hong13d41cb2019-09-16 13:18:22 -0700418 << " to size " << partition_size << ". Not enough space?";
Yifan Hong012508e2019-07-22 18:30:40 -0700419 return false;
420 }
421 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
Yifan Hong13d41cb2019-09-16 13:18:22 -0700422 << group_name_suffix << " with size " << partition_size;
Yifan Hong012508e2019-07-22 18:30:40 -0700423 }
424 }
425
426 return true;
427}
428
Yifan Hong537802d2018-08-15 13:15:42 -0700429} // namespace chromeos_update_engine