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