Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2015 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 | |
Alex Deymo | 1b03f9f | 2015-12-09 00:38:36 -0800 | [diff] [blame] | 17 | #include "update_engine/boot_control_android.h" |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 18 | |
Sen Jiang | d944faa | 2018-08-22 18:46:39 -0700 | [diff] [blame] | 19 | #include <memory> |
| 20 | #include <utility> |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 21 | #include <vector> |
Sen Jiang | d944faa | 2018-08-22 18:46:39 -0700 | [diff] [blame] | 22 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 23 | #include <base/bind.h> |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 24 | #include <base/logging.h> |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 25 | #include <base/strings/string_util.h> |
Sen Jiang | d944faa | 2018-08-22 18:46:39 -0700 | [diff] [blame] | 26 | #include <bootloader_message/bootloader_message.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 27 | #include <brillo/message_loops/message_loop.h> |
David Anderson | d63cb3c | 2018-10-01 14:15:00 -0700 | [diff] [blame] | 28 | #include <fs_mgr.h> |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 29 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include "update_engine/common/utils.h" |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 31 | #include "update_engine/dynamic_partition_control_android.h" |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 32 | |
| 33 | using std::string; |
| 34 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 35 | using android::dm::DmDeviceState; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 36 | using android::fs_mgr::Partition; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 37 | using android::hardware::hidl_string; |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 38 | using android::hardware::Return; |
| 39 | using android::hardware::boot::V1_0::BoolResult; |
| 40 | using android::hardware::boot::V1_0::CommandResult; |
| 41 | using android::hardware::boot::V1_0::IBootControl; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 42 | using Slot = chromeos_update_engine::BootControlInterface::Slot; |
Yifan Hong | 6b0a984 | 2018-10-16 16:54:18 -0700 | [diff] [blame] | 43 | using PartitionMetadata = |
| 44 | chromeos_update_engine::BootControlInterface::PartitionMetadata; |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 45 | |
| 46 | namespace { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 47 | |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 48 | auto StoreResultCallback(CommandResult* dest) { |
| 49 | return [dest](const CommandResult& result) { *dest = result; }; |
| 50 | } |
| 51 | } // namespace |
Alex Deymo | 44348e0 | 2016-07-29 16:22:26 -0700 | [diff] [blame] | 52 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 53 | namespace chromeos_update_engine { |
| 54 | |
| 55 | namespace boot_control { |
| 56 | |
| 57 | // Factory defined in boot_control.h. |
| 58 | std::unique_ptr<BootControlInterface> CreateBootControl() { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 59 | auto boot_control = std::make_unique<BootControlAndroid>(); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 60 | if (!boot_control->Init()) { |
| 61 | return nullptr; |
| 62 | } |
Alex Vakulenko | ce8c8ee | 2016-04-08 08:59:26 -0700 | [diff] [blame] | 63 | return std::move(boot_control); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace boot_control |
| 67 | |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 68 | bool BootControlAndroid::Init() { |
Chris Phoenix | afde8e8 | 2017-01-17 23:14:58 -0800 | [diff] [blame] | 69 | module_ = IBootControl::getService(); |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 70 | if (module_ == nullptr) { |
Steven Moreland | 927e00d | 2017-01-04 12:58:40 -0800 | [diff] [blame] | 71 | LOG(ERROR) << "Error getting bootctrl HIDL module."; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | |
Steven Moreland | 927e00d | 2017-01-04 12:58:40 -0800 | [diff] [blame] | 75 | LOG(INFO) << "Loaded boot control hidl hal."; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 76 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 77 | dynamic_control_ = std::make_unique<DynamicPartitionControlAndroid>(); |
| 78 | |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 79 | return true; |
| 80 | } |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 81 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 82 | void BootControlAndroid::Cleanup() { |
| 83 | dynamic_control_->Cleanup(); |
| 84 | } |
| 85 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 86 | unsigned int BootControlAndroid::GetNumSlots() const { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 87 | return module_->getNumberSlots(); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 91 | return module_->getCurrentSlot(); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 94 | bool BootControlAndroid::GetSuffix(Slot slot, string* suffix) const { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 95 | auto store_suffix_cb = [&suffix](hidl_string cb_suffix) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 96 | *suffix = cb_suffix.c_str(); |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 97 | }; |
| 98 | Return<void> ret = module_->getSuffix(slot, store_suffix_cb); |
| 99 | |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 100 | if (!ret.isOk()) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 101 | LOG(ERROR) << "boot_control impl returned no suffix for slot " |
| 102 | << SlotName(slot); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 103 | return false; |
| 104 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 105 | return true; |
| 106 | } |
| 107 | |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame^] | 108 | namespace { |
| 109 | |
| 110 | enum class DynamicPartitionDeviceStatus { |
| 111 | SUCCESS, |
| 112 | ERROR, |
| 113 | TRY_STATIC, |
| 114 | }; |
| 115 | |
| 116 | DynamicPartitionDeviceStatus GetDynamicPartitionDevice( |
| 117 | DynamicPartitionControlInterface* dynamic_control, |
| 118 | const string& super_device, |
| 119 | const string& partition_name_suffix, |
| 120 | Slot slot, |
| 121 | string* device) { |
| 122 | auto builder = dynamic_control->LoadMetadataBuilder(super_device, slot); |
| 123 | |
| 124 | if (builder == nullptr) { |
| 125 | if (!dynamic_control->IsDynamicPartitionsEnabled()) { |
| 126 | return DynamicPartitionDeviceStatus::TRY_STATIC; |
| 127 | } |
| 128 | LOG(ERROR) << "No metadata in slot " |
| 129 | << BootControlInterface::SlotName(slot); |
| 130 | return DynamicPartitionDeviceStatus::ERROR; |
| 131 | } |
| 132 | |
| 133 | if (builder->FindPartition(partition_name_suffix) == nullptr) { |
| 134 | LOG(INFO) << partition_name_suffix |
| 135 | << " is not in super partition metadata."; |
| 136 | return DynamicPartitionDeviceStatus::TRY_STATIC; |
| 137 | } |
| 138 | |
| 139 | DmDeviceState state = dynamic_control->GetState(partition_name_suffix); |
| 140 | |
| 141 | if (state == DmDeviceState::ACTIVE) { |
| 142 | if (dynamic_control->GetDmDevicePathByName(partition_name_suffix, device)) { |
| 143 | LOG(INFO) << partition_name_suffix |
| 144 | << " is mapped on device mapper: " << *device; |
| 145 | return DynamicPartitionDeviceStatus::SUCCESS; |
| 146 | } |
| 147 | LOG(ERROR) << partition_name_suffix << " is mapped but path is unknown."; |
| 148 | return DynamicPartitionDeviceStatus::ERROR; |
| 149 | } |
| 150 | |
| 151 | // DeltaPerformer calls InitPartitionMetadata before calling |
| 152 | // InstallPlan::LoadPartitionsFromSlots. After InitPartitionMetadata, |
| 153 | // the target partition must be re-mapped with force_writable == true. |
| 154 | // Hence, if it is not mapped, we assume it is a source partition and |
| 155 | // map it without force_writable. |
| 156 | if (state == DmDeviceState::INVALID) { |
| 157 | if (dynamic_control->MapPartitionOnDeviceMapper(super_device, |
| 158 | partition_name_suffix, |
| 159 | slot, |
| 160 | false /* force_writable */, |
| 161 | device)) { |
| 162 | return DynamicPartitionDeviceStatus::SUCCESS; |
| 163 | } |
| 164 | return DynamicPartitionDeviceStatus::ERROR; |
| 165 | } |
| 166 | |
| 167 | LOG(ERROR) << partition_name_suffix |
| 168 | << " is mapped on device mapper but state is unknown: " |
| 169 | << static_cast<std::underlying_type_t<DmDeviceState>>(state); |
| 170 | return DynamicPartitionDeviceStatus::ERROR; |
| 171 | } |
| 172 | } // namespace |
| 173 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 174 | bool BootControlAndroid::GetPartitionDevice(const string& partition_name, |
| 175 | Slot slot, |
| 176 | string* device) const { |
| 177 | string suffix; |
| 178 | if (!GetSuffix(slot, &suffix)) { |
| 179 | return false; |
| 180 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame^] | 181 | const string partition_name_suffix = partition_name + suffix; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 182 | |
| 183 | string device_dir_str; |
| 184 | if (!dynamic_control_->GetDeviceDir(&device_dir_str)) { |
| 185 | return false; |
| 186 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame^] | 187 | base::FilePath device_dir(device_dir_str); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 188 | |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame^] | 189 | string super_device = |
| 190 | device_dir.Append(fs_mgr_get_super_partition_name()).value(); |
| 191 | switch (GetDynamicPartitionDevice(dynamic_control_.get(), |
| 192 | super_device, |
| 193 | partition_name_suffix, |
| 194 | slot, |
| 195 | device)) { |
| 196 | case DynamicPartitionDeviceStatus::SUCCESS: |
| 197 | return true; |
| 198 | case DynamicPartitionDeviceStatus::TRY_STATIC: |
| 199 | break; |
| 200 | case DynamicPartitionDeviceStatus::ERROR: // fallthrough |
| 201 | default: |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | base::FilePath path = device_dir.Append(partition_name_suffix); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 206 | if (!dynamic_control_->DeviceExists(path.value())) { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 207 | LOG(ERROR) << "Device file " << path.value() << " does not exist."; |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | *device = path.value(); |
| 212 | return true; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | bool BootControlAndroid::IsSlotBootable(Slot slot) const { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 216 | Return<BoolResult> ret = module_->isSlotBootable(slot); |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 217 | if (!ret.isOk()) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 218 | LOG(ERROR) << "Unable to determine if slot " << SlotName(slot) |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 219 | << " is bootable: " |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 220 | << ret.description(); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 221 | return false; |
| 222 | } |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 223 | if (ret == BoolResult::INVALID_SLOT) { |
| 224 | LOG(ERROR) << "Invalid slot: " << SlotName(slot); |
| 225 | return false; |
| 226 | } |
| 227 | return ret == BoolResult::TRUE; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | bool BootControlAndroid::MarkSlotUnbootable(Slot slot) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 231 | CommandResult result; |
| 232 | auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result)); |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 233 | if (!ret.isOk()) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 234 | LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot " |
| 235 | << SlotName(slot) << ": " |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 236 | << ret.description(); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 237 | return false; |
| 238 | } |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 239 | if (!result.success) { |
| 240 | LOG(ERROR) << "Unable to mark slot " << SlotName(slot) |
| 241 | << " as unbootable: " << result.errMsg.c_str(); |
| 242 | } |
| 243 | return result.success; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 246 | bool BootControlAndroid::SetActiveBootSlot(Slot slot) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 247 | CommandResult result; |
| 248 | auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result)); |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 249 | if (!ret.isOk()) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 250 | LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot) |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 251 | << ": " << ret.description(); |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 252 | return false; |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame] | 253 | } |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 254 | if (!result.success) { |
| 255 | LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot) |
| 256 | << ": " << result.errMsg.c_str(); |
| 257 | } |
| 258 | return result.success; |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 261 | bool BootControlAndroid::MarkBootSuccessfulAsync( |
| 262 | base::Callback<void(bool)> callback) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 263 | CommandResult result; |
| 264 | auto ret = module_->markBootSuccessful(StoreResultCallback(&result)); |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 265 | if (!ret.isOk()) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 266 | LOG(ERROR) << "Unable to call MarkBootSuccessful: " |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 267 | << ret.description(); |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 268 | return false; |
| 269 | } |
| 270 | if (!result.success) { |
| 271 | LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str(); |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 272 | } |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 273 | return brillo::MessageLoop::current()->PostTask( |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 274 | FROM_HERE, base::Bind(callback, result.success)) != |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 275 | brillo::MessageLoop::kTaskIdNull; |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 278 | namespace { |
| 279 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 280 | bool InitPartitionMetadataInternal( |
| 281 | DynamicPartitionControlInterface* dynamic_control, |
| 282 | const string& super_device, |
| 283 | Slot source_slot, |
| 284 | Slot target_slot, |
| 285 | const string& target_suffix, |
| 286 | const PartitionMetadata& partition_metadata) { |
| 287 | auto builder = |
| 288 | dynamic_control->LoadMetadataBuilder(super_device, source_slot); |
| 289 | if (builder == nullptr) { |
| 290 | // TODO(elsk): allow reconstructing metadata from partition_metadata |
| 291 | // in recovery sideload. |
| 292 | LOG(ERROR) << "No metadata at " |
| 293 | << BootControlInterface::SlotName(source_slot); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 297 | std::vector<string> groups = builder->ListGroups(); |
| 298 | for (const auto& group_name : groups) { |
| 299 | if (base::EndsWith( |
| 300 | group_name, target_suffix, base::CompareCase::SENSITIVE)) { |
| 301 | LOG(INFO) << "Removing group " << group_name; |
| 302 | builder->RemoveGroupAndPartitions(group_name); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | uint64_t total_size = 0; |
| 307 | for (const auto& group : partition_metadata.groups) { |
| 308 | total_size += group.size; |
| 309 | } |
| 310 | |
| 311 | if (total_size > (builder->AllocatableSpace() / 2)) { |
| 312 | LOG(ERROR) |
| 313 | << "The maximum size of all groups with suffix " << target_suffix |
| 314 | << " (" << total_size |
| 315 | << ") has exceeded half of allocatable space for dynamic partitions " |
| 316 | << (builder->AllocatableSpace() / 2) << "."; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 320 | for (const auto& group : partition_metadata.groups) { |
| 321 | auto group_name_suffix = group.name + target_suffix; |
| 322 | if (!builder->AddGroup(group_name_suffix, group.size)) { |
| 323 | LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size " |
| 324 | << group.size; |
| 325 | return false; |
| 326 | } |
| 327 | LOG(INFO) << "Added group " << group_name_suffix << " with size " |
| 328 | << group.size; |
| 329 | |
| 330 | for (const auto& partition : group.partitions) { |
| 331 | auto parition_name_suffix = partition.name + target_suffix; |
| 332 | Partition* p = builder->AddPartition( |
| 333 | parition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY); |
| 334 | if (!p) { |
| 335 | LOG(ERROR) << "Cannot add partition " << parition_name_suffix |
| 336 | << " to group " << group_name_suffix; |
| 337 | return false; |
| 338 | } |
| 339 | if (!builder->ResizePartition(p, partition.size)) { |
| 340 | LOG(ERROR) << "Cannot resize partition " << parition_name_suffix |
| 341 | << " to size " << partition.size << ". Not enough space?"; |
| 342 | return false; |
| 343 | } |
| 344 | LOG(INFO) << "Added partition " << parition_name_suffix << " to group " |
| 345 | << group_name_suffix << " with size " << partition.size; |
| 346 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 349 | return dynamic_control->StoreMetadata( |
| 350 | super_device, builder.get(), target_slot); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 353 | // Unmap all partitions, and remap partitions as writable. |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 354 | bool Remap(DynamicPartitionControlInterface* dynamic_control, |
| 355 | const string& super_device, |
| 356 | Slot target_slot, |
| 357 | const string& target_suffix, |
| 358 | const PartitionMetadata& partition_metadata) { |
| 359 | for (const auto& group : partition_metadata.groups) { |
| 360 | for (const auto& partition : group.partitions) { |
| 361 | if (!dynamic_control->UnmapPartitionOnDeviceMapper( |
| 362 | partition.name + target_suffix, true /* wait */)) { |
| 363 | return false; |
| 364 | } |
| 365 | if (partition.size == 0) { |
| 366 | continue; |
| 367 | } |
| 368 | string map_path; |
| 369 | if (!dynamic_control->MapPartitionOnDeviceMapper( |
| 370 | super_device, |
| 371 | partition.name + target_suffix, |
| 372 | target_slot, |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 373 | true /* force writable */, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 374 | &map_path)) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 375 | return false; |
| 376 | } |
| 377 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 378 | } |
| 379 | return true; |
| 380 | } |
| 381 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 382 | } // namespace |
| 383 | |
| 384 | bool BootControlAndroid::InitPartitionMetadata( |
Yifan Hong | 6b0a984 | 2018-10-16 16:54:18 -0700 | [diff] [blame] | 385 | Slot target_slot, const PartitionMetadata& partition_metadata) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 386 | if (!dynamic_control_->IsDynamicPartitionsEnabled()) { |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | string device_dir_str; |
| 391 | if (!dynamic_control_->GetDeviceDir(&device_dir_str)) { |
| 392 | return false; |
| 393 | } |
| 394 | base::FilePath device_dir(device_dir_str); |
David Anderson | d63cb3c | 2018-10-01 14:15:00 -0700 | [diff] [blame] | 395 | string super_device = |
| 396 | device_dir.Append(fs_mgr_get_super_partition_name()).value(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 397 | |
| 398 | Slot current_slot = GetCurrentSlot(); |
| 399 | if (target_slot == current_slot) { |
| 400 | LOG(ERROR) << "Cannot call InitPartitionMetadata on current slot."; |
| 401 | return false; |
| 402 | } |
| 403 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 404 | string target_suffix; |
| 405 | if (!GetSuffix(target_slot, &target_suffix)) { |
| 406 | return false; |
| 407 | } |
| 408 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 409 | if (!InitPartitionMetadataInternal(dynamic_control_.get(), |
| 410 | super_device, |
| 411 | current_slot, |
| 412 | target_slot, |
| 413 | target_suffix, |
| 414 | partition_metadata)) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 415 | return false; |
| 416 | } |
| 417 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 418 | if (!Remap(dynamic_control_.get(), |
| 419 | super_device, |
| 420 | target_slot, |
| 421 | target_suffix, |
| 422 | partition_metadata)) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 423 | return false; |
| 424 | } |
| 425 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 426 | return true; |
| 427 | } |
| 428 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 429 | } // namespace chromeos_update_engine |