blob: 27e117cf400c5f15a1e247f1774e9c47a95f85ca [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
19#include <memory>
20#include <set>
21#include <string>
22
23#include <android-base/properties.h>
24#include <android-base/strings.h>
25#include <base/files/file_util.h>
26#include <base/logging.h>
27#include <bootloader_message/bootloader_message.h>
28#include <fs_mgr_dm_linear.h>
29
30#include "update_engine/common/boot_control_interface.h"
31#include "update_engine/common/utils.h"
32
33using android::base::GetBoolProperty;
34using android::base::Join;
35using android::dm::DeviceMapper;
36using android::dm::DmDeviceState;
37using android::fs_mgr::CreateLogicalPartition;
38using android::fs_mgr::DestroyLogicalPartition;
39using android::fs_mgr::MetadataBuilder;
40
41namespace chromeos_update_engine {
42
43constexpr char kUseDynamicPartitions[] = "ro.boot.logical_partitions";
44constexpr uint64_t kMapTimeoutMillis = 1000;
45
46DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
47 CleanupInternal(false /* wait */);
48}
49
50bool DynamicPartitionControlAndroid::IsDynamicPartitionsEnabled() {
51 return GetBoolProperty(kUseDynamicPartitions, false);
52}
53
54bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
55 const std::string& super_device,
56 const std::string& target_partition_name,
57 uint32_t slot,
58 std::string* path) {
59 if (!CreateLogicalPartition(super_device.c_str(),
60 slot,
61 target_partition_name,
62 true /* force_writable */,
63 std::chrono::milliseconds(kMapTimeoutMillis),
64 path)) {
65 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
66 << super_device << " on device mapper.";
67 return false;
68 }
69 LOG(INFO) << "Succesfully mapped " << target_partition_name
70 << " to device mapper; device path at " << *path;
71 mapped_devices_.insert(target_partition_name);
72 return true;
73}
74
75bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
76 const std::string& target_partition_name, bool wait) {
77 if (DeviceMapper::Instance().GetState(target_partition_name) !=
78 DmDeviceState::INVALID) {
79 if (!DestroyLogicalPartition(
80 target_partition_name,
81 std::chrono::milliseconds(wait ? kMapTimeoutMillis : 0))) {
82 LOG(ERROR) << "Cannot unmap " << target_partition_name
83 << " from device mapper.";
84 return false;
85 }
86 LOG(INFO) << "Successfully unmapped " << target_partition_name
87 << " from device mapper.";
88 }
89 mapped_devices_.erase(target_partition_name);
90 return true;
91}
92
93void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
94 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
95 // a copy is needed for the loop.
96 std::set<std::string> mapped = mapped_devices_;
97 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
98 for (const auto& partition_name : mapped) {
99 ignore_result(UnmapPartitionOnDeviceMapper(partition_name, wait));
100 }
101}
102
103void DynamicPartitionControlAndroid::Cleanup() {
104 CleanupInternal(true /* wait */);
105}
106
107bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
108 return base::PathExists(base::FilePath(path));
109}
110
111android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
112 const std::string& name) {
113 return DeviceMapper::Instance().GetState(name);
114}
115
116bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
117 const std::string& name, std::string* path) {
118 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
119}
120
121std::unique_ptr<MetadataBuilder>
122DynamicPartitionControlAndroid::LoadMetadataBuilder(
123 const std::string& super_device, uint32_t source_slot) {
124 auto builder = MetadataBuilder::New(super_device, source_slot);
125 if (builder == nullptr) {
126 LOG(WARNING) << "No metadata slot "
127 << BootControlInterface::SlotName(source_slot) << " in "
128 << super_device;
129 }
130 LOG(INFO) << "Loaded metadata from slot "
131 << BootControlInterface::SlotName(source_slot) << " in "
132 << super_device;
133 return builder;
134}
135
136bool DynamicPartitionControlAndroid::StoreMetadata(
137 const std::string& super_device,
138 MetadataBuilder* builder,
139 uint32_t target_slot) {
140 auto metadata = builder->Export();
141 if (metadata == nullptr) {
142 LOG(ERROR) << "Cannot export metadata to slot "
143 << BootControlInterface::SlotName(target_slot) << " in "
144 << super_device;
145 return false;
146 }
147
148 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
149 LOG(ERROR) << "Cannot write metadata to slot "
150 << BootControlInterface::SlotName(target_slot) << " in "
151 << super_device;
152 return false;
153 }
154
155 LOG(INFO) << "Copied metadata to slot "
156 << BootControlInterface::SlotName(target_slot) << " in "
157 << super_device;
158 return true;
159}
160
161bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
162 // We can't use fs_mgr to look up |partition_name| because fstab
163 // doesn't list every slot partition (it uses the slotselect option
164 // to mask the suffix).
165 //
166 // We can however assume that there's an entry for the /misc mount
167 // point and use that to get the device file for the misc
168 // partition. This helps us locate the disk that |partition_name|
169 // resides on. From there we'll assume that a by-name scheme is used
170 // so we can just replace the trailing "misc" by the given
171 // |partition_name| and suffix corresponding to |slot|, e.g.
172 //
173 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
174 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
175 //
176 // If needed, it's possible to relax the by-name assumption in the
177 // future by trawling /sys/block looking for the appropriate sibling
178 // of misc and then finding an entry in /dev matching the sysfs
179 // entry.
180
181 std::string err, misc_device = get_bootloader_message_blk_device(&err);
182 if (misc_device.empty()) {
183 LOG(ERROR) << "Unable to get misc block device: " << err;
184 return false;
185 }
186
187 if (!utils::IsSymlink(misc_device.c_str())) {
188 LOG(ERROR) << "Device file " << misc_device << " for /misc "
189 << "is not a symlink.";
190 return false;
191 }
192 *out = base::FilePath(misc_device).DirName().value();
193 return true;
194}
195} // namespace chromeos_update_engine