blob: 5a172b0b68fb170c3dbeec3f78aa0abb1cda456b [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>
Yifan Hong012508e2019-07-22 18:30:40 -070022#include <vector>
Yifan Hong537802d2018-08-15 13:15:42 -070023
24#include <android-base/properties.h>
25#include <android-base/strings.h>
26#include <base/files/file_util.h>
27#include <base/logging.h>
Yifan Hong012508e2019-07-22 18:30:40 -070028#include <base/strings/string_util.h>
Yifan Hong537802d2018-08-15 13:15:42 -070029#include <bootloader_message/bootloader_message.h>
Yifan Hong012508e2019-07-22 18:30:40 -070030#include <fs_mgr.h>
Yifan Hong537802d2018-08-15 13:15:42 -070031#include <fs_mgr_dm_linear.h>
32
33#include "update_engine/common/boot_control_interface.h"
34#include "update_engine/common/utils.h"
Yifan Hong012508e2019-07-22 18:30:40 -070035#include "update_engine/dynamic_partition_utils.h"
Yifan Hong537802d2018-08-15 13:15:42 -070036
37using android::base::GetBoolProperty;
38using android::base::Join;
39using android::dm::DeviceMapper;
40using android::dm::DmDeviceState;
41using android::fs_mgr::CreateLogicalPartition;
42using android::fs_mgr::DestroyLogicalPartition;
43using android::fs_mgr::MetadataBuilder;
Yifan Hong012508e2019-07-22 18:30:40 -070044using android::fs_mgr::Partition;
Yifan Hong6e706b12018-11-09 16:50:51 -080045using android::fs_mgr::PartitionOpener;
Yifan Hong012508e2019-07-22 18:30:40 -070046using android::fs_mgr::SlotSuffixForSlotNumber;
Yifan Hong537802d2018-08-15 13:15:42 -070047
48namespace chromeos_update_engine {
49
Yifan Hong012508e2019-07-22 18:30:40 -070050using PartitionMetadata = BootControlInterface::PartitionMetadata;
51
Yifan Hong6e706b12018-11-09 16:50:51 -080052constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
53constexpr char kRetrfoitDynamicPartitions[] =
54 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong537802d2018-08-15 13:15:42 -070055constexpr uint64_t kMapTimeoutMillis = 1000;
56
57DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
58 CleanupInternal(false /* wait */);
59}
60
61bool DynamicPartitionControlAndroid::IsDynamicPartitionsEnabled() {
62 return GetBoolProperty(kUseDynamicPartitions, false);
63}
64
Yifan Hong6e38b352018-11-19 14:12:37 -080065bool DynamicPartitionControlAndroid::IsDynamicPartitionsRetrofit() {
Yifan Hong6e706b12018-11-09 16:50:51 -080066 return GetBoolProperty(kRetrfoitDynamicPartitions, false);
67}
68
Yifan Hong8546a712019-03-28 14:42:53 -070069bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -070070 const std::string& super_device,
71 const std::string& target_partition_name,
72 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -070073 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070074 std::string* path) {
75 if (!CreateLogicalPartition(super_device.c_str(),
76 slot,
77 target_partition_name,
Yifan Hongaf65ef12018-10-29 11:09:06 -070078 force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070079 std::chrono::milliseconds(kMapTimeoutMillis),
80 path)) {
81 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
82 << super_device << " on device mapper.";
83 return false;
84 }
85 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -070086 << " to device mapper (force_writable = " << force_writable
87 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -070088 mapped_devices_.insert(target_partition_name);
89 return true;
90}
91
Yifan Hong8546a712019-03-28 14:42:53 -070092bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
93 const std::string& super_device,
94 const std::string& target_partition_name,
95 uint32_t slot,
96 bool force_writable,
97 std::string* path) {
98 DmDeviceState state = GetState(target_partition_name);
99 if (state == DmDeviceState::ACTIVE) {
100 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
101 if (GetDmDevicePathByName(target_partition_name, path)) {
102 LOG(INFO) << target_partition_name
103 << " is mapped on device mapper: " << *path;
104 return true;
105 }
106 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
107 return false;
108 }
109 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
110 // the device might be mapped incorrectly before. Attempt to unmap it.
111 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
112 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
113 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700114 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700115 LOG(ERROR) << target_partition_name
116 << " is mapped before the update, and it cannot be unmapped.";
117 return false;
118 }
119 state = GetState(target_partition_name);
120 if (state != DmDeviceState::INVALID) {
121 LOG(ERROR) << target_partition_name << " is unmapped but state is "
122 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
123 return false;
124 }
125 }
126 if (state == DmDeviceState::INVALID) {
127 return MapPartitionInternal(
128 super_device, target_partition_name, slot, force_writable, path);
129 }
130
131 LOG(ERROR) << target_partition_name
132 << " is mapped on device mapper but state is unknown: "
133 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
134 return false;
135}
136
Yifan Hong537802d2018-08-15 13:15:42 -0700137bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700138 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700139 if (DeviceMapper::Instance().GetState(target_partition_name) !=
140 DmDeviceState::INVALID) {
David Anderson4c891c92019-06-21 17:45:23 -0700141 if (!DestroyLogicalPartition(target_partition_name)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700142 LOG(ERROR) << "Cannot unmap " << target_partition_name
143 << " from device mapper.";
144 return false;
145 }
146 LOG(INFO) << "Successfully unmapped " << target_partition_name
147 << " from device mapper.";
148 }
149 mapped_devices_.erase(target_partition_name);
150 return true;
151}
152
153void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
154 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
155 // a copy is needed for the loop.
156 std::set<std::string> mapped = mapped_devices_;
157 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
158 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700159 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700160 }
161}
162
163void DynamicPartitionControlAndroid::Cleanup() {
164 CleanupInternal(true /* wait */);
165}
166
167bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
168 return base::PathExists(base::FilePath(path));
169}
170
171android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
172 const std::string& name) {
173 return DeviceMapper::Instance().GetState(name);
174}
175
176bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
177 const std::string& name, std::string* path) {
178 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
179}
180
181std::unique_ptr<MetadataBuilder>
182DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700183 const std::string& super_device, uint32_t source_slot) {
184 return LoadMetadataBuilder(
185 super_device, source_slot, BootControlInterface::kInvalidSlot);
186}
187
188std::unique_ptr<MetadataBuilder>
189DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800190 const std::string& super_device,
191 uint32_t source_slot,
192 uint32_t target_slot) {
Yifan Hongf9464b42019-07-22 18:18:24 -0700193 auto builder = MetadataBuilder::NewForUpdate(
194 PartitionOpener(), super_device, source_slot, target_slot);
Yifan Hong6e706b12018-11-09 16:50:51 -0800195
Yifan Hong537802d2018-08-15 13:15:42 -0700196 if (builder == nullptr) {
197 LOG(WARNING) << "No metadata slot "
198 << BootControlInterface::SlotName(source_slot) << " in "
199 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700200 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700201 }
202 LOG(INFO) << "Loaded metadata from slot "
203 << BootControlInterface::SlotName(source_slot) << " in "
204 << super_device;
205 return builder;
206}
207
208bool DynamicPartitionControlAndroid::StoreMetadata(
209 const std::string& super_device,
210 MetadataBuilder* builder,
211 uint32_t target_slot) {
212 auto metadata = builder->Export();
213 if (metadata == nullptr) {
214 LOG(ERROR) << "Cannot export metadata to slot "
215 << BootControlInterface::SlotName(target_slot) << " in "
216 << super_device;
217 return false;
218 }
219
Yifan Hong6e706b12018-11-09 16:50:51 -0800220 if (IsDynamicPartitionsRetrofit()) {
221 if (!FlashPartitionTable(super_device, *metadata)) {
222 LOG(ERROR) << "Cannot write metadata to " << super_device;
223 return false;
224 }
225 LOG(INFO) << "Written metadata to " << super_device;
226 } else {
227 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
228 LOG(ERROR) << "Cannot write metadata to slot "
229 << BootControlInterface::SlotName(target_slot) << " in "
230 << super_device;
231 return false;
232 }
233 LOG(INFO) << "Copied metadata to slot "
234 << BootControlInterface::SlotName(target_slot) << " in "
235 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700236 }
237
Yifan Hong537802d2018-08-15 13:15:42 -0700238 return true;
239}
240
241bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
242 // We can't use fs_mgr to look up |partition_name| because fstab
243 // doesn't list every slot partition (it uses the slotselect option
244 // to mask the suffix).
245 //
246 // We can however assume that there's an entry for the /misc mount
247 // point and use that to get the device file for the misc
248 // partition. This helps us locate the disk that |partition_name|
249 // resides on. From there we'll assume that a by-name scheme is used
250 // so we can just replace the trailing "misc" by the given
251 // |partition_name| and suffix corresponding to |slot|, e.g.
252 //
253 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
254 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
255 //
256 // If needed, it's possible to relax the by-name assumption in the
257 // future by trawling /sys/block looking for the appropriate sibling
258 // of misc and then finding an entry in /dev matching the sysfs
259 // entry.
260
261 std::string err, misc_device = get_bootloader_message_blk_device(&err);
262 if (misc_device.empty()) {
263 LOG(ERROR) << "Unable to get misc block device: " << err;
264 return false;
265 }
266
267 if (!utils::IsSymlink(misc_device.c_str())) {
268 LOG(ERROR) << "Device file " << misc_device << " for /misc "
269 << "is not a symlink.";
270 return false;
271 }
272 *out = base::FilePath(misc_device).DirName().value();
273 return true;
274}
Yifan Hong012508e2019-07-22 18:30:40 -0700275
276bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
277 uint32_t source_slot,
278 uint32_t target_slot,
279 const PartitionMetadata& partition_metadata) {
280 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
281
282 // Unmap all the target dynamic partitions because they would become
283 // inconsistent with the new metadata.
284 for (const auto& group : partition_metadata.groups) {
285 for (const auto& partition : group.partitions) {
286 if (!UnmapPartitionOnDeviceMapper(partition.name + target_suffix)) {
287 return false;
288 }
289 }
290 }
291
292 std::string device_dir_str;
293 if (!GetDeviceDir(&device_dir_str)) {
294 return false;
295 }
296 base::FilePath device_dir(device_dir_str);
297 auto source_device =
298 device_dir.Append(fs_mgr_get_super_partition_name(source_slot)).value();
299
300 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
301 if (builder == nullptr) {
302 LOG(ERROR) << "No metadata at "
303 << BootControlInterface::SlotName(source_slot);
304 return false;
305 }
306
307 if (!UpdatePartitionMetadata(
308 builder.get(), target_slot, partition_metadata)) {
309 return false;
310 }
311
312 auto target_device =
313 device_dir.Append(fs_mgr_get_super_partition_name(target_slot)).value();
314 return StoreMetadata(target_device, builder.get(), target_slot);
315}
316
317bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
318 MetadataBuilder* builder,
319 uint32_t target_slot,
320 const PartitionMetadata& partition_metadata) {
321 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
322 DeleteGroupsWithSuffix(builder, target_suffix);
323
324 uint64_t total_size = 0;
325 for (const auto& group : partition_metadata.groups) {
326 total_size += group.size;
327 }
328
329 std::string expr;
330 uint64_t allocatable_space = builder->AllocatableSpace();
331 if (!IsDynamicPartitionsRetrofit()) {
332 allocatable_space /= 2;
333 expr = "half of ";
334 }
335 if (total_size > allocatable_space) {
336 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
337 << " (" << total_size << ") has exceeded " << expr
338 << "allocatable space for dynamic partitions "
339 << allocatable_space << ".";
340 return false;
341 }
342
343 for (const auto& group : partition_metadata.groups) {
344 auto group_name_suffix = group.name + target_suffix;
345 if (!builder->AddGroup(group_name_suffix, group.size)) {
346 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
347 << group.size;
348 return false;
349 }
350 LOG(INFO) << "Added group " << group_name_suffix << " with size "
351 << group.size;
352
353 for (const auto& partition : group.partitions) {
354 auto partition_name_suffix = partition.name + target_suffix;
355 Partition* p = builder->AddPartition(
356 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
357 if (!p) {
358 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
359 << " to group " << group_name_suffix;
360 return false;
361 }
362 if (!builder->ResizePartition(p, partition.size)) {
363 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
364 << " to size " << partition.size << ". Not enough space?";
365 return false;
366 }
367 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
368 << group_name_suffix << " with size " << partition.size;
369 }
370 }
371
372 return true;
373}
374
Yifan Hong537802d2018-08-15 13:15:42 -0700375} // namespace chromeos_update_engine