blob: 8a15049d19b0058cfceed3fb9f56d9280b4fb827 [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
Alex Deymoaa26f622015-09-16 18:21:27 -070019#include <base/bind.h>
David Zeuthen753fadc2015-09-15 16:34:09 -040020#include <base/files/file_util.h>
Alex Deymoaa26f622015-09-16 18:21:27 -070021#include <base/logging.h>
David Zeuthen753fadc2015-09-15 16:34:09 -040022#include <base/strings/string_util.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070023#include <brillo/make_unique_ptr.h>
24#include <brillo/message_loops/message_loop.h>
Alex Deymob17327c2015-09-04 10:29:00 -070025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/utils.h"
Alex Deymofb905d92016-06-03 19:26:58 -070027#include "update_engine/utils_android.h"
Alex Deymob17327c2015-09-04 10:29:00 -070028
29using std::string;
30
Connor O'Briencee6ad92016-11-21 13:53:52 -080031using android::hardware::Return;
32using android::hardware::boot::V1_0::BoolResult;
33using android::hardware::boot::V1_0::CommandResult;
34using android::hardware::boot::V1_0::IBootControl;
35using android::hardware::hidl_string;
36
37namespace {
38auto StoreResultCallback(CommandResult* dest) {
39 return [dest](const CommandResult& result) { *dest = result; };
40}
41} // namespace
Alex Deymo44348e02016-07-29 16:22:26 -070042
Alex Deymob17327c2015-09-04 10:29:00 -070043namespace chromeos_update_engine {
44
45namespace boot_control {
46
47// Factory defined in boot_control.h.
48std::unique_ptr<BootControlInterface> CreateBootControl() {
David Zeuthen753fadc2015-09-15 16:34:09 -040049 std::unique_ptr<BootControlAndroid> boot_control(new BootControlAndroid());
50 if (!boot_control->Init()) {
51 return nullptr;
52 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070053 return std::move(boot_control);
Alex Deymob17327c2015-09-04 10:29:00 -070054}
55
56} // namespace boot_control
57
David Zeuthen753fadc2015-09-15 16:34:09 -040058bool BootControlAndroid::Init() {
Connor O'Briencee6ad92016-11-21 13:53:52 -080059 module_ = IBootControl::getService("bootctrl");
60 if (module_ == nullptr) {
61 LOG(ERROR) << "Error getting bootctrl HIDL module.\n";
David Zeuthen753fadc2015-09-15 16:34:09 -040062 return false;
63 }
64
Connor O'Briencee6ad92016-11-21 13:53:52 -080065 LOG(INFO) << "Loaded boot HIDL HAL version "
66 << module_->getInterfaceVersion().get_major() << "."
67 << module_->getInterfaceVersion().get_minor();
David Zeuthen753fadc2015-09-15 16:34:09 -040068
David Zeuthen753fadc2015-09-15 16:34:09 -040069 return true;
70}
Alex Deymob17327c2015-09-04 10:29:00 -070071
72unsigned int BootControlAndroid::GetNumSlots() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080073 return module_->getNumberSlots();
Alex Deymob17327c2015-09-04 10:29:00 -070074}
75
76BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080077 return module_->getCurrentSlot();
Alex Deymob17327c2015-09-04 10:29:00 -070078}
79
80bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
David Zeuthen753fadc2015-09-15 16:34:09 -040081 Slot slot,
Alex Deymob17327c2015-09-04 10:29:00 -070082 string* device) const {
David Zeuthen753fadc2015-09-15 16:34:09 -040083 // We can't use fs_mgr to look up |partition_name| because fstab
84 // doesn't list every slot partition (it uses the slotselect option
85 // to mask the suffix).
86 //
87 // We can however assume that there's an entry for the /misc mount
88 // point and use that to get the device file for the misc
89 // partition. This helps us locate the disk that |partition_name|
90 // resides on. From there we'll assume that a by-name scheme is used
91 // so we can just replace the trailing "misc" by the given
92 // |partition_name| and suffix corresponding to |slot|, e.g.
93 //
94 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
95 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
96 //
97 // If needed, it's possible to relax the by-name assumption in the
98 // future by trawling /sys/block looking for the appropriate sibling
99 // of misc and then finding an entry in /dev matching the sysfs
100 // entry.
101
Alex Deymofb905d92016-06-03 19:26:58 -0700102 base::FilePath misc_device;
103 if (!utils::DeviceForMountPoint("/misc", &misc_device))
David Zeuthen753fadc2015-09-15 16:34:09 -0400104 return false;
David Zeuthen753fadc2015-09-15 16:34:09 -0400105
Alex Deymo12542ac2016-02-03 15:48:10 -0800106 if (!utils::IsSymlink(misc_device.value().c_str())) {
David Zeuthen753fadc2015-09-15 16:34:09 -0400107 LOG(ERROR) << "Device file " << misc_device.value() << " for /misc "
Alex Deymo12542ac2016-02-03 15:48:10 -0800108 << "is not a symlink.";
David Zeuthen753fadc2015-09-15 16:34:09 -0400109 return false;
110 }
111
Connor O'Briencee6ad92016-11-21 13:53:52 -0800112 string suffix;
113 auto store_suffix_cb = [&suffix](hidl_string cb_suffix) {
114 suffix = cb_suffix.c_str();
115 };
116 Return<void> ret = module_->getSuffix(slot, store_suffix_cb);
117
Yifan Hong7b514b42016-12-21 13:02:00 -0800118 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700119 LOG(ERROR) << "boot_control impl returned no suffix for slot "
120 << SlotName(slot);
David Zeuthen753fadc2015-09-15 16:34:09 -0400121 return false;
122 }
123
124 base::FilePath path = misc_device.DirName().Append(partition_name + suffix);
125 if (!base::PathExists(path)) {
126 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
127 return false;
128 }
129
130 *device = path.value();
131 return true;
Alex Deymob17327c2015-09-04 10:29:00 -0700132}
133
134bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800135 Return<BoolResult> ret = module_->isSlotBootable(slot);
Yifan Hong7b514b42016-12-21 13:02:00 -0800136 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700137 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Connor O'Briencee6ad92016-11-21 13:53:52 -0800138 << " is bootable: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800139 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400140 return false;
141 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800142 if (ret == BoolResult::INVALID_SLOT) {
143 LOG(ERROR) << "Invalid slot: " << SlotName(slot);
144 return false;
145 }
146 return ret == BoolResult::TRUE;
Alex Deymob17327c2015-09-04 10:29:00 -0700147}
148
149bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800150 CommandResult result;
151 auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800152 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800153 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
154 << SlotName(slot) << ": "
Yifan Hong7b514b42016-12-21 13:02:00 -0800155 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400156 return false;
157 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800158 if (!result.success) {
159 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
160 << " as unbootable: " << result.errMsg.c_str();
161 }
162 return result.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700163}
164
Alex Deymo31d95ac2015-09-17 11:56:18 -0700165bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800166 CommandResult result;
167 auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800168 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800169 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Yifan Hong7b514b42016-12-21 13:02:00 -0800170 << ": " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800171 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700172 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800173 if (!result.success) {
174 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
175 << ": " << result.errMsg.c_str();
176 }
177 return result.success;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700178}
179
Alex Deymoaa26f622015-09-16 18:21:27 -0700180bool BootControlAndroid::MarkBootSuccessfulAsync(
181 base::Callback<void(bool)> callback) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800182 CommandResult result;
183 auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800184 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800185 LOG(ERROR) << "Unable to call MarkBootSuccessful: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800186 << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800187 return false;
188 }
189 if (!result.success) {
190 LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str();
Alex Deymoaa26f622015-09-16 18:21:27 -0700191 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700192 return brillo::MessageLoop::current()->PostTask(
Connor O'Briencee6ad92016-11-21 13:53:52 -0800193 FROM_HERE, base::Bind(callback, result.success)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700194 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700195}
196
Alex Deymob17327c2015-09-04 10:29:00 -0700197} // namespace chromeos_update_engine