| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 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 |  | 
| Alex Deymo | 1b03f9f | 2015-12-09 00:38:36 -0800 | [diff] [blame] | 17 | #include "update_engine/boot_control_android.h" | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 18 |  | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 19 | #include <base/bind.h> | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 20 | #include <base/files/file_util.h> | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 21 | #include <base/logging.h> | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 22 | #include <base/strings/string_util.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 23 | #include <brillo/make_unique_ptr.h> | 
|  | 24 | #include <brillo/message_loops/message_loop.h> | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 25 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/utils.h" | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 27 | #include "update_engine/utils_android.h" | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 28 |  | 
|  | 29 | using std::string; | 
|  | 30 |  | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 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 { | 
|  | 38 | auto StoreResultCallback(CommandResult* dest) { | 
|  | 39 | return [dest](const CommandResult& result) { *dest = result; }; | 
|  | 40 | } | 
|  | 41 | }  // namespace | 
| Alex Deymo | 44348e0 | 2016-07-29 16:22:26 -0700 | [diff] [blame] | 42 |  | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 43 | namespace chromeos_update_engine { | 
|  | 44 |  | 
|  | 45 | namespace boot_control { | 
|  | 46 |  | 
|  | 47 | // Factory defined in boot_control.h. | 
|  | 48 | std::unique_ptr<BootControlInterface> CreateBootControl() { | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 49 | std::unique_ptr<BootControlAndroid> boot_control(new BootControlAndroid()); | 
|  | 50 | if (!boot_control->Init()) { | 
|  | 51 | return nullptr; | 
|  | 52 | } | 
| Alex Vakulenko | ce8c8ee | 2016-04-08 08:59:26 -0700 | [diff] [blame] | 53 | return std::move(boot_control); | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
|  | 56 | }  // namespace boot_control | 
|  | 57 |  | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 58 | bool BootControlAndroid::Init() { | 
| Chris Phoenix | afde8e8 | 2017-01-17 23:14:58 -0800 | [diff] [blame] | 59 | module_ = IBootControl::getService(); | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 60 | if (module_ == nullptr) { | 
| Steven Moreland | 927e00d | 2017-01-04 12:58:40 -0800 | [diff] [blame] | 61 | LOG(ERROR) << "Error getting bootctrl HIDL module."; | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 62 | return false; | 
|  | 63 | } | 
|  | 64 |  | 
| Steven Moreland | 927e00d | 2017-01-04 12:58:40 -0800 | [diff] [blame] | 65 | LOG(INFO) << "Loaded boot control hidl hal."; | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 66 |  | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 67 | return true; | 
|  | 68 | } | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 69 |  | 
|  | 70 | unsigned int BootControlAndroid::GetNumSlots() const { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 71 | return module_->getNumberSlots(); | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 72 | } | 
|  | 73 |  | 
|  | 74 | BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 75 | return module_->getCurrentSlot(); | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
|  | 78 | bool BootControlAndroid::GetPartitionDevice(const string& partition_name, | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 79 | Slot slot, | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 80 | string* device) const { | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 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 |  | 
| Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 100 | base::FilePath misc_device; | 
|  | 101 | if (!utils::DeviceForMountPoint("/misc", &misc_device)) | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 102 | return false; | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 103 |  | 
| Alex Deymo | 12542ac | 2016-02-03 15:48:10 -0800 | [diff] [blame] | 104 | if (!utils::IsSymlink(misc_device.value().c_str())) { | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 105 | LOG(ERROR) << "Device file " << misc_device.value() << " for /misc " | 
| Alex Deymo | 12542ac | 2016-02-03 15:48:10 -0800 | [diff] [blame] | 106 | << "is not a symlink."; | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 107 | return false; | 
|  | 108 | } | 
|  | 109 |  | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 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 |  | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 116 | if (!ret.isOk()) { | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 117 | LOG(ERROR) << "boot_control impl returned no suffix for slot " | 
|  | 118 | << SlotName(slot); | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 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; | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
|  | 132 | bool BootControlAndroid::IsSlotBootable(Slot slot) const { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 133 | Return<BoolResult> ret = module_->isSlotBootable(slot); | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 134 | if (!ret.isOk()) { | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 135 | LOG(ERROR) << "Unable to determine if slot " << SlotName(slot) | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 136 | << " is bootable: " | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 137 | << ret.description(); | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 138 | return false; | 
|  | 139 | } | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 140 | if (ret == BoolResult::INVALID_SLOT) { | 
|  | 141 | LOG(ERROR) << "Invalid slot: " << SlotName(slot); | 
|  | 142 | return false; | 
|  | 143 | } | 
|  | 144 | return ret == BoolResult::TRUE; | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
|  | 147 | bool BootControlAndroid::MarkSlotUnbootable(Slot slot) { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 148 | CommandResult result; | 
|  | 149 | auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result)); | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 150 | if (!ret.isOk()) { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 151 | LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot " | 
|  | 152 | << SlotName(slot) << ": " | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 153 | << ret.description(); | 
| David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 154 | return false; | 
|  | 155 | } | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 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; | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 161 | } | 
|  | 162 |  | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 163 | bool BootControlAndroid::SetActiveBootSlot(Slot slot) { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 164 | CommandResult result; | 
|  | 165 | auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result)); | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 166 | if (!ret.isOk()) { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 167 | LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot) | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 168 | << ": " << ret.description(); | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 169 | return false; | 
| Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame] | 170 | } | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 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; | 
| Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 178 | bool BootControlAndroid::MarkBootSuccessfulAsync( | 
|  | 179 | base::Callback<void(bool)> callback) { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 180 | CommandResult result; | 
|  | 181 | auto ret = module_->markBootSuccessful(StoreResultCallback(&result)); | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 182 | if (!ret.isOk()) { | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 183 | LOG(ERROR) << "Unable to call MarkBootSuccessful: " | 
| Yifan Hong | 7b514b4 | 2016-12-21 13:02:00 -0800 | [diff] [blame] | 184 | << ret.description(); | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 185 | return false; | 
|  | 186 | } | 
|  | 187 | if (!result.success) { | 
|  | 188 | LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str(); | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 189 | } | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 190 | return brillo::MessageLoop::current()->PostTask( | 
| Connor O'Brien | cee6ad9 | 2016-11-21 13:53:52 -0800 | [diff] [blame] | 191 | FROM_HERE, base::Bind(callback, result.success)) != | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 192 | brillo::MessageLoop::kTaskIdNull; | 
| Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
| Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 195 | }  // namespace chromeos_update_engine |