blob: 8c1603b7855e4f59ddcdc7f00369da2e96fbb96e [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/message_loops/message_loop.h>
Alex Deymob17327c2015-09-04 10:29:00 -070024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/utils.h"
Alex Deymofb905d92016-06-03 19:26:58 -070026#include "update_engine/utils_android.h"
Alex Deymob17327c2015-09-04 10:29:00 -070027
28using std::string;
29
Connor O'Briencee6ad92016-11-21 13:53:52 -080030using android::hardware::Return;
31using android::hardware::boot::V1_0::BoolResult;
32using android::hardware::boot::V1_0::CommandResult;
33using android::hardware::boot::V1_0::IBootControl;
34using android::hardware::hidl_string;
35
36namespace {
37auto StoreResultCallback(CommandResult* dest) {
38 return [dest](const CommandResult& result) { *dest = result; };
39}
40} // namespace
Alex Deymo44348e02016-07-29 16:22:26 -070041
Alex Deymob17327c2015-09-04 10:29:00 -070042namespace chromeos_update_engine {
43
44namespace boot_control {
45
46// Factory defined in boot_control.h.
47std::unique_ptr<BootControlInterface> CreateBootControl() {
David Zeuthen753fadc2015-09-15 16:34:09 -040048 std::unique_ptr<BootControlAndroid> boot_control(new BootControlAndroid());
49 if (!boot_control->Init()) {
50 return nullptr;
51 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070052 return std::move(boot_control);
Alex Deymob17327c2015-09-04 10:29:00 -070053}
54
55} // namespace boot_control
56
David Zeuthen753fadc2015-09-15 16:34:09 -040057bool BootControlAndroid::Init() {
Chris Phoenixafde8e82017-01-17 23:14:58 -080058 module_ = IBootControl::getService();
Connor O'Briencee6ad92016-11-21 13:53:52 -080059 if (module_ == nullptr) {
Steven Moreland927e00d2017-01-04 12:58:40 -080060 LOG(ERROR) << "Error getting bootctrl HIDL module.";
David Zeuthen753fadc2015-09-15 16:34:09 -040061 return false;
62 }
63
Steven Moreland927e00d2017-01-04 12:58:40 -080064 LOG(INFO) << "Loaded boot control hidl hal.";
David Zeuthen753fadc2015-09-15 16:34:09 -040065
David Zeuthen753fadc2015-09-15 16:34:09 -040066 return true;
67}
Alex Deymob17327c2015-09-04 10:29:00 -070068
69unsigned int BootControlAndroid::GetNumSlots() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080070 return module_->getNumberSlots();
Alex Deymob17327c2015-09-04 10:29:00 -070071}
72
73BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080074 return module_->getCurrentSlot();
Alex Deymob17327c2015-09-04 10:29:00 -070075}
76
77bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
David Zeuthen753fadc2015-09-15 16:34:09 -040078 Slot slot,
Alex Deymob17327c2015-09-04 10:29:00 -070079 string* device) const {
David Zeuthen753fadc2015-09-15 16:34:09 -040080 // We can't use fs_mgr to look up |partition_name| because fstab
81 // doesn't list every slot partition (it uses the slotselect option
82 // to mask the suffix).
83 //
84 // We can however assume that there's an entry for the /misc mount
85 // point and use that to get the device file for the misc
86 // partition. This helps us locate the disk that |partition_name|
87 // resides on. From there we'll assume that a by-name scheme is used
88 // so we can just replace the trailing "misc" by the given
89 // |partition_name| and suffix corresponding to |slot|, e.g.
90 //
91 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
92 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
93 //
94 // If needed, it's possible to relax the by-name assumption in the
95 // future by trawling /sys/block looking for the appropriate sibling
96 // of misc and then finding an entry in /dev matching the sysfs
97 // entry.
98
Alex Deymofb905d92016-06-03 19:26:58 -070099 base::FilePath misc_device;
100 if (!utils::DeviceForMountPoint("/misc", &misc_device))
David Zeuthen753fadc2015-09-15 16:34:09 -0400101 return false;
David Zeuthen753fadc2015-09-15 16:34:09 -0400102
Alex Deymo12542ac2016-02-03 15:48:10 -0800103 if (!utils::IsSymlink(misc_device.value().c_str())) {
David Zeuthen753fadc2015-09-15 16:34:09 -0400104 LOG(ERROR) << "Device file " << misc_device.value() << " for /misc "
Alex Deymo12542ac2016-02-03 15:48:10 -0800105 << "is not a symlink.";
David Zeuthen753fadc2015-09-15 16:34:09 -0400106 return false;
107 }
108
Connor O'Briencee6ad92016-11-21 13:53:52 -0800109 string suffix;
110 auto store_suffix_cb = [&suffix](hidl_string cb_suffix) {
111 suffix = cb_suffix.c_str();
112 };
113 Return<void> ret = module_->getSuffix(slot, store_suffix_cb);
114
Yifan Hong7b514b42016-12-21 13:02:00 -0800115 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700116 LOG(ERROR) << "boot_control impl returned no suffix for slot "
117 << SlotName(slot);
David Zeuthen753fadc2015-09-15 16:34:09 -0400118 return false;
119 }
120
121 base::FilePath path = misc_device.DirName().Append(partition_name + suffix);
122 if (!base::PathExists(path)) {
123 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
124 return false;
125 }
126
127 *device = path.value();
128 return true;
Alex Deymob17327c2015-09-04 10:29:00 -0700129}
130
131bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800132 Return<BoolResult> ret = module_->isSlotBootable(slot);
Yifan Hong7b514b42016-12-21 13:02:00 -0800133 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700134 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Connor O'Briencee6ad92016-11-21 13:53:52 -0800135 << " is bootable: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800136 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400137 return false;
138 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800139 if (ret == BoolResult::INVALID_SLOT) {
140 LOG(ERROR) << "Invalid slot: " << SlotName(slot);
141 return false;
142 }
143 return ret == BoolResult::TRUE;
Alex Deymob17327c2015-09-04 10:29:00 -0700144}
145
146bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800147 CommandResult result;
148 auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800149 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800150 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
151 << SlotName(slot) << ": "
Yifan Hong7b514b42016-12-21 13:02:00 -0800152 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400153 return false;
154 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800155 if (!result.success) {
156 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
157 << " as unbootable: " << result.errMsg.c_str();
158 }
159 return result.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700160}
161
Alex Deymo31d95ac2015-09-17 11:56:18 -0700162bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800163 CommandResult result;
164 auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800165 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800166 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Yifan Hong7b514b42016-12-21 13:02:00 -0800167 << ": " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800168 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700169 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800170 if (!result.success) {
171 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
172 << ": " << result.errMsg.c_str();
173 }
174 return result.success;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700175}
176
Alex Deymoaa26f622015-09-16 18:21:27 -0700177bool BootControlAndroid::MarkBootSuccessfulAsync(
178 base::Callback<void(bool)> callback) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800179 CommandResult result;
180 auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800181 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800182 LOG(ERROR) << "Unable to call MarkBootSuccessful: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800183 << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800184 return false;
185 }
186 if (!result.success) {
187 LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str();
Alex Deymoaa26f622015-09-16 18:21:27 -0700188 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700189 return brillo::MessageLoop::current()->PostTask(
Connor O'Briencee6ad92016-11-21 13:53:52 -0800190 FROM_HERE, base::Bind(callback, result.success)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700191 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700192}
193
Alex Deymob17327c2015-09-04 10:29:00 -0700194} // namespace chromeos_update_engine