blob: 0a1d3deb0c8da74480a245ebd9b769fd419fe7a2 [file] [log] [blame]
Alex Deymob17327c2015-09-04 10:29:00 -07001//
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 Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/aosp/boot_control_android.h"
Alex Deymob17327c2015-09-04 10:29:00 -070018
Sen Jiangd944faa2018-08-22 18:46:39 -070019#include <memory>
20#include <utility>
Yifan Hongd4db07e2018-10-18 17:46:27 -070021#include <vector>
Sen Jiangd944faa2018-08-22 18:46:39 -070022
Alex Deymoaa26f622015-09-16 18:21:27 -070023#include <base/bind.h>
Alex Deymoaa26f622015-09-16 18:21:27 -070024#include <base/logging.h>
Sen Jiangd944faa2018-08-22 18:46:39 -070025#include <bootloader_message/bootloader_message.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070026#include <brillo/message_loops/message_loop.h>
Alex Deymob17327c2015-09-04 10:29:00 -070027
Amin Hassaniec7bc112020-10-29 16:47:58 -070028#include "update_engine/aosp/dynamic_partition_control_android.h"
Alex Deymob17327c2015-09-04 10:29:00 -070029
30using std::string;
31
Yifan Hong537802d2018-08-15 13:15:42 -070032using Slot = chromeos_update_engine::BootControlInterface::Slot;
Connor O'Briencee6ad92016-11-21 13:53:52 -080033
34namespace {
Yifan Hong537802d2018-08-15 13:15:42 -070035
Connor O'Briencee6ad92016-11-21 13:53:52 -080036} // namespace
Alex Deymo44348e02016-07-29 16:22:26 -070037
Alex Deymob17327c2015-09-04 10:29:00 -070038namespace chromeos_update_engine {
39
40namespace boot_control {
41
42// Factory defined in boot_control.h.
43std::unique_ptr<BootControlInterface> CreateBootControl() {
Yifan Hong537802d2018-08-15 13:15:42 -070044 auto boot_control = std::make_unique<BootControlAndroid>();
David Zeuthen753fadc2015-09-15 16:34:09 -040045 if (!boot_control->Init()) {
46 return nullptr;
47 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070048 return std::move(boot_control);
Alex Deymob17327c2015-09-04 10:29:00 -070049}
50
51} // namespace boot_control
52
Kelvin Zhange9c1d372022-06-13 15:40:44 -070053using android::hal::BootControlClient;
54using android::hal::CommandResult;
55using android::hal::BootControlVersion;
56
David Zeuthen753fadc2015-09-15 16:34:09 -040057bool BootControlAndroid::Init() {
Kelvin Zhange9c1d372022-06-13 15:40:44 -070058 module_ = BootControlClient::WaitForService();
Connor O'Briencee6ad92016-11-21 13:53:52 -080059 if (module_ == nullptr) {
Kelvin Zhange9c1d372022-06-13 15:40:44 -070060 LOG(ERROR) << "Error getting bootctrl module.";
David Zeuthen753fadc2015-09-15 16:34:09 -040061 return false;
62 }
63
Kelvin Zhange9c1d372022-06-13 15:40:44 -070064 LOG(INFO) << "Loaded boot control hal.";
David Zeuthen753fadc2015-09-15 16:34:09 -040065
Kelvin Zhangebd115e2021-03-08 16:10:25 -050066 dynamic_control_ =
67 std::make_unique<DynamicPartitionControlAndroid>(GetCurrentSlot());
Yifan Hong537802d2018-08-15 13:15:42 -070068
David Zeuthen753fadc2015-09-15 16:34:09 -040069 return true;
70}
Alex Deymob17327c2015-09-04 10:29:00 -070071
72unsigned int BootControlAndroid::GetNumSlots() const {
Kelvin Zhange9c1d372022-06-13 15:40:44 -070073 return module_->GetNumSlots();
Alex Deymob17327c2015-09-04 10:29:00 -070074}
75
76BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
Kelvin Zhange9c1d372022-06-13 15:40:44 -070077 return module_->GetCurrentSlot();
Alex Deymob17327c2015-09-04 10:29:00 -070078}
79
Tianjie51a5a392020-06-03 14:39:32 -070080bool 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 Hongae04e192018-10-29 11:00:28 -070092
Yifan Hong537802d2018-08-15 13:15:42 -070093bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
Tianjie51a5a392020-06-03 14:39:32 -070094 BootControlInterface::Slot slot,
Yifan Hong537802d2018-08-15 13:15:42 -070095 string* device) const {
Tianjie51a5a392020-06-03 14:39:32 -070096 return GetPartitionDevice(
97 partition_name, slot, false /* not_in_payload */, device, nullptr);
Alex Deymob17327c2015-09-04 10:29:00 -070098}
99
100bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700101 const auto ret = module_->IsSlotBootable(slot);
102 if (!ret.has_value()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700103 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700104 << " is bootable";
David Zeuthen753fadc2015-09-15 16:34:09 -0400105 return false;
106 }
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700107 return ret.value();
Alex Deymob17327c2015-09-04 10:29:00 -0700108}
109
110bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700111 const auto ret = module_->MarkSlotUnbootable(slot);
112 if (!ret.IsOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800113 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700114 << SlotName(slot) << ": " << ret.errMsg;
David Zeuthen753fadc2015-09-15 16:34:09 -0400115 return false;
116 }
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700117 return ret.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700118}
119
Alex Deymo31d95ac2015-09-17 11:56:18 -0700120bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700121 const auto result = module_->SetActiveBootSlot(slot);
122 if (!result.IsOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800123 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700124 << ": " << result.errMsg;
Connor O'Briencee6ad92016-11-21 13:53:52 -0800125 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700126 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800127 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 Deymo31d95ac2015-09-17 11:56:18 -0700132}
133
Alex Deymoaa26f622015-09-16 18:21:27 -0700134bool BootControlAndroid::MarkBootSuccessfulAsync(
135 base::Callback<void(bool)> callback) {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700136 auto ret = module_->MarkBootSuccessful();
137 if (!ret.IsOk()) {
138 LOG(ERROR) << "Unable to MarkBootSuccessful: " << ret.errMsg;
Connor O'Briencee6ad92016-11-21 13:53:52 -0800139 return false;
140 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700141 return brillo::MessageLoop::current()->PostTask(
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700142 FROM_HERE, base::Bind(callback, ret.success)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700143 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700144}
145
Yifan Hongf1415942020-02-24 18:34:49 -0800146bool BootControlAndroid::IsSlotMarkedSuccessful(
147 BootControlInterface::Slot slot) const {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700148 const auto ret = module_->IsSlotMarkedSuccessful(slot);
Yifan Hongf1415942020-02-24 18:34:49 -0800149 CommandResult result;
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700150 if (!ret.has_value()) {
Yifan Hongf1415942020-02-24 18:34:49 -0800151 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700152 << " is marked successful";
Yifan Hongf1415942020-02-24 18:34:49 -0800153 return false;
154 }
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700155 return ret.value();
Yifan Hongf1415942020-02-24 18:34:49 -0800156}
157
Kelvin Zhangcb419e62021-06-16 13:56:47 -0400158Slot BootControlAndroid::GetActiveBootSlot() {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700159 if (module_->GetVersion() >= android::hal::BootControlVersion::BOOTCTL_V1_2) {
160 return module_->GetActiveBootSlot();
Kelvin Zhangcb419e62021-06-16 13:56:47 -0400161 }
162 LOG(WARNING) << "BootControl module version is lower than 1.2, "
163 << __FUNCTION__ << " failed";
164 return kInvalidSlot;
165}
166
Yifan Hongdaac7322019-11-07 10:48:26 -0800167DynamicPartitionControlInterface*
168BootControlAndroid::GetDynamicPartitionControl() {
169 return dynamic_control_.get();
170}
171
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500172std::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 Deymob17327c2015-09-04 10:29:00 -0700180} // namespace chromeos_update_engine