blob: b3b7630687a1347f8242c95e351f19091c0c20ff [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
31namespace chromeos_update_engine {
32
33namespace boot_control {
34
35// Factory defined in boot_control.h.
36std::unique_ptr<BootControlInterface> CreateBootControl() {
David Zeuthen753fadc2015-09-15 16:34:09 -040037 std::unique_ptr<BootControlAndroid> boot_control(new BootControlAndroid());
38 if (!boot_control->Init()) {
39 return nullptr;
40 }
Alex Vakulenkoce8c8ee2016-04-08 08:59:26 -070041 return std::move(boot_control);
Alex Deymob17327c2015-09-04 10:29:00 -070042}
43
44} // namespace boot_control
45
David Zeuthen753fadc2015-09-15 16:34:09 -040046bool BootControlAndroid::Init() {
47 const hw_module_t* hw_module;
48 int ret;
49
50 ret = hw_get_module(BOOT_CONTROL_HARDWARE_MODULE_ID, &hw_module);
51 if (ret != 0) {
52 LOG(ERROR) << "Error loading boot_control HAL implementation.";
53 return false;
54 }
55
56 module_ = reinterpret_cast<boot_control_module_t*>(const_cast<hw_module_t*>(hw_module));
57 module_->init(module_);
58
59 LOG(INFO) << "Loaded boot_control HAL "
60 << "'" << hw_module->name << "' "
61 << "version " << (hw_module->module_api_version>>8) << "."
62 << (hw_module->module_api_version&0xff) << " "
63 << "authored by '" << hw_module->author << "'.";
64 return true;
65}
Alex Deymob17327c2015-09-04 10:29:00 -070066
67unsigned int BootControlAndroid::GetNumSlots() const {
David Zeuthen753fadc2015-09-15 16:34:09 -040068 return module_->getNumberSlots(module_);
Alex Deymob17327c2015-09-04 10:29:00 -070069}
70
71BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
David Zeuthen753fadc2015-09-15 16:34:09 -040072 return module_->getCurrentSlot(module_);
Alex Deymob17327c2015-09-04 10:29:00 -070073}
74
75bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
David Zeuthen753fadc2015-09-15 16:34:09 -040076 Slot slot,
Alex Deymob17327c2015-09-04 10:29:00 -070077 string* device) const {
David Zeuthen753fadc2015-09-15 16:34:09 -040078 // We can't use fs_mgr to look up |partition_name| because fstab
79 // doesn't list every slot partition (it uses the slotselect option
80 // to mask the suffix).
81 //
82 // We can however assume that there's an entry for the /misc mount
83 // point and use that to get the device file for the misc
84 // partition. This helps us locate the disk that |partition_name|
85 // resides on. From there we'll assume that a by-name scheme is used
86 // so we can just replace the trailing "misc" by the given
87 // |partition_name| and suffix corresponding to |slot|, e.g.
88 //
89 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
90 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
91 //
92 // If needed, it's possible to relax the by-name assumption in the
93 // future by trawling /sys/block looking for the appropriate sibling
94 // of misc and then finding an entry in /dev matching the sysfs
95 // entry.
96
Alex Deymofb905d92016-06-03 19:26:58 -070097 base::FilePath misc_device;
98 if (!utils::DeviceForMountPoint("/misc", &misc_device))
David Zeuthen753fadc2015-09-15 16:34:09 -040099 return false;
David Zeuthen753fadc2015-09-15 16:34:09 -0400100
Alex Deymo12542ac2016-02-03 15:48:10 -0800101 if (!utils::IsSymlink(misc_device.value().c_str())) {
David Zeuthen753fadc2015-09-15 16:34:09 -0400102 LOG(ERROR) << "Device file " << misc_device.value() << " for /misc "
Alex Deymo12542ac2016-02-03 15:48:10 -0800103 << "is not a symlink.";
David Zeuthen753fadc2015-09-15 16:34:09 -0400104 return false;
105 }
106
107 const char* suffix = module_->getSuffix(module_, slot);
108 if (suffix == nullptr) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700109 LOG(ERROR) << "boot_control impl returned no suffix for slot "
110 << SlotName(slot);
David Zeuthen753fadc2015-09-15 16:34:09 -0400111 return false;
112 }
113
114 base::FilePath path = misc_device.DirName().Append(partition_name + suffix);
115 if (!base::PathExists(path)) {
116 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
117 return false;
118 }
119
120 *device = path.value();
121 return true;
Alex Deymob17327c2015-09-04 10:29:00 -0700122}
123
124bool BootControlAndroid::IsSlotBootable(Slot slot) const {
David Zeuthen753fadc2015-09-15 16:34:09 -0400125 int ret = module_->isSlotBootable(module_, slot);
126 if (ret < 0) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700127 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
David Zeuthen753fadc2015-09-15 16:34:09 -0400128 << " is bootable: " << strerror(-ret);
129 return false;
130 }
131 return ret == 1;
Alex Deymob17327c2015-09-04 10:29:00 -0700132}
133
134bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
David Zeuthen753fadc2015-09-15 16:34:09 -0400135 int ret = module_->setSlotAsUnbootable(module_, slot);
136 if (ret < 0) {
Alex Deymo31d95ac2015-09-17 11:56:18 -0700137 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
David Zeuthen753fadc2015-09-15 16:34:09 -0400138 << " as bootable: " << strerror(-ret);
139 return false;
140 }
141 return ret == 0;
Alex Deymob17327c2015-09-04 10:29:00 -0700142}
143
Alex Deymo31d95ac2015-09-17 11:56:18 -0700144bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
145 int ret = module_->setActiveBootSlot(module_, slot);
146 if (ret < 0) {
147 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
148 << ": " << strerror(-ret);
149 }
150 return ret == 0;
151}
152
Alex Deymoaa26f622015-09-16 18:21:27 -0700153bool BootControlAndroid::MarkBootSuccessfulAsync(
154 base::Callback<void(bool)> callback) {
155 int ret = module_->markBootSuccessful(module_);
156 if (ret < 0) {
157 LOG(ERROR) << "Unable to mark boot successful: " << strerror(-ret);
158 }
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700159 return brillo::MessageLoop::current()->PostTask(
Alex Deymoaa26f622015-09-16 18:21:27 -0700160 FROM_HERE, base::Bind(callback, ret == 0)) !=
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700161 brillo::MessageLoop::kTaskIdNull;
Alex Deymoaa26f622015-09-16 18:21:27 -0700162}
163
Alex Deymob17327c2015-09-04 10:29:00 -0700164} // namespace chromeos_update_engine