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