blob: 40c266373ba9ed70b7de982891013d964e7374b0 [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 Hong8546a712019-03-28 14:42:53 -070061bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -070062 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
Yifan Hong8546a712019-03-28 14:42:53 -070084bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
85 const std::string& super_device,
86 const std::string& target_partition_name,
87 uint32_t slot,
88 bool force_writable,
89 std::string* path) {
90 DmDeviceState state = GetState(target_partition_name);
91 if (state == DmDeviceState::ACTIVE) {
92 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
93 if (GetDmDevicePathByName(target_partition_name, path)) {
94 LOG(INFO) << target_partition_name
95 << " is mapped on device mapper: " << *path;
96 return true;
97 }
98 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
99 return false;
100 }
101 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
102 // the device might be mapped incorrectly before. Attempt to unmap it.
103 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
104 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
105 // should directly call GetDmDevicePathByName.
106 if (!UnmapPartitionOnDeviceMapper(target_partition_name, true /* wait */)) {
107 LOG(ERROR) << target_partition_name
108 << " is mapped before the update, and it cannot be unmapped.";
109 return false;
110 }
111 state = GetState(target_partition_name);
112 if (state != DmDeviceState::INVALID) {
113 LOG(ERROR) << target_partition_name << " is unmapped but state is "
114 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
115 return false;
116 }
117 }
118 if (state == DmDeviceState::INVALID) {
119 return MapPartitionInternal(
120 super_device, target_partition_name, slot, force_writable, path);
121 }
122
123 LOG(ERROR) << target_partition_name
124 << " is mapped on device mapper but state is unknown: "
125 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
126 return false;
127}
128
Yifan Hong537802d2018-08-15 13:15:42 -0700129bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
130 const std::string& target_partition_name, bool wait) {
131 if (DeviceMapper::Instance().GetState(target_partition_name) !=
132 DmDeviceState::INVALID) {
133 if (!DestroyLogicalPartition(
134 target_partition_name,
135 std::chrono::milliseconds(wait ? kMapTimeoutMillis : 0))) {
136 LOG(ERROR) << "Cannot unmap " << target_partition_name
137 << " from device mapper.";
138 return false;
139 }
140 LOG(INFO) << "Successfully unmapped " << target_partition_name
141 << " from device mapper.";
142 }
143 mapped_devices_.erase(target_partition_name);
144 return true;
145}
146
147void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
148 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
149 // a copy is needed for the loop.
150 std::set<std::string> mapped = mapped_devices_;
151 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
152 for (const auto& partition_name : mapped) {
153 ignore_result(UnmapPartitionOnDeviceMapper(partition_name, wait));
154 }
155}
156
157void DynamicPartitionControlAndroid::Cleanup() {
158 CleanupInternal(true /* wait */);
159}
160
161bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
162 return base::PathExists(base::FilePath(path));
163}
164
165android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
166 const std::string& name) {
167 return DeviceMapper::Instance().GetState(name);
168}
169
170bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
171 const std::string& name, std::string* path) {
172 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
173}
174
175std::unique_ptr<MetadataBuilder>
176DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800177 const std::string& super_device,
178 uint32_t source_slot,
179 uint32_t target_slot) {
180 std::unique_ptr<MetadataBuilder> builder;
181
182 if (target_slot != BootControlInterface::kInvalidSlot &&
183 IsDynamicPartitionsRetrofit()) {
184 builder = MetadataBuilder::NewForUpdate(
185 PartitionOpener(), super_device, source_slot, target_slot);
186 } else {
187 builder =
188 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
189 }
190
Yifan Hong537802d2018-08-15 13:15:42 -0700191 if (builder == nullptr) {
192 LOG(WARNING) << "No metadata slot "
193 << BootControlInterface::SlotName(source_slot) << " in "
194 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700195 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700196 }
197 LOG(INFO) << "Loaded metadata from slot "
198 << BootControlInterface::SlotName(source_slot) << " in "
199 << super_device;
200 return builder;
201}
202
203bool DynamicPartitionControlAndroid::StoreMetadata(
204 const std::string& super_device,
205 MetadataBuilder* builder,
206 uint32_t target_slot) {
207 auto metadata = builder->Export();
208 if (metadata == nullptr) {
209 LOG(ERROR) << "Cannot export metadata to slot "
210 << BootControlInterface::SlotName(target_slot) << " in "
211 << super_device;
212 return false;
213 }
214
Yifan Hong6e706b12018-11-09 16:50:51 -0800215 if (IsDynamicPartitionsRetrofit()) {
216 if (!FlashPartitionTable(super_device, *metadata)) {
217 LOG(ERROR) << "Cannot write metadata to " << super_device;
218 return false;
219 }
220 LOG(INFO) << "Written metadata to " << super_device;
221 } else {
222 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
223 LOG(ERROR) << "Cannot write metadata to slot "
224 << BootControlInterface::SlotName(target_slot) << " in "
225 << super_device;
226 return false;
227 }
228 LOG(INFO) << "Copied metadata to slot "
229 << BootControlInterface::SlotName(target_slot) << " in "
230 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700231 }
232
Yifan Hong537802d2018-08-15 13:15:42 -0700233 return true;
234}
235
236bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
237 // We can't use fs_mgr to look up |partition_name| because fstab
238 // doesn't list every slot partition (it uses the slotselect option
239 // to mask the suffix).
240 //
241 // We can however assume that there's an entry for the /misc mount
242 // point and use that to get the device file for the misc
243 // partition. This helps us locate the disk that |partition_name|
244 // resides on. From there we'll assume that a by-name scheme is used
245 // so we can just replace the trailing "misc" by the given
246 // |partition_name| and suffix corresponding to |slot|, e.g.
247 //
248 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
249 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
250 //
251 // If needed, it's possible to relax the by-name assumption in the
252 // future by trawling /sys/block looking for the appropriate sibling
253 // of misc and then finding an entry in /dev matching the sysfs
254 // entry.
255
256 std::string err, misc_device = get_bootloader_message_blk_device(&err);
257 if (misc_device.empty()) {
258 LOG(ERROR) << "Unable to get misc block device: " << err;
259 return false;
260 }
261
262 if (!utils::IsSymlink(misc_device.c_str())) {
263 LOG(ERROR) << "Device file " << misc_device << " for /misc "
264 << "is not a symlink.";
265 return false;
266 }
267 *out = base::FilePath(misc_device).DirName().value();
268 return true;
269}
270} // namespace chromeos_update_engine