blob: 329ddd3432e4609058029264672241f768572b8b [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 Hongf9464b42019-07-22 18:18:24 -0700207 auto builder = MetadataBuilder::NewForUpdate(
208 PartitionOpener(), super_device, source_slot, target_slot);
Yifan Hong6e706b12018-11-09 16:50:51 -0800209
Yifan Hong537802d2018-08-15 13:15:42 -0700210 if (builder == nullptr) {
211 LOG(WARNING) << "No metadata slot "
212 << BootControlInterface::SlotName(source_slot) << " in "
213 << super_device;
Yifan Hongf48a0052018-10-29 16:30:43 -0700214 return nullptr;
Yifan Hong537802d2018-08-15 13:15:42 -0700215 }
216 LOG(INFO) << "Loaded metadata from slot "
217 << BootControlInterface::SlotName(source_slot) << " in "
218 << super_device;
219 return builder;
220}
221
222bool DynamicPartitionControlAndroid::StoreMetadata(
223 const std::string& super_device,
224 MetadataBuilder* builder,
225 uint32_t target_slot) {
226 auto metadata = builder->Export();
227 if (metadata == nullptr) {
228 LOG(ERROR) << "Cannot export metadata to slot "
229 << BootControlInterface::SlotName(target_slot) << " in "
230 << super_device;
231 return false;
232 }
233
Yifan Hong186bb682019-07-23 14:04:39 -0700234 if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong6e706b12018-11-09 16:50:51 -0800235 if (!FlashPartitionTable(super_device, *metadata)) {
236 LOG(ERROR) << "Cannot write metadata to " << super_device;
237 return false;
238 }
239 LOG(INFO) << "Written metadata to " << super_device;
240 } else {
241 if (!UpdatePartitionTable(super_device, *metadata, target_slot)) {
242 LOG(ERROR) << "Cannot write metadata to slot "
243 << BootControlInterface::SlotName(target_slot) << " in "
244 << super_device;
245 return false;
246 }
247 LOG(INFO) << "Copied metadata to slot "
248 << BootControlInterface::SlotName(target_slot) << " in "
249 << super_device;
Yifan Hong537802d2018-08-15 13:15:42 -0700250 }
251
Yifan Hong537802d2018-08-15 13:15:42 -0700252 return true;
253}
254
255bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) {
256 // We can't use fs_mgr to look up |partition_name| because fstab
257 // doesn't list every slot partition (it uses the slotselect option
258 // to mask the suffix).
259 //
260 // We can however assume that there's an entry for the /misc mount
261 // point and use that to get the device file for the misc
262 // partition. This helps us locate the disk that |partition_name|
263 // resides on. From there we'll assume that a by-name scheme is used
264 // so we can just replace the trailing "misc" by the given
265 // |partition_name| and suffix corresponding to |slot|, e.g.
266 //
267 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
268 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
269 //
270 // If needed, it's possible to relax the by-name assumption in the
271 // future by trawling /sys/block looking for the appropriate sibling
272 // of misc and then finding an entry in /dev matching the sysfs
273 // entry.
274
275 std::string err, misc_device = get_bootloader_message_blk_device(&err);
276 if (misc_device.empty()) {
277 LOG(ERROR) << "Unable to get misc block device: " << err;
278 return false;
279 }
280
281 if (!utils::IsSymlink(misc_device.c_str())) {
282 LOG(ERROR) << "Device file " << misc_device << " for /misc "
283 << "is not a symlink.";
284 return false;
285 }
286 *out = base::FilePath(misc_device).DirName().value();
287 return true;
288}
Yifan Hong012508e2019-07-22 18:30:40 -0700289
290bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate(
291 uint32_t source_slot,
292 uint32_t target_slot,
293 const PartitionMetadata& partition_metadata) {
294 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
295
296 // Unmap all the target dynamic partitions because they would become
297 // inconsistent with the new metadata.
298 for (const auto& group : partition_metadata.groups) {
299 for (const auto& partition : group.partitions) {
300 if (!UnmapPartitionOnDeviceMapper(partition.name + target_suffix)) {
301 return false;
302 }
303 }
304 }
305
306 std::string device_dir_str;
307 if (!GetDeviceDir(&device_dir_str)) {
308 return false;
309 }
310 base::FilePath device_dir(device_dir_str);
311 auto source_device =
312 device_dir.Append(fs_mgr_get_super_partition_name(source_slot)).value();
313
314 auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot);
315 if (builder == nullptr) {
316 LOG(ERROR) << "No metadata at "
317 << BootControlInterface::SlotName(source_slot);
318 return false;
319 }
320
321 if (!UpdatePartitionMetadata(
322 builder.get(), target_slot, partition_metadata)) {
323 return false;
324 }
325
326 auto target_device =
327 device_dir.Append(fs_mgr_get_super_partition_name(target_slot)).value();
328 return StoreMetadata(target_device, builder.get(), target_slot);
329}
330
331bool DynamicPartitionControlAndroid::UpdatePartitionMetadata(
332 MetadataBuilder* builder,
333 uint32_t target_slot,
334 const PartitionMetadata& partition_metadata) {
335 const std::string target_suffix = SlotSuffixForSlotNumber(target_slot);
336 DeleteGroupsWithSuffix(builder, target_suffix);
337
338 uint64_t total_size = 0;
339 for (const auto& group : partition_metadata.groups) {
340 total_size += group.size;
341 }
342
343 std::string expr;
344 uint64_t allocatable_space = builder->AllocatableSpace();
Yifan Hong186bb682019-07-23 14:04:39 -0700345 if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) {
Yifan Hong012508e2019-07-22 18:30:40 -0700346 allocatable_space /= 2;
347 expr = "half of ";
348 }
349 if (total_size > allocatable_space) {
350 LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix
351 << " (" << total_size << ") has exceeded " << expr
352 << "allocatable space for dynamic partitions "
353 << allocatable_space << ".";
354 return false;
355 }
356
357 for (const auto& group : partition_metadata.groups) {
358 auto group_name_suffix = group.name + target_suffix;
359 if (!builder->AddGroup(group_name_suffix, group.size)) {
360 LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size "
361 << group.size;
362 return false;
363 }
364 LOG(INFO) << "Added group " << group_name_suffix << " with size "
365 << group.size;
366
367 for (const auto& partition : group.partitions) {
368 auto partition_name_suffix = partition.name + target_suffix;
369 Partition* p = builder->AddPartition(
370 partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY);
371 if (!p) {
372 LOG(ERROR) << "Cannot add partition " << partition_name_suffix
373 << " to group " << group_name_suffix;
374 return false;
375 }
376 if (!builder->ResizePartition(p, partition.size)) {
377 LOG(ERROR) << "Cannot resize partition " << partition_name_suffix
378 << " to size " << partition.size << ". Not enough space?";
379 return false;
380 }
381 LOG(INFO) << "Added partition " << partition_name_suffix << " to group "
382 << group_name_suffix << " with size " << partition.size;
383 }
384 }
385
386 return true;
387}
388
Yifan Hong537802d2018-08-15 13:15:42 -0700389} // namespace chromeos_update_engine