blob: bd34ea914fe8a9c0d9bc12051820233986853481 [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;
Yifan Hong6e706b12018-11-09 16:50:51 -080040using android::fs_mgr::PartitionOpener;
Yifan Hong537802d2018-08-15 13:15:42 -070041
42namespace chromeos_update_engine {
43
Yifan Hong6e706b12018-11-09 16:50:51 -080044constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
45constexpr char kRetrfoitDynamicPartitions[] =
46 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong537802d2018-08-15 13:15:42 -070047constexpr uint64_t kMapTimeoutMillis = 1000;
48
49DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
50 CleanupInternal(false /* wait */);
51}
52
53bool DynamicPartitionControlAndroid::IsDynamicPartitionsEnabled() {
54 return GetBoolProperty(kUseDynamicPartitions, false);
55}
56
Yifan Hong6e38b352018-11-19 14:12:37 -080057bool DynamicPartitionControlAndroid::IsDynamicPartitionsRetrofit() {
Yifan Hong6e706b12018-11-09 16:50:51 -080058 return GetBoolProperty(kRetrfoitDynamicPartitions, false);
59}
60
Yifan Hong537802d2018-08-15 13:15:42 -070061bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
62 const std::string& super_device,
63 const std::string& target_partition_name,
64 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -070065 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070066 std::string* path) {
67 if (!CreateLogicalPartition(super_device.c_str(),
68 slot,
69 target_partition_name,
Yifan Hongaf65ef12018-10-29 11:09:06 -070070 force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070071 std::chrono::milliseconds(kMapTimeoutMillis),
72 path)) {
73 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
74 << super_device << " on device mapper.";
75 return false;
76 }
77 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -070078 << " to device mapper (force_writable = " << force_writable
79 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -070080 mapped_devices_.insert(target_partition_name);
81 return true;
82}
83
84bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
85 const std::string& target_partition_name, bool wait) {
86 if (DeviceMapper::Instance().GetState(target_partition_name) !=
87 DmDeviceState::INVALID) {
88 if (!DestroyLogicalPartition(
89 target_partition_name,
90 std::chrono::milliseconds(wait ? kMapTimeoutMillis : 0))) {
91 LOG(ERROR) << "Cannot unmap " << target_partition_name
92 << " from device mapper.";
93 return false;
94 }
95 LOG(INFO) << "Successfully unmapped " << target_partition_name
96 << " from device mapper.";
97 }
98 mapped_devices_.erase(target_partition_name);
99 return true;
100}
101
102void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
103 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
104 // a copy is needed for the loop.
105 std::set<std::string> mapped = mapped_devices_;
106 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
107 for (const auto& partition_name : mapped) {
108 ignore_result(UnmapPartitionOnDeviceMapper(partition_name, wait));
109 }
110}
111
112void DynamicPartitionControlAndroid::Cleanup() {
113 CleanupInternal(true /* wait */);
114}
115
116bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
117 return base::PathExists(base::FilePath(path));
118}
119
120android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
121 const std::string& name) {
122 return DeviceMapper::Instance().GetState(name);
123}
124
125bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
126 const std::string& name, std::string* path) {
127 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
128}
129
130std::unique_ptr<MetadataBuilder>
131DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800132 const std::string& super_device,
133 uint32_t source_slot,
134 uint32_t target_slot) {
135 std::unique_ptr<MetadataBuilder> builder;
136
137 if (target_slot != BootControlInterface::kInvalidSlot &&
138 IsDynamicPartitionsRetrofit()) {
139 builder = MetadataBuilder::NewForUpdate(
140 PartitionOpener(), super_device, source_slot, target_slot);
141 } else {
142 builder =
143 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
144 }
145
Yifan Hong537802d2018-08-15 13:15:42 -0700146 if (builder == nullptr) {
147 LOG(WARNING) << "No metadata slot "
148 << BootControlInterface::SlotName(source_slot) << " in "
149 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700150 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700151 }
152 LOG(INFO) << "Loaded metadata from slot "
153 << BootControlInterface::SlotName(source_slot) << " in "
154 << super_device;
155 return builder;
156}
157
158bool DynamicPartitionControlAndroid::StoreMetadata(
159 const std::string& super_device,
160 MetadataBuilder* builder,
161 uint32_t target_slot) {
162 auto metadata = builder->Export();
163 if (metadata == nullptr) {
164 LOG(ERROR) << "Cannot export metadata to slot "
165 << BootControlInterface::SlotName(target_slot) << " in "
166 << super_device;
167 return false;
168 }
169
Yifan Hong6e706b12018-11-09 16:50:51 -0800170 if (IsDynamicPartitionsRetrofit()) {
171 if (!FlashPartitionTable(super_device, *metadata)) {
172 LOG(ERROR) << "Cannot write metadata to " << super_device;
173 return false;
174 }
175 LOG(INFO) << "Written metadata to " << super_device;
176 } else {
177 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
178 LOG(ERROR) << "Cannot write metadata to slot "
179 << BootControlInterface::SlotName(target_slot) << " in "
180 << super_device;
181 return false;
182 }
183 LOG(INFO) << "Copied metadata to slot "
184 << BootControlInterface::SlotName(target_slot) << " in "
185 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700186 }
187
Yifan Hong537802d2018-08-15 13:15:42 -0700188 return true;
189}
190
191bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
192 // We can't use fs_mgr to look up |partition_name| because fstab
193 // doesn't list every slot partition (it uses the slotselect option
194 // to mask the suffix).
195 //
196 // We can however assume that there's an entry for the /misc mount
197 // point and use that to get the device file for the misc
198 // partition. This helps us locate the disk that |partition_name|
199 // resides on. From there we'll assume that a by-name scheme is used
200 // so we can just replace the trailing "misc" by the given
201 // |partition_name| and suffix corresponding to |slot|, e.g.
202 //
203 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
204 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
205 //
206 // If needed, it's possible to relax the by-name assumption in the
207 // future by trawling /sys/block looking for the appropriate sibling
208 // of misc and then finding an entry in /dev matching the sysfs
209 // entry.
210
211 std::string err, misc_device = get_bootloader_message_blk_device(&err);
212 if (misc_device.empty()) {
213 LOG(ERROR) << "Unable to get misc block device: " << err;
214 return false;
215 }
216
217 if (!utils::IsSymlink(misc_device.c_str())) {
218 LOG(ERROR) << "Device file " << misc_device << " for /misc "
219 << "is not a symlink.";
220 return false;
221 }
222 *out = base::FilePath(misc_device).DirName().value();
223 return true;
224}
225} // namespace chromeos_update_engine