blob: b9732322b56ba782e12fdeab989a5e557bb63ba8 [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 Hong413d5722019-07-23 14:21:09 -070056constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled";
57constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit";
Yifan Hong537802d2018-08-15 13:15:42 -070058constexpr uint64_t kMapTimeoutMillis = 1000;
59
60DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() {
61 CleanupInternal(false /* wait */);
62}
63
Yifan Hong186bb682019-07-23 14:04:39 -070064static FeatureFlag GetFeatureFlag(const char* enable_prop,
65 const char* retrofit_prop) {
66 bool retrofit = GetBoolProperty(retrofit_prop, false);
67 bool enabled = GetBoolProperty(enable_prop, false);
68 if (retrofit && !enabled) {
69 LOG(ERROR) << retrofit_prop << " is true but " << enable_prop
70 << " is not. These sysprops are inconsistent. Assume that "
71 << enable_prop << " is true from now on.";
72 }
73 if (retrofit) {
74 return FeatureFlag(FeatureFlag::Value::RETROFIT);
75 }
76 if (enabled) {
77 return FeatureFlag(FeatureFlag::Value::LAUNCH);
78 }
79 return FeatureFlag(FeatureFlag::Value::NONE);
Yifan Hong537802d2018-08-15 13:15:42 -070080}
81
Yifan Hong186bb682019-07-23 14:04:39 -070082FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() {
83 return GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions);
Yifan Hong6e706b12018-11-09 16:50:51 -080084}
85
Yifan Hong413d5722019-07-23 14:21:09 -070086FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() {
87 return GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit);
88}
89
Yifan Hong8546a712019-03-28 14:42:53 -070090bool DynamicPartitionControlAndroid::MapPartitionInternal(
Yifan Hong537802d2018-08-15 13:15:42 -070091 const std::string& super_device,
92 const std::string& target_partition_name,
93 uint32_t slot,
Yifan Hongaf65ef12018-10-29 11:09:06 -070094 bool force_writable,
Yifan Hong537802d2018-08-15 13:15:42 -070095 std::string* path) {
David Andersonbb90dfb2019-08-13 14:14:56 -070096 CreateLogicalPartitionParams params = {
97 .block_device = super_device,
98 .metadata_slot = slot,
99 .partition_name = target_partition_name,
100 .force_writable = force_writable,
101 .timeout_ms = std::chrono::milliseconds(kMapTimeoutMillis),
102 };
103
104 if (!CreateLogicalPartition(params, path)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700105 LOG(ERROR) << "Cannot map " << target_partition_name << " in "
106 << super_device << " on device mapper.";
107 return false;
108 }
109 LOG(INFO) << "Succesfully mapped " << target_partition_name
Yifan Hongaf65ef12018-10-29 11:09:06 -0700110 << " to device mapper (force_writable = " << force_writable
111 << "); device path at " << *path;
Yifan Hong537802d2018-08-15 13:15:42 -0700112 mapped_devices_.insert(target_partition_name);
113 return true;
114}
115
Yifan Hong8546a712019-03-28 14:42:53 -0700116bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper(
117 const std::string& super_device,
118 const std::string& target_partition_name,
119 uint32_t slot,
120 bool force_writable,
121 std::string* path) {
122 DmDeviceState state = GetState(target_partition_name);
123 if (state == DmDeviceState::ACTIVE) {
124 if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) {
125 if (GetDmDevicePathByName(target_partition_name, path)) {
126 LOG(INFO) << target_partition_name
127 << " is mapped on device mapper: " << *path;
128 return true;
129 }
130 LOG(ERROR) << target_partition_name << " is mapped but path is unknown.";
131 return false;
132 }
133 // If target_partition_name is not in mapped_devices_ but state is ACTIVE,
134 // the device might be mapped incorrectly before. Attempt to unmap it.
135 // Note that for source partitions, if GetState() == ACTIVE, callers (e.g.
136 // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but
137 // should directly call GetDmDevicePathByName.
David Anderson4c891c92019-06-21 17:45:23 -0700138 if (!UnmapPartitionOnDeviceMapper(target_partition_name)) {
Yifan Hong8546a712019-03-28 14:42:53 -0700139 LOG(ERROR) << target_partition_name
140 << " is mapped before the update, and it cannot be unmapped.";
141 return false;
142 }
143 state = GetState(target_partition_name);
144 if (state != DmDeviceState::INVALID) {
145 LOG(ERROR) << target_partition_name << " is unmapped but state is "
146 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
147 return false;
148 }
149 }
150 if (state == DmDeviceState::INVALID) {
151 return MapPartitionInternal(
152 super_device, target_partition_name, slot, force_writable, path);
153 }
154
155 LOG(ERROR) << target_partition_name
156 << " is mapped on device mapper but state is unknown: "
157 << static_cast<std::underlying_type_t<DmDeviceState>>(state);
158 return false;
159}
160
Yifan Hong537802d2018-08-15 13:15:42 -0700161bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper(
David Anderson4c891c92019-06-21 17:45:23 -0700162 const std::string& target_partition_name) {
Yifan Hong537802d2018-08-15 13:15:42 -0700163 if (DeviceMapper::Instance().GetState(target_partition_name) !=
164 DmDeviceState::INVALID) {
David Anderson4c891c92019-06-21 17:45:23 -0700165 if (!DestroyLogicalPartition(target_partition_name)) {
Yifan Hong537802d2018-08-15 13:15:42 -0700166 LOG(ERROR) << "Cannot unmap " << target_partition_name
167 << " from device mapper.";
168 return false;
169 }
170 LOG(INFO) << "Successfully unmapped " << target_partition_name
171 << " from device mapper.";
172 }
173 mapped_devices_.erase(target_partition_name);
174 return true;
175}
176
177void DynamicPartitionControlAndroid::CleanupInternal(bool wait) {
Tao Bao8c4d0082019-08-08 08:56:16 -0700178 if (mapped_devices_.empty()) {
179 return;
180 }
Yifan Hong537802d2018-08-15 13:15:42 -0700181 // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence
182 // a copy is needed for the loop.
183 std::set<std::string> mapped = mapped_devices_;
184 LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper";
185 for (const auto& partition_name : mapped) {
David Anderson4c891c92019-06-21 17:45:23 -0700186 ignore_result(UnmapPartitionOnDeviceMapper(partition_name));
Yifan Hong537802d2018-08-15 13:15:42 -0700187 }
188}
189
190void DynamicPartitionControlAndroid::Cleanup() {
191 CleanupInternal(true /* wait */);
192}
193
194bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) {
195 return base::PathExists(base::FilePath(path));
196}
197
198android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState(
199 const std::string& name) {
200 return DeviceMapper::Instance().GetState(name);
201}
202
203bool DynamicPartitionControlAndroid::GetDmDevicePathByName(
204 const std::string& name, std::string* path) {
205 return DeviceMapper::Instance().GetDmDevicePathByName(name, path);
206}
207
208std::unique_ptr<MetadataBuilder>
209DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong012508e2019-07-22 18:30:40 -0700210 const std::string& super_device, uint32_t source_slot) {
211 return LoadMetadataBuilder(
212 super_device, source_slot, BootControlInterface::kInvalidSlot);
213}
214
215std::unique_ptr<MetadataBuilder>
216DynamicPartitionControlAndroid::LoadMetadataBuilder(
Yifan Hong6e706b12018-11-09 16:50:51 -0800217 const std::string& super_device,
218 uint32_t source_slot,
219 uint32_t target_slot) {
Yifan Hong30fa5f52019-08-05 16:39:59 -0700220 std::unique_ptr<MetadataBuilder> builder;
221 if (target_slot == BootControlInterface::kInvalidSlot) {
222 builder =
223 MetadataBuilder::New(PartitionOpener(), super_device, source_slot);
224 } else {
225 builder = MetadataBuilder::NewForUpdate(
226 PartitionOpener(), super_device, source_slot, target_slot);
227 }
Yifan Hong6e706b12018-11-09 16:50:51 -0800228
Yifan Hong537802d2018-08-15 13:15:42 -0700229 if (builder == nullptr) {
230 LOG(WARNING) << "No metadata slot "
231 << BootControlInterface::SlotName(source_slot) << " in "
232 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700233 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700234 }
235 LOG(INFO) << "Loaded metadata from slot "
236 << BootControlInterface::SlotName(source_slot) << " in "
237 << super_device;
238 return builder;
239}
240
241bool DynamicPartitionControlAndroid::StoreMetadata(
242 const std::string& super_device,
243 MetadataBuilder* builder,
244 uint32_t target_slot) {
245 auto metadata = builder->Export();
246 if (metadata == nullptr) {
247 LOG(ERROR) << "Cannot export metadata to slot "
248 << BootControlInterface::SlotName(target_slot) << " in "
249 << super_device;
250 return false;
251 }
252
Yifan Hong186bb682019-07-23 14:04:39 -0700253 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800254 if (!FlashPartitionTable(super_device, *metadata)) {
255 LOG(ERROR) << "Cannot write metadata to " << super_device;
256 return false;
257 }
258 LOG(INFO) << "Written metadata to " << super_device;
259 } else {
260 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
261 LOG(ERROR) << "Cannot write metadata to slot "
262 << BootControlInterface::SlotName(target_slot) << " in "
263 << super_device;
264 return false;
265 }
266 LOG(INFO) << "Copied metadata to slot "
267 << BootControlInterface::SlotName(target_slot) << " in "
268 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700269 }
270
Yifan Hong537802d2018-08-15 13:15:42 -0700271 return true;
272}
273
274bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
275 // We can't use fs_mgr to look up |partition_name| because fstab
276 // doesn't list every slot partition (it uses the slotselect option
277 // to mask the suffix).
278 //
279 // We can however assume that there's an entry for the /misc mount
280 // point and use that to get the device file for the misc
281 // partition. This helps us locate the disk that |partition_name|
282 // resides on. From there we'll assume that a by-name scheme is used
283 // so we can just replace the trailing "misc" by the given
284 // |partition_name| and suffix corresponding to |slot|, e.g.
285 //
286 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
287 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
288 //
289 // If needed, it's possible to relax the by-name assumption in the
290 // future by trawling /sys/block looking for the appropriate sibling
291 // of misc and then finding an entry in /dev matching the sysfs
292 // entry.
293
294 std::string err, misc_device = get_bootloader_message_blk_device(&err);
295 if (misc_device.empty()) {
296 LOG(ERROR) << "Unable to get misc block device: " << err;
297 return false;
298 }
299
300 if (!utils::IsSymlink(misc_device.c_str())) {
301 LOG(ERROR) << "Device file " << misc_device << " for /misc "
302 << "is not a symlink.";
303 return false;
304 }
305 *out = base::FilePath(misc_device).DirName().value();
306 return true;
307}
Yifan Hong012508e2019-07-22 18:30:40 -0700308
309bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
310 uint32_t source_slot,
311 uint32_t target_slot,
312 const PartitionMetadata& partition_metadata) {
313 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
314
315 // Unmap all the target dynamic partitions because they would become
316 // inconsistent with the new metadata.
317 for (const auto& group : partition_metadata.groups) {
318 for (const auto& partition : group.partitions) {
319 if (!UnmapPartitionOnDeviceMapper(partition.name + target_suffix)) {
320 return false;
321 }
322 }
323 }
324
325 std::string device_dir_str;
326 if (!GetDeviceDir(&device_dir_str)) {
327 return false;
328 }
329 base::FilePath device_dir(device_dir_str);
330 auto source_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700331 device_dir.Append(GetSuperPartitionName(source_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700332
333 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
334 if (builder == nullptr) {
335 LOG(ERROR) << "No metadata at "
336 << BootControlInterface::SlotName(source_slot);
337 return false;
338 }
339
340 if (!UpdatePartitionMetadata(
341 builder.get(), target_slot, partition_metadata)) {
342 return false;
343 }
344
345 auto target_device =
Yifan Hong700d7c12019-07-23 20:49:16 -0700346 device_dir.Append(GetSuperPartitionName(target_slot)).value();
Yifan Hong012508e2019-07-22 18:30:40 -0700347 return StoreMetadata(target_device, builder.get(), target_slot);
348}
349
Yifan Hong700d7c12019-07-23 20:49:16 -0700350std::string DynamicPartitionControlAndroid::GetSuperPartitionName(
351 uint32_t slot) {
352 return fs_mgr_get_super_partition_name(slot);
353}
354
Yifan Hong012508e2019-07-22 18:30:40 -0700355bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
356 MetadataBuilder* builder,
357 uint32_t target_slot,
358 const PartitionMetadata& partition_metadata) {
359 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
360 DeleteGroupsWithSuffix(builder, target_suffix);
361
362 uint64_t total_size = 0;
363 for (const auto& group : partition_metadata.groups) {
364 total_size += group.size;
365 }
366
367 std::string expr;
368 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700369 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700370 allocatable_space /= 2;
371 expr = "half of ";
372 }
373 if (total_size > allocatable_space) {
374 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
375 << " (" << total_size << ") has exceeded " << expr
376 << "allocatable space for dynamic partitions "
377 << allocatable_space << ".";
378 return false;
379 }
380
381 for (const auto& group : partition_metadata.groups) {
382 auto group_name_suffix = group.name + target_suffix;
383 if (!builder->AddGroup(group_name_suffix, group.size)) {
384 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
385 << group.size;
386 return false;
387 }
388 LOG(INFO) << "Added group " << group_name_suffix << " with size "
389 << group.size;
390
391 for (const auto& partition : group.partitions) {
392 auto partition_name_suffix = partition.name + target_suffix;
393 Partition* p = builder->AddPartition(
394 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
395 if (!p) {
396 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
397 << " to group " << group_name_suffix;
398 return false;
399 }
400 if (!builder->ResizePartition(p, partition.size)) {
401 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
402 << " to size " << partition.size << ". Not enough space?";
403 return false;
404 }
405 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
406 << group_name_suffix << " with size " << partition.size;
407 }
408 }
409
410 return true;
411}
412
Yifan Hong537802d2018-08-15 13:15:42 -0700413} // namespace chromeos_update_engine