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> |
Mark Salyzyn | 62b42c8 | 2018-12-05 13:33:17 -0800 | [diff] [blame] | 29 | #include <fs_mgr_overlayfs.h> |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 30 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 31 | #include "update_engine/common/utils.h" |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 32 | #include "update_engine/dynamic_partition_control_android.h" |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 33 | |
| 34 | using std::string; |
| 35 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 36 | using android::dm::DmDeviceState; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 37 | using android::fs_mgr::Partition; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 38 | using android::hardware::hidl_string; |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 39 | using android::hardware::Return; |
| 40 | using android::hardware::boot::V1_0::BoolResult; |
| 41 | using android::hardware::boot::V1_0::CommandResult; |
| 42 | using android::hardware::boot::V1_0::IBootControl; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 43 | using Slot = chromeos_update_engine::BootControlInterface::Slot; |
Yifan Hong | 6b0a984 | 2018-10-16 16:54:18 -0700 | [diff] [blame] | 44 | using PartitionMetadata = |
| 45 | chromeos_update_engine::BootControlInterface::PartitionMetadata; |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 46 | |
| 47 | namespace { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 48 | |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 49 | auto StoreResultCallback(CommandResult* dest) { |
| 50 | return [dest](const CommandResult& result) { *dest = result; }; |
| 51 | } |
| 52 | } // namespace |
Alex Deymo | 44348e0 | 2016-07-29 16:22:26 -0700 | [diff] [blame] | 53 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 54 | namespace chromeos_update_engine { |
| 55 | |
| 56 | namespace boot_control { |
| 57 | |
| 58 | // Factory defined in boot_control.h. |
| 59 | std::unique_ptr<BootControlInterface> CreateBootControl() { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 60 | auto boot_control = std::make_unique<BootControlAndroid>(); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 61 | if (!boot_control->Init()) { |
| 62 | return nullptr; |
| 63 | } |
Alex Vakulenko | ce8c8ee | 2016-04-08 08:59:26 -0700 | [diff] [blame] | 64 | return std::move(boot_control); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | } // namespace boot_control |
| 68 | |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 69 | bool BootControlAndroid::Init() { |
Chris Phoenix | afde8e8 | 2017-01-17 23:14:58 -0800 | [diff] [blame] | 70 | module_ = IBootControl::getService(); |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 71 | if (module_ == nullptr) { |
Steven Moreland | 927e00d | 2017-01-04 12:58:40 -0800 | [diff] [blame] | 72 | LOG(ERROR) << "Error getting bootctrl HIDL module."; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
Steven Moreland | 927e00d | 2017-01-04 12:58:40 -0800 | [diff] [blame] | 76 | LOG(INFO) << "Loaded boot control hidl hal."; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 77 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 78 | dynamic_control_ = std::make_unique<DynamicPartitionControlAndroid>(); |
| 79 | |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 80 | return true; |
| 81 | } |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 82 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 83 | void BootControlAndroid::Cleanup() { |
| 84 | dynamic_control_->Cleanup(); |
| 85 | } |
| 86 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 87 | unsigned int BootControlAndroid::GetNumSlots() const { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 88 | return module_->getNumberSlots(); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 92 | return module_->getCurrentSlot(); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 95 | bool BootControlAndroid::GetSuffix(Slot slot, string* suffix) const { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 96 | auto store_suffix_cb = [&suffix](hidl_string cb_suffix) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 97 | *suffix = cb_suffix.c_str(); |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 98 | }; |
| 99 | Return<void> ret = module_->getSuffix(slot, store_suffix_cb); |
| 100 | |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 101 | if (!ret.isOk()) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 102 | LOG(ERROR) << "boot_control impl returned no suffix for slot " |
| 103 | << SlotName(slot); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 104 | return false; |
| 105 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 109 | bool BootControlAndroid::IsSuperBlockDevice( |
| 110 | const base::FilePath& device_dir, |
| 111 | Slot slot, |
| 112 | const string& partition_name_suffix) const { |
| 113 | string source_device = |
| 114 | device_dir.Append(fs_mgr_get_super_partition_name(slot)).value(); |
| 115 | auto source_metadata = dynamic_control_->LoadMetadataBuilder( |
| 116 | source_device, slot, BootControlInterface::kInvalidSlot); |
| 117 | return source_metadata->HasBlockDevice(partition_name_suffix); |
| 118 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 119 | |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 120 | BootControlAndroid::DynamicPartitionDeviceStatus |
| 121 | BootControlAndroid::GetDynamicPartitionDevice( |
| 122 | const base::FilePath& device_dir, |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 123 | const string& partition_name_suffix, |
| 124 | Slot slot, |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 125 | string* device) const { |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 126 | string super_device = |
| 127 | device_dir.Append(fs_mgr_get_super_partition_name(slot)).value(); |
| 128 | |
| 129 | auto builder = dynamic_control_->LoadMetadataBuilder( |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 130 | super_device, slot, BootControlInterface::kInvalidSlot); |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 131 | |
| 132 | if (builder == nullptr) { |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 133 | LOG(ERROR) << "No metadata in slot " |
| 134 | << BootControlInterface::SlotName(slot); |
| 135 | return DynamicPartitionDeviceStatus::ERROR; |
| 136 | } |
| 137 | |
| 138 | if (builder->FindPartition(partition_name_suffix) == nullptr) { |
| 139 | LOG(INFO) << partition_name_suffix |
| 140 | << " is not in super partition metadata."; |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 141 | |
| 142 | Slot current_slot = GetCurrentSlot(); |
| 143 | if (IsSuperBlockDevice(device_dir, current_slot, partition_name_suffix)) { |
| 144 | LOG(ERROR) << "The static partition " << partition_name_suffix |
| 145 | << " is a block device for current metadata (" |
| 146 | << fs_mgr_get_super_partition_name(current_slot) << ", slot " |
| 147 | << BootControlInterface::SlotName(current_slot) |
| 148 | << "). It cannot be used as a logical partition."; |
| 149 | return DynamicPartitionDeviceStatus::ERROR; |
| 150 | } |
| 151 | |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 152 | return DynamicPartitionDeviceStatus::TRY_STATIC; |
| 153 | } |
| 154 | |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 155 | DmDeviceState state = dynamic_control_->GetState(partition_name_suffix); |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 156 | |
Yifan Hong | 35410ed | 2018-12-05 03:30:10 +0000 | [diff] [blame] | 157 | // Device is mapped in the previous GetPartitionDevice() call. Just return |
| 158 | // the path. |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 159 | if (state == DmDeviceState::ACTIVE) { |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 160 | if (dynamic_control_->GetDmDevicePathByName(partition_name_suffix, |
| 161 | device)) { |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 162 | LOG(INFO) << partition_name_suffix |
| 163 | << " is mapped on device mapper: " << *device; |
| 164 | return DynamicPartitionDeviceStatus::SUCCESS; |
| 165 | } |
| 166 | LOG(ERROR) << partition_name_suffix << " is mapped but path is unknown."; |
| 167 | return DynamicPartitionDeviceStatus::ERROR; |
| 168 | } |
| 169 | |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 170 | if (state == DmDeviceState::INVALID) { |
Yifan Hong | 35410ed | 2018-12-05 03:30:10 +0000 | [diff] [blame] | 171 | bool force_writable = slot != GetCurrentSlot(); |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 172 | if (dynamic_control_->MapPartitionOnDeviceMapper(super_device, |
| 173 | partition_name_suffix, |
| 174 | slot, |
Yifan Hong | 35410ed | 2018-12-05 03:30:10 +0000 | [diff] [blame] | 175 | force_writable, |
Yifan Hong | 124a460 | 2018-11-14 13:17:01 -0800 | [diff] [blame] | 176 | device)) { |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 177 | return DynamicPartitionDeviceStatus::SUCCESS; |
| 178 | } |
| 179 | return DynamicPartitionDeviceStatus::ERROR; |
| 180 | } |
| 181 | |
| 182 | LOG(ERROR) << partition_name_suffix |
| 183 | << " is mapped on device mapper but state is unknown: " |
| 184 | << static_cast<std::underlying_type_t<DmDeviceState>>(state); |
| 185 | return DynamicPartitionDeviceStatus::ERROR; |
| 186 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 187 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 188 | bool BootControlAndroid::GetPartitionDevice(const string& partition_name, |
| 189 | Slot slot, |
| 190 | string* device) const { |
| 191 | string suffix; |
| 192 | if (!GetSuffix(slot, &suffix)) { |
| 193 | return false; |
| 194 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 195 | const string partition_name_suffix = partition_name + suffix; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 196 | |
| 197 | string device_dir_str; |
| 198 | if (!dynamic_control_->GetDeviceDir(&device_dir_str)) { |
| 199 | return false; |
| 200 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 201 | base::FilePath device_dir(device_dir_str); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 202 | |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 203 | // When looking up target partition devices, treat them as static if the |
| 204 | // current payload doesn't encode them as dynamic partitions. This may happen |
| 205 | // when applying a retrofit update on top of a dynamic-partitions-enabled |
| 206 | // build. |
| 207 | if (dynamic_control_->IsDynamicPartitionsEnabled() && |
| 208 | (slot == GetCurrentSlot() || is_target_dynamic_)) { |
| 209 | switch (GetDynamicPartitionDevice( |
| 210 | device_dir, partition_name_suffix, slot, device)) { |
| 211 | case DynamicPartitionDeviceStatus::SUCCESS: |
| 212 | return true; |
| 213 | case DynamicPartitionDeviceStatus::TRY_STATIC: |
| 214 | break; |
| 215 | case DynamicPartitionDeviceStatus::ERROR: // fallthrough |
| 216 | default: |
| 217 | return false; |
| 218 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | base::FilePath path = device_dir.Append(partition_name_suffix); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 222 | if (!dynamic_control_->DeviceExists(path.value())) { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 223 | LOG(ERROR) << "Device file " << path.value() << " does not exist."; |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | *device = path.value(); |
| 228 | return true; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | bool BootControlAndroid::IsSlotBootable(Slot slot) const { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 232 | Return<BoolResult> ret = module_->isSlotBootable(slot); |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 233 | if (!ret.isOk()) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 234 | LOG(ERROR) << "Unable to determine if slot " << SlotName(slot) |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 235 | << " is bootable: " << ret.description(); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 236 | return false; |
| 237 | } |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 238 | if (ret == BoolResult::INVALID_SLOT) { |
| 239 | LOG(ERROR) << "Invalid slot: " << SlotName(slot); |
| 240 | return false; |
| 241 | } |
| 242 | return ret == BoolResult::TRUE; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | bool BootControlAndroid::MarkSlotUnbootable(Slot slot) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 246 | CommandResult result; |
| 247 | auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result)); |
Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 248 | if (!ret.isOk()) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 249 | LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot " |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 250 | << SlotName(slot) << ": " << 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()) { |
Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 280 | LOG(ERROR) << "Unable to call MarkBootSuccessful: " << ret.description(); |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | if (!result.success) { |
| 284 | LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str(); |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 285 | } |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 286 | return brillo::MessageLoop::current()->PostTask( |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 287 | FROM_HERE, base::Bind(callback, result.success)) != |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 288 | brillo::MessageLoop::kTaskIdNull; |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 291 | namespace { |
| 292 | |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 293 | bool UpdatePartitionMetadata(DynamicPartitionControlInterface* dynamic_control, |
| 294 | Slot source_slot, |
| 295 | Slot target_slot, |
| 296 | const string& target_suffix, |
| 297 | const PartitionMetadata& partition_metadata) { |
| 298 | string device_dir_str; |
| 299 | if (!dynamic_control->GetDeviceDir(&device_dir_str)) { |
| 300 | return false; |
| 301 | } |
| 302 | base::FilePath device_dir(device_dir_str); |
| 303 | auto source_device = |
| 304 | device_dir.Append(fs_mgr_get_super_partition_name(source_slot)).value(); |
| 305 | |
Yifan Hong | 6e706b1 | 2018-11-09 16:50:51 -0800 | [diff] [blame] | 306 | auto builder = dynamic_control->LoadMetadataBuilder( |
Yifan Hong | b97e3ab | 2018-11-14 13:50:39 -0800 | [diff] [blame] | 307 | source_device, source_slot, target_slot); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 308 | if (builder == nullptr) { |
| 309 | // TODO(elsk): allow reconstructing metadata from partition_metadata |
| 310 | // in recovery sideload. |
| 311 | LOG(ERROR) << "No metadata at " |
| 312 | << BootControlInterface::SlotName(source_slot); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 313 | return false; |
| 314 | } |
| 315 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 316 | std::vector<string> groups = builder->ListGroups(); |
| 317 | for (const auto& group_name : groups) { |
| 318 | if (base::EndsWith( |
| 319 | group_name, target_suffix, base::CompareCase::SENSITIVE)) { |
| 320 | LOG(INFO) << "Removing group " << group_name; |
| 321 | builder->RemoveGroupAndPartitions(group_name); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | uint64_t total_size = 0; |
| 326 | for (const auto& group : partition_metadata.groups) { |
| 327 | total_size += group.size; |
| 328 | } |
| 329 | |
Yifan Hong | 6e38b35 | 2018-11-19 14:12:37 -0800 | [diff] [blame] | 330 | string expr; |
| 331 | uint64_t allocatable_space = builder->AllocatableSpace(); |
| 332 | if (!dynamic_control->IsDynamicPartitionsRetrofit()) { |
| 333 | allocatable_space /= 2; |
| 334 | expr = "half of "; |
| 335 | } |
| 336 | if (total_size > allocatable_space) { |
| 337 | LOG(ERROR) << "The maximum size of all groups with suffix " << target_suffix |
| 338 | << " (" << total_size << ") has exceeded " << expr |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 339 | << " allocatable space for dynamic partitions " |
Yifan Hong | 6e38b35 | 2018-11-19 14:12:37 -0800 | [diff] [blame] | 340 | << allocatable_space << "."; |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 344 | for (const auto& group : partition_metadata.groups) { |
| 345 | auto group_name_suffix = group.name + target_suffix; |
| 346 | if (!builder->AddGroup(group_name_suffix, group.size)) { |
| 347 | LOG(ERROR) << "Cannot add group " << group_name_suffix << " with size " |
| 348 | << group.size; |
| 349 | return false; |
| 350 | } |
| 351 | LOG(INFO) << "Added group " << group_name_suffix << " with size " |
| 352 | << group.size; |
| 353 | |
| 354 | for (const auto& partition : group.partitions) { |
Yifan Hong | a7897f3 | 2018-11-15 14:28:50 -0800 | [diff] [blame] | 355 | auto partition_name_suffix = partition.name + target_suffix; |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 356 | Partition* p = builder->AddPartition( |
Yifan Hong | a7897f3 | 2018-11-15 14:28:50 -0800 | [diff] [blame] | 357 | partition_name_suffix, group_name_suffix, LP_PARTITION_ATTR_READONLY); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 358 | if (!p) { |
Yifan Hong | a7897f3 | 2018-11-15 14:28:50 -0800 | [diff] [blame] | 359 | LOG(ERROR) << "Cannot add partition " << partition_name_suffix |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 360 | << " to group " << group_name_suffix; |
| 361 | return false; |
| 362 | } |
| 363 | if (!builder->ResizePartition(p, partition.size)) { |
Yifan Hong | a7897f3 | 2018-11-15 14:28:50 -0800 | [diff] [blame] | 364 | LOG(ERROR) << "Cannot resize partition " << partition_name_suffix |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 365 | << " to size " << partition.size << ". Not enough space?"; |
| 366 | return false; |
| 367 | } |
Yifan Hong | a7897f3 | 2018-11-15 14:28:50 -0800 | [diff] [blame] | 368 | LOG(INFO) << "Added partition " << partition_name_suffix << " to group " |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 369 | << group_name_suffix << " with size " << partition.size; |
| 370 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 373 | auto target_device = |
| 374 | device_dir.Append(fs_mgr_get_super_partition_name(target_slot)).value(); |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 375 | return dynamic_control->StoreMetadata( |
Yifan Hong | b97e3ab | 2018-11-14 13:50:39 -0800 | [diff] [blame] | 376 | target_device, builder.get(), target_slot); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 379 | bool UnmapTargetPartitions(DynamicPartitionControlInterface* dynamic_control, |
| 380 | const string& target_suffix, |
| 381 | const PartitionMetadata& partition_metadata) { |
Yifan Hong | d4db07e | 2018-10-18 17:46:27 -0700 | [diff] [blame] | 382 | for (const auto& group : partition_metadata.groups) { |
| 383 | for (const auto& partition : group.partitions) { |
| 384 | if (!dynamic_control->UnmapPartitionOnDeviceMapper( |
| 385 | partition.name + target_suffix, true /* wait */)) { |
| 386 | return false; |
| 387 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 388 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 389 | } |
| 390 | return true; |
| 391 | } |
| 392 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 393 | } // namespace |
| 394 | |
| 395 | bool BootControlAndroid::InitPartitionMetadata( |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 396 | Slot target_slot, |
| 397 | const PartitionMetadata& partition_metadata, |
| 398 | bool update_metadata) { |
Mark Salyzyn | 62b42c8 | 2018-12-05 13:33:17 -0800 | [diff] [blame] | 399 | if (fs_mgr_overlayfs_is_setup()) { |
| 400 | // Non DAP devices can use overlayfs as well. |
| 401 | LOG(WARNING) |
| 402 | << "overlayfs overrides are active and can interfere with our " |
| 403 | "resources.\n" |
| 404 | << "run adb enable-verity to deactivate if required and try again."; |
| 405 | } |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 406 | if (!dynamic_control_->IsDynamicPartitionsEnabled()) { |
| 407 | return true; |
| 408 | } |
| 409 | |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 410 | auto source_slot = GetCurrentSlot(); |
| 411 | if (target_slot == source_slot) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 412 | LOG(ERROR) << "Cannot call InitPartitionMetadata on current slot."; |
| 413 | return false; |
| 414 | } |
| 415 | |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 416 | // Although the current build supports dynamic partitions, the given payload |
| 417 | // doesn't use it for target partitions. This could happen when applying a |
| 418 | // retrofit update. Skip updating the partition metadata for the target slot. |
| 419 | is_target_dynamic_ = !partition_metadata.groups.empty(); |
| 420 | if (!is_target_dynamic_) { |
| 421 | return true; |
| 422 | } |
| 423 | |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 424 | if (!update_metadata) { |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | string target_suffix; |
| 429 | if (!GetSuffix(target_slot, &target_suffix)) { |
| 430 | return false; |
| 431 | } |
| 432 | |
Yifan Hong | 1d9077f | 2018-12-07 12:09:37 -0800 | [diff] [blame] | 433 | // Unmap all the target dynamic partitions because they would become |
| 434 | // inconsistent with the new metadata. |
| 435 | if (!UnmapTargetPartitions( |
| 436 | dynamic_control_.get(), target_suffix, partition_metadata)) { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 437 | return false; |
| 438 | } |
| 439 | |
Tao Bao | 3406c77 | 2019-01-02 15:34:35 -0800 | [diff] [blame] | 440 | return UpdatePartitionMetadata(dynamic_control_.get(), |
| 441 | source_slot, |
| 442 | target_slot, |
| 443 | target_suffix, |
| 444 | partition_metadata); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 447 | } // namespace chromeos_update_engine |