blob: c1ac0d429ab28bc5ec1497f34028692570bc7772 [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
Connor O'Briencee6ad92016-11-21 13:53:52 -080033using android::hardware::Return;
34using android::hardware::boot::V1_0::BoolResult;
35using android::hardware::boot::V1_0::CommandResult;
36using android::hardware::boot::V1_0::IBootControl;
Yifan Hong537802d2018-08-15 13:15:42 -070037using Slot = chromeos_update_engine::BootControlInterface::Slot;
Connor O'Briencee6ad92016-11-21 13:53:52 -080038
39namespace {
Yifan Hong537802d2018-08-15 13:15:42 -070040
Connor O'Briencee6ad92016-11-21 13:53:52 -080041auto StoreResultCallback(CommandResult* dest) {
42 return [dest](const CommandResult& result) { *dest = result; };
43}
44} // namespace
Alex Deymo44348e02016-07-29 16:22:26 -070045
Alex Deymob17327c2015-09-04 10:29:00 -070046namespace chromeos_update_engine {
47
48namespace boot_control {
49
50// Factory defined in boot_control.h.
51std::unique_ptr<BootControlInterface> CreateBootControl() {
Yifan Hong537802d2018-08-15 13:15:42 -070052 auto boot_control = std::make_unique<BootControlAndroid>();
David Zeuthen753fadc2015-09-15 16:34:09 -040053 if (!boot_control->Init()) {
54 return nullptr;
55 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070056 return std::move(boot_control);
Alex Deymob17327c2015-09-04 10:29:00 -070057}
58
59} // namespace boot_control
60
David Zeuthen753fadc2015-09-15 16:34:09 -040061bool BootControlAndroid::Init() {
Chris Phoenixafde8e82017-01-17 23:14:58 -080062 module_ = IBootControl::getService();
Connor O'Briencee6ad92016-11-21 13:53:52 -080063 if (module_ == nullptr) {
Steven Moreland927e00d2017-01-04 12:58:40 -080064 LOG(ERROR) << "Error getting bootctrl HIDL module.";
David Zeuthen753fadc2015-09-15 16:34:09 -040065 return false;
66 }
67
Steven Moreland927e00d2017-01-04 12:58:40 -080068 LOG(INFO) << "Loaded boot control hidl hal.";
David Zeuthen753fadc2015-09-15 16:34:09 -040069
Kelvin Zhangebd115e2021-03-08 16:10:25 -050070 dynamic_control_ =
71 std::make_unique<DynamicPartitionControlAndroid>(GetCurrentSlot());
Yifan Hong537802d2018-08-15 13:15:42 -070072
David Zeuthen753fadc2015-09-15 16:34:09 -040073 return true;
74}
Alex Deymob17327c2015-09-04 10:29:00 -070075
76unsigned int BootControlAndroid::GetNumSlots() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080077 return module_->getNumberSlots();
Alex Deymob17327c2015-09-04 10:29:00 -070078}
79
80BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080081 return module_->getCurrentSlot();
Alex Deymob17327c2015-09-04 10:29:00 -070082}
83
Tianjie51a5a392020-06-03 14:39:32 -070084bool BootControlAndroid::GetPartitionDevice(const std::string& partition_name,
85 BootControlInterface::Slot slot,
86 bool not_in_payload,
87 std::string* device,
88 bool* is_dynamic) const {
89 return dynamic_control_->GetPartitionDevice(partition_name,
90 slot,
91 GetCurrentSlot(),
92 not_in_payload,
93 device,
94 is_dynamic);
95}
Yifan Hongae04e192018-10-29 11:00:28 -070096
Yifan Hong537802d2018-08-15 13:15:42 -070097bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
Tianjie51a5a392020-06-03 14:39:32 -070098 BootControlInterface::Slot slot,
Yifan Hong537802d2018-08-15 13:15:42 -070099 string* device) const {
Tianjie51a5a392020-06-03 14:39:32 -0700100 return GetPartitionDevice(
101 partition_name, slot, false /* not_in_payload */, device, nullptr);
Alex Deymob17327c2015-09-04 10:29:00 -0700102}
103
104bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800105 Return<BoolResult> ret = module_->isSlotBootable(slot);
Yifan Hong7b514b42016-12-21 13:02:00 -0800106 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700107 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800108 << " is bootable: " << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400109 return false;
110 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800111 if (ret == BoolResult::INVALID_SLOT) {
112 LOG(ERROR) << "Invalid slot: " << SlotName(slot);
113 return false;
114 }
115 return ret == BoolResult::TRUE;
Alex Deymob17327c2015-09-04 10:29:00 -0700116}
117
118bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800119 CommandResult result;
120 auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800121 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800122 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800123 << SlotName(slot) << ": " << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400124 return false;
125 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800126 if (!result.success) {
127 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
128 << " as unbootable: " << result.errMsg.c_str();
129 }
130 return result.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700131}
132
Alex Deymo31d95ac2015-09-17 11:56:18 -0700133bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800134 CommandResult result;
135 auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800136 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800137 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Yifan Hong7b514b42016-12-21 13:02:00 -0800138 << ": " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800139 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700140 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800141 if (!result.success) {
142 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
143 << ": " << result.errMsg.c_str();
144 }
145 return result.success;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700146}
147
Alex Deymoaa26f622015-09-16 18:21:27 -0700148bool BootControlAndroid::MarkBootSuccessfulAsync(
149 base::Callback<void(bool)> callback) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800150 CommandResult result;
151 auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800152 if (!ret.isOk()) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800153 LOG(ERROR) << "Unable to call MarkBootSuccessful: " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800154 return false;
155 }
156 if (!result.success) {
157 LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str();
Alex Deymoaa26f622015-09-16 18:21:27 -0700158 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700159 return brillo::MessageLoop::current()->PostTask(
Connor O'Briencee6ad92016-11-21 13:53:52 -0800160 FROM_HERE, base::Bind(callback, result.success)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700161 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700162}
163
Yifan Hongf1415942020-02-24 18:34:49 -0800164bool BootControlAndroid::IsSlotMarkedSuccessful(
165 BootControlInterface::Slot slot) const {
166 Return<BoolResult> ret = module_->isSlotMarkedSuccessful(slot);
167 CommandResult result;
168 if (!ret.isOk()) {
169 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
170 << " is marked successful: " << ret.description();
171 return false;
172 }
173 if (ret == BoolResult::INVALID_SLOT) {
174 LOG(ERROR) << "Invalid slot: " << SlotName(slot);
175 return false;
176 }
177 return ret == BoolResult::TRUE;
178}
179
Yifan Hongdaac7322019-11-07 10:48:26 -0800180DynamicPartitionControlInterface*
181BootControlAndroid::GetDynamicPartitionControl() {
182 return dynamic_control_.get();
183}
184
Kelvin Zhang91d95fa2020-11-05 13:52:00 -0500185std::optional<PartitionDevice> BootControlAndroid::GetPartitionDevice(
186 const std::string& partition_name,
187 uint32_t slot,
188 uint32_t current_slot,
189 bool not_in_payload) const {
190 return dynamic_control_->GetPartitionDevice(
191 partition_name, slot, current_slot, not_in_payload);
192}
Alex Deymob17327c2015-09-04 10:29:00 -0700193} // namespace chromeos_update_engine