blob: 12a3a10a1582d599f7efcb88e9b636e483030d42 [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>
21
Alex Deymoaa26f622015-09-16 18:21:27 -070022#include <base/bind.h>
David Zeuthen753fadc2015-09-15 16:34:09 -040023#include <base/files/file_util.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"
Alex Deymob17327c2015-09-04 10:29:00 -070029
30using std::string;
31
Connor O'Briencee6ad92016-11-21 13:53:52 -080032using android::hardware::Return;
33using android::hardware::boot::V1_0::BoolResult;
34using android::hardware::boot::V1_0::CommandResult;
35using android::hardware::boot::V1_0::IBootControl;
36using android::hardware::hidl_string;
37
38namespace {
39auto StoreResultCallback(CommandResult* dest) {
40 return [dest](const CommandResult& result) { *dest = result; };
41}
42} // namespace
Alex Deymo44348e02016-07-29 16:22:26 -070043
Alex Deymob17327c2015-09-04 10:29:00 -070044namespace chromeos_update_engine {
45
46namespace boot_control {
47
48// Factory defined in boot_control.h.
49std::unique_ptr<BootControlInterface> CreateBootControl() {
David Zeuthen753fadc2015-09-15 16:34:09 -040050 std::unique_ptr<BootControlAndroid> boot_control(new BootControlAndroid());
51 if (!boot_control->Init()) {
52 return nullptr;
53 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070054 return std::move(boot_control);
Alex Deymob17327c2015-09-04 10:29:00 -070055}
56
57} // namespace boot_control
58
David Zeuthen753fadc2015-09-15 16:34:09 -040059bool BootControlAndroid::Init() {
Chris Phoenixafde8e82017-01-17 23:14:58 -080060 module_ = IBootControl::getService();
Connor O'Briencee6ad92016-11-21 13:53:52 -080061 if (module_ == nullptr) {
Steven Moreland927e00d2017-01-04 12:58:40 -080062 LOG(ERROR) << "Error getting bootctrl HIDL module.";
David Zeuthen753fadc2015-09-15 16:34:09 -040063 return false;
64 }
65
Steven Moreland927e00d2017-01-04 12:58:40 -080066 LOG(INFO) << "Loaded boot control hidl hal.";
David Zeuthen753fadc2015-09-15 16:34:09 -040067
David Zeuthen753fadc2015-09-15 16:34:09 -040068 return true;
69}
Alex Deymob17327c2015-09-04 10:29:00 -070070
71unsigned int BootControlAndroid::GetNumSlots() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080072 return module_->getNumberSlots();
Alex Deymob17327c2015-09-04 10:29:00 -070073}
74
75BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
Connor O'Briencee6ad92016-11-21 13:53:52 -080076 return module_->getCurrentSlot();
Alex Deymob17327c2015-09-04 10:29:00 -070077}
78
79bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
David Zeuthen753fadc2015-09-15 16:34:09 -040080 Slot slot,
Alex Deymob17327c2015-09-04 10:29:00 -070081 string* device) const {
David Zeuthen753fadc2015-09-15 16:34:09 -040082 // We can't use fs_mgr to look up |partition_name| because fstab
83 // doesn't list every slot partition (it uses the slotselect option
84 // to mask the suffix).
85 //
86 // We can however assume that there's an entry for the /misc mount
87 // point and use that to get the device file for the misc
88 // partition. This helps us locate the disk that |partition_name|
89 // resides on. From there we'll assume that a by-name scheme is used
90 // so we can just replace the trailing "misc" by the given
91 // |partition_name| and suffix corresponding to |slot|, e.g.
92 //
93 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
94 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
95 //
96 // If needed, it's possible to relax the by-name assumption in the
97 // future by trawling /sys/block looking for the appropriate sibling
98 // of misc and then finding an entry in /dev matching the sysfs
99 // entry.
100
Sen Jiangd944faa2018-08-22 18:46:39 -0700101 string err, misc_device = get_bootloader_message_blk_device(&err);
102 if (misc_device.empty()) {
103 LOG(ERROR) << "Unable to get misc block device: " << err;
David Zeuthen753fadc2015-09-15 16:34:09 -0400104 return false;
Sen Jiangd944faa2018-08-22 18:46:39 -0700105 }
David Zeuthen753fadc2015-09-15 16:34:09 -0400106
Sen Jiangd944faa2018-08-22 18:46:39 -0700107 if (!utils::IsSymlink(misc_device.c_str())) {
108 LOG(ERROR) << "Device file " << misc_device << " for /misc "
Alex Deymo12542ac2016-02-03 15:48:10 -0800109 << "is not a symlink.";
David Zeuthen753fadc2015-09-15 16:34:09 -0400110 return false;
111 }
112
Connor O'Briencee6ad92016-11-21 13:53:52 -0800113 string suffix;
114 auto store_suffix_cb = [&suffix](hidl_string cb_suffix) {
115 suffix = cb_suffix.c_str();
116 };
117 Return<void> ret = module_->getSuffix(slot, store_suffix_cb);
118
Yifan Hong7b514b42016-12-21 13:02:00 -0800119 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700120 LOG(ERROR) << "boot_control impl returned no suffix for slot "
121 << SlotName(slot);
David Zeuthen753fadc2015-09-15 16:34:09 -0400122 return false;
123 }
124
Sen Jiangd944faa2018-08-22 18:46:39 -0700125 base::FilePath path =
126 base::FilePath(misc_device).DirName().Append(partition_name + suffix);
David Zeuthen753fadc2015-09-15 16:34:09 -0400127 if (!base::PathExists(path)) {
128 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
129 return false;
130 }
131
132 *device = path.value();
133 return true;
Alex Deymob17327c2015-09-04 10:29:00 -0700134}
135
136bool BootControlAndroid::IsSlotBootable(Slot slot) const {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800137 Return<BoolResult> ret = module_->isSlotBootable(slot);
Yifan Hong7b514b42016-12-21 13:02:00 -0800138 if (!ret.isOk()) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700139 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
Connor O'Briencee6ad92016-11-21 13:53:52 -0800140 << " is bootable: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800141 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400142 return false;
143 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800144 if (ret == BoolResult::INVALID_SLOT) {
145 LOG(ERROR) << "Invalid slot: " << SlotName(slot);
146 return false;
147 }
148 return ret == BoolResult::TRUE;
Alex Deymob17327c2015-09-04 10:29:00 -0700149}
150
151bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800152 CommandResult result;
153 auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800154 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800155 LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
156 << SlotName(slot) << ": "
Yifan Hong7b514b42016-12-21 13:02:00 -0800157 << ret.description();
David Zeuthen753fadc2015-09-15 16:34:09 -0400158 return false;
159 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800160 if (!result.success) {
161 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
162 << " as unbootable: " << result.errMsg.c_str();
163 }
164 return result.success;
Alex Deymob17327c2015-09-04 10:29:00 -0700165}
166
Alex Deymo31d95ac2015-09-17 11:56:18 -0700167bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800168 CommandResult result;
169 auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800170 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800171 LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
Yifan Hong7b514b42016-12-21 13:02:00 -0800172 << ": " << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800173 return false;
Alex Deymo29dcbf32016-10-06 13:33:20 -0700174 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800175 if (!result.success) {
176 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
177 << ": " << result.errMsg.c_str();
178 }
179 return result.success;
Alex Deymo31d95ac2015-09-17 11:56:18 -0700180}
181
Alex Deymoaa26f622015-09-16 18:21:27 -0700182bool BootControlAndroid::MarkBootSuccessfulAsync(
183 base::Callback<void(bool)> callback) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800184 CommandResult result;
185 auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
Yifan Hong7b514b42016-12-21 13:02:00 -0800186 if (!ret.isOk()) {
Connor O'Briencee6ad92016-11-21 13:53:52 -0800187 LOG(ERROR) << "Unable to call MarkBootSuccessful: "
Yifan Hong7b514b42016-12-21 13:02:00 -0800188 << ret.description();
Connor O'Briencee6ad92016-11-21 13:53:52 -0800189 return false;
190 }
191 if (!result.success) {
192 LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str();
Alex Deymoaa26f622015-09-16 18:21:27 -0700193 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700194 return brillo::MessageLoop::current()->PostTask(
Connor O'Briencee6ad92016-11-21 13:53:52 -0800195 FROM_HERE, base::Bind(callback, result.success)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700196 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700197}
198
Alex Deymob17327c2015-09-04 10:29:00 -0700199} // namespace chromeos_update_engine