blob: bfdd37520c2b1694d4f68c978959a68eeacdad3f [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) {
178 std::unique_ptr<MetadataBuilder> builder;
179
180 if (target_slot != BootControlInterface::kInvalidSlot &&
181 IsDynamicPartitionsRetrofit()) {
182 builder = MetadataBuilder::NewForUpdate(
183 PartitionOpener(), super_device, source_slot, target_slot);
184 } else {
185 builder =
186 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
187 }
188
Yifan Hong537802d2018-08-15 13:15:42 -0700189 if (builder == nullptr) {
190 LOG(WARNING) << "No metadata slot "
191 << BootControlInterface::SlotName(source_slot) << " in "
192 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700193 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700194 }
195 LOG(INFO) << "Loaded metadata from slot "
196 << BootControlInterface::SlotName(source_slot) << " in "
197 << super_device;
198 return builder;
199}
200
201bool DynamicPartitionControlAndroid::StoreMetadata(
202 const std::string& super_device,
203 MetadataBuilder* builder,
204 uint32_t target_slot) {
205 auto metadata = builder->Export();
206 if (metadata == nullptr) {
207 LOG(ERROR) << "Cannot export metadata to slot "
208 << BootControlInterface::SlotName(target_slot) << " in "
209 << super_device;
210 return false;
211 }
212
Yifan Hong6e706b12018-11-09 16:50:51 -0800213 if (IsDynamicPartitionsRetrofit()) {
214 if (!FlashPartitionTable(super_device, *metadata)) {
215 LOG(ERROR) << "Cannot write metadata to " << super_device;
216 return false;
217 }
218 LOG(INFO) << "Written metadata to " << super_device;
219 } else {
220 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
221 LOG(ERROR) << "Cannot write metadata to slot "
222 << BootControlInterface::SlotName(target_slot) << " in "
223 << super_device;
224 return false;
225 }
226 LOG(INFO) << "Copied metadata to slot "
227 << BootControlInterface::SlotName(target_slot) << " in "
228 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700229 }
230
Yifan Hong537802d2018-08-15 13:15:42 -0700231 return true;
232}
233
234bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
235 // We can't use fs_mgr to look up |partition_name| because fstab
236 // doesn't list every slot partition (it uses the slotselect option
237 // to mask the suffix).
238 //
239 // We can however assume that there's an entry for the /misc mount
240 // point and use that to get the device file for the misc
241 // partition. This helps us locate the disk that |partition_name|
242 // resides on. From there we'll assume that a by-name scheme is used
243 // so we can just replace the trailing "misc" by the given
244 // |partition_name| and suffix corresponding to |slot|, e.g.
245 //
246 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
247 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
248 //
249 // If needed, it's possible to relax the by-name assumption in the
250 // future by trawling /sys/block looking for the appropriate sibling
251 // of misc and then finding an entry in /dev matching the sysfs
252 // entry.
253
254 std::string err, misc_device = get_bootloader_message_blk_device(&err);
255 if (misc_device.empty()) {
256 LOG(ERROR) << "Unable to get misc block device: " << err;
257 return false;
258 }
259
260 if (!utils::IsSymlink(misc_device.c_str())) {
261 LOG(ERROR) << "Device file " << misc_device << " for /misc "
262 << "is not a symlink.";
263 return false;
264 }
265 *out = base::FilePath(misc_device).DirName().value();
266 return true;
267}
268} // namespace chromeos_update_engine