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> |
| 22 | |
| 23 | #include <android-base/properties.h> |
| 24 | #include <android-base/strings.h> |
| 25 | #include <base/files/file_util.h> |
| 26 | #include <base/logging.h> |
| 27 | #include <bootloader_message/bootloader_message.h> |
| 28 | #include <fs_mgr_dm_linear.h> |
| 29 | |
| 30 | #include "update_engine/common/boot_control_interface.h" |
| 31 | #include "update_engine/common/utils.h" |
| 32 | |
| 33 | using android::base::GetBoolProperty; |
| 34 | using android::base::Join; |
| 35 | using android::dm::DeviceMapper; |
| 36 | using android::dm::DmDeviceState; |
| 37 | using android::fs_mgr::CreateLogicalPartition; |
| 38 | using android::fs_mgr::DestroyLogicalPartition; |
| 39 | using android::fs_mgr::MetadataBuilder; |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 40 | using android::fs_mgr::PartitionOpener; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 41 | |
| 42 | namespace chromeos_update_engine { |
| 43 | |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 44 | constexpr char kUseDynamicPartitions[] = "ro.boot.dynamic_partitions"; |
| 45 | constexpr char kRetrfoitDynamicPartitions[] = |
| 46 | "ro.boot.dynamic_partitions_retrofit"; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 47 | constexpr uint64_t kMapTimeoutMillis = 1000; |
| 48 | |
| 49 | DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() { |
| 50 | CleanupInternal(false /* wait */); |
| 51 | } |
| 52 | |
| 53 | bool DynamicPartitionControlAndroid::IsDynamicPartitionsEnabled() { |
| 54 | return GetBoolProperty(kUseDynamicPartitions, false); |
| 55 | } |
| 56 | |
Yifan Hong | 6e38b35 | 2018-11-19 14:12:37 -0800 | [diff] [blame] | 57 | bool DynamicPartitionControlAndroid::IsDynamicPartitionsRetrofit() { |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 58 | return GetBoolProperty(kRetrfoitDynamicPartitions, false); |
| 59 | } |
| 60 | |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 61 | bool DynamicPartitionControlAndroid::MapPartitionInternal( |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 62 | const std::string& super_device, |
| 63 | const std::string& target_partition_name, |
| 64 | uint32_t slot, |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 65 | bool force_writable, |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 66 | std::string* path) { |
| 67 | if (!CreateLogicalPartition(super_device.c_str(), |
| 68 | slot, |
| 69 | target_partition_name, |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 70 | force_writable, |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 71 | std::chrono::milliseconds(kMapTimeoutMillis), |
| 72 | path)) { |
| 73 | LOG(ERROR) << "Cannot map " << target_partition_name << " in " |
| 74 | << super_device << " on device mapper."; |
| 75 | return false; |
| 76 | } |
| 77 | LOG(INFO) << "Succesfully mapped " << target_partition_name |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 78 | << " to device mapper (force_writable = " << force_writable |
| 79 | << "); device path at " << *path; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 80 | mapped_devices_.insert(target_partition_name); |
| 81 | return true; |
| 82 | } |
| 83 | |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 84 | bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper( |
| 85 | const std::string& super_device, |
| 86 | const std::string& target_partition_name, |
| 87 | uint32_t slot, |
| 88 | bool force_writable, |
| 89 | std::string* path) { |
| 90 | DmDeviceState state = GetState(target_partition_name); |
| 91 | if (state == DmDeviceState::ACTIVE) { |
| 92 | if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) { |
| 93 | if (GetDmDevicePathByName(target_partition_name, path)) { |
| 94 | LOG(INFO) << target_partition_name |
| 95 | << " is mapped on device mapper: " << *path; |
| 96 | return true; |
| 97 | } |
| 98 | LOG(ERROR) << target_partition_name << " is mapped but path is unknown."; |
| 99 | return false; |
| 100 | } |
| 101 | // If target_partition_name is not in mapped_devices_ but state is ACTIVE, |
| 102 | // the device might be mapped incorrectly before. Attempt to unmap it. |
| 103 | // Note that for source partitions, if GetState() == ACTIVE, callers (e.g. |
| 104 | // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but |
| 105 | // should directly call GetDmDevicePathByName. |
| 106 | if (!UnmapPartitionOnDeviceMapper(target_partition_name, true /* wait */)) { |
| 107 | LOG(ERROR) << target_partition_name |
| 108 | << " is mapped before the update, and it cannot be unmapped."; |
| 109 | return false; |
| 110 | } |
| 111 | state = GetState(target_partition_name); |
| 112 | if (state != DmDeviceState::INVALID) { |
| 113 | LOG(ERROR) << target_partition_name << " is unmapped but state is " |
| 114 | << static_cast<std::underlying_type_t<DmDeviceState>>(state); |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | if (state == DmDeviceState::INVALID) { |
| 119 | return MapPartitionInternal( |
| 120 | super_device, target_partition_name, slot, force_writable, path); |
| 121 | } |
| 122 | |
| 123 | LOG(ERROR) << target_partition_name |
| 124 | << " is mapped on device mapper but state is unknown: " |
| 125 | << static_cast<std::underlying_type_t<DmDeviceState>>(state); |
| 126 | return false; |
| 127 | } |
| 128 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 129 | bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper( |
| 130 | const std::string& target_partition_name, bool wait) { |
| 131 | if (DeviceMapper::Instance().GetState(target_partition_name) != |
| 132 | DmDeviceState::INVALID) { |
| 133 | if (!DestroyLogicalPartition( |
| 134 | target_partition_name, |
| 135 | std::chrono::milliseconds(wait ? kMapTimeoutMillis : 0))) { |
| 136 | LOG(ERROR) << "Cannot unmap " << target_partition_name |
| 137 | << " from device mapper."; |
| 138 | return false; |
| 139 | } |
| 140 | LOG(INFO) << "Successfully unmapped " << target_partition_name |
| 141 | << " from device mapper."; |
| 142 | } |
| 143 | mapped_devices_.erase(target_partition_name); |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | void DynamicPartitionControlAndroid::CleanupInternal(bool wait) { |
| 148 | // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence |
| 149 | // a copy is needed for the loop. |
| 150 | std::set<std::string> mapped = mapped_devices_; |
| 151 | LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper"; |
| 152 | for (const auto& partition_name : mapped) { |
| 153 | ignore_result(UnmapPartitionOnDeviceMapper(partition_name, wait)); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | void DynamicPartitionControlAndroid::Cleanup() { |
| 158 | CleanupInternal(true /* wait */); |
| 159 | } |
| 160 | |
| 161 | bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) { |
| 162 | return base::PathExists(base::FilePath(path)); |
| 163 | } |
| 164 | |
| 165 | android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState( |
| 166 | const std::string& name) { |
| 167 | return DeviceMapper::Instance().GetState(name); |
| 168 | } |
| 169 | |
| 170 | bool DynamicPartitionControlAndroid::GetDmDevicePathByName( |
| 171 | const std::string& name, std::string* path) { |
| 172 | return DeviceMapper::Instance().GetDmDevicePathByName(name, path); |
| 173 | } |
| 174 | |
| 175 | std::unique_ptr<MetadataBuilder> |
| 176 | DynamicPartitionControlAndroid::LoadMetadataBuilder( |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 177 | const std::string& super_device, |
| 178 | uint32_t source_slot, |
| 179 | uint32_t target_slot) { |
| 180 | std::unique_ptr<MetadataBuilder> builder; |
| 181 | |
| 182 | if (target_slot != BootControlInterface::kInvalidSlot && |
| 183 | IsDynamicPartitionsRetrofit()) { |
| 184 | builder = MetadataBuilder::NewForUpdate( |
| 185 | PartitionOpener(), super_device, source_slot, target_slot); |
| 186 | } else { |
| 187 | builder = |
| 188 | MetadataBuilder::New(PartitionOpener(), super_device, source_slot); |
| 189 | } |
| 190 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 191 | if (builder == nullptr) { |
| 192 | LOG(WARNING) << "No metadata slot " |
| 193 | << BootControlInterface::SlotName(source_slot) << " in " |
| 194 | << super_device; |
Yifan Hong | f48a005 | 2018-10-29 16:30:43 -0700 | [diff] [blame] | 195 | return nullptr; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 196 | } |
| 197 | LOG(INFO) << "Loaded metadata from slot " |
| 198 | << BootControlInterface::SlotName(source_slot) << " in " |
| 199 | << super_device; |
| 200 | return builder; |
| 201 | } |
| 202 | |
| 203 | bool DynamicPartitionControlAndroid::StoreMetadata( |
| 204 | const std::string& super_device, |
| 205 | MetadataBuilder* builder, |
| 206 | uint32_t target_slot) { |
| 207 | auto metadata = builder->Export(); |
| 208 | if (metadata == nullptr) { |
| 209 | LOG(ERROR) << "Cannot export metadata to slot " |
| 210 | << BootControlInterface::SlotName(target_slot) << " in " |
| 211 | << super_device; |
| 212 | return false; |
| 213 | } |
| 214 | |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 215 | if (IsDynamicPartitionsRetrofit()) { |
| 216 | if (!FlashPartitionTable(super_device, *metadata)) { |
| 217 | LOG(ERROR) << "Cannot write metadata to " << super_device; |
| 218 | return false; |
| 219 | } |
| 220 | LOG(INFO) << "Written metadata to " << super_device; |
| 221 | } else { |
| 222 | if (!UpdatePartitionTable(super_device, *metadata, target_slot)) { |
| 223 | LOG(ERROR) << "Cannot write metadata to slot " |
| 224 | << BootControlInterface::SlotName(target_slot) << " in " |
| 225 | << super_device; |
| 226 | return false; |
| 227 | } |
| 228 | LOG(INFO) << "Copied metadata to slot " |
| 229 | << BootControlInterface::SlotName(target_slot) << " in " |
| 230 | << super_device; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | |
| 236 | bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) { |
| 237 | // We can't use fs_mgr to look up |partition_name| because fstab |
| 238 | // doesn't list every slot partition (it uses the slotselect option |
| 239 | // to mask the suffix). |
| 240 | // |
| 241 | // We can however assume that there's an entry for the /misc mount |
| 242 | // point and use that to get the device file for the misc |
| 243 | // partition. This helps us locate the disk that |partition_name| |
| 244 | // resides on. From there we'll assume that a by-name scheme is used |
| 245 | // so we can just replace the trailing "misc" by the given |
| 246 | // |partition_name| and suffix corresponding to |slot|, e.g. |
| 247 | // |
| 248 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc -> |
| 249 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a |
| 250 | // |
| 251 | // If needed, it's possible to relax the by-name assumption in the |
| 252 | // future by trawling /sys/block looking for the appropriate sibling |
| 253 | // of misc and then finding an entry in /dev matching the sysfs |
| 254 | // entry. |
| 255 | |
| 256 | std::string err, misc_device = get_bootloader_message_blk_device(&err); |
| 257 | if (misc_device.empty()) { |
| 258 | LOG(ERROR) << "Unable to get misc block device: " << err; |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | if (!utils::IsSymlink(misc_device.c_str())) { |
| 263 | LOG(ERROR) << "Device file " << misc_device << " for /misc " |
| 264 | << "is not a symlink."; |
| 265 | return false; |
| 266 | } |
| 267 | *out = base::FilePath(misc_device).DirName().value(); |
| 268 | return true; |
| 269 | } |
| 270 | } // namespace chromeos_update_engine |