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 | |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [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() { |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 59 | module_ = IBootControl::getService("bootctrl"); |
| 60 | if (module_ == nullptr) { |
| 61 | LOG(ERROR) << "Error getting bootctrl HIDL module.\n"; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 65 | LOG(INFO) << "Loaded boot HIDL HAL version " |
| 66 | << module_->getInterfaceVersion().get_major() << "." |
| 67 | << module_->getInterfaceVersion().get_minor(); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 68 | |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 69 | return true; |
| 70 | } |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 71 | |
| 72 | unsigned int BootControlAndroid::GetNumSlots() const { |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 73 | return module_->getNumberSlots(); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const { |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 77 | return module_->getCurrentSlot(); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | bool BootControlAndroid::GetPartitionDevice(const string& partition_name, |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 81 | Slot slot, |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 82 | string* device) const { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 83 | // We can't use fs_mgr to look up |partition_name| because fstab |
| 84 | // doesn't list every slot partition (it uses the slotselect option |
| 85 | // to mask the suffix). |
| 86 | // |
| 87 | // We can however assume that there's an entry for the /misc mount |
| 88 | // point and use that to get the device file for the misc |
| 89 | // partition. This helps us locate the disk that |partition_name| |
| 90 | // resides on. From there we'll assume that a by-name scheme is used |
| 91 | // so we can just replace the trailing "misc" by the given |
| 92 | // |partition_name| and suffix corresponding to |slot|, e.g. |
| 93 | // |
| 94 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc -> |
| 95 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a |
| 96 | // |
| 97 | // If needed, it's possible to relax the by-name assumption in the |
| 98 | // future by trawling /sys/block looking for the appropriate sibling |
| 99 | // of misc and then finding an entry in /dev matching the sysfs |
| 100 | // entry. |
| 101 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 102 | base::FilePath misc_device; |
| 103 | if (!utils::DeviceForMountPoint("/misc", &misc_device)) |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 104 | return false; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 105 | |
Alex Deymo | 12542ac | 2016-02-03 15:48:10 -0800 | [diff] [blame] | 106 | if (!utils::IsSymlink(misc_device.value().c_str())) { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 107 | LOG(ERROR) << "Device file " << misc_device.value() << " for /misc " |
Alex Deymo | 12542ac | 2016-02-03 15:48:10 -0800 | [diff] [blame] | 108 | << "is not a symlink."; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 112 | string suffix; |
| 113 | auto store_suffix_cb = [&suffix](hidl_string cb_suffix) { |
| 114 | suffix = cb_suffix.c_str(); |
| 115 | }; |
| 116 | Return<void> ret = module_->getSuffix(slot, store_suffix_cb); |
| 117 | |
| 118 | if (!ret.getStatus().isOk()) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 119 | LOG(ERROR) << "boot_control impl returned no suffix for slot " |
| 120 | << SlotName(slot); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
| 124 | base::FilePath path = misc_device.DirName().Append(partition_name + suffix); |
| 125 | if (!base::PathExists(path)) { |
| 126 | LOG(ERROR) << "Device file " << path.value() << " does not exist."; |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | *device = path.value(); |
| 131 | return true; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | bool BootControlAndroid::IsSlotBootable(Slot slot) const { |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 135 | Return<BoolResult> ret = module_->isSlotBootable(slot); |
| 136 | if (!ret.getStatus().isOk()) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 137 | LOG(ERROR) << "Unable to determine if slot " << SlotName(slot) |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 138 | << " is bootable: " |
| 139 | << ret.getStatus().exceptionMessage().string(); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 140 | return false; |
| 141 | } |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 142 | if (ret == BoolResult::INVALID_SLOT) { |
| 143 | LOG(ERROR) << "Invalid slot: " << SlotName(slot); |
| 144 | return false; |
| 145 | } |
| 146 | return ret == BoolResult::TRUE; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bool BootControlAndroid::MarkSlotUnbootable(Slot slot) { |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 150 | CommandResult result; |
| 151 | auto ret = module_->setSlotAsUnbootable(slot, StoreResultCallback(&result)); |
| 152 | if (!ret.getStatus().isOk()) { |
| 153 | LOG(ERROR) << "Unable to call MarkSlotUnbootable for slot " |
| 154 | << SlotName(slot) << ": " |
| 155 | << ret.getStatus().exceptionMessage().string(); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 156 | return false; |
| 157 | } |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 158 | if (!result.success) { |
| 159 | LOG(ERROR) << "Unable to mark slot " << SlotName(slot) |
| 160 | << " as unbootable: " << result.errMsg.c_str(); |
| 161 | } |
| 162 | return result.success; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 165 | bool BootControlAndroid::SetActiveBootSlot(Slot slot) { |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 166 | CommandResult result; |
| 167 | auto ret = module_->setActiveBootSlot(slot, StoreResultCallback(&result)); |
| 168 | if (!ret.getStatus().isOk()) { |
| 169 | LOG(ERROR) << "Unable to call SetActiveBootSlot for slot " << SlotName(slot) |
| 170 | << ": " << ret.getStatus().exceptionMessage().string(); |
| 171 | return false; |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 172 | } |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 173 | if (!result.success) { |
| 174 | LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot) |
| 175 | << ": " << result.errMsg.c_str(); |
| 176 | } |
| 177 | return result.success; |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 180 | bool BootControlAndroid::MarkBootSuccessfulAsync( |
| 181 | base::Callback<void(bool)> callback) { |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 182 | CommandResult result; |
| 183 | auto ret = module_->markBootSuccessful(StoreResultCallback(&result)); |
| 184 | if (!ret.getStatus().isOk()) { |
| 185 | LOG(ERROR) << "Unable to call MarkBootSuccessful: " |
| 186 | << ret.getStatus().exceptionMessage().string(); |
| 187 | return false; |
| 188 | } |
| 189 | if (!result.success) { |
| 190 | LOG(ERROR) << "Unable to mark boot successful: " << result.errMsg.c_str(); |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 191 | } |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 192 | return brillo::MessageLoop::current()->PostTask( |
Alex Deymo | 29dcbf3 | 2016-10-06 13:33:20 -0700 | [diff] [blame^] | 193 | FROM_HERE, base::Bind(callback, result.success)) != |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 194 | brillo::MessageLoop::kTaskIdNull; |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 197 | } // namespace chromeos_update_engine |