• 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 
26 #include "update_engine/common/utils.h"
27 #include "update_engine/utils_android.h"
28 
29 using std::string;
30 
31 using android::hardware::Return;
32 using android::hardware::boot::V1_0::BoolResult;
33 using android::hardware::boot::V1_0::CommandResult;
34 using android::hardware::boot::V1_0::IBootControl;
35 using android::hardware::hidl_string;
36 
37 namespace {
StoreResultCallback(CommandResult * dest)38 auto StoreResultCallback(CommandResult* dest) {
39   return [dest](const CommandResult& result) { *dest = result; };
40 }
41 }  // namespace
42 
43 namespace chromeos_update_engine {
44 
45 namespace boot_control {
46 
47 // Factory defined in boot_control.h.
CreateBootControl()48 std::unique_ptr<BootControlInterface> CreateBootControl() {
49   std::unique_ptr<BootControlAndroid> boot_control(new BootControlAndroid());
50   if (!boot_control->Init()) {
51     return nullptr;
52   }
53   return std::move(boot_control);
54 }
55 
56 }  // namespace boot_control
57 
Init()58 bool BootControlAndroid::Init() {
59   module_ = IBootControl::getService();
60   if (module_ == nullptr) {
61     LOG(ERROR) << "Error getting bootctrl HIDL module.";
62     return false;
63   }
64 
65   LOG(INFO) << "Loaded boot control hidl hal.";
66 
67   return true;
68 }
69 
GetNumSlots() const70 unsigned int BootControlAndroid::GetNumSlots() const {
71   return module_->getNumberSlots();
72 }
73 
GetCurrentSlot() const74 BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const {
75   return module_->getCurrentSlot();
76 }
77 
GetPartitionDevice(const string & partition_name,Slot slot,string * device) const78 bool BootControlAndroid::GetPartitionDevice(const string& partition_name,
79                                             Slot slot,
80                                             string* device) const {
81   // We can't use fs_mgr to look up |partition_name| because fstab
82   // doesn't list every slot partition (it uses the slotselect option
83   // to mask the suffix).
84   //
85   // We can however assume that there's an entry for the /misc mount
86   // point and use that to get the device file for the misc
87   // partition. This helps us locate the disk that |partition_name|
88   // resides on. From there we'll assume that a by-name scheme is used
89   // so we can just replace the trailing "misc" by the given
90   // |partition_name| and suffix corresponding to |slot|, e.g.
91   //
92   //   /dev/block/platform/soc.0/7824900.sdhci/by-name/misc ->
93   //   /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a
94   //
95   // If needed, it's possible to relax the by-name assumption in the
96   // future by trawling /sys/block looking for the appropriate sibling
97   // of misc and then finding an entry in /dev matching the sysfs
98   // entry.
99 
100   base::FilePath misc_device;
101   if (!utils::DeviceForMountPoint("/misc", &misc_device))
102     return false;
103 
104   if (!utils::IsSymlink(misc_device.value().c_str())) {
105     LOG(ERROR) << "Device file " << misc_device.value() << " for /misc "
106                << "is not a symlink.";
107     return false;
108   }
109 
110   string suffix;
111   auto store_suffix_cb = [&suffix](hidl_string cb_suffix) {
112     suffix = cb_suffix.c_str();
113   };
114   Return<void> ret = module_->getSuffix(slot, store_suffix_cb);
115 
116   if (!ret.isOk()) {
117     LOG(ERROR) << "boot_control impl returned no suffix for slot "
118                << SlotName(slot);
119     return false;
120   }
121 
122   base::FilePath path = misc_device.DirName().Append(partition_name + suffix);
123   if (!base::PathExists(path)) {
124     LOG(ERROR) << "Device file " << path.value() << " does not exist.";
125     return false;
126   }
127 
128   *device = path.value();
129   return true;
130 }
131 
IsSlotBootable(Slot slot) const132 bool BootControlAndroid::IsSlotBootable(Slot slot) const {
133   Return<BoolResult> ret = module_->isSlotBootable(slot);
134   if (!ret.isOk()) {
135     LOG(ERROR) << "Unable to determine if slot " << SlotName(slot)
136                << " is bootable: "
137                << ret.description();
138     return false;
139   }
140   if (ret == BoolResult::INVALID_SLOT) {
141     LOG(ERROR) << "Invalid slot: " << SlotName(slot);
142     return false;
143   }
144   return ret == BoolResult::TRUE;
145 }
146 
MarkSlotUnbootable(Slot slot)147 bool BootControlAndroid::MarkSlotUnbootable(Slot slot) {
148   CommandResult result;
149   auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result));
150   if (!ret.isOk()) {
151     LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot "
152                << SlotName(slot) << ": "
153                << ret.description();
154     return false;
155   }
156   if (!result.success) {
157     LOG(ERROR) << "Unable to mark slot " << SlotName(slot)
158                << " as unbootable: " << result.errMsg.c_str();
159   }
160   return result.success;
161 }
162 
SetActiveBootSlot(Slot slot)163 bool BootControlAndroid::SetActiveBootSlot(Slot slot) {
164   CommandResult result;
165   auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result));
166   if (!ret.isOk()) {
167     LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot)
168                << ": " << ret.description();
169     return false;
170   }
171   if (!result.success) {
172     LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot)
173                << ": " << result.errMsg.c_str();
174   }
175   return result.success;
176 }
177 
MarkBootSuccessfulAsync(base::Callback<void (bool)> callback)178 bool BootControlAndroid::MarkBootSuccessfulAsync(
179     base::Callback<void(bool)> callback) {
180   CommandResult result;
181   auto ret = module_->markBootSuccessful(StoreResultCallback(&result));
182   if (!ret.isOk()) {
183     LOG(ERROR) << "Unable to call MarkBootSuccessful: "
184                << ret.description();
185     return false;
186   }
187   if (!result.success) {
188     LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str();
189   }
190   return brillo::MessageLoop::current()->PostTask(
191              FROM_HERE, base::Bind(callback, result.success)) !=
192          brillo::MessageLoop::kTaskIdNull;
193 }
194 
195 }  // namespace chromeos_update_engine
196