• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
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 
19 #include <base/bind.h>
20 #include <base/files/file_util.h>
21 #include <base/logging.h>
22 #include <base/strings/string_util.h>
23 #include <brillo/make_unique_ptr.h>
24 #include <brillo/message_loops/message_loop.h>
25 #include <cutils/properties.h>
26 #include <fs_mgr.h>
27 
28 #include "update_engine/common/utils.h"
29 
30 using std::string;
31 
32 namespace {
33 
34 // Open the appropriate fstab file and fallback to /fstab.device if
35 // that's what's being used.
OpenFSTab()36 static 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 
53 namespace chromeos_update_engine {
54 
55 namespace boot_control {
56 
57 // Factory defined in boot_control.h.
CreateBootControl()58 std::unique_ptr<BootControlInterface> CreateBootControl() {
59   std::unique_ptr<BootControlAndroid> boot_control(new BootControlAndroid());
60   if (!boot_control->Init()) {
61     return nullptr;
62   }
63   return brillo::make_unique_ptr(boot_control.release());
64 }
65 
66 }  // namespace boot_control
67 
Init()68 bool 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 }
88 
GetNumSlots() const89 unsigned int BootControlAndroid::GetNumSlots() const {
90   return module_->getNumberSlots(module_);
91 }
92 
GetCurrentSlot() const93 BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
94   return module_->getCurrentSlot(module_);
95 }
96 
GetPartitionDevice(const string & partition_name,Slot slot,string * device) const97 bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
98                                             Slot slot,
99                                             string* device) const {
100   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 (!utils::IsSymlink(misc_device.value().c_str())) {
138     LOG(ERROR) << "Device file " << misc_device.value() << " for /misc "
139                << "is not a symlink.";
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 "
146                << SlotName(slot);
147     return false;
148   }
149 
150   base::FilePath path = misc_device.DirName().Append(partition_name + suffix);
151   if (!base::PathExists(path)) {
152     LOG(ERROR) << "Device file " << path.value() << " does not exist.";
153     return false;
154   }
155 
156   *device = path.value();
157   return true;
158 }
159 
IsSlotBootable(Slot slot) const160 bool BootControlAndroid::IsSlotBootable(Slot slot) const {
161   int ret = module_->isSlotBootable(module_, slot);
162   if (ret < 0) {
163     LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
164                << " is bootable: " << strerror(-ret);
165     return false;
166   }
167   return ret == 1;
168 }
169 
MarkSlotUnbootable(Slot slot)170 bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
171   int ret = module_->setSlotAsUnbootable(module_, slot);
172   if (ret < 0) {
173     LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
174                << " as bootable: " << strerror(-ret);
175     return false;
176   }
177   return ret == 0;
178 }
179 
SetActiveBootSlot(Slot slot)180 bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
181   int ret = module_->setActiveBootSlot(module_, slot);
182   if (ret < 0) {
183     LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
184                << ": " << strerror(-ret);
185   }
186   return ret == 0;
187 }
188 
MarkBootSuccessfulAsync(base::Callback<void (bool)> callback)189 bool BootControlAndroid::MarkBootSuccessfulAsync(
190     base::Callback<void(bool)> callback) {
191   int ret = module_->markBootSuccessful(module_);
192   if (ret < 0) {
193     LOG(ERROR) << "Unable to mark boot successful: " << strerror(-ret);
194   }
195   return brillo::MessageLoop::current()->PostTask(
196              FROM_HERE, base::Bind(callback, ret == 0)) !=
197          brillo::MessageLoop::kTaskIdNull;
198 }
199 
200 }  // namespace chromeos_update_engine
201