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