blob: 5e2c2a450e022c796533b76de1e5fd8ef2edc381 [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;
Yifan Hongf48a0052018-10-29 16:30:43 -0700129 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700130 }
131 LOG(INFO) << "Loaded metadata from slot "
132 << BootControlInterface::SlotName(source_slot) << " in "
133 << super_device;
134 return builder;
135}
136
137bool DynamicPartitionControlAndroid::StoreMetadata(
138 const std::string& super_device,
139 MetadataBuilder* builder,
140 uint32_t target_slot) {
141 auto metadata = builder->Export();
142 if (metadata == nullptr) {
143 LOG(ERROR) << "Cannot export metadata to slot "
144 << BootControlInterface::SlotName(target_slot) << " in "
145 << super_device;
146 return false;
147 }
148
149 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
150 LOG(ERROR) << "Cannot write metadata to slot "
151 << BootControlInterface::SlotName(target_slot) << " in "
152 << super_device;
153 return false;
154 }
155
156 LOG(INFO) << "Copied metadata to slot "
157 << BootControlInterface::SlotName(target_slot) << " in "
158 << super_device;
159 return true;
160}
161
162bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
163 // We can't use fs_mgr to look up |partition_name| because fstab
164 // doesn't list every slot partition (it uses the slotselect option
165 // to mask the suffix).
166 //
167 // We can however assume that there's an entry for the /misc mount
168 // point and use that to get the device file for the misc
169 // partition. This helps us locate the disk that |partition_name|
170 // resides on. From there we'll assume that a by-name scheme is used
171 // so we can just replace the trailing "misc" by the given
172 // |partition_name| and suffix corresponding to |slot|, e.g.
173 //
174 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
175 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
176 //
177 // If needed, it's possible to relax the by-name assumption in the
178 // future by trawling /sys/block looking for the appropriate sibling
179 // of misc and then finding an entry in /dev matching the sysfs
180 // entry.
181
182 std::string err, misc_device = get_bootloader_message_blk_device(&err);
183 if (misc_device.empty()) {
184 LOG(ERROR) << "Unable to get misc block device: " << err;
185 return false;
186 }
187
188 if (!utils::IsSymlink(misc_device.c_str())) {
189 LOG(ERROR) << "Device file " << misc_device << " for /misc "
190 << "is not a symlink.";
191 return false;
192 }
193 *out = base::FilePath(misc_device).DirName().value();
194 return true;
195}
196} // namespace chromeos_update_engine