blob: c6e3ffda3849e6fe56113bdc69c73225269b8be1 [file] [log] [blame]
Connor O'Briencee6ad92016-11-21 13:53:52 -08001//
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
17#include "update_engine/boot_control_recovery.h"
18
Sen Jiangd944faa2018-08-22 18:46:39 -070019#include <memory>
20#include <utility>
21
Connor O'Briencee6ad92016-11-21 13:53:52 -080022#include <base/bind.h>
23#include <base/files/file_util.h>
24#include <base/logging.h>
Sen Jiangd944faa2018-08-22 18:46:39 -070025#include <bootloader_message/bootloader_message.h>
Connor O'Briencee6ad92016-11-21 13:53:52 -080026#include <brillo/message_loops/message_loop.h>
27
28#include "update_engine/common/utils.h"
Connor O'Briencee6ad92016-11-21 13:53:52 -080029
30using std::string;
31
32#ifndef _UE_SIDELOAD
33#error "BootControlRecovery should only be used for update_engine_sideload."
34#endif
35
36// When called from update_engine_sideload, we don't attempt to dynamically load
37// the right boot_control HAL, instead we use the only HAL statically linked in
38// via the PRODUCT_STATIC_BOOT_CONTROL_HAL make variable and access the module
39// struct directly.
40extern const hw_module_t HAL_MODULE_INFO_SYM;
41
42namespace chromeos_update_engine {
43
44namespace boot_control {
45
46// Factory defined in boot_control.h.
47std::unique_ptr<BootControlInterface> CreateBootControl() {
48 std::unique_ptr<BootControlRecovery> boot_control(new BootControlRecovery());
49 if (!boot_control->Init()) {
50 return nullptr;
51 }
52 return std::move(boot_control);
53}
54
55} // namespace boot_control
56
57bool BootControlRecovery::Init() {
58 const hw_module_t* hw_module;
59 int ret;
60
61 // For update_engine_sideload, we simulate the hw_get_module() by accessing it
62 // from the current process directly.
63 hw_module = &HAL_MODULE_INFO_SYM;
64 ret = 0;
65 if (!hw_module ||
66 strcmp(BOOT_CONTROL_HARDWARE_MODULE_ID, hw_module->id) != 0) {
67 ret = -EINVAL;
68 }
69 if (ret != 0) {
70 LOG(ERROR) << "Error loading boot_control HAL implementation.";
71 return false;
72 }
73
74 module_ = reinterpret_cast<boot_control_module_t*>(
75 const_cast<hw_module_t*>(hw_module));
76 module_->init(module_);
77
78 LOG(INFO) << "Loaded boot_control HAL "
79 << "'" << hw_module->name << "' "
80 << "version " << (hw_module->module_api_version >> 8) << "."
81 << (hw_module->module_api_version & 0xff) << " "
82 << "authored by '" << hw_module->author << "'.";
83 return true;
84}
85
86unsigned int BootControlRecovery::GetNumSlots() const {
87 return module_->getNumberSlots(module_);
88}
89
90BootControlInterface::Slot BootControlRecovery::GetCurrentSlot() const {
91 return module_->getCurrentSlot(module_);
92}
93
94bool BootControlRecovery::GetPartitionDevice(const string& partition_name,
95 Slot slot,
96 string* device) const {
97 // We can't use fs_mgr to look up |partition_name| because fstab
98 // doesn't list every slot partition (it uses the slotselect option
99 // to mask the suffix).
100 //
101 // We can however assume that there's an entry for the /misc mount
102 // point and use that to get the device file for the misc
103 // partition. This helps us locate the disk that |partition_name|
104 // resides on. From there we'll assume that a by-name scheme is used
105 // so we can just replace the trailing "misc" by the given
106 // |partition_name| and suffix corresponding to |slot|, e.g.
107 //
108 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
109 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
110 //
111 // If needed, it's possible to relax the by-name assumption in the
112 // future by trawling /sys/block looking for the appropriate sibling
113 // of misc and then finding an entry in /dev matching the sysfs
114 // entry.
115
Sen Jiangd944faa2018-08-22 18:46:39 -0700116 string err, misc_device = get_bootloader_message_blk_device(&err);
117 if (misc_device.empty()) {
118 LOG(ERROR) << "Unable to get misc block device: " << err;
Connor O'Briencee6ad92016-11-21 13:53:52 -0800119 return false;
Sen Jiangd944faa2018-08-22 18:46:39 -0700120 }
Connor O'Briencee6ad92016-11-21 13:53:52 -0800121
Sen Jiangd944faa2018-08-22 18:46:39 -0700122 if (!utils::IsSymlink(misc_device.c_str())) {
123 LOG(ERROR) << "Device file " << misc_device << " for /misc "
Connor O'Briencee6ad92016-11-21 13:53:52 -0800124 << "is not a symlink.";
125 return false;
126 }
127
128 const char* suffix = module_->getSuffix(module_, slot);
129 if (suffix == nullptr) {
130 LOG(ERROR) << "boot_control impl returned no suffix for slot "
131 << SlotName(slot);
132 return false;
133 }
134
Sen Jiangd944faa2018-08-22 18:46:39 -0700135 base::FilePath path =
136 base::FilePath(misc_device).DirName().Append(partition_name + suffix);
Connor O'Briencee6ad92016-11-21 13:53:52 -0800137 if (!base::PathExists(path)) {
138 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
139 return false;
140 }
141
142 *device = path.value();
143 return true;
144}
145
146bool BootControlRecovery::IsSlotBootable(Slot slot) const {
147 int ret = module_->isSlotBootable(module_, slot);
148 if (ret < 0) {
149 LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
150 << " is bootable: " << strerror(-ret);
151 return false;
152 }
153 return ret == 1;
154}
155
156bool BootControlRecovery::MarkSlotUnbootable(Slot slot) {
157 int ret = module_->setSlotAsUnbootable(module_, slot);
158 if (ret < 0) {
159 LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
160 << " as bootable: " << strerror(-ret);
161 return false;
162 }
163 return ret == 0;
164}
165
166bool BootControlRecovery::SetActiveBootSlot(Slot slot) {
167 int ret = module_->setActiveBootSlot(module_, slot);
168 if (ret < 0) {
169 LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
170 << ": " << strerror(-ret);
171 }
172 return ret == 0;
173}
174
175bool BootControlRecovery::MarkBootSuccessfulAsync(
176 base::Callback<void(bool)> callback) {
177 int ret = module_->markBootSuccessful(module_);
178 if (ret < 0) {
179 LOG(ERROR) << "Unable to mark boot successful: " << strerror(-ret);
180 }
181 return brillo::MessageLoop::current()->PostTask(
182 FROM_HERE, base::Bind(callback, ret == 0)) !=
183 brillo::MessageLoop::kTaskIdNull;
184}
185
Yifan Hong537802d2018-08-15 13:15:42 -0700186// TODO(b/78598708): Remove BootControlRecovery, so don't bother resizing
187// partitions here.
188bool BootControlRecovery::InitPartitionMetadata(
189 Slot slot, const PartitionSizes& partition_sizes) {
190 LOG(ERROR) << __FUNCTION__
191 << " is not implemented. BootControlRecovery will be removed.";
192 return true;
193}
194
195void BootControlRecovery::Cleanup() {
196 LOG(ERROR) << __FUNCTION__
197 << " is not implemented. BootControlRecovery will be removed.";
198}
199
Connor O'Briencee6ad92016-11-21 13:53:52 -0800200} // namespace chromeos_update_engine