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 | |
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> |
Sen Jiang | d944faa | 2018-08-22 18:46:39 -0700 | [diff] [blame] | 25 | #include <bootloader_message/bootloader_message.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 26 | #include <brillo/message_loops/message_loop.h> |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 27 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 28 | #include "update_engine/aosp/dynamic_partition_control_android.h" |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 29 | |
| 30 | using std::string; |
| 31 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 32 | using Slot = chromeos_update_engine::BootControlInterface::Slot; |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 33 | |
| 34 | namespace { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 35 | |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 36 | } // namespace |
Alex Deymo | 44348e0 | 2016-07-29 16:22:26 -0700 | [diff] [blame] | 37 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 38 | namespace chromeos_update_engine { |
| 39 | |
| 40 | namespace boot_control { |
| 41 | |
| 42 | // Factory defined in boot_control.h. |
| 43 | std::unique_ptr<BootControlInterface> CreateBootControl() { |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 44 | auto boot_control = std::make_unique<BootControlAndroid>(); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 45 | if (!boot_control->Init()) { |
| 46 | return nullptr; |
| 47 | } |
Alex Vakulenko | ce8c8ee | 2016-04-08 08:59:26 -0700 | [diff] [blame] | 48 | return std::move(boot_control); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | } // namespace boot_control |
| 52 | |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 53 | using android::hal::BootControlClient; |
| 54 | using android::hal::CommandResult; |
| 55 | using android::hal::BootControlVersion; |
| 56 | |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 57 | bool BootControlAndroid::Init() { |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 58 | module_ = BootControlClient::WaitForService(); |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 59 | if (module_ == nullptr) { |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 60 | LOG(ERROR) << "Error getting bootctrl module."; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 64 | LOG(INFO) << "Loaded boot control hal."; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 65 | |
Kelvin Zhang | ebd115e | 2021-03-08 16:10:25 -0500 | [diff] [blame] | 66 | dynamic_control_ = |
| 67 | std::make_unique<DynamicPartitionControlAndroid>(GetCurrentSlot()); |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 68 | |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 69 | return true; |
| 70 | } |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 71 | |
| 72 | unsigned int BootControlAndroid::GetNumSlots() const { |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 73 | return module_->GetNumSlots(); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const { |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 77 | return module_->GetCurrentSlot(); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 80 | bool BootControlAndroid::GetPartitionDevice(const std::string& partition_name, |
| 81 | BootControlInterface::Slot slot, |
| 82 | bool not_in_payload, |
| 83 | std::string* device, |
| 84 | bool* is_dynamic) const { |
| 85 | return dynamic_control_->GetPartitionDevice(partition_name, |
| 86 | slot, |
| 87 | GetCurrentSlot(), |
| 88 | not_in_payload, |
| 89 | device, |
| 90 | is_dynamic); |
| 91 | } |
Yifan Hong | ae04e19 | 2018-10-29 11:00:28 -0700 | [diff] [blame] | 92 | |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 93 | bool BootControlAndroid::GetPartitionDevice(const string& partition_name, |
Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 94 | BootControlInterface::Slot slot, |
Yifan Hong | 537802d | 2018-08-15 13:15:42 -0700 | [diff] [blame] | 95 | string* device) const { |
Tianjie | 51a5a39 | 2020-06-03 14:39:32 -0700 | [diff] [blame] | 96 | return GetPartitionDevice( |
| 97 | partition_name, slot, false /* not_in_payload */, device, nullptr); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | bool BootControlAndroid::IsSlotBootable(Slot slot) const { |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 101 | const auto ret = module_->IsSlotBootable(slot); |
| 102 | if (!ret.has_value()) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 103 | LOG(ERROR) << "Unable to determine if slot " << SlotName(slot) |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 104 | << " is bootable"; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 105 | return false; |
| 106 | } |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 107 | return ret.value(); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool BootControlAndroid::MarkSlotUnbootable(Slot slot) { |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 111 | const auto ret = module_->MarkSlotUnbootable(slot); |
| 112 | if (!ret.IsOk()) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 113 | LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot " |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 114 | << SlotName(slot) << ": " << ret.errMsg; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 115 | return false; |
| 116 | } |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 117 | return ret.success; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 120 | bool BootControlAndroid::SetActiveBootSlot(Slot slot) { |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 121 | const auto result = module_->SetActiveBootSlot(slot); |
| 122 | if (!result.IsOk()) { |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 123 | LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot) |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 124 | << ": " << result.errMsg; |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 125 | return false; |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame] | 126 | } |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 127 | if (!result.success) { |
| 128 | LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot) |
| 129 | << ": " << result.errMsg.c_str(); |
| 130 | } |
| 131 | return result.success; |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 134 | bool BootControlAndroid::MarkBootSuccessfulAsync( |
| 135 | base::Callback<void(bool)> callback) { |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 136 | auto ret = module_->MarkBootSuccessful(); |
| 137 | if (!ret.IsOk()) { |
| 138 | LOG(ERROR) << "Unable to MarkBootSuccessful: " << ret.errMsg; |
Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 139 | return false; |
| 140 | } |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 141 | return brillo::MessageLoop::current()->PostTask( |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 142 | FROM_HERE, base::Bind(callback, ret.success)) != |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 143 | brillo::MessageLoop::kTaskIdNull; |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Yifan Hong | f141594 | 2020-02-24 18:34:49 -0800 | [diff] [blame] | 146 | bool BootControlAndroid::IsSlotMarkedSuccessful( |
| 147 | BootControlInterface::Slot slot) const { |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 148 | const auto ret = module_->IsSlotMarkedSuccessful(slot); |
Yifan Hong | f141594 | 2020-02-24 18:34:49 -0800 | [diff] [blame] | 149 | CommandResult result; |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 150 | if (!ret.has_value()) { |
Yifan Hong | f141594 | 2020-02-24 18:34:49 -0800 | [diff] [blame] | 151 | LOG(ERROR) << "Unable to determine if slot " << SlotName(slot) |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 152 | << " is marked successful"; |
Yifan Hong | f141594 | 2020-02-24 18:34:49 -0800 | [diff] [blame] | 153 | return false; |
| 154 | } |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 155 | return ret.value(); |
Yifan Hong | f141594 | 2020-02-24 18:34:49 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Kelvin Zhang | cb419e6 | 2021-06-16 13:56:47 -0400 | [diff] [blame] | 158 | Slot BootControlAndroid::GetActiveBootSlot() { |
Kelvin Zhang | e9c1d37 | 2022-06-13 15:40:44 -0700 | [diff] [blame] | 159 | if (module_->GetVersion() >= android::hal::BootControlVersion::BOOTCTL_V1_2) { |
| 160 | return module_->GetActiveBootSlot(); |
Kelvin Zhang | cb419e6 | 2021-06-16 13:56:47 -0400 | [diff] [blame] | 161 | } |
| 162 | LOG(WARNING) << "BootControl module version is lower than 1.2, " |
| 163 | << __FUNCTION__ << " failed"; |
| 164 | return kInvalidSlot; |
| 165 | } |
| 166 | |
Yifan Hong | daac732 | 2019-11-07 10:48:26 -0800 | [diff] [blame] | 167 | DynamicPartitionControlInterface* |
| 168 | BootControlAndroid::GetDynamicPartitionControl() { |
| 169 | return dynamic_control_.get(); |
| 170 | } |
| 171 | |
Kelvin Zhang | 91d95fa | 2020-11-05 13:52:00 -0500 | [diff] [blame] | 172 | std::optional<PartitionDevice> BootControlAndroid::GetPartitionDevice( |
| 173 | const std::string& partition_name, |
| 174 | uint32_t slot, |
| 175 | uint32_t current_slot, |
| 176 | bool not_in_payload) const { |
| 177 | return dynamic_control_->GetPartitionDevice( |
| 178 | partition_name, slot, current_slot, not_in_payload); |
| 179 | } |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 180 | } // namespace chromeos_update_engine |