blob: 4bb3b92c8b9eb30c01c9671dd74ef6fdc6255e36 [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
17#include "update_engine/boot_control_android.h"
18
David Zeuthen753fadc2015-09-15 16:34:09 -040019#include <base/logging.h>
20#include <base/files/file_util.h>
21#include <base/strings/string_util.h>
Alex Deymob17327c2015-09-04 10:29:00 -070022#include <chromeos/make_unique_ptr.h>
David Zeuthen753fadc2015-09-15 16:34:09 -040023#include <cutils/properties.h>
24#include <fs_mgr.h>
Alex Deymob17327c2015-09-04 10:29:00 -070025
David Zeuthen753fadc2015-09-15 16:34:09 -040026#include "update_engine/utils.h"
Alex Deymob17327c2015-09-04 10:29:00 -070027
28using std::string;
29
David Zeuthen753fadc2015-09-15 16:34:09 -040030namespace {
31
32// Open the appropriate fstab file and fallback to /fstab.device if
33// that's what's being used.
34static struct fstab* OpenFSTab() {
35 char propbuf[PROPERTY_VALUE_MAX];
36 struct fstab* fstab;
37
38 property_get("ro.hardware", propbuf, "");
39 string fstab_name = string("/fstab.") + propbuf;
40 fstab = fs_mgr_read_fstab(fstab_name.c_str());
41 if (fstab != nullptr)
42 return fstab;
43
44 fstab = fs_mgr_read_fstab("/fstab.device");
45 return fstab;
46}
47
48} // namespace
49
50
Alex Deymob17327c2015-09-04 10:29:00 -070051namespace chromeos_update_engine {
52
53namespace boot_control {
54
55// Factory defined in boot_control.h.
56std::unique_ptr<BootControlInterface> CreateBootControl() {
David Zeuthen753fadc2015-09-15 16:34:09 -040057 std::unique_ptr<BootControlAndroid> boot_control(new BootControlAndroid());
58 if (!boot_control->Init()) {
59 return nullptr;
60 }
61 return chromeos::make_unique_ptr(boot_control.release());
Alex Deymob17327c2015-09-04 10:29:00 -070062}
63
64} // namespace boot_control
65
David Zeuthen753fadc2015-09-15 16:34:09 -040066bool BootControlAndroid::Init() {
67 const hw_module_t* hw_module;
68 int ret;
69
70 ret = hw_get_module(BOOT_CONTROL_HARDWARE_MODULE_ID, &hw_module);
71 if (ret != 0) {
72 LOG(ERROR) << "Error loading boot_control HAL implementation.";
73 return false;
74 }
75
76 module_ = reinterpret_cast<boot_control_module_t*>(const_cast<hw_module_t*>(hw_module));
77 module_->init(module_);
78
79 LOG(INFO) << "Loaded boot_control HAL "
80 << "'" << hw_module->name << "' "
81 << "version " << (hw_module->module_api_version>>8) << "."
82 << (hw_module->module_api_version&0xff) << " "
83 << "authored by '" << hw_module->author << "'.";
84 return true;
85}
Alex Deymob17327c2015-09-04 10:29:00 -070086
87unsigned int BootControlAndroid::GetNumSlots() const {
David Zeuthen753fadc2015-09-15 16:34:09 -040088 return module_->getNumberSlots(module_);
Alex Deymob17327c2015-09-04 10:29:00 -070089}
90
91BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
David Zeuthen753fadc2015-09-15 16:34:09 -040092 return module_->getCurrentSlot(module_);
Alex Deymob17327c2015-09-04 10:29:00 -070093}
94
95bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
David Zeuthen753fadc2015-09-15 16:34:09 -040096 Slot slot,
Alex Deymob17327c2015-09-04 10:29:00 -070097 string* device) const {
David Zeuthen753fadc2015-09-15 16:34:09 -040098 struct fstab* fstab;
99 struct fstab_rec* record;
100
101 // We can't use fs_mgr to look up |partition_name| because fstab
102 // doesn't list every slot partition (it uses the slotselect option
103 // to mask the suffix).
104 //
105 // We can however assume that there's an entry for the /misc mount
106 // point and use that to get the device file for the misc
107 // partition. This helps us locate the disk that |partition_name|
108 // resides on. From there we'll assume that a by-name scheme is used
109 // so we can just replace the trailing "misc" by the given
110 // |partition_name| and suffix corresponding to |slot|, e.g.
111 //
112 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
113 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
114 //
115 // If needed, it's possible to relax the by-name assumption in the
116 // future by trawling /sys/block looking for the appropriate sibling
117 // of misc and then finding an entry in /dev matching the sysfs
118 // entry.
119
120 fstab = OpenFSTab();
121 if (fstab == nullptr) {
122 LOG(ERROR) << "Error opening fstab file.";
123 return false;
124 }
125 record = fs_mgr_get_entry_for_mount_point(fstab, "/misc");
126 if (record == nullptr) {
127 LOG(ERROR) << "Error finding /misc entry in fstab file.";
128 fs_mgr_free_fstab(fstab);
129 return false;
130 }
131
132 base::FilePath misc_device = base::FilePath(record->blk_device);
133 fs_mgr_free_fstab(fstab);
134
135 if (misc_device.BaseName() != base::FilePath("misc")) {
136 LOG(ERROR) << "Device file " << misc_device.value() << " for /misc "
137 << "is not in the expected format.";
138 return false;
139 }
140
141 const char* suffix = module_->getSuffix(module_, slot);
142 if (suffix == nullptr) {
143 LOG(ERROR) << "boot_control impl returned no suffix for slot " << slot;
144 return false;
145 }
146
147 base::FilePath path = misc_device.DirName().Append(partition_name + suffix);
148 if (!base::PathExists(path)) {
149 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
150 return false;
151 }
152
153 *device = path.value();
154 return true;
Alex Deymob17327c2015-09-04 10:29:00 -0700155}
156
157bool BootControlAndroid::IsSlotBootable(Slot slot) const {
David Zeuthen753fadc2015-09-15 16:34:09 -0400158 int ret = module_->isSlotBootable(module_, slot);
159 if (ret < 0) {
160 LOG(ERROR) << "Unable to determine if slot " << slot
161 << " is bootable: " << strerror(-ret);
162 return false;
163 }
164 return ret == 1;
Alex Deymob17327c2015-09-04 10:29:00 -0700165}
166
167bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
David Zeuthen753fadc2015-09-15 16:34:09 -0400168 int ret = module_->setSlotAsUnbootable(module_, slot);
169 if (ret < 0) {
170 LOG(ERROR) << "Unable to mark slot " << slot
171 << " as bootable: " << strerror(-ret);
172 return false;
173 }
174 return ret == 0;
Alex Deymob17327c2015-09-04 10:29:00 -0700175}
176
177} // namespace chromeos_update_engine