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