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 | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 108 | bool BootControlAndroid::IsSuperBlockDevice( |
| 109 | const base::FilePath& device_dir, |
| 110 | Slot slot, |
| 111 | const string& partition_name_suffix) const { |
| 112 | string source_device = |
| 113 | device_dir.Append(fs_mgr_get_super_partition_name(slot)).value(); |
| 114 | auto source_metadata = dynamic_control_->LoadMetadataBuilder( |
| 115 | source_device, slot, BootControlInterface::kInvalidSlot); |
| 116 | return source_metadata->HasBlockDevice(partition_name_suffix); |
| 117 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 118 | |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 119 | BootControlAndroid::DynamicPartitionDeviceStatus |
| 120 | BootControlAndroid::GetDynamicPartitionDevice( |
| 121 | const base::FilePath& device_dir, |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 122 | const string& partition_name_suffix, |
| 123 | Slot slot, |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 124 | string* device) const { |
| 125 | if (!dynamic_control_->IsDynamicPartitionsEnabled()) { |
Yifan Hong | 8cc1e9e | 2018-11-14 13:52:12 -0800 | [diff] [blame] | 126 | return DynamicPartitionDeviceStatus::TRY_STATIC; |
| 127 | } |
| 128 | |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 129 | string super_device = |
| 130 | device_dir.Append(fs_mgr_get_super_partition_name(slot)).value(); |
| 131 | |
| 132 | auto builder = dynamic_control_->LoadMetadataBuilder( |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 133 | super_device, slot, BootControlInterface::kInvalidSlot); |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 134 | |
| 135 | if (builder == nullptr) { |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 136 | LOG(ERROR) << "No metadata in slot " |
| 137 | << BootControlInterface::SlotName(slot); |
| 138 | return DynamicPartitionDeviceStatus::ERROR; |
| 139 | } |
| 140 | |
| 141 | if (builder->FindPartition(partition_name_suffix) == nullptr) { |
| 142 | LOG(INFO) << partition_name_suffix |
| 143 | << " is not in super partition metadata."; |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 144 | |
| 145 | Slot current_slot = GetCurrentSlot(); |
| 146 | if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) { |
| 147 | LOG(ERROR) << "The static partition " << partition_name_suffix |
| 148 | << " is a block device for current metadata (" |
| 149 | << fs_mgr_get_super_partition_name(current_slot) << ", slot " |
| 150 | << BootControlInterface::SlotName(current_slot) |
| 151 | << "). It cannot be used as a logical partition."; |
| 152 | return DynamicPartitionDeviceStatus::ERROR; |
| 153 | } |
| 154 | |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 155 | return DynamicPartitionDeviceStatus::TRY_STATIC; |
| 156 | } |
| 157 | |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 158 | DmDeviceState state = dynamic_control_->GetState(partition_name_suffix); |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 159 | |
| 160 | if (state == DmDeviceState::ACTIVE) { |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 161 | if (dynamic_control_->GetDmDevicePathByName(partition_name_suffix, |
| 162 | device)) { |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 163 | LOG(INFO) << partition_name_suffix |
| 164 | << " is mapped on device mapper: " << *device; |
| 165 | return DynamicPartitionDeviceStatus::SUCCESS; |
| 166 | } |
| 167 | LOG(ERROR) << partition_name_suffix << " is mapped but path is unknown."; |
| 168 | return DynamicPartitionDeviceStatus::ERROR; |
| 169 | } |
| 170 | |
| 171 | // DeltaPerformer calls InitPartitionMetadata before calling |
| 172 | // InstallPlan::LoadPartitionsFromSlots. After InitPartitionMetadata, |
| 173 | // the target partition must be re-mapped with force_writable == true. |
| 174 | // Hence, if it is not mapped, we assume it is a source partition and |
| 175 | // map it without force_writable. |
| 176 | if (state == DmDeviceState::INVALID) { |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 177 | if (dynamic_control_->MapPartitionOnDeviceMapper(super_device, |
| 178 | partition_name_suffix, |
| 179 | slot, |
| 180 | false /* force_writable */, |
| 181 | device)) { |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 182 | return DynamicPartitionDeviceStatus::SUCCESS; |
| 183 | } |
| 184 | return DynamicPartitionDeviceStatus::ERROR; |
| 185 | } |
| 186 | |
| 187 | LOG(ERROR) << partition_name_suffix |
| 188 | << " is mapped on device mapper but state is unknown: " |
| 189 | << static_cast<std::underlying_type_t<DmDeviceState>>(state); |
| 190 | return DynamicPartitionDeviceStatus::ERROR; |
| 191 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 192 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 193 | bool BootControlAndroid::GetPartitionDevice(const string& partition_name, |
| 194 | Slot slot, |
| 195 | string* device) const { |
| 196 | string suffix; |
| 197 | if (!GetSuffix(slot, &suffix)) { |
| 198 | return false; |
| 199 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 200 | const string partition_name_suffix = partition_name + suffix; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 201 | |
| 202 | string device_dir_str; |
| 203 | if (!dynamic_control_->GetDeviceDir(&device_dir_str)) { |
| 204 | return false; |
| 205 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 206 | base::FilePath device_dir(device_dir_str); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 207 | |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 208 | switch (GetDynamicPartitionDevice( |
| 209 | device_dir, partition_name_suffix, slot, device)) { |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 210 | case DynamicPartitionDeviceStatus::SUCCESS: |
| 211 | return true; |
| 212 | case DynamicPartitionDeviceStatus::TRY_STATIC: |
| 213 | break; |
| 214 | case DynamicPartitionDeviceStatus::ERROR: // fallthrough |
| 215 | default: |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | base::FilePath path = device_dir.Append(partition_name_suffix); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 220 | if (!dynamic_control_->DeviceExists(path.value())) { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 221 | LOG(ERROR) << "Device file " << path.value() << " does not exist."; |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | *device = path.value(); |
| 226 | return true; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | bool BootControlAndroid::IsSlotBootable(Slot slot) const { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 230 | Return<BoolResult> ret = module_->isSlotBootable(slot); |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 231 | if (!ret.isOk()) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 232 | LOG(ERROR) << "Unable to determine if slot " << SlotName(slot) |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 233 | << " is bootable: " |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 234 | << ret.description(); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 235 | return false; |
| 236 | } |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 237 | if (ret == BoolResult::INVALID_SLOT) { |
| 238 | LOG(ERROR) << "Invalid slot: " << SlotName(slot); |
| 239 | return false; |
| 240 | } |
| 241 | return ret == BoolResult::TRUE; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | bool BootControlAndroid::MarkSlotUnbootable(Slot slot) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 245 | CommandResult result; |
| 246 | auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result)); |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 247 | if (!ret.isOk()) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 248 | LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot " |
| 249 | << SlotName(slot) << ": " |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 250 | << ret.description(); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 251 | return false; |
| 252 | } |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 253 | if (!result.success) { |
| 254 | LOG(ERROR) << "Unable to mark slot " << SlotName(slot) |
| 255 | << " as unbootable: " << result.errMsg.c_str(); |
| 256 | } |
| 257 | return result.success; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 260 | bool BootControlAndroid::SetActiveBootSlot(Slot slot) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 261 | CommandResult result; |
| 262 | auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result)); |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 263 | if (!ret.isOk()) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 264 | LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot) |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 265 | << ": " << ret.description(); |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 266 | return false; |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame] | 267 | } |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 268 | if (!result.success) { |
| 269 | LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot) |
| 270 | << ": " << result.errMsg.c_str(); |
| 271 | } |
| 272 | return result.success; |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 275 | bool BootControlAndroid::MarkBootSuccessfulAsync( |
| 276 | base::Callback<void(bool)> callback) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 277 | CommandResult result; |
| 278 | auto ret = module_->markBootSuccessful(StoreResultCallback(&result)); |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 279 | if (!ret.isOk()) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 280 | LOG(ERROR) << "Unable to call MarkBootSuccessful: " |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 281 | << ret.description(); |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 282 | return false; |
| 283 | } |
| 284 | if (!result.success) { |
| 285 | LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str(); |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 286 | } |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 287 | return brillo::MessageLoop::current()->PostTask( |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 288 | FROM_HERE, base::Bind(callback, result.success)) != |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 289 | brillo::MessageLoop::kTaskIdNull; |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 292 | namespace { |
| 293 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 294 | bool InitPartitionMetadataInternal( |
| 295 | DynamicPartitionControlInterface* dynamic_control, |
Yifan Hong | b97e3ab | 2018-11-14 13:50:39 -0800 | [diff] [blame] | 296 | const string& source_device, |
| 297 | const string& target_device, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 298 | Slot source_slot, |
| 299 | Slot target_slot, |
| 300 | const string& target_suffix, |
| 301 | const PartitionMetadata& partition_metadata) { |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 302 | auto builder = dynamic_control->LoadMetadataBuilder( |
Yifan Hong | b97e3ab | 2018-11-14 13:50:39 -0800 | [diff] [blame] | 303 | source_device, source_slot, target_slot); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 304 | if (builder == nullptr) { |
| 305 | // TODO(elsk): allow reconstructing metadata from partition_metadata |
| 306 | // in recovery sideload. |
| 307 | LOG(ERROR) << "No metadata at " |
| 308 | << BootControlInterface::SlotName(source_slot); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 312 | std::vector<string> groups = builder->ListGroups(); |
| 313 | for (const auto& group_name : groups) { |
| 314 | if (base::EndsWith( |
| 315 | group_name, target_suffix, base::CompareCase::SENSITIVE)) { |
| 316 | LOG(INFO) << "Removing group " << group_name; |
| 317 | builder->RemoveGroupAndPartitions(group_name); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | uint64_t total_size = 0; |
| 322 | for (const auto& group : partition_metadata.groups) { |
| 323 | total_size += group.size; |
| 324 | } |
| 325 | |
Yifan Hong | 6e38b35 | 2018-11-19 14:12:37 -0800 | [diff] [blame^] | 326 | string expr; |
| 327 | uint64_t allocatable_space = builder->AllocatableSpace(); |
| 328 | if (!dynamic_control->IsDynamicPartitionsRetrofit()) { |
| 329 | allocatable_space /= 2; |
| 330 | expr = "half of "; |
| 331 | } |
| 332 | if (total_size > allocatable_space) { |
| 333 | LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix |
| 334 | << " (" << total_size << ") has exceeded " << expr |
| 335 | << "allocatable space for dynamic partitions " |
| 336 | << allocatable_space << "."; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 337 | return false; |
| 338 | } |
| 339 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 340 | for (const auto& group : partition_metadata.groups) { |
| 341 | auto group_name_suffix = group.name + target_suffix; |
| 342 | if (!builder->AddGroup(group_name_suffix, group.size)) { |
| 343 | LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size " |
| 344 | << group.size; |
| 345 | return false; |
| 346 | } |
| 347 | LOG(INFO) << "Added group " << group_name_suffix << " with size " |
| 348 | << group.size; |
| 349 | |
| 350 | for (const auto& partition : group.partitions) { |
Yifan Hong | a7897f3 | 2018-11-15 14:28:50 -0800 | [diff] [blame] | 351 | auto partition_name_suffix = partition.name + target_suffix; |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 352 | Partition* p = builder->AddPartition( |
Yifan Hong | a7897f3 | 2018-11-15 14:28:50 -0800 | [diff] [blame] | 353 | partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 354 | if (!p) { |
Yifan Hong | a7897f3 | 2018-11-15 14:28:50 -0800 | [diff] [blame] | 355 | LOG(ERROR) << "Cannot add partition " << partition_name_suffix |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 356 | << " to group " << group_name_suffix; |
| 357 | return false; |
| 358 | } |
| 359 | if (!builder->ResizePartition(p, partition.size)) { |
Yifan Hong | a7897f3 | 2018-11-15 14:28:50 -0800 | [diff] [blame] | 360 | LOG(ERROR) << "Cannot resize partition " << partition_name_suffix |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 361 | << " to size " << partition.size << ". Not enough space?"; |
| 362 | return false; |
| 363 | } |
Yifan Hong | a7897f3 | 2018-11-15 14:28:50 -0800 | [diff] [blame] | 364 | LOG(INFO) << "Added partition " << partition_name_suffix << " to group " |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 365 | << group_name_suffix << " with size " << partition.size; |
| 366 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 369 | return dynamic_control->StoreMetadata( |
Yifan Hong | b97e3ab | 2018-11-14 13:50:39 -0800 | [diff] [blame] | 370 | target_device, builder.get(), target_slot); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 373 | // Unmap all partitions, and remap partitions as writable. |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 374 | bool Remap(DynamicPartitionControlInterface* dynamic_control, |
Yifan Hong | b97e3ab | 2018-11-14 13:50:39 -0800 | [diff] [blame] | 375 | const string& target_device, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 376 | Slot target_slot, |
| 377 | const string& target_suffix, |
| 378 | const PartitionMetadata& partition_metadata) { |
| 379 | for (const auto& group : partition_metadata.groups) { |
| 380 | for (const auto& partition : group.partitions) { |
| 381 | if (!dynamic_control->UnmapPartitionOnDeviceMapper( |
| 382 | partition.name + target_suffix, true /* wait */)) { |
| 383 | return false; |
| 384 | } |
| 385 | if (partition.size == 0) { |
| 386 | continue; |
| 387 | } |
| 388 | string map_path; |
| 389 | if (!dynamic_control->MapPartitionOnDeviceMapper( |
Yifan Hong | b97e3ab | 2018-11-14 13:50:39 -0800 | [diff] [blame] | 390 | target_device, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 391 | partition.name + target_suffix, |
| 392 | target_slot, |
Yifan Hong | af65ef1 | 2018-10-29 11:09:06 -0700 | [diff] [blame] | 393 | true /* force writable */, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 394 | &map_path)) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 395 | return false; |
| 396 | } |
| 397 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 398 | } |
| 399 | return true; |
| 400 | } |
| 401 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 402 | } // namespace |
| 403 | |
| 404 | bool BootControlAndroid::InitPartitionMetadata( |
Yifan Hong | 6b0a984 | 2018-10-16 16:54:18 -0700 | [diff] [blame] | 405 | Slot target_slot, const PartitionMetadata& partition_metadata) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 406 | if (!dynamic_control_->IsDynamicPartitionsEnabled()) { |
| 407 | return true; |
| 408 | } |
| 409 | |
| 410 | string device_dir_str; |
| 411 | if (!dynamic_control_->GetDeviceDir(&device_dir_str)) { |
| 412 | return false; |
| 413 | } |
| 414 | base::FilePath device_dir(device_dir_str); |
Yifan Hong | b97e3ab | 2018-11-14 13:50:39 -0800 | [diff] [blame] | 415 | string target_device = |
| 416 | device_dir.Append(fs_mgr_get_super_partition_name(target_slot)).value(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 417 | |
| 418 | Slot current_slot = GetCurrentSlot(); |
| 419 | if (target_slot == current_slot) { |
| 420 | LOG(ERROR) << "Cannot call InitPartitionMetadata on current slot."; |
| 421 | return false; |
| 422 | } |
Yifan Hong | b97e3ab | 2018-11-14 13:50:39 -0800 | [diff] [blame] | 423 | string source_device = |
| 424 | device_dir.Append(fs_mgr_get_super_partition_name(current_slot)).value(); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 425 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 426 | string target_suffix; |
| 427 | if (!GetSuffix(target_slot, &target_suffix)) { |
| 428 | return false; |
| 429 | } |
| 430 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 431 | if (!InitPartitionMetadataInternal(dynamic_control_.get(), |
Yifan Hong | b97e3ab | 2018-11-14 13:50:39 -0800 | [diff] [blame] | 432 | source_device, |
| 433 | target_device, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 434 | current_slot, |
| 435 | target_slot, |
| 436 | target_suffix, |
| 437 | partition_metadata)) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 438 | return false; |
| 439 | } |
| 440 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 441 | if (!Remap(dynamic_control_.get(), |
Yifan Hong | b97e3ab | 2018-11-14 13:50:39 -0800 | [diff] [blame] | 442 | target_device, |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 443 | target_slot, |
| 444 | target_suffix, |
| 445 | partition_metadata)) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 446 | return false; |
| 447 | } |
| 448 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 449 | return true; |
| 450 | } |
| 451 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 452 | } // namespace chromeos_update_engine |