blob: 5a2ccb1dd0f9a2d38904b3a76d3dc26b29222749 [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;
David Andersonbb90dfb2019-08-13 14:14:56 -070042using android::fs_mgr::CreateLogicalPartitionParams;
Yifan Hong537802d2018-08-15 13:15:42 -070043using android::fs_mgr::DestroyLogicalPartition;
44using android::fs_mgr::MetadataBuilder;
Yifan Hong012508e2019-07-22 18:30:40 -070045using android::fs_mgr::Partition;
Yifan Hong6e706b12018-11-09 16:50:51 -080046using android::fs_mgr::PartitionOpener;
Yifan Hong012508e2019-07-22 18:30:40 -070047using android::fs_mgr::SlotSuffixForSlotNumber;
Yifan Hong537802d2018-08-15 13:15:42 -070048
49namespace chromeos_update_engine {
50
Yifan Hong012508e2019-07-22 18:30:40 -070051using PartitionMetadata = BootControlInterface::PartitionMetadata;
52
Yifan Hong6e706b12018-11-09 16:50:51 -080053constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions";
54constexpr char kRetrfoitDynamicPartitions[] =
55 "ro.boot.dynamic_partitions_retrofit";
Yifan Hong537802d2018-08-15 13:15:42 -070056constexpr uint64_t kMapTimeoutMillis = 1000;
57
58DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
59 CleanupInternal(false /* wait */);
60}
61
Yifan Hong186bb682019-07-23 14:04:39 -070062static FeatureFlag GetFeatureFlag(const char* enable_prop,
63 const char* retrofit_prop) {
64 bool retrofit = GetBoolProperty(retrofit_prop, false);
65 bool enabled = GetBoolProperty(enable_prop, false);
66 if (retrofit && !enabled) {
67 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
68 << " is not. These sysprops are inconsistent. Assume that "
69 << enable_prop << " is true from now on.";
70 }
71 if (retrofit) {
72 return FeatureFlag(FeatureFlag::Value::RETROFIT);
73 }
74 if (enabled) {
75 return FeatureFlag(FeatureFlag::Value::LAUNCH);
76 }
77 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070078}
79
Yifan Hong186bb682019-07-23 14:04:39 -070080FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
81 return GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions);
Yifan Hong6e706b12018-11-09 16:50:51 -080082}
83
Yifan Hong8546a712019-03-28 14:42:53 -070084bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -070085 const std::string& super_device,
86 const std::string& target_partition_name,
87 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -070088 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070089 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -070090 CreateLogicalPartitionParams params = {
91 .block_device = super_device,
92 .metadata_slot = slot,
93 .partition_name = target_partition_name,
94 .force_writable = force_writable,
95 .timeout_ms = std::chrono::milliseconds(kMapTimeoutMillis),
96 };
97
98 if (!CreateLogicalPartition(params, path)) {
Yifan Hong537802d2018-08-15 13:15:42 -070099 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
100 << super_device << " on device mapper.";
101 return false;
102 }
103 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700104 << " to device mapper (force_writable = " << force_writable
105 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700106 mapped_devices_.insert(target_partition_name);
107 return true;
108}
109
Yifan Hong8546a712019-03-28 14:42:53 -0700110bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
111 const std::string& super_device,
112 const std::string& target_partition_name,
113 uint32_t slot,
114 bool force_writable,
115 std::string* path) {
116 DmDeviceState state = GetState(target_partition_name);
117 if (state == DmDeviceState::ACTIVE) {
118 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
119 if (GetDmDevicePathByName(target_partition_name, path)) {
120 LOG(INFO) << target_partition_name
121 << " is mapped on device mapper: " << *path;
122 return true;
123 }
124 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
125 return false;
126 }
127 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
128 // the device might be mapped incorrectly before. Attempt to unmap it.
129 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
130 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
131 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700132 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700133 LOG(ERROR) << target_partition_name
134 << " is mapped before the update, and it cannot be unmapped.";
135 return false;
136 }
137 state = GetState(target_partition_name);
138 if (state != DmDeviceState::INVALID) {
139 LOG(ERROR) << target_partition_name << " is unmapped but state is "
140 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
141 return false;
142 }
143 }
144 if (state == DmDeviceState::INVALID) {
145 return MapPartitionInternal(
146 super_device, target_partition_name, slot, force_writable, path);
147 }
148
149 LOG(ERROR) << target_partition_name
150 << " is mapped on device mapper but state is unknown: "
151 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
152 return false;
153}
154
Yifan Hong537802d2018-08-15 13:15:42 -0700155bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700156 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700157 if (DeviceMapper::Instance().GetState(target_partition_name) !=
158 DmDeviceState::INVALID) {
David Anderson4c891c92019-06-21 17:45:23 -0700159 if (!DestroyLogicalPartition(target_partition_name)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700160 LOG(ERROR) << "Cannot unmap " << target_partition_name
161 << " from device mapper.";
162 return false;
163 }
164 LOG(INFO) << "Successfully unmapped " << target_partition_name
165 << " from device mapper.";
166 }
167 mapped_devices_.erase(target_partition_name);
168 return true;
169}
170
171void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
Tao Bao8c4d0082019-08-08 08:56:16 -0700172 if (mapped_devices_.empty()) {
173 return;
174 }
Yifan Hong537802d2018-08-15 13:15:42 -0700175 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
176 // a copy is needed for the loop.
177 std::set<std::string> mapped = mapped_devices_;
178 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
179 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700180 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700181 }
182}
183
184void DynamicPartitionControlAndroid::Cleanup() {
185 CleanupInternal(true /* wait */);
186}
187
188bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
189 return base::PathExists(base::FilePath(path));
190}
191
192android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
193 const std::string& name) {
194 return DeviceMapper::Instance().GetState(name);
195}
196
197bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
198 const std::string& name, std::string* path) {
199 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
200}
201
202std::unique_ptr<MetadataBuilder>
203DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700204 const std::string& super_device, uint32_t source_slot) {
205 return LoadMetadataBuilder(
206 super_device, source_slot, BootControlInterface::kInvalidSlot);
207}
208
209std::unique_ptr<MetadataBuilder>
210DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800211 const std::string& super_device,
212 uint32_t source_slot,
213 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700214 std::unique_ptr<MetadataBuilder> builder;
215 if (target_slot == BootControlInterface::kInvalidSlot) {
216 builder =
217 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
218 } else {
219 builder = MetadataBuilder::NewForUpdate(
220 PartitionOpener(), super_device, source_slot, target_slot);
221 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800222
Yifan Hong537802d2018-08-15 13:15:42 -0700223 if (builder == nullptr) {
224 LOG(WARNING) << "No metadata slot "
225 << BootControlInterface::SlotName(source_slot) << " in "
226 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700227 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700228 }
229 LOG(INFO) << "Loaded metadata from slot "
230 << BootControlInterface::SlotName(source_slot) << " in "
231 << super_device;
232 return builder;
233}
234
235bool DynamicPartitionControlAndroid::StoreMetadata(
236 const std::string& super_device,
237 MetadataBuilder* builder,
238 uint32_t target_slot) {
239 auto metadata = builder->Export();
240 if (metadata == nullptr) {
241 LOG(ERROR) << "Cannot export metadata to slot "
242 << BootControlInterface::SlotName(target_slot) << " in "
243 << super_device;
244 return false;
245 }
246
Yifan Hong186bb682019-07-23 14:04:39 -0700247 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800248 if (!FlashPartitionTable(super_device, *metadata)) {
249 LOG(ERROR) << "Cannot write metadata to " << super_device;
250 return false;
251 }
252 LOG(INFO) << "Written metadata to " << super_device;
253 } else {
254 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
255 LOG(ERROR) << "Cannot write metadata to slot "
256 << BootControlInterface::SlotName(target_slot) << " in "
257 << super_device;
258 return false;
259 }
260 LOG(INFO) << "Copied metadata to slot "
261 << BootControlInterface::SlotName(target_slot) << " in "
262 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700263 }
264
Yifan Hong537802d2018-08-15 13:15:42 -0700265 return true;
266}
267
268bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
269 // We can't use fs_mgr to look up |partition_name| because fstab
270 // doesn't list every slot partition (it uses the slotselect option
271 // to mask the suffix).
272 //
273 // We can however assume that there's an entry for the /misc mount
274 // point and use that to get the device file for the misc
275 // partition. This helps us locate the disk that |partition_name|
276 // resides on. From there we'll assume that a by-name scheme is used
277 // so we can just replace the trailing "misc" by the given
278 // |partition_name| and suffix corresponding to |slot|, e.g.
279 //
280 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
281 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
282 //
283 // If needed, it's possible to relax the by-name assumption in the
284 // future by trawling /sys/block looking for the appropriate sibling
285 // of misc and then finding an entry in /dev matching the sysfs
286 // entry.
287
288 std::string err, misc_device = get_bootloader_message_blk_device(&err);
289 if (misc_device.empty()) {
290 LOG(ERROR) << "Unable to get misc block device: " << err;
291 return false;
292 }
293
294 if (!utils::IsSymlink(misc_device.c_str())) {
295 LOG(ERROR) << "Device file " << misc_device << " for /misc "
296 << "is not a symlink.";
297 return false;
298 }
299 *out = base::FilePath(misc_device).DirName().value();
300 return true;
301}
Yifan Hong012508e2019-07-22 18:30:40 -0700302
303bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
304 uint32_t source_slot,
305 uint32_t target_slot,
306 const PartitionMetadata& partition_metadata) {
307 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
308
309 // Unmap all the target dynamic partitions because they would become
310 // inconsistent with the new metadata.
311 for (const auto& group : partition_metadata.groups) {
312 for (const auto& partition : group.partitions) {
313 if (!UnmapPartitionOnDeviceMapper(partition.name + target_suffix)) {
314 return false;
315 }
316 }
317 }
318
319 std::string device_dir_str;
320 if (!GetDeviceDir(&device_dir_str)) {
321 return false;
322 }
323 base::FilePath device_dir(device_dir_str);
324 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700325 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700326
327 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
328 if (builder == nullptr) {
329 LOG(ERROR) << "No metadata at "
330 << BootControlInterface::SlotName(source_slot);
331 return false;
332 }
333
334 if (!UpdatePartitionMetadata(
335 builder.get(), target_slot, partition_metadata)) {
336 return false;
337 }
338
339 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700340 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700341 return StoreMetadata(target_device, builder.get(), target_slot);
342}
343
Yifan Hong700d7c12019-07-23 20:49:16 -0700344std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
345 uint32_t slot) {
346 return fs_mgr_get_super_partition_name(slot);
347}
348
Yifan Hong012508e2019-07-22 18:30:40 -0700349bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
350 MetadataBuilder* builder,
351 uint32_t target_slot,
352 const PartitionMetadata& partition_metadata) {
353 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
354 DeleteGroupsWithSuffix(builder, target_suffix);
355
356 uint64_t total_size = 0;
357 for (const auto& group : partition_metadata.groups) {
358 total_size += group.size;
359 }
360
361 std::string expr;
362 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700363 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700364 allocatable_space /= 2;
365 expr = "half of ";
366 }
367 if (total_size > allocatable_space) {
368 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
369 << " (" << total_size << ") has exceeded " << expr
370 << "allocatable space for dynamic partitions "
371 << allocatable_space << ".";
372 return false;
373 }
374
375 for (const auto& group : partition_metadata.groups) {
376 auto group_name_suffix = group.name + target_suffix;
377 if (!builder->AddGroup(group_name_suffix, group.size)) {
378 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
379 << group.size;
380 return false;
381 }
382 LOG(INFO) << "Added group " << group_name_suffix << " with size "
383 << group.size;
384
385 for (const auto& partition : group.partitions) {
386 auto partition_name_suffix = partition.name + target_suffix;
387 Partition* p = builder->AddPartition(
388 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
389 if (!p) {
390 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
391 << " to group " << group_name_suffix;
392 return false;
393 }
394 if (!builder->ResizePartition(p, partition.size)) {
395 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
396 << " to size " << partition.size << ". Not enough space?";
397 return false;
398 }
399 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
400 << group_name_suffix << " with size " << partition.size;
401 }
402 }
403
404 return true;
405}
406
Yifan Hong537802d2018-08-15 13:15:42 -0700407} // namespace chromeos_update_engine