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 | |
| 17 | #include "update_engine/boot_control_android.h" |
| 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 Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 23 | #include <chromeos/make_unique_ptr.h> |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 24 | #include <chromeos/message_loops/message_loop.h> |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 25 | #include <cutils/properties.h> |
| 26 | #include <fs_mgr.h> |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 27 | |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 28 | #include "update_engine/utils.h" |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 29 | |
| 30 | using std::string; |
| 31 | |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | // Open the appropriate fstab file and fallback to /fstab.device if |
| 35 | // that's what's being used. |
| 36 | static struct fstab* OpenFSTab() { |
| 37 | char propbuf[PROPERTY_VALUE_MAX]; |
| 38 | struct fstab* fstab; |
| 39 | |
| 40 | property_get("ro.hardware", propbuf, ""); |
| 41 | string fstab_name = string("/fstab.") + propbuf; |
| 42 | fstab = fs_mgr_read_fstab(fstab_name.c_str()); |
| 43 | if (fstab != nullptr) |
| 44 | return fstab; |
| 45 | |
| 46 | fstab = fs_mgr_read_fstab("/fstab.device"); |
| 47 | return fstab; |
| 48 | } |
| 49 | |
| 50 | } // namespace |
| 51 | |
| 52 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 53 | namespace chromeos_update_engine { |
| 54 | |
| 55 | namespace boot_control { |
| 56 | |
| 57 | // Factory defined in boot_control.h. |
| 58 | std::unique_ptr<BootControlInterface> CreateBootControl() { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 59 | std::unique_ptr<BootControlAndroid> boot_control(new BootControlAndroid()); |
| 60 | if (!boot_control->Init()) { |
| 61 | return nullptr; |
| 62 | } |
| 63 | return chromeos::make_unique_ptr(boot_control.release()); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | } // namespace boot_control |
| 67 | |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 68 | bool BootControlAndroid::Init() { |
| 69 | const hw_module_t* hw_module; |
| 70 | int ret; |
| 71 | |
| 72 | ret = hw_get_module(BOOT_CONTROL_HARDWARE_MODULE_ID, &hw_module); |
| 73 | if (ret != 0) { |
| 74 | LOG(ERROR) << "Error loading boot_control HAL implementation."; |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | module_ = reinterpret_cast<boot_control_module_t*>(const_cast<hw_module_t*>(hw_module)); |
| 79 | module_->init(module_); |
| 80 | |
| 81 | LOG(INFO) << "Loaded boot_control HAL " |
| 82 | << "'" << hw_module->name << "' " |
| 83 | << "version " << (hw_module->module_api_version>>8) << "." |
| 84 | << (hw_module->module_api_version&0xff) << " " |
| 85 | << "authored by '" << hw_module->author << "'."; |
| 86 | return true; |
| 87 | } |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 88 | |
| 89 | unsigned int BootControlAndroid::GetNumSlots() const { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 90 | return module_->getNumberSlots(module_); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | BootControlInterface::Slot BootControlAndroid::GetCurrentSlot() const { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 94 | return module_->getCurrentSlot(module_); |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | bool BootControlAndroid::GetPartitionDevice(const string& partition_name, |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 98 | Slot slot, |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 99 | string* device) const { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 100 | struct fstab* fstab; |
| 101 | struct fstab_rec* record; |
| 102 | |
| 103 | // We can't use fs_mgr to look up |partition_name| because fstab |
| 104 | // doesn't list every slot partition (it uses the slotselect option |
| 105 | // to mask the suffix). |
| 106 | // |
| 107 | // We can however assume that there's an entry for the /misc mount |
| 108 | // point and use that to get the device file for the misc |
| 109 | // partition. This helps us locate the disk that |partition_name| |
| 110 | // resides on. From there we'll assume that a by-name scheme is used |
| 111 | // so we can just replace the trailing "misc" by the given |
| 112 | // |partition_name| and suffix corresponding to |slot|, e.g. |
| 113 | // |
| 114 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/misc -> |
| 115 | // /dev/block/platform/soc.0/7824900.sdhci/by-name/boot_a |
| 116 | // |
| 117 | // If needed, it's possible to relax the by-name assumption in the |
| 118 | // future by trawling /sys/block looking for the appropriate sibling |
| 119 | // of misc and then finding an entry in /dev matching the sysfs |
| 120 | // entry. |
| 121 | |
| 122 | fstab = OpenFSTab(); |
| 123 | if (fstab == nullptr) { |
| 124 | LOG(ERROR) << "Error opening fstab file."; |
| 125 | return false; |
| 126 | } |
| 127 | record = fs_mgr_get_entry_for_mount_point(fstab, "/misc"); |
| 128 | if (record == nullptr) { |
| 129 | LOG(ERROR) << "Error finding /misc entry in fstab file."; |
| 130 | fs_mgr_free_fstab(fstab); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | base::FilePath misc_device = base::FilePath(record->blk_device); |
| 135 | fs_mgr_free_fstab(fstab); |
| 136 | |
| 137 | if (misc_device.BaseName() != base::FilePath("misc")) { |
| 138 | LOG(ERROR) << "Device file " << misc_device.value() << " for /misc " |
| 139 | << "is not in the expected format."; |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | const char* suffix = module_->getSuffix(module_, slot); |
| 144 | if (suffix == nullptr) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame^] | 145 | LOG(ERROR) << "boot_control impl returned no suffix for slot " |
| 146 | << SlotName(slot); |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | |
| 150 | base::FilePath path = misc_device.DirName().Append(partition_name + suffix); |
| 151 | if (!base::PathExists(path)) { |
| 152 | LOG(ERROR) << "Device file " << path.value() << " does not exist."; |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | *device = path.value(); |
| 157 | return true; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | bool BootControlAndroid::IsSlotBootable(Slot slot) const { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 161 | int ret = module_->isSlotBootable(module_, slot); |
| 162 | if (ret < 0) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame^] | 163 | LOG(ERROR) << "Unable to determine if slot " << SlotName(slot) |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 164 | << " is bootable: " << strerror(-ret); |
| 165 | return false; |
| 166 | } |
| 167 | return ret == 1; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | bool BootControlAndroid::MarkSlotUnbootable(Slot slot) { |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 171 | int ret = module_->setSlotAsUnbootable(module_, slot); |
| 172 | if (ret < 0) { |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame^] | 173 | LOG(ERROR) << "Unable to mark slot " << SlotName(slot) |
David Zeuthen | 753fadc | 2015-09-15 16:34:09 -0400 | [diff] [blame] | 174 | << " as bootable: " << strerror(-ret); |
| 175 | return false; |
| 176 | } |
| 177 | return ret == 0; |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Alex Deymo | 31d95ac | 2015-09-17 11:56:18 -0700 | [diff] [blame^] | 180 | bool BootControlAndroid::SetActiveBootSlot(Slot slot) { |
| 181 | int ret = module_->setActiveBootSlot(module_, slot); |
| 182 | if (ret < 0) { |
| 183 | LOG(ERROR) << "Unable to set the active slot to slot " << SlotName(slot) |
| 184 | << ": " << strerror(-ret); |
| 185 | } |
| 186 | return ret == 0; |
| 187 | } |
| 188 | |
Alex Deymo | aa26f62 | 2015-09-16 18:21:27 -0700 | [diff] [blame] | 189 | bool BootControlAndroid::MarkBootSuccessfulAsync( |
| 190 | base::Callback<void(bool)> callback) { |
| 191 | int ret = module_->markBootSuccessful(module_); |
| 192 | if (ret < 0) { |
| 193 | LOG(ERROR) << "Unable to mark boot successful: " << strerror(-ret); |
| 194 | } |
| 195 | return chromeos::MessageLoop::current()->PostTask( |
| 196 | FROM_HERE, base::Bind(callback, ret == 0)) != |
| 197 | chromeos::MessageLoop::kTaskIdNull; |
| 198 | } |
| 199 | |
Alex Deymo | b17327c | 2015-09-04 10:29:00 -0700 | [diff] [blame] | 200 | } // namespace chromeos_update_engine |