blob: 291fc617c0afdf21804d5dc7fd53b39695144fd7 [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 Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/utils.h"
Alex Deymob17327c2015-09-04 10:29:00 -070030
31using std::string;
32
Yifan Hong537802d2018-08-15 13:15:42 -070033using Slot = chromeos_update_engine::BootControlInterface::Slot;
Connor O'Briencee6ad92016-11-21 13:53:52 -080034
35namespace {
Yifan Hong537802d2018-08-15 13:15:42 -070036
Connor O'Briencee6ad92016-11-21 13:53:52 -080037} // namespace
Alex Deymo44348e02016-07-29 16:22:26 -070038
Alex Deymob17327c2015-09-04 10:29:00 -070039namespace chromeos_update_engine {
40
41namespace boot_control {
42
43// Factory defined in boot_control.h.
44std::unique_ptr<BootControlInterface> CreateBootControl() {
Yifan Hong537802d2018-08-15 13:15:42 -070045 auto boot_control = std::make_unique<BootControlAndroid>();
David Zeuthen753fadc2015-09-15 16:34:09 -040046 if (!boot_control->Init()) {
47 return nullptr;
48 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070049 return std::move(boot_control);
Alex Deymob17327c2015-09-04 10:29:00 -070050}
51
52} // namespace boot_control
53
Kelvin Zhange9c1d372022-06-13 15:40:44 -070054using android::hal::BootControlClient;
55using android::hal::CommandResult;
56using android::hal::BootControlVersion;
57
David Zeuthen753fadc2015-09-15 16:34:09 -040058bool BootControlAndroid::Init() {
Kelvin Zhange9c1d372022-06-13 15:40:44 -070059 module_ = BootControlClient::WaitForService();
Connor O'Briencee6ad92016-11-21 13:53:52 -080060 if (module_ == nullptr) {
Kelvin Zhange9c1d372022-06-13 15:40:44 -070061 LOG(ERROR) << "Error getting bootctrl module.";
David Zeuthen753fadc2015-09-15 16:34:09 -040062 return false;
63 }
64
Kelvin Zhange9c1d372022-06-13 15:40:44 -070065 LOG(INFO) << "Loaded boot control hal.";
David Zeuthen753fadc2015-09-15 16:34:09 -040066
Kelvin Zhangebd115e2021-03-08 16:10:25 -050067 dynamic_control_ =
68 std::make_unique<DynamicPartitionControlAndroid>(GetCurrentSlot());
Yifan Hong537802d2018-08-15 13:15:42 -070069
David Zeuthen753fadc2015-09-15 16:34:09 -040070 return true;
71}
Alex Deymob17327c2015-09-04 10:29:00 -070072
73unsigned int BootControlAndroid::GetNumSlots() const {
Kelvin Zhange9c1d372022-06-13 15:40:44 -070074 return module_->GetNumSlots();
Alex Deymob17327c2015-09-04 10:29:00 -070075}
76
77BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
Kelvin Zhange9c1d372022-06-13 15:40:44 -070078 return module_->GetCurrentSlot();
Alex Deymob17327c2015-09-04 10:29:00 -070079}
80
Tianjie51a5a392020-06-03 14:39:32 -070081bool 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 Hongae04e192018-10-29 11:00:28 -070093
Yifan Hong537802d2018-08-15 13:15:42 -070094bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
Tianjie51a5a392020-06-03 14:39:32 -070095 BootControlInterface::Slot slot,
Yifan Hong537802d2018-08-15 13:15:42 -070096 string* device) const {
Tianjie51a5a392020-06-03 14:39:32 -070097 return GetPartitionDevice(
98 partition_name, slot, false /* not_in_payload */, device, nullptr);
Alex Deymob17327c2015-09-04 10:29:00 -070099}
100
101bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700102 const auto ret = module_->IsSlotBootable(slot);
103 if (!ret.has_value()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700104 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700105 << " is bootable";
David Zeuthen753fadc2015-09-15 16:34:09 -0400106 return false;
107 }
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700108 return ret.value();
Alex Deymob17327c2015-09-04 10:29:00 -0700109}
110
111bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700112 const auto ret = module_->MarkSlotUnbootable(slot);
113 if (!ret.IsOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800114 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700115 << SlotName(slot) << ": " << ret.errMsg;
David Zeuthen753fadc2015-09-15 16:34:09 -0400116 return false;
117 }
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700118 return ret.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700119}
120
Alex Deymo31d95ac2015-09-17 11:56:18 -0700121bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700122 const auto result = module_->SetActiveBootSlot(slot);
123 if (!result.IsOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800124 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700125 << ": " << result.errMsg;
Connor O'Briencee6ad92016-11-21 13:53:52 -0800126 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700127 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800128 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 Deymo31d95ac2015-09-17 11:56:18 -0700133}
134
Alex Deymoaa26f622015-09-16 18:21:27 -0700135bool BootControlAndroid::MarkBootSuccessfulAsync(
136 base::Callback<void(bool)> callback) {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700137 auto ret = module_->MarkBootSuccessful();
138 if (!ret.IsOk()) {
139 LOG(ERROR) << "Unable to MarkBootSuccessful: " << ret.errMsg;
Connor O'Briencee6ad92016-11-21 13:53:52 -0800140 return false;
141 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700142 return brillo::MessageLoop::current()->PostTask(
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700143 FROM_HERE, base::Bind(callback, ret.success)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700144 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700145}
146
Yifan Hongf1415942020-02-24 18:34:49 -0800147bool BootControlAndroid::IsSlotMarkedSuccessful(
148 BootControlInterface::Slot slot) const {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700149 const auto ret = module_->IsSlotMarkedSuccessful(slot);
Yifan Hongf1415942020-02-24 18:34:49 -0800150 CommandResult result;
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700151 if (!ret.has_value()) {
Yifan Hongf1415942020-02-24 18:34:49 -0800152 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700153 << " is marked successful";
Yifan Hongf1415942020-02-24 18:34:49 -0800154 return false;
155 }
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700156 return ret.value();
Yifan Hongf1415942020-02-24 18:34:49 -0800157}
158
Kelvin Zhangcb419e62021-06-16 13:56:47 -0400159Slot BootControlAndroid::GetActiveBootSlot() {
Kelvin Zhange9c1d372022-06-13 15:40:44 -0700160 if (module_->GetVersion() >= android::hal::BootControlVersion::BOOTCTL_V1_2) {
161 return module_->GetActiveBootSlot();
Kelvin Zhangcb419e62021-06-16 13:56:47 -0400162 }
163 LOG(WARNING) << "BootControl module version is lower than 1.2, "
164 << __FUNCTION__ << " failed";
165 return kInvalidSlot;
166}
167
Yifan Hongdaac7322019-11-07 10:48:26 -0800168DynamicPartitionControlInterface*
169BootControlAndroid::GetDynamicPartitionControl() {
170 return dynamic_control_.get();
171}
172
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500173std::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 Deymob17327c2015-09-04 10:29:00 -0700181} // namespace chromeos_update_engine