blob: 38c67594e873db980755b7aaa39b5e5a31f613bc [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,
Yifan Hongaf65ef12018-10-29 11:09:06 -070058 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070059 std::string* path) {
60 if (!CreateLogicalPartition(super_device.c_str(),
61 slot,
62 target_partition_name,
Yifan Hongaf65ef12018-10-29 11:09:06 -070063 force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070064 std::chrono::milliseconds(kMapTimeoutMillis),
65 path)) {
66 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
67 << super_device << " on device mapper.";
68 return false;
69 }
70 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -070071 << " to device mapper (force_writable = " << force_writable
72 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -070073 mapped_devices_.insert(target_partition_name);
74 return true;
75}
76
77bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
78 const std::string& target_partition_name, bool wait) {
79 if (DeviceMapper::Instance().GetState(target_partition_name) !=
80 DmDeviceState::INVALID) {
81 if (!DestroyLogicalPartition(
82 target_partition_name,
83 std::chrono::milliseconds(wait ? kMapTimeoutMillis : 0))) {
84 LOG(ERROR) << "Cannot unmap " << target_partition_name
85 << " from device mapper.";
86 return false;
87 }
88 LOG(INFO) << "Successfully unmapped " << target_partition_name
89 << " from device mapper.";
90 }
91 mapped_devices_.erase(target_partition_name);
92 return true;
93}
94
95void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
96 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
97 // a copy is needed for the loop.
98 std::set<std::string> mapped = mapped_devices_;
99 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
100 for (const auto& partition_name : mapped) {
101 ignore_result(UnmapPartitionOnDeviceMapper(partition_name, wait));
102 }
103}
104
105void DynamicPartitionControlAndroid::Cleanup() {
106 CleanupInternal(true /* wait */);
107}
108
109bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
110 return base::PathExists(base::FilePath(path));
111}
112
113android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
114 const std::string& name) {
115 return DeviceMapper::Instance().GetState(name);
116}
117
118bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
119 const std::string& name, std::string* path) {
120 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
121}
122
123std::unique_ptr<MetadataBuilder>
124DynamicPartitionControlAndroid::LoadMetadataBuilder(
125 const std::string& super_device, uint32_t source_slot) {
126 auto builder = MetadataBuilder::New(super_device, source_slot);
127 if (builder == nullptr) {
128 LOG(WARNING) << "No metadata slot "
129 << BootControlInterface::SlotName(source_slot) << " in "
130 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700131 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700132 }
133 LOG(INFO) << "Loaded metadata from slot "
134 << BootControlInterface::SlotName(source_slot) << " in "
135 << super_device;
136 return builder;
137}
138
139bool DynamicPartitionControlAndroid::StoreMetadata(
140 const std::string& super_device,
141 MetadataBuilder* builder,
142 uint32_t target_slot) {
143 auto metadata = builder->Export();
144 if (metadata == nullptr) {
145 LOG(ERROR) << "Cannot export metadata to slot "
146 << BootControlInterface::SlotName(target_slot) << " in "
147 << super_device;
148 return false;
149 }
150
151 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
152 LOG(ERROR) << "Cannot write metadata to slot "
153 << BootControlInterface::SlotName(target_slot) << " in "
154 << super_device;
155 return false;
156 }
157
158 LOG(INFO) << "Copied metadata to slot "
159 << BootControlInterface::SlotName(target_slot) << " in "
160 << super_device;
161 return true;
162}
163
164bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
165 // We can't use fs_mgr to look up |partition_name| because fstab
166 // doesn't list every slot partition (it uses the slotselect option
167 // to mask the suffix).
168 //
169 // We can however assume that there's an entry for the /misc mount
170 // point and use that to get the device file for the misc
171 // partition. This helps us locate the disk that |partition_name|
172 // resides on. From there we'll assume that a by-name scheme is used
173 // so we can just replace the trailing "misc" by the given
174 // |partition_name| and suffix corresponding to |slot|, e.g.
175 //
176 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
177 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
178 //
179 // If needed, it's possible to relax the by-name assumption in the
180 // future by trawling /sys/block looking for the appropriate sibling
181 // of misc and then finding an entry in /dev matching the sysfs
182 // entry.
183
184 std::string err, misc_device = get_bootloader_message_blk_device(&err);
185 if (misc_device.empty()) {
186 LOG(ERROR) << "Unable to get misc block device: " << err;
187 return false;
188 }
189
190 if (!utils::IsSymlink(misc_device.c_str())) {
191 LOG(ERROR) << "Device file " << misc_device << " for /misc "
192 << "is not a symlink.";
193 return false;
194 }
195 *out = base::FilePath(misc_device).DirName().value();
196 return true;
197}
198} // namespace chromeos_update_engine