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 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 19 | #include <map> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <set> |
| 22 | #include <string> |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 23 | #include <vector> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 24 | |
| 25 | #include <android-base/properties.h> |
| 26 | #include <android-base/strings.h> |
| 27 | #include <base/files/file_util.h> |
| 28 | #include <base/logging.h> |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 29 | #include <base/strings/string_util.h> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 30 | #include <bootloader_message/bootloader_message.h> |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 31 | #include <fs_mgr.h> |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 32 | #include <fs_mgr_dm_linear.h> |
| 33 | |
| 34 | #include "update_engine/common/boot_control_interface.h" |
| 35 | #include "update_engine/common/utils.h" |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 36 | #include "update_engine/dynamic_partition_utils.h" |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 37 | |
| 38 | using android::base::GetBoolProperty; |
| 39 | using android::base::Join; |
| 40 | using android::dm::DeviceMapper; |
| 41 | using android::dm::DmDeviceState; |
| 42 | using android::fs_mgr::CreateLogicalPartition; |
David Anderson | bb90dfb | 2019-08-13 14:14:56 -0700 | [diff] [blame] | 43 | using android::fs_mgr::CreateLogicalPartitionParams; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 44 | using android::fs_mgr::DestroyLogicalPartition; |
| 45 | using android::fs_mgr::MetadataBuilder; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 46 | using android::fs_mgr::Partition; |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 47 | using android::fs_mgr::PartitionOpener; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 48 | using android::fs_mgr::SlotSuffixForSlotNumber; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 49 | |
| 50 | namespace chromeos_update_engine { |
| 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 | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 55 | constexpr char kVirtualAbEnabled[] = "ro.virtual_ab.enabled"; |
| 56 | constexpr char kVirtualAbRetrofit[] = "ro.virtual_ab.retrofit"; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 57 | constexpr uint64_t kMapTimeoutMillis = 1000; |
| 58 | |
| 59 | DynamicPartitionControlAndroid::~DynamicPartitionControlAndroid() { |
| 60 | CleanupInternal(false /* wait */); |
| 61 | } |
| 62 | |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 63 | static FeatureFlag GetFeatureFlag(const char* enable_prop, |
| 64 | const char* retrofit_prop) { |
| 65 | bool retrofit = GetBoolProperty(retrofit_prop, false); |
| 66 | bool enabled = GetBoolProperty(enable_prop, false); |
| 67 | if (retrofit && !enabled) { |
| 68 | LOG(ERROR) << retrofit_prop << " is true but " << enable_prop |
| 69 | << " is not. These sysprops are inconsistent. Assume that " |
| 70 | << enable_prop << " is true from now on."; |
| 71 | } |
| 72 | if (retrofit) { |
| 73 | return FeatureFlag(FeatureFlag::Value::RETROFIT); |
| 74 | } |
| 75 | if (enabled) { |
| 76 | return FeatureFlag(FeatureFlag::Value::LAUNCH); |
| 77 | } |
| 78 | return FeatureFlag(FeatureFlag::Value::NONE); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 81 | FeatureFlag DynamicPartitionControlAndroid::GetDynamicPartitionsFeatureFlag() { |
| 82 | return GetFeatureFlag(kUseDynamicPartitions, kRetrfoitDynamicPartitions); |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Yifan Hong | 413d572 | 2019-07-23 14:21:09 -0700 | [diff] [blame] | 85 | FeatureFlag DynamicPartitionControlAndroid::GetVirtualAbFeatureFlag() { |
| 86 | return GetFeatureFlag(kVirtualAbEnabled, kVirtualAbRetrofit); |
| 87 | } |
| 88 | |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 89 | bool DynamicPartitionControlAndroid::MapPartitionInternal( |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 90 | const std::string& super_device, |
| 91 | const std::string& target_partition_name, |
| 92 | uint32_t slot, |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 93 | bool force_writable, |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 94 | std::string* path) { |
David Anderson | bb90dfb | 2019-08-13 14:14:56 -0700 | [diff] [blame] | 95 | CreateLogicalPartitionParams params = { |
| 96 | .block_device = super_device, |
| 97 | .metadata_slot = slot, |
| 98 | .partition_name = target_partition_name, |
| 99 | .force_writable = force_writable, |
| 100 | .timeout_ms = std::chrono::milliseconds(kMapTimeoutMillis), |
| 101 | }; |
| 102 | |
| 103 | if (!CreateLogicalPartition(params, path)) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 104 | LOG(ERROR) << "Cannot map " << target_partition_name << " in " |
| 105 | << super_device << " on device mapper."; |
| 106 | return false; |
| 107 | } |
| 108 | LOG(INFO) << "Succesfully mapped " << target_partition_name |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 109 | << " to device mapper (force_writable = " << force_writable |
| 110 | << "); device path at " << *path; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 111 | mapped_devices_.insert(target_partition_name); |
| 112 | return true; |
| 113 | } |
| 114 | |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 115 | bool DynamicPartitionControlAndroid::MapPartitionOnDeviceMapper( |
| 116 | const std::string& super_device, |
| 117 | const std::string& target_partition_name, |
| 118 | uint32_t slot, |
| 119 | bool force_writable, |
| 120 | std::string* path) { |
| 121 | DmDeviceState state = GetState(target_partition_name); |
| 122 | if (state == DmDeviceState::ACTIVE) { |
| 123 | if (mapped_devices_.find(target_partition_name) != mapped_devices_.end()) { |
| 124 | if (GetDmDevicePathByName(target_partition_name, path)) { |
| 125 | LOG(INFO) << target_partition_name |
| 126 | << " is mapped on device mapper: " << *path; |
| 127 | return true; |
| 128 | } |
| 129 | LOG(ERROR) << target_partition_name << " is mapped but path is unknown."; |
| 130 | return false; |
| 131 | } |
| 132 | // If target_partition_name is not in mapped_devices_ but state is ACTIVE, |
| 133 | // the device might be mapped incorrectly before. Attempt to unmap it. |
| 134 | // Note that for source partitions, if GetState() == ACTIVE, callers (e.g. |
| 135 | // BootControlAndroid) should not call MapPartitionOnDeviceMapper, but |
| 136 | // should directly call GetDmDevicePathByName. |
David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 137 | if (!UnmapPartitionOnDeviceMapper(target_partition_name)) { |
Yifan Hong | 8546a71 | 2019-03-28 14:42:53 -0700 | [diff] [blame] | 138 | LOG(ERROR) << target_partition_name |
| 139 | << " is mapped before the update, and it cannot be unmapped."; |
| 140 | return false; |
| 141 | } |
| 142 | state = GetState(target_partition_name); |
| 143 | if (state != DmDeviceState::INVALID) { |
| 144 | LOG(ERROR) << target_partition_name << " is unmapped but state is " |
| 145 | << static_cast<std::underlying_type_t<DmDeviceState>>(state); |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 | if (state == DmDeviceState::INVALID) { |
| 150 | return MapPartitionInternal( |
| 151 | super_device, target_partition_name, slot, force_writable, path); |
| 152 | } |
| 153 | |
| 154 | LOG(ERROR) << target_partition_name |
| 155 | << " is mapped on device mapper but state is unknown: " |
| 156 | << static_cast<std::underlying_type_t<DmDeviceState>>(state); |
| 157 | return false; |
| 158 | } |
| 159 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 160 | bool DynamicPartitionControlAndroid::UnmapPartitionOnDeviceMapper( |
David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 161 | const std::string& target_partition_name) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 162 | if (DeviceMapper::Instance().GetState(target_partition_name) != |
| 163 | DmDeviceState::INVALID) { |
David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 164 | if (!DestroyLogicalPartition(target_partition_name)) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 165 | LOG(ERROR) << "Cannot unmap " << target_partition_name |
| 166 | << " from device mapper."; |
| 167 | return false; |
| 168 | } |
| 169 | LOG(INFO) << "Successfully unmapped " << target_partition_name |
| 170 | << " from device mapper."; |
| 171 | } |
| 172 | mapped_devices_.erase(target_partition_name); |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | void DynamicPartitionControlAndroid::CleanupInternal(bool wait) { |
Tao Bao | 8c4d008 | 2019-08-08 08:56:16 -0700 | [diff] [blame] | 177 | if (mapped_devices_.empty()) { |
| 178 | return; |
| 179 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 180 | // UnmapPartitionOnDeviceMapper removes objects from mapped_devices_, hence |
| 181 | // a copy is needed for the loop. |
| 182 | std::set<std::string> mapped = mapped_devices_; |
| 183 | LOG(INFO) << "Destroying [" << Join(mapped, ", ") << "] from device mapper"; |
| 184 | for (const auto& partition_name : mapped) { |
David Anderson | 4c891c9 | 2019-06-21 17:45:23 -0700 | [diff] [blame] | 185 | ignore_result(UnmapPartitionOnDeviceMapper(partition_name)); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | void DynamicPartitionControlAndroid::Cleanup() { |
| 190 | CleanupInternal(true /* wait */); |
| 191 | } |
| 192 | |
| 193 | bool DynamicPartitionControlAndroid::DeviceExists(const std::string& path) { |
| 194 | return base::PathExists(base::FilePath(path)); |
| 195 | } |
| 196 | |
| 197 | android::dm::DmDeviceState DynamicPartitionControlAndroid::GetState( |
| 198 | const std::string& name) { |
| 199 | return DeviceMapper::Instance().GetState(name); |
| 200 | } |
| 201 | |
| 202 | bool DynamicPartitionControlAndroid::GetDmDevicePathByName( |
| 203 | const std::string& name, std::string* path) { |
| 204 | return DeviceMapper::Instance().GetDmDevicePathByName(name, path); |
| 205 | } |
| 206 | |
| 207 | std::unique_ptr<MetadataBuilder> |
| 208 | DynamicPartitionControlAndroid::LoadMetadataBuilder( |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 209 | const std::string& super_device, uint32_t source_slot) { |
| 210 | return LoadMetadataBuilder( |
| 211 | super_device, source_slot, BootControlInterface::kInvalidSlot); |
| 212 | } |
| 213 | |
| 214 | std::unique_ptr<MetadataBuilder> |
| 215 | DynamicPartitionControlAndroid::LoadMetadataBuilder( |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 216 | const std::string& super_device, |
| 217 | uint32_t source_slot, |
| 218 | uint32_t target_slot) { |
Yifan Hong | 30fa5f5 | 2019-08-05 16:39:59 -0700 | [diff] [blame] | 219 | std::unique_ptr<MetadataBuilder> builder; |
| 220 | if (target_slot == BootControlInterface::kInvalidSlot) { |
| 221 | builder = |
| 222 | MetadataBuilder::New(PartitionOpener(), super_device, source_slot); |
| 223 | } else { |
| 224 | builder = MetadataBuilder::NewForUpdate( |
| 225 | PartitionOpener(), super_device, source_slot, target_slot); |
| 226 | } |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 227 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 228 | if (builder == nullptr) { |
| 229 | LOG(WARNING) << "No metadata slot " |
| 230 | << BootControlInterface::SlotName(source_slot) << " in " |
| 231 | << super_device; |
Yifan Hong | f48a005 | 2018-10-29 16:30:43 -0700 | [diff] [blame] | 232 | return nullptr; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 233 | } |
| 234 | LOG(INFO) << "Loaded metadata from slot " |
| 235 | << BootControlInterface::SlotName(source_slot) << " in " |
| 236 | << super_device; |
| 237 | return builder; |
| 238 | } |
| 239 | |
| 240 | bool DynamicPartitionControlAndroid::StoreMetadata( |
| 241 | const std::string& super_device, |
| 242 | MetadataBuilder* builder, |
| 243 | uint32_t target_slot) { |
| 244 | auto metadata = builder->Export(); |
| 245 | if (metadata == nullptr) { |
| 246 | LOG(ERROR) << "Cannot export metadata to slot " |
| 247 | << BootControlInterface::SlotName(target_slot) << " in " |
| 248 | << super_device; |
| 249 | return false; |
| 250 | } |
| 251 | |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 252 | if (GetDynamicPartitionsFeatureFlag().IsRetrofit()) { |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 253 | if (!FlashPartitionTable(super_device, *metadata)) { |
| 254 | LOG(ERROR) << "Cannot write metadata to " << super_device; |
| 255 | return false; |
| 256 | } |
| 257 | LOG(INFO) << "Written metadata to " << super_device; |
| 258 | } else { |
| 259 | if (!UpdatePartitionTable(super_device, *metadata, target_slot)) { |
| 260 | LOG(ERROR) << "Cannot write metadata to slot " |
| 261 | << BootControlInterface::SlotName(target_slot) << " in " |
| 262 | << super_device; |
| 263 | return false; |
| 264 | } |
| 265 | LOG(INFO) << "Copied metadata to slot " |
| 266 | << BootControlInterface::SlotName(target_slot) << " in " |
| 267 | << super_device; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 270 | return true; |
| 271 | } |
| 272 | |
| 273 | bool DynamicPartitionControlAndroid::GetDeviceDir(std::string* out) { |
| 274 | // We can't use fs_mgr to look up |partition_name| because fstab |
| 275 | // doesn't list every slot partition (it uses the slotselect option |
| 276 | // to mask the suffix). |
| 277 | // |
| 278 | // We can however assume that there's an entry for the /misc mount |
| 279 | // point and use that to get the device file for the misc |
| 280 | // partition. This helps us locate the disk that |partition_name| |
| 281 | // resides on. From there we'll assume that a by-name scheme is used |
| 282 | // so we can just replace the trailing "misc" by the given |
| 283 | // |partition_name| and suffix corresponding to |slot|, e.g. |
| 284 | // |
| 285 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc -> |
| 286 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a |
| 287 | // |
| 288 | // If needed, it's possible to relax the by-name assumption in the |
| 289 | // future by trawling /sys/block looking for the appropriate sibling |
| 290 | // of misc and then finding an entry in /dev matching the sysfs |
| 291 | // entry. |
| 292 | |
| 293 | std::string err, misc_device = get_bootloader_message_blk_device(&err); |
| 294 | if (misc_device.empty()) { |
| 295 | LOG(ERROR) << "Unable to get misc block device: " << err; |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | if (!utils::IsSymlink(misc_device.c_str())) { |
| 300 | LOG(ERROR) << "Device file " << misc_device << " for /misc " |
| 301 | << "is not a symlink."; |
| 302 | return false; |
| 303 | } |
| 304 | *out = base::FilePath(misc_device).DirName().value(); |
| 305 | return true; |
| 306 | } |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 307 | |
| 308 | bool DynamicPartitionControlAndroid::PreparePartitionsForUpdate( |
| 309 | uint32_t source_slot, |
| 310 | uint32_t target_slot, |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 311 | const DeltaArchiveManifest& manifest) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 312 | const std::string target_suffix = SlotSuffixForSlotNumber(target_slot); |
| 313 | |
| 314 | // Unmap all the target dynamic partitions because they would become |
| 315 | // inconsistent with the new metadata. |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 316 | for (const auto& group : manifest.dynamic_partition_metadata().groups()) { |
| 317 | for (const auto& partition_name : group.partition_names()) { |
| 318 | if (!UnmapPartitionOnDeviceMapper(partition_name + target_suffix)) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 319 | return false; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | std::string device_dir_str; |
| 325 | if (!GetDeviceDir(&device_dir_str)) { |
| 326 | return false; |
| 327 | } |
| 328 | base::FilePath device_dir(device_dir_str); |
| 329 | auto source_device = |
Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 330 | device_dir.Append(GetSuperPartitionName(source_slot)).value(); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 331 | |
| 332 | auto builder = LoadMetadataBuilder(source_device, source_slot, target_slot); |
| 333 | if (builder == nullptr) { |
| 334 | LOG(ERROR) << "No metadata at " |
| 335 | << BootControlInterface::SlotName(source_slot); |
| 336 | return false; |
| 337 | } |
| 338 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 339 | if (!UpdatePartitionMetadata(builder.get(), target_slot, manifest)) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 340 | return false; |
| 341 | } |
| 342 | |
| 343 | auto target_device = |
Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 344 | device_dir.Append(GetSuperPartitionName(target_slot)).value(); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 345 | return StoreMetadata(target_device, builder.get(), target_slot); |
| 346 | } |
| 347 | |
Yifan Hong | 700d7c1 | 2019-07-23 20:49:16 -0700 | [diff] [blame] | 348 | std::string DynamicPartitionControlAndroid::GetSuperPartitionName( |
| 349 | uint32_t slot) { |
| 350 | return fs_mgr_get_super_partition_name(slot); |
| 351 | } |
| 352 | |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 353 | bool DynamicPartitionControlAndroid::UpdatePartitionMetadata( |
| 354 | MetadataBuilder* builder, |
| 355 | uint32_t target_slot, |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 356 | const DeltaArchiveManifest& manifest) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 357 | const std::string target_suffix = SlotSuffixForSlotNumber(target_slot); |
| 358 | DeleteGroupsWithSuffix(builder, target_suffix); |
| 359 | |
| 360 | uint64_t total_size = 0; |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 361 | for (const auto& group : manifest.dynamic_partition_metadata().groups()) { |
| 362 | total_size += group.size(); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | std::string expr; |
| 366 | uint64_t allocatable_space = builder->AllocatableSpace(); |
Yifan Hong | 186bb68 | 2019-07-23 14:04:39 -0700 | [diff] [blame] | 367 | if (!GetDynamicPartitionsFeatureFlag().IsRetrofit()) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 368 | allocatable_space /= 2; |
| 369 | expr = "half of "; |
| 370 | } |
| 371 | if (total_size > allocatable_space) { |
| 372 | LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix |
| 373 | << " (" << total_size << ") has exceeded " << expr |
| 374 | << "allocatable space for dynamic partitions " |
| 375 | << allocatable_space << "."; |
| 376 | return false; |
| 377 | } |
| 378 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 379 | // name of partition(e.g. "system") -> size in bytes |
| 380 | std::map<std::string, uint64_t> partition_sizes; |
| 381 | for (const auto& partition : manifest.partitions()) { |
| 382 | partition_sizes.emplace(partition.partition_name(), |
| 383 | partition.new_partition_info().size()); |
| 384 | } |
| 385 | |
| 386 | for (const auto& group : manifest.dynamic_partition_metadata().groups()) { |
| 387 | auto group_name_suffix = group.name() + target_suffix; |
| 388 | if (!builder->AddGroup(group_name_suffix, group.size())) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 389 | LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size " |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 390 | << group.size(); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 391 | return false; |
| 392 | } |
| 393 | LOG(INFO) << "Added group " << group_name_suffix << " with size " |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 394 | << group.size(); |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 395 | |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 396 | for (const auto& partition_name : group.partition_names()) { |
| 397 | auto partition_sizes_it = partition_sizes.find(partition_name); |
| 398 | if (partition_sizes_it == partition_sizes.end()) { |
| 399 | // TODO(tbao): Support auto-filling partition info for framework-only |
| 400 | // OTA. |
| 401 | LOG(ERROR) << "dynamic_partition_metadata contains partition " |
| 402 | << partition_name << " but it is not part of the manifest. " |
| 403 | << "This is not supported."; |
| 404 | return false; |
| 405 | } |
| 406 | uint64_t partition_size = partition_sizes_it->second; |
| 407 | |
| 408 | auto partition_name_suffix = partition_name + target_suffix; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 409 | Partition* p = builder->AddPartition( |
| 410 | partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY); |
| 411 | if (!p) { |
| 412 | LOG(ERROR) << "Cannot add partition " << partition_name_suffix |
| 413 | << " to group " << group_name_suffix; |
| 414 | return false; |
| 415 | } |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 416 | if (!builder->ResizePartition(p, partition_size)) { |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 417 | LOG(ERROR) << "Cannot resize partition " << partition_name_suffix |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 418 | << " to size " << partition_size << ". Not enough space?"; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | LOG(INFO) << "Added partition " << partition_name_suffix << " to group " |
Yifan Hong | 13d41cb | 2019-09-16 13:18:22 -0700 | [diff] [blame^] | 422 | << group_name_suffix << " with size " << partition_size; |
Yifan Hong | 012508e | 2019-07-22 18:30:40 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | return true; |
| 427 | } |
| 428 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 429 | } // namespace chromeos_update_engine |