Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 1 | // |
| 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 Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame^] | 22 | #include <vector> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 23 | |
| 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 Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame^] | 28 | #include <base/strings/string_util.h> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 29 | #include <bootloader_message/bootloader_message.h> |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame^] | 30 | #include <fs_mgr.h> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 31 | #include <fs_mgr_dm_linear.h> |
| 32 | |
| 33 | #include "update_engine/common/boot_control_interface.h" |
| 34 | #include "update_engine/common/utils.h" |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame^] | 35 | #include "update_engine/dynamic_partition_utils.h" |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 36 | |
| 37 | using android::base::GetBoolProperty; |
| 38 | using android::base::Join; |
| 39 | using android::dm::DeviceMapper; |
| 40 | using android::dm::DmDeviceState; |
| 41 | using android::fs_mgr::CreateLogicalPartition; |
| 42 | using android::fs_mgr::DestroyLogicalPartition; |
| 43 | using android::fs_mgr::MetadataBuilder; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame^] | 44 | using android::fs_mgr::Partition; |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 45 | using android::fs_mgr::PartitionOpener; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame^] | 46 | using android::fs_mgr::SlotSuffixForSlotNumber; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 47 | |
| 48 | namespace chromeos_update_engine { |
| 49 | |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame^] | 50 | using PartitionMetadata = BootControlInterface::PartitionMetadata; |
| 51 | |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 52 | constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions"; |
| 53 | constexpr char kRetrfoitDynamicPartitions[] = |
| 54 | "ro.boot.dynamic_partitions_retrofit"; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 55 | constexpr uint64_t kMapTimeoutMillis = 1000; |
| 56 | |
| 57 | DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() { |
| 58 | CleanupInternal(false /* wait */); |
| 59 | } |
| 60 | |
| 61 | bool DynamicPartitionControlAndroid::IsDynamicPartitionsEnabled() { |
| 62 | return GetBoolProperty(kUseDynamicPartitions, false); |
| 63 | } |
| 64 | |
Yifan Hong | 6e38b35 | 2018-11-19 14:12:37 -0800 | [diff] [blame] | 65 | bool DynamicPartitionControlAndroid::IsDynamicPartitionsRetrofit() { |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 66 | return GetBoolProperty(kRetrfoitDynamicPartitions, false); |
| 67 | } |
| 68 | |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 69 | bool DynamicPartitionControlAndroid::MapPartitionInternal( |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 70 | const std::string& super_device, |
| 71 | const std::string& target_partition_name, |
| 72 | uint32_t slot, |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 73 | bool force_writable, |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 74 | std::string* path) { |
| 75 | if (!CreateLogicalPartition(super_device.c_str(), |
| 76 | slot, |
| 77 | target_partition_name, |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 78 | force_writable, |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 79 | 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 Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 86 | << " to device mapper (force_writable = " << force_writable |
| 87 | << "); device path at " << *path; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 88 | mapped_devices_.insert(target_partition_name); |
| 89 | return true; |
| 90 | } |
| 91 | |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 92 | bool 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 Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 114 | if (!UnmapPartitionOnDeviceMapper(target_partition_name)) { |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 115 | 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 Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 137 | bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper( |
David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 138 | const std::string& target_partition_name) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 139 | if (DeviceMapper::Instance().GetState(target_partition_name) != |
| 140 | DmDeviceState::INVALID) { |
David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 141 | if (!DestroyLogicalPartition(target_partition_name)) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 142 | 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 | |
| 153 | void 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 Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 159 | ignore_result(UnmapPartitionOnDeviceMapper(partition_name)); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | void DynamicPartitionControlAndroid::Cleanup() { |
| 164 | CleanupInternal(true /* wait */); |
| 165 | } |
| 166 | |
| 167 | bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) { |
| 168 | return base::PathExists(base::FilePath(path)); |
| 169 | } |
| 170 | |
| 171 | android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState( |
| 172 | const std::string& name) { |
| 173 | return DeviceMapper::Instance().GetState(name); |
| 174 | } |
| 175 | |
| 176 | bool DynamicPartitionControlAndroid::GetDmDevicePathByName( |
| 177 | const std::string& name, std::string* path) { |
| 178 | return DeviceMapper::Instance().GetDmDevicePathByName(name, path); |
| 179 | } |
| 180 | |
| 181 | std::unique_ptr<MetadataBuilder> |
| 182 | DynamicPartitionControlAndroid::LoadMetadataBuilder( |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame^] | 183 | const std::string& super_device, uint32_t source_slot) { |
| 184 | return LoadMetadataBuilder( |
| 185 | super_device, source_slot, BootControlInterface::kInvalidSlot); |
| 186 | } |
| 187 | |
| 188 | std::unique_ptr<MetadataBuilder> |
| 189 | DynamicPartitionControlAndroid::LoadMetadataBuilder( |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 190 | const std::string& super_device, |
| 191 | uint32_t source_slot, |
| 192 | uint32_t target_slot) { |
Yifan Hong | f9464b4 | 2019-07-22 18:18:24 -0700 | [diff] [blame] | 193 | auto builder = MetadataBuilder::NewForUpdate( |
| 194 | PartitionOpener(), super_device, source_slot, target_slot); |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 195 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 196 | if (builder == nullptr) { |
| 197 | LOG(WARNING) << "No metadata slot " |
| 198 | << BootControlInterface::SlotName(source_slot) << " in " |
| 199 | << super_device; |
Yifan Hong | f48a005 | 2018-10-29 16:30:43 -0700 | [diff] [blame] | 200 | return nullptr; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 201 | } |
| 202 | LOG(INFO) << "Loaded metadata from slot " |
| 203 | << BootControlInterface::SlotName(source_slot) << " in " |
| 204 | << super_device; |
| 205 | return builder; |
| 206 | } |
| 207 | |
| 208 | bool 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 Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 220 | 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 Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 238 | return true; |
| 239 | } |
| 240 | |
| 241 | bool 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 Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame^] | 275 | |
| 276 | bool 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 | |
| 317 | bool 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 Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 375 | } // namespace chromeos_update_engine |