blob: b5b22df11b2eea2c0fdfc9b242f78ee8ca10ed74 [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.
David Anderson4c891c92019-06-21 17:45:23 -0700106 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700107 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(
David Anderson4c891c92019-06-21 17:45:23 -0700130 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700131 if (DeviceMapper::Instance().GetState(target_partition_name) !=
132 DmDeviceState::INVALID) {
David Anderson4c891c92019-06-21 17:45:23 -0700133 if (!DestroyLogicalPartition(target_partition_name)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700134 LOG(ERROR) << "Cannot unmap " << target_partition_name
135 << " from device mapper.";
136 return false;
137 }
138 LOG(INFO) << "Successfully unmapped " << target_partition_name
139 << " from device mapper.";
140 }
141 mapped_devices_.erase(target_partition_name);
142 return true;
143}
144
145void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
146 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
147 // a copy is needed for the loop.
148 std::set<std::string> mapped = mapped_devices_;
149 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
150 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700151 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700152 }
153}
154
155void DynamicPartitionControlAndroid::Cleanup() {
156 CleanupInternal(true /* wait */);
157}
158
159bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
160 return base::PathExists(base::FilePath(path));
161}
162
163android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
164 const std::string& name) {
165 return DeviceMapper::Instance().GetState(name);
166}
167
168bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
169 const std::string& name, std::string* path) {
170 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
171}
172
173std::unique_ptr<MetadataBuilder>
174DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800175 const std::string& super_device,
176 uint32_t source_slot,
177 uint32_t target_slot) {
Yifan Hongf9464b42019-07-22 18:18:24 -0700178 auto builder = MetadataBuilder::NewForUpdate(
179 PartitionOpener(), super_device, source_slot, target_slot);
Yifan Hong6e706b12018-11-09 16:50:51 -0800180
Yifan Hong537802d2018-08-15 13:15:42 -0700181 if (builder == nullptr) {
182 LOG(WARNING) << "No metadata slot "
183 << BootControlInterface::SlotName(source_slot) << " in "
184 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700185 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700186 }
187 LOG(INFO) << "Loaded metadata from slot "
188 << BootControlInterface::SlotName(source_slot) << " in "
189 << super_device;
190 return builder;
191}
192
193bool DynamicPartitionControlAndroid::StoreMetadata(
194 const std::string& super_device,
195 MetadataBuilder* builder,
196 uint32_t target_slot) {
197 auto metadata = builder->Export();
198 if (metadata == nullptr) {
199 LOG(ERROR) << "Cannot export metadata to slot "
200 << BootControlInterface::SlotName(target_slot) << " in "
201 << super_device;
202 return false;
203 }
204
Yifan Hong6e706b12018-11-09 16:50:51 -0800205 if (IsDynamicPartitionsRetrofit()) {
206 if (!FlashPartitionTable(super_device, *metadata)) {
207 LOG(ERROR) << "Cannot write metadata to " << super_device;
208 return false;
209 }
210 LOG(INFO) << "Written metadata to " << super_device;
211 } else {
212 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
213 LOG(ERROR) << "Cannot write metadata to slot "
214 << BootControlInterface::SlotName(target_slot) << " in "
215 << super_device;
216 return false;
217 }
218 LOG(INFO) << "Copied metadata to slot "
219 << BootControlInterface::SlotName(target_slot) << " in "
220 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700221 }
222
Yifan Hong537802d2018-08-15 13:15:42 -0700223 return true;
224}
225
226bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
227 // We can't use fs_mgr to look up |partition_name| because fstab
228 // doesn't list every slot partition (it uses the slotselect option
229 // to mask the suffix).
230 //
231 // We can however assume that there's an entry for the /misc mount
232 // point and use that to get the device file for the misc
233 // partition. This helps us locate the disk that |partition_name|
234 // resides on. From there we'll assume that a by-name scheme is used
235 // so we can just replace the trailing "misc" by the given
236 // |partition_name| and suffix corresponding to |slot|, e.g.
237 //
238 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
239 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
240 //
241 // If needed, it's possible to relax the by-name assumption in the
242 // future by trawling /sys/block looking for the appropriate sibling
243 // of misc and then finding an entry in /dev matching the sysfs
244 // entry.
245
246 std::string err, misc_device = get_bootloader_message_blk_device(&err);
247 if (misc_device.empty()) {
248 LOG(ERROR) << "Unable to get misc block device: " << err;
249 return false;
250 }
251
252 if (!utils::IsSymlink(misc_device.c_str())) {
253 LOG(ERROR) << "Device file " << misc_device << " for /misc "
254 << "is not a symlink.";
255 return false;
256 }
257 *out = base::FilePath(misc_device).DirName().value();
258 return true;
259}
260} // namespace chromeos_update_engine