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