| 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 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/aosp/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 |  | 
| Kelvin Zhang | cb419e6 | 2021-06-16 13:56:47 -0400 | [diff] [blame] | 23 | #include <android/hardware/boot/1.2/IBootControl.h> | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 24 | #include <base/bind.h> | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 25 | #include <base/logging.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> | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 28 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 29 | #include "update_engine/aosp/dynamic_partition_control_android.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 30 | #include "update_engine/common/utils.h" | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 31 |  | 
|  | 32 | using std::string; | 
|  | 33 |  | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 34 | using android::hardware::Return; | 
|  | 35 | using android::hardware::boot::V1_0::BoolResult; | 
|  | 36 | using android::hardware::boot::V1_0::CommandResult; | 
|  | 37 | using android::hardware::boot::V1_0::IBootControl; | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 38 | using Slot = chromeos_update_engine::BootControlInterface::Slot; | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 39 |  | 
|  | 40 | namespace { | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 41 |  | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 42 | auto StoreResultCallback(CommandResult* dest) { | 
|  | 43 | return [dest](const CommandResult& result) { *dest = result; }; | 
|  | 44 | } | 
|  | 45 | }  // namespace | 
| Alex Deymo | 44348e0 | 2016-07-29 16:22:26 -0700 | [diff] [blame] | 46 |  | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 47 | namespace chromeos_update_engine { | 
|  | 48 |  | 
|  | 49 | namespace boot_control { | 
|  | 50 |  | 
|  | 51 | // Factory defined in boot_control.h. | 
|  | 52 | std::unique_ptr<BootControlInterface> CreateBootControl() { | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 53 | auto boot_control = std::make_unique<BootControlAndroid>(); | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 54 | if (!boot_control->Init()) { | 
|  | 55 | return nullptr; | 
|  | 56 | } | 
| Alex Vakulenko | ce8c8ee | 2016-04-08 08:59:26 -0700 | [diff] [blame] | 57 | return std::move(boot_control); | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
|  | 60 | }  // namespace boot_control | 
|  | 61 |  | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 62 | bool BootControlAndroid::Init() { | 
| Chris Phoenix | afde8e8 | 2017-01-17 23:14:58 -0800 | [diff] [blame] | 63 | module_ = IBootControl::getService(); | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 64 | if (module_ == nullptr) { | 
| Steven Moreland | 927e00d | 2017-01-04 12:58:40 -0800 | [diff] [blame] | 65 | LOG(ERROR) << "Error getting bootctrl HIDL module."; | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 66 | return false; | 
|  | 67 | } | 
|  | 68 |  | 
| Steven Moreland | 927e00d | 2017-01-04 12:58:40 -0800 | [diff] [blame] | 69 | LOG(INFO) << "Loaded boot control hidl hal."; | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 70 |  | 
| Kelvin Zhang | ebd115e | 2021-03-08 16:10:25 -0500 | [diff] [blame] | 71 | dynamic_control_ = | 
|  | 72 | std::make_unique<DynamicPartitionControlAndroid>(GetCurrentSlot()); | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 73 |  | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 74 | return true; | 
|  | 75 | } | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 76 |  | 
|  | 77 | unsigned int BootControlAndroid::GetNumSlots() const { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 78 | return module_->getNumberSlots(); | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
|  | 81 | BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 82 | return module_->getCurrentSlot(); | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 85 | bool BootControlAndroid::GetPartitionDevice(const std::string& partition_name, | 
|  | 86 | BootControlInterface::Slot slot, | 
|  | 87 | bool not_in_payload, | 
|  | 88 | std::string* device, | 
|  | 89 | bool* is_dynamic) const { | 
|  | 90 | return dynamic_control_->GetPartitionDevice(partition_name, | 
|  | 91 | slot, | 
|  | 92 | GetCurrentSlot(), | 
|  | 93 | not_in_payload, | 
|  | 94 | device, | 
|  | 95 | is_dynamic); | 
|  | 96 | } | 
| Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 97 |  | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 98 | bool BootControlAndroid::GetPartitionDevice(const string& partition_name, | 
| Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 99 | BootControlInterface::Slot slot, | 
| Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 100 | string* device) const { | 
| Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 101 | return GetPartitionDevice( | 
|  | 102 | partition_name, slot, false /* not_in_payload */, device, nullptr); | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 103 | } | 
|  | 104 |  | 
|  | 105 | bool BootControlAndroid::IsSlotBootable(Slot slot) const { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 106 | Return<BoolResult> ret = module_->isSlotBootable(slot); | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 107 | if (!ret.isOk()) { | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 108 | LOG(ERROR) << "Unable to determine if slot " << SlotName(slot) | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 109 | << " is bootable: " << ret.description(); | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 110 | return false; | 
|  | 111 | } | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 112 | if (ret == BoolResult::INVALID_SLOT) { | 
|  | 113 | LOG(ERROR) << "Invalid slot: " << SlotName(slot); | 
|  | 114 | return false; | 
|  | 115 | } | 
|  | 116 | return ret == BoolResult::TRUE; | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
|  | 119 | bool BootControlAndroid::MarkSlotUnbootable(Slot slot) { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 120 | CommandResult result; | 
|  | 121 | auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result)); | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 122 | if (!ret.isOk()) { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 123 | LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot " | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 124 | << SlotName(slot) << ": " << ret.description(); | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 125 | return false; | 
|  | 126 | } | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 127 | if (!result.success) { | 
|  | 128 | LOG(ERROR) << "Unable to mark slot " << SlotName(slot) | 
|  | 129 | << " as unbootable: " << result.errMsg.c_str(); | 
|  | 130 | } | 
|  | 131 | return result.success; | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 134 | bool BootControlAndroid::SetActiveBootSlot(Slot slot) { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 135 | CommandResult result; | 
|  | 136 | auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result)); | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 137 | if (!ret.isOk()) { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 138 | LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot) | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 139 | << ": " << ret.description(); | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 140 | return false; | 
| Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame] | 141 | } | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 142 | if (!result.success) { | 
|  | 143 | LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot) | 
|  | 144 | << ": " << result.errMsg.c_str(); | 
|  | 145 | } | 
|  | 146 | return result.success; | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 147 | } | 
|  | 148 |  | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 149 | bool BootControlAndroid::MarkBootSuccessfulAsync( | 
|  | 150 | base::Callback<void(bool)> callback) { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 151 | CommandResult result; | 
|  | 152 | auto ret = module_->markBootSuccessful(StoreResultCallback(&result)); | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 153 | if (!ret.isOk()) { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 154 | LOG(ERROR) << "Unable to call MarkBootSuccessful: " << ret.description(); | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 155 | return false; | 
|  | 156 | } | 
|  | 157 | if (!result.success) { | 
|  | 158 | LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str(); | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 159 | } | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 160 | return brillo::MessageLoop::current()->PostTask( | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 161 | FROM_HERE, base::Bind(callback, result.success)) != | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 162 | brillo::MessageLoop::kTaskIdNull; | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
| Yifan Hong | f141594 | 2020-02-24 18:34:49 -0800 | [diff] [blame] | 165 | bool BootControlAndroid::IsSlotMarkedSuccessful( | 
|  | 166 | BootControlInterface::Slot slot) const { | 
|  | 167 | Return<BoolResult> ret = module_->isSlotMarkedSuccessful(slot); | 
|  | 168 | CommandResult result; | 
|  | 169 | if (!ret.isOk()) { | 
|  | 170 | LOG(ERROR) << "Unable to determine if slot " << SlotName(slot) | 
|  | 171 | << " is marked successful: " << ret.description(); | 
|  | 172 | return false; | 
|  | 173 | } | 
|  | 174 | if (ret == BoolResult::INVALID_SLOT) { | 
|  | 175 | LOG(ERROR) << "Invalid slot: " << SlotName(slot); | 
|  | 176 | return false; | 
|  | 177 | } | 
|  | 178 | return ret == BoolResult::TRUE; | 
|  | 179 | } | 
|  | 180 |  | 
| Kelvin Zhang | cb419e6 | 2021-06-16 13:56:47 -0400 | [diff] [blame] | 181 | Slot BootControlAndroid::GetActiveBootSlot() { | 
|  | 182 | namespace V1_2 = android::hardware::boot::V1_2; | 
|  | 183 | using android::sp; | 
|  | 184 | sp<V1_2::IBootControl> v1_2_module = V1_2::IBootControl::castFrom(module_); | 
|  | 185 | if (v1_2_module != nullptr) { | 
|  | 186 | return v1_2_module->getActiveBootSlot(); | 
|  | 187 | } | 
|  | 188 | LOG(WARNING) << "BootControl module version is lower than 1.2, " | 
|  | 189 | << __FUNCTION__ << " failed"; | 
|  | 190 | return kInvalidSlot; | 
|  | 191 | } | 
|  | 192 |  | 
| Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame] | 193 | DynamicPartitionControlInterface* | 
|  | 194 | BootControlAndroid::GetDynamicPartitionControl() { | 
|  | 195 | return dynamic_control_.get(); | 
|  | 196 | } | 
|  | 197 |  | 
| Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 198 | std::optional<PartitionDevice> BootControlAndroid::GetPartitionDevice( | 
|  | 199 | const std::string& partition_name, | 
|  | 200 | uint32_t slot, | 
|  | 201 | uint32_t current_slot, | 
|  | 202 | bool not_in_payload) const { | 
|  | 203 | return dynamic_control_->GetPartitionDevice( | 
|  | 204 | partition_name, slot, current_slot, not_in_payload); | 
|  | 205 | } | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 206 | }  // namespace chromeos_update_engine |