blob: d9d3be568539145b09244e57881303f5ce289daf [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
Yifan Hong186bb682019-07-23 14:04:39 -070061static FeatureFlag GetFeatureFlag(const char* enable_prop,
62 const char* retrofit_prop) {
63 bool retrofit = GetBoolProperty(retrofit_prop, false);
64 bool enabled = GetBoolProperty(enable_prop, false);
65 if (retrofit && !enabled) {
66 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
67 << " is not. These sysprops are inconsistent. Assume that "
68 << enable_prop << " is true from now on.";
69 }
70 if (retrofit) {
71 return FeatureFlag(FeatureFlag::Value::RETROFIT);
72 }
73 if (enabled) {
74 return FeatureFlag(FeatureFlag::Value::LAUNCH);
75 }
76 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070077}
78
Yifan Hong186bb682019-07-23 14:04:39 -070079FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
80 return GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions);
Yifan Hong6e706b12018-11-09 16:50:51 -080081}
82
Yifan Hong8546a712019-03-28 14:42:53 -070083bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -070084 const std::string& super_device,
85 const std::string& target_partition_name,
86 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -070087 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070088 std::string* path) {
89 if (!CreateLogicalPartition(super_device.c_str(),
90 slot,
91 target_partition_name,
Yifan Hongaf65ef12018-10-29 11:09:06 -070092 force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070093 std::chrono::milliseconds(kMapTimeoutMillis),
94 path)) {
95 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
96 << super_device << " on device mapper.";
97 return false;
98 }
99 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700100 << " to device mapper (force_writable = " << force_writable
101 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700102 mapped_devices_.insert(target_partition_name);
103 return true;
104}
105
Yifan Hong8546a712019-03-28 14:42:53 -0700106bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
107 const std::string& super_device,
108 const std::string& target_partition_name,
109 uint32_t slot,
110 bool force_writable,
111 std::string* path) {
112 DmDeviceState state = GetState(target_partition_name);
113 if (state == DmDeviceState::ACTIVE) {
114 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
115 if (GetDmDevicePathByName(target_partition_name, path)) {
116 LOG(INFO) << target_partition_name
117 << " is mapped on device mapper: " << *path;
118 return true;
119 }
120 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
121 return false;
122 }
123 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
124 // the device might be mapped incorrectly before. Attempt to unmap it.
125 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
126 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
127 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700128 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700129 LOG(ERROR) << target_partition_name
130 << " is mapped before the update, and it cannot be unmapped.";
131 return false;
132 }
133 state = GetState(target_partition_name);
134 if (state != DmDeviceState::INVALID) {
135 LOG(ERROR) << target_partition_name << " is unmapped but state is "
136 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
137 return false;
138 }
139 }
140 if (state == DmDeviceState::INVALID) {
141 return MapPartitionInternal(
142 super_device, target_partition_name, slot, force_writable, path);
143 }
144
145 LOG(ERROR) << target_partition_name
146 << " is mapped on device mapper but state is unknown: "
147 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
148 return false;
149}
150
Yifan Hong537802d2018-08-15 13:15:42 -0700151bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700152 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700153 if (DeviceMapper::Instance().GetState(target_partition_name) !=
154 DmDeviceState::INVALID) {
David Anderson4c891c92019-06-21 17:45:23 -0700155 if (!DestroyLogicalPartition(target_partition_name)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700156 LOG(ERROR) << "Cannot unmap " << target_partition_name
157 << " from device mapper.";
158 return false;
159 }
160 LOG(INFO) << "Successfully unmapped " << target_partition_name
161 << " from device mapper.";
162 }
163 mapped_devices_.erase(target_partition_name);
164 return true;
165}
166
167void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
168 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
169 // a copy is needed for the loop.
170 std::set<std::string> mapped = mapped_devices_;
171 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
172 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700173 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700174 }
175}
176
177void DynamicPartitionControlAndroid::Cleanup() {
178 CleanupInternal(true /* wait */);
179}
180
181bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
182 return base::PathExists(base::FilePath(path));
183}
184
185android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
186 const std::string& name) {
187 return DeviceMapper::Instance().GetState(name);
188}
189
190bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
191 const std::string& name, std::string* path) {
192 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
193}
194
195std::unique_ptr<MetadataBuilder>
196DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700197 const std::string& super_device, uint32_t source_slot) {
198 return LoadMetadataBuilder(
199 super_device, source_slot, BootControlInterface::kInvalidSlot);
200}
201
202std::unique_ptr<MetadataBuilder>
203DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800204 const std::string& super_device,
205 uint32_t source_slot,
206 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700207 std::unique_ptr<MetadataBuilder> builder;
208 if (target_slot == BootControlInterface::kInvalidSlot) {
209 builder =
210 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
211 } else {
212 builder = MetadataBuilder::NewForUpdate(
213 PartitionOpener(), super_device, source_slot, target_slot);
214 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800215
Yifan Hong537802d2018-08-15 13:15:42 -0700216 if (builder == nullptr) {
217 LOG(WARNING) << "No metadata slot "
218 << BootControlInterface::SlotName(source_slot) << " in "
219 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700220 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700221 }
222 LOG(INFO) << "Loaded metadata from slot "
223 << BootControlInterface::SlotName(source_slot) << " in "
224 << super_device;
225 return builder;
226}
227
228bool DynamicPartitionControlAndroid::StoreMetadata(
229 const std::string& super_device,
230 MetadataBuilder* builder,
231 uint32_t target_slot) {
232 auto metadata = builder->Export();
233 if (metadata == nullptr) {
234 LOG(ERROR) << "Cannot export metadata to slot "
235 << BootControlInterface::SlotName(target_slot) << " in "
236 << super_device;
237 return false;
238 }
239
Yifan Hong186bb682019-07-23 14:04:39 -0700240 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800241 if (!FlashPartitionTable(super_device, *metadata)) {
242 LOG(ERROR) << "Cannot write metadata to " << super_device;
243 return false;
244 }
245 LOG(INFO) << "Written metadata to " << super_device;
246 } else {
247 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
248 LOG(ERROR) << "Cannot write metadata to slot "
249 << BootControlInterface::SlotName(target_slot) << " in "
250 << super_device;
251 return false;
252 }
253 LOG(INFO) << "Copied metadata to slot "
254 << BootControlInterface::SlotName(target_slot) << " in "
255 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700256 }
257
Yifan Hong537802d2018-08-15 13:15:42 -0700258 return true;
259}
260
261bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
262 // We can't use fs_mgr to look up |partition_name| because fstab
263 // doesn't list every slot partition (it uses the slotselect option
264 // to mask the suffix).
265 //
266 // We can however assume that there's an entry for the /misc mount
267 // point and use that to get the device file for the misc
268 // partition. This helps us locate the disk that |partition_name|
269 // resides on. From there we'll assume that a by-name scheme is used
270 // so we can just replace the trailing "misc" by the given
271 // |partition_name| and suffix corresponding to |slot|, e.g.
272 //
273 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
274 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
275 //
276 // If needed, it's possible to relax the by-name assumption in the
277 // future by trawling /sys/block looking for the appropriate sibling
278 // of misc and then finding an entry in /dev matching the sysfs
279 // entry.
280
281 std::string err, misc_device = get_bootloader_message_blk_device(&err);
282 if (misc_device.empty()) {
283 LOG(ERROR) << "Unable to get misc block device: " << err;
284 return false;
285 }
286
287 if (!utils::IsSymlink(misc_device.c_str())) {
288 LOG(ERROR) << "Device file " << misc_device << " for /misc "
289 << "is not a symlink.";
290 return false;
291 }
292 *out = base::FilePath(misc_device).DirName().value();
293 return true;
294}
Yifan Hong012508e2019-07-22 18:30:40 -0700295
296bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
297 uint32_t source_slot,
298 uint32_t target_slot,
299 const PartitionMetadata& partition_metadata) {
300 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
301
302 // Unmap all the target dynamic partitions because they would become
303 // inconsistent with the new metadata.
304 for (const auto& group : partition_metadata.groups) {
305 for (const auto& partition : group.partitions) {
306 if (!UnmapPartitionOnDeviceMapper(partition.name + target_suffix)) {
307 return false;
308 }
309 }
310 }
311
312 std::string device_dir_str;
313 if (!GetDeviceDir(&device_dir_str)) {
314 return false;
315 }
316 base::FilePath device_dir(device_dir_str);
317 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700318 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700319
320 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
321 if (builder == nullptr) {
322 LOG(ERROR) << "No metadata at "
323 << BootControlInterface::SlotName(source_slot);
324 return false;
325 }
326
327 if (!UpdatePartitionMetadata(
328 builder.get(), target_slot, partition_metadata)) {
329 return false;
330 }
331
332 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700333 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700334 return StoreMetadata(target_device, builder.get(), target_slot);
335}
336
Yifan Hong700d7c12019-07-23 20:49:16 -0700337std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
338 uint32_t slot) {
339 return fs_mgr_get_super_partition_name(slot);
340}
341
Yifan Hong012508e2019-07-22 18:30:40 -0700342bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
343 MetadataBuilder* builder,
344 uint32_t target_slot,
345 const PartitionMetadata& partition_metadata) {
346 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
347 DeleteGroupsWithSuffix(builder, target_suffix);
348
349 uint64_t total_size = 0;
350 for (const auto& group : partition_metadata.groups) {
351 total_size += group.size;
352 }
353
354 std::string expr;
355 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700356 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700357 allocatable_space /= 2;
358 expr = "half of ";
359 }
360 if (total_size > allocatable_space) {
361 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
362 << " (" << total_size << ") has exceeded " << expr
363 << "allocatable space for dynamic partitions "
364 << allocatable_space << ".";
365 return false;
366 }
367
368 for (const auto& group : partition_metadata.groups) {
369 auto group_name_suffix = group.name + target_suffix;
370 if (!builder->AddGroup(group_name_suffix, group.size)) {
371 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
372 << group.size;
373 return false;
374 }
375 LOG(INFO) << "Added group " << group_name_suffix << " with size "
376 << group.size;
377
378 for (const auto& partition : group.partitions) {
379 auto partition_name_suffix = partition.name + target_suffix;
380 Partition* p = builder->AddPartition(
381 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
382 if (!p) {
383 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
384 << " to group " << group_name_suffix;
385 return false;
386 }
387 if (!builder->ResizePartition(p, partition.size)) {
388 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
389 << " to size " << partition.size << ". Not enough space?";
390 return false;
391 }
392 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
393 << group_name_suffix << " with size " << partition.size;
394 }
395 }
396
397 return true;
398}
399
Yifan Hong537802d2018-08-15 13:15:42 -0700400} // namespace chromeos_update_engine