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