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