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 | 44348e0 | 2016-07-29 16:22:26 -0700 | [diff] [blame^] | 31 | #ifdef _UE_SIDELOAD |
| 32 | // When called from update_engine_sideload, we don't attempt to dynamically load |
| 33 | // the right boot_control HAL, instead we use the only HAL statically linked in |
| 34 | // via the PRODUCT_STATIC_BOOT_CONTROL_HAL make variable and access the module |
| 35 | // struct directly. |
| 36 | extern const hw_module_t HAL_MODULE_INFO_SYM; |
| 37 | #endif // _UE_SIDELOAD |
| 38 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 39 | namespace chromeos_update_engine { |
| 40 | |
| 41 | namespace boot_control { |
| 42 | |
| 43 | // Factory defined in boot_control.h. |
| 44 | std::unique_ptr<BootControlInterface> CreateBootControl() { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 45 | std::unique_ptr<BootControlAndroid> boot_control(new BootControlAndroid()); |
| 46 | if (!boot_control->Init()) { |
| 47 | return nullptr; |
| 48 | } |
Alex Vakulenko | ce8c8ee | 2016-04-08 08:59:26 -0700 | [diff] [blame] | 49 | return std::move(boot_control); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | } // namespace boot_control |
| 53 | |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 54 | bool BootControlAndroid::Init() { |
| 55 | const hw_module_t* hw_module; |
| 56 | int ret; |
| 57 | |
Alex Deymo | 44348e0 | 2016-07-29 16:22:26 -0700 | [diff] [blame^] | 58 | #ifdef _UE_SIDELOAD |
| 59 | // For update_engine_sideload, we simulate the hw_get_module() by accessing it |
| 60 | // from the current process directly. |
| 61 | hw_module = &HAL_MODULE_INFO_SYM; |
| 62 | ret = 0; |
| 63 | if (!hw_module || |
| 64 | strcmp(BOOT_CONTROL_HARDWARE_MODULE_ID, hw_module->id) != 0) { |
| 65 | ret = -EINVAL; |
| 66 | } |
| 67 | #else // !_UE_SIDELOAD |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 68 | ret = hw_get_module(BOOT_CONTROL_HARDWARE_MODULE_ID, &hw_module); |
Alex Deymo | 44348e0 | 2016-07-29 16:22:26 -0700 | [diff] [blame^] | 69 | #endif // _UE_SIDELOAD |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 70 | if (ret != 0) { |
| 71 | LOG(ERROR) << "Error loading boot_control HAL implementation."; |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | module_ = reinterpret_cast<boot_control_module_t*>(const_cast<hw_module_t*>(hw_module)); |
| 76 | module_->init(module_); |
| 77 | |
| 78 | LOG(INFO) << "Loaded boot_control HAL " |
| 79 | << "'" << hw_module->name << "' " |
| 80 | << "version " << (hw_module->module_api_version>>8) << "." |
| 81 | << (hw_module->module_api_version&0xff) << " " |
| 82 | << "authored by '" << hw_module->author << "'."; |
| 83 | return true; |
| 84 | } |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 85 | |
| 86 | unsigned int BootControlAndroid::GetNumSlots() const { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 87 | return module_->getNumberSlots(module_); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 91 | return module_->getCurrentSlot(module_); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | bool BootControlAndroid::GetPartitionDevice(const string& partition_name, |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 95 | Slot slot, |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 96 | string* device) const { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 97 | // We can't use fs_mgr to look up |partition_name| because fstab |
| 98 | // doesn't list every slot partition (it uses the slotselect option |
| 99 | // to mask the suffix). |
| 100 | // |
| 101 | // We can however assume that there's an entry for the /misc mount |
| 102 | // point and use that to get the device file for the misc |
| 103 | // partition. This helps us locate the disk that |partition_name| |
| 104 | // resides on. From there we'll assume that a by-name scheme is used |
| 105 | // so we can just replace the trailing "misc" by the given |
| 106 | // |partition_name| and suffix corresponding to |slot|, e.g. |
| 107 | // |
| 108 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc -> |
| 109 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a |
| 110 | // |
| 111 | // If needed, it's possible to relax the by-name assumption in the |
| 112 | // future by trawling /sys/block looking for the appropriate sibling |
| 113 | // of misc and then finding an entry in /dev matching the sysfs |
| 114 | // entry. |
| 115 | |
Alex Deymo | fb905d9 | 2016-06-03 19:26:58 -0700 | [diff] [blame] | 116 | base::FilePath misc_device; |
| 117 | if (!utils::DeviceForMountPoint("/misc", &misc_device)) |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 118 | return false; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 119 | |
Alex Deymo | 12542ac | 2016-02-03 15:48:10 -0800 | [diff] [blame] | 120 | if (!utils::IsSymlink(misc_device.value().c_str())) { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 121 | LOG(ERROR) << "Device file " << misc_device.value() << " for /misc " |
Alex Deymo | 12542ac | 2016-02-03 15:48:10 -0800 | [diff] [blame] | 122 | << "is not a symlink."; |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
| 126 | const char* suffix = module_->getSuffix(module_, slot); |
| 127 | if (suffix == nullptr) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 128 | LOG(ERROR) << "boot_control impl returned no suffix for slot " |
| 129 | << SlotName(slot); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
| 133 | base::FilePath path = misc_device.DirName().Append(partition_name + suffix); |
| 134 | if (!base::PathExists(path)) { |
| 135 | LOG(ERROR) << "Device file " << path.value() << " does not exist."; |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | *device = path.value(); |
| 140 | return true; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | bool BootControlAndroid::IsSlotBootable(Slot slot) const { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 144 | int ret = module_->isSlotBootable(module_, slot); |
| 145 | if (ret < 0) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 146 | LOG(ERROR) << "Unable to determine if slot " << SlotName(slot) |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 147 | << " is bootable: " << strerror(-ret); |
| 148 | return false; |
| 149 | } |
| 150 | return ret == 1; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | bool BootControlAndroid::MarkSlotUnbootable(Slot slot) { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 154 | int ret = module_->setSlotAsUnbootable(module_, slot); |
| 155 | if (ret < 0) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame] | 156 | LOG(ERROR) << "Unable to mark slot " << SlotName(slot) |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 157 | << " as bootable: " << strerror(-ret); |
| 158 | return false; |
| 159 | } |
| 160 | return ret == 0; |
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) { |
| 164 | int ret = module_->setActiveBootSlot(module_, slot); |
| 165 | if (ret < 0) { |
| 166 | LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot) |
| 167 | << ": " << strerror(-ret); |
| 168 | } |
| 169 | return ret == 0; |
| 170 | } |
| 171 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 172 | bool BootControlAndroid::MarkBootSuccessfulAsync( |
| 173 | base::Callback<void(bool)> callback) { |
| 174 | int ret = module_->markBootSuccessful(module_); |
| 175 | if (ret < 0) { |
| 176 | LOG(ERROR) << "Unable to mark boot successful: " << strerror(-ret); |
| 177 | } |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 178 | return brillo::MessageLoop::current()->PostTask( |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 179 | FROM_HERE, base::Bind(callback, ret == 0)) != |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 180 | brillo::MessageLoop::kTaskIdNull; |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 183 | } // namespace chromeos_update_engine |