blob: 43a1c18f4ed48575c5d97b0e31ea1682f2d83a72 [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
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 Deymob17327c2015-09-04 10:29:00 -070023#include <chromeos/make_unique_ptr.h>
Alex Deymoaa26f622015-09-16 18:21:27 -070024#include <chromeos/message_loops/message_loop.h>
David Zeuthen753fadc2015-09-15 16:34:09 -040025#include <cutils/properties.h>
26#include <fs_mgr.h>
Alex Deymob17327c2015-09-04 10:29:00 -070027
David Zeuthen753fadc2015-09-15 16:34:09 -040028#include "update_engine/utils.h"
Alex Deymob17327c2015-09-04 10:29:00 -070029
30using std::string;
31
David Zeuthen753fadc2015-09-15 16:34:09 -040032namespace {
33
34// Open the appropriate fstab file and fallback to /fstab.device if
35// that's what's being used.
36static struct fstab* OpenFSTab() {
37 char propbuf[PROPERTY_VALUE_MAX];
38 struct fstab* fstab;
39
40 property_get("ro.hardware", propbuf, "");
41 string fstab_name = string("/fstab.") + propbuf;
42 fstab = fs_mgr_read_fstab(fstab_name.c_str());
43 if (fstab != nullptr)
44 return fstab;
45
46 fstab = fs_mgr_read_fstab("/fstab.device");
47 return fstab;
48}
49
50} // namespace
51
52
Alex Deymob17327c2015-09-04 10:29:00 -070053namespace chromeos_update_engine {
54
55namespace boot_control {
56
57// Factory defined in boot_control.h.
58std::unique_ptr<BootControlInterface> CreateBootControl() {
David Zeuthen753fadc2015-09-15 16:34:09 -040059 std::unique_ptr<BootControlAndroid> boot_control(new BootControlAndroid());
60 if (!boot_control->Init()) {
61 return nullptr;
62 }
63 return chromeos::make_unique_ptr(boot_control.release());
Alex Deymob17327c2015-09-04 10:29:00 -070064}
65
66} // namespace boot_control
67
David Zeuthen753fadc2015-09-15 16:34:09 -040068bool BootControlAndroid::Init() {
69 const hw_module_t* hw_module;
70 int ret;
71
72 ret = hw_get_module(BOOT_CONTROL_HARDWARE_MODULE_ID, &hw_module);
73 if (ret != 0) {
74 LOG(ERROR) << "Error loading boot_control HAL implementation.";
75 return false;
76 }
77
78 module_ = reinterpret_cast<boot_control_module_t*>(const_cast<hw_module_t*>(hw_module));
79 module_->init(module_);
80
81 LOG(INFO) << "Loaded boot_control HAL "
82 << "'" << hw_module->name << "' "
83 << "version " << (hw_module->module_api_version>>8) << "."
84 << (hw_module->module_api_version&0xff) << " "
85 << "authored by '" << hw_module->author << "'.";
86 return true;
87}
Alex Deymob17327c2015-09-04 10:29:00 -070088
89unsigned int BootControlAndroid::GetNumSlots() const {
David Zeuthen753fadc2015-09-15 16:34:09 -040090 return module_->getNumberSlots(module_);
Alex Deymob17327c2015-09-04 10:29:00 -070091}
92
93BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
David Zeuthen753fadc2015-09-15 16:34:09 -040094 return module_->getCurrentSlot(module_);
Alex Deymob17327c2015-09-04 10:29:00 -070095}
96
97bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
David Zeuthen753fadc2015-09-15 16:34:09 -040098 Slot slot,
Alex Deymob17327c2015-09-04 10:29:00 -070099 string* device) const {
David Zeuthen753fadc2015-09-15 16:34:09 -0400100 struct fstab* fstab;
101 struct fstab_rec* record;
102
103 // We can't use fs_mgr to look up |partition_name| because fstab
104 // doesn't list every slot partition (it uses the slotselect option
105 // to mask the suffix).
106 //
107 // We can however assume that there's an entry for the /misc mount
108 // point and use that to get the device file for the misc
109 // partition. This helps us locate the disk that |partition_name|
110 // resides on. From there we'll assume that a by-name scheme is used
111 // so we can just replace the trailing "misc" by the given
112 // |partition_name| and suffix corresponding to |slot|, e.g.
113 //
114 // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
115 // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
116 //
117 // If needed, it's possible to relax the by-name assumption in the
118 // future by trawling /sys/block looking for the appropriate sibling
119 // of misc and then finding an entry in /dev matching the sysfs
120 // entry.
121
122 fstab = OpenFSTab();
123 if (fstab == nullptr) {
124 LOG(ERROR) << "Error opening fstab file.";
125 return false;
126 }
127 record = fs_mgr_get_entry_for_mount_point(fstab, "/misc");
128 if (record == nullptr) {
129 LOG(ERROR) << "Error finding /misc entry in fstab file.";
130 fs_mgr_free_fstab(fstab);
131 return false;
132 }
133
134 base::FilePath misc_device = base::FilePath(record->blk_device);
135 fs_mgr_free_fstab(fstab);
136
137 if (misc_device.BaseName() != base::FilePath("misc")) {
138 LOG(ERROR) << "Device file " << misc_device.value() << " for /misc "
139 << "is not in the expected format.";
140 return false;
141 }
142
143 const char* suffix = module_->getSuffix(module_, slot);
144 if (suffix == nullptr) {
145 LOG(ERROR) << "boot_control impl returned no suffix for slot " << slot;
146 return false;
147 }
148
149 base::FilePath path = misc_device.DirName().Append(partition_name + suffix);
150 if (!base::PathExists(path)) {
151 LOG(ERROR) << "Device file " << path.value() << " does not exist.";
152 return false;
153 }
154
155 *device = path.value();
156 return true;
Alex Deymob17327c2015-09-04 10:29:00 -0700157}
158
159bool BootControlAndroid::IsSlotBootable(Slot slot) const {
David Zeuthen753fadc2015-09-15 16:34:09 -0400160 int ret = module_->isSlotBootable(module_, slot);
161 if (ret < 0) {
162 LOG(ERROR) << "Unable to determine if slot " << slot
163 << " is bootable: " << strerror(-ret);
164 return false;
165 }
166 return ret == 1;
Alex Deymob17327c2015-09-04 10:29:00 -0700167}
168
169bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
David Zeuthen753fadc2015-09-15 16:34:09 -0400170 int ret = module_->setSlotAsUnbootable(module_, slot);
171 if (ret < 0) {
172 LOG(ERROR) << "Unable to mark slot " << slot
173 << " as bootable: " << strerror(-ret);
174 return false;
175 }
176 return ret == 0;
Alex Deymob17327c2015-09-04 10:29:00 -0700177}
178
Alex Deymoaa26f622015-09-16 18:21:27 -0700179bool BootControlAndroid::MarkBootSuccessfulAsync(
180 base::Callback<void(bool)> callback) {
181 int ret = module_->markBootSuccessful(module_);
182 if (ret < 0) {
183 LOG(ERROR) << "Unable to mark boot successful: " << strerror(-ret);
184 }
185 return chromeos::MessageLoop::current()->PostTask(
186 FROM_HERE, base::Bind(callback, ret == 0)) !=
187 chromeos::MessageLoop::kTaskIdNull;
188}
189
Alex Deymob17327c2015-09-04 10:29:00 -0700190} // namespace chromeos_update_engine