blob: e3ea66d71af3d95ed539c219f31835d7885ff371 [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() {
Chris Phoenix8da5d222017-01-17 23:14:58 -080059 module_ = IBootControl::getService();
Connor O'Briencee6ad92016-11-21 13:53:52 -080060 if (module_ == nullptr) {
Steven Moreland927e00d2017-01-04 12:58:40 -080061 LOG(ERROR) << "Error getting bootctrl HIDL module.";
David Zeuthen753fadc2015-09-15 16:34:09 -040062 return false;
63 }
64
Steven Moreland927e00d2017-01-04 12:58:40 -080065 LOG(INFO) << "Loaded boot control hidl hal.";
David Zeuthen753fadc2015-09-15 16:34:09 -040066
David Zeuthen753fadc2015-09-15 16:34:09 -040067 return true;
68}
Alex Deymob17327c2015-09-04 10:29:00 -070069
70unsigned int BootControlAndroid::GetNumSlots() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080071 return module_->getNumberSlots();
Alex Deymob17327c2015-09-04 10:29:00 -070072}
73
74BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080075 return module_->getCurrentSlot();
Alex Deymob17327c2015-09-04 10:29:00 -070076}
77
78bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
David Zeuthen753fadc2015-09-15 16:34:09 -040079 Slot slot,
Alex Deymob17327c2015-09-04 10:29:00 -070080 string* device) const {
David Zeuthen753fadc2015-09-15 16:34:09 -040081 // We can't use fs_mgr to look up |partition_name| because fstab
82 // doesn't list every slot partition (it uses the slotselect option
83 // to mask the suffix).
84 //
85 // We can however assume that there's an entry for the /misc mount
86 // point and use that to get the device file for the misc
87 // partition. This helps us locate the disk that |partition_name|
88 // resides on. From there we'll assume that a by-name scheme is used
89 // so we can just replace the trailing "misc" by the given
90 // |partition_name| and suffix corresponding to |slot|, e.g.
91 //
92 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
93 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
94 //
95 // If needed, it's possible to relax the by-name assumption in the
96 // future by trawling /sys/block looking for the appropriate sibling
97 // of misc and then finding an entry in /dev matching the sysfs
98 // entry.
99
Alex Deymofb905d92016-06-03 19:26:58 -0700100 base::FilePath misc_device;
101 if (!utils::DeviceForMountPoint("/misc", &misc_device))
David Zeuthen753fadc2015-09-15 16:34:09 -0400102 return false;
David Zeuthen753fadc2015-09-15 16:34:09 -0400103
Alex Deymo12542ac2016-02-03 15:48:10 -0800104 if (!utils::IsSymlink(misc_device.value().c_str())) {
David Zeuthen753fadc2015-09-15 16:34:09 -0400105 LOG(ERROR) << "Device file " << misc_device.value() << " for /misc "
Alex Deymo12542ac2016-02-03 15:48:10 -0800106 << "is not a symlink.";
David Zeuthen753fadc2015-09-15 16:34:09 -0400107 return false;
108 }
109
Connor O'Briencee6ad92016-11-21 13:53:52 -0800110 string suffix;
111 auto store_suffix_cb = [&suffix](hidl_string cb_suffix) {
112 suffix = cb_suffix.c_str();
113 };
114 Return<void> ret = module_->getSuffix(slot, store_suffix_cb);
115
Yifan Hong7b514b42016-12-21 13:02:00 -0800116 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700117 LOG(ERROR) << "boot_control impl returned no suffix for slot "
118 << SlotName(slot);
David Zeuthen753fadc2015-09-15 16:34:09 -0400119 return false;
120 }
121
122 base::FilePath path = misc_device.DirName().Append(partition_name + suffix);
123 if (!base::PathExists(path)) {
124 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
125 return false;
126 }
127
128 *device = path.value();
129 return true;
Alex Deymob17327c2015-09-04 10:29:00 -0700130}
131
132bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800133 Return<BoolResult> ret = module_->isSlotBootable(slot);
Yifan Hong7b514b42016-12-21 13:02:00 -0800134 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700135 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Connor O'Briencee6ad92016-11-21 13:53:52 -0800136 << " is bootable: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800137 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400138 return false;
139 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800140 if (ret == BoolResult::INVALID_SLOT) {
141 LOG(ERROR) << "Invalid slot: " << SlotName(slot);
142 return false;
143 }
144 return ret == BoolResult::TRUE;
Alex Deymob17327c2015-09-04 10:29:00 -0700145}
146
147bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800148 CommandResult result;
149 auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800150 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800151 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
152 << SlotName(slot) << ": "
Yifan Hong7b514b42016-12-21 13:02:00 -0800153 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400154 return false;
155 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800156 if (!result.success) {
157 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
158 << " as unbootable: " << result.errMsg.c_str();
159 }
160 return result.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700161}
162
Alex Deymo31d95ac2015-09-17 11:56:18 -0700163bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800164 CommandResult result;
165 auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800166 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800167 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Yifan Hong7b514b42016-12-21 13:02:00 -0800168 << ": " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800169 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700170 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800171 if (!result.success) {
172 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
173 << ": " << result.errMsg.c_str();
174 }
175 return result.success;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700176}
177
Alex Deymoaa26f622015-09-16 18:21:27 -0700178bool BootControlAndroid::MarkBootSuccessfulAsync(
179 base::Callback<void(bool)> callback) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800180 CommandResult result;
181 auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800182 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800183 LOG(ERROR) << "Unable to call MarkBootSuccessful: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800184 << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800185 return false;
186 }
187 if (!result.success) {
188 LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str();
Alex Deymoaa26f622015-09-16 18:21:27 -0700189 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700190 return brillo::MessageLoop::current()->PostTask(
Connor O'Briencee6ad92016-11-21 13:53:52 -0800191 FROM_HERE, base::Bind(callback, result.success)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700192 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700193}
194
Alex Deymob17327c2015-09-04 10:29:00 -0700195} // namespace chromeos_update_engine