blob: 5d11e422cbbc61b4ad3ae0db5db0b248c520b4b8 [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
Alex Deymo1b03f9f2015-12-09 00:38:36 -080017#include "update_engine/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
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/utils.h"
Yifan Hong537802d2018-08-15 13:15:42 -070029#include "update_engine/dynamic_partition_control_android.h"
Alex Deymob17327c2015-09-04 10:29:00 -070030
31using std::string;
32
Yifan Hong537802d2018-08-15 13:15:42 -070033using android::dm::DmDeviceState;
Yifan Hong537802d2018-08-15 13:15:42 -070034using android::hardware::hidl_string;
Connor O'Briencee6ad92016-11-21 13:53:52 -080035using android::hardware::Return;
36using android::hardware::boot::V1_0::BoolResult;
37using android::hardware::boot::V1_0::CommandResult;
38using android::hardware::boot::V1_0::IBootControl;
Yifan Hong537802d2018-08-15 13:15:42 -070039using Slot = chromeos_update_engine::BootControlInterface::Slot;
Connor O'Briencee6ad92016-11-21 13:53:52 -080040
41namespace {
Yifan Hong537802d2018-08-15 13:15:42 -070042
Connor O'Briencee6ad92016-11-21 13:53:52 -080043auto StoreResultCallback(CommandResult* dest) {
44 return [dest](const CommandResult& result) { *dest = result; };
45}
46} // namespace
Alex Deymo44348e02016-07-29 16:22:26 -070047
Alex Deymob17327c2015-09-04 10:29:00 -070048namespace chromeos_update_engine {
49
50namespace boot_control {
51
52// Factory defined in boot_control.h.
53std::unique_ptr<BootControlInterface> CreateBootControl() {
Yifan Hong537802d2018-08-15 13:15:42 -070054 auto boot_control = std::make_unique<BootControlAndroid>();
David Zeuthen753fadc2015-09-15 16:34:09 -040055 if (!boot_control->Init()) {
56 return nullptr;
57 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070058 return std::move(boot_control);
Alex Deymob17327c2015-09-04 10:29:00 -070059}
60
61} // namespace boot_control
62
David Zeuthen753fadc2015-09-15 16:34:09 -040063bool BootControlAndroid::Init() {
Chris Phoenixafde8e82017-01-17 23:14:58 -080064 module_ = IBootControl::getService();
Connor O'Briencee6ad92016-11-21 13:53:52 -080065 if (module_ == nullptr) {
Steven Moreland927e00d2017-01-04 12:58:40 -080066 LOG(ERROR) << "Error getting bootctrl HIDL module.";
David Zeuthen753fadc2015-09-15 16:34:09 -040067 return false;
68 }
69
Steven Moreland927e00d2017-01-04 12:58:40 -080070 LOG(INFO) << "Loaded boot control hidl hal.";
David Zeuthen753fadc2015-09-15 16:34:09 -040071
Yifan Hong537802d2018-08-15 13:15:42 -070072 dynamic_control_ = std::make_unique<DynamicPartitionControlAndroid>();
73
David Zeuthen753fadc2015-09-15 16:34:09 -040074 return true;
75}
Alex Deymob17327c2015-09-04 10:29:00 -070076
Yifan Hong537802d2018-08-15 13:15:42 -070077void BootControlAndroid::Cleanup() {
78 dynamic_control_->Cleanup();
79}
80
Alex Deymob17327c2015-09-04 10:29:00 -070081unsigned int BootControlAndroid::GetNumSlots() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080082 return module_->getNumberSlots();
Alex Deymob17327c2015-09-04 10:29:00 -070083}
84
85BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080086 return module_->getCurrentSlot();
Alex Deymob17327c2015-09-04 10:29:00 -070087}
88
Yifan Hongae04e192018-10-29 11:00:28 -070089
Yifan Hong537802d2018-08-15 13:15:42 -070090bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
91 Slot slot,
92 string* device) const {
Yifan Hong3a1a5612019-11-05 16:34:32 -080093 return dynamic_control_->GetPartitionDevice(
94 partition_name, slot, GetCurrentSlot(), device);
Alex Deymob17327c2015-09-04 10:29:00 -070095}
96
97bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080098 Return<BoolResult> ret = module_->isSlotBootable(slot);
Yifan Hong7b514b42016-12-21 13:02:00 -080099 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700100 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800101 << " is bootable: " << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400102 return false;
103 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800104 if (ret == BoolResult::INVALID_SLOT) {
105 LOG(ERROR) << "Invalid slot: " << SlotName(slot);
106 return false;
107 }
108 return ret == BoolResult::TRUE;
Alex Deymob17327c2015-09-04 10:29:00 -0700109}
110
111bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800112 CommandResult result;
113 auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800114 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800115 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800116 << SlotName(slot) << ": " << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400117 return false;
118 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800119 if (!result.success) {
120 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
121 << " as unbootable: " << result.errMsg.c_str();
122 }
123 return result.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700124}
125
Alex Deymo31d95ac2015-09-17 11:56:18 -0700126bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Yifan Honga33bca42019-09-03 20:29:45 -0700127 if (slot != GetCurrentSlot() && !dynamic_control_->FinishUpdate()) {
128 return false;
129 }
130
Connor O'Briencee6ad92016-11-21 13:53:52 -0800131 CommandResult result;
132 auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800133 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800134 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Yifan Hong7b514b42016-12-21 13:02:00 -0800135 << ": " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800136 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700137 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800138 if (!result.success) {
139 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
140 << ": " << result.errMsg.c_str();
141 }
142 return result.success;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700143}
144
Alex Deymoaa26f622015-09-16 18:21:27 -0700145bool BootControlAndroid::MarkBootSuccessfulAsync(
146 base::Callback<void(bool)> callback) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800147 CommandResult result;
148 auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800149 if (!ret.isOk()) {
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800150 LOG(ERROR) << "Unable to call MarkBootSuccessful: " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800151 return false;
152 }
153 if (!result.success) {
154 LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str();
Alex Deymoaa26f622015-09-16 18:21:27 -0700155 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700156 return brillo::MessageLoop::current()->PostTask(
Connor O'Briencee6ad92016-11-21 13:53:52 -0800157 FROM_HERE, base::Bind(callback, result.success)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700158 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700159}
160
Yifan Hong13d41cb2019-09-16 13:18:22 -0700161bool BootControlAndroid::PreparePartitionsForUpdate(
Tao Bao3406c772019-01-02 15:34:35 -0800162 Slot target_slot,
Yifan Hong13d41cb2019-09-16 13:18:22 -0700163 const DeltaArchiveManifest& manifest,
Tao Bao3406c772019-01-02 15:34:35 -0800164 bool update_metadata) {
Yifan Hong537802d2018-08-15 13:15:42 -0700165
Yifan Hong1d9077f2018-12-07 12:09:37 -0800166 auto source_slot = GetCurrentSlot();
Yifan Hong012508e2019-07-22 18:30:40 -0700167 return dynamic_control_->PreparePartitionsForUpdate(
Yifan Hongf0f4a912019-09-26 17:51:33 -0700168 source_slot, target_slot, manifest, update_metadata);
Yifan Hong537802d2018-08-15 13:15:42 -0700169}
170
Yifan Hongdaac7322019-11-07 10:48:26 -0800171DynamicPartitionControlInterface*
172BootControlAndroid::GetDynamicPartitionControl() {
173 return dynamic_control_.get();
174}
175
Alex Deymob17327c2015-09-04 10:29:00 -0700176} // namespace chromeos_update_engine